From c2da007f4065e2520e0d948b146e0fb12fa75751 Mon Sep 17 00:00:00 2001 From: initial commit Date: Sat, 7 Sep 2019 14:03:22 +0400 Subject: [PATCH] initial commit --- .clang-format | 88 + .clang_complete | 12 + .gitattributes | 36 + .gitignore | 13 + .gitmodules | 12 + CMake/AddCXXCompilerFlag.cmake | 74 + CMake/FindJeMalloc.cmake | 21 + CMake/FindMHD.cmake | 38 + CMake/FindReadline.cmake | 25 + CMake/UseLATEX.cmake | 1936 ++++ CMakeLists.txt | 452 + GPLv2 | 27 + LGPLv2 | 18 + LICENSE.LGPL | 481 + adnl/CMakeLists.txt | 98 + adnl/adnl-address-list.cpp | 160 + adnl/adnl-address-list.h | 105 + adnl/adnl-address-list.hpp | 90 + adnl/adnl-channel.cpp | 129 + adnl/adnl-channel.h | 46 + adnl/adnl-channel.hpp | 82 + adnl/adnl-db.cpp | 82 + adnl/adnl-db.h | 47 + adnl/adnl-db.hpp | 47 + adnl/adnl-ext-client.cpp | 181 + adnl/adnl-ext-client.h | 48 + adnl/adnl-ext-client.hpp | 145 + adnl/adnl-ext-connection.cpp | 192 + adnl/adnl-ext-connection.hpp | 163 + adnl/adnl-ext-server.cpp | 182 + adnl/adnl-ext-server.h | 35 + adnl/adnl-ext-server.hpp | 92 + adnl/adnl-local-id.cpp | 279 + adnl/adnl-local-id.h | 117 + adnl/adnl-message.cpp | 54 + adnl/adnl-message.h | 296 + adnl/adnl-network-manager.cpp | 114 + adnl/adnl-network-manager.h | 107 + adnl/adnl-network-manager.hpp | 121 + adnl/adnl-node-id.hpp | 130 + adnl/adnl-node.cpp | 50 + adnl/adnl-node.h | 74 + adnl/adnl-packet.cpp | 135 + adnl/adnl-packet.h | 211 + adnl/adnl-peer-table.cpp | 336 + adnl/adnl-peer-table.h | 124 + adnl/adnl-peer-table.hpp | 137 + adnl/adnl-peer.cpp | 878 ++ adnl/adnl-peer.h | 101 + adnl/adnl-peer.hpp | 318 + adnl/adnl-proxy-types.cpp | 87 + adnl/adnl-proxy-types.h | 45 + adnl/adnl-proxy-types.hpp | 62 + adnl/adnl-proxy.cpp | 265 + adnl/adnl-query.cpp | 44 + adnl/adnl-query.h | 67 + adnl/adnl-static-nodes.cpp | 55 + adnl/adnl-static-nodes.h | 43 + adnl/adnl-static-nodes.hpp | 42 + adnl/adnl-test-loopback-implementation.cpp | 38 + adnl/adnl-test-loopback-implementation.h | 86 + adnl/adnl.h | 120 + adnl/test/adnl-test-ping.cpp | 240 + adnl/utils.cpp | 27 + adnl/utils.hpp | 45 + blockchain-explorer/CMakeLists.txt | 22 + .../blockchain-explorer-http.cpp | 695 ++ .../blockchain-explorer-http.hpp | 222 + .../blockchain-explorer-query.cpp | 1029 ++ .../blockchain-explorer-query.hpp | 274 + blockchain-explorer/blockchain-explorer.cpp | 582 ++ blockchain-explorer/blockchain-explorer.hpp | 56 + catchain/CMakeLists.txt | 39 + catchain/catchain-block.cpp | 59 + catchain/catchain-block.hpp | 113 + catchain/catchain-received-block.cpp | 523 + catchain/catchain-received-block.h | 114 + catchain/catchain-received-block.hpp | 179 + catchain/catchain-receiver-interface.h | 70 + catchain/catchain-receiver-source.cpp | 179 + catchain/catchain-receiver-source.h | 85 + catchain/catchain-receiver-source.hpp | 137 + catchain/catchain-receiver.cpp | 988 ++ catchain/catchain-receiver.h | 89 + catchain/catchain-receiver.hpp | 240 + catchain/catchain-types.h | 47 + catchain/catchain.cpp | 317 + catchain/catchain.h | 125 + catchain/catchain.hpp | 138 + common/CMakeLists.txt | 21 + common/checksum.h | 33 + common/delay.h | 52 + common/errorcode.h | 25 + common/errorlog.cpp | 74 + common/errorlog.h | 37 + common/io.hpp | 45 + common/status.h | 33 + crypto/CMakeLists.txt | 333 + crypto/Ed25519.cpp | 394 + crypto/Ed25519.h | 72 + crypto/block/Binlog.cpp | 493 + crypto/block/Binlog.h | 157 + crypto/block/block-binlog.h | 111 + crypto/block/block-db-impl.h | 135 + crypto/block/block-db.cpp | 835 ++ crypto/block/block-db.h | 164 + crypto/block/block-parse.cpp | 2135 ++++ crypto/block/block-parse.h | 1081 ++ crypto/block/block.cpp | 1923 ++++ crypto/block/block.h | 569 + crypto/block/block.tlb | 662 ++ crypto/block/check-proof.cpp | 294 + crypto/block/check-proof.h | 80 + crypto/block/create-state.cpp | 780 ++ crypto/block/mc-config.cpp | 1817 ++++ crypto/block/mc-config.h | 617 ++ crypto/block/output-queue-merger.cpp | 221 + crypto/block/output-queue-merger.h | 80 + crypto/block/test-block.cpp | 248 + crypto/block/transaction.cpp | 2089 ++++ crypto/block/transaction.h | 401 + crypto/common/AtomicRef.h | 200 + crypto/common/bigint.cpp | 41 + crypto/common/bigint.hpp | 2525 +++++ crypto/common/bitstring.cpp | 668 ++ crypto/common/bitstring.h | 662 ++ crypto/common/refcnt.cpp | 55 + crypto/common/refcnt.hpp | 474 + crypto/common/refint.cpp | 321 + crypto/common/refint.h | 127 + crypto/common/util.cpp | 212 + crypto/common/util.h | 42 + crypto/ellcurve/Ed25519.cpp | 280 + crypto/ellcurve/Ed25519.h | 188 + crypto/ellcurve/Fp25519.cpp | 33 + crypto/ellcurve/Fp25519.h | 32 + crypto/ellcurve/Montgomery.cpp | 138 + crypto/ellcurve/Montgomery.h | 123 + crypto/ellcurve/TwEdwards.cpp | 255 + crypto/ellcurve/TwEdwards.h | 145 + crypto/fift/Dictionary.cpp | 200 + crypto/fift/Dictionary.h | 182 + crypto/fift/Fift.cpp | 76 + crypto/fift/Fift.h | 53 + crypto/fift/IntCtx.cpp | 192 + crypto/fift/IntCtx.h | 147 + crypto/fift/SourceLookup.cpp | 89 + crypto/fift/SourceLookup.h | 71 + crypto/fift/fift-main.cpp | 221 + crypto/fift/lib/Asm.fif | 1101 ++ crypto/fift/lib/Fift.fif | 83 + crypto/fift/lib/Lisp.fif | 436 + crypto/fift/lib/Lists.fif | 184 + crypto/fift/lib/Stack.fif | 266 + crypto/fift/lib/TonUtil.fif | 125 + crypto/fift/utils.cpp | 175 + crypto/fift/utils.h | 35 + crypto/fift/words.cpp | 2747 +++++ crypto/fift/words.h | 43 + crypto/func/abscode.cpp | 492 + crypto/func/analyzer.cpp | 846 ++ crypto/func/asmops.cpp | 329 + crypto/func/builtins.cpp | 974 ++ crypto/func/codegen.cpp | 689 ++ crypto/func/func.cpp | 248 + crypto/func/func.h | 1445 +++ crypto/func/gen-abscode.cpp | 325 + crypto/func/keywords.cpp | 115 + crypto/func/optimize.cpp | 539 + crypto/func/parse-func.cpp | 1105 ++ crypto/func/stack-transform.cpp | 826 ++ crypto/func/test/a6.fc | 85 + crypto/func/test/a6.fp | 75 + crypto/func/test/a6_1.fc | 6 + crypto/func/test/a6_2.fc | 130 + crypto/func/test/a7.fc | 12 + crypto/func/test/a8.fc | 28 + crypto/func/test/a9.fc | 23 + crypto/func/test/a9_1.fc | 28 + crypto/func/test/b1.fc | 63 + crypto/func/test/b2.fc | 60 + crypto/func/test/b2_0.fc | 63 + crypto/func/test/b2_1.fc | 62 + crypto/func/test/b2_2.fc | 58 + crypto/func/unify-types.cpp | 384 + crypto/openssl/bignum.cpp | 256 + crypto/openssl/bignum.h | 368 + crypto/openssl/digest.h | 151 + crypto/openssl/rand.cpp | 122 + crypto/openssl/rand.hpp | 56 + crypto/openssl/residue.cpp | 176 + crypto/openssl/residue.h | 189 + crypto/parser/lexer.cpp | 288 + crypto/parser/lexer.h | 114 + crypto/parser/srcread.cpp | 180 + crypto/parser/srcread.h | 150 + crypto/parser/symtable.cpp | 181 + crypto/parser/symtable.h | 177 + crypto/smartcont/CreateState.fif | 167 + crypto/smartcont/config-code.fc | 149 + crypto/smartcont/elector-code.fc | 798 ++ crypto/smartcont/gen-zerostate-test.fif | 238 + crypto/smartcont/gen-zerostate.fif | 231 + crypto/smartcont/new-pinger.fif | 53 + crypto/smartcont/new-wallet.fif | 58 + crypto/smartcont/recover-stake.fif | 17 + crypto/smartcont/show-addr.fif | 21 + crypto/smartcont/simple-wallet-code.fc | 25 + crypto/smartcont/stdlib.fc | 101 + crypto/smartcont/testgiver.fif | 43 + crypto/smartcont/update-config-smc.fif | 43 + crypto/smartcont/update-config.fif | 48 + crypto/smartcont/update-elector-smc.fif | 43 + crypto/smartcont/validator-elect-req.fif | 30 + crypto/smartcont/validator-elect-signed.fif | 43 + crypto/smartcont/wallet-code.fc | 27 + crypto/smartcont/wallet.fif | 51 + crypto/test/Ed25519.cpp | 144 + crypto/test/fift.cpp | 92 + crypto/test/fift/bug.fif | 3 + crypto/test/fift/bug_div.fif | 12 + crypto/test/fift/cmdline.fif | 8 + crypto/test/fift/contfrac.fif | 18 + crypto/test/fift/fixed.fif | 19 + crypto/test/fift/sort.fif | 27 + crypto/test/fift/sort2.fif | 28 + crypto/test/fift/test.fif | 24 + crypto/test/fift/testdb.fif | 102 + crypto/test/fift/testdict.fif | 24 + crypto/test/fift/testvm.fif | 10 + crypto/test/fift/testvm2.fif | 22 + crypto/test/fift/testvm3.fif | 8 + crypto/test/fift/testvm4.fif | 14 + crypto/test/fift/testvm4a.fif | 5 + crypto/test/fift/testvm4b.fif | 16 + crypto/test/fift/testvm4c.fif | 52 + crypto/test/fift/testvm4d.fif | 26 + crypto/test/fift/testvm4e.fif | 61 + crypto/test/fift/testvm5.fif | 11 + crypto/test/fift/testvm6.fif | 66 + crypto/test/fift/testvm7.fif | 47 + crypto/test/fift/testvm8.fif | 5 + crypto/test/fift/testvm9.fif | 7 + crypto/test/fift/testvmprog.fif | 40 + crypto/test/test-cells.cpp | 571 + crypto/test/test-db.cpp | 2008 ++++ crypto/test/test-ed25519-crypto.cpp | 314 + crypto/test/vm.cpp | 427 + crypto/tl/boc.tlb | 45 + crypto/tl/hashmap.tlb | 60 + crypto/tl/tlbc-aux.h | 262 + crypto/tl/tlbc-data.h | 328 + crypto/tl/tlbc-gen-cpp.cpp | 3412 ++++++ crypto/tl/tlbc-gen-cpp.h | 290 + crypto/tl/tlbc.cpp | 3078 ++++++ crypto/tl/tlblib.cpp | 342 + crypto/tl/tlblib.hpp | 981 ++ crypto/vm/arithops.cpp | 946 ++ crypto/vm/arithops.h | 27 + crypto/vm/atom.cpp | 97 + crypto/vm/atom.h | 70 + crypto/vm/boc.cpp | 1133 ++ crypto/vm/boc.h | 314 + crypto/vm/box.hpp | 63 + crypto/vm/cellops.cpp | 1420 +++ crypto/vm/cellops.h | 31 + crypto/vm/cellparse.hpp | 105 + crypto/vm/cells.h | 73 + crypto/vm/cells/Cell.cpp | 59 + crypto/vm/cells/Cell.h | 89 + crypto/vm/cells/CellBuilder.cpp | 593 ++ crypto/vm/cells/CellBuilder.h | 238 + crypto/vm/cells/CellHash.cpp | 28 + crypto/vm/cells/CellHash.h | 92 + crypto/vm/cells/CellSlice.cpp | 1066 ++ crypto/vm/cells/CellSlice.h | 328 + crypto/vm/cells/CellTraits.cpp | 45 + crypto/vm/cells/CellTraits.h | 52 + crypto/vm/cells/CellUsageTree.cpp | 136 + crypto/vm/cells/CellUsageTree.h | 75 + crypto/vm/cells/CellWithStorage.h | 92 + crypto/vm/cells/DataCell.cpp | 368 + crypto/vm/cells/DataCell.h | 212 + crypto/vm/cells/ExtCell.h | 140 + crypto/vm/cells/LevelMask.cpp | 32 + crypto/vm/cells/LevelMask.h | 84 + crypto/vm/cells/MerkleProof.cpp | 263 + crypto/vm/cells/MerkleProof.h | 46 + crypto/vm/cells/MerkleUpdate.cpp | 510 + crypto/vm/cells/MerkleUpdate.h | 50 + crypto/vm/cells/PrunnedCell.h | 139 + crypto/vm/cells/UsageCell.h | 88 + crypto/vm/cells/VirtualCell.h | 87 + crypto/vm/cells/VirtualizationParameters.h | 72 + crypto/vm/cellslice.h | 19 + crypto/vm/continuation.cpp | 877 ++ crypto/vm/continuation.h | 585 ++ crypto/vm/contops.cpp | 1043 ++ crypto/vm/contops.h | 28 + crypto/vm/cp0.cpp | 50 + crypto/vm/cp0.h | 28 + crypto/vm/db/BlobView.cpp | 181 + crypto/vm/db/BlobView.h | 45 + crypto/vm/db/CellHashTable.h | 71 + crypto/vm/db/CellStorage.cpp | 163 + crypto/vm/db/CellStorage.h | 65 + crypto/vm/db/DynamicBagOfCellsDb.cpp | 500 + crypto/vm/db/DynamicBagOfCellsDb.h | 62 + crypto/vm/db/StaticBagOfCellsDb.cpp | 545 + crypto/vm/db/StaticBagOfCellsDb.h | 60 + crypto/vm/db/TonDb.cpp | 325 + crypto/vm/db/TonDb.h | 179 + crypto/vm/debugops.cpp | 110 + crypto/vm/debugops.h | 29 + crypto/vm/dict.cpp | 2647 +++++ crypto/vm/dict.h | 474 + crypto/vm/dictops.cpp | 811 ++ crypto/vm/dictops.h | 27 + crypto/vm/dispatch.cpp | 71 + crypto/vm/dispatch.h | 62 + crypto/vm/excno.hpp | 98 + crypto/vm/fmt.hpp | 503 + crypto/vm/log.h | 53 + crypto/vm/opctable.cpp | 450 + crypto/vm/opctable.h | 191 + crypto/vm/stack.cpp | 632 ++ crypto/vm/stack.hpp | 496 + crypto/vm/stackops.cpp | 576 + crypto/vm/stackops.h | 27 + crypto/vm/tonops.cpp | 675 ++ crypto/vm/tonops.h | 27 + crypto/vm/tupleops.cpp | 381 + crypto/vm/tupleops.h | 27 + crypto/vm/vmstate.h | 39 + dht-server/CMakeLists.txt | 14 + dht-server/dht-server.cpp | 1238 +++ dht-server/dht-server.hpp | 217 + dht/CMakeLists.txt | 32 + dht/dht-bucket.cpp | 208 + dht/dht-bucket.hpp | 64 + dht/dht-in.hpp | 168 + dht/dht-node.cpp | 64 + dht/dht-node.hpp | 114 + dht/dht-query.cpp | 273 + dht/dht-query.hpp | 177 + dht/dht-remote-node.cpp | 134 + dht/dht-remote-node.hpp | 86 + dht/dht-types.cpp | 383 + dht/dht-types.h | 268 + dht/dht.cpp | 552 + dht/dht.h | 60 + dht/dht.hpp | 122 + dht/test/dht-test-ping.cpp | 303 + doc/FullNode-HOWTO | 172 + doc/LiteClient-HOWTO | 485 + doc/Validator-HOWTO | 234 + doc/fiftbase.tex | 2173 ++++ doc/tblkch.tex | 2317 +++++ doc/ton.tex | 6133 +++++++++++ doc/tvm.tex | 3138 ++++++ example/android/AddIntDef.php | 47 + example/android/CMakeLists.txt | 62 + example/android/build-all.sh | 5 + example/android/build.sh | 44 + example/android/export.sh | 29 + example/android/native-lib.cpp | 199 + .../android/src/drinkless/org/ton/Client.java | 290 + example/android/test.sh | 6 + example/android/test/.gitignore | 13 + example/android/test/build.gradle | 26 + example/android/test/gradle.properties | 15 + .../test/gradle/wrapper/gradle-wrapper.jar | Bin 0 -> 54329 bytes .../gradle/wrapper/gradle-wrapper.properties | 6 + example/android/test/gradlew | 172 + example/android/test/gradlew.bat | 84 + example/android/test/settings.gradle | 1 + example/android/test/ton/.gitignore | 1 + example/android/test/ton/build.gradle | 44 + example/android/test/ton/proguard-rules.pro | 21 + .../java/drinkless/org/ton/TonTest.kt | 81 + .../java/drinkless/org/ton/TonTestJava.java | 161 + .../test/ton/src/main/AndroidManifest.xml | 4 + .../main/java/drinkless/org/ton/Client.java | 290 + .../test/ton/src/main/res/values/strings.xml | 3 + .../drinkless/org/ton/ExampleUnitTest.java | 17 + .../internal/__DECC_INCLUDE_EPILOGUE.H | 16 + .../internal/__DECC_INCLUDE_PROLOGUE.H | 20 + .../crypto/arm/include/internal/bio.h | 33 + .../crypto/arm/include/internal/comp.h | 12 + .../crypto/arm/include/internal/conf.h | 23 + .../arm/include/internal/constant_time_locl.h | 327 + .../crypto/arm/include/internal/cryptlib.h | 96 + .../crypto/arm/include/internal/dane.h | 103 + .../crypto/arm/include/internal/dso.h | 165 + .../crypto/arm/include/internal/dsoerr.h | 83 + .../crypto/arm/include/internal/err.h | 15 + .../crypto/arm/include/internal/nelem.h | 14 + .../crypto/arm/include/internal/numbers.h | 68 + .../crypto/arm/include/internal/o_dir.h | 52 + .../crypto/arm/include/internal/o_str.h | 17 + .../crypto/arm/include/internal/refcount.h | 140 + .../crypto/arm/include/internal/sockets.h | 159 + .../crypto/arm/include/internal/sslconf.h | 20 + .../crypto/arm/include/internal/thread_once.h | 45 + .../crypto/arm/include/internal/tsan_assist.h | 144 + .../include/openssl/__DECC_INCLUDE_EPILOGUE.H | 16 + .../include/openssl/__DECC_INCLUDE_PROLOGUE.H | 20 + .../crypto/arm/include/openssl/aes.h | 92 + .../crypto/arm/include/openssl/asn1.h | 886 ++ .../crypto/arm/include/openssl/asn1_mac.h | 10 + .../crypto/arm/include/openssl/asn1err.h | 252 + .../crypto/arm/include/openssl/asn1t.h | 945 ++ .../crypto/arm/include/openssl/async.h | 76 + .../crypto/arm/include/openssl/asyncerr.h | 38 + .../crypto/arm/include/openssl/bio.h | 804 ++ .../crypto/arm/include/openssl/bioerr.h | 120 + .../crypto/arm/include/openssl/blowfish.h | 61 + .../crypto/arm/include/openssl/bn.h | 539 + .../crypto/arm/include/openssl/bnerr.h | 96 + .../crypto/arm/include/openssl/buffer.h | 58 + .../crypto/arm/include/openssl/buffererr.h | 30 + .../crypto/arm/include/openssl/camellia.h | 83 + .../crypto/arm/include/openssl/cast.h | 53 + .../crypto/arm/include/openssl/cmac.h | 41 + .../crypto/arm/include/openssl/cms.h | 342 + .../crypto/arm/include/openssl/cmserr.h | 196 + .../crypto/arm/include/openssl/comp.h | 53 + .../crypto/arm/include/openssl/comperr.h | 40 + .../crypto/arm/include/openssl/conf.h | 168 + .../crypto/arm/include/openssl/conf_api.h | 40 + .../crypto/arm/include/openssl/conferr.h | 72 + .../crypto/arm/include/openssl/crypto.h | 445 + .../crypto/arm/include/openssl/cryptoerr.h | 56 + .../crypto/arm/include/openssl/ct.h | 476 + .../crypto/arm/include/openssl/cterr.h | 76 + .../crypto/arm/include/openssl/des.h | 174 + .../crypto/arm/include/openssl/dh.h | 340 + .../crypto/arm/include/openssl/dherr.h | 84 + .../crypto/arm/include/openssl/dsa.h | 238 + .../crypto/arm/include/openssl/dsaerr.h | 67 + .../crypto/arm/include/openssl/dtls1.h | 55 + .../crypto/arm/include/openssl/e_os2.h | 293 + .../crypto/arm/include/openssl/ebcdic.h | 33 + .../crypto/arm/include/openssl/ec.h | 1478 +++ .../crypto/arm/include/openssl/ecdh.h | 10 + .../crypto/arm/include/openssl/ecdsa.h | 10 + .../crypto/arm/include/openssl/ecerr.h | 267 + .../crypto/arm/include/openssl/engine.h | 751 ++ .../crypto/arm/include/openssl/engineerr.h | 107 + .../crypto/arm/include/openssl/err.h | 273 + .../crypto/arm/include/openssl/evp.h | 1638 +++ .../crypto/arm/include/openssl/evperr.h | 193 + .../crypto/arm/include/openssl/hmac.h | 51 + .../crypto/arm/include/openssl/idea.h | 64 + .../crypto/arm/include/openssl/kdf.h | 97 + .../crypto/arm/include/openssl/kdferr.h | 51 + .../crypto/arm/include/openssl/lhash.h | 217 + .../crypto/arm/include/openssl/md2.h | 44 + .../crypto/arm/include/openssl/md4.h | 51 + .../crypto/arm/include/openssl/md5.h | 50 + .../crypto/arm/include/openssl/mdc2.h | 42 + .../crypto/arm/include/openssl/modes.h | 208 + .../crypto/arm/include/openssl/obj_mac.h | 5198 +++++++++ .../crypto/arm/include/openssl/objects.h | 175 + .../crypto/arm/include/openssl/objectserr.h | 38 + .../crypto/arm/include/openssl/ocsp.h | 352 + .../crypto/arm/include/openssl/ocsperr.h | 74 + .../crypto/arm/include/openssl/opensslconf.h | 195 + .../arm/include/openssl/opensslconf.h.in | 155 + .../crypto/arm/include/openssl/opensslv.h | 101 + .../crypto/arm/include/openssl/ossl_typ.h | 196 + .../crypto/arm/include/openssl/pem.h | 378 + .../crypto/arm/include/openssl/pem2.h | 13 + .../crypto/arm/include/openssl/pemerr.h | 99 + .../crypto/arm/include/openssl/pkcs12.h | 223 + .../crypto/arm/include/openssl/pkcs12err.h | 77 + .../crypto/arm/include/openssl/pkcs7.h | 319 + .../crypto/arm/include/openssl/pkcs7err.h | 99 + .../crypto/arm/include/openssl/rand.h | 77 + .../crypto/arm/include/openssl/rand_drbg.h | 130 + .../crypto/arm/include/openssl/randerr.h | 89 + .../crypto/arm/include/openssl/rc2.h | 51 + .../crypto/arm/include/openssl/rc4.h | 36 + .../crypto/arm/include/openssl/rc5.h | 63 + .../crypto/arm/include/openssl/ripemd.h | 47 + .../crypto/arm/include/openssl/rsa.h | 512 + .../crypto/arm/include/openssl/rsaerr.h | 162 + .../crypto/arm/include/openssl/safestack.h | 172 + .../crypto/arm/include/openssl/seed.h | 96 + .../crypto/arm/include/openssl/sha.h | 119 + .../crypto/arm/include/openssl/srp.h | 135 + .../crypto/arm/include/openssl/srtp.h | 50 + .../crypto/arm/include/openssl/ssl.h | 2425 +++++ .../crypto/arm/include/openssl/ssl2.h | 24 + .../crypto/arm/include/openssl/ssl3.h | 339 + .../crypto/arm/include/openssl/sslerr.h | 767 ++ .../crypto/arm/include/openssl/stack.h | 83 + .../crypto/arm/include/openssl/store.h | 266 + .../crypto/arm/include/openssl/storeerr.h | 87 + .../crypto/arm/include/openssl/symhacks.h | 37 + .../crypto/arm/include/openssl/tls1.h | 1237 +++ .../crypto/arm/include/openssl/ts.h | 559 + .../crypto/arm/include/openssl/tserr.h | 128 + .../crypto/arm/include/openssl/txt_db.h | 57 + .../crypto/arm/include/openssl/ui.h | 368 + .../crypto/arm/include/openssl/uierr.h | 61 + .../crypto/arm/include/openssl/whrlpool.h | 48 + .../crypto/arm/include/openssl/x509.h | 1047 ++ .../crypto/arm/include/openssl/x509_vfy.h | 624 ++ .../crypto/arm/include/openssl/x509err.h | 125 + .../crypto/arm/include/openssl/x509v3.h | 935 ++ .../crypto/arm/include/openssl/x509v3err.h | 158 + .../third_party/crypto/arm/lib/libcrypto.a | Bin 0 -> 5341188 bytes .../third_party/crypto/arm/lib/libssl.a | Bin 0 -> 1007752 bytes .../internal/__DECC_INCLUDE_EPILOGUE.H | 16 + .../internal/__DECC_INCLUDE_PROLOGUE.H | 20 + .../crypto/arm64/include/internal/bio.h | 33 + .../crypto/arm64/include/internal/comp.h | 12 + .../crypto/arm64/include/internal/conf.h | 23 + .../include/internal/constant_time_locl.h | 327 + .../crypto/arm64/include/internal/cryptlib.h | 96 + .../crypto/arm64/include/internal/dane.h | 103 + .../crypto/arm64/include/internal/dso.h | 165 + .../crypto/arm64/include/internal/dsoerr.h | 83 + .../crypto/arm64/include/internal/err.h | 15 + .../crypto/arm64/include/internal/nelem.h | 14 + .../crypto/arm64/include/internal/numbers.h | 68 + .../crypto/arm64/include/internal/o_dir.h | 52 + .../crypto/arm64/include/internal/o_str.h | 17 + .../crypto/arm64/include/internal/refcount.h | 140 + .../crypto/arm64/include/internal/sockets.h | 159 + .../crypto/arm64/include/internal/sslconf.h | 20 + .../arm64/include/internal/thread_once.h | 45 + .../arm64/include/internal/tsan_assist.h | 144 + .../include/openssl/__DECC_INCLUDE_EPILOGUE.H | 16 + .../include/openssl/__DECC_INCLUDE_PROLOGUE.H | 20 + .../crypto/arm64/include/openssl/aes.h | 92 + .../crypto/arm64/include/openssl/asn1.h | 886 ++ .../crypto/arm64/include/openssl/asn1_mac.h | 10 + .../crypto/arm64/include/openssl/asn1err.h | 252 + .../crypto/arm64/include/openssl/asn1t.h | 945 ++ .../crypto/arm64/include/openssl/async.h | 76 + .../crypto/arm64/include/openssl/asyncerr.h | 38 + .../crypto/arm64/include/openssl/bio.h | 804 ++ .../crypto/arm64/include/openssl/bioerr.h | 120 + .../crypto/arm64/include/openssl/blowfish.h | 61 + .../crypto/arm64/include/openssl/bn.h | 539 + .../crypto/arm64/include/openssl/bnerr.h | 96 + .../crypto/arm64/include/openssl/buffer.h | 58 + .../crypto/arm64/include/openssl/buffererr.h | 30 + .../crypto/arm64/include/openssl/camellia.h | 83 + .../crypto/arm64/include/openssl/cast.h | 53 + .../crypto/arm64/include/openssl/cmac.h | 41 + .../crypto/arm64/include/openssl/cms.h | 342 + .../crypto/arm64/include/openssl/cmserr.h | 196 + .../crypto/arm64/include/openssl/comp.h | 53 + .../crypto/arm64/include/openssl/comperr.h | 40 + .../crypto/arm64/include/openssl/conf.h | 168 + .../crypto/arm64/include/openssl/conf_api.h | 40 + .../crypto/arm64/include/openssl/conferr.h | 72 + .../crypto/arm64/include/openssl/crypto.h | 445 + .../crypto/arm64/include/openssl/cryptoerr.h | 56 + .../crypto/arm64/include/openssl/ct.h | 476 + .../crypto/arm64/include/openssl/cterr.h | 76 + .../crypto/arm64/include/openssl/des.h | 174 + .../crypto/arm64/include/openssl/dh.h | 340 + .../crypto/arm64/include/openssl/dherr.h | 84 + .../crypto/arm64/include/openssl/dsa.h | 238 + .../crypto/arm64/include/openssl/dsaerr.h | 67 + .../crypto/arm64/include/openssl/dtls1.h | 55 + .../crypto/arm64/include/openssl/e_os2.h | 293 + .../crypto/arm64/include/openssl/ebcdic.h | 33 + .../crypto/arm64/include/openssl/ec.h | 1478 +++ .../crypto/arm64/include/openssl/ecdh.h | 10 + .../crypto/arm64/include/openssl/ecdsa.h | 10 + .../crypto/arm64/include/openssl/ecerr.h | 267 + .../crypto/arm64/include/openssl/engine.h | 751 ++ .../crypto/arm64/include/openssl/engineerr.h | 107 + .../crypto/arm64/include/openssl/err.h | 273 + .../crypto/arm64/include/openssl/evp.h | 1638 +++ .../crypto/arm64/include/openssl/evperr.h | 193 + .../crypto/arm64/include/openssl/hmac.h | 51 + .../crypto/arm64/include/openssl/idea.h | 64 + .../crypto/arm64/include/openssl/kdf.h | 97 + .../crypto/arm64/include/openssl/kdferr.h | 51 + .../crypto/arm64/include/openssl/lhash.h | 217 + .../crypto/arm64/include/openssl/md2.h | 44 + .../crypto/arm64/include/openssl/md4.h | 51 + .../crypto/arm64/include/openssl/md5.h | 50 + .../crypto/arm64/include/openssl/mdc2.h | 42 + .../crypto/arm64/include/openssl/modes.h | 208 + .../crypto/arm64/include/openssl/obj_mac.h | 5198 +++++++++ .../crypto/arm64/include/openssl/objects.h | 175 + .../crypto/arm64/include/openssl/objectserr.h | 38 + .../crypto/arm64/include/openssl/ocsp.h | 352 + .../crypto/arm64/include/openssl/ocsperr.h | 74 + .../arm64/include/openssl/opensslconf.h | 195 + .../arm64/include/openssl/opensslconf.h.in | 155 + .../crypto/arm64/include/openssl/opensslv.h | 101 + .../crypto/arm64/include/openssl/ossl_typ.h | 196 + .../crypto/arm64/include/openssl/pem.h | 378 + .../crypto/arm64/include/openssl/pem2.h | 13 + .../crypto/arm64/include/openssl/pemerr.h | 99 + .../crypto/arm64/include/openssl/pkcs12.h | 223 + .../crypto/arm64/include/openssl/pkcs12err.h | 77 + .../crypto/arm64/include/openssl/pkcs7.h | 319 + .../crypto/arm64/include/openssl/pkcs7err.h | 99 + .../crypto/arm64/include/openssl/rand.h | 77 + .../crypto/arm64/include/openssl/rand_drbg.h | 130 + .../crypto/arm64/include/openssl/randerr.h | 89 + .../crypto/arm64/include/openssl/rc2.h | 51 + .../crypto/arm64/include/openssl/rc4.h | 36 + .../crypto/arm64/include/openssl/rc5.h | 63 + .../crypto/arm64/include/openssl/ripemd.h | 47 + .../crypto/arm64/include/openssl/rsa.h | 512 + .../crypto/arm64/include/openssl/rsaerr.h | 162 + .../crypto/arm64/include/openssl/safestack.h | 172 + .../crypto/arm64/include/openssl/seed.h | 96 + .../crypto/arm64/include/openssl/sha.h | 119 + .../crypto/arm64/include/openssl/srp.h | 135 + .../crypto/arm64/include/openssl/srtp.h | 50 + .../crypto/arm64/include/openssl/ssl.h | 2425 +++++ .../crypto/arm64/include/openssl/ssl2.h | 24 + .../crypto/arm64/include/openssl/ssl3.h | 339 + .../crypto/arm64/include/openssl/sslerr.h | 767 ++ .../crypto/arm64/include/openssl/stack.h | 83 + .../crypto/arm64/include/openssl/store.h | 266 + .../crypto/arm64/include/openssl/storeerr.h | 87 + .../crypto/arm64/include/openssl/symhacks.h | 37 + .../crypto/arm64/include/openssl/tls1.h | 1237 +++ .../crypto/arm64/include/openssl/ts.h | 559 + .../crypto/arm64/include/openssl/tserr.h | 128 + .../crypto/arm64/include/openssl/txt_db.h | 57 + .../crypto/arm64/include/openssl/ui.h | 368 + .../crypto/arm64/include/openssl/uierr.h | 61 + .../crypto/arm64/include/openssl/whrlpool.h | 48 + .../crypto/arm64/include/openssl/x509.h | 1047 ++ .../crypto/arm64/include/openssl/x509_vfy.h | 624 ++ .../crypto/arm64/include/openssl/x509err.h | 125 + .../crypto/arm64/include/openssl/x509v3.h | 935 ++ .../crypto/arm64/include/openssl/x509v3err.h | 158 + .../third_party/crypto/arm64/lib/libcrypto.a | Bin 0 -> 5881028 bytes .../third_party/crypto/arm64/lib/libssl.a | Bin 0 -> 1168856 bytes .../android/third_party/crypto/build-all.sh | 5 + example/android/third_party/crypto/build.sh | 48 + .../internal/__DECC_INCLUDE_EPILOGUE.H | 16 + .../internal/__DECC_INCLUDE_PROLOGUE.H | 20 + .../crypto/x86/include/internal/bio.h | 33 + .../crypto/x86/include/internal/comp.h | 12 + .../crypto/x86/include/internal/conf.h | 23 + .../x86/include/internal/constant_time_locl.h | 327 + .../crypto/x86/include/internal/cryptlib.h | 96 + .../crypto/x86/include/internal/dane.h | 103 + .../crypto/x86/include/internal/dso.h | 165 + .../crypto/x86/include/internal/dsoerr.h | 83 + .../crypto/x86/include/internal/err.h | 15 + .../crypto/x86/include/internal/nelem.h | 14 + .../crypto/x86/include/internal/numbers.h | 68 + .../crypto/x86/include/internal/o_dir.h | 52 + .../crypto/x86/include/internal/o_str.h | 17 + .../crypto/x86/include/internal/refcount.h | 140 + .../crypto/x86/include/internal/sockets.h | 159 + .../crypto/x86/include/internal/sslconf.h | 20 + .../crypto/x86/include/internal/thread_once.h | 45 + .../crypto/x86/include/internal/tsan_assist.h | 144 + .../include/openssl/__DECC_INCLUDE_EPILOGUE.H | 16 + .../include/openssl/__DECC_INCLUDE_PROLOGUE.H | 20 + .../crypto/x86/include/openssl/aes.h | 92 + .../crypto/x86/include/openssl/asn1.h | 886 ++ .../crypto/x86/include/openssl/asn1_mac.h | 10 + .../crypto/x86/include/openssl/asn1err.h | 252 + .../crypto/x86/include/openssl/asn1t.h | 945 ++ .../crypto/x86/include/openssl/async.h | 76 + .../crypto/x86/include/openssl/asyncerr.h | 38 + .../crypto/x86/include/openssl/bio.h | 804 ++ .../crypto/x86/include/openssl/bioerr.h | 120 + .../crypto/x86/include/openssl/blowfish.h | 61 + .../crypto/x86/include/openssl/bn.h | 539 + .../crypto/x86/include/openssl/bnerr.h | 96 + .../crypto/x86/include/openssl/buffer.h | 58 + .../crypto/x86/include/openssl/buffererr.h | 30 + .../crypto/x86/include/openssl/camellia.h | 83 + .../crypto/x86/include/openssl/cast.h | 53 + .../crypto/x86/include/openssl/cmac.h | 41 + .../crypto/x86/include/openssl/cms.h | 342 + .../crypto/x86/include/openssl/cmserr.h | 196 + .../crypto/x86/include/openssl/comp.h | 53 + .../crypto/x86/include/openssl/comperr.h | 40 + .../crypto/x86/include/openssl/conf.h | 168 + .../crypto/x86/include/openssl/conf_api.h | 40 + .../crypto/x86/include/openssl/conferr.h | 72 + .../crypto/x86/include/openssl/crypto.h | 445 + .../crypto/x86/include/openssl/cryptoerr.h | 56 + .../crypto/x86/include/openssl/ct.h | 476 + .../crypto/x86/include/openssl/cterr.h | 76 + .../crypto/x86/include/openssl/des.h | 174 + .../crypto/x86/include/openssl/dh.h | 340 + .../crypto/x86/include/openssl/dherr.h | 84 + .../crypto/x86/include/openssl/dsa.h | 238 + .../crypto/x86/include/openssl/dsaerr.h | 67 + .../crypto/x86/include/openssl/dtls1.h | 55 + .../crypto/x86/include/openssl/e_os2.h | 293 + .../crypto/x86/include/openssl/ebcdic.h | 33 + .../crypto/x86/include/openssl/ec.h | 1478 +++ .../crypto/x86/include/openssl/ecdh.h | 10 + .../crypto/x86/include/openssl/ecdsa.h | 10 + .../crypto/x86/include/openssl/ecerr.h | 267 + .../crypto/x86/include/openssl/engine.h | 751 ++ .../crypto/x86/include/openssl/engineerr.h | 107 + .../crypto/x86/include/openssl/err.h | 273 + .../crypto/x86/include/openssl/evp.h | 1638 +++ .../crypto/x86/include/openssl/evperr.h | 193 + .../crypto/x86/include/openssl/hmac.h | 51 + .../crypto/x86/include/openssl/idea.h | 64 + .../crypto/x86/include/openssl/kdf.h | 97 + .../crypto/x86/include/openssl/kdferr.h | 51 + .../crypto/x86/include/openssl/lhash.h | 217 + .../crypto/x86/include/openssl/md2.h | 44 + .../crypto/x86/include/openssl/md4.h | 51 + .../crypto/x86/include/openssl/md5.h | 50 + .../crypto/x86/include/openssl/mdc2.h | 42 + .../crypto/x86/include/openssl/modes.h | 208 + .../crypto/x86/include/openssl/obj_mac.h | 5198 +++++++++ .../crypto/x86/include/openssl/objects.h | 175 + .../crypto/x86/include/openssl/objectserr.h | 38 + .../crypto/x86/include/openssl/ocsp.h | 352 + .../crypto/x86/include/openssl/ocsperr.h | 74 + .../crypto/x86/include/openssl/opensslconf.h | 195 + .../x86/include/openssl/opensslconf.h.in | 155 + .../crypto/x86/include/openssl/opensslv.h | 101 + .../crypto/x86/include/openssl/ossl_typ.h | 196 + .../crypto/x86/include/openssl/pem.h | 378 + .../crypto/x86/include/openssl/pem2.h | 13 + .../crypto/x86/include/openssl/pemerr.h | 99 + .../crypto/x86/include/openssl/pkcs12.h | 223 + .../crypto/x86/include/openssl/pkcs12err.h | 77 + .../crypto/x86/include/openssl/pkcs7.h | 319 + .../crypto/x86/include/openssl/pkcs7err.h | 99 + .../crypto/x86/include/openssl/rand.h | 77 + .../crypto/x86/include/openssl/rand_drbg.h | 130 + .../crypto/x86/include/openssl/randerr.h | 89 + .../crypto/x86/include/openssl/rc2.h | 51 + .../crypto/x86/include/openssl/rc4.h | 36 + .../crypto/x86/include/openssl/rc5.h | 63 + .../crypto/x86/include/openssl/ripemd.h | 47 + .../crypto/x86/include/openssl/rsa.h | 512 + .../crypto/x86/include/openssl/rsaerr.h | 162 + .../crypto/x86/include/openssl/safestack.h | 172 + .../crypto/x86/include/openssl/seed.h | 96 + .../crypto/x86/include/openssl/sha.h | 119 + .../crypto/x86/include/openssl/srp.h | 135 + .../crypto/x86/include/openssl/srtp.h | 50 + .../crypto/x86/include/openssl/ssl.h | 2425 +++++ .../crypto/x86/include/openssl/ssl2.h | 24 + .../crypto/x86/include/openssl/ssl3.h | 339 + .../crypto/x86/include/openssl/sslerr.h | 767 ++ .../crypto/x86/include/openssl/stack.h | 83 + .../crypto/x86/include/openssl/store.h | 266 + .../crypto/x86/include/openssl/storeerr.h | 87 + .../crypto/x86/include/openssl/symhacks.h | 37 + .../crypto/x86/include/openssl/tls1.h | 1237 +++ .../crypto/x86/include/openssl/ts.h | 559 + .../crypto/x86/include/openssl/tserr.h | 128 + .../crypto/x86/include/openssl/txt_db.h | 57 + .../crypto/x86/include/openssl/ui.h | 368 + .../crypto/x86/include/openssl/uierr.h | 61 + .../crypto/x86/include/openssl/whrlpool.h | 48 + .../crypto/x86/include/openssl/x509.h | 1047 ++ .../crypto/x86/include/openssl/x509_vfy.h | 624 ++ .../crypto/x86/include/openssl/x509err.h | 125 + .../crypto/x86/include/openssl/x509v3.h | 935 ++ .../crypto/x86/include/openssl/x509v3err.h | 158 + .../third_party/crypto/x86/lib/libcrypto.a | Bin 0 -> 4437020 bytes .../third_party/crypto/x86/lib/libssl.a | Bin 0 -> 844872 bytes .../internal/__DECC_INCLUDE_EPILOGUE.H | 16 + .../internal/__DECC_INCLUDE_PROLOGUE.H | 20 + .../crypto/x86_64/include/internal/bio.h | 33 + .../crypto/x86_64/include/internal/comp.h | 12 + .../crypto/x86_64/include/internal/conf.h | 23 + .../include/internal/constant_time_locl.h | 327 + .../crypto/x86_64/include/internal/cryptlib.h | 96 + .../crypto/x86_64/include/internal/dane.h | 103 + .../crypto/x86_64/include/internal/dso.h | 165 + .../crypto/x86_64/include/internal/dsoerr.h | 83 + .../crypto/x86_64/include/internal/err.h | 15 + .../crypto/x86_64/include/internal/nelem.h | 14 + .../crypto/x86_64/include/internal/numbers.h | 68 + .../crypto/x86_64/include/internal/o_dir.h | 52 + .../crypto/x86_64/include/internal/o_str.h | 17 + .../crypto/x86_64/include/internal/refcount.h | 140 + .../crypto/x86_64/include/internal/sockets.h | 159 + .../crypto/x86_64/include/internal/sslconf.h | 20 + .../x86_64/include/internal/thread_once.h | 45 + .../x86_64/include/internal/tsan_assist.h | 144 + .../include/openssl/__DECC_INCLUDE_EPILOGUE.H | 16 + .../include/openssl/__DECC_INCLUDE_PROLOGUE.H | 20 + .../crypto/x86_64/include/openssl/aes.h | 92 + .../crypto/x86_64/include/openssl/asn1.h | 886 ++ .../crypto/x86_64/include/openssl/asn1_mac.h | 10 + .../crypto/x86_64/include/openssl/asn1err.h | 252 + .../crypto/x86_64/include/openssl/asn1t.h | 945 ++ .../crypto/x86_64/include/openssl/async.h | 76 + .../crypto/x86_64/include/openssl/asyncerr.h | 38 + .../crypto/x86_64/include/openssl/bio.h | 804 ++ .../crypto/x86_64/include/openssl/bioerr.h | 120 + .../crypto/x86_64/include/openssl/blowfish.h | 61 + .../crypto/x86_64/include/openssl/bn.h | 539 + .../crypto/x86_64/include/openssl/bnerr.h | 96 + .../crypto/x86_64/include/openssl/buffer.h | 58 + .../crypto/x86_64/include/openssl/buffererr.h | 30 + .../crypto/x86_64/include/openssl/camellia.h | 83 + .../crypto/x86_64/include/openssl/cast.h | 53 + .../crypto/x86_64/include/openssl/cmac.h | 41 + .../crypto/x86_64/include/openssl/cms.h | 342 + .../crypto/x86_64/include/openssl/cmserr.h | 196 + .../crypto/x86_64/include/openssl/comp.h | 53 + .../crypto/x86_64/include/openssl/comperr.h | 40 + .../crypto/x86_64/include/openssl/conf.h | 168 + .../crypto/x86_64/include/openssl/conf_api.h | 40 + .../crypto/x86_64/include/openssl/conferr.h | 72 + .../crypto/x86_64/include/openssl/crypto.h | 445 + .../crypto/x86_64/include/openssl/cryptoerr.h | 56 + .../crypto/x86_64/include/openssl/ct.h | 476 + .../crypto/x86_64/include/openssl/cterr.h | 76 + .../crypto/x86_64/include/openssl/des.h | 174 + .../crypto/x86_64/include/openssl/dh.h | 340 + .../crypto/x86_64/include/openssl/dherr.h | 84 + .../crypto/x86_64/include/openssl/dsa.h | 238 + .../crypto/x86_64/include/openssl/dsaerr.h | 67 + .../crypto/x86_64/include/openssl/dtls1.h | 55 + .../crypto/x86_64/include/openssl/e_os2.h | 293 + .../crypto/x86_64/include/openssl/ebcdic.h | 33 + .../crypto/x86_64/include/openssl/ec.h | 1478 +++ .../crypto/x86_64/include/openssl/ecdh.h | 10 + .../crypto/x86_64/include/openssl/ecdsa.h | 10 + .../crypto/x86_64/include/openssl/ecerr.h | 267 + .../crypto/x86_64/include/openssl/engine.h | 751 ++ .../crypto/x86_64/include/openssl/engineerr.h | 107 + .../crypto/x86_64/include/openssl/err.h | 273 + .../crypto/x86_64/include/openssl/evp.h | 1638 +++ .../crypto/x86_64/include/openssl/evperr.h | 193 + .../crypto/x86_64/include/openssl/hmac.h | 51 + .../crypto/x86_64/include/openssl/idea.h | 64 + .../crypto/x86_64/include/openssl/kdf.h | 97 + .../crypto/x86_64/include/openssl/kdferr.h | 51 + .../crypto/x86_64/include/openssl/lhash.h | 217 + .../crypto/x86_64/include/openssl/md2.h | 44 + .../crypto/x86_64/include/openssl/md4.h | 51 + .../crypto/x86_64/include/openssl/md5.h | 50 + .../crypto/x86_64/include/openssl/mdc2.h | 42 + .../crypto/x86_64/include/openssl/modes.h | 208 + .../crypto/x86_64/include/openssl/obj_mac.h | 5198 +++++++++ .../crypto/x86_64/include/openssl/objects.h | 175 + .../x86_64/include/openssl/objectserr.h | 38 + .../crypto/x86_64/include/openssl/ocsp.h | 352 + .../crypto/x86_64/include/openssl/ocsperr.h | 74 + .../x86_64/include/openssl/opensslconf.h | 195 + .../x86_64/include/openssl/opensslconf.h.in | 155 + .../crypto/x86_64/include/openssl/opensslv.h | 101 + .../crypto/x86_64/include/openssl/ossl_typ.h | 196 + .../crypto/x86_64/include/openssl/pem.h | 378 + .../crypto/x86_64/include/openssl/pem2.h | 13 + .../crypto/x86_64/include/openssl/pemerr.h | 99 + .../crypto/x86_64/include/openssl/pkcs12.h | 223 + .../crypto/x86_64/include/openssl/pkcs12err.h | 77 + .../crypto/x86_64/include/openssl/pkcs7.h | 319 + .../crypto/x86_64/include/openssl/pkcs7err.h | 99 + .../crypto/x86_64/include/openssl/rand.h | 77 + .../crypto/x86_64/include/openssl/rand_drbg.h | 130 + .../crypto/x86_64/include/openssl/randerr.h | 89 + .../crypto/x86_64/include/openssl/rc2.h | 51 + .../crypto/x86_64/include/openssl/rc4.h | 36 + .../crypto/x86_64/include/openssl/rc5.h | 63 + .../crypto/x86_64/include/openssl/ripemd.h | 47 + .../crypto/x86_64/include/openssl/rsa.h | 512 + .../crypto/x86_64/include/openssl/rsaerr.h | 162 + .../crypto/x86_64/include/openssl/safestack.h | 172 + .../crypto/x86_64/include/openssl/seed.h | 96 + .../crypto/x86_64/include/openssl/sha.h | 119 + .../crypto/x86_64/include/openssl/srp.h | 135 + .../crypto/x86_64/include/openssl/srtp.h | 50 + .../crypto/x86_64/include/openssl/ssl.h | 2425 +++++ .../crypto/x86_64/include/openssl/ssl2.h | 24 + .../crypto/x86_64/include/openssl/ssl3.h | 339 + .../crypto/x86_64/include/openssl/sslerr.h | 767 ++ .../crypto/x86_64/include/openssl/stack.h | 83 + .../crypto/x86_64/include/openssl/store.h | 266 + .../crypto/x86_64/include/openssl/storeerr.h | 87 + .../crypto/x86_64/include/openssl/symhacks.h | 37 + .../crypto/x86_64/include/openssl/tls1.h | 1237 +++ .../crypto/x86_64/include/openssl/ts.h | 559 + .../crypto/x86_64/include/openssl/tserr.h | 128 + .../crypto/x86_64/include/openssl/txt_db.h | 57 + .../crypto/x86_64/include/openssl/ui.h | 368 + .../crypto/x86_64/include/openssl/uierr.h | 61 + .../crypto/x86_64/include/openssl/whrlpool.h | 48 + .../crypto/x86_64/include/openssl/x509.h | 1047 ++ .../crypto/x86_64/include/openssl/x509_vfy.h | 624 ++ .../crypto/x86_64/include/openssl/x509err.h | 125 + .../crypto/x86_64/include/openssl/x509v3.h | 935 ++ .../crypto/x86_64/include/openssl/x509v3err.h | 158 + .../third_party/crypto/x86_64/lib/libcrypto.a | Bin 0 -> 6415826 bytes .../third_party/crypto/x86_64/lib/libssl.a | Bin 0 -> 1208608 bytes example/cpp/CMakeLists.txt | 14 + example/cpp/README.md | 24 + example/cpp/tonjson_example.cpp | 51 + fec/CMakeLists.txt | 22 + fec/fec.cpp | 124 + fec/fec.h | 58 + install-scripts/generate-config.sh | 148 + install-scripts/known_hosts | 2 + install-scripts/prepare.sh | 41 + keyring/CMakeLists.txt | 12 + keyring/keyring.cpp | 183 + keyring/keyring.h | 52 + keyring/keyring.hpp | 74 + keys/CMakeLists.txt | 14 + keys/encryptor.cpp | 205 + keys/encryptor.h | 109 + keys/encryptor.hpp | 154 + keys/keys.cpp | 198 + keys/keys.hpp | 477 + lite-client-docs/DISCLAIMER | 9 + lite-client-docs/README | 48 + lite-client/CMakeLists.txt | 5 + lite-client/lite-client.cpp | 2078 ++++ lite-client/lite-client.h | 243 + memprof/CMakeLists.txt | 22 + memprof/memprof/memprof.cpp | 370 + memprof/memprof/memprof.h | 39 + overlay/CMakeLists.txt | 32 + overlay/overlay-broadcast.cpp | 149 + overlay/overlay-broadcast.hpp | 111 + overlay/overlay-fec-broadcast.cpp | 320 + overlay/overlay-fec-broadcast.hpp | 314 + overlay/overlay-fec.cpp | 74 + overlay/overlay-fec.hpp | 58 + overlay/overlay-id.hpp | 119 + overlay/overlay-manager.cpp | 349 + overlay/overlay-manager.h | 147 + overlay/overlay-peers.cpp | 300 + overlay/overlay.cpp | 544 + overlay/overlay.h | 73 + overlay/overlay.hpp | 336 + overlay/overlays.h | 214 + rldp/CMakeLists.txt | 25 + rldp/rldp-in.hpp | 126 + rldp/rldp-peer.cpp | 193 + rldp/rldp-peer.h | 61 + rldp/rldp-peer.hpp | 133 + rldp/rldp.cpp | 288 + rldp/rldp.h | 45 + rldp/rldp.hpp | 49 + run-clang-format.sh | 2 + tdactor/.gitrepo | 11 + tdactor/CMakeLists.txt | 62 + tdactor/benchmark/CMakeLists.txt | 19 + tdactor/benchmark/benchmark.cpp | 1202 +++ tdactor/benchmark/third_party/FAAArrayQueue.h | 371 + .../benchmark/third_party/HazardPointers.h | 152 + tdactor/benchmark/third_party/LCRQueue.h | 313 + .../third_party/LazyIndexArrayQueue.h | 0 .../benchmark/third_party/MoodyCamelQueue.h | 3716 +++++++ tdactor/benchmark/third_party/mp-queue.c | 684 ++ tdactor/benchmark/third_party/mp-queue.h | 149 + tdactor/td/actor/ActorId.h | 129 + tdactor/td/actor/ActorOwn.h | 100 + tdactor/td/actor/ActorShared.h | 120 + tdactor/td/actor/MultiPromise.cpp | 112 + tdactor/td/actor/MultiPromise.h | 65 + tdactor/td/actor/PromiseFuture.h | 336 + tdactor/td/actor/actor.h | 165 + tdactor/td/actor/common.h | 379 + tdactor/td/actor/core/Actor.h | 108 + tdactor/td/actor/core/ActorExecuteContext.h | 101 + tdactor/td/actor/core/ActorExecutor.cpp | 281 + tdactor/td/actor/core/ActorExecutor.h | 119 + tdactor/td/actor/core/ActorInfo.h | 109 + tdactor/td/actor/core/ActorInfoCreator.h | 95 + tdactor/td/actor/core/ActorLocker.h | 152 + tdactor/td/actor/core/ActorMailbox.h | 68 + tdactor/td/actor/core/ActorMessage.h | 97 + tdactor/td/actor/core/ActorSignals.h | 118 + tdactor/td/actor/core/ActorState.h | 181 + tdactor/td/actor/core/Context.h | 59 + tdactor/td/actor/core/CpuWorker.cpp | 47 + tdactor/td/actor/core/CpuWorker.h | 41 + tdactor/td/actor/core/IoWorker.cpp | 100 + tdactor/td/actor/core/IoWorker.h | 43 + tdactor/td/actor/core/Scheduler.cpp | 288 + tdactor/td/actor/core/Scheduler.h | 223 + tdactor/td/actor/core/SchedulerContext.h | 61 + tdactor/td/actor/core/SchedulerId.h | 49 + tdactor/td/actor/core/SchedulerMessage.h | 29 + tdactor/test/actors_core.cpp | 1099 ++ tdactor/test/actors_promise.cpp | 498 + tddb/CMakeLists.txt | 71 + tddb/td/db/KeyValue.h | 116 + tddb/td/db/KeyValueAsync.h | 158 + tddb/td/db/MemoryKeyValue.cpp | 74 + tddb/td/db/MemoryKeyValue.h | 48 + tddb/td/db/RocksDb.cpp | 199 + tddb/td/db/RocksDb.h | 81 + tddb/td/db/binlog/Binlog.cpp | 349 + tddb/td/db/binlog/Binlog.h | 135 + tddb/td/db/binlog/BinlogReaderHelper.cpp | 96 + tddb/td/db/binlog/BinlogReaderHelper.h | 46 + tddb/td/db/binlog/BinlogReaderInterface.h | 42 + tddb/td/db/utils/ChainBuffer.cpp | 139 + tddb/td/db/utils/ChainBuffer.h | 37 + tddb/td/db/utils/CyclicBuffer.cpp | 155 + tddb/td/db/utils/CyclicBuffer.h | 48 + tddb/td/db/utils/FileSyncState.cpp | 67 + tddb/td/db/utils/FileSyncState.h | 65 + tddb/td/db/utils/FileToStreamActor.cpp | 78 + tddb/td/db/utils/FileToStreamActor.h | 53 + tddb/td/db/utils/StreamInterface.cpp | 79 + tddb/td/db/utils/StreamInterface.h | 102 + tddb/td/db/utils/StreamToFileActor.cpp | 112 + tddb/td/db/utils/StreamToFileActor.h | 73 + tddb/test/binlog.cpp | 810 ++ tddb/test/io-bench.cpp | 685 ++ tddb/test/key_value.cpp | 245 + tdfec/CMakeLists.txt | 76 + tdfec/benchmark/CMakeLists.txt | 5 + tdfec/benchmark/benchmark.cpp | 304 + .../fec/algebra/BeliefPropagationDecoding.cpp | 114 + .../fec/algebra/BeliefPropagationDecoding.h | 62 + tdfec/td/fec/algebra/GaussianElimination.cpp | 59 + tdfec/td/fec/algebra/GaussianElimination.h | 28 + tdfec/td/fec/algebra/InactivationDecoding.cpp | 186 + tdfec/td/fec/algebra/InactivationDecoding.h | 73 + tdfec/td/fec/algebra/MatrixGF2.cpp | 20 + tdfec/td/fec/algebra/MatrixGF2.h | 103 + tdfec/td/fec/algebra/MatrixGF256.cpp | 20 + tdfec/td/fec/algebra/MatrixGF256.h | 199 + tdfec/td/fec/algebra/Octet.cpp | 9242 +++++++++++++++++ tdfec/td/fec/algebra/Octet.h | 152 + tdfec/td/fec/algebra/Simd.h | 266 + tdfec/td/fec/algebra/SparseMatrixGF2.cpp | 20 + tdfec/td/fec/algebra/SparseMatrixGF2.h | 277 + tdfec/td/fec/common/SymbolRef.h | 28 + tdfec/td/fec/common/SymbolsView.cpp | 46 + tdfec/td/fec/common/SymbolsView.h | 41 + tdfec/td/fec/fec.cpp | 212 + tdfec/td/fec/fec.h | 196 + tdfec/td/fec/online/Decoder.cpp | 77 + tdfec/td/fec/online/Decoder.h | 49 + tdfec/td/fec/online/Encoder.cpp | 63 + tdfec/td/fec/online/Encoder.h | 48 + tdfec/td/fec/online/Rfc.cpp | 79 + tdfec/td/fec/online/Rfc.h | 103 + tdfec/td/fec/raptorq/Decoder.cpp | 151 + tdfec/td/fec/raptorq/Decoder.h | 71 + tdfec/td/fec/raptorq/Encoder.cpp | 81 + tdfec/td/fec/raptorq/Encoder.h | 68 + tdfec/td/fec/raptorq/RawEncoder.cpp | 30 + tdfec/td/fec/raptorq/RawEncoder.h | 42 + tdfec/td/fec/raptorq/Rfc.cpp | 446 + tdfec/td/fec/raptorq/Rfc.h | 243 + tdfec/td/fec/raptorq/Solver.cpp | 244 + tdfec/td/fec/raptorq/Solver.h | 35 + tdfec/test/LibRaptorQ.cpp | 157 + tdfec/test/LibRaptorQ.h | 68 + tdfec/test/fec-test.cpp | 242 + tdnet/CMakeLists.txt | 26 + tdnet/example/tcp_ping_pong.cpp | 153 + tdnet/example/udp_ping_pong.cpp | 153 + tdnet/td/net/FdListener.cpp | 30 + tdnet/td/net/FdListener.h | 42 + tdnet/td/net/TcpListener.cpp | 77 + tdnet/td/net/TcpListener.h | 52 + tdnet/td/net/UdpServer.cpp | 466 + tdnet/td/net/UdpServer.h | 42 + tdnet/test/net-test.cpp | 167 + tdtl/.gitrepo | 11 + tdtl/CMakeLists.txt | 29 + tdtl/td/tl/tl_config.cpp | 384 + tdtl/td/tl/tl_config.h | 99 + tdtl/td/tl/tl_core.cpp | 33 + tdtl/td/tl/tl_core.h | 171 + tdtl/td/tl/tl_file_outputer.cpp | 53 + tdtl/td/tl/tl_file_outputer.h | 45 + tdtl/td/tl/tl_file_utils.cpp | 105 + tdtl/td/tl/tl_file_utils.h | 33 + tdtl/td/tl/tl_generate.cpp | 856 ++ tdtl/td/tl/tl_generate.h | 36 + tdtl/td/tl/tl_outputer.cpp | 28 + tdtl/td/tl/tl_outputer.h | 34 + tdtl/td/tl/tl_simple.h | 247 + tdtl/td/tl/tl_simple_parser.h | 119 + tdtl/td/tl/tl_string_outputer.cpp | 44 + tdtl/td/tl/tl_string_outputer.h | 38 + tdtl/td/tl/tl_writer.cpp | 243 + tdtl/td/tl/tl_writer.h | 175 + tdutils/.gitrepo | 12 + tdutils/CMakeLists.txt | 330 + tdutils/generate/CMakeLists.txt | 64 + .../generate/generate_mime_types_gperf.cpp | 177 + tdutils/generate/mime_types.txt | 777 ++ tdutils/td/utils/AesCtrByteFlow.h | 68 + tdutils/td/utils/BigNum.cpp | 321 + tdutils/td/utils/BigNum.h | 138 + tdutils/td/utils/BufferedFd.h | 226 + tdutils/td/utils/BufferedReader.h | 73 + tdutils/td/utils/BufferedUdp.cpp | 29 + tdutils/td/utils/BufferedUdp.h | 184 + tdutils/td/utils/ByteFlow.h | 303 + tdutils/td/utils/CancellationToken.h | 78 + tdutils/td/utils/ChangesProcessor.h | 73 + tdutils/td/utils/Closure.h | 180 + tdutils/td/utils/ConcurrentHashTable.h | 334 + tdutils/td/utils/Container.h | 160 + tdutils/td/utils/Context.h | 56 + tdutils/td/utils/DecTree.h | 228 + tdutils/td/utils/Destructor.h | 64 + tdutils/td/utils/Enumerator.h | 56 + .../td/utils/EpochBasedMemoryReclamation.h | 213 + tdutils/td/utils/FileLog.cpp | 131 + tdutils/td/utils/FileLog.h | 57 + tdutils/td/utils/FloodControlFast.h | 74 + tdutils/td/utils/FloodControlStrict.h | 108 + tdutils/td/utils/GitInfo.cpp | 32 + tdutils/td/utils/GitInfo.h | 31 + tdutils/td/utils/Gzip.cpp | 220 + tdutils/td/utils/Gzip.h | 118 + tdutils/td/utils/GzipByteFlow.cpp | 82 + tdutils/td/utils/GzipByteFlow.h | 60 + tdutils/td/utils/Hash.h | 87 + tdutils/td/utils/HashMap.h | 39 + tdutils/td/utils/HashSet.h | 39 + tdutils/td/utils/HazardPointers.h | 153 + tdutils/td/utils/Heap.h | 170 + tdutils/td/utils/Hints.cpp | 245 + tdutils/td/utils/Hints.h | 97 + tdutils/td/utils/HttpUrl.cpp | 211 + tdutils/td/utils/HttpUrl.h | 60 + tdutils/td/utils/JsonBuilder.cpp | 698 ++ tdutils/td/utils/JsonBuilder.h | 902 ++ tdutils/td/utils/List.h | 104 + tdutils/td/utils/MemoryLog.h | 95 + tdutils/td/utils/MimeType.cpp | 56 + tdutils/td/utils/MimeType.h | 32 + tdutils/td/utils/MovableValue.h | 52 + tdutils/td/utils/MpmcQueue.cpp | 27 + tdutils/td/utils/MpmcQueue.h | 469 + tdutils/td/utils/MpmcWaiter.h | 120 + tdutils/td/utils/MpscLinkQueue.h | 185 + tdutils/td/utils/MpscPollableQueue.h | 164 + tdutils/td/utils/Named.h | 39 + tdutils/td/utils/ObjectPool.h | 261 + tdutils/td/utils/Observer.h | 53 + tdutils/td/utils/OptionsParser.cpp | 142 + tdutils/td/utils/OptionsParser.h | 60 + tdutils/td/utils/OrderedEventsProcessor.h | 111 + tdutils/td/utils/Parser.h | 209 + tdutils/td/utils/PathView.h | 128 + tdutils/td/utils/Random.cpp | 187 + tdutils/td/utils/Random.h | 59 + tdutils/td/utils/ScopeGuard.h | 89 + tdutils/td/utils/SharedObjectPool.h | 294 + tdutils/td/utils/SharedSlice.cpp | 29 + tdutils/td/utils/SharedSlice.h | 392 + tdutils/td/utils/Slice-decl.h | 224 + tdutils/td/utils/Slice.cpp | 43 + tdutils/td/utils/Slice.h | 339 + tdutils/td/utils/Span.h | 138 + tdutils/td/utils/SpinLock.h | 72 + tdutils/td/utils/StackAllocator.cpp | 31 + tdutils/td/utils/StackAllocator.h | 95 + tdutils/td/utils/Status.cpp | 66 + tdutils/td/utils/Status.h | 557 + tdutils/td/utils/Storer.h | 100 + tdutils/td/utils/StorerBase.h | 37 + tdutils/td/utils/StringBuilder.cpp | 226 + tdutils/td/utils/StringBuilder.h | 150 + tdutils/td/utils/ThreadLocalStorage.h | 66 + tdutils/td/utils/ThreadSafeCounter.h | 123 + tdutils/td/utils/Time.cpp | 32 + tdutils/td/utils/Time.h | 124 + tdutils/td/utils/TimedStat.h | 83 + tdutils/td/utils/Timer.cpp | 62 + tdutils/td/utils/Timer.h | 53 + tdutils/td/utils/TsFileLog.cpp | 92 + tdutils/td/utils/TsFileLog.h | 28 + tdutils/td/utils/UInt.h | 105 + tdutils/td/utils/Variant.h | 303 + tdutils/td/utils/VectorQueue.h | 84 + tdutils/td/utils/as.h | 98 + tdutils/td/utils/base64.cpp | 302 + tdutils/td/utils/base64.h | 40 + tdutils/td/utils/benchmark.h | 144 + tdutils/td/utils/bits.h | 267 + tdutils/td/utils/buffer.cpp | 196 + tdutils/td/utils/buffer.h | 815 ++ tdutils/td/utils/check.cpp | 34 + tdutils/td/utils/check.h | 44 + tdutils/td/utils/common.h | 130 + tdutils/td/utils/config.h.in | 9 + tdutils/td/utils/crypto.cpp | 851 ++ tdutils/td/utils/crypto.h | 133 + tdutils/td/utils/filesystem.cpp | 205 + tdutils/td/utils/filesystem.h | 46 + tdutils/td/utils/find_boundary.cpp | 63 + tdutils/td/utils/find_boundary.h | 29 + tdutils/td/utils/format.h | 337 + tdutils/td/utils/int_types.h | 57 + tdutils/td/utils/invoke.h | 222 + tdutils/td/utils/logging.cpp | 275 + tdutils/td/utils/logging.h | 321 + tdutils/td/utils/misc.cpp | 208 + tdutils/td/utils/misc.h | 422 + tdutils/td/utils/optional.h | 99 + tdutils/td/utils/overloaded.h | 51 + tdutils/td/utils/port/Clocks.cpp | 35 + tdutils/td/utils/port/Clocks.h | 40 + tdutils/td/utils/port/CxCli.h | 159 + tdutils/td/utils/port/EventFd.h | 45 + tdutils/td/utils/port/EventFdBase.h | 44 + tdutils/td/utils/port/FileFd.cpp | 587 ++ tdutils/td/utils/port/FileFd.h | 87 + tdutils/td/utils/port/IPAddress.cpp | 622 ++ tdutils/td/utils/port/IPAddress.h | 100 + tdutils/td/utils/port/IoSlice.h | 54 + tdutils/td/utils/port/MemoryMapping.cpp | 123 + tdutils/td/utils/port/MemoryMapping.h | 64 + tdutils/td/utils/port/Poll.h | 47 + tdutils/td/utils/port/PollBase.h | 40 + tdutils/td/utils/port/PollFlags.cpp | 83 + tdutils/td/utils/port/PollFlags.h | 134 + tdutils/td/utils/port/RwMutex.h | 161 + tdutils/td/utils/port/ServerSocketFd.cpp | 373 + tdutils/td/utils/port/ServerSocketFd.h | 66 + tdutils/td/utils/port/SocketFd.cpp | 632 ++ tdutils/td/utils/port/SocketFd.h | 82 + tdutils/td/utils/port/Stat.cpp | 373 + tdutils/td/utils/port/Stat.h | 65 + tdutils/td/utils/port/StdStreams.cpp | 250 + tdutils/td/utils/port/StdStreams.h | 61 + tdutils/td/utils/port/UdpSocketFd.cpp | 873 ++ tdutils/td/utils/port/UdpSocketFd.h | 105 + tdutils/td/utils/port/config.h | 65 + tdutils/td/utils/port/detail/Epoll.cpp | 136 + tdutils/td/utils/port/detail/Epoll.h | 71 + tdutils/td/utils/port/detail/EventFdBsd.cpp | 114 + tdutils/td/utils/port/detail/EventFdBsd.h | 61 + tdutils/td/utils/port/detail/EventFdLinux.cpp | 138 + tdutils/td/utils/port/detail/EventFdLinux.h | 63 + .../td/utils/port/detail/EventFdWindows.cpp | 80 + tdutils/td/utils/port/detail/EventFdWindows.h | 60 + tdutils/td/utils/port/detail/Iocp.cpp | 122 + tdutils/td/utils/port/detail/Iocp.h | 83 + tdutils/td/utils/port/detail/KQueue.cpp | 192 + tdutils/td/utils/port/detail/KQueue.h | 84 + tdutils/td/utils/port/detail/NativeFd.cpp | 259 + tdutils/td/utils/port/detail/NativeFd.h | 80 + tdutils/td/utils/port/detail/Poll.cpp | 114 + tdutils/td/utils/port/detail/Poll.h | 68 + tdutils/td/utils/port/detail/PollableFd.h | 262 + tdutils/td/utils/port/detail/Select.cpp | 132 + tdutils/td/utils/port/detail/Select.h | 76 + .../td/utils/port/detail/ThreadIdGuard.cpp | 64 + tdutils/td/utils/port/detail/ThreadIdGuard.h | 38 + tdutils/td/utils/port/detail/ThreadPthread.h | 121 + tdutils/td/utils/port/detail/ThreadStl.h | 89 + tdutils/td/utils/port/detail/WineventPoll.cpp | 55 + tdutils/td/utils/port/detail/WineventPoll.h | 62 + tdutils/td/utils/port/path.cpp | 635 ++ tdutils/td/utils/port/path.h | 84 + tdutils/td/utils/port/platform.h | 128 + tdutils/td/utils/port/signals.cpp | 339 + tdutils/td/utils/port/signals.h | 48 + tdutils/td/utils/port/sleep.cpp | 49 + tdutils/td/utils/port/sleep.h | 27 + tdutils/td/utils/port/stacktrace.cpp | 129 + tdutils/td/utils/port/stacktrace.h | 33 + tdutils/td/utils/port/thread.h | 46 + tdutils/td/utils/port/thread_local.cpp | 53 + tdutils/td/utils/port/thread_local.h | 77 + tdutils/td/utils/port/user.cpp | 41 + tdutils/td/utils/port/user.h | 11 + tdutils/td/utils/port/wstring_convert.cpp | 127 + tdutils/td/utils/port/wstring_convert.h | 43 + tdutils/td/utils/queue.h | 484 + tdutils/td/utils/tests.cpp | 243 + tdutils/td/utils/tests.h | 182 + tdutils/td/utils/tl_helpers.h | 276 + tdutils/td/utils/tl_parsers.cpp | 42 + tdutils/td/utils/tl_parsers.h | 268 + tdutils/td/utils/tl_storers.h | 328 + tdutils/td/utils/translit.cpp | 127 + tdutils/td/utils/translit.h | 28 + tdutils/td/utils/type_traits.h | 42 + tdutils/td/utils/uint128.h | 291 + tdutils/td/utils/unicode.cpp | 1194 +++ tdutils/td/utils/unicode.h | 45 + tdutils/td/utils/unique_ptr.h | 117 + tdutils/td/utils/utf8.cpp | 136 + tdutils/td/utils/utf8.h | 118 + tdutils/test/ConcurrentHashMap.cpp | 266 + tdutils/test/Enumerator.cpp | 36 + tdutils/test/EpochBasedMemoryReclamation.cpp | 80 + tdutils/test/HazardPointers.cpp | 72 + tdutils/test/MpmcQueue.cpp | 219 + tdutils/test/MpmcWaiter.cpp | 129 + tdutils/test/MpscLinkQueue.cpp | 128 + tdutils/test/OrderedEventsProcessor.cpp | 48 + tdutils/test/SharedObjectPool.cpp | 117 + tdutils/test/SharedSlice.cpp | 97 + tdutils/test/buffer.cpp | 70 + tdutils/test/crypto.cpp | 304 + tdutils/test/filesystem.cpp | 54 + tdutils/test/gzip.cpp | 125 + tdutils/test/heap.cpp | 189 + tdutils/test/json.cpp | 107 + tdutils/test/log.cpp | 136 + tdutils/test/misc.cpp | 939 ++ tdutils/test/port.cpp | 216 + tdutils/test/pq.cpp | 130 + tdutils/test/variant.cpp | 85 + terminal/CMakeLists.txt | 49 + terminal/terminal.cpp | 272 + terminal/terminal.h | 88 + terminal/terminal.hpp | 100 + test/ed25519_crypto.cpp | 2053 ++++ test/generate-test-node-config.py | 207 + test/regression-tests.ans | 54 + test/test-adnl.cpp | 322 + test/test-catchain.cpp | 318 + test/test-dht.cpp | 407 + test/test-dummy-0-lite-client.cpp | 236 + test/test-ext-client.cpp | 220 + test/test-ext-server.cpp | 221 + test/test-hello-world.cpp | 138 + test/test-node.cpp | 376 + test/test-rldp.cpp | 202 + test/test-td-main.cpp | 55 + test/test-ton-collator.cpp | 446 + test/test-ton-dummy-0-disk.cpp | 236 + test/test-ton-dummy-0.cpp | 277 + test/test-validator-session-state.cpp | 996 ++ test/test-validator-session.cpp | 355 + test/test-validator.cpp | 356 + third-party/abseil-cpp | 1 + third-party/crc32c | 1 + third-party/libraptorq | 1 + third-party/rocksdb | 1 + third-party/wingetopt/.gitignore | 8 + third-party/wingetopt/.gitrepo | 11 + third-party/wingetopt/CMakeLists.txt | 7 + third-party/wingetopt/LICENSE | 54 + third-party/wingetopt/README.md | 63 + third-party/wingetopt/src/getopt.c | 563 + third-party/wingetopt/src/getopt.h | 105 + tl-utils/CMakeLists.txt | 23 + tl-utils/common-utils.hpp | 208 + tl-utils/lite-utils.cpp | 132 + tl-utils/lite-utils.hpp | 49 + tl-utils/tl-utils.cpp | 138 + tl-utils/tl-utils.hpp | 51 + tl/CMakeLists.txt | 55 + tl/generate/CMakeLists.txt | 83 + .../JavadocTlDocumentationGenerator.php | 268 + tl/generate/TlDocumentationGenerator.php | 323 + tl/generate/generate_common.cpp | 83 + tl/generate/generate_java.cpp | 50 + tl/generate/scheme/lite_api.tl | 73 + tl/generate/scheme/lite_api.tlo | Bin 0 -> 10364 bytes tl/generate/scheme/ton_api.tl | 553 + tl/generate/scheme/ton_api.tlo | Bin 0 -> 50056 bytes tl/generate/scheme/tonlib_api.tl | 89 + tl/generate/scheme/tonlib_api.tlo | Bin 0 -> 8420 bytes tl/generate/scheme/update-tlo.sh | 5 + tl/generate/tl_json_converter.cpp | 270 + tl/generate/tl_json_converter.h | 29 + tl/generate/tl_writer_cpp.cpp | 698 ++ tl/generate/tl_writer_cpp.h | 120 + tl/generate/tl_writer_h.cpp | 355 + tl/generate/tl_writer_h.h | 106 + tl/generate/tl_writer_hpp.cpp | 267 + tl/generate/tl_writer_hpp.h | 121 + tl/generate/tl_writer_java.cpp | 465 + tl/generate/tl_writer_java.h | 131 + tl/generate/tl_writer_jni_cpp.cpp | 676 ++ tl/generate/tl_writer_jni_cpp.h | 123 + tl/generate/tl_writer_jni_h.cpp | 204 + tl/generate/tl_writer_jni_h.h | 70 + tl/generate/tl_writer_td.cpp | 276 + tl/generate/tl_writer_td.h | 81 + tl/tl/TlObject.h | 177 + tl/tl/tl_jni_object.cpp | 416 + tl/tl/tl_jni_object.h | 250 + tl/tl/tl_json.h | 305 + tl/tl/tl_object_parse.h | 158 + tl/tl/tl_object_store.h | 107 + ton/lite-tl.hpp | 56 + ton/ton-io.hpp | 46 + ton/ton-shard.h | 210 + ton/ton-tl.hpp | 56 + ton/ton-types.h | 418 + tonlib/CMakeLists.txt | 156 + tonlib/test/offline.cpp | 414 + tonlib/test/online.cpp | 336 + tonlib/tonlib/Client.cpp | 158 + tonlib/tonlib/Client.h | 52 + tonlib/tonlib/ClientActor.cpp | 18 + tonlib/tonlib/ClientActor.h | 41 + tonlib/tonlib/ClientJson.cpp | 119 + tonlib/tonlib/ClientJson.h | 48 + tonlib/tonlib/Config.cpp | 70 + tonlib/tonlib/Config.h | 32 + tonlib/tonlib/ExtClient.cpp | 52 + tonlib/tonlib/ExtClient.h | 80 + tonlib/tonlib/ExtClientLazy.cpp | 100 + tonlib/tonlib/ExtClientLazy.h | 36 + tonlib/tonlib/GenericAccount.cpp | 79 + tonlib/tonlib/GenericAccount.h | 30 + tonlib/tonlib/KeyStorage.cpp | 148 + tonlib/tonlib/KeyStorage.h | 77 + tonlib/tonlib/LastBlock.cpp | 86 + tonlib/tonlib/LastBlock.h | 47 + tonlib/tonlib/TestGiver.cpp | 50 + tonlib/tonlib/TestGiver.h | 29 + tonlib/tonlib/TestWallet.cpp | 74 + tonlib/tonlib/TestWallet.h | 37 + tonlib/tonlib/TonlibCallback.h | 30 + tonlib/tonlib/TonlibClient.cpp | 996 ++ tonlib/tonlib/TonlibClient.h | 136 + tonlib/tonlib/keys/DecryptedKey.cpp | 81 + tonlib/tonlib/keys/DecryptedKey.h | 64 + tonlib/tonlib/keys/EncryptedKey.cpp | 53 + tonlib/tonlib/keys/EncryptedKey.h | 38 + tonlib/tonlib/keys/Mnemonic.cpp | 188 + tonlib/tonlib/keys/Mnemonic.h | 65 + tonlib/tonlib/keys/SimpleEncryption.cpp | 101 + tonlib/tonlib/keys/SimpleEncryption.h | 39 + tonlib/tonlib/keys/bip39.cpp | 2072 ++++ tonlib/tonlib/keys/bip39.h | 24 + tonlib/tonlib/tonlib_client_json.cpp | 57 + tonlib/tonlib/tonlib_client_json.h | 39 + tonlib/tonlib/utils.cpp | 35 + tonlib/tonlib/utils.h | 26 + tonlib/tonlibclientjson_export_list | 5 + utils/CMakeLists.txt | 16 + utils/generate-random-id.cpp | 151 + utils/json2tlo.cpp | 118 + validator-engine-console/CMakeLists.txt | 7 + .../validator-engine-console-query.cpp | 657 ++ .../validator-engine-console-query.h | 823 ++ .../validator-engine-console.cpp | 307 + .../validator-engine-console.h | 124 + validator-engine/CMakeLists.txt | 16 + validator-engine/validator-engine.cpp | 2761 +++++ validator-engine/validator-engine.hpp | 359 + validator-session/CMakeLists.txt | 29 + validator-session/persistent-vector.cpp | 62 + validator-session/persistent-vector.h | 808 ++ .../validator-session-description.cpp | 203 + .../validator-session-description.h | 118 + .../validator-session-description.hpp | 165 + validator-session/validator-session-state.cpp | 1611 +++ validator-session/validator-session-state.h | 1132 ++ validator-session/validator-session-types.h | 65 + validator-session/validator-session.cpp | 893 ++ validator-session/validator-session.h | 119 + validator-session/validator-session.hpp | 210 + validator/CMakeLists.txt | 159 + validator/apply-block.cpp | 254 + validator/apply-block.hpp | 80 + validator/block-handle.cpp | 60 + validator/block-handle.hpp | 503 + validator/db/archiver.cpp | 147 + validator/db/archiver.hpp | 62 + validator/db/blockdb.cpp | 234 + validator/db/blockdb.hpp | 86 + validator/db/celldb.cpp | 283 + validator/db/celldb.hpp | 117 + validator/db/filedb.cpp | 333 + validator/db/filedb.hpp | 202 + validator/db/files-async.hpp | 108 + validator/db/ltdb.cpp | 221 + validator/db/ltdb.hpp | 63 + validator/db/rootdb.cpp | 470 + validator/db/rootdb.hpp | 127 + validator/db/statedb.cpp | 191 + validator/db/statedb.hpp | 65 + validator/db/staticfilesdb.cpp | 33 + validator/db/staticfilesdb.hpp | 43 + validator/downloaders/download-state.cpp | 233 + validator/downloaders/download-state.hpp | 74 + .../downloaders/wait-block-data-disk.cpp | 79 + .../downloaders/wait-block-data-disk.hpp | 65 + validator/downloaders/wait-block-data.cpp | 154 + validator/downloaders/wait-block-data.hpp | 78 + .../downloaders/wait-block-state-merge.cpp | 87 + .../downloaders/wait-block-state-merge.hpp | 70 + validator/downloaders/wait-block-state.cpp | 322 + validator/downloaders/wait-block-state.hpp | 86 + validator/dummy0/CMakeLists.txt | 40 + validator/dummy0/accept-block.cpp | 332 + validator/dummy0/accept-block.hpp | 94 + validator/dummy0/block.hpp | 62 + validator/dummy0/check-proof.cpp | 225 + validator/dummy0/check-proof.hpp | 99 + validator/dummy0/collate-query.cpp | 238 + validator/dummy0/collate-query.hpp | 73 + validator/dummy0/external-message.hpp | 70 + validator/dummy0/fabric.cpp | 169 + validator/dummy0/fake-accept-block.cpp | 227 + validator/dummy0/fake-accept-block.hpp | 88 + validator/dummy0/proof.hpp | 85 + validator/dummy0/shard.cpp | 324 + validator/dummy0/shard.hpp | 224 + validator/dummy0/signature-set.cpp | 55 + validator/dummy0/signature-set.hpp | 53 + validator/dummy0/top-shard-description.cpp | 168 + validator/dummy0/top-shard-description.hpp | 118 + validator/dummy0/validate-query.cpp | 164 + validator/dummy0/validate-query.hpp | 68 + validator/dummy0/validator-set.cpp | 116 + validator/dummy0/validator-set.hpp | 74 + validator/fabric.h | 81 + validator/full-node-shard.cpp | 649 ++ validator/full-node-shard.h | 75 + validator/full-node-shard.hpp | 156 + validator/full-node.cpp | 446 + validator/full-node.h | 81 + validator/full-node.hpp | 109 + validator/get-next-key-blocks.cpp | 91 + validator/get-next-key-blocks.h | 64 + validator/impl/CMakeLists.txt | 52 + validator/impl/accept-block.cpp | 820 ++ validator/impl/accept-block.hpp | 141 + validator/impl/block.cpp | 79 + validator/impl/block.hpp | 62 + validator/impl/check-proof.cpp | 478 + validator/impl/check-proof.hpp | 137 + validator/impl/collate-query-impl.h | 63 + validator/impl/collator-impl.h | 293 + validator/impl/collator.cpp | 3800 +++++++ validator/impl/collator.h | 49 + validator/impl/external-message.cpp | 74 + validator/impl/external-message.hpp | 55 + validator/impl/fabric.cpp | 215 + validator/impl/ihr-message.cpp | 132 + validator/impl/ihr-message.hpp | 56 + validator/impl/liteserver.cpp | 1456 +++ validator/impl/liteserver.hpp | 139 + validator/impl/message-queue.cpp | 30 + validator/impl/message-queue.hpp | 66 + validator/impl/proof.cpp | 143 + validator/impl/proof.hpp | 94 + validator/impl/shard.cpp | 534 + validator/impl/shard.hpp | 157 + validator/impl/signature-set.cpp | 118 + validator/impl/signature-set.hpp | 45 + validator/impl/top-shard-descr.cpp | 541 + validator/impl/top-shard-descr.hpp | 183 + validator/impl/validate-query.cpp | 5242 ++++++++++ validator/impl/validate-query.hpp | 352 + validator/impl/validator-set.cpp | 193 + validator/impl/validator-set.hpp | 85 + validator/interfaces/block-handle.h | 105 + validator/interfaces/block.h | 39 + validator/interfaces/config.h | 41 + validator/interfaces/db.h | 98 + validator/interfaces/external-message.h | 42 + validator/interfaces/ihr-message.h | 42 + validator/interfaces/message-queue.h | 41 + validator/interfaces/proof.h | 45 + validator/interfaces/shard-block.h | 55 + validator/interfaces/shard.h | 89 + validator/interfaces/signature-set.h | 51 + validator/interfaces/validator-full-id.h | 42 + validator/interfaces/validator-manager.h | 168 + validator/interfaces/validator-set.h | 46 + validator/invariants.hpp | 83 + validator/manager-disk.cpp | 912 ++ validator/manager-disk.h | 36 + validator/manager-disk.hpp | 358 + validator/manager-init.cpp | 387 + validator/manager-init.h | 45 + validator/manager-init.hpp | 120 + validator/manager.cpp | 1936 ++++ validator/manager.h | 40 + validator/manager.hpp | 560 + validator/net/download-block.cpp | 367 + validator/net/download-block.hpp | 94 + validator/net/download-next-block.cpp | 152 + validator/net/download-next-block.hpp | 74 + validator/net/download-proof.cpp | 205 + validator/net/download-proof.hpp | 76 + validator/net/download-state.cpp | 181 + validator/net/download-state.hpp | 75 + validator/net/get-next-key-blocks.cpp | 220 + validator/net/get-next-key-blocks.hpp | 82 + validator/shard-client.cpp | 223 + validator/shard-client.hpp | 95 + validator/state-serializer.cpp | 250 + validator/state-serializer.hpp | 93 + validator/token-manager.cpp | 102 + validator/token-manager.h | 65 + validator/validate-broadcast.cpp | 203 + validator/validate-broadcast.hpp | 74 + validator/validator-full-id.cpp | 32 + validator/validator-group.cpp | 254 + validator/validator-group.hpp | 105 + validator/validator-options.cpp | 38 + validator/validator-options.hpp | 134 + validator/validator.h | 181 + 1610 files changed, 398047 insertions(+) create mode 100644 .clang-format create mode 100644 .clang_complete create mode 100644 .gitattributes create mode 100644 .gitignore create mode 100644 .gitmodules create mode 100644 CMake/AddCXXCompilerFlag.cmake create mode 100644 CMake/FindJeMalloc.cmake create mode 100644 CMake/FindMHD.cmake create mode 100644 CMake/FindReadline.cmake create mode 100644 CMake/UseLATEX.cmake create mode 100644 CMakeLists.txt create mode 100644 GPLv2 create mode 100644 LGPLv2 create mode 100644 LICENSE.LGPL create mode 100644 adnl/CMakeLists.txt create mode 100644 adnl/adnl-address-list.cpp create mode 100644 adnl/adnl-address-list.h create mode 100644 adnl/adnl-address-list.hpp create mode 100644 adnl/adnl-channel.cpp create mode 100644 adnl/adnl-channel.h create mode 100644 adnl/adnl-channel.hpp create mode 100644 adnl/adnl-db.cpp create mode 100644 adnl/adnl-db.h create mode 100644 adnl/adnl-db.hpp create mode 100644 adnl/adnl-ext-client.cpp create mode 100644 adnl/adnl-ext-client.h create mode 100644 adnl/adnl-ext-client.hpp create mode 100644 adnl/adnl-ext-connection.cpp create mode 100644 adnl/adnl-ext-connection.hpp create mode 100644 adnl/adnl-ext-server.cpp create mode 100644 adnl/adnl-ext-server.h create mode 100644 adnl/adnl-ext-server.hpp create mode 100644 adnl/adnl-local-id.cpp create mode 100644 adnl/adnl-local-id.h create mode 100644 adnl/adnl-message.cpp create mode 100644 adnl/adnl-message.h create mode 100644 adnl/adnl-network-manager.cpp create mode 100644 adnl/adnl-network-manager.h create mode 100644 adnl/adnl-network-manager.hpp create mode 100644 adnl/adnl-node-id.hpp create mode 100644 adnl/adnl-node.cpp create mode 100644 adnl/adnl-node.h create mode 100644 adnl/adnl-packet.cpp create mode 100644 adnl/adnl-packet.h create mode 100644 adnl/adnl-peer-table.cpp create mode 100644 adnl/adnl-peer-table.h create mode 100644 adnl/adnl-peer-table.hpp create mode 100644 adnl/adnl-peer.cpp create mode 100644 adnl/adnl-peer.h create mode 100644 adnl/adnl-peer.hpp create mode 100644 adnl/adnl-proxy-types.cpp create mode 100644 adnl/adnl-proxy-types.h create mode 100644 adnl/adnl-proxy-types.hpp create mode 100644 adnl/adnl-proxy.cpp create mode 100644 adnl/adnl-query.cpp create mode 100644 adnl/adnl-query.h create mode 100644 adnl/adnl-static-nodes.cpp create mode 100644 adnl/adnl-static-nodes.h create mode 100644 adnl/adnl-static-nodes.hpp create mode 100644 adnl/adnl-test-loopback-implementation.cpp create mode 100644 adnl/adnl-test-loopback-implementation.h create mode 100644 adnl/adnl.h create mode 100644 adnl/test/adnl-test-ping.cpp create mode 100644 adnl/utils.cpp create mode 100644 adnl/utils.hpp create mode 100644 blockchain-explorer/CMakeLists.txt create mode 100644 blockchain-explorer/blockchain-explorer-http.cpp create mode 100644 blockchain-explorer/blockchain-explorer-http.hpp create mode 100644 blockchain-explorer/blockchain-explorer-query.cpp create mode 100644 blockchain-explorer/blockchain-explorer-query.hpp create mode 100644 blockchain-explorer/blockchain-explorer.cpp create mode 100644 blockchain-explorer/blockchain-explorer.hpp create mode 100644 catchain/CMakeLists.txt create mode 100644 catchain/catchain-block.cpp create mode 100644 catchain/catchain-block.hpp create mode 100644 catchain/catchain-received-block.cpp create mode 100644 catchain/catchain-received-block.h create mode 100644 catchain/catchain-received-block.hpp create mode 100644 catchain/catchain-receiver-interface.h create mode 100644 catchain/catchain-receiver-source.cpp create mode 100644 catchain/catchain-receiver-source.h create mode 100644 catchain/catchain-receiver-source.hpp create mode 100644 catchain/catchain-receiver.cpp create mode 100644 catchain/catchain-receiver.h create mode 100644 catchain/catchain-receiver.hpp create mode 100644 catchain/catchain-types.h create mode 100644 catchain/catchain.cpp create mode 100644 catchain/catchain.h create mode 100644 catchain/catchain.hpp create mode 100644 common/CMakeLists.txt create mode 100644 common/checksum.h create mode 100644 common/delay.h create mode 100644 common/errorcode.h create mode 100644 common/errorlog.cpp create mode 100644 common/errorlog.h create mode 100644 common/io.hpp create mode 100644 common/status.h create mode 100644 crypto/CMakeLists.txt create mode 100644 crypto/Ed25519.cpp create mode 100644 crypto/Ed25519.h create mode 100644 crypto/block/Binlog.cpp create mode 100644 crypto/block/Binlog.h create mode 100644 crypto/block/block-binlog.h create mode 100644 crypto/block/block-db-impl.h create mode 100644 crypto/block/block-db.cpp create mode 100644 crypto/block/block-db.h create mode 100644 crypto/block/block-parse.cpp create mode 100644 crypto/block/block-parse.h create mode 100644 crypto/block/block.cpp create mode 100644 crypto/block/block.h create mode 100644 crypto/block/block.tlb create mode 100644 crypto/block/check-proof.cpp create mode 100644 crypto/block/check-proof.h create mode 100644 crypto/block/create-state.cpp create mode 100644 crypto/block/mc-config.cpp create mode 100644 crypto/block/mc-config.h create mode 100644 crypto/block/output-queue-merger.cpp create mode 100644 crypto/block/output-queue-merger.h create mode 100644 crypto/block/test-block.cpp create mode 100644 crypto/block/transaction.cpp create mode 100644 crypto/block/transaction.h create mode 100644 crypto/common/AtomicRef.h create mode 100644 crypto/common/bigint.cpp create mode 100644 crypto/common/bigint.hpp create mode 100644 crypto/common/bitstring.cpp create mode 100644 crypto/common/bitstring.h create mode 100644 crypto/common/refcnt.cpp create mode 100644 crypto/common/refcnt.hpp create mode 100644 crypto/common/refint.cpp create mode 100644 crypto/common/refint.h create mode 100644 crypto/common/util.cpp create mode 100644 crypto/common/util.h create mode 100644 crypto/ellcurve/Ed25519.cpp create mode 100644 crypto/ellcurve/Ed25519.h create mode 100644 crypto/ellcurve/Fp25519.cpp create mode 100644 crypto/ellcurve/Fp25519.h create mode 100644 crypto/ellcurve/Montgomery.cpp create mode 100644 crypto/ellcurve/Montgomery.h create mode 100644 crypto/ellcurve/TwEdwards.cpp create mode 100644 crypto/ellcurve/TwEdwards.h create mode 100644 crypto/fift/Dictionary.cpp create mode 100644 crypto/fift/Dictionary.h create mode 100644 crypto/fift/Fift.cpp create mode 100644 crypto/fift/Fift.h create mode 100644 crypto/fift/IntCtx.cpp create mode 100644 crypto/fift/IntCtx.h create mode 100644 crypto/fift/SourceLookup.cpp create mode 100644 crypto/fift/SourceLookup.h create mode 100644 crypto/fift/fift-main.cpp create mode 100644 crypto/fift/lib/Asm.fif create mode 100644 crypto/fift/lib/Fift.fif create mode 100644 crypto/fift/lib/Lisp.fif create mode 100644 crypto/fift/lib/Lists.fif create mode 100644 crypto/fift/lib/Stack.fif create mode 100644 crypto/fift/lib/TonUtil.fif create mode 100644 crypto/fift/utils.cpp create mode 100644 crypto/fift/utils.h create mode 100644 crypto/fift/words.cpp create mode 100644 crypto/fift/words.h create mode 100644 crypto/func/abscode.cpp create mode 100644 crypto/func/analyzer.cpp create mode 100644 crypto/func/asmops.cpp create mode 100644 crypto/func/builtins.cpp create mode 100644 crypto/func/codegen.cpp create mode 100644 crypto/func/func.cpp create mode 100644 crypto/func/func.h create mode 100644 crypto/func/gen-abscode.cpp create mode 100644 crypto/func/keywords.cpp create mode 100644 crypto/func/optimize.cpp create mode 100644 crypto/func/parse-func.cpp create mode 100644 crypto/func/stack-transform.cpp create mode 100644 crypto/func/test/a6.fc create mode 100644 crypto/func/test/a6.fp create mode 100644 crypto/func/test/a6_1.fc create mode 100644 crypto/func/test/a6_2.fc create mode 100644 crypto/func/test/a7.fc create mode 100644 crypto/func/test/a8.fc create mode 100644 crypto/func/test/a9.fc create mode 100644 crypto/func/test/a9_1.fc create mode 100644 crypto/func/test/b1.fc create mode 100644 crypto/func/test/b2.fc create mode 100644 crypto/func/test/b2_0.fc create mode 100644 crypto/func/test/b2_1.fc create mode 100644 crypto/func/test/b2_2.fc create mode 100644 crypto/func/unify-types.cpp create mode 100644 crypto/openssl/bignum.cpp create mode 100644 crypto/openssl/bignum.h create mode 100644 crypto/openssl/digest.h create mode 100644 crypto/openssl/rand.cpp create mode 100644 crypto/openssl/rand.hpp create mode 100644 crypto/openssl/residue.cpp create mode 100644 crypto/openssl/residue.h create mode 100644 crypto/parser/lexer.cpp create mode 100644 crypto/parser/lexer.h create mode 100644 crypto/parser/srcread.cpp create mode 100644 crypto/parser/srcread.h create mode 100644 crypto/parser/symtable.cpp create mode 100644 crypto/parser/symtable.h create mode 100644 crypto/smartcont/CreateState.fif create mode 100644 crypto/smartcont/config-code.fc create mode 100644 crypto/smartcont/elector-code.fc create mode 100644 crypto/smartcont/gen-zerostate-test.fif create mode 100644 crypto/smartcont/gen-zerostate.fif create mode 100644 crypto/smartcont/new-pinger.fif create mode 100644 crypto/smartcont/new-wallet.fif create mode 100644 crypto/smartcont/recover-stake.fif create mode 100644 crypto/smartcont/show-addr.fif create mode 100644 crypto/smartcont/simple-wallet-code.fc create mode 100644 crypto/smartcont/stdlib.fc create mode 100644 crypto/smartcont/testgiver.fif create mode 100644 crypto/smartcont/update-config-smc.fif create mode 100644 crypto/smartcont/update-config.fif create mode 100644 crypto/smartcont/update-elector-smc.fif create mode 100644 crypto/smartcont/validator-elect-req.fif create mode 100644 crypto/smartcont/validator-elect-signed.fif create mode 100644 crypto/smartcont/wallet-code.fc create mode 100644 crypto/smartcont/wallet.fif create mode 100644 crypto/test/Ed25519.cpp create mode 100644 crypto/test/fift.cpp create mode 100644 crypto/test/fift/bug.fif create mode 100644 crypto/test/fift/bug_div.fif create mode 100755 crypto/test/fift/cmdline.fif create mode 100644 crypto/test/fift/contfrac.fif create mode 100644 crypto/test/fift/fixed.fif create mode 100644 crypto/test/fift/sort.fif create mode 100644 crypto/test/fift/sort2.fif create mode 100644 crypto/test/fift/test.fif create mode 100644 crypto/test/fift/testdb.fif create mode 100644 crypto/test/fift/testdict.fif create mode 100644 crypto/test/fift/testvm.fif create mode 100644 crypto/test/fift/testvm2.fif create mode 100644 crypto/test/fift/testvm3.fif create mode 100644 crypto/test/fift/testvm4.fif create mode 100644 crypto/test/fift/testvm4a.fif create mode 100644 crypto/test/fift/testvm4b.fif create mode 100644 crypto/test/fift/testvm4c.fif create mode 100644 crypto/test/fift/testvm4d.fif create mode 100644 crypto/test/fift/testvm4e.fif create mode 100644 crypto/test/fift/testvm5.fif create mode 100644 crypto/test/fift/testvm6.fif create mode 100644 crypto/test/fift/testvm7.fif create mode 100644 crypto/test/fift/testvm8.fif create mode 100644 crypto/test/fift/testvm9.fif create mode 100644 crypto/test/fift/testvmprog.fif create mode 100644 crypto/test/test-cells.cpp create mode 100644 crypto/test/test-db.cpp create mode 100644 crypto/test/test-ed25519-crypto.cpp create mode 100644 crypto/test/vm.cpp create mode 100644 crypto/tl/boc.tlb create mode 100644 crypto/tl/hashmap.tlb create mode 100644 crypto/tl/tlbc-aux.h create mode 100644 crypto/tl/tlbc-data.h create mode 100644 crypto/tl/tlbc-gen-cpp.cpp create mode 100644 crypto/tl/tlbc-gen-cpp.h create mode 100644 crypto/tl/tlbc.cpp create mode 100644 crypto/tl/tlblib.cpp create mode 100644 crypto/tl/tlblib.hpp create mode 100644 crypto/vm/arithops.cpp create mode 100644 crypto/vm/arithops.h create mode 100644 crypto/vm/atom.cpp create mode 100644 crypto/vm/atom.h create mode 100644 crypto/vm/boc.cpp create mode 100644 crypto/vm/boc.h create mode 100644 crypto/vm/box.hpp create mode 100644 crypto/vm/cellops.cpp create mode 100644 crypto/vm/cellops.h create mode 100644 crypto/vm/cellparse.hpp create mode 100644 crypto/vm/cells.h create mode 100644 crypto/vm/cells/Cell.cpp create mode 100644 crypto/vm/cells/Cell.h create mode 100644 crypto/vm/cells/CellBuilder.cpp create mode 100644 crypto/vm/cells/CellBuilder.h create mode 100644 crypto/vm/cells/CellHash.cpp create mode 100644 crypto/vm/cells/CellHash.h create mode 100644 crypto/vm/cells/CellSlice.cpp create mode 100644 crypto/vm/cells/CellSlice.h create mode 100644 crypto/vm/cells/CellTraits.cpp create mode 100644 crypto/vm/cells/CellTraits.h create mode 100644 crypto/vm/cells/CellUsageTree.cpp create mode 100644 crypto/vm/cells/CellUsageTree.h create mode 100644 crypto/vm/cells/CellWithStorage.h create mode 100644 crypto/vm/cells/DataCell.cpp create mode 100644 crypto/vm/cells/DataCell.h create mode 100644 crypto/vm/cells/ExtCell.h create mode 100644 crypto/vm/cells/LevelMask.cpp create mode 100644 crypto/vm/cells/LevelMask.h create mode 100644 crypto/vm/cells/MerkleProof.cpp create mode 100644 crypto/vm/cells/MerkleProof.h create mode 100644 crypto/vm/cells/MerkleUpdate.cpp create mode 100644 crypto/vm/cells/MerkleUpdate.h create mode 100644 crypto/vm/cells/PrunnedCell.h create mode 100644 crypto/vm/cells/UsageCell.h create mode 100644 crypto/vm/cells/VirtualCell.h create mode 100644 crypto/vm/cells/VirtualizationParameters.h create mode 100644 crypto/vm/cellslice.h create mode 100644 crypto/vm/continuation.cpp create mode 100644 crypto/vm/continuation.h create mode 100644 crypto/vm/contops.cpp create mode 100644 crypto/vm/contops.h create mode 100644 crypto/vm/cp0.cpp create mode 100644 crypto/vm/cp0.h create mode 100644 crypto/vm/db/BlobView.cpp create mode 100644 crypto/vm/db/BlobView.h create mode 100644 crypto/vm/db/CellHashTable.h create mode 100644 crypto/vm/db/CellStorage.cpp create mode 100644 crypto/vm/db/CellStorage.h create mode 100644 crypto/vm/db/DynamicBagOfCellsDb.cpp create mode 100644 crypto/vm/db/DynamicBagOfCellsDb.h create mode 100644 crypto/vm/db/StaticBagOfCellsDb.cpp create mode 100644 crypto/vm/db/StaticBagOfCellsDb.h create mode 100644 crypto/vm/db/TonDb.cpp create mode 100644 crypto/vm/db/TonDb.h create mode 100644 crypto/vm/debugops.cpp create mode 100644 crypto/vm/debugops.h create mode 100644 crypto/vm/dict.cpp create mode 100644 crypto/vm/dict.h create mode 100644 crypto/vm/dictops.cpp create mode 100644 crypto/vm/dictops.h create mode 100644 crypto/vm/dispatch.cpp create mode 100644 crypto/vm/dispatch.h create mode 100644 crypto/vm/excno.hpp create mode 100644 crypto/vm/fmt.hpp create mode 100644 crypto/vm/log.h create mode 100644 crypto/vm/opctable.cpp create mode 100644 crypto/vm/opctable.h create mode 100644 crypto/vm/stack.cpp create mode 100644 crypto/vm/stack.hpp create mode 100644 crypto/vm/stackops.cpp create mode 100644 crypto/vm/stackops.h create mode 100644 crypto/vm/tonops.cpp create mode 100644 crypto/vm/tonops.h create mode 100644 crypto/vm/tupleops.cpp create mode 100644 crypto/vm/tupleops.h create mode 100644 crypto/vm/vmstate.h create mode 100644 dht-server/CMakeLists.txt create mode 100644 dht-server/dht-server.cpp create mode 100644 dht-server/dht-server.hpp create mode 100644 dht/CMakeLists.txt create mode 100644 dht/dht-bucket.cpp create mode 100644 dht/dht-bucket.hpp create mode 100644 dht/dht-in.hpp create mode 100644 dht/dht-node.cpp create mode 100644 dht/dht-node.hpp create mode 100644 dht/dht-query.cpp create mode 100644 dht/dht-query.hpp create mode 100644 dht/dht-remote-node.cpp create mode 100644 dht/dht-remote-node.hpp create mode 100644 dht/dht-types.cpp create mode 100644 dht/dht-types.h create mode 100644 dht/dht.cpp create mode 100644 dht/dht.h create mode 100644 dht/dht.hpp create mode 100644 dht/test/dht-test-ping.cpp create mode 100644 doc/FullNode-HOWTO create mode 100644 doc/LiteClient-HOWTO create mode 100644 doc/Validator-HOWTO create mode 100644 doc/fiftbase.tex create mode 100644 doc/tblkch.tex create mode 100644 doc/ton.tex create mode 100644 doc/tvm.tex create mode 100644 example/android/AddIntDef.php create mode 100644 example/android/CMakeLists.txt create mode 100755 example/android/build-all.sh create mode 100755 example/android/build.sh create mode 100755 example/android/export.sh create mode 100644 example/android/native-lib.cpp create mode 100644 example/android/src/drinkless/org/ton/Client.java create mode 100755 example/android/test.sh create mode 100644 example/android/test/.gitignore create mode 100644 example/android/test/build.gradle create mode 100644 example/android/test/gradle.properties create mode 100644 example/android/test/gradle/wrapper/gradle-wrapper.jar create mode 100644 example/android/test/gradle/wrapper/gradle-wrapper.properties create mode 100755 example/android/test/gradlew create mode 100644 example/android/test/gradlew.bat create mode 100644 example/android/test/settings.gradle create mode 100644 example/android/test/ton/.gitignore create mode 100644 example/android/test/ton/build.gradle create mode 100644 example/android/test/ton/proguard-rules.pro create mode 100644 example/android/test/ton/src/androidTest/java/drinkless/org/ton/TonTest.kt create mode 100644 example/android/test/ton/src/androidTest/java/drinkless/org/ton/TonTestJava.java create mode 100644 example/android/test/ton/src/main/AndroidManifest.xml create mode 100644 example/android/test/ton/src/main/java/drinkless/org/ton/Client.java create mode 100644 example/android/test/ton/src/main/res/values/strings.xml create mode 100644 example/android/test/ton/src/test/java/drinkless/org/ton/ExampleUnitTest.java create mode 100644 example/android/third_party/crypto/arm/include/internal/__DECC_INCLUDE_EPILOGUE.H create mode 100644 example/android/third_party/crypto/arm/include/internal/__DECC_INCLUDE_PROLOGUE.H create mode 100644 example/android/third_party/crypto/arm/include/internal/bio.h create mode 100644 example/android/third_party/crypto/arm/include/internal/comp.h create mode 100644 example/android/third_party/crypto/arm/include/internal/conf.h create mode 100644 example/android/third_party/crypto/arm/include/internal/constant_time_locl.h create mode 100644 example/android/third_party/crypto/arm/include/internal/cryptlib.h create mode 100644 example/android/third_party/crypto/arm/include/internal/dane.h create mode 100644 example/android/third_party/crypto/arm/include/internal/dso.h create mode 100644 example/android/third_party/crypto/arm/include/internal/dsoerr.h create mode 100644 example/android/third_party/crypto/arm/include/internal/err.h create mode 100644 example/android/third_party/crypto/arm/include/internal/nelem.h create mode 100644 example/android/third_party/crypto/arm/include/internal/numbers.h create mode 100644 example/android/third_party/crypto/arm/include/internal/o_dir.h create mode 100644 example/android/third_party/crypto/arm/include/internal/o_str.h create mode 100644 example/android/third_party/crypto/arm/include/internal/refcount.h create mode 100644 example/android/third_party/crypto/arm/include/internal/sockets.h create mode 100644 example/android/third_party/crypto/arm/include/internal/sslconf.h create mode 100644 example/android/third_party/crypto/arm/include/internal/thread_once.h create mode 100644 example/android/third_party/crypto/arm/include/internal/tsan_assist.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/__DECC_INCLUDE_EPILOGUE.H create mode 100644 example/android/third_party/crypto/arm/include/openssl/__DECC_INCLUDE_PROLOGUE.H create mode 100644 example/android/third_party/crypto/arm/include/openssl/aes.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/asn1.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/asn1_mac.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/asn1err.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/asn1t.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/async.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/asyncerr.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/bio.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/bioerr.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/blowfish.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/bn.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/bnerr.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/buffer.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/buffererr.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/camellia.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/cast.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/cmac.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/cms.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/cmserr.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/comp.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/comperr.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/conf.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/conf_api.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/conferr.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/crypto.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/cryptoerr.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/ct.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/cterr.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/des.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/dh.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/dherr.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/dsa.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/dsaerr.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/dtls1.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/e_os2.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/ebcdic.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/ec.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/ecdh.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/ecdsa.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/ecerr.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/engine.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/engineerr.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/err.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/evp.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/evperr.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/hmac.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/idea.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/kdf.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/kdferr.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/lhash.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/md2.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/md4.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/md5.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/mdc2.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/modes.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/obj_mac.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/objects.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/objectserr.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/ocsp.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/ocsperr.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/opensslconf.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/opensslconf.h.in create mode 100644 example/android/third_party/crypto/arm/include/openssl/opensslv.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/ossl_typ.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/pem.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/pem2.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/pemerr.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/pkcs12.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/pkcs12err.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/pkcs7.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/pkcs7err.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/rand.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/rand_drbg.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/randerr.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/rc2.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/rc4.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/rc5.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/ripemd.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/rsa.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/rsaerr.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/safestack.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/seed.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/sha.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/srp.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/srtp.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/ssl.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/ssl2.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/ssl3.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/sslerr.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/stack.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/store.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/storeerr.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/symhacks.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/tls1.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/ts.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/tserr.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/txt_db.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/ui.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/uierr.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/whrlpool.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/x509.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/x509_vfy.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/x509err.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/x509v3.h create mode 100644 example/android/third_party/crypto/arm/include/openssl/x509v3err.h create mode 100644 example/android/third_party/crypto/arm/lib/libcrypto.a create mode 100644 example/android/third_party/crypto/arm/lib/libssl.a create mode 100644 example/android/third_party/crypto/arm64/include/internal/__DECC_INCLUDE_EPILOGUE.H create mode 100644 example/android/third_party/crypto/arm64/include/internal/__DECC_INCLUDE_PROLOGUE.H create mode 100644 example/android/third_party/crypto/arm64/include/internal/bio.h create mode 100644 example/android/third_party/crypto/arm64/include/internal/comp.h create mode 100644 example/android/third_party/crypto/arm64/include/internal/conf.h create mode 100644 example/android/third_party/crypto/arm64/include/internal/constant_time_locl.h create mode 100644 example/android/third_party/crypto/arm64/include/internal/cryptlib.h create mode 100644 example/android/third_party/crypto/arm64/include/internal/dane.h create mode 100644 example/android/third_party/crypto/arm64/include/internal/dso.h create mode 100644 example/android/third_party/crypto/arm64/include/internal/dsoerr.h create mode 100644 example/android/third_party/crypto/arm64/include/internal/err.h create mode 100644 example/android/third_party/crypto/arm64/include/internal/nelem.h create mode 100644 example/android/third_party/crypto/arm64/include/internal/numbers.h create mode 100644 example/android/third_party/crypto/arm64/include/internal/o_dir.h create mode 100644 example/android/third_party/crypto/arm64/include/internal/o_str.h create mode 100644 example/android/third_party/crypto/arm64/include/internal/refcount.h create mode 100644 example/android/third_party/crypto/arm64/include/internal/sockets.h create mode 100644 example/android/third_party/crypto/arm64/include/internal/sslconf.h create mode 100644 example/android/third_party/crypto/arm64/include/internal/thread_once.h create mode 100644 example/android/third_party/crypto/arm64/include/internal/tsan_assist.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/__DECC_INCLUDE_EPILOGUE.H create mode 100644 example/android/third_party/crypto/arm64/include/openssl/__DECC_INCLUDE_PROLOGUE.H create mode 100644 example/android/third_party/crypto/arm64/include/openssl/aes.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/asn1.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/asn1_mac.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/asn1err.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/asn1t.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/async.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/asyncerr.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/bio.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/bioerr.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/blowfish.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/bn.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/bnerr.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/buffer.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/buffererr.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/camellia.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/cast.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/cmac.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/cms.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/cmserr.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/comp.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/comperr.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/conf.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/conf_api.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/conferr.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/crypto.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/cryptoerr.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/ct.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/cterr.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/des.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/dh.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/dherr.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/dsa.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/dsaerr.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/dtls1.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/e_os2.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/ebcdic.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/ec.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/ecdh.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/ecdsa.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/ecerr.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/engine.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/engineerr.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/err.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/evp.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/evperr.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/hmac.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/idea.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/kdf.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/kdferr.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/lhash.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/md2.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/md4.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/md5.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/mdc2.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/modes.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/obj_mac.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/objects.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/objectserr.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/ocsp.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/ocsperr.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/opensslconf.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/opensslconf.h.in create mode 100644 example/android/third_party/crypto/arm64/include/openssl/opensslv.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/ossl_typ.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/pem.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/pem2.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/pemerr.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/pkcs12.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/pkcs12err.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/pkcs7.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/pkcs7err.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/rand.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/rand_drbg.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/randerr.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/rc2.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/rc4.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/rc5.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/ripemd.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/rsa.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/rsaerr.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/safestack.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/seed.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/sha.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/srp.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/srtp.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/ssl.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/ssl2.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/ssl3.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/sslerr.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/stack.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/store.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/storeerr.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/symhacks.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/tls1.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/ts.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/tserr.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/txt_db.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/ui.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/uierr.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/whrlpool.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/x509.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/x509_vfy.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/x509err.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/x509v3.h create mode 100644 example/android/third_party/crypto/arm64/include/openssl/x509v3err.h create mode 100644 example/android/third_party/crypto/arm64/lib/libcrypto.a create mode 100644 example/android/third_party/crypto/arm64/lib/libssl.a create mode 100755 example/android/third_party/crypto/build-all.sh create mode 100755 example/android/third_party/crypto/build.sh create mode 100644 example/android/third_party/crypto/x86/include/internal/__DECC_INCLUDE_EPILOGUE.H create mode 100644 example/android/third_party/crypto/x86/include/internal/__DECC_INCLUDE_PROLOGUE.H create mode 100644 example/android/third_party/crypto/x86/include/internal/bio.h create mode 100644 example/android/third_party/crypto/x86/include/internal/comp.h create mode 100644 example/android/third_party/crypto/x86/include/internal/conf.h create mode 100644 example/android/third_party/crypto/x86/include/internal/constant_time_locl.h create mode 100644 example/android/third_party/crypto/x86/include/internal/cryptlib.h create mode 100644 example/android/third_party/crypto/x86/include/internal/dane.h create mode 100644 example/android/third_party/crypto/x86/include/internal/dso.h create mode 100644 example/android/third_party/crypto/x86/include/internal/dsoerr.h create mode 100644 example/android/third_party/crypto/x86/include/internal/err.h create mode 100644 example/android/third_party/crypto/x86/include/internal/nelem.h create mode 100644 example/android/third_party/crypto/x86/include/internal/numbers.h create mode 100644 example/android/third_party/crypto/x86/include/internal/o_dir.h create mode 100644 example/android/third_party/crypto/x86/include/internal/o_str.h create mode 100644 example/android/third_party/crypto/x86/include/internal/refcount.h create mode 100644 example/android/third_party/crypto/x86/include/internal/sockets.h create mode 100644 example/android/third_party/crypto/x86/include/internal/sslconf.h create mode 100644 example/android/third_party/crypto/x86/include/internal/thread_once.h create mode 100644 example/android/third_party/crypto/x86/include/internal/tsan_assist.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/__DECC_INCLUDE_EPILOGUE.H create mode 100644 example/android/third_party/crypto/x86/include/openssl/__DECC_INCLUDE_PROLOGUE.H create mode 100644 example/android/third_party/crypto/x86/include/openssl/aes.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/asn1.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/asn1_mac.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/asn1err.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/asn1t.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/async.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/asyncerr.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/bio.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/bioerr.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/blowfish.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/bn.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/bnerr.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/buffer.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/buffererr.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/camellia.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/cast.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/cmac.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/cms.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/cmserr.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/comp.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/comperr.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/conf.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/conf_api.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/conferr.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/crypto.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/cryptoerr.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/ct.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/cterr.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/des.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/dh.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/dherr.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/dsa.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/dsaerr.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/dtls1.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/e_os2.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/ebcdic.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/ec.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/ecdh.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/ecdsa.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/ecerr.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/engine.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/engineerr.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/err.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/evp.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/evperr.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/hmac.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/idea.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/kdf.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/kdferr.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/lhash.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/md2.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/md4.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/md5.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/mdc2.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/modes.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/obj_mac.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/objects.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/objectserr.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/ocsp.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/ocsperr.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/opensslconf.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/opensslconf.h.in create mode 100644 example/android/third_party/crypto/x86/include/openssl/opensslv.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/ossl_typ.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/pem.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/pem2.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/pemerr.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/pkcs12.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/pkcs12err.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/pkcs7.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/pkcs7err.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/rand.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/rand_drbg.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/randerr.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/rc2.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/rc4.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/rc5.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/ripemd.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/rsa.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/rsaerr.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/safestack.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/seed.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/sha.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/srp.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/srtp.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/ssl.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/ssl2.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/ssl3.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/sslerr.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/stack.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/store.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/storeerr.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/symhacks.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/tls1.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/ts.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/tserr.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/txt_db.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/ui.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/uierr.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/whrlpool.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/x509.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/x509_vfy.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/x509err.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/x509v3.h create mode 100644 example/android/third_party/crypto/x86/include/openssl/x509v3err.h create mode 100644 example/android/third_party/crypto/x86/lib/libcrypto.a create mode 100644 example/android/third_party/crypto/x86/lib/libssl.a create mode 100644 example/android/third_party/crypto/x86_64/include/internal/__DECC_INCLUDE_EPILOGUE.H create mode 100644 example/android/third_party/crypto/x86_64/include/internal/__DECC_INCLUDE_PROLOGUE.H create mode 100644 example/android/third_party/crypto/x86_64/include/internal/bio.h create mode 100644 example/android/third_party/crypto/x86_64/include/internal/comp.h create mode 100644 example/android/third_party/crypto/x86_64/include/internal/conf.h create mode 100644 example/android/third_party/crypto/x86_64/include/internal/constant_time_locl.h create mode 100644 example/android/third_party/crypto/x86_64/include/internal/cryptlib.h create mode 100644 example/android/third_party/crypto/x86_64/include/internal/dane.h create mode 100644 example/android/third_party/crypto/x86_64/include/internal/dso.h create mode 100644 example/android/third_party/crypto/x86_64/include/internal/dsoerr.h create mode 100644 example/android/third_party/crypto/x86_64/include/internal/err.h create mode 100644 example/android/third_party/crypto/x86_64/include/internal/nelem.h create mode 100644 example/android/third_party/crypto/x86_64/include/internal/numbers.h create mode 100644 example/android/third_party/crypto/x86_64/include/internal/o_dir.h create mode 100644 example/android/third_party/crypto/x86_64/include/internal/o_str.h create mode 100644 example/android/third_party/crypto/x86_64/include/internal/refcount.h create mode 100644 example/android/third_party/crypto/x86_64/include/internal/sockets.h create mode 100644 example/android/third_party/crypto/x86_64/include/internal/sslconf.h create mode 100644 example/android/third_party/crypto/x86_64/include/internal/thread_once.h create mode 100644 example/android/third_party/crypto/x86_64/include/internal/tsan_assist.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/__DECC_INCLUDE_EPILOGUE.H create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/__DECC_INCLUDE_PROLOGUE.H create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/aes.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/asn1.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/asn1_mac.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/asn1err.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/asn1t.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/async.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/asyncerr.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/bio.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/bioerr.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/blowfish.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/bn.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/bnerr.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/buffer.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/buffererr.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/camellia.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/cast.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/cmac.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/cms.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/cmserr.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/comp.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/comperr.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/conf.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/conf_api.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/conferr.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/crypto.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/cryptoerr.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/ct.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/cterr.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/des.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/dh.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/dherr.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/dsa.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/dsaerr.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/dtls1.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/e_os2.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/ebcdic.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/ec.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/ecdh.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/ecdsa.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/ecerr.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/engine.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/engineerr.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/err.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/evp.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/evperr.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/hmac.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/idea.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/kdf.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/kdferr.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/lhash.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/md2.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/md4.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/md5.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/mdc2.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/modes.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/obj_mac.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/objects.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/objectserr.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/ocsp.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/ocsperr.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/opensslconf.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/opensslconf.h.in create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/opensslv.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/ossl_typ.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/pem.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/pem2.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/pemerr.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/pkcs12.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/pkcs12err.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/pkcs7.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/pkcs7err.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/rand.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/rand_drbg.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/randerr.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/rc2.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/rc4.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/rc5.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/ripemd.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/rsa.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/rsaerr.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/safestack.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/seed.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/sha.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/srp.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/srtp.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/ssl.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/ssl2.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/ssl3.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/sslerr.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/stack.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/store.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/storeerr.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/symhacks.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/tls1.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/ts.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/tserr.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/txt_db.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/ui.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/uierr.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/whrlpool.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/x509.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/x509_vfy.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/x509err.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/x509v3.h create mode 100644 example/android/third_party/crypto/x86_64/include/openssl/x509v3err.h create mode 100644 example/android/third_party/crypto/x86_64/lib/libcrypto.a create mode 100644 example/android/third_party/crypto/x86_64/lib/libssl.a create mode 100644 example/cpp/CMakeLists.txt create mode 100644 example/cpp/README.md create mode 100644 example/cpp/tonjson_example.cpp create mode 100644 fec/CMakeLists.txt create mode 100644 fec/fec.cpp create mode 100644 fec/fec.h create mode 100755 install-scripts/generate-config.sh create mode 100644 install-scripts/known_hosts create mode 100755 install-scripts/prepare.sh create mode 100644 keyring/CMakeLists.txt create mode 100644 keyring/keyring.cpp create mode 100644 keyring/keyring.h create mode 100644 keyring/keyring.hpp create mode 100644 keys/CMakeLists.txt create mode 100644 keys/encryptor.cpp create mode 100644 keys/encryptor.h create mode 100644 keys/encryptor.hpp create mode 100644 keys/keys.cpp create mode 100644 keys/keys.hpp create mode 100644 lite-client-docs/DISCLAIMER create mode 100644 lite-client-docs/README create mode 100644 lite-client/CMakeLists.txt create mode 100644 lite-client/lite-client.cpp create mode 100644 lite-client/lite-client.h create mode 100644 memprof/CMakeLists.txt create mode 100644 memprof/memprof/memprof.cpp create mode 100644 memprof/memprof/memprof.h create mode 100644 overlay/CMakeLists.txt create mode 100644 overlay/overlay-broadcast.cpp create mode 100644 overlay/overlay-broadcast.hpp create mode 100644 overlay/overlay-fec-broadcast.cpp create mode 100644 overlay/overlay-fec-broadcast.hpp create mode 100644 overlay/overlay-fec.cpp create mode 100644 overlay/overlay-fec.hpp create mode 100644 overlay/overlay-id.hpp create mode 100644 overlay/overlay-manager.cpp create mode 100644 overlay/overlay-manager.h create mode 100644 overlay/overlay-peers.cpp create mode 100644 overlay/overlay.cpp create mode 100644 overlay/overlay.h create mode 100644 overlay/overlay.hpp create mode 100644 overlay/overlays.h create mode 100644 rldp/CMakeLists.txt create mode 100644 rldp/rldp-in.hpp create mode 100644 rldp/rldp-peer.cpp create mode 100644 rldp/rldp-peer.h create mode 100644 rldp/rldp-peer.hpp create mode 100644 rldp/rldp.cpp create mode 100644 rldp/rldp.h create mode 100644 rldp/rldp.hpp create mode 100755 run-clang-format.sh create mode 100644 tdactor/.gitrepo create mode 100644 tdactor/CMakeLists.txt create mode 100644 tdactor/benchmark/CMakeLists.txt create mode 100644 tdactor/benchmark/benchmark.cpp create mode 100644 tdactor/benchmark/third_party/FAAArrayQueue.h create mode 100644 tdactor/benchmark/third_party/HazardPointers.h create mode 100644 tdactor/benchmark/third_party/LCRQueue.h create mode 100644 tdactor/benchmark/third_party/LazyIndexArrayQueue.h create mode 100644 tdactor/benchmark/third_party/MoodyCamelQueue.h create mode 100644 tdactor/benchmark/third_party/mp-queue.c create mode 100644 tdactor/benchmark/third_party/mp-queue.h create mode 100644 tdactor/td/actor/ActorId.h create mode 100644 tdactor/td/actor/ActorOwn.h create mode 100644 tdactor/td/actor/ActorShared.h create mode 100644 tdactor/td/actor/MultiPromise.cpp create mode 100644 tdactor/td/actor/MultiPromise.h create mode 100644 tdactor/td/actor/PromiseFuture.h create mode 100644 tdactor/td/actor/actor.h create mode 100644 tdactor/td/actor/common.h create mode 100644 tdactor/td/actor/core/Actor.h create mode 100644 tdactor/td/actor/core/ActorExecuteContext.h create mode 100644 tdactor/td/actor/core/ActorExecutor.cpp create mode 100644 tdactor/td/actor/core/ActorExecutor.h create mode 100644 tdactor/td/actor/core/ActorInfo.h create mode 100644 tdactor/td/actor/core/ActorInfoCreator.h create mode 100644 tdactor/td/actor/core/ActorLocker.h create mode 100644 tdactor/td/actor/core/ActorMailbox.h create mode 100644 tdactor/td/actor/core/ActorMessage.h create mode 100644 tdactor/td/actor/core/ActorSignals.h create mode 100644 tdactor/td/actor/core/ActorState.h create mode 100644 tdactor/td/actor/core/Context.h create mode 100644 tdactor/td/actor/core/CpuWorker.cpp create mode 100644 tdactor/td/actor/core/CpuWorker.h create mode 100644 tdactor/td/actor/core/IoWorker.cpp create mode 100644 tdactor/td/actor/core/IoWorker.h create mode 100644 tdactor/td/actor/core/Scheduler.cpp create mode 100644 tdactor/td/actor/core/Scheduler.h create mode 100644 tdactor/td/actor/core/SchedulerContext.h create mode 100644 tdactor/td/actor/core/SchedulerId.h create mode 100644 tdactor/td/actor/core/SchedulerMessage.h create mode 100644 tdactor/test/actors_core.cpp create mode 100644 tdactor/test/actors_promise.cpp create mode 100644 tddb/CMakeLists.txt create mode 100644 tddb/td/db/KeyValue.h create mode 100644 tddb/td/db/KeyValueAsync.h create mode 100644 tddb/td/db/MemoryKeyValue.cpp create mode 100644 tddb/td/db/MemoryKeyValue.h create mode 100644 tddb/td/db/RocksDb.cpp create mode 100644 tddb/td/db/RocksDb.h create mode 100644 tddb/td/db/binlog/Binlog.cpp create mode 100644 tddb/td/db/binlog/Binlog.h create mode 100644 tddb/td/db/binlog/BinlogReaderHelper.cpp create mode 100644 tddb/td/db/binlog/BinlogReaderHelper.h create mode 100644 tddb/td/db/binlog/BinlogReaderInterface.h create mode 100644 tddb/td/db/utils/ChainBuffer.cpp create mode 100644 tddb/td/db/utils/ChainBuffer.h create mode 100644 tddb/td/db/utils/CyclicBuffer.cpp create mode 100644 tddb/td/db/utils/CyclicBuffer.h create mode 100644 tddb/td/db/utils/FileSyncState.cpp create mode 100644 tddb/td/db/utils/FileSyncState.h create mode 100644 tddb/td/db/utils/FileToStreamActor.cpp create mode 100644 tddb/td/db/utils/FileToStreamActor.h create mode 100644 tddb/td/db/utils/StreamInterface.cpp create mode 100644 tddb/td/db/utils/StreamInterface.h create mode 100644 tddb/td/db/utils/StreamToFileActor.cpp create mode 100644 tddb/td/db/utils/StreamToFileActor.h create mode 100644 tddb/test/binlog.cpp create mode 100644 tddb/test/io-bench.cpp create mode 100644 tddb/test/key_value.cpp create mode 100644 tdfec/CMakeLists.txt create mode 100644 tdfec/benchmark/CMakeLists.txt create mode 100644 tdfec/benchmark/benchmark.cpp create mode 100644 tdfec/td/fec/algebra/BeliefPropagationDecoding.cpp create mode 100644 tdfec/td/fec/algebra/BeliefPropagationDecoding.h create mode 100644 tdfec/td/fec/algebra/GaussianElimination.cpp create mode 100644 tdfec/td/fec/algebra/GaussianElimination.h create mode 100644 tdfec/td/fec/algebra/InactivationDecoding.cpp create mode 100644 tdfec/td/fec/algebra/InactivationDecoding.h create mode 100644 tdfec/td/fec/algebra/MatrixGF2.cpp create mode 100644 tdfec/td/fec/algebra/MatrixGF2.h create mode 100644 tdfec/td/fec/algebra/MatrixGF256.cpp create mode 100644 tdfec/td/fec/algebra/MatrixGF256.h create mode 100644 tdfec/td/fec/algebra/Octet.cpp create mode 100644 tdfec/td/fec/algebra/Octet.h create mode 100644 tdfec/td/fec/algebra/Simd.h create mode 100644 tdfec/td/fec/algebra/SparseMatrixGF2.cpp create mode 100644 tdfec/td/fec/algebra/SparseMatrixGF2.h create mode 100644 tdfec/td/fec/common/SymbolRef.h create mode 100644 tdfec/td/fec/common/SymbolsView.cpp create mode 100644 tdfec/td/fec/common/SymbolsView.h create mode 100644 tdfec/td/fec/fec.cpp create mode 100644 tdfec/td/fec/fec.h create mode 100644 tdfec/td/fec/online/Decoder.cpp create mode 100644 tdfec/td/fec/online/Decoder.h create mode 100644 tdfec/td/fec/online/Encoder.cpp create mode 100644 tdfec/td/fec/online/Encoder.h create mode 100644 tdfec/td/fec/online/Rfc.cpp create mode 100644 tdfec/td/fec/online/Rfc.h create mode 100644 tdfec/td/fec/raptorq/Decoder.cpp create mode 100644 tdfec/td/fec/raptorq/Decoder.h create mode 100644 tdfec/td/fec/raptorq/Encoder.cpp create mode 100644 tdfec/td/fec/raptorq/Encoder.h create mode 100644 tdfec/td/fec/raptorq/RawEncoder.cpp create mode 100644 tdfec/td/fec/raptorq/RawEncoder.h create mode 100644 tdfec/td/fec/raptorq/Rfc.cpp create mode 100644 tdfec/td/fec/raptorq/Rfc.h create mode 100644 tdfec/td/fec/raptorq/Solver.cpp create mode 100644 tdfec/td/fec/raptorq/Solver.h create mode 100644 tdfec/test/LibRaptorQ.cpp create mode 100644 tdfec/test/LibRaptorQ.h create mode 100644 tdfec/test/fec-test.cpp create mode 100644 tdnet/CMakeLists.txt create mode 100644 tdnet/example/tcp_ping_pong.cpp create mode 100644 tdnet/example/udp_ping_pong.cpp create mode 100644 tdnet/td/net/FdListener.cpp create mode 100644 tdnet/td/net/FdListener.h create mode 100644 tdnet/td/net/TcpListener.cpp create mode 100644 tdnet/td/net/TcpListener.h create mode 100644 tdnet/td/net/UdpServer.cpp create mode 100644 tdnet/td/net/UdpServer.h create mode 100644 tdnet/test/net-test.cpp create mode 100644 tdtl/.gitrepo create mode 100644 tdtl/CMakeLists.txt create mode 100644 tdtl/td/tl/tl_config.cpp create mode 100644 tdtl/td/tl/tl_config.h create mode 100644 tdtl/td/tl/tl_core.cpp create mode 100644 tdtl/td/tl/tl_core.h create mode 100644 tdtl/td/tl/tl_file_outputer.cpp create mode 100644 tdtl/td/tl/tl_file_outputer.h create mode 100644 tdtl/td/tl/tl_file_utils.cpp create mode 100644 tdtl/td/tl/tl_file_utils.h create mode 100644 tdtl/td/tl/tl_generate.cpp create mode 100644 tdtl/td/tl/tl_generate.h create mode 100644 tdtl/td/tl/tl_outputer.cpp create mode 100644 tdtl/td/tl/tl_outputer.h create mode 100644 tdtl/td/tl/tl_simple.h create mode 100644 tdtl/td/tl/tl_simple_parser.h create mode 100644 tdtl/td/tl/tl_string_outputer.cpp create mode 100644 tdtl/td/tl/tl_string_outputer.h create mode 100644 tdtl/td/tl/tl_writer.cpp create mode 100644 tdtl/td/tl/tl_writer.h create mode 100644 tdutils/.gitrepo create mode 100644 tdutils/CMakeLists.txt create mode 100644 tdutils/generate/CMakeLists.txt create mode 100644 tdutils/generate/generate_mime_types_gperf.cpp create mode 100644 tdutils/generate/mime_types.txt create mode 100644 tdutils/td/utils/AesCtrByteFlow.h create mode 100644 tdutils/td/utils/BigNum.cpp create mode 100644 tdutils/td/utils/BigNum.h create mode 100644 tdutils/td/utils/BufferedFd.h create mode 100644 tdutils/td/utils/BufferedReader.h create mode 100644 tdutils/td/utils/BufferedUdp.cpp create mode 100644 tdutils/td/utils/BufferedUdp.h create mode 100644 tdutils/td/utils/ByteFlow.h create mode 100644 tdutils/td/utils/CancellationToken.h create mode 100644 tdutils/td/utils/ChangesProcessor.h create mode 100644 tdutils/td/utils/Closure.h create mode 100644 tdutils/td/utils/ConcurrentHashTable.h create mode 100644 tdutils/td/utils/Container.h create mode 100644 tdutils/td/utils/Context.h create mode 100644 tdutils/td/utils/DecTree.h create mode 100644 tdutils/td/utils/Destructor.h create mode 100644 tdutils/td/utils/Enumerator.h create mode 100644 tdutils/td/utils/EpochBasedMemoryReclamation.h create mode 100644 tdutils/td/utils/FileLog.cpp create mode 100644 tdutils/td/utils/FileLog.h create mode 100644 tdutils/td/utils/FloodControlFast.h create mode 100644 tdutils/td/utils/FloodControlStrict.h create mode 100644 tdutils/td/utils/GitInfo.cpp create mode 100644 tdutils/td/utils/GitInfo.h create mode 100644 tdutils/td/utils/Gzip.cpp create mode 100644 tdutils/td/utils/Gzip.h create mode 100644 tdutils/td/utils/GzipByteFlow.cpp create mode 100644 tdutils/td/utils/GzipByteFlow.h create mode 100644 tdutils/td/utils/Hash.h create mode 100644 tdutils/td/utils/HashMap.h create mode 100644 tdutils/td/utils/HashSet.h create mode 100644 tdutils/td/utils/HazardPointers.h create mode 100644 tdutils/td/utils/Heap.h create mode 100644 tdutils/td/utils/Hints.cpp create mode 100644 tdutils/td/utils/Hints.h create mode 100644 tdutils/td/utils/HttpUrl.cpp create mode 100644 tdutils/td/utils/HttpUrl.h create mode 100644 tdutils/td/utils/JsonBuilder.cpp create mode 100644 tdutils/td/utils/JsonBuilder.h create mode 100644 tdutils/td/utils/List.h create mode 100644 tdutils/td/utils/MemoryLog.h create mode 100644 tdutils/td/utils/MimeType.cpp create mode 100644 tdutils/td/utils/MimeType.h create mode 100644 tdutils/td/utils/MovableValue.h create mode 100644 tdutils/td/utils/MpmcQueue.cpp create mode 100644 tdutils/td/utils/MpmcQueue.h create mode 100644 tdutils/td/utils/MpmcWaiter.h create mode 100644 tdutils/td/utils/MpscLinkQueue.h create mode 100644 tdutils/td/utils/MpscPollableQueue.h create mode 100644 tdutils/td/utils/Named.h create mode 100644 tdutils/td/utils/ObjectPool.h create mode 100644 tdutils/td/utils/Observer.h create mode 100644 tdutils/td/utils/OptionsParser.cpp create mode 100644 tdutils/td/utils/OptionsParser.h create mode 100644 tdutils/td/utils/OrderedEventsProcessor.h create mode 100644 tdutils/td/utils/Parser.h create mode 100644 tdutils/td/utils/PathView.h create mode 100644 tdutils/td/utils/Random.cpp create mode 100644 tdutils/td/utils/Random.h create mode 100644 tdutils/td/utils/ScopeGuard.h create mode 100644 tdutils/td/utils/SharedObjectPool.h create mode 100644 tdutils/td/utils/SharedSlice.cpp create mode 100644 tdutils/td/utils/SharedSlice.h create mode 100644 tdutils/td/utils/Slice-decl.h create mode 100644 tdutils/td/utils/Slice.cpp create mode 100644 tdutils/td/utils/Slice.h create mode 100644 tdutils/td/utils/Span.h create mode 100644 tdutils/td/utils/SpinLock.h create mode 100644 tdutils/td/utils/StackAllocator.cpp create mode 100644 tdutils/td/utils/StackAllocator.h create mode 100644 tdutils/td/utils/Status.cpp create mode 100644 tdutils/td/utils/Status.h create mode 100644 tdutils/td/utils/Storer.h create mode 100644 tdutils/td/utils/StorerBase.h create mode 100644 tdutils/td/utils/StringBuilder.cpp create mode 100644 tdutils/td/utils/StringBuilder.h create mode 100644 tdutils/td/utils/ThreadLocalStorage.h create mode 100644 tdutils/td/utils/ThreadSafeCounter.h create mode 100644 tdutils/td/utils/Time.cpp create mode 100644 tdutils/td/utils/Time.h create mode 100644 tdutils/td/utils/TimedStat.h create mode 100644 tdutils/td/utils/Timer.cpp create mode 100644 tdutils/td/utils/Timer.h create mode 100644 tdutils/td/utils/TsFileLog.cpp create mode 100644 tdutils/td/utils/TsFileLog.h create mode 100644 tdutils/td/utils/UInt.h create mode 100644 tdutils/td/utils/Variant.h create mode 100644 tdutils/td/utils/VectorQueue.h create mode 100644 tdutils/td/utils/as.h create mode 100644 tdutils/td/utils/base64.cpp create mode 100644 tdutils/td/utils/base64.h create mode 100644 tdutils/td/utils/benchmark.h create mode 100644 tdutils/td/utils/bits.h create mode 100644 tdutils/td/utils/buffer.cpp create mode 100644 tdutils/td/utils/buffer.h create mode 100644 tdutils/td/utils/check.cpp create mode 100644 tdutils/td/utils/check.h create mode 100644 tdutils/td/utils/common.h create mode 100644 tdutils/td/utils/config.h.in create mode 100644 tdutils/td/utils/crypto.cpp create mode 100644 tdutils/td/utils/crypto.h create mode 100644 tdutils/td/utils/filesystem.cpp create mode 100644 tdutils/td/utils/filesystem.h create mode 100644 tdutils/td/utils/find_boundary.cpp create mode 100644 tdutils/td/utils/find_boundary.h create mode 100644 tdutils/td/utils/format.h create mode 100644 tdutils/td/utils/int_types.h create mode 100644 tdutils/td/utils/invoke.h create mode 100644 tdutils/td/utils/logging.cpp create mode 100644 tdutils/td/utils/logging.h create mode 100644 tdutils/td/utils/misc.cpp create mode 100644 tdutils/td/utils/misc.h create mode 100644 tdutils/td/utils/optional.h create mode 100644 tdutils/td/utils/overloaded.h create mode 100644 tdutils/td/utils/port/Clocks.cpp create mode 100644 tdutils/td/utils/port/Clocks.h create mode 100644 tdutils/td/utils/port/CxCli.h create mode 100644 tdutils/td/utils/port/EventFd.h create mode 100644 tdutils/td/utils/port/EventFdBase.h create mode 100644 tdutils/td/utils/port/FileFd.cpp create mode 100644 tdutils/td/utils/port/FileFd.h create mode 100644 tdutils/td/utils/port/IPAddress.cpp create mode 100644 tdutils/td/utils/port/IPAddress.h create mode 100644 tdutils/td/utils/port/IoSlice.h create mode 100644 tdutils/td/utils/port/MemoryMapping.cpp create mode 100644 tdutils/td/utils/port/MemoryMapping.h create mode 100644 tdutils/td/utils/port/Poll.h create mode 100644 tdutils/td/utils/port/PollBase.h create mode 100644 tdutils/td/utils/port/PollFlags.cpp create mode 100644 tdutils/td/utils/port/PollFlags.h create mode 100644 tdutils/td/utils/port/RwMutex.h create mode 100644 tdutils/td/utils/port/ServerSocketFd.cpp create mode 100644 tdutils/td/utils/port/ServerSocketFd.h create mode 100644 tdutils/td/utils/port/SocketFd.cpp create mode 100644 tdutils/td/utils/port/SocketFd.h create mode 100644 tdutils/td/utils/port/Stat.cpp create mode 100644 tdutils/td/utils/port/Stat.h create mode 100644 tdutils/td/utils/port/StdStreams.cpp create mode 100644 tdutils/td/utils/port/StdStreams.h create mode 100644 tdutils/td/utils/port/UdpSocketFd.cpp create mode 100644 tdutils/td/utils/port/UdpSocketFd.h create mode 100644 tdutils/td/utils/port/config.h create mode 100644 tdutils/td/utils/port/detail/Epoll.cpp create mode 100644 tdutils/td/utils/port/detail/Epoll.h create mode 100644 tdutils/td/utils/port/detail/EventFdBsd.cpp create mode 100644 tdutils/td/utils/port/detail/EventFdBsd.h create mode 100644 tdutils/td/utils/port/detail/EventFdLinux.cpp create mode 100644 tdutils/td/utils/port/detail/EventFdLinux.h create mode 100644 tdutils/td/utils/port/detail/EventFdWindows.cpp create mode 100644 tdutils/td/utils/port/detail/EventFdWindows.h create mode 100644 tdutils/td/utils/port/detail/Iocp.cpp create mode 100644 tdutils/td/utils/port/detail/Iocp.h create mode 100644 tdutils/td/utils/port/detail/KQueue.cpp create mode 100644 tdutils/td/utils/port/detail/KQueue.h create mode 100644 tdutils/td/utils/port/detail/NativeFd.cpp create mode 100644 tdutils/td/utils/port/detail/NativeFd.h create mode 100644 tdutils/td/utils/port/detail/Poll.cpp create mode 100644 tdutils/td/utils/port/detail/Poll.h create mode 100644 tdutils/td/utils/port/detail/PollableFd.h create mode 100644 tdutils/td/utils/port/detail/Select.cpp create mode 100644 tdutils/td/utils/port/detail/Select.h create mode 100644 tdutils/td/utils/port/detail/ThreadIdGuard.cpp create mode 100644 tdutils/td/utils/port/detail/ThreadIdGuard.h create mode 100644 tdutils/td/utils/port/detail/ThreadPthread.h create mode 100644 tdutils/td/utils/port/detail/ThreadStl.h create mode 100644 tdutils/td/utils/port/detail/WineventPoll.cpp create mode 100644 tdutils/td/utils/port/detail/WineventPoll.h create mode 100644 tdutils/td/utils/port/path.cpp create mode 100644 tdutils/td/utils/port/path.h create mode 100644 tdutils/td/utils/port/platform.h create mode 100644 tdutils/td/utils/port/signals.cpp create mode 100644 tdutils/td/utils/port/signals.h create mode 100644 tdutils/td/utils/port/sleep.cpp create mode 100644 tdutils/td/utils/port/sleep.h create mode 100644 tdutils/td/utils/port/stacktrace.cpp create mode 100644 tdutils/td/utils/port/stacktrace.h create mode 100644 tdutils/td/utils/port/thread.h create mode 100644 tdutils/td/utils/port/thread_local.cpp create mode 100644 tdutils/td/utils/port/thread_local.h create mode 100644 tdutils/td/utils/port/user.cpp create mode 100644 tdutils/td/utils/port/user.h create mode 100644 tdutils/td/utils/port/wstring_convert.cpp create mode 100644 tdutils/td/utils/port/wstring_convert.h create mode 100644 tdutils/td/utils/queue.h create mode 100644 tdutils/td/utils/tests.cpp create mode 100644 tdutils/td/utils/tests.h create mode 100644 tdutils/td/utils/tl_helpers.h create mode 100644 tdutils/td/utils/tl_parsers.cpp create mode 100644 tdutils/td/utils/tl_parsers.h create mode 100644 tdutils/td/utils/tl_storers.h create mode 100644 tdutils/td/utils/translit.cpp create mode 100644 tdutils/td/utils/translit.h create mode 100644 tdutils/td/utils/type_traits.h create mode 100644 tdutils/td/utils/uint128.h create mode 100644 tdutils/td/utils/unicode.cpp create mode 100644 tdutils/td/utils/unicode.h create mode 100644 tdutils/td/utils/unique_ptr.h create mode 100644 tdutils/td/utils/utf8.cpp create mode 100644 tdutils/td/utils/utf8.h create mode 100644 tdutils/test/ConcurrentHashMap.cpp create mode 100644 tdutils/test/Enumerator.cpp create mode 100644 tdutils/test/EpochBasedMemoryReclamation.cpp create mode 100644 tdutils/test/HazardPointers.cpp create mode 100644 tdutils/test/MpmcQueue.cpp create mode 100644 tdutils/test/MpmcWaiter.cpp create mode 100644 tdutils/test/MpscLinkQueue.cpp create mode 100644 tdutils/test/OrderedEventsProcessor.cpp create mode 100644 tdutils/test/SharedObjectPool.cpp create mode 100644 tdutils/test/SharedSlice.cpp create mode 100644 tdutils/test/buffer.cpp create mode 100644 tdutils/test/crypto.cpp create mode 100644 tdutils/test/filesystem.cpp create mode 100644 tdutils/test/gzip.cpp create mode 100644 tdutils/test/heap.cpp create mode 100644 tdutils/test/json.cpp create mode 100644 tdutils/test/log.cpp create mode 100644 tdutils/test/misc.cpp create mode 100644 tdutils/test/port.cpp create mode 100644 tdutils/test/pq.cpp create mode 100644 tdutils/test/variant.cpp create mode 100644 terminal/CMakeLists.txt create mode 100644 terminal/terminal.cpp create mode 100644 terminal/terminal.h create mode 100644 terminal/terminal.hpp create mode 100644 test/ed25519_crypto.cpp create mode 100644 test/generate-test-node-config.py create mode 100644 test/regression-tests.ans create mode 100644 test/test-adnl.cpp create mode 100644 test/test-catchain.cpp create mode 100644 test/test-dht.cpp create mode 100644 test/test-dummy-0-lite-client.cpp create mode 100644 test/test-ext-client.cpp create mode 100644 test/test-ext-server.cpp create mode 100644 test/test-hello-world.cpp create mode 100644 test/test-node.cpp create mode 100644 test/test-rldp.cpp create mode 100644 test/test-td-main.cpp create mode 100644 test/test-ton-collator.cpp create mode 100644 test/test-ton-dummy-0-disk.cpp create mode 100644 test/test-ton-dummy-0.cpp create mode 100644 test/test-validator-session-state.cpp create mode 100644 test/test-validator-session.cpp create mode 100644 test/test-validator.cpp create mode 160000 third-party/abseil-cpp create mode 160000 third-party/crc32c create mode 160000 third-party/libraptorq create mode 160000 third-party/rocksdb create mode 100644 third-party/wingetopt/.gitignore create mode 100644 third-party/wingetopt/.gitrepo create mode 100644 third-party/wingetopt/CMakeLists.txt create mode 100644 third-party/wingetopt/LICENSE create mode 100644 third-party/wingetopt/README.md create mode 100644 third-party/wingetopt/src/getopt.c create mode 100644 third-party/wingetopt/src/getopt.h create mode 100644 tl-utils/CMakeLists.txt create mode 100644 tl-utils/common-utils.hpp create mode 100644 tl-utils/lite-utils.cpp create mode 100644 tl-utils/lite-utils.hpp create mode 100644 tl-utils/tl-utils.cpp create mode 100644 tl-utils/tl-utils.hpp create mode 100644 tl/CMakeLists.txt create mode 100644 tl/generate/CMakeLists.txt create mode 100644 tl/generate/JavadocTlDocumentationGenerator.php create mode 100644 tl/generate/TlDocumentationGenerator.php create mode 100644 tl/generate/generate_common.cpp create mode 100644 tl/generate/generate_java.cpp create mode 100644 tl/generate/scheme/lite_api.tl create mode 100644 tl/generate/scheme/lite_api.tlo create mode 100644 tl/generate/scheme/ton_api.tl create mode 100644 tl/generate/scheme/ton_api.tlo create mode 100644 tl/generate/scheme/tonlib_api.tl create mode 100644 tl/generate/scheme/tonlib_api.tlo create mode 100755 tl/generate/scheme/update-tlo.sh create mode 100644 tl/generate/tl_json_converter.cpp create mode 100644 tl/generate/tl_json_converter.h create mode 100644 tl/generate/tl_writer_cpp.cpp create mode 100644 tl/generate/tl_writer_cpp.h create mode 100644 tl/generate/tl_writer_h.cpp create mode 100644 tl/generate/tl_writer_h.h create mode 100644 tl/generate/tl_writer_hpp.cpp create mode 100644 tl/generate/tl_writer_hpp.h create mode 100644 tl/generate/tl_writer_java.cpp create mode 100644 tl/generate/tl_writer_java.h create mode 100644 tl/generate/tl_writer_jni_cpp.cpp create mode 100644 tl/generate/tl_writer_jni_cpp.h create mode 100644 tl/generate/tl_writer_jni_h.cpp create mode 100644 tl/generate/tl_writer_jni_h.h create mode 100644 tl/generate/tl_writer_td.cpp create mode 100644 tl/generate/tl_writer_td.h create mode 100644 tl/tl/TlObject.h create mode 100644 tl/tl/tl_jni_object.cpp create mode 100644 tl/tl/tl_jni_object.h create mode 100644 tl/tl/tl_json.h create mode 100644 tl/tl/tl_object_parse.h create mode 100644 tl/tl/tl_object_store.h create mode 100644 ton/lite-tl.hpp create mode 100644 ton/ton-io.hpp create mode 100644 ton/ton-shard.h create mode 100644 ton/ton-tl.hpp create mode 100644 ton/ton-types.h create mode 100644 tonlib/CMakeLists.txt create mode 100644 tonlib/test/offline.cpp create mode 100644 tonlib/test/online.cpp create mode 100644 tonlib/tonlib/Client.cpp create mode 100644 tonlib/tonlib/Client.h create mode 100644 tonlib/tonlib/ClientActor.cpp create mode 100644 tonlib/tonlib/ClientActor.h create mode 100644 tonlib/tonlib/ClientJson.cpp create mode 100644 tonlib/tonlib/ClientJson.h create mode 100644 tonlib/tonlib/Config.cpp create mode 100644 tonlib/tonlib/Config.h create mode 100644 tonlib/tonlib/ExtClient.cpp create mode 100644 tonlib/tonlib/ExtClient.h create mode 100644 tonlib/tonlib/ExtClientLazy.cpp create mode 100644 tonlib/tonlib/ExtClientLazy.h create mode 100644 tonlib/tonlib/GenericAccount.cpp create mode 100644 tonlib/tonlib/GenericAccount.h create mode 100644 tonlib/tonlib/KeyStorage.cpp create mode 100644 tonlib/tonlib/KeyStorage.h create mode 100644 tonlib/tonlib/LastBlock.cpp create mode 100644 tonlib/tonlib/LastBlock.h create mode 100644 tonlib/tonlib/TestGiver.cpp create mode 100644 tonlib/tonlib/TestGiver.h create mode 100644 tonlib/tonlib/TestWallet.cpp create mode 100644 tonlib/tonlib/TestWallet.h create mode 100644 tonlib/tonlib/TonlibCallback.h create mode 100644 tonlib/tonlib/TonlibClient.cpp create mode 100644 tonlib/tonlib/TonlibClient.h create mode 100644 tonlib/tonlib/keys/DecryptedKey.cpp create mode 100644 tonlib/tonlib/keys/DecryptedKey.h create mode 100644 tonlib/tonlib/keys/EncryptedKey.cpp create mode 100644 tonlib/tonlib/keys/EncryptedKey.h create mode 100644 tonlib/tonlib/keys/Mnemonic.cpp create mode 100644 tonlib/tonlib/keys/Mnemonic.h create mode 100644 tonlib/tonlib/keys/SimpleEncryption.cpp create mode 100644 tonlib/tonlib/keys/SimpleEncryption.h create mode 100644 tonlib/tonlib/keys/bip39.cpp create mode 100644 tonlib/tonlib/keys/bip39.h create mode 100644 tonlib/tonlib/tonlib_client_json.cpp create mode 100644 tonlib/tonlib/tonlib_client_json.h create mode 100644 tonlib/tonlib/utils.cpp create mode 100644 tonlib/tonlib/utils.h create mode 100644 tonlib/tonlibclientjson_export_list create mode 100644 utils/CMakeLists.txt create mode 100644 utils/generate-random-id.cpp create mode 100644 utils/json2tlo.cpp create mode 100644 validator-engine-console/CMakeLists.txt create mode 100644 validator-engine-console/validator-engine-console-query.cpp create mode 100644 validator-engine-console/validator-engine-console-query.h create mode 100644 validator-engine-console/validator-engine-console.cpp create mode 100644 validator-engine-console/validator-engine-console.h create mode 100644 validator-engine/CMakeLists.txt create mode 100644 validator-engine/validator-engine.cpp create mode 100644 validator-engine/validator-engine.hpp create mode 100644 validator-session/CMakeLists.txt create mode 100644 validator-session/persistent-vector.cpp create mode 100644 validator-session/persistent-vector.h create mode 100644 validator-session/validator-session-description.cpp create mode 100644 validator-session/validator-session-description.h create mode 100644 validator-session/validator-session-description.hpp create mode 100644 validator-session/validator-session-state.cpp create mode 100644 validator-session/validator-session-state.h create mode 100644 validator-session/validator-session-types.h create mode 100644 validator-session/validator-session.cpp create mode 100644 validator-session/validator-session.h create mode 100644 validator-session/validator-session.hpp create mode 100644 validator/CMakeLists.txt create mode 100644 validator/apply-block.cpp create mode 100644 validator/apply-block.hpp create mode 100644 validator/block-handle.cpp create mode 100644 validator/block-handle.hpp create mode 100644 validator/db/archiver.cpp create mode 100644 validator/db/archiver.hpp create mode 100644 validator/db/blockdb.cpp create mode 100644 validator/db/blockdb.hpp create mode 100644 validator/db/celldb.cpp create mode 100644 validator/db/celldb.hpp create mode 100644 validator/db/filedb.cpp create mode 100644 validator/db/filedb.hpp create mode 100644 validator/db/files-async.hpp create mode 100644 validator/db/ltdb.cpp create mode 100644 validator/db/ltdb.hpp create mode 100644 validator/db/rootdb.cpp create mode 100644 validator/db/rootdb.hpp create mode 100644 validator/db/statedb.cpp create mode 100644 validator/db/statedb.hpp create mode 100644 validator/db/staticfilesdb.cpp create mode 100644 validator/db/staticfilesdb.hpp create mode 100644 validator/downloaders/download-state.cpp create mode 100644 validator/downloaders/download-state.hpp create mode 100644 validator/downloaders/wait-block-data-disk.cpp create mode 100644 validator/downloaders/wait-block-data-disk.hpp create mode 100644 validator/downloaders/wait-block-data.cpp create mode 100644 validator/downloaders/wait-block-data.hpp create mode 100644 validator/downloaders/wait-block-state-merge.cpp create mode 100644 validator/downloaders/wait-block-state-merge.hpp create mode 100644 validator/downloaders/wait-block-state.cpp create mode 100644 validator/downloaders/wait-block-state.hpp create mode 100644 validator/dummy0/CMakeLists.txt create mode 100644 validator/dummy0/accept-block.cpp create mode 100644 validator/dummy0/accept-block.hpp create mode 100644 validator/dummy0/block.hpp create mode 100644 validator/dummy0/check-proof.cpp create mode 100644 validator/dummy0/check-proof.hpp create mode 100644 validator/dummy0/collate-query.cpp create mode 100644 validator/dummy0/collate-query.hpp create mode 100644 validator/dummy0/external-message.hpp create mode 100644 validator/dummy0/fabric.cpp create mode 100644 validator/dummy0/fake-accept-block.cpp create mode 100644 validator/dummy0/fake-accept-block.hpp create mode 100644 validator/dummy0/proof.hpp create mode 100644 validator/dummy0/shard.cpp create mode 100644 validator/dummy0/shard.hpp create mode 100644 validator/dummy0/signature-set.cpp create mode 100644 validator/dummy0/signature-set.hpp create mode 100644 validator/dummy0/top-shard-description.cpp create mode 100644 validator/dummy0/top-shard-description.hpp create mode 100644 validator/dummy0/validate-query.cpp create mode 100644 validator/dummy0/validate-query.hpp create mode 100644 validator/dummy0/validator-set.cpp create mode 100644 validator/dummy0/validator-set.hpp create mode 100644 validator/fabric.h create mode 100644 validator/full-node-shard.cpp create mode 100644 validator/full-node-shard.h create mode 100644 validator/full-node-shard.hpp create mode 100644 validator/full-node.cpp create mode 100644 validator/full-node.h create mode 100644 validator/full-node.hpp create mode 100644 validator/get-next-key-blocks.cpp create mode 100644 validator/get-next-key-blocks.h create mode 100644 validator/impl/CMakeLists.txt create mode 100644 validator/impl/accept-block.cpp create mode 100644 validator/impl/accept-block.hpp create mode 100644 validator/impl/block.cpp create mode 100644 validator/impl/block.hpp create mode 100644 validator/impl/check-proof.cpp create mode 100644 validator/impl/check-proof.hpp create mode 100644 validator/impl/collate-query-impl.h create mode 100644 validator/impl/collator-impl.h create mode 100644 validator/impl/collator.cpp create mode 100644 validator/impl/collator.h create mode 100644 validator/impl/external-message.cpp create mode 100644 validator/impl/external-message.hpp create mode 100644 validator/impl/fabric.cpp create mode 100644 validator/impl/ihr-message.cpp create mode 100644 validator/impl/ihr-message.hpp create mode 100644 validator/impl/liteserver.cpp create mode 100644 validator/impl/liteserver.hpp create mode 100644 validator/impl/message-queue.cpp create mode 100644 validator/impl/message-queue.hpp create mode 100644 validator/impl/proof.cpp create mode 100644 validator/impl/proof.hpp create mode 100644 validator/impl/shard.cpp create mode 100644 validator/impl/shard.hpp create mode 100644 validator/impl/signature-set.cpp create mode 100644 validator/impl/signature-set.hpp create mode 100644 validator/impl/top-shard-descr.cpp create mode 100644 validator/impl/top-shard-descr.hpp create mode 100644 validator/impl/validate-query.cpp create mode 100644 validator/impl/validate-query.hpp create mode 100644 validator/impl/validator-set.cpp create mode 100644 validator/impl/validator-set.hpp create mode 100644 validator/interfaces/block-handle.h create mode 100644 validator/interfaces/block.h create mode 100644 validator/interfaces/config.h create mode 100644 validator/interfaces/db.h create mode 100644 validator/interfaces/external-message.h create mode 100644 validator/interfaces/ihr-message.h create mode 100644 validator/interfaces/message-queue.h create mode 100644 validator/interfaces/proof.h create mode 100644 validator/interfaces/shard-block.h create mode 100644 validator/interfaces/shard.h create mode 100644 validator/interfaces/signature-set.h create mode 100644 validator/interfaces/validator-full-id.h create mode 100644 validator/interfaces/validator-manager.h create mode 100644 validator/interfaces/validator-set.h create mode 100644 validator/invariants.hpp create mode 100644 validator/manager-disk.cpp create mode 100644 validator/manager-disk.h create mode 100644 validator/manager-disk.hpp create mode 100644 validator/manager-init.cpp create mode 100644 validator/manager-init.h create mode 100644 validator/manager-init.hpp create mode 100644 validator/manager.cpp create mode 100644 validator/manager.h create mode 100644 validator/manager.hpp create mode 100644 validator/net/download-block.cpp create mode 100644 validator/net/download-block.hpp create mode 100644 validator/net/download-next-block.cpp create mode 100644 validator/net/download-next-block.hpp create mode 100644 validator/net/download-proof.cpp create mode 100644 validator/net/download-proof.hpp create mode 100644 validator/net/download-state.cpp create mode 100644 validator/net/download-state.hpp create mode 100644 validator/net/get-next-key-blocks.cpp create mode 100644 validator/net/get-next-key-blocks.hpp create mode 100644 validator/shard-client.cpp create mode 100644 validator/shard-client.hpp create mode 100644 validator/state-serializer.cpp create mode 100644 validator/state-serializer.hpp create mode 100644 validator/token-manager.cpp create mode 100644 validator/token-manager.h create mode 100644 validator/validate-broadcast.cpp create mode 100644 validator/validate-broadcast.hpp create mode 100644 validator/validator-full-id.cpp create mode 100644 validator/validator-group.cpp create mode 100644 validator/validator-group.hpp create mode 100644 validator/validator-options.cpp create mode 100644 validator/validator-options.hpp create mode 100644 validator/validator.h diff --git a/.clang-format b/.clang-format new file mode 100644 index 00000000..098799e1 --- /dev/null +++ b/.clang-format @@ -0,0 +1,88 @@ +--- +Language: Cpp +# BasedOnStyle: Google +AccessModifierOffset: -1 +AlignAfterOpenBracket: Align +AlignConsecutiveAssignments: false +AlignConsecutiveDeclarations: false +AlignEscapedNewlinesLeft: true +AlignOperands: true +AlignTrailingComments: true +AllowAllParametersOfDeclarationOnNextLine: true +AllowShortBlocksOnASingleLine: false +AllowShortCaseLabelsOnASingleLine: false +AllowShortFunctionsOnASingleLine: None # All +AllowShortIfStatementsOnASingleLine: false # true +AllowShortLoopsOnASingleLine: false # true +AlwaysBreakAfterDefinitionReturnType: None +AlwaysBreakAfterReturnType: None +AlwaysBreakBeforeMultilineStrings: true +AlwaysBreakTemplateDeclarations: true +BinPackArguments: true +BinPackParameters: true +BraceWrapping: + AfterClass: false + AfterControlStatement: false + AfterEnum: false + AfterFunction: false + AfterNamespace: false + AfterObjCDeclaration: false + AfterStruct: false + AfterUnion: false + BeforeCatch: false + BeforeElse: false + IndentBraces: false +BreakBeforeBinaryOperators: None +BreakBeforeBraces: Attach +BreakBeforeTernaryOperators: true +BreakConstructorInitializersBeforeComma: true # false +# BreakAfterJavaFieldAnnotations: false +BreakStringLiterals: true +ColumnLimit: 120 # 80 +CommentPragmas: '^ IWYU pragma:' +ConstructorInitializerAllOnOneLineOrOnePerLine: true +ConstructorInitializerIndentWidth: 4 +ContinuationIndentWidth: 4 +Cpp11BracedListStyle: true +DerivePointerAlignment: true +DisableFormat: false +ExperimentalAutoDetectBinPacking: false +FixNamespaceComments: true +# ForEachMacros: [ foreach, Q_FOREACH, BOOST_FOREACH ] +IndentCaseLabels: true +IndentWidth: 2 +IndentWrappedFunctionNames: false +# JavaScriptQuotes: Leave +# JavaScriptWrapImports: true +KeepEmptyLinesAtTheStartOfBlocks: false +MacroBlockBegin: '' +MacroBlockEnd: '' +MaxEmptyLinesToKeep: 1 +NamespaceIndentation: None +# ObjCBlockIndentWidth: 2 +# ObjCSpaceAfterProperty: false +# ObjCSpaceBeforeProtocolList: false +PenaltyBreakBeforeFirstCallParameter: 1 +PenaltyBreakComment: 300 +PenaltyBreakFirstLessLess: 120 +PenaltyBreakString: 1000 +PenaltyExcessCharacter: 1000000 +PenaltyReturnTypeOnItsOwnLine: 200 +PointerAlignment: Left +ReflowComments: false # true +SortIncludes: false # disabled, because we need case insensitive sort +SpaceAfterCStyleCast: false +SpaceAfterTemplateKeyword: true +SpaceBeforeAssignmentOperators: true +SpaceBeforeParens: ControlStatements +SpaceInEmptyParentheses: false +SpacesBeforeTrailingComments: 2 +SpacesInAngles: false +SpacesInContainerLiterals: true +SpacesInCStyleCastParentheses: false +SpacesInParentheses: false +SpacesInSquareBrackets: false +Standard: Auto +TabWidth: 100 # 8 +UseTab: Never +... diff --git a/.clang_complete b/.clang_complete new file mode 100644 index 00000000..d7cda0ff --- /dev/null +++ b/.clang_complete @@ -0,0 +1,12 @@ +-xc++ +-std=c++14 +-iquote . +-iquote tdtl/ +-iquote tl/ +-iquote tl/generate/ +-iquote tdactor/ +-iquote tdnet/ +-iquote tdutils/ +-iquote ../ton-build/tdutils/ +-iquote crypto/ +-I/opt/local/include/ diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 00000000..d69dd2b1 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,36 @@ +* text=auto + +*.cpp text whitespace=blank-at-eol,space-before-tab,blank-at-eof,tab-in-indent +*.hpp text whitespace=blank-at-eol,space-before-tab,blank-at-eof,tab-in-indent +*.h text whitespace=blank-at-eol,space-before-tab,blank-at-eof,tab-in-indent +*.c text whitespace=blank-at-eol,space-before-tab,blank-at-eof,tab-in-indent +*.fc text whitespace=blank-at-eol,space-before-tab,blank-at-eof,tab-in-indent +*.fif text whitespace=blank-at-eol,space-before-tab,blank-at-eof,tab-in-indent +*.tl text whitespace=blank-at-eol,space-before-tab,blank-at-eof,tab-in-indent +*.tlb text whitespace=blank-at-eol,space-before-tab,blank-at-eof,tab-in-indent +*.txt text whitespace=blank-at-eol,space-before-tab,blank-at-eof,tab-in-indent +*.sh text whitespace=blank-at-eol,space-before-tab,blank-at-eof,tab-in-indent eol=lf +*.cmake text whitespace=blank-at-eol,space-before-tab,blank-at-eof,tab-in-indent +*.in text whitespace=blank-at-eol,space-before-tab,blank-at-eof,tab-in-indent +*.tex text whitespace=blank-at-eol,space-before-tab,blank-at-eof,tab-in-indent +*.py text whitespace=blank-at-eol,space-before-tab,blank-at-eof,tab-in-indent + +*.ans text whitespace=blank-at-eol,space-before-tab,blank-at-eof,tab-in-indent +*.bat text whitespace=blank-at-eol,space-before-tab,blank-at-eof,tab-in-indent eol=crlf +*.fp text whitespace=blank-at-eol,space-before-tab,blank-at-eof,tab-in-indent +*.gradle text whitespace=blank-at-eol,space-before-tab,blank-at-eof,tab-in-indent +*.java text whitespace=blank-at-eol,space-before-tab,blank-at-eof,tab-in-indent +*.kt text whitespace=blank-at-eol,space-before-tab,blank-at-eof,tab-in-indent +*.md text whitespace=blank-at-eol,space-before-tab,blank-at-eof,tab-in-indent +*.php text whitespace=blank-at-eol,space-before-tab,blank-at-eof,tab-in-indent +*.pro text whitespace=blank-at-eol,space-before-tab,blank-at-eof,tab-in-indent +*.properties text whitespace=blank-at-eol,space-before-tab,blank-at-eof,tab-in-indent +*.ps1 text whitespace=blank-at-eol,space-before-tab,blank-at-eof,tab-in-indent eol=crlf +*.xml text whitespace=blank-at-eol,space-before-tab,blank-at-eof,tab-in-indent +*.yml text whitespace=blank-at-eol,space-before-tab,blank-at-eof,tab-in-indent + +third_party/** linguist-vendored + +*.a binary +*.jar binary +*.tlo binary diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..cc307279 --- /dev/null +++ b/.gitignore @@ -0,0 +1,13 @@ +__pycache__ +parser.out +parsetab.py +parsetab.pyc +tdutils/generate/auto/ +tl/generate/auto/ +compile_commands.json +crypto/block/block-auto.cpp +crypto/block/block-auto.h +crypto/smartcont/*-code.fif +test/regression-tests.cache/ +*.swp +**/*build*/ diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 00000000..e6a47e8b --- /dev/null +++ b/.gitmodules @@ -0,0 +1,12 @@ +[submodule "third-party/rocksdb"] + path = third-party/rocksdb + url = https://github.com/facebook/rocksdb.git +[submodule "third-party/crc32c"] + path = third-party/crc32c + url = https://github.com/google/crc32c +[submodule "third-party/abseil-cpp"] + path = third-party/abseil-cpp + url = https://github.com/abseil/abseil-cpp.git +[submodule "third-party/libraptorq"] + path = third-party/libraptorq + url = https://github.com/ton-blockchain/libRaptorQ diff --git a/CMake/AddCXXCompilerFlag.cmake b/CMake/AddCXXCompilerFlag.cmake new file mode 100644 index 00000000..6fb615a1 --- /dev/null +++ b/CMake/AddCXXCompilerFlag.cmake @@ -0,0 +1,74 @@ +# - Adds a compiler flag if it is supported by the compiler +# +# This function checks that the supplied compiler flag is supported and then +# adds it to the corresponding compiler flags +# +# add_cxx_compiler_flag( []) +# +# - Example +# +# include(AddCXXCompilerFlag) +# add_cxx_compiler_flag(-Wall) +# add_cxx_compiler_flag(-no-strict-aliasing RELEASE) +# Requires CMake 2.6+ + +if (__add_cxx_compiler_flag) + return() +endif() +set(__add_cxx_compiler_flag INCLUDED) + +include(CheckCXXCompilerFlag) + +function(mangle_compiler_flag FLAG OUTPUT) + string(TOUPPER "HAVE_CXX_FLAG_${FLAG}" SANITIZED_FLAG) + string(REPLACE "+" "X" SANITIZED_FLAG ${SANITIZED_FLAG}) + string(REGEX REPLACE "[^A-Za-z_0-9]" "_" SANITIZED_FLAG ${SANITIZED_FLAG}) + string(REGEX REPLACE "_+" "_" SANITIZED_FLAG ${SANITIZED_FLAG}) + set(${OUTPUT} "${SANITIZED_FLAG}" PARENT_SCOPE) +endfunction(mangle_compiler_flag) + +function(add_cxx_compiler_flag FLAG) + string(REPLACE "-Wno-" "-W" MAIN_FLAG ${FLAG}) + mangle_compiler_flag("${MAIN_FLAG}" MANGLED_FLAG_NAME) + if (DEFINED CMAKE_REQUIRED_FLAGS) + set(OLD_CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}") + set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} ${FLAG}") + else() + set(CMAKE_REQUIRED_FLAGS "${FLAG}") + endif() + check_cxx_compiler_flag("${MAIN_FLAG}" ${MANGLED_FLAG_NAME}) + if (DEFINED OLD_CMAKE_REQUIRED_FLAGS) + set(CMAKE_REQUIRED_FLAGS "${OLD_CMAKE_REQUIRED_FLAGS}") + else() + unset(CMAKE_REQUIRED_FLAGS) + endif() + if (${MANGLED_FLAG_NAME}) + set(VARIANT ${ARGV1}) + if (ARGV1) + string(TOUPPER "_${VARIANT}" VARIANT) + endif() + set(CMAKE_CXX_FLAGS${VARIANT} "${CMAKE_CXX_FLAGS${VARIANT}} ${FLAG}" PARENT_SCOPE) + endif() +endfunction() + +function(add_required_cxx_compiler_flag FLAG) + string(REPLACE "-Wno-" "-W" MAIN_FLAG ${FLAG}) + mangle_compiler_flag("${MAIN_FLAG}" MANGLED_FLAG_NAME) + set(OLD_CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}") + set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} ${FLAG}") + check_cxx_compiler_flag("${MAIN_FLAG}" ${MANGLED_FLAG_NAME}) + set(CMAKE_REQUIRED_FLAGS "${OLD_CMAKE_REQUIRED_FLAGS}") + if (${MANGLED_FLAG_NAME}) + set(VARIANT ${ARGV1}) + if (ARGV1) + string(TOUPPER "_${VARIANT}" VARIANT) + endif() + set(CMAKE_CXX_FLAGS${VARIANT} "${CMAKE_CXX_FLAGS${VARIANT}} ${FLAG}" PARENT_SCOPE) + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${FLAG}" PARENT_SCOPE) + set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${FLAG}" PARENT_SCOPE) + set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} ${FLAG}" PARENT_SCOPE) + set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} ${FLAG}" PARENT_SCOPE) + else() + message(FATAL_ERROR "Required flag '${FLAG}' is not supported by the compiler") + endif() +endfunction() diff --git a/CMake/FindJeMalloc.cmake b/CMake/FindJeMalloc.cmake new file mode 100644 index 00000000..7911f77c --- /dev/null +++ b/CMake/FindJeMalloc.cmake @@ -0,0 +1,21 @@ +# - Find JeMalloc library +# Find the native JeMalloc includes and library +# +# JEMALLOC_INCLUDE_DIR - where to find jemalloc.h, etc. +# JEMALLOC_LIBRARIES - List of libraries when using jemalloc. +# JEMALLOC_FOUND - True if jemalloc found. + +find_path(JEMALLOC_INCLUDE_DIR + NAMES jemalloc/jemalloc.h + HINTS ${JEMALLOC_ROOT_DIR}/include) + +find_library(JEMALLOC_LIBRARIES + NAMES jemalloc + HINTS ${JEMALLOC_ROOT_DIR}/lib) + +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args(jemalloc DEFAULT_MSG JEMALLOC_LIBRARIES JEMALLOC_INCLUDE_DIR) + +mark_as_advanced( + JEMALLOC_LIBRARIES + JEMALLOC_INCLUDE_DIR) diff --git a/CMake/FindMHD.cmake b/CMake/FindMHD.cmake new file mode 100644 index 00000000..822714a2 --- /dev/null +++ b/CMake/FindMHD.cmake @@ -0,0 +1,38 @@ +# - Try to find MHD +# Once done this will define +# +# MHD_FOUND - system has MHD +# MHD_INCLUDE_DIRS - the MHD include directory +# MHD_LIBRARY - Link these to use MHD + +find_path( + MHD_INCLUDE_DIR + NAMES microhttpd.h + DOC "microhttpd include dir" +) + +find_library( + MHD_LIBRARY + NAMES microhttpd microhttpd-10 libmicrohttpd libmicrohttpd-dll + DOC "microhttpd library" +) + +set(MHD_INCLUDE_DIRS ${MHD_INCLUDE_DIR}) +set(MHD_LIBRARIES ${MHD_LIBRARY}) + +# debug library on windows +# same naming convention as in qt (appending debug library with d) +# boost is using the same "hack" as us with "optimized" and "debug" +# official MHD project actually uses _d suffix +if (MSVC) + find_library( + MHD_LIBRARY_DEBUG + NAMES microhttpd_d microhttpd-10_d libmicrohttpd_d libmicrohttpd-dll_d + DOC "mhd debug library" + ) + set(MHD_LIBRARIES optimized ${MHD_LIBRARIES} debug ${MHD_LIBRARY_DEBUG}) +endif() + +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args(mhd DEFAULT_MSG MHD_INCLUDE_DIR MHD_LIBRARY) +mark_as_advanced(MHD_INCLUDE_DIR MHD_LIBRARY) diff --git a/CMake/FindReadline.cmake b/CMake/FindReadline.cmake new file mode 100644 index 00000000..3b1892cb --- /dev/null +++ b/CMake/FindReadline.cmake @@ -0,0 +1,25 @@ +if (APPLE) + find_path(READLINE_INCLUDE_DIR readline/readline.h /usr/local/opt/readline/include /opt/local/include /opt/include /usr/local/include /usr/include NO_DEFAULT_PATH) +endif() +find_path(READLINE_INCLUDE_DIR readline/readline.h) + +if (APPLE) + find_library(READLINE_LIBRARY readline /usr/local/opt/readline/lib /opt/local/lib /opt/lib /usr/local/lib /usr/lib NO_DEFAULT_PATH) +endif() +find_library(READLINE_LIBRARY readline) + +if (READLINE_INCLUDE_DIR AND READLINE_LIBRARY AND NOT GNU_READLINE_FOUND) + set(CMAKE_REQUIRED_INCLUDES "${READLINE_INCLUDE_DIR}") + set(CMAKE_REQUIRED_LIBRARIES "${READLINE_LIBRARY}") + include(CheckCXXSourceCompiles) + unset(GNU_READLINE_FOUND CACHE) + check_cxx_source_compiles("#include \n#include \nint main() { rl_replace_line(\"\", 0); }" GNU_READLINE_FOUND) + if (NOT GNU_READLINE_FOUND) + unset(READLINE_INCLUDE_DIR CACHE) + unset(READLINE_LIBRARY CACHE) + endif() +endif() + +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args(Readline DEFAULT_MSG READLINE_INCLUDE_DIR READLINE_LIBRARY) +mark_as_advanced(READLINE_INCLUDE_DIR READLINE_LIBRARY) diff --git a/CMake/UseLATEX.cmake b/CMake/UseLATEX.cmake new file mode 100644 index 00000000..53805225 --- /dev/null +++ b/CMake/UseLATEX.cmake @@ -0,0 +1,1936 @@ +# File: UseLATEX.cmake +# CMAKE commands to actually use the LaTeX compiler +# Version: 2.4.6 +# Author: Kenneth Moreland +# +# Copyright 2004, 2015 Sandia Corporation. +# Under the terms of Contract DE-AC04-94AL85000, there is a non-exclusive +# license for use of this work by or on behalf of the U.S. Government. +# +# This software is released under the BSD 3-Clause License. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are +# met: +# +# 1. Redistributions of source code must retain the above copyright notice, +# this list of conditions and the following disclaimer. +# +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# 3. Neither the name of the copyright holder nor the names of its +# contributors may be used to endorse or promote products derived from this +# software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS +# IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, +# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# +# +# The following function is defined: +# +# add_latex_document( +# [BIBFILES ] +# [INPUTS ] +# [IMAGE_DIRS] +# [IMAGES] +# [CONFIGURE] +# [DEPENDS] +# [MULTIBIB_NEWCITES] +# [USE_BIBLATEX] +# [USE_INDEX] +# [INDEX_NAMES ] +# [USE_GLOSSARY] [USE_NOMENCL] +# [FORCE_PDF] [FORCE_DVI] [FORCE_HTML] +# [TARGET_NAME] +# [EXCLUDE_FROM_ALL] +# [EXCLUDE_FROM_DEFAULTS]) +# Adds targets that compile . The latex output is placed +# in LATEX_OUTPUT_PATH or CMAKE_CURRENT_BINARY_DIR if the former is +# not set. The latex program is picky about where files are located, +# so all input files are copied from the source directory to the +# output directory. This includes the target tex file, any tex file +# listed with the INPUTS option, the bibliography files listed with +# the BIBFILES option, and any .cls, .bst, .clo, .sty, .ist, and .fd +# files found in the current source directory. Images found in the +# IMAGE_DIRS directories or listed by IMAGES are also copied to the +# output directory and converted to an appropriate format if necessary. +# Any tex files also listed with the CONFIGURE option are also processed +# with the CMake CONFIGURE_FILE command (with the @ONLY flag). Any file +# listed in CONFIGURE but not the target tex file or listed with INPUTS +# has no effect. DEPENDS can be used to specify generated files that are +# needed to compile the latex target. +# +# The following targets are made. The name prefix is based off of the +# base name of the tex file unless TARGET_NAME is specified. If +# TARGET_NAME is specified, then that name is used for the targets. +# +# name_dvi: Makes .dvi +# name_pdf: Makes .pdf using pdflatex. +# name_safepdf: Makes .pdf using ps2pdf. If using the +# default program arguments, this will ensure all fonts +# are embedded and no lossy compression has been +# performed on images. +# name_ps: Makes .ps +# name_html: Makes .html +# name_auxclean: Deletes .aux and other auxiliary files. +# This is sometimes necessary if a LaTeX error occurs +# and writes a bad aux file. Unlike the regular clean +# target, it does not delete other input files, such as +# converted images, to save time on the rebuild. +# +# Unless the EXCLUDE_FROM_ALL option is given, one of these targets +# is added to the ALL target and built by default. Which target is +# determined by the LATEX_DEFAULT_BUILD CMake variable. See the +# documentation of that variable for more details. +# +# Unless the EXCLUDE_FROM_DEFAULTS option is given, all these targets +# are added as dependencies to targets named dvi, pdf, safepdf, ps, +# html, and auxclean, respectively. +# +# USE_BIBLATEX enables the use of biblatex/biber as an alternative to +# bibtex. Bibtex remains the default if USE_BIBLATEX is not +# specified. +# +# If the argument USE_INDEX is given, then commands to build an index +# are made. If the argument INDEX_NAMES is given, an index file is +# generated for each name in this list. See the LaTeX package multind +# for more information about how to generate multiple indices. +# +# If the argument USE_GLOSSARY is given, then commands to +# build a glossary are made. If the argument MULTIBIB_NEWCITES is +# given, then additional bibtex calls are added to the build to +# support the extra auxiliary files created with the \newcite command +# in the multibib package. +# +# History: +# +# 2.4.6 Fix parse issue with older versions of CMake. +# +# 2.4.5 Fix issues with files and paths containing spaces. +# +# 2.4.4 Improve error reporting message when LaTeX fails. +# +# When LaTeX fails, delete the output file, which is invalid. +# +# Add warnings for "missing characters." These usually mean that a +# non-ASCII character is in the document and will not be printed +# correctly. +# +# 2.4.3 Check for warnings from the natbib package. When using natbib, +# warnings for missing bibliography references look different. So +# far, natbib seems to be quiet unless something is important, so +# look for all natbib warnings. (We can change this later if +# necessary.) +# +# 2.4.2 Fix an issue where new versions of ImageMagick expect the order of +# options in command line execution of magick/convert. (See, for +# example, http://www.imagemagick.org/Usage/basics/#why.) +# +# 2.4.1 Add ability to dump LaTeX log file when using batch mode. Batch +# mode suppresses most output, often including error messages. To +# make sure critical error messages get displayed, show the full log +# on failures. +# +# 2.4.0 Remove "-r 600" from the default PDFTOPS_CONVERTER_FLAGS. The -r flag +# is available from the Poppler version of pdftops, but not the Xpdf +# version. +# +# Fix an issue with the flags for the different programs not being +# properly separated. +# +# Fix an issue on windows where the = character is not allowed for +# ps2pdf arguments. +# +# Change default arguments for latex and pdflatex commands. Makes the +# output more quiet and prints out the file/line where errors occur. +# (Thanks to Nikos Koukis.) +# +# After a LaTeX build, check the log file for warnings that are +# indicative of problems with the build. +# +# Remove support for latex2html. Instead, use the htlatex program. +# This is now part of TeX Live and most other distributions. It also +# behaves much more like the other LaTeX programs. Also fixed some +# nasty issues with the htlatex arguments. +# +# 2.3.2 Declare LaTeX input files as sources for targets so that they show +# up in IDEs like QtCreator. +# +# Fix issue where main tex files in subdirectories were creating +# invalid targets for building HTML. Just disable the HTML targets in +# this case. +# +# 2.3.1 Support use of magick command instead of convert command for +# ImageMagick 7. +# +# 2.3.0 Add USE_BIBLATEX option to support the biblatex package, which +# requires using the program biber as a replacement for bibtex +# (thanks to David Tracey). +# +# 2.2.1 Add STRINGS property to LATEX_DEFAULT_BUILD to make it easier to +# select the default build in the CMake GUI. +# +# 2.2.0 Add TARGET_NAME option. +# +# 2.1.1 Support for finding bmp, ppm, and other image files. +# +# 2.1.0 Fix an error where the pdf target and others were defined multiple +# times if UseLATEX.cmake was included multiple times. +# +# Added INDEX_NAMES option to support multiple indexes in a single +# document from the multind package (thanks to Dan Lipsa). +# +# 2.0.0 First major revision of UseLATEX.cmake updates to more recent features +# of CMake and some non-backward compatible changes. +# +# Changed all function and macro names to lower case. CMake's identifiers +# are case insensitive, but the convention moved from all upper case to +# all lower case somewhere around the release of CMake 2. (The original +# version of UseLATEX.cmake predates that.) +# +# Remove condition matching in if statements. They are no longer necessary +# and are even discouraged (because else clauses get confusing). +# +# Use "new" features available in CMake such as list and argument parsing. +# +# Remove some code that has been deprecated for a while. +# +# Mark variables for compiler and converter executables as advanced to +# match the more conventional CMake behavior. +# +# Changed how default builds are specified and add the ability to force +# a particular build. +# +# Made the base targets (pdf, dvi, etc.) global. add_latex_document +# always mangles its target names and these base targets depend on +# the targets with mangled names. +# +# 1.10.5 Fix for Window's convert check (thanks to Martin Baute). +# +# 1.10.4 Copy font files to binary directory for packages that come with +# their own fonts. +# +# 1.10.3 Check for Windows version of convert being used instead of +# ImageMagick's version (thanks to Martin Baute). +# +# 1.10.2 Use htlatex as a fallback when latex2html is not available (thanks +# to Tomasz Grzegurzko). +# +# 1.10.1 Make convert program mandatory only if actually used (thanks to +# Julien Schueller). +# +# 1.10.0 Added NO_DEFAULT and DEFAULT_PS options. +# Fixed issue with cleaning files for LaTeX documents originating in +# a subdirectory. +# +# 1.9.6 Fixed problem with LATEX_SMALL_IMAGES. +# Strengthened check to make sure the output directory does not contain +# the source files. +# +# 1.9.5 Add support for image types not directly supported by either latex +# or pdflatex. (Thanks to Jorge Gerardo Pena Pastor for SVG support.) +# +# 1.9.4 Fix issues with filenames containing multiple periods. +# +# 1.9.3 Hide some variables that are now cached but should not show up in +# the ccmake list of variables. +# +# 1.9.2 Changed MACRO declarations to FUNCTION declarations. The better +# FUNCTION scoping will hopefully avoid some common but subtle bugs. +# This implicitly increases the minimum CMake version to 4.6 (although +# I honestly only test it with the latest 4.8 version). +# +# Since we are updating the minimum CMake version, I'm going to start +# using the builtin LIST commands that are now available. +# +# Favor using pdftops from the Poppler package to convert from pdf to +# eps. It does a much better job than ImageMagick or ghostscript. +# +# 1.9.1 Fixed typo that caused the LATEX_SMALL_IMAGES option to fail to +# activate. +# +# 1.9.0 Add support for the multibib package (thanks to Antonio LaTorre). +# +# 1.8.2 Fix corner case when an argument name was also a variable containing +# the text of an argument. In this case, the CMake IF was matching +# the argument text with the contents of the variable with the same +# argument name. +# +# 1.8.1 Fix problem where ps2pdf was not getting the appropriate arguments. +# +# 1.8.0 Add support for synctex. +# +# 1.7.7 Support calling xindy when making glossaries. +# +# Improved make clean support. +# +# 1.7.6 Add support for the nomencl package (thanks to Myles English). +# +# 1.7.5 Fix issue with bibfiles being copied two different ways, which causes +# Problems with dependencies (thanks to Edwin van Leeuwen). +# +# 1.7.4 Added the DEFAULT_SAFEPDF option (thanks to Raymond Wan). +# +# Added warnings when image directories are not found (and were +# probably not given relative to the source directory). +# +# 1.7.3 Fix some issues with interactions between makeglossaries and bibtex +# (thanks to Mark de Wever). +# +# 1.7.2 Use ps2pdf to convert eps to pdf to get around the problem with +# ImageMagick dropping the bounding box (thanks to Lukasz Lis). +# +# 1.7.1 Fixed some dependency issues. +# +# 1.7.0 Added DEPENDS options (thanks to Theodore Papadopoulo). +# +# 1.6.1 Ported the makeglossaries command to CMake and embedded the port +# into UseLATEX.cmake. +# +# 1.6.0 Allow the use of the makeglossaries command. Thanks to Oystein +# S. Haaland for the patch. +# +# 1.5.0 Allow any type of file in the INPUTS lists, not just tex file +# (suggested by Eric Noulard). As a consequence, the ability to +# specify tex files without the .tex extension is removed. The removed +# function is of dubious value anyway. +# +# When copying input files, skip over any file that exists in the +# binary directory but does not exist in the source directory with the +# assumption that these files were added by some other mechanism. I +# find this useful when creating large documents with multiple +# chapters that I want to build separately (for speed) as I work on +# them. I use the same boilerplate as the starting point for all +# and just copy it with different configurations. This was what the +# separate ADD_LATEX_DOCUMENT method was supposed to originally be for. +# Since its external use is pretty much deprecated, I removed that +# documentation. +# +# 1.4.1 Copy .sty files along with the other class and package files. +# +# 1.4.0 Added a MANGLE_TARGET_NAMES option that will mangle the target names. +# +# Fixed problem with copying bib files that became apparent with +# CMake 2.4. +# +# 1.3.0 Added a LATEX_OUTPUT_PATH variable that allows you or the user to +# specify where the built latex documents to go. This is especially +# handy if you want to do in-source builds. +# +# Removed the ADD_LATEX_IMAGES macro and absorbed the functionality +# into ADD_LATEX_DOCUMENT. The old interface was always kind of +# clunky anyway since you had to specify the image directory in both +# places. It also made supporting LATEX_OUTPUT_PATH problematic. +# +# Added support for jpeg files. +# +# 1.2.0 Changed the configuration options yet again. Removed the NO_CONFIGURE +# Replaced it with a CONFIGURE option that lists input files for which +# configure should be run. +# +# The pdf target no longer depends on the dvi target. This allows you +# to build latex documents that require pdflatex. Also added an option +# to make the pdf target the default one. +# +# 1.1.1 Added the NO_CONFIGURE option. The @ character can be used when +# specifying table column separators. If two or more are used, then +# will incorrectly substitute them. +# +# 1.1.0 Added ability include multiple bib files. Added ability to do copy +# sub-tex files for multipart tex files. +# +# 1.0.0 If both ps and pdf type images exist, just copy the one that +# matches the current render mode. Replaced a bunch of STRING +# commands with GET_FILENAME_COMPONENT commands that were made to do +# the desired function. +# +# 0.4.0 First version posted to CMake Wiki. +# + +if(__USE_LATEX_INCLUDED) + return() +endif() +set(__USE_LATEX_INCLUDED TRUE) + +############################################################################# +# Find the location of myself while originally executing. If you do this +# inside of a macro, it will recode where the macro was invoked. +############################################################################# +set(LATEX_USE_LATEX_LOCATION ${CMAKE_CURRENT_LIST_FILE} + CACHE INTERNAL "Location of UseLATEX.cmake file." FORCE + ) + +############################################################################# +# Generic helper functions +############################################################################# + +include(CMakeParseArguments) + +function(latex_list_contains var value) + set(input_list ${ARGN}) + list(FIND input_list "${value}" index) + if(index GREATER -1) + set(${var} TRUE PARENT_SCOPE) + else() + set(${var} PARENT_SCOPE) + endif() +endfunction(latex_list_contains) + +# Match the contents of a file to a regular expression. +function(latex_file_match variable filename regexp default) + # The FILE STRINGS command would be a bit better, but I'm not totally sure + # the match will always be to a whole line, and I don't want to break things. + file(READ ${filename} file_contents) + string(REGEX MATCHALL "${regexp}" + match_result ${file_contents} + ) + if(match_result) + set(${variable} "${match_result}" PARENT_SCOPE) + else() + set(${variable} "${default}" PARENT_SCOPE) + endif() +endfunction(latex_file_match) + +# A version of GET_FILENAME_COMPONENT that treats extensions after the last +# period rather than the first. To the best of my knowledge, all filenames +# typically used by LaTeX, including image files, have small extensions +# after the last dot. +function(latex_get_filename_component varname filename type) + set(result) + if("${type}" STREQUAL "NAME_WE") + get_filename_component(name ${filename} NAME) + string(REGEX REPLACE "\\.[^.]*\$" "" result "${name}") + elseif("${type}" STREQUAL "EXT") + get_filename_component(name ${filename} NAME) + string(REGEX MATCH "\\.[^.]*\$" result "${name}") + else() + get_filename_component(result ${filename} ${type}) + endif() + set(${varname} "${result}" PARENT_SCOPE) +endfunction(latex_get_filename_component) + +############################################################################# +# Functions that perform processing during a LaTeX build. +############################################################################# +function(latex_execute_latex) + if(NOT LATEX_TARGET) + message(SEND_ERROR "Need to define LATEX_TARGET") + endif() + + if(NOT LATEX_WORKING_DIRECTORY) + message(SEND_ERROR "Need to define LATEX_WORKING_DIRECTORY") + endif() + + if(NOT LATEX_FULL_COMMAND) + message(SEND_ERROR "Need to define LATEX_FULL_COMMAND") + endif() + + if(NOT LATEX_OUTPUT_FILE) + message(SEND_ERROR "Need to define LATEX_OUTPUT_FILE") + endif() + + set(full_command_original "${LATEX_FULL_COMMAND}") + + # Chose the native method for parsing command arguments. Newer versions of + # CMake allow you to just use NATIVE_COMMAND. + if (CMAKE_VERSION VERSION_GREATER 3.8) + set(separate_arguments_mode NATIVE_COMMAND) + else() + if (WIN32) + set(separate_arguments_mode WINDOWS_COMMAND) + else() + set(separate_arguments_mode UNIX_COMMAND) + endif() + endif() + + # Preps variables for use in execute_process. + # Even though we expect LATEX_WORKING_DIRECTORY to have a single "argument," + # we also want to make sure that we strip out any escape characters that can + # foul up the WORKING_DIRECTORY argument. + separate_arguments(LATEX_FULL_COMMAND UNIX_COMMAND "${LATEX_FULL_COMMAND}") + separate_arguments(LATEX_WORKING_DIRECTORY UNIX_COMMAND "${LATEX_WORKING_DIRECTORY}") + + execute_process( + COMMAND ${LATEX_FULL_COMMAND} + WORKING_DIRECTORY ${LATEX_WORKING_DIRECTORY} + RESULT_VARIABLE execute_result + ) + + if(NOT ${execute_result} EQUAL 0) + # LaTeX tends to write a file when a failure happens. Delete that file so + # that LaTeX will run again. + file(REMOVE "${LATEX_WORKING_DIRECTORY}/${LATEX_OUTPUT_FILE}") + + message("\n\nLaTeX command failed") + message("${full_command_original}") + message("Log output:") + file(READ ${LATEX_WORKING_DIRECTORY}/${LATEX_TARGET}.log log_output) + message("${log_output}") + message(FATAL_ERROR + "Successfully executed LaTeX, but LaTeX returned an error.") + endif() +endfunction(latex_execute_latex) + +function(latex_makeglossaries) + # This is really a bare bones port of the makeglossaries perl script into + # CMake scripting. + message("**************************** In makeglossaries") + if(NOT LATEX_TARGET) + message(SEND_ERROR "Need to define LATEX_TARGET") + endif() + + set(aux_file ${LATEX_TARGET}.aux) + + if(NOT EXISTS ${aux_file}) + message(SEND_ERROR "${aux_file} does not exist. Run latex on your target file.") + endif() + + latex_file_match(newglossary_lines ${aux_file} + "@newglossary[ \t]*{([^}]*)}{([^}]*)}{([^}]*)}{([^}]*)}" + "@newglossary{main}{glg}{gls}{glo}" + ) + + latex_file_match(istfile_line ${aux_file} + "@istfilename[ \t]*{([^}]*)}" + "@istfilename{${LATEX_TARGET}.ist}" + ) + string(REGEX REPLACE "@istfilename[ \t]*{([^}]*)}" "\\1" + istfile ${istfile_line} + ) + + string(REGEX MATCH ".*\\.xdy" use_xindy "${istfile}") + if(use_xindy) + message("*************** Using xindy") + if(NOT XINDY_COMPILER) + message(SEND_ERROR "Need to define XINDY_COMPILER") + endif() + else() + message("*************** Using makeindex") + if(NOT MAKEINDEX_COMPILER) + message(SEND_ERROR "Need to define MAKEINDEX_COMPILER") + endif() + endif() + + foreach(newglossary ${newglossary_lines}) + string(REGEX REPLACE + "@newglossary[ \t]*{([^}]*)}{([^}]*)}{([^}]*)}{([^}]*)}" + "\\1" glossary_name ${newglossary} + ) + string(REGEX REPLACE + "@newglossary[ \t]*{([^}]*)}{([^}]*)}{([^}]*)}{([^}]*)}" + "${LATEX_TARGET}.\\2" glossary_log ${newglossary} + ) + string(REGEX REPLACE + "@newglossary[ \t]*{([^}]*)}{([^}]*)}{([^}]*)}{([^}]*)}" + "${LATEX_TARGET}.\\3" glossary_out ${newglossary} + ) + string(REGEX REPLACE + "@newglossary[ \t]*{([^}]*)}{([^}]*)}{([^}]*)}{([^}]*)}" + "${LATEX_TARGET}.\\4" glossary_in ${newglossary} + ) + + if(use_xindy) + latex_file_match(xdylanguage_line ${aux_file} + "@xdylanguage[ \t]*{${glossary_name}}{([^}]*)}" + "@xdylanguage{${glossary_name}}{english}" + ) + string(REGEX REPLACE + "@xdylanguage[ \t]*{${glossary_name}}{([^}]*)}" + "\\1" + language + ${xdylanguage_line} + ) + # What crazy person makes a LaTeX index generator that uses different + # identifiers for language than babel (or at least does not support + # the old ones)? + if(${language} STREQUAL "frenchb") + set(language "french") + elseif(${language} MATCHES "^n?germanb?$") + set(language "german") + elseif(${language} STREQUAL "magyar") + set(language "hungarian") + elseif(${language} STREQUAL "lsorbian") + set(language "lower-sorbian") + elseif(${language} STREQUAL "norsk") + set(language "norwegian") + elseif(${language} STREQUAL "portuges") + set(language "portuguese") + elseif(${language} STREQUAL "russianb") + set(language "russian") + elseif(${language} STREQUAL "slovene") + set(language "slovenian") + elseif(${language} STREQUAL "ukraineb") + set(language "ukrainian") + elseif(${language} STREQUAL "usorbian") + set(language "upper-sorbian") + endif() + if(language) + set(language_flags "-L ${language}") + else() + set(language_flags "") + endif() + + latex_file_match(codepage_line ${aux_file} + "@gls@codepage[ \t]*{${glossary_name}}{([^}]*)}" + "@gls@codepage{${glossary_name}}{utf}" + ) + string(REGEX REPLACE + "@gls@codepage[ \t]*{${glossary_name}}{([^}]*)}" + "\\1" + codepage + ${codepage_line} + ) + if(codepage) + set(codepage_flags "-C ${codepage}") + else() + # Ideally, we would check that the language is compatible with the + # default codepage, but I'm hoping that distributions will be smart + # enough to specify their own codepage. I know, it's asking a lot. + set(codepage_flags "") + endif() + + message("${XINDY_COMPILER} ${MAKEGLOSSARIES_COMPILER_ARGS} ${language_flags} ${codepage_flags} -I xindy -M ${glossary_name} -t ${glossary_log} -o ${glossary_out} ${glossary_in}" + ) + exec_program(${XINDY_COMPILER} + ARGS ${MAKEGLOSSARIES_COMPILER_ARGS} + ${language_flags} + ${codepage_flags} + -I xindy + -M ${glossary_name} + -t ${glossary_log} + -o ${glossary_out} + ${glossary_in} + OUTPUT_VARIABLE xindy_output + ) + message("${xindy_output}") + + # So, it is possible (perhaps common?) for aux files to specify a + # language and codepage that are incompatible with each other. Check + # for that condition, and if it happens run again with the default + # codepage. + if("${xindy_output}" MATCHES "^Cannot locate xindy module for language (.+) in codepage (.+)\\.$") + message("*************** Retrying xindy with default codepage.") + exec_program(${XINDY_COMPILER} + ARGS ${MAKEGLOSSARIES_COMPILER_ARGS} + ${language_flags} + -I xindy + -M ${glossary_name} + -t ${glossary_log} + -o ${glossary_out} + ${glossary_in} + ) + endif() + + else() + message("${MAKEINDEX_COMPILER} ${MAKEGLOSSARIES_COMPILER_ARGS} -s ${istfile} -t ${glossary_log} -o ${glossary_out} ${glossary_in}") + exec_program(${MAKEINDEX_COMPILER} ARGS ${MAKEGLOSSARIES_COMPILER_ARGS} + -s ${istfile} -t ${glossary_log} -o ${glossary_out} ${glossary_in} + ) + endif() + + endforeach(newglossary) +endfunction(latex_makeglossaries) + +function(latex_makenomenclature) + message("**************************** In makenomenclature") + if(NOT LATEX_TARGET) + message(SEND_ERROR "Need to define LATEX_TARGET") + endif() + + if(NOT MAKEINDEX_COMPILER) + message(SEND_ERROR "Need to define MAKEINDEX_COMPILER") + endif() + + set(nomencl_out ${LATEX_TARGET}.nls) + set(nomencl_in ${LATEX_TARGET}.nlo) + + exec_program(${MAKEINDEX_COMPILER} ARGS ${MAKENOMENCLATURE_COMPILER_ARGS} + ${nomencl_in} -s "nomencl.ist" -o ${nomencl_out} + ) +endfunction(latex_makenomenclature) + +function(latex_correct_synctex) + message("**************************** In correct SyncTeX") + if(NOT LATEX_TARGET) + message(SEND_ERROR "Need to define LATEX_TARGET") + endif() + + if(NOT GZIP) + message(SEND_ERROR "Need to define GZIP") + endif() + + if(NOT LATEX_SOURCE_DIRECTORY) + message(SEND_ERROR "Need to define LATEX_SOURCE_DIRECTORY") + endif() + + if(NOT LATEX_BINARY_DIRECTORY) + message(SEND_ERROR "Need to define LATEX_BINARY_DIRECTORY") + endif() + + set(synctex_file ${LATEX_BINARY_DIRECTORY}/${LATEX_TARGET}.synctex) + set(synctex_file_gz ${synctex_file}.gz) + + if(EXISTS ${synctex_file_gz}) + + message("Making backup of synctex file.") + configure_file(${synctex_file_gz} ${synctex_file}.bak.gz COPYONLY) + + message("Uncompressing synctex file.") + exec_program(${GZIP} + ARGS --decompress ${synctex_file_gz} + ) + + message("Reading synctex file.") + file(READ ${synctex_file} synctex_data) + + message("Replacing relative with absolute paths.") + string(REGEX REPLACE + "(Input:[0-9]+:)([^/\n][^\n]*)" + "\\1${LATEX_SOURCE_DIRECTORY}/\\2" + synctex_data + "${synctex_data}" + ) + + message("Writing synctex file.") + file(WRITE ${synctex_file} "${synctex_data}") + + message("Compressing synctex file.") + exec_program(${GZIP} + ARGS ${synctex_file} + ) + + else() + + message(SEND_ERROR "File ${synctex_file_gz} not found. Perhaps synctex is not supported by your LaTeX compiler.") + + endif() + +endfunction(latex_correct_synctex) + +function(latex_check_important_warnings) + set(log_file ${LATEX_TARGET}.log) + + message("\nChecking ${log_file} for important warnings.") + if(NOT LATEX_TARGET) + message(SEND_ERROR "Need to define LATEX_TARGET") + endif() + + if(NOT EXISTS ${log_file}) + message("Could not find log file: ${log_file}") + return() + endif() + + set(found_error) + + file(READ ${log_file} log) + + # Check for undefined references + string(REGEX MATCHALL + "\n[^\n]*Reference[^\n]*undefined[^\n]*" + reference_warnings + "${log}") + if(reference_warnings) + set(found_error TRUE) + message("\nFound missing reference warnings.") + foreach(warning ${reference_warnings}) + string(STRIP "${warning}" warning_no_newline) + message("${warning_no_newline}") + endforeach(warning) + endif() + + # Check for natbib warnings + string(REGEX MATCHALL + "\nPackage natbib Warning:[^\n]*" + natbib_warnings + "${log}") + if(natbib_warnings) + set(found_error TRUE) + message("\nFound natbib package warnings.") + foreach(warning ${natbib_warnings}) + string(STRIP "${warning}" warning_no_newline) + message("${warning_no_newline}") + endforeach(warning) + endif() + + # Check for overfull + string(REGEX MATCHALL + "\nOverfull[^\n]*" + overfull_warnings + "${log}") + if(overfull_warnings) + set(found_error TRUE) + message("\nFound overfull warnings. These are indicative of layout errors.") + foreach(warning ${overfull_warnings}) + string(STRIP "${warning}" warning_no_newline) + message("${warning_no_newline}") + endforeach(warning) + endif() + + # Check for invalid characters + string(REGEX MATCHALL + "\nMissing character:[^\n]*" + invalid_character_warnings + "${log}") + if(invalid_character_warnings) + set(found_error TRUE) + message("\nFound invalid character warnings. These characters are likely not printed correctly.") + foreach(warning ${invalid_character_warnings}) + string(STRIP "${warning}" warning_no_newline) + message("${warning_no_newline}") + endforeach(warning) + endif() + + if(found_error) + latex_get_filename_component(log_file_path ${log_file} ABSOLUTE) + message("\nConsult ${log_file_path} for more information on LaTeX build.") + else() + message("No known important warnings found.") + endif(found_error) +endfunction(latex_check_important_warnings) + +############################################################################# +# Helper functions for establishing LaTeX build. +############################################################################# + +function(latex_needit VAR NAME) + if(NOT ${VAR}) + message(SEND_ERROR "I need the ${NAME} command.") + endif() +endfunction(latex_needit) + +function(latex_wantit VAR NAME) + if(NOT ${VAR}) + message(STATUS "I could not find the ${NAME} command.") + endif() +endfunction(latex_wantit) + +function(latex_setup_variables) + set(LATEX_OUTPUT_PATH "${LATEX_OUTPUT_PATH}" + CACHE PATH "If non empty, specifies the location to place LaTeX output." + ) + + find_package(LATEX) + + find_program(XINDY_COMPILER + NAME xindy + PATHS ${MIKTEX_BINARY_PATH} /usr/bin + ) + + find_package(UnixCommands) + + find_program(PDFTOPS_CONVERTER + NAMES pdftops + DOC "The pdf to ps converter program from the Poppler package." + ) + + find_program(HTLATEX_COMPILER + NAMES htlatex + PATHS ${MIKTEX_BINARY_PATH} + /usr/bin + ) + + mark_as_advanced( + LATEX_COMPILER + PDFLATEX_COMPILER + BIBTEX_COMPILER + BIBER_COMPILER + MAKEINDEX_COMPILER + XINDY_COMPILER + DVIPS_CONVERTER + PS2PDF_CONVERTER + PDFTOPS_CONVERTER + LATEX2HTML_CONVERTER + HTLATEX_COMPILER + ) + + latex_needit(LATEX_COMPILER latex) + latex_wantit(PDFLATEX_COMPILER pdflatex) + latex_wantit(HTLATEX_COMPILER htlatex) + latex_needit(BIBTEX_COMPILER bibtex) + latex_wantit(BIBER_COMPILER biber) + latex_needit(MAKEINDEX_COMPILER makeindex) + latex_wantit(DVIPS_CONVERTER dvips) + latex_wantit(PS2PDF_CONVERTER ps2pdf) + latex_wantit(PDFTOPS_CONVERTER pdftops) + + set(LATEX_COMPILER_FLAGS "-interaction=batchmode -file-line-error" + CACHE STRING "Flags passed to latex.") + set(PDFLATEX_COMPILER_FLAGS ${LATEX_COMPILER_FLAGS} + CACHE STRING "Flags passed to pdflatex.") + set(HTLATEX_COMPILER_TEX4HT_FLAGS "html" + CACHE STRING "Options for the tex4ht.sty and *.4ht style files.") + set(HTLATEX_COMPILER_TEX4HT_POSTPROCESSOR_FLAGS "" + CACHE STRING "Options for the text4ht postprocessor.") + set(HTLATEX_COMPILER_T4HT_POSTPROCESSOR_FLAGS "" + CACHE STRING "Options for the t4ht postprocessor.") + set(HTLATEX_COMPILER_LATEX_FLAGS ${LATEX_COMPILER_FLAGS} + CACHE STRING "Flags passed from htlatex to the LaTeX compiler.") + set(LATEX_SYNCTEX_FLAGS "-synctex=1" + CACHE STRING "latex/pdflatex flags used to create synctex file.") + set(BIBTEX_COMPILER_FLAGS "" + CACHE STRING "Flags passed to bibtex.") + set(BIBER_COMPILER_FLAGS "" + CACHE STRING "Flags passed to biber.") + set(MAKEINDEX_COMPILER_FLAGS "" + CACHE STRING "Flags passed to makeindex.") + set(MAKEGLOSSARIES_COMPILER_FLAGS "" + CACHE STRING "Flags passed to makeglossaries.") + set(MAKENOMENCLATURE_COMPILER_FLAGS "" + CACHE STRING "Flags passed to makenomenclature.") + set(DVIPS_CONVERTER_FLAGS "-Ppdf -G0 -t letter" + CACHE STRING "Flags passed to dvips.") + if(NOT WIN32) + set(PS2PDF_CONVERTER_FLAGS "-dMaxSubsetPct=100 -dCompatibilityLevel=1.3 -dSubsetFonts=true -dEmbedAllFonts=true -dAutoFilterColorImages=false -dAutoFilterGrayImages=false -dColorImageFilter=/FlateEncode -dGrayImageFilter=/FlateEncode -dMonoImageFilter=/FlateEncode" + CACHE STRING "Flags passed to ps2pdf.") + else() + # Most windows ports of ghostscript utilities use .bat files for ps2pdf + # commands. bat scripts interpret "=" as a special character and separate + # those arguments. To get around this, the ghostscript utilities also + # support using "#" in place of "=". + set(PS2PDF_CONVERTER_FLAGS "-dMaxSubsetPct#100 -dCompatibilityLevel#1.3 -dSubsetFonts#true -dEmbedAllFonts#true -dAutoFilterColorImages#false -dAutoFilterGrayImages#false -dColorImageFilter#/FlateEncode -dGrayImageFilter#/FlateEncode -dMonoImageFilter#/FlateEncode" + CACHE STRING "Flags passed to ps2pdf.") + endif() + set(PDFTOPS_CONVERTER_FLAGS "" + CACHE STRING "Flags passed to pdftops.") + mark_as_advanced( + LATEX_COMPILER_FLAGS + PDFLATEX_COMPILER_FLAGS + HTLATEX_COMPILER_TEX4HT_FLAGS + HTLATEX_COMPILER_TEX4HT_POSTPROCESSOR_FLAGS + HTLATEX_COMPILER_T4HT_POSTPROCESSOR_FLAGS + HTLATEX_COMPILER_LATEX_FLAGS + LATEX_SYNCTEX_FLAGS + BIBTEX_COMPILER_FLAGS + BIBER_COMPILER_FLAGS + MAKEINDEX_COMPILER_FLAGS + MAKEGLOSSARIES_COMPILER_FLAGS + MAKENOMENCLATURE_COMPILER_FLAGS + DVIPS_CONVERTER_FLAGS + PS2PDF_CONVERTER_FLAGS + PDFTOPS_CONVERTER_FLAGS + ) + + # Because it is easier to type, the flags variables are entered as + # space-separated strings much like you would in a shell. However, when + # using a CMake command to execute a program, it works better to hold the + # arguments in semicolon-separated lists (otherwise the whole string will + # be interpreted as a single argument). Use the separate_arguments to + # convert the space-separated strings to semicolon-separated lists. + separate_arguments(LATEX_COMPILER_FLAGS) + separate_arguments(PDFLATEX_COMPILER_FLAGS) + separate_arguments(HTLATEX_COMPILER_LATEX_FLAGS) + separate_arguments(LATEX_SYNCTEX_FLAGS) + separate_arguments(BIBTEX_COMPILER_FLAGS) + separate_arguments(BIBER_COMPILER_FLAGS) + separate_arguments(MAKEINDEX_COMPILER_FLAGS) + separate_arguments(MAKEGLOSSARIES_COMPILER_FLAGS) + separate_arguments(MAKENOMENCLATURE_COMPILER_FLAGS) + separate_arguments(DVIPS_CONVERTER_FLAGS) + separate_arguments(PS2PDF_CONVERTER_FLAGS) + separate_arguments(PDFTOPS_CONVERTER_FLAGS) + + # Not quite done. When you call separate_arguments on a cache variable, + # the result is written to a local variable. That local variable goes + # away when this function returns (which is before any of them are used). + # So, copy these variables with local scope to cache variables with + # global scope. + set(LATEX_COMPILER_ARGS "${LATEX_COMPILER_FLAGS}" CACHE INTERNAL "") + set(PDFLATEX_COMPILER_ARGS "${PDFLATEX_COMPILER_FLAGS}" CACHE INTERNAL "") + set(HTLATEX_COMPILER_ARGS "${HTLATEX_COMPILER_LATEX_FLAGS}" CACHE INTERNAL "") + set(LATEX_SYNCTEX_ARGS "${LATEX_SYNCTEX_FLAGS}" CACHE INTERNAL "") + set(BIBTEX_COMPILER_ARGS "${BIBTEX_COMPILER_FLAGS}" CACHE INTERNAL "") + set(BIBER_COMPILER_ARGS "${BIBER_COMPILER_FLAGS}" CACHE INTERNAL "") + set(MAKEINDEX_COMPILER_ARGS "${MAKEINDEX_COMPILER_FLAGS}" CACHE INTERNAL "") + set(MAKEGLOSSARIES_COMPILER_ARGS "${MAKEGLOSSARIES_COMPILER_FLAGS}" CACHE INTERNAL "") + set(MAKENOMENCLATURE_COMPILER_ARGS "${MAKENOMENCLATURE_COMPILER_FLAGS}" CACHE INTERNAL "") + set(DVIPS_CONVERTER_ARGS "${DVIPS_CONVERTER_FLAGS}" CACHE INTERNAL "") + set(PS2PDF_CONVERTER_ARGS "${PS2PDF_CONVERTER_FLAGS}" CACHE INTERNAL "") + set(PDFTOPS_CONVERTER_ARGS "${PDFTOPS_CONVERTER_FLAGS}" CACHE INTERNAL "") + + find_program(IMAGEMAGICK_CONVERT + NAMES magick convert + DOC "The convert program that comes with ImageMagick (available at http://www.imagemagick.org)." + ) + mark_as_advanced(IMAGEMAGICK_CONVERT) + + if(DEFINED ENV{LATEX_DEFAULT_BUILD}) + set(default_build $ENV{LATEX_DEFAULT_BUILD}) + else() + set(default_build pdf) + endif() + + set(LATEX_DEFAULT_BUILD "${default_build}" CACHE STRING + "Choose the default type of LaTeX build. Valid options are pdf, dvi, ps, safepdf, html" + ) + set_property(CACHE LATEX_DEFAULT_BUILD + PROPERTY STRINGS pdf dvi ps safepdf html + ) + + option(LATEX_USE_SYNCTEX + "If on, have LaTeX generate a synctex file, which WYSIWYG editors can use to correlate output files like dvi and pdf with the lines of LaTeX source that generates them. In addition to adding the LATEX_SYNCTEX_FLAGS to the command line, this option also adds build commands that \"corrects\" the resulting synctex file to point to the original LaTeX files rather than those generated by UseLATEX.cmake." + OFF + ) + + option(LATEX_SMALL_IMAGES + "If on, the raster images will be converted to 1/6 the original size. This is because papers usually require 600 dpi images whereas most monitors only require at most 96 dpi. Thus, smaller images make smaller files for web distribution and can make it faster to read dvi files." + OFF) + if(LATEX_SMALL_IMAGES) + set(LATEX_RASTER_SCALE 16 PARENT_SCOPE) + set(LATEX_OPPOSITE_RASTER_SCALE 100 PARENT_SCOPE) + else() + set(LATEX_RASTER_SCALE 100 PARENT_SCOPE) + set(LATEX_OPPOSITE_RASTER_SCALE 16 PARENT_SCOPE) + endif() + + # Just holds extensions for known image types. They should all be lower case. + # For historical reasons, these are all declared in the global scope. + set(LATEX_DVI_VECTOR_IMAGE_EXTENSIONS .eps CACHE INTERNAL "") + set(LATEX_DVI_RASTER_IMAGE_EXTENSIONS CACHE INTERNAL "") + set(LATEX_DVI_IMAGE_EXTENSIONS + ${LATEX_DVI_VECTOR_IMAGE_EXTENSIONS} + ${LATEX_DVI_RASTER_IMAGE_EXTENSIONS} + CACHE INTERNAL "" + ) + + set(LATEX_PDF_VECTOR_IMAGE_EXTENSIONS .pdf CACHE INTERNAL "") + set(LATEX_PDF_RASTER_IMAGE_EXTENSIONS .jpeg .jpg .png CACHE INTERNAL "") + set(LATEX_PDF_IMAGE_EXTENSIONS + ${LATEX_PDF_VECTOR_IMAGE_EXTENSIONS} + ${LATEX_PDF_RASTER_IMAGE_EXTENSIONS} + CACHE INTERNAL "" + ) + + set(LATEX_OTHER_VECTOR_IMAGE_EXTENSIONS .ai .dot .svg CACHE INTERNAL "") + set(LATEX_OTHER_RASTER_IMAGE_EXTENSIONS + .bmp .bmp2 .bmp3 .dcm .dcx .ico .gif .pict .ppm .tif .tiff + CACHE INTERNAL "") + set(LATEX_OTHER_IMAGE_EXTENSIONS + ${LATEX_OTHER_VECTOR_IMAGE_EXTENSIONS} + ${LATEX_OTHER_RASTER_IMAGE_EXTENSIONS} + CACHE INTERNAL "" + ) + + set(LATEX_VECTOR_IMAGE_EXTENSIONS + ${LATEX_DVI_VECTOR_IMAGE_EXTENSIONS} + ${LATEX_PDF_VECTOR_IMAGE_EXTENSIONS} + ${LATEX_OTHER_VECTOR_IMAGE_EXTENSIONS} + CACHE INTERNAL "" + ) + set(LATEX_RASTER_IMAGE_EXTENSIONS + ${LATEX_DVI_RASTER_IMAGE_EXTENSIONS} + ${LATEX_PDF_RASTER_IMAGE_EXTENSIONS} + ${LATEX_OTHER_RASTER_IMAGE_EXTENSIONS} + CACHE INTERNAL "" + ) + set(LATEX_IMAGE_EXTENSIONS + ${LATEX_DVI_IMAGE_EXTENSIONS} + ${LATEX_PDF_IMAGE_EXTENSIONS} + ${LATEX_OTHER_IMAGE_EXTENSIONS} + CACHE INTERNAL "" + ) +endfunction(latex_setup_variables) + +function(latex_setup_targets) + if(NOT TARGET pdf) + add_custom_target(pdf) + endif() + if(NOT TARGET dvi) + add_custom_target(dvi) + endif() + if(NOT TARGET ps) + add_custom_target(ps) + endif() + if(NOT TARGET safepdf) + add_custom_target(safepdf) + endif() + if(NOT TARGET html) + add_custom_target(html) + endif() + if(NOT TARGET auxclean) + add_custom_target(auxclean) + endif() +endfunction(latex_setup_targets) + +function(latex_get_output_path var) + set(latex_output_path) + if(LATEX_OUTPUT_PATH) + get_filename_component( + LATEX_OUTPUT_PATH_FULL "${LATEX_OUTPUT_PATH}" ABSOLUTE + ) + if("${LATEX_OUTPUT_PATH_FULL}" STREQUAL "${CMAKE_CURRENT_SOURCE_DIR}") + message(SEND_ERROR "You cannot set LATEX_OUTPUT_PATH to the same directory that contains LaTeX input files.") + else() + set(latex_output_path "${LATEX_OUTPUT_PATH_FULL}") + endif() + else() + if("${CMAKE_CURRENT_BINARY_DIR}" STREQUAL "${CMAKE_CURRENT_SOURCE_DIR}") + message(SEND_ERROR "LaTeX files must be built out of source or you must set LATEX_OUTPUT_PATH.") + else() + set(latex_output_path "${CMAKE_CURRENT_BINARY_DIR}") + endif() + endif() + set(${var} ${latex_output_path} PARENT_SCOPE) +endfunction(latex_get_output_path) + +function(latex_add_convert_command + output_path + input_path + output_extension + input_extension + flags + ) + set(require_imagemagick_convert TRUE) + set(convert_flags "") + if(${input_extension} STREQUAL ".eps" AND ${output_extension} STREQUAL ".pdf") + # ImageMagick has broken eps to pdf conversion + # use ps2pdf instead + if(PS2PDF_CONVERTER) + set(require_imagemagick_convert FALSE) + set(converter ${PS2PDF_CONVERTER}) + set(convert_flags -dEPSCrop ${PS2PDF_CONVERTER_ARGS}) + else() + message(SEND_ERROR "Using postscript files with pdflatex requires ps2pdf for conversion.") + endif() + elseif(${input_extension} STREQUAL ".pdf" AND ${output_extension} STREQUAL ".eps") + # ImageMagick can also be sketchy on pdf to eps conversion. Not good with + # color spaces and tends to unnecessarily rasterize. + # use pdftops instead + if(PDFTOPS_CONVERTER) + set(require_imagemagick_convert FALSE) + set(converter ${PDFTOPS_CONVERTER}) + set(convert_flags -eps ${PDFTOPS_CONVERTER_ARGS}) + else() + message(STATUS "Consider getting pdftops from Poppler to convert PDF images to EPS images.") + set(convert_flags ${flags}) + endif() + else() + set(convert_flags ${flags}) + endif() + + if(require_imagemagick_convert) + if(IMAGEMAGICK_CONVERT) + string(TOLOWER ${IMAGEMAGICK_CONVERT} IMAGEMAGICK_CONVERT_LOWERCASE) + if(${IMAGEMAGICK_CONVERT_LOWERCASE} MATCHES "system32[/\\\\]convert\\.exe") + message(SEND_ERROR "IMAGEMAGICK_CONVERT set to Window's convert.exe for changing file systems rather than ImageMagick's convert for changing image formats. Please make sure ImageMagick is installed (available at http://www.imagemagick.org). If you have a recent version of ImageMagick (7.0 or higher), use the magick program instead of convert for IMAGEMAGICK_CONVERT.") + else() + set(converter ${IMAGEMAGICK_CONVERT}) + # ImageMagick requires a special order of arguments where resize and + # arguments of that nature must be placed after the input image path. + add_custom_command(OUTPUT ${output_path} + COMMAND ${converter} + ARGS ${input_path} ${convert_flags} ${output_path} + DEPENDS ${input_path} + ) + endif() + else() + message(SEND_ERROR "Could not find convert program. Please download ImageMagick from http://www.imagemagick.org and install.") + endif() + else() # Not ImageMagick convert + add_custom_command(OUTPUT ${output_path} + COMMAND ${converter} + ARGS ${convert_flags} ${input_path} ${output_path} + DEPENDS ${input_path} + ) + endif() +endfunction(latex_add_convert_command) + +# Makes custom commands to convert a file to a particular type. +function(latex_convert_image + output_files_var + input_file + output_extension + convert_flags + output_extensions + other_files + ) + set(output_file_list) + set(input_dir ${CMAKE_CURRENT_SOURCE_DIR}) + latex_get_output_path(output_dir) + + latex_get_filename_component(extension "${input_file}" EXT) + + # Check input filename for potential problems with LaTeX. + latex_get_filename_component(name "${input_file}" NAME_WE) + set(suggested_name "${name}") + if(suggested_name MATCHES ".*\\..*") + string(REPLACE "." "-" suggested_name "${suggested_name}") + endif() + if(suggested_name MATCHES ".* .*") + string(REPLACE " " "-" suggested_name "${suggested_name}") + endif() + if(NOT suggested_name STREQUAL name) + message(WARNING "Some LaTeX distributions have problems with image file names with multiple extensions or spaces. Consider changing ${name}${extension} to something like ${suggested_name}${extension}.") + endif() + + string(REGEX REPLACE "\\.[^.]*\$" ${output_extension} output_file + "${input_file}") + + latex_list_contains(is_type ${extension} ${output_extensions}) + if(is_type) + if(convert_flags) + latex_add_convert_command(${output_dir}/${output_file} + ${input_dir}/${input_file} ${output_extension} ${extension} + "${convert_flags}") + set(output_file_list ${output_dir}/${output_file}) + else() + # As a shortcut, we can just copy the file. + add_custom_command(OUTPUT ${output_dir}/${input_file} + COMMAND ${CMAKE_COMMAND} + ARGS -E copy ${input_dir}/${input_file} ${output_dir}/${input_file} + DEPENDS ${input_dir}/${input_file} + ) + set(output_file_list ${output_dir}/${input_file}) + endif() + else() + set(do_convert TRUE) + # Check to see if there is another input file of the appropriate type. + foreach(valid_extension ${output_extensions}) + string(REGEX REPLACE "\\.[^.]*\$" ${output_extension} try_file + "${input_file}") + latex_list_contains(has_native_file "${try_file}" ${other_files}) + if(has_native_file) + set(do_convert FALSE) + endif() + endforeach(valid_extension) + + # If we still need to convert, do it. + if(do_convert) + latex_add_convert_command(${output_dir}/${output_file} + ${input_dir}/${input_file} ${output_extension} ${extension} + "${convert_flags}") + set(output_file_list ${output_dir}/${output_file}) + endif() + endif() + + set(${output_files_var} ${output_file_list} PARENT_SCOPE) +endfunction(latex_convert_image) + +# Adds custom commands to process the given files for dvi and pdf builds. +# Adds the output files to the given variables (does not replace). +function(latex_process_images dvi_outputs_var pdf_outputs_var) + latex_get_output_path(output_dir) + set(dvi_outputs) + set(pdf_outputs) + foreach(file ${ARGN}) + if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${file}") + latex_get_filename_component(extension "${file}" EXT) + set(convert_flags) + + # Check to see if we need to downsample the image. + latex_list_contains(is_raster "${extension}" + ${LATEX_RASTER_IMAGE_EXTENSIONS}) + if(LATEX_SMALL_IMAGES) + if(is_raster) + set(convert_flags -resize ${LATEX_RASTER_SCALE}%) + endif() + endif() + + # Make sure the output directory exists. + latex_get_filename_component(path "${output_dir}/${file}" PATH) + make_directory("${path}") + + # Do conversions for dvi. + latex_convert_image(output_files "${file}" .eps "${convert_flags}" + "${LATEX_DVI_IMAGE_EXTENSIONS}" "${ARGN}") + list(APPEND dvi_outputs ${output_files}) + + # Do conversions for pdf. + if(is_raster) + latex_convert_image(output_files "${file}" .png "${convert_flags}" + "${LATEX_PDF_IMAGE_EXTENSIONS}" "${ARGN}") + list(APPEND pdf_outputs ${output_files}) + else() + latex_convert_image(output_files "${file}" .pdf "${convert_flags}" + "${LATEX_PDF_IMAGE_EXTENSIONS}" "${ARGN}") + list(APPEND pdf_outputs ${output_files}) + endif() + else() + message(WARNING "Could not find file ${CMAKE_CURRENT_SOURCE_DIR}/${file}. Are you sure you gave relative paths to IMAGES?") + endif() + endforeach(file) + + set(${dvi_outputs_var} ${dvi_outputs} PARENT_SCOPE) + set(${pdf_outputs_var} ${pdf_outputs} PARENT_SCOPE) +endfunction(latex_process_images) + +function(latex_copy_globbed_files pattern dest) + file(GLOB file_list ${pattern}) + foreach(in_file ${file_list}) + latex_get_filename_component(out_file ${in_file} NAME) + configure_file(${in_file} ${dest}/${out_file} COPYONLY) + endforeach(in_file) +endfunction(latex_copy_globbed_files) + +function(latex_copy_input_file file) + latex_get_output_path(output_dir) + + if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${file}) + latex_get_filename_component(path ${file} PATH) + file(MAKE_DIRECTORY ${output_dir}/${path}) + + latex_list_contains(use_config ${file} ${LATEX_CONFIGURE}) + if(use_config) + configure_file(${CMAKE_CURRENT_SOURCE_DIR}/${file} + ${output_dir}/${file} + @ONLY + ) + add_custom_command(OUTPUT ${output_dir}/${file} + COMMAND ${CMAKE_COMMAND} + ARGS ${CMAKE_BINARY_DIR} + DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${file} + ) + else() + add_custom_command(OUTPUT ${output_dir}/${file} + COMMAND ${CMAKE_COMMAND} + ARGS -E copy ${CMAKE_CURRENT_SOURCE_DIR}/${file} ${output_dir}/${file} + DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${file} + ) + endif() + else() + if(EXISTS ${output_dir}/${file}) + # Special case: output exists but input does not. Assume that it was + # created elsewhere and skip the input file copy. + else() + message("Could not find input file ${CMAKE_CURRENT_SOURCE_DIR}/${file}") + endif() + endif() +endfunction(latex_copy_input_file) + +############################################################################# +# Commands provided by the UseLATEX.cmake "package" +############################################################################# + +function(latex_usage command message) + message(SEND_ERROR + "${message}\n Usage: ${command}(\n [BIBFILES ...]\n [INPUTS ...]\n [IMAGE_DIRS ...]\n [IMAGES \n [CONFIGURE ...]\n [DEPENDS ...]\n [MULTIBIB_NEWCITES] \n [USE_BIBLATEX] [USE_INDEX] [USE_GLOSSARY] [USE_NOMENCL]\n [FORCE_PDF] [FORCE_DVI] [FORCE_HTML]\n [TARGET_NAME] \n [EXCLUDE_FROM_ALL]\n [EXCLUDE_FROM_DEFAULTS])" + ) +endfunction(latex_usage command message) + +# Parses arguments to add_latex_document and ADD_LATEX_TARGETS and sets the +# variables LATEX_TARGET, LATEX_IMAGE_DIR, LATEX_BIBFILES, LATEX_DEPENDS, and +# LATEX_INPUTS. +function(parse_add_latex_arguments command latex_main_input) + set(options + USE_BIBLATEX + USE_INDEX + USE_GLOSSARY + USE_NOMENCL + FORCE_PDF + FORCE_DVI + FORCE_HTML + EXCLUDE_FROM_ALL + EXCLUDE_FROM_DEFAULTS + # Deprecated options + USE_GLOSSARIES + DEFAULT_PDF + DEFAULT_SAFEPDF + DEFAULT_PS + NO_DEFAULT + MANGLE_TARGET_NAMES + ) + set(oneValueArgs + TARGET_NAME + ) + set(multiValueArgs + BIBFILES + MULTIBIB_NEWCITES + INPUTS + IMAGE_DIRS + IMAGES + CONFIGURE + DEPENDS + INDEX_NAMES + ) + cmake_parse_arguments( + LATEX "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) + + # Handle invalid and deprecated arguments + if(LATEX_UNPARSED_ARGUMENTS) + latex_usage(${command} "Invalid or deprecated arguments: ${LATEX_UNPARSED_ARGUMENTS}") + endif() + if(LATEX_USE_GLOSSARIES) + latex_usage(${command} "USE_GLOSSARIES option removed in version 1.6.1. Use USE_GLOSSARY instead.") + endif() + if(LATEX_DEFAULT_PDF) + latex_usage(${command} "DEFAULT_PDF option removed in version 2.0. Use FORCE_PDF option or LATEX_DEFAULT_BUILD CMake variable instead.") + endif() + if(LATEX_DEFAULT_SAFEPDF) + latex_usage(${command} "DEFAULT_SAFEPDF option removed in version 2.0. Use LATEX_DEFAULT_BUILD CMake variable instead.") + endif() + if(LATEX_DEFAULT_DVI) + latex_usage(${command} "DEFAULT_DVI option removed in version 2.0. Use FORCE_DVI option or LATEX_DEFAULT_BUILD CMake variable instead.") + endif() + if(LATEX_NO_DEFAULT) + latex_usage(${command} "NO_DEFAULT option removed in version 2.0. Use EXCLUDE_FROM_ALL instead.") + endif() + if(LATEX_MANGLE_TARGET_NAMES) + latex_usage(${command} "MANGLE_TARGET_NAMES option removed in version 2.0. All LaTeX targets use mangled names now.") + endif() + + # Capture the first argument, which is the main LaTeX input. + latex_get_filename_component(latex_target ${latex_main_input} NAME_WE) + set(LATEX_MAIN_INPUT ${latex_main_input} PARENT_SCOPE) + set(LATEX_TARGET ${latex_target} PARENT_SCOPE) + + # Propagate the result variables to the caller + foreach(arg_name ${options} ${oneValueArgs} ${multiValueArgs}) + set(var_name LATEX_${arg_name}) + set(${var_name} ${${var_name}} PARENT_SCOPE) + endforeach(arg_name) +endfunction(parse_add_latex_arguments) + +function(add_latex_targets_internal) + latex_get_output_path(output_dir) + + if(LATEX_USE_SYNCTEX) + set(synctex_flags ${LATEX_SYNCTEX_ARGS}) + else() + set(synctex_flags) + endif() + + # The commands to run LaTeX. They are repeated multiple times. + set(latex_build_command + ${LATEX_COMPILER} ${LATEX_COMPILER_ARGS} ${synctex_flags} ${LATEX_MAIN_INPUT} + ) + if(LATEX_COMPILER_ARGS MATCHES ".*batchmode.*") + # Wrap command in script that dumps the log file on error. This makes sure + # errors can be seen. + set(latex_build_command + ${CMAKE_COMMAND} + -D LATEX_BUILD_COMMAND=execute_latex + -D LATEX_TARGET=${LATEX_TARGET} + -D LATEX_WORKING_DIRECTORY="${output_dir}" + -D LATEX_FULL_COMMAND="${latex_build_command}" + -D LATEX_OUTPUT_FILE="${LATEX_TARGET}.dvi" + -P "${LATEX_USE_LATEX_LOCATION}" + ) + endif() + set(pdflatex_build_command + ${PDFLATEX_COMPILER} ${PDFLATEX_COMPILER_ARGS} ${synctex_flags} ${LATEX_MAIN_INPUT} + ) + if(PDFLATEX_COMPILER_ARGS MATCHES ".*batchmode.*") + # Wrap command in script that dumps the log file on error. This makes sure + # errors can be seen. + set(pdflatex_build_command + ${CMAKE_COMMAND} + -D LATEX_BUILD_COMMAND=execute_latex + -D LATEX_TARGET=${LATEX_TARGET} + -D LATEX_WORKING_DIRECTORY="${output_dir}" + -D LATEX_FULL_COMMAND="${pdflatex_build_command}" + -D LATEX_OUTPUT_FILE="${LATEX_TARGET}.pdf" + -P "${LATEX_USE_LATEX_LOCATION}" + ) + endif() + + if(NOT LATEX_TARGET_NAME) + # Use the main filename (minus the .tex) as the target name. Remove any + # spaces since CMake cannot have spaces in its target names. + string(REPLACE " " "_" LATEX_TARGET_NAME ${LATEX_TARGET}) + endif() + + # Some LaTeX commands may need to be modified (or may not work) if the main + # tex file is in a subdirectory. Make a flag for that. + get_filename_component(LATEX_MAIN_INPUT_SUBDIR ${LATEX_MAIN_INPUT} DIRECTORY) + + # Set up target names. + set(dvi_target ${LATEX_TARGET_NAME}_dvi) + set(pdf_target ${LATEX_TARGET_NAME}_pdf) + set(ps_target ${LATEX_TARGET_NAME}_ps) + set(safepdf_target ${LATEX_TARGET_NAME}_safepdf) + set(html_target ${LATEX_TARGET_NAME}_html) + set(auxclean_target ${LATEX_TARGET_NAME}_auxclean) + + # Probably not all of these will be generated, but they could be. + # Note that the aux file is added later. + set(auxiliary_clean_files + ${output_dir}/${LATEX_TARGET}.aux + ${output_dir}/${LATEX_TARGET}.bbl + ${output_dir}/${LATEX_TARGET}.blg + ${output_dir}/${LATEX_TARGET}-blx.bib + ${output_dir}/${LATEX_TARGET}.glg + ${output_dir}/${LATEX_TARGET}.glo + ${output_dir}/${LATEX_TARGET}.gls + ${output_dir}/${LATEX_TARGET}.idx + ${output_dir}/${LATEX_TARGET}.ilg + ${output_dir}/${LATEX_TARGET}.ind + ${output_dir}/${LATEX_TARGET}.ist + ${output_dir}/${LATEX_TARGET}.log + ${output_dir}/${LATEX_TARGET}.out + ${output_dir}/${LATEX_TARGET}.toc + ${output_dir}/${LATEX_TARGET}.lof + ${output_dir}/${LATEX_TARGET}.xdy + ${output_dir}/${LATEX_TARGET}.synctex.gz + ${output_dir}/${LATEX_TARGET}.synctex.bak.gz + ${output_dir}/${LATEX_TARGET}.dvi + ${output_dir}/${LATEX_TARGET}.ps + ${output_dir}/${LATEX_TARGET}.pdf + ) + + set(image_list ${LATEX_IMAGES}) + + # For each directory in LATEX_IMAGE_DIRS, glob all the image files and + # place them in LATEX_IMAGES. + foreach(dir ${LATEX_IMAGE_DIRS}) + if(NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${dir}) + message(WARNING "Image directory ${CMAKE_CURRENT_SOURCE_DIR}/${dir} does not exist. Are you sure you gave relative directories to IMAGE_DIRS?") + endif() + foreach(extension ${LATEX_IMAGE_EXTENSIONS}) + file(GLOB files ${CMAKE_CURRENT_SOURCE_DIR}/${dir}/*${extension}) + foreach(file ${files}) + latex_get_filename_component(filename ${file} NAME) + list(APPEND image_list ${dir}/${filename}) + endforeach(file) + endforeach(extension) + endforeach(dir) + + latex_process_images(dvi_images pdf_images ${image_list}) + + set(make_dvi_command + ${CMAKE_COMMAND} -E chdir ${output_dir} + ${latex_build_command}) + set(make_pdf_command + ${CMAKE_COMMAND} -E chdir ${output_dir} + ${pdflatex_build_command} + ) + + set(make_dvi_depends ${LATEX_DEPENDS} ${dvi_images}) + set(make_pdf_depends ${LATEX_DEPENDS} ${pdf_images}) + foreach(input ${LATEX_MAIN_INPUT} ${LATEX_INPUTS}) + list(APPEND make_dvi_depends ${output_dir}/${input}) + list(APPEND make_pdf_depends ${output_dir}/${input}) + if(${input} MATCHES "\\.tex$") + # Dependent .tex files might have their own .aux files created. Make + # sure these get cleaned as well. This might replicate the cleaning + # of the main .aux file, which is OK. + string(REGEX REPLACE "\\.tex$" "" input_we ${input}) + list(APPEND auxiliary_clean_files + ${output_dir}/${input_we}.aux + ${output_dir}/${input}.aux + ) + endif() + endforeach(input) + + set(all_latex_sources ${LATEX_MAIN_INPUT} ${LATEX_INPUTS} ${image_list}) + + if(LATEX_USE_GLOSSARY) + foreach(dummy 0 1) # Repeat these commands twice. + set(make_dvi_command ${make_dvi_command} + COMMAND ${CMAKE_COMMAND} -E chdir ${output_dir} + ${CMAKE_COMMAND} + -D LATEX_BUILD_COMMAND=makeglossaries + -D LATEX_TARGET=${LATEX_TARGET} + -D MAKEINDEX_COMPILER=${MAKEINDEX_COMPILER} + -D XINDY_COMPILER=${XINDY_COMPILER} + -D MAKEGLOSSARIES_COMPILER_ARGS=${MAKEGLOSSARIES_COMPILER_ARGS} + -P ${LATEX_USE_LATEX_LOCATION} + COMMAND ${CMAKE_COMMAND} -E chdir ${output_dir} + ${latex_build_command} + ) + set(make_pdf_command ${make_pdf_command} + COMMAND ${CMAKE_COMMAND} -E chdir ${output_dir} + ${CMAKE_COMMAND} + -D LATEX_BUILD_COMMAND=makeglossaries + -D LATEX_TARGET=${LATEX_TARGET} + -D MAKEINDEX_COMPILER=${MAKEINDEX_COMPILER} + -D XINDY_COMPILER=${XINDY_COMPILER} + -D MAKEGLOSSARIES_COMPILER_ARGS=${MAKEGLOSSARIES_COMPILER_ARGS} + -P ${LATEX_USE_LATEX_LOCATION} + COMMAND ${CMAKE_COMMAND} -E chdir ${output_dir} + ${pdflatex_build_command} + ) + endforeach(dummy) + endif() + + if(LATEX_USE_NOMENCL) + foreach(dummy 0 1) # Repeat these commands twice. + set(make_dvi_command ${make_dvi_command} + COMMAND ${CMAKE_COMMAND} -E chdir ${output_dir} + ${CMAKE_COMMAND} + -D LATEX_BUILD_COMMAND=makenomenclature + -D LATEX_TARGET=${LATEX_TARGET} + -D MAKEINDEX_COMPILER=${MAKEINDEX_COMPILER} + -D MAKENOMENCLATURE_COMPILER_ARGS=${MAKENOMENCLATURE_COMPILER_ARGS} + -P ${LATEX_USE_LATEX_LOCATION} + COMMAND ${CMAKE_COMMAND} -E chdir ${output_dir} + ${latex_build_command} + ) + set(make_pdf_command ${make_pdf_command} + COMMAND ${CMAKE_COMMAND} -E chdir ${output_dir} + ${CMAKE_COMMAND} + -D LATEX_BUILD_COMMAND=makenomenclature + -D LATEX_TARGET=${LATEX_TARGET} + -D MAKEINDEX_COMPILER=${MAKEINDEX_COMPILER} + -D MAKENOMENCLATURE_COMPILER_ARGS=${MAKENOMENCLATURE_COMPILER_ARGS} + -P ${LATEX_USE_LATEX_LOCATION} + COMMAND ${CMAKE_COMMAND} -E chdir ${output_dir} + ${pdflatex_build_command} + ) + endforeach(dummy) + endif() + + if(LATEX_BIBFILES) + if(LATEX_USE_BIBLATEX) + if(NOT BIBER_COMPILER) + message(SEND_ERROR "I need the biber command.") + endif() + set(bib_compiler ${BIBER_COMPILER}) + set(bib_compiler_flags ${BIBER_COMPILER_ARGS}) + else() + set(bib_compiler ${BIBTEX_COMPILER}) + set(bib_compiler_flags ${BIBTEX_COMPILER_ARGS}) + endif() + if(LATEX_MULTIBIB_NEWCITES) + foreach (multibib_auxfile ${LATEX_MULTIBIB_NEWCITES}) + latex_get_filename_component(multibib_target ${multibib_auxfile} NAME_WE) + set(make_dvi_command ${make_dvi_command} + COMMAND ${CMAKE_COMMAND} -E chdir ${output_dir} + ${bib_compiler} ${bib_compiler_flags} ${multibib_target}) + set(make_pdf_command ${make_pdf_command} + COMMAND ${CMAKE_COMMAND} -E chdir ${output_dir} + ${bib_compiler} ${bib_compiler_flags} ${multibib_target}) + set(auxiliary_clean_files ${auxiliary_clean_files} + ${output_dir}/${multibib_target}.aux) + endforeach (multibib_auxfile ${LATEX_MULTIBIB_NEWCITES}) + else() + set(make_dvi_command ${make_dvi_command} + COMMAND ${CMAKE_COMMAND} -E chdir ${output_dir} + ${bib_compiler} ${bib_compiler_flags} ${LATEX_TARGET}) + set(make_pdf_command ${make_pdf_command} + COMMAND ${CMAKE_COMMAND} -E chdir ${output_dir} + ${bib_compiler} ${bib_compiler_flags} ${LATEX_TARGET}) + endif() + + foreach (bibfile ${LATEX_BIBFILES}) + list(APPEND make_dvi_depends ${output_dir}/${bibfile}) + list(APPEND make_pdf_depends ${output_dir}/${bibfile}) + endforeach (bibfile ${LATEX_BIBFILES}) + else() + if(LATEX_MULTIBIB_NEWCITES) + message(WARNING "MULTIBIB_NEWCITES has no effect without BIBFILES option.") + endif() + endif() + + if(LATEX_USE_INDEX) + if(LATEX_INDEX_NAMES) + set(INDEX_NAMES ${LATEX_INDEX_NAMES}) + else() + set(INDEX_NAMES ${LATEX_TARGET}) + endif() + foreach(idx_name ${INDEX_NAMES}) + set(make_dvi_command ${make_dvi_command} + COMMAND ${CMAKE_COMMAND} -E chdir ${output_dir} + ${latex_build_command} + COMMAND ${CMAKE_COMMAND} -E chdir ${output_dir} + ${MAKEINDEX_COMPILER} ${MAKEINDEX_COMPILER_ARGS} ${idx_name}.idx) + set(make_pdf_command ${make_pdf_command} + COMMAND ${CMAKE_COMMAND} -E chdir ${output_dir} + ${pdflatex_build_command} + COMMAND ${CMAKE_COMMAND} -E chdir ${output_dir} + ${MAKEINDEX_COMPILER} ${MAKEINDEX_COMPILER_ARGS} ${idx_name}.idx) + set(auxiliary_clean_files ${auxiliary_clean_files} + ${output_dir}/${idx_name}.idx + ${output_dir}/${idx_name}.ilg + ${output_dir}/${idx_name}.ind) + endforeach() + else() + if(LATEX_INDEX_NAMES) + message(WARNING "INDEX_NAMES has no effect without USE_INDEX option.") + endif() + endif() + + set(make_dvi_command ${make_dvi_command} + COMMAND ${CMAKE_COMMAND} -E chdir ${output_dir} + ${latex_build_command} + COMMAND ${CMAKE_COMMAND} -E chdir ${output_dir} + ${latex_build_command}) + set(make_pdf_command ${make_pdf_command} + COMMAND ${CMAKE_COMMAND} -E chdir ${output_dir} + ${pdflatex_build_command} + COMMAND ${CMAKE_COMMAND} -E chdir ${output_dir} + ${pdflatex_build_command}) + + # Need to run one more time to remove biblatex' warning + # about page breaks that have changed. + if(LATEX_USE_BIBLATEX) + set(make_dvi_command ${make_dvi_command} + COMMAND ${CMAKE_COMMAND} -E chdir ${output_dir} + ${latex_build_command}) + set(make_pdf_command ${make_pdf_command} + COMMAND ${CMAKE_COMMAND} -E chdir ${output_dir} + ${pdflatex_build_command}) + endif() + + if(LATEX_USE_SYNCTEX) + if(NOT GZIP) + message(SEND_ERROR "UseLATEX.cmake: USE_SYNTEX option requires gzip program. Set GZIP variable.") + endif() + set(make_dvi_command ${make_dvi_command} + COMMAND ${CMAKE_COMMAND} + -D LATEX_BUILD_COMMAND=correct_synctex + -D LATEX_TARGET=${LATEX_TARGET} + -D GZIP=${GZIP} + -D "LATEX_SOURCE_DIRECTORY=${CMAKE_CURRENT_SOURCE_DIR}" + -D "LATEX_BINARY_DIRECTORY=${output_dir}" + -P ${LATEX_USE_LATEX_LOCATION} + ) + set(make_pdf_command ${make_pdf_command} + COMMAND ${CMAKE_COMMAND} + -D LATEX_BUILD_COMMAND=correct_synctex + -D LATEX_TARGET=${LATEX_TARGET} + -D GZIP=${GZIP} + -D "LATEX_SOURCE_DIRECTORY=${CMAKE_CURRENT_SOURCE_DIR}" + -D "LATEX_BINARY_DIRECTORY=${output_dir}" + -P ${LATEX_USE_LATEX_LOCATION} + ) + endif() + + # Check LaTeX output for important warnings at end of build + set(make_dvi_command ${make_dvi_command} + COMMAND ${CMAKE_COMMAND} -E chdir ${output_dir} + ${CMAKE_COMMAND} + -D LATEX_BUILD_COMMAND=check_important_warnings + -D LATEX_TARGET=${LATEX_TARGET} + -P ${LATEX_USE_LATEX_LOCATION} + ) + set(make_pdf_command ${make_pdf_command} + COMMAND ${CMAKE_COMMAND} -E chdir ${output_dir} + ${CMAKE_COMMAND} + -D LATEX_BUILD_COMMAND=check_important_warnings + -D LATEX_TARGET=${LATEX_TARGET} + -P ${LATEX_USE_LATEX_LOCATION} + ) + + # Capture the default build. + string(TOLOWER "${LATEX_DEFAULT_BUILD}" default_build) + + if((NOT LATEX_FORCE_PDF) AND (NOT LATEX_FORCE_DVI) AND (NOT LATEX_FORCE_HTML)) + set(no_force TRUE) + endif() + + # Add commands and targets for building pdf outputs (with pdflatex). + if(LATEX_FORCE_PDF OR no_force) + if(LATEX_FORCE_PDF) + set(default_build pdf) + endif() + + if(PDFLATEX_COMPILER) + add_custom_command(OUTPUT ${output_dir}/${LATEX_TARGET}.pdf + COMMAND ${make_pdf_command} + DEPENDS ${make_pdf_depends} + ) + add_custom_target(${pdf_target} + DEPENDS ${output_dir}/${LATEX_TARGET}.pdf + SOURCES ${all_latex_sources} + ) + if(NOT LATEX_EXCLUDE_FROM_DEFAULTS) + add_dependencies(pdf ${pdf_target}) + endif() + endif() + endif() + + # Add commands and targets for building dvi outputs. + if(LATEX_FORCE_DVI OR LATEX_FORCE_HTML OR no_force) + if(LATEX_FORCE_DVI) + if((NOT default_build STREQUAL dvi) AND + (NOT default_build STREQUAL ps) AND + (NOT default_build STREQUAL safepdf)) + set(default_build dvi) + endif() + endif() + + add_custom_command(OUTPUT ${output_dir}/${LATEX_TARGET}.dvi + COMMAND ${make_dvi_command} + DEPENDS ${make_dvi_depends} + ) + add_custom_target(${dvi_target} + DEPENDS ${output_dir}/${LATEX_TARGET}.dvi + SOURCES ${all_latex_sources} + ) + if(NOT LATEX_EXCLUDE_FROM_DEFAULTS) + add_dependencies(dvi ${dvi_target}) + endif() + + if(DVIPS_CONVERTER) + add_custom_command(OUTPUT ${output_dir}/${LATEX_TARGET}.ps + COMMAND ${CMAKE_COMMAND} -E chdir ${output_dir} + ${DVIPS_CONVERTER} ${DVIPS_CONVERTER_ARGS} -o ${LATEX_TARGET}.ps ${LATEX_TARGET}.dvi + DEPENDS ${output_dir}/${LATEX_TARGET}.dvi) + add_custom_target(${ps_target} + DEPENDS ${output_dir}/${LATEX_TARGET}.ps + SOURCES ${all_latex_sources} + ) + if(NOT LATEX_EXCLUDE_FROM_DEFAULTS) + add_dependencies(ps ${ps_target}) + endif() + if(PS2PDF_CONVERTER) + # Since both the pdf and safepdf targets have the same output, we + # cannot properly do the dependencies for both. When selecting safepdf, + # simply force a recompile every time. + add_custom_target(${safepdf_target} + ${CMAKE_COMMAND} -E chdir ${output_dir} + ${PS2PDF_CONVERTER} ${PS2PDF_CONVERTER_ARGS} ${LATEX_TARGET}.ps ${LATEX_TARGET}.pdf + DEPENDS ${ps_target} + ) + if(NOT LATEX_EXCLUDE_FROM_DEFAULTS) + add_dependencies(safepdf ${safepdf_target}) + endif() + endif() + endif() + endif() + + if(LATEX_FORCE_HTML OR no_force) + if (LATEX_FORCE_HTML) + set(default_build html) + endif() + + if(HTLATEX_COMPILER AND LATEX_MAIN_INPUT_SUBDIR) + message(STATUS + "Disabling HTML build for ${LATEX_TARGET_NAME}.tex because the main file is in subdirectory ${LATEX_MAIN_INPUT_SUBDIR}" + ) + # The code below to run HTML assumes that LATEX_TARGET.tex is in the + # current directory. I have tried to specify that LATEX_TARGET.tex is + # in a subdirectory. That makes the build targets correct, but the + # HTML build still fails (at least for htlatex) because files are not + # generated where expected. I am getting around the problem by simply + # disabling HTML in this case. If someone really cares, they can fix + # this, but make sure it runs on many platforms and build programs. + elseif(HTLATEX_COMPILER) + # htlatex places the output in a different location + set(HTML_OUTPUT "${output_dir}/${LATEX_TARGET}.html") + add_custom_command(OUTPUT ${HTML_OUTPUT} + COMMAND ${CMAKE_COMMAND} -E chdir ${output_dir} + ${HTLATEX_COMPILER} ${LATEX_MAIN_INPUT} + "${HTLATEX_COMPILER_TEX4HT_FLAGS}" + "${HTLATEX_COMPILER_TEX4HT_POSTPROCESSOR_FLAGS}" + "${HTLATEX_COMPILER_T4HT_POSTPROCESSOR_FLAGS}" + ${HTLATEX_COMPILER_ARGS} + DEPENDS + ${output_dir}/${LATEX_TARGET}.tex + ${output_dir}/${LATEX_TARGET}.dvi + VERBATIM + ) + add_custom_target(${html_target} + DEPENDS ${HTML_OUTPUT} ${dvi_target} + SOURCES ${all_latex_sources} + ) + if(NOT LATEX_EXCLUDE_FROM_DEFAULTS) + add_dependencies(html ${html_target}) + endif() + endif() + endif() + + # Set default targets. + if("${default_build}" STREQUAL "pdf") + add_custom_target(${LATEX_TARGET_NAME} DEPENDS ${pdf_target}) + elseif("${default_build}" STREQUAL "dvi") + add_custom_target(${LATEX_TARGET_NAME} DEPENDS ${dvi_target}) + elseif("${default_build}" STREQUAL "ps") + add_custom_target(${LATEX_TARGET_NAME} DEPENDS ${ps_target}) + elseif("${default_build}" STREQUAL "safepdf") + add_custom_target(${LATEX_TARGET_NAME} DEPENDS ${safepdf_target}) + elseif("${default_build}" STREQUAL "html") + add_custom_target(${LATEX_TARGET_NAME} DEPENDS ${html_target}) + else() + message(SEND_ERROR "LATEX_DEFAULT_BUILD set to an invalid value. See the documentation for that variable.") + endif() + + if(NOT LATEX_EXCLUDE_FROM_ALL) + add_custom_target(_${LATEX_TARGET_NAME} ALL DEPENDS ${LATEX_TARGET_NAME}) + endif() + + set_directory_properties(. + ADDITIONAL_MAKE_CLEAN_FILES "${auxiliary_clean_files}" + ) + + add_custom_target(${auxclean_target} + COMMENT "Cleaning auxiliary LaTeX files." + COMMAND ${CMAKE_COMMAND} -E remove ${auxiliary_clean_files} + ) + add_dependencies(auxclean ${auxclean_target}) +endfunction(add_latex_targets_internal) + +function(add_latex_targets latex_main_input) + latex_get_output_path(output_dir) + parse_add_latex_arguments(ADD_LATEX_TARGETS ${latex_main_input} ${ARGN}) + + add_latex_targets_internal() +endfunction(add_latex_targets) + +function(add_latex_document latex_main_input) + latex_get_output_path(output_dir) + if(output_dir) + parse_add_latex_arguments(add_latex_document ${latex_main_input} ${ARGN}) + + latex_copy_input_file(${LATEX_MAIN_INPUT}) + + foreach (bib_file ${LATEX_BIBFILES}) + latex_copy_input_file(${bib_file}) + endforeach (bib_file) + + foreach (input ${LATEX_INPUTS}) + latex_copy_input_file(${input}) + endforeach(input) + + latex_copy_globbed_files(${CMAKE_CURRENT_SOURCE_DIR}/*.cls ${output_dir}) + latex_copy_globbed_files(${CMAKE_CURRENT_SOURCE_DIR}/*.bst ${output_dir}) + latex_copy_globbed_files(${CMAKE_CURRENT_SOURCE_DIR}/*.clo ${output_dir}) + latex_copy_globbed_files(${CMAKE_CURRENT_SOURCE_DIR}/*.sty ${output_dir}) + latex_copy_globbed_files(${CMAKE_CURRENT_SOURCE_DIR}/*.ist ${output_dir}) + latex_copy_globbed_files(${CMAKE_CURRENT_SOURCE_DIR}/*.fd ${output_dir}) + + add_latex_targets_internal() + endif() +endfunction(add_latex_document) + +############################################################################# +# Actually do stuff +############################################################################# + +if(LATEX_BUILD_COMMAND) + set(command_handled) + + if("${LATEX_BUILD_COMMAND}" STREQUAL execute_latex) + latex_execute_latex() + set(command_handled TRUE) + endif() + + if("${LATEX_BUILD_COMMAND}" STREQUAL makeglossaries) + latex_makeglossaries() + set(command_handled TRUE) + endif() + + if("${LATEX_BUILD_COMMAND}" STREQUAL makenomenclature) + latex_makenomenclature() + set(command_handled TRUE) + endif() + + if("${LATEX_BUILD_COMMAND}" STREQUAL correct_synctex) + latex_correct_synctex() + set(command_handled TRUE) + endif() + + if("${LATEX_BUILD_COMMAND}" STREQUAL check_important_warnings) + latex_check_important_warnings() + set(command_handled TRUE) + endif() + + if(NOT command_handled) + message(SEND_ERROR "Unknown command: ${LATEX_BUILD_COMMAND}") + endif() + +else() + # Must be part of the actual configure (included from CMakeLists.txt). + latex_setup_variables() + latex_setup_targets() +endif() diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 00000000..0e3638cd --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,452 @@ +cmake_minimum_required(VERSION 3.0.2 FATAL_ERROR) + +project(TON VERSION 0.5 LANGUAGES C CXX) +set(CMAKE_POSITION_INDEPENDENT_CODE ON) +#set(OPENSSL_USE_STATIC_LIBS TRUE) + +# Prevent in-source build +get_filename_component(TON_REAL_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}" REALPATH) +get_filename_component(TON_REAL_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}" REALPATH) + +if (TON_REAL_BINARY_DIR STREQUAL TON_REAL_SOURCE_DIR) + message(" Out-of-source build should be used to build TDLib.") + message(" You need to remove the files already created by CMake and") + message(" rerun CMake from a new directory:") + message(" rm -rf CMakeFiles CMakeCache.txt") + message(" mkdir build") + message(" cd build") + message(" cmake ..") + message(FATAL_ERROR "In-source build failed.") +endif() + +#BEGIN internal +option(TON_USE_ROCKSDB "Use \"ON\" to enable RocksDb." ON) +option(TON_USE_ABSEIL "Use \"ON\" to enable Abseil." ON) +option(TON_USE_JEMALLOC "Use \"ON\" to enable JeMalloc." OFF) +#END internal + +option(TONLIB_ENABLE_JNI "Use \"ON\" to enable JNI-compatible TonLib API.") +option(TON_USE_ASAN "Use \"ON\" to enable AddressSanitizer." OFF) +option(TON_USE_TSAN "Use \"ON\" to enable ThreadSanitizer." OFF) +option(TON_USE_UBSAN "Use \"ON\" to enable UndefinedBehaviorSanitizer." OFF) +set(TON_ARCH "native" CACHE STRING "Architecture, will be passed to -march=") + +if (TON_USE_ABSEIL) + message("Add abseil-cpp") + add_subdirectory(third-party/abseil-cpp EXCLUDE_FROM_ALL) + set(ABSL_FOUND 1) +endif() + +#add_subdirectory(third-party/libcuckoo EXCLUDE_FROM_ALL) +#add_subdirectory(third-party/junction EXCLUDE_FROM_ALL) + +if (WIN32) + message("Add wingetopt") + add_subdirectory(third-party/wingetopt EXCLUDE_FROM_ALL) + set(WINGETOPT_FOUND 1) + message(STATUS "Use wingetopt") +endif() + +set(CRC32C_BUILD_TESTS OFF CACHE BOOL "Build CRC32C's unit tests") +set(CRC32C_BUILD_BENCHMARKS OFF CACHE BOOL "Build CRC32C's benchmarks") +set(CRC32C_USE_GLOG OFF CACHE BOOL "Build CRC32C's tests with Google Logging") +set(CRC32C_INSTALL OFF CACHE BOOL "Install CRC32C's header and library") +message("Add crc32c") +add_subdirectory(third-party/crc32c EXCLUDE_FROM_ALL) +set(CRC32C_FOUND 1) + +if (TON_USE_ROCKSDB) + if (ANDROID) + set(PORTABLE ON CACHE BOOL "portable") + endif() + set(WITH_TESTS OFF CACHE BOOL "build with tests") + set(WITH_TOOLS OFF CACHE BOOL "build with tools") + set(FAIL_ON_WARNINGS OFF CACHE BOOL "fail on warnings") + message("Add rocksdb") + add_subdirectory(third-party/rocksdb EXCLUDE_FROM_ALL) +endif() + +option(USE_COROUTINES "experimental support of coroutines" OFF) +if (USE_COROUTINES) + if (CMAKE_CXX_COMPILER_ID MATCHES "Clang") + set(TD_HAVE_COROUTINES 1) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fcoroutines-ts") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++") + endif() +endif() + +option(USE_LIBRAPTORQ "use libraptorq for tests" OFF) +if (USE_LIBRAPTORQ) + set(USE_LZ4 OFF CACHE BOOL "use lz4") + if (CMAKE_CXX_COMPILER_ID MATCHES "Clang") + set(CLANG_STDLIB "ON") # for libraptorq + endif() + message("Add libraptorq") + add_subdirectory(third-party/libraptorq EXCLUDE_FROM_ALL) +endif() + +message("Add ton") +set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/CMake" ${CMAKE_MODULE_PATH}) + +# Configure CCache if available +find_program(CCACHE_FOUND ccache) +#set(CCACHE_FOUND 0) +if (CCACHE_FOUND) + message(STATUS "Found ccache") + set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ccache) + set_property(GLOBAL PROPERTY RULE_LAUNCH_LINK ccache) +else() + message(STATUS "Could NOT find ccache") +endif() + +if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU") + set(GCC 1) +elseif (CMAKE_CXX_COMPILER_ID MATCHES "Clang") + set(CLANG 1) +elseif (CMAKE_CXX_COMPILER_ID STREQUAL "Intel") + set(INTEL 1) +elseif (NOT MSVC) + message(FATAL_ERROR "Compiler isn't supported") +endif() + +include(CheckCXXCompilerFlag) + +if (GCC OR CLANG OR INTEL) + if (WIN32 AND INTEL) + set(STD14_FLAG /Qstd=c++14) + else() + set(STD14_FLAG -std=c++14) + endif() + check_cxx_compiler_flag(${STD14_FLAG} HAVE_STD14) + if (NOT HAVE_STD14) + string(REPLACE "c++14" "c++1y" STD14_FLAG "${STD14_FLAG}") + check_cxx_compiler_flag(${STD14_FLAG} HAVE_STD1Y) + set(HAVE_STD14 ${HAVE_STD1Y}) + endif() +elseif (MSVC) + set(HAVE_STD14 MSVC_VERSION>=1900) +endif() + +if (NOT HAVE_STD14) + message(FATAL_ERROR "No C++14 support in the compiler. Please upgrade the compiler.") +endif() + +set(CMAKE_THREAD_PREFER_PTHREAD ON) +set(THREADS_PREFER_PTHREAD_FLAG ON) +find_package(Threads REQUIRED) + +if (TON_ARCH AND NOT MSVC) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=${TON_ARCH}") +endif() +if (THREADS_HAVE_PTHREAD_ARG) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread") +endif() + +if (TON_USE_JEMALLOC) + find_package(JeMalloc REQUIRED) +endif() + +set(MEMPROF "" CACHE STRING "Use one of \"ON\", \"FAST\" or \"SAFE\" to enable memory profiling. \ +Works under macOS and Linux when compiled using glibc. \ +In FAST mode stack is unwinded only using frame pointers, which may fail. \ +In SAFE mode stack is unwinded using backtrace function from execinfo.h, which may be very slow. \ +By default both methods are used to achieve maximum speed and accuracy") + +if (CLANG OR GCC) + if (MEMPROF) + check_cxx_compiler_flag(-no-pie CXX_NO_PIE_FLAG) + if (CXX_NO_PIE_FLAG) + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -no-pie") + elseif (APPLE) + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-no_pie") + endif() + endif() +endif() + +if (MSVC) + if (CMAKE_CXX_FLAGS_DEBUG MATCHES "/RTC1") + string(REPLACE "/RTC1" " " CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}") + endif() + add_definitions(-D_SCL_SECURE_NO_WARNINGS -D_CRT_SECURE_NO_WARNINGS) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MP /W4 /wd4100 /wd4127 /wd4324 /wd4456 /wd4457 /wd4458 /wd4505 /wd4702") +elseif (CLANG OR GCC) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${STD14_FLAG} -fno-omit-frame-pointer") + if (APPLE) + #use "-Wl,-exported_symbols_list,${CMAKE_CURRENT_SOURCE_DIR}/export_list" for exported symbols + set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fvisibility=hidden -Wl,-dead_strip,-x,-S") + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fvisibility=hidden -Wl,-dead_strip,-x,-S") + else() + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ffunction-sections -fdata-sections") + set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--gc-sections -Wl,--exclude-libs,ALL") + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections") + if (NOT TON_USE_ASAN AND NOT TON_USE_TSAN AND NOT MEMPROF) + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--exclude-libs,ALL") + endif() + endif() +elseif (INTEL) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${STD14_FLAG}") +endif() + +if (WIN32) + add_definitions(-DNTDDI_VERSION=0x06020000 -DWINVER=0x0602 -D_WIN32_WINNT=0x0602 -DNOMINMAX -DUNICODE -D_UNICODE) +endif() +if (CYGWIN) + add_definitions(-D_DEFAULT_SOURCE=1 -DFD_SETSIZE=4096) +endif() + +if (NOT ANDROID) # _FILE_OFFSET_BITS is broken in ndk r15 and r15b and doesn't work prior to Android 7.0 + add_definitions(-D_FILE_OFFSET_BITS=64) +endif() + +include(AddCXXCompilerFlag) +if (MSVC) + add_cxx_compiler_flag("/experimental:external /external:anglebrackets /external:W0") +endif() +if (NOT MSVC) + add_cxx_compiler_flag("-Wall") +endif() +add_cxx_compiler_flag("-Wextra") +add_cxx_compiler_flag("-Wimplicit-fallthrough=2") +add_cxx_compiler_flag("-Wpointer-arith") +add_cxx_compiler_flag("-Wcast-qual") +add_cxx_compiler_flag("-Wsign-compare") +add_cxx_compiler_flag("-Wduplicated-branches") +add_cxx_compiler_flag("-Wduplicated-cond") +add_cxx_compiler_flag("-Walloc-zero") +add_cxx_compiler_flag("-Wlogical-op") +add_cxx_compiler_flag("-Wno-tautological-compare") +add_cxx_compiler_flag("-Wpointer-arith") +add_cxx_compiler_flag("-Wvla") +add_cxx_compiler_flag("-Wnon-virtual-dtor") +add_cxx_compiler_flag("-Wno-unused-parameter") +add_cxx_compiler_flag("-Wconversion") +add_cxx_compiler_flag("-Wno-sign-conversion") +add_cxx_compiler_flag("-Qunused-arguments") +add_cxx_compiler_flag("-Wno-unused-private-field") +add_cxx_compiler_flag("-Wno-redundant-move") +#add_cxx_compiler_flag("-Werror") + +#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -isystem /usr/include/c++/v1") +if (CLANG) + #set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++") +endif() +if (TON_USE_ASAN) + if (CLANG) + add_cxx_compiler_flag("-stdlib=libc++") + endif() + add_cxx_compiler_flag("-fsanitize=address") + add_definitions(-DTD_USE_ASAN=1) +endif() +if (TON_USE_TSAN) + if (CLANG) + add_cxx_compiler_flag("-stdlib=libc++") + endif() + add_cxx_compiler_flag("-fsanitize=thread") +endif() +if (TON_USE_UBSAN) + if (CLANG) + add_cxx_compiler_flag("-stdlib=libc++") + endif() + add_cxx_compiler_flag("-fsanitize=undefined") +endif() +#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread") +#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address") +#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined") +#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=leak") +#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -finstrument-functions") + +#Compilation database +set(CMAKE_EXPORT_COMPILE_COMMANDS 1) + +#BEGIN internal +find_package(LATEX) +if (LATEX_FOUND) + include(UseLATEX) + add_latex_document(doc/ton.tex TARGET_NAME ton_white_paper) + add_latex_document(doc/tvm.tex TARGET_NAME ton_vm_description) + add_latex_document(doc/tblkch.tex TARGET_NAME ton_blockchain_description) + add_latex_document(doc/fiftbase.tex TARGET_NAME fift_basic_description) +endif() +#END internal + +function(target_link_libraries_system target) + set(libs ${ARGN}) + foreach(lib ${libs}) + get_target_property(lib_include_dirs ${lib} INTERFACE_INCLUDE_DIRECTORIES) + target_include_directories(${target} SYSTEM PUBLIC ${lib_include_dirs}) + target_link_libraries(${target} PUBLIC ${lib}) + endforeach(lib) +endfunction(target_link_libraries_system) + +add_subdirectory(tdutils) +add_subdirectory(memprof) +add_subdirectory(tdactor) +add_subdirectory(tdnet) +if (TON_USE_ROCKSDB) + option(TDDB_USE_ROCKSDB "Use rockdb" ON) +endif() +add_subdirectory(tddb) +add_subdirectory(tdtl) +add_subdirectory(tl) +add_subdirectory(terminal) +add_subdirectory(keys) +add_subdirectory(tl-utils) +add_subdirectory(adnl) +add_subdirectory(crypto) +add_subdirectory(lite-client) + +#BEGIN tonlib +add_subdirectory(tonlib) +#END tonlib + +#BEGIN internal +add_subdirectory(common) +add_subdirectory(tdfec) +add_subdirectory(keyring) +add_subdirectory(fec) +add_subdirectory(rldp) +add_subdirectory(dht) +add_subdirectory(overlay) +add_subdirectory(catchain) +add_subdirectory(validator-session) +add_subdirectory(validator) +add_subdirectory(blockchain-explorer) +add_subdirectory(validator-engine) +add_subdirectory(validator-engine-console) +add_subdirectory(dht-server) +add_subdirectory(utils) +#END internal + +if (NOT CMAKE_CROSSCOMPILING) + add_custom_target(prepare_cross_compiling DEPENDS tl_generate_common tdmime_auto tlb_generate_block) +endif() + +#TESTS +add_executable(test-ed25519 test/test-td-main.cpp ${ED25519_TEST_SOURCE}) +target_link_libraries(test-ed25519 PRIVATE ton_crypto) + +add_executable(test-vm test/test-td-main.cpp ${TONVM_TEST_SOURCE}) +target_link_libraries(test-vm PRIVATE ton_crypto fift-lib) + +add_executable(test-cells test/test-td-main.cpp ${CELLS_TEST_SOURCE}) +target_link_libraries(test-cells PRIVATE ton_crypto) + +add_executable(test-fift test/test-td-main.cpp ${FIFT_TEST_SOURCE}) +target_link_libraries(test-fift PRIVATE fift-lib) + +add_executable(test-tdutils test/test-td-main.cpp ${TDUTILS_TEST_SOURCE}) +target_link_libraries(test-tdutils PRIVATE tdutils ${CMAKE_THREAD_LIBS_INIT} memprof ${JEMALLOC_LIBRARIES}) +#target_link_libraries_system(test-tdutils absl::base absl::container absl::hash ) +#target_link_libraries_system(test-tdutils libcuckoo) +#target_include_directories(test-tdutils PRIVATE SYSTEM ${JUNCTION_ALL_INCLUDE_DIRS}) +#target_link_libraries(test-tdutils PRIVATE ${JUNCTION_ALL_LIBRARIES}) + +add_executable(test-tdactor test/test-td-main.cpp ${TDACTOR_TEST_SOURCE}) +target_link_libraries(test-tdactor PRIVATE tdactor ${CMAKE_THREAD_LIBS_INIT}) + +add_executable(test-net test/test-td-main.cpp ${NET_TEST_SOURCE}) +target_link_libraries(test-net PRIVATE tdnet tdutils ${CMAKE_THREAD_LIBS_INIT}) + +#BEGIN tonlib +add_executable(test-tonlib ${TONLIB_ONLINE_TEST_SOURCE}) +target_link_libraries(test-tonlib tdutils tdactor adnllite tl_api ton_crypto ton_block tl_tonlib_api tonlib) + +add_executable(test-tonlib-offline test/test-td-main.cpp ${TONLIB_OFFLINE_TEST_SOURCE}) +target_link_libraries(test-tonlib-offline tdutils tdactor adnllite tl_api ton_crypto ton_block fift-lib tl_tonlib_api tonlib) +#END tonlib + +#BEGIN internal +add_executable(test-db test/test-td-main.cpp ${TONDB_TEST_SOURCE}) +target_link_libraries(test-db PRIVATE ton_db memprof) + +add_executable(test-tddb test/test-td-main.cpp ${TDDB_TEST_SOURCE}) +target_link_libraries(test-tddb PRIVATE tdutils tddb ${CMAKE_THREAD_LIBS_INIT} memprof) + +add_executable(test-fec test/test-td-main.cpp ${FEC_TEST_SOURCE}) +target_link_libraries(test-fec PRIVATE tdfec tdutils ${CMAKE_THREAD_LIBS_INIT}) +if (USE_LIBRAPTORQ) + target_link_libraries(test-fec PRIVATE third_party_fec) + target_compile_definitions(test-fec PRIVATE "USE_LIBRAPTORQ=1") +endif() + +add_executable(test-hello-world test/test-hello-world.cpp ) +target_link_libraries(test-hello-world tl_api) + +add_executable(test-adnl test/test-adnl.cpp) +target_link_libraries(test-adnl adnl adnltest dht tl_api) +add_executable(test-dht test/test-dht.cpp) +target_link_libraries(test-dht adnl adnltest dht tl_api) +add_executable(test-rldp test/test-rldp.cpp) +target_link_libraries(test-rldp adnl adnltest dht rldp tl_api) +add_executable(test-validator-session-state test/test-validator-session-state.cpp) +target_link_libraries(test-validator-session-state adnl dht rldp validatorsession tl_api) + +#add_executable(test-node test/test-node.cpp) +#target_link_libraries(test-node overlay tdutils tdactor adnl tl_api dht +# catchain validatorsession) + +add_executable(test-catchain test/test-catchain.cpp) +target_link_libraries(test-catchain overlay tdutils tdactor adnl adnltest rldp tl_api dht + catchain ) +#add_executable(test-validator-session test/test-validator-session.cpp) +#target_link_libraries(test-validator-session overlay tdutils tdactor adnl tl_api dht +# catchain validatorsession) +#add_executable(test-ton-dummy-0 test/test-ton-dummy-0.cpp) +#target_link_libraries(test-ton-dummy-0 overlay tdutils tdactor adnl tl_api dht +# catchain validatorsession ton-node validator dummy_validator validator ) +#add_executable(test-dummy-0-lite-client test/test-dummy-0-lite-client.cpp) +#target_link_libraries(test-dummy-0-lite-client overlay tdutils tdactor adnl tl_api dht +# catchain validatorsession ton-node validator dummy_validator validator +# terminal ) +#add_executable(test-ton-dummy-0-collator test/test-ton-collator.cpp) +#target_link_libraries(test-ton-dummy-0-collator overlay tdutils tdactor adnl tl_api +# dht catchain validatorsession ton-node validator_disk dummy_validator validator_disk ) +add_executable(test-ton-collator test/test-ton-collator.cpp) +target_link_libraries(test-ton-collator overlay tdutils tdactor adnl tl_api dht + catchain validatorsession validator-disk ton_validator validator-disk ) +#add_executable(test-validator test/test-validator.cpp) +#target_link_libraries(test-validator overlay tdutils tdactor adnl tl_api dht +# rldp catchain validatorsession ton-node validator ton_validator validator memprof ${JEMALLOC_LIBRARIES}) +#add_executable(test-ext-server test/test-ext-server.cpp) +#target_link_libraries(test-ext-server tdutils tdactor adnl tl_api dht ) +#add_executable(test-ext-client test/test-ext-client.cpp) +#target_link_libraries(test-ext-client tdutils tdactor adnl tl_api tl-lite-utils) + +get_directory_property(HAS_PARENT PARENT_DIRECTORY) +if (HAS_PARENT) + set(ALL_TEST_SOURCE + ${TDUTILS_TEST_SOURCE} + ${TDACTOR_TEST_SOURCE} + ${NET_TEST_SOURCE} + ${TDDB_TEST_SOURCE} + ${FEC_TEST_SOURCE} + ${ED25519_TEST_SOURCE} + ${TONDB_TEST_SOURCE} + ${CELLS_TEST_SOURCE} # ${TONVM_TEST_SOURCE} ${FIFT_TEST_SOURCE} ${TONLIB_ONLINE_TEST_SOURCE} + PARENT_SCOPE) +endif() +add_library(all_tests INTERFACE) +target_link_libraries(all_tests INTERFACE tdutils tdactor tdnet tdfec ton_db ton_crypto fift-lib) +#END internal + +enable_testing() +set(TEST_OPTIONS "--regression ${CMAKE_CURRENT_SOURCE_DIR}/test/regression-tests.ans --filter -Bench") +separate_arguments(TEST_OPTIONS) +add_test(test-ed25519 test-ed25519) +add_test(test-vm test-vm ${TEST_OPTIONS}) +add_test(test-fift test-fift ${TEST_OPTIONS}) +add_test(test-cells test-cells ${TEST_OPTIONS}) +add_test(test-net test-net) +add_test(test-actors test-tdactor) + +#BEGIN tonlib +add_test(test-tdutils test-tdutils) +add_test(test-tonlib-offline test-tonlib-offline) +#END tonlib + +#BEGIN internal +add_test(test-fec test-fec) +add_test(test-tddb test-tddb ${TEST_OPTIONS}) +add_test(test-db test-db ${TEST_OPTIONS}) +#END internal + diff --git a/GPLv2 b/GPLv2 new file mode 100644 index 00000000..c2a8d683 --- /dev/null +++ b/GPLv2 @@ -0,0 +1,27 @@ +/* + This file is part of TON Blockchain source code. + + TON Blockchain is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + TON Blockchain is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with TON Blockchain. If not, see . + + In addition, as a special exception, the copyright holders give permission + to link the code of portions of this program with the OpenSSL library. + You must obey the GNU General Public License in all respects for all + of the code used other than OpenSSL. If you modify file(s) with this + exception, you may extend this exception to your version of the file(s), + but you are not obligated to do so. If you do not wish to do so, delete this + exception statement from your version. If you delete this exception statement + from all source files in the program, then also delete it here. + + Copyright 2017-2019 Telegram Systems LLP +*/ diff --git a/LGPLv2 b/LGPLv2 new file mode 100644 index 00000000..d93ab1b9 --- /dev/null +++ b/LGPLv2 @@ -0,0 +1,18 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ diff --git a/LICENSE.LGPL b/LICENSE.LGPL new file mode 100644 index 00000000..b482fc4e --- /dev/null +++ b/LICENSE.LGPL @@ -0,0 +1,481 @@ + GNU LIBRARY GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1991 Free Software Foundation, Inc. + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + +[This is the first released version of the library GPL. It is + numbered 2 because it goes with version 2 of the ordinary GPL.] + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +Licenses are intended to guarantee your freedom to share and change +free software--to make sure the software is free for all its users. + + This license, the Library General Public License, applies to some +specially designated Free Software Foundation software, and to any +other libraries whose authors decide to use it. You can use it for +your libraries, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +this service if you wish), that you receive source code or can get it +if you want it, that you can change the software or use pieces of it +in new free programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if +you distribute copies of the library, or if you modify it. + + For example, if you distribute copies of the library, whether gratis +or for a fee, you must give the recipients all the rights that we gave +you. You must make sure that they, too, receive or can get the source +code. If you link a program with the library, you must provide +complete object files to the recipients so that they can relink them +with the library, after making changes to the library and recompiling +it. And you must show them these terms so they know their rights. + + Our method of protecting your rights has two steps: (1) copyright +the library, and (2) offer you this license which gives you legal +permission to copy, distribute and/or modify the library. + + Also, for each distributor's protection, we want to make certain +that everyone understands that there is no warranty for this free +library. If the library is modified by someone else and passed on, we +want its recipients to know that what they have is not the original +version, so that any problems introduced by others will not reflect on +the original authors' reputations. + + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that companies distributing free +software will individually obtain patent licenses, thus in effect +transforming the program into proprietary software. To prevent this, +we have made it clear that any patent must be licensed for everyone's +free use or not licensed at all. + + Most GNU software, including some libraries, is covered by the ordinary +GNU General Public License, which was designed for utility programs. This +license, the GNU Library General Public License, applies to certain +designated libraries. This license is quite different from the ordinary +one; be sure to read it in full, and don't assume that anything in it is +the same as in the ordinary license. + + The reason we have a separate public license for some libraries is that +they blur the distinction we usually make between modifying or adding to a +program and simply using it. Linking a program with a library, without +changing the library, is in some sense simply using the library, and is +analogous to running a utility program or application program. However, in +a textual and legal sense, the linked executable is a combined work, a +derivative of the original library, and the ordinary General Public License +treats it as such. + + Because of this blurred distinction, using the ordinary General +Public License for libraries did not effectively promote software +sharing, because most developers did not use the libraries. We +concluded that weaker conditions might promote sharing better. + + However, unrestricted linking of non-free programs would deprive the +users of those programs of all benefit from the free status of the +libraries themselves. This Library General Public License is intended to +permit developers of non-free programs to use free libraries, while +preserving your freedom as a user of such programs to change the free +libraries that are incorporated in them. (We have not seen how to achieve +this as regards changes in header files, but we have achieved it as regards +changes in the actual functions of the Library.) The hope is that this +will lead to faster development of free libraries. + + The precise terms and conditions for copying, distribution and +modification follow. Pay close attention to the difference between a +"work based on the library" and a "work that uses the library". The +former contains code derived from the library, while the latter only +works together with the library. + + Note that it is possible for a library to be covered by the ordinary +General Public License rather than by this special one. + + GNU LIBRARY GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License Agreement applies to any software library which +contains a notice placed by the copyright holder or other authorized +party saying it may be distributed under the terms of this Library +General Public License (also called "this License"). Each licensee is +addressed as "you". + + A "library" means a collection of software functions and/or data +prepared so as to be conveniently linked with application programs +(which use some of those functions and data) to form executables. + + The "Library", below, refers to any such software library or work +which has been distributed under these terms. A "work based on the +Library" means either the Library or any derivative work under +copyright law: that is to say, a work containing the Library or a +portion of it, either verbatim or with modifications and/or translated +straightforwardly into another language. (Hereinafter, translation is +included without limitation in the term "modification".) + + "Source code" for a work means the preferred form of the work for +making modifications to it. For a library, complete source code means +all the source code for all modules it contains, plus any associated +interface definition files, plus the scripts used to control compilation +and installation of the library. + + Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running a program using the Library is not restricted, and output from +such a program is covered only if its contents constitute a work based +on the Library (independent of the use of the Library in a tool for +writing it). Whether that is true depends on what the Library does +and what the program that uses the Library does. + + 1. You may copy and distribute verbatim copies of the Library's +complete source code as you receive it, in any medium, provided that +you conspicuously and appropriately publish on each copy an +appropriate copyright notice and disclaimer of warranty; keep intact +all the notices that refer to this License and to the absence of any +warranty; and distribute a copy of this License along with the +Library. + + You may charge a fee for the physical act of transferring a copy, +and you may at your option offer warranty protection in exchange for a +fee. + + 2. You may modify your copy or copies of the Library or any portion +of it, thus forming a work based on the Library, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) The modified work must itself be a software library. + + b) You must cause the files modified to carry prominent notices + stating that you changed the files and the date of any change. + + c) You must cause the whole of the work to be licensed at no + charge to all third parties under the terms of this License. + + d) If a facility in the modified Library refers to a function or a + table of data to be supplied by an application program that uses + the facility, other than as an argument passed when the facility + is invoked, then you must make a good faith effort to ensure that, + in the event an application does not supply such function or + table, the facility still operates, and performs whatever part of + its purpose remains meaningful. + + (For example, a function in a library to compute square roots has + a purpose that is entirely well-defined independent of the + application. Therefore, Subsection 2d requires that any + application-supplied function or table used by this function must + be optional: if the application does not supply it, the square + root function must still compute square roots.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Library, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Library, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote +it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Library. + +In addition, mere aggregation of another work not based on the Library +with the Library (or with a work based on the Library) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may opt to apply the terms of the ordinary GNU General Public +License instead of this License to a given copy of the Library. To do +this, you must alter all the notices that refer to this License, so +that they refer to the ordinary GNU General Public License, version 2, +instead of to this License. (If a newer version than version 2 of the +ordinary GNU General Public License has appeared, then you can specify +that version instead if you wish.) Do not make any other change in +these notices. + + Once this change is made in a given copy, it is irreversible for +that copy, so the ordinary GNU General Public License applies to all +subsequent copies and derivative works made from that copy. + + This option is useful when you wish to copy part of the code of +the Library into a program that is not a library. + + 4. You may copy and distribute the Library (or a portion or +derivative of it, under Section 2) in object code or executable form +under the terms of Sections 1 and 2 above provided that you accompany +it with the complete corresponding machine-readable source code, which +must be distributed under the terms of Sections 1 and 2 above on a +medium customarily used for software interchange. + + If distribution of object code is made by offering access to copy +from a designated place, then offering equivalent access to copy the +source code from the same place satisfies the requirement to +distribute the source code, even though third parties are not +compelled to copy the source along with the object code. + + 5. A program that contains no derivative of any portion of the +Library, but is designed to work with the Library by being compiled or +linked with it, is called a "work that uses the Library". Such a +work, in isolation, is not a derivative work of the Library, and +therefore falls outside the scope of this License. + + However, linking a "work that uses the Library" with the Library +creates an executable that is a derivative of the Library (because it +contains portions of the Library), rather than a "work that uses the +library". The executable is therefore covered by this License. +Section 6 states terms for distribution of such executables. + + When a "work that uses the Library" uses material from a header file +that is part of the Library, the object code for the work may be a +derivative work of the Library even though the source code is not. +Whether this is true is especially significant if the work can be +linked without the Library, or if the work is itself a library. The +threshold for this to be true is not precisely defined by law. + + If such an object file uses only numerical parameters, data +structure layouts and accessors, and small macros and small inline +functions (ten lines or less in length), then the use of the object +file is unrestricted, regardless of whether it is legally a derivative +work. (Executables containing this object code plus portions of the +Library will still fall under Section 6.) + + Otherwise, if the work is a derivative of the Library, you may +distribute the object code for the work under the terms of Section 6. +Any executables containing that work also fall under Section 6, +whether or not they are linked directly with the Library itself. + + 6. As an exception to the Sections above, you may also compile or +link a "work that uses the Library" with the Library to produce a +work containing portions of the Library, and distribute that work +under terms of your choice, provided that the terms permit +modification of the work for the customer's own use and reverse +engineering for debugging such modifications. + + You must give prominent notice with each copy of the work that the +Library is used in it and that the Library and its use are covered by +this License. You must supply a copy of this License. If the work +during execution displays copyright notices, you must include the +copyright notice for the Library among them, as well as a reference +directing the user to the copy of this License. Also, you must do one +of these things: + + a) Accompany the work with the complete corresponding + machine-readable source code for the Library including whatever + changes were used in the work (which must be distributed under + Sections 1 and 2 above); and, if the work is an executable linked + with the Library, with the complete machine-readable "work that + uses the Library", as object code and/or source code, so that the + user can modify the Library and then relink to produce a modified + executable containing the modified Library. (It is understood + that the user who changes the contents of definitions files in the + Library will not necessarily be able to recompile the application + to use the modified definitions.) + + b) Accompany the work with a written offer, valid for at + least three years, to give the same user the materials + specified in Subsection 6a, above, for a charge no more + than the cost of performing this distribution. + + c) If distribution of the work is made by offering access to copy + from a designated place, offer equivalent access to copy the above + specified materials from the same place. + + d) Verify that the user has already received a copy of these + materials or that you have already sent this user a copy. + + For an executable, the required form of the "work that uses the +Library" must include any data and utility programs needed for +reproducing the executable from it. However, as a special exception, +the source code distributed need not include anything that is normally +distributed (in either source or binary form) with the major +components (compiler, kernel, and so on) of the operating system on +which the executable runs, unless that component itself accompanies +the executable. + + It may happen that this requirement contradicts the license +restrictions of other proprietary libraries that do not normally +accompany the operating system. Such a contradiction means you cannot +use both them and the Library together in an executable that you +distribute. + + 7. You may place library facilities that are a work based on the +Library side-by-side in a single library together with other library +facilities not covered by this License, and distribute such a combined +library, provided that the separate distribution of the work based on +the Library and of the other library facilities is otherwise +permitted, and provided that you do these two things: + + a) Accompany the combined library with a copy of the same work + based on the Library, uncombined with any other library + facilities. This must be distributed under the terms of the + Sections above. + + b) Give prominent notice with the combined library of the fact + that part of it is a work based on the Library, and explaining + where to find the accompanying uncombined form of the same work. + + 8. You may not copy, modify, sublicense, link with, or distribute +the Library except as expressly provided under this License. Any +attempt otherwise to copy, modify, sublicense, link with, or +distribute the Library is void, and will automatically terminate your +rights under this License. However, parties who have received copies, +or rights, from you under this License will not have their licenses +terminated so long as such parties remain in full compliance. + + 9. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Library or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Library (or any work based on the +Library), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Library or works based on it. + + 10. Each time you redistribute the Library (or any work based on the +Library), the recipient automatically receives a license from the +original licensor to copy, distribute, link with or modify the Library +subject to these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties to +this License. + + 11. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Library at all. For example, if a patent +license would not permit royalty-free redistribution of the Library by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Library. + +If any portion of this section is held invalid or unenforceable under any +particular circumstance, the balance of the section is intended to apply, +and the section as a whole is intended to apply in other circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 12. If the distribution and/or use of the Library is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Library under this License may add +an explicit geographical distribution limitation excluding those countries, +so that distribution is permitted only in or among countries not thus +excluded. In such case, this License incorporates the limitation as if +written in the body of this License. + + 13. The Free Software Foundation may publish revised and/or new +versions of the Library General Public License from time to time. +Such new versions will be similar in spirit to the present version, +but may differ in detail to address new problems or concerns. + +Each version is given a distinguishing version number. If the Library +specifies a version number of this License which applies to it and +"any later version", you have the option of following the terms and +conditions either of that version or of any later version published by +the Free Software Foundation. If the Library does not specify a +license version number, you may choose any version ever published by +the Free Software Foundation. + + 14. If you wish to incorporate parts of the Library into other free +programs whose distribution conditions are incompatible with these, +write to the author to ask for permission. For software which is +copyrighted by the Free Software Foundation, write to the Free +Software Foundation; we sometimes make exceptions for this. Our +decision will be guided by the two goals of preserving the free status +of all derivatives of our free software and of promoting the sharing +and reuse of software generally. + + NO WARRANTY + + 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO +WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. +EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR +OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY +KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE +LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME +THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN +WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY +AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU +FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR +CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE +LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING +RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A +FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF +SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH +DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Libraries + + If you develop a new library, and you want it to be of the greatest +possible use to the public, we recommend making it free software that +everyone can redistribute and change. You can do so by permitting +redistribution under these terms (or, alternatively, under the terms of the +ordinary General Public License). + + To apply these terms, attach the following notices to the library. It is +safest to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least the +"copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +Also add information on how to contact you by electronic and paper mail. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the library, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the + library `Frob' (a library for tweaking knobs) written by James Random Hacker. + + , 1 April 1990 + Ty Coon, President of Vice + +That's all there is to it! diff --git a/adnl/CMakeLists.txt b/adnl/CMakeLists.txt new file mode 100644 index 00000000..e519e9cd --- /dev/null +++ b/adnl/CMakeLists.txt @@ -0,0 +1,98 @@ +cmake_minimum_required(VERSION 3.0.2 FATAL_ERROR) + +#BEGIN internal +set(ADNL_HEADERS + adnl-address-list.h + adnl-address-list.hpp + adnl-db.h + adnl-db.hpp + adnl-channel.h + adnl-channel.hpp + adnl-ext-client.h + adnl-ext-client.hpp + adnl-ext-connection.hpp + adnl-ext-server.h + adnl-ext-server.hpp + adnl-local-id.h + adnl-message.h + adnl-network-manager.h + adnl-network-manager.hpp + adnl-node.h + adnl-packet.h + adnl-peer-table.h + adnl-peer-table.hpp + adnl-peer.h + adnl-peer.hpp + adnl-query.h + adnl-static-nodes.h + adnl-static-nodes.hpp + adnl-proxy-types.h + adnl-proxy-types.hpp + adnl.h + utils.hpp +) +set(ADNL_SOURCE + adnl-address-list.cpp + adnl-db.cpp + adnl-ext-client.cpp + adnl-ext-server.cpp + adnl-ext-connection.cpp + adnl-local-id.cpp + adnl-message.cpp + adnl-network-manager.cpp + adnl-node.cpp + adnl-packet.cpp + adnl-peer-table.cpp + adnl-peer.cpp + adnl-query.cpp + adnl-channel.cpp + adnl-static-nodes.cpp + adnl-proxy-types.cpp + utils.cpp + ${ADNL_HEADERS} +) + +set(ADNL_TEST_SOURCE + adnl-test-loopback-implementation.h + adnl-test-loopback-implementation.cpp +) + +set(ADNL_PROXY_SOURCE + adnl-proxy.cpp + adnl-proxy-types.h + adnl-proxy-types.hpp + adnl-proxy-types.cpp +) + +#FIXME +set(ADNL_LITE_HEADERS ${ADNL_HEADERS}) +#END internal + +set(ADNL_LITE_SOURCE + adnl-ext-client.cpp + adnl-ext-connection.cpp + adnl-query.cpp + + ${ADNL_LITE_HEADERS} +) + +#BEGIN internal +add_library(adnl STATIC ${ADNL_SOURCE}) + +target_include_directories(adnl PUBLIC $) +target_link_libraries(adnl PUBLIC tdactor ton_crypto tl_api tdnet tddb keys keyring ) + +add_executable(adnl-proxy ${ADNL_PROXY_SOURCE}) +target_include_directories(adnl-proxy PUBLIC $) +target_link_libraries(adnl-proxy PUBLIC tdactor ton_crypto tl_api tdnet common + tl-utils) + +add_library(adnltest STATIC ${ADNL_TEST_SOURCE}) +target_include_directories(adnltest PUBLIC $) +target_link_libraries(adnltest PUBLIC adnl ) +#END internal + +add_library(adnllite STATIC ${ADNL_LITE_SOURCE}) + +target_include_directories(adnllite PUBLIC $) +target_link_libraries(adnllite PUBLIC tdactor ton_crypto tl_lite_api tdnet keys ) diff --git a/adnl/adnl-address-list.cpp b/adnl/adnl-address-list.cpp new file mode 100644 index 00000000..ab395144 --- /dev/null +++ b/adnl/adnl-address-list.cpp @@ -0,0 +1,160 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "adnl-address-list.hpp" +#include "adnl-peer-table.h" +#include "auto/tl/ton_api.hpp" +#include "td/utils/overloaded.h" +#include "td/net/UdpServer.h" + +namespace ton { + +namespace adnl { + +class AdnlNetworkConnectionUdp : public AdnlNetworkConnection { + public: + void send(AdnlNodeIdShort src, AdnlNodeIdShort dst, td::uint32 priority, td::BufferSlice message) override; + bool is_alive() const override { + return true; + } + bool is_active() const override { + return true; + } + void start_up() override { + callback_->on_change_state(true); + } + + AdnlNetworkConnectionUdp(td::actor::ActorId network_manager, td::uint32 ip, td::uint16 port, + std::unique_ptr callback); + AdnlNetworkConnectionUdp(td::actor::ActorId network_manager, td::Bits128 ip, td::uint16 port, + std::unique_ptr callback); + + private: + td::actor::ActorId network_manager_; + td::IPAddress addr_; + std::unique_ptr callback_; +}; + +void AdnlNetworkConnectionUdp::send(AdnlNodeIdShort src, AdnlNodeIdShort dst, td::uint32 priority, + td::BufferSlice message) { + LOG_CHECK(message.size() <= AdnlNetworkManager::get_mtu()) << "dst=" << addr_ << " size=" << message.size(); + td::actor::send_closure(network_manager_, &AdnlNetworkManager::send_udp_packet, src, dst, addr_, priority, + std::move(message)); +} + +AdnlNetworkConnectionUdp::AdnlNetworkConnectionUdp(td::actor::ActorId network_manager, + td::uint32 ip, td::uint16 port, + std::unique_ptr callback) + : network_manager_(network_manager), callback_(std::move(callback)) { + addr_.init_host_port(td::IPAddress::ipv4_to_str(ip), port).ensure(); +} + +AdnlNetworkConnectionUdp::AdnlNetworkConnectionUdp(td::actor::ActorId network_manager, + td::Bits128 ip, td::uint16 port, + std::unique_ptr callback) + : network_manager_(network_manager), callback_(std::move(callback)) { + addr_.init_host_port(td::IPAddress::ipv6_to_str(ip.as_slice()), port).ensure(); +} + +AdnlAddressImpl::Hash AdnlAddressImpl::get_hash() const { + return get_tl_object_sha_bits256(tl()); +} + +td::actor::ActorOwn AdnlAddressUdp::create_connection( + td::actor::ActorId network_manager, + std::unique_ptr callback) const { + return td::actor::create_actor("udpconn", network_manager, ip_, port_, std::move(callback)); +} + +AdnlAddressUdp::AdnlAddressUdp(const ton_api::adnl_address_udp &obj) { + ip_ = obj.ip_; + port_ = static_cast(obj.port_); +} + +td::actor::ActorOwn AdnlAddressUdp6::create_connection( + td::actor::ActorId network_manager, + std::unique_ptr callback) const { + return td::actor::create_actor("udpconn", network_manager, ip_, port_, std::move(callback)); +} + +AdnlAddressUdp6::AdnlAddressUdp6(const ton_api::adnl_address_udp6 &obj) { + ip_ = obj.ip_; + port_ = static_cast(obj.port_); +} + +td::Ref AdnlAddressImpl::create(const tl_object_ptr &addr) { + td::Ref res = td::Ref{}; + ton_api::downcast_call(*const_cast(addr.get()), + td::overloaded( + [&](const ton_api::adnl_address_udp &obj) { + res = td::Ref{true, obj}; + }, + [&](const ton_api::adnl_address_udp6 &obj) { + res = td::Ref{true, obj}; + })); + return res; +} + +bool AdnlAddressList::public_only() const { + for (auto &addr : addrs_) { + if (!addr->is_public()) { + return false; + } + } + return true; +} + +AdnlAddressList::AdnlAddressList(const tl_object_ptr &addrs) { + version_ = static_cast(addrs->version_); + std::vector> vec; + for (auto &addr : addrs->addrs_) { + vec.push_back(AdnlAddressImpl::create(addr)); + } + addrs_ = std::move(vec); + reinit_date_ = addrs->reinit_date_; + priority_ = addrs->priority_; + expire_at_ = addrs->expire_at_; +} + +tl_object_ptr AdnlAddressList::tl() const { + std::vector> addrs; + for (auto &v : addrs_) { + addrs.emplace_back(v->tl()); + } + return create_tl_object(std::move(addrs), version_, reinit_date_, priority_, expire_at_); +} + +td::uint32 AdnlAddressList::serialized_size() const { + td::uint32 res = 24; + for (auto &addr : addrs_) { + res += addr->serialized_size(); + } + return res; +} + +td::Result AdnlAddressList::create(const tl_object_ptr &addr_list) { + auto A = AdnlAddressList{addr_list}; + if (A.serialized_size() > max_serialized_size()) { + return td::Status::Error(ErrorCode::protoviolation, PSTRING() << "too big addr list: size=" << A.serialized_size()); + } + return A; +} + +} // namespace adnl + +} // namespace ton diff --git a/adnl/adnl-address-list.h b/adnl/adnl-address-list.h new file mode 100644 index 00000000..d13fce6f --- /dev/null +++ b/adnl/adnl-address-list.h @@ -0,0 +1,105 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +#include "adnl-network-manager.h" +#include "crypto/common/refcnt.hpp" + +namespace ton { + +namespace adnl { + +class AdnlAddressImpl : public td::CntObject { + public: + using Hash = td::Bits256; + virtual ~AdnlAddressImpl() = default; + + virtual Hash get_hash() const; + virtual bool is_public() const = 0; + virtual td::uint32 serialized_size() const = 0; + virtual tl_object_ptr tl() const = 0; + virtual td::actor::ActorOwn create_connection( + td::actor::ActorId network_manager, + std::unique_ptr callback) const = 0; + + static td::Ref create(const tl_object_ptr &addr); +}; + +using AdnlAddress = td::Ref; + +class AdnlAddressList { + private: + AdnlAddressList(const tl_object_ptr &addrs); + + td::int32 version_; + td::int32 reinit_date_; + td::int32 priority_; + td::int32 expire_at_; + std::vector addrs_; + + public: + static constexpr td::uint32 max_serialized_size() { + return 128; + } + + const auto &addrs() const { + return addrs_; + } + auto version() const { + return version_; + } + auto reinit_date() const { + return reinit_date_; + } + auto priority() const { + return priority_; + } + auto expire_at() const { + return expire_at_; + } + void set_version(td::uint32 version) { + version_ = version; + } + void set_reinit_date(td::int32 date) { + reinit_date_ = date; + } + void set_expire_at(td::int32 date) { + expire_at_ = date; + } + bool empty() const { + return version_ == -1; + } + void add_addr(AdnlAddress addr) { + addrs_.push_back(addr); + } + bool public_only() const; + td::uint32 size() const { + return static_cast(addrs_.size()); + } + td::uint32 serialized_size() const; + tl_object_ptr tl() const; + AdnlAddressList() : version_{-1}, reinit_date_{0}, priority_{0}, expire_at_{0} { + } + + static td::Result create(const tl_object_ptr &addr_list); +}; + +} // namespace adnl + +} // namespace ton diff --git a/adnl/adnl-address-list.hpp b/adnl/adnl-address-list.hpp new file mode 100644 index 00000000..f66f2351 --- /dev/null +++ b/adnl/adnl-address-list.hpp @@ -0,0 +1,90 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +#include "adnl-node-id.hpp" +#include "adnl-address-list.h" + +namespace ton { + +namespace adnl { + +class AdnlAddressUdp : public AdnlAddressImpl { + private: + td::uint32 ip_; + td::uint16 port_; + + public: + explicit AdnlAddressUdp(const ton_api::adnl_address_udp &obj); + + AdnlAddressUdp(td::uint32 ip, td::uint16 port) : ip_(ip), port_(port) { + } + + AdnlAddressUdp *make_copy() const override { + return new AdnlAddressUdp{ip_, port_}; + } + + bool is_public() const override { + return true; + } + td::uint32 serialized_size() const override { + return 12; + } + + tl_object_ptr tl() const override { + return create_tl_object(ip_, port_); + } + td::actor::ActorOwn create_connection( + td::actor::ActorId network_manager, + std::unique_ptr callback) const override; +}; + +class AdnlAddressUdp6 : public AdnlAddressImpl { + private: + td::Bits128 ip_; + td::uint16 port_; + + public: + explicit AdnlAddressUdp6(const ton_api::adnl_address_udp6 &obj); + + AdnlAddressUdp6(td::Bits128 ip, td::uint16 port) : ip_(ip), port_(port) { + } + + AdnlAddressUdp6 *make_copy() const override { + return new AdnlAddressUdp6{ip_, port_}; + } + + bool is_public() const override { + return true; + } + td::uint32 serialized_size() const override { + return 12; + } + + tl_object_ptr tl() const override { + return create_tl_object(ip_, port_); + } + td::actor::ActorOwn create_connection( + td::actor::ActorId network_manager, + std::unique_ptr callback) const override; +}; + +} // namespace adnl + +} // namespace ton diff --git a/adnl/adnl-channel.cpp b/adnl/adnl-channel.cpp new file mode 100644 index 00000000..9314d54e --- /dev/null +++ b/adnl/adnl-channel.cpp @@ -0,0 +1,129 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "adnl-channel.hpp" +#include "adnl-peer.h" +#include "adnl-peer-table.h" + +#include "td/utils/crypto.h" +#include "crypto/Ed25519.h" + +namespace ton { + +namespace adnl { + +td::Result> AdnlChannel::create(privkeys::Ed25519 pk_data, pubkeys::Ed25519 pub_data, + AdnlNodeIdShort local_id, AdnlNodeIdShort peer_id, + AdnlChannelIdShort &out_id, AdnlChannelIdShort &in_id, + td::actor::ActorId peer_pair) { + td::Ed25519::PublicKey pub_k = pub_data.export_key(); + td::Ed25519::PrivateKey priv_k = pk_data.export_key(); + + TRY_RESULT_PREFIX(shared_secret, td::Ed25519::compute_shared_secret(pub_k, priv_k), + "failed to compute channel shared secret: "); + CHECK(shared_secret.length() == 32); + + td::SecureString rev_secret{32}; + for (td::uint32 i = 0; i < 32; i++) { + rev_secret.as_mutable_slice()[i] = shared_secret[31 - i]; + } + + auto R = [&]() -> std::pair { + if (local_id < peer_id) { + return {privkeys::AES{std::move(shared_secret)}, pubkeys::AES{std::move(rev_secret)}}; + } else if (peer_id < local_id) { + return {privkeys::AES{std::move(rev_secret)}, pubkeys::AES{std::move(shared_secret)}}; + } else { + auto c = shared_secret.copy(); + return {privkeys::AES{std::move(c)}, pubkeys::AES{std::move(shared_secret)}}; + } + }(); + + in_id = AdnlChannelIdShort{R.first.compute_short_id()}; + out_id = AdnlChannelIdShort{R.second.compute_short_id()}; + + TRY_RESULT_PREFIX(encryptor, R.second.create_encryptor(), "failed to init channel encryptor: "); + TRY_RESULT_PREFIX(decryptor, R.first.create_decryptor(), "failed to init channel decryptor: "); + + return td::actor::create_actor("channel", local_id, peer_id, peer_pair, in_id, out_id, + std::move(encryptor), std::move(decryptor)); +} + +AdnlChannelImpl::AdnlChannelImpl(AdnlNodeIdShort local_id, AdnlNodeIdShort peer_id, + td::actor::ActorId peer_pair, AdnlChannelIdShort in_id, + AdnlChannelIdShort out_id, std::unique_ptr encryptor, + std::unique_ptr decryptor) { + local_id_ = local_id; + peer_id_ = peer_id; + + encryptor_ = std::move(encryptor); + decryptor_ = std::move(decryptor); + + channel_in_id_ = in_id; + channel_out_id_ = out_id; + + peer_pair_ = peer_pair; + + VLOG(ADNL_INFO) << this << ": created"; +} + +void AdnlChannelImpl::decrypt(td::BufferSlice raw_data, td::Promise promise) { + TRY_RESULT_PROMISE_PREFIX(promise, data, decryptor_->decrypt(raw_data.as_slice()), + "failed to decrypt channel message: "); + TRY_RESULT_PROMISE_PREFIX(promise, tl_packet, fetch_tl_object(std::move(data), true), + "decrypted channel packet contains invalid TL scheme: "); + TRY_RESULT_PROMISE_PREFIX(promise, packet, AdnlPacket::create(std::move(tl_packet)), "received bad packet: "); + if (packet.inited_from_short() && packet.from_short() != peer_id_) { + promise.set_error(td::Status::Error(ErrorCode::protoviolation, "bad channel packet destination")); + return; + } + promise.set_value(std::move(packet)); +} + +void AdnlChannelImpl::send_message(td::uint32 priority, td::actor::ActorId conn, + td::BufferSlice data) { + auto E = encryptor_->encrypt(data.as_slice()); + if (E.is_error()) { + VLOG(ADNL_ERROR) << this << ": dropping OUT message: can not encrypt: " << E.move_as_error(); + return; + } + auto enc = E.move_as_ok(); + auto B = td::BufferSlice(enc.size() + 32); + td::MutableSlice S = B.as_slice(); + S.copy_from(channel_out_id_.as_slice()); + S.remove_prefix(32); + S.copy_from(enc.as_slice()); + td::actor::send_closure(conn, &AdnlNetworkConnection::send, local_id_, peer_id_, priority, std::move(B)); +} + +void AdnlChannelImpl::receive(td::BufferSlice data) { + auto P = td::PromiseCreator::lambda( + [peer = peer_pair_, channel_id = channel_in_id_, id = print_id()](td::Result R) { + if (R.is_error()) { + VLOG(ADNL_WARNING) << id << ": dropping IN message: can not decrypt: " << R.move_as_error(); + } else { + td::actor::send_closure(peer, &AdnlPeerPair::receive_packet_from_channel, channel_id, R.move_as_ok()); + } + }); + + decrypt(std::move(data), std::move(P)); +} + +} // namespace adnl + +} // namespace ton diff --git a/adnl/adnl-channel.h b/adnl/adnl-channel.h new file mode 100644 index 00000000..e56f040c --- /dev/null +++ b/adnl/adnl-channel.h @@ -0,0 +1,46 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +#include "adnl-local-id.h" +#include "adnl-peer.h" +#include "adnl-peer-table.h" +#include "adnl-network-manager.h" + +namespace ton { + +namespace adnl { + +class AdnlPeerPair; + +class AdnlChannel : public td::actor::Actor { + public: + static td::Result> create(privkeys::Ed25519 pk, pubkeys::Ed25519 pub, + AdnlNodeIdShort local_id, AdnlNodeIdShort peer_id, + AdnlChannelIdShort &out_id, AdnlChannelIdShort &in_id, + td::actor::ActorId peer_pair); + virtual void receive(td::BufferSlice data) = 0; + virtual void send_message(td::uint32 priority, td::actor::ActorId conn, + td::BufferSlice data) = 0; + virtual ~AdnlChannel() = default; +}; + +} // namespace adnl + +} // namespace ton diff --git a/adnl/adnl-channel.hpp b/adnl/adnl-channel.hpp new file mode 100644 index 00000000..1499f5c4 --- /dev/null +++ b/adnl/adnl-channel.hpp @@ -0,0 +1,82 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +#include "adnl-channel.h" +#include "keys/encryptor.h" + +namespace ton { + +namespace adnl { + +class AdnlPeerPair; + +class AdnlChannelImpl : public AdnlChannel { + public: + AdnlChannelImpl(AdnlNodeIdShort local_id, AdnlNodeIdShort peer_id, td::actor::ActorId peer_pair, + AdnlChannelIdShort in_id, AdnlChannelIdShort out_id, std::unique_ptr encryptor, + std::unique_ptr decryptor); + void decrypt(td::BufferSlice data, td::Promise promise); + void receive(td::BufferSlice data) override; + void send_message(td::uint32 priority, td::actor::ActorId conn, td::BufferSlice data) override; + + struct AdnlChannelPrintId { + AdnlChannelIdShort channel_out_id_; + AdnlChannelIdShort channel_in_id_; + AdnlNodeIdShort local_id_; + AdnlNodeIdShort peer_id_; + }; + + AdnlChannelPrintId print_id() const { + return AdnlChannelPrintId{channel_out_id_, channel_in_id_, local_id_, peer_id_}; + } + + private: + AdnlChannelIdShort channel_out_id_; + AdnlChannelIdShort channel_in_id_; + AdnlNodeIdShort local_id_; + AdnlNodeIdShort peer_id_; + std::unique_ptr encryptor_; + std::unique_ptr decryptor_; + td::actor::ActorId peer_pair_; +}; + +} // namespace adnl + +} // namespace ton + +namespace td { + +inline td::StringBuilder &operator<<(td::StringBuilder &sb, const ton::adnl::AdnlChannelImpl::AdnlChannelPrintId &id) { + sb << "[channel " << id.peer_id_ << "-" << id.local_id_ << " " << id.channel_out_id_ << "-" << id.channel_in_id_ + << "]"; + return sb; +} + +inline td::StringBuilder &operator<<(td::StringBuilder &sb, const ton::adnl::AdnlChannelImpl &channel) { + sb << channel.print_id(); + return sb; +} + +inline td::StringBuilder &operator<<(td::StringBuilder &sb, const ton::adnl::AdnlChannelImpl *channel) { + sb << channel->print_id(); + return sb; +} + +} // namespace td diff --git a/adnl/adnl-db.cpp b/adnl/adnl-db.cpp new file mode 100644 index 00000000..7043199e --- /dev/null +++ b/adnl/adnl-db.cpp @@ -0,0 +1,82 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "adnl-db.hpp" +#include "td/db/RocksDb.h" + +namespace ton { + +namespace adnl { + +void AdnlDbImpl::update(AdnlNodeIdShort local_id, AdnlNodeIdShort peer_id, AdnlDbItem item, + td::Promise promise) { + td::BufferSlice b{64}; + auto S = b.as_slice(); + S.copy_from(local_id.as_slice()); + S.remove_prefix(32); + S.copy_from(peer_id.as_slice()); + + auto obj = create_tl_object(static_cast(td::Clocks::system()), item.id.tl(), + item.addr_list.tl(), item.priority_addr_list.tl()); + + kv_->begin_transaction().ensure(); + kv_->set(b.as_slice(), serialize_tl_object(obj, true).as_slice()).ensure(); + kv_->commit_transaction().ensure(); +} + +void AdnlDbImpl::get(AdnlNodeIdShort local_id, AdnlNodeIdShort peer_id, td::Promise promise) { + td::BufferSlice b{64}; + auto S = b.as_slice(); + S.copy_from(local_id.as_slice()); + S.remove_prefix(32); + S.copy_from(peer_id.as_slice()); + + std::string value; + auto R = kv_->get(b.as_slice(), value); + R.ensure(); + if (R.move_as_ok() == td::KeyValue::GetStatus::NotFound) { + promise.set_error(td::Status::Error(ErrorCode::notready, "not in db")); + return; + } + auto F = fetch_tl_object(td::BufferSlice{value}, true); + F.ensure(); + auto f = F.move_as_ok(); + AdnlDbItem n; + auto id = AdnlNodeIdFull::create(f->id_); + id.ensure(); + n.id = id.move_as_ok(); + auto addr_list = AdnlAddressList::create(std::move(f->addr_list_)); + addr_list.ensure(); + n.addr_list = addr_list.move_as_ok(); + auto priority_addr_list = AdnlAddressList::create(std::move(f->priority_addr_list_)); + priority_addr_list.ensure(); + n.priority_addr_list = priority_addr_list.move_as_ok(); + promise.set_value(std::move(n)); +} + +void AdnlDbImpl::start_up() { + kv_ = std::make_shared(td::RocksDb::open(path_).move_as_ok()); +} + +td::actor::ActorOwn AdnlDb::create(std::string path) { + return td::actor::create_actor("adnldb", path); +} + +} // namespace adnl + +} // namespace ton diff --git a/adnl/adnl-db.h b/adnl/adnl-db.h new file mode 100644 index 00000000..d7bf9fd7 --- /dev/null +++ b/adnl/adnl-db.h @@ -0,0 +1,47 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +#include "td/actor/actor.h" + +#include "adnl.h" + +namespace ton { + +namespace adnl { + +struct AdnlDbItem { + AdnlNodeIdFull id; + AdnlAddressList addr_list; + AdnlAddressList priority_addr_list; +}; + +class AdnlDb : public td::actor::Actor { + public: + virtual ~AdnlDb() = default; + virtual void update(AdnlNodeIdShort local_id, AdnlNodeIdShort peer_id, AdnlDbItem item, + td::Promise promise) = 0; + virtual void get(AdnlNodeIdShort local_id, AdnlNodeIdShort peer_id, td::Promise promise) = 0; + + static td::actor::ActorOwn create(std::string path); +}; + +} // namespace adnl + +} // namespace ton diff --git a/adnl/adnl-db.hpp b/adnl/adnl-db.hpp new file mode 100644 index 00000000..077279fd --- /dev/null +++ b/adnl/adnl-db.hpp @@ -0,0 +1,47 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +#include "adnl-db.h" +#include "td/db/KeyValue.h" + +namespace ton { + +namespace adnl { + +class AdnlDbImpl : public AdnlDb { + public: + void update(AdnlNodeIdShort local_id, AdnlNodeIdShort peer_id, AdnlDbItem item, + td::Promise promise) override; + void get(AdnlNodeIdShort local_id, AdnlNodeIdShort peer_id, td::Promise promise) override; + + void start_up() override; + + AdnlDbImpl(std::string path) : path_(path) { + } + + private: + std::string path_; + + std::shared_ptr kv_; +}; + +} // namespace adnl + +} // namespace ton diff --git a/adnl/adnl-ext-client.cpp b/adnl/adnl-ext-client.cpp new file mode 100644 index 00000000..efc71e6f --- /dev/null +++ b/adnl/adnl-ext-client.cpp @@ -0,0 +1,181 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "adnl-ext-client.hpp" +#include "adnl-ext-client.h" + +namespace ton { + +namespace adnl { + +void AdnlExtClientImpl::alarm() { + if (is_closing_) { + return; + } + if (conn_.empty() || !conn_.is_alive()) { + next_create_at_ = td::Timestamp::in(10.0); + alarm_timestamp() = next_create_at_; + + auto fd = td::SocketFd::open(dst_addr_); + if (fd.is_error()) { + LOG(INFO) << "failed to connect to " << dst_addr_ << ": " << fd.move_as_error(); + return; + } + + class Cb : public AdnlExtConnection::Callback { + private: + td::actor::ActorId id_; + + public: + void on_ready(td::actor::ActorId conn) { + td::actor::send_closure(id_, &AdnlExtClientImpl::conn_ready, conn); + } + void on_close(td::actor::ActorId conn) { + td::actor::send_closure(id_, &AdnlExtClientImpl::conn_stopped, conn); + } + Cb(td::actor::ActorId id) : id_(id) { + } + }; + + conn_ = td::actor::create_actor(td::actor::ActorOptions().with_name("outconn").with_poll(), + fd.move_as_ok(), std::make_unique(actor_id(this)), dst_, + local_id_, actor_id(this)); + } +} + +void AdnlExtClientImpl::hangup() { + conn_ = {}; + is_closing_ = true; + ref_cnt_--; + try_stop(); +} + +void AdnlExtClientImpl::try_stop() { + if (is_closing_ && ref_cnt_ == 0 && out_queries_.empty()) { + stop(); + } +} + +td::Status AdnlOutboundConnection::process_custom_packet(td::BufferSlice &data, bool &processed) { + if (data.size() == 12) { + auto F = fetch_tl_object(data.clone(), true); + if (F.is_ok()) { + processed = true; + return td::Status::OK(); + } + } + if (!local_id_.empty() && nonce_.size() != 0) { + auto F = fetch_tl_object(data.clone(), true); + if (F.is_ok()) { + auto f = F.move_as_ok(); + if (f->nonce_.size() == 0 || f->nonce_.size() > 512) { + return td::Status::Error(ErrorCode::protoviolation, "bad nonce size"); + } + td::SecureString ss{nonce_.size() + f->nonce_.size()}; + ss.as_mutable_slice().copy_from(nonce_.as_slice()); + ss.as_mutable_slice().remove_prefix(nonce_.size()).copy_from(f->nonce_.as_slice()); + + TRY_RESULT(dec, local_id_.create_decryptor()); + TRY_RESULT(B, dec->sign(ss.as_slice())); + + auto obj = + create_tl_object(local_id_.compute_public_key().tl(), std::move(B)); + send(serialize_tl_object(obj, true)); + + nonce_.clear(); + + processed = true; + authorization_complete_ = true; + return td::Status::OK(); + } + } + return td::Status::OK(); +} + +void AdnlOutboundConnection::start_up() { + AdnlExtConnection::start_up(); + auto X = dst_.pubkey().create_encryptor(); + if (X.is_error()) { + LOG(ERROR) << "failed to init encryptor: " << X.move_as_error(); + stop(); + return; + } + auto enc = X.move_as_ok(); + + td::BufferSlice d{256}; + auto id = dst_.compute_short_id(); + auto S = d.as_slice(); + S.copy_from(id.as_slice()); + S.remove_prefix(32); + S.truncate(256 - 64 - 32); + td::Random::secure_bytes(S); + init_crypto(S); + + auto R = enc->encrypt(S); + if (R.is_error()) { + LOG(ERROR) << "failed to encrypt: " << R.move_as_error(); + stop(); + return; + } + auto data = R.move_as_ok(); + LOG_CHECK(data.size() == 256 - 32) << "size=" << data.size(); + S = d.as_slice(); + S.remove_prefix(32); + CHECK(S.size() == data.size()); + S.copy_from(data.as_slice()); + + send_uninit(std::move(d)); + + if (!local_id_.empty()) { + nonce_ = td::SecureString{32}; + td::Random::secure_bytes(nonce_.as_mutable_slice()); + auto obj = create_tl_object(td::BufferSlice{nonce_.as_slice()}); + send(serialize_tl_object(obj, true)); + } +} + +void AdnlExtClientImpl::check_ready(td::Promise promise) { + if (conn_.empty() || !conn_.is_alive()) { + promise.set_error(td::Status::Error(ErrorCode::notready, "not ready")); + return; + } + td::actor::send_closure(td::actor::ActorId{conn_.get()}, &AdnlExtConnection::check_ready_async, + std::move(promise)); +} + +td::actor::ActorOwn AdnlExtClient::create(AdnlNodeIdFull dst, td::IPAddress dst_addr, + std::unique_ptr callback) { + return td::actor::create_actor("extclient", std::move(dst), dst_addr, std::move(callback)); +} + +td::actor::ActorOwn AdnlExtClient::create(AdnlNodeIdFull dst, PrivateKey local_id, + td::IPAddress dst_addr, + std::unique_ptr callback) { + return td::actor::create_actor("extclient", std::move(dst), std::move(local_id), dst_addr, + std::move(callback)); +} + +td::Status AdnlOutboundConnection::process_packet(td::BufferSlice data) { + TRY_RESULT(F, fetch_tl_object(std::move(data), true)); + td::actor::send_closure(ext_client_, &AdnlExtClientImpl::answer_query, F->query_id_, std::move(F->answer_)); + return td::Status::OK(); +} + +} // namespace adnl + +} // namespace ton diff --git a/adnl/adnl-ext-client.h b/adnl/adnl-ext-client.h new file mode 100644 index 00000000..babccced --- /dev/null +++ b/adnl/adnl-ext-client.h @@ -0,0 +1,48 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +#include "adnl-node-id.hpp" +#include "td/utils/port/IPAddress.h" + +namespace ton { + +namespace adnl { + +class AdnlExtClient : public td::actor::Actor { + public: + class Callback { + public: + virtual ~Callback() = default; + virtual void on_ready() = 0; + virtual void on_stop_ready() = 0; + }; + virtual ~AdnlExtClient() = default; + virtual void check_ready(td::Promise promise) = 0; + virtual void send_query(std::string name, td::BufferSlice data, td::Timestamp timeout, + td::Promise promise) = 0; + static td::actor::ActorOwn create(AdnlNodeIdFull dst, td::IPAddress dst_addr, + std::unique_ptr callback); + static td::actor::ActorOwn create(AdnlNodeIdFull dst, PrivateKey local_id, td::IPAddress dst_addr, + std::unique_ptr callback); +}; + +} // namespace adnl + +} // namespace ton diff --git a/adnl/adnl-ext-client.hpp b/adnl/adnl-ext-client.hpp new file mode 100644 index 00000000..f50a3130 --- /dev/null +++ b/adnl/adnl-ext-client.hpp @@ -0,0 +1,145 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +#include "auto/tl/lite_api.h" +#include "adnl-ext-connection.hpp" +#include "tl-utils/lite-utils.hpp" +#include "td/utils/Random.h" +#include "adnl-query.h" +#include "keys/encryptor.h" +#include "adnl-ext-client.h" + +namespace ton { + +namespace adnl { + +class AdnlExtClientImpl; + +class AdnlOutboundConnection : public AdnlExtConnection { + private: + AdnlNodeIdFull dst_; + PrivateKey local_id_; + td::actor::ActorId ext_client_; + td::SecureString nonce_; + bool authorization_complete_ = false; + + public: + AdnlOutboundConnection(td::SocketFd fd, std::unique_ptr callback, AdnlNodeIdFull dst, + td::actor::ActorId ext_client) + : AdnlExtConnection(std::move(fd), std::move(callback), true), dst_(std::move(dst)), ext_client_(ext_client) { + } + AdnlOutboundConnection(td::SocketFd fd, std::unique_ptr callback, AdnlNodeIdFull dst, + PrivateKey local_id, td::actor::ActorId ext_client) + : AdnlExtConnection(std::move(fd), std::move(callback), true) + , dst_(std::move(dst)) + , local_id_(local_id) + , ext_client_(ext_client) { + } + td::Status process_packet(td::BufferSlice data) override; + td::Status process_init_packet(td::BufferSlice data) override { + UNREACHABLE(); + } + td::Status process_custom_packet(td::BufferSlice &data, bool &processed) override; + void start_up() override; + bool authorized() const override { + return local_id_.empty() ? true : authorization_complete_; + } +}; + +class AdnlExtClientImpl : public AdnlExtClient { + public: + AdnlExtClientImpl(AdnlNodeIdFull dst_id, td::IPAddress dst_addr, std::unique_ptr callback) + : dst_(std::move(dst_id)), dst_addr_(dst_addr), callback_(std::move(callback)) { + } + AdnlExtClientImpl(AdnlNodeIdFull dst_id, PrivateKey local_id, td::IPAddress dst_addr, + std::unique_ptr callback) + : dst_(std::move(dst_id)), local_id_(local_id), dst_addr_(dst_addr), callback_(std::move(callback)) { + } + + void start_up() override { + alarm_timestamp() = next_create_at_; + } + void conn_stopped(td::actor::ActorId conn) { + if (!conn_.empty() && conn_.get() == conn) { + callback_->on_stop_ready(); + conn_ = {}; + alarm_timestamp() = next_create_at_; + try_stop(); + } + } + void conn_ready(td::actor::ActorId conn) { + if (!conn_.empty() && conn_.get() == conn) { + callback_->on_ready(); + } + } + void check_ready(td::Promise promise) override; + void send_query(std::string name, td::BufferSlice data, td::Timestamp timeout, + td::Promise promise) override { + auto P = [SelfId = actor_id(this)](AdnlQueryId id) { + td::actor::send_closure(SelfId, &AdnlExtClientImpl::destroy_query, id); + }; + auto q_id = generate_next_query_id(); + out_queries_.emplace(q_id, AdnlQuery::create(std::move(promise), std::move(P), name, timeout, q_id)); + if (!conn_.empty()) { + auto obj = create_tl_object(q_id, std::move(data)); + td::actor::send_closure(conn_, &AdnlOutboundConnection::send, serialize_tl_object(obj, true)); + } + } + void destroy_query(AdnlQueryId id) { + out_queries_.erase(id); + try_stop(); + } + void answer_query(AdnlQueryId id, td::BufferSlice data) { + auto it = out_queries_.find(id); + if (it != out_queries_.end()) { + td::actor::send_closure(it->second, &AdnlQuery::result, std::move(data)); + } + } + void alarm() override; + void hangup() override; + AdnlQueryId generate_next_query_id() { + while (true) { + AdnlQueryId q_id = AdnlQuery::random_query_id(); + if (out_queries_.count(q_id) == 0) { + return q_id; + } + } + } + + private: + AdnlNodeIdFull dst_; + PrivateKey local_id_; + td::IPAddress dst_addr_; + + std::unique_ptr callback_; + + td::actor::ActorOwn conn_; + td::Timestamp next_create_at_ = td::Timestamp::now_cached(); + + std::map> out_queries_; + + bool is_closing_{false}; + td::uint32 ref_cnt_{1}; + void try_stop(); +}; + +} // namespace adnl + +} // namespace ton diff --git a/adnl/adnl-ext-connection.cpp b/adnl/adnl-ext-connection.cpp new file mode 100644 index 00000000..ffae186b --- /dev/null +++ b/adnl/adnl-ext-connection.cpp @@ -0,0 +1,192 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "adnl-ext-connection.hpp" + +namespace ton { + +namespace adnl { + +void AdnlExtConnection::send_uninit(td::BufferSlice data) { + buffered_fd_.output_buffer().append(std::move(data)); + loop(); +} + +void AdnlExtConnection::send(td::BufferSlice data) { + LOG(DEBUG) << "sending packet of size " << data.size(); + auto data_size = td::narrow_cast(data.size()) + 32 + 32; + if (data_size < 32 || data_size > (1 << 24)) { + LOG(WARNING) << "bad packet size " << data_size; + return; + } + + td::BufferSlice d{data.size() + 4 + 32 + 32}; + auto S = d.as_slice(); + + S.copy_from(td::Slice(reinterpret_cast(&data_size), 4)); + S.remove_prefix(4); + auto Sc = S; + td::Random::secure_bytes(S.copy().truncate(32)); + S.remove_prefix(32); + S.copy_from(data.as_slice()); + S.remove_prefix(data.size()); + + td::sha256(Sc.truncate(32 + data.size()), S); + + td::BufferSlice e{d.size()}; + + out_ctr_.encrypt(d.as_slice(), e.as_slice()); + + buffered_fd_.output_buffer().append(std::move(e)); + loop(); +} + +td::Status AdnlExtConnection::receive(td::ChainBufferReader &input, bool &exit_loop) { + if (stop_read_) { + exit_loop = true; + return td::Status::OK(); + } + if (input.size() > 0) { + received_bytes_ = 1; + } + if (inited_) { + if (!read_len_) { + if (input.size() < 4) { + exit_loop = true; + return td::Status::OK(); + } + + char x[4]; + td::MutableSlice s{x, 4}; + input.advance(4, s); + + td::MutableSlice e{reinterpret_cast(&len_), 4}; + in_ctr_.encrypt(s, e); + LOG(DEBUG) << "len=" << len_; + if (len_ > (1 << 24) || len_ < 32) { + return td::Status::Error("Too big packet"); + } + read_len_ = true; + } + if (input.size() < len_) { + exit_loop = true; + return td::Status::OK(); + } + auto data = input.cut_head(len_).move_as_buffer_slice(); + update_timer(); + + td::BufferSlice dec_data{data.size()}; + in_ctr_.encrypt(data.as_slice(), dec_data.as_slice()); + + exit_loop = false; + read_len_ = false; + len_ = 0; + return receive_packet(std::move(dec_data)); + } else { + if (input.size() < 256) { + exit_loop = true; + return td::Status::OK(); + } + + auto data = input.cut_head(256).move_as_buffer_slice(); + update_timer(); + + exit_loop = false; + return process_init_packet(std::move(data)); + } +} + +void AdnlExtConnection::loop() { + auto status = [&] { + TRY_STATUS(buffered_fd_.flush_read()); + auto &input = buffered_fd_.input_buffer(); + bool exit_loop = false; + while (!exit_loop) { + TRY_STATUS(receive(input, exit_loop)); + } + TRY_STATUS(buffered_fd_.flush_write()); + if (td::can_close(buffered_fd_)) { + stop(); + } + return td::Status::OK(); + }(); + if (status.is_error()) { + LOG(ERROR) << "Client got error " << status; + stop(); + } else { + send_ready(); + } +} + +td::Status AdnlExtConnection::init_crypto(td::Slice S) { + if (S.size() < 96) { + return td::Status::Error(ErrorCode::protoviolation, "too small enc data"); + } + CHECK(S.size() >= 96); + td::SecureString s1(32), s2(32); + td::SecureString v1(16), v2(16); + s1.as_mutable_slice().copy_from(S.copy().truncate(32)); + S.remove_prefix(32); + s2.as_mutable_slice().copy_from(S.copy().truncate(32)); + S.remove_prefix(32); + v1.as_mutable_slice().copy_from(S.copy().truncate(16)); + S.remove_prefix(16); + v2.as_mutable_slice().copy_from(S.copy().truncate(16)); + S.remove_prefix(16); + if (is_client_) { + in_ctr_.init(s1, v1); + out_ctr_.init(s2, v2); + } else { + in_ctr_.init(s2, v2); + out_ctr_.init(s1, v1); + } + inited_ = true; + return td::Status::OK(); +} + +td::Status AdnlExtConnection::receive_packet(td::BufferSlice data) { + LOG(DEBUG) << "received packet of size " << data.size(); + auto S = data.as_slice(); + S.truncate(data.size() - 32); + auto D = data.as_slice(); + D.remove_prefix(data.size() - 32); + + if (td::sha256(S) != D) { + return td::Status::Error(ErrorCode::protoviolation, "sha256 mismatch"); + } + + data.truncate(data.size() - 32); + data.confirm_read(32); + + if (data.size() == 0) { + // keepalive + return td::Status::OK(); + } + + bool processed = false; + TRY_STATUS(process_custom_packet(data, processed)); + if (processed) { + return td::Status::OK(); + } + + return process_packet(std::move(data)); +} + +} // namespace adnl + +} // namespace ton diff --git a/adnl/adnl-ext-connection.hpp b/adnl/adnl-ext-connection.hpp new file mode 100644 index 00000000..0b7f1012 --- /dev/null +++ b/adnl/adnl-ext-connection.hpp @@ -0,0 +1,163 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +#include "td/net/TcpListener.h" +#include "td/utils/crypto.h" +#include "td/utils/BufferedFd.h" +#include "tl-utils/tl-utils.hpp" +#include "td/utils/Random.h" +#include "common/errorcode.h" + +#include +#include + +namespace ton { + +namespace adnl { + +class AdnlExtConnection : public td::actor::Actor, public td::ObserverBase { + public: + class Callback { + public: + virtual ~Callback() = default; + virtual void on_close(td::actor::ActorId conn) = 0; + virtual void on_ready(td::actor::ActorId conn) = 0; + }; + + double timeout() { + return is_client_ ? 20.0 : 60.0; + } + + AdnlExtConnection(td::SocketFd fd, std::unique_ptr callback, bool is_client) + : buffered_fd_(std::move(fd)), callback_(std::move(callback)), is_client_(is_client) { + } + void send(td::BufferSlice data); + void send_uninit(td::BufferSlice data); + td::Status receive(td::ChainBufferReader &input, bool &exit_loop); + virtual td::Status process_packet(td::BufferSlice data) = 0; + td::Status receive_packet(td::BufferSlice data); + virtual td::Status process_custom_packet(td::BufferSlice &data, bool &processed) = 0; + virtual td::Status process_init_packet(td::BufferSlice data) = 0; + virtual bool authorized() const { + return false; + } + td::Status init_crypto(td::Slice data); + void stop_read() { + stop_read_ = true; + } + void resume_read() { + stop_read_ = false; + } + bool check_ready() const { + return received_bytes_ && inited_ && authorized() && !td::can_close(buffered_fd_); + } + void check_ready_async(td::Promise promise) { + if (check_ready()) { + promise.set_value(td::Unit()); + } else { + promise.set_error(td::Status::Error(ErrorCode::notready, "not ready")); + } + } + void send_ready() { + if (check_ready() && !sent_ready_ && callback_) { + callback_->on_ready(actor_id(this)); + sent_ready_ = true; + } + } + + protected: + td::BufferedFd buffered_fd_; + td::actor::ActorId self_; + std::unique_ptr callback_; + bool sent_ready_ = false; + bool is_client_; + + void notify() override { + // NB: Interface will be changed + td::actor::send_closure_later(self_, &AdnlExtConnection::on_net); + } + + void start_up() override { + self_ = actor_id(this); + // Subscribe for socket updates + // NB: Interface will be changed + td::actor::SchedulerContext::get()->get_poll().subscribe(buffered_fd_.get_poll_info().extract_pollable_fd(this), + td::PollFlags::ReadWrite()); + update_timer(); + notify(); + } + + private: + td::AesCtrState in_ctr_; + td::AesCtrState out_ctr_; + bool inited_ = false; + bool stop_read_ = false; + bool read_len_ = false; + td::uint32 len_; + td::uint32 received_bytes_ = 0; + td::Timestamp fail_at_; + td::Timestamp send_ping_at_; + bool ping_sent_ = false; + + void on_net() { + loop(); + } + + void tear_down() override { + if (callback_) { + callback_->on_close(actor_id(this)); + callback_ = nullptr; + } + // unsubscribe from socket updates + // nb: interface will be changed + td::actor::SchedulerContext::get()->get_poll().unsubscribe(buffered_fd_.get_poll_info().get_pollable_fd_ref()); + } + + void update_timer() { + fail_at_ = td::Timestamp::in(timeout()); + alarm_timestamp() = fail_at_; + if (is_client_) { + ping_sent_ = false; + send_ping_at_ = td::Timestamp::in(timeout() / 2); + alarm_timestamp().relax(send_ping_at_); + } + } + + void loop() override; + + void alarm() override { + alarm_timestamp() = fail_at_; + if (fail_at_.is_in_past()) { + stop(); + } else if (is_client_ && !ping_sent_) { + if (send_ping_at_.is_in_past()) { + auto obj = create_tl_object(td::Random::fast_uint64()); + send(serialize_tl_object(obj, true)); + ping_sent_ = true; + } else { + alarm_timestamp().relax(send_ping_at_); + } + } + } +}; + +} // namespace adnl + +} // namespace ton diff --git a/adnl/adnl-ext-server.cpp b/adnl/adnl-ext-server.cpp new file mode 100644 index 00000000..1fab1143 --- /dev/null +++ b/adnl/adnl-ext-server.cpp @@ -0,0 +1,182 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "adnl-ext-server.hpp" +#include "keys/encryptor.h" +#include "utils.hpp" + +namespace ton { + +namespace adnl { + +td::Status AdnlInboundConnection::process_packet(td::BufferSlice data) { + TRY_RESULT(f, fetch_tl_object(std::move(data), true)); + + auto P = + td::PromiseCreator::lambda([SelfId = actor_id(this), query_id = f->query_id_](td::Result R) { + if (R.is_error()) { + auto S = R.move_as_error(); + LOG(WARNING) << "failed ext query: " << S; + } else { + auto B = create_tl_object(query_id, R.move_as_ok()); + td::actor::send_closure(SelfId, &AdnlInboundConnection::send, serialize_tl_object(B, true)); + } + }); + td::actor::send_closure(peer_table_, &AdnlPeerTable::deliver_query, remote_id_, local_id_, std::move(f->query_), + std::move(P)); + return td::Status::OK(); +} + +td::Status AdnlInboundConnection::process_init_packet(td::BufferSlice data) { + if (data.size() < 32) { + return td::Status::Error(ErrorCode::protoviolation, "too small init packet"); + } + local_id_ = AdnlNodeIdShort{data.as_slice().truncate(32)}; + data.confirm_read(32); + + auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result R) { + td::actor::send_closure(SelfId, &AdnlInboundConnection::inited_crypto, std::move(R)); + }); + + td::actor::send_closure(ext_server_, &AdnlExtServerImpl::decrypt_init_packet, local_id_, std::move(data), + std::move(P)); + stop_read(); + return td::Status::OK(); +} + +void AdnlInboundConnection::inited_crypto(td::Result R) { + if (R.is_error()) { + LOG(ERROR) << "failed to init crypto: " << R.move_as_error(); + stop(); + return; + } + auto S = init_crypto(R.move_as_ok().as_slice()); + if (S.is_error()) { + LOG(ERROR) << "failed to init crypto (2): " << R.move_as_error(); + stop(); + return; + } + send(td::BufferSlice()); + resume_read(); + notify(); +} + +td::Status AdnlInboundConnection::process_custom_packet(td::BufferSlice &data, bool &processed) { + if (data.size() == 12) { + auto F = fetch_tl_object(data.clone(), true); + if (F.is_ok()) { + auto f = F.move_as_ok(); + auto obj = create_tl_object(f->random_id_); + send(serialize_tl_object(obj, true)); + processed = true; + return td::Status::OK(); + } + } + if (1) { + auto F = fetch_tl_object(data.clone(), true); + if (F.is_ok()) { + if (nonce_.size() > 0 || !remote_id_.is_zero()) { + return td::Status::Error(ErrorCode::protoviolation, "duplicate authentificate"); + } + auto f = F.move_as_ok(); + nonce_ = td::SecureString{f->nonce_.size() + 256}; + nonce_.as_mutable_slice().truncate(f->nonce_.size()).copy_from(f->nonce_.as_slice()); + td::Random::secure_bytes(nonce_.as_mutable_slice().remove_prefix(f->nonce_.size())); + + auto obj = create_tl_object( + td::BufferSlice{nonce_.as_slice().remove_prefix(f->nonce_.size())}); + send(serialize_tl_object(obj, true)); + processed = true; + return td::Status::OK(); + } + } + + if (nonce_.size() != 0) { + auto F = fetch_tl_object(data.clone(), true); + if (F.is_ok()) { + auto f = F.move_as_ok(); + if (nonce_.size() == 0 || !remote_id_.is_zero()) { + return td::Status::Error(ErrorCode::protoviolation, "duplicate authentificate"); + } + + auto pub_key = PublicKey{f->key_}; + TRY_RESULT(enc, pub_key.create_encryptor()); + TRY_STATUS(enc->check_signature(nonce_.as_slice(), f->signature_.as_slice())); + + remote_id_ = AdnlNodeIdShort{pub_key.compute_short_id()}; + nonce_.clear(); + processed = true; + return td::Status::OK(); + } + } + + return td::Status::OK(); +} + +void AdnlExtServerImpl::add_tcp_port(td::uint16 port) { + auto it = listeners_.find(port); + if (it != listeners_.end()) { + return; + } + + class Callback : public td::TcpListener::Callback { + private: + td::actor::ActorId id_; + + public: + Callback(td::actor::ActorId id) : id_(id) { + } + void accept(td::SocketFd fd) override { + td::actor::send_closure(id_, &AdnlExtServerImpl::accepted, std::move(fd)); + } + }; + + auto act = td::actor::create_actor(td::actor::ActorOptions().with_name("listener").with_poll(), port, + std::make_unique(actor_id(this))); + listeners_.emplace(port, std::move(act)); +} + +void AdnlExtServerImpl::add_local_id(AdnlNodeIdShort id) { + local_ids_.insert(id); +} + +void AdnlExtServerImpl::accepted(td::SocketFd fd) { + td::actor::create_actor(td::actor::ActorOptions().with_name("inconn").with_poll(), + std::move(fd), peer_table_, actor_id(this)) + .release(); +} + +void AdnlExtServerImpl::decrypt_init_packet(AdnlNodeIdShort dst, td::BufferSlice data, + td::Promise promise) { + auto it = local_ids_.find(dst); + if (it != local_ids_.end()) { + td::actor::send_closure(peer_table_, &AdnlPeerTable::decrypt_message, dst, std::move(data), std::move(promise)); + } else { + promise.set_error(td::Status::Error()); + } +} + +td::actor::ActorOwn AdnlExtServerCreator::create(td::actor::ActorId adnl, + std::vector ids, + std::vector ports) { + return td::actor::create_actor("extserver", adnl, std::move(ids), std::move(ports)); +} + +} // namespace adnl + +} // namespace ton diff --git a/adnl/adnl-ext-server.h b/adnl/adnl-ext-server.h new file mode 100644 index 00000000..10e0ca9c --- /dev/null +++ b/adnl/adnl-ext-server.h @@ -0,0 +1,35 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +#include "adnl-peer-table.h" + +namespace ton { + +namespace adnl { + +class AdnlExtServerCreator { + public: + static td::actor::ActorOwn create(td::actor::ActorId adnl, + std::vector ids, std::vector ports); +}; + +} // namespace adnl + +} // namespace ton diff --git a/adnl/adnl-ext-server.hpp b/adnl/adnl-ext-server.hpp new file mode 100644 index 00000000..3cc32f25 --- /dev/null +++ b/adnl/adnl-ext-server.hpp @@ -0,0 +1,92 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +#include "adnl-peer-table.h" +#include "td/net/TcpListener.h" +#include "td/utils/crypto.h" +#include "td/utils/BufferedFd.h" +#include "adnl-ext-connection.hpp" +#include "adnl-ext-server.h" + +#include +#include + +namespace ton { + +namespace adnl { + +class AdnlExtServerImpl; + +class AdnlInboundConnection : public AdnlExtConnection { + public: + AdnlInboundConnection(td::SocketFd fd, td::actor::ActorId peer_table, + td::actor::ActorId ext_server) + : AdnlExtConnection(std::move(fd), nullptr, false), peer_table_(peer_table), ext_server_(ext_server) { + } + + td::Status process_packet(td::BufferSlice data) override; + td::Status process_init_packet(td::BufferSlice data) override; + td::Status process_custom_packet(td::BufferSlice &data, bool &processed) override; + void inited_crypto(td::Result R); + + private: + td::actor::ActorId peer_table_; + td::actor::ActorId ext_server_; + AdnlNodeIdShort local_id_; + + td::SecureString nonce_; + AdnlNodeIdShort remote_id_ = AdnlNodeIdShort::zero(); +}; + +class AdnlExtServerImpl : public AdnlExtServer { + public: + void add_tcp_port(td::uint16 port) override; + void add_local_id(AdnlNodeIdShort id) override; + void accepted(td::SocketFd fd); + void decrypt_init_packet(AdnlNodeIdShort dst, td::BufferSlice data, td::Promise promise); + + void start_up() override { + for (auto &port : ports_) { + add_tcp_port(port); + } + ports_.clear(); + } + + AdnlExtServerImpl(td::actor::ActorId adnl, std::vector ids, + std::vector ports) + : peer_table_(adnl) { + for (auto &id : ids) { + add_local_id(id); + } + for (auto &port : ports) { + ports_.insert(port); + } + } + + private: + td::actor::ActorId peer_table_; + std::set local_ids_; + std::set ports_; + std::map> listeners_; +}; + +} // namespace adnl + +} // namespace ton diff --git a/adnl/adnl-local-id.cpp b/adnl/adnl-local-id.cpp new file mode 100644 index 00000000..79b2bcd2 --- /dev/null +++ b/adnl/adnl-local-id.cpp @@ -0,0 +1,279 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "td/utils/crypto.h" +#include "td/utils/Random.h" + +#include "adnl-local-id.h" +#include "keys/encryptor.h" +#include "utils.hpp" + +namespace ton { + +namespace adnl { + +AdnlNodeIdFull AdnlLocalId::get_id() const { + return id_; +} + +AdnlNodeIdShort AdnlLocalId::get_short_id() const { + return short_id_; +} + +AdnlAddressList AdnlLocalId::get_addr_list() const { + CHECK(!addr_list_.empty()); + return addr_list_; +} + +void AdnlLocalId::receive(td::BufferSlice data) { + auto P = td::PromiseCreator::lambda( + [peer_table = peer_table_, dst = short_id_, id = print_id()](td::Result R) { + if (R.is_error()) { + VLOG(ADNL_WARNING) << id << ": dropping IN message: cannot decrypt: " << R.move_as_error(); + } else { + td::actor::send_closure(peer_table, &AdnlPeerTable::receive_decrypted_packet, dst, R.move_as_ok()); + } + }); + + decrypt(std::move(data), std::move(P)); +} + +void AdnlLocalId::deliver(AdnlNodeIdShort src, td::BufferSlice data) { + auto s = std::move(data); + for (auto &cb : cb_) { + auto f = cb.first; + if (f.length() <= s.length() && s.as_slice().substr(0, f.length()) == f) { + cb.second->receive_message(src, short_id_, std::move(s)); + return; + } + } + VLOG(ADNL_INFO) << this << ": dropping IN message from " << src + << ": no callbacks for custom message. firstint=" << td::TlParser(s.as_slice()).fetch_int(); +} + +void AdnlLocalId::deliver_query(AdnlNodeIdShort src, td::BufferSlice data, td::Promise promise) { + auto s = std::move(data); + for (auto &cb : cb_) { + auto f = cb.first; + if (f.length() <= s.length() && s.as_slice().substr(0, f.length()) == f) { + cb.second->receive_query(src, short_id_, std::move(s), std::move(promise)); + return; + } + } + VLOG(ADNL_INFO) << this << ": dropping IN message from " << src + << ": no callbacks for custom query. firstint=" << td::TlParser(s.as_slice()).fetch_int(); + promise.set_error(td::Status::Error(ErrorCode::warning, "no callbacks for query")); +} + +void AdnlLocalId::subscribe(std::string prefix, std::unique_ptr callback) { + auto S = td::Slice(prefix); + for (auto &cb : cb_) { + auto G = td::Slice(cb.first); + if (S.size() < G.size()) { + LOG_CHECK(G.substr(0, S.size()) != S) << this << ": duplicate subscribe prefix"; + } else { + LOG_CHECK(S.substr(0, G.size()) != G) << this << ": duplicate subscribe prefix"; + } + } + cb_.emplace_back(prefix, std::move(callback)); +} + +void AdnlLocalId::unsubscribe(std::string prefix) { + bool deleted = false; + for (auto it = cb_.begin(); it != cb_.end();) { + if (it->first == prefix) { + it = cb_.erase(it); + deleted = true; + } else { + it++; + } + } + LOG_CHECK(deleted) << this << ": cannot unsubscribe: prefix not found"; +} + +void AdnlLocalId::update_address_list(AdnlAddressList addr_list) { + addr_list_ = std::move(addr_list); + addr_list_.set_reinit_date(Adnl::adnl_start_time()); + addr_list_.set_version(static_cast(td::Clocks::system())); + + VLOG(ADNL_INFO) << this << ": updated addr list. New version set to " << addr_list_.version(); + + publish_address_list(); +} + +void AdnlLocalId::publish_address_list() { + if (dht_node_.empty() || addr_list_.empty()) { + VLOG(ADNL_NOTICE) << this << ": skipping public addr list, because localid (or dht node) not fully initialized"; + return; + } + + dht::DhtKey dht_key{short_id_.pubkey_hash(), "address", 0}; + auto dht_update_rule = dht::DhtUpdateRuleSignature::create().move_as_ok(); + dht::DhtKeyDescription dht_key_description{std::move(dht_key), id_.pubkey(), std::move(dht_update_rule), + td::BufferSlice()}; + + auto B = serialize_tl_object(dht_key_description.tl(), true); + + auto P = td::PromiseCreator::lambda([dht_node = dht_node_, SelfId = actor_id(this), addr_list = addr_list_.tl(), + dht_key_description = std::move(dht_key_description), + print_id = print_id()](td::Result R) mutable { + if (R.is_error()) { + LOG(ERROR) << print_id << ": cannot sign: " << R.move_as_error(); + return; + } + + dht_key_description.update_signature(R.move_as_ok()); + dht_key_description.check().ensure(); + + auto ttl = static_cast(td::Clocks::system() + 3600); + dht::DhtValue dht_value{std::move(dht_key_description), serialize_tl_object(addr_list, true), ttl, + td::BufferSlice("")}; + + auto B = serialize_tl_object(dht_value.tl(), true); + + auto Q = td::PromiseCreator::lambda( + [dht_node, dht_value = std::move(dht_value), print_id](td::Result R) mutable { + if (R.is_error()) { + LOG(ERROR) << print_id << ": cannot sign: " << R.move_as_error(); + return; + } + dht_value.update_signature(R.move_as_ok()); + dht_value.check().ensure(); + + auto E = td::PromiseCreator::lambda([print_id](td::Result R) { + if (R.is_error()) { + VLOG(ADNL_NOTICE) << print_id << ": failed to update addr list in DHT: " << R.move_as_error(); + } else { + VLOG(ADNL_INFO) << print_id << ": updated dht addr list"; + } + }); + + td::actor::send_closure(dht_node, &dht::Dht::set_value, std::move(dht_value), std::move(E)); + }); + + td::actor::send_closure(SelfId, &AdnlLocalId::sign_async, std::move(B), std::move(Q)); + }); + + td::actor::send_closure(keyring_, &keyring::Keyring::sign_message, short_id_.pubkey_hash(), std::move(B), + std::move(P)); +} + +AdnlLocalId::AdnlLocalId(AdnlNodeIdFull id, AdnlAddressList addr_list, td::actor::ActorId peer_table, + td::actor::ActorId keyring, td::actor::ActorId dht_node) { + id_ = std::move(id); + short_id_ = id_.compute_short_id(); + addr_list_ = std::move(addr_list); + if (addr_list_.addrs().size() > 0) { + addr_list_.set_version(static_cast(td::Clocks::system())); + } + peer_table_ = peer_table; + keyring_ = keyring; + dht_node_ = dht_node; + + VLOG(ADNL_INFO) << this << ": created local id " << short_id_; +} + +void AdnlLocalId::get_self_node(td::Promise promise) { + //addr_list_->version_ = static_cast(td::Clocks::system()); + promise.set_value(AdnlNode{id_, addr_list_}); +} + +void AdnlLocalId::decrypt_message(td::BufferSlice data, td::Promise promise) { + td::actor::send_closure(keyring_, &keyring::Keyring::decrypt_message, short_id_.pubkey_hash(), std::move(data), + std::move(promise)); +} + +void AdnlLocalId::decrypt(td::BufferSlice data, td::Promise promise) { + auto P = td::PromiseCreator::lambda( + [SelfId = actor_id(this), p = std::move(promise)](td::Result res) mutable { + if (res.is_error()) { + p.set_error(res.move_as_error()); + } else { + td::actor::send_closure_later(SelfId, &AdnlLocalId::decrypt_continue, res.move_as_ok(), std::move(p)); + } + }); + td::actor::send_closure(keyring_, &keyring::Keyring::decrypt_message, short_id_.pubkey_hash(), std::move(data), + std::move(P)); +} + +void AdnlLocalId::decrypt_continue(td::BufferSlice data, td::Promise promise) { + auto R = fetch_tl_object(std::move(data), true); + if (R.is_error()) { + promise.set_error(R.move_as_error()); + return; + } + + auto packetR = AdnlPacket::create(R.move_as_ok()); + if (packetR.is_error()) { + promise.set_error(packetR.move_as_error()); + return; + } + promise.set_value(packetR.move_as_ok()); +} + +void AdnlLocalId::sign_async(td::BufferSlice data, td::Promise promise) { + td::actor::send_closure(keyring_, &keyring::Keyring::sign_message, short_id_.pubkey_hash(), std::move(data), + std::move(promise)); +} + +void AdnlLocalId::sign_batch_async(std::vector data, + td::Promise>> promise) { + td::actor::send_closure(keyring_, &keyring::Keyring::sign_messages, short_id_.pubkey_hash(), std::move(data), + std::move(promise)); +} + +void AdnlLocalId::start_up() { + publish_address_list(); + alarm_timestamp() = td::Timestamp::in(AdnlPeerTable::republish_addr_list_timeout() * td::Random::fast(1.0, 2.0)); +} + +void AdnlLocalId::alarm() { + publish_address_list(); + alarm_timestamp() = td::Timestamp::in(AdnlPeerTable::republish_addr_list_timeout() * td::Random::fast(1.0, 2.0)); +} + +void AdnlLocalId::update_packet(AdnlPacket packet, bool update_id, bool sign, td::int32 update_addr_list_if, + td::int32 update_priority_addr_list_if, td::Promise promise) { + packet.init_random(); + if (update_id) { + packet.set_source(id_); + } + if (!addr_list_.empty() && update_addr_list_if < addr_list_.version()) { + packet.set_addr_list(addr_list_); + } + if (!sign) { + promise.set_result(std::move(packet)); + } else { + auto to_sign = packet.to_sign(); + auto P = td::PromiseCreator::lambda( + [packet = std::move(packet), promise = std::move(promise)](td::Result R) mutable { + if (R.is_error()) { + promise.set_error(R.move_as_error()); + } else { + packet.set_signature(R.move_as_ok()); + promise.set_value(std::move(packet)); + } + }); + td::actor::send_closure(keyring_, &keyring::Keyring::sign_message, short_id_.pubkey_hash(), std::move(to_sign), + std::move(P)); + } +} + +} // namespace adnl + +} // namespace ton diff --git a/adnl/adnl-local-id.h b/adnl/adnl-local-id.h new file mode 100644 index 00000000..07326f79 --- /dev/null +++ b/adnl/adnl-local-id.h @@ -0,0 +1,117 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +#include + +#include "td/actor/actor.h" +#include "td/utils/BufferedUdp.h" +#include "auto/tl/ton_api.h" +#include "keys/encryptor.h" +#include "adnl-peer-table.h" +#include "dht/dht.h" + +#include "adnl-peer-table.h" +#include "utils.hpp" + +namespace ton { + +namespace adnl { + +class AdnlLocalId : public td::actor::Actor { + public: + AdnlNodeIdFull get_id() const; + AdnlNodeIdShort get_short_id() const; + AdnlAddressList get_addr_list() const; + void get_addr_list_async(td::Promise P) { + P.set_value(get_addr_list()); + } + + void update_dht_node(td::actor::ActorId dht_node) { + dht_node_ = dht_node; + + publish_address_list(); + } + + void decrypt(td::BufferSlice data, td::Promise promise); + void decrypt_continue(td::BufferSlice data, td::Promise promise); + void decrypt_message(td::BufferSlice data, td::Promise promise); + void deliver(AdnlNodeIdShort src, td::BufferSlice data); + void deliver_query(AdnlNodeIdShort src, td::BufferSlice data, td::Promise promise); + void receive(td::BufferSlice data); + + void subscribe(std::string prefix, std::unique_ptr callback); + void unsubscribe(std::string prefix); + + void update_address_list(AdnlAddressList addr_list); + + void get_self_node(td::Promise promise); + + void sign_async(td::BufferSlice data, td::Promise promise); + void sign_batch_async(std::vector data, + td::Promise>> promise); + + AdnlLocalId(AdnlNodeIdFull id, AdnlAddressList addr_list, td::actor::ActorId peer_table, + td::actor::ActorId keyring, td::actor::ActorId dht_node); + + void start_up() override; + void alarm() override; + + void update_packet(AdnlPacket packet, bool update_id, bool sign, td::int32 update_addr_list_if, + td::int32 update_priority_addr_list_if, td::Promise promise); + + struct PrintId { + AdnlNodeIdShort id; + }; + + PrintId print_id() const { + return PrintId{short_id_}; + } + + private: + td::actor::ActorId peer_table_; + td::actor::ActorId keyring_; + td::actor::ActorId dht_node_; + std::vector>> cb_; + + AdnlAddressList addr_list_; + AdnlNodeIdFull id_; + AdnlNodeIdShort short_id_; + + void publish_address_list(); +}; + +inline td::StringBuilder &operator<<(td::StringBuilder &sb, const AdnlLocalId::PrintId &id) { + sb << "[localid " << id.id << "]"; + return sb; +} + +inline td::StringBuilder &operator<<(td::StringBuilder &sb, const AdnlLocalId &localid) { + sb << localid.print_id(); + return sb; +} + +inline td::StringBuilder &operator<<(td::StringBuilder &sb, const AdnlLocalId *localid) { + sb << localid->print_id(); + return sb; +} + +} // namespace adnl + +} // namespace ton diff --git a/adnl/adnl-message.cpp b/adnl/adnl-message.cpp new file mode 100644 index 00000000..4e09429a --- /dev/null +++ b/adnl/adnl-message.cpp @@ -0,0 +1,54 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "adnl/adnl-message.h" +#include "auto/tl/ton_api.hpp" +#include "td/utils/overloaded.h" + +namespace ton { + +namespace adnl { + +AdnlMessage::AdnlMessage(tl_object_ptr message) { + ton_api::downcast_call( + *message.get(), + td::overloaded( + [&](ton_api::adnl_message_createChannel &msg) { + message_ = adnlmessage::AdnlMessageCreateChannel{msg.key_, msg.date_}; + }, + [&](ton_api::adnl_message_confirmChannel &msg) { + message_ = adnlmessage::AdnlMessageConfirmChannel{msg.key_, msg.peer_key_, msg.date_}; + }, + [&](ton_api::adnl_message_custom &msg) { message_ = adnlmessage::AdnlMessageCustom{std::move(msg.data_)}; }, + [&](ton_api::adnl_message_nop &msg) { message_ = adnlmessage::AdnlMessageNop{}; }, + [&](ton_api::adnl_message_reinit &msg) { message_ = adnlmessage::AdnlMessageReinit{msg.date_}; }, + [&](ton_api::adnl_message_query &msg) { + message_ = adnlmessage::AdnlMessageQuery{msg.query_id_, std::move(msg.query_)}; + }, + [&](ton_api::adnl_message_answer &msg) { + message_ = adnlmessage::AdnlMessageAnswer{msg.query_id_, std::move(msg.answer_)}; + }, + [&](ton_api::adnl_message_part &msg) { + message_ = adnlmessage::AdnlMessagePart{msg.hash_, static_cast(msg.total_size_), + static_cast(msg.offset_), std::move(msg.data_)}; + })); +} + +} // namespace adnl + +} // namespace ton diff --git a/adnl/adnl-message.h b/adnl/adnl-message.h new file mode 100644 index 00000000..baf5cf4c --- /dev/null +++ b/adnl/adnl-message.h @@ -0,0 +1,296 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +#include "adnl/adnl.h" +#include "adnl/adnl-query.h" + +namespace ton { + +namespace adnl { + +namespace adnlmessage { + +class AdnlMessageCreateChannel { + public: + AdnlMessageCreateChannel(pubkeys::Ed25519 key, td::int32 date) : key_(key), date_(date) { + } + const auto &key() const { + return key_; + } + auto date() const { + return date_; + } + td::uint32 size() const { + return 40; + } + tl_object_ptr tl() const { + return create_tl_object(key_.raw(), date_); + } + + private: + pubkeys::Ed25519 key_; + td::int32 date_; +}; + +class AdnlMessageConfirmChannel { + public: + AdnlMessageConfirmChannel(pubkeys::Ed25519 key, pubkeys::Ed25519 peer_key, td::int32 date) + : key_(key), peer_key_(peer_key), date_(date) { + } + const auto &key() const { + return key_; + } + const auto &peer_key() const { + return peer_key_; + } + auto date() const { + return date_; + } + td::uint32 size() const { + return 72; + } + tl_object_ptr tl() const { + return create_tl_object(key_.raw(), peer_key_.raw(), date_); + } + + private: + pubkeys::Ed25519 key_; + pubkeys::Ed25519 peer_key_; + td::int32 date_; +}; + +class AdnlMessageCustom { + public: + AdnlMessageCustom(td::BufferSlice data) : data_(std::move(data)) { + } + auto data() const { + return data_.clone(); + } + td::uint32 size() const { + return static_cast(data_.size()) + 12; + } + tl_object_ptr tl() const { + return create_tl_object(data_.clone()); + } + + private: + td::BufferSlice data_; +}; + +class AdnlMessageNop { + public: + AdnlMessageNop() { + } + td::uint32 size() const { + return 4; + } + tl_object_ptr tl() const { + return create_tl_object(); + } + + private: +}; + +class AdnlMessageReinit { + public: + AdnlMessageReinit(td::int32 date) : date_(date) { + } + auto date() const { + return date_; + } + td::uint32 size() const { + return 8; + } + tl_object_ptr tl() const { + return create_tl_object(date_); + } + + private: + td::int32 date_; +}; + +class AdnlMessageQuery { + public: + AdnlMessageQuery(AdnlQueryId query_id, td::BufferSlice data) : query_id_(query_id), data_(std::move(data)) { + } + const auto &query_id() const { + return query_id_; + } + auto data() const { + return data_.clone(); + } + td::uint32 size() const { + return static_cast(data_.size()) + 44; + } + tl_object_ptr tl() const { + return create_tl_object(query_id_, data_.clone()); + } + + private: + AdnlQueryId query_id_; + td::BufferSlice data_; +}; + +class AdnlMessageAnswer { + public: + AdnlMessageAnswer(AdnlQueryId query_id, td::BufferSlice data) : query_id_(query_id), data_(std::move(data)) { + } + const auto &query_id() const { + return query_id_; + } + auto data() const { + return data_.clone(); + } + td::uint32 size() const { + return static_cast(data_.size()) + 44; + } + tl_object_ptr tl() const { + return create_tl_object(query_id_, data_.clone()); + } + + private: + AdnlQueryId query_id_; + td::BufferSlice data_; +}; + +class AdnlMessagePart { + public: + AdnlMessagePart(td::Bits256 hash, td::uint32 total_size, td::uint32 offset, td::BufferSlice data) + : hash_(hash), total_size_(total_size), offset_(offset), data_(std::move(data)) { + } + const auto &hash() const { + return hash_; + } + auto offset() const { + return offset_; + } + auto total_size() const { + return total_size_; + } + auto data() const { + return data_.clone(); + } + td::uint32 size() const { + return static_cast(data_.size()) + 48; + } + tl_object_ptr tl() const { + return create_tl_object(hash_, total_size_, offset_, data_.clone()); + } + + private: + td::Bits256 hash_; + td::uint32 total_size_; + td::uint32 offset_; + td::BufferSlice data_; +}; + +} // namespace adnlmessage + +class AdnlMessage { + public: + class Empty { + public: + Empty() { + } + td::uint32 size() const { + UNREACHABLE(); + } + tl_object_ptr tl() const { + UNREACHABLE(); + } + }; + + private: + td::Variant + message_{Empty{}}; + + public: + explicit AdnlMessage(tl_object_ptr message); + template + AdnlMessage(T m) : message_(std::move(m)) { + } + + tl_object_ptr tl() const { + tl_object_ptr res; + message_.visit([&](const auto &obj) { res = obj.tl(); }); + return res; + } + td::uint32 size() const { + td::uint32 res; + message_.visit([&](const auto &obj) { res = obj.size(); }); + return res; + } + template + void visit(F &&f) { + message_.visit(std::move(f)); + } + template + void visit(F &&f) const { + message_.visit(std::move(f)); + } +}; + +class AdnlMessageList { + public: + AdnlMessageList() { + } + AdnlMessageList(tl_object_ptr message) { + auto msg = AdnlMessage{std::move(message)}; + messages_.emplace_back(std::move(msg)); + } + AdnlMessageList(std::vector> messages) { + for (auto &message : messages) { + messages_.push_back(AdnlMessage{std::move(message)}); + } + } + void push_back(AdnlMessage message) { + messages_.push_back(std::move(message)); + } + + td::uint32 size() const { + return static_cast(messages_.size()); + } + tl_object_ptr one_message() const { + CHECK(size() == 1); + return messages_[0].tl(); + } + std::vector> mult_messages() const { + std::vector> vec; + for (auto &m : messages_) { + vec.emplace_back(m.tl()); + } + return vec; + } + static std::vector> empty_vector() { + return std::vector>{}; + } + auto &vector() { + return messages_; + } + + private: + std::vector messages_; +}; + +} // namespace adnl + +} // namespace ton diff --git a/adnl/adnl-network-manager.cpp b/adnl/adnl-network-manager.cpp new file mode 100644 index 00000000..c5c25463 --- /dev/null +++ b/adnl/adnl-network-manager.cpp @@ -0,0 +1,114 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "adnl-network-manager.hpp" +#include "adnl-peer-table.h" + +namespace ton { + +namespace adnl { + +td::actor::ActorOwn AdnlNetworkManager::create(td::uint16 port) { + return td::actor::create_actor("NetworkManager", port); +} + +void AdnlNetworkManagerImpl::add_listening_udp_port(td::uint16 port) { + class Callback : public td::UdpServer::Callback { + public: + Callback(td::actor::ActorShared manager) : manager_(std::move(manager)) { + } + + private: + td::actor::ActorShared manager_; + void on_udp_message(td::UdpMessage udp_message) override { + td::actor::send_closure_later(manager_, &AdnlNetworkManagerImpl::receive_udp_message, std::move(udp_message)); + } + }; + + auto X = td::UdpServer::create("udp server", port, std::make_unique(actor_shared(this))); + X.ensure(); + udp_servers_.emplace(port, X.move_as_ok()); +} + +void AdnlNetworkManagerImpl::receive_udp_message(td::UdpMessage message) { + if (!callback_) { + LOG(ERROR) << this << ": dropping IN message [?->?]: peer table unitialized"; + return; + } + if (message.error.is_error()) { + VLOG(ADNL_WARNING) << this << ": dropping ERROR message: " << message.error; + return; + } + if (message.data.size() >= get_mtu()) { + VLOG(ADNL_NOTICE) << this << ": received huge packet of size " << message.data.size(); + } + received_messages_++; + if (received_messages_ % 64 == 0) { + VLOG(ADNL_DEBUG) << this << ": received " << received_messages_ << "udp messages"; + } + + VLOG(ADNL_EXTRA_DEBUG) << this << ": received message of size " << message.data.size(); + callback_->receive_packet(message.address, std::move(message.data)); +} + +void AdnlNetworkManagerImpl::send_udp_packet(AdnlNodeIdShort src_id, AdnlNodeIdShort dst_id, td::IPAddress dst_addr, + td::uint32 priority, td::BufferSlice data) { + auto randseed = 1; // use DST? + while (priority > 0) { + if (out_desc_[priority].size() > 0) { + break; + } + priority--; + } + if (out_desc_[priority].size() == 0) { + VLOG(ADNL_WARNING) << this << ": dropping OUT message [" << src_id << "->" << dst_id << "]: no out desc"; + return; + } + + auto &dv = out_desc_[priority]; + auto &v = dv[randseed % dv.size()]; + + if (!v.is_proxy()) { + auto it = udp_servers_.find(static_cast(v.addr.get_port())); + CHECK(it != udp_servers_.end()); + + td::UdpMessage M; + M.address = dst_addr; + M.data = std::move(data); + + CHECK(M.data.size() <= get_mtu()); + + td::actor::send_closure(it->second, &td::UdpServer::send, std::move(M)); + } else { + auto it = udp_servers_.find(out_udp_port_); + CHECK(it != udp_servers_.end()); + + auto enc = v.proxy->encrypt( + AdnlProxy::Packet{dst_addr.get_ipv4(), static_cast(dst_addr.get_port()), std::move(data)}); + + td::UdpMessage M; + M.address = v.addr; + M.data = std::move(enc); + + td::actor::send_closure(it->second, &td::UdpServer::send, std::move(M)); + } +} + +} // namespace adnl + +} // namespace ton diff --git a/adnl/adnl-network-manager.h b/adnl/adnl-network-manager.h new file mode 100644 index 00000000..c090aa2a --- /dev/null +++ b/adnl/adnl-network-manager.h @@ -0,0 +1,107 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +#include "td/actor/actor.h" + +#include "td/actor/PromiseFuture.h" +#include "td/utils/port/IPAddress.h" + +#include "adnl-node-id.hpp" +#include "adnl-proxy-types.h" + +namespace td { +class UdpServer; +} + +namespace ton { + +namespace adnl { + +class AdnlPeerTable; + +class AdnlNetworkConnection : public td::actor::Actor { + public: + class Callback { + public: + virtual void on_change_state(bool ready) = 0; + virtual ~Callback() = default; + }; + virtual void send(AdnlNodeIdShort src, AdnlNodeIdShort dst, td::uint32 priority, td::BufferSlice message) = 0; + virtual bool is_alive() const = 0; + virtual bool is_active() const = 0; + virtual ~AdnlNetworkConnection() = default; +}; + +class AdnlNetworkManager : public td::actor::Actor { + public: + //using ConnHandle = td::uint64; + class Callback { + public: + virtual ~Callback() = default; + //virtual void receive_packet(td::IPAddress addr, ConnHandle conn_handle, td::BufferSlice data) = 0; + virtual void receive_packet(td::IPAddress addr, td::BufferSlice data) = 0; + }; + static td::actor::ActorOwn create(td::uint16 out_port); + + virtual ~AdnlNetworkManager() = default; + + virtual void install_callback(std::unique_ptr callback) = 0; + + virtual void add_self_addr(td::IPAddress addr, td::uint32 priority) = 0; + virtual void add_proxy_addr(td::IPAddress addr, std::shared_ptr proxy, td::uint32 priority) = 0; + virtual void send_udp_packet(AdnlNodeIdShort src_id, AdnlNodeIdShort dst_id, td::IPAddress dst_addr, + td::uint32 priority, td::BufferSlice data) = 0; + //virtual void send_tcp_packet(AdnlNodeIdShort src_id, AdnlNodeIdShort dst_id, td::IPAddress dst_addr, + // td::uint32 priority, td::BufferSlice data) = 0; + //virtual void send_answer_packet(AdnlNodeIdShort src_id, AdnlNodeIdShort dst_id, td::IPAddress dst_addr, + // ConnHandle conn_handle, td::uint32 priority, td::BufferSlice data) = 0; + + static constexpr td::uint32 get_mtu() { + return 1440; + } + + struct PrintId {}; + PrintId print_id() const { + return PrintId{}; + } +}; + +} // namespace adnl + +} // namespace ton + +namespace td { + +inline td::StringBuilder &operator<<(td::StringBuilder &sb, const ton::adnl::AdnlNetworkManager::PrintId &id) { + sb << "[networkmanager]"; + return sb; +} + +inline td::StringBuilder &operator<<(td::StringBuilder &sb, const ton::adnl::AdnlNetworkManager &manager) { + sb << manager.print_id(); + return sb; +} + +inline td::StringBuilder &operator<<(td::StringBuilder &sb, const ton::adnl::AdnlNetworkManager *manager) { + sb << manager->print_id(); + return sb; +} + +} // namespace td diff --git a/adnl/adnl-network-manager.hpp b/adnl/adnl-network-manager.hpp new file mode 100644 index 00000000..818a322f --- /dev/null +++ b/adnl/adnl-network-manager.hpp @@ -0,0 +1,121 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +#include "td/actor/actor.h" +#include "td/utils/BufferedUdp.h" +#include "td/net/UdpServer.h" +#include "td/net/TcpListener.h" + +#include "td/actor/PromiseFuture.h" +#include "adnl-network-manager.h" + +#include + +namespace td { +class UdpServer; +} + +namespace ton { + +namespace adnl { + +class AdnlPeerTable; + +class AdnlNetworkManagerImpl : public AdnlNetworkManager { + public: + struct OutDesc { + td::IPAddress addr; + std::shared_ptr proxy; + + bool is_proxy() const { + return proxy != nullptr; + } + bool operator==(const OutDesc &with) const { + return addr == with.addr && is_proxy() == with.is_proxy(); + } + }; + + AdnlNetworkManagerImpl(td::uint16 out_udp_port) : out_udp_port_(out_udp_port) { + } + + void install_callback(std::unique_ptr callback) override { + callback_ = std::move(callback); + } + + void add_self_addr(td::IPAddress addr, td::uint32 priority) override { + auto x = OutDesc{addr, nullptr}; + auto &v = out_desc_[priority]; + for (auto &y : v) { + if (x == y) { + return; + } + } + out_desc_[priority].push_back(std::move(x)); + add_listening_udp_port(static_cast(addr.get_port())); + } + void add_proxy_addr(td::IPAddress addr, std::shared_ptr proxy, td::uint32 priority) override { + auto x = OutDesc{addr, std::move(proxy)}; + auto &v = out_desc_[priority]; + for (auto &y : v) { + if (x == y) { + return; + } + } + out_desc_[priority].push_back(std::move(x)); + if (!udp_servers_.count(out_udp_port_)) { + add_listening_udp_port(out_udp_port_); + } + } + void send_udp_packet(AdnlNodeIdShort src_id, AdnlNodeIdShort dst_id, td::IPAddress dst_addr, td::uint32 priority, + td::BufferSlice data) override; + + void add_listening_udp_port(td::uint16 port); + void receive_udp_message(td::UdpMessage message); + + private: + std::unique_ptr callback_; + + std::map> out_desc_; + + td::uint64 received_messages_ = 0; + td::uint64 sent_messages_ = 0; + + std::map> udp_servers_; + + td::uint16 out_udp_port_; +}; + +} // namespace adnl + +} // namespace ton + +namespace td { + +inline td::StringBuilder &operator<<(td::StringBuilder &sb, const ton::adnl::AdnlNetworkManagerImpl &manager) { + sb << manager.print_id(); + return sb; +} + +inline td::StringBuilder &operator<<(td::StringBuilder &sb, const ton::adnl::AdnlNetworkManagerImpl *manager) { + sb << manager->print_id(); + return sb; +} + +} // namespace td diff --git a/adnl/adnl-node-id.hpp b/adnl/adnl-node-id.hpp new file mode 100644 index 00000000..84f425f8 --- /dev/null +++ b/adnl/adnl-node-id.hpp @@ -0,0 +1,130 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +#include "keys/keys.hpp" +#include "common/io.hpp" + +namespace ton { + +namespace adnl { + +class AdnlNodeIdShort { + public: + explicit AdnlNodeIdShort(const PublicKeyHash &hash) : hash_(hash) { + } + explicit AdnlNodeIdShort(PublicKeyHash &&hash) : hash_(std::move(hash)) { + } + AdnlNodeIdShort() { + } + explicit AdnlNodeIdShort(td::Slice data) : hash_(data) { + } + explicit AdnlNodeIdShort(td::Bits256 value) : hash_(value) { + } + explicit AdnlNodeIdShort(tl_object_ptr obj) : hash_(obj->id_) { + } + + const auto &pubkey_hash() const { + return hash_; + } + + bool operator==(const AdnlNodeIdShort &with) const { + return hash_ == with.hash_; + } + bool operator!=(const AdnlNodeIdShort &with) const { + return hash_ != with.hash_; + } + bool operator<(const AdnlNodeIdShort &with) const { + return hash_ < with.hash_; + } + tl_object_ptr tl() const { + return create_tl_object(hash_.tl()); + } + auto as_slice() { + return hash_.as_slice(); + } + auto as_slice() const { + return hash_.as_slice(); + } + auto uint256_value() const { + return hash_.uint256_value(); + } + auto bits256_value() const { + return hash_.bits256_value(); + } + static AdnlNodeIdShort zero() { + return AdnlNodeIdShort{PublicKeyHash::zero()}; + } + bool is_zero() const { + return hash_.is_zero(); + } + + private: + PublicKeyHash hash_; +}; + +class AdnlNodeIdFull { + private: + explicit AdnlNodeIdFull(const tl_object_ptr &pub) : pub_(pub) { + } + + public: + explicit AdnlNodeIdFull(const PublicKey &pub) : pub_(pub) { + } + explicit AdnlNodeIdFull(PublicKey &&pub) : pub_(std::move(pub)) { + } + static td::Result create(const tl_object_ptr &pub) { + return AdnlNodeIdFull{pub}; + } + AdnlNodeIdFull() { + } + const auto &pubkey() const { + return pub_; + } + bool empty() const { + return pub_.empty(); + } + bool operator==(const AdnlNodeIdFull &with) const { + return pub_ == with.pub_; + } + bool operator!=(const AdnlNodeIdFull &with) const { + return pub_ != with.pub_; + } + auto tl() const { + return pub_.tl(); + } + AdnlNodeIdShort compute_short_id() const { + return AdnlNodeIdShort{pub_.compute_short_id()}; + } + + private: + PublicKey pub_; +}; + +} // namespace adnl + +} // namespace ton + +namespace td { + +inline StringBuilder &operator<<(StringBuilder &stream, const ton::adnl::AdnlNodeIdShort &value) { + return stream << value.bits256_value(); +} + +} // namespace td diff --git a/adnl/adnl-node.cpp b/adnl/adnl-node.cpp new file mode 100644 index 00000000..fe14f0ed --- /dev/null +++ b/adnl/adnl-node.cpp @@ -0,0 +1,50 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "adnl-node.h" + +namespace ton { + +namespace adnl { + +td::Result AdnlNode::create(const tl_object_ptr &obj) { + TRY_RESULT(id, AdnlNodeIdFull::create(obj->id_)); + TRY_RESULT(addr_list, AdnlAddressList::create(std::move(obj->addr_list_))); + return AdnlNode{std::move(id), std::move(addr_list)}; +} + +tl_object_ptr AdnlNodesList::tl() const { + std::vector> vec; + for (auto &node : nodes_) { + vec.emplace_back(node.tl()); + } + return create_tl_object(std::move(vec)); +} + +td::Result AdnlNodesList::create(const tl_object_ptr &nodes) { + AdnlNodesList res{}; + for (auto &node : nodes->nodes_) { + TRY_RESULT(N, AdnlNode::create(node)); + res.push(std::move(N)); + } + return res; +} + +} // namespace adnl + +} // namespace ton diff --git a/adnl/adnl-node.h b/adnl/adnl-node.h new file mode 100644 index 00000000..509e64f5 --- /dev/null +++ b/adnl/adnl-node.h @@ -0,0 +1,74 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +#include "auto/tl/ton_api.h" +#include "adnl-node-id.hpp" +#include "adnl-address-list.h" + +namespace ton { + +namespace adnl { + +class AdnlNode { + private: + AdnlNodeIdFull pub_; + AdnlAddressList addr_list_; + + public: + AdnlNode(AdnlNodeIdFull pub, AdnlAddressList addr_list) : pub_(std::move(pub)), addr_list_(std::move(addr_list)) { + } + AdnlNode(const AdnlNode& from) : pub_(from.pub_), addr_list_(from.addr_list_) { + } + static td::Result create(const tl_object_ptr& obj); + + tl_object_ptr tl() const { + return create_tl_object(pub_.tl(), addr_list_.tl()); + } + AdnlNodeIdFull pub_id() const { + return pub_; + } + AdnlNodeIdShort compute_short_id() const { + return pub_.compute_short_id(); + } + const AdnlAddressList& addr_list() const { + return addr_list_; + } +}; + +class AdnlNodesList { + private: + std::vector nodes_; + + public: + const auto& nodes() const { + return nodes_; + } + AdnlNodesList() { + } + void push(AdnlNode node) { + nodes_.push_back(std::move(node)); + } + tl_object_ptr tl() const; + static td::Result create(const tl_object_ptr& nodes); +}; + +} // namespace adnl + +} // namespace ton diff --git a/adnl/adnl-packet.cpp b/adnl/adnl-packet.cpp new file mode 100644 index 00000000..6043d287 --- /dev/null +++ b/adnl/adnl-packet.cpp @@ -0,0 +1,135 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "adnl-packet.h" +#include "td/utils/Random.h" + +namespace ton { + +namespace adnl { + +/*adnl.packetContents rand1:bytes flags:# from:flags.0?PublicKey from_short:flags.1?adnl.id.short + message:flags.2?adnl.Message messages:flags.3?(vector adnl.Message) + address:flags.6?adnl.addressList seqno:flags.8?long recv_addr_list_version:flags.9?int + confirm_seqno:flags.10?long reinit_date:flags.11?int dst_reinit_date:flags.11?int + signature:flags.7?bytes rand2:bytes = adnl.PacketContents;*/ + +td::Result AdnlPacket::create(tl_object_ptr packet) { + AdnlPacket R; + R.rand1_ = std::move(packet->rand1_); + R.flags_ = packet->flags_; + if (R.flags_ & Flags::f_from) { + TRY_RESULT(F, AdnlNodeIdFull::create(packet->from_)); + R.from_ = std::move(F); + } + if (R.flags_ & Flags::f_from_short) { + R.from_short_ = AdnlNodeIdShort{packet->from_short_->id_}; + } else if (packet->flags_ & Flags::f_from) { + R.from_short_ = R.from_.compute_short_id(); + } + if (R.flags_ & Flags::f_one_message) { + R.messages_ = AdnlMessageList{std::move(packet->message_)}; + } + if (R.flags_ & Flags::f_mult_messages) { + // may override messages_ if (flags & 0x4) + // but this message will fail in run_basic_checks() + // so it doesn't matter + R.messages_ = AdnlMessageList{std::move(packet->messages_)}; + } + if (R.flags_ & Flags::f_address) { + TRY_RESULT(addr_list, AdnlAddressList::create(std::move(packet->address_))); + R.addr_ = std::move(addr_list); + } + if (R.flags_ & Flags::f_priority_address) { + TRY_RESULT(addr_list, AdnlAddressList::create(std::move(packet->address_))); + R.priority_addr_ = std::move(addr_list); + } + if (R.flags_ & Flags::f_seqno) { + R.seqno_ = packet->seqno_; + } + if (R.flags_ & Flags::f_confirm_seqno) { + R.confirm_seqno_ = packet->confirm_seqno_; + } + if (R.flags_ & Flags::f_recv_addr_version) { + R.recv_addr_list_version_ = packet->recv_addr_list_version_; + } + if (R.flags_ & Flags::f_recv_priority_addr_version) { + R.recv_priority_addr_list_version_ = packet->recv_priority_addr_list_version_; + } + if (R.flags_ & Flags::f_reinit_date) { + R.reinit_date_ = packet->reinit_date_; + R.dst_reinit_date_ = packet->dst_reinit_date_; + } + if (R.flags_ & Flags::f_signature) { + R.signature_ = std::move(packet->signature_); + } + R.rand2_ = std::move(packet->rand2_); + + TRY_STATUS(R.run_basic_checks()); + return std::move(R); +} + +td::Status AdnlPacket::run_basic_checks() const { + if ((flags_ & Flags::f_all) != flags_) { + return td::Status::Error(ErrorCode::protoviolation, "bad flags"); + } + if ((flags_ & Flags::f_one_message) && (flags_ & Flags::f_mult_messages)) { + return td::Status::Error(ErrorCode::protoviolation, "both flags 0x4 and 0x8 set"); + } + if ((flags_ & Flags::f_from) && (flags_ & Flags::f_from_short) && from_.compute_short_id() != from_short_) { + return td::Status::Error(ErrorCode::protoviolation, "source and short source mismatch"); + } + if ((flags_ & Flags::f_address) && addr_.empty()) { + return td::Status::Error(ErrorCode::protoviolation, "bad addr list"); + } + if ((flags_ & Flags::f_priority_address) && priority_addr_.empty()) { + return td::Status::Error(ErrorCode::protoviolation, "bad addr list"); + } + return td::Status::OK(); +} + +tl_object_ptr AdnlPacket::tl() const { + return create_tl_object( + rand1_.clone(), flags_ & ~Flags::f_priority, (flags_ & Flags::f_from) ? from_.tl() : nullptr, + (flags_ & Flags::f_from_short) ? from_short_.tl() : nullptr, + (flags_ & Flags::f_one_message) ? messages_.one_message() : nullptr, + (flags_ & Flags::f_mult_messages) ? messages_.mult_messages() : messages_.empty_vector(), + (flags_ & Flags::f_address) ? addr_.tl() : nullptr, + (flags_ & Flags::f_priority_address) ? priority_addr_.tl() : nullptr, seqno_, confirm_seqno_, + recv_addr_list_version_, recv_priority_addr_list_version_, reinit_date_, dst_reinit_date_, signature_.clone(), + rand2_.clone()); +} + +td::BufferSlice AdnlPacket::to_sign() const { + auto obj = tl(); + obj->signature_.clear(); + obj->flags_ &= ~Flags::f_signature; + CHECK(obj->signature_.size() == 0); + return serialize_tl_object(obj, true); +} + +void AdnlPacket::init_random() { + rand1_ = td::BufferSlice{(td::Random::fast_uint32() & 1) ? 7u : 15u}; + rand2_ = td::BufferSlice{(td::Random::fast_uint32() & 1) ? 7u : 15u}; + td::Random::secure_bytes(rand1_.as_slice()); + td::Random::secure_bytes(rand2_.as_slice()); +} + +} // namespace adnl + +} // namespace ton diff --git a/adnl/adnl-packet.h b/adnl/adnl-packet.h new file mode 100644 index 00000000..6459e06b --- /dev/null +++ b/adnl/adnl-packet.h @@ -0,0 +1,211 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +#include "adnl/adnl.h" +#include "adnl/adnl-message.h" + +namespace ton { + +namespace adnl { + +/* + from:flags.0?PublicKey + from_short:flags.1?adnl.id.short + message:flags.2?adnl.Message + messages:flags.3?(vector adnl.Message) + address:flags.4?adnl.addressList + priority_address:flags.5?adnl.addressList + seqno:flags.6?long + confirm_seqno:flags.7?long + recv_addr_list_version:flags.8?int + recv_priority_addr_list_version:flags.9?int + reinit_date:flags.10?int + dst_reinit_date:flags.10?int + signature:flags.11?bytes + */ + +// total packet length: +// for full packet: +// 32 (dst) + 64 (encryption overhead) + 4 (magic) + 36 (pubkey) + 4 + M (sum of messages) + +// + A1 + A2 + 8 + 8 + 4 + 4 + 4 + 4 + 68 (signature) + 16 (r1) + 16 (r2) = +// = 272 + M + A1 + A2 +// for channel: +// 32 (channel id) + 32 (encryption overhead) + 4 (magic) + 4 + M (sum of messages) + +// + A1 + A2 + 8 + 8 + 4 + 4 + 16(r1) + 16(r2) = 128 + M + A1 + A2 + +class AdnlPacket { + private: + enum Flags : td::uint32 { + f_from = 0x1, + f_from_short = 0x2, + f_one_message = 0x4, + f_mult_messages = 0x8, + f_address = 0x10, + f_priority_address = 0x20, + f_seqno = 0x40, + f_confirm_seqno = 0x80, + f_recv_addr_version = 0x100, + f_recv_priority_addr_version = 0x200, + f_reinit_date = 0x400, + f_signature = 0x800, + f_priority = 0x1000, + f_all = 0x1fff + }; + + public: + AdnlPacket() { + } + static td::Result create(tl_object_ptr packet); + tl_object_ptr tl() const; + td::BufferSlice to_sign() const; + + td::Status run_basic_checks() const; + + auto flags() const { + return flags_; + } + bool priority() const { + return flags_ & f_priority; + } + bool inited_from_short() const { + return flags_ & (Flags::f_from | Flags::f_from_short); + } + bool inited_from() const { + return flags_ & Flags::f_from; + } + auto from() const { + return from_; + } + auto from_short() const { + return from_short_; + } + const auto &messages() const { + return messages_; + } + auto &messages() { + return messages_; + } + bool inited_addr_list() const { + return flags_ & Flags::f_address; + } + auto addr_list() const { + return addr_; + } + auto priority_addr_list() const { + return priority_addr_; + } + auto seqno() const { + return seqno_; + } + auto confirm_seqno() const { + return confirm_seqno_; + } + auto recv_addr_list_version() const { + return recv_addr_list_version_; + } + auto recv_priority_addr_list_version() const { + return recv_priority_addr_list_version_; + } + auto reinit_date() const { + return reinit_date_; + } + auto dst_reinit_date() const { + return dst_reinit_date_; + } + auto signature() const { + return signature_.clone(); + } + + void init_random(); + + void set_signature(td::BufferSlice signature) { + signature_ = std::move(signature); + flags_ |= Flags::f_signature; + } + void set_source(AdnlNodeIdFull src) { + from_ = src; + from_short_ = src.compute_short_id(); + flags_ = (flags_ | Flags::f_from) & ~Flags::f_from_short; + } + void set_source(AdnlNodeIdShort src) { + if (!(flags_ & Flags::f_from)) { + from_short_ = src; + flags_ |= Flags::f_from_short; + } + } + void add_message(AdnlMessage message) { + messages_.push_back(std::move(message)); + if (messages_.size() == 1) { + flags_ = (flags_ | Flags::f_one_message) & ~Flags::f_mult_messages; + } else { + flags_ = (flags_ | Flags::f_mult_messages) & ~Flags::f_one_message; + } + } + void set_addr_list(AdnlAddressList addr_list) { + addr_ = std::move(addr_list); + flags_ |= Flags::f_address; + } + void set_priority_addr_list(AdnlAddressList addr_list) { + priority_addr_ = std::move(addr_list); + flags_ |= Flags::f_priority_address; + } + void set_seqno(td::uint64 seqno) { + seqno_ = seqno; + flags_ |= Flags::f_seqno; + } + void set_confirm_seqno(td::uint64 seqno) { + confirm_seqno_ = seqno; + flags_ |= Flags::f_confirm_seqno; + } + void set_received_addr_list_version(td::int32 version) { + recv_addr_list_version_ = version; + flags_ |= Flags::f_recv_addr_version; + } + void set_received_priority_addr_list_version(td::int32 version) { + recv_priority_addr_list_version_ = version; + flags_ |= Flags::f_recv_priority_addr_version; + } + void set_reinit_date(td::int32 date, td::int32 dst_reinit_date) { + reinit_date_ = date; + dst_reinit_date_ = dst_reinit_date; + flags_ |= Flags::f_reinit_date; + } + + private: + td::BufferSlice rand1_; + td::uint32 flags_{0}; + AdnlNodeIdFull from_; + AdnlNodeIdShort from_short_; + AdnlMessageList messages_; + AdnlAddressList addr_; + AdnlAddressList priority_addr_; + td::uint64 seqno_{0}; + td::uint64 confirm_seqno_{0}; + td::int32 recv_addr_list_version_{0}; + td::int32 recv_priority_addr_list_version_{0}; + td::int32 reinit_date_{0}; + td::int32 dst_reinit_date_{0}; + td::BufferSlice signature_; + td::BufferSlice rand2_; +}; + +} // namespace adnl + +} // namespace ton diff --git a/adnl/adnl-peer-table.cpp b/adnl/adnl-peer-table.cpp new file mode 100644 index 00000000..c9a11391 --- /dev/null +++ b/adnl/adnl-peer-table.cpp @@ -0,0 +1,336 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "adnl-peer-table.hpp" +#include "adnl-peer.h" +#include "adnl-channel.h" +#include "utils.hpp" + +#include "td/utils/tl_storers.h" +#include "td/utils/crypto.h" +#include "td/utils/tl_parsers.h" +#include "td/utils/Random.h" +#include "td/db/RocksDb.h" + +#include "utils.hpp" +#include "adnl-query.h" +#include "adnl-ext-client.h" + +namespace ton { + +namespace adnl { + +td::int32 Adnl::adnl_start_time() { + static td::int32 start_time = [] { + auto init_start_time = static_cast(td::Clocks::system()); + CHECK(init_start_time > 0); + return init_start_time; + }(); + return start_time; +} + +td::actor::ActorOwn Adnl::create(std::string db, td::actor::ActorId keyring) { + adnl_start_time(); + return td::actor::ActorOwn(td::actor::create_actor("PeerTable", db, keyring)); +} + +void AdnlPeerTableImpl::receive_packet(td::BufferSlice data) { + if (data.size() < 32) { + VLOG(ADNL_WARNING) << this << ": dropping IN message [?->?]: message too short: len=" << data.size(); + return; + } + + AdnlNodeIdShort dst{data.as_slice().truncate(32)}; + data.confirm_read(32); + + auto it = local_ids_own_.find(dst); + if (it != local_ids_own_.end()) { + td::actor::send_closure(it->second, &AdnlLocalId::receive, std::move(data)); + return; + } + + AdnlChannelIdShort dst_chan_id{dst.pubkey_hash()}; + auto it2 = channels_.find(dst_chan_id); + if (it2 != channels_.end()) { + td::actor::send_closure(it2->second, &AdnlChannel::receive, std::move(data)); + return; + } + + VLOG(ADNL_DEBUG) << this << ": dropping IN message [?->" << dst << "]: unknown dst " << dst + << " (len=" << (data.size() + 32) << ")"; +} + +void AdnlPeerTableImpl::receive_decrypted_packet(AdnlNodeIdShort dst, AdnlPacket packet) { + packet.run_basic_checks().ensure(); + + if (!packet.inited_from_short()) { + VLOG(ADNL_INFO) << this << ": dropping IN message [?->" << dst << "]: destination not set"; + return; + } + + auto it = peers_.find(packet.from_short()); + if (it == peers_.end()) { + if (!packet.inited_from()) { + VLOG(ADNL_NOTICE) << this << ": dropping IN message [" << packet.from_short() << "->" << dst + << "]: unknown peer and no full src in packet"; + return; + } + if (network_manager_.empty()) { + VLOG(ADNL_NOTICE) << this << ": dropping IN message [" << packet.from_short() << "->" << dst + << "]: unknown peer and network manager uninitialized"; + return; + } + + it = peers_ + .emplace(packet.from_short(), + AdnlPeer::create(network_manager_, actor_id(this), dht_node_, packet.from_short())) + .first; + CHECK(it != peers_.end()); + } + + auto it2 = local_ids_own_.find(dst); + if (it2 == local_ids_own_.end()) { + VLOG(ADNL_ERROR) << this << ": dropping IN message [" << packet.from_short() << "->" << dst + << "]: unknown dst (but how did we decrypt message?)"; + return; + } + td::actor::send_closure(it->second, &AdnlPeer::receive_packet, dst, it2->second.get(), std::move(packet)); +} + +void AdnlPeerTableImpl::add_peer(AdnlNodeIdShort local_id, AdnlNodeIdFull id, AdnlAddressList addr_list) { + auto id_short = id.compute_short_id(); + VLOG(ADNL_DEBUG) << this << ": adding peer " << id_short << " for local id " << local_id; + + auto it2 = local_ids_own_.find(local_id); + CHECK(it2 != local_ids_own_.end()); + + auto it = peers_.find(id_short); + if (it == peers_.end()) { + it = peers_.emplace(id_short, AdnlPeer::create(network_manager_, actor_id(this), dht_node_, id_short)).first; + CHECK(it != peers_.end()); + } + td::actor::send_closure(it->second, &AdnlPeer::update_id, std::move(id)); + if (!addr_list.empty()) { + td::actor::send_closure(it->second, &AdnlPeer::update_addr_list, local_id, it2->second.get(), std::move(addr_list)); + } +} + +void AdnlPeerTableImpl::add_static_nodes_from_config(AdnlNodesList nodes) { + for (auto &it : nodes.nodes()) { + add_static_node(it); + } +} + +void AdnlPeerTableImpl::send_message_in(AdnlNodeIdShort src, AdnlNodeIdShort dst, AdnlMessage message) { + auto it = peers_.find(dst); + + if (it == peers_.end()) { + it = peers_.emplace(dst, AdnlPeer::create(network_manager_, actor_id(this), dht_node_, dst)).first; + } + + auto it2 = local_ids_own_.find(src); + if (it2 == local_ids_own_.end()) { + LOG(ERROR) << this << ": dropping OUT message [" << src << "->" << dst << "]: unknown src"; + return; + } + + td::actor::send_closure(it->second, &AdnlPeer::send_one_message, src, it2->second.get(), std::move(message)); +} + +void AdnlPeerTableImpl::answer_query(AdnlNodeIdShort src, AdnlNodeIdShort dst, AdnlQueryId query_id, + td::BufferSlice data) { + if (data.size() > get_mtu()) { + LOG(ERROR) << this << ": dropping OUT message [" << src << "->" << dst + << "]: message too big: size=" << data.size(); + return; + } + send_message_in(src, dst, adnlmessage::AdnlMessageAnswer{query_id, std::move(data)}); +} + +void AdnlPeerTableImpl::send_query(AdnlNodeIdShort src, AdnlNodeIdShort dst, std::string name, + td::Promise promise, td::Timestamp timeout, td::BufferSlice data) { + if (data.size() > huge_packet_max_size()) { + VLOG(ADNL_WARNING) << "dropping too big packet [" << src << "->" << dst << "]: size=" << data.size(); + VLOG(ADNL_WARNING) << "DUMP: " << td::buffer_to_hex(data.as_slice().truncate(128)); + return; + } + auto it = peers_.find(dst); + + if (it == peers_.end()) { + it = peers_.emplace(dst, AdnlPeer::create(network_manager_, actor_id(this), dht_node_, dst)).first; + } + + auto it2 = local_ids_own_.find(src); + if (it2 == local_ids_own_.end()) { + LOG(ERROR) << this << ": dropping OUT message [" << src << "->" << dst << "]: unknown src"; + return; + } + + td::actor::send_closure(it->second, &AdnlPeer::send_query, src, it2->second.get(), name, std::move(promise), timeout, + std::move(data)); +} + +void AdnlPeerTableImpl::add_id(AdnlNodeIdFull id, AdnlAddressList addr_list) { + auto a = id.compute_short_id(); + VLOG(ADNL_INFO) << "adnl: adding local id " << a; + + auto it = local_ids_own_.find(a); + + if (it != local_ids_own_.end()) { + td::actor::send_closure(it->second, &AdnlLocalId::update_address_list, std::move(addr_list)); + } else { + local_ids_own_[a] = td::actor::create_actor("localid", std::move(id), std::move(addr_list), + actor_id(this), keyring_, dht_node_); + } +} + +void AdnlPeerTableImpl::del_id(AdnlNodeIdShort id, td::Promise promise) { + VLOG(ADNL_INFO) << "adnl: deleting local id " << id; + local_ids_own_.erase(id); + promise.set_value(td::Unit()); +} + +void AdnlPeerTableImpl::subscribe(AdnlNodeIdShort dst, std::string prefix, std::unique_ptr callback) { + auto it = local_ids_own_.find(dst); + LOG_CHECK(it != local_ids_own_.end()) << "dst=" << dst; + + td::actor::send_closure(it->second, &AdnlLocalId::subscribe, prefix, std::move(callback)); +} + +void AdnlPeerTableImpl::unsubscribe(AdnlNodeIdShort dst, std::string prefix) { + auto it = local_ids_own_.find(dst); + + if (it != local_ids_own_.end()) { + td::actor::send_closure(it->second, &AdnlLocalId::unsubscribe, prefix); + } +} + +void AdnlPeerTableImpl::register_dht_node(td::actor::ActorId dht_node) { + dht_node_ = dht_node; + + for (auto it = peers_.begin(); it != peers_.end(); it++) { + td::actor::send_closure(it->second, &AdnlPeer::update_dht_node, dht_node_); + } + for (auto it = local_ids_own_.begin(); it != local_ids_own_.end(); it++) { + td::actor::send_closure(it->second, &AdnlLocalId::update_dht_node, dht_node_); + } +} + +void AdnlPeerTableImpl::register_network_manager(td::actor::ActorId network_manager) { + network_manager_ = std::move(network_manager); + + class Cb : public AdnlNetworkManager::Callback { + public: + void receive_packet(td::IPAddress addr, td::BufferSlice data) override { + td::actor::send_closure(id_, &AdnlPeerTableImpl::receive_packet, std::move(data)); + } + Cb(td::actor::ActorId id) : id_(id) { + } + + private: + td::actor::ActorId id_; + }; + + auto cb = std::make_unique(actor_id(this)); + td::actor::send_closure(network_manager_, &AdnlNetworkManager::install_callback, std::move(cb)); +} + +void AdnlPeerTableImpl::get_addr_list(AdnlNodeIdShort id, td::Promise promise) { + auto it = local_ids_own_.find(id); + if (it == local_ids_own_.end()) { + promise.set_error(td::Status::Error(ErrorCode::notready)); + return; + } + td::actor::send_closure(it->second, &AdnlLocalId::get_addr_list_async, std::move(promise)); +} + +void AdnlPeerTableImpl::get_self_node(AdnlNodeIdShort id, td::Promise promise) { + auto it = local_ids_own_.find(id); + if (it == local_ids_own_.end()) { + promise.set_error(td::Status::Error(ErrorCode::notready)); + return; + } + td::actor::send_closure(it->second, &AdnlLocalId::get_self_node, std::move(promise)); +} + +void AdnlPeerTableImpl::register_channel(AdnlChannelIdShort id, td::actor::ActorId channel) { + auto success = channels_.emplace(id, channel).second; + CHECK(success); +} + +void AdnlPeerTableImpl::unregister_channel(AdnlChannelIdShort id) { + auto erased = channels_.erase(id); + CHECK(erased == 1); +} + +void AdnlPeerTableImpl::start_up() { +} + +void AdnlPeerTableImpl::write_new_addr_list_to_db(AdnlNodeIdShort local_id, AdnlNodeIdShort peer_id, AdnlDbItem node, + td::Promise promise) { + td::actor::send_closure(db_, &AdnlDb::update, local_id, peer_id, std::move(node), std::move(promise)); +} + +void AdnlPeerTableImpl::get_addr_list_from_db(AdnlNodeIdShort local_id, AdnlNodeIdShort peer_id, + td::Promise promise) { + td::actor::send_closure(db_, &AdnlDb::get, local_id, peer_id, std::move(promise)); +} + +AdnlPeerTableImpl::AdnlPeerTableImpl(std::string db_root, td::actor::ActorId keyring) { + keyring_ = keyring; + static_nodes_manager_ = AdnlStaticNodesManager::create(); + + db_ = AdnlDb::create(db_root + "/adnl"); +} + +void AdnlPeerTableImpl::deliver(AdnlNodeIdShort src, AdnlNodeIdShort dst, td::BufferSlice data) { + auto it = local_ids_own_.find(dst); + if (it != local_ids_own_.end()) { + td::actor::send_closure(it->second, &AdnlLocalId::deliver, src, std::move(data)); + } +} +void AdnlPeerTableImpl::deliver_query(AdnlNodeIdShort src, AdnlNodeIdShort dst, td::BufferSlice data, + td::Promise promise) { + auto it = local_ids_own_.find(dst); + if (it != local_ids_own_.end()) { + td::actor::send_closure(it->second, &AdnlLocalId::deliver_query, src, std::move(data), std::move(promise)); + } else { + LOG(WARNING) << "deliver query: unknown dst " << dst; + promise.set_error(td::Status::Error(ErrorCode::notready, "cannot deliver: unknown DST")); + } +} + +void AdnlPeerTableImpl::decrypt_message(AdnlNodeIdShort dst, td::BufferSlice data, + td::Promise promise) { + auto it = local_ids_own_.find(dst); + if (it != local_ids_own_.end()) { + td::actor::send_closure(it->second, &AdnlLocalId::decrypt_message, std::move(data), std::move(promise)); + } else { + LOG(WARNING) << "decrypt message: unknown dst " << dst; + promise.set_error(td::Status::Error(ErrorCode::notready, "cannot decrypt: unknown DST")); + } +} + +void AdnlPeerTableImpl::create_ext_server(std::vector ids, std::vector ports, + td::Promise> promise) { + promise.set_value(AdnlExtServerCreator::create(actor_id(this), std::move(ids), std::move(ports))); +} + +} // namespace adnl + +} // namespace ton diff --git a/adnl/adnl-peer-table.h b/adnl/adnl-peer-table.h new file mode 100644 index 00000000..1050222c --- /dev/null +++ b/adnl/adnl-peer-table.h @@ -0,0 +1,124 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +#include "td/actor/actor.h" +#include "td/utils/BufferedUdp.h" + +#include "adnl.h" +#include "utils.hpp" +#include "adnl/adnl-query.h" +#include "adnl/adnl-db.h" +#include "common/io.hpp" + +#include "adnl-packet.h" + +#include "auto/tl/ton_api.h" + +namespace ton { + +namespace adnl { + +constexpr int VERBOSITY_NAME(ADNL_ERROR) = verbosity_WARNING; +constexpr int VERBOSITY_NAME(ADNL_WARNING) = verbosity_INFO; +constexpr int VERBOSITY_NAME(ADNL_NOTICE) = verbosity_DEBUG; +constexpr int VERBOSITY_NAME(ADNL_INFO) = verbosity_DEBUG; +constexpr int VERBOSITY_NAME(ADNL_DEBUG) = verbosity_DEBUG + 1; +constexpr int VERBOSITY_NAME(ADNL_EXTRA_DEBUG) = verbosity_DEBUG + 10; + +class AdnlChannelIdShortImpl { + public: + explicit AdnlChannelIdShortImpl(PublicKeyHash value) { + value_ = value.bits256_value(); + } + explicit AdnlChannelIdShortImpl(td::Bits256 value) { + value_ = value; + } + AdnlChannelIdShortImpl() { + } + td::Bits256 bits256_value() const { + return value_; + } + auto tl() const { + return value_; + } + + bool operator<(const AdnlChannelIdShortImpl &with) const { + return value_ < with.value_; + } + bool operator==(const AdnlChannelIdShortImpl &with) const { + return value_ == with.value_; + } + bool operator!=(const AdnlChannelIdShortImpl &with) const { + return value_ != with.value_; + } + td::Slice as_slice() const { + return td::as_slice(value_); + } + + private: + td::Bits256 value_; +}; + +using AdnlChannelIdShort = AdnlChannelIdShortImpl; + +class AdnlLocalId; +class AdnlChannel; + +class AdnlPeerTable : public Adnl { + public: + static constexpr double republish_addr_list_timeout() { + return 60.0; + } + + virtual void answer_query(AdnlNodeIdShort src, AdnlNodeIdShort dst, AdnlQueryId query_id, td::BufferSlice data) = 0; + + virtual void receive_packet(td::BufferSlice data) = 0; + virtual void receive_decrypted_packet(AdnlNodeIdShort dst, AdnlPacket packet) = 0; + virtual void send_message_in(AdnlNodeIdShort src, AdnlNodeIdShort dst, AdnlMessage message) = 0; + + virtual void register_channel(AdnlChannelIdShort id, td::actor::ActorId channel) = 0; + virtual void unregister_channel(AdnlChannelIdShort id) = 0; + + virtual void add_static_node(AdnlNode node) = 0; + virtual void del_static_node(AdnlNodeIdShort id) = 0; + virtual void get_static_node(AdnlNodeIdShort id, td::Promise promise) = 0; + + virtual void write_new_addr_list_to_db(AdnlNodeIdShort local_id, AdnlNodeIdShort peer_id, AdnlDbItem node, + td::Promise promise) = 0; + virtual void get_addr_list_from_db(AdnlNodeIdShort local_id, AdnlNodeIdShort peer_id, + td::Promise promise) = 0; + + virtual void deliver(AdnlNodeIdShort src, AdnlNodeIdShort dst, td::BufferSlice data) = 0; + virtual void deliver_query(AdnlNodeIdShort src, AdnlNodeIdShort dst, td::BufferSlice data, + td::Promise promise) = 0; + virtual void decrypt_message(AdnlNodeIdShort dst, td::BufferSlice data, td::Promise promise) = 0; +}; + +} // namespace adnl + +} // namespace ton + +namespace td { + +inline td::StringBuilder &operator<<(td::StringBuilder &stream, const ton::adnl::AdnlChannelIdShort &value) { + return stream << value.bits256_value(); +} + +} // namespace td diff --git a/adnl/adnl-peer-table.hpp b/adnl/adnl-peer-table.hpp new file mode 100644 index 00000000..a4873727 --- /dev/null +++ b/adnl/adnl-peer-table.hpp @@ -0,0 +1,137 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +#include +#include + +#include "adnl-peer-table.h" +#include "adnl-peer.h" +#include "keys/encryptor.h" +//#include "adnl-decryptor.h" +#include "adnl-local-id.h" +#include "adnl-query.h" +#include "utils.hpp" +#include "adnl-static-nodes.h" +#include "adnl-ext-server.h" +#include "adnl-address-list.h" + +namespace ton { + +namespace adnl { + +class AdnlPeerTableImpl : public AdnlPeerTable { + public: + AdnlPeerTableImpl(std::string db_root, td::actor::ActorId keyring); + + void add_peer(AdnlNodeIdShort local_id, AdnlNodeIdFull id, AdnlAddressList addr_list) override; + void add_static_nodes_from_config(AdnlNodesList nodes) override; + + void receive_packet(td::BufferSlice data) override; + void receive_decrypted_packet(AdnlNodeIdShort dst, AdnlPacket data) override; + void send_message_in(AdnlNodeIdShort src, AdnlNodeIdShort dst, AdnlMessage message) override; + void send_message(AdnlNodeIdShort src, AdnlNodeIdShort dst, td::BufferSlice data) override { + if (data.size() > huge_packet_max_size()) { + VLOG(ADNL_WARNING) << "dropping too big packet [" << src << "->" << dst << "]: size=" << data.size(); + VLOG(ADNL_WARNING) << "DUMP: " << td::buffer_to_hex(data.as_slice().truncate(128)); + return; + } + send_message_in(src, dst, AdnlMessage{adnlmessage::AdnlMessageCustom{std::move(data)}}); + } + void answer_query(AdnlNodeIdShort src, AdnlNodeIdShort dst, AdnlQueryId query_id, td::BufferSlice data) override; + void send_query(AdnlNodeIdShort src, AdnlNodeIdShort dst, std::string name, td::Promise promise, + td::Timestamp timeout, td::BufferSlice data) override; + void send_query_ex(AdnlNodeIdShort src, AdnlNodeIdShort dst, std::string name, td::Promise promise, + td::Timestamp timeout, td::BufferSlice data, td::uint64 max_answer_size) override { + send_query(src, dst, name, std::move(promise), timeout, std::move(data)); + } + void add_id(AdnlNodeIdFull id, AdnlAddressList addr_list) override; + void del_id(AdnlNodeIdShort id, td::Promise promise) override; + void subscribe(AdnlNodeIdShort dst, std::string prefix, std::unique_ptr callback) override; + void unsubscribe(AdnlNodeIdShort dst, std::string prefix) override; + void register_dht_node(td::actor::ActorId dht_node) override; + void register_network_manager(td::actor::ActorId network_manager) override; + void get_addr_list(AdnlNodeIdShort id, td::Promise promise) override; + void get_self_node(AdnlNodeIdShort id, td::Promise promise) override; + void start_up() override; + void register_channel(AdnlChannelIdShort id, td::actor::ActorId channel) override; + void unregister_channel(AdnlChannelIdShort id) override; + + void write_new_addr_list_to_db(AdnlNodeIdShort local_id, AdnlNodeIdShort peer_id, AdnlDbItem node, + td::Promise promise) override; + void get_addr_list_from_db(AdnlNodeIdShort local_id, AdnlNodeIdShort peer_id, + td::Promise promise) override; + + void add_static_node(AdnlNode node) override { + CHECK(!static_nodes_manager_.empty()); + td::actor::send_closure(static_nodes_manager_, &AdnlStaticNodesManager::add_node, std::move(node)); + } + void del_static_node(AdnlNodeIdShort id) override { + td::actor::send_closure(static_nodes_manager_, &AdnlStaticNodesManager::del_node, id); + } + void get_static_node(AdnlNodeIdShort id, td::Promise promise) override { + td::actor::send_closure(static_nodes_manager_, &AdnlStaticNodesManager::get_node, id, std::move(promise)); + } + void deliver(AdnlNodeIdShort src, AdnlNodeIdShort dst, td::BufferSlice data) override; + void deliver_query(AdnlNodeIdShort src, AdnlNodeIdShort dst, td::BufferSlice data, + td::Promise promise) override; + void decrypt_message(AdnlNodeIdShort dst, td::BufferSlice data, td::Promise promise) override; + + void create_ext_server(std::vector ids, std::vector ports, + td::Promise> promise) override; + + struct PrintId {}; + PrintId print_id() const { + return PrintId{}; + } + + private: + td::actor::ActorId keyring_; + + td::actor::ActorId network_manager_; + td::actor::ActorId dht_node_; + td::actor::ActorOwn static_nodes_manager_; + + void deliver_one_message(AdnlNodeIdShort src, AdnlNodeIdShort dst, AdnlMessage message); + + std::map> peers_; + std::map> local_ids_own_; + std::map> channels_; + + td::actor::ActorOwn db_; + + td::actor::ActorOwn ext_server_; + + //std::map> out_queries_; + //td::uint64 last_query_id_ = 1; +}; + +inline td::StringBuilder &operator<<(td::StringBuilder &sb, const AdnlPeerTableImpl::PrintId &id) { + sb << "[peertable]"; + return sb; +} + +inline td::StringBuilder &operator<<(td::StringBuilder &sb, const AdnlPeerTableImpl &manager) { + sb << manager.print_id(); + return sb; +} + +} // namespace adnl + +} // namespace ton diff --git a/adnl/adnl-peer.cpp b/adnl/adnl-peer.cpp new file mode 100644 index 00000000..ba077682 --- /dev/null +++ b/adnl/adnl-peer.cpp @@ -0,0 +1,878 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "adnl-peer.h" +#include "adnl-peer.hpp" +#include "adnl-local-id.h" + +#include "utils.hpp" + +#include "td/actor/PromiseFuture.h" +#include "td/utils/base64.h" +#include "td/utils/Random.h" +#include "auto/tl/ton_api.h" + +namespace ton { + +namespace adnl { + +static_assert(AdnlPeerPairImpl::get_mtu() + AdnlPeerPairImpl::packet_header_max_size() <= AdnlNetworkManager::get_mtu(), + "wrong mtu configuration"); + +void AdnlPeerPairImpl::start_up() { + auto P1 = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result R) { + td::actor::send_closure(SelfId, &AdnlPeerPairImpl::got_data_from_db, std::move(R)); + }); + td::actor::send_closure(peer_table_, &AdnlPeerTable::get_addr_list_from_db, local_id_, peer_id_short_, std::move(P1)); + auto P2 = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result R) { + td::actor::send_closure(SelfId, &AdnlPeerPairImpl::got_data_from_static_nodes, std::move(R)); + }); + td::actor::send_closure(peer_table_, &AdnlPeerTable::get_static_node, peer_id_short_, std::move(P2)); + + if (!dht_node_.empty()) { + discover(); + } +} + +void AdnlPeerPairImpl::alarm() { + if (next_dht_query_at_ && next_dht_query_at_.is_in_past()) { + next_dht_query_at_ = td::Timestamp::never(); + discover(); + } + if (next_db_update_at_ && next_db_update_at_.is_in_past()) { + if (received_from_db_ && received_from_static_nodes_ && !peer_id_.empty()) { + AdnlDbItem item; + item.id = peer_id_; + item.addr_list = addr_list_; + item.priority_addr_list = priority_addr_list_; + + td::actor::send_closure(peer_table_, &AdnlPeerTable::write_new_addr_list_to_db, local_id_, peer_id_short_, + std::move(item), [](td::Unit) {}); + } + next_db_update_at_ = td::Timestamp::in(td::Random::fast(60.0, 120.0)); + } + if (retry_send_at_ && retry_send_at_.is_in_past()) { + retry_send_at_ = td::Timestamp::never(); + send_messages_in(std::move(pending_messages_), false); + } + alarm_timestamp().relax(next_dht_query_at_); + alarm_timestamp().relax(next_db_update_at_); + alarm_timestamp().relax(retry_send_at_); +} + +void AdnlPeerPairImpl::discover() { + CHECK(!dht_query_active_); + CHECK(!dht_node_.empty()); + dht_query_active_ = true; + + auto P = td::PromiseCreator::lambda([SelfId = actor_id(this), id = print_id(), + peer_id = peer_id_short_](td::Result kv) { + if (kv.is_error()) { + td::actor::send_closure(SelfId, &AdnlPeerPairImpl::got_data_from_dht, + kv.move_as_error_prefix("failed to get from dht: ")); + return; + } + auto k = kv.move_as_ok(); + auto pub = AdnlNodeIdFull{k.key().public_key()}; + CHECK(pub.compute_short_id() == peer_id); + + auto addr_list = fetch_tl_object(k.value().clone(), true); + if (addr_list.is_error()) { + td::actor::send_closure(SelfId, &AdnlPeerPairImpl::got_data_from_dht, + addr_list.move_as_error_prefix("bad dht value: ")); + return; + } + + auto F = AdnlAddressList::create(addr_list.move_as_ok()); + if (F.is_error()) { + td::actor::send_closure(SelfId, &AdnlPeerPairImpl::got_data_from_dht, F.move_as_error_prefix("bad dht value: ")); + return; + } + + AdnlNode node{pub, F.move_as_ok()}; + td::actor::send_closure(SelfId, &AdnlPeerPairImpl::got_data_from_dht, std::move(node)); + }); + + td::actor::send_closure(dht_node_, &dht::Dht::get_value, dht::DhtKey{peer_id_short_.pubkey_hash(), "address", 0}, + std::move(P)); +} + +void AdnlPeerPairImpl::receive_packet_checked(AdnlPacket packet) { + auto d = Adnl::adnl_start_time(); + if (packet.dst_reinit_date() > d) { + VLOG(ADNL_WARNING) << this << ": dropping IN message: too new our reinit date " << packet.dst_reinit_date(); + return; + } + if (packet.reinit_date() > td::Clocks::system() + 60) { + VLOG(ADNL_NOTICE) << this << ": dropping IN message: too new peer reinit date " << packet.reinit_date(); + return; + } + if (packet.reinit_date() > reinit_date_) { + reinit(packet.reinit_date()); + } + if (packet.reinit_date() > 0 && packet.reinit_date() < reinit_date_) { + VLOG(ADNL_NOTICE) << this << ": dropping IN message: old peer reinit date " << packet.reinit_date(); + return; + } + if (packet.dst_reinit_date() > 0 && packet.dst_reinit_date() < d) { + if (!packet.addr_list().empty()) { + update_addr_list(packet.addr_list()); + } + if (!packet.priority_addr_list().empty()) { + update_addr_list(packet.priority_addr_list()); + } + VLOG(ADNL_NOTICE) << this << ": dropping IN message old our reinit date " << packet.reinit_date() << " date=" << d; + auto M = AdnlMessage{adnlmessage::AdnlMessageNop{}}; + send_message(std::move(M)); + return; + } + if (packet.seqno() > 0) { + if (received_packet(static_cast(packet.seqno()))) { + VLOG(ADNL_INFO) << this << ": dropping IN message: old seqno: " << packet.seqno() << " (current max " << in_seqno_ + << ")"; + return; + } + } + if (packet.confirm_seqno() > 0) { + if (packet.confirm_seqno() > out_seqno_) { + VLOG(ADNL_WARNING) << this << ": dropping IN message: new ack seqno: " << packet.confirm_seqno() + << " (current max sent " << out_seqno_ << ")"; + return; + } + } + + // accepted + // delivering + + add_received_packet(static_cast(packet.seqno())); + + if (packet.confirm_seqno() > ack_seqno_) { + ack_seqno_ = packet.confirm_seqno(); + } + + if (packet.recv_addr_list_version() > peer_recv_addr_list_version_) { + peer_recv_addr_list_version_ = packet.recv_addr_list_version(); + } + + if (packet.recv_priority_addr_list_version() > peer_recv_priority_addr_list_version_) { + peer_recv_priority_addr_list_version_ = packet.recv_priority_addr_list_version(); + } + + if (!packet.addr_list().empty()) { + update_addr_list(packet.addr_list()); + } + if (!packet.priority_addr_list().empty()) { + update_addr_list(packet.priority_addr_list()); + } + + received_messages_++; + if (received_messages_ % 64 == 0) { + VLOG(ADNL_INFO) << this << ": received " << received_messages_ << " messages"; + } + for (auto &M : packet.messages().vector()) { + deliver_message(std::move(M)); + } +} + +void AdnlPeerPairImpl::receive_packet_from_channel(AdnlChannelIdShort id, AdnlPacket packet) { + if (id != channel_in_id_) { + VLOG(ADNL_NOTICE) << this << ": dropping IN message: outdated channel id" << id; + return; + } + channel_ready_ = true; + receive_packet_checked(std::move(packet)); +} + +void AdnlPeerPairImpl::receive_packet(AdnlPacket packet) { + packet.run_basic_checks().ensure(); + + if (!encryptor_) { + VLOG(ADNL_NOTICE) << this << "dropping IN message: unitialized id"; + return; + } + + auto S = encryptor_->check_signature(packet.to_sign().as_slice(), packet.signature().as_slice()); + if (S.is_error()) { + VLOG(ADNL_NOTICE) << this << "dropping IN message: bad signature: " << S; + return; + } + + receive_packet_checked(std::move(packet)); +} + +void AdnlPeerPairImpl::deliver_message(AdnlMessage message) { + message.visit([&](const auto &obj) { this->process_message(obj); }); +} + +void AdnlPeerPairImpl::send_messages_in(std::vector messages, bool allow_postpone) { + auto connR = get_conn(); + if (connR.is_error()) { + if (!allow_postpone) { + VLOG(ADNL_NOTICE) << this << ": dropping OUT messages: cannot get conn: " << connR.move_as_error(); + return; + } + VLOG(ADNL_INFO) << this << ": delaying OUT messages: cannot get conn: " << connR.move_as_error(); + if (!retry_send_at_) { + retry_send_at_.relax(td::Timestamp::in(10.0)); + alarm_timestamp().relax(retry_send_at_); + } + for (auto &m : messages) { + pending_messages_.push_back(std::move(m)); + } + return; + } + auto conn = connR.move_as_ok(); + + size_t ptr = 0; + bool first = true; + do { + size_t s = (channel_ready_ ? channel_packet_header_max_size() : packet_header_max_size()); + if (first) { + s += 2 * addr_list_max_size(); + } + + AdnlPacket packet; + packet.set_seqno(++out_seqno_); + packet.set_confirm_seqno(in_seqno_); + + if (first) { + if (!channel_inited_) { + auto M = adnlmessage::AdnlMessageCreateChannel{channel_pub_, channel_pk_date_}; + s += M.size(); + packet.add_message(std::move(M)); + } else if (!channel_ready_) { + auto M = adnlmessage::AdnlMessageConfirmChannel{channel_pub_, peer_channel_pub_, channel_pk_date_}; + s += M.size(); + packet.add_message(std::move(M)); + } + } + + if (!addr_list_.empty()) { + packet.set_received_addr_list_version(addr_list_.version()); + } + if (!priority_addr_list_.empty()) { + packet.set_received_priority_addr_list_version(priority_addr_list_.version()); + } + + while (ptr < messages.size()) { + auto &M = messages[ptr]; + CHECK(M.size() <= get_mtu()); + if (s + M.size() <= AdnlNetworkManager::get_mtu()) { + s += M.size(); + packet.add_message(std::move(M)); + ptr++; + } else { + break; + } + } + + if (!channel_ready_) { + packet.set_reinit_date(Adnl::adnl_start_time(), reinit_date_); + packet.set_source(local_id_); + } + + if (!first) { + if (!channel_inited_) { + auto M = adnlmessage::AdnlMessageCreateChannel{channel_pub_, channel_pk_date_}; + if (s + M.size() <= AdnlNetworkManager::get_mtu()) { + s += M.size(); + packet.add_message(std::move(M)); + } + } else if (!channel_ready_) { + auto M = adnlmessage::AdnlMessageConfirmChannel{channel_pub_, peer_channel_pub_, channel_pk_date_}; + if (s + M.size() <= AdnlNetworkManager::get_mtu()) { + s += M.size(); + packet.add_message(std::move(M)); + } + } + } + + packet.run_basic_checks().ensure(); + auto P = td::PromiseCreator::lambda([SelfId = actor_id(this), conn, id = print_id(), + via_channel = channel_ready_](td::Result res) { + if (res.is_error()) { + LOG(ERROR) << id << ": dropping OUT message: error while creating packet: " << res.move_as_error(); + } else { + td::actor::send_closure(SelfId, &AdnlPeerPairImpl::send_packet_continue, res.move_as_ok(), conn, via_channel); + } + }); + + td::actor::send_closure( + local_actor_, &AdnlLocalId::update_packet, std::move(packet), + !channel_ready_ && ack_seqno_ == 0 && in_seqno_ == 0, !channel_ready_, + (first || s + addr_list_max_size() <= AdnlNetworkManager::get_mtu()) ? peer_recv_addr_list_version_ + : 0x7fffffff, + (first || s + 2 * addr_list_max_size() <= AdnlNetworkManager::get_mtu()) ? peer_recv_priority_addr_list_version_ + : 0x7fffffff, + std::move(P)); + first = false; + } while (ptr < messages.size()); +} + +void AdnlPeerPairImpl::send_messages(std::vector messages) { + std::vector new_vec; + for (auto &M : messages) { + if (M.size() <= get_mtu()) { + new_vec.push_back(std::move(M)); + } else { + auto B = serialize_tl_object(M.tl(), true); + CHECK(B.size() <= huge_packet_max_size()); + + auto hash = sha256_bits256(B.as_slice()); + + auto size = static_cast(B.size()); + td::uint32 offset = 0; + td::uint32 part_size = Adnl::get_mtu(); + while (offset < size) { + auto data = B.clone(); + if (data.size() > part_size) { + data.truncate(part_size); + } + B.confirm_read(data.size()); + + new_vec.push_back(AdnlMessage{adnlmessage::AdnlMessagePart{hash, size, offset, std::move(data)}}); + offset += part_size; + } + } + } + send_messages_in(std::move(new_vec), true); +} + +void AdnlPeerPairImpl::send_packet_continue(AdnlPacket packet, td::actor::ActorId conn, + bool via_channel) { + packet.run_basic_checks().ensure(); + auto B = serialize_tl_object(packet.tl(), true); + if (via_channel) { + if (channel_ready_) { + td::actor::send_closure(channel_, &AdnlChannel::send_message, priority_, conn, std::move(B)); + } else { + VLOG(ADNL_WARNING) << this << ": dropping OUT message [" << local_id_ << "->" << peer_id_short_ + << "]: channel destroyed in process"; + } + return; + } + + if (!encryptor_) { + VLOG(ADNL_INFO) << this << ": dropping OUT message [" << local_id_ << "->" << peer_id_short_ + << "]: empty encryptor"; + return; + } + + auto res = encryptor_->encrypt(B.as_slice()); + if (res.is_error()) { + VLOG(ADNL_WARNING) << this << ": dropping OUT message [" << local_id_ << "->" << peer_id_short_ + << "]: failed to encrypt: " << res.move_as_error(); + return; + } + auto X = res.move_as_ok(); + auto enc = td::BufferSlice(X.size() + 32); + td::MutableSlice S = enc.as_slice(); + S.copy_from(peer_id_short_.as_slice()); + S.remove_prefix(32); + S.copy_from(X.as_slice()); + + td::actor::send_closure(conn, &AdnlNetworkConnection::send, local_id_, peer_id_short_, priority_, std::move(enc)); +} + +void AdnlPeerPairImpl::send_query(std::string name, td::Promise promise, td::Timestamp timeout, + td::BufferSlice data) { + AdnlQueryId id = AdnlQuery::random_query_id(); + CHECK(out_queries_.count(id) == 0); + + auto P = [SelfId = actor_id(this)](AdnlQueryId id) { + td::actor::send_closure(SelfId, &AdnlPeerPairImpl::delete_query, id); + }; + + out_queries_[id] = AdnlQuery::create(std::move(promise), std::move(P), name, timeout, id); + + send_message(adnlmessage::AdnlMessageQuery{id, std::move(data)}); +} + +void AdnlPeerPairImpl::alarm_query(AdnlQueryId id) { + out_queries_.erase(id); +} + +AdnlPeerPairImpl::AdnlPeerPairImpl(td::actor::ActorId network_manager, + td::actor::ActorId peer_table, + td::actor::ActorId local_actor, td::actor::ActorId peer, + td::actor::ActorId dht_node, AdnlNodeIdShort local_id, + AdnlNodeIdShort peer_id) { + network_manager_ = network_manager; + peer_table_ = peer_table; + local_actor_ = local_actor; + peer_ = peer; + dht_node_ = dht_node; + + local_id_ = local_id; + peer_id_short_ = peer_id; + + channel_pk_ = privkeys::Ed25519::random(); + channel_pub_ = channel_pk_.pub(); + channel_pk_date_ = static_cast(td::Clocks::system()); +} + +void AdnlPeerPairImpl::create_channel(pubkeys::Ed25519 pub, td::uint32 date) { + if (channel_inited_ && peer_channel_pub_ == pub) { + return; + } + if (channel_inited_ && date <= peer_channel_date_) { + return; + } + if (channel_inited_) { + td::actor::send_closure(peer_table_, &AdnlPeerTable::unregister_channel, channel_in_id_); + channel_.reset(); + channel_inited_ = false; + channel_ready_ = false; + } + CHECK(!channel_ready_); + + peer_channel_pub_ = pub; + peer_channel_date_ = date; + + auto R = AdnlChannel::create(channel_pk_, peer_channel_pub_, local_id_, peer_id_short_, channel_out_id_, + channel_in_id_, actor_id(this)); + if (R.is_ok()) { + channel_ = R.move_as_ok(); + channel_inited_ = true; + + td::actor::send_closure_later(peer_table_, &AdnlPeerTable::register_channel, channel_in_id_, channel_.get()); + } else { + VLOG(ADNL_WARNING) << this << ": failed to create channel: " << R.move_as_error(); + } +} + +void AdnlPeerPairImpl::process_message(const adnlmessage::AdnlMessageCreateChannel &message) { + create_channel(message.key(), message.date()); +} + +void AdnlPeerPairImpl::process_message(const adnlmessage::AdnlMessageConfirmChannel &message) { + if (message.peer_key() != channel_pub_) { + VLOG(ADNL_NOTICE) << this << ": received adnl.message.confirmChannel with bad peer_key"; + return; + } + create_channel(message.key(), message.date()); + if (!channel_inited_ || peer_channel_pub_ != message.key()) { + VLOG(ADNL_NOTICE) << this << ": received adnl.message.confirmChannel with old key"; + return; + } + channel_ready_ = true; +} + +void AdnlPeerPairImpl::process_message(const adnlmessage::AdnlMessageCustom &message) { + td::actor::send_closure(local_actor_, &AdnlLocalId::deliver, peer_id_short_, message.data()); +} + +void AdnlPeerPairImpl::process_message(const adnlmessage::AdnlMessageNop &message) { + // nop +} + +void AdnlPeerPairImpl::process_message(const adnlmessage::AdnlMessageReinit &message) { + reinit(message.date()); +} + +void AdnlPeerPairImpl::process_message(const adnlmessage::AdnlMessageQuery &message) { + auto P = td::PromiseCreator::lambda( + [SelfId = actor_id(this), query_id = message.query_id()](td::Result R) { + if (R.is_error()) { + LOG(WARNING) << "failed to answer query: " << R.move_as_error(); + } else { + auto data = R.move_as_ok(); + if (data.size() > Adnl::huge_packet_max_size()) { + LOG(WARNING) << "dropping too big answer query: size=" << data.size(); + } else { + td::actor::send_closure(SelfId, &AdnlPeerPairImpl::send_message, + AdnlMessage{adnlmessage::AdnlMessageAnswer{query_id, std::move(data)}}); + } + } + }); + td::actor::send_closure(local_actor_, &AdnlLocalId::deliver_query, peer_id_short_, message.data(), std::move(P)); +} + +void AdnlPeerPairImpl::process_message(const adnlmessage::AdnlMessageAnswer &message) { + auto Q = out_queries_.find(message.query_id()); + + if (Q == out_queries_.end()) { + VLOG(ADNL_NOTICE) << this << ": dropping IN answer: unknown query id " << message.query_id(); + return; + } + + if (message.data().size() > Adnl::huge_packet_max_size()) { + VLOG(ADNL_NOTICE) << this << ": dropping IN answer: too big answer size"; + return; + } + + td::actor::send_closure_later(Q->second, &AdnlQuery::result, message.data()); + out_queries_.erase(Q); +} + +void AdnlPeerPairImpl::process_message(const adnlmessage::AdnlMessagePart &message) { + auto size = message.total_size(); + if (size > huge_packet_max_size()) { + VLOG(ADNL_WARNING) << this << ": dropping too big huge message: size=" << size; + return; + } + if (message.hash().is_zero()) { + VLOG(ADNL_WARNING) << this << ": dropping huge message with zero hash"; + return; + } + if (message.hash() != huge_message_hash_) { + huge_message_hash_.set_zero(); + huge_message_.clear(); + huge_message_offset_ = 0; + if (message.offset() == 0) { + huge_message_hash_ = message.hash(); + huge_message_ = td::BufferSlice{size}; + } else { + return; + } + } + auto data = message.data(); + if (data.size() + message.offset() > size) { + VLOG(ADNL_WARNING) << this << ": dropping huge message with bad part"; + return; + } + if (size != huge_message_.size()) { + VLOG(ADNL_WARNING) << this << ": dropping huge message part with inconsistent size"; + return; + } + if (message.offset() == huge_message_offset_) { + auto S = huge_message_.as_slice(); + S.remove_prefix(huge_message_offset_); + S.copy_from(data.as_slice()); + huge_message_offset_ += static_cast(data.size()); + + if (huge_message_offset_ == huge_message_.size()) { + //td::actor::send_closure(local_actor_, &AdnlLocalId::deliver, peer_id_short_, std::move(huge_message_)); + if (sha256_bits256(huge_message_.as_slice()) != huge_message_hash_) { + VLOG(ADNL_WARNING) << this << ": dropping huge message: hash mismatch"; + return; + } + huge_message_hash_.set_zero(); + huge_message_offset_ = 0; + auto MR = fetch_tl_object(std::move(huge_message_), true); + if (MR.is_error()) { + VLOG(ADNL_WARNING) << this << ": dropping huge message part with bad data"; + return; + } + auto M = AdnlMessage{MR.move_as_ok()}; + deliver_message(std::move(M)); + } + } +} + +void AdnlPeerPairImpl::delete_query(AdnlQueryId id) { + auto Q = out_queries_.find(id); + + if (Q != out_queries_.end()) { + out_queries_.erase(Q); + } +} + +void AdnlPeerPairImpl::reinit(td::int32 date) { + if (reinit_date_ == 0) { + reinit_date_ = date; + } + if (reinit_date_ < date) { + if (channel_inited_) { + td::actor::send_closure(peer_table_, &AdnlPeerTable::unregister_channel, channel_in_id_); + } + + in_seqno_ = 0; + out_seqno_ = 0; + ack_seqno_ = 0; + recv_seqno_mask_ = 0; + + channel_ready_ = false; + channel_inited_ = false; + + peer_recv_addr_list_version_ = 0; + + huge_message_offset_ = 0; + huge_message_hash_.set_zero(); + huge_message_.clear(); + + channel_.release(); + + reinit_date_ = date; + } +} + +td::Result> AdnlPeerPairImpl::get_conn() { + if (!priority_addr_list_.empty() && priority_addr_list_.expire_at() < td::Clocks::system()) { + priority_addr_list_ = AdnlAddressList{}; + priority_conns_.clear(); + } + + if (conns_.size() == 0 && priority_conns_.size() == 0) { + return td::Status::Error(ErrorCode::notready, PSTRING() + << "empty network information: version=" << addr_list_.version() + << " reinit_date=" << addr_list_.reinit_date() + << " real_reinit_date=" << reinit_date_); + } + + for (auto &conn : priority_conns_) { + if (conn.ready()) { + return conn.conn.get(); + } + } + for (auto &conn : conns_) { + if (conn.ready()) { + return conn.conn.get(); + } + } + return td::Status::Error(ErrorCode::notready, "no active connections"); +} + +void AdnlPeerPairImpl::update_addr_list(AdnlAddressList addr_list) { + if (addr_list.empty()) { + return; + } + CHECK(addr_list.size() > 0); + + if (addr_list.reinit_date() > td::Clocks::system() + 60) { + VLOG(ADNL_WARNING) << "dropping addr list with too new reinit date"; + return; + } + + if (addr_list.reinit_date() > reinit_date_) { + reinit(addr_list.reinit_date()); + } else if (addr_list.reinit_date() < reinit_date_) { + return; + } + + bool priority = addr_list.priority() > 0; + + if ((priority ? priority_addr_list_ : addr_list_).version() >= addr_list.version()) { + if (priority && priority_addr_list_.version() == addr_list.version()) { + auto expire_at = addr_list.expire_at(); + if (expire_at > priority_addr_list_.expire_at()) { + priority_addr_list_.set_expire_at(expire_at); + } + } + return; + } + + VLOG(ADNL_INFO) << this << ": updating addr list to version " << addr_list.version() << " size=" << addr_list.size(); + + const auto addrs = addr_list.addrs(); + std::vector conns; + conns.resize(std::min(addr_list.size(), 3u)); + auto &old_conns = priority ? priority_conns_ : conns_; + + for (size_t i = 0; i < conns.size(); i++) { + auto &addr = addrs[i]; + auto hash = addr->get_hash(); + if (i < old_conns.size() && old_conns[i].addr->get_hash() == hash) { + conns[i] = std::move(old_conns[i]); + } else { + conns[i] = Conn{addr, actor_id(this), network_manager_}; + } + } + + old_conns = std::move(conns); + (priority ? priority_addr_list_ : addr_list_) = addr_list; +} + +void AdnlPeerImpl::update_id(AdnlNodeIdFull id) { + CHECK(id.compute_short_id() == peer_id_short_); + if (!peer_id_.empty()) { + return; + } + + peer_id_ = std::move(id); + + for (auto &it : peer_pairs_) { + td::actor::send_closure(it.second.get(), &AdnlPeerPair::update_peer_id, peer_id_); + } +} + +void AdnlPeerPairImpl::Conn::create_conn(td::actor::ActorId peer, + td::actor::ActorId network_manager) { + auto id = addr->get_hash(); + + conn = addr->create_connection(network_manager, std::make_unique(peer, id)); +} + +void AdnlPeerPairImpl::conn_change_state(AdnlConnectionIdShort id, bool ready) { + if (ready) { + if (pending_messages_.size() > 0) { + send_messages_in(std::move(pending_messages_), true); + } + } +} + +td::actor::ActorOwn AdnlPeerPair::create(td::actor::ActorId network_manager, + td::actor::ActorId peer_table, + td::actor::ActorId local_actor, + td::actor::ActorId peer_actor, + td::actor::ActorId dht_node, AdnlNodeIdShort local_id, + AdnlNodeIdShort peer_id) { + auto X = td::actor::create_actor("peerpair", network_manager, peer_table, local_actor, peer_actor, + dht_node, local_id, peer_id); + return td::actor::ActorOwn(std::move(X)); +} + +td::actor::ActorOwn AdnlPeer::create(td::actor::ActorId network_manager, + td::actor::ActorId peer_table, + td::actor::ActorId dht_node, AdnlNodeIdShort peer_id) { + auto X = td::actor::create_actor("peer", network_manager, peer_table, dht_node, peer_id); + return td::actor::ActorOwn(std::move(X)); +} + +void AdnlPeerImpl::receive_packet(AdnlNodeIdShort dst, td::actor::ActorId dst_actor, AdnlPacket packet) { + if (packet.inited_from()) { + update_id(packet.from()); + } + + auto it = peer_pairs_.find(dst); + if (it == peer_pairs_.end()) { + auto X = + AdnlPeerPair::create(network_manager_, peer_table_, dst_actor, actor_id(this), dht_node_, dst, peer_id_short_); + peer_pairs_.emplace(dst, std::move(X)); + it = peer_pairs_.find(dst); + CHECK(it != peer_pairs_.end()); + + if (!peer_id_.empty()) { + td::actor::send_closure(it->second.get(), &AdnlPeerPair::update_peer_id, peer_id_); + } + } + + td::actor::send_closure(it->second.get(), &AdnlPeerPair::receive_packet_checked, std::move(packet)); +} + +void AdnlPeerImpl::send_messages(AdnlNodeIdShort src, td::actor::ActorId src_actor, + std::vector messages) { + auto it = peer_pairs_.find(src); + if (it == peer_pairs_.end()) { + auto X = + AdnlPeerPair::create(network_manager_, peer_table_, src_actor, actor_id(this), dht_node_, src, peer_id_short_); + peer_pairs_.emplace(src, std::move(X)); + it = peer_pairs_.find(src); + CHECK(it != peer_pairs_.end()); + + if (!peer_id_.empty()) { + td::actor::send_closure(it->second.get(), &AdnlPeerPair::update_peer_id, peer_id_); + } + } + + td::actor::send_closure(it->second, &AdnlPeerPair::send_messages, std::move(messages)); +} + +void AdnlPeerImpl::send_query(AdnlNodeIdShort src, td::actor::ActorId src_actor, std::string name, + td::Promise promise, td::Timestamp timeout, td::BufferSlice data) { + auto it = peer_pairs_.find(src); + if (it == peer_pairs_.end()) { + auto X = + AdnlPeerPair::create(network_manager_, peer_table_, src_actor, actor_id(this), dht_node_, src, peer_id_short_); + peer_pairs_.emplace(src, std::move(X)); + it = peer_pairs_.find(src); + CHECK(it != peer_pairs_.end()); + + if (!peer_id_.empty()) { + td::actor::send_closure(it->second.get(), &AdnlPeerPair::update_peer_id, peer_id_); + } + } + + td::actor::send_closure(it->second, &AdnlPeerPair::send_query, name, std::move(promise), timeout, std::move(data)); +} + +void AdnlPeerImpl::del_local_id(AdnlNodeIdShort local_id) { + peer_pairs_.erase(local_id); +} + +void AdnlPeerImpl::update_dht_node(td::actor::ActorId dht_node) { + dht_node_ = dht_node; + for (auto it = peer_pairs_.begin(); it != peer_pairs_.end(); it++) { + td::actor::send_closure(it->second, &AdnlPeerPair::update_dht_node, dht_node_); + } +} + +void AdnlPeerImpl::update_addr_list(AdnlNodeIdShort local_id, td::actor::ActorId local_actor, + AdnlAddressList addr_list) { + auto it = peer_pairs_.find(local_id); + if (it == peer_pairs_.end()) { + auto X = AdnlPeerPair::create(network_manager_, peer_table_, local_actor, actor_id(this), dht_node_, local_id, + peer_id_short_); + peer_pairs_.emplace(local_id, std::move(X)); + it = peer_pairs_.find(local_id); + CHECK(it != peer_pairs_.end()); + + if (!peer_id_.empty()) { + td::actor::send_closure(it->second.get(), &AdnlPeerPair::update_peer_id, peer_id_); + } + } + + td::actor::send_closure(it->second, &AdnlPeerPair::update_addr_list, std::move(addr_list)); +} + +void AdnlPeerPairImpl::got_data_from_db(td::Result R) { + received_from_db_ = false; + if (R.is_error()) { + return; + } + auto value = R.move_as_ok(); + if (!value.id.empty()) { + update_peer_id(value.id); + } + update_addr_list(value.addr_list); + update_addr_list(value.priority_addr_list); +} + +void AdnlPeerPairImpl::got_data_from_static_nodes(td::Result R) { + received_from_static_nodes_ = false; + if (R.is_error()) { + return; + } + auto value = R.move_as_ok(); + if (!value.pub_id().empty()) { + update_peer_id(value.pub_id()); + } + update_addr_list(value.addr_list()); +} + +void AdnlPeerPairImpl::got_data_from_dht(td::Result R) { + CHECK(dht_query_active_); + dht_query_active_ = false; + next_dht_query_at_ = td::Timestamp::in(td::Random::fast(60.0, 120.0)); + if (R.is_error()) { + VLOG(ADNL_INFO) << this << ": dht query failed: " << R.move_as_error(); + return; + } + auto value = R.move_as_ok(); + if (!value.pub_id().empty()) { + update_peer_id(value.pub_id()); + } + update_addr_list(value.addr_list()); +} + +void AdnlPeerPairImpl::update_peer_id(AdnlNodeIdFull id) { + if (peer_id_.empty()) { + peer_id_ = std::move(id); + auto R = peer_id_.pubkey().create_encryptor(); + if (R.is_ok()) { + encryptor_ = R.move_as_ok(); + } else { + VLOG(ADNL_WARNING) << this << ": failed to create encryptor: " << R.move_as_error(); + } + } + CHECK(!peer_id_.empty()); +} + +} // namespace adnl + +} // namespace ton diff --git a/adnl/adnl-peer.h b/adnl/adnl-peer.h new file mode 100644 index 00000000..91e8dd9b --- /dev/null +++ b/adnl/adnl-peer.h @@ -0,0 +1,101 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +#include "td/actor/actor.h" +#include "td/utils/BufferedUdp.h" + +#include "dht/dht.h" +#include "adnl-peer-table.h" +#include "utils.hpp" +#include "auto/tl/ton_api.h" + +namespace ton { + +namespace adnl { + +class AdnlPeerTable; +class AdnlNetworkManager; +class AdnlLocalId; +class AdnlNetworkConnection; + +class AdnlPeer; + +class AdnlPeerPair : public td::actor::Actor { + public: + virtual void receive_packet_from_channel(AdnlChannelIdShort id, AdnlPacket packet) = 0; + virtual void receive_packet_checked(AdnlPacket packet) = 0; + virtual void receive_packet(AdnlPacket packet) = 0; + + virtual void send_messages(std::vector message) = 0; + inline void send_message(AdnlMessage message) { + std::vector vec; + vec.push_back(std::move(message)); + send_messages(std::move(vec)); + } + static constexpr td::uint32 get_mtu() { + return Adnl::get_mtu() + 128; + } + virtual void send_query(std::string name, td::Promise promise, td::Timestamp timeout, + td::BufferSlice data) = 0; + virtual void alarm_query(AdnlQueryId query_id) = 0; + virtual void update_dht_node(td::actor::ActorId dht_node) = 0; + virtual void update_peer_id(AdnlNodeIdFull id) = 0; + virtual void update_addr_list(AdnlAddressList addr_list) = 0; + + static td::actor::ActorOwn create(td::actor::ActorId network_manager, + td::actor::ActorId peer_table, + td::actor::ActorId local_actor, + td::actor::ActorId peer_actor, + td::actor::ActorId dht_node, AdnlNodeIdShort local_id, + AdnlNodeIdShort peer_id); +}; + +class AdnlPeer : public td::actor::Actor { + public: + virtual void receive_packet(AdnlNodeIdShort dst, td::actor::ActorId dst_actor, AdnlPacket message) = 0; + virtual void send_messages(AdnlNodeIdShort src, td::actor::ActorId src_actor, + std::vector messages) = 0; + virtual void send_query(AdnlNodeIdShort src, td::actor::ActorId src_actor, std::string name, + td::Promise promise, td::Timestamp timeout, td::BufferSlice data) = 0; + void send_one_message(AdnlNodeIdShort src, td::actor::ActorId src_actor, AdnlMessage message) { + std::vector vec; + vec.push_back(std::move(message)); + send_messages(src, src_actor, std::move(vec)); + } + + void send_message(AdnlNodeIdShort src, td::actor::ActorId src_actor, td::BufferSlice data) { + auto M = AdnlMessage{adnlmessage::AdnlMessageCustom{std::move(data)}}; + send_one_message(src, src_actor, std::move(M)); + } + + static td::actor::ActorOwn create(td::actor::ActorId network_manager, + td::actor::ActorId peer_table, + td::actor::ActorId dht_node, AdnlNodeIdShort peer_id); + + virtual void del_local_id(AdnlNodeIdShort local_id) = 0; + virtual void update_id(AdnlNodeIdFull id) = 0; + virtual void update_addr_list(AdnlNodeIdShort local_id, td::actor::ActorId local_actor, + AdnlAddressList addr_list) = 0; + virtual void update_dht_node(td::actor::ActorId dht_node) = 0; +}; + +} // namespace adnl + +} // namespace ton diff --git a/adnl/adnl-peer.hpp b/adnl/adnl-peer.hpp new file mode 100644 index 00000000..171be5ea --- /dev/null +++ b/adnl/adnl-peer.hpp @@ -0,0 +1,318 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +#include +#include + +#include "adnl-peer.h" +#include "adnl-peer-table.h" +#include "adnl-network-manager.h" +#include "keys/encryptor.h" +#include "adnl-channel.h" +#include "adnl-query.h" + +#include "crypto/Ed25519.h" +#include "td/utils/DecTree.h" + +#include "utils.hpp" + +namespace ton { + +namespace adnl { + +using AdnlConnectionIdShort = AdnlAddressImpl::Hash; + +class AdnlPeerPairImpl : public AdnlPeerPair { + public: + static constexpr td::uint32 packet_header_max_size() { + return 272; + } + static constexpr td::uint32 channel_packet_header_max_size() { + return 128; + } + static constexpr td::uint32 addr_list_max_size() { + return 128; + } + static constexpr td::uint32 get_mtu() { + return Adnl::get_mtu() + 128; + } + static constexpr td::uint32 huge_packet_max_size() { + return Adnl::huge_packet_max_size() + 128; + } + + AdnlPeerPairImpl(td::actor::ActorId network_manager, td::actor::ActorId peer_table, + td::actor::ActorId local_actor, td::actor::ActorId peer, + td::actor::ActorId dht_node, AdnlNodeIdShort local_id, AdnlNodeIdShort peer_id); + void start_up() override; + void alarm() override; + + void discover(); + + void receive_packet_from_channel(AdnlChannelIdShort id, AdnlPacket packet) override; + void receive_packet_checked(AdnlPacket packet) override; + void receive_packet(AdnlPacket packet) override; + void deliver_message(AdnlMessage message); + + void send_messages_in(std::vector messages, bool allow_postpone); + void send_messages(std::vector messages) override; + void send_packet_continue(AdnlPacket packet, td::actor::ActorId conn, bool via_channel); + void send_query(std::string name, td::Promise promise, td::Timestamp timeout, + td::BufferSlice data) override; + + void alarm_query(AdnlQueryId id) override; + + void discover_query_result(td::Result B, bool dummy); + + void update_dht_node(td::actor::ActorId dht_node) override { + dht_node_ = dht_node; + } + + void update_addr_list(AdnlAddressList addr_list) override; + void update_peer_id(AdnlNodeIdFull id) override; + + void got_data_from_db(td::Result R); + void got_data_from_static_nodes(td::Result R); + void got_data_from_dht(td::Result R); + + //void conn_ready(AdnlConnectionIdShort id, td::Result> R); + + void process_message(const adnlmessage::AdnlMessageCreateChannel &message); + void process_message(const adnlmessage::AdnlMessageConfirmChannel &message); + void process_message(const adnlmessage::AdnlMessageCustom &message); + void process_message(const adnlmessage::AdnlMessageNop &message); + void process_message(const adnlmessage::AdnlMessageReinit &message); + void process_message(const adnlmessage::AdnlMessageQuery &message); + void process_message(const adnlmessage::AdnlMessageAnswer &message); + void process_message(const adnlmessage::AdnlMessagePart &message); + void process_message(const AdnlMessage::Empty &message) { + UNREACHABLE(); + } + + void conn_change_state(AdnlConnectionIdShort conn_id, bool ready); + + void delete_query(AdnlQueryId id); + + struct PrintId { + AdnlNodeIdShort peer_id; + AdnlNodeIdShort local_id; + }; + + PrintId print_id() const { + return PrintId{peer_id_short_, local_id_}; + } + + private: + void reinit(td::int32 date); + td::Result> get_conn(); + void create_channel(pubkeys::Ed25519 pub, td::uint32 date); + + bool received_packet(td::uint32 seqno) const { + CHECK(seqno > 0); + if (seqno + 64 <= in_seqno_) { + return true; + } + if (seqno > in_seqno_) { + return false; + } + return recv_seqno_mask_ & (1ull << (in_seqno_ - seqno)); + } + + void add_received_packet(td::uint32 seqno) { + CHECK(!received_packet(seqno)); + if (seqno <= in_seqno_) { + recv_seqno_mask_ |= (1ull << (in_seqno_ - seqno)); + } else { + auto old = in_seqno_; + in_seqno_ = seqno; + if (in_seqno_ - old >= 64) { + recv_seqno_mask_ = 1; + } else { + recv_seqno_mask_ = recv_seqno_mask_ << (in_seqno_ - old); + recv_seqno_mask_ |= 1; + } + } + } + + struct Conn { + class ConnCallback : public AdnlNetworkConnection::Callback { + public: + void on_change_state(bool ready) override { + td::actor::send_closure(root_, &AdnlPeerPairImpl::conn_change_state, conn_id_, ready); + } + ConnCallback(td::actor::ActorId root, AdnlConnectionIdShort conn_id) + : root_(root), conn_id_(conn_id) { + } + + private: + td::actor::ActorId root_; + AdnlConnectionIdShort conn_id_; + }; + + AdnlAddress addr; + td::actor::ActorOwn conn; + + Conn(AdnlAddress addr, td::actor::ActorId peer, + td::actor::ActorId network_manager) + : addr(std::move(addr)) { + create_conn(peer, network_manager); + } + Conn() { + } + + bool ready() { + return !conn.empty() && conn.get_actor_unsafe().is_active(); + } + + void create_conn(td::actor::ActorId peer, td::actor::ActorId network_manager); + }; + + std::vector pending_messages_; + + td::actor::ActorId network_manager_; + td::actor::ActorId peer_table_; + td::actor::ActorId local_actor_; + td::actor::ActorId peer_; + td::actor::ActorId dht_node_; + + td::uint32 priority_ = 0; + + td::int32 reinit_date_ = 0; + + bool channel_ready_ = false; + bool channel_inited_ = false; + AdnlChannelIdShort channel_in_id_; + AdnlChannelIdShort channel_out_id_; + privkeys::Ed25519 channel_pk_; + pubkeys::Ed25519 channel_pub_; + td::int32 channel_pk_date_; + td::actor::ActorOwn channel_; + + td::uint64 in_seqno_ = 0; + td::uint64 out_seqno_ = 0; + td::uint64 ack_seqno_ = 0; + td::uint64 recv_seqno_mask_ = 0; + + td::uint32 peer_channel_date_ = 0; + pubkeys::Ed25519 peer_channel_pub_; + td::int32 peer_recv_addr_list_version_ = -1; + td::int32 peer_recv_priority_addr_list_version_ = -1; + + td::Bits256 huge_message_hash_ = td::Bits256::zero(); + td::BufferSlice huge_message_; + td::uint32 huge_message_offset_ = 0; + + AdnlAddressList addr_list_; + AdnlAddressList priority_addr_list_; + + std::vector conns_; + std::vector priority_conns_; + + AdnlNodeIdFull peer_id_; + AdnlNodeIdShort peer_id_short_; + AdnlNodeIdShort local_id_; + + std::unique_ptr encryptor_; + + std::map> out_queries_; + + td::uint32 received_messages_ = 0; + bool received_from_db_ = false; + bool received_from_static_nodes_ = false; + bool dht_query_active_ = false; + + td::Timestamp next_dht_query_at_ = td::Timestamp::never(); + td::Timestamp next_db_update_at_ = td::Timestamp::never(); + td::Timestamp retry_send_at_ = td::Timestamp::never(); +}; + +class AdnlPeerImpl : public AdnlPeer { + public: + void receive_packet(AdnlNodeIdShort dst, td::actor::ActorId dst_actor, AdnlPacket packet) override; + void send_messages(AdnlNodeIdShort src, td::actor::ActorId src_actor, + std::vector messages) override; + void send_query(AdnlNodeIdShort src, td::actor::ActorId src_actor, std::string name, + td::Promise promise, td::Timestamp timeout, td::BufferSlice data) override; + + void del_local_id(AdnlNodeIdShort local_id) override; + void update_id(AdnlNodeIdFull id) override; + void update_addr_list(AdnlNodeIdShort local_id, td::actor::ActorId local_actor, + AdnlAddressList addr_list) override; + void update_dht_node(td::actor::ActorId dht_node) override; + //void check_signature(td::BufferSlice data, td::BufferSlice signature, td::Promise promise) override; + + AdnlPeerImpl(td::actor::ActorId network_manager, td::actor::ActorId peer_table, + td::actor::ActorId dht_node, AdnlNodeIdShort peer_id) + : peer_id_short_(peer_id), dht_node_(dht_node), peer_table_(peer_table), network_manager_(network_manager) { + } + + struct PrintId { + AdnlNodeIdShort peer_id; + }; + + PrintId print_id() const { + return PrintId{peer_id_short_}; + } + + private: + AdnlNodeIdShort peer_id_short_; + AdnlNodeIdFull peer_id_; + std::map> peer_pairs_; + td::actor::ActorId dht_node_; + td::actor::ActorId peer_table_; + td::actor::ActorId network_manager_; +}; + +} // namespace adnl + +} // namespace ton + +namespace td { + +inline td::StringBuilder &operator<<(td::StringBuilder &sb, const ton::adnl::AdnlPeerImpl::PrintId &id) { + sb << "[peer " << id.peer_id << "]"; + return sb; +} + +inline td::StringBuilder &operator<<(td::StringBuilder &sb, const ton::adnl::AdnlPeerImpl &peer) { + sb << peer.print_id(); + return sb; +} + +inline td::StringBuilder &operator<<(td::StringBuilder &sb, const ton::adnl::AdnlPeerImpl *peer) { + sb << peer->print_id(); + return sb; +} + +inline td::StringBuilder &operator<<(td::StringBuilder &sb, const ton::adnl::AdnlPeerPairImpl::PrintId &id) { + sb << "[peerpair " << id.peer_id << "-" << id.local_id << "]"; + return sb; +} + +inline td::StringBuilder &operator<<(td::StringBuilder &sb, const ton::adnl::AdnlPeerPairImpl &peer) { + sb << peer.print_id(); + return sb; +} + +inline td::StringBuilder &operator<<(td::StringBuilder &sb, const ton::adnl::AdnlPeerPairImpl *peer) { + sb << peer->print_id(); + return sb; +} + +} // namespace td diff --git a/adnl/adnl-proxy-types.cpp b/adnl/adnl-proxy-types.cpp new file mode 100644 index 00000000..3ab3d6fb --- /dev/null +++ b/adnl/adnl-proxy-types.cpp @@ -0,0 +1,87 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "adnl-proxy-types.hpp" +#include "tl-utils/tl-utils.hpp" +#include "auto/tl/ton_api.hpp" +#include "td/utils/overloaded.h" +#include "td/utils/Time.h" +#include "common/errorcode.h" + +namespace ton { + +namespace adnl { + +td::BufferSlice AdnlProxyFast::encrypt(Packet packet) const { + auto date = static_cast(td::Clocks::system()); + auto signature = create_hash_tl_object( + packet.ip, packet.port, date, sha256_bits256(packet.data.as_slice()), shared_secret_); + + auto obj = create_serialize_tl_object(packet.ip, packet.port, date, signature); + td::BufferSlice res{32 + obj.size() + packet.data.size()}; + auto S = res.as_slice(); + S.copy_from(td::Bits256::zero().as_slice()); + S.remove_prefix(32); + S.copy_from(obj.as_slice()); + S.remove_prefix(obj.size()); + S.copy_from(packet.data.as_slice()); + + return res; +} + +td::Result AdnlProxyFast::decrypt(td::BufferSlice packet) const { + if (packet.size() < 36) { + return td::Status::Error(ErrorCode::protoviolation, "too short packet"); + } + + td::Bits256 v; + v.as_slice().copy_from(packet.as_slice().truncate(32)); + if (!v.is_zero()) { + return td::Status::Error(ErrorCode::protoviolation, "non-zero DST"); + } + packet.confirm_read(32); + + TRY_RESULT(R, fetch_tl_prefix(packet, true)); + + if (R->date_ < td::Clocks::system() - 8) { + return td::Status::Error(ErrorCode::protoviolation, "too old date"); + } + + auto signature = create_hash_tl_object( + R->ip_, R->port_, R->date_, sha256_bits256(packet.as_slice()), shared_secret_); + if (signature != R->signature_) { + return td::Status::Error(ErrorCode::protoviolation, "bad signature"); + } + + return Packet{static_cast(R->ip_), static_cast(R->port_), std::move(packet)}; +} + +td::Result> AdnlProxy::create(const ton_api::adnl_Proxy &proxy_type) { + std::shared_ptr R; + ton_api::downcast_call( + const_cast(proxy_type), + td::overloaded([&](const ton_api::adnl_proxy_none &x) { R = std::make_shared(); }, + [&](const ton_api::adnl_proxy_fast &x) { + R = std::make_shared(x.shared_secret_.as_slice()); + })); + return R; +} + +} // namespace adnl + +} // namespace ton diff --git a/adnl/adnl-proxy-types.h b/adnl/adnl-proxy-types.h new file mode 100644 index 00000000..ef80d09e --- /dev/null +++ b/adnl/adnl-proxy-types.h @@ -0,0 +1,45 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +#include "td/utils/buffer.h" +#include "auto/tl/ton_api.h" + +namespace ton { + +namespace adnl { + +class AdnlProxy { + public: + struct Packet { + td::uint32 ip; + td::uint16 port; + td::BufferSlice data; + }; + virtual ~AdnlProxy() = default; + virtual td::BufferSlice encrypt(Packet packet) const = 0; + virtual td::Result decrypt(td::BufferSlice packet) const = 0; + virtual tl_object_ptr tl() const = 0; + + static td::Result> create(const ton_api::adnl_Proxy &proxy_type); +}; + +} // namespace adnl + +} // namespace ton diff --git a/adnl/adnl-proxy-types.hpp b/adnl/adnl-proxy-types.hpp new file mode 100644 index 00000000..1b34cdc0 --- /dev/null +++ b/adnl/adnl-proxy-types.hpp @@ -0,0 +1,62 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +#include "adnl-proxy-types.h" + +#include "common/checksum.h" + +namespace ton { + +namespace adnl { + +class AdnlProxyNone : public AdnlProxy { + public: + AdnlProxyNone() { + } + td::BufferSlice encrypt(Packet packet) const override { + return std::move(packet.data); + } + td::Result decrypt(td::BufferSlice packet) const override { + return Packet{0, 0, std::move(packet)}; + } + tl_object_ptr tl() const override { + return create_tl_object(); + } +}; + +class AdnlProxyFast : public AdnlProxy { + public: + AdnlProxyFast(td::Slice shared_secret) + : shared_secret_(sha256_bits256(shared_secret)), shared_secret_raw_(shared_secret) { + } + td::BufferSlice encrypt(Packet packet) const override; + td::Result decrypt(td::BufferSlice packet) const override; + tl_object_ptr tl() const override { + return create_tl_object(shared_secret_raw_.clone_as_buffer_slice()); + } + + private: + td::Bits256 shared_secret_; + td::SharedSlice shared_secret_raw_; +}; + +} // namespace adnl + +} // namespace ton diff --git a/adnl/adnl-proxy.cpp b/adnl/adnl-proxy.cpp new file mode 100644 index 00000000..91b875bc --- /dev/null +++ b/adnl/adnl-proxy.cpp @@ -0,0 +1,265 @@ +/* + This file is part of TON Blockchain source code. + + TON Blockchain is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + TON Blockchain is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with TON Blockchain. If not, see . + + In addition, as a special exception, the copyright holders give permission + to link the code of portions of this program with the OpenSSL library. + You must obey the GNU General Public License in all respects for all + of the code used other than OpenSSL. If you modify file(s) with this + exception, you may extend this exception to your version of the file(s), + but you are not obligated to do so. If you do not wish to do so, delete this + exception statement from your version. If you delete this exception statement + from all source files in the program, then also delete it here. + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "td/actor/actor.h" +#include "td/utils/buffer.h" +#include "td/utils/port/IPAddress.h" +#include "td/net/UdpServer.h" +#include "td/utils/port/signals.h" +#include "td/utils/OptionsParser.h" +#include "td/utils/FileLog.h" +#include "td/utils/port/path.h" +#include "td/utils/port/user.h" +#include "td/utils/filesystem.h" +#include "common/checksum.h" +#include "common/errorcode.h" +#include "tl-utils/tl-utils.hpp" +#include "auto/tl/ton_api_json.h" +#include "adnl-proxy-types.h" +#include + +#if TD_DARWIN || TD_LINUX +#include +#endif + +namespace ton { + +namespace adnl { + +class Receiver : public td::actor::Actor { + public: + void start_up() override; + void receive_common(td::BufferSlice data); + void receive_from_client(td::BufferSlice data); + void receive_to_client(td::BufferSlice data); + + Receiver(td::uint16 in_port, td::uint16 out_port, std::shared_ptr proxy, td::IPAddress client_addr) + : in_port_(in_port), out_port_(out_port), proxy_(std::move(proxy)), addr_(client_addr) { + } + + private: + td::uint16 in_port_; + td::uint16 out_port_; + std::shared_ptr proxy_; + td::IPAddress addr_; + td::actor::ActorOwn out_udp_server_; + td::actor::ActorOwn in_udp_server_; +}; + +void Receiver::start_up() { + class Callback : public td::UdpServer::Callback { + public: + Callback(td::actor::ActorId manager, td::uint32 mode) : manager_(std::move(manager)), mode_(mode) { + } + + private: + td::actor::ActorId manager_; + const td::uint32 mode_; + void on_udp_message(td::UdpMessage udp_message) override { + if (udp_message.error.is_error()) { + LOG(DEBUG) << udp_message.error; + return; + } + if (mode_ == 0) { + td::actor::send_closure_later(manager_, &Receiver::receive_common, std::move(udp_message.data)); + } else if (mode_ == 1) { + td::actor::send_closure_later(manager_, &Receiver::receive_from_client, std::move(udp_message.data)); + } else { + td::actor::send_closure_later(manager_, &Receiver::receive_to_client, std::move(udp_message.data)); + } + } + }; + + if (in_port_ == out_port_) { + auto X = td::UdpServer::create("udp server", in_port_, std::make_unique(actor_id(this), 0)); + X.ensure(); + in_udp_server_ = X.move_as_ok(); + } else { + auto X = td::UdpServer::create("udp server", in_port_, std::make_unique(actor_id(this), 1)); + X.ensure(); + in_udp_server_ = X.move_as_ok(); + X = td::UdpServer::create("udp server", out_port_, std::make_unique(actor_id(this), 2)); + X.ensure(); + out_udp_server_ = X.move_as_ok(); + } +} + +void Receiver::receive_common(td::BufferSlice data) { + if (data.size() <= 32) { + return; + } + + td::Bits256 id; + id.as_slice().copy_from(data.as_slice().truncate(32)); + + if (id.is_zero()) { + receive_from_client(std::move(data)); + } else { + receive_to_client(std::move(data)); + } +} + +void Receiver::receive_from_client(td::BufferSlice data) { + auto F = proxy_->decrypt(std::move(data)); + if (F.is_error()) { + return; + } + auto f = F.move_as_ok(); + + td::IPAddress a; + if (a.init_ipv4_port(td::IPAddress::ipv4_to_str(f.ip), f.port).is_error()) { + return; + } + + td::UdpMessage M; + M.address = a; + M.data = std::move(f.data); + + td::actor::send_closure(out_udp_server_.empty() ? in_udp_server_.get() : out_udp_server_.get(), &td::UdpServer::send, + std::move(M)); +} + +void Receiver::receive_to_client(td::BufferSlice data) { + LOG(DEBUG) << "proxying to " << addr_; + td::UdpMessage M; + M.address = addr_; + M.data = std::move(data); + + td::actor::send_closure(in_udp_server_.empty() ? out_udp_server_.get() : in_udp_server_.get(), &td::UdpServer::send, + std::move(M)); +} + +} // namespace adnl + +} // namespace ton + +int main(int argc, char *argv[]) { + SET_VERBOSITY_LEVEL(verbosity_INFO); + + td::set_default_failure_signal_handler().ensure(); + + std::vector> x; + std::unique_ptr logger_; + SCOPE_EXIT { + td::log_interface = td::default_log_interface; + }; + + std::string config = "/var/ton-work/etc/adnl-proxy.conf.json"; + + td::OptionsParser p; + p.set_description("validator or full node for TON network"); + p.add_option('v', "verbosity", "set verbosity level", [&](td::Slice arg) { + int v = VERBOSITY_NAME(FATAL) + (td::to_integer(arg)); + SET_VERBOSITY_LEVEL(v); + return td::Status::OK(); + }); + p.add_option('h', "help", "prints_help", [&]() { + char b[10240]; + td::StringBuilder sb(td::MutableSlice{b, 10000}); + sb << p; + std::cout << sb.as_cslice().c_str(); + std::exit(2); + return td::Status::OK(); + }); + p.add_option('c', "config", "config file", [&](td::Slice arg) { + config = arg.str(); + return td::Status::OK(); + }); + p.add_option('d', "daemonize", "set SIGHUP", [&]() { + td::set_signal_handler(td::SignalType::HangUp, [](int sig) { +#if TD_DARWIN || TD_LINUX + close(0); + setsid(); +#endif + }).ensure(); + return td::Status::OK(); + }); +#if TD_DARWIN || TD_LINUX + p.add_option('l', "logname", "log to file", [&](td::Slice fname) { + auto F = std::make_unique(); + TRY_STATUS(F->init(fname.str())); + logger_ = std::move(F); + td::log_interface = logger_.get(); + return td::Status::OK(); + }); +#endif + td::uint32 threads = 7; + p.add_option('t', "threads", PSTRING() << "number of threads (default=" << threads << ")", [&](td::Slice fname) { + td::int32 v; + try { + v = std::stoi(fname.str()); + } catch (...) { + return td::Status::Error(ton::ErrorCode::error, "bad value for --threads: not a number"); + } + if (v < 1 || v > 256) { + return td::Status::Error(ton::ErrorCode::error, "bad value for --threads: should be in range [1..256]"); + } + threads = v; + return td::Status::OK(); + }); + p.add_option('u', "user", "change user", [&](td::Slice user) { return td::change_user(user); }); + + p.run(argc, argv).ensure(); + + td::actor::Scheduler scheduler({threads}); + + auto R = [&]() -> td::Status { + TRY_RESULT_PREFIX(conf_data, td::read_file(config), "failed to read: "); + TRY_RESULT_PREFIX(conf_json, td::json_decode(conf_data.as_slice()), "failed to parse json: "); + + ton::ton_api::engine_adnlProxy_config conf; + TRY_STATUS_PREFIX(ton::ton_api::from_json(conf, conf_json.get_object()), "json does not fit TL scheme: "); + + if (!conf.ports_.size()) { + return td::Status::Error("empty config"); + } + + for (auto &y : conf.ports_) { + auto in_port = static_cast(y->in_port_); + auto out_port = static_cast(y->out_port_); + if (!y->proxy_type_) { + return td::Status::Error("empty proxy type"); + } + TRY_RESULT(proxy, ton::adnl::AdnlProxy::create(*y->proxy_type_.get())); + td::IPAddress a; + a.init_ipv4_port(td::IPAddress::ipv4_to_str(y->dst_ip_), static_cast(y->dst_port_)).ensure(); + + scheduler.run_in_context([&] { + x.push_back(td::actor::create_actor("adnl-proxy", in_port, out_port, std::move(proxy), a)); + }); + } + + return td::Status::OK(); + }(); + + if (R.is_error()) { + LOG(FATAL) << "bad config: " << R.move_as_error(); + } + + while (scheduler.run(1)) { + } +} diff --git a/adnl/adnl-query.cpp b/adnl/adnl-query.cpp new file mode 100644 index 00000000..d2f0f52d --- /dev/null +++ b/adnl/adnl-query.cpp @@ -0,0 +1,44 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "adnl-query.h" +#include "common/errorcode.h" +#include "td/utils/Random.h" + +namespace ton { + +namespace adnl { + +void AdnlQuery::alarm() { + promise_.set_error(td::Status::Error(ErrorCode::timeout, "adnl query timeout")); + stop(); +} +void AdnlQuery::result(td::BufferSlice data) { + promise_.set_value(std::move(data)); + stop(); +} + +AdnlQueryId AdnlQuery::random_query_id() { + AdnlQueryId q_id; + td::Random::secure_bytes(q_id.as_slice()); + return q_id; +} + +} // namespace adnl + +} // namespace ton diff --git a/adnl/adnl-query.h b/adnl/adnl-query.h new file mode 100644 index 00000000..e900c699 --- /dev/null +++ b/adnl/adnl-query.h @@ -0,0 +1,67 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +#include "td/actor/actor.h" +#include "common/bitstring.h" +#include "td/utils/buffer.h" + +#include + +namespace ton { + +namespace adnl { + +class AdnlPeerPair; + +using AdnlQueryId = td::Bits256; + +class AdnlQuery : public td::actor::Actor { + public: + static td::actor::ActorId create(td::Promise promise, + std::function destroy, std::string name, + td::Timestamp timeout, AdnlQueryId id) { + return td::actor::create_actor("query", name, std::move(promise), std::move(destroy), timeout, id) + .release(); + } + static AdnlQueryId random_query_id(); + AdnlQuery(std::string name, td::Promise promise, std::function destroy, + td::Timestamp timeout, AdnlQueryId id) + : name_(std::move(name)), timeout_(timeout), promise_(std::move(promise)), destroy_(std::move(destroy)), id_(id) { + } + void alarm() override; + void result(td::BufferSlice data); + void start_up() override { + alarm_timestamp() = timeout_; + } + void tear_down() override { + destroy_(id_); + } + + private: + std::string name_; + td::Timestamp timeout_; + td::Promise promise_; + std::function destroy_; + AdnlQueryId id_; +}; + +} // namespace adnl + +} // namespace ton diff --git a/adnl/adnl-static-nodes.cpp b/adnl/adnl-static-nodes.cpp new file mode 100644 index 00000000..edb7efb3 --- /dev/null +++ b/adnl/adnl-static-nodes.cpp @@ -0,0 +1,55 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "adnl-static-nodes.h" +#include "adnl-static-nodes.hpp" + +#include "utils.hpp" + +namespace ton { + +namespace adnl { + +void AdnlStaticNodesManagerImpl::add_node(AdnlNode node) { + auto id_short = node.compute_short_id(); + VLOG(ADNL_INFO) << "[staticnodes] adding static node " << id_short; + + nodes_.emplace(id_short, std::move(node)); +} + +void AdnlStaticNodesManagerImpl::del_node(AdnlNodeIdShort id) { + nodes_.erase(id); +} + +td::Result AdnlStaticNodesManagerImpl::get_node(AdnlNodeIdShort id) { + auto it = nodes_.find(id); + if (it == nodes_.end()) { + return td::Status::Error(ErrorCode::notready, "static node not found"); + } + + return it->second; +} + +td::actor::ActorOwn AdnlStaticNodesManager::create() { + auto X = td::actor::create_actor("staticnodesmanager"); + return td::actor::ActorOwn(std::move(X)); +} + +} // namespace adnl + +} // namespace ton diff --git a/adnl/adnl-static-nodes.h b/adnl/adnl-static-nodes.h new file mode 100644 index 00000000..3ca06f15 --- /dev/null +++ b/adnl/adnl-static-nodes.h @@ -0,0 +1,43 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +#include "td/actor/actor.h" +#include "td/utils/Status.h" +#include "td/actor/PromiseFuture.h" +#include "auto/tl/ton_api.h" + +#include "adnl-peer-table.h" + +namespace ton { + +namespace adnl { + +class AdnlStaticNodesManager : public td::actor::Actor { + public: + virtual void add_node(AdnlNode node) = 0; + virtual void del_node(AdnlNodeIdShort id) = 0; + virtual td::Result get_node(AdnlNodeIdShort id) = 0; + + static td::actor::ActorOwn create(); +}; + +} // namespace adnl + +} // namespace ton diff --git a/adnl/adnl-static-nodes.hpp b/adnl/adnl-static-nodes.hpp new file mode 100644 index 00000000..51ecbb8e --- /dev/null +++ b/adnl/adnl-static-nodes.hpp @@ -0,0 +1,42 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +#include +#include "adnl-static-nodes.h" + +namespace ton { + +namespace adnl { + +class AdnlStaticNodesManagerImpl : public AdnlStaticNodesManager { + public: + void add_node(AdnlNode node) override; + void del_node(AdnlNodeIdShort id) override; + td::Result get_node(AdnlNodeIdShort id) override; + AdnlStaticNodesManagerImpl() { + } + + private: + std::map nodes_; +}; + +} // namespace adnl + +} // namespace ton diff --git a/adnl/adnl-test-loopback-implementation.cpp b/adnl/adnl-test-loopback-implementation.cpp new file mode 100644 index 00000000..1ba0e920 --- /dev/null +++ b/adnl/adnl-test-loopback-implementation.cpp @@ -0,0 +1,38 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "adnl-test-loopback-implementation.h" + +namespace ton { + +namespace adnl { + +AdnlAddressList TestLoopbackNetworkManager::generate_dummy_addr_list(bool empty) { + auto obj = ton::create_tl_object(1, 1); + auto objv = std::vector>(); + objv.push_back(std::move(obj)); + td::uint32 now = Adnl::adnl_start_time(); + auto addrR = ton::adnl::AdnlAddressList::create( + ton::create_tl_object(std::move(objv), empty ? 0 : now, empty ? 0 : now, 0, 0)); + addrR.ensure(); + return addrR.move_as_ok(); +} + +} // namespace adnl + +} // namespace ton diff --git a/adnl/adnl-test-loopback-implementation.h b/adnl/adnl-test-loopback-implementation.h new file mode 100644 index 00000000..40701d52 --- /dev/null +++ b/adnl/adnl-test-loopback-implementation.h @@ -0,0 +1,86 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +#include "adnl/adnl.h" +#include "td/utils/Random.h" + +#include + +namespace ton { + +namespace adnl { + +class TestLoopbackNetworkManager : public ton::adnl::AdnlNetworkManager { + public: + void install_callback(std::unique_ptr callback) override { + CHECK(!callback_); + callback_ = std::move(callback); + } + + void add_self_addr(td::IPAddress addr, td::uint32 priority) override { + } + void add_proxy_addr(td::IPAddress addr, std::shared_ptr proxy, td::uint32 priority) override { + } + void send_udp_packet(ton::adnl::AdnlNodeIdShort src_id, ton::adnl::AdnlNodeIdShort dst_id, td::IPAddress dst_addr, + td::uint32 priority, td::BufferSlice data) override { + if (allowed_sources_.count(src_id) == 0 || allowed_destinations_.count(dst_id) == 0) { + // just drop + return; + } + if (loss_probability_ > 0 && td::Random::fast(0, 10000) < loss_probability_ * 10000) { + return; + } + CHECK(callback_); + callback_->receive_packet(dst_addr, std::move(data)); + } + + void add_node_id(AdnlNodeIdShort id, bool allow_send, bool allow_receive) { + if (allow_send) { + allowed_sources_.insert(id); + } else { + allowed_sources_.erase(id); + } + if (allow_receive) { + allowed_destinations_.insert(id); + } else { + allowed_destinations_.erase(id); + } + } + + void set_loss_probability(double p) { + CHECK(p >= 0 && p <= 1); + loss_probability_ = p; + } + + TestLoopbackNetworkManager() { + } + + static AdnlAddressList generate_dummy_addr_list(bool empty = false); + + private: + std::set allowed_sources_; + std::set allowed_destinations_; + std::unique_ptr callback_; + double loss_probability_ = 0.0; +}; + +} // namespace adnl + +} // namespace ton diff --git a/adnl/adnl.h b/adnl/adnl.h new file mode 100644 index 00000000..30790aae --- /dev/null +++ b/adnl/adnl.h @@ -0,0 +1,120 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +#include "td/actor/actor.h" +#include "auto/tl/ton_api.h" +#include "td/utils/port/IPAddress.h" +#include "adnl-node-id.hpp" +#include "adnl-node.h" +#include "common/errorcode.h" +#include "keyring/keyring.h" + +namespace ton { + +namespace dht { +class Dht; +} + +namespace adnl { + +class AdnlNetworkManager; + +class AdnlExtServer : public td::actor::Actor { + public: + virtual void add_local_id(AdnlNodeIdShort id) = 0; + virtual void add_tcp_port(td::uint16 port) = 0; + virtual ~AdnlExtServer() = default; +}; + +class AdnlSenderInterface : public td::actor::Actor { + public: + virtual ~AdnlSenderInterface() = default; + + virtual void send_message(AdnlNodeIdShort src, AdnlNodeIdShort dst, td::BufferSlice data) = 0; + + virtual void send_query(AdnlNodeIdShort src, AdnlNodeIdShort dst, std::string name, + td::Promise promise, td::Timestamp timeout, td::BufferSlice data) = 0; + virtual void send_query_ex(AdnlNodeIdShort src, AdnlNodeIdShort dst, std::string name, + td::Promise promise, td::Timestamp timeout, td::BufferSlice data, + td::uint64 max_answer_size) = 0; +}; + +class Adnl : public AdnlSenderInterface { + public: + class Callback { + public: + virtual void receive_message(AdnlNodeIdShort src, AdnlNodeIdShort dst, td::BufferSlice data) = 0; + virtual void receive_query(AdnlNodeIdShort src, AdnlNodeIdShort dst, td::BufferSlice data, + td::Promise promise) = 0; + virtual ~Callback() = default; + }; + + static constexpr td::uint32 get_mtu() { + return 1024; + } + static constexpr td::uint32 huge_packet_max_size() { + return 1024 * 8; + } + + // adds node to peer table + // used mostly from DHT to avoid loops + virtual void add_peer(AdnlNodeIdShort local_id, AdnlNodeIdFull id, AdnlAddressList addr_list) = 0; + + // adds address list for nodes from config + virtual void add_static_nodes_from_config(AdnlNodesList nodes) = 0; + + // adds local id. After that you can send/receive messages from/to this id + virtual void add_id(AdnlNodeIdFull id, AdnlAddressList addr_list) = 0; + virtual void del_id(AdnlNodeIdShort id, td::Promise promise) = 0; + + // subscribe to (some) messages(+queries) to this local id + virtual void subscribe(AdnlNodeIdShort dst, std::string prefix, std::unique_ptr callback) = 0; + virtual void unsubscribe(AdnlNodeIdShort dst, std::string prefix) = 0; + + // register (main) dht node + // it will be used to send queries to DHT from adnl + // there are two types of queries: + // - discover node addr list for unknown node + // - update local node information + virtual void register_dht_node(td::actor::ActorId dht_node) = 0; + virtual void register_network_manager(td::actor::ActorId network_manager) = 0; + + // get local id information + // for example when you need to sent it further + virtual void get_addr_list(AdnlNodeIdShort id, td::Promise promise) = 0; + virtual void get_self_node(AdnlNodeIdShort id, td::Promise promise) = 0; + + virtual void create_ext_server(std::vector ids, std::vector ports, + td::Promise> promise) = 0; + + static td::actor::ActorOwn create(std::string db, td::actor::ActorId keyring); + + static std::string int_to_bytestring(td::int32 id) { + return std::string(reinterpret_cast(&id), 4); + } + + static td::int32 adnl_start_time(); +}; + +} // namespace adnl + +using Adnl = adnl::Adnl; + +} // namespace ton diff --git a/adnl/test/adnl-test-ping.cpp b/adnl/test/adnl-test-ping.cpp new file mode 100644 index 00000000..593f1226 --- /dev/null +++ b/adnl/test/adnl-test-ping.cpp @@ -0,0 +1,240 @@ +/* + This file is part of TON Blockchain source code. + + TON Blockchain is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + TON Blockchain is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with TON Blockchain. If not, see . + + In addition, as a special exception, the copyright holders give permission + to link the code of portions of this program with the OpenSSL library. + You must obey the GNU General Public License in all respects for all + of the code used other than OpenSSL. If you modify file(s) with this + exception, you may extend this exception to your version of the file(s), + but you are not obligated to do so. If you do not wish to do so, delete this + exception statement from your version. If you delete this exception statement + from all source files in the program, then also delete it here. + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "adnl/adnl-network-manager.h" +#include "adnl/adnl-peer-table.h" +#include "adnl/utils.hpp" +#include "keys/encryptor.h" +#include "td/utils/Time.h" +#include "td/utils/format.h" +#include "td/utils/OptionsParser.h" + +#include +#include + +template +std::ostream &operator<<(std::ostream &stream, const td::UInt &x) { + for (size_t i = 0; i < size / 8; i++) { + stream << td::format::hex_digit((x.raw[i] >> 4) & 15) << td::format::hex_digit(x.raw[i] & 15); + } + + return stream; +} + +class AdnlNode : public td::actor::Actor { + private: + std::vector ping_ids_; + + td::actor::ActorOwn network_manager_; + td::actor::ActorOwn peer_table_; + + td::UInt256 local_id_; + bool local_id_set_ = false; + + std::string host_ = "127.0.0.1"; + td::uint32 ip_ = 0x7f000001; + td::uint16 port_ = 2380; + + void receive_message(td::UInt256 src, td::UInt256 dst, td::BufferSlice data) { + std::cout << "MESSAGE FROM " << src << " to " << dst << " of size " << std::to_string(data.size()) << "\n"; + } + + void receive_query(td::UInt256 src, td::UInt256 dst, td::uint64 query_id, td::BufferSlice data) { + std::cout << "QUERY " << std::to_string(query_id) << " FROM " << src << " to " << dst << " of size " + << std::to_string(data.size()) << "\n"; + td::actor::send_closure(peer_table_, &ton::AdnlPeerTable::answer_query, dst, src, query_id, + ton::create_tl_object()); + } + + std::unique_ptr make_callback() { + class Callback : public ton::AdnlPeerTable::Callback { + public: + void receive_message(td::UInt256 src, td::UInt256 dst, td::BufferSlice data) override { + td::actor::send_closure(id_, &AdnlNode::receive_message, src, dst, std::move(data)); + } + void receive_query(td::UInt256 src, td::UInt256 dst, td::uint64 query_id, td::BufferSlice data) override { + td::actor::send_closure(id_, &AdnlNode::receive_query, src, dst, query_id, std::move(data)); + } + Callback(td::actor::ActorId id) : id_(std::move(id)) { + } + + private: + td::actor::ActorId id_; + }; + + return std::make_unique(td::actor::actor_id(this)); + } + + public: + void start_up() override { + alarm_timestamp() = td::Timestamp::in(1); + } + AdnlNode() { + network_manager_ = ton::AdnlNetworkManager::create(); + peer_table_ = ton::AdnlPeerTable::create(); + td::actor::send_closure(network_manager_, &ton::AdnlNetworkManager::register_peer_table, peer_table_.get()); + td::actor::send_closure(peer_table_, &ton::AdnlPeerTable::register_network_manager, network_manager_.get()); + } + void listen_udp(td::uint16 port) { + td::actor::send_closure(network_manager_, &ton::AdnlNetworkManager::add_listening_udp_port, "0.0.0.0", port); + port_ = port; + } + void set_host(td::IPAddress ip, std::string host) { + ip_ = ip.get_ipv4(); + host_ = host; + } + void send_pings_to(td::UInt256 id) { + std::cout << "send pings to " << id << "\n"; + ping_ids_.push_back(id); + } + void add_local_id(ton::tl_object_ptr pk_) { + auto pub_ = ton::get_public_key(pk_); + local_id_ = ton::adnl_short_id(pub_); + std::cout << "local_id = '" << local_id_ << "'\n"; + auto x = ton::create_tl_object(ip_, port_); + auto v = std::vector>(); + v.push_back(ton::move_tl_object_as(x)); + auto y = + ton::create_tl_object(std::move(v), static_cast(td::Time::now())); + + LOG(INFO) << "local_addr_list: " << ton::ton_api::to_string(y); + td::actor::send_closure(peer_table_, &ton::AdnlPeerTable::add_id, std::move(pk_), std::move(y)); + td::actor::send_closure(peer_table_, &ton::AdnlPeerTable::subscribe, local_id_, "", make_callback()); + local_id_set_ = true; + } + + void add_foreign(ton::tl_object_ptr id, + ton::tl_object_ptr addr_list) { + std::cout << ton::adnl_short_id(id) << "\n"; + td::actor::send_closure(peer_table_, &ton::AdnlPeerTable::add_peer, std::move(id), std::move(addr_list)); + } + + void alarm() override { + std::cout << "alarm\n"; + if (local_id_set_) { + for (auto it = ping_ids_.begin(); it != ping_ids_.end(); it++) { + auto P = td::PromiseCreator::lambda([](td::Result result) { + if (result.is_error()) { + std::cout << "received error " << result.move_as_error().to_string() << "\n"; + } else { + auto message = result.move_as_ok(); + std::cout << "received answer to query\n"; + } + }); + td::actor::send_closure(peer_table_, &ton::AdnlPeerTable::send_query, local_id_, *it, std::move(P), + td::Timestamp::in(5), ton::create_tl_object()); + } + } + + alarm_timestamp() = td::Timestamp::in(1); + } +}; + +td::Result get_uint256(std::string str) { + if (str.size() != 64) { + return td::Status::Error("uint256 must have 64 bytes"); + } + td::UInt256 res; + for (size_t i = 0; i < 32; i++) { + res.raw[i] = static_cast(td::hex_to_int(str[2 * i]) * 16 + td::hex_to_int(str[2 * i + 1])); + } + return res; +} + +int main(int argc, char *argv[]) { + td::actor::ActorOwn x; + + td::OptionsParser p; + p.set_description("test basic adnl functionality"); + p.add_option('h', "help", "prints_help", [&]() { + char b[10240]; + td::StringBuilder sb({b, 10000}); + sb << p; + std::cout << sb.as_cslice().c_str(); + std::exit(2); + return td::Status::OK(); + }); + p.add_option('p', "port", "sets udp port", [&](td::Slice port) { + td::actor::send_closure(x, &AdnlNode::listen_udp, static_cast(std::stoi(port.str()))); + return td::Status::OK(); + }); + p.add_option('a', "host", "sets local ip", [&](td::Slice ip) { + td::IPAddress addr; + auto R = addr.init_host_port(ip.str(), 0); + if (R.is_error()) { + return R; + } + td::actor::send_closure(x, &AdnlNode::set_host, addr, ip.str()); + return td::Status::OK(); + }); + p.add_option('i', "id", "sets local id", [&](td::Slice id) { + td::actor::send_closure(x, &AdnlNode::add_local_id, + ton::create_tl_object(id.str())); + return td::Status::OK(); + }); + p.add_option('P', "peer", "adds peer id@host:port", [&](td::Slice id) { + auto pos = id.rfind('@'); + if (pos == static_cast(-1)) { + return td::Status::Error("--peer expected randomtag@host:port as argument"); + } + auto s1 = id.substr(0, pos); + auto f_id = ton::create_tl_object(s1.str()); + td::IPAddress addr; + auto R = addr.init_host_port(td::CSlice(id.substr(pos + 1).str())); + if (R.is_error()) { + return R.move_as_error(); + } + + auto f_addr = ton::create_tl_object(addr.get_ipv4(), addr.get_port()); + std::vector> vv; + vv.push_back(ton::move_tl_object_as(f_addr)); + + auto f_addr_list = + ton::create_tl_object(std::move(vv), static_cast(td::Time::now())); + + td::actor::send_closure(x, &AdnlNode::add_foreign, ton::move_tl_object_as(f_id), + std::move(f_addr_list)); + + return td::Status::OK(); + }); + p.add_option('n', "node", "node to send pings to", [&](td::Slice node) { + auto R = get_uint256(node.str()); + if (R.is_error()) { + return R.move_as_error(); + } + + td::actor::send_closure(x, &AdnlNode::send_pings_to, R.move_as_ok()); + return td::Status::OK(); + }); + + td::actor::Scheduler scheduler({2}); + scheduler.run_in_context([&] { + x = td::actor::create_actor(td::actor::ActorInfoCreator::Options().with_name("A").with_poll()); + }); + scheudler.run(); + return 0; +} diff --git a/adnl/utils.cpp b/adnl/utils.cpp new file mode 100644 index 00000000..9fe93bb1 --- /dev/null +++ b/adnl/utils.cpp @@ -0,0 +1,27 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "utils.hpp" +#include "tl/tl_object_store.h" +#include "td/utils/format.h" +#include "td/utils/Random.h" +#include "keys/encryptor.h" +#include "auto/tl/ton_api.hpp" +#include "adnl-node-id.hpp" + +namespace ton {} // namespace ton diff --git a/adnl/utils.hpp b/adnl/utils.hpp new file mode 100644 index 00000000..e0008260 --- /dev/null +++ b/adnl/utils.hpp @@ -0,0 +1,45 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +#include "td/utils/buffer.h" +#include "td/utils/misc.h" +#include "td/utils/crypto.h" +#include "td/utils/format.h" +#include "td/utils/base64.h" +#include "tl-utils/tl-utils.hpp" + +#include "common/errorcode.h" +#include "common/checksum.h" +#include "adnl-node-id.hpp" +#include "common/status.h" +#include "adnl-node.h" +#include "adnl-address-list.hpp" + +namespace ton { + +namespace adnl { + +inline bool adnl_node_is_older(AdnlNode &a, AdnlNode &b) { + return a.addr_list().version() < b.addr_list().version(); +} + +} // namespace adnl + +} // namespace ton diff --git a/blockchain-explorer/CMakeLists.txt b/blockchain-explorer/CMakeLists.txt new file mode 100644 index 00000000..0d02f01f --- /dev/null +++ b/blockchain-explorer/CMakeLists.txt @@ -0,0 +1,22 @@ +cmake_minimum_required(VERSION 3.0.2 FATAL_ERROR) + + +find_package(MHD) + +if (MHD_FOUND) + + set(BLOCHAIN_EXPLORER_SOURCE + blockchain-explorer.cpp + blockchain-explorer.hpp + blockchain-explorer-http.cpp + blockchain-explorer-http.hpp + blockchain-explorer-query.cpp + blockchain-explorer-query.hpp + ) + + add_executable(blockchain-explorer ${BLOCHAIN_EXPLORER_SOURCE}) + target_include_directories(blockchain-explorer PUBLIC ${MHD_INCLUDE_DIRS}) + target_link_libraries(blockchain-explorer tdutils tdactor adnllite tl_lite_api tl-lite-utils + ton_crypto ton_block ${MHD_LIBRARY}) + +endif() diff --git a/blockchain-explorer/blockchain-explorer-http.cpp b/blockchain-explorer/blockchain-explorer-http.cpp new file mode 100644 index 00000000..5d02c454 --- /dev/null +++ b/blockchain-explorer/blockchain-explorer-http.cpp @@ -0,0 +1,695 @@ +/* + This file is part of TON Blockchain source code. + + TON Blockchain is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + TON Blockchain is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with TON Blockchain. If not, see . + + In addition, as a special exception, the copyright holders give permission + to link the code of portions of this program with the OpenSSL library. + You must obey the GNU General Public License in all respects for all + of the code used other than OpenSSL. If you modify file(s) with this + exception, you may extend this exception to your version of the file(s), + but you are not obligated to do so. If you do not wish to do so, delete this + exception statement from your version. If you delete this exception statement + from all source files in the program, then also delete it here. + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "blockchain-explorer-http.hpp" +#include "block/block-db.h" +#include "block/block.h" +#include "block/block-parse.h" +#include "block/block-auto.h" +#include "vm/boc.h" +#include "vm/cellops.h" +#include "vm/cells/MerkleProof.h" +#include "block/mc-config.h" +#include "ton/ton-shard.h" + +HttpAnswer& HttpAnswer::operator<<(AddressCell addr_c) { + ton::WorkchainId wc; + ton::StdSmcAddress addr; + if (!block::tlb::t_MsgAddressInt.extract_std_address(addr_c.root, wc, addr)) { + abort(""); + return *this; + } + block::StdAddress caddr{wc, addr}; + *this << "" << caddr.rserialize(true) << ""; + return *this; +} + +HttpAnswer& HttpAnswer::operator<<(MessageCell msg) { + if (msg.root.is_null()) { + abort(" src, dest; + *this << "
get_hash() << "\">"; + *this << "
\n" + << "\n" + << "\n"; + switch (block::gen::t_CommonMsgInfo.get_tag(cs)) { + case block::gen::CommonMsgInfo::ext_in_msg_info: { + block::gen::CommonMsgInfo::Record_ext_in_msg_info info; + if (!tlb::unpack(cs, info)) { + abort(""); + return *this; + } + *this << "\n" + << "\n" + << "\n"; + break; + } + case block::gen::CommonMsgInfo::ext_out_msg_info: { + block::gen::CommonMsgInfo::Record_ext_out_msg_info info; + if (!tlb::unpack(cs, info)) { + abort(""); + return *this; + } + *this << "\n" + << "\n" + << "\n" + << "\n" + << "\n"; + break; + } + case block::gen::CommonMsgInfo::int_msg_info: { + block::gen::CommonMsgInfo::Record_int_msg_info info; + if (!tlb::unpack(cs, info)) { + abort("cannot unpack internal message"); + return *this; + } + td::RefInt256 value; + td::Ref extra; + if (!block::unpack_CurrencyCollection(info.value, value, extra)) { + abort("cannot unpack message value"); + return *this; + } + *this << "\n" + << "\n" + << "\n" + << "\n" + << "\n" + << "\n"; + break; + } + default: + abort("cannot unpack message"); + return *this; + } + + *this << "
hash" << msg.root->get_hash().to_hex() << "
typeexternal
sourceNONE
destination" << AddressCell{info.dest} << "
typeexternal OUT
source" << AddressCell{info.src} << "
destinationNONE
lt" << info.created_lt << "
time" << info.created_at << "
typeinternal
source" << AddressCell{info.src} << "
destination" << AddressCell{info.dest} << "
lt" << info.created_lt << "
time" << info.created_at << "
value" << value << "
\n"; + *this << RawData{msg.root, block::gen::t_Anything} << "
"; + return *this; +} + +HttpAnswer& HttpAnswer::operator<<(ton::BlockIdExt block_id) { + return *this << "" << block_id.id.to_str() << ""; +} + +HttpAnswer& HttpAnswer::operator<<(ton::BlockId block_id) { + return *this << "" + << block_id.to_str() << ""; +} + +HttpAnswer& HttpAnswer::operator<<(BlockSearch bs) { + *this << "
" + << "
" + << "
" + << "" + << "" + << "
\n" + << "
" + << "" + << "
" + << "
" + << "" + << "
" + << "
" + << "" + << "
" + << "
" + << "
" + << "" + << "" + << "
\n" + << "
" + << "" + << "
" + << "
" + << "
" + << "" + << "
" + << "
" + << "" + << "
" + << "
\n"; + return *this; +} + +HttpAnswer& HttpAnswer::operator<<(AccountSearch bs) { + *this << "
" + << "
" + << "
" + << "" + << "
" + << "
" + << "" + << "
" + << "
" + << "" + << "
" + << "
" + << "" + << "
" + << "
" + << "
" + << "
" + << "" + << "
" + << "
" + << "" + << "
" + << "
" + << "
" + << "" + << "
" + << "
\n" + << "
\n"; + return *this; +} + +HttpAnswer& HttpAnswer::operator<<(TransactionSearch bs) { + *this << "
" + << "
" + << "
" + << "" + << "
" + << "
" + << "" + << "
" + << "
" + << "" + << "
" + << "
" + << "" + << "
" + << "
" + << "
" + << "
" + << "" + << "
" + << "
" + << "" + << "
" + << "
" + << "
" + << "" + << "
" + << "
" + << "
" + << "" + << "
" + << "
" + << "" + << "
" + << "
\n" + << "
\n"; + return *this; +} + +HttpAnswer& HttpAnswer::operator<<(TransactionCell trans_c) { + if (trans_c.root.is_null()) { + abort("transaction not found"); + return *this; + } + block::gen::Transaction::Record trans; + if (!tlb::unpack_cell(trans_c.root, trans)) { + abort("cannot unpack"); + return *this; + } + *this << "
\n" + << "\n" + << "" + << "" + << "" + << "" + << "\n" + << "\n" + << "\n" + << "\n" + << "\n" + << "
block" + << trans_c.block_id.id.to_str() << "
workchain" << trans_c.addr.workchain << "
account hex" << trans_c.addr.addr.to_hex() << "
account" << trans_c.addr.rserialize(true) << "
hash" << trans_c.root->get_hash().to_hex() << "
lt" << trans.lt << "
time" << trans.now << "
out messages"; + vm::Dictionary dict{trans.r1.out_msgs, 15}; + for (td::int32 i = 0; i < trans.outmsg_cnt; i++) { + auto out_msg = dict.lookup_ref(td::BitArray<15>{i}); + *this << " " << i << ""; + } + *this << "
in message"; + auto in_msg = trans.r1.in_msg->prefetch_ref(); + if (in_msg.is_null()) { + *this << "NONE"; + } else { + *this << "" << in_msg->get_hash() << ""; + } + *this << "
prev transaction"; + + auto prev_lt = trans.prev_trans_lt; + auto prev_hash = trans.prev_trans_hash; + if (prev_lt > 0) { + *this << "lt=" << prev_lt + << " hash=" << prev_hash.to_hex() << ""; + } else { + *this << "NONE"; + } + *this << "
\n"; + if (in_msg.not_null()) { + *this << "
" << MessageCell{in_msg}; + } + for (int x = 0; x < trans.outmsg_cnt && x < 100; x++) { + auto out_msg = dict.lookup_ref(td::BitArray<15>{x}); + *this << "
" << MessageCell{out_msg}; + } + *this << "
"; + + return *this << RawData{trans_c.root} << ""; +} + +HttpAnswer& HttpAnswer::operator<<(AccountCell acc_c) { + *this << "
"; + auto block_id = acc_c.block_id; + if (!block_id.is_valid_full()) { + abort(PSTRING() << "shard block id " << block_id.to_str() << " in answer is invalid"); + return *this; + } + if (!ton::shard_contains(block_id.shard_full(), ton::extract_addr_prefix(acc_c.addr.workchain, acc_c.addr.addr))) { + abort(PSTRING() << "received data from shard block " << block_id.to_str() + << " that cannot contain requested account " << acc_c.addr.workchain << ":" + << acc_c.addr.addr.to_hex()); + return *this; + } + if (acc_c.q_roots.size() != 2) { + abort(PSTRING() << "account state proof must have exactly two roots"); + return *this; + } + ton::LogicalTime last_trans_lt = 0; + ton::Bits256 last_trans_hash; + last_trans_hash.set_zero(); + try { + auto state_root = vm::MerkleProof::virtualize(acc_c.q_roots[1], 1); + if (state_root.is_null()) { + abort("account state proof is invalid"); + return *this; + } + block::gen::ShardStateUnsplit::Record sstate; + if (!(tlb::unpack_cell(std::move(state_root), sstate))) { + abort("cannot unpack state header"); + return *this; + } + vm::AugmentedDictionary accounts_dict{vm::load_cell_slice_ref(sstate.accounts), 256, block::tlb::aug_ShardAccounts}; + auto acc_csr = accounts_dict.lookup(acc_c.addr.addr); + if (acc_csr.not_null()) { + if (acc_c.root.is_null()) { + abort(PSTRING() << "account state proof shows that account state for " << acc_c.addr.workchain << ":" + << acc_c.addr.addr.to_hex() << " must be non-empty, but it actually is empty"); + return *this; + } + block::gen::ShardAccount::Record acc_info; + if (!tlb::csr_unpack(std::move(acc_csr), acc_info)) { + abort("cannot unpack ShardAccount from proof"); + return *this; + } + if (acc_info.account->get_hash().bits().compare(acc_c.root->get_hash().bits(), 256)) { + abort(PSTRING() << "account state hash mismatch: Merkle proof expects " + << acc_info.account->get_hash().bits().to_hex(256) << " but received data has " + << acc_c.root->get_hash().bits().to_hex(256)); + return *this; + } + last_trans_hash = acc_info.last_trans_hash; + last_trans_lt = acc_info.last_trans_lt; + } else if (acc_c.root.not_null()) { + abort(PSTRING() << "account state proof shows that account state for " << acc_c.addr.workchain << ":" + << acc_c.addr.addr.to_hex() << " must be empty, but it is not"); + return *this; + } + } catch (vm::VmError err) { + abort(PSTRING() << "error while traversing account proof : " << err.get_msg()); + return *this; + } catch (vm::VmVirtError err) { + abort(PSTRING() << "virtualization error while traversing account proof : " << err.get_msg()); + return *this; + } + + *this << "
\n" + << "\n"; + *this << ""; + *this << ""; + *this << ""; + *this << ""; + if (last_trans_lt > 0) { + *this << "\n"; + } else { + *this << ""; + } + *this << "
block" + << block_id.id.to_str() << "
workchain" << acc_c.addr.workchain << "
account hex" << acc_c.addr.addr.to_hex() << "
account" << acc_c.addr.rserialize(true) << "
last transaction" + << "lt=" << last_trans_lt + << " hash=" << last_trans_hash.to_hex() << "
last transactionno transactions
\n"; + + *this << "

go to current state

\n"; + + if (acc_c.root.not_null()) { + *this << RawData{acc_c.root}; + } else { + *this << "
account state is empty
"; + } + return *this << "
"; +} + +HttpAnswer& HttpAnswer::operator<<(BlockHeaderCell head_c) { + *this << "
"; + vm::CellSlice cs{vm::NoVm{}, head_c.root}; + auto block_id = head_c.block_id; + try { + auto virt_root = vm::MerkleProof::virtualize(head_c.root, 1); + if (virt_root.is_null()) { + abort("invalid merkle proof"); + return *this; + } + ton::RootHash vhash{virt_root->get_hash().bits()}; + std::vector prev; + ton::BlockIdExt mc_blkid; + bool after_split; + auto res = block::unpack_block_prev_blk_ext(virt_root, block_id, prev, mc_blkid, after_split); + if (res.is_error()) { + abort(PSTRING() << "cannot unpack header for block " << block_id.to_str() << ": " << res); + return *this; + } + block::gen::Block::Record blk; + block::gen::BlockInfo::Record info; + if (!(tlb::unpack_cell(virt_root, blk) && tlb::unpack_cell(blk.info, info))) { + abort(PSTRING() << "cannot unpack header for block " << block_id.to_str()); + return *this; + } + bool before_split = info.before_split; + *this << "
\n" + << "\n" + << "\n" + << "\n" + << "\n" + << "\n" + << "\n" + << "\n" + << "\n" + << "\n" + << "\n" + << "\n" + << "\n" + << "\n" + << "\n" + << "\n" + << "\n" + << "\n"; + for (auto id : prev) { + *this << "\n"; + } + if (!before_split) { + *this << "\n"; + } else { + *this << "\n"; + *this << "\n"; + } + *this << "\n" + << "
block" << block_id.id.to_str() << "
roothash" << block_id.root_hash.to_hex() << "
filehash" << block_id.file_hash.to_hex() << "
time" << info.gen_utime << "
lt" << info.start_lt << " .. " << info.end_lt + << "
global_id" << blk.global_id << "
version" << info.version << "
not_master" << info.not_master << "
after_merge" << info.after_merge << "
after_split" << info.after_split << "
before_split" << info.before_split << "
want_merge" << info.want_merge << "
want_split" << info.want_split << "
validator_list_hash_short" + << info.gen_validator_list_hash_short << "
catchain_seqno" << info.gen_catchain_seqno << "
min_ref_mc_seqno" << info.min_ref_mc_seqno + << "
prev block" << id << "
next block" + << ton::BlockId{block_id.id.workchain, block_id.id.shard, block_id.id.seqno + 1} << "
next block" + << ton::BlockId{block_id.id.workchain, ton::shard_child(block_id.id.shard, true), block_id.id.seqno + 1} + << "
next block" + << ton::BlockId{block_id.id.workchain, ton::shard_child(block_id.id.shard, false), block_id.id.seqno + 1} + << "
masterchain block" << mc_blkid << "
"; + } catch (vm::VmError err) { + abort(PSTRING() << "error processing header : " << err.get_msg()); + return *this; + } catch (vm::VmVirtError err) { + abort(PSTRING() << "error processing header : " << err.get_msg()); + return *this; + } + + return *this << "

download block" + << "view block\n" + << "

"; +} + +HttpAnswer& HttpAnswer::operator<<(BlockShardsCell shards_c) { + block::ShardConfig sh_conf; + if (!sh_conf.unpack(vm::load_cell_slice_ref(shards_c.root))) { + abort("cannot extract shard block list from shard configuration"); + return *this; + } else { + auto ids = sh_conf.get_shard_hash_ids(true); + + auto workchain = ton::masterchainId; + *this << "
\n" + << "\n\n" + << "\n" + << "\n" + << "" + << "" + << "" + << "" + << "" + << "" + << "" + << "\n" + << "\n"; + for (auto id : ids) { + auto ref = sh_conf.get_shard_hash(ton::ShardIdFull(id)); + + if (id.workchain != workchain) { + if (workchain != ton::masterchainId) { + *this << "\n"; + } + workchain = id.workchain; + } + *this << ""; + ton::ShardIdFull shard{id.workchain, id.shard}; + if (ref.not_null()) { + *this << "" + << "" + << "" + << "" + << ""; + } else { + *this << ""; + } + *this << ""; + } + return *this << "
shardseqnocreatedwantsplitwantmergebeforesplitbeforemerge
" << shard.to_str() << "top_block_id()} + << "\">" << ref->top_block_id().id.seqno << "" << ref->created_at() << "" << ref->want_split_ << "" << ref->want_merge_ << "" << ref->before_split_ << "" << ref->before_merge_ << "" << shard.to_str() << "
"; + } +} + +HttpAnswer& HttpAnswer::operator<<(AccountLink account) { + *this << prefix_ << "account?"; + if (account.block_id.is_valid()) { + block_id_link(account.block_id); + *this << "&"; + } + return *this << "account=" << account.account_id.rserialize(true); +} + +HttpAnswer& HttpAnswer::operator<<(MessageLink msg) { + return *this << "#msg" << msg.root->get_hash(); +} + +HttpAnswer& HttpAnswer::operator<<(TransactionLink trans) { + return *this << prefix_ << "transaction?" + << "account=" << trans.account_id.rserialize(true) << "<=" << trans.lt << "&hash=" << trans.hash; +} + +HttpAnswer& HttpAnswer::operator<<(TransactionLinkShort trans) { + *this << prefix_ << "transaction2?"; + block_id_link(trans.block_id); + return *this << "&account=" << trans.account_id.rserialize(true) << "<=" << trans.lt; +} + +HttpAnswer& HttpAnswer::operator<<(BlockLink block) { + *this << prefix_ << "block?"; + block_id_link(block.block_id); + return *this; +} + +HttpAnswer& HttpAnswer::operator<<(BlockViewLink block) { + *this << prefix_ << "viewblock?"; + block_id_link(block.block_id); + return *this; +} + +HttpAnswer& HttpAnswer::operator<<(BlockDownloadLink block) { + *this << prefix_ << "download?"; + block_id_link(block.block_id); + return *this; +} + +HttpAnswer& HttpAnswer::operator<<(TransactionList trans) { + *this << "
\n" + << "\n\n" + << "\n" + << "\n" + << "" + << "" + << "" + << "" + << "" + << "\n" + << "\n"; + td::uint32 idx = 0; + for (auto& x : trans.vec) { + *this << "" + << "" + << "" + << "" + << ""; + } + if (trans.vec.size() == trans.req_count_) { + *this << "" + << "" + << "" + << ""; + } + return *this << "
seqaccountlthashlink
" << ++idx << "" << x.addr.rserialize(true) << "" << x.lt << "" << x.hash.to_hex() << "view
" << ++idx << "moremoremore
"; +} + +HttpAnswer& HttpAnswer::operator<<(Error error) { + return *this << "
" << error.error.to_string() << "
"; +} + +void HttpAnswer::block_id_link(ton::BlockIdExt block_id) { + *this << "workchain=" << block_id.id.workchain << "&shard=" << ton::shard_to_str(block_id.id.shard) + << "&seqno=" << block_id.id.seqno << "&roothash=" << block_id.root_hash << "&filehash=" << block_id.file_hash; +} + +std::string HttpAnswer::abort(td::Status error) { + if (error_.is_ok()) { + error_ = std::move(error); + } + return header() + "
" + error_.to_string() + "
" + footer(); +} + +std::string HttpAnswer::abort(std::string error) { + return abort(td::Status::Error(404, error)); +} + +std::string HttpAnswer::header() { + sb_->clear(); + *this << "\n" + << "" << title_ << "\n" + << "\n" + << "\n" + << "\n" + << "\n" + << "" + << "\n" + << "\n" + << "\n" + << "\n" + << "\n" + << "\n" + << "
\n" + << "\n"; + + *this << "

\n" + << "\n" + << "Search block\n" + << "\n" + << "\n" + << "Search account\n" + << "\n" + << "\n" + << "Search transaction\n" + << "\n" + << "

\n"; + + *this << "
\n" + << "
\n" + << "
\n" + << BlockSearch{block_id_} << "
\n"; + *this << "
\n" + << "
\n" + << AccountSearch{block_id_, account_id_} << "
\n"; + *this << "
\n" + << "
\n" + << TransactionSearch{block_id_, account_id_, 0, ton::Bits256::zero()} << "
\n"; + + return sb_->as_cslice().c_str(); +} + +std::string HttpAnswer::footer() { + return PSTRING() << "
"; +} + +std::string HttpAnswer::finish() { + if (error_.is_ok()) { + std::string data = sb_->as_cslice().c_str(); + return header() + data + footer(); + } else { + return header() + "
" + error_.to_string() + "
" + footer(); + } +} diff --git a/blockchain-explorer/blockchain-explorer-http.hpp b/blockchain-explorer/blockchain-explorer-http.hpp new file mode 100644 index 00000000..dadd7e17 --- /dev/null +++ b/blockchain-explorer/blockchain-explorer-http.hpp @@ -0,0 +1,222 @@ +/* + This file is part of TON Blockchain source code. + + TON Blockchain is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + TON Blockchain is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with TON Blockchain. If not, see . + + In addition, as a special exception, the copyright holders give permission + to link the code of portions of this program with the OpenSSL library. + You must obey the GNU General Public License in all respects for all + of the code used other than OpenSSL. If you modify file(s) with this + exception, you may extend this exception to your version of the file(s), + but you are not obligated to do so. If you do not wish to do so, delete this + exception statement from your version. If you delete this exception statement + from all source files in the program, then also delete it here. + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +#include "ton/ton-types.h" +#include "vm/boc.h" +#include "vm/cellops.h" +#include "td/utils/Random.h" +#include "block/block.h" + +class HttpAnswer { + public: + struct MessageCell { + td::Ref root; + }; + struct AddressCell { + td::Ref root; + }; + struct TransactionCell { + block::StdAddress addr; + ton::BlockIdExt block_id; + td::Ref root; + }; + struct AccountCell { + block::StdAddress addr; + ton::BlockIdExt block_id; + td::Ref root; + std::vector> q_roots; + }; + struct BlockHeaderCell { + ton::BlockIdExt block_id; + td::Ref root; + }; + struct BlockShardsCell { + ton::BlockIdExt block_id; + td::Ref root; + }; + + struct AccountLink { + block::StdAddress account_id; + ton::BlockIdExt block_id; + }; + struct MessageLink { + td::Ref root; + }; + struct TransactionLink { + block::StdAddress account_id; + ton::LogicalTime lt; + ton::Bits256 hash; + }; + struct TransactionLinkShort { + ton::BlockIdExt block_id; + block::StdAddress account_id; + ton::LogicalTime lt; + }; + struct BlockLink { + ton::BlockIdExt block_id; + }; + struct BlockViewLink { + ton::BlockIdExt block_id; + }; + struct BlockDownloadLink { + ton::BlockIdExt block_id; + }; + struct BlockSearch { + ton::BlockIdExt block_id; + }; + struct AccountSearch { + ton::BlockIdExt block_id; + block::StdAddress addr; + }; + struct TransactionSearch { + ton::BlockIdExt block_id; + block::StdAddress addr; + ton::LogicalTime lt; + ton::Bits256 hash; + }; + struct TransactionList { + struct TransactionDescr { + TransactionDescr(block::StdAddress addr, ton::LogicalTime lt, ton::Bits256 hash) + : addr(addr), lt(lt), hash(hash) { + } + block::StdAddress addr; + ton::LogicalTime lt; + ton::Bits256 hash; + }; + ton::BlockIdExt block_id; + std::vector vec; + td::uint32 req_count_; + }; + struct CodeBlock { + std::string data; + }; + struct Error { + td::Status error; + }; + template + struct RawData { + td::Ref root; + T x; + template + RawData(td::Ref root, Args &&... args) : root(std::move(root)), x(std::forward(args)...) { + } + }; + + public: + HttpAnswer(std::string title, std::string prefix) : title_(title), prefix_(prefix) { + buf_ = td::BufferSlice{1 << 28}; + sb_ = std::make_unique(buf_.as_slice()); + } + + void set_title(std::string title) { + title_ = title; + } + void set_block_id(ton::BlockIdExt block_id) { + block_id_ = block_id; + workchain_id_ = block_id_.id.workchain; + } + void set_account_id(block::StdAddress addr) { + account_id_ = addr; + } + void set_workchain(ton::WorkchainId workchain_id) { + workchain_id_ = workchain_id; + } + + std::string abort(td::Status error); + std::string abort(std::string error); + + std::string finish(); + std::string header(); + std::string footer(); + + template + HttpAnswer &operator<<(T x) { + sb() << x; + return *this; + } + td::StringBuilder &sb() { + return *sb_; + } + HttpAnswer &operator<<(td::Bits256 x) { + sb() << x.to_hex(); + return *this; + } + HttpAnswer &operator<<(td::BitString x) { + sb() << x.to_hex(); + return *this; + } + HttpAnswer &operator<<(AddressCell addr); + HttpAnswer &operator<<(MessageCell msg); + HttpAnswer &operator<<(ton::BlockIdExt block_id); + HttpAnswer &operator<<(ton::BlockId block_id); + HttpAnswer &operator<<(TransactionCell trans); + HttpAnswer &operator<<(AccountCell trans); + HttpAnswer &operator<<(BlockHeaderCell head); + HttpAnswer &operator<<(BlockShardsCell shards); + HttpAnswer &operator<<(BlockSearch head); + HttpAnswer &operator<<(AccountSearch head); + HttpAnswer &operator<<(TransactionSearch head); + + HttpAnswer &operator<<(AccountLink account); + HttpAnswer &operator<<(MessageLink msg); + HttpAnswer &operator<<(TransactionLink trans); + HttpAnswer &operator<<(TransactionLinkShort trans); + HttpAnswer &operator<<(BlockLink block); + HttpAnswer &operator<<(BlockViewLink block); + HttpAnswer &operator<<(BlockDownloadLink block); + + HttpAnswer &operator<<(Error error); + + HttpAnswer &operator<<(TransactionList trans); + HttpAnswer &operator<<(CodeBlock block) { + return *this << "
" << block.data << "
"; + } + + template + HttpAnswer &operator<<(RawData data) { + std::ostringstream outp; + data.x.print_ref(outp, data.root); + vm::load_cell_slice(data.root).print_rec(outp); + return *this << CodeBlock{outp.str()}; + } + + private: + void block_id_link(ton::BlockIdExt block_id); + + std::string title_; + ton::BlockIdExt block_id_; + ton::WorkchainId workchain_id_ = ton::workchainInvalid; + block::StdAddress account_id_; + + std::string prefix_; + td::Status error_; + + std::unique_ptr sb_; + td::BufferSlice buf_; +}; diff --git a/blockchain-explorer/blockchain-explorer-query.cpp b/blockchain-explorer/blockchain-explorer-query.cpp new file mode 100644 index 00000000..770f9b2b --- /dev/null +++ b/blockchain-explorer/blockchain-explorer-query.cpp @@ -0,0 +1,1029 @@ +/* + This file is part of TON Blockchain source code. + + TON Blockchain is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + TON Blockchain is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with TON Blockchain. If not, see . + + In addition, as a special exception, the copyright holders give permission + to link the code of portions of this program with the OpenSSL library. + You must obey the GNU General Public License in all respects for all + of the code used other than OpenSSL. If you modify file(s) with this + exception, you may extend this exception to your version of the file(s), + but you are not obligated to do so. If you do not wish to do so, delete this + exception statement from your version. If you delete this exception statement + from all source files in the program, then also delete it here. + along with TON Blockchain. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "blockchain-explorer-query.hpp" +#include "blockchain-explorer-http.hpp" + +#include "auto/tl/lite_api.h" + +#include "tl-utils/tl-utils.hpp" +#include "tl-utils/lite-utils.hpp" + +#include "ton/ton-tl.hpp" +#include "ton/lite-tl.hpp" + +#include "common/errorcode.h" +#include "block/block-auto.h" + +td::Result parse_block_id(std::map &opts, bool allow_empty) { + if (allow_empty) { + if (opts.count("workchain") == 0 && opts.count("shard") == 0 && opts.count("seqno") == 0) { + return ton::BlockIdExt{}; + } + } + try { + ton::BlockIdExt block_id; + auto it = opts.find("workchain"); + if (it == opts.end()) { + return td::Status::Error(ton::ErrorCode::protoviolation, "workchain not set"); + } + block_id.id.workchain = std::stoi(it->second); + it = opts.find("shard"); + if (it == opts.end()) { + return td::Status::Error(ton::ErrorCode::protoviolation, "shard not set"); + } + block_id.id.shard = std::stoull(it->second, nullptr, 16); + it = opts.find("seqno"); + if (it == opts.end()) { + return td::Status::Error(ton::ErrorCode::protoviolation, "seqno not set"); + } + auto s = std::stoull(it->second); + auto seqno = static_cast(s); + if (s != seqno) { + return td::Status::Error(ton::ErrorCode::protoviolation, "seqno too big"); + } + block_id.id.seqno = seqno; + it = opts.find("roothash"); + if (it == opts.end()) { + return td::Status::Error(ton::ErrorCode::protoviolation, "roothash not set"); + } + if (it->second.length() != 64) { + return td::Status::Error(ton::ErrorCode::protoviolation, "roothash bad length"); + } + auto R = td::hex_decode(td::Slice(it->second)); + if (R.is_error()) { + return td::Status::Error(ton::ErrorCode::protoviolation, "roothash bad hex"); + } + block_id.root_hash.as_slice().copy_from(td::as_slice(R.move_as_ok())); + it = opts.find("filehash"); + if (it == opts.end()) { + return td::Status::Error(ton::ErrorCode::protoviolation, "filehash not set"); + } + if (it->second.length() != 64) { + return td::Status::Error(ton::ErrorCode::protoviolation, "filehash bad length"); + } + R = td::hex_decode(td::Slice(it->second)); + if (R.is_error()) { + return td::Status::Error(ton::ErrorCode::protoviolation, "filehash bad hex"); + } + block_id.file_hash.as_slice().copy_from(td::as_slice(R.move_as_ok())); + return block_id; + } catch (...) { + return td::Status::Error(ton::ErrorCode::protoviolation, "cannot parse int"); + } +} + +td::Result parse_account_prefix(std::map &opts, bool allow_empty) { + if (allow_empty) { + if (opts.count("workchain") == 0 && opts.count("shard") == 0 && opts.count("account") == 0) { + return ton::AccountIdPrefixFull{ton::masterchainId, 0}; + } + } + try { + ton::AccountIdPrefixFull account_id; + auto it = opts.find("workchain"); + if (it == opts.end()) { + return td::Status::Error(ton::ErrorCode::protoviolation, "workchain not set"); + } + account_id.workchain = std::stoi(it->second); + it = opts.find("shard"); + if (it == opts.end()) { + it = opts.find("account"); + if (it == opts.end()) { + return td::Status::Error(ton::ErrorCode::protoviolation, "shard/account not set"); + } + } + account_id.account_id_prefix = std::stoull(it->second, nullptr, 16); + return account_id; + } catch (...) { + return td::Status::Error(ton::ErrorCode::protoviolation, "cannot parse int"); + } +} + +td::Result parse_account_addr(std::map &opts) { + auto it = opts.find("account"); + if (it == opts.end()) { + return td::Status::Error(ton::ErrorCode::error, "no account id"); + } + std::string acc_string = it->second; + block::StdAddress a{acc_string}; + if (a.is_valid()) { + return a; + } + ton::WorkchainId workchain_id; + it = opts.find("accountworkchain"); + if (it == opts.end()) { + return td::Status::Error(ton::ErrorCode::error, "no account workchain id"); + } + try { + workchain_id = std::stoi(it->second); + } catch (...) { + return td::Status::Error(ton::ErrorCode::error, "bad account workchain id"); + } + if (acc_string.size() == 64) { + TRY_RESULT(R, td::hex_decode(acc_string)); + a.addr.as_slice().copy_from(td::Slice(R)); + a.workchain = workchain_id; + return a; + } + return td::Status::Error(ton::ErrorCode::error, "bad account id"); +} + +void HttpQueryCommon::abort_query(td::Status error) { + if (promise_) { + HttpAnswer A{"error", prefix_}; + A.abort(std::move(error)); + auto page = A.finish(); + promise_.set_value( + MHD_create_response_from_buffer(page.length(), const_cast(page.c_str()), MHD_RESPMEM_MUST_COPY)); + } + stop(); +} + +HttpQueryBlockData::HttpQueryBlockData(ton::BlockIdExt block_id, std::string prefix, + td::Promise promise) + : HttpQueryCommon(std::move(prefix), std::move(promise)), block_id_(block_id) { +} + +HttpQueryBlockData::HttpQueryBlockData(std::map opts, std::string prefix, + td::Promise promise) + : HttpQueryCommon(std::move(prefix), std::move(promise)) { + auto R = parse_block_id(opts); + if (R.is_ok()) { + block_id_ = R.move_as_ok(); + } else { + error_ = R.move_as_error(); + } +} + +void HttpQueryBlockData::abort_query(td::Status error) { + if (promise_) { + promise_.set_result(nullptr); + } + stop(); +} + +void HttpQueryBlockData::finish_query() { + if (promise_) { + auto response = MHD_create_response_from_buffer(data_.length(), data_.as_slice().begin(), MHD_RESPMEM_MUST_COPY); + promise_.set_result(response); + } + stop(); +} + +void HttpQueryBlockData::start_up() { + auto query = ton::serialize_tl_object( + ton::create_tl_object(ton::create_tl_lite_block_id(block_id_)), true); + + auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result R) { + if (R.is_error()) { + td::actor::send_closure(SelfId, &HttpQueryBlockData::abort_query, R.move_as_error_prefix("litequery failed: ")); + } else { + td::actor::send_closure(SelfId, &HttpQueryBlockData::got_block_data, R.move_as_ok()); + } + }); + + td::actor::send_closure(CoreActorInterface::instance_actor_id(), &CoreActorInterface::send_lite_query, + std::move(query), std::move(P)); +} + +void HttpQueryBlockData::got_block_data(td::BufferSlice data) { + auto F = ton::fetch_tl_object(std::move(data), true); + if (F.is_error()) { + abort_query(F.move_as_error()); + return; + } + data_ = std::move(F.move_as_ok()->data_); + finish_query(); +} + +HttpQueryBlockView::HttpQueryBlockView(ton::BlockIdExt block_id, std::string prefix, + td::Promise promise) + : HttpQueryCommon(std::move(prefix), std::move(promise)), block_id_(block_id) { +} + +HttpQueryBlockView::HttpQueryBlockView(std::map opts, std::string prefix, + td::Promise promise) + : HttpQueryCommon(std::move(prefix), std::move(promise)) { + auto R = parse_block_id(opts); + if (R.is_ok()) { + block_id_ = R.move_as_ok(); + } else { + error_ = R.move_as_error(); + } +} + +void HttpQueryBlockView::finish_query() { + if (promise_) { + auto page = [&]() -> std::string { + HttpAnswer A{"viewblock", prefix_}; + A.set_block_id(block_id_); + auto res = vm::std_boc_deserialize(data_.clone()); + if (res.is_error()) { + return A.abort(PSTRING() << "cannot deserialize block: " << res.move_as_error()); + } + create_header(A); + auto root = res.move_as_ok(); + A << HttpAnswer::RawData{root}; + return A.finish(); + }(); + promise_.set_value( + MHD_create_response_from_buffer(page.length(), const_cast(page.c_str()), MHD_RESPMEM_MUST_COPY)); + } + stop(); +} + +void HttpQueryBlockView::start_up_query() { + auto query = ton::serialize_tl_object( + ton::create_tl_object(ton::create_tl_lite_block_id(block_id_)), true); + + auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result R) { + if (R.is_error()) { + td::actor::send_closure(SelfId, &HttpQueryBlockView::abort_query, R.move_as_error_prefix("litequery failed: ")); + } else { + td::actor::send_closure(SelfId, &HttpQueryBlockView::got_block_data, R.move_as_ok()); + } + }); + + td::actor::send_closure(CoreActorInterface::instance_actor_id(), &CoreActorInterface::send_lite_query, + std::move(query), std::move(P)); +} + +void HttpQueryBlockView::got_block_data(td::BufferSlice data) { + auto F = ton::fetch_tl_object(std::move(data), true); + if (F.is_error()) { + abort_query(F.move_as_error()); + } + data_ = std::move(F.move_as_ok()->data_); + finish_query(); +} + +HttpQueryBlockInfo::HttpQueryBlockInfo(ton::BlockIdExt block_id, std::string prefix, + td::Promise promise) + : HttpQueryCommon(std::move(prefix), std::move(promise)), block_id_(block_id) { +} + +HttpQueryBlockInfo::HttpQueryBlockInfo(std::map opts, std::string prefix, + td::Promise promise) + : HttpQueryCommon(std::move(prefix), std::move(promise)) { + auto R = parse_block_id(opts); + if (R.is_ok()) { + block_id_ = R.move_as_ok(); + } else { + error_ = R.move_as_error(); + } +} + +void HttpQueryBlockInfo::start_up_query() { + auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result R) { + if (R.is_error()) { + td::actor::send_closure(SelfId, &HttpQueryBlockInfo::abort_query, R.move_as_error_prefix("litequery failed: ")); + } else { + td::actor::send_closure(SelfId, &HttpQueryBlockInfo::got_block_header, R.move_as_ok()); + } + }); + auto query = ton::serialize_tl_object( + ton::create_tl_object(ton::create_tl_lite_block_id(block_id_), 0), + true); + td::actor::send_closure(CoreActorInterface::instance_actor_id(), &CoreActorInterface::send_lite_query, + std::move(query), std::move(P)); + pending_queries_ = 1; + + if (block_id_.is_masterchain()) { + auto P_2 = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result R) { + if (R.is_error()) { + td::actor::send_closure(SelfId, &HttpQueryBlockInfo::failed_to_get_shard_info, + R.move_as_error_prefix("litequery failed: ")); + } else { + td::actor::send_closure(SelfId, &HttpQueryBlockInfo::got_shard_info, R.move_as_ok()); + } + }); + auto query_2 = ton::serialize_tl_object( + ton::create_tl_object(ton::create_tl_lite_block_id(block_id_)), + true); + td::actor::send_closure(CoreActorInterface::instance_actor_id(), &CoreActorInterface::send_lite_query, + std::move(query_2), std::move(P_2)); + pending_queries_++; + } + auto query_3 = ton::serialize_tl_object(ton::create_tl_object( + ton::create_tl_lite_block_id(block_id_), 7, 1024, nullptr, false, false), + true); + auto P_3 = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result R) { + if (R.is_error()) { + td::actor::send_closure(SelfId, &HttpQueryBlockInfo::abort_query, R.move_as_error_prefix("litequery failed: ")); + } else { + td::actor::send_closure(SelfId, &HttpQueryBlockInfo::got_transactions, R.move_as_ok()); + } + }); + td::actor::send_closure(CoreActorInterface::instance_actor_id(), &CoreActorInterface::send_lite_query, + std::move(query_3), std::move(P_3)); + pending_queries_++; +} + +void HttpQueryBlockInfo::got_block_header(td::BufferSlice data) { + auto F = ton::fetch_tl_object(std::move(data), true); + if (F.is_error()) { + abort_query(F.move_as_error()); + return; + } + data_ = std::move(F.move_as_ok()->header_proof_); + + if (!--pending_queries_) { + finish_query(); + } +} + +void HttpQueryBlockInfo::got_shard_info(td::BufferSlice data) { + auto F = ton::fetch_tl_object(std::move(data), true); + if (F.is_error()) { + abort_query(F.move_as_error()); + return; + } + shard_data_ = std::move(F.move_as_ok()->data_); + + if (!--pending_queries_) { + finish_query(); + } +} + +void HttpQueryBlockInfo::failed_to_get_shard_info(td::Status error) { + shard_data_error_ = std::move(error); + if (!--pending_queries_) { + finish_query(); + } +} + +void HttpQueryBlockInfo::got_transactions(td::BufferSlice data) { + auto F = ton::fetch_tl_object(std::move(data), true); + if (F.is_error()) { + abort_query(F.move_as_error()); + return; + } + auto f = F.move_as_ok(); + trans_req_count_ = f->req_count_; + + for (auto &T : f->ids_) { + transactions_.emplace_back(block::StdAddress{block_id_.id.workchain, T->account_}, + static_cast(T->lt_), T->hash_); + } + + if (!--pending_queries_) { + finish_query(); + } +} + +void HttpQueryBlockInfo::finish_query() { + if (promise_) { + auto page = [&]() -> std::string { + HttpAnswer A{"blockinfo", prefix_}; + A.set_block_id(block_id_); + create_header(A); + auto res = vm::std_boc_deserialize(data_.clone()); + if (res.is_error()) { + return A.abort(PSTRING() << "cannot deserialize block header data: " << res.move_as_error()); + } + A << HttpAnswer::BlockHeaderCell{block_id_, res.move_as_ok()}; + + if (shard_data_.size() > 0) { + auto R = vm::std_boc_deserialize(shard_data_.clone()); + if (R.is_error()) { + return A.abort(PSTRING() << "cannot deserialize shard configuration: " << R.move_as_error()); + } else { + A << HttpAnswer::BlockShardsCell{block_id_, R.move_as_ok()}; + } + } + if (shard_data_error_.is_error()) { + A << HttpAnswer::Error{shard_data_error_.clone()}; + } + + HttpAnswer::TransactionList I; + I.block_id = block_id_; + I.req_count_ = trans_req_count_; + for (auto &T : transactions_) { + I.vec.emplace_back(T.addr, T.lt, T.hash); + } + A << I; + + return A.finish(); + }(); + promise_.set_value( + MHD_create_response_from_buffer(page.length(), const_cast(page.c_str()), MHD_RESPMEM_MUST_COPY)); + } + stop(); +} + +HttpQueryBlockSearch::HttpQueryBlockSearch(ton::WorkchainId workchain, ton::AccountIdPrefix account, + ton::BlockSeqno seqno, std::string prefix, + td::Promise promise) + : HttpQueryCommon(std::move(prefix), std::move(promise)) + , account_prefix_{workchain, account} + , mode_(1) + , seqno_(seqno) { +} +HttpQueryBlockSearch::HttpQueryBlockSearch(ton::WorkchainId workchain, ton::AccountIdPrefix account, + ton::LogicalTime lt, std::string prefix, td::Promise promise) + : HttpQueryCommon(std::move(prefix), std::move(promise)), account_prefix_{workchain, account}, mode_(2), lt_(lt) { +} +HttpQueryBlockSearch::HttpQueryBlockSearch(ton::WorkchainId workchain, ton::AccountIdPrefix account, bool dummy, + ton::UnixTime utime, std::string prefix, td::Promise promise) + : HttpQueryCommon(std::move(prefix), std::move(promise)) + , account_prefix_{workchain, account} + , mode_(4) + , utime_(utime) { +} + +HttpQueryBlockSearch::HttpQueryBlockSearch(std::map opts, std::string prefix, + td::Promise promise) + : HttpQueryCommon(std::move(prefix), std::move(promise)) { + auto R2 = parse_account_prefix(opts, false); + if (R2.is_ok()) { + account_prefix_ = R2.move_as_ok(); + } else { + error_ = R2.move_as_error(); + return; + } + if (opts.count("seqno") + opts.count("lt") + opts.count("utime") != 1) { + error_ = td::Status::Error(ton::ErrorCode::protoviolation, "exactly one of seqno/lt/utime must be set"); + return; + } + if (opts.count("seqno") == 1) { + try { + seqno_ = static_cast(std::stoull(opts["seqno"])); + mode_ = 1; + } catch (...) { + error_ = td::Status::Error("cannot parse seqno"); + return; + } + } + if (opts.count("lt") == 1) { + try { + lt_ = std::stoull(opts["lt"]); + mode_ = 2; + } catch (...) { + error_ = td::Status::Error("cannot parse lt"); + return; + } + } + if (opts.count("utime") == 1) { + try { + seqno_ = static_cast(std::stoull(opts["utime"])); + mode_ = 1; + } catch (...) { + error_ = td::Status::Error("cannot parse utime"); + return; + } + } +} + +void HttpQueryBlockSearch::start_up_query() { + auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result R) { + if (R.is_error()) { + td::actor::send_closure(SelfId, &HttpQueryBlockSearch::abort_query, R.move_as_error_prefix("litequery failed: ")); + } else { + td::actor::send_closure(SelfId, &HttpQueryBlockSearch::got_block_header, R.move_as_ok()); + } + }); + auto query = ton::serialize_tl_object(ton::create_tl_object( + mode_, + ton::create_tl_lite_block_id_simple(ton::BlockId{ + account_prefix_.workchain, account_prefix_.account_id_prefix, seqno_}), + lt_, utime_), + true); + td::actor::send_closure(CoreActorInterface::instance_actor_id(), &CoreActorInterface::send_lite_query, + std::move(query), std::move(P)); +} + +void HttpQueryBlockSearch::got_block_header(td::BufferSlice data) { + auto F = ton::fetch_tl_object(std::move(data), true); + if (F.is_error()) { + abort_query(F.move_as_error()); + return; + } + auto f = F.move_as_ok(); + data_ = std::move(f->header_proof_); + block_id_ = ton::create_block_id(f->id_); + + if (block_id_.is_masterchain()) { + auto P_2 = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result R) { + if (R.is_error()) { + td::actor::send_closure(SelfId, &HttpQueryBlockSearch::failed_to_get_shard_info, + R.move_as_error_prefix("litequery failed: ")); + } else { + td::actor::send_closure(SelfId, &HttpQueryBlockSearch::got_shard_info, R.move_as_ok()); + } + }); + auto query_2 = ton::serialize_tl_object( + ton::create_tl_object(ton::create_tl_lite_block_id(block_id_)), + true); + td::actor::send_closure(CoreActorInterface::instance_actor_id(), &CoreActorInterface::send_lite_query, + std::move(query_2), std::move(P_2)); + pending_queries_++; + } + + auto query_3 = ton::serialize_tl_object(ton::create_tl_object( + ton::create_tl_lite_block_id(block_id_), 7, 1024, nullptr, false, false), + true); + auto P_3 = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result R) { + if (R.is_error()) { + td::actor::send_closure(SelfId, &HttpQueryBlockSearch::abort_query, R.move_as_error_prefix("litequery failed: ")); + } else { + td::actor::send_closure(SelfId, &HttpQueryBlockSearch::got_transactions, R.move_as_ok()); + } + }); + td::actor::send_closure(CoreActorInterface::instance_actor_id(), &CoreActorInterface::send_lite_query, + std::move(query_3), std::move(P_3)); + pending_queries_++; +} + +void HttpQueryBlockSearch::got_shard_info(td::BufferSlice data) { + auto F = ton::fetch_tl_object(std::move(data), true); + if (F.is_error()) { + abort_query(F.move_as_error()); + return; + } + shard_data_ = std::move(F.move_as_ok()->data_); + + if (!--pending_queries_) { + finish_query(); + } +} + +void HttpQueryBlockSearch::failed_to_get_shard_info(td::Status error) { + shard_data_error_ = std::move(error); + if (!--pending_queries_) { + finish_query(); + } +} + +void HttpQueryBlockSearch::got_transactions(td::BufferSlice data) { + auto F = ton::fetch_tl_object(std::move(data), true); + if (F.is_error()) { + abort_query(F.move_as_error()); + return; + } + auto f = F.move_as_ok(); + trans_req_count_ = f->req_count_; + + for (auto &T : f->ids_) { + transactions_.emplace_back(block::StdAddress{block_id_.id.workchain, T->account_}, + static_cast(T->lt_), T->hash_); + } + + if (!--pending_queries_) { + finish_query(); + } +} + +void HttpQueryBlockSearch::finish_query() { + if (promise_) { + auto page = [&]() -> std::string { + HttpAnswer A{"blockinfo", prefix_}; + A.set_block_id(block_id_); + create_header(A); + auto res = vm::std_boc_deserialize(data_.clone()); + if (res.is_error()) { + return A.abort(PSTRING() << "cannot deserialize block header data: " << res.move_as_error()); + } + A << HttpAnswer::BlockHeaderCell{block_id_, res.move_as_ok()}; + + if (shard_data_.size() > 0) { + auto R = vm::std_boc_deserialize(shard_data_.clone()); + if (R.is_error()) { + return A.abort(PSTRING() << "cannot deserialize shard configuration: " << R.move_as_error()); + } else { + A << HttpAnswer::BlockShardsCell{block_id_, R.move_as_ok()}; + } + } + if (shard_data_error_.is_error()) { + A << HttpAnswer::Error{shard_data_error_.clone()}; + } + + HttpAnswer::TransactionList I; + I.block_id = block_id_; + I.req_count_ = trans_req_count_; + for (auto &T : transactions_) { + I.vec.emplace_back(T.addr, T.lt, T.hash); + } + A << I; + + return A.finish(); + }(); + promise_.set_value( + MHD_create_response_from_buffer(page.length(), const_cast(page.c_str()), MHD_RESPMEM_MUST_COPY)); + } + stop(); +} +HttpQueryViewAccount::HttpQueryViewAccount(ton::BlockIdExt block_id, block::StdAddress addr, std::string prefix, + td::Promise promise) + : HttpQueryCommon(std::move(prefix), std::move(promise)), block_id_(block_id), addr_(addr) { +} + +HttpQueryViewAccount::HttpQueryViewAccount(std::map opts, std::string prefix, + td::Promise promise) + : HttpQueryCommon(std::move(prefix), std::move(promise)) { + auto R = parse_block_id(opts, true); + if (R.is_ok()) { + block_id_ = R.move_as_ok(); + if (!block_id_.is_valid()) { + block_id_.id.workchain = ton::masterchainId; + block_id_.id.shard = ton::shardIdAll; + block_id_.id.seqno = static_cast(0xffffffff); + block_id_.root_hash.set_zero(); + block_id_.file_hash.set_zero(); + } + } else { + error_ = R.move_as_error(); + return; + } + auto R2 = parse_account_addr(opts); + if (R2.is_ok()) { + addr_ = R2.move_as_ok(); + } else { + error_ = R2.move_as_error(); + return; + } +} + +void HttpQueryViewAccount::start_up_query() { + auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result R) { + if (R.is_error()) { + td::actor::send_closure(SelfId, &HttpQueryViewAccount::abort_query, R.move_as_error_prefix("litequery failed: ")); + } else { + td::actor::send_closure(SelfId, &HttpQueryViewAccount::got_account, R.move_as_ok()); + } + }); + auto a = ton::create_tl_object(addr_.workchain, addr_.addr); + auto query = ton::serialize_tl_object(ton::create_tl_object( + ton::create_tl_lite_block_id(block_id_), std::move(a)), + true); + td::actor::send_closure(CoreActorInterface::instance_actor_id(), &CoreActorInterface::send_lite_query, + std::move(query), std::move(P)); +} + +void HttpQueryViewAccount::got_account(td::BufferSlice data) { + auto F = ton::fetch_tl_object(std::move(data), true); + if (F.is_error()) { + abort_query(F.move_as_error()); + return; + } + + auto f = F.move_as_ok(); + data_ = std::move(f->state_); + proof_ = std::move(f->proof_); + res_block_id_ = ton::create_block_id(f->shardblk_); + + finish_query(); +} + +void HttpQueryViewAccount::finish_query() { + if (promise_) { + auto page = [&]() -> std::string { + HttpAnswer A{"account", prefix_}; + A.set_account_id(addr_); + A.set_block_id(res_block_id_); + auto R = vm::std_boc_deserialize(data_.clone()); + if (R.is_error()) { + return A.abort(PSTRING() << "FATAL: cannot deserialize account state" << R.move_as_error()); + } + auto Q = vm::std_boc_deserialize_multi(proof_.clone()); + if (Q.is_error()) { + return A.abort(PSTRING() << "FATAL: cannot deserialize account proof" << Q.move_as_error()); + } + auto Q_roots = Q.move_as_ok(); + auto root = R.move_as_ok(); + A << HttpAnswer::AccountCell{addr_, res_block_id_, root, Q_roots}; + return A.finish(); + }(); + promise_.set_value( + MHD_create_response_from_buffer(page.length(), const_cast(page.c_str()), MHD_RESPMEM_MUST_COPY)); + } + stop(); +} + +HttpQueryViewTransaction::HttpQueryViewTransaction(block::StdAddress addr, ton::LogicalTime lt, ton::Bits256 hash, + std::string prefix, td::Promise promise) + : HttpQueryCommon(std::move(prefix), std::move(promise)), addr_(addr), lt_(lt), hash_(hash) { +} + +HttpQueryViewTransaction::HttpQueryViewTransaction(std::map opts, std::string prefix, + td::Promise promise) + : HttpQueryCommon(std::move(prefix), std::move(promise)) { + auto R2 = parse_account_addr(opts); + if (R2.is_ok()) { + addr_ = R2.move_as_ok(); + } else { + error_ = R2.move_as_error(); + return; + } + try { + lt_ = std::stoull(opts["lt"]); + } catch (...) { + error_ = td::Status::Error("cannot trans parse lt"); + return; + } + try { + auto h = opts["hash"]; + if (h.length() != 64) { + error_ = td::Status::Error("cannot trans parse hash"); + return; + } + auto R = td::hex_decode(td::Slice(h)); + if (R.is_error()) { + error_ = td::Status::Error("cannot trans parse hash"); + return; + } + hash_.as_slice().copy_from(R.move_as_ok()); + } catch (...) { + error_ = td::Status::Error("cannot trans parse hash"); + return; + } +} + +void HttpQueryViewTransaction::start_up_query() { + auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result R) { + if (R.is_error()) { + td::actor::send_closure(SelfId, &HttpQueryViewTransaction::abort_query, + R.move_as_error_prefix("litequery failed: ")); + } else { + td::actor::send_closure(SelfId, &HttpQueryViewTransaction::got_transaction, R.move_as_ok()); + } + }); + auto a = ton::create_tl_object(addr_.workchain, addr_.addr); + auto query = ton::serialize_tl_object( + ton::create_tl_object(1, std::move(a), lt_, hash_), true); + td::actor::send_closure(CoreActorInterface::instance_actor_id(), &CoreActorInterface::send_lite_query, + std::move(query), std::move(P)); +} + +void HttpQueryViewTransaction::got_transaction(td::BufferSlice data) { + auto F = ton::fetch_tl_object(std::move(data), true); + if (F.is_error()) { + abort_query(F.move_as_error()); + return; + } + + auto f = F.move_as_ok(); + data_ = std::move(f->transactions_); + if (f->ids_.size() == 0) { + abort_query(td::Status::Error("no transactions found")); + return; + } + res_block_id_ = ton::create_block_id(f->ids_[0]); + + finish_query(); +} + +void HttpQueryViewTransaction::finish_query() { + if (promise_) { + auto page = [&]() -> std::string { + HttpAnswer A{"transaction", prefix_}; + A.set_block_id(res_block_id_); + A.set_account_id(addr_); + auto R = vm::std_boc_deserialize_multi(std::move(data_)); + if (R.is_error()) { + return A.abort(PSTRING() << "FATAL: cannot deserialize transactions BoC"); + } + auto list = R.move_as_ok(); + auto n = list.size(); + if (n != 1) { + return A.abort(PSTRING() << "obtained " << n << " transaction, but only 1 have been requested"); + } else { + A << HttpAnswer::TransactionCell{addr_, res_block_id_, list[0]}; + } + return A.finish(); + }(); + promise_.set_value( + MHD_create_response_from_buffer(page.length(), const_cast(page.c_str()), MHD_RESPMEM_MUST_COPY)); + } + stop(); +} + +HttpQueryViewTransaction2::HttpQueryViewTransaction2(ton::BlockIdExt block_id, block::StdAddress addr, + ton::LogicalTime lt, std::string prefix, + td::Promise promise) + : HttpQueryCommon(std::move(prefix), std::move(promise)), block_id_(block_id), addr_(addr), lt_(lt) { +} + +HttpQueryViewTransaction2::HttpQueryViewTransaction2(std::map opts, std::string prefix, + td::Promise promise) + : HttpQueryCommon(std::move(prefix), std::move(promise)) { + auto R = parse_block_id(opts); + if (R.is_ok()) { + block_id_ = R.move_as_ok(); + } else { + error_ = R.move_as_error(); + return; + } + auto R2 = parse_account_addr(opts); + if (R2.is_ok()) { + addr_ = R2.move_as_ok(); + } else { + error_ = R2.move_as_error(); + return; + } + try { + lt_ = std::stoull(opts["lt"]); + } catch (...) { + error_ = td::Status::Error("cannot trans parse lt"); + return; + } +} + +void HttpQueryViewTransaction2::start_up_query() { + auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result R) { + if (R.is_error()) { + td::actor::send_closure(SelfId, &HttpQueryViewTransaction2::abort_query, + R.move_as_error_prefix("litequery failed: ")); + } else { + td::actor::send_closure(SelfId, &HttpQueryViewTransaction2::got_transaction, R.move_as_ok()); + } + }); + auto a = ton::create_tl_object(addr_.workchain, addr_.addr); + auto query = ton::serialize_tl_object(ton::create_tl_object( + ton::create_tl_lite_block_id(block_id_), std::move(a), lt_), + true); + td::actor::send_closure(CoreActorInterface::instance_actor_id(), &CoreActorInterface::send_lite_query, + std::move(query), std::move(P)); +} + +void HttpQueryViewTransaction2::got_transaction(td::BufferSlice data) { + auto F = ton::fetch_tl_object(std::move(data), true); + if (F.is_error()) { + abort_query(F.move_as_error()); + return; + } + + auto f = F.move_as_ok(); + data_ = std::move(f->transaction_); + + finish_query(); +} + +void HttpQueryViewTransaction2::finish_query() { + if (promise_) { + auto page = [&]() -> std::string { + HttpAnswer A{"transaction", prefix_}; + A.set_block_id(block_id_); + A.set_account_id(addr_); + auto R = vm::std_boc_deserialize(std::move(data_)); + if (R.is_error()) { + return A.abort(PSTRING() << "FATAL: cannot deserialize transactions BoC"); + } + auto list = R.move_as_ok(); + A << HttpAnswer::TransactionCell{addr_, block_id_, list}; + return A.finish(); + }(); + promise_.set_value( + MHD_create_response_from_buffer(page.length(), const_cast(page.c_str()), MHD_RESPMEM_MUST_COPY)); + } + stop(); +} + +HttpQueryViewLastBlock::HttpQueryViewLastBlock(std::string prefix, td::Promise promise) + : HttpQueryCommon(std::move(prefix), std::move(promise)) { +} + +HttpQueryViewLastBlock::HttpQueryViewLastBlock(std::map opts, std::string prefix, + td::Promise promise) + : HttpQueryCommon(std::move(prefix), std::move(promise)) { +} + +void HttpQueryViewLastBlock::start_up() { + if (error_.is_error()) { + abort_query(std::move(error_)); + return; + } + auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result R) { + if (R.is_error()) { + td::actor::send_closure(SelfId, &HttpQueryViewLastBlock::abort_query, R.move_as_error()); + } else { + td::actor::send_closure(SelfId, &HttpQueryViewLastBlock::got_result, R.move_as_ok()); + } + }); + + auto query = ton::serialize_tl_object(ton::create_tl_object(), true); + td::actor::send_closure(CoreActorInterface::instance_actor_id(), &CoreActorInterface::send_lite_query, + std::move(query), std::move(P)); +} + +void HttpQueryViewLastBlock::got_result(td::BufferSlice data) { + auto F = ton::fetch_tl_object(std::move(data), true); + if (F.is_error()) { + abort_query(F.move_as_error()); + return; + } + auto f = F.move_as_ok(); + res_block_id_ = ton::create_block_id(f->last_); + + finish_query(); +} + +void HttpQueryViewLastBlock::finish_query() { + if (promise_) { + td::actor::create_actor("blockinfo", res_block_id_, prefix_, std::move(promise_)).release(); + } + stop(); +} + +HttpQueryStatus::HttpQueryStatus(std::string prefix, td::Promise promise) + : HttpQueryCommon(std::move(prefix), std::move(promise)) { +} + +HttpQueryStatus::HttpQueryStatus(std::map opts, std::string prefix, + td::Promise promise) + : HttpQueryCommon(std::move(prefix), std::move(promise)) { +} + +void HttpQueryStatus::start_up() { + if (error_.is_error()) { + abort_query(std::move(error_)); + return; + } + + auto P = + td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result R) { + if (R.is_error()) { + td::actor::send_closure(SelfId, &HttpQueryStatus::abort_query, R.move_as_error()); + } else { + td::actor::send_closure(SelfId, &HttpQueryStatus::got_results, R.move_as_ok()); + } + }); + td::actor::send_closure(CoreActorInterface::instance_actor_id(), &CoreActorInterface::get_results, 60, std::move(P)); +} + +void HttpQueryStatus::got_results(CoreActorInterface::RemoteNodeStatusList results) { + results_ = std::move(results); + + finish_query(); +} + +void HttpQueryStatus::finish_query() { + if (promise_) { + auto page = [&]() -> std::string { + std::map> m; + + HttpAnswer A{"status", prefix_}; + A << "
\n" + << "\n" + << ""; + for (auto &x : results_.results) { + A << ""; + } + A << "\n"; + for (td::uint32 i = 0; i < results_.ips.size(); i++) { + A << ""; + if (results_.ips[i].is_valid()) { + A << ""; + } else { + A << ""; + } + td::uint32 j = 0; + for (auto &X : results_.results) { + if (!X->values_[i].is_valid()) { + A << ""; + } else { + if (m[j].count(X->values_[i].id.seqno) == 0) { + m[j].insert(X->values_[i].id.seqno); + A << ""; + } else { + A << ""; + } + } + j++; + } + A << "\n"; + } + A << "
ip" << static_cast(x->ts_.at_unix()) << "
" << results_.ips[i] << "hiddenFAILvalues_[i]} << "\">" << X->values_[i].id.seqno + << "" << X->values_[i].id.seqno << "
"; + return A.finish(); + }(); + promise_.set_value( + MHD_create_response_from_buffer(page.length(), const_cast(page.c_str()), MHD_RESPMEM_MUST_COPY)); + } + stop(); +} diff --git a/blockchain-explorer/blockchain-explorer-query.hpp b/blockchain-explorer/blockchain-explorer-query.hpp new file mode 100644 index 00000000..2acf9efb --- /dev/null +++ b/blockchain-explorer/blockchain-explorer-query.hpp @@ -0,0 +1,274 @@ +/* + This file is part of TON Blockchain source code. + + TON Blockchain is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + TON Blockchain is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with TON Blockchain. If not, see . + + In addition, as a special exception, the copyright holders give permission + to link the code of portions of this program with the OpenSSL library. + You must obey the GNU General Public License in all respects for all + of the code used other than OpenSSL. If you modify file(s) with this + exception, you may extend this exception to your version of the file(s), + but you are not obligated to do so. If you do not wish to do so, delete this + exception statement from your version. If you delete this exception statement + from all source files in the program, then also delete it here. + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +#include "td/actor/actor.h" +#include "ton/ton-types.h" +#include "block/block.h" +#include "blockchain-explorer.hpp" + +#include + +#include + +td::Result parse_block_id(std::map &opts, bool allow_empty = false); +td::Result parse_account_addr(std::map &opts); + +class HttpAnswer; + +class HttpQueryCommon : public td::actor::Actor { + public: + HttpQueryCommon(std::string prefix, td::Promise promise) + : prefix_(std::move(prefix)), promise_(std::move(promise)) { + } + void start_up() override { + if (error_.is_error()) { + abort_query(std::move(error_)); + return; + } + start_up_query(); + } + virtual void start_up_query() { + UNREACHABLE(); + } + virtual void abort_query(td::Status error); + void create_header(HttpAnswer &ans) { + } + + protected: + td::Status error_; + + std::string prefix_; + td::Promise promise_; +}; + +class HttpQueryBlockData : public HttpQueryCommon { + public: + HttpQueryBlockData(ton::BlockIdExt block_id, std::string prefix, td::Promise promise); + HttpQueryBlockData(std::map opts, std::string prefix, td::Promise promise); + + void abort_query(td::Status error) override; + void finish_query(); + + void start_up() override; + void got_block_data(td::BufferSlice result); + + private: + ton::BlockIdExt block_id_; + + td::BufferSlice data_; +}; + +class HttpQueryBlockView : public HttpQueryCommon { + public: + HttpQueryBlockView(ton::BlockIdExt block_id, std::string prefix, td::Promise promise); + HttpQueryBlockView(std::map opts, std::string prefix, td::Promise promise); + + void finish_query(); + + void start_up_query() override; + void got_block_data(td::BufferSlice result); + + private: + ton::BlockIdExt block_id_; + + td::BufferSlice data_; +}; + +class HttpQueryBlockInfo : public HttpQueryCommon { + public: + HttpQueryBlockInfo(ton::BlockIdExt block_id, std::string prefix, td::Promise promise); + HttpQueryBlockInfo(std::map opts, std::string prefix, td::Promise promise); + + void finish_query(); + + void start_up_query() override; + void got_block_header(td::BufferSlice result); + void got_shard_info(td::BufferSlice result); + void got_transactions(td::BufferSlice result); + + void failed_to_get_shard_info(td::Status error); + + private: + ton::BlockIdExt block_id_; + + td::int32 pending_queries_ = 0; + + td::BufferSlice data_; + td::BufferSlice shard_data_; + td::Status shard_data_error_; + + struct TransactionDescr { + TransactionDescr(block::StdAddress addr, ton::LogicalTime lt, ton::Bits256 hash) : addr(addr), lt(lt), hash(hash) { + } + block::StdAddress addr; + ton::LogicalTime lt; + ton::Bits256 hash; + }; + std::vector transactions_; + td::uint32 trans_req_count_; +}; + +class HttpQueryBlockSearch : public HttpQueryCommon { + public: + HttpQueryBlockSearch(ton::WorkchainId workchain, ton::AccountIdPrefix account, ton::BlockSeqno seqno, + std::string prefix, td::Promise promise); + HttpQueryBlockSearch(ton::WorkchainId workchain, ton::AccountIdPrefix account, ton::LogicalTime lt, + std::string prefix, td::Promise promise); + HttpQueryBlockSearch(ton::WorkchainId workchain, ton::AccountIdPrefix account, bool dummy, ton::UnixTime utime, + std::string prefix, td::Promise promise); + HttpQueryBlockSearch(std::map opts, std::string prefix, + td::Promise promise); + + void finish_query(); + + void start_up_query() override; + void got_block_header(td::BufferSlice result); + void got_shard_info(td::BufferSlice result); + void got_transactions(td::BufferSlice result); + + void failed_to_get_shard_info(td::Status error); + + private: + ton::AccountIdPrefixFull account_prefix_; + td::uint32 mode_ = 0; + ton::BlockSeqno seqno_ = 0; + ton::LogicalTime lt_ = 0; + ton::UnixTime utime_ = 0; + + ton::BlockIdExt block_id_; + + td::BufferSlice data_; + td::BufferSlice shard_data_; + td::Status shard_data_error_; + + td::uint32 pending_queries_ = 0; + + struct TransactionDescr { + TransactionDescr(block::StdAddress addr, ton::LogicalTime lt, ton::Bits256 hash) : addr(addr), lt(lt), hash(hash) { + } + block::StdAddress addr; + ton::LogicalTime lt; + ton::Bits256 hash; + }; + std::vector transactions_; + td::uint32 trans_req_count_; +}; + +class HttpQueryViewAccount : public HttpQueryCommon { + public: + HttpQueryViewAccount(ton::BlockIdExt block_id, block::StdAddress addr, std::string prefix, + td::Promise promise); + HttpQueryViewAccount(std::map opts, std::string prefix, + td::Promise promise); + + void finish_query(); + + void start_up_query() override; + void got_account(td::BufferSlice result); + + private: + ton::BlockIdExt block_id_; + block::StdAddress addr_; + + td::BufferSlice data_; + td::BufferSlice proof_; + ton::BlockIdExt res_block_id_; +}; + +class HttpQueryViewTransaction : public HttpQueryCommon { + public: + HttpQueryViewTransaction(block::StdAddress addr, ton::LogicalTime lt, ton::Bits256 hash, std::string prefix, + td::Promise promise); + HttpQueryViewTransaction(std::map opts, std::string prefix, + td::Promise promise); + + void finish_query(); + + void start_up_query() override; + void got_transaction(td::BufferSlice result); + + private: + block::StdAddress addr_; + ton::LogicalTime lt_; + ton::Bits256 hash_; + + td::BufferSlice data_; + ton::BlockIdExt res_block_id_; +}; + +class HttpQueryViewTransaction2 : public HttpQueryCommon { + public: + HttpQueryViewTransaction2(ton::BlockIdExt block_id, block::StdAddress addr, ton::LogicalTime lt, std::string prefix, + td::Promise promise); + HttpQueryViewTransaction2(std::map opts, std::string prefix, + td::Promise promise); + + void finish_query(); + + void start_up_query() override; + void got_transaction(td::BufferSlice result); + + private: + ton::BlockIdExt block_id_; + block::StdAddress addr_; + ton::LogicalTime lt_; + ton::Bits256 hash_; + + td::BufferSlice data_; +}; + +class HttpQueryViewLastBlock : public HttpQueryCommon { + public: + HttpQueryViewLastBlock(std::string prefix, td::Promise promise); + HttpQueryViewLastBlock(std::map opts, std::string prefix, + td::Promise promise); + + void finish_query(); + + void start_up() override; + void got_result(td::BufferSlice result); + + private: + ton::BlockIdExt res_block_id_; +}; + +class HttpQueryStatus : public HttpQueryCommon { + public: + HttpQueryStatus(std::string prefix, td::Promise promise); + HttpQueryStatus(std::map opts, std::string prefix, td::Promise promise); + + void finish_query(); + + void start_up() override; + void got_results(CoreActorInterface::RemoteNodeStatusList results); + + private: + CoreActorInterface::RemoteNodeStatusList results_; +}; + diff --git a/blockchain-explorer/blockchain-explorer.cpp b/blockchain-explorer/blockchain-explorer.cpp new file mode 100644 index 00000000..01e4f954 --- /dev/null +++ b/blockchain-explorer/blockchain-explorer.cpp @@ -0,0 +1,582 @@ +/* + This file is part of TON Blockchain source code. + + TON Blockchain is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + TON Blockchain is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + + In addition, as a special exception, the copyright holders give permission + to link the code of portions of this program with the OpenSSL library. + You must obey the GNU General Public License in all respects for all + of the code used other than OpenSSL. If you modify file(s) with this + exception, you may extend this exception to your version of the file(s), + but you are not obligated to do so. If you do not wish to do so, delete this + exception statement from your version. If you delete this exception statement + from all source files in the program, then also delete it here. + along with TON Blockchain. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "adnl/adnl-ext-client.h" +#include "adnl/utils.hpp" +#include "auto/tl/ton_api_json.h" +#include "td/utils/OptionsParser.h" +#include "td/utils/Time.h" +#include "td/utils/filesystem.h" +#include "td/utils/format.h" +#include "td/utils/Random.h" +#include "td/utils/crypto.h" +#include "td/utils/port/signals.h" +#include "td/utils/port/user.h" +#include "td/utils/port/FileFd.h" +#include "ton/ton-tl.hpp" +#include "block/block-db.h" +#include "block/block.h" +#include "block/block-auto.h" +#include "vm/boc.h" +#include "vm/cellops.h" +#include "vm/cells/MerkleProof.h" +#include "block/mc-config.h" +#include "blockchain-explorer.hpp" +#include "blockchain-explorer-http.hpp" +#include "blockchain-explorer-query.hpp" + +#include "auto/tl/lite_api.h" +#include "ton/lite-tl.hpp" +#include "tl-utils/lite-utils.hpp" + +#include + +#if TD_DARWIN || TD_LINUX +#include +#include +#endif +#include +#include + +int verbosity; + +td::actor::Scheduler* scheduler_ptr; + +static std::string urldecode(td::Slice from, bool decode_plus_sign_as_space) { + size_t to_i = 0; + + td::BufferSlice x{from.size()}; + auto to = x.as_slice(); + + for (size_t from_i = 0, n = from.size(); from_i < n; from_i++) { + if (from[from_i] == '%' && from_i + 2 < n) { + int high = td::hex_to_int(from[from_i + 1]); + int low = td::hex_to_int(from[from_i + 2]); + if (high < 16 && low < 16) { + to[to_i++] = static_cast(high * 16 + low); + from_i += 2; + continue; + } + } + to[to_i++] = decode_plus_sign_as_space && from[from_i] == '+' ? ' ' : from[from_i]; + } + + return to.truncate(to_i).str(); +} + +class HttpQueryRunner { + public: + HttpQueryRunner(std::function)> func) { + auto P = td::PromiseCreator::lambda([Self = this](td::Result R) { + if (R.is_ok()) { + Self->finish(R.move_as_ok()); + } else { + Self->finish(nullptr); + } + }); + mutex_.lock(); + scheduler_ptr->run_in_context_external([&]() { func(std::move(P)); }); + } + void finish(MHD_Response* response) { + response_ = response; + mutex_.unlock(); + } + MHD_Response* wait() { + mutex_.lock(); + mutex_.unlock(); + return response_; + } + + private: + std::function)> func_; + MHD_Response* response_; + std::mutex mutex_; +}; + +class CoreActor : public CoreActorInterface { + private: + std::string global_config_ = "ton-global.config"; + + std::vector> clients_; + + td::uint32 http_port_ = 80; + MHD_Daemon* daemon_ = nullptr; + + td::IPAddress remote_addr_; + ton::PublicKey remote_public_key_; + + bool hide_ips_ = false; + + std::unique_ptr make_callback(td::uint32 idx) { + class Callback : public ton::adnl::AdnlExtClient::Callback { + public: + void on_ready() override { + td::actor::send_closure(id_, &CoreActor::conn_ready, idx_); + } + void on_stop_ready() override { + td::actor::send_closure(id_, &CoreActor::conn_closed, idx_); + } + Callback(td::actor::ActorId id, td::uint32 idx) : id_(std::move(id)), idx_(idx) { + } + + private: + td::actor::ActorId id_; + td::uint32 idx_; + }; + + return std::make_unique(actor_id(this), idx); + } + + std::shared_ptr new_result_; + td::int32 attempt_ = 0; + td::int32 waiting_ = 0; + + std::vector ready_; + + void run_queries(); + void got_result(td::uint32 idx, td::int32 attempt, td::Result data); + void send_query(td::uint32 idx); + + void add_result() { + if (new_result_) { + auto ts = static_cast(new_result_->ts_.at_unix()); + results_.emplace(ts, std::move(new_result_)); + } + } + + void alarm() override { + auto t = static_cast(td::Clocks::system() / 60); + if (t <= attempt_) { + alarm_timestamp() = td::Timestamp::at_unix((attempt_ + 1) * 60); + return; + } + if (waiting_ > 0 && new_result_) { + add_result(); + } + attempt_ = t; + run_queries(); + alarm_timestamp() = td::Timestamp::at_unix((attempt_ + 1) * 60); + } + + public: + std::mutex queue_mutex_; + std::mutex res_mutex_; + std::map> results_; + std::vector addrs_; + static CoreActor* instance_; + td::actor::ActorId self_id_; + + void conn_ready(td::uint32 idx) { + ready_.at(idx) = true; + } + void conn_closed(td::uint32 idx) { + ready_.at(idx) = false; + } + void set_global_config(std::string str) { + global_config_ = str; + } + void set_http_port(td::uint32 port) { + http_port_ = port; + } + void set_remote_addr(td::IPAddress addr) { + remote_addr_ = addr; + } + void set_remote_public_key(td::BufferSlice file_name) { + auto R = [&]() -> td::Result { + TRY_RESULT_PREFIX(conf_data, td::read_file(file_name.as_slice().str()), "failed to read: "); + return ton::PublicKey::import(conf_data.as_slice()); + }(); + + if (R.is_error()) { + LOG(FATAL) << "bad server public key: " << R.move_as_error(); + } + remote_public_key_ = R.move_as_ok(); + } + void set_hide_ips(bool value) { + hide_ips_ = value; + } + + void send_lite_query(td::uint32 idx, td::BufferSlice query, td::Promise promise); + void send_lite_query(td::BufferSlice data, td::Promise promise) override { + return send_lite_query(0, std::move(data), std::move(promise)); + } + void get_last_result(td::Promise> promise) override { + } + void get_results(td::uint32 max, td::Promise promise) override { + RemoteNodeStatusList r; + r.ips = hide_ips_ ? std::vector{addrs_.size()} : addrs_; + auto it = results_.rbegin(); + while (it != results_.rend() && r.results.size() < max) { + r.results.push_back(it->second); + it++; + } + promise.set_value(std::move(r)); + } + + void start_up() override { + instance_ = this; + auto t = td::Clocks::system(); + attempt_ = static_cast(t / 60); + auto next_t = (attempt_ + 1) * 60; + alarm_timestamp() = td::Timestamp::at_unix(next_t); + self_id_ = actor_id(this); + } + void tear_down() override { + if (daemon_) { + MHD_stop_daemon(daemon_); + daemon_ = nullptr; + } + } + + CoreActor() { + } + + static int get_arg_iterate(void* cls, enum MHD_ValueKind kind, const char* key, const char* value) { + auto X = static_cast*>(cls); + if (key && value && std::strlen(key) > 0 && std::strlen(value) > 0) { + X->emplace(key, urldecode(td::Slice{value}, false)); + } + return MHD_YES; + } + + static int process_http_request(void* cls, struct MHD_Connection* connection, const char* url, const char* method, + const char* version, const char* upload_data, size_t* upload_data_size, void** ptr) { + static int dummy; + struct MHD_Response* response = nullptr; + int ret; + + if (0 != std::strcmp(method, "GET")) + return MHD_NO; /* unexpected method */ + if (&dummy != *ptr) { + /* The first time only the headers are valid, + do not respond in the first round... */ + *ptr = &dummy; + return MHD_YES; + } + if (0 != *upload_data_size) + return MHD_NO; /* upload data in a GET!? */ + + std::string url_s = url; + + *ptr = nullptr; /* clear context pointer */ + + auto pos = url_s.rfind('/'); + std::string prefix; + std::string command; + if (pos == std::string::npos) { + prefix = ""; + command = url_s; + } else { + prefix = url_s.substr(0, pos + 1); + command = url_s.substr(pos + 1); + } + + std::map opts; + MHD_get_connection_values(connection, MHD_GET_ARGUMENT_KIND, get_arg_iterate, static_cast(&opts)); + + if (command == "status") { + HttpQueryRunner g{[&](td::Promise promise) { + td::actor::create_actor("blockinfo", opts, prefix, std::move(promise)).release(); + }}; + response = g.wait(); + } else if (command == "block") { + HttpQueryRunner g{[&](td::Promise promise) { + td::actor::create_actor("blockinfo", opts, prefix, std::move(promise)).release(); + }}; + response = g.wait(); + } else if (command == "search") { + if (opts.count("roothash") + opts.count("filehash") > 0) { + HttpQueryRunner g{[&](td::Promise promise) { + td::actor::create_actor("blockinfo", opts, prefix, std::move(promise)).release(); + }}; + response = g.wait(); + } else { + HttpQueryRunner g{[&](td::Promise promise) { + td::actor::create_actor("blocksearch", opts, prefix, std::move(promise)).release(); + }}; + response = g.wait(); + } + } else if (command == "last") { + HttpQueryRunner g{[&](td::Promise promise) { + td::actor::create_actor("", opts, prefix, std::move(promise)).release(); + }}; + response = g.wait(); + } else if (command == "download") { + HttpQueryRunner g{[&](td::Promise promise) { + td::actor::create_actor("downloadblock", opts, prefix, std::move(promise)).release(); + }}; + response = g.wait(); + } else if (command == "viewblock") { + HttpQueryRunner g{[&](td::Promise promise) { + td::actor::create_actor("viewblock", opts, prefix, std::move(promise)).release(); + }}; + response = g.wait(); + } else if (command == "account") { + HttpQueryRunner g{[&](td::Promise promise) { + td::actor::create_actor("viewaccount", opts, prefix, std::move(promise)).release(); + }}; + response = g.wait(); + } else if (command == "transaction") { + HttpQueryRunner g{[&](td::Promise promise) { + td::actor::create_actor("viewtransaction", opts, prefix, std::move(promise)) + .release(); + }}; + response = g.wait(); + } else if (command == "transaction2") { + HttpQueryRunner g{[&](td::Promise promise) { + td::actor::create_actor("viewtransaction2", opts, prefix, std::move(promise)) + .release(); + }}; + response = g.wait(); + } else { + ret = MHD_NO; + } + if (response) { + ret = MHD_queue_response(connection, MHD_HTTP_OK, response); + MHD_destroy_response(response); + } else { + ret = MHD_NO; + } + + return ret; + } + + void run() { + if (remote_public_key_.empty()) { + auto G = td::read_file(global_config_).move_as_ok(); + auto gc_j = td::json_decode(G.as_slice()).move_as_ok(); + ton::ton_api::liteclient_config_global gc; + ton::ton_api::from_json(gc, gc_j.get_object()).ensure(); + + CHECK(gc.liteservers_.size() > 0); + td::uint32 size = static_cast(gc.liteservers_.size()); + ready_.resize(size, false); + + for (td::uint32 i = 0; i < size; i++) { + auto& cli = gc.liteservers_[i]; + td::IPAddress addr; + addr.init_host_port(td::IPAddress::ipv4_to_str(cli->ip_), cli->port_).ensure(); + addrs_.push_back(addr); + clients_.emplace_back(ton::adnl::AdnlExtClient::create(ton::adnl::AdnlNodeIdFull::create(cli->id_).move_as_ok(), + addr, make_callback(i))); + } + } else { + if (!remote_addr_.is_valid()) { + LOG(FATAL) << "remote addr not set"; + } + ready_.resize(1, false); + addrs_.push_back(remote_addr_); + clients_.emplace_back(ton::adnl::AdnlExtClient::create(ton::adnl::AdnlNodeIdFull{remote_public_key_}, + remote_addr_, make_callback(0))); + } + daemon_ = MHD_start_daemon(MHD_USE_THREAD_PER_CONNECTION, static_cast(http_port_), nullptr, nullptr, + &process_http_request, nullptr, MHD_OPTION_END); + CHECK(daemon_ != nullptr); + } +}; + +void CoreActor::got_result(td::uint32 idx, td::int32 attempt, td::Result R) { + if (attempt != attempt_) { + return; + } + if (R.is_error()) { + waiting_--; + if (waiting_ == 0) { + add_result(); + } + return; + } + auto data = R.move_as_ok(); + { + auto F = ton::fetch_tl_object(data.clone(), true); + if (F.is_ok()) { + auto f = F.move_as_ok(); + auto err = td::Status::Error(f->code_, f->message_); + waiting_--; + if (waiting_ == 0) { + add_result(); + } + return; + } + } + auto F = ton::fetch_tl_object(std::move(data), true); + if (F.is_error()) { + waiting_--; + if (waiting_ == 0) { + add_result(); + } + return; + } + auto f = F.move_as_ok(); + new_result_->values_[idx] = ton::create_block_id(f->last_); + waiting_--; + CHECK(waiting_ >= 0); + if (waiting_ == 0) { + add_result(); + } +} + +void CoreActor::send_query(td::uint32 idx) { + if (!ready_[idx]) { + return; + } + waiting_++; + auto query = ton::create_tl_object(); + auto q = ton::create_tl_object(serialize_tl_object(query, true)); + + auto P = + td::PromiseCreator::lambda([SelfId = actor_id(this), idx, attempt = attempt_](td::Result R) { + td::actor::send_closure(SelfId, &CoreActor::got_result, idx, attempt, std::move(R)); + }); + td::actor::send_closure(clients_[idx], &ton::adnl::AdnlExtClient::send_query, "query", serialize_tl_object(q, true), + td::Timestamp::in(10.0), std::move(P)); +} + +void CoreActor::run_queries() { + waiting_ = 0; + new_result_ = std::make_shared(ready_.size(), td::Timestamp::at_unix(attempt_ * 60)); + for (td::uint32 i = 0; i < ready_.size(); i++) { + send_query(i); + } + CHECK(waiting_ >= 0); + if (waiting_ == 0) { + add_result(); + } +} + +void CoreActor::send_lite_query(td::uint32 idx, td::BufferSlice query, td::Promise promise) { + if (!ready_[idx]) { + promise.set_error(td::Status::Error(ton::ErrorCode::notready, "ext conn not ready")); + return; + } + auto P = td::PromiseCreator::lambda([promise = std::move(promise)](td::Result R) mutable { + if (R.is_error()) { + promise.set_error(R.move_as_error()); + return; + } + auto B = R.move_as_ok(); + { + auto F = ton::fetch_tl_object(B.clone(), true); + if (F.is_ok()) { + auto f = F.move_as_ok(); + promise.set_error(td::Status::Error(f->code_, f->message_)); + return; + } + } + promise.set_value(std::move(B)); + }); + auto q = ton::create_tl_object(std::move(query)); + td::actor::send_closure(clients_[idx], &ton::adnl::AdnlExtClient::send_query, "query", serialize_tl_object(q, true), + td::Timestamp::in(10.0), std::move(P)); +} + +td::actor::ActorId CoreActorInterface::instance_actor_id() { + auto instance = CoreActor::instance_; + CHECK(instance); + return instance->self_id_; +} + +CoreActor* CoreActor::instance_ = nullptr; + +int main(int argc, char* argv[]) { + SET_VERBOSITY_LEVEL(verbosity_INFO); + td::set_default_failure_signal_handler().ensure(); + + td::actor::ActorOwn x; + + td::OptionsParser p; + p.set_description("TON Blockchain explorer"); + p.add_option('h', "help", "prints_help", [&]() { + char b[10240]; + td::StringBuilder sb(td::MutableSlice{b, 10000}); + sb << p; + std::cout << sb.as_cslice().c_str(); + std::exit(2); + return td::Status::OK(); + }); + p.add_option('I', "hide-ips", "hides ips from status", [&]() { + td::actor::send_closure(x, &CoreActor::set_hide_ips, true); + return td::Status::OK(); + }); + p.add_option('u', "user", "change user", [&](td::Slice user) { return td::change_user(user); }); + p.add_option('C', "global-config", "file to read global config", [&](td::Slice fname) { + td::actor::send_closure(x, &CoreActor::set_global_config, fname.str()); + return td::Status::OK(); + }); + p.add_option('a', "addr", "connect to ip:port", [&](td::Slice arg) { + td::IPAddress addr; + TRY_STATUS(addr.init_host_port(arg.str())); + td::actor::send_closure(x, &CoreActor::set_remote_addr, addr); + return td::Status::OK(); + }); + p.add_option('p', "pub", "remote public key", [&](td::Slice arg) { + td::actor::send_closure(x, &CoreActor::set_remote_public_key, td::BufferSlice{arg}); + return td::Status::OK(); + }); + p.add_option('v', "verbosity", "set verbosity level", [&](td::Slice arg) { + verbosity = td::to_integer(arg); + SET_VERBOSITY_LEVEL(VERBOSITY_NAME(FATAL) + verbosity); + return (verbosity >= 0 && verbosity <= 9) ? td::Status::OK() : td::Status::Error("verbosity must be 0..9"); + }); + p.add_option('d', "daemonize", "set SIGHUP", [&]() { + td::set_signal_handler(td::SignalType::HangUp, [](int sig) { +#if TD_DARWIN || TD_LINUX + close(0); + setsid(); +#endif + }).ensure(); + return td::Status::OK(); + }); + p.add_option('H', "http-port", "listen on http port", [&](td::Slice arg) { + td::actor::send_closure(x, &CoreActor::set_http_port, td::to_integer(arg)); + return td::Status::OK(); + }); +#if TD_DARWIN || TD_LINUX + p.add_option('l', "logname", "log to file", [&](td::Slice fname) { + auto FileLog = td::FileFd::open(td::CSlice(fname.str().c_str()), + td::FileFd::Flags::Create | td::FileFd::Flags::Append | td::FileFd::Flags::Write) + .move_as_ok(); + + dup2(FileLog.get_native_fd().fd(), 1); + dup2(FileLog.get_native_fd().fd(), 2); + return td::Status::OK(); + }); +#endif + + td::actor::Scheduler scheduler({2}); + scheduler_ptr = &scheduler; + scheduler.run_in_context([&] { x = td::actor::create_actor("testnode"); }); + + scheduler.run_in_context([&] { p.run(argc, argv).ensure(); }); + scheduler.run_in_context([&] { + td::actor::send_closure(x, &CoreActor::run); + x.release(); + }); + scheduler.run(); + + return 0; +} diff --git a/blockchain-explorer/blockchain-explorer.hpp b/blockchain-explorer/blockchain-explorer.hpp new file mode 100644 index 00000000..84b86658 --- /dev/null +++ b/blockchain-explorer/blockchain-explorer.hpp @@ -0,0 +1,56 @@ +/* + This file is part of TON Blockchain source code. + + TON Blockchain is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + TON Blockchain is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with TON Blockchain. If not, see . + + In addition, as a special exception, the copyright holders give permission + to link the code of portions of this program with the OpenSSL library. + You must obey the GNU General Public License in all respects for all + of the code used other than OpenSSL. If you modify file(s) with this + exception, you may extend this exception to your version of the file(s), + but you are not obligated to do so. If you do not wish to do so, delete this + exception statement from your version. If you delete this exception statement + from all source files in the program, then also delete it here. + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +#include "td/actor/actor.h" +#include "td/utils/buffer.h" +#include "ton/ton-types.h" +#include "td/utils/port/IPAddress.h" + +class CoreActorInterface : public td::actor::Actor { + public: + struct RemoteNodeStatus { + std::vector values_; + td::Timestamp ts_; + RemoteNodeStatus(size_t size, td::Timestamp ts) : ts_(ts) { + values_.resize(size); + } + }; + + struct RemoteNodeStatusList { + std::vector ips; + std::vector> results; + }; + virtual ~CoreActorInterface() = default; + + virtual void send_lite_query(td::BufferSlice data, td::Promise promise) = 0; + virtual void get_last_result(td::Promise> promise) = 0; + virtual void get_results(td::uint32 max, td::Promise promise) = 0; + + static td::actor::ActorId instance_actor_id(); +}; diff --git a/catchain/CMakeLists.txt b/catchain/CMakeLists.txt new file mode 100644 index 00000000..a57d3788 --- /dev/null +++ b/catchain/CMakeLists.txt @@ -0,0 +1,39 @@ +cmake_minimum_required(VERSION 3.0.2 FATAL_ERROR) + +if (NOT OPENSSL_FOUND) + find_package(OpenSSL REQUIRED) +endif() + +set(CATCHAIN_SOURCE + catchain-received-block.cpp + #catchain-receiver-fork.cpp + catchain-receiver-source.cpp + catchain-receiver.cpp + catchain-block.cpp + catchain.cpp + + catchain-block.hpp + catchain-received-block.h + catchain-received-block.hpp + #catchain-receiver-fork.h + #catchain-receiver-fork.hpp + catchain-receiver-interface.h + catchain-receiver-source.h + catchain-receiver-source.hpp + catchain-receiver.h + catchain-receiver.hpp + catchain-types.h + catchain.h + catchain.hpp +) + +add_library(catchain STATIC ${CATCHAIN_SOURCE}) + +target_include_directories(overlay PUBLIC + $ + $/.. + ${OPENSSL_INCLUDE_DIR} +) +target_link_libraries(catchain PRIVATE tdutils tdactor adnl tl_api dht tdfec + overlay) + diff --git a/catchain/catchain-block.cpp b/catchain/catchain-block.cpp new file mode 100644 index 00000000..10c04eb7 --- /dev/null +++ b/catchain/catchain-block.cpp @@ -0,0 +1,59 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "catchain-block.hpp" + +namespace ton { + +namespace catchain { + +std::unique_ptr CatChainBlock::create(td::uint32 src, td::uint32 fork, PublicKeyHash src_hash, + CatChainBlockHeight height, CatChainBlockHash hash, + td::SharedSlice payload, CatChainBlock *prev, + std::vector deps, + std::vector vt) { + return std::make_unique(src, fork, src_hash, height, hash, std::move(payload), prev, + std::move(deps), std::move(vt)); +} + +CatChainBlockImpl::CatChainBlockImpl(td::uint32 src, td::uint32 fork, PublicKeyHash src_hash, + CatChainBlockHeight height, CatChainBlockHash hash, td::SharedSlice payload, + CatChainBlock *prev, std::vector deps, + std::vector vt) + : src_(src) + , fork_(fork) + , src_hash_(src_hash) + , height_(height) + , hash_(hash) + , payload_(std::move(payload)) + , prev_(prev) + , deps_(std::move(deps)) + , vt_(std::move(vt)) { +} + +bool CatChainBlockImpl::is_descendant_of(CatChainBlock *block) { + auto fork = block->fork(); + if (fork >= vt_.size()) { + return false; + } + return block->height() <= vt_[fork]; +} + +} // namespace catchain + +} // namespace ton diff --git a/catchain/catchain-block.hpp b/catchain/catchain-block.hpp new file mode 100644 index 00000000..c6e10348 --- /dev/null +++ b/catchain/catchain-block.hpp @@ -0,0 +1,113 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +#include "catchain.h" + +namespace ton { + +namespace catchain { + +class CatChainBlockImpl : public CatChainBlock { + private: + std::unique_ptr extra_; + td::uint32 src_; + td::uint32 fork_; + PublicKeyHash src_hash_; + CatChainBlockHeight height_; + CatChainBlockHash hash_; + td::SharedSlice payload_; + CatChainBlock *prev_; + std::vector deps_; + std::vector vt_; + bool preprocess_sent_ = false; + bool is_processed_ = false; + + public: + td::SharedSlice &payload() override { + return payload_; + } + const td::SharedSlice &payload() const override { + return payload_; + } + + CatChainBlock::Extra *extra() const override { + return extra_.get(); + } + std::unique_ptr move_extra() override { + return std::move(extra_); + } + void set_extra(std::unique_ptr extra) override { + extra_ = std::move(extra); + } + + td::uint32 source() const override { + return src_; + } + td::uint32 fork() const override { + return fork_; + } + PublicKeyHash source_hash() const override { + return src_hash_; + } + CatChainBlockHash hash() const override { + return hash_; + } + CatChainBlockHeight height() const override { + return height_; + } + + CatChainBlock *prev() override { + return prev_; + } + const CatChainBlock *prev() const override { + return prev_; + } + + const std::vector &deps() const override { + return deps_; + } + const std::vector &vt() const override { + return vt_; + } + + bool preprocess_is_sent() const override { + return preprocess_sent_; + } + void preprocess_sent() override { + preprocess_sent_ = true; + } + + bool is_processed() const override { + return is_processed_; + } + void set_processed() override { + is_processed_ = true; + } + + bool is_descendant_of(CatChainBlock *block) override; + + CatChainBlockImpl(td::uint32 src, td::uint32 fork, PublicKeyHash src_hash, CatChainBlockHeight height, + CatChainBlockHash hash, td::SharedSlice payload, CatChainBlock *prev, + std::vector deps, std::vector vt); +}; + +} // namespace catchain + +} // namespace ton diff --git a/catchain/catchain-received-block.cpp b/catchain/catchain-received-block.cpp new file mode 100644 index 00000000..711ee29c --- /dev/null +++ b/catchain/catchain-received-block.cpp @@ -0,0 +1,523 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include + +#include "catchain-received-block.hpp" +#include "catchain-receiver-source.h" + +#include "auto/tl/ton_api.hpp" + +namespace ton { + +namespace catchain { + +void CatChainReceivedBlockImpl::initialize(tl_object_ptr block, td::SharedSlice payload) { + if (state_ != bs_none) { + return; + } + + payload_ = std::move(payload); + CHECK(payload_.size() > 0); + + prev_ = dynamic_cast(chain_->create_block(std::move(block->data_->prev_))); + CHECK(prev_ != nullptr); + for (auto &X : block->data_->deps_) { + auto B = dynamic_cast(chain_->create_block(std::move(X))); + CHECK(B != nullptr); + block_deps_.push_back(B); + } + signature_ = td::SharedSlice{block->signature_.as_slice()}; + + state_ = bs_initialized; + VLOG(CATCHAIN_DEBUG) << this << ": initialized payload_size=" << payload_.size(); + + if (prev_->is_ill()) { + set_ill(); + return; + } + for (auto &X : block_deps_) { + if (X->is_ill()) { + set_ill(); + return; + } + } + + td::uint32 pending_deps = 0; + { + if (!prev_->delivered()) { + pending_deps++; + } else { + update_deps(prev_); + } + if (!prev_->delivered()) { + prev_->add_rev_dep(this); + } + } + for (auto &X : block_deps_) { + if (!X->delivered()) { + pending_deps++; + } else { + update_deps(X); + } + if (!X->delivered()) { + X->add_rev_dep(this); + } + } + pending_deps_ = pending_deps; + if (pending_deps_ == 0 && in_db_) { + schedule(); + } + + chain_->get_source(source_id_)->block_received(height_); +} + +void CatChainReceivedBlockImpl::run() { + if (is_ill()) { + return; + } + if (state_ == bs_none) { + return; + } + if (state_ == bs_delivered) { + return; + } + CHECK(state_ == bs_initialized); + CHECK(!pending_deps_); + CHECK(in_db_); + + initialize_fork(); + pre_deliver(); + deliver(); +} + +void CatChainReceivedBlockImpl::initialize_fork() { + CHECK(state_ == bs_initialized); + CHECK(!fork_id_); + CatChainReceiverSource *S = chain_->get_source(source_id_); + if (height_ == 1) { + fork_id_ = S->add_fork(); + } else { + if (!prev_->next_) { + prev_->next_ = this; + fork_id_ = prev_->fork_id_; + } else { + fork_id_ = S->add_fork(); + } + } + + if (deps_.size() < fork_id_ + 1) { + deps_.resize(fork_id_ + 1, 0); + } + CHECK(deps_[fork_id_] < height_); + deps_[fork_id_] = height_; +} + +void CatChainReceivedBlockImpl::pre_deliver(ton_api::catchain_block_data_fork &b) { + { + td::Status S; + S = chain_->validate_block_sync(b.left_); + if (S.is_error()) { + VLOG(CATCHAIN_WARNING) << this << ": incorrect fork blame: left is invalid: " << S.move_as_error(); + set_ill(); + return; + } + S = chain_->validate_block_sync(b.right_); + if (S.is_error()) { + VLOG(CATCHAIN_WARNING) << this << ": incorrect fork blame: right is invalid: " << S.move_as_error(); + set_ill(); + return; + } + } + + // block is incorrect, since blocks are + if (b.left_->height_ != b.right_->height_ || b.left_->src_ != b.right_->src_ || + b.left_->data_hash_ == b.right_->data_hash_) { + VLOG(CATCHAIN_WARNING) << this << ": incorrect fork blame: not a fork"; + set_ill(); + return; + } + + auto S = chain_->get_source(b.left_->src_); + S->on_found_fork_proof( + create_serialize_tl_object(std::move(b.left_), std::move(b.right_))); + S->blame(fork_id_, height_); +} + +void CatChainReceivedBlockImpl::pre_deliver(ton_api::catchain_block_data_badBlock &b) { +} + +void CatChainReceivedBlockImpl::pre_deliver(ton_api::catchain_block_data_nop &b) { +} + +void CatChainReceivedBlockImpl::pre_deliver() { + if (is_ill()) { + return; + } + CHECK(state_ == bs_initialized); + CHECK(pending_deps_ == 0); + CHECK(in_db_); + + auto M = chain_->get_source(source_id_); + + auto d = prev_ ? &prev_->deps_ : nullptr; + + for (auto &X : block_deps_) { + auto S = chain_->get_source(X->get_source_id()); + auto &f = S->get_forks(); + if (d) { + auto &dd = *d; + if (X->get_fork_id() < dd.size() && X->get_height() <= dd[X->get_fork_id()]) { + VLOG(CATCHAIN_WARNING) << this << ": has direct dep from source " << X->get_source_id() << " and prev block " + << " has newer indirect dep"; + set_ill(); + return; + } + } + if (S->blamed() && d) { + auto &dd = *d; + for (auto x : f) { + if (x != X->get_fork_id() && dd.size() > x && dd[x] > 0) { + VLOG(CATCHAIN_WARNING) << this << ": has direct dep from source " << X->get_source_id() << " and prev block " + << " has indirect dep to another fork of this source " << x << " " << X->get_fork_id() + << " " << dd[x] << " " << f; + M->blame(fork_id_, height_); + set_ill(); + return; + } + } + auto v = S->get_blamed_heights(); + + for (size_t i = 0; i < v.size() && i < dd.size(); i++) { + if (v[i] > 0 && dd[i] >= v[i]) { + VLOG(CATCHAIN_WARNING) << this << ": has direct dep from source " << X->get_source_id() << " and prev block " + << " has indirect dep to block f" << i << "@" << v[i] + << " which is known to blame this source"; + M->blame(fork_id_, height_); + set_ill(); + return; + } + } + } + } + + auto X = fetch_tl_object(payload_.as_slice(), true); + if (X.is_error()) { + is_custom_ = true; + } else { + ton_api::downcast_call(*X.move_as_ok().get(), [Self = this](auto &obj) { Self->pre_deliver(obj); }); + } +} + +void CatChainReceivedBlockImpl::deliver() { + if (is_ill()) { + return; + } + CHECK(state_ == bs_initialized); + CHECK(pending_deps_ == 0); + CHECK(in_db_); + + chain_->deliver_block(this); + + state_ = bs_delivered; + VLOG(CATCHAIN_DEBUG) << this << ": delivered"; + + for (auto &B : rev_deps_) { + B->dep_delivered(this); + } + rev_deps_.clear(); + + chain_->get_source(source_id_)->block_delivered(height_); +} + +void CatChainReceivedBlockImpl::set_ill() { + if (state_ == bs_ill) { + return; + } + VLOG(CATCHAIN_WARNING) << this << ": got ill"; + auto M = chain_->get_source(source_id_); + M->blame(); + state_ = bs_ill; + for (auto &B : rev_deps_) { + B->dep_ill(this); + } +} + +void CatChainReceivedBlockImpl::dep_ill(CatChainReceivedBlockImpl *block) { + set_ill(); +} + +void CatChainReceivedBlockImpl::update_deps(CatChainReceivedBlockImpl *block) { + auto &d = block->deps_; + if (d.size() > deps_.size()) { + deps_.resize(d.size(), 0); + } + for (size_t i = 0; i < d.size(); i++) { + if (deps_[i] < d[i]) { + deps_[i] = d[i]; + } + } +} + +void CatChainReceivedBlockImpl::dep_delivered(CatChainReceivedBlockImpl *block) { + if (is_ill()) { + return; + } + CHECK(!block->is_ill()); + update_deps(block); + pending_deps_--; + if (pending_deps_ == 0 && in_db_) { + schedule(); + } +} + +void CatChainReceivedBlockImpl::written() { + if (!in_db_) { + in_db_ = true; + if (pending_deps_ == 0) { + schedule(); + } + } +} + +void CatChainReceivedBlockImpl::add_rev_dep(CatChainReceivedBlockImpl *block) { + rev_deps_.push_back(block); +} + +CatChainReceivedBlock *CatChainReceivedBlockImpl::get_prev() const { + return prev_; +} + +CatChainBlockHash CatChainReceivedBlockImpl::get_prev_hash() const { + CHECK(prev_); + return prev_->get_hash(); +} + +std::vector CatChainReceivedBlockImpl::get_dep_hashes() const { + std::vector v; + v.resize(block_deps_.size()); + for (size_t i = 0; i < v.size(); i++) { + v[i] = block_deps_[i]->get_hash(); + } + return v; +} + +void CatChainReceivedBlockImpl::schedule() { + chain_->run_block(this); +} + +void CatChainReceivedBlockImpl::find_pending_deps(std::vector &vec, td::uint32 max_size) const { + if (height_ == 0 || is_ill() || delivered() || vec.size() == max_size) { + return; + } + if (!initialized()) { + vec.push_back(get_hash()); + return; + } + if (prev_) { + prev_->find_pending_deps(vec, max_size); + } + for (auto &X : block_deps_) { + X->find_pending_deps(vec, max_size); + } +} + +tl_object_ptr CatChainReceivedBlock::block_id(CatChainReceiver *chain, + tl_object_ptr &block, + td::Slice payload) { + return create_tl_object(block->incarnation_, chain->get_source_hash(block->src_).tl(), + block->height_, sha256_bits256(payload)); +} +tl_object_ptr CatChainReceivedBlock::block_id( + CatChainReceiver *chain, tl_object_ptr &block) { + return create_tl_object( + chain->get_incarnation(), chain->get_source_hash(block->src_).tl(), block->height_, block->data_hash_); +} + +CatChainBlockHash CatChainReceivedBlock::block_hash(CatChainReceiver *chain, + tl_object_ptr &block, td::Slice payload) { + return get_tl_object_sha_bits256(block_id(chain, block, payload)); +} + +CatChainBlockHash CatChainReceivedBlock::block_hash(CatChainReceiver *chain, + tl_object_ptr &block) { + return get_tl_object_sha_bits256(block_id(chain, block)); +} + +td::Status CatChainReceivedBlock::pre_validate_block(CatChainReceiver *chain, + tl_object_ptr &block, td::Slice payload) { + CHECK(block->incarnation_ == chain->get_incarnation()); + if (block->height_ <= 0) { + return td::Status::Error(ErrorCode::protoviolation, std::string("bad height ") + std::to_string(block->height_)); + } + if (block->src_ < 0 || static_cast(block->src_) >= chain->get_sources_cnt()) { + return td::Status::Error(ErrorCode::protoviolation, std::string("bad src ") + std::to_string(block->src_)); + } + + if (block->data_->prev_->src_ < 0) { + return td::Status::Error(ErrorCode::protoviolation, + std::string("bad prev block src ") + std::to_string(block->data_->prev_->src_)); + } + if (block->data_->deps_.size() > chain->opts().max_deps) { + return td::Status::Error(ErrorCode::protoviolation, std::string("too many deps")); + } + auto prev_src = static_cast(block->data_->prev_->src_); + + if (block->height_ > 1) { + if (prev_src != static_cast(block->src_)) { + return td::Status::Error(ErrorCode::protoviolation, + std::string("bad prev block src ") + std::to_string(block->data_->prev_->src_)); + } + } else { + if (prev_src != chain->get_sources_cnt()) { + return td::Status::Error(ErrorCode::protoviolation, + std::string("bad prev(first) block src ") + std::to_string(block->data_->prev_->src_)); + } + } + if (block->data_->prev_->height_ + 1 != block->height_) { + return td::Status::Error(ErrorCode::protoviolation, std::string("bad prev block height ") + + std::to_string(block->data_->prev_->height_) + " (our " + + std::to_string(block->height_) + ")"); + } + + std::set used; + used.insert(block->src_); + for (auto &X : block->data_->deps_) { + if (used.find(X->src_) != used.end()) { + return td::Status::Error(ErrorCode::protoviolation, "two deps from same source"); + } + used.insert(X->src_); + } + + TRY_STATUS(chain->validate_block_sync(block->data_->prev_)); + for (auto &X : block->data_->deps_) { + TRY_STATUS(chain->validate_block_sync(X)); + } + + if (payload.size() == 0) { + return td::Status::Error(ErrorCode::protoviolation, "empty payload"); + } + + return td::Status::OK(); +} + +td::Status CatChainReceivedBlock::pre_validate_block(CatChainReceiver *chain, + tl_object_ptr &block) { + if (block->height_ < 0) { + return td::Status::Error(ErrorCode::protoviolation, std::string("bad height ") + std::to_string(block->height_)); + } + if (block->height_ > 0) { + if (block->src_ < 0 || static_cast(block->src_) >= chain->get_sources_cnt()) { + return td::Status::Error(ErrorCode::protoviolation, std::string("bad src ") + std::to_string(block->src_)); + } + } else { + if (block->src_ < 0 || static_cast(block->src_) != chain->get_sources_cnt()) { + return td::Status::Error(ErrorCode::protoviolation, + std::string("bad src (first block) ") + std::to_string(block->src_)); + } + if (block->data_hash_ != chain->get_incarnation() || block->signature_.size() != 0) { + return td::Status::Error(ErrorCode::protoviolation, std::string("bad first block")); + } + } + + return td::Status::OK(); +} + +tl_object_ptr CatChainReceivedBlockImpl::export_tl() const { + CHECK(initialized()); + CHECK(height_ > 0); + std::vector> deps; + + for (auto &B : block_deps_) { + deps.push_back(B->export_tl_dep()); + } + + return create_tl_object( + chain_->get_incarnation(), source_id_, height_, + create_tl_object(prev_->export_tl_dep(), std::move(deps)), + signature_.clone_as_buffer_slice()); +} + +tl_object_ptr CatChainReceivedBlockImpl::export_tl_dep() const { + return create_tl_object(source_id_, height_, data_hash_, + signature_.clone_as_buffer_slice()); +} + +CatChainReceivedBlockImpl::CatChainReceivedBlockImpl(td::uint32 source_id, CatChainSessionId hash, + CatChainReceiver *chain) { + chain_ = chain; + state_ = bs_delivered; + fork_id_ = 0; + source_id_ = source_id; + data_ = nullptr; + prev_ = nullptr; + height_ = 0; + + data_hash_ = hash; + hash_ = get_tl_object_sha_bits256(create_tl_object( + chain->get_incarnation(), chain->get_incarnation(), height_, data_hash_)); +} + +CatChainReceivedBlockImpl::CatChainReceivedBlockImpl(tl_object_ptr block, + td::SharedSlice payload, CatChainReceiver *chain) { + chain_ = chain; + data_hash_ = sha256_bits256(payload.as_slice()); + hash_ = get_tl_object_sha_bits256(create_tl_object( + block->incarnation_, chain->get_source_hash(block->src_).tl(), block->height_, data_hash_)); + height_ = block->height_; + source_id_ = block->src_; + + auto S = chain_->get_source(source_id_); + S->on_new_block(this); + + initialize(std::move(block), std::move(payload)); +} + +CatChainReceivedBlockImpl::CatChainReceivedBlockImpl(tl_object_ptr block, + CatChainReceiver *chain) { + chain_ = chain; + data_hash_ = block->data_hash_; + source_id_ = block->src_; + signature_ = td::SharedSlice{block->signature_.as_slice()}; + hash_ = get_tl_object_sha_bits256(create_tl_object( + chain_->get_incarnation(), chain_->get_source_hash(source_id_).tl(), block->height_, data_hash_)); + height_ = block->height_; + + auto S = chain_->get_source(source_id_); + S->on_new_block(this); +} + +std::unique_ptr CatChainReceivedBlock::create(tl_object_ptr block, + td::SharedSlice payload, CatChainReceiver *chain) { + return std::make_unique(std::move(block), std::move(payload), chain); +} + +std::unique_ptr CatChainReceivedBlock::create(tl_object_ptr block, + CatChainReceiver *chain) { + return std::make_unique(std::move(block), chain); +} + +std::unique_ptr CatChainReceivedBlock::create_root(td::uint32 source_id, + CatChainSessionId data_hash, + CatChainReceiver *chain) { + return std::make_unique(source_id, data_hash, chain); +} + +} // namespace catchain + +} // namespace ton diff --git a/catchain/catchain-received-block.h b/catchain/catchain-received-block.h new file mode 100644 index 00000000..fc1d5522 --- /dev/null +++ b/catchain/catchain-received-block.h @@ -0,0 +1,114 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +#include "td/utils/buffer.h" +#include "td/utils/SharedSlice.h" +#include "auto/tl/ton_api.h" + +#include "catchain/catchain-receiver.h" + +namespace ton { + +namespace catchain { + +class CatChainReceiver; +class CatChainReceiverSource; +class CatChainReceiverFork; + +class CatChainReceivedBlock { + public: + // getters + virtual const td::SharedSlice &get_payload() const = 0; + virtual CatChainBlockHash get_hash() const = 0; + virtual const td::SharedSlice &get_signature() const = 0; + + virtual CatChainBlockHeight get_height() const = 0; + virtual CatChainReceivedBlock *get_prev() const = 0; + virtual CatChainBlockHash get_prev_hash() const = 0; + + virtual const std::vector &get_deps() const = 0; + virtual std::vector get_dep_hashes() const = 0; + + virtual CatChainReceiver *get_chain() const = 0; + + virtual td::uint32 get_fork_id() const = 0; + virtual td::uint32 get_source_id() const = 0; + + virtual tl_object_ptr export_tl() const = 0; + virtual tl_object_ptr export_tl_dep() const = 0; + + virtual void find_pending_deps(std::vector &vec, td::uint32 max_size) const = 0; + + public: + // state + virtual bool initialized() const = 0; + virtual bool delivered() const = 0; + virtual bool is_ill() const = 0; + virtual bool is_custom() const = 0; + virtual bool in_db() const = 0; + + public: + //change state + virtual void initialize(tl_object_ptr block, td::SharedSlice payload) = 0; + virtual void set_ill() = 0; + virtual void written() = 0; + virtual void run() = 0; + + public: + static std::unique_ptr create(tl_object_ptr block, + td::SharedSlice payload, CatChainReceiver *chain); + static std::unique_ptr create(tl_object_ptr block, + CatChainReceiver *chain); + static std::unique_ptr create_root(td::uint32 source_id, CatChainBlockPayloadHash data_hash, + CatChainReceiver *chain); + + static tl_object_ptr block_id(CatChainReceiver *chain, + tl_object_ptr &block, + td::Slice payload); + static tl_object_ptr block_id(CatChainReceiver *chain, + tl_object_ptr &block); + static CatChainBlockHash block_hash(CatChainReceiver *chain, tl_object_ptr &block, + td::Slice payload); + static CatChainBlockHash block_hash(CatChainReceiver *chain, tl_object_ptr &block); + static td::Status pre_validate_block(CatChainReceiver *chain, tl_object_ptr &block, + td::Slice payload); + static td::Status pre_validate_block(CatChainReceiver *chain, tl_object_ptr &block); + + virtual ~CatChainReceivedBlock() = default; +}; + +} // namespace catchain + +} // namespace ton + +namespace td { + +inline td::StringBuilder &operator<<(td::StringBuilder &sb, const ton::catchain::CatChainReceivedBlock &block) { + sb << "[block " << block.get_chain()->get_incarnation() << " " << block.get_source_id() << " " << block.get_fork_id() + << " " << block.get_hash() << "]"; + return sb; +} +inline td::StringBuilder &operator<<(td::StringBuilder &sb, const ton::catchain::CatChainReceivedBlock *block) { + sb << *block; + return sb; +} + +} // namespace td + diff --git a/catchain/catchain-received-block.hpp b/catchain/catchain-received-block.hpp new file mode 100644 index 00000000..914fa91c --- /dev/null +++ b/catchain/catchain-received-block.hpp @@ -0,0 +1,179 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +#include "catchain/catchain-received-block.h" + +namespace ton { + +namespace catchain { + +class CatChainReceiver; +class CatChainReceiverSource; +class CatChainReceiverFork; + +class CatChainReceivedBlockImpl : public CatChainReceivedBlock { + public: + const td::SharedSlice &get_payload() const override { + return payload_; + } + CatChainBlockHash get_hash() const override { + return hash_; + } + const td::SharedSlice &get_signature() const override { + return signature_; + } + + CatChainBlockHeight get_height() const override { + return height_; + } + CatChainReceivedBlock *get_prev() const override; + CatChainBlockHash get_prev_hash() const override; + + const std::vector &get_deps() const override { + return deps_; + } + + std::vector get_dep_hashes() const override; + + CatChainReceiver *get_chain() const override { + return chain_; + } + + td::uint32 get_fork_id() const override { + return fork_id_; + } + + td::uint32 get_source_id() const override { + return source_id_; + } + + tl_object_ptr export_tl() const override; + tl_object_ptr export_tl_dep() const override; + + void find_pending_deps(std::vector &vec, td::uint32 max_size) const override; + + public: + bool initialized() const override { + return state_ >= bs_initialized; + } + bool delivered() const override { + return state_ >= bs_delivered; + } + bool is_ill() const override { + return state_ == bs_ill; + } + bool is_custom() const override { + return is_custom_; + } + bool in_db() const override { + return in_db_; + } + + public: + void initialize(tl_object_ptr block, td::SharedSlice payload) override; + + void run() override; + void pre_deliver(ton_api::catchain_block_data_fork &b); + void pre_deliver(ton_api::catchain_block_data_badBlock &b); + void pre_deliver(ton_api::catchain_block_data_nop &b); + template + void pre_deliver(T &b) { + // do nothing, it is custom block + is_custom_ = true; + } + void pre_deliver(); + void deliver(); + + void dep_delivered(CatChainReceivedBlockImpl *block); + void dep_ill(CatChainReceivedBlockImpl *block); + + void set_ill() override; + void schedule(); + + void written() override; + + public: + CatChainReceivedBlockImpl(tl_object_ptr block, td::SharedSlice payload, + CatChainReceiver *chain); + CatChainReceivedBlockImpl(tl_object_ptr block, CatChainReceiver *chain); + + CatChainReceivedBlockImpl(td::uint32 source_id, CatChainSessionId hash, CatChainReceiver *chain); + + private: + enum State { + bs_none, + bs_ill, + bs_initialized, + bs_delivered, + } state_ = bs_none; + + void update_deps(CatChainReceivedBlockImpl *block); + + void add_rev_dep(CatChainReceivedBlockImpl *block); + void add_child_dep(CatChainReceivedBlockImpl *block); + + void initialize_fork(); + void on_ready_to_deliver(); + + td::uint32 fork_id_{0}; + td::uint32 source_id_; + CatChainReceiver *chain_; + + tl_object_ptr data_; + td::SharedSlice payload_; + + CatChainBlockHash hash_; + CatChainBlockPayloadHash data_hash_; + + CatChainReceivedBlockImpl *prev_; + CatChainBlockHeight height_; + + CatChainReceivedBlockImpl *next_ = nullptr; + + std::vector block_deps_; + std::vector deps_; + + td::SharedSlice signature_; + + std::vector rev_deps_; + + td::uint32 pending_deps_ = 0; + + bool is_custom_ = false; + bool in_db_ = false; +}; + +} // namespace catchain + +} // namespace ton + +namespace td { + +inline td::StringBuilder &operator<<(td::StringBuilder &sb, const ton::catchain::CatChainReceivedBlockImpl &block) { + sb << "[block " << block.get_chain()->get_incarnation() << " " << block.get_source_id() << " " << block.get_fork_id() + << " " << block.get_hash() << "]"; + return sb; +} +inline td::StringBuilder &operator<<(td::StringBuilder &sb, const ton::catchain::CatChainReceivedBlockImpl *block) { + sb << *block; + return sb; +} + +} // namespace td diff --git a/catchain/catchain-receiver-interface.h b/catchain/catchain-receiver-interface.h new file mode 100644 index 00000000..9580c3fb --- /dev/null +++ b/catchain/catchain-receiver-interface.h @@ -0,0 +1,70 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +#include "td/actor/actor.h" +#include "adnl/adnl.h" +#include "overlay/overlays.h" +#include "catchain-types.h" + +namespace ton { + +namespace catchain { + +class CatChainReceiverInterface : public td::actor::Actor { + public: + class Callback { + public: + virtual void new_block(td::uint32 src_id, td::uint32 fork_id, CatChainBlockHash hash, CatChainBlockHeight height, + CatChainBlockHash prev, std::vector deps, + std::vector vt, td::SharedSlice data) = 0; + virtual void blame(td::uint32 src_id) = 0; + virtual void on_custom_message(PublicKeyHash src, td::BufferSlice data) = 0; + virtual void on_custom_query(PublicKeyHash src, td::BufferSlice data, td::Promise promise) = 0; + virtual void on_broadcast(PublicKeyHash src, td::BufferSlice data) = 0; + virtual void start() = 0; + virtual ~Callback() = default; + }; + virtual void add_block(td::BufferSlice payload, std::vector deps) = 0; + virtual void debug_add_fork(td::BufferSlice payload, CatChainBlockHeight height, + std::vector deps) = 0; + virtual void blame_node(td::uint32 idx) = 0; + virtual void send_fec_broadcast(td::BufferSlice data) = 0; + virtual void send_custom_query_data(PublicKeyHash dst, std::string name, td::Promise promise, + td::Timestamp timeout, td::BufferSlice query) = 0; + virtual void send_custom_query_data_via(PublicKeyHash dst, std::string name, td::Promise promise, + td::Timestamp timeout, td::BufferSlice query, td::uint64 max_answer_size, + td::actor::ActorId via) = 0; + virtual void send_custom_message_data(PublicKeyHash dst, td::BufferSlice query) = 0; + + virtual void destroy() = 0; + + static td::actor::ActorOwn create(std::unique_ptr callback, CatChainOptions opts, + td::actor::ActorId keyring, + td::actor::ActorId adnl, + td::actor::ActorId overlay_manager, + std::vector ids, PublicKeyHash local_id, + CatChainSessionId unique_hash, std::string db_root); + + virtual ~CatChainReceiverInterface() = default; +}; + +} // namespace catchain + +} // namespace ton diff --git a/catchain/catchain-receiver-source.cpp b/catchain/catchain-receiver-source.cpp new file mode 100644 index 00000000..502843f9 --- /dev/null +++ b/catchain/catchain-receiver-source.cpp @@ -0,0 +1,179 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "catchain-receiver-source.hpp" +#include "common/errorlog.h" + +namespace ton { + +namespace catchain { + +td::uint32 CatChainReceiverSourceImpl::add_fork() { + if (fork_ids_.size() > 0) { + blame(); + } + auto F = chain_->add_fork(); + CHECK(F > 0); + + fork_ids_.push_back(F); + + VLOG(CATCHAIN_INFO) << this << ": adding new fork " << F << " of source " << id_; + + if (fork_ids_.size() > 1) { + CHECK(blamed()); + } + + return F; +} + +CatChainReceiverSourceImpl::CatChainReceiverSourceImpl(CatChainReceiver *chain, PublicKey source, + adnl::AdnlNodeIdShort adnl_id, td::uint32 id) + : chain_(chain), id_(id), adnl_id_(adnl_id) { + src_ = source.compute_short_id(); + + encryptor_ = source.create_encryptor_async().move_as_ok(); + encryptor_sync_ = source.create_encryptor().move_as_ok(); + full_id_ = std::move(source); +} + +td::Result> CatChainReceiverSource::create(CatChainReceiver *chain, + PublicKey source, + adnl::AdnlNodeIdShort adnl_id, + td::uint32 id) { + return std::make_unique(chain, std::move(source), adnl_id, id); +} + +void CatChainReceiverSourceImpl::blame(td::uint32 fork, CatChainBlockHeight height) { + blame(); + if (blamed_heights_.size() > 0) { + if (blamed_heights_.size() <= fork) { + blamed_heights_.resize(fork + 1, 0); + } + if (blamed_heights_[fork] == 0 || blamed_heights_[fork] > height) { + VLOG(CATCHAIN_INFO) << this << ": blamed at " << fork << " " << height; + blamed_heights_[fork] = height; + } + } +} + +void CatChainReceiverSourceImpl::blame() { + if (!blamed_) { + LOG(ERROR) << this << ": CATCHAIN: blaming source " << id_; + blocks_.clear(); + chain_->on_blame(id_); + } + blamed_ = true; +} + +CatChainReceivedBlock *CatChainReceiverSourceImpl::get_block(CatChainBlockHeight height) const { + auto it = blocks_.find(height); + if (it != blocks_.end()) { + return it->second; + } else { + return nullptr; + } +} + +void CatChainReceiverSourceImpl::block_received(CatChainBlockHeight height) { + if (blamed()) { + return; + } + + if (received_height_ + 1 == height) { + received_height_ = height; + } + while (true) { + auto it = blocks_.find(received_height_ + 1); + if (it == blocks_.end()) { + return; + } + if (!it->second->initialized()) { + return; + } + received_height_++; + } +} + +void CatChainReceiverSourceImpl::block_delivered(CatChainBlockHeight height) { + if (blamed()) { + return; + } + + if (delivered_height_ + 1 == height) { + delivered_height_ = height; + } + while (true) { + auto it = blocks_.find(delivered_height_ + 1); + if (it == blocks_.end()) { + return; + } + if (!it->second->delivered()) { + return; + } + delivered_height_++; + } +} + +td::Status CatChainReceiverSourceImpl::validate_dep_sync(tl_object_ptr &dep) { + auto S = std::move(dep->signature_); + auto str = serialize_tl_object(dep, true); + dep->signature_ = std::move(S); + + auto R = encryptor_sync_->check_signature(str.as_slice(), dep->signature_.as_slice()); + if (R.is_error()) { + return R.move_as_error(); + } + + return td::Status::OK(); +} + +void CatChainReceiverSourceImpl::on_new_block(CatChainReceivedBlock *block) { + if (fork_is_found()) { + return; + } + + CHECK(block->get_source_id() == id_); + auto it = blocks_.find(block->get_height()); + if (it != blocks_.end()) { + CHECK(block->get_hash() != it->second->get_hash()); + VLOG(CATCHAIN_WARNING) << this << ": found fork on height " << block->get_height(); + if (!fork_is_found()) { + on_found_fork_proof(create_serialize_tl_object(block->export_tl_dep(), + it->second->export_tl_dep()) + .as_slice()); + chain_->add_prepared_event(fork_proof()); + } + blame(); + return; + } + blocks_[block->get_height()] = block; +} + +void CatChainReceiverSourceImpl::on_found_fork_proof(td::Slice proof) { + if (!fork_is_found()) { + fetch_tl_object(proof, true).ensure(); + fork_proof_ = td::SharedSlice{proof}; + errorlog::ErrorLog::log(PSTRING() << "catchain " << chain_->get_incarnation() << " source " << id_ + << " found fork. hash=" << sha256_bits256(fork_proof_.as_slice()).to_hex()); + errorlog::ErrorLog::log_file(fork_proof_.clone_as_buffer_slice()); + } +} + +} // namespace catchain + +} // namespace ton diff --git a/catchain/catchain-receiver-source.h b/catchain/catchain-receiver-source.h new file mode 100644 index 00000000..d11d7c74 --- /dev/null +++ b/catchain/catchain-receiver-source.h @@ -0,0 +1,85 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +#include "catchain-receiver.h" + +#include "keys/encryptor.h" + +namespace ton { + +namespace catchain { + +class CatChainReceiver; +class CatChainReceivedBlock; + +class CatChainReceiverSource { + public: + virtual td::uint32 get_id() const = 0; + virtual PublicKeyHash get_hash() const = 0; + virtual PublicKey get_full_id() const = 0; + virtual adnl::AdnlNodeIdShort get_adnl_id() const = 0; + + virtual td::uint32 add_fork() = 0; + + virtual bool blamed() const = 0; + virtual void blame(td::uint32 fork, CatChainBlockHeight height) = 0; + virtual void blame() = 0; + + virtual const std::vector &get_forks() const = 0; + virtual const std::vector &get_blamed_heights() const = 0; + virtual Encryptor *get_encryptor_sync() const = 0; + virtual td::uint32 get_forks_cnt() const = 0; + + virtual CatChainBlockHeight delivered_height() const = 0; + virtual CatChainBlockHeight received_height() const = 0; + virtual CatChainReceivedBlock *get_block(CatChainBlockHeight height) const = 0; + virtual void block_received(CatChainBlockHeight height) = 0; + virtual void block_delivered(CatChainBlockHeight height) = 0; + + virtual td::Status validate_dep_sync(tl_object_ptr &dep) = 0; + virtual void on_new_block(CatChainReceivedBlock *block) = 0; + virtual void on_found_fork_proof(td::Slice fork) = 0; + virtual td::BufferSlice fork_proof() const = 0; + virtual bool fork_is_found() const = 0; + + static td::Result> create(CatChainReceiver *chain, PublicKey pub_key, + adnl::AdnlNodeIdShort adnl_id, td::uint32 id); + + virtual CatChainReceiver *get_chain() const = 0; + + virtual ~CatChainReceiverSource() = default; +}; + +} // namespace catchain + +} // namespace ton + +namespace td { + +inline td::StringBuilder &operator<<(td::StringBuilder &sb, const ton::catchain::CatChainReceiverSource &source) { + sb << "[source " << source.get_chain()->get_incarnation() << " " << source.get_id() << "]"; + return sb; +} +inline td::StringBuilder &operator<<(td::StringBuilder &sb, const ton::catchain::CatChainReceiverSource *source) { + sb << *source; + return sb; +} + +} // namespace td diff --git a/catchain/catchain-receiver-source.hpp b/catchain/catchain-receiver-source.hpp new file mode 100644 index 00000000..ad6935b9 --- /dev/null +++ b/catchain/catchain-receiver-source.hpp @@ -0,0 +1,137 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +#include + +#include "catchain-receiver-source.h" +#include "catchain-receiver.h" +#include "catchain-received-block.h" + +namespace ton { + +namespace catchain { + +class CatChainReceiverSourceImpl : public CatChainReceiverSource { + public: + td::uint32 get_id() const override { + return id_; + } + PublicKeyHash get_hash() const override { + return src_; + } + PublicKey get_full_id() const override { + return full_id_; + } + adnl::AdnlNodeIdShort get_adnl_id() const override { + return adnl_id_; + } + + td::uint32 add_fork() override; + + bool blamed() const override { + return blamed_; + } + void blame(td::uint32 fork, CatChainBlockHeight height) override; + void blame() override; + void block_received(CatChainBlockHeight height) override; + void block_delivered(CatChainBlockHeight height) override; + + const std::vector &get_forks() const override { + return fork_ids_; + } + + const std::vector &get_blamed_heights() const override { + return blamed_heights_; + } + + td::actor::ActorId get_encryptor() const { + return encryptor_.get(); + } + Encryptor *get_encryptor_sync() const override { + return encryptor_sync_.get(); + } + + td::uint32 get_forks_cnt() const override { + return static_cast(fork_ids_.size()); + } + + CatChainBlockHeight delivered_height() const override { + return delivered_height_; + } + CatChainBlockHeight received_height() const override { + return received_height_; + } + CatChainReceivedBlock *get_block(CatChainBlockHeight height) const override; + + td::Status validate_dep_sync(tl_object_ptr &dep) override; + void on_new_block(CatChainReceivedBlock *block) override; + void on_found_fork_proof(td::Slice proof) override; + bool fork_is_found() const override { + return !fork_proof_.empty(); + } + td::BufferSlice fork_proof() const override { + if (!fork_proof_.empty()) { + return fork_proof_.clone_as_buffer_slice(); + } else { + return td::BufferSlice(); + } + } + + CatChainReceiver *get_chain() const override { + return chain_; + } + + CatChainReceiverSourceImpl(CatChainReceiver *chain, PublicKey source, adnl::AdnlNodeIdShort adnl_id, td::uint32 id); + + private: + CatChainReceiver *chain_; + td::uint32 id_; + PublicKeyHash src_; + bool blamed_ = false; + PublicKey full_id_; + adnl::AdnlNodeIdShort adnl_id_; + + std::vector fork_ids_; + td::actor::ActorOwn encryptor_; + std::unique_ptr encryptor_sync_; + std::vector blamed_heights_; + std::map blocks_; + td::SharedSlice fork_proof_; + + CatChainBlockHeight delivered_height_ = 0; + CatChainBlockHeight received_height_ = 0; +}; + +} // namespace catchain + +} // namespace ton + +namespace td { + +inline td::StringBuilder &operator<<(td::StringBuilder &sb, const ton::catchain::CatChainReceiverSourceImpl &source) { + sb << "[source " << source.get_chain()->get_incarnation() << " " << source.get_id() << "]"; + return sb; +} +inline td::StringBuilder &operator<<(td::StringBuilder &sb, const ton::catchain::CatChainReceiverSourceImpl *source) { + sb << *source; + return sb; +} + +} // namespace td diff --git a/catchain/catchain-receiver.cpp b/catchain/catchain-receiver.cpp new file mode 100644 index 00000000..85f17498 --- /dev/null +++ b/catchain/catchain-receiver.cpp @@ -0,0 +1,988 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include +#include "td/actor/PromiseFuture.h" +#include "td/utils/Random.h" +#include "td/db/RocksDb.h" +#include "td/utils/port/path.h" +#include "td/utils/overloaded.h" +#include "common/delay.h" + +#include "catchain-receiver.hpp" + +namespace ton { + +namespace catchain { + +PublicKeyHash CatChainReceiverImpl::get_source_hash(td::uint32 source_id) const { + CHECK(source_id < sources_.size()); + return sources_[source_id]->get_hash(); +} + +td::uint32 CatChainReceiverImpl::add_fork() { + return ++total_forks_; +} + +void CatChainReceiverImpl::deliver_block(CatChainReceivedBlock *block) { + VLOG(CATCHAIN_INFO) << this << ": delivering block " << block->get_hash() << " src=" << block->get_source_id() + << " fork=" << block->get_fork_id() << " height=" << block->get_height() + << " custom=" << block->is_custom(); + callback_->new_block(block->get_source_id(), block->get_fork_id(), block->get_hash(), block->get_height(), + block->get_height() == 1 ? CatChainBlockHash::zero() : block->get_prev_hash(), + block->get_dep_hashes(), block->get_deps(), + block->is_custom() ? block->get_payload().clone() : td::SharedSlice()); + + std::vector v; + + for (auto it : neighbours_) { + auto S = get_source(it); + v.push_back(S->get_adnl_id()); + } + + auto update = create_tl_object(block->export_tl()); + auto D = serialize_tl_object(update, true, block->get_payload().as_slice()); + + td::actor::send_closure(overlay_manager_, &overlay::Overlays::send_multiple_messages, std::move(v), + get_source(local_idx_)->get_adnl_id(), overlay_id_, std::move(D)); +} + +void CatChainReceiverImpl::receive_block(adnl::AdnlNodeIdShort src, tl_object_ptr block, + td::BufferSlice payload) { + auto id = CatChainReceivedBlock::block_hash(this, block, payload); + auto B = get_block(id); + if (B && B->initialized()) { + return; + } + + if (block->incarnation_ != incarnation_) { + VLOG(CATCHAIN_WARNING) << this << ": dropping broken block from " << src << ": bad incarnation " + << block->incarnation_; + return; + } + + auto S = validate_block_sync(block, payload.as_slice()); + + if (S.is_error()) { + VLOG(CATCHAIN_WARNING) << this << ": received broken block from " << src << ": " << S.move_as_error(); + return; + } + + auto raw_data = serialize_tl_object(block, true, payload.as_slice()); + create_block(std::move(block), td::SharedSlice{payload.as_slice()}); + + if (!opts_.debug_disable_db) { + db_.set( + id, std::move(raw_data), [](td::Unit) {}, 1.0); + } + block_written_to_db(id); +} + +void CatChainReceiverImpl::receive_block_answer(adnl::AdnlNodeIdShort src, td::BufferSlice data) { + auto F = fetch_tl_prefix(data, true); + if (F.is_error()) { + VLOG(CATCHAIN_INFO) << this << ": received bad block result: " << F.move_as_error(); + return; + } + auto f = F.move_as_ok(); + ton_api::downcast_call( + *f.get(), + td::overloaded( + [&](ton_api::catchain_blockNotFound &r) { VLOG(CATCHAIN_INFO) << this << ": catchain block not found"; }, + [&](ton_api::catchain_blockResult &r) { receive_block(src, std::move(r.block_), std::move(data)); })); +} + +void CatChainReceiverImpl::receive_message_from_overlay(adnl::AdnlNodeIdShort src, td::BufferSlice data) { + if (!read_db_) { + return; + } + + /*auto S = get_source_by_hash(src); + CHECK(S != nullptr); + + if (S->blamed()) { + VLOG(CATCHAIN_INFO) << this << ": dropping block update from blamed source " << src; + return; + }*/ + auto R = fetch_tl_prefix(data, true); + if (R.is_error()) { + VLOG(CATCHAIN_WARNING) << this << ": dropping broken block from " << src << ": " << R.move_as_error(); + return; + } + + auto U = R.move_as_ok(); + receive_block(src, std::move(U->block_), std::move(data)); +} + +void CatChainReceiverImpl::receive_broadcast_from_overlay(PublicKeyHash src, td::BufferSlice data) { + if (!read_db_) { + return; + } + callback_->on_broadcast(src, std::move(data)); +} + +/*void CatChainReceiverImpl::send_block(PublicKeyHash src, tl_object_ptr block, + td::BufferSlice payload) { + CHECK(read_db_); + CHECK(src == local_id_); + + validate_block_sync(block, payload.as_slice()).ensure(); + auto B = create_block(std::move(block), td::SharedSlice{payload.as_slice()}); + CHECK(B != nullptr); + + run_scheduler(); + CHECK(B->delivered()); +}*/ + +CatChainReceivedBlock *CatChainReceiverImpl::create_block(tl_object_ptr block, + td::SharedSlice payload) { + if (block->height_ == 0) { + return root_block_; + } + auto hash = CatChainReceivedBlock::block_hash(this, block, payload.as_slice()); + + auto it = blocks_.find(hash); + if (it != blocks_.end()) { + if (!it->second->initialized()) { + it->second->initialize(std::move(block), std::move(payload)); + } + return it->second.get(); + } else { + blocks_.emplace(hash, CatChainReceivedBlock::create(std::move(block), std::move(payload), this)); + it = blocks_.find(hash); + return it->second.get(); + } +} + +CatChainReceivedBlock *CatChainReceiverImpl::create_block(tl_object_ptr block) { + if (block->height_ == 0) { + return root_block_; + } + auto hash = CatChainReceivedBlock::block_hash(this, block); + auto it = blocks_.find(hash); + if (it != blocks_.end()) { + return it->second.get(); + } else { + blocks_.emplace(hash, CatChainReceivedBlock::create(std::move(block), this)); + it = blocks_.find(hash); + return it->second.get(); + } +} + +td::Status CatChainReceiverImpl::validate_block_sync(tl_object_ptr &dep) { + TRY_STATUS_PREFIX(CatChainReceivedBlock::pre_validate_block(this, dep), "failed to validate block: "); + + if (dep->height_ > 0) { + auto id = CatChainReceivedBlock::block_id(this, dep); + auto B = serialize_tl_object(id, true); + auto block = get_block(get_tl_object_sha_bits256(id)); + if (block) { + return td::Status::OK(); + } + + auto S = get_source_by_hash(PublicKeyHash{id->src_}); + CHECK(S != nullptr); + auto E = S->get_encryptor_sync(); + CHECK(E != nullptr); + return E->check_signature(B.as_slice(), dep->signature_.as_slice()); + } else { + return td::Status::OK(); + } +} + +td::Status CatChainReceiverImpl::validate_block_sync(tl_object_ptr &block, td::Slice payload) { + //LOG(INFO) << ton_api::to_string(block); + TRY_STATUS_PREFIX(CatChainReceivedBlock::pre_validate_block(this, block, payload), "failed to validate block: "); + + if (block->height_ > 0) { + auto id = CatChainReceivedBlock::block_id(this, block, payload); + auto B = serialize_tl_object(id, true); + + auto S = get_source_by_hash(PublicKeyHash{id->src_}); + CHECK(S != nullptr); + auto E = S->get_encryptor_sync(); + CHECK(E != nullptr); + return E->check_signature(B.as_slice(), block->signature_.as_slice()); + } else { + return td::Status::OK(); + } +} + +void CatChainReceiverImpl::run_scheduler() { + while (!to_run_.empty()) { + auto B = to_run_.front(); + to_run_.pop_front(); + + B->run(); + } +} + +void CatChainReceiverImpl::run_block(CatChainReceivedBlock *block) { + to_run_.push_back(block); +} + +CatChainReceivedBlock *CatChainReceiverImpl::get_block(CatChainBlockHash hash) const { + auto it = blocks_.find(hash); + if (it == blocks_.end()) { + return nullptr; + } else { + return it->second.get(); + } +} + +void CatChainReceiverImpl::add_block_cont_3(tl_object_ptr block, td::BufferSlice payload) { + last_sent_block_ = create_block(std::move(block), td::SharedSlice{payload.as_slice()}); + last_sent_block_->written(); + + run_scheduler(); + if (!intentional_fork_) { + CHECK(last_sent_block_->delivered()); + } + + active_send_ = false; + if (pending_blocks_.size() > 0) { + auto B = std::move(pending_blocks_.front()); + pending_blocks_.pop_front(); + add_block(std::move(B->payload_), std::move(B->deps_)); + } +} + +void CatChainReceiverImpl::add_block_cont_2(tl_object_ptr block, td::BufferSlice payload) { + if (opts_.debug_disable_db) { + add_block_cont_3(std::move(block), std::move(payload)); + return; + } + + auto id = CatChainReceivedBlock::block_hash(this, block, payload); + + td::BufferSlice raw_data{32}; + raw_data.as_slice().copy_from(as_slice(id)); + + auto P = td::PromiseCreator::lambda([SelfId = actor_id(this), block = std::move(block), + payload = std::move(payload)](td::Result R) mutable { + R.ensure(); + td::actor::send_closure(SelfId, &CatChainReceiverImpl::add_block_cont_3, std::move(block), std::move(payload)); + }); + + db_.set(CatChainBlockHash::zero(), std::move(raw_data), std::move(P), 0); +} + +void CatChainReceiverImpl::add_block_cont(tl_object_ptr block, td::BufferSlice payload) { + validate_block_sync(block, payload.as_slice()).ensure(); + if (opts_.debug_disable_db) { + add_block_cont_2(std::move(block), std::move(payload)); + return; + } + auto id = CatChainReceivedBlock::block_hash(this, block, payload.as_slice()); + + auto raw_data = serialize_tl_object(block, true, payload.as_slice()); + + auto P = td::PromiseCreator::lambda([SelfId = actor_id(this), block = std::move(block), + payload = std::move(payload)](td::Result R) mutable { + R.ensure(); + td::actor::send_closure(SelfId, &CatChainReceiverImpl::add_block_cont_2, std::move(block), std::move(payload)); + }); + + db_.set(id, std::move(raw_data), std::move(P), 0); +} + +void CatChainReceiverImpl::add_block(td::BufferSlice payload, std::vector deps) { + if (active_send_) { + auto B = std::make_unique(std::move(payload), std::move(deps)); + pending_blocks_.push_back(std::move(B)); + return; + } + active_send_ = true; + + auto S = get_source_by_hash(local_id_); + CHECK(S != nullptr); + CHECK(S->get_id() == local_idx_); + if (!intentional_fork_) { + CHECK(!S->blamed()); + } + + auto prev = last_sent_block_->export_tl_dep(); + + std::vector> deps_arr; + deps_arr.resize(deps.size()); + for (size_t i = 0; i < deps.size(); i++) { + auto B = get_block(deps[i]); + LOG_CHECK(B != nullptr) << this << ": cannot find block with hash " << deps[i]; + if (!intentional_fork_) { + CHECK(B->get_source_id() != local_idx_); + } + deps_arr[i] = B->export_tl_dep(); + } + + auto height = prev->height_ + 1; + auto block_data = create_tl_object(std::move(prev), std::move(deps_arr)); + auto block = create_tl_object(incarnation_, local_idx_, height, std::move(block_data), + td::BufferSlice()); + + auto id = CatChainReceivedBlock::block_id(this, block, payload); + auto id_s = serialize_tl_object(id, true); + + auto P = td::PromiseCreator::lambda([SelfId = actor_id(this), print_id = print_id(), block = std::move(block), + payload = std::move(payload)](td::Result R) mutable { + if (R.is_error()) { + LOG(FATAL) << print_id << ": failed to sign: " << R.move_as_error(); + return; + } + block->signature_ = R.move_as_ok(); + td::actor::send_closure(SelfId, &CatChainReceiverImpl::add_block_cont, std::move(block), std::move(payload)); + }); + + td::actor::send_closure_later(keyring_, &keyring::Keyring::sign_message, local_id_, std::move(id_s), std::move(P)); +} + +void CatChainReceiverImpl::debug_add_fork_cont(tl_object_ptr block, td::BufferSlice payload) { + validate_block_sync(block, payload.as_slice()).ensure(); + auto B = create_block(std::move(block), td::SharedSlice{payload.as_slice()}); + B->written(); + + run_scheduler(); + CHECK(B->delivered()); + + active_send_ = false; + if (pending_blocks_.size() > 0) { + auto B = std::move(pending_blocks_.front()); + pending_blocks_.pop_front(); + add_block(std::move(B->payload_), std::move(B->deps_)); + } +} + +void CatChainReceiverImpl::debug_add_fork(td::BufferSlice payload, CatChainBlockHeight height, + std::vector deps) { + intentional_fork_ = true; + auto S = get_source_by_hash(local_id_); + CHECK(S != nullptr); + CHECK(S->get_id() == local_idx_); + + if (height > S->received_height() + 1) { + height = S->received_height() + 1; + } + + CHECK(height > 0); + CatChainReceivedBlock *prev; + if (height == 1) { + prev = root_block_; + } else { + prev = sources_[local_idx_]->get_block(height - 1); + CHECK(prev); + } + + std::vector> deps_arr; + deps_arr.resize(deps.size()); + for (size_t i = 0; i < deps.size(); i++) { + auto B = get_block(deps[i]); + LOG_CHECK(B != nullptr) << this << ": cannot find block with hash " << deps[i]; + CHECK(B->get_source_id() != local_idx_); + deps_arr[i] = B->export_tl_dep(); + } + + auto block_data = create_tl_object(prev->export_tl_dep(), std::move(deps_arr)); + auto block = create_tl_object(incarnation_, local_idx_, height, std::move(block_data), + td::BufferSlice()); + + auto id = CatChainReceivedBlock::block_id(this, block, payload); + auto id_s = serialize_tl_object(id, true); + + auto P = td::PromiseCreator::lambda([SelfId = actor_id(this), print_id = print_id(), block = std::move(block), + payload = std::move(payload)](td::Result R) mutable { + if (R.is_error()) { + LOG(FATAL) << print_id << ": failed to sign: " << R.move_as_error(); + return; + } + block->signature_ = R.move_as_ok(); + td::actor::send_closure(SelfId, &CatChainReceiverImpl::debug_add_fork_cont, std::move(block), std::move(payload)); + }); + + td::actor::send_closure_later(keyring_, &keyring::Keyring::sign_message, local_id_, std::move(id_s), std::move(P)); +} + +CatChainReceiverImpl::CatChainReceiverImpl(std::unique_ptr callback, CatChainOptions opts, + td::actor::ActorId keyring, + td::actor::ActorId adnl, + td::actor::ActorId overlay_manager, + std::vector ids, PublicKeyHash local_id, + CatChainSessionId unique_hash, std::string db_root) + : callback_(std::move(callback)) + , opts_(std::move(opts)) + , keyring_(keyring) + , adnl_(adnl) + , overlay_manager_(overlay_manager) + , local_id_(local_id) + , db_root_(db_root) { + std::vector short_ids; + local_idx_ = static_cast(ids.size()); + for (auto &id : ids) { + td::uint32 seq = static_cast(sources_.size()); + auto R = CatChainReceiverSource::create(this, id.pub_key, id.adnl_id, seq); + auto S = R.move_as_ok(); + auto h = id.pub_key.compute_short_id(); + short_ids.push_back(h.bits256_value()); + sources_hashes_[h] = seq; + sources_adnl_addrs_[id.adnl_id] = seq; + sources_.push_back(std::move(S)); + + if (h == local_id_) { + CHECK(local_idx_ == static_cast(ids.size())); + local_idx_ = seq; + } + } + CHECK(local_idx_ != static_cast(ids.size())); + + //std::sort(short_ids.begin(), short_ids.end()); + auto F = create_tl_object(unique_hash, std::move(short_ids)); + + overlay_full_id_ = overlay::OverlayIdFull{serialize_tl_object(F, true)}; + overlay_id_ = overlay_full_id_.compute_short_id(); + incarnation_ = overlay_id_.bits256_value(); + + auto R = CatChainReceivedBlock::create_root(get_sources_cnt(), incarnation_, this); + root_block_ = R.get(); + blocks_[root_block_->get_hash()] = std::move(R); + last_sent_block_ = root_block_; + + choose_neighbours(); +} + +void CatChainReceiverImpl::start_up() { + std::vector ids; + for (td::uint32 i = 0; i < get_sources_cnt(); i++) { + ids.push_back(get_source(i)->get_adnl_id()); + } + std::map root_keys; + for (td::uint32 i = 0; i < get_sources_cnt(); i++) { + root_keys.emplace(get_source(i)->get_hash(), 16 << 20); + } + td::actor::send_closure(overlay_manager_, &overlay::Overlays::create_private_overlay, + get_source(local_idx_)->get_adnl_id(), overlay_full_id_.clone(), std::move(ids), + make_callback(), overlay::OverlayPrivacyRules{0, std::move(root_keys)}); + + CHECK(root_block_); + + if (!opts_.debug_disable_db) { + std::shared_ptr kv = std::make_shared( + td::RocksDb::open(db_root_ + "/catchainreceiver-" + td::base64url_encode(as_slice(incarnation_))).move_as_ok()); + db_ = DbType{std::move(kv)}; + + auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result R) { + R.ensure(); + auto g = R.move_as_ok(); + if (g.status == td::KeyValue::GetStatus::NotFound) { + td::actor::send_closure(SelfId, &CatChainReceiverImpl::read_db); + } else { + auto B = std::move(g.value); + CHECK(B.size() == 32); + CatChainBlockHash x; + as_slice(x).copy_from(B.as_slice()); + td::actor::send_closure(SelfId, &CatChainReceiverImpl::read_db_from, x); + } + }); + + db_.get(CatChainBlockHash::zero(), std::move(P)); + } else { + read_db(); + } +} + +void CatChainReceiverImpl::tear_down() { + td::actor::send_closure(overlay_manager_, &overlay::Overlays::delete_overlay, get_source(local_idx_)->get_adnl_id(), + overlay_id_); +} + +void CatChainReceiverImpl::read_db_from(CatChainBlockHash id) { + pending_in_db_ = 1; + db_root_block_ = id; + + auto P = td::PromiseCreator::lambda([SelfId = actor_id(this), id](td::Result R) { + R.ensure(); + auto g = R.move_as_ok(); + CHECK(g.status == td::KeyValue::GetStatus::Ok); + + td::actor::send_closure(SelfId, &CatChainReceiverImpl::read_block_from_db, id, std::move(g.value)); + }); + + db_.get(id, std::move(P)); +} + +void CatChainReceiverImpl::read_block_from_db(CatChainBlockHash id, td::BufferSlice data) { + pending_in_db_--; + + auto F = fetch_tl_prefix(data, true); + F.ensure(); + + auto block = F.move_as_ok(); + auto payload = std::move(data); + + auto block_id = CatChainReceivedBlock::block_hash(this, block, payload); + CHECK(block_id == id); + + auto B = get_block(id); + if (B && B->initialized()) { + CHECK(B->in_db()); + if (!pending_in_db_) { + read_db(); + } + return; + } + + auto source = get_source(block->src_); + CHECK(source != nullptr); + + CHECK(block->incarnation_ == incarnation_); + + validate_block_sync(block, payload).ensure(); + + B = create_block(std::move(block), td::SharedSlice{payload.as_slice()}); + B->written(); + CHECK(B); + + auto deps = B->get_dep_hashes(); + deps.push_back(B->get_prev_hash()); + for (auto &dep : deps) { + auto dep_block = get_block(dep); + if (!dep_block || !dep_block->initialized()) { + pending_in_db_++; + auto P = td::PromiseCreator::lambda([SelfId = actor_id(this), dep](td::Result R) { + R.ensure(); + auto g = R.move_as_ok(); + CHECK(g.status == td::KeyValue::GetStatus::Ok); + + td::actor::send_closure(SelfId, &CatChainReceiverImpl::read_block_from_db, dep, std::move(g.value)); + }); + + db_.get(dep, std::move(P)); + } + } + + if (!pending_in_db_) { + read_db(); + } +} + +void CatChainReceiverImpl::read_db() { + if (!db_root_block_.is_zero()) { + run_scheduler(); + last_sent_block_ = get_block(db_root_block_); + CHECK(last_sent_block_); + CHECK(last_sent_block_->delivered()); + } + + read_db_ = true; + + next_rotate_ = td::Timestamp::in(60 + td::Random::fast(0, 60)); + next_sync_ = td::Timestamp::in(0.01 * td::Random::fast(0, 60)); + alarm_timestamp().relax(next_rotate_); + alarm_timestamp().relax(next_sync_); + + callback_->start(); +} + +td::actor::ActorOwn CatChainReceiverInterface::create( + std::unique_ptr callback, CatChainOptions opts, td::actor::ActorId keyring, + td::actor::ActorId adnl, td::actor::ActorId overlay_manager, + std::vector ids, PublicKeyHash local_id, CatChainSessionId unique_hash, std::string db_root) { + auto A = td::actor::create_actor("catchainreceiver", std::move(callback), std::move(opts), + keyring, adnl, overlay_manager, std::move(ids), local_id, + unique_hash, db_root); + return std::move(A); +} + +CatChainReceiverSource *CatChainReceiverImpl::get_source_by_hash(PublicKeyHash source_hash) const { + auto it = sources_hashes_.find(source_hash); + if (it == sources_hashes_.end()) { + return nullptr; + } + return get_source(it->second); +} + +CatChainReceiverSource *CatChainReceiverImpl::get_source_by_adnl_id(adnl::AdnlNodeIdShort source_hash) const { + auto it = sources_adnl_addrs_.find(source_hash); + if (it == sources_adnl_addrs_.end()) { + return nullptr; + } + return get_source(it->second); +} + +void CatChainReceiverImpl::receive_query_from_overlay(adnl::AdnlNodeIdShort src, td::BufferSlice data, + td::Promise promise) { + if (!read_db_) { + promise.set_error(td::Status::Error(ErrorCode::notready, "db not read")); + return; + } + td::PerfWarningTimer t{"catchain query process", 0.001}; + auto F = fetch_tl_object(data.clone(), true); + if (F.is_error()) { + callback_->on_custom_query(get_source_by_adnl_id(src)->get_hash(), std::move(data), std::move(promise)); + //LOG(WARNING) << this << ": unknown query from " << src; + return; + } + auto f = F.move_as_ok(); + ton_api::downcast_call(*f.get(), [&](auto &obj) { this->process_query(src, obj, std::move(promise)); }); +} + +void CatChainReceiverImpl::process_query(adnl::AdnlNodeIdShort src, ton_api::catchain_getBlock &query, + td::Promise promise) { + auto it = blocks_.find(query.block_); + if (it == blocks_.end() || it->second->get_height() == 0 || !it->second->initialized()) { + promise.set_value(serialize_tl_object(create_tl_object(), true)); + } else { + promise.set_value(serialize_tl_object(create_tl_object(it->second->export_tl()), + true, it->second->get_payload().as_slice())); + } +} + +void CatChainReceiverImpl::process_query(adnl::AdnlNodeIdShort src, ton_api::catchain_getBlocks &query, + td::Promise promise) { + if (query.blocks_.size() > 100) { + promise.set_error(td::Status::Error(ErrorCode::protoviolation, "too many blocks")); + return; + } + td::int32 cnt = 0; + for (auto &b : query.blocks_) { + auto it = blocks_.find(b); + if (it != blocks_.end() && it->second->get_height() > 0) { + auto block = create_tl_object(it->second->export_tl()); + CHECK(it->second->get_payload().size() > 0); + auto B = serialize_tl_object(block, true, it->second->get_payload().clone()); + td::actor::send_closure(overlay_manager_, &overlay::Overlays::send_message, src, + get_source(local_idx_)->get_adnl_id(), overlay_id_, std::move(B)); + cnt++; + } + } + promise.set_value(serialize_tl_object(create_tl_object(cnt), true)); +} + +void CatChainReceiverImpl::process_query(adnl::AdnlNodeIdShort src, ton_api::catchain_getBlockHistory &query, + td::Promise promise) { + auto h = query.height_; + if (h <= 0) { + promise.set_error(td::Status::Error(ErrorCode::protoviolation, "not-positive height")); + return; + } + if (h > 100) { + h = 100; + } + std::set s{query.stop_if_.begin(), query.stop_if_.end()}; + + auto B = get_block(query.block_); + if (B == nullptr) { + promise.set_value(serialize_tl_object(create_tl_object(0), true)); + return; + } + if (static_cast(h) > B->get_height()) { + h = B->get_height(); + } + td::uint32 cnt = 0; + while (h-- > 0) { + if (s.find(B->get_hash()) != s.end()) { + break; + } + auto block = create_tl_object(B->export_tl()); + CHECK(B->get_payload().size() > 0); + auto BB = serialize_tl_object(block, true, B->get_payload().as_slice()); + td::actor::send_closure(overlay_manager_, &overlay::Overlays::send_message, src, + get_source(local_idx_)->get_adnl_id(), overlay_id_, std::move(BB)); + B = B->get_prev(); + cnt++; + } + promise.set_value(serialize_tl_object(create_tl_object(cnt), true)); +} + +void CatChainReceiverImpl::process_query(adnl::AdnlNodeIdShort src, ton_api::catchain_getDifference &query, + td::Promise promise) { + auto &vt = query.rt_; + if (vt.size() != get_sources_cnt()) { + VLOG(CATCHAIN_WARNING) << this << ": incorrect query from " << src; + promise.set_error(td::Status::Error(ErrorCode::protoviolation, "bad vt size")); + return; + } + for (td::uint32 i = 0; i < get_sources_cnt(); i++) { + if (vt[i] >= 0) { + auto S = get_source(i); + if (S->fork_is_found()) { + auto obj = fetch_tl_object(S->fork_proof(), true); + obj.ensure(); + auto f = obj.move_as_ok(); + promise.set_value( + create_serialize_tl_object(std::move(f->left_), std::move(f->right_))); + return; + } + } + } + + std::vector my_vt(get_sources_cnt()); + td::uint64 total = 0; + for (td::uint32 i = 0; i < get_sources_cnt(); i++) { + if (vt[i] >= 0) { + auto x = static_cast(vt[i]); + auto S = get_source(i); + if (S->delivered_height() > x) { + total += S->delivered_height() - x; + } + my_vt[i] = S->delivered_height(); + } else { + my_vt[i] = -1; + } + } + + const td::uint32 max_send = 100; + + td::int32 l = 0; + td::int32 r = max_send + 1; + while (r - l > 1) { + td::int32 x = (r + l) / 2; + td::uint64 sum = 0; + for (td::uint32 i = 0; i < get_sources_cnt(); i++) { + if (vt[i] >= 0 && my_vt[i] > vt[i]) { + sum += (my_vt[i] - vt[i] > x) ? x : (my_vt[i] - vt[i]); + } + } + if (sum > max_send) { + r = x; + } else { + l = x; + } + } + CHECK(r > 0); + for (td::uint32 i = 0; i < get_sources_cnt(); i++) { + if (vt[i] >= 0 && my_vt[i] > vt[i]) { + auto S = get_source(i); + auto t = (my_vt[i] - vt[i] > r) ? r : (my_vt[i] - vt[i]); + CHECK(t > 0); + while (t-- > 0) { + auto M = S->get_block(++vt[i]); + CHECK(M != nullptr); + auto block = create_tl_object(M->export_tl()); + CHECK(M->get_payload().size() > 0); + auto BB = serialize_tl_object(block, true, M->get_payload().as_slice()); + td::actor::send_closure(overlay_manager_, &overlay::Overlays::send_message, src, + get_source(local_idx_)->get_adnl_id(), overlay_id_, std::move(BB)); + } + } + } + + promise.set_value(serialize_tl_object(create_tl_object(std::move(vt)), true)); +} + +void CatChainReceiverImpl::got_fork_proof(td::BufferSlice data) { + auto F = fetch_tl_object(std::move(data), true); + if (F.is_error()) { + VLOG(CATCHAIN_WARNING) << this << ": received bad fork proof: " << F.move_as_error(); + return; + } + auto f = F.move_as_ok(); + { + td::Status S; + S = validate_block_sync(f->left_); + if (S.is_error()) { + VLOG(CATCHAIN_WARNING) << this << ": incorrect fork blame: left is invalid: " << S.move_as_error(); + return; + } + S = validate_block_sync(f->right_); + if (S.is_error()) { + VLOG(CATCHAIN_WARNING) << this << ": incorrect fork blame: right is invalid: " << S.move_as_error(); + return; + } + } + + // block is incorrect, since blocks are + if (f->left_->height_ != f->right_->height_ || f->left_->src_ != f->right_->src_ || + f->left_->data_hash_ == f->right_->data_hash_) { + VLOG(CATCHAIN_WARNING) << this << ": incorrect fork blame: not a fork"; + return; + } + + auto S = get_source(f->left_->src_); + S->on_found_fork_proof( + create_serialize_tl_object(std::move(f->left_), std::move(f->right_))); + S->blame(); +} + +void CatChainReceiverImpl::synchronize_with(CatChainReceiverSource *S) { + CHECK(!S->blamed()); + std::vector rt(get_sources_cnt()); + for (td::uint32 i = 0; i < get_sources_cnt(); i++) { + auto SS = get_source(i); + if (SS->blamed()) { + rt[i] = -1; + } else { + rt[i] = S->delivered_height(); + } + } + + auto P = td::PromiseCreator::lambda( + [SelfId = actor_id(this), src = S->get_hash(), print_id = print_id()](td::Result R) { + if (R.is_error()) { + VLOG(CATCHAIN_INFO) << print_id << ": timedout syncronize query to " << src; + return; + } + auto data = R.move_as_ok(); + auto X = fetch_tl_object(data.clone(), true); + if (X.is_error()) { + VLOG(CATCHAIN_WARNING) << print_id << ": received incorrect answer to syncronize query from " << src << ": " + << X.move_as_error(); + return; + } + auto A = X.move_as_ok(); + + if (A->get_id() == ton_api::catchain_differenceFork::ID) { + td::actor::send_closure(SelfId, &CatChainReceiverImpl::got_fork_proof, std::move(data)); + } + // use answer ? + return; + }); + td::actor::send_closure(overlay_manager_, &overlay::Overlays::send_query, S->get_adnl_id(), + get_source(local_idx_)->get_adnl_id(), overlay_id_, "sync", std::move(P), + td::Timestamp::in(5.0), + serialize_tl_object(create_tl_object(std::move(rt)), true)); + + if (S->delivered_height() < S->received_height()) { + auto B = S->get_block(S->delivered_height() + 1); + CHECK(B->initialized()); + + std::vector vec; + B->find_pending_deps(vec, 16); + + for (auto &hash : vec) { + auto P = td::PromiseCreator::lambda( + [SelfId = actor_id(this), print_id = print_id(), src = S->get_adnl_id()](td::Result R) { + if (R.is_error()) { + VLOG(CATCHAIN_INFO) << print_id << ": timedout syncronize query to " << src; + } else { + td::actor::send_closure(SelfId, &CatChainReceiverImpl::receive_block_answer, src, R.move_as_ok()); + } + }); + auto query = serialize_tl_object(create_tl_object(hash), true); + td::actor::send_closure(overlay_manager_, &overlay::Overlays::send_query, S->get_adnl_id(), + get_source(local_idx_)->get_adnl_id(), overlay_id_, "sync blocks", std::move(P), + td::Timestamp::in(2.0), std::move(query)); + } + } +} + +void CatChainReceiverImpl::choose_neighbours() { + std::vector n; + n.resize(get_max_neighbours()); + + td::uint32 size = 0; + for (td::uint32 i = 0; i < get_sources_cnt(); i++) { + if (i == local_idx_) { + continue; + } + auto S = get_source(i); + if (!S->blamed()) { + size++; + if (size <= n.size()) { + n[size - 1] = i; + } else { + td::uint32 id = td::Random::fast(0, size - 1); + if (id < n.size()) { + n[id] = i; + } + } + } + } + if (size < n.size()) { + n.resize(size); + } + neighbours_ = std::move(n); +} + +void CatChainReceiverImpl::alarm() { + alarm_timestamp() = td::Timestamp::never(); + if (next_sync_ && next_sync_.is_in_past()) { + next_sync_ = td::Timestamp::in(td::Random::fast(2.0, 3.0)); + for (auto i = 0; i < 3; i++) { + auto S = get_source(td::Random::fast(0, get_sources_cnt() - 1)); + CHECK(S != nullptr); + if (!S->blamed()) { + synchronize_with(S); + break; + } + } + } + if (next_rotate_ && next_rotate_.is_in_past()) { + next_rotate_ = td::Timestamp::in(td::Random::fast(60.0, 120.0)); + choose_neighbours(); + } + alarm_timestamp().relax(next_rotate_); + alarm_timestamp().relax(next_sync_); +} + +void CatChainReceiverImpl::send_fec_broadcast(td::BufferSlice data) { + td::actor::send_closure(overlay_manager_, &overlay::Overlays::send_broadcast_fec_ex, + get_source(local_idx_)->get_adnl_id(), overlay_id_, local_id_, 0, std::move(data)); +} +void CatChainReceiverImpl::send_custom_query_data(PublicKeyHash dst, std::string name, + td::Promise promise, td::Timestamp timeout, + td::BufferSlice query) { + auto S = get_source_by_hash(dst); + td::actor::send_closure(overlay_manager_, &overlay::Overlays::send_query, S->get_adnl_id(), + get_source(local_idx_)->get_adnl_id(), overlay_id_, std::move(name), std::move(promise), + timeout, std::move(query)); +} + +void CatChainReceiverImpl::send_custom_query_data_via(PublicKeyHash dst, std::string name, + td::Promise promise, td::Timestamp timeout, + td::BufferSlice query, td::uint64 max_answer_size, + td::actor::ActorId via) { + auto S = get_source_by_hash(dst); + td::actor::send_closure(overlay_manager_, &overlay::Overlays::send_query_via, S->get_adnl_id(), + get_source(local_idx_)->get_adnl_id(), overlay_id_, std::move(name), std::move(promise), + timeout, std::move(query), max_answer_size, via); +} + +void CatChainReceiverImpl::send_custom_message_data(PublicKeyHash dst, td::BufferSlice data) { + auto S = get_source_by_hash(dst); + td::actor::send_closure(overlay_manager_, &overlay::Overlays::send_message, S->get_adnl_id(), + get_source(local_idx_)->get_adnl_id(), overlay_id_, std::move(data)); +} + +void CatChainReceiverImpl::block_written_to_db(CatChainBlockHash hash) { + auto block = get_block(hash); + CHECK(block); + + block->written(); + run_scheduler(); +} + +static void destroy_db(std::string name, td::uint32 attempt) { + auto S = td::RocksDb::destroy(name); + if (S.is_ok()) { + return; + } + if (S.is_error() && attempt >= 10) { + LOG(ERROR) << "failed to destroy catchain " << name << ": " << S; + } else { + LOG(DEBUG) << "failed to destroy catchain " << name << ": " << S; + delay_action([name, attempt]() { destroy_db(name, attempt); }, td::Timestamp::in(1.0)); + } +} + +void CatChainReceiverImpl::destroy() { + auto name = db_root_ + "/catchainreceiver-" + td::base64url_encode(as_slice(incarnation_)); + delay_action([name]() { destroy_db(name, 0); }, td::Timestamp::in(1.0)); + stop(); +} + +} // namespace catchain + +} // namespace ton diff --git a/catchain/catchain-receiver.h b/catchain/catchain-receiver.h new file mode 100644 index 00000000..ab04b312 --- /dev/null +++ b/catchain/catchain-receiver.h @@ -0,0 +1,89 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +#include "adnl/adnl.h" +#include "adnl/utils.hpp" +#include "auto/tl/ton_api.h" + +#include "catchain-types.h" +#include "catchain-receiver-interface.h" + +namespace ton { + +namespace catchain { + +constexpr int VERBOSITY_NAME(CATCHAIN_WARNING) = verbosity_WARNING; +constexpr int VERBOSITY_NAME(CATCHAIN_NOTICE) = verbosity_DEBUG; +constexpr int VERBOSITY_NAME(CATCHAIN_INFO) = verbosity_DEBUG; +constexpr int VERBOSITY_NAME(CATCHAIN_DEBUG) = verbosity_DEBUG; +constexpr int VERBOSITY_NAME(CATCHAIN_EXTRA_DEBUG) = verbosity_DEBUG + 1; + +class CatChainReceivedBlock; +class CatChainReceiverSource; + +class CatChainReceiver : public CatChainReceiverInterface { + public: + struct PrintId { + CatChainSessionId instance_; + PublicKeyHash local_id_; + }; + td::uint32 get_max_neighbours() const { + return 5; + } + virtual PrintId print_id() const = 0; + virtual CatChainReceivedBlock *create_block(tl_object_ptr block, + td::SharedSlice payload) = 0; + virtual CatChainReceivedBlock *create_block(tl_object_ptr block) = 0; + virtual CatChainReceiverSource *get_source(td::uint32 source_id) const = 0; + virtual PublicKeyHash get_source_hash(td::uint32 source_id) const = 0; + virtual td::uint32 get_forks_cnt() const = 0; + virtual td::uint32 get_sources_cnt() const = 0; + virtual CatChainSessionId get_incarnation() const = 0; + virtual void run_block(CatChainReceivedBlock *block) = 0; + virtual void deliver_block(CatChainReceivedBlock *block) = 0; + virtual td::uint32 add_fork() = 0; + virtual void add_prepared_event(td::BufferSlice data) = 0; + virtual void on_blame(td::uint32 source_id) = 0; + + virtual const CatChainOptions &opts() const = 0; + + virtual td::Status validate_block_sync(tl_object_ptr &dep) = 0; + virtual td::Status validate_block_sync(tl_object_ptr &block, td::Slice payload) = 0; + + virtual ~CatChainReceiver() = default; +}; + +} // namespace catchain + +} // namespace ton + +namespace td { + +inline td::StringBuilder &operator<<(td::StringBuilder &sb, const ton::catchain::CatChainReceiver::PrintId &print_id) { + sb << "[catchainreceiver " << print_id.instance_ << "@" << print_id.local_id_ << "]"; + return sb; +} + +inline td::StringBuilder &operator<<(td::StringBuilder &sb, const ton::catchain::CatChainReceiver *catchain) { + sb << catchain->print_id(); + return sb; +} + +} // namespace td diff --git a/catchain/catchain-receiver.hpp b/catchain/catchain-receiver.hpp new file mode 100644 index 00000000..a75d4cec --- /dev/null +++ b/catchain/catchain-receiver.hpp @@ -0,0 +1,240 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +#include +#include +#include + +#include "catchain-types.h" +#include "catchain-receiver.h" +#include "catchain-receiver-source.h" +#include "catchain-received-block.h" + +#include "td/db/KeyValueAsync.h" + +namespace ton { + +namespace catchain { + +class CatChainReceiverImpl : public CatChainReceiver { + public: + PrintId print_id() const override { + return PrintId{incarnation_, local_id_}; + } + + void add_prepared_event(td::BufferSlice data) override { + add_block(std::move(data), std::vector()); + } + CatChainSessionId get_incarnation() const override { + return incarnation_; + } + void run_block(CatChainReceivedBlock *block) override; + + td::uint32 get_forks_cnt() const override { + return total_forks_; + } + td::uint32 get_sources_cnt() const override { + return static_cast(sources_.size()); + } + + CatChainReceiverSource *get_source(td::uint32 source_id) const override { + if (source_id >= get_sources_cnt()) { + return nullptr; + } + + return sources_[source_id].get(); + } + PublicKeyHash get_source_hash(td::uint32 source_id) const override; + CatChainReceiverSource *get_source_by_hash(PublicKeyHash source_hash) const; + CatChainReceiverSource *get_source_by_adnl_id(adnl::AdnlNodeIdShort source_hash) const; + + td::uint32 add_fork() override; + + void deliver_block(CatChainReceivedBlock *block) override; + + void receive_message_from_overlay(adnl::AdnlNodeIdShort src, td::BufferSlice data); + void receive_query_from_overlay(adnl::AdnlNodeIdShort src, td::BufferSlice data, + td::Promise promise); + void process_query(adnl::AdnlNodeIdShort src, ton_api::catchain_getBlock &query, + td::Promise promise); + void process_query(adnl::AdnlNodeIdShort src, ton_api::catchain_getBlocks &query, + td::Promise promise); + void process_query(adnl::AdnlNodeIdShort src, ton_api::catchain_getBlockHistory &query, + td::Promise promise); + void process_query(adnl::AdnlNodeIdShort src, ton_api::catchain_getDifference &query, + td::Promise promise); + template + void process_query(adnl::AdnlNodeIdShort src, T &query, td::Promise promise) { + //LOG(WARNING) << this << ": unknown query from " << src; + callback_->on_custom_query(get_source_by_adnl_id(src)->get_hash(), serialize_tl_object(&query, true), + std::move(promise)); + } + void receive_broadcast_from_overlay(PublicKeyHash src, td::BufferSlice data); + + void receive_block(adnl::AdnlNodeIdShort src, tl_object_ptr block, td::BufferSlice payload); + void receive_block_answer(adnl::AdnlNodeIdShort src, td::BufferSlice); + //void send_block(PublicKeyHash src, tl_object_ptr block, td::BufferSlice payload); + + CatChainReceivedBlock *create_block(tl_object_ptr block, td::SharedSlice payload) override; + CatChainReceivedBlock *create_block(tl_object_ptr block) override; + + td::Status validate_block_sync(tl_object_ptr &dep) override; + td::Status validate_block_sync(tl_object_ptr &block, td::Slice payload) override; + + void send_fec_broadcast(td::BufferSlice data) override; + void send_custom_query_data(PublicKeyHash dst, std::string name, td::Promise promise, + td::Timestamp timeout, td::BufferSlice query) override; + void send_custom_query_data_via(PublicKeyHash dst, std::string name, td::Promise promise, + td::Timestamp timeout, td::BufferSlice query, td::uint64 max_answer_size, + td::actor::ActorId via) override; + void send_custom_message_data(PublicKeyHash dst, td::BufferSlice query) override; + + void run_scheduler(); + void add_block(td::BufferSlice data, std::vector deps) override; + void add_block_cont(tl_object_ptr block, td::BufferSlice payload); + void add_block_cont_2(tl_object_ptr block, td::BufferSlice payload); + void add_block_cont_3(tl_object_ptr block, td::BufferSlice payload); + void debug_add_fork(td::BufferSlice payload, CatChainBlockHeight height, + std::vector deps) override; + void debug_add_fork_cont(tl_object_ptr block, td::BufferSlice payload); + void on_blame(td::uint32 src) override { + callback_->blame(src); + } + void blame_node(td::uint32 idx) override { + } + const CatChainOptions &opts() const override { + return opts_; + } + + void got_fork_proof(td::BufferSlice data); + void synchronize_with(CatChainReceiverSource *source); + void alarm() override; + void start_up() override; + void tear_down() override; + void read_db(); + void read_db_from(CatChainBlockHash id); + void read_block_from_db(CatChainBlockHash id, td::BufferSlice data); + + void block_written_to_db(CatChainBlockHash hash); + + void destroy() override; + + CatChainReceivedBlock *get_block(CatChainBlockHash hash) const; + + CatChainReceiverImpl(std::unique_ptr callback, CatChainOptions opts, + td::actor::ActorId keyring, td::actor::ActorId adnl, + td::actor::ActorId, std::vector ids, PublicKeyHash local_id, + CatChainBlockHash unique_hash, std::string db_root); + + private: + std::unique_ptr make_callback() { + class Callback : public overlay::Overlays::Callback { + public: + void receive_message(adnl::AdnlNodeIdShort src, overlay::OverlayIdShort overlay_id, + td::BufferSlice data) override { + td::actor::send_closure(id_, &CatChainReceiverImpl::receive_message_from_overlay, src, std::move(data)); + } + void receive_query(adnl::AdnlNodeIdShort src, overlay::OverlayIdShort overlay_id, td::BufferSlice data, + td::Promise promise) override { + td::actor::send_closure(id_, &CatChainReceiverImpl::receive_query_from_overlay, src, std::move(data), + std::move(promise)); + } + + void receive_broadcast(PublicKeyHash src, overlay::OverlayIdShort overlay_id, td::BufferSlice data) override { + td::actor::send_closure(id_, &CatChainReceiverImpl::receive_broadcast_from_overlay, src, std::move(data)); + } + Callback(td::actor::ActorId id) : id_(std::move(id)) { + } + + private: + td::actor::ActorId id_; + }; + + return std::make_unique(actor_id(this)); + } + + struct PendingBlock { + td::BufferSlice payload_; + std::vector deps_; + + PendingBlock(td::BufferSlice &&payload, std::vector &&deps) + : payload_(std::move(payload)), deps_(std::move(deps)) { + } + }; + + std::list> pending_blocks_; + bool active_send_ = false; + bool read_db_ = false; + td::uint32 pending_in_db_ = 0; + CatChainBlockHash db_root_block_ = CatChainBlockHash::zero(); + + void choose_neighbours(); + + std::vector> sources_; + std::map sources_hashes_; + std::map sources_adnl_addrs_; + td::uint32 total_forks_ = 0; + std::map> blocks_; + CatChainReceivedBlock *root_block_; + CatChainReceivedBlock *last_sent_block_; + + CatChainSessionId incarnation_; + + std::unique_ptr callback_; + CatChainOptions opts_; + + std::vector neighbours_; + + //std::queue> events_; + //std::queue raw_events_; + + td::actor::ActorId keyring_; + td::actor::ActorId adnl_; + td::actor::ActorId overlay_manager_; + overlay::OverlayIdShort overlay_id_; + overlay::OverlayIdFull overlay_full_id_; + PublicKeyHash local_id_; + td::uint32 local_idx_; + + td::Timestamp next_sync_; + td::Timestamp next_rotate_; + + std::string db_root_; + + using DbType = td::KeyValueAsync; + DbType db_; + + bool intentional_fork_ = false; + + std::list to_run_; +}; + +} // namespace catchain + +} // namespace ton + +namespace td { + +inline td::StringBuilder &operator<<(td::StringBuilder &sb, const ton::catchain::CatChainReceiverImpl *catchain) { + sb << catchain->print_id(); + return sb; +} + +} // namespace td diff --git a/catchain/catchain-types.h b/catchain/catchain-types.h new file mode 100644 index 00000000..217ef753 --- /dev/null +++ b/catchain/catchain-types.h @@ -0,0 +1,47 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +#include "td/utils/int_types.h" +#include "adnl/adnl-node-id.hpp" + +namespace ton { + +namespace catchain { + +using CatChainBlockHash = td::Bits256; +using CatChainBlockPayloadHash = td::Bits256; +using CatChainBlockHeight = td::uint32; +using CatChainSessionId = td::Bits256; + +struct CatChainNode { + adnl::AdnlNodeIdShort adnl_id; + PublicKey pub_key; +}; + +struct CatChainOptions { + td::Clocks::Duration idle_timeout = 16.0; + td::uint32 max_deps = 4; + + bool debug_disable_db = false; +}; + +} // namespace catchain + +} // namespace ton diff --git a/catchain/catchain.cpp b/catchain/catchain.cpp new file mode 100644 index 00000000..56f70028 --- /dev/null +++ b/catchain/catchain.cpp @@ -0,0 +1,317 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "catchain-types.h" +#include "catchain.hpp" +#include "catchain-receiver.h" + +#include "adnl/utils.hpp" + +namespace ton { + +namespace catchain { + +void CatChainImpl::send_process() { + CHECK(receiver_started_); + + std::vector v; + std::vector w; + while (top_blocks_.size() > 0 && v.size() < opts_.max_deps) { + auto B = *top_blocks_.get_random(); + CHECK(B != nullptr); + top_blocks_.remove(B->hash()); + if (B->source() == sources_.size() || !blamed_sources_[B->source()]) { + w.push_back(B->hash()); + v.push_back(B); + set_processed(B); + } + } + + process_deps_ = std::move(w); + VLOG(CATCHAIN_INFO) << this << ": creating block. deps=" << process_deps_; + callback_->process_blocks(std::move(v)); + VLOG(CATCHAIN_INFO) << this << ": sent creating block"; +} + +void CatChainImpl::send_preprocess(CatChainBlock *block) { + if (block->preprocess_is_sent()) { + return; + } + auto prev = block->prev(); + if (prev) { + send_preprocess(prev); + } + + auto deps = block->deps(); + for (auto X : deps) { + send_preprocess(X); + } + + block->preprocess_sent(); + VLOG(CATCHAIN_INFO) << this << ": preprocessing block " << block->hash() << " src=" << block->source(); + callback_->preprocess_block(block); + VLOG(CATCHAIN_INFO) << this << ": sent preprocessing block " << block->hash() << " src=" << block->source(); +} + +void CatChainImpl::set_processed(CatChainBlock *block) { + if (block->is_processed()) { + return; + } + auto prev = block->prev(); + if (prev) { + set_processed(prev); + } + + auto deps = block->deps(); + for (auto X : deps) { + set_processed(X); + } + + block->set_processed(); +} + +void CatChainImpl::processed_block(td::BufferSlice payload) { + CHECK(receiver_started_); + VLOG(CATCHAIN_INFO) << this << ": created block. deps=" << process_deps_ << " payload_size=" << payload.size(); + td::actor::send_closure(receiver_, &CatChainReceiverInterface::add_block, std::move(payload), + std::move(process_deps_)); + CHECK(active_process_); + if (top_blocks_.size() > 0 || force_process_) { + force_process_ = false; + send_process(); + } else { + active_process_ = false; + VLOG(CATCHAIN_INFO) << this << ": finished processing"; + callback_->finished_processing(); + VLOG(CATCHAIN_INFO) << this << ": sent finished processing"; + alarm_timestamp() = td::Timestamp::in(opts_.idle_timeout); + } +} + +void CatChainImpl::need_new_block(td::Timestamp t) { + if (!receiver_started_) { + return; + } + if (!force_process_) { + VLOG(CATCHAIN_INFO) << this << ": forcing creation of new block"; + } + force_process_ = true; + if (!active_process_) { + alarm_timestamp().relax(t); + } +} + +void CatChainImpl::on_new_block(td::uint32 src_id, td::uint32 fork, CatChainBlockHash hash, CatChainBlockHeight height, + CatChainBlockHash prev, std::vector deps, + std::vector vt, td::SharedSlice data) { + VLOG(CATCHAIN_DEBUG) << this << ": new block " << hash; + if (top_blocks_.size() == 0 && !active_process_ && receiver_started_) { + alarm_timestamp().relax(td::Timestamp::in(opts_.idle_timeout)); + } + + CatChainBlock *p = nullptr; + if (!prev.is_zero()) { + p = get_block(prev); + CHECK(p != nullptr); + if (top_blocks_.exists(prev)) { + top_blocks_.remove(prev); + } + } + + std::vector v; + v.resize(deps.size()); + for (size_t i = 0; i < deps.size(); i++) { + if (!blamed_sources_[src_id] && top_blocks_.exists(deps[i])) { + top_blocks_.remove(deps[i]); + } + v[i] = get_block(deps[i]); + CHECK(v[i] != nullptr); + } + + CHECK(src_id < sources_.size()); + auto src_hash = sources_[src_id]; + blocks_[hash] = + CatChainBlock::create(src_id, fork, src_hash, height, hash, std::move(data), p, std::move(v), std::move(vt)); + + auto B = get_block(hash); + CHECK(B != nullptr); + + if (!blamed_sources_[src_id]) { + send_preprocess(B); + top_source_blocks_[src_id] = B; + + if (src_id != local_idx_) { + top_blocks_.insert(B->hash(), B); + } + + if (top_blocks_.size() == 0 && !active_process_ && receiver_started_) { + alarm_timestamp().relax(td::Timestamp::in(opts_.idle_timeout)); + } + } +} + +void CatChainImpl::on_blame(td::uint32 src_id) { + if (blamed_sources_[src_id]) { + return; + } + blamed_sources_[src_id] = true; + top_source_blocks_[src_id] = nullptr; + + // recompute top blocks + top_blocks_.reset(); + auto size = static_cast(sources_.size()); + for (td::uint32 i = 0; i < size; i++) { + if (!blamed_sources_[i] && top_source_blocks_[i] && i != local_idx_) { + auto B = top_source_blocks_[i]; + bool f = true; + if (B->is_processed()) { + continue; + } + for (td::uint32 j = 0; j < size; j++) { + if (i != j && !blamed_sources_[j] && top_source_blocks_[j]) { + if (top_source_blocks_[j]->is_descendant_of(B)) { + f = false; + break; + } + } + } + if (f) { + top_blocks_.insert(B->hash(), B); + } + } + } +} + +void CatChainImpl::on_custom_message(PublicKeyHash src, td::BufferSlice data) { + callback_->process_message(src, std::move(data)); +} + +void CatChainImpl::on_custom_query(PublicKeyHash src, td::BufferSlice data, td::Promise promise) { + callback_->process_query(src, std::move(data), std::move(promise)); +} + +void CatChainImpl::on_broadcast(PublicKeyHash src, td::BufferSlice data) { + VLOG(CATCHAIN_INFO) << this << ": processing broadcast"; + callback_->process_broadcast(src, std::move(data)); + VLOG(CATCHAIN_INFO) << this << ": sent processing broadcast"; +} + +void CatChainImpl::on_receiver_started() { + receiver_started_ = true; + callback_->started(); + CHECK(!active_process_); + active_process_ = true; + send_process(); +} + +CatChainImpl::CatChainImpl(std::unique_ptr callback, CatChainOptions opts, + td::actor::ActorId keyring, td::actor::ActorId adnl, + td::actor::ActorId overlay_manager, std::vector ids, + PublicKeyHash local_id, CatChainSessionId unique_hash, std::string db_root) + : opts_(std::move(opts)), db_root_(db_root) { + callback_ = std::move(callback); + sources_.resize(ids.size()); + unique_hash_ = unique_hash; + for (size_t i = 0; i < ids.size(); i++) { + sources_[i] = ids[i].pub_key.compute_short_id(); + if (sources_[i] == local_id) { + local_idx_ = static_cast(i); + } + } + blamed_sources_.resize(ids.size(), false); + top_source_blocks_.resize(ids.size(), nullptr); + + args_ = std::make_unique(keyring, adnl, overlay_manager, std::move(ids), local_id, unique_hash); +} + +void CatChainImpl::alarm() { + alarm_timestamp() = td::Timestamp::never(); + if (!active_process_) { + active_process_ = true; + send_process(); + } +} + +void CatChainImpl::start_up() { + class ChainCb : public CatChainReceiverInterface::Callback { + public: + void new_block(td::uint32 src_id, td::uint32 fork_id, CatChainBlockHash hash, CatChainBlockHeight height, + CatChainBlockHash prev, std::vector deps, std::vector vt, + td::SharedSlice data) override { + td::actor::send_closure(id_, &CatChainImpl::on_new_block, src_id, fork_id, hash, height, prev, std::move(deps), + std::move(vt), std::move(data)); + } + void blame(td::uint32 src_id) override { + td::actor::send_closure(id_, &CatChainImpl::on_blame, src_id); + } + void on_custom_message(PublicKeyHash src, td::BufferSlice data) override { + td::actor::send_closure(id_, &CatChainImpl::on_custom_message, src, std::move(data)); + } + void on_custom_query(PublicKeyHash src, td::BufferSlice data, td::Promise promise) override { + td::actor::send_closure(id_, &CatChainImpl::on_custom_query, src, std::move(data), std::move(promise)); + } + void on_broadcast(PublicKeyHash src, td::BufferSlice data) override { + td::actor::send_closure(id_, &CatChainImpl::on_broadcast, src, std::move(data)); + } + void start() override { + td::actor::send_closure(id_, &CatChainImpl::on_receiver_started); + } + ChainCb(td::actor::ActorId id) : id_(id) { + } + + private: + td::actor::ActorId id_; + }; + + auto cb = std::make_unique(actor_id(this)); + + receiver_ = + CatChainReceiverInterface::create(std::move(cb), opts_, args_->keyring, args_->adnl, args_->overlay_manager, + std::move(args_->ids), args_->local_id, args_->unique_hash, db_root_); + args_ = nullptr; + //alarm_timestamp() = td::Timestamp::in(opts_.idle_timeout); +} + +td::actor::ActorOwn CatChain::create(std::unique_ptr callback, CatChainOptions opts, + td::actor::ActorId keyring, + td::actor::ActorId adnl, + td::actor::ActorId overlay_manager, + std::vector ids, PublicKeyHash local_id, + CatChainSessionId unique_hash, std::string db_root) { + return td::actor::create_actor("catchain", std::move(callback), std::move(opts), keyring, adnl, + overlay_manager, std::move(ids), local_id, unique_hash, db_root); +} + +CatChainBlock *CatChainImpl::get_block(CatChainBlockHash hash) const { + auto it = blocks_.find(hash); + if (it == blocks_.end()) { + return nullptr; + } else { + return it->second.get(); + } +} + +void CatChainImpl::destroy() { + td::actor::send_closure(receiver_, &CatChainReceiverInterface::destroy); + receiver_.release(); + stop(); +} + +} // namespace catchain + +} // namespace ton diff --git a/catchain/catchain.h b/catchain/catchain.h new file mode 100644 index 00000000..6b622913 --- /dev/null +++ b/catchain/catchain.h @@ -0,0 +1,125 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +#include "adnl/adnl.h" +#include "adnl/utils.hpp" +#include "overlay/overlays.h" +#include "catchain-types.h" + +namespace ton { + +namespace catchain { + +class CatChainBlock { + public: + class Extra { + public: + virtual ~Extra() = default; + }; + virtual td::SharedSlice &payload() = 0; + virtual const td::SharedSlice &payload() const = 0; + virtual Extra *extra() const = 0; + virtual std::unique_ptr move_extra() = 0; + virtual void set_extra(std::unique_ptr extra) = 0; + + virtual td::uint32 source() const = 0; + virtual td::uint32 fork() const = 0; + virtual PublicKeyHash source_hash() const = 0; + virtual CatChainBlockHash hash() const = 0; + virtual CatChainBlockHeight height() const = 0; + + virtual CatChainBlock *prev() = 0; + virtual const CatChainBlock *prev() const = 0; + virtual const std::vector &deps() const = 0; + virtual const std::vector &vt() const = 0; + + virtual bool preprocess_is_sent() const = 0; + virtual void preprocess_sent() = 0; + + virtual bool is_processed() const = 0; + virtual void set_processed() = 0; + + virtual bool is_descendant_of(CatChainBlock *block) = 0; + + static std::unique_ptr create(td::uint32 src, td::uint32 fork_id, PublicKeyHash src_hash, + CatChainBlockHeight height, CatChainBlockHash hash, + td::SharedSlice payload, CatChainBlock *prev, + std::vector deps, std::vector vt); + + virtual ~CatChainBlock() = default; +}; + +class CatChain : public td::actor::Actor { + public: + class Callback { + public: + virtual void process_blocks(std::vector blocks) = 0; + virtual void finished_processing() = 0; + virtual void preprocess_block(CatChainBlock *block) = 0; + virtual void process_broadcast(PublicKeyHash src, td::BufferSlice data) = 0; + virtual void process_message(PublicKeyHash src, td::BufferSlice data) = 0; + virtual void process_query(PublicKeyHash src, td::BufferSlice data, td::Promise promise) = 0; + virtual void started() = 0; + virtual ~Callback() = default; + }; + struct PrintId { + CatChainSessionId instance_; + PublicKeyHash local_id_; + }; + virtual PrintId print_id() const = 0; + virtual void processed_block(td::BufferSlice payload) = 0; + virtual void need_new_block(td::Timestamp t) = 0; + virtual void debug_add_fork(td::BufferSlice payload, CatChainBlockHeight height) = 0; + + virtual void send_broadcast(td::BufferSlice data) = 0; + virtual void send_message(PublicKeyHash dst, td::BufferSlice data) = 0; + virtual void send_query(PublicKeyHash dst, std::string name, td::Promise promise, + td::Timestamp timeout, td::BufferSlice query) = 0; + virtual void send_query_via(PublicKeyHash dst, std::string name, td::Promise promise, + td::Timestamp timeout, td::BufferSlice query, td::uint64 max_answer_size, + td::actor::ActorId via) = 0; + virtual void destroy() = 0; + + static td::actor::ActorOwn create(std::unique_ptr callback, CatChainOptions opts, + td::actor::ActorId keyring, + td::actor::ActorId adnl, + td::actor::ActorId overlay_manager, + std::vector ids, PublicKeyHash local_id, + CatChainSessionId unique_hash, std::string db_root); + virtual ~CatChain() = default; +}; + +} // namespace catchain + +} // namespace ton + +namespace td { + +inline td::StringBuilder &operator<<(td::StringBuilder &sb, const ton::catchain::CatChain::PrintId &print_id) { + sb << "[catchain " << print_id.instance_ << "@" << print_id.local_id_ << "]"; + return sb; +} + +inline td::StringBuilder &operator<<(td::StringBuilder &sb, const ton::catchain::CatChain *catchain) { + sb << catchain->print_id(); + return sb; +} + +} // namespace td diff --git a/catchain/catchain.hpp b/catchain/catchain.hpp new file mode 100644 index 00000000..176491f5 --- /dev/null +++ b/catchain/catchain.hpp @@ -0,0 +1,138 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +#include + +#include "catchain.h" +#include "catchain-types.h" +#include "catchain-receiver-interface.h" +#include "td/utils/DecTree.h" + +namespace ton { + +namespace catchain { + +class CatChainImpl : public CatChain { + private: + std::unique_ptr callback_; + CatChainOptions opts_; + td::DecTree top_blocks_; + std::map> blocks_; + std::vector top_source_blocks_; + + std::vector sources_; + std::vector blamed_sources_; + + std::vector process_deps_; + + CatChainSessionId unique_hash_; + td::uint32 local_idx_; + bool active_process_ = false; + bool force_process_ = false; + td::actor::ActorOwn receiver_; + + bool receiver_started_ = false; + + std::string db_root_; + + void send_process(); + void send_preprocess(CatChainBlock *block); + void set_processed(CatChainBlock *block); + + struct Args { + td::actor::ActorId keyring; + td::actor::ActorId adnl; + td::actor::ActorId overlay_manager; + std::vector ids; + PublicKeyHash local_id; + CatChainSessionId unique_hash; + + Args(td::actor::ActorId keyring, td::actor::ActorId adnl, + td::actor::ActorId overlay_manager, std::vector ids, PublicKeyHash local_id, + CatChainSessionId unique_hash) + : keyring(keyring) + , adnl(adnl) + , overlay_manager(overlay_manager) + , ids(std::move(ids)) + , local_id(local_id) + , unique_hash(unique_hash) { + } + }; + std::unique_ptr args_; + + public: + PrintId print_id() const override { + return PrintId{unique_hash_, sources_[local_idx_]}; + } + CatChainBlock *get_block(CatChainBlockHash hash) const; + void on_new_block(td::uint32 src_id, td::uint32 fork, CatChainBlockHash hash, CatChainBlockHeight height, + CatChainBlockHash prev, std::vector deps, std::vector vt, + td::SharedSlice data); + void on_blame(td::uint32 src_id); + void on_custom_message(PublicKeyHash src, td::BufferSlice data); + void on_custom_query(PublicKeyHash src, td::BufferSlice data, td::Promise promise); + void on_broadcast(PublicKeyHash src, td::BufferSlice data); + void on_receiver_started(); + void processed_block(td::BufferSlice payload) override; + void need_new_block(td::Timestamp t) override; + void debug_add_fork(td::BufferSlice payload, CatChainBlockHeight height) override { + td::actor::send_closure(receiver_, &CatChainReceiverInterface::debug_add_fork, std::move(payload), height, + std::vector{}); + } + + void send_broadcast(td::BufferSlice data) override { + td::actor::send_closure(receiver_, &CatChainReceiverInterface::send_fec_broadcast, std::move(data)); + } + void send_message(PublicKeyHash dst, td::BufferSlice data) override { + td::actor::send_closure(receiver_, &CatChainReceiverInterface::send_custom_message_data, dst, std::move(data)); + } + void send_query(PublicKeyHash dst, std::string name, td::Promise promise, td::Timestamp timeout, + td::BufferSlice query) override { + td::actor::send_closure(receiver_, &CatChainReceiverInterface::send_custom_query_data, dst, name, + std::move(promise), timeout, std::move(query)); + } + void send_query_via(PublicKeyHash dst, std::string name, td::Promise promise, td::Timestamp timeout, + td::BufferSlice query, td::uint64 max_answer_size, + td::actor::ActorId via) override { + td::actor::send_closure(receiver_, &CatChainReceiverInterface::send_custom_query_data_via, dst, name, + std::move(promise), timeout, std::move(query), max_answer_size, via); + } + void destroy() override; + CatChainImpl(std::unique_ptr callback, CatChainOptions opts, td::actor::ActorId keyring, + td::actor::ActorId adnl, td::actor::ActorId overlay_manager, + std::vector ids, PublicKeyHash local_id, CatChainSessionId unique_hash, + std::string db_root); + + void alarm() override; + void start_up() override; +}; + +} // namespace catchain + +} // namespace ton + +namespace td { + +inline td::StringBuilder &operator<<(td::StringBuilder &sb, const ton::catchain::CatChainImpl *catchain) { + sb << catchain->print_id(); + return sb; +} + +} // namespace td diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt new file mode 100644 index 00000000..8fd70f7b --- /dev/null +++ b/common/CMakeLists.txt @@ -0,0 +1,21 @@ +cmake_minimum_required(VERSION 3.0.2 FATAL_ERROR) + +set(COMMON_SOURCE + checksum.h + errorcode.h + status.h + io.hpp + + errorlog.h + errorlog.cpp +) + + +add_library(common STATIC ${COMMON_SOURCE}) + +target_include_directories(common PUBLIC + $ + $/.. + ${OPENSSL_INCLUDE_DIR} +) +target_link_libraries(common PRIVATE tdutils ton_crypto ) diff --git a/common/checksum.h b/common/checksum.h new file mode 100644 index 00000000..440d9359 --- /dev/null +++ b/common/checksum.h @@ -0,0 +1,33 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +#include "td/utils/int_types.h" +#include "crypto/common/bitstring.h" +#include "td/utils/crypto.h" + +namespace td { + +inline Bits256 sha256_bits256(Slice data) { + Bits256 id; + sha256(data, id.as_slice()); + return id; +} + +} // namespace td diff --git a/common/delay.h b/common/delay.h new file mode 100644 index 00000000..111a9f35 --- /dev/null +++ b/common/delay.h @@ -0,0 +1,52 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +#include "td/actor/actor.h" + +namespace ton { + +template +class DelayedAction : public td::actor::Actor { + public: + DelayedAction(T promise) : promise_(std::move(promise)) { + } + void set_timer(td::Timestamp t) { + alarm_timestamp() = t; + } + void alarm() override { + promise_(); + stop(); + } + + static void create(T promise, td::Timestamp t) { + auto A = td::actor::create_actor("delayed", std::move(promise)); + td::actor::send_closure(A, &DelayedAction::set_timer, t); + A.release(); + } + + private: + T promise_; +}; + +template +void delay_action(T promise, td::Timestamp timeout) { + DelayedAction::create(std::move(promise), timeout); +} +} // namespace ton diff --git a/common/errorcode.h b/common/errorcode.h new file mode 100644 index 00000000..12d5a704 --- /dev/null +++ b/common/errorcode.h @@ -0,0 +1,25 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +namespace ton { + +enum ErrorCode : int { failure = 601, error = 602, warning = 603, protoviolation = 621, notready = 651, timeout = 652 }; + +} diff --git a/common/errorlog.cpp b/common/errorlog.cpp new file mode 100644 index 00000000..3dfcf9c8 --- /dev/null +++ b/common/errorlog.cpp @@ -0,0 +1,74 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "errorlog.h" +#include "checksum.h" + +#include "td/utils/port/FileFd.h" +#include "td/utils/filesystem.h" +#include "td/utils/port/path.h" +#include "td/utils/Time.h" + +#include + +namespace ton { + +namespace errorlog { + +td::FileFd fd; +std::mutex init_mutex_; +std::string files_path_; + +void ErrorLog::create(std::string db_root) { + init_mutex_.lock(); + if (!fd.empty()) { + init_mutex_.unlock(); + return; + } + auto path = db_root + "/error"; + td::mkdir(path).ensure(); + files_path_ = path + "/files"; + td::mkdir(files_path_).ensure(); + auto R = td::FileFd::open(path + "/log.txt", + td::FileFd::Flags::Write | td::FileFd::Flags::Append | td::FileFd::Flags::Create); + R.ensure(); + fd = R.move_as_ok(); + init_mutex_.unlock(); +} + +void ErrorLog::log(std::string error) { + error = PSTRING() << "[" << td::Clocks::system() << "] " << error << "\n"; + CHECK(!fd.empty()); + auto s = td::Slice{error}; + while (s.size() > 0) { + auto R = fd.write(s); + R.ensure(); + s.remove_prefix(R.move_as_ok()); + } +} + +void ErrorLog::log_file(td::BufferSlice data) { + auto filename = sha256_bits256(data.as_slice()); + auto path = files_path_ + "/" + filename.to_hex(); + + td::write_file(path, data.as_slice()).ensure(); +} + +} // namespace errorlog + +} // namespace ton diff --git a/common/errorlog.h b/common/errorlog.h new file mode 100644 index 00000000..ed233000 --- /dev/null +++ b/common/errorlog.h @@ -0,0 +1,37 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +#include +#include "td/utils/buffer.h" + +namespace ton { + +namespace errorlog { + +class ErrorLog { + public: + static void create(std::string db_root); + static void log(std::string error); + static void log_file(td::BufferSlice data); +}; + +} // namespace errorlog + +} // namespace ton diff --git a/common/io.hpp b/common/io.hpp new file mode 100644 index 00000000..c222e95a --- /dev/null +++ b/common/io.hpp @@ -0,0 +1,45 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +#include "td/utils/buffer.h" +#include "td/utils/misc.h" +#include "td/utils/crypto.h" +#include "td/utils/format.h" +#include "td/utils/base64.h" +#include "tl-utils/tl-utils.hpp" + +#include "common/errorcode.h" +#include "common/status.h" +#include "keys/keys.hpp" + +#include "crypto/common/bitstring.h" + +namespace td { + +template +StringBuilder &operator<<(StringBuilder &stream, const td::BitArray &x) { + return stream << td::base64_encode(as_slice(x)); +} + +inline StringBuilder &operator<<(StringBuilder &stream, const ton::PublicKeyHash &value) { + return stream << value.bits256_value(); +} + +} // namespace td diff --git a/common/status.h b/common/status.h new file mode 100644 index 00000000..ddcdbfb9 --- /dev/null +++ b/common/status.h @@ -0,0 +1,33 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +#include "td/utils/Status.h" + +namespace td { + +inline td::Status status_prefix(td::Status &&status, std::string prefix) { + if (status.is_ok()) { + return std::move(status); + } else { + return td::Status::Error(status.code(), prefix + status.message().str()); + } +} + +} // namespace td diff --git a/crypto/CMakeLists.txt b/crypto/CMakeLists.txt new file mode 100644 index 00000000..4ffa2a19 --- /dev/null +++ b/crypto/CMakeLists.txt @@ -0,0 +1,333 @@ +cmake_minimum_required(VERSION 3.0.2 FATAL_ERROR) + +if (NOT OPENSSL_FOUND) + find_package(OpenSSL REQUIRED) +endif() + +set(TON_CRYPTO_SOURCE + Ed25519.cpp + common/bigint.cpp + common/refcnt.cpp + common/refint.cpp + common/bitstring.cpp + common/util.cpp + ellcurve/Ed25519.cpp + ellcurve/Fp25519.cpp + ellcurve/Montgomery.cpp + ellcurve/TwEdwards.cpp + openssl/bignum.cpp + openssl/residue.cpp + openssl/rand.cpp + vm/stack.cpp + vm/atom.cpp + vm/continuation.cpp + vm/dict.cpp + vm/dispatch.cpp + vm/opctable.cpp + vm/cp0.cpp + vm/stackops.cpp + vm/tupleops.cpp + vm/arithops.cpp + vm/cellops.cpp + vm/contops.cpp + vm/dictops.cpp + vm/debugops.cpp + vm/tonops.cpp + vm/boc.cpp + tl/tlblib.cpp + + Ed25519.h + common/AtomicRef.h + common/bigint.hpp + common/bitstring.h + common/refcnt.hpp + common/refint.h + common/util.h + + ellcurve/Ed25519.h + ellcurve/Fp25519.h + ellcurve/Montgomery.h + ellcurve/TwEdwards.h + + openssl/bignum.h + openssl/digest.h + openssl/rand.hpp + openssl/residue.h + + tl/tlbc-aux.h + tl/tlbc-data.h + tl/tlblib.hpp + + vm/arithops.h + vm/atom.h + vm/boc.h + vm/box.hpp + vm/cellops.h + vm/continuation.h + vm/contops.h + vm/cp0.h + vm/debugops.h + vm/dict.h + vm/dictops.h + vm/excno.hpp + vm/fmt.hpp + vm/log.h + vm/opctable.h + vm/stack.hpp + vm/stackops.h + vm/tupleops.h + vm/tonops.h + vm/vmstate.h + + vm/cells.h + vm/cellslice.h + + vm/cells/Cell.cpp + vm/cells/CellBuilder.cpp + vm/cells/CellHash.cpp + vm/cells/CellSlice.cpp + vm/cells/CellTraits.cpp + vm/cells/CellUsageTree.cpp + vm/cells/DataCell.cpp + vm/cells/LevelMask.cpp + vm/cells/MerkleProof.cpp + vm/cells/MerkleUpdate.cpp + + vm/cells/Cell.h + vm/cells/CellBuilder.h + vm/cells/CellHash.h + vm/cells/CellSlice.h + vm/cells/CellTraits.h + vm/cells/CellUsageTree.h + vm/cells/CellWithStorage.h + vm/cells/DataCell.h + vm/cells/ExtCell.h + vm/cells/LevelMask.h + vm/cells/MerkleProof.h + vm/cells/MerkleUpdate.h + vm/cells/PrunnedCell.h + vm/cells/UsageCell.h + vm/cells/VirtualCell.h + vm/cells/VirtualizationParameters.h + + vm/db/StaticBagOfCellsDb.h + vm/db/StaticBagOfCellsDb.cpp + + vm/db/BlobView.h + vm/db/BlobView.cpp +) + +set(TON_DB_SOURCE + vm/db/DynamicBagOfCellsDb.cpp + vm/db/CellStorage.cpp + vm/db/TonDb.cpp + + vm/db/DynamicBagOfCellsDb.h + vm/db/CellHashTable.h + vm/db/CellStorage.h + vm/db/TonDb.h +) + +set(FIFT_SOURCE + fift/Dictionary.cpp + fift/Fift.cpp + fift/IntCtx.cpp + fift/SourceLookup.cpp + fift/utils.cpp + fift/words.cpp + + fift/Dictionary.h + fift/Fift.h + fift/IntCtx.h + fift/SourceLookup.h + fift/utils.h + fift/words.h +) + +set(PARSER_SOURCE + parser/srcread.cpp + parser/lexer.cpp + parser/symtable.cpp + + parser/srcread.h + parser/lexer.h + parser/symtable.h +) + +set(FUNC_LIB_SOURCE + func/keywords.cpp + func/unify-types.cpp + func/parse-func.cpp + func/abscode.cpp + func/gen-abscode.cpp + func/analyzer.cpp + func/asmops.cpp + func/builtins.cpp + func/stack-transform.cpp + func/optimize.cpp + func/codegen.cpp +) + +set(TLB_BLOCK_AUTO + ${CMAKE_CURRENT_SOURCE_DIR}/block/block-auto.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/block/block-auto.h +) + +set(BLOCK_SOURCE + block/Binlog.h + block/Binlog.cpp + block/block.cpp + block/block-db.cpp + block/block-parse.cpp + block/check-proof.cpp + block/mc-config.cpp + block/output-queue-merger.cpp + block/transaction.cpp + ${TLB_BLOCK_AUTO} + + block/block-binlog.h + block/block-db-impl.h + block/block-db.h + block/block.h + block/block-parse.h + block/check-proof.h + block/output-queue-merger.h + block/transaction.h +) + +set(ED25519_TEST_SOURCE + ${CMAKE_CURRENT_SOURCE_DIR}/test/Ed25519.cpp + PARENT_SCOPE +) + +set(TONDB_TEST_SOURCE + ${CMAKE_CURRENT_SOURCE_DIR}/test/test-db.cpp + PARENT_SCOPE +) + +set(CELLS_TEST_SOURCE + ${CMAKE_CURRENT_SOURCE_DIR}/test/test-cells.cpp + PARENT_SCOPE +) + +set(TONVM_TEST_SOURCE + ${CMAKE_CURRENT_SOURCE_DIR}/test/vm.cpp + PARENT_SCOPE +) + +set(FIFT_TEST_SOURCE + ${CMAKE_CURRENT_SOURCE_DIR}/test/fift.cpp + PARENT_SCOPE +) + +add_library(ton_crypto STATIC ${TON_CRYPTO_SOURCE}) +target_include_directories(ton_crypto PUBLIC $ + $) +target_link_libraries(ton_crypto PUBLIC ${OPENSSL_CRYPTO_LIBRARY} tdutils) +if (NOT WIN32) + target_link_libraries(ton_crypto PUBLIC dl z) +endif() +target_include_directories(ton_crypto SYSTEM PUBLIC ${OPENSSL_INCLUDE_DIR}) + +add_library(ton_db STATIC ${TON_DB_SOURCE}) +target_include_directories(ton_db PUBLIC $ + $) +target_link_libraries(ton_db PUBLIC tdutils tddb ton_crypto) + +add_executable(test-ed25519-crypto test/test-ed25519-crypto.cpp) +target_include_directories(test-ed25519-crypto PUBLIC $) +target_link_libraries(test-ed25519-crypto PUBLIC ton_crypto) + +add_library(fift-lib ${FIFT_SOURCE}) +target_include_directories(fift-lib PUBLIC $) +target_link_libraries(fift-lib PUBLIC ton_crypto ton_db tdutils ton_block) +set_target_properties(fift-lib PROPERTIES OUTPUT_NAME fift) + +add_executable(fift fift/fift-main.cpp) +target_link_libraries(fift PUBLIC fift-lib) +if (WINGETOPT_FOUND) + target_link_libraries_system(fift wingetopt) +endif() + +add_library(src_parser ${PARSER_SOURCE}) +target_include_directories(src_parser PUBLIC $) +target_link_libraries(src_parser PUBLIC ton_crypto) + +add_executable(func func/func.cpp ${FUNC_LIB_SOURCE}) +target_include_directories(func PUBLIC $) +target_link_libraries(func PUBLIC ton_crypto src_parser) +if (WINGETOPT_FOUND) + target_link_libraries_system(func wingetopt) +endif() + +add_executable(tlbc tl/tlbc.cpp) +target_include_directories(tlbc PUBLIC $) +target_link_libraries(tlbc PUBLIC ton_crypto src_parser) +if (WINGETOPT_FOUND) + target_link_libraries_system(tlbc wingetopt) +endif() + +add_library(ton_block ${BLOCK_SOURCE}) +target_include_directories(ton_block PUBLIC $ + $ $) +target_link_libraries(ton_block PUBLIC ton_crypto tdutils tdactor tl_api) + +set(TURN_OFF_LSAN cd .) +if (TON_USE_ASAN AND NOT WIN32) + set(TURN_OFF_LSAN export LSAN_OPTIONS=detect_leaks=0) +endif() + +if (NOT CMAKE_CROSSCOMPILING) + set(GENERATE_TLB_CMD tlbc) + add_custom_command( + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/block + COMMAND ${TURN_OFF_LSAN} + COMMAND ${GENERATE_TLB_CMD} -o block-auto -n block::gen -z block.tlb + COMMENT "Generate block tlb source files" + OUTPUT ${TLB_BLOCK_AUTO} + DEPENDS tlbc block/block.tlb + ) + add_custom_target(tlb_generate_block DEPENDS ${TLB_BLOCK_AUTO}) + add_dependencies(ton_block tlb_generate_block) + + add_custom_target(gen_fif ALL) + function(GenFif) + set(options ) + set(oneValueArgs DEST) + set(multiValueArgs SOURCE) + set(FUNC_LIB_SOURCE smartcont/stdlib.fc) + cmake_parse_arguments(ARG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN} ) + string(REGEX REPLACE "[^a-zA-Z_]" "_" ID ${ARG_DEST}) + add_custom_command( + COMMENT "Generate ${ARG_DEST}" + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} + COMMAND func -PS -o ${ARG_DEST} ${FUNC_LIB_SOURCE} ${ARG_SOURCE} + MAIN_DEPENDENCY ${ARG_SOURCE} + DEPENDS func ${FUNC_LIB_SOURCE} + OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/${ARG_DEST} + ) + add_custom_target(gen_fif_${ID} DEPENDS ${ARG_DEST}) + add_dependencies(gen_fif gen_fif_${ID}) + endfunction() + + GenFif(DEST smartcont/config-code.fif SOURCE smartcont/config-code.fc) + GenFif(DEST smartcont/wallet-code.fif SOURCE smartcont/wallet-code.fc) + GenFif(DEST smartcont/simple-wallet-code.fif SOURCE smartcont/simple-wallet-code.fc) + GenFif(DEST smartcont/elector-code.fif SOURCE smartcont/elector-code.fc) +endif() + +add_executable(create-state block/create-state.cpp) +target_include_directories(create-state PUBLIC $ + $) +target_link_libraries(create-state PUBLIC ton_crypto fift-lib ton_block) +if (WINGETOPT_FOUND) + target_link_libraries_system(create-state wingetopt) +endif() + +add_executable(test-block block/test-block.cpp) +target_include_directories(test-block PUBLIC $ + $) +target_link_libraries(test-block PUBLIC ton_crypto fift-lib ton_block) +if (WINGETOPT_FOUND) + target_link_libraries_system(test-block wingetopt) +endif() diff --git a/crypto/Ed25519.cpp b/crypto/Ed25519.cpp new file mode 100644 index 00000000..6a21f83f --- /dev/null +++ b/crypto/Ed25519.cpp @@ -0,0 +1,394 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "crypto/Ed25519.h" + +#if TD_HAVE_OPENSSL + +#include + +#if OPENSSL_VERSION_NUMBER >= 0x10101000L && OPENSSL_VERSION_NUMBER != 0x20000000L + +#include "td/utils/base64.h" +#include "td/utils/BigNum.h" +#include "td/utils/format.h" +#include "td/utils/logging.h" +#include "td/utils/misc.h" +#include "td/utils/ScopeGuard.h" + +#include +#include +#include + +#else + +#include "crypto/ellcurve/Ed25519.h" + +#endif + +namespace td { + +Ed25519::PublicKey::PublicKey(SecureString octet_string) : octet_string_(std::move(octet_string)) { +} + +SecureString Ed25519::PublicKey::as_octet_string() const { + return octet_string_.copy(); +} + +Ed25519::PrivateKey::PrivateKey(SecureString octet_string) : octet_string_(std::move(octet_string)) { +} + +SecureString Ed25519::PrivateKey::as_octet_string() const { + return octet_string_.copy(); +} + +#if OPENSSL_VERSION_NUMBER >= 0x10101000L && OPENSSL_VERSION_NUMBER != 0x20000000L + +namespace detail { + +static Result X25519_key_from_PKEY(EVP_PKEY *pkey, bool is_private) { + auto func = is_private ? &EVP_PKEY_get_raw_private_key : &EVP_PKEY_get_raw_public_key; + size_t len = 0; + if (func(pkey, nullptr, &len) == 0) { + return Status::Error("Failed to get raw key length"); + } + CHECK(len == 32); + + SecureString result(len); + if (func(pkey, result.as_mutable_slice().ubegin(), &len) == 0) { + return Status::Error("Failed to get raw key"); + } + return std::move(result); +} + +static EVP_PKEY *X25519_key_to_PKEY(Slice key, bool is_private) { + auto func = is_private ? &EVP_PKEY_new_raw_private_key : &EVP_PKEY_new_raw_public_key; + return func(EVP_PKEY_ED25519, nullptr, key.ubegin(), key.size()); +} + +static Result X25519_pem_from_PKEY(EVP_PKEY *pkey, bool is_private, Slice password) { + BIO *mem_bio = BIO_new(BIO_s_mem()); + SCOPE_EXIT { + BIO_vfree(mem_bio); + }; + if (is_private) { + PEM_write_bio_PrivateKey(mem_bio, pkey, EVP_aes_256_cbc(), const_cast(password.ubegin()), + narrow_cast(password.size()), nullptr, nullptr); + } else { + PEM_write_bio_PUBKEY(mem_bio, pkey); + } + char *data_ptr = nullptr; + auto data_size = BIO_get_mem_data(mem_bio, &data_ptr); + return std::string(data_ptr, data_size); +} + +static int password_cb(char *buf, int size, int rwflag, void *u) { + auto &password = *reinterpret_cast(u); + auto password_size = narrow_cast(password.size()); + if (size < password_size) { + return -1; + } + if (rwflag == 0) { + MutableSlice(buf, size).copy_from(password); + } + return password_size; +} + +static EVP_PKEY *X25519_pem_to_PKEY(Slice pem, Slice password) { + BIO *mem_bio = BIO_new_mem_buf(pem.ubegin(), narrow_cast(pem.size())); + SCOPE_EXIT { + BIO_vfree(mem_bio); + }; + + return PEM_read_bio_PrivateKey(mem_bio, nullptr, password_cb, &password); +} + +} // namespace detail + +Result Ed25519::generate_private_key() { + EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new_id(NID_ED25519, nullptr); + if (pctx == nullptr) { + return Status::Error("Can't create EVP_PKEY_CTX"); + } + SCOPE_EXIT { + EVP_PKEY_CTX_free(pctx); + }; + + if (EVP_PKEY_keygen_init(pctx) <= 0) { + return Status::Error("Can't init keygen"); + } + + EVP_PKEY *pkey = nullptr; + if (EVP_PKEY_keygen(pctx, &pkey) <= 0) { + return Status::Error("Can't generate random private key"); + } + SCOPE_EXIT { + EVP_PKEY_free(pkey); + }; + + TRY_RESULT(private_key, detail::X25519_key_from_PKEY(pkey, true)); + return std::move(private_key); +} + +Result Ed25519::PrivateKey::get_public_key() const { + auto pkey = detail::X25519_key_to_PKEY(octet_string_, true); + if (pkey == nullptr) { + return Status::Error("Can't import private key"); + } + SCOPE_EXIT { + EVP_PKEY_free(pkey); + }; + + TRY_RESULT(key, detail::X25519_key_from_PKEY(pkey, false)); + return Ed25519::PublicKey(std::move(key)); +} + +Result Ed25519::PrivateKey::as_pem(Slice password) const { + auto pkey = detail::X25519_key_to_PKEY(octet_string_, true); + if (pkey == nullptr) { + return Status::Error("Can't import private key"); + } + SCOPE_EXIT { + EVP_PKEY_free(pkey); + }; + + return detail::X25519_pem_from_PKEY(pkey, true, password); +} + +Result Ed25519::PrivateKey::from_pem(Slice pem, Slice password) { + auto pkey = detail::X25519_pem_to_PKEY(pem, password); + if (pkey == nullptr) { + return Status::Error("Can't import private key from pem"); + } + TRY_RESULT(key, detail::X25519_key_from_PKEY(pkey, true)); + return Ed25519::PrivateKey(std::move(key)); +} + +Result Ed25519::PrivateKey::sign(Slice data) const { + auto pkey = detail::X25519_key_to_PKEY(octet_string_, true); + if (pkey == nullptr) { + return Status::Error("Can't import private key"); + } + SCOPE_EXIT { + EVP_PKEY_free(pkey); + }; + + EVP_MD_CTX *md_ctx = EVP_MD_CTX_new(); + if (md_ctx == nullptr) { + return Status::Error("Can't create EVP_MD_CTX"); + } + SCOPE_EXIT { + EVP_MD_CTX_free(md_ctx); + }; + + if (EVP_DigestSignInit(md_ctx, nullptr, nullptr, nullptr, pkey) <= 0) { + return Status::Error("Can't init DigestSign"); + } + + SecureString res(64, '\0'); + size_t len = 64; + if (EVP_DigestSign(md_ctx, res.as_mutable_slice().ubegin(), &len, data.ubegin(), data.size()) <= 0) { + return Status::Error("Can't sign data"); + } + return std::move(res); +} + +Status Ed25519::PublicKey::verify_signature(Slice data, Slice signature) const { + auto pkey = detail::X25519_key_to_PKEY(octet_string_, false); + if (pkey == nullptr) { + return Status::Error("Can't import public key"); + } + SCOPE_EXIT { + EVP_PKEY_free(pkey); + }; + + EVP_MD_CTX *md_ctx = EVP_MD_CTX_new(); + if (md_ctx == nullptr) { + return Status::Error("Can't create EVP_MD_CTX"); + } + SCOPE_EXIT { + EVP_MD_CTX_free(md_ctx); + }; + + if (EVP_DigestVerifyInit(md_ctx, nullptr, nullptr, nullptr, pkey) <= 0) { + return Status::Error("Can't init DigestVerify"); + } + + if (EVP_DigestVerify(md_ctx, signature.ubegin(), signature.size(), data.ubegin(), data.size())) { + return Status::OK(); + } + return Status::Error("Wrong signature"); +} + +Result Ed25519::compute_shared_secret(const PublicKey &public_key, const PrivateKey &private_key) { + BigNum p = BigNum::from_hex("7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffed").move_as_ok(); + auto public_y = public_key.as_octet_string(); + public_y.as_mutable_slice()[31] = static_cast(public_y[31] & 127); + BigNum y = BigNum::from_le_binary(public_y); + BigNum y2 = y.clone(); + y += 1; + y2 -= 1; + + BigNumContext context; + + BigNum::mod_sub(y2, p, y2, p, context); + + BigNum inverse_y_plus_1; + BigNum::mod_inverse(inverse_y_plus_1, y2, p, context); + + BigNum u; + BigNum::mod_mul(u, y, inverse_y_plus_1, p, context); + + auto pr_key = private_key.as_octet_string(); + unsigned char buf[64]; + SHA512(Slice(pr_key).ubegin(), 32, buf); + buf[0] &= 248; + buf[31] &= 127; + buf[31] |= 64; + + auto pkey_private = EVP_PKEY_new_raw_private_key(EVP_PKEY_X25519, nullptr, buf, 32); + if (pkey_private == nullptr) { + return Status::Error("Can't import private key"); + } + SCOPE_EXIT { + EVP_PKEY_free(pkey_private); + }; + // LOG(ERROR) << buffer_to_hex(Slice(buf, 32)); + + auto pub_key = u.to_le_binary(32); + auto pkey_public = EVP_PKEY_new_raw_public_key(EVP_PKEY_X25519, nullptr, Slice(pub_key).ubegin(), pub_key.size()); + if (pkey_public == nullptr) { + return Status::Error("Can't import public key"); + } + SCOPE_EXIT { + EVP_PKEY_free(pkey_public); + }; + // LOG(ERROR) << buffer_to_hex(pub_key); + + EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new(pkey_private, nullptr); + if (ctx == nullptr) { + return Status::Error("Can't create EVP_PKEY_CTX"); + } + SCOPE_EXIT { + EVP_PKEY_CTX_free(ctx); + }; + + if (EVP_PKEY_derive_init(ctx) <= 0) { + return Status::Error("Can't init derive"); + } + if (EVP_PKEY_derive_set_peer(ctx, pkey_public) <= 0) { + return Status::Error("Can't init derive"); + } + + size_t result_len = 0; + if (EVP_PKEY_derive(ctx, nullptr, &result_len) <= 0) { + return Status::Error("Can't get result length"); + } + if (result_len != 32) { + return Status::Error("Unexpected result length"); + } + + SecureString result(result_len, '\0'); + if (EVP_PKEY_derive(ctx, result.as_mutable_slice().ubegin(), &result_len) <= 0) { + return Status::Error("Failed to compute shared secret"); + } + return std::move(result); +} + +#else + +Result Ed25519::generate_private_key() { + crypto::Ed25519::PrivateKey private_key; + if (!private_key.random_private_key(true)) { + return Status::Error("Can't generate random private key"); + } + SecureString private_key_buf(32); + if (!private_key.export_private_key(private_key_buf.as_mutable_slice())) { + return Status::Error("Failed to export private key"); + } + return PrivateKey(std::move(private_key_buf)); +} + +Result Ed25519::PrivateKey::get_public_key() const { + crypto::Ed25519::PrivateKey private_key; + if (!private_key.import_private_key(Slice(octet_string_).ubegin())) { + return Status::Error("Bad private key"); + } + SecureString public_key(32); + if (!private_key.get_public_key().export_public_key(public_key.as_mutable_slice())) { + return Status::Error("Failed to export public key"); + } + return PublicKey(std::move(public_key)); +} + +Result Ed25519::PrivateKey::as_pem(Slice password) const { + return Status::Error("Not supported"); +} + +Result Ed25519::PrivateKey::from_pem(Slice pem, Slice password) { + return Status::Error("Not supported"); +} + +Result Ed25519::PrivateKey::sign(Slice data) const { + crypto::Ed25519::PrivateKey private_key; + if (!private_key.import_private_key(Slice(octet_string_).ubegin())) { + return Status::Error("Bad private key"); + } + SecureString signature(crypto::Ed25519::sign_bytes, '\0'); + if (!private_key.sign_message(signature.as_mutable_slice(), data)) { + return Status::Error("Failed to sign message"); + } + return std::move(signature); +} + +Status Ed25519::PublicKey::verify_signature(Slice data, Slice signature) const { + if (signature.size() != crypto::Ed25519::sign_bytes) { + return Status::Error("Signature has invalid length"); + } + + crypto::Ed25519::PublicKey public_key; + if (!public_key.import_public_key(Slice(octet_string_).ubegin())) { + return Status::Error("Bad public key"); + } + if (public_key.check_message_signature(signature, data)) { + return Status::OK(); + } + return Status::Error("Wrong signature"); +} + +Result Ed25519::compute_shared_secret(const PublicKey &public_key, const PrivateKey &private_key) { + crypto::Ed25519::PrivateKey tmp_private_key; + if (!tmp_private_key.import_private_key(Slice(private_key.as_octet_string()).ubegin())) { + return Status::Error("Bad private key"); + } + crypto::Ed25519::PublicKey tmp_public_key; + if (!tmp_public_key.import_public_key(Slice(public_key.as_octet_string()).ubegin())) { + return Status::Error("Bad public key"); + } + SecureString shared_secret(32, '\0'); + if (!tmp_private_key.compute_shared_secret(shared_secret.as_mutable_slice(), tmp_public_key)) { + return Status::Error("Failed to compute shared secret"); + } + return std::move(shared_secret); +} + +#endif + +} // namespace td + +#endif diff --git a/crypto/Ed25519.h b/crypto/Ed25519.h new file mode 100644 index 00000000..21dcf7dd --- /dev/null +++ b/crypto/Ed25519.h @@ -0,0 +1,72 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +#include "td/utils/common.h" +#include "td/utils/SharedSlice.h" +#include "td/utils/Status.h" + +#if TD_HAVE_OPENSSL + +namespace td { + +class Ed25519 { + public: + class PublicKey { + public: + static constexpr size_t LENGTH = 32; + + explicit PublicKey(SecureString octet_string); + + SecureString as_octet_string() const; + + Status verify_signature(Slice data, Slice signature) const; + + private: + SecureString octet_string_; + }; + + class PrivateKey { + public: + static constexpr size_t LENGTH = 32; + + explicit PrivateKey(SecureString octet_string); + + SecureString as_octet_string() const; + + Result get_public_key() const; + + Result sign(Slice data) const; + + Result as_pem(Slice password) const; + + static Result from_pem(Slice pem, Slice password); + + private: + SecureString octet_string_; + }; + + static Result generate_private_key(); + + static Result compute_shared_secret(const PublicKey &public_key, const PrivateKey &private_key); +}; + +} // namespace td + +#endif diff --git a/crypto/block/Binlog.cpp b/crypto/block/Binlog.cpp new file mode 100644 index 00000000..ac1f1415 --- /dev/null +++ b/crypto/block/Binlog.cpp @@ -0,0 +1,493 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "crypto/block/Binlog.h" + +#include "td/utils/as.h" +#include "td/utils/misc.h" +#include "td/utils/port/path.h" + +#include + +namespace block { +/* + * + * GENERIC BINLOG (move to separate file) + * + */ + +BinlogBuffer::BinlogBuffer(std::unique_ptr cb, std::size_t _max_size, td::FileFd fd) + : cb(std::move(cb)) + , need_more_bytes(0) + , eptr(nullptr) + , log_rpos(0) + , log_cpos(0) + , log_wpos(0) + , fd(std::move(fd)) + , replica(false) + , writing(false) + , dirty(false) + , created(false) + , ok(false) { + max_size = _max_size; + start = static_cast(std::malloc(max_size)); + DCHECK(start); + rptr = wptr = cptr = start; + end = start + max_size; +} + +unsigned char* BinlogBuffer::alloc_log_event_force(std::size_t size) { + unsigned char* res = alloc_log_event(size); + if (!res) { + throw LevAllocError{size}; + } + return res; +} + +unsigned char* BinlogBuffer::try_alloc_log_event(std::size_t size) { + if (!eptr) { + if (end - wptr >= (long)size) { + unsigned char* res = wptr; + wptr += size; + log_wpos += size; + return res; + } + eptr = wptr; + wptr = start; + if (rptr == eptr) { + rptr = start; + } + if (cptr == eptr) { + cptr = start; + } + } + if (rptr - wptr > (long)size) { + unsigned char* res = wptr; + wptr += size; + log_wpos += size; + return res; + } + return nullptr; +} + +bool BinlogBuffer::flush(int mode) { + auto r_res = try_flush(mode); + if (r_res.is_ok()) { + return r_res.ok(); + } + std::string msg = PSTRING() << "cannot flush binlog file " << binlog_name << " at position " << log_rpos << " " + << r_res.error(); + LOG(ERROR) << msg; + throw BinlogError{msg}; +} + +td::Result BinlogBuffer::try_flush(int mode) { + LOG(DEBUG) << "in flush: writing=" << writing << " r=" << rptr - start << " c=" << cptr - start + << " w=" << wptr - start << "; rp=" << log_rpos << " cp=" << log_cpos << " wp=" << log_wpos; + if (!writing || rptr == cptr) { + return false; // nothing to flush + } + DCHECK(!fd.empty()); // must have an open binlog file + while (rptr != cptr) { + unsigned char* tptr = (cptr >= rptr ? cptr : eptr); + DCHECK(rptr <= tptr); + auto sz = tptr - rptr; + if (sz) { + LOG(INFO) << "writing " << sz << " bytes to binlog " << binlog_name << " at position " << log_rpos; + TRY_RESULT(res, fd.pwrite(td::Slice(rptr, sz), log_rpos)); + if (static_cast(res) != sz) { + return td::Status::Error(PSLICE() << "written " << res << " bytes instead of " << sz); + } + log_rpos += sz; + rptr += sz; + } + if (rptr == eptr) { + rptr = start; + eptr = nullptr; + } + } + if (mode >= 3) { + LOG(INFO) << "syncing binlog " << binlog_name << " (position " << log_rpos << ")"; + TRY_STATUS(fd.sync()); + } + return true; +} + +unsigned char* BinlogBuffer::alloc_log_event(std::size_t size) { + if (!writing) { + throw BinlogError{"cannot create new binlog event: binlog not open for writing"}; + } + if (size >= max_size || size > max_event_size) { + return nullptr; + } + size = (size + 3) & -4; + unsigned char* res = try_alloc_log_event(size); + if (!res) { + flush(); + return try_alloc_log_event(size); + } else { + return res; + } +} + +bool BinlogBuffer::commit_range(unsigned long long pos_start, unsigned long long pos_end) { + // TODO: make something more clever, with partially committed/uncommitted segments in [cpos..wpos] range + if (pos_start != log_cpos || pos_end < pos_start || pos_end > log_wpos) { + return false; + } + if (!pos_start && pos_end >= pos_start + 4 && td::as(cptr) != 0x0442446b) { + throw BinlogError{"incorrect magic"}; + } + long long size = pos_end - pos_start; + replay_range(cptr, pos_start, pos_end); + log_cpos = pos_end; + cptr += size; + if (eptr && cptr >= eptr) { + cptr -= eptr - start; + } + return true; +} + +bool BinlogBuffer::rollback_range(unsigned long long pos_start, unsigned long long pos_end) { + if (pos_start < log_cpos || pos_end < pos_start || pos_end != log_wpos) { + return false; + } + long long size = pos_end - pos_start; + log_wpos = pos_end; + if (size >= wptr - start) { + wptr -= size; + } else { + DCHECK(eptr); + wptr += eptr - start - size; + } + return true; +} + +void BinlogBuffer::NewBinlogEvent::commit() { + //LOG(DEBUG) << "in NewBinlogEvent::commit (status = " << status << ")"; + if (!(status & 4)) { + throw BinlogError{"cannot commit new binlog event: already committed or rolled back"}; + } + if (!bb.commit_range(pos, pos + size)) { + throw BinlogError{"cannot commit new binlog event: possibly some earlier log events are not committed yet"}; + } + status = 1; + //LOG(DEBUG) << "after NewBinlogEvent::commit (status = " << status << ")"; +} + +void BinlogBuffer::NewBinlogEvent::rollback() { + if (!(status & 4)) { + throw BinlogError{"cannot roll back new binlog event: already committed or rolled back"}; + } + if (!bb.rollback_range(pos, pos + size)) { + throw BinlogError{"cannot roll back new binlog event: possibly some later log event are already committed"}; + } + status = 2; +} + +BinlogBuffer::NewBinlogEvent::~NewBinlogEvent() { + if (status & 4) { + if (status == 5) { + status = 4; + commit(); + } else if (status == 6) { + status = 4; + rollback(); + } else { + LOG(ERROR) << "newly-allocated binlog event is neither committed nor rolled back (automatically rolling back)"; + rollback(); + } + } +} + +void BinlogBuffer::replay_range(unsigned char* ptr, unsigned long long pos_start, unsigned long long pos_end) { + unsigned char* tptr = (ptr <= wptr ? wptr : eptr); + long long avail = tptr - ptr; + while (pos_start < pos_end) { + if (ptr == eptr) { + ptr = start; + tptr = wptr; + avail = tptr - ptr; + if (avail > (long long)(pos_end - pos_start)) { + avail = pos_end - pos_start; + } + } + int res = (avail >= 4 ? cb->replay_log_event(*this, reinterpret_cast(ptr), + td::narrow_cast(avail), pos_start) + : -0x7ffffffc); + if (res <= 0 || res > avail) { + std::ostringstream ss; + ss << "cannot interpret newly-committed binlog event 0x" << std::hex + << (avail >= 4 ? (unsigned)td::as(ptr) : 0u) << std::dec << ": error " << res; + throw BinlogError{ss.str()}; + } + ptr += res; + pos_start += res; + avail -= res; + } +} + +int BinlogBuffer::replay_pending(bool allow_partial) { + if (rptr == cptr) { + return 0; + } + unsigned char* tptr = (rptr <= cptr ? cptr : eptr); + long long avail = tptr - rptr; + DCHECK(tptr && avail >= 0); + while (rptr != cptr) { + int res = (avail >= 4 ? cb->replay_log_event(*this, reinterpret_cast(rptr), + td::narrow_cast(avail), log_rpos) + : -0x7ffffffc); + if (res > 0) { + if (res > avail) { + throw BinlogError{"binlog event used more bytes than available"}; + } + avail -= res; + log_rpos += res; + rptr += res; + if (rptr != eptr) { + continue; + } + rptr = start; + tptr = cptr; + avail = tptr - rptr; + continue; + } + long long prev_need = 0; + while (res < -0x40000000) { + long long need = res - 0x80000000; + need = (need + 3) & -4; + if (need > (long long)max_event_size) { + throw BinlogError{"binlog event requires too many bytes"}; + } + if (need <= avail) { + throw BinlogError{"binlog event requires more bytes, but we already had them"}; + } + if (need <= prev_need) { + throw BinlogError{"binlog event requires more bytes, but we already had them"}; + } + prev_need = need; + long long total_avail = avail + (rptr > cptr ? cptr - start : 0); + if (need > total_avail) { + if (allow_partial) { + need_more_bytes = td::narrow_cast(need - total_avail); + return 2; + } else { + throw BinlogError{"binlog event extends past end of buffer"}; + } + } + if (need <= 1024) { + unsigned char tmp[1024]; + std::memcpy(tmp, rptr, td::narrow_cast(avail)); + std::memcpy(tmp + avail, start, td::narrow_cast(need - avail)); + res = cb->replay_log_event(*this, reinterpret_cast(tmp), td::narrow_cast(need), + log_rpos); + } else { + unsigned char* tmp = static_cast(std::malloc(td::narrow_cast(need))); + std::memcpy(tmp, rptr, td::narrow_cast(avail)); + std::memcpy(tmp + avail, start, td::narrow_cast(need - avail)); + res = cb->replay_log_event(*this, reinterpret_cast(tmp), td::narrow_cast(need), + log_rpos); + std::free(tmp); + } + if (res > need) { + throw BinlogError{"binlog event used more bytes than available"}; + } + } + if (res < 0) { + return res; + } + if (!res) { + throw BinlogError{"unknown error while interpreting binlog event"}; + } + if (res < avail) { + avail -= res; + log_rpos += res; + rptr += res; + continue; + } + DCHECK(eptr); + log_rpos += res; + rptr += res; + rptr = start + (rptr - eptr); + eptr = nullptr; + DCHECK(start <= rptr && rptr <= cptr && cptr <= wptr && wptr <= end); + } + return 1; +} + +BinlogBuffer::~BinlogBuffer() { + if (start) { + if (writing) { + flush(2); + } + std::free(start); + } +} + +td::Status BinlogBuffer::set_binlog(std::string new_binlog_name, int mode) { + if (!binlog_name.empty() || !fd.empty()) { + return td::Status::Error("binlog buffer already attached to a file"); + } + td::int32 flags = td::FileFd::Read; + if ((mode & 1) != 0) { + flags |= td::FileFd::Write; + } + auto r_fd = td::FileFd::open(new_binlog_name, flags, 0640); + if (r_fd.is_error()) { + if (!(~mode & 3)) { + TRY_RESULT(new_fd, td::FileFd::open(new_binlog_name, flags | td::FileFd::CreateNew, 0640)); + fd = std::move(new_fd); + created = true; + } else { + return r_fd.move_as_error(); + } + } else { + fd = r_fd.move_as_ok(); + } + replica = !(mode & 1); + if (!replica) { + TRY_STATUS(fd.lock(td::FileFd::LockFlags::Write, new_binlog_name, 100)); + } + if (created) { + writing = true; + td::Status res; + try { + res = cb->init_new_binlog(*this); + } catch (BinlogBuffer::BinlogError& err) { + res = td::Status::Error(err.msg); + } + if (res.is_error()) { + fd.close(); + td::unlink(new_binlog_name).ignore(); + writing = false; + return res; + } + binlog_name = new_binlog_name; + ok = true; + return td::Status::OK(); + } + binlog_name = new_binlog_name; + auto res = replay_binlog(replica); + if (res.is_error()) { + return res.move_as_error(); + } + if (!replica) { + if (log_rpos != log_wpos || log_rpos != log_cpos || rptr != wptr || rptr != cptr) { + std::string msg = (PSLICE() << "error while interpreting binlog `" << binlog_name << "`: " << log_wpos - log_rpos + << " bytes left uninterpreted at position " << log_rpos << ", truncated binlog?") + .c_str(); + LOG(ERROR) << msg; + return td::Status::Error(msg); + } + //rptr = wptr = cptr = start; + //eptr = nullptr; + LOG(INFO) << "read and interpreted " << res.move_as_ok() << " bytes from binlog `" << binlog_name + << "`, final position " << log_rpos << ", reopening in write mode"; + writing = true; + if (!log_rpos) { + td::Status status; + try { + status = cb->init_new_binlog(*this); + } catch (BinlogBuffer::BinlogError& err) { + status = td::Status::Error(err.msg); + } + if (status.is_error()) { + fd.close(); + td::unlink(new_binlog_name).ignore(); + writing = false; + return status; + } + } + } + ok = true; + return td::Status::OK(); +} + +td::Result BinlogBuffer::replay_binlog(bool allow_partial) { + if (writing) { + return 0; + } + long long total = 0; + while (true) { + auto res = read_file(); + if (res.is_error()) { + return res.move_as_error(); + } + long long sz = res.move_as_ok(); + total += sz; + try { + cptr = wptr; + log_cpos = log_wpos; + if (!log_rpos && rptr == start && wptr >= rptr + 4 && td::as(rptr) != 0x0442446b) { + throw BinlogError{"incorrect magic"}; + } + int r = replay_pending(allow_partial || sz != 0); + if (r < 0 && r >= -0x40000000) { + throw InterpretError{(PSLICE() << "binlog error " << r).c_str()}; + } + } catch (BinlogError err) { + LOG(ERROR) << "error reading binlog " << binlog_name << ": " << err.msg << " at position " << log_rpos; + return td::Status::Error(PSLICE() << "error reading binlog " << binlog_name << ": " << err.msg << " at position " + << log_rpos); + } catch (InterpretError err) { + LOG(ERROR) << "error interpreting binlog " << binlog_name << ": " << err.msg << " at position " << log_rpos; + return td::Status::Error(PSLICE() << "error interpreting binlog " << binlog_name << ": " << err.msg + << " at position " << log_rpos); + } + if (!sz) { + break; + } + }; + return total; +} + +td::Result BinlogBuffer::read_file() { + unsigned char* ptr = wptr; + std::size_t sz = end - wptr; + if (rptr > wptr) { + DCHECK(eptr); + sz = rptr - wptr; + if (sz <= 4) { + return 0; // buffer full + } + sz -= 4; + } else if (!sz) { + DCHECK(!eptr); + if (rptr <= start + 4) { + return 0; // buffer full + } + eptr = end; + ptr = wptr = start; + sz = rptr - start - 4; + } + auto r_res = fd.pread(td::MutableSlice(ptr, sz), log_wpos); + if (r_res.is_error()) { + std::string msg = PSTRING() << "error reading binlog file `" << binlog_name << "` at position " << log_wpos << " : " + << r_res.error(); + LOG(ERROR) << msg; + return td::Status::Error(msg); + } + auto res = r_res.move_as_ok(); + DCHECK(std::size_t(res) <= sz); + LOG(INFO) << "read " << res << " bytes from binlog `" << binlog_name << "` at position " << log_wpos; + log_wpos += res; + wptr += res; + return (int)res; +} +} // namespace block diff --git a/crypto/block/Binlog.h b/crypto/block/Binlog.h new file mode 100644 index 00000000..f5b66060 --- /dev/null +++ b/crypto/block/Binlog.h @@ -0,0 +1,157 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +#include "td/utils/Status.h" +#include "td/utils/port/FileFd.h" + +namespace block { +/* + * + * BINLOG (BUFFERS) -- move to separate file + * + */ + +class BinlogBuffer; + +class BinlogCallback { + public: + virtual ~BinlogCallback() = default; + virtual td::Status init_new_binlog(BinlogBuffer& bb) = 0; + virtual int replay_log_event(BinlogBuffer& bb, const unsigned* ptr, std::size_t len, unsigned long long pos) = 0; +}; + +class BinlogBuffer { + static constexpr std::size_t max_event_size = 0xfffc; + std::unique_ptr cb; + std::size_t max_size; + std::size_t need_more_bytes; + unsigned char *start, *rptr, *cptr, *wptr, *eptr, *end; + unsigned long long log_rpos, log_cpos, log_wpos; + std::string binlog_name; + td::FileFd fd; + bool replica; + bool writing; + bool dirty; + bool created; + bool ok; + td::Result read_file(); + td::Result replay_binlog(bool allow_partial); + unsigned char* try_alloc_log_event(std::size_t size); + int replay_pending(bool allow_partial = false); + void replay_range(unsigned char* ptr, unsigned long long pos_start, unsigned long long pos_end); + td::Status set_fd(td::FileFd fd); + + public: + struct LevAllocError { + std::size_t size; + explicit LevAllocError(std::size_t _size) : size(_size) { + } + }; + struct InterpretError { + std::string msg; + explicit InterpretError(std::string _msg) : msg(_msg) { + } + }; + struct BinlogError { + std::string msg; + explicit BinlogError(std::string _msg) : msg(_msg) { + } + }; + BinlogBuffer(std::unique_ptr _cb, std::size_t _max_size = (1 << 24), td::FileFd fd = {}); + BinlogBuffer(const BinlogBuffer&) = delete; + BinlogBuffer& operator=(const BinlogBuffer&) = delete; + BinlogBuffer(BinlogBuffer&&) = delete; + BinlogBuffer& operator=(BinlogBuffer&&) = delete; + ~BinlogBuffer(); + td::Status set_binlog(std::string _binlog_name, int mode = 0); + unsigned char* alloc_log_event(std::size_t size); + unsigned char* alloc_log_event_force(std::size_t size); + bool flush(int mode = 0); + td::Result try_flush(int mode); + unsigned long long get_rpos() const { + return log_rpos; + } + // + class NewBinlogEvent { + protected: + BinlogBuffer& bb; + unsigned long long pos; + unsigned size; + int status; + + public: + NewBinlogEvent(BinlogBuffer& _bb, unsigned long long _pos, unsigned _size) + : bb(_bb), pos(_pos), size(_size), status(4) { + } + ~NewBinlogEvent(); + unsigned long long get_log_pos() const { + return pos; + } + void commit(); + void commit_later() { + if (status & 4) { + status = 5; + } + } + void rollback(); + void rollback_later() { + if (status & 4) { + status = 6; + } + }; + }; + template + class NewEvent : public NewBinlogEvent { + T* ptr; + + protected: + friend class BinlogBuffer; + NewEvent(BinlogBuffer& _bb, unsigned long long _pos, unsigned _size, T* _ptr) + : NewBinlogEvent(_bb, _pos, _size), ptr(_ptr) { + } + + public: + T* operator->() const { + return ptr; + } + T& operator*() const { + return *ptr; + } + void commit() { + NewBinlogEvent::commit(); + ptr = nullptr; + } + void rollback() { + NewBinlogEvent::rollback(); + ptr = nullptr; + } + }; + template + NewEvent alloc(Args... args) { + unsigned long long pos = log_wpos; + return NewEvent(*this, pos, sizeof(T), new (alloc_log_event_force(sizeof(T))) T(args...)); + } + + protected: + friend class NewBinlogEvent; + bool commit_range(unsigned long long pos_start, unsigned long long pos_end); + bool rollback_range(unsigned long long pos_start, unsigned long long pos_end); +}; +} // namespace block diff --git a/crypto/block/block-binlog.h b/crypto/block/block-binlog.h new file mode 100644 index 00000000..48346e77 --- /dev/null +++ b/crypto/block/block-binlog.h @@ -0,0 +1,111 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once +#include "td/utils/int_types.h" +#include "ton/ton-types.h" + +#include + +namespace block { + +namespace log { + +#pragma pack(push, 4) + +struct Start { + static constexpr unsigned tag = 0x0442446b; + static constexpr unsigned log_type = 0x290100; + unsigned tag_field; + unsigned type_field; + unsigned created_at; + unsigned char zerostate_root_hash[32]; + Start(const RootHash& hash, unsigned _now = 0) + : tag_field(tag), type_field(log_type), created_at(_now ? _now : (unsigned)std::time(nullptr)) { + td::as(zerostate_root_hash) = hash; + } +}; + +struct SetZeroState { + static constexpr unsigned tag = 0x63ab3cd9; + unsigned tag_field; + unsigned flags; + long long file_size; + unsigned char file_hash[32]; + unsigned char root_hash[32]; + SetZeroState(const RootHash& rhash, const FileHash& fhash, unsigned long long _fsize, unsigned _flags = 0) + : tag_field(tag), flags(_flags), file_size(_fsize) { + td::as(file_hash) = fhash; + td::as(root_hash) = rhash; + } +}; + +struct NewBlock { + static constexpr unsigned tag = 0x19f4bc63; + unsigned tag_field; + unsigned flags; // lower 8 bits = authority + int workchain; + unsigned seqno; + unsigned long long shard; + long long file_size; + unsigned char file_hash[32]; + unsigned char root_hash[32]; + unsigned char last_bytes[8]; + NewBlock(const ton::BlockId& block, const RootHash& rhash, const FileHash& fhash, unsigned long long _fsize, + unsigned _flags) + : tag_field(tag) + , flags(_flags) + , workchain(block.workchain) + , seqno(block.seqno) + , shard(block.shard) + , file_size(_fsize) { + td::as(file_hash) = fhash; + td::as(root_hash) = rhash; + td::as(last_bytes) = 0; + } +}; + +struct NewState { + static constexpr unsigned tag = 0x4190a21f; + unsigned tag_field; + unsigned flags; // lower 8 bits = authority + int workchain; + unsigned seqno; + unsigned long long shard; + long long file_size; + unsigned char file_hash[32]; + unsigned char root_hash[32]; + unsigned char last_bytes[8]; + NewState(const ton::BlockId& state, const RootHash& rhash, const FileHash& fhash, unsigned long long _fsize, + unsigned _flags) + : tag_field(tag) + , flags(_flags) + , workchain(state.workchain) + , seqno(state.seqno) + , shard(state.shard) + , file_size(_fsize) { + td::as(file_hash) = fhash; + td::as(root_hash) = rhash; + td::as(last_bytes) = 0; + } +}; + +#pragma pack(pop) + +} // namespace log +} // namespace block diff --git a/crypto/block/block-db-impl.h b/crypto/block/block-db-impl.h new file mode 100644 index 00000000..868206d8 --- /dev/null +++ b/crypto/block/block-db-impl.h @@ -0,0 +1,135 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once +#include +#include +#include "vm/cells.h" +#include "block/Binlog.h" +#include "block/block-db.h" +#include "block/block-binlog.h" + +namespace block { +using td::Ref; + +/* + * + * BLOCK DATABASE + * + */ + +class BlockDbImpl; + +class BlockBinlogCallback : public BinlogCallback { + BlockDbImpl& db; + td::Status init_new_binlog(BinlogBuffer& bb) override; + int replay_log_event(BinlogBuffer& bb, const unsigned* ptr, std::size_t len, unsigned long long log_pos) override; + int replay(const block::log::Start& lev, unsigned long long log_pos) const; + int replay(const block::log::SetZeroState& lev, unsigned long long log_pos) const; + int replay(const block::log::NewBlock& lev, unsigned long long log_pos) const; + int replay(const block::log::NewState& lev, unsigned long long log_pos) const; + template + inline int try_interpret(const unsigned* ptr, std::size_t len, unsigned long long log_pos); + + public: + BlockBinlogCallback(BlockDbImpl& _db) : db(_db) { + } +}; + +template +inline int BlockBinlogCallback::try_interpret(const unsigned* ptr, std::size_t len, unsigned long long log_pos) { + if (len < sizeof(T)) { + return 0x80000000 + sizeof(T); + } else { + int res = replay(*reinterpret_cast(ptr), log_pos); + return res >= 0 ? sizeof(T) : res; + } +} + +class BlockDbImpl final : public BlockDb { + int status; + bool allow_uninit; + bool created; + int depth; + std::unique_ptr zstate; + std::string base_dir; + std::string binlog_name; + BinlogBuffer bb; + ton::Bits256 zstate_rhash, zstate_fhash; + unsigned created_at; + std::map file_cache; + std::map> block_info; + std::map> state_info; + // + td::Result do_init(); + + public: + enum FMode { + chk_none = 0, + chk_if_exists = 1, + fail_if_exists = 2, + overwrite = 4, + chk_size_only = 16, + chk_file_hash = 32 + }; + static constexpr const char* default_binlog_name = "blockdb"; + static constexpr const char* default_binlog_suffix = ".bin"; + static constexpr int default_depth = 4; + BlockDbImpl(td::Result& _res, std::string _base_dir, std::unique_ptr _zstate = nullptr, + bool _allow_uninit = false, int _depth = 4, std::string _binlog_name = ""); + ~BlockDbImpl(); + bool ok() const { + return status >= 0; + } + bool initialized() const { + return status != 0; + } + bool init_ok() const { + return status > 0; + } + + protected: + friend class BlockBinlogCallback; + td::Ref zerostate; + td::Status init_from_zstate(); + td::Status update_block_info(Ref blk_info); + td::Status update_state_info(Ref state); + + private: + std::string compute_db_filename(const FileHash& file_hash) const; + std::string compute_db_tmp_filename(const FileHash& file_hash, int i, bool makedirs) const; + td::Status save_db_file(const FileHash& file_hash, const td::BufferSlice& data, int fmode = 0); + td::Status load_data(FileInfo& file_info, bool force = false); + // actor BlockDb implementation + void get_top_block_id(ton::ShardIdFull shard, int authority, td::Promise promise) override; + void get_top_block_state_id(ton::ShardIdFull shard, int authority, td::Promise promise) override; + void get_block_by_id(ton::BlockId blk_id, bool need_data, td::Promise> promise) override; + void get_state_by_id(ton::BlockId blk_id, bool need_data, td::Promise> promise) override; + void get_out_queue_info_by_id(ton::BlockId blk_id, td::Promise> promise) override; + void get_object_by_file_hash(FileHash file_hash, bool need_data, bool force_file_load, + td::Promise> promise) override; + void get_object_by_root_hash(RootHash root_hash, bool need_data, bool force_file_load, + td::Promise> promise) override; + void save_new_block(ton::BlockIdExt blk_id, td::BufferSlice data, int authority, + td::Promise promise) override; + void save_new_state(ton::BlockIdExt state_id, td::BufferSlice data, int authority, + td::Promise promise) override; + bool file_cache_insert(const FileHash& file_hash, const td::BufferSlice& data, int mode = 0); +}; + +} // namespace block diff --git a/crypto/block/block-db.cpp b/crypto/block/block-db.cpp new file mode 100644 index 00000000..f2018b80 --- /dev/null +++ b/crypto/block/block-db.cpp @@ -0,0 +1,835 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "block-db.h" +#include "block-db-impl.h" +#include "block-binlog.h" +#include "td/utils/common.h" +#include "td/utils/crypto.h" +#include "td/utils/format.h" +#include "td/utils/misc.h" +#include "td/utils/port/FileFd.h" +#include "td/utils/port/path.h" +#include "td/utils/filesystem.h" +#include "vm/cellslice.h" +#include "vm/boc.h" +#include "vm/db/StaticBagOfCellsDb.h" + +#include + +namespace block { + +//static constexpr std::string default_binlog_name = "blockdb"; +//static constexpr std::string default_binlog_suffix = ".bin"; + +bool parse_hash_string(std::string arg, RootHash& res) { + if (arg.size() != 64) { + res.set_zero(); + return false; + } + int f = 1; + unsigned char* ptr = res.data(); + for (char c : arg) { + f <<= 4; + if (c >= '0' && c <= '9') { + f += c - '0'; + } else { + c |= 0x20; + if (c >= 'a' && c <= 'f') { + f += c - ('a' - 10); + } else { + res.set_zero(); + return false; + } + } + if (f >= 0x100) { + *ptr++ = (unsigned char)f; + f = 1; + } + } + return true; +} + +td::Result load_binary_file(std::string filename, td::int64 max_size) { + //TODO: use td::read_file + auto res = [&]() -> td::Result { + TRY_RESULT(fd, td::FileFd::open(filename, td::FileFd::Read)); + TRY_RESULT(stat, fd.stat()); + if (!stat.is_reg_) { + return td::Status::Error("file is not regular"); + } + td::int64 size = stat.size_; + if (!size) { + return td::Status::Error("file is empty"); + } + if ((max_size && size > max_size) || static_cast(size) > std::numeric_limits::max()) { + return td::Status::Error("file is too long"); + } + td::BufferSlice res(td::narrow_cast(size)); + TRY_RESULT(r, fd.read(res.as_slice())); + if (r != static_cast(size)) { + return td::Status::Error(PSLICE() << "read " << r << " bytes out of " << size); + } + return std::move(res); + }(); + LOG_IF(ERROR, res.is_error()) << "error reading file `" << filename << "` : " << res.error(); + return res; +} + +td::Status save_binary_file(std::string filename, const td::BufferSlice& data, unsigned long long max_size) { + //TODO: use td::write_file + auto status = [&]() { + if (max_size && data.size() > max_size) { + return td::Status::Error("contents too long"); + } + auto size = data.size(); + TRY_RESULT(to_file, td::FileFd::open(filename, td::FileFd::CreateNew | td::FileFd::Write)); + TRY_RESULT(written, to_file.write(data)); + if (written != static_cast(size)) { + return td::Status::Error(PSLICE() << "written " << written << " bytes instead of " << size); + } + to_file.close(); + return td::Status::OK(); + }(); + LOG_IF(ERROR, status.is_error()) << "error writing new file `" << filename << "` : " << status; + return status; +} + +FileHash compute_file_hash(const td::BufferSlice& data) { + ton::Bits256 data_hash; + td::sha256(data, td::MutableSlice{data_hash.data(), 32}); + return data_hash; +} + +FileHash compute_file_hash(td::Slice data) { + ton::Bits256 data_hash; + td::sha256(data, td::MutableSlice{data_hash.data(), 32}); + return data_hash; +} + +/* + * + * ZEROSTATE CONFIGURATION + * + */ + +td::Status ZerostateInfo::base_check() { + if (!has_data()) { + return td::Status::OK(); + } + auto data_hash = compute_file_hash(data); + if (!has_file_hash()) { + file_hash = data_hash; + } else if (file_hash != data_hash) { + return td::Status::Error("zerostate file hash mismatch"); + } + vm::BagOfCells boc; + auto res = boc.deserialize(data); + if (!res.is_ok() || boc.get_root_count() != 1) { + return td::Status::Error("zerostate is not a valid bag of cells"); // not a valid bag-of-Cells + } + data_hash = boc.get_root_cell()->get_hash().bits(); + if (!has_root_hash()) { + root_hash = data_hash; + } else if (root_hash != data_hash) { + return td::Status::Error("zerostate root hash mismatch"); + } + return td::Status::OK(); +} + +/* + * + * BLOCK DATABASE + * + */ + +std::string compute_db_filename(std::string base_dir, const FileHash& file_hash, int depth) { + static const char hex_digits[] = "0123456789ABCDEF"; + assert(depth >= 0 && depth <= 8); + std::string res = std::move(base_dir); + res.reserve(res.size() + 32 + depth * 3 + 4); + for (int i = 0; i < depth; i++) { + unsigned u = file_hash.data()[i]; + res.push_back(hex_digits[u >> 4]); + res.push_back(hex_digits[u & 15]); + res.push_back('/'); + } + for (int i = 0; i < 32; i++) { + unsigned u = file_hash.data()[i]; + res.push_back(hex_digits[u >> 4]); + res.push_back(hex_digits[u & 15]); + } + res += ".boc"; + return res; +} + +std::string BlockDbImpl::compute_db_filename(const FileHash& file_hash) const { + return block::compute_db_filename(base_dir, file_hash, depth); +} + +std::string compute_db_tmp_filename(std::string base_dir, const FileHash& file_hash, int i, bool makedirs, int depth) { + static const char hex_digits[] = "0123456789ABCDEF"; + assert(depth >= 0 && depth <= 8); + std::string res = std::move(base_dir); + res.reserve(res.size() + 32 + depth * 3 + 4); + for (int j = 0; j < depth; j++) { + unsigned u = file_hash.data()[j]; + res.push_back(hex_digits[u >> 4]); + res.push_back(hex_digits[u & 15]); + res.push_back('/'); + if (makedirs) { + td::mkdir(res, 0755).ignore(); + } + } + for (int j = 0; j < 32; j++) { + unsigned u = file_hash.data()[j]; + res.push_back(hex_digits[u >> 4]); + res.push_back(hex_digits[u & 15]); + } + res += ".tmp"; + if (i > 0) { + if (i < 10) { + res.push_back((char)('0' + i)); + } else { + res.push_back((char)('0' + i / 10)); + res.push_back((char)('0' + i % 10)); + } + } + return res; +} + +std::string BlockDbImpl::compute_db_tmp_filename(const FileHash& file_hash, int i, bool makedirs) const { + return block::compute_db_tmp_filename(base_dir, file_hash, i, makedirs, depth); +} + +bool BlockDbImpl::file_cache_insert(const FileHash& file_hash, const td::BufferSlice& data, int mode) { + auto it = file_cache.find(file_hash); + if (it != file_cache.end()) { + // found + return true; + } + auto res = file_cache.emplace(file_hash, data.clone()); + return res.second; +} + +td::Status BlockDbImpl::save_db_file(const FileHash& file_hash, const td::BufferSlice& data, int fmode) { + if (fmode & FMode::chk_file_hash && file_hash != compute_file_hash(data)) { + return td::Status::Error("file hash passed for creation of a new file does not match contents"); + } + std::string filename = compute_db_filename(file_hash); + bool overwrite = false; + auto r_stat = td::stat(filename); + if (r_stat.is_ok()) { + auto stat = r_stat.move_as_ok(); + // file exists + if (fmode & FMode::fail_if_exists) { + return td::Status::Error(PSLICE() << "file " << filename << " cannot be created, it already exists"); + } + if (!(fmode & (FMode::chk_if_exists | FMode::overwrite))) { + file_cache_insert(file_hash, data); + return td::Status::OK(); + } + if (fmode & FMode::chk_if_exists) { + if (stat.size_ != (long long)data.size()) { + LOG(ERROR) << "file " << filename << " already exists with wrong content"; + if (!(fmode & FMode::overwrite)) { + return td::Status::Error(PSLICE() << "file " << filename << " already exists with wrong content"); + } + } else if (fmode & FMode::chk_size_only) { + file_cache_insert(file_hash, data); + return td::Status::OK(); + } else { + auto res = load_binary_file(filename); + if (res.is_error()) { + return res.move_as_error(); + } + auto old_contents = res.move_as_ok(); + if (old_contents.size() != data.size() || old_contents.as_slice() != data.as_slice()) { + LOG(ERROR) << "file " << filename << " already exists with wrong content"; + if (!(fmode & FMode::overwrite)) { + return td::Status::Error(PSLICE() << "file " << filename << " already exists with wrong content"); + } + } else { + file_cache_insert(file_hash, data); + return td::Status::OK(); + } + } + } + overwrite = true; + } + std::string tmp_filename; + for (int i = 0; i < 10; i++) { + tmp_filename = compute_db_tmp_filename(file_hash, i, true); + auto res = save_binary_file(tmp_filename, data); + if (res.is_ok()) { + break; + } + if (i == 9) { + return res; + } + } + auto rename_status = td::rename(tmp_filename, filename); + if (rename_status.is_error()) { + td::unlink(tmp_filename).ignore(); + LOG(ERROR) << rename_status; + return rename_status; + } + if (overwrite) { + LOG(DEBUG) << "database file `" << filename << "` overwritten, " << data.size() << " bytes"; + } else { + LOG(DEBUG) << "new database file `" << filename << "` created, " << data.size() << " bytes"; + } + file_cache_insert(file_hash, data); + return td::Status::OK(); +} + +td::Result> BlockDb::create_block_db(std::string base_dir, + std::unique_ptr zstate, + bool allow_uninit, int depth, + std::string binlog_name) { + using td::actor::ActorId; + using td::actor::ActorOwn; + td::Result res; + ActorOwn actor = + td::actor::create_actor(td::actor::ActorOptions().with_name("BlockDB"), res, base_dir, + std::move(zstate), allow_uninit, depth, binlog_name); + if (res.is_error()) { + return std::move(res).move_as_error(); + } else { + return std::move(actor); + } +} + +BlockDbImpl::BlockDbImpl(td::Result& _res, std::string _base_dir, std::unique_ptr _zstate, + bool _allow_uninit, int _depth, std::string _binlog_name) + : status(0) + , allow_uninit(_allow_uninit) + , created(false) + , depth(_depth) + , zstate(std::move(_zstate)) + , base_dir(_base_dir) + , binlog_name(_binlog_name) + , bb(std::unique_ptr(new BlockBinlogCallback(*this))) + , created_at(0) { + auto res = do_init(); + status = (res.is_ok() && res.ok() > 0 ? res.ok() : -1); + if (res.is_error()) { + _res = std::move(res); + } else { + _res = res.move_as_ok(); + } +} + +td::Result BlockDbImpl::do_init() { + if (base_dir.empty()) { + return td::Status::Error("block database cannot have empty base directory"); + } + if (depth < 0 || depth >= 8) { + return td::Status::Error("block database directory tree depth must be in range 0..8"); + } + if (base_dir.back() != '/') { + base_dir.push_back('/'); + } + if (binlog_name.empty()) { + binlog_name = default_binlog_name; + } + bool f = true; + for (char c : binlog_name) { + if (c == '.') { + f = false; + } else if (c == '/') { + f = true; + } + } + if (f) { + binlog_name += default_binlog_suffix; + } + if (binlog_name.at(0) != '/') { + binlog_name = base_dir + binlog_name; + } + if (zstate) { + if (!zstate->has_data() && zstate->has_filename()) { + auto data = load_binary_file(zstate->filename, 1 << 20); + if (data.is_error()) { + return data.move_as_error(); + } + zstate->data = data.move_as_ok(); + } + auto res = zstate->base_check(); + if (res.is_error()) { + return res; + } + } + try { + auto res = bb.set_binlog(binlog_name, allow_uninit ? 3 : 1); + if (res.is_error()) { + return res; + } + } catch (BinlogBuffer::BinlogError& err) { + return td::Status::Error(-2, std::string{"error while initializing block database binlog: "} + err.msg); + } catch (BinlogBuffer::InterpretError& err) { + return td::Status::Error(-3, std::string{"error while interpreting block database binlog: "} + err.msg); + } + return created; +} + +BlockDbImpl::~BlockDbImpl() { +} + +td::Status BlockDbImpl::init_from_zstate() { + if (!zstate) { + return td::Status::Error("no zero state provided, cannot initialize from scratch"); + } + if (!zstate->has_data()) { + if (zstate->has_filename() || zstate->has_file_hash()) { + if (!zstate->has_filename()) { + zstate->filename = compute_db_filename(zstate->file_hash); + } + auto res = load_binary_file(zstate->filename, 1 << 20); + if (res.is_error()) { + return res.move_as_error(); + } + zstate->data = res.move_as_ok(); + } else { + return td::Status::Error("cannot load zero state for block DB creation"); + } + } + auto res = zstate->base_check(); + if (res.is_error()) { + return res; + } + assert(zstate->has_file_hash() && zstate->has_root_hash()); + res = save_db_file(zstate->file_hash, zstate->data, FMode::chk_if_exists | FMode::chk_file_hash); + if (res.is_error()) { + return res; + } + return res; +} + +td::Status BlockBinlogCallback::init_new_binlog(BinlogBuffer& bb) { + auto res = db.init_from_zstate(); + if (res.is_error()) { + return res; + } + auto lev = bb.alloc(db.zstate->root_hash); + assert(!lev.get_log_pos()); + auto lev2 = bb.alloc(db.zstate->root_hash, db.zstate->file_hash, db.zstate->data.size()); + lev.commit(); + lev2.commit(); // TODO: introduce multi-commit bb.commit(lev, lev2) + bb.flush(3); + db.created = true; + return td::Status::OK(); +} + +#define REPLAY_CASE(__T) \ + case __T::tag: \ + return try_interpret<__T>(ptr, len, log_pos); + +int BlockBinlogCallback::replay_log_event(BinlogBuffer& bb, const unsigned* ptr, std::size_t len, + unsigned long long log_pos) { + assert(len >= 4); + LOG(DEBUG) << "replay_log_event(" << len << ", " << log_pos << ", " << *ptr << ")"; + switch (*ptr) { + REPLAY_CASE(log::Start); + REPLAY_CASE(log::SetZeroState); + REPLAY_CASE(log::NewBlock); + REPLAY_CASE(log::NewState); + } + std::ostringstream ss; + ss << "unknown binlog event 0x" << std::hex << *ptr << std::dec; + LOG(ERROR) << ss.str() << " at position " << log_pos; + throw BinlogBuffer::InterpretError{ss.str()}; +} + +#undef REPLAY_CASE + +int BlockBinlogCallback::replay(const log::Start& lev, unsigned long long log_pos) const { + LOG(DEBUG) << "in replay(Start{" << lev.tag_field << ", " << lev.type_field << ", " << lev.created_at << "})"; + if (lev.type_field != lev.log_type) { + throw BinlogBuffer::InterpretError{(PSLICE() << "unsupported binlog type " << lev.type_field).str()}; + } + if (log_pos) { + throw BinlogBuffer::InterpretError{"LEV_START can only be the very first record in a binlog"}; + } + db.zstate_rhash = lev.zerostate_root_hash; + db.created_at = lev.created_at; + if (db.zstate) { + if (!db.zstate->has_root_hash()) { + db.zstate->root_hash = db.zstate_rhash; + } else if (db.zstate->root_hash != db.zstate_rhash) { + throw BinlogBuffer::InterpretError{PSTRING() << "zerostate hash mismatch: in binlog " << db.zstate_rhash.to_hex() + << ", required " << db.zstate->root_hash.to_hex()}; + } + } + return 0; // ok +} + +int BlockBinlogCallback::replay(const log::SetZeroState& lev, unsigned long long log_pos) const { + LOG(DEBUG) << "in replay(SetZeroState)"; + // LOG(DEBUG) << "db.zstate_rhash = " << db.zstate_rhash.to_hex(); + if (db.zstate_rhash != td::ConstBitPtr{lev.root_hash}) { + throw BinlogBuffer::InterpretError{std::string{"SetZeroState: zerostate root hash mismatch: in binlog "} + + ton::Bits256{lev.root_hash}.to_hex() + ", required " + db.zstate_rhash.to_hex()}; + } + db.zerostate = td::Ref{true, + FileType::state, + ton::BlockId{ton::masterchainId, 1ULL << 63, 0}, + 0, + td::as(lev.file_hash), + td::as(lev.root_hash), + lev.file_size}; + return 0; // ok +} + +int BlockBinlogCallback::replay(const log::NewBlock& lev, unsigned long long log_pos) const { + LOG(DEBUG) << "in replay(NewBlock)"; + if (!lev.seqno || lev.workchain == ton::workchainInvalid) { + return -1; + } + ton::BlockId blkid{lev.workchain, lev.shard, lev.seqno}; + auto blk_info = td::Ref{true, + FileType::block, + blkid, + lev.flags & 0xff, + td::as(lev.file_hash), + td::as(lev.root_hash), + lev.file_size}; + auto res = db.update_block_info(blk_info); + if (res.is_error()) { + LOG(ERROR) << "cannot update block information in the local DB: " << res.to_string(); + return -1; + } else { + return 0; // ok + } +} + +int BlockBinlogCallback::replay(const log::NewState& lev, unsigned long long log_pos) const { + LOG(DEBUG) << "in replay(NewState)"; + if (!lev.seqno || lev.workchain == ton::workchainInvalid) { + return -1; + } + ton::BlockId id{lev.workchain, lev.shard, lev.seqno}; + auto state_info = td::Ref{true, + FileType::state, + id, + lev.flags & 0xff, + td::as(lev.file_hash), + td::as(lev.root_hash), + lev.file_size}; + auto res = db.update_state_info(state_info); + if (res.is_error()) { + LOG(ERROR) << "cannot update shardchain state information in the local DB: " << res.to_string(); + return -1; + } else { + return 0; // ok + } +} + +td::Status BlockDbImpl::update_block_info(Ref blk_info) { + auto it = block_info.find(blk_info->blk.id); + if (it != block_info.end()) { + // already exists + if (it->second->blk.file_hash != blk_info->blk.file_hash || it->second->blk.root_hash != blk_info->blk.root_hash) { + return td::Status::Error(-666, std::string{"fatal error in block DB: block "} + blk_info->blk.id.to_str() + + " has two records with different file or root hashes"); + } else { + return td::Status::OK(); + } + } else { + auto id = blk_info->blk.id; + auto res = block_info.emplace(std::move(id), std::move(blk_info)); + if (res.second) { + return td::Status::OK(); + } else { + return td::Status::Error(-666, "cannot insert block information into DB"); + } + } +} + +td::Status BlockDbImpl::update_state_info(Ref state) { + auto it = state_info.find(state->blk.id); + if (it != state_info.end()) { + // already exists + if (it->second->blk.root_hash != state->blk.root_hash) { + return td::Status::Error(-666, std::string{"fatal error in block DB: state for block "} + state->blk.id.to_str() + + " has two records with different root hashes"); + } else { + return td::Status::OK(); + } + } else { + auto id = state->blk.id; + auto res = state_info.emplace(std::move(id), std::move(state)); + if (res.second) { + return td::Status::OK(); + } else { + return td::Status::Error(-666, "cannot insert state information into DB"); + } + } +} + +void BlockDbImpl::get_top_block_id(ton::ShardIdFull shard, int authority, td::Promise promise) { + LOG(DEBUG) << "in BlockDb::get_top_block_id()"; + auto it = block_info.upper_bound(ton::BlockId{shard, std::numeric_limits::max()}); + if (it != block_info.begin() && ton::ShardIdFull{(--it)->first} == shard) { + promise(it->second->blk); + return; + } + if (shard.is_masterchain()) { + promise(ton::BlockIdExt{ton::BlockId{ton::masterchainId, 1ULL << 63, 0}}); + return; + } + promise(td::Status::Error(-666)); +} + +void BlockDbImpl::get_top_block_state_id(ton::ShardIdFull shard, int authority, td::Promise promise) { + LOG(DEBUG) << "in BlockDb::get_top_block_state_id()"; + auto it = state_info.upper_bound(ton::BlockId{shard, std::numeric_limits::max()}); + if (it != state_info.begin() && ton::ShardIdFull{(--it)->first} == shard) { + promise(it->second->blk); + return; + } + if (shard.is_masterchain() && zerostate.not_null()) { + promise(zerostate->blk); + return; + } + promise(td::Status::Error(-666, "no state for given workchain found in database")); +} + +void BlockDbImpl::get_block_by_id(ton::BlockId blk_id, bool need_data, td::Promise> promise) { + LOG(DEBUG) << "in BlockDb::get_block_by_id({" << blk_id.workchain << ", " << blk_id.shard << ", " << blk_id.seqno + << "}, " << need_data << ")"; + auto it = block_info.find(blk_id); + if (it != block_info.end()) { + if (need_data && it->second->data.is_null()) { + LOG(DEBUG) << "loading data for block " << blk_id.to_str(); + auto res = load_data(it->second.write()); + if (res.is_error()) { + promise(std::move(res)); + return; + } + } + promise(it->second); + } + promise(td::Status::Error(-666, "block not found in database")); +} + +void BlockDbImpl::get_state_by_id(ton::BlockId blk_id, bool need_data, td::Promise> promise) { + LOG(DEBUG) << "in BlockDb::get_state_by_id({" << blk_id.workchain << ", " << blk_id.shard << ", " << blk_id.seqno + << "}, " << need_data << ")"; + auto it = state_info.find(blk_id); + if (it != state_info.end()) { + if (need_data && it->second->data.is_null()) { + LOG(DEBUG) << "loading data for state " << blk_id.to_str(); + auto res = load_data(it->second.write()); + if (res.is_error()) { + promise(std::move(res)); + return; + } + } + promise(it->second); + } + if (zerostate.not_null() && blk_id == zerostate->blk.id) { + LOG(DEBUG) << "get_state_by_id(): zerostate requested"; + if (need_data && zerostate->data.is_null()) { + LOG(DEBUG) << "loading data for zerostate"; + auto res = load_data(zerostate.write()); + if (res.is_error()) { + promise(std::move(res)); + return; + } + } + promise(zerostate); + return; + } + promise(td::Status::Error(-666, "requested state not found in database")); +} + +void BlockDbImpl::get_out_queue_info_by_id(ton::BlockId blk_id, td::Promise> promise) { + LOG(DEBUG) << "in BlockDb::get_out_queue_info_by_id({" << blk_id.workchain << ", " << blk_id.shard << ", " + << blk_id.seqno << ")"; + auto it = state_info.find(blk_id); + if (it == state_info.end()) { + promise(td::Status::Error( + -666, std::string{"cannot obtain output queue info for block "} + blk_id.to_str() + " : cannot load state")); + } + if (it->second->data.is_null()) { + LOG(DEBUG) << "loading data for state " << blk_id.to_str(); + auto res = load_data(it->second.write()); + if (res.is_error()) { + promise(std::move(res)); + return; + } + } + auto it2 = block_info.find(blk_id); + if (it2 == block_info.end()) { + promise(td::Status::Error(-666, std::string{"cannot obtain output queue info for block "} + blk_id.to_str() + + " : cannot load block description")); + } + vm::StaticBagOfCellsDbLazy::Options options; + auto res = vm::StaticBagOfCellsDbLazy::create(it->second->data.clone(), options); + if (res.is_error()) { + td::Status err = res.move_as_error(); + LOG(ERROR) << "cannot deserialize state for block " << blk_id.to_str() << " : " << err.to_string(); + promise(std::move(err)); + return; + } + auto static_boc = res.move_as_ok(); + auto rc = static_boc->get_root_count(); + if (rc.is_error()) { + promise(rc.move_as_error()); + return; + } + if (rc.move_as_ok() != 1) { + promise(td::Status::Error(-668, std::string{"state for block "} + blk_id.to_str() + " is invalid")); + return; + } + auto res3 = static_boc->get_root_cell(0); + if (res3.is_error()) { + promise(res3.move_as_error()); + return; + } + auto state_root = res3.move_as_ok(); + if (it->second->blk.root_hash != state_root->get_hash().bits()) { + promise(td::Status::Error( + -668, std::string{"state for block "} + blk_id.to_str() + " is invalid : state root hash mismatch")); + } + vm::CellSlice cs = vm::load_cell_slice(state_root); + if (!cs.have(64, 1) || cs.prefetch_ulong(32) != 0x9023afde) { + promise(td::Status::Error(-668, std::string{"state for block "} + blk_id.to_str() + " is invalid")); + } + auto out_queue_info = cs.prefetch_ref(); + promise(Ref{true, blk_id, it2->second->blk.root_hash.cbits(), state_root->get_hash().bits(), + std::move(out_queue_info)}); +} + +void BlockDbImpl::get_object_by_file_hash(FileHash file_hash, bool need_data, bool force_file_load, + td::Promise> promise) { + if (zerostate.not_null() && zerostate->blk.file_hash == file_hash) { + if (need_data && zerostate->data.is_null()) { + auto res = load_data(zerostate.write()); + if (res.is_error()) { + promise(std::move(res)); + return; + } + } + promise(zerostate); + return; + } + promise(td::Status::Error(-666)); +} + +void BlockDbImpl::get_object_by_root_hash(RootHash root_hash, bool need_data, bool force_file_load, + td::Promise> promise) { + if (zerostate.not_null() && zerostate->blk.root_hash == root_hash) { + if (need_data && zerostate->data.is_null()) { + auto res = load_data(zerostate.write()); + if (res.is_error()) { + promise(std::move(res)); + return; + } + } + promise(zerostate); + return; + } + promise(td::Status::Error(-666)); +} + +void BlockDbImpl::save_new_block(ton::BlockIdExt id, td::BufferSlice data, int authority, + td::Promise promise) { + // TODO: add verification that data is a BoC with correct root hash, and that it is a Block corresponding to blk_id + // ... + // TODO: check whether we already have a block with blk_id + // ... + auto save_res = save_db_file(id.file_hash, data, FMode::chk_if_exists | FMode::overwrite | FMode::chk_file_hash); + if (save_res.is_error()) { + promise(std::move(save_res)); + } + auto sz = data.size(); + auto lev = bb.alloc(id.id, id.root_hash, id.file_hash, data.size(), authority & 0xff); + if (sz <= 8) { + std::memcpy(lev->last_bytes, data.data(), sz); + } else { + std::memcpy(lev->last_bytes, data.data() + sz - 8, 8); + } + lev.commit(); + bb.flush(); + promise(td::Unit{}); +} + +void BlockDbImpl::save_new_state(ton::BlockIdExt id, td::BufferSlice data, int authority, + td::Promise promise) { + // TODO: add verification that data is a BoC with correct root hash, and that it is a Block corresponding to blk_id + // ... + // TODO: check whether we already have a block with blk_id + // ... + auto save_res = save_db_file(id.file_hash, data, FMode::chk_if_exists | FMode::overwrite | FMode::chk_file_hash); + if (save_res.is_error()) { + promise(std::move(save_res)); + } + auto sz = data.size(); + auto lev = bb.alloc(id.id, id.root_hash, id.file_hash, data.size(), authority & 0xff); + if (sz <= 8) { + std::memcpy(lev->last_bytes, data.data(), sz); + } else { + std::memcpy(lev->last_bytes, data.data() + sz - 8, 8); + } + lev.commit(); + bb.flush(); + promise(td::Unit{}); +} + +td::Status BlockDbImpl::load_data(FileInfo& file_info, bool force) { + if (!file_info.data.is_null() && !force) { + return td::Status::OK(); + } + if (file_info.blk.file_hash.is_zero()) { + return td::Status::Error("cannot load a block file without knowing its file hash"); + } + auto it = file_cache.find(file_info.blk.file_hash); + if (it != file_cache.end() && !force) { + file_info.data = it->second.clone(); + return td::Status::OK(); + } + std::string filename = compute_db_filename(file_info.blk.file_hash); + auto res = load_binary_file(filename); + if (res.is_error()) { + return res.move_as_error(); + } + file_info.data = res.move_as_ok(); + file_cache_insert(file_info.blk.file_hash, file_info.data); + return td::Status::OK(); +} + +FileInfo FileInfo::clone() const { + return FileInfo{*this}; +} + +FileInfo::FileInfo(const FileInfo& other) + : td::CntObject() + , blk(other.blk) + , type(other.type) + , status(other.status) + , file_size(other.file_size) + , data(other.data.clone()) { +} + +FileInfo* FileInfo::make_copy() const { + return new FileInfo(*this); +} + +} // namespace block diff --git a/crypto/block/block-db.h b/crypto/block/block-db.h new file mode 100644 index 00000000..755403c1 --- /dev/null +++ b/crypto/block/block-db.h @@ -0,0 +1,164 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once +#include "td/utils/int_types.h" +#include "td/utils/buffer.h" +#include "td/actor/actor.h" +#include "ton/ton-types.h" +#include "crypto/common/refcnt.hpp" +#include "crypto/vm/cells.h" + +namespace block { + +using FileHash = ton::Bits256; +using RootHash = ton::Bits256; +using td::Ref; + +struct ZerostateInfo { + td::BufferSlice data; + std::string filename; + FileHash file_hash; + RootHash root_hash; + ZerostateInfo() { + file_hash.set_zero(); + root_hash.set_zero(); + } + ZerostateInfo(RootHash hash, std::string _filename) : filename(_filename), root_hash(std::move(hash)) { + file_hash.set_zero(); + } + ZerostateInfo(std::string _filename) : filename(_filename) { + file_hash.set_zero(); + root_hash.set_zero(); + } + ZerostateInfo(RootHash hash, FileHash fhash) : file_hash(std::move(fhash)), root_hash(std::move(hash)) { + } + bool has_file_hash() const { + return !file_hash.is_zero(); + } + bool has_root_hash() const { + return !root_hash.is_zero(); + } + bool has_filename() const { + return !filename.empty(); + } + bool has_data() const { + return !data.empty(); + } + td::Status base_check(); +}; + +enum class FileType { + unknown = 0, + unknown_boc = 1, + block = 2, + block_candidate = 3, + collated_data = 4, + block_signatures = 5, + state = 6, + out_queue = 7 +}; + +struct FileInfo : public td::CntObject { + ton::BlockIdExt blk; + FileType type; + int status; + long long file_size; + td::BufferSlice data; + FileInfo() : type(FileType::unknown), status(0), file_size(-1) { + blk.file_hash.set_zero(); + blk.root_hash.set_zero(); + } + FileInfo(FileType _type, const ton::BlockId& _id, int _status, const FileHash& _fhash, long long _fsize = -1) + : blk(_id, _fhash), type(_type), status(_status), file_size(_fsize) { + blk.root_hash.set_zero(); + } + FileInfo(FileType _type, const ton::BlockId& _id, int _status, const FileHash& _fhash, const RootHash& _rhash, + long long _fsize = -1) + : blk(_id, _rhash, _fhash), type(_type), status(_status), file_size(_fsize) { + } + FileInfo(FileType _type, const ton::BlockId& _id, int _status, const FileHash& _fhash, const RootHash& _rhash, + td::BufferSlice _data) + : blk(_id, _rhash, _fhash), type(_type), status(_status), file_size(_data.size()), data(std::move(_data)) { + } + FileInfo(FileInfo&& other) = default; + FileInfo clone() const; + FileInfo* make_copy() const override; + + private: + FileInfo(const FileInfo& other); +}; + +struct OutputQueueInfoDescr : public td::CntObject { + ton::BlockId id; + RootHash block_hash; + RootHash state_hash; + RootHash output_queue_info_hash; + td::Ref queue_info; + OutputQueueInfoDescr(ton::BlockId _id, const RootHash& _bhash, const RootHash& _shash, Ref _qinfo) + : id(_id) + , block_hash(_bhash) + , state_hash(_shash) + , output_queue_info_hash(_qinfo->get_hash().bits()) + , queue_info(std::move(_qinfo)) { + } + OutputQueueInfoDescr(ton::BlockId _id, td::ConstBitPtr _bhash, td::ConstBitPtr _shash, Ref _qinfo) + : id(_id) + , block_hash(_bhash) + , state_hash(_shash) + , output_queue_info_hash(_qinfo->get_hash().bits()) + , queue_info(std::move(_qinfo)) { + } +}; + +class BlockDb : public td::actor::Actor { + public: + BlockDb() = default; + virtual ~BlockDb() = default; + static td::Result> create_block_db(std::string _base_dir, + std::unique_ptr _zstate = nullptr, + bool _allow_uninit = false, int _depth = 4, + std::string _binlog_name = ""); + // authority: 0 = standard (inclusion in mc block), 1 = validator (by 2/3 validator signatures) + virtual void get_top_block_id(ton::ShardIdFull shard, int authority, td::Promise promise) = 0; + virtual void get_top_block_state_id(ton::ShardIdFull shard, int authority, td::Promise promise) = 0; + virtual void get_block_by_id(ton::BlockId blk_id, bool need_data, td::Promise> promise) = 0; + virtual void get_state_by_id(ton::BlockId blk_id, bool need_data, td::Promise> promise) = 0; + virtual void get_out_queue_info_by_id(ton::BlockId blk_id, td::Promise> promise) = 0; + virtual void get_object_by_file_hash(FileHash file_hash, bool need_data, bool force_file_load, + td::Promise> promise) = 0; + virtual void get_object_by_root_hash(RootHash root_hash, bool need_data, bool force_file_load, + td::Promise> promise) = 0; + virtual void save_new_block(ton::BlockIdExt blk_id, td::BufferSlice data, int authority, + td::Promise promise) = 0; + virtual void save_new_state(ton::BlockIdExt state_id, td::BufferSlice data, int authority, + td::Promise promise) = 0; +}; + +bool parse_hash_string(std::string arg, RootHash& res); + +FileHash compute_file_hash(const td::BufferSlice& data); +FileHash compute_file_hash(td::Slice data); +td::Result load_binary_file(std::string filename, td::int64 max_size = 0); +td::Status save_binary_file(std::string filename, const td::BufferSlice& data, unsigned long long max_size = 0); + +std::string compute_db_filename(std::string base_dir, const FileHash& file_hash, int depth = 4); +std::string compute_db_tmp_filename(std::string base_dir, const FileHash& file_hash, int i = 0, bool makedirs = true, + int depth = 4); + +} // namespace block diff --git a/crypto/block/block-parse.cpp b/crypto/block/block-parse.cpp new file mode 100644 index 00000000..89fc3d6f --- /dev/null +++ b/crypto/block/block-parse.cpp @@ -0,0 +1,2135 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "td/utils/bits.h" +#include "block/block-parse.h" +#include "block/block-auto.h" +#include "ton/ton-shard.h" +#include "common/util.h" +#include "td/utils/crypto.h" + +namespace block { +using namespace std::literals::string_literals; + +using CombineError = vm::CombineError; + +namespace { +bool debug(const char* str) TD_UNUSED; +bool debug(const char* str) { + std::cerr << str; + return true; +} + +bool debug(int x) TD_UNUSED; +bool debug(int x) { + if (x < 100) { + std::cerr << '[' << (char)(64 + x) << ']'; + } else { + std::cerr << '[' << (char)(64 + x / 100) << x % 100 << ']'; + } + return true; +} +} // namespace + +#define DBG_START int dbg = 0; +#define DBG debug(++dbg)&& +#define DEB_START DBG_START +#define DEB DBG + +namespace tlb { + +using namespace ::tlb; + +int MsgAddressExt::get_size(const vm::CellSlice& cs) const { + switch (get_tag(cs)) { + case addr_none: // 00, addr_none + return 2; + case addr_ext: // 01, addr_extern + if (cs.have(2 + 9)) { + int len = cs.prefetch_long(2 + 9) & 0x1ff; + return 2 + 9 + len; + } + } + return -1; +} + +const MsgAddressExt t_MsgAddressExt; + +const Anycast t_Anycast; + +bool Maybe_Anycast::skip_get_depth(vm::CellSlice& cs, int& depth) const { + depth = 0; + bool have; + return cs.fetch_bool_to(have) && (!have || t_Anycast.skip_get_depth(cs, depth)); +} + +const Maybe_Anycast t_Maybe_Anycast; + +bool MsgAddressInt::validate_skip(vm::CellSlice& cs, bool weak) const { + if (!cs.have(3)) { + return false; + } + switch (get_tag(cs)) { + case addr_std: + return cs.advance(2) && t_Maybe_Anycast.skip(cs) && cs.advance(8 + 256); + case addr_var: + if (cs.advance(2) && t_Maybe_Anycast.skip(cs) && cs.have(9 + 32)) { + int addr_len = (int)cs.fetch_ulong(9); + int workchain_id = (int)cs.fetch_long(32); + return cs.advance(addr_len) && (workchain_id < -0x80 || workchain_id > 0x7f || addr_len != 256) && + (workchain_id != 0 && workchain_id != -1); + } + } + return false; +} + +bool MsgAddressInt::skip_get_depth(vm::CellSlice& cs, int& depth) const { + if (!cs.have(3)) { + return false; + } + switch (get_tag(cs)) { + case addr_std: + return cs.advance(2) && t_Maybe_Anycast.skip_get_depth(cs, depth) && cs.advance(8 + 256); + case addr_var: + if (cs.advance(2) && t_Maybe_Anycast.skip_get_depth(cs, depth) && cs.have(9 + 32)) { + int addr_len = (int)cs.fetch_ulong(9); + return cs.advance(32 + addr_len); + } + } + return false; +} + +ton::AccountIdPrefixFull MsgAddressInt::get_prefix(vm::CellSlice&& cs) { + if (!cs.have(3 + 8 + 64)) { + return {}; + } + ton::WorkchainId workchain; + unsigned long long prefix; + int t = (int)cs.prefetch_ulong(2 + 1 + 5); + switch (t >> 5) { + case 4: { // addr_std$10, anycast=nothing$0 + if (cs.advance(3) && cs.fetch_int_to(8, workchain) && cs.fetch_uint_to(64, prefix)) { + return {workchain, prefix}; + } + break; + } + case 5: { // addr_std$10, anycast=just$1 (Anycast) + t &= 31; // depth:(## 5) + unsigned long long rewrite; + if (cs.advance(8) && cs.fetch_uint_to(t, rewrite) // rewrite_pfx:(bits depth) + && cs.fetch_int_to(8, workchain) // workchain_id:int8 + && cs.fetch_uint_to(64, prefix)) { // address:bits256 + rewrite <<= 64 - t; + return {workchain, (prefix & (std::numeric_limits::max() >> t)) | rewrite}; + } + break; + } + case 6: { // addr_var$11, anycast=nothing$0 + int len; + if (cs.advance(3) && cs.fetch_uint_to(9, len) // addr_len:(## 9) + && len >= 64 // { len >= 64 } + && cs.fetch_int_to(32, workchain) // workchain_id:int32 + && cs.fetch_uint_to(64, prefix)) { // address:(bits addr_len) + return {workchain, prefix}; + } + break; + } + case 7: { // addr_var$11, anycast=just$1 (Anycast) + t &= 31; // depth:(## 5) + int len; + unsigned long long rewrite; + if (cs.advance(8) && cs.fetch_uint_to(t, rewrite) // rewrite_pfx:(bits depth) + && cs.fetch_uint_to(9, len) // addr_len:(## 9) + && len >= 64 // { len >= 64 } + && cs.fetch_int_to(32, workchain) // workchain_id:int32 + && cs.fetch_uint_to(64, prefix)) { // address:bits256 + rewrite <<= 64 - t; + return {workchain, (prefix & (std::numeric_limits::max() >> t)) | rewrite}; + } + break; + } + } + return {}; +} + +ton::AccountIdPrefixFull MsgAddressInt::get_prefix(const vm::CellSlice& cs) { + return get_prefix(vm::CellSlice{cs}); +} + +ton::AccountIdPrefixFull MsgAddressInt::get_prefix(Ref cs_ref) { + if (cs_ref->is_unique()) { + return get_prefix(std::move(cs_ref.unique_write())); + } else { + return get_prefix(vm::CellSlice{*cs_ref}); + } +} + +bool MsgAddressInt::extract_std_address(Ref cs_ref, ton::WorkchainId& workchain, + ton::StdSmcAddress& addr, bool rewrite) const { + if (cs_ref.is_null()) { + return false; + } else if (cs_ref->is_unique()) { + return extract_std_address(cs_ref.unique_write(), workchain, addr, rewrite); + } else { + vm::CellSlice cs{*cs_ref}; + return extract_std_address(cs, workchain, addr, rewrite); + } +} + +bool MsgAddressInt::extract_std_address(vm::CellSlice& cs, ton::WorkchainId& workchain, ton::StdSmcAddress& addr, + bool do_rewrite) const { + if (!cs.have(3 + 8 + 64)) { + return {}; + } + int t = (int)cs.prefetch_ulong(2 + 1 + 5); + switch (t >> 5) { + case 4: { // addr_std$10, anycast=nothing$0 + return cs.advance(3) && cs.fetch_int_to(8, workchain) && cs.fetch_bits_to(addr); + } + case 5: { // addr_std$10, anycast=just$1 (Anycast) + t &= 31; // depth:(## 5) + unsigned long long rewrite; + if (cs.advance(8) && cs.fetch_uint_to(t, rewrite) // rewrite_pfx:(bits depth) + && cs.fetch_int_to(8, workchain) // workchain_id:int8 + && cs.fetch_bits_to(addr)) { // address:bits256 + if (do_rewrite) { + addr.bits().store_uint(rewrite, t); + } + return true; + } + break; + } + case 6: { // addr_var$11, anycast=nothing$0 + int len; + return cs.advance(3) && cs.fetch_uint_to(9, len) // addr_len:(## 9) + && len == 256 // only 256-bit addresses are standard + && cs.fetch_int_to(32, workchain) // workchain_id:int32 + && cs.fetch_bits_to(addr); // address:(bits addr_len) + } + case 7: { // addr_var$11, anycast=just$1 (Anycast) + t &= 31; // depth:(## 5) + int len; + unsigned long long rewrite; + if (cs.advance(8) && cs.fetch_uint_to(t, rewrite) // rewrite_pfx:(bits depth) + && cs.fetch_uint_to(9, len) // addr_len:(## 9) + && len == 256 // only 256-bit addresses are standard + && cs.fetch_int_to(32, workchain) // workchain_id:int32 + && cs.fetch_bits_to(addr)) { // address:bits256 + if (do_rewrite) { + addr.bits().store_uint(rewrite, t); + } + return true; + } + break; + } + } + return false; +} + +const MsgAddressInt t_MsgAddressInt; + +bool MsgAddress::validate_skip(vm::CellSlice& cs, bool weak) const { + switch (get_tag(cs)) { + case addr_none: + case addr_ext: + return t_MsgAddressExt.validate_skip(cs, weak); + case addr_std: + case addr_var: + return t_MsgAddressInt.validate_skip(cs, weak); + } + return false; +} + +const MsgAddress t_MsgAddress; + +bool VarUInteger::skip(vm::CellSlice& cs) const { + int len = (int)cs.fetch_ulong(ln); + return len >= 0 && len < n && cs.advance(len * 8); +} + +bool VarUInteger::validate_skip(vm::CellSlice& cs, bool weak) const { + int len = (int)cs.fetch_ulong(ln); + return len >= 0 && len < n && (!len || cs.prefetch_ulong(8)) && cs.advance(len * 8); +} + +td::RefInt256 VarUInteger::as_integer_skip(vm::CellSlice& cs) const { + int len = (int)cs.fetch_ulong(ln); + return (len >= 0 && len < n && (!len || cs.prefetch_ulong(8))) ? cs.fetch_int256(len * 8, false) : td::RefInt256{}; +} + +unsigned long long VarUInteger::as_uint(const vm::CellSlice& cs) const { + int len = (int)cs.prefetch_ulong(ln); + return len >= 0 && len <= 8 && cs.have(ln + len * 8) ? td::bitstring::bits_load_ulong(cs.data_bits() + ln, len * 8) + : std::numeric_limits::max(); +} + +bool VarUInteger::store_integer_value(vm::CellBuilder& cb, const td::BigInt256& value) const { + int k = value.bit_size(false); + return k <= (n - 1) * 8 && cb.store_long_bool((k + 7) >> 3, ln) && cb.store_int256_bool(value, (k + 7) & -8, false); +} + +unsigned VarUInteger::precompute_integer_size(const td::BigInt256& value) const { + int k = value.bit_size(false); + return k <= (n - 1) * 8 ? ln + ((k + 7) & -8) : 0xfff; +} + +unsigned VarUInteger::precompute_integer_size(td::RefInt256 value) const { + if (value.is_null()) { + return 0xfff; + } + int k = value->bit_size(false); + return k <= (n - 1) * 8 ? ln + ((k + 7) & -8) : 0xfff; +} + +const VarUInteger t_VarUInteger_3{3}, t_VarUInteger_7{7}, t_VarUInteger_16{16}, t_VarUInteger_32{32}; + +bool VarUIntegerPos::skip(vm::CellSlice& cs) const { + int len = (int)cs.fetch_ulong(ln); + return len > 0 && len < n && cs.advance(len * 8); +} + +bool VarUIntegerPos::validate_skip(vm::CellSlice& cs, bool weak) const { + int len = (int)cs.fetch_ulong(ln); + return len > 0 && len < n && cs.prefetch_ulong(8) && cs.advance(len * 8); +} + +td::RefInt256 VarUIntegerPos::as_integer_skip(vm::CellSlice& cs) const { + int len = (int)cs.fetch_ulong(ln); + return (len > 0 && len < n && cs.prefetch_ulong(8)) ? cs.fetch_int256(len * 8, false) : td::RefInt256{}; +} + +unsigned long long VarUIntegerPos::as_uint(const vm::CellSlice& cs) const { + int len = (int)cs.prefetch_ulong(ln); + return len > 0 && len <= 8 && cs.have(ln + len * 8) && cs.prefetch_ulong(8) + ? td::bitstring::bits_load_ulong(cs.data_bits() + ln, len * 8) + : std::numeric_limits::max(); +} + +bool VarUIntegerPos::store_integer_value(vm::CellBuilder& cb, const td::BigInt256& value) const { + int k = value.bit_size(false); + return k <= (n - 1) * 8 && value.sgn() > 0 && cb.store_long_bool((k + 7) >> 3, ln) && + cb.store_int256_bool(value, (k + 7) & -8, false); +} + +const VarUIntegerPos t_VarUIntegerPos_16{16}, t_VarUIntegerPos_32{32}; + +static inline bool redundant_int(const vm::CellSlice& cs) { + int t = (int)cs.prefetch_long(9); + return t == 0 || t == -1; +} + +bool VarInteger::skip(vm::CellSlice& cs) const { + int len = (int)cs.fetch_ulong(ln); + return len >= 0 && len < n && cs.advance(len * 8); +} + +bool VarInteger::validate_skip(vm::CellSlice& cs, bool weak) const { + int len = (int)cs.fetch_ulong(ln); + return len >= 0 && len < n && (!len || !redundant_int(cs)) && cs.advance(len * 8); +} + +td::RefInt256 VarInteger::as_integer_skip(vm::CellSlice& cs) const { + int len = (int)cs.fetch_ulong(ln); + return (len >= 0 && len < n && (!len || !redundant_int(cs))) ? cs.fetch_int256(len * 8, true) : td::RefInt256{}; +} + +long long VarInteger::as_int(const vm::CellSlice& cs) const { + int len = (int)cs.prefetch_ulong(ln); + return len >= 0 && len <= 8 && cs.have(ln + len * 8) ? td::bitstring::bits_load_long(cs.data_bits() + ln, len * 8) + : (1ULL << 63); +} + +bool VarInteger::store_integer_value(vm::CellBuilder& cb, const td::BigInt256& value) const { + int k = value.bit_size(true); + return k <= (n - 1) * 8 && cb.store_long_bool((k + 7) >> 3, ln) && cb.store_int256_bool(value, (k + 7) & -8, true); +} + +bool VarIntegerNz::skip(vm::CellSlice& cs) const { + int len = (int)cs.fetch_ulong(ln); + return len > 0 && len < n && cs.advance(len * 8); +} + +bool VarIntegerNz::validate_skip(vm::CellSlice& cs, bool weak) const { + int len = (int)cs.fetch_ulong(ln); + return len > 0 && len < n && !redundant_int(cs) && cs.advance(len * 8); +} + +td::RefInt256 VarIntegerNz::as_integer_skip(vm::CellSlice& cs) const { + int len = (int)cs.fetch_ulong(ln); + return (len > 0 && len < n && !redundant_int(cs)) ? cs.fetch_int256(len * 8, true) : td::RefInt256{}; +} + +long long VarIntegerNz::as_int(const vm::CellSlice& cs) const { + int len = (int)cs.prefetch_ulong(ln); + return len > 0 && len <= 8 && cs.have(ln + len * 8) && !redundant_int(cs) + ? td::bitstring::bits_load_long(cs.data_bits() + ln, len * 8) + : (1ULL << 63); +} + +bool VarIntegerNz::store_integer_value(vm::CellBuilder& cb, const td::BigInt256& value) const { + int k = value.bit_size(true); + return k <= (n - 1) * 8 && value.sgn() != 0 && cb.store_long_bool((k + 7) >> 3, ln) && + cb.store_int256_bool(value, (k + 7) & -8, true); +} + +bool Grams::validate_skip(vm::CellSlice& cs, bool weak) const { + return t_VarUInteger_16.validate_skip(cs, weak); +} + +td::RefInt256 Grams::as_integer_skip(vm::CellSlice& cs) const { + return t_VarUInteger_16.as_integer_skip(cs); +} + +bool Grams::null_value(vm::CellBuilder& cb) const { + return t_VarUInteger_16.null_value(cb); +} + +bool Grams::store_integer_value(vm::CellBuilder& cb, const td::BigInt256& value) const { + return t_VarUInteger_16.store_integer_value(cb, value); +} + +unsigned Grams::precompute_size(const td::BigInt256& value) const { + return t_VarUInteger_16.precompute_integer_size(value); +} + +unsigned Grams::precompute_size(td::RefInt256 value) const { + return t_VarUInteger_16.precompute_integer_size(std::move(value)); +} + +const Grams t_Grams; + +const Unary t_Unary; + +bool HmLabel::validate_skip(vm::CellSlice& cs, bool weak, int& n) const { + switch (get_tag(cs)) { + case hml_short: + return cs.advance(1) && (n = cs.count_leading(1)) <= m && cs.advance(2 * n + 1); + case hml_long: + return cs.advance(2) && cs.fetch_uint_leq(m, n) && cs.advance(n); + case hml_same: + return cs.advance(3) && cs.fetch_uint_leq(m, n); + } + return false; +} + +int HmLabel::get_tag(const vm::CellSlice& cs) const { + int tag = (int)cs.prefetch_ulong(2); + return tag != 1 ? tag : hml_short; +} + +int HashmapNode::get_size(const vm::CellSlice& cs) const { + assert(n >= 0); + return n ? 0x20000 : value_type.get_size(cs); +} + +bool HashmapNode::skip(vm::CellSlice& cs) const { + assert(n >= 0); + return n ? cs.advance_refs(2) : value_type.skip(cs); +} + +bool HashmapNode::validate_skip(vm::CellSlice& cs, bool weak) const { + assert(n >= 0); + if (!n) { + // hmn_leaf + return value_type.validate_skip(cs, weak); + } else { + // hmn_fork + Hashmap branch_type{n - 1, value_type}; + return branch_type.validate_ref(cs.fetch_ref(), weak) && branch_type.validate_ref(cs.fetch_ref(), weak); + } +} + +bool Hashmap::skip(vm::CellSlice& cs) const { + int l; + return HmLabel{n}.skip(cs, l) && HashmapNode{n - l, value_type}.skip(cs); +} + +bool Hashmap::validate_skip(vm::CellSlice& cs, bool weak) const { + int l; + return HmLabel{n}.validate_skip(cs, weak, l) && HashmapNode{n - l, value_type}.validate_skip(cs, weak); +} + +int HashmapE::get_size(const vm::CellSlice& cs) const { + int tag = get_tag(cs); + return (tag >= 0 ? (tag > 0 ? 0x10001 : 1) : -1); +} + +bool HashmapE::validate(const vm::CellSlice& cs, bool weak) const { + int tag = get_tag(cs); + return tag <= 0 ? !tag : root_type.validate_ref(cs.prefetch_ref(), weak); +} + +bool HashmapE::add_values(vm::CellBuilder& cb, vm::CellSlice& cs1, vm::CellSlice& cs2) const { + int n = root_type.n; + vm::Dictionary dict1{vm::DictAdvance(), cs1, n}, dict2{vm::DictAdvance(), cs2, n}; + const TLB& vt = root_type.value_type; + vm::Dictionary::simple_combine_func_t combine = [vt](vm::CellBuilder& cb, Ref cs1_ref, + Ref cs2_ref) -> bool { + if (!vt.add_values(cb, cs1_ref.write(), cs2_ref.write())) { + throw CombineError{}; + } + return true; + }; + return dict1.combine_with(dict2, combine) && std::move(dict1).append_dict_to_bool(cb); +} + +bool HashmapE::add_values_ref(Ref& res, Ref arg1, Ref arg2) const { + int n = root_type.n; + vm::Dictionary dict1{std::move(arg1), n}, dict2{std::move(arg2), n}; + const TLB& vt = root_type.value_type; + vm::Dictionary::simple_combine_func_t combine = [vt](vm::CellBuilder& cb, Ref cs1_ref, + Ref cs2_ref) -> bool { + if (!vt.add_values(cb, cs1_ref.write(), cs2_ref.write())) { + throw CombineError{}; + } + return true; + }; + if (dict1.combine_with(dict2, combine)) { + dict2.reset(); + res = std::move(dict1).extract_root_cell(); + return true; + } else { + res = Ref{}; + return false; + } +} + +int HashmapE::sub_values(vm::CellBuilder& cb, vm::CellSlice& cs1, vm::CellSlice& cs2) const { + int n = root_type.n; + vm::Dictionary dict1{vm::DictAdvance(), cs1, n}, dict2{vm::DictAdvance(), cs2, n}; + const TLB& vt = root_type.value_type; + vm::Dictionary::simple_combine_func_t combine = [vt](vm::CellBuilder& cb, Ref cs1_ref, + Ref cs2_ref) -> bool { + int r = vt.sub_values(cb, cs1_ref.write(), cs2_ref.write()); + if (r < 0) { + throw CombineError{}; + } + return r; + }; + if (!dict1.combine_with(dict2, combine, 1)) { + return -1; + } + dict2.reset(); + bool not_empty = !dict1.is_empty(); + return std::move(dict1).append_dict_to_bool(cb) ? not_empty : -1; +} + +int HashmapE::sub_values_ref(Ref& res, Ref arg1, Ref arg2) const { + int n = root_type.n; + vm::Dictionary dict1{std::move(arg1), n}, dict2{std::move(arg2), n}; + const TLB& vt = root_type.value_type; + vm::Dictionary::simple_combine_func_t combine = [vt](vm::CellBuilder& cb, Ref cs1_ref, + Ref cs2_ref) -> bool { + int r = vt.sub_values(cb, cs1_ref.write(), cs2_ref.write()); + if (r < 0) { + throw CombineError{}; + } + return r; + }; + if (dict1.combine_with(dict2, combine, 1)) { + dict2.reset(); + res = std::move(dict1).extract_root_cell(); + return res.not_null(); + } else { + res = Ref{}; + return -1; + } +} + +bool HashmapE::store_ref(vm::CellBuilder& cb, Ref arg) const { + if (arg.is_null()) { + return cb.store_long_bool(0, 1); + } else { + return cb.store_long_bool(1, 1) && cb.store_ref_bool(std::move(arg)); + } +} + +const ExtraCurrencyCollection t_ExtraCurrencyCollection; + +bool CurrencyCollection::validate_skip(vm::CellSlice& cs, bool weak) const { + return t_Grams.validate_skip(cs, weak) && t_ExtraCurrencyCollection.validate_skip(cs, weak); +} + +bool CurrencyCollection::skip(vm::CellSlice& cs) const { + return t_Grams.skip(cs) && t_ExtraCurrencyCollection.skip(cs); +} + +td::RefInt256 CurrencyCollection::as_integer_skip(vm::CellSlice& cs) const { + auto res = t_Grams.as_integer_skip(cs); + if (res.not_null() && t_ExtraCurrencyCollection.skip(cs)) { + return res; + } else { + return {}; + } +} + +bool CurrencyCollection::add_values(vm::CellBuilder& cb, vm::CellSlice& cs1, vm::CellSlice& cs2) const { + return t_Grams.add_values(cb, cs1, cs2) && t_ExtraCurrencyCollection.add_values(cb, cs1, cs2); +} + +bool CurrencyCollection::unpack_special(vm::CellSlice& cs, td::RefInt256& balance, Ref& extra, + bool inexact) const { + balance = t_Grams.as_integer_skip(cs); + if (cs.fetch_ulong(1) == 1) { + return balance.not_null() && cs.fetch_ref_to(extra) && (inexact || cs.empty_ext()); + } else { + extra.clear(); + return balance.not_null() && (inexact || cs.empty_ext()); + } +} + +bool CurrencyCollection::unpack_special(vm::CellSlice& cs, block::CurrencyCollection& value, bool inexact) const { + return unpack_special(cs, value.grams, value.extra, inexact); +} + +bool CurrencyCollection::pack_special(vm::CellBuilder& cb, td::RefInt256 balance, Ref extra) const { + return t_Grams.store_integer_ref(cb, std::move(balance)) && t_ExtraCurrencyCollection.store_ref(cb, std::move(extra)); +} + +bool CurrencyCollection::pack_special(vm::CellBuilder& cb, const block::CurrencyCollection& value) const { + return value.is_valid() && pack_special(cb, value.grams, value.extra); +} + +bool CurrencyCollection::pack_special(vm::CellBuilder& cb, block::CurrencyCollection&& value) const { + return value.is_valid() && pack_special(cb, std::move(value.grams), std::move(value.extra)); +} + +bool CurrencyCollection::unpack(vm::CellSlice& cs, block::CurrencyCollection& res) const { + return unpack_special(cs, res.grams, res.extra); +} + +bool CurrencyCollection::pack(vm::CellBuilder& cb, const block::CurrencyCollection& res) const { + return res.is_valid() && pack_special(cb, res.grams, res.extra); +} + +const CurrencyCollection t_CurrencyCollection; + +bool CommonMsgInfo::validate_skip(vm::CellSlice& cs, bool weak) const { + int tag = get_tag(cs); + switch (tag) { + case int_msg_info: + return cs.advance(4) // int_msg_info$0 ihr_disabled:Bool bounce:Bool bounced:Bool + && t_MsgAddressInt.validate_skip(cs, weak) // src + && t_MsgAddressInt.validate_skip(cs, weak) // dest + && t_CurrencyCollection.validate_skip(cs, weak) // value + && t_Grams.validate_skip(cs, weak) // ihr_fee + && t_Grams.validate_skip(cs, weak) // fwd_fee + && cs.advance(64 + 32); // created_lt:uint64 created_at:uint32 + case ext_in_msg_info: + return cs.advance(2) && t_MsgAddressExt.validate_skip(cs, weak) // src + && t_MsgAddressInt.validate_skip(cs, weak) // dest + && t_Grams.validate_skip(cs, weak); // import_fee + case ext_out_msg_info: + return cs.advance(2) && t_MsgAddressInt.validate_skip(cs, weak) // src + && t_MsgAddressExt.validate_skip(cs, weak) // dest + && cs.advance(64 + 32); // created_lt:uint64 created_at:uint32 + } + return false; +} + +bool CommonMsgInfo::unpack(vm::CellSlice& cs, CommonMsgInfo::Record_int_msg_info& data) const { + return get_tag(cs) == int_msg_info && cs.advance(1) && cs.fetch_bool_to(data.ihr_disabled) && + cs.fetch_bool_to(data.bounce) && cs.fetch_bool_to(data.bounced) && t_MsgAddressInt.fetch_to(cs, data.src) && + t_MsgAddressInt.fetch_to(cs, data.dest) && t_CurrencyCollection.fetch_to(cs, data.value) && + t_Grams.fetch_to(cs, data.ihr_fee) && t_Grams.fetch_to(cs, data.fwd_fee) && + cs.fetch_uint_to(64, data.created_lt) && cs.fetch_uint_to(32, data.created_at); +} + +bool CommonMsgInfo::skip(vm::CellSlice& cs) const { + int tag = get_tag(cs); + switch (tag) { + case int_msg_info: + return cs.advance(4) // int_msg_info$0 ihr_disabled:Bool bounce:Bool bounced:Bool + && t_MsgAddressInt.skip(cs) // src + && t_MsgAddressInt.skip(cs) // dest + && t_CurrencyCollection.skip(cs) // value + && t_Grams.skip(cs) // ihr_fee + && t_Grams.skip(cs) // fwd_fee + && cs.advance(64 + 32); // created_lt:uint64 created_at:uint32 + case ext_in_msg_info: + return cs.advance(2) && t_MsgAddressExt.skip(cs) // src + && t_MsgAddressInt.skip(cs) // dest + && t_Grams.skip(cs); // import_fee + case ext_out_msg_info: + return cs.advance(2) && t_MsgAddressInt.skip(cs) // src + && t_MsgAddressExt.skip(cs) // dest + && cs.advance(64 + 32); // created_lt:uint64 created_at:uint32 + } + return false; +} + +bool CommonMsgInfo::get_created_lt(vm::CellSlice& cs, unsigned long long& created_lt) const { + switch (get_tag(cs)) { + case int_msg_info: + return cs.advance(4) // int_msg_info$0 ihr_disabled:Bool bounce:Bool bounced:Bool + && t_MsgAddressInt.skip(cs) // src + && t_MsgAddressInt.skip(cs) // dest + && t_CurrencyCollection.skip(cs) // value + && t_Grams.skip(cs) // ihr_fee + && t_Grams.skip(cs) // fwd_fee + && cs.fetch_ulong_bool(64, created_lt) // created_lt:uint64 + && cs.advance(32); // created_at:uint32 + case ext_in_msg_info: + return false; + case ext_out_msg_info: + return cs.advance(2) && t_MsgAddressInt.skip(cs) // src + && t_MsgAddressExt.skip(cs) // dest + && cs.fetch_ulong_bool(64, created_lt) // created_lt:uint64 + && cs.advance(32); // created_at:uint32 + } + return false; +} + +const CommonMsgInfo t_CommonMsgInfo; +const TickTock t_TickTock; +const RefAnything t_RefCell; + +bool StateInit::validate_skip(vm::CellSlice& cs, bool weak) const { + return Maybe{5}.validate_skip(cs, weak) // split_depth:(Maybe (## 5)) + && Maybe{}.validate_skip(cs, weak) // special:(Maybe TickTock) + && Maybe{}.validate_skip(cs, weak) // code:(Maybe ^Cell) + && Maybe{}.validate_skip(cs, weak) // data:(Maybe ^Cell) + && Maybe{}.validate_skip(cs, weak); // library:(Maybe ^Cell) +} + +bool StateInit::get_ticktock(vm::CellSlice& cs, int& ticktock) const { + bool have_tt; + ticktock = 0; + return Maybe{5}.validate_skip(cs) && cs.fetch_bool_to(have_tt) && (!have_tt || cs.fetch_uint_to(2, ticktock)); +} + +const StateInit t_StateInit; + +bool Message::validate_skip(vm::CellSlice& cs, bool weak) const { + static const Maybe>> init_type; + static const Either body_type; + return t_CommonMsgInfo.validate_skip(cs, weak) // info:CommonMsgInfo + && init_type.validate_skip(cs, weak) // init:(Maybe (Either StateInit ^StateInit)) + && body_type.validate_skip(cs, weak); // body:(Either X ^X) +} + +bool Message::extract_info(vm::CellSlice& cs) const { + return t_CommonMsgInfo.extract(cs); +} + +bool Message::get_created_lt(vm::CellSlice& cs, unsigned long long& created_lt) const { + return t_CommonMsgInfo.get_created_lt(cs, created_lt); +} + +bool Message::is_internal(Ref ref) const { + return is_internal(load_cell_slice(std::move(ref))); +} + +const Message t_Message; +const RefTo t_Ref_Message; + +bool IntermediateAddress::validate_skip(vm::CellSlice& cs, bool weak) const { + switch (get_tag(cs)) { + case interm_addr_regular: + return cs.advance(1) && cs.fetch_ulong(7) <= 96U; + case interm_addr_simple: + return cs.advance(2 + 8 + 64); + case interm_addr_ext: + if (cs.have(2 + 32 + 64)) { + cs.advance(2); + int workchain_id = (int)cs.fetch_long(32); + return (workchain_id < -128 || workchain_id >= 128) && cs.advance(64); + } + // no break + } + return false; +} + +bool IntermediateAddress::skip(vm::CellSlice& cs) const { + return cs.advance(get_size(cs)); +} + +int IntermediateAddress::get_size(const vm::CellSlice& cs) const { + switch (get_tag(cs)) { + case interm_addr_regular: + return 1 + 7; + case interm_addr_simple: + return 2 + 8 + 64; + case interm_addr_ext: + return 2 + 32 + 64; + } + return -1; +} + +const IntermediateAddress t_IntermediateAddress; + +bool MsgEnvelope::validate_skip(vm::CellSlice& cs, bool weak) const { + return cs.fetch_ulong(4) == 4 // msg_envelope#4 + && t_IntermediateAddress.validate_skip(cs, weak) // cur_addr:IntermediateAddress + && t_IntermediateAddress.validate_skip(cs, weak) // next_addr:IntermediateAddress + && t_Grams.validate_skip(cs, weak) // fwd_fee_remaining:Grams + && t_Ref_Message.validate_skip(cs, weak); // msg:^Message +} + +bool MsgEnvelope::skip(vm::CellSlice& cs) const { + return cs.advance(4) // msg_envelope#4 + && t_IntermediateAddress.skip(cs) // cur_addr:IntermediateAddress + && t_IntermediateAddress.skip(cs) // next_addr:IntermediateAddress + && t_Grams.skip(cs) // fwd_fee_remaining:Grams + && t_Ref_Message.skip(cs); // msg:^Message +} + +bool MsgEnvelope::extract_fwd_fees_remaining(vm::CellSlice& cs) const { + return t_IntermediateAddress.skip(cs) && t_IntermediateAddress.skip(cs) && t_Grams.extract(cs); +} + +bool MsgEnvelope::unpack(vm::CellSlice& cs, MsgEnvelope::Record& data) const { + return cs.fetch_ulong(4) == 4 // msg_envelope#4 + && t_IntermediateAddress.fetch_to(cs, data.cur_addr) // cur_addr:IntermediateAddress + && t_IntermediateAddress.fetch_to(cs, data.next_addr) // next_addr:IntermediateAddress + && t_Grams.fetch_to(cs, data.fwd_fee_remaining) // fwd_fee_remaining:Grams + && cs.fetch_ref_to(data.msg); // msg:^Message +} + +bool MsgEnvelope::unpack(vm::CellSlice& cs, MsgEnvelope::Record_std& data) const { + return cs.fetch_ulong(4) == 4 // msg_envelope#4 + && t_IntermediateAddress.fetch_regular(cs, data.cur_addr) // cur_addr:IntermediateAddress + && t_IntermediateAddress.fetch_regular(cs, data.next_addr) // next_addr:IntermediateAddress + && t_Grams.as_integer_skip_to(cs, data.fwd_fee_remaining) // fwd_fee_remaining:Grams + && cs.fetch_ref_to(data.msg); // msg:^Message +} + +bool MsgEnvelope::unpack_std(vm::CellSlice& cs, int& cur_a, int& nhop_a, Ref& msg) const { + return cs.fetch_ulong(4) == 4 // msg_envelope#4 + && t_IntermediateAddress.fetch_regular(cs, cur_a) // cur_addr:IntermediateAddress + && t_IntermediateAddress.fetch_regular(cs, nhop_a) // next_addr:IntermediateAddress + && cs.fetch_ref_to(msg); +} + +bool MsgEnvelope::get_created_lt(const vm::CellSlice& cs, unsigned long long& created_lt) const { + if (!cs.size_refs()) { + return false; + } + auto msg_cs = load_cell_slice(cs.prefetch_ref()); + return t_Message.get_created_lt(msg_cs, created_lt); +} + +const MsgEnvelope t_MsgEnvelope; +const RefTo t_Ref_MsgEnvelope; + +bool StorageUsed::validate_skip(vm::CellSlice& cs, bool weak) const { + return t_VarUInteger_7.validate_skip(cs, weak) // cells:(VarUInteger 7) + && t_VarUInteger_7.validate_skip(cs, weak) // bits:(VarUInteger 7) + && t_VarUInteger_7.validate_skip(cs, weak); // public_cells:(VarUInteger 7) +} + +bool StorageUsed::skip(vm::CellSlice& cs) const { + return t_VarUInteger_7.skip(cs) // cells:(VarUInteger 7) + && t_VarUInteger_7.skip(cs) // bits:(VarUInteger 7) + && t_VarUInteger_7.skip(cs); // public_cells:(VarUInteger 7) +} + +const StorageUsed t_StorageUsed; + +bool StorageUsedShort::validate_skip(vm::CellSlice& cs, bool weak) const { + return t_VarUInteger_7.validate_skip(cs, weak) // cells:(VarUInteger 7) + && t_VarUInteger_7.validate_skip(cs, weak); // bits:(VarUInteger 7) +} + +bool StorageUsedShort::skip(vm::CellSlice& cs) const { + return t_VarUInteger_7.skip(cs) // cells:(VarUInteger 7) + && t_VarUInteger_7.skip(cs); // bits:(VarUInteger 7) +} + +const StorageUsedShort t_StorageUsedShort; + +const Maybe t_Maybe_Grams; + +bool StorageInfo::skip(vm::CellSlice& cs) const { + return t_StorageUsed.skip(cs) // used:StorageUsed + && cs.advance(32) // last_paid:uint32 + && t_Maybe_Grams.skip(cs); // due_payment:(Maybe Grams) +} + +bool StorageInfo::validate_skip(vm::CellSlice& cs, bool weak) const { + return t_StorageUsed.validate_skip(cs, weak) // used:StorageUsed + && cs.advance(32) // last_paid:uint32 + && t_Maybe_Grams.validate_skip(cs, weak); // due_payment:(Maybe Grams) +} + +const StorageInfo t_StorageInfo; + +bool AccountState::validate_skip(vm::CellSlice& cs, bool weak) const { + switch (get_tag(cs)) { + case account_uninit: + return cs.advance(2); + case account_frozen: + return cs.advance(2 + 256); + case account_active: + return cs.advance(1) && t_StateInit.validate_skip(cs, weak); + } + return false; +} + +bool AccountState::get_ticktock(vm::CellSlice& cs, int& ticktock) const { + if (get_tag(cs) != account_active) { + ticktock = 0; + return true; + } + return cs.advance(1) && t_StateInit.get_ticktock(cs, ticktock); +} + +const AccountState t_AccountState; + +bool AccountStorage::skip(vm::CellSlice& cs) const { + return cs.advance(64) && t_CurrencyCollection.skip(cs) && t_AccountState.skip(cs); +} + +bool AccountStorage::skip_copy_balance(vm::CellBuilder& cb, vm::CellSlice& cs) const { + return cs.advance(64) && t_CurrencyCollection.skip_copy(cb, cs) && t_AccountState.skip(cs); +} + +bool AccountStorage::validate_skip(vm::CellSlice& cs, bool weak) const { + return cs.advance(64) && t_CurrencyCollection.validate_skip(cs, weak) && t_AccountState.validate_skip(cs, weak); +} + +const AccountStorage t_AccountStorage; + +bool Account::skip(vm::CellSlice& cs) const { + switch (get_tag(cs)) { + case account_none: + return cs.advance(1); + case account: + return cs.advance(1) // account$1 + && t_MsgAddressInt.skip(cs) // addr:MsgAddressInt + && t_StorageInfo.skip(cs) // storage_stat:StorageInfo + && t_AccountStorage.skip(cs); // storage:AccountStorage + } + return false; +} + +bool Account::validate_skip(vm::CellSlice& cs, bool weak) const { + switch (get_tag(cs)) { + case account_none: + return allow_empty && cs.advance(1); + case account: + return cs.advance(1) // account$1 + && t_MsgAddressInt.validate_skip(cs, weak) // addr:MsgAddressInt + && t_StorageInfo.validate_skip(cs, weak) // storage_stat:StorageInfo + && t_AccountStorage.validate_skip(cs, weak); // storage:AccountStorage + } + return false; +} + +bool Account::skip_copy_balance(vm::CellBuilder& cb, vm::CellSlice& cs) const { + switch (get_tag(cs)) { + case account_none: + return allow_empty && cs.advance(1) && t_CurrencyCollection.null_value(cb); + case account: + return cs.advance(1) // account$1 + && t_MsgAddressInt.skip(cs) // addr:MsgAddressInt + && t_StorageInfo.skip(cs) // storage_stat:StorageInfo + && t_AccountStorage.skip_copy_balance(cb, cs); // storage:AccountStorage + } + return false; +} + +bool Account::skip_copy_depth_balance(vm::CellBuilder& cb, vm::CellSlice& cs) const { + int depth; + switch (get_tag(cs)) { + case account_none: + return allow_empty && cs.advance(1) && t_DepthBalanceInfo.null_value(cb); + case account: + return cs.advance(1) // account$1 + && t_MsgAddressInt.skip_get_depth(cs, depth) // addr:MsgAddressInt + && cb.store_uint_leq(30, depth) // -> store split_depth:(#<= 30) + && t_StorageInfo.skip(cs) // storage_stat:StorageInfo + && t_AccountStorage.skip_copy_balance(cb, cs); // storage:AccountStorage + } + return false; +} + +const Account t_Account, t_AccountE{true}; +const RefTo t_Ref_Account; + +bool ShardAccount::extract_account_state(Ref cs_ref, Ref& acc_state) { + if (cs_ref.is_null()) { + vm::CellBuilder cb; + return cb.store_bool_bool(false) && cb.finalize_to(acc_state); + } else { + return cs_ref->prefetch_ref_to(acc_state); + } +} + +bool ShardAccount::Record::reset() { + last_trans_hash.set_zero(); + last_trans_lt = 0; + is_zero = valid = true; + vm::CellBuilder cb; + return (cb.store_bool_bool(false) && cb.finalize_to(account)) || invalidate(); +} + +bool ShardAccount::Record::unpack(vm::CellSlice& cs) { + is_zero = false; + valid = true; + return (cs.fetch_ref_to(account) && cs.fetch_bits_to(last_trans_hash) && cs.fetch_uint_to(64, last_trans_lt)) || + invalidate(); +} + +bool ShardAccount::Record::unpack(Ref cs_ref) { + if (cs_ref.not_null()) { + return unpack(cs_ref.write()) && (cs_ref->empty_ext() || invalidate()); + } else { + return reset(); + } +} + +const ShardAccount t_ShardAccount; + +const AccountStatus t_AccountStatus; + +bool HashmapAugNode::skip(vm::CellSlice& cs) const { + if (n < 0) { + return false; + } else if (!n) { + // ahmn_leaf + return aug.extra_type.skip(cs) && aug.value_type.skip(cs); + } else { + // ahmn_fork + return cs.advance_refs(2) && aug.extra_type.skip(cs); + } +} + +bool HashmapAugNode::validate_skip(vm::CellSlice& cs, bool weak) const { + if (n < 0) { + return false; + } + if (!n) { + // ahmn_leaf + vm::CellSlice cs_extra{cs}; + if (!aug.extra_type.validate_skip(cs, weak)) { + return false; + } + cs_extra.cut_tail(cs); + vm::CellSlice cs_value{cs}; + if (!aug.value_type.validate_skip(cs, weak)) { + return false; + } + cs_value.cut_tail(cs); + return aug.check_leaf(cs_extra, cs_value); + } + // ahmn_fork + if (!cs.have_refs(2)) { + return false; + } + HashmapAug branch_type{n - 1, aug}; + if (!branch_type.validate_ref(cs.prefetch_ref(0), weak) || !branch_type.validate_ref(cs.prefetch_ref(1), weak)) { + return false; + } + auto cs_left = load_cell_slice(cs.fetch_ref()); + auto cs_right = load_cell_slice(cs.fetch_ref()); + vm::CellSlice cs_extra{cs}; + if (!aug.extra_type.validate_skip(cs, weak)) { + return false; + } + cs_extra.cut_tail(cs); + return branch_type.extract_extra(cs_left) && branch_type.extract_extra(cs_right) && + aug.check_fork(cs_extra, cs_left, cs_right); +} + +bool HashmapAug::skip(vm::CellSlice& cs) const { + int l; + return HmLabel{n}.skip(cs, l) && HashmapAugNode{n - l, aug}.skip(cs); +} + +bool HashmapAug::validate_skip(vm::CellSlice& cs, bool weak) const { + int l; + return HmLabel{n}.validate_skip(cs, weak, l) && HashmapAugNode{n - l, aug}.validate_skip(cs, weak); +} + +bool HashmapAug::extract_extra(vm::CellSlice& cs) const { + int l; + return HmLabel{n}.skip(cs, l) && (l == n || cs.advance_refs(2)) && aug.extra_type.extract(cs); +} + +bool HashmapAugE::validate_skip(vm::CellSlice& cs, bool weak) const { + Ref extra; + switch (get_tag(cs)) { + case ahme_empty: + return cs.advance(1) && (extra = root_type.aug.extra_type.validate_fetch(cs, weak)).not_null() && + root_type.aug.check_empty(extra.unique_write()); + case ahme_root: + if (cs.advance(1) && root_type.validate_ref(cs.prefetch_ref(), weak)) { + bool special; + auto cs_root = load_cell_slice_special(cs.fetch_ref(), special); + if (special) { + return weak; + } + return (extra = root_type.aug.extra_type.validate_fetch(cs, weak)).not_null() && + root_type.extract_extra(cs_root) && extra->contents_equal(cs_root); + } + break; + } + return false; +} + +bool HashmapAugE::skip(vm::CellSlice& cs) const { + int tag = (int)cs.fetch_ulong(1); + return tag >= 0 && cs.advance_refs(tag) && root_type.aug.extra_type.skip(cs); +} + +bool HashmapAugE::extract_extra(vm::CellSlice& cs) const { + int tag = (int)cs.fetch_ulong(1); + return tag >= 0 && cs.advance_refs(tag) && root_type.aug.extra_type.extract(cs); +} + +bool DepthBalanceInfo::skip(vm::CellSlice& cs) const { + return cs.advance(5) && + t_CurrencyCollection.skip( + cs); // depth_balance$_ split_depth:(#<= 30) balance:CurrencyCollection = DepthBalanceInfo; +} + +bool DepthBalanceInfo::validate_skip(vm::CellSlice& cs, bool weak) const { + return cs.fetch_ulong(5) <= 30 && t_CurrencyCollection.validate_skip( + cs, weak); // depth_balance$_ split_depth:(#<= 30) balance:CurrencyCollection +} + +bool DepthBalanceInfo::null_value(vm::CellBuilder& cb) const { + return cb.store_zeroes_bool(5) && t_CurrencyCollection.null_value(cb); +} + +bool DepthBalanceInfo::add_values(vm::CellBuilder& cb, vm::CellSlice& cs1, vm::CellSlice& cs2) const { + unsigned d1, d2; + return cs1.fetch_uint_leq(30, d1) && cs2.fetch_uint_leq(30, d2) && cb.store_uint_leq(30, std::max(d1, d2)) && + t_CurrencyCollection.add_values(cb, cs1, cs2); +} + +const DepthBalanceInfo t_DepthBalanceInfo; + +bool Aug_ShardAccounts::eval_leaf(vm::CellBuilder& cb, vm::CellSlice& cs) const { + if (cs.have_refs()) { + auto cs2 = load_cell_slice(cs.prefetch_ref()); + return t_Account.skip_copy_depth_balance(cb, cs2); + } else { + return false; + } +} + +const Aug_ShardAccounts aug_ShardAccounts; + +const ShardAccounts t_ShardAccounts; + +const AccStatusChange t_AccStatusChange; + +bool TrStoragePhase::skip(vm::CellSlice& cs) const { + return t_Grams.skip(cs) // storage_fees_collected:Grams + && t_Maybe_Grams.skip(cs) // storage_fees_due:Grams + && t_AccStatusChange.skip(cs); // status_change:AccStatusChange +} + +bool TrStoragePhase::validate_skip(vm::CellSlice& cs, bool weak) const { + return t_Grams.validate_skip(cs, weak) // storage_fees_collected:Grams + && t_Maybe_Grams.validate_skip(cs, weak) // storage_fees_due:Grams + && t_AccStatusChange.validate_skip(cs, weak); // status_change:AccStatusChange +} + +const TrStoragePhase t_TrStoragePhase; + +bool TrCreditPhase::skip(vm::CellSlice& cs) const { + return t_Maybe_Grams.skip(cs) // due_fees_collected:(Maybe Grams) + && t_CurrencyCollection.skip(cs); // credit:CurrencyCollection +} + +bool TrCreditPhase::validate_skip(vm::CellSlice& cs, bool weak) const { + return t_Maybe_Grams.validate_skip(cs, weak) // due_fees_collected:(Maybe Grams) + && t_CurrencyCollection.validate_skip(cs, weak); // credit:CurrencyCollection +} + +const TrCreditPhase t_TrCreditPhase; + +bool TrComputeInternal1::skip(vm::CellSlice& cs) const { + return t_VarUInteger_7.skip(cs) // gas_used:(VarUInteger 7) + && t_VarUInteger_7.skip(cs) // gas_limit:(VarUInteger 7) + && Maybe{3}.skip(cs) // gas_credit:(Maybe (VarUInteger 3)) + && cs.advance(8 + 32) // mode:int8 exit_code:int32 + && Maybe{32}.skip(cs) // exit_arg:(Maybe int32) + && cs.advance(32 + 256 + 256); // vm_steps:uint32 + // vm_init_state_hash:uint256 + // vm_final_state_hash:uint256 +} + +bool TrComputeInternal1::validate_skip(vm::CellSlice& cs, bool weak) const { + return t_VarUInteger_7.validate_skip(cs, weak) // gas_used:(VarUInteger 7) + && t_VarUInteger_7.validate_skip(cs, weak) // gas_limit:(VarUInteger 7) + && Maybe{3}.validate_skip(cs, weak) // gas_credit:(Maybe (VarUInteger 3)) + && cs.advance(8 + 32) // mode:int8 exit_code:int32 + && Maybe{32}.validate_skip(cs, weak) // exit_arg:(Maybe int32) + && cs.advance(32 + 256 + 256); // vm_steps:uint32 + // vm_init_state_hash:uint256 + // vm_final_state_hash:uint256 +} + +const TrComputeInternal1 t_TrComputeInternal1; +const RefTo t_Ref_TrComputeInternal1; +const ComputeSkipReason t_ComputeSkipReason; + +bool TrComputePhase::skip(vm::CellSlice& cs) const { + switch (get_tag(cs)) { + case tr_phase_compute_skipped: + return cs.advance(1) && t_ComputeSkipReason.skip(cs); + case tr_phase_compute_vm: + return cs.advance(1 + 3) // tr_phase_compute_vm$1 success:Bool msg_state_used:Bool account_activated:Bool + && t_Grams.skip(cs) // gas_fees:Grams + && t_Ref_TrComputeInternal1.skip(cs); // ^[ gas_used:(..) .. ] + } + return false; +} + +bool TrComputePhase::validate_skip(vm::CellSlice& cs, bool weak) const { + switch (get_tag(cs)) { + case tr_phase_compute_skipped: + return cs.advance(1) && t_ComputeSkipReason.validate_skip(cs, weak); + case tr_phase_compute_vm: + return cs.advance(1 + 3) // tr_phase_compute_vm$1 success:Bool msg_state_used:Bool account_activated:Bool + && t_Grams.validate_skip(cs, weak) // gas_fees:Grams + && t_Ref_TrComputeInternal1.validate_skip(cs, weak); // ^[ gas_used:(..) .. ] + } + return false; +} + +const TrComputePhase t_TrComputePhase; + +bool TrActionPhase::skip(vm::CellSlice& cs) const { + return cs.advance(3) // success:Bool valid:Bool no_funds:Bool + && t_AccStatusChange.skip(cs) // status_change:AccStatusChange + && t_Maybe_Grams.skip(cs) // total_fwd_fees:(Maybe Grams) + && t_Maybe_Grams.skip(cs) // total_action_fees:(Maybe Grams) + && cs.advance(32) // result_code:int32 + && Maybe{32}.skip(cs) // result_arg:(Maybe int32) + && cs.advance(16 * 4 + 256) // tot_actions:uint16 spec_actions:uint16 + // skipped_actions:uint16 msgs_created:uint16 + // action_list_hash:uint256 + && t_StorageUsedShort.skip(cs); // tot_msg_size:StorageUsedShort +} + +bool TrActionPhase::validate_skip(vm::CellSlice& cs, bool weak) const { + return cs.advance(3) // success:Bool valid:Bool no_funds:Bool + && t_AccStatusChange.validate_skip(cs, weak) // status_change:AccStatusChange + && t_Maybe_Grams.validate_skip(cs, weak) // total_fwd_fees:(Maybe Grams) + && t_Maybe_Grams.validate_skip(cs, weak) // total_action_fees:(Maybe Grams) + && cs.advance(32) // result_code:int32 + && Maybe{32}.validate_skip(cs, weak) // result_arg:(Maybe int32) + && cs.advance(16 * 4 + 256) // tot_actions:uint16 spec_actions:uint16 + // skipped_actions:uint16 msgs_created:uint16 + // action_list_hash:uint256 + && t_StorageUsedShort.validate_skip(cs, weak); // tot_msg_size:StorageUsed +} + +const TrActionPhase t_TrActionPhase; + +bool TrBouncePhase::skip(vm::CellSlice& cs) const { + switch (get_tag(cs)) { + case tr_phase_bounce_negfunds: + return cs.advance(2); // tr_phase_bounce_negfunds$00 + case tr_phase_bounce_nofunds: + return cs.advance(2) // tr_phase_bounce_nofunds$01 + && t_StorageUsedShort.skip(cs) // msg_size:StorageUsedShort + && t_Grams.skip(cs); // req_fwd_fees:Grams + case tr_phase_bounce_ok: + return cs.advance(1) // tr_phase_bounce_ok$1 + && t_StorageUsedShort.skip(cs) // msg_size:StorageUsedShort + && t_Grams.skip(cs) // msg_fees:Grams + && t_Grams.skip(cs); // fwd_fees:Grams + } + return false; +} + +bool TrBouncePhase::validate_skip(vm::CellSlice& cs, bool weak) const { + switch (get_tag(cs)) { + case tr_phase_bounce_negfunds: + return cs.advance(2); // tr_phase_bounce_negfunds$00 + case tr_phase_bounce_nofunds: + return cs.advance(2) // tr_phase_bounce_nofunds$01 + && t_StorageUsedShort.validate_skip(cs, weak) // msg_size:StorageUsedShort + && t_Grams.validate_skip(cs, weak); // req_fwd_fees:Grams + case tr_phase_bounce_ok: + return cs.advance(1) // tr_phase_bounce_ok$1 + && t_StorageUsedShort.validate_skip(cs, weak) // msg_size:StorageUsedShort + && t_Grams.validate_skip(cs, weak) // msg_fees:Grams + && t_Grams.validate_skip(cs, weak); // fwd_fees:Grams + } + return false; +} + +int TrBouncePhase::get_tag(const vm::CellSlice& cs) const { + if (cs.size() == 1) { + return (int)cs.prefetch_ulong(1) == 1 ? tr_phase_bounce_ok : -1; + } + int v = (int)cs.prefetch_ulong(2); + return v == 3 ? tr_phase_bounce_ok : v; +}; + +const TrBouncePhase t_TrBouncePhase; + +bool SplitMergeInfo::skip(vm::CellSlice& cs) const { + // cur_shard_pfx_len:(## 6) acc_split_depth:(##6) this_addr:uint256 sibling_addr:uint256 + return cs.advance(6 + 6 + 256 + 256); +} + +bool SplitMergeInfo::validate_skip(vm::CellSlice& cs, bool weak) const { + if (!cs.have(6 + 6 + 256 + 256)) { + return false; + } + int cur_pfx_len = (int)cs.fetch_ulong(6); + int split_depth = (int)cs.fetch_ulong(6); + unsigned char this_addr[32], sibling_addr[32]; + if (!cs.fetch_bytes(this_addr, 32) || !cs.fetch_bytes(sibling_addr, 32)) { + return false; + } + // cur_pfx_len < split_depth, addresses match except in bit cur_pfx_len + if (cur_pfx_len >= split_depth) { + return false; + } + sibling_addr[cur_pfx_len >> 3] ^= (unsigned char)(0x80 >> (cur_pfx_len & 7)); + return !std::memcmp(this_addr, sibling_addr, 32); +} + +const SplitMergeInfo t_SplitMergeInfo; + +bool TransactionDescr::skip(vm::CellSlice& cs) const { + switch (get_tag(cs)) { + case trans_ord: + return cs.advance(4 + 1) // trans_ord$0000 storage_first:Bool + && Maybe{}.skip(cs) // storage_ph:(Maybe TrStoragePhase) + && Maybe{}.skip(cs) // credit_ph:(Maybe TrCreditPhase) + && t_TrComputePhase.skip(cs) // compute_ph:TrComputePhase + && Maybe>{}.skip(cs) // action:(Maybe ^TrActionPhase) + && cs.advance(1) // aborted:Bool + && Maybe{}.skip(cs) // bounce:(Maybe TrBouncePhase) + && cs.advance(1); // destroyed:Bool + case trans_storage: + return cs.advance(4) // trans_storage$0001 + && t_TrStoragePhase.skip(cs); // storage_ph:TrStoragePhase + case trans_tick_tock: + return cs.advance(4) // trans_tick_tock$001 is_tock:Bool + && t_TrStoragePhase.skip(cs) // storage:TrStoragePhase + && t_TrComputePhase.skip(cs) // compute_ph:TrComputePhase + && Maybe>{}.skip(cs) // action:(Maybe ^TrActionPhase) + && cs.advance(2); // aborted:Bool destroyed:Bool + case trans_split_prepare: + return cs.advance(4) // trans_split_prepare$0100 + && t_SplitMergeInfo.skip(cs) // split_info:SplitMergeInfo + && t_TrComputePhase.skip(cs) // compute_ph:TrComputePhase + && Maybe>{}.skip(cs) // action:(Maybe ^TrActionPhase) + && cs.advance(2); // aborted:Bool destroyed:Bool + case trans_split_install: + return cs.advance(4) // trans_split_install$0101 + && t_SplitMergeInfo.skip(cs) // split_info:SplitMergeInfo + && t_Ref_Transaction.skip(cs) // prepare_transaction:^Transaction + && cs.advance(1); // installed:Bool + case trans_merge_prepare: + return cs.advance(4) // trans_merge_prepare$0110 + && t_SplitMergeInfo.skip(cs) // split_info:SplitMergeInfo + && t_TrStoragePhase.skip(cs) // storage_ph:TrStoragePhase + && cs.advance(1); // aborted:Bool + case trans_merge_install: + return cs.advance(4) // trans_merge_install$0111 + && t_SplitMergeInfo.skip(cs) // split_info:SplitMergeInfo + && t_Ref_Transaction.skip(cs) // prepare_transaction:^Transaction + && Maybe{}.skip(cs) // credit_ph:(Maybe TrCreditPhase) + && Maybe{}.skip(cs) // compute_ph:TrComputePhase + && Maybe>{}.skip(cs) // action:(Maybe ^TrActionPhase) + && cs.advance(2); // aborted:Bool destroyed:Bool + } + return false; +} + +bool TransactionDescr::validate_skip(vm::CellSlice& cs, bool weak) const { + switch (get_tag(cs)) { + case trans_ord: + return cs.advance(4 + 1) // trans_ord$0000 credit_first:Bool + && Maybe{}.validate_skip(cs, weak) // storage_ph:(Maybe TrStoragePhase) + && Maybe{}.validate_skip(cs, weak) // credit_ph:(Maybe TrCreditPhase) + && t_TrComputePhase.validate_skip(cs, weak) // compute_ph:TrComputePhase + && Maybe>{}.validate_skip(cs, weak) // action:(Maybe ^TrActionPhase) + && cs.advance(1) // aborted:Bool + && Maybe{}.validate_skip(cs, weak) // bounce:(Maybe TrBouncePhase) + && cs.advance(1); // destroyed:Bool + case trans_storage: + return cs.advance(4) // trans_storage$0001 + && t_TrStoragePhase.validate_skip(cs, weak); // storage_ph:TrStoragePhase + case trans_tick_tock: + return cs.advance(4) // trans_tick_tock$001 is_tock:Bool + && t_TrStoragePhase.validate_skip(cs, weak) // storage:TrStoragePhase + && t_TrComputePhase.validate_skip(cs, weak) // compute_ph:TrComputePhase + && Maybe>{}.validate_skip(cs, weak) // action:(Maybe ^TrActionPhase) + && cs.advance(2); // aborted:Bool destroyed:Bool + case trans_split_prepare: + return cs.advance(4) // trans_split_prepare$0100 + && t_SplitMergeInfo.validate_skip(cs, weak) // split_info:SplitMergeInfo + && t_TrComputePhase.validate_skip(cs, weak) // compute_ph:TrComputePhase + && Maybe>{}.validate_skip(cs, weak) // action:(Maybe ^TrActionPhase) + && cs.advance(2); // aborted:Bool destroyed:Bool + case trans_split_install: + return cs.advance(4) // trans_split_install$0101 + && t_SplitMergeInfo.validate_skip(cs, weak) // split_info:SplitMergeInfo + && t_Ref_Transaction.validate_skip(cs, weak) // prepare_transaction:^Transaction + && cs.advance(1); // installed:Bool + case trans_merge_prepare: + return cs.advance(4) // trans_merge_prepare$0110 + && t_SplitMergeInfo.validate_skip(cs, weak) // split_info:SplitMergeInfo + && t_TrStoragePhase.validate_skip(cs, weak) // storage_ph:TrStoragePhase + && cs.advance(1); // aborted:Bool + case trans_merge_install: + return cs.advance(4) // trans_merge_install$0111 + && t_SplitMergeInfo.validate_skip(cs, weak) // split_info:SplitMergeInfo + && t_Ref_Transaction.validate_skip(cs, weak) // prepare_transaction:^Transaction + && Maybe{}.validate_skip(cs, weak) // credit_ph:(Maybe TrCreditPhase) + && Maybe{}.validate_skip(cs, weak) // compute_ph:TrComputePhase + && Maybe>{}.validate_skip(cs, weak) // action:(Maybe ^TrActionPhase) + && cs.advance(2); // aborted:Bool destroyed:Bool + } + return false; +} + +int TransactionDescr::get_tag(const vm::CellSlice& cs) const { + int t = (int)cs.prefetch_ulong(4); + return (t >= 0 && t <= 7) ? (t == 3 ? 2 : t) : -1; +} + +const TransactionDescr t_TransactionDescr; + +bool Transaction_aux::skip(vm::CellSlice& cs) const { + return Maybe>{}.skip(cs) // in_msg:(Maybe ^Message) + && HashmapE{15, t_Ref_Message}.skip(cs); // out_msgs:(HashmapE 15 ^Message) +} + +bool Transaction_aux::validate_skip(vm::CellSlice& cs, bool weak) const { + return Maybe>{}.validate_skip(cs, weak) // in_msg:(Maybe ^Message) + && HashmapE{15, t_Ref_Message}.validate_skip(cs, weak); // out_msgs:(HashmapE 15 ^Message) +} + +const Transaction_aux t_Transaction_aux; + +bool Transaction::skip(vm::CellSlice& cs) const { + return cs.advance( + 4 + 256 + 64 + 256 + 64 + 32 + + 15) // transaction$0111 account_addr:uint256 lt:uint64 prev_trans_hash:bits256 prev_trans_lt:uint64 now:uint32 outmsg_cnt:uint15 + && t_AccountStatus.skip(cs) // orig_status:AccountStatus + && t_AccountStatus.skip(cs) // end_status:AccountStatus + && cs.advance_refs(1) // ^[ in_msg:(Maybe ^Message) out_msgs:(HashmapE 15 ^Message) ] + && t_CurrencyCollection.skip(cs) // total_fees:CurrencyCollection + && cs.advance_refs(1) // state_update:^(MERKLE_UPDATE Account) + && RefTo{}.skip(cs); // description:^TransactionDescr +} + +bool Transaction::validate_skip(vm::CellSlice& cs, bool weak) const { + return cs.fetch_ulong(4) == 7 // transaction$0111 + && + cs.advance( + 256 + 64 + 256 + 64 + 32 + + 15) // account_addr:uint256 lt:uint64 prev_trans_hash:bits256 prev_trans_lt:uint64 now:uint32 outmsg_cnt:uint15 + && t_AccountStatus.validate_skip(cs, weak) // orig_status:AccountStatus + && t_AccountStatus.validate_skip(cs, weak) // end_status:AccountStatus + && RefTo{}.validate_skip(cs, weak) // ^[ in_msg:... out_msgs:... ] + && t_CurrencyCollection.validate_skip(cs, weak) // total_fees:CurrencyCollection + && t_Ref_HashUpdate.validate_skip(cs, weak) // state_update:^(HASH_UPDATE Account) + && RefTo{}.validate_skip(cs, weak); // description:^TransactionDescr +} + +bool Transaction::get_total_fees(vm::CellSlice&& cs, block::CurrencyCollection& total_fees) const { + return cs.is_valid() && cs.fetch_ulong(4) == 7 // transaction$0111 + && + cs.advance( + 256 + 64 + 256 + 64 + 32 + + 15) // account_addr:uint256 lt:uint64 prev_trans_hash:bits256 prev_trans_lt:uint64 now:uint32 outmsg_cnt:uint15 + && t_AccountStatus.skip(cs) // orig_status:AccountStatus + && t_AccountStatus.skip(cs) // end_status:AccountStatus + && cs.advance_refs(1) // ^[ in_msg:... out_msg:... ] + && total_fees.fetch(cs); // total_fees:CurrencyCollection +} + +const Transaction t_Transaction; +const RefTo t_Ref_Transaction; + +// leaf evaluation for (HashmapAug 64 ^Transaction CurrencyCollection) +bool Aug_AccountTransactions::eval_leaf(vm::CellBuilder& cb, vm::CellSlice& cs) const { + auto cell_ref = cs.prefetch_ref(); + block::CurrencyCollection total_fees; + return cell_ref.not_null() && t_Transaction.get_total_fees(vm::load_cell_slice(std::move(cell_ref)), total_fees) && + total_fees.store(cb); +} + +const Aug_AccountTransactions aug_AccountTransactions; +const HashmapAug t_AccountTransactions{64, aug_AccountTransactions}; + +const HashUpdate t_HashUpdate; +const RefTo t_Ref_HashUpdate; + +bool AccountBlock::skip(vm::CellSlice& cs) const { + return cs.advance(4 + 256) // acc_trans#5 account_addr:bits256 + && t_AccountTransactions.skip(cs) // transactions:(HashmapAug 64 ^Transaction CurrencyCollection) + && cs.advance_refs(1); // state_update:^(HASH_UPDATE Account) +} + +bool AccountBlock::validate_skip(vm::CellSlice& cs, bool weak) const { + return cs.fetch_ulong(4) == 5 // acc_trans#5 + && cs.advance(256) // account_addr:bits256 + && + t_AccountTransactions.validate_skip(cs, weak) // transactions:(HashmapAug 64 ^Transaction CurrencyCollection) + && t_Ref_HashUpdate.validate_skip(cs, weak); // state_update:^(HASH_UPDATE Account) +} + +bool AccountBlock::get_total_fees(vm::CellSlice&& cs, block::CurrencyCollection& total_fees) const { + return cs.advance(4 + 256) // acc_trans#5 account_addr:bits256 + && t_AccountTransactions.extract_extra(cs) // transactions:(HashmapAug 64 ^Transaction Grams) + && total_fees.fetch(cs); +} + +const AccountBlock t_AccountBlock; + +bool Aug_ShardAccountBlocks::eval_leaf(vm::CellBuilder& cb, vm::CellSlice& cs) const { + block::CurrencyCollection total_fees; + return t_AccountBlock.get_total_fees(std::move(cs), total_fees) && total_fees.store(cb); +} + +const Aug_ShardAccountBlocks aug_ShardAccountBlocks; +const HashmapAugE t_ShardAccountBlocks{256, + aug_ShardAccountBlocks}; // (HashmapAugE 256 AccountBlock CurrencyCollection) + +bool ImportFees::validate_skip(vm::CellSlice& cs, bool weak) const { + return t_Grams.validate_skip(cs, weak) && t_CurrencyCollection.validate_skip(cs, weak); +} + +bool ImportFees::skip(vm::CellSlice& cs) const { + return t_Grams.skip(cs) && t_CurrencyCollection.skip(cs); +} + +bool ImportFees::add_values(vm::CellBuilder& cb, vm::CellSlice& cs1, vm::CellSlice& cs2) const { + return t_Grams.add_values(cb, cs1, cs2) && t_CurrencyCollection.add_values(cb, cs1, cs2); +} + +const ImportFees t_ImportFees; + +bool InMsg::skip(vm::CellSlice& cs) const { + switch (get_tag(cs)) { + case msg_import_ext: + return cs.advance(3) // msg_import_ext$000 + && t_Ref_Message.skip(cs) // msg:^Message + && t_Ref_Transaction.skip(cs); // transaction:^Transaction + case msg_import_ihr: + return cs.advance(3) // msg_import_ihr$010 + && t_Ref_Message.skip(cs) // msg:^Message + && t_Ref_Transaction.skip(cs) // transaction:^Transaction + && t_Grams.skip(cs) // ihr_fee:Grams + && t_RefCell.skip(cs); // proof_created:^Cell + case msg_import_imm: + return cs.advance(3) // msg_import_imm$011 + && t_Ref_MsgEnvelope.skip(cs) // in_msg:^MsgEnvelope + && t_Ref_Transaction.skip(cs) // transaction:^Transaction + && t_Grams.skip(cs); // fwd_fee:Grams + case msg_import_fin: + return cs.advance(3) // msg_import_fin$100 + && t_Ref_MsgEnvelope.skip(cs) // in_msg:^MsgEnvelope + && t_Ref_Transaction.skip(cs) // transaction:^Transaction + && t_Grams.skip(cs); // fwd_fee:Grams + case msg_import_tr: + return cs.advance(3) // msg_import_tr$101 + && t_Ref_MsgEnvelope.skip(cs) // in_msg:^MsgEnvelope + && t_Ref_MsgEnvelope.skip(cs) // out_msg:^MsgEnvelope + && t_Grams.skip(cs); // transit_fee:Grams + case msg_discard_fin: + return cs.advance(3) // msg_discard_fin$110 + && t_Ref_MsgEnvelope.skip(cs) // in_msg:^MsgEnvelope + && cs.advance(64) // transaction_id:uint64 + && t_Grams.skip(cs); // fwd_fee:Grams + case msg_discard_tr: + return cs.advance(3) // msg_discard_tr$111 + && t_Ref_MsgEnvelope.skip(cs) // in_msg:^MsgEnvelope + && cs.advance(64) // transaction_id:uint64 + && t_Grams.skip(cs) // fwd_fee:Grams + && t_RefCell.skip(cs); // proof_delivered:^Cell + } + return false; +} + +bool InMsg::validate_skip(vm::CellSlice& cs, bool weak) const { + switch (get_tag(cs)) { + case msg_import_ext: + return cs.advance(3) // msg_import_ext$000 + && t_Ref_Message.validate_skip(cs, weak) // msg:^Message + && t_Ref_Transaction.validate_skip(cs, weak); // transaction:^Transaction + case msg_import_ihr: + return cs.advance(3) // msg_import_ihr$010 + && t_Ref_Message.validate_skip(cs, weak) // msg:^Message + && t_Ref_Transaction.validate_skip(cs, weak) // transaction:^Transaction + && t_Grams.validate_skip(cs, weak) // ihr_fee:Grams + && t_RefCell.validate_skip(cs, weak); // proof_created:^Cell + case msg_import_imm: + return cs.advance(3) // msg_import_imm$011 + && t_Ref_MsgEnvelope.validate_skip(cs, weak) // in_msg:^MsgEnvelope + && t_Ref_Transaction.validate_skip(cs, weak) // transaction:^Transaction + && t_Grams.validate_skip(cs, weak); // fwd_fee:Grams + case msg_import_fin: + return cs.advance(3) // msg_import_fin$100 + && t_Ref_MsgEnvelope.validate_skip(cs, weak) // in_msg:^MsgEnvelope + && t_Ref_Transaction.validate_skip(cs, weak) // transaction:^Transaction + && t_Grams.validate_skip(cs, weak); // fwd_fee:Grams + case msg_import_tr: + return cs.advance(3) // msg_import_tr$101 + && t_Ref_MsgEnvelope.validate_skip(cs, weak) // in_msg:^MsgEnvelope + && t_Ref_MsgEnvelope.validate_skip(cs, weak) // out_msg:^MsgEnvelope + && t_Grams.validate_skip(cs, weak); // transit_fee:Grams + case msg_discard_fin: + return cs.advance(3) // msg_discard_fin$110 + && t_Ref_MsgEnvelope.validate_skip(cs, weak) // in_msg:^MsgEnvelope + && cs.advance(64) // transaction_id:uint64 + && t_Grams.validate_skip(cs, weak); // fwd_fee:Grams + case msg_discard_tr: + return cs.advance(3) // msg_discard_tr$111 + && t_Ref_MsgEnvelope.validate_skip(cs, weak) // in_msg:^MsgEnvelope + && cs.advance(64) // transaction_id:uint64 + && t_Grams.validate_skip(cs, weak) // fwd_fee:Grams + && t_RefCell.validate_skip(cs, weak); // proof_delivered:^Cell + } + return false; +} + +bool InMsg::get_import_fees(vm::CellBuilder& cb, vm::CellSlice& cs) const { + switch (get_tag(cs)) { + case msg_import_ext: // inbound external message + return t_ImportFees.null_value(cb); // external messages have no value and no import fees + case msg_import_ihr: // IHR-forwarded internal message to its final destination + if (cs.advance(3) && cs.size_refs() >= 3) { + auto msg_cs = load_cell_slice(cs.fetch_ref()); + CommonMsgInfo::Record_int_msg_info msg_info; + td::RefInt256 ihr_fee; + vm::CellBuilder aux; + // sort of Prolog-style in C++ + return t_Message.extract_info(msg_cs) && t_CommonMsgInfo.unpack(msg_cs, msg_info) && + cs.fetch_ref().not_null() && (ihr_fee = t_Grams.as_integer_skip(cs)).not_null() && + cs.fetch_ref().not_null() && !cmp(ihr_fee, t_Grams.as_integer(*msg_info.ihr_fee)) && + cb.append_cellslice_bool(msg_info.ihr_fee) // fees_collected := ihr_fee + && aux.append_cellslice_bool(msg_info.ihr_fee) && t_ExtraCurrencyCollection.null_value(aux) && + t_CurrencyCollection.add_values(cb, aux.as_cellslice_ref().write(), + msg_info.value.write()); // value_imported := ihr_fee + value + } + return false; + case msg_import_imm: // internal message re-imported from this very block + if (cs.advance(3) && cs.size_refs() >= 2) { + return cs.fetch_ref().not_null() && cs.fetch_ref().not_null() && + cb.append_cellslice_bool(t_Grams.fetch(cs)) // fees_collected := fwd_fees + && t_CurrencyCollection.null_value(cb); // value_imported := 0 + } + return false; + case msg_import_fin: // internal message delivered to its final destination in this block + if (cs.advance(3) && cs.size_refs() >= 2) { + auto msg_env_cs = load_cell_slice(cs.fetch_ref()); + MsgEnvelope::Record in_msg; + td::RefInt256 fwd_fee, fwd_fee_remaining, value_grams, ihr_fee; + if (!(t_MsgEnvelope.unpack(msg_env_cs, in_msg) && cs.fetch_ref().not_null() && + t_Grams.as_integer_skip_to(cs, fwd_fee) && + (fwd_fee_remaining = t_Grams.as_integer(in_msg.fwd_fee_remaining)).not_null() && + !(cmp(fwd_fee, fwd_fee_remaining)))) { + return false; + } + auto msg_cs = load_cell_slice(std::move(in_msg.msg)); + CommonMsgInfo::Record_int_msg_info msg_info; + return t_Message.extract_info(msg_cs) && t_CommonMsgInfo.unpack(msg_cs, msg_info) && + cb.append_cellslice_bool(in_msg.fwd_fee_remaining) // fees_collected := fwd_fee_remaining + && t_Grams.as_integer_skip_to(msg_info.value.write(), value_grams) && + (ihr_fee = t_Grams.as_integer(std::move(msg_info.ihr_fee))).not_null() && + t_Grams.store_integer_ref(cb, value_grams + ihr_fee + fwd_fee_remaining) && + cb.append_cellslice_bool( + msg_info.value.write()); // value_imported = msg.value + msg.ihr_fee + fwd_fee_remaining + } + return false; + case msg_import_tr: // transit internal message + if (cs.advance(3) && cs.size_refs() >= 2) { + auto msg_env_cs = load_cell_slice(cs.fetch_ref()); + MsgEnvelope::Record in_msg; + td::RefInt256 transit_fee, fwd_fee_remaining, value_grams, ihr_fee; + if (!(t_MsgEnvelope.unpack(msg_env_cs, in_msg) && cs.fetch_ref().not_null() && + t_Grams.as_integer_skip_to(cs, transit_fee) && + (fwd_fee_remaining = t_Grams.as_integer(in_msg.fwd_fee_remaining)).not_null() && + cmp(transit_fee, fwd_fee_remaining) <= 0)) { + return false; + } + auto msg_cs = load_cell_slice(in_msg.msg); + CommonMsgInfo::Record_int_msg_info msg_info; + return t_Message.extract_info(msg_cs) && t_CommonMsgInfo.unpack(msg_cs, msg_info) && + t_Grams.store_integer_ref(cb, std::move(transit_fee)) // fees_collected := transit_fees + && t_Grams.as_integer_skip_to(msg_info.value.write(), value_grams) && + (ihr_fee = t_Grams.as_integer(std::move(msg_info.ihr_fee))).not_null() && + t_Grams.store_integer_ref(cb, value_grams + ihr_fee + fwd_fee_remaining) && + cb.append_cellslice_bool( + msg_info.value.write()); // value_imported = msg.value + msg.ihr_fee + fwd_fee_remaining + } + return false; + case msg_discard_fin: // internal message discarded at its final destination because of previous IHR delivery + if (cs.advance(3) && cs.size_refs() >= 1) { + Ref fwd_fee; + return cs.fetch_ref().not_null() && cs.advance(64) && (fwd_fee = t_Grams.fetch(cs)).not_null() && + cb.append_cellslice_bool(fwd_fee) // fees_collected := fwd_fee + && cb.append_cellslice_bool(std::move(fwd_fee)) && + t_ExtraCurrencyCollection.null_value(cb); // value_imported := fwd_fee + } + return false; + case msg_discard_tr: // internal message discarded at an intermediate destination + if (cs.advance(3) && cs.size_refs() >= 2) { + Ref fwd_fee; + return cs.fetch_ref().not_null() && cs.advance(64) && (fwd_fee = t_Grams.fetch(cs)).not_null() && + cs.fetch_ref().not_null() && cb.append_cellslice_bool(fwd_fee) // fees_collected := fwd_fee + && cb.append_cellslice_bool(std::move(fwd_fee)) && + t_ExtraCurrencyCollection.null_value(cb); // value_imported := fwd_fee + } + return false; + } + return false; +} + +const InMsg t_InMsg; + +const Aug_InMsgDescr aug_InMsgDescr; +const InMsgDescr t_InMsgDescr; + +bool OutMsg::skip(vm::CellSlice& cs) const { + switch (get_tag(cs)) { + case msg_export_ext: + return cs.advance(3) // msg_export_ext$000 + && t_Ref_Message.skip(cs) // msg:^Message + && t_Ref_Transaction.skip(cs); // transaction:^Transaction + case msg_export_imm: + return cs.advance(3) // msg_export_imm$010 + && t_Ref_MsgEnvelope.skip(cs) // out_msg:^MsgEnvelope + && t_Ref_Transaction.skip(cs) // transaction:^Transaction + && RefTo{}.skip(cs); // reimport:^InMsg + case msg_export_new: + return cs.advance(3) // msg_export_new$001 + && t_Ref_MsgEnvelope.skip(cs) // out_msg:^MsgEnvelope + && t_Ref_Transaction.skip(cs); // transaction:^Transaction + case msg_export_tr: + return cs.advance(3) // msg_export_tr$011 + && t_Ref_MsgEnvelope.skip(cs) // out_msg:^MsgEnvelope + && RefTo{}.skip(cs); // imported:^InMsg + case msg_export_deq_imm: + return cs.advance(3) // msg_export_deq_imm$100 + && t_Ref_MsgEnvelope.skip(cs) // out_msg:^MsgEnvelope + && RefTo{}.skip(cs); // reimport:^InMsg + case msg_export_deq: + return cs.advance(3) // msg_export_deq$110 + && t_Ref_MsgEnvelope.skip(cs) // out_msg:^MsgEnvelope + && cs.advance(64); // import_block_lt:uint64 + case msg_export_tr_req: + return cs.advance(3) // msg_export_tr_req$111 + && t_Ref_MsgEnvelope.skip(cs) // out_msg:^MsgEnvelope + && RefTo{}.skip(cs); // imported:^InMsg + } + return false; +} + +bool OutMsg::validate_skip(vm::CellSlice& cs, bool weak) const { + switch (get_tag(cs)) { + case msg_export_ext: + return cs.advance(3) // msg_export_ext$000 + && t_Ref_Message.validate_skip(cs, weak) // msg:^Message + && t_Ref_Transaction.validate_skip(cs, weak); // transaction:^Transaction + case msg_export_imm: + return cs.advance(3) // msg_export_imm$010 + && t_Ref_MsgEnvelope.validate_skip(cs, weak) // out_msg:^MsgEnvelope + && t_Ref_Transaction.validate_skip(cs, weak) // transaction:^Transaction + && RefTo{}.validate_skip(cs, weak); // reimport:^InMsg + case msg_export_new: + return cs.advance(3) // msg_export_new$001 + && t_Ref_MsgEnvelope.validate_skip(cs, weak) // out_msg:^MsgEnvelope + && t_Ref_Transaction.validate_skip(cs, weak); // transaction:^Transaction + case msg_export_tr: + return cs.advance(3) // msg_export_tr$011 + && t_Ref_MsgEnvelope.validate_skip(cs, weak) // out_msg:^MsgEnvelope + && RefTo{}.validate_skip(cs, weak); // imported:^InMsg + case msg_export_deq_imm: + return cs.advance(3) // msg_export_deq_imm$100 + && t_Ref_MsgEnvelope.validate_skip(cs, weak) // out_msg:^MsgEnvelope + && RefTo{}.validate_skip(cs, weak); // reimport:^InMsg + case msg_export_deq: + return cs.advance(3) // msg_export_deq$110 + && t_Ref_MsgEnvelope.validate_skip(cs, weak) // out_msg:^MsgEnvelope + && cs.advance(64); // import_block_lt:uint64 + case msg_export_tr_req: + return cs.advance(3) // msg_export_tr_req$111 + && t_Ref_MsgEnvelope.validate_skip(cs, weak) // out_msg:^MsgEnvelope + && RefTo{}.validate_skip(cs, weak); // imported:^InMsg + } + return false; +} + +bool OutMsg::get_export_value(vm::CellBuilder& cb, vm::CellSlice& cs) const { + switch (get_tag(cs)) { + case msg_export_ext: // external outbound message carries no value + if (cs.have(3, 2)) { + return t_CurrencyCollection.null_value(cb); + } + return false; + case msg_export_imm: // outbound internal message delivered in this very block, no value exported + return cs.have(3, 3) && t_CurrencyCollection.null_value(cb); + case msg_export_deq_imm: // dequeuing record for outbound message delivered in this very block, no value exported + return cs.have(3, 2) && t_CurrencyCollection.null_value(cb); + case msg_export_deq: // dequeueing record for outbound message, no exported value + return cs.have(3, 1) && t_CurrencyCollection.null_value(cb); + case msg_export_new: // newly-generated outbound internal message, queued + case msg_export_tr: // transit internal message, queued + case msg_export_tr_req: // transit internal message, re-queued from this shardchain + if (cs.advance(3) && cs.size_refs() >= 2) { + auto msg_env_cs = load_cell_slice(cs.fetch_ref()); + MsgEnvelope::Record out_msg; + if (!(cs.fetch_ref().not_null() && t_MsgEnvelope.unpack(msg_env_cs, out_msg))) { + return false; + } + auto msg_cs = load_cell_slice(std::move(out_msg.msg)); + CommonMsgInfo::Record_int_msg_info msg_info; + td::RefInt256 value_grams, ihr_fee, fwd_fee_remaining; + return t_Message.extract_info(msg_cs) && t_CommonMsgInfo.unpack(msg_cs, msg_info) && + (value_grams = t_Grams.as_integer_skip(msg_info.value.write())).not_null() && + (ihr_fee = t_Grams.as_integer(std::move(msg_info.ihr_fee))).not_null() && + (fwd_fee_remaining = t_Grams.as_integer(out_msg.fwd_fee_remaining)).not_null() && + t_Grams.store_integer_ref(cb, value_grams + ihr_fee + fwd_fee_remaining) && + cb.append_cellslice_bool(std::move(msg_info.value)); + // exported value = msg.value + msg.ihr_fee + fwd_fee_remaining + } + return false; + } + return false; +} + +bool OutMsg::get_created_lt(vm::CellSlice& cs, unsigned long long& created_lt) const { + switch (get_tag(cs)) { + case msg_export_ext: + if (cs.have(3, 1)) { + auto msg_cs = load_cell_slice(cs.prefetch_ref()); + return t_Message.get_created_lt(msg_cs, created_lt); + } else { + return false; + } + case msg_export_imm: + case msg_export_new: + case msg_export_tr: + case msg_export_deq: + case msg_export_deq_imm: + case msg_export_tr_req: + if (cs.have(3, 1)) { + auto out_msg_cs = load_cell_slice(cs.prefetch_ref()); + return t_MsgEnvelope.get_created_lt(out_msg_cs, created_lt); + } else { + return false; + } + } + return false; +} + +const OutMsg t_OutMsg; + +const Aug_OutMsgDescr aug_OutMsgDescr; +const OutMsgDescr t_OutMsgDescr; + +bool EnqueuedMsg::validate_skip(vm::CellSlice& cs, bool weak) const { + return cs.advance(64) && t_Ref_MsgEnvelope.validate_skip(cs, weak); +} + +const EnqueuedMsg t_EnqueuedMsg; + +bool Aug_OutMsgQueue::eval_fork(vm::CellBuilder& cb, vm::CellSlice& left_cs, vm::CellSlice& right_cs) const { + unsigned long long x, y; + return left_cs.fetch_ulong_bool(64, x) && right_cs.fetch_ulong_bool(64, y) && + cb.store_ulong_rchk_bool(std::min(x, y), 64); +} + +bool Aug_OutMsgQueue::eval_empty(vm::CellBuilder& cb) const { + return cb.store_long_bool(0, 64); +} + +bool Aug_OutMsgQueue::eval_leaf(vm::CellBuilder& cb, vm::CellSlice& cs) const { + Ref msg_env; + unsigned long long created_lt; + return cs.fetch_ref_to(msg_env) && t_MsgEnvelope.get_created_lt(load_cell_slice(std::move(msg_env)), created_lt) && + cb.store_ulong_rchk_bool(created_lt, 64); +} + +const Aug_OutMsgQueue aug_OutMsgQueue; +const OutMsgQueue t_OutMsgQueue; + +const ProcessedUpto t_ProcessedUpto; +const HashmapE t_ProcessedInfo{96, t_ProcessedUpto}; +const HashmapE t_IhrPendingInfo{256, t_uint128}; + +// _ out_queue:OutMsgQueue proc_info:ProcessedInfo = OutMsgQueueInfo; +bool OutMsgQueueInfo::skip(vm::CellSlice& cs) const { + return t_OutMsgQueue.skip(cs) && t_ProcessedInfo.skip(cs) && t_IhrPendingInfo.skip(cs); +} + +bool OutMsgQueueInfo::validate_skip(vm::CellSlice& cs, bool weak) const { + return t_OutMsgQueue.validate_skip(cs, weak) && t_ProcessedInfo.validate_skip(cs, weak) && + t_IhrPendingInfo.validate_skip(cs, weak); +} + +const OutMsgQueueInfo t_OutMsgQueueInfo; +const RefTo t_Ref_OutMsgQueueInfo; + +bool ExtBlkRef::unpack(vm::CellSlice& cs, ton::BlockIdExt& blkid, ton::LogicalTime* end_lt) const { + block::gen::ExtBlkRef::Record data; + if (!tlb::unpack(cs, data)) { + blkid.invalidate(); + return false; + } + blkid.id = ton::BlockId{ton::masterchainId, ton::shardIdAll, data.seq_no}; + blkid.root_hash = data.root_hash; + blkid.file_hash = data.file_hash; + if (end_lt) { + *end_lt = data.end_lt; + } + return true; +} + +bool ExtBlkRef::unpack(Ref cs_ref, ton::BlockIdExt& blkid, ton::LogicalTime* end_lt) const { + block::gen::ExtBlkRef::Record data; + if (!tlb::csr_unpack_safe(std::move(cs_ref), data)) { + blkid.invalidate(); + return false; + } + blkid.id = ton::BlockId{ton::masterchainId, ton::shardIdAll, data.seq_no}; + blkid.root_hash = data.root_hash; + blkid.file_hash = data.file_hash; + if (end_lt) { + *end_lt = data.end_lt; + } + return true; +} + +const ExtBlkRef t_ExtBlkRef; +const BlkMasterInfo t_BlkMasterInfo; + +bool ShardIdent::validate_skip(vm::CellSlice& cs, bool weak) const { + int shard_pfx_len, workchain_id; + unsigned long long shard_pfx; + if (cs.fetch_ulong(2) == 0 && cs.fetch_uint_to(6, shard_pfx_len) && cs.fetch_int_to(32, workchain_id) && + workchain_id != ton::workchainInvalid && cs.fetch_uint_to(64, shard_pfx)) { + auto pow2 = (1ULL << (63 - shard_pfx_len)); + if (!(shard_pfx & (pow2 - 1))) { + return true; + } + } + return false; +} + +bool ShardIdent::Record::check() const { + return workchain_id != ton::workchainInvalid && !(shard_prefix & ((1ULL << (63 - shard_pfx_bits)) - 1)); +} + +bool ShardIdent::unpack(vm::CellSlice& cs, ShardIdent::Record& data) const { + if (cs.fetch_ulong(2) == 0 && cs.fetch_uint_to(6, data.shard_pfx_bits) && cs.fetch_int_to(32, data.workchain_id) && + cs.fetch_uint_to(64, data.shard_prefix) && data.check()) { + return true; + } else { + data.invalidate(); + return false; + } +} + +bool ShardIdent::pack(vm::CellBuilder& cb, const Record& data) const { + return data.check() && cb.store_ulong_rchk_bool(0, 2) && cb.store_ulong_rchk_bool(data.shard_pfx_bits, 6) && + cb.store_long_rchk_bool(data.workchain_id, 32) && cb.store_ulong_rchk_bool(data.shard_prefix, 64); +} + +bool ShardIdent::unpack(vm::CellSlice& cs, ton::WorkchainId& workchain, ton::ShardId& shard) const { + int bits; + unsigned long long pow2; + auto assign = [](auto& a, auto b) { return a = b; }; + auto assign_or = [](auto& a, auto b) { return a |= b; }; + return cs.fetch_ulong(2) == 0 // shard_ident$00 + && cs.fetch_uint_leq(60, bits) // shard_pfx_bits:(#<= 60) + && assign(pow2, (1ULL << (63 - bits))) // (power) + && cs.fetch_int_to(32, workchain) // workchain_id:int32 + && cs.fetch_uint_to(64, shard) // shard_prefix:uint64 + && workchain != ton::workchainInvalid && !(shard & (2 * pow2 - 1)) && assign_or(shard, pow2); +} + +bool ShardIdent::unpack(vm::CellSlice& cs, ton::ShardIdFull& data) const { + return unpack(cs, data.workchain, data.shard); +} + +bool ShardIdent::pack(vm::CellBuilder& cb, ton::WorkchainId workchain, ton::ShardId shard) const { + int bits = ton::shard_prefix_length(shard); + return workchain != ton::workchainInvalid // check workchain + && shard // check shard + && cb.store_long_bool(0, 2) // shard_ident$00 + && cb.store_uint_leq(60, bits) // shard_pfx_bits:(#<= 60) + && cb.store_long_bool(workchain, 32) // workchain_id:int32 + && cb.store_long_bool(shard & (shard - 1), 64); // shard_prefix:uint64 +} + +bool ShardIdent::pack(vm::CellBuilder& cb, ton::ShardIdFull data) const { + return pack(cb, data.workchain, data.shard); +} + +const ShardIdent t_ShardIdent; + +bool BlockIdExt::validate_skip(vm::CellSlice& cs, bool weak) const { + return t_ShardIdent.validate_skip(cs, weak) && cs.advance(32 + 256 * 2); +} + +bool BlockIdExt::unpack(vm::CellSlice& cs, ton::BlockIdExt& data) const { + return t_ShardIdent.unpack(cs, data.id.workchain, data.id.shard) // block_id_ext$_ shard_id:ShardIdent + && cs.fetch_uint_to(32, data.id.seqno) // seq_no:uint32 + && cs.fetch_bits_to(data.root_hash) // root_hash:bits256 + && cs.fetch_bits_to(data.file_hash); // file_hash:bits256 +} + +bool BlockIdExt::pack(vm::CellBuilder& cb, const ton::BlockIdExt& data) const { + return t_ShardIdent.pack(cb, data.id.workchain, data.id.shard) // block_id_ext$_ shard_id:ShardIdent + && cb.store_long_bool(data.id.seqno, 32) // seq_no:uint32 + && cb.store_bits_bool(data.root_hash) // root_hash:bits256 + && cb.store_bits_bool(data.file_hash); // file_hash:bits256 +} + +const BlockIdExt t_BlockIdExt; + +bool ShardState::skip(vm::CellSlice& cs) const { + return get_tag(cs) == shard_state && cs.advance(64) // shard_state#9023afe2 blockchain_id:int32 + && t_ShardIdent.skip(cs) // shard_id:ShardIdent + && cs.advance(32 + 32 + 32 + 64 + + 32) // seq_no:int32 vert_seq_no:# gen_utime:uint32 gen_lt:uint64 min_ref_mc_seqno:uint32 + && t_Ref_OutMsgQueueInfo.skip(cs) // out_msg_queue_info:^OutMsgQueueInfo + && cs.advance(1) // before_split:Bool + && cs.advance_refs(1) // accounts:^ShardAccounts + && + cs.advance_refs( + 1) // ^[ total_balance:CurrencyCollection total_validator_fees:CurrencyCollection libraries:(HashmapE 256 LibDescr) master_ref:(Maybe BlkMasterInfo) ] + && Maybe>{}.skip(cs); // custom:(Maybe ^McStateExtra) +} + +bool ShardState::validate_skip(vm::CellSlice& cs, bool weak) const { + int seq_no; + return get_tag(cs) == shard_state && cs.advance(64) // shard_state#9023afe2 blockchain_id:int32 + && t_ShardIdent.validate_skip(cs, weak) // shard_id:ShardIdent + && cs.fetch_int_to(32, seq_no) // seq_no:int32 + && seq_no >= -1 // { seq_no >= -1 } + && cs.advance(32 + 32 + 64 + 32) // vert_seq_no:# gen_utime:uint32 gen_lt:uint64 min_ref_mc_seqno:uint32 + && t_Ref_OutMsgQueueInfo.validate_skip(cs, weak) // out_msg_queue_info:^OutMsgQueueInfo + && cs.advance(1) // before_split:Bool + && t_ShardAccounts.validate_skip_ref(cs, weak) // accounts:^ShardAccounts + && + t_ShardState_aux.validate_skip_ref( + cs, + weak) // ^[ total_balance:CurrencyCollection total_validator_fees:CurrencyCollection libraries:(HashmapE 256 LibDescr) master_ref:(Maybe BlkMasterInfo) ] + && Maybe>{}.validate_skip(cs, weak); // custom:(Maybe ^McStateExtra) +} + +const ShardState t_ShardState; + +bool ShardState_aux::skip(vm::CellSlice& cs) const { + return cs.advance(128) // overload_history:uint64 underload_history:uint64 + && t_CurrencyCollection.skip(cs) // total_balance:CurrencyCollection + && t_CurrencyCollection.skip(cs) // total_validator_fees:CurrencyCollection + && HashmapE{256, t_LibDescr}.skip(cs) // libraries:(HashmapE 256 LibDescr) + && Maybe{}.skip(cs); // master_ref:(Maybe BlkMasterInfo) +} + +bool ShardState_aux::validate_skip(vm::CellSlice& cs, bool weak) const { + return cs.advance(128) // overload_history:uint64 underload_history:uint64 + && t_CurrencyCollection.validate_skip(cs, weak) // total_balance:CurrencyCollection + && t_CurrencyCollection.validate_skip(cs, weak) // total_validator_fees:CurrencyCollection + && HashmapE{256, t_LibDescr}.validate_skip(cs, weak) // libraries:(HashmapE 256 LibDescr) + && Maybe{}.validate_skip(cs, weak); // master_ref:(Maybe BlkMasterInfo) +} + +const ShardState_aux t_ShardState_aux; + +bool LibDescr::skip(vm::CellSlice& cs) const { + return cs.advance(2) // shared_lib_descr$00 + && cs.fetch_ref().not_null() // lib:^Cell + && Hashmap{256, t_True}.skip(cs); // publishers:(Hashmap 256 False) +} + +bool LibDescr::validate_skip(vm::CellSlice& cs, bool weak) const { + return get_tag(cs) == shared_lib_descr && cs.advance(2) // shared_lib_descr$00 + && cs.fetch_ref().not_null() // lib:^Cell + && Hashmap{256, t_True}.validate_skip(cs, weak); // publishers:(Hashmap 256 False) +} + +const LibDescr t_LibDescr; + +bool BlkPrevInfo::skip(vm::CellSlice& cs) const { + return t_ExtBlkRef.skip(cs) // prev_blk_info$_ {merged:#} prev:ExtBlkRef + && (!merged || t_ExtBlkRef.skip(cs)); // prev_alt:merged?ExtBlkRef +} + +bool BlkPrevInfo::validate_skip(vm::CellSlice& cs, bool weak) const { + return t_ExtBlkRef.validate_skip(cs, weak) // prev_blk_info$_ {merged:#} prev:ExtBlkRef + && (!merged || t_ExtBlkRef.validate_skip(cs, weak)); // prev_alt:merged?ExtBlkRef +} + +const BlkPrevInfo t_BlkPrevInfo_0{0}; + +bool McStateExtra::skip(vm::CellSlice& cs) const { + return block::gen::t_McStateExtra.skip(cs); +} + +bool McStateExtra::validate_skip(vm::CellSlice& cs, bool weak) const { + return block::gen::t_McStateExtra.validate_skip(cs, weak); // ?? +} + +const McStateExtra t_McStateExtra; + +const KeyExtBlkRef t_KeyExtBlkRef; + +bool KeyMaxLt::add_values(vm::CellBuilder& cb, vm::CellSlice& cs1, vm::CellSlice& cs2) const { + bool key1, key2; + unsigned long long lt1, lt2; + return cs1.fetch_bool_to(key1) && cs1.fetch_ulong_bool(64, lt1) // cs1 => _ key:Bool max_end_lt:uint64 = KeyMaxLt; + && cs2.fetch_bool_to(key2) && cs2.fetch_ulong_bool(64, lt2) // cs2 => _ key:Bool max_end_lt:uint64 = KeyMaxLt; + && cb.store_bool_bool(key1 | key2) && cb.store_long_bool(std::max(lt1, lt2), 64); +} + +const KeyMaxLt t_KeyMaxLt; + +bool Aug_OldMcBlocksInfo::eval_leaf(vm::CellBuilder& cb, vm::CellSlice& cs) const { + return cs.have(65) && cb.append_bitslice(cs.prefetch_bits(65)); // copy first 1+64 bits +}; + +const Aug_OldMcBlocksInfo aug_OldMcBlocksInfo; + +bool ShardFeeCreated::skip(vm::CellSlice& cs) const { + return t_CurrencyCollection.skip(cs) && t_CurrencyCollection.skip(cs); +} + +bool ShardFeeCreated::validate_skip(vm::CellSlice& cs, bool weak) const { + return t_CurrencyCollection.validate_skip(cs, weak) && t_CurrencyCollection.validate_skip(cs, weak); +} + +bool ShardFeeCreated::null_value(vm::CellBuilder& cb) const { + return t_CurrencyCollection.null_value(cb) && t_CurrencyCollection.null_value(cb); +} + +bool ShardFeeCreated::add_values(vm::CellBuilder& cb, vm::CellSlice& cs1, vm::CellSlice& cs2) const { + return t_CurrencyCollection.add_values(cb, cs1, cs2) && t_CurrencyCollection.add_values(cb, cs1, cs2); +} + +const ShardFeeCreated t_ShardFeeCreated; + +bool Aug_ShardFees::eval_leaf(vm::CellBuilder& cb, vm::CellSlice& cs) const { + return cb.append_cellslice_bool(cs) && t_ShardFeeCreated.skip(cs) && cs.empty_ext(); +}; + +const Aug_ShardFees aug_ShardFees; + +} // namespace tlb +} // namespace block diff --git a/crypto/block/block-parse.h b/crypto/block/block-parse.h new file mode 100644 index 00000000..381dc149 --- /dev/null +++ b/crypto/block/block-parse.h @@ -0,0 +1,1081 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once +#include "common/refcnt.hpp" +#include "vm/cells.h" +#include "vm/cellslice.h" +#include "vm/dict.h" +#include "vm/boc.h" +#include "block/block.h" +#include +#include "tl/tlblib.hpp" +#include "td/utils/bits.h" +#include "td/utils/StringBuilder.h" +#include "ton/ton-types.h" + +namespace block { + +using td::Ref; + +namespace tlb { +using namespace ::tlb; + +struct Anycast final : TLB { + int get_size(const vm::CellSlice& cs) const override { + return cs.have(5) ? 5 + (int)cs.prefetch_ulong(5) : -1; + } + bool skip_get_depth(vm::CellSlice& cs, int& depth) const { + return cs.fetch_uint_leq(30, depth) && cs.advance(depth); + } +}; + +extern const Anycast t_Anycast; + +struct Maybe_Anycast final : public Maybe { + bool skip_get_depth(vm::CellSlice& cs, int& depth) const; +}; + +extern const Maybe_Anycast t_Maybe_Anycast; + +struct VarUInteger final : TLB_Complex { + int n, ln; + VarUInteger(int _n) : n(_n) { + ln = 32 - td::count_leading_zeroes32(n - 1); + } + bool skip(vm::CellSlice& cs) const override; + bool validate_skip(vm::CellSlice& cs, bool weak = false) const override; + td::RefInt256 as_integer_skip(vm::CellSlice& cs) const override; + unsigned long long as_uint(const vm::CellSlice& cs) const override; + bool null_value(vm::CellBuilder& cb) const override { + return cb.store_zeroes_bool(ln); + } + bool store_integer_value(vm::CellBuilder& cb, const td::BigInt256& value) const override; + unsigned precompute_integer_size(const td::BigInt256& value) const; + unsigned precompute_integer_size(td::RefInt256 value) const; +}; + +extern const VarUInteger t_VarUInteger_3, t_VarUInteger_7, t_VarUInteger_16, t_VarUInteger_32; + +struct VarUIntegerPos final : TLB_Complex { + int n, ln; + VarUIntegerPos(int _n) : n(_n) { + ln = 32 - td::count_leading_zeroes32(n - 1); + } + bool skip(vm::CellSlice& cs) const override; + bool validate_skip(vm::CellSlice& cs, bool weak = false) const override; + td::RefInt256 as_integer_skip(vm::CellSlice& cs) const override; + unsigned long long as_uint(const vm::CellSlice& cs) const override; + bool store_integer_value(vm::CellBuilder& cb, const td::BigInt256& value) const override; +}; + +extern const VarUIntegerPos t_VarUIntegerPos_16, t_VarUIntegerPos_32; + +struct VarInteger final : TLB_Complex { + int n, ln; + VarInteger(int _n) : n(_n) { + ln = 32 - td::count_leading_zeroes32(n - 1); + } + bool skip(vm::CellSlice& cs) const override; + bool validate_skip(vm::CellSlice& cs, bool weak = false) const override; + td::RefInt256 as_integer_skip(vm::CellSlice& cs) const override; + long long as_int(const vm::CellSlice& cs) const override; + bool null_value(vm::CellBuilder& cb) const override { + return cb.store_zeroes_bool(ln); + } + bool store_integer_value(vm::CellBuilder& cb, const td::BigInt256& value) const override; +}; + +struct VarIntegerNz final : TLB_Complex { + int n, ln; + VarIntegerNz(int _n) : n(_n) { + ln = 32 - td::count_leading_zeroes32(n - 1); + } + bool skip(vm::CellSlice& cs) const override; + bool validate_skip(vm::CellSlice& cs, bool weak = false) const override; + td::RefInt256 as_integer_skip(vm::CellSlice& cs) const override; + long long as_int(const vm::CellSlice& cs) const override; + bool store_integer_value(vm::CellBuilder& cb, const td::BigInt256& value) const override; +}; + +struct Unary final : TLB { + int get_size(const vm::CellSlice& cs) const override { + return cs.count_leading(1) + 1; + } + bool validate_skip(vm::CellSlice& cs, bool weak, int& n) const { + return cs.advance((n = cs.count_leading(1)) + 1); + } + bool skip(vm::CellSlice& cs, int& n) const { + return validate_skip(cs, false, n); + } + bool validate_skip(vm::CellSlice& cs, bool weak = false) const override { + return cs.advance(get_size(cs)); + } + bool skip(vm::CellSlice& cs) const override { + return validate_skip(cs); + } + bool validate(const vm::CellSlice& cs, bool weak = false) const override { + return cs.have(get_size(cs)); + } +}; + +extern const Unary t_Unary; + +struct HmLabel final : TLB_Complex { + enum { hml_short = 0, hml_long = 2, hml_same = 3 }; + int m; // max size + HmLabel(int _m) : m(_m) { + } + bool validate_skip(vm::CellSlice& cs, bool weak, int& n) const; + bool skip(vm::CellSlice& cs, int& n) const { + return validate_skip(cs, false, n); + } + bool skip(vm::CellSlice& cs) const override { + int n; + return skip(cs, n); + } + bool validate_skip(vm::CellSlice& cs, bool weak = false) const override { + int n; + return validate_skip(cs, weak, n); + } + int get_tag(const vm::CellSlice& cs) const override; +}; + +struct Hashmap final : TLB_Complex { + const TLB& value_type; + int n; + Hashmap(int _n, const TLB& _val_type) : value_type(_val_type), n(_n) { + } + bool skip(vm::CellSlice& cs) const override; + bool validate_skip(vm::CellSlice& cs, bool weak = false) const override; +}; + +struct HashmapNode final : TLB_Complex { + enum { hmn_leaf = 0, hmn_fork = 1 }; + const TLB& value_type; + int n; + HashmapNode(int _n, const TLB& _val_type) : value_type(_val_type), n(_n) { + } + int get_size(const vm::CellSlice& cs) const override; + bool skip(vm::CellSlice& cs) const override; + bool validate_skip(vm::CellSlice& cs, bool weak = false) const override; + int get_tag(const vm::CellSlice& cs) const override { + return n > 0 ? hmn_fork : n; + } +}; + +struct HashmapE final : TLB { + enum { hme_empty = 0, hme_root = 1 }; + Hashmap root_type; + HashmapE(int _n, const TLB& _val_type) : root_type(_n, _val_type) { + } + int get_size(const vm::CellSlice& cs) const override; + bool validate(const vm::CellSlice& cs, bool weak = false) const override; + int get_tag(const vm::CellSlice& cs) const override { + return (int)cs.prefetch_ulong(1); + } + bool null_value(vm::CellBuilder& cb) const override { + return cb.store_zeroes_bool(1); + } + bool add_values(vm::CellBuilder& cb, vm::CellSlice& cs1, vm::CellSlice& cs2) const override; + int sub_values(vm::CellBuilder& cb, vm::CellSlice& cs1, vm::CellSlice& cs2) const override; + bool add_values_ref(Ref& res, Ref arg1, Ref arg2) const; + int sub_values_ref(Ref& res, Ref arg1, Ref arg2) const; + bool store_ref(vm::CellBuilder& cb, Ref arg) const; +}; + +struct AugmentationCheckData : vm::dict::AugmentationData { + const TLB& value_type; + const TLB& extra_type; + AugmentationCheckData(const TLB& val_type, const TLB& ex_type) : value_type(val_type), extra_type(ex_type) { + } + bool skip_extra(vm::CellSlice& cs) const override { + return extra_type.skip(cs); + } + bool eval_fork(vm::CellBuilder& cb, vm::CellSlice& left_cs, vm::CellSlice& right_cs) const override { + return extra_type.add_values(cb, left_cs, right_cs); + } + bool eval_empty(vm::CellBuilder& cb) const override { + return extra_type.null_value(cb); + } +}; + +struct HashmapAug final : TLB_Complex { + const AugmentationCheckData& aug; + int n; + HashmapAug(int _n, const AugmentationCheckData& _aug) : aug(_aug), n(_n) { + } + bool skip(vm::CellSlice& cs) const override; + bool validate_skip(vm::CellSlice& cs, bool weak = false) const override; + bool extract_extra(vm::CellSlice& cs) const; +}; + +struct HashmapAugNode final : TLB_Complex { + enum { ahmn_leaf = 0, ahmn_fork = 1 }; + const AugmentationCheckData& aug; + int n; + HashmapAugNode(int _n, const AugmentationCheckData& _aug) : aug(_aug), n(_n) { + } + bool skip(vm::CellSlice& cs) const override; + bool validate_skip(vm::CellSlice& cs, bool weak = false) const override; + int get_tag(const vm::CellSlice& cs) const override { + return n > 0 ? ahmn_fork : n; + } +}; + +struct HashmapAugE final : TLB_Complex { + enum { ahme_empty = 0, ahme_root = 1 }; + HashmapAug root_type; + HashmapAugE(int _n, const AugmentationCheckData& _aug) : root_type(_n, std::move(_aug)) { + } + bool skip(vm::CellSlice& cs) const override; + bool validate_skip(vm::CellSlice& cs, bool weak = false) const override; + bool extract_extra(vm::CellSlice& cs) const; + int get_tag(const vm::CellSlice& cs) const override { + return (int)cs.prefetch_ulong(1); + } +}; + +struct Grams final : TLB_Complex { + bool validate_skip(vm::CellSlice& cs, bool weak = false) const override; + td::RefInt256 as_integer_skip(vm::CellSlice& cs) const override; + bool null_value(vm::CellBuilder& cb) const override; + bool store_integer_value(vm::CellBuilder& cb, const td::BigInt256& value) const override; + unsigned precompute_size(const td::BigInt256& value) const; + unsigned precompute_size(td::RefInt256 value) const; +}; + +extern const Grams t_Grams; + +struct MsgAddressInt final : TLB_Complex { + enum { addr_std = 2, addr_var = 3 }; + bool validate_skip(vm::CellSlice& cs, bool weak = false) const override; + int get_tag(const vm::CellSlice& cs) const override { + return (int)cs.prefetch_ulong(2); + } + static ton::AccountIdPrefixFull get_prefix(vm::CellSlice&& cs); + static ton::AccountIdPrefixFull get_prefix(const vm::CellSlice& cs); + static ton::AccountIdPrefixFull get_prefix(Ref cs_ref); + static bool get_prefix_to(vm::CellSlice&& cs, ton::AccountIdPrefixFull& pfx) { + return (pfx = get_prefix(std::move(cs))).is_valid(); + } + static bool get_prefix_to(const vm::CellSlice& cs, ton::AccountIdPrefixFull& pfx) { + return (pfx = get_prefix(cs)).is_valid(); + } + static bool get_prefix_to(Ref cs_ref, ton::AccountIdPrefixFull& pfx) { + return cs_ref.not_null() && (pfx = get_prefix(std::move(cs_ref))).is_valid(); + } + bool skip_get_depth(vm::CellSlice& cs, int& depth) const; + bool extract_std_address(Ref cs_ref, ton::WorkchainId& workchain, ton::StdSmcAddress& addr, + bool rewrite = true) const; + bool extract_std_address(vm::CellSlice& cs, ton::WorkchainId& workchain, ton::StdSmcAddress& addr, + bool rewrite = true) const; +}; + +extern const MsgAddressInt t_MsgAddressInt; + +struct MsgAddressExt final : TLB { + enum { addr_none = 0, addr_ext = 1 }; + int get_size(const vm::CellSlice& cs) const override; + int get_tag(const vm::CellSlice& cs) const override { + return (int)cs.prefetch_ulong(2); + } +}; + +extern const MsgAddressExt t_MsgAddressExt; + +struct MsgAddress final : TLB_Complex { + enum { addr_none = 0, addr_ext = 1, addr_std = 2, addr_var = 3 }; + bool validate_skip(vm::CellSlice& cs, bool weak = false) const override; + int get_tag(const vm::CellSlice& cs) const override { + return (int)cs.prefetch_ulong(2); + } +}; + +extern const MsgAddress t_MsgAddress; + +struct ExtraCurrencyCollection final : TLB { + HashmapE dict_type; + ExtraCurrencyCollection() : dict_type(32, t_VarUIntegerPos_32) { + } + int get_size(const vm::CellSlice& cs) const override { + return dict_type.get_size(cs); + } + bool validate(const vm::CellSlice& cs, bool weak) const override { + return dict_type.validate(cs, weak); + } + bool null_value(vm::CellBuilder& cb) const override { + return cb.store_zeroes_bool(1); + } + bool add_values(vm::CellBuilder& cb, vm::CellSlice& cs1, vm::CellSlice& cs2) const override { + return dict_type.add_values(cb, cs1, cs2); + } + int sub_values(vm::CellBuilder& cb, vm::CellSlice& cs1, vm::CellSlice& cs2) const override { + return dict_type.sub_values(cb, cs1, cs2); + } + bool add_values_ref(Ref& res, Ref arg1, Ref arg2) const { + return dict_type.add_values_ref(res, std::move(arg1), std::move(arg2)); + } + int sub_values_ref(Ref& res, Ref arg1, Ref arg2) const { + return dict_type.sub_values_ref(res, std::move(arg1), std::move(arg2)); + } + bool store_ref(vm::CellBuilder& cb, Ref arg) const { + return dict_type.store_ref(cb, std::move(arg)); + } + unsigned precompute_size(Ref arg) const { + return arg.is_null() ? 1 : 0x10001; + } +}; + +extern const ExtraCurrencyCollection t_ExtraCurrencyCollection; + +struct CurrencyCollection final : TLB_Complex { + bool skip(vm::CellSlice& cs) const override; + bool validate_skip(vm::CellSlice& cs, bool weak = false) const override; + td::RefInt256 as_integer_skip(vm::CellSlice& cs) const override; + bool null_value(vm::CellBuilder& cb) const override { + return cb.store_bits_same_bool(1 + 4, false); + } + bool add_values(vm::CellBuilder& cb, vm::CellSlice& cs1, vm::CellSlice& cs2) const override; + bool unpack_special(vm::CellSlice& cs, td::RefInt256& balance, Ref& extra, bool inexact = false) const; + bool unpack_special(vm::CellSlice& cs, block::CurrencyCollection& value, bool inexact = false) const; + bool pack_special(vm::CellBuilder& cb, td::RefInt256 balance, Ref extra) const; + bool pack_special(vm::CellBuilder& cb, const block::CurrencyCollection& value) const; + bool pack_special(vm::CellBuilder& cb, block::CurrencyCollection&& value) const; + bool unpack(vm::CellSlice& cs, block::CurrencyCollection& res) const; + bool pack(vm::CellBuilder& cb, const block::CurrencyCollection& res) const; + unsigned precompute_size(td::RefInt256 balance, Ref extra) const { + return t_Grams.precompute_size(std::move(balance)) + t_ExtraCurrencyCollection.precompute_size(std::move(extra)); + } +}; + +extern const CurrencyCollection t_CurrencyCollection; + +struct CommonMsgInfo final : TLB_Complex { + enum { int_msg_info = 0, ext_in_msg_info = 2, ext_out_msg_info = 3 }; + bool skip(vm::CellSlice& cs) const override; + bool validate_skip(vm::CellSlice& cs, bool weak = false) const override; + int get_tag(const vm::CellSlice& cs) const override { + int v = (int)cs.prefetch_ulong(2); + return v == 1 ? int_msg_info : v; + } + struct Record_int_msg_info; + bool unpack(vm::CellSlice& cs, Record_int_msg_info& data) const; + bool get_created_lt(vm::CellSlice& cs, unsigned long long& created_lt) const; + bool is_internal(const vm::CellSlice& cs) const { + return get_tag(cs) == int_msg_info; + } +}; + +struct CommonMsgInfo::Record_int_msg_info { + bool ihr_disabled, bounce, bounced; + Ref src, dest, value, ihr_fee, fwd_fee; + unsigned long long created_lt; + unsigned created_at; +}; + +extern const CommonMsgInfo t_CommonMsgInfo; + +struct TickTock final : TLB { + int get_size(const vm::CellSlice& cs) const override { + return 2; + } +}; + +extern const TickTock t_TickTock; + +struct StateInit final : TLB_Complex { + bool validate_skip(vm::CellSlice& cs, bool weak = false) const override; + bool get_ticktock(vm::CellSlice& cs, int& ticktock) const; +}; + +extern const StateInit t_StateInit; + +struct Message final : TLB_Complex { + bool validate_skip(vm::CellSlice& cs, bool weak = false) const override; + bool extract_info(vm::CellSlice& cs) const; + bool get_created_lt(vm::CellSlice& cs, unsigned long long& created_lt) const; + bool is_internal(const vm::CellSlice& cs) const { + return t_CommonMsgInfo.is_internal(cs); + } + bool is_internal(Ref ref) const; +}; + +extern const Message t_Message; +extern const RefTo t_Ref_Message; + +struct IntermediateAddress final : TLB_Complex { + enum { interm_addr_regular = 0, interm_addr_simple = 2, interm_addr_ext = 3 }; + int get_size(const vm::CellSlice& cs) const override; + bool skip(vm::CellSlice& cs) const override; + bool validate_skip(vm::CellSlice& cs, bool weak = false) const override; + bool fetch_regular(vm::CellSlice& cs, int& use_dst_bits) const { + return cs.fetch_uint_to(8, use_dst_bits) && use_dst_bits <= 96; + } + int get_tag(const vm::CellSlice& cs) const override { + int v = (int)cs.prefetch_ulong(2); + return v == 1 ? interm_addr_regular : v; + } +}; + +extern const IntermediateAddress t_IntermediateAddress; + +struct MsgEnvelope final : TLB_Complex { + bool skip(vm::CellSlice& cs) const override; + bool validate_skip(vm::CellSlice& cs, bool weak = false) const override; + bool extract_fwd_fees_remaining(vm::CellSlice& cs) const; + struct Record { + typedef MsgEnvelope type_class; + Ref cur_addr, next_addr, fwd_fee_remaining; + Ref msg; + }; + struct Record_std { + typedef MsgEnvelope type_class; + int cur_addr, next_addr; + td::RefInt256 fwd_fee_remaining; + Ref msg; + }; + bool unpack(vm::CellSlice& cs, Record& data) const; + bool unpack(vm::CellSlice& cs, Record_std& data) const; + bool unpack_std(vm::CellSlice& cs, int& cur_a, int& nhop_a, Ref& msg) const; + bool get_created_lt(const vm::CellSlice& cs, unsigned long long& created_lt) const; +}; + +extern const MsgEnvelope t_MsgEnvelope; +extern const RefTo t_Ref_MsgEnvelope; + +struct StorageUsed final : TLB_Complex { + bool skip(vm::CellSlice& cs) const override; + bool validate_skip(vm::CellSlice& cs, bool weak = false) const override; +}; + +extern const StorageUsed t_StorageUsed; + +struct StorageUsedShort final : TLB_Complex { + bool skip(vm::CellSlice& cs) const override; + bool validate_skip(vm::CellSlice& cs, bool weak = false) const override; +}; + +extern const StorageUsedShort t_StorageUsedShort; + +struct StorageInfo final : TLB_Complex { + bool skip(vm::CellSlice& cs) const override; + bool validate_skip(vm::CellSlice& cs, bool weak = false) const override; +}; + +extern const StorageInfo t_StorageInfo; + +struct AccountState final : TLB_Complex { + enum { account_uninit = 0, account_frozen = 1, account_active = 2 }; + bool validate_skip(vm::CellSlice& cs, bool weak = false) const override; + int get_tag(const vm::CellSlice& cs) const override { + int t = (int)cs.prefetch_ulong(2); + return t == 3 ? account_active : t; + } + bool get_ticktock(vm::CellSlice& cs, int& ticktock) const; +}; + +extern const AccountState t_AccountState; + +struct AccountStorage final : TLB_Complex { + bool skip(vm::CellSlice& cs) const override; + bool validate_skip(vm::CellSlice& cs, bool weak = false) const override; + bool skip_copy_balance(vm::CellBuilder& cb, vm::CellSlice& cs) const; +}; + +extern const AccountStorage t_AccountStorage; + +struct Account final : TLB_Complex { + enum { account_none = 0, account = 1 }; + bool allow_empty; + Account(bool _allow_empty = false) : allow_empty(_allow_empty) { + } + bool skip(vm::CellSlice& cs) const override; + bool validate_skip(vm::CellSlice& cs, bool weak = false) const override; + // Ref get_balance(const vm::CellSlice& cs) const; + bool skip_copy_balance(vm::CellBuilder& cb, vm::CellSlice& cs) const; + bool skip_copy_depth_balance(vm::CellBuilder& cb, vm::CellSlice& cs) const; + int get_tag(const vm::CellSlice& cs) const override { + return (int)cs.prefetch_ulong(1); + } +}; + +extern const Account t_Account, t_AccountE; +extern const RefTo t_Ref_Account; + +struct AccountStatus final : TLB { + enum { acc_state_uninit, acc_state_frozen, acc_state_active, acc_state_nonexist }; + int get_size(const vm::CellSlice& cs) const override { + return 2; + } + int get_tag(const vm::CellSlice& cs) const override { + return (int)cs.prefetch_ulong(2); + } +}; + +extern const AccountStatus t_AccountStatus; + +struct ShardAccount final : TLB_Complex { + struct Record { + using Type = ShardAccount; + Ref account; + ton::LogicalTime last_trans_lt; + ton::Bits256 last_trans_hash; + bool valid{false}; + bool is_zero{false}; + bool reset(); + bool unpack(vm::CellSlice& cs); + bool unpack(Ref cs_ref); + bool invalidate() { + return valid = false; + } + }; + int get_size(const vm::CellSlice& cs) const override { + return 0x10140; + } + bool skip(vm::CellSlice& cs) const override { + return cs.advance_ext(0x140, 1); + } + bool validate_skip(vm::CellSlice& cs, bool weak = false) const override { + return cs.advance(0x140) && t_Ref_Account.validate_skip(cs, weak); + } + static bool unpack(vm::CellSlice& cs, Record& info) { + return info.unpack(cs); + } + static bool unpack(Ref cs_ref, Record& info) { + return info.unpack(std::move(cs_ref)); + } + static bool extract_account_state(Ref cs_ref, Ref& acc_state); +}; + +extern const ShardAccount t_ShardAccount; + +struct DepthBalanceInfo final : TLB_Complex { + bool skip(vm::CellSlice& cs) const override; + bool validate_skip(vm::CellSlice& cs, bool weak = false) const override; + bool null_value(vm::CellBuilder& cb) const override; + bool add_values(vm::CellBuilder& cb, vm::CellSlice& cs1, vm::CellSlice& cs2) const override; +}; + +extern const DepthBalanceInfo t_DepthBalanceInfo; + +struct Aug_ShardAccounts final : AugmentationCheckData { + Aug_ShardAccounts() : AugmentationCheckData(t_ShardAccount, t_DepthBalanceInfo) { + } + bool eval_leaf(vm::CellBuilder& cb, vm::CellSlice& cs) const override; +}; + +extern const Aug_ShardAccounts aug_ShardAccounts; + +struct ShardAccounts final : TLB_Complex { + HashmapAugE dict_type; + ShardAccounts() : dict_type(256, aug_ShardAccounts){}; + bool skip(vm::CellSlice& cs) const override { + return dict_type.skip(cs); + } + bool validate_skip(vm::CellSlice& cs, bool weak = false) const override { + return dict_type.validate_skip(cs, weak); + } +}; + +extern const ShardAccounts t_ShardAccounts; + +struct AccStatusChange final : TLB { + enum { acst_unchanged = 0, acst_frozen = 2, acst_deleted = 3 }; + int get_size(const vm::CellSlice& cs) const override { + return cs.prefetch_ulong(1) ? 2 : 1; + } + int get_tag(const vm::CellSlice& cs) const override { + if (cs.size() == 1) { + return (int)cs.prefetch_ulong(1) ? -1 : acst_unchanged; + } + int v = (int)cs.prefetch_ulong(2); + return v == 1 ? acst_unchanged : v; + } +}; + +extern const AccStatusChange t_AccStatusChange; + +struct TrStoragePhase final : TLB_Complex { + bool skip(vm::CellSlice& cs) const override; + bool validate_skip(vm::CellSlice& cs, bool weak = false) const override; +}; + +extern const TrStoragePhase t_TrStoragePhase; + +struct TrCreditPhase final : TLB_Complex { + bool skip(vm::CellSlice& cs) const override; + bool validate_skip(vm::CellSlice& cs, bool weak = false) const override; +}; + +extern const TrCreditPhase t_TrCreditPhase; + +struct TrComputeInternal1 final : TLB_Complex { + bool skip(vm::CellSlice& cs) const override; + bool validate_skip(vm::CellSlice& cs, bool weak = false) const override; +}; + +struct ComputeSkipReason final : TLB { + enum { cskip_no_state = 0, cskip_bad_state = 1, cskip_no_gas = 2 }; + int get_size(const vm::CellSlice& cs) const override { + return 2; + } + bool validate_skip(vm::CellSlice& cs, bool weak = false) const override { + return get_tag(cs) >= 0 && cs.advance(2); + } + int get_tag(const vm::CellSlice& cs) const override { + int t = (int)cs.prefetch_ulong(2); + return t < 3 ? t : -1; + } +}; + +extern const ComputeSkipReason t_ComputeSkipReason; + +struct TrComputePhase final : TLB_Complex { + enum { tr_phase_compute_skipped = 0, tr_phase_compute_vm = 1 }; + bool skip(vm::CellSlice& cs) const override; + bool validate_skip(vm::CellSlice& cs, bool weak = false) const override; + int get_tag(const vm::CellSlice& cs) const override { + return (int)cs.prefetch_ulong(1); + } +}; + +extern const TrComputePhase t_TrComputePhase; + +struct TrActionPhase final : TLB_Complex { + bool skip(vm::CellSlice& cs) const override; + bool validate_skip(vm::CellSlice& cs, bool weak = false) const override; +}; + +extern const TrActionPhase t_TrActionPhase; + +struct TrBouncePhase final : TLB_Complex { + enum { tr_phase_bounce_negfunds = 0, tr_phase_bounce_nofunds = 1, tr_phase_bounce_ok = 2 }; + bool skip(vm::CellSlice& cs) const override; + bool validate_skip(vm::CellSlice& cs, bool weak = false) const override; + int get_tag(const vm::CellSlice& cs) const override; +}; + +extern const TrBouncePhase t_TrBouncePhase; + +struct SplitMergeInfo final : TLB_Complex { + bool skip(vm::CellSlice& cs) const override; + bool validate_skip(vm::CellSlice& cs, bool weak = false) const override; +}; + +extern const SplitMergeInfo t_SplitMergeInfo; + +struct TransactionDescr final : TLB_Complex { + enum { + trans_ord = 0, + trans_storage = 1, + trans_tick_tock = 2, + trans_split_prepare = 4, + trans_split_install = 5, + trans_merge_prepare = 6, + trans_merge_install = 7 + }; + bool skip(vm::CellSlice& cs) const override; + bool validate_skip(vm::CellSlice& cs, bool weak = false) const override; + int get_tag(const vm::CellSlice& cs) const override; +}; + +extern const TransactionDescr t_TransactionDescr; + +struct Transaction_aux final : TLB_Complex { + bool skip(vm::CellSlice& cs) const override; + bool validate_skip(vm::CellSlice& cs, bool weak = false) const override; +}; + +extern const Transaction_aux t_Transaction_aux; + +struct Transaction final : TLB_Complex { + bool skip(vm::CellSlice& cs) const override; + bool validate_skip(vm::CellSlice& cs, bool weak = false) const override; + bool get_total_fees(vm::CellSlice&& cs, block::CurrencyCollection& total_fees) const; +}; + +extern const Transaction t_Transaction; +extern const RefTo t_Ref_Transaction; + +struct Aug_AccountTransactions final : AugmentationCheckData { + Aug_AccountTransactions() : AugmentationCheckData(t_Ref_Transaction, t_CurrencyCollection) { + } + bool eval_leaf(vm::CellBuilder& cb, vm::CellSlice& cs) const override; +}; + +extern const Aug_AccountTransactions aug_AccountTransactions; +extern const HashmapAug t_AccountTransactions; // (HashmapAug 64 ^Transaction CurrencyCollection) + +struct HashUpdate final : TLB_Complex { + bool skip(vm::CellSlice& cs) const override { + return cs.advance(8 + 256 * 2); + } + bool validate_skip(vm::CellSlice& cs, bool weak = false) const override { + return cs.fetch_ulong(8) == 0x72 && cs.advance(256 * 2); + } +}; + +extern const HashUpdate t_HashUpdate; +extern const RefTo t_Ref_HashUpdate; + +struct AccountBlock final : TLB_Complex { + bool skip(vm::CellSlice& cs) const override; + bool validate_skip(vm::CellSlice& cs, bool weak = false) const override; + bool get_total_fees(vm::CellSlice&& cs, block::CurrencyCollection& total_fees) const; +}; + +extern const AccountBlock t_AccountBlock; + +struct Aug_ShardAccountBlocks final : AugmentationCheckData { + Aug_ShardAccountBlocks() : AugmentationCheckData(t_AccountBlock, t_CurrencyCollection) { + } + bool eval_leaf(vm::CellBuilder& cb, vm::CellSlice& cs) const override; +}; + +extern const Aug_ShardAccountBlocks aug_ShardAccountBlocks; +extern const HashmapAugE t_ShardAccountBlocks; // (HashmapAugE 256 AccountBlock CurrencyCollection) + +struct ImportFees final : TLB_Complex { + bool skip(vm::CellSlice& cs) const override; + bool validate_skip(vm::CellSlice& cs, bool weak = false) const override; + bool null_value(vm::CellBuilder& cb) const override { + return cb.store_bits_same_bool(4 + 4 + 1, false); + } + bool add_values(vm::CellBuilder& cb, vm::CellSlice& cs1, vm::CellSlice& cs2) const override; +}; + +extern const ImportFees t_ImportFees; + +struct InMsg final : TLB_Complex { + enum { + msg_import_ext = 0, + msg_import_ihr = 2, + msg_import_imm = 3, + msg_import_fin = 4, + msg_import_tr = 5, + msg_discard_fin = 6, + msg_discard_tr = 7 + }; + bool skip(vm::CellSlice& cs) const override; + bool validate_skip(vm::CellSlice& cs, bool weak = false) const override; + int get_tag(const vm::CellSlice& cs) const override { + return (int)cs.prefetch_ulong(3); + } + bool get_import_fees(vm::CellBuilder& cb, vm::CellSlice& cs) const; +}; + +extern const InMsg t_InMsg; + +struct OutMsg final : TLB_Complex { + enum { + msg_export_ext = 0, + msg_export_new = 1, + msg_export_imm = 2, + msg_export_tr = 3, + msg_export_deq_imm = 4, + msg_export_deq = 6, + msg_export_tr_req = 7 + }; + bool skip(vm::CellSlice& cs) const override; + bool validate_skip(vm::CellSlice& cs, bool weak = false) const override; + int get_tag(const vm::CellSlice& cs) const override { + return (int)cs.prefetch_ulong(3); + } + bool get_export_value(vm::CellBuilder& cb, vm::CellSlice& cs) const; + bool get_created_lt(vm::CellSlice& cs, unsigned long long& created_lt) const; +}; + +extern const OutMsg t_OutMsg; + +// next: InMsgDescr, OutMsgDescr, OutMsgQueue, and their augmentations + +struct Aug_InMsgDescr final : AugmentationCheckData { + Aug_InMsgDescr() : AugmentationCheckData(t_InMsg, t_ImportFees) { + } + bool eval_leaf(vm::CellBuilder& cb, vm::CellSlice& cs) const override { + return t_InMsg.get_import_fees(cb, cs); + } +}; + +extern const Aug_InMsgDescr aug_InMsgDescr; + +struct InMsgDescr final : TLB_Complex { + HashmapAugE dict_type; + InMsgDescr() : dict_type(256, aug_InMsgDescr){}; + bool skip(vm::CellSlice& cs) const override { + return dict_type.skip(cs); + } + bool validate_skip(vm::CellSlice& cs, bool weak = false) const override { + return dict_type.validate_skip(cs, weak); + } +}; + +extern const InMsgDescr t_InMsgDescr; + +struct Aug_OutMsgDescr final : AugmentationCheckData { + Aug_OutMsgDescr() : AugmentationCheckData(t_OutMsg, t_CurrencyCollection) { + } + bool eval_leaf(vm::CellBuilder& cb, vm::CellSlice& cs) const override { + return t_OutMsg.get_export_value(cb, cs); + } +}; + +extern const Aug_OutMsgDescr aug_OutMsgDescr; + +struct OutMsgDescr final : TLB_Complex { + HashmapAugE dict_type; + OutMsgDescr() : dict_type(256, aug_OutMsgDescr){}; + bool skip(vm::CellSlice& cs) const override { + return dict_type.skip(cs); + } + bool validate_skip(vm::CellSlice& cs, bool weak = false) const override { + return dict_type.validate_skip(cs, weak); + } +}; + +extern const OutMsgDescr t_OutMsgDescr; + +struct EnqueuedMsg final : TLB_Complex { + int get_size(const vm::CellSlice& cs) const override { + return 0x10040; + } + bool skip(vm::CellSlice& cs) const override { + return cs.advance_ext(0x10040); + } + bool validate_skip(vm::CellSlice& cs, bool weak = false) const override; + bool unpack(vm::CellSlice& cs, EnqueuedMsgDescr& descr) const { + return descr.unpack(cs); + } +}; + +extern const EnqueuedMsg t_EnqueuedMsg; + +struct Aug_OutMsgQueue final : AugmentationCheckData { + Aug_OutMsgQueue() : AugmentationCheckData(t_EnqueuedMsg, t_uint64) { + } + bool eval_fork(vm::CellBuilder& cb, vm::CellSlice& left_cs, vm::CellSlice& right_cs) const override; + bool eval_empty(vm::CellBuilder& cb) const override; + bool eval_leaf(vm::CellBuilder& cb, vm::CellSlice& cs) const override; +}; + +extern const Aug_OutMsgQueue aug_OutMsgQueue; + +struct OutMsgQueue final : TLB_Complex { + HashmapAugE dict_type; + OutMsgQueue() : dict_type(32 + 64 + 256, aug_OutMsgQueue){}; + bool skip(vm::CellSlice& cs) const override { + return dict_type.skip(cs); + } + bool validate_skip(vm::CellSlice& cs, bool weak = false) const override { + return dict_type.validate_skip(cs, weak); + } +}; + +extern const OutMsgQueue t_OutMsgQueue; + +struct ProcessedUpto final : TLB { + int get_size(const vm::CellSlice& cs) const override { + return 64 + 256; + } +}; + +extern const ProcessedUpto t_ProcessedUpto; +extern const HashmapE t_ProcessedInfo; +extern const HashmapE t_IhrPendingInfo; + +struct OutMsgQueueInfo final : TLB_Complex { + bool skip(vm::CellSlice& cs) const override; + bool validate_skip(vm::CellSlice& cs, bool weak = false) const override; +}; + +extern const OutMsgQueueInfo t_OutMsgQueueInfo; +extern const RefTo t_Ref_OutMsgQueueInfo; + +struct ExtBlkRef final : TLB { + enum { fixed_size = 64 + 32 + 256 * 2 }; + int get_size(const vm::CellSlice& cs) const override { + return fixed_size; + } + bool unpack(vm::CellSlice& cs, ton::BlockIdExt& blkid, ton::LogicalTime* end_lt = nullptr) const; + bool unpack(Ref cs_ref, ton::BlockIdExt& blkid, ton::LogicalTime* end_lt = nullptr) const; +}; + +extern const ExtBlkRef t_ExtBlkRef; + +struct BlkMasterInfo final : TLB { + int get_size(const vm::CellSlice& cs) const override { + return t_ExtBlkRef.get_size(cs); + } +}; + +extern const BlkMasterInfo t_BlkMasterInfo; + +struct ShardIdent final : TLB_Complex { + struct Record; + int get_size(const vm::CellSlice& cs) const override { + return 2 + 6 + 32 + 64; + } + bool skip(vm::CellSlice& cs) const override { + return cs.advance(get_size(cs)); + } + bool validate_skip(vm::CellSlice& cs, bool weak = false) const override; + int get_tag(const vm::CellSlice& cs) const override { + return 0; + } + bool unpack(vm::CellSlice& cs, Record& data) const; + bool pack(vm::CellBuilder& cb, const Record& data) const; + bool unpack(vm::CellSlice& cs, ton::ShardIdFull& data) const; + bool pack(vm::CellBuilder& cb, ton::ShardIdFull data) const; + bool unpack(vm::CellSlice& cs, ton::WorkchainId& workchain, ton::ShardId& shard) const; + bool pack(vm::CellBuilder& cb, ton::WorkchainId workchain, ton::ShardId shard) const; +}; + +struct ShardIdent::Record { + int shard_pfx_bits; + int workchain_id; + unsigned long long shard_prefix; + Record() : shard_pfx_bits(-1) { + } + Record(int _pfxlen, int _wcid, unsigned long long _pfx) + : shard_pfx_bits(_pfxlen), workchain_id(_wcid), shard_prefix(_pfx) { + } + bool check() const; + bool is_valid() const { + return shard_pfx_bits >= 0; + } + void invalidate() { + shard_pfx_bits = -1; + } +}; + +extern const ShardIdent t_ShardIdent; + +struct BlockIdExt final : TLB_Complex { + int get_size(const vm::CellSlice& cs) const override { + return 2 + 6 + 32 + 64 + 32 + 256 * 2; + } + bool skip(vm::CellSlice& cs) const override { + return cs.advance(get_size(cs)); + } + bool validate_skip(vm::CellSlice& cs, bool weak = false) const override; + bool unpack(vm::CellSlice& cs, ton::BlockIdExt& data) const; + bool pack(vm::CellBuilder& cb, const ton::BlockIdExt& data) const; +}; + +extern const BlockIdExt t_BlockIdExt; + +struct ShardState final : TLB_Complex { + enum { shard_state = (int)0x9023afe2, split_state = 0x5f327da5 }; + bool skip(vm::CellSlice& cs) const override; + bool validate_skip(vm::CellSlice& cs, bool weak = false) const override; + int get_tag(const vm::CellSlice& cs) const override { + return (int)cs.prefetch_ulong(32) == shard_state ? shard_state : -1; + } +}; + +extern const ShardState t_ShardState; + +struct ShardState_aux final : TLB_Complex { + bool skip(vm::CellSlice& cs) const override; + bool validate_skip(vm::CellSlice& cs, bool weak = false) const override; + int get_tag(const vm::CellSlice& cs) const override { + return 0; + } +}; + +extern const ShardState_aux t_ShardState_aux; + +struct LibDescr final : TLB_Complex { + enum { shared_lib_descr = 0 }; + bool skip(vm::CellSlice& cs) const override; + bool validate_skip(vm::CellSlice& cs, bool weak = false) const override; + int get_tag(const vm::CellSlice& cs) const override { + return (int)cs.prefetch_ulong(2); + } +}; + +extern const LibDescr t_LibDescr; + +struct BlkPrevInfo final : TLB_Complex { + bool merged; + BlkPrevInfo(bool _merged) : merged(_merged) { + } + bool skip(vm::CellSlice& cs) const override; + bool validate_skip(vm::CellSlice& cs, bool weak = false) const override; +}; + +extern const BlkPrevInfo t_BlkPrevInfo_0; + +struct McStateExtra final : TLB_Complex { + enum { masterchain_state_extra = 0xcc26 }; + bool skip(vm::CellSlice& cs) const override; + bool validate_skip(vm::CellSlice& cs, bool weak = false) const override; +}; + +extern const McStateExtra t_McStateExtra; + +struct KeyExtBlkRef final : TLB { + enum { fixed_size = 1 + ExtBlkRef::fixed_size }; + int get_size(const vm::CellSlice& cs) const override { + return fixed_size; + } +}; + +extern const KeyExtBlkRef t_KeyExtBlkRef; + +struct KeyMaxLt final : TLB { + enum { fixed_size = 1 + 64 }; + int get_size(const vm::CellSlice& cs) const override { + return fixed_size; + } + bool null_value(vm::CellBuilder& cb) const override { + return cb.store_bits_same_bool(fixed_size, false); + } + bool add_values(vm::CellBuilder& cb, vm::CellSlice& cs1, vm::CellSlice& cs2) const override; +}; + +extern const KeyMaxLt t_KeyMaxLt; + +struct Aug_OldMcBlocksInfo final : AugmentationCheckData { + Aug_OldMcBlocksInfo() : AugmentationCheckData(t_KeyExtBlkRef, t_KeyMaxLt) { + } + bool eval_leaf(vm::CellBuilder& cb, vm::CellSlice& cs) const override; +}; + +extern const Aug_OldMcBlocksInfo aug_OldMcBlocksInfo; + +struct ShardFeeCreated final : TLB_Complex { + bool skip(vm::CellSlice& cs) const override; + bool validate_skip(vm::CellSlice& cs, bool weak = false) const override; + bool null_value(vm::CellBuilder& cb) const override; + bool add_values(vm::CellBuilder& cb, vm::CellSlice& cs1, vm::CellSlice& cs2) const override; +}; + +extern const ShardFeeCreated t_ShardFeeCreated; + +struct Aug_ShardFees final : AugmentationCheckData { + Aug_ShardFees() : AugmentationCheckData(t_ShardFeeCreated, t_ShardFeeCreated) { + } + bool eval_leaf(vm::CellBuilder& cb, vm::CellSlice& cs) const override; +}; + +extern const Aug_ShardFees aug_ShardFees; + +} // namespace tlb +} // namespace block diff --git a/crypto/block/block.cpp b/crypto/block/block.cpp new file mode 100644 index 00000000..2340bb98 --- /dev/null +++ b/crypto/block/block.cpp @@ -0,0 +1,1923 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "td/utils/bits.h" +#include "block/block.h" +#include "block/block-auto.h" +#include "block/block-parse.h" +#include "ton/ton-shard.h" +#include "common/util.h" +#include "td/utils/crypto.h" +#include "td/utils/tl_storers.h" + +namespace block { +using namespace std::literals::string_literals; + +bool pack_std_smc_addr_to(char result[48], bool base64_url, ton::WorkchainId wc, const ton::StdSmcAddress& addr, + bool bounceable, bool testnet) { + if (wc < -128 || wc >= 128) { + return false; + } + unsigned char buffer[36]; + buffer[0] = (unsigned char)(0x51 - bounceable * 0x40 + testnet * 0x80); + buffer[1] = (unsigned char)wc; + std::memcpy(buffer + 2, addr.data(), 32); + unsigned crc = td::crc16(td::Slice{buffer, 34}); + buffer[34] = (unsigned char)(crc >> 8); + buffer[35] = (unsigned char)(crc & 0xff); + CHECK(buff_base64_encode(td::MutableSlice{result, 48}, td::Slice{buffer, 36}, base64_url) == 48); + return true; +} + +std::string pack_std_smc_addr(bool base64_url, ton::WorkchainId wc, const ton::StdSmcAddress& addr, bool bounceable, + bool testnet) { + char result[48]; + if (pack_std_smc_addr_to(result, base64_url, wc, addr, bounceable, testnet)) { + return std::string{result, 48}; + } else { + return ""; + } +} + +bool unpack_std_smc_addr(const char packed[48], ton::WorkchainId& wc, ton::StdSmcAddress& addr, bool& bounceable, + bool& testnet) { + unsigned char buffer[36]; + wc = ton::workchainInvalid; + if (!buff_base64_decode(td::MutableSlice{buffer, 36}, td::Slice{packed, 48}, true)) { + return false; + } + unsigned crc = td::crc16(td::Slice{buffer, 34}); + if (buffer[34] != (unsigned char)(crc >> 8) || buffer[35] != (unsigned char)(crc & 0xff)) { + return false; + } + if ((buffer[0] & 0x3f) != 0x11) { + return false; + } + testnet = (buffer[0] & 0x80); + bounceable = !(buffer[0] & 0x40); + wc = (td::int8)buffer[1]; + std::memcpy(addr.data(), buffer + 2, 32); + return true; +} + +bool unpack_std_smc_addr(td::Slice packed, ton::WorkchainId& wc, ton::StdSmcAddress& addr, bool& bounceable, + bool& testnet) { + return packed.size() == 48 && unpack_std_smc_addr(packed.data(), wc, addr, bounceable, testnet); +} + +bool unpack_std_smc_addr(std::string packed, ton::WorkchainId& wc, ton::StdSmcAddress& addr, bool& bounceable, + bool& testnet) { + return packed.size() == 48 && unpack_std_smc_addr(packed.data(), wc, addr, bounceable, testnet); +} + +StdAddress::StdAddress(std::string serialized) { + rdeserialize(std::move(serialized)); +} + +StdAddress::StdAddress(td::Slice from) { + rdeserialize(std::move(from)); +} + +std::string StdAddress::rserialize(bool base64_url) const { + char buffer[48]; + return rserialize_to(buffer, base64_url) ? std::string{buffer, 48} : ""; +} + +bool StdAddress::rserialize_to(td::MutableSlice to, bool base64_url) const { + return to.size() == 48 && rserialize_to(to.data(), base64_url); +} + +bool StdAddress::rserialize_to(char to[48], bool base64_url) const { + return pack_std_smc_addr_to(to, base64_url, workchain, addr, bounceable, testnet); +} + +bool StdAddress::rdeserialize(td::Slice from) { + return from.size() == 48 && unpack_std_smc_addr(from.data(), workchain, addr, bounceable, testnet); +} + +bool StdAddress::rdeserialize(std::string from) { + return from.size() == 48 && unpack_std_smc_addr(from.data(), workchain, addr, bounceable, testnet); +} + +bool StdAddress::rdeserialize(const char from[48]) { + return unpack_std_smc_addr(from, workchain, addr, bounceable, testnet); +} + +bool StdAddress::operator==(const StdAddress& other) const { + return workchain == other.workchain && addr == other.addr && bounceable == other.bounceable && + testnet == other.testnet; +} + +int parse_hex_digit(int c) { + if (c >= '0' && c <= '9') { + return c - '0'; + } + c |= 0x20; + if (c >= 'a' && c <= 'z') { + return c - 'a' + 10; + } + return -1; +} + +bool StdAddress::parse_addr(td::Slice acc_string) { + if (rdeserialize(acc_string)) { + return true; + } + testnet = false; + bounceable = true; + auto pos = acc_string.find(':'); + if (pos != std::string::npos) { + if (pos > 10) { + return invalidate(); + } + auto tmp = acc_string.substr(0, pos); + auto r_wc = td::to_integer_safe(tmp); + if (r_wc.is_error()) { + return invalidate(); + } + workchain = r_wc.move_as_ok(); + if (workchain == ton::workchainInvalid) { + return invalidate(); + } + ++pos; + } else { + pos = 0; + } + // LOG(DEBUG) << "parsing " << acc_string << " address"; + if (acc_string.size() != pos + 64) { + return invalidate(); + } + for (unsigned i = 0; i < 64; i++) { + int x = parse_hex_digit(acc_string[pos + i]), m = 15; + if (x < 0) { + return invalidate(); + } + if (!(i & 1)) { + x <<= 4; + m <<= 4; + } + addr.data()[i >> 1] = (unsigned char)((addr.data()[i >> 1] & ~m) | x); + } + return true; +} + +bool parse_std_account_addr(td::Slice acc_string, ton::WorkchainId& wc, ton::StdSmcAddress& addr, bool* bounceable, + bool* testnet_only) { + StdAddress a; + if (!a.parse_addr(acc_string)) { + return false; + } + wc = a.workchain; + addr = a.addr; + if (testnet_only) { + *testnet_only = a.testnet; + } + if (bounceable) { + *bounceable = a.bounceable; + } + return true; +} + +td::Result StdAddress::parse(td::Slice acc_string) { + StdAddress res; + if (res.parse_addr(acc_string)) { + return res; + } + return td::Status::Error("Failed to parse account address"); +} + +void ShardId::init() { + if (!shard_pfx) { + shard_pfx = (1ULL << 63); + shard_pfx_len = 0; + } else { + shard_pfx_len = 63 - td::count_trailing_zeroes_non_zero64(shard_pfx); + } +} + +ShardId::ShardId(ton::WorkchainId wc_id, unsigned long long sh_pfx) : workchain_id(wc_id), shard_pfx(sh_pfx) { + init(); +} + +ShardId::ShardId(ton::ShardIdFull ton_shard_id) : workchain_id(ton_shard_id.workchain), shard_pfx(ton_shard_id.shard) { + init(); +} + +ShardId::ShardId(ton::BlockId ton_block_id) : workchain_id(ton_block_id.workchain), shard_pfx(ton_block_id.shard) { + init(); +} + +ShardId::ShardId(const ton::BlockIdExt& ton_block_id) + : workchain_id(ton_block_id.id.workchain), shard_pfx(ton_block_id.id.shard) { + init(); +} + +ShardId::ShardId(ton::WorkchainId wc_id, unsigned long long sh_pfx, int sh_pfx_len) + : workchain_id(wc_id), shard_pfx_len(sh_pfx_len) { + if (sh_pfx_len < 0) { + shard_pfx_len = 0; + shard_pfx = (1ULL << 63); + } else if (sh_pfx_len > 63) { + shard_pfx_len = 63; + shard_pfx = sh_pfx | 1; + } else { + unsigned long long pow = 1ULL << (63 - sh_pfx_len); + shard_pfx = (sh_pfx | pow) & (pow - 1); + } +} + +std::ostream& operator<<(std::ostream& os, const ShardId& shard_id) { + shard_id.show(os); + return os; +} + +void ShardId::show(std::ostream& os) const { + if (workchain_id == ton::workchainInvalid) { + os << '?'; + return; + } + os << workchain_id << ':' << shard_pfx_len << ','; + unsigned long long t = shard_pfx; + int cnt = 0; + while ((t & ((1ULL << 63) - 1)) != 0) { + static const char hex_digit[] = "0123456789ABCDEF"; + os << (char)hex_digit[t >> 60]; + t <<= 4; + ++cnt; + } + if (!t || !cnt) { + os << '_'; + } +} + +std::string ShardId::to_str() const { + std::ostringstream os; + show(os); + return os.str(); +} + +bool ShardId::serialize(vm::CellBuilder& cb) const { + if (workchain_id == ton::workchainInvalid || cb.remaining_bits() < 104) { + return false; + } + return cb.store_long_bool(0, 2) && cb.store_ulong_rchk_bool(shard_pfx_len, 6) && + cb.store_long_bool(workchain_id, 32) && cb.store_long_bool(shard_pfx & (shard_pfx - 1)); +} + +bool ShardId::deserialize(vm::CellSlice& cs) { + if (cs.fetch_ulong(2) == 0 && cs.fetch_uint_to(6, shard_pfx_len) && cs.fetch_int_to(32, workchain_id) && + workchain_id != ton::workchainInvalid && cs.fetch_uint_to(64, shard_pfx)) { + auto pow2 = (1ULL << (63 - shard_pfx_len)); + if (!(shard_pfx & (pow2 - 1))) { + shard_pfx |= pow2; + return true; + } + } + + invalidate(); + return false; +} + +MsgProcessedUptoCollection::MsgProcessedUptoCollection(ton::ShardIdFull _owner, Ref cs_ref) + : owner(_owner) { + vm::Dictionary dict{std::move(cs_ref), 96}; + valid = dict.check_for_each([&](Ref value, td::ConstBitPtr key, int n) -> bool { + if (value->size_ext() != 64 + 256) { + return false; + } + list.emplace_back(); + MsgProcessedUpto& z = list.back(); + z.shard = key.get_uint(64); + z.mc_seqno = (unsigned)((key + 64).get_uint(32)); + z.last_inmsg_lt = value.write().fetch_ulong(64); + // std::cerr << "ProcessedUpto shard " << std::hex << z.shard << std::dec << std::endl; + return value.write().fetch_bits_to(z.last_inmsg_hash) && z.shard && ton::shard_contains(owner.shard, z.shard); + }); +} + +std::unique_ptr MsgProcessedUptoCollection::unpack(ton::ShardIdFull _owner, + Ref cs_ref) { + auto v = std::make_unique(_owner, std::move(cs_ref)); + return v && v->valid ? std::move(v) : std::unique_ptr{}; +} + +bool MsgProcessedUpto::contains(const MsgProcessedUpto& other) const & { + return ton::shard_is_ancestor(shard, other.shard) && mc_seqno >= other.mc_seqno && + (last_inmsg_lt > other.last_inmsg_lt || + (last_inmsg_lt == other.last_inmsg_lt && !(last_inmsg_hash < other.last_inmsg_hash))); +} + +bool MsgProcessedUpto::contains(ton::ShardId other_shard, ton::LogicalTime other_lt, td::ConstBitPtr other_hash, + ton::BlockSeqno other_mc_seqno) const & { + return ton::shard_is_ancestor(shard, other_shard) && mc_seqno >= other_mc_seqno && + (last_inmsg_lt > other_lt || (last_inmsg_lt == other_lt && !(last_inmsg_hash < other_hash))); +} + +bool MsgProcessedUptoCollection::insert(ton::BlockSeqno mc_seqno, ton::LogicalTime last_proc_lt, + td::ConstBitPtr last_proc_hash) { + if (!last_proc_lt) { + return false; + } + for (const auto& z : list) { + if (z.contains(owner.shard, last_proc_lt, last_proc_hash, mc_seqno)) { + return true; + } + } + list.emplace_back(owner.shard, mc_seqno, last_proc_lt, last_proc_hash); + return true; +} + +bool MsgProcessedUptoCollection::insert_infty(ton::BlockSeqno mc_seqno, ton::LogicalTime last_proc_lt) { + return insert(mc_seqno, last_proc_lt, td::Bits256::ones().bits()); +} + +bool MsgProcessedUptoCollection::is_reduced() const { + if (!valid) { + return false; + } + for (auto it = list.begin(); it < list.end(); ++it) { + for (auto it2 = it + 1; it2 < list.end(); ++it2) { + if (it->contains(*it2) || it2->contains(*it)) { + return false; + } + } + } + return true; +} + +bool MsgProcessedUptoCollection::contains(const MsgProcessedUpto& p_upto) const { + for (const auto& z : list) { + if (z.contains(p_upto)) { + return true; + } + } + return false; +} + +bool MsgProcessedUptoCollection::contains(const MsgProcessedUptoCollection& other) const { + for (const auto& w : other.list) { + if (!contains(w)) { + return false; + } + } + return true; +} + +const MsgProcessedUpto* MsgProcessedUptoCollection::is_simple_update_of(const MsgProcessedUptoCollection& other, + bool& ok) const { + ok = false; + if (!contains(other)) { + LOG(DEBUG) << "does not cointain the previous value"; + return nullptr; + } + if (other.contains(*this)) { + LOG(DEBUG) << "coincides with the previous value"; + ok = true; + return nullptr; + } + const MsgProcessedUpto* found = nullptr; + for (const auto& z : list) { + if (!other.contains(z)) { + if (found) { + LOG(DEBUG) << "has more than two new entries"; + return found; // ok = false: update is not simple + } + found = &z; + } + } + ok = true; + return found; +} + +ton::BlockSeqno MsgProcessedUptoCollection::min_mc_seqno() const { + ton::BlockSeqno min_mc_seqno = ~0U; + for (const auto& z : list) { + min_mc_seqno = std::min(min_mc_seqno, z.mc_seqno); + } + return min_mc_seqno; +} + +bool MsgProcessedUptoCollection::compactify() { + std::sort(list.begin(), list.end()); + std::size_t i, j, k = 0, m = 0, n = list.size(); + std::vector mark(n, false); + assert(mark.size() == n); + for (i = 0; i < n; i++) { + for (j = 0; j < n; j++) { + if (j != i && !mark[j] && list[j].contains(list[i])) { + mark[i] = true; + ++m; + break; + } + } + } + if (m) { + for (i = 0; i < n; i++) { + if (!mark[i]) { + list[k++] = list[i]; + } + } + list.resize(k); + } + return true; +} + +bool MsgProcessedUptoCollection::pack(vm::CellBuilder& cb) { + if (!compactify()) { + return false; + } + vm::Dictionary dict{96}; + for (const auto& z : list) { + td::BitArray<96> key; + vm::CellBuilder cb2; + key.bits().store_uint(z.shard, 64); + (key.bits() + 64).store_uint(z.mc_seqno, 32); + if (!(cb2.store_long_bool(z.last_inmsg_lt) && cb2.store_bits_bool(z.last_inmsg_hash) && + dict.set_builder(key, cb2, vm::Dictionary::SetMode::Add))) { + return false; + } + } + return std::move(dict).append_dict_to_bool(cb); +} + +bool MsgProcessedUptoCollection::split(ton::ShardIdFull new_owner) { + if (!ton::shard_is_ancestor(owner, new_owner)) { + return false; + } + if (owner == new_owner) { + return true; + } + std::size_t n = list.size(), i, j = 0; + for (i = 0; i < n; i++) { + if (ton::shard_intersects(list[i].shard, new_owner.shard)) { + list[i].shard = ton::shard_intersection(list[i].shard, new_owner.shard); + if (j < i) { + list[j] = std::move(list[i]); + } + j++; + } + } + list.resize(j); + owner = new_owner; + return compactify(); +} + +bool MsgProcessedUptoCollection::combine_with(const MsgProcessedUptoCollection& other) { + if (!(other.owner == owner || ton::shard_is_sibling(other.owner, owner))) { + return false; + } + list.insert(list.end(), other.list.begin(), other.list.end()); + if (owner != other.owner) { + owner = ton::shard_parent(owner); + } + return compactify(); +} + +bool MsgProcessedUpto::already_processed(const EnqueuedMsgDescr& msg) const { + // LOG(DEBUG) << "compare msg (" << msg.lt_ << "," << msg.hash_.to_hex() << ") against record's (" << last_inmsg_lt + // << "," << last_inmsg_hash.to_hex() << ")"; + if (msg.lt_ > last_inmsg_lt) { + return false; + } + if (!ton::shard_contains(shard, msg.next_prefix_.account_id_prefix)) { + return false; + } + if (msg.lt_ == last_inmsg_lt && last_inmsg_hash < msg.hash_) { + return false; + } + if (ton::shard_contains(shard, msg.cur_prefix_.account_id_prefix)) { + // ? enable this branch only if an extra boolean parameter is set ? + return true; + } + auto shard_end_lt = compute_shard_end_lt(msg.cur_prefix_); + // LOG(DEBUG) << "enqueued_lt = " << msg.enqueued_lt_ << " , shard_end_lt = " << shard_end_lt; + return msg.enqueued_lt_ < shard_end_lt; +} + +bool MsgProcessedUptoCollection::already_processed(const EnqueuedMsgDescr& msg) const { + // LOG(DEBUG) << "checking message with cur_addr=" << msg.cur_prefix_.to_str() + // << " next_addr=" << msg.next_prefix_.to_str() << " against ProcessedUpto of neighbor " << owner.to_str(); + if (!ton::shard_contains(owner, msg.next_prefix_)) { + return false; + } + for (const auto& rec : list) { + if (rec.already_processed(msg)) { + return true; + } + } + return false; +} + +bool MsgProcessedUptoCollection::for_each_mcseqno(std::function func) const { + for (const auto& entry : list) { + if (!func(entry.mc_seqno)) { + return false; + } + } + return true; +} + +// unpacks some fields from EnqueuedMsg +bool EnqueuedMsgDescr::unpack(vm::CellSlice& cs) { + block::gen::EnqueuedMsg::Record enq; + block::tlb::MsgEnvelope::Record_std env; + block::gen::CommonMsgInfo::Record_int_msg_info info; + if (!(tlb::unpack(cs, enq) && tlb::unpack_cell(enq.out_msg, env) && tlb::unpack_cell_inexact(env.msg, info))) { + return invalidate(); + } + src_prefix_ = block::tlb::t_MsgAddressInt.get_prefix(std::move(info.src)); + dest_prefix_ = block::tlb::t_MsgAddressInt.get_prefix(std::move(info.dest)); + if (!(src_prefix_.is_valid() && dest_prefix_.is_valid())) { + return invalidate(); + } + cur_prefix_ = interpolate_addr(src_prefix_, dest_prefix_, env.cur_addr); + next_prefix_ = interpolate_addr(src_prefix_, dest_prefix_, env.next_addr); + lt_ = info.created_lt; + enqueued_lt_ = enq.enqueued_lt; + hash_ = env.msg->get_hash().bits(); + msg_ = std::move(env.msg); + msg_env_ = std::move(enq.out_msg); + return true; +} + +bool EnqueuedMsgDescr::check_key(td::ConstBitPtr key) const { + return key.get_int(32) == next_prefix_.workchain && (key + 32).get_uint(64) == next_prefix_.account_id_prefix && + hash_ == key + 96; +} + +bool ParamLimits::deserialize(vm::CellSlice& cs) { + return cs.fetch_ulong(8) == 0xc3 // param_limits#c3 + && cs.fetch_uint_to(32, limits_[0]) // underload:uint32 + && cs.fetch_uint_to(32, limits_[1]) // soft_limit:uint32 + && cs.fetch_uint_to(32, limits_[3]) // hard_limit:uint32 + && limits_[0] <= limits_[1] // { underload <= soft_limit } + && limits_[1] <= limits_[3] // { soft_limit <= hard_limit } = ParamLimits; + && compute_medium_limit(); +} + +bool BlockLimits::deserialize(vm::CellSlice& cs) { + return cs.fetch_ulong(8) == 0x5d // block_limits#5d + && bytes.deserialize(cs) // bytes:ParamLimits + && gas.deserialize(cs) // gas:ParamLimits + && lt_delta.deserialize(cs); // lt_delta:ParamLimits +} + +int ParamLimits::classify(td::uint64 value) const { + int a = -1, b = limits_cnt; + while (b - a > 1) { + int c = (a + b) >> 1; + if (value >= limits_[c]) { + a = c; + } else { + b = c; + } + } + return a + 1; +} + +bool ParamLimits::fits(unsigned cls, td::uint64 value) const { + return cls >= limits_cnt || value < limits_[cls]; +} + +int BlockLimits::classify_size(td::uint64 size) const { + return bytes.classify(size); +} + +int BlockLimits::classify_gas(td::uint64 gas_value) const { + return gas.classify(gas_value); +} + +int BlockLimits::classify_lt(ton::LogicalTime lt) const { + return lt_delta.classify(lt - start_lt); +} + +int BlockLimits::classify(td::uint64 size, td::uint64 gas, ton::LogicalTime lt) const { + return std::max(std::max(classify_size(size), classify_gas(gas)), classify_lt(lt)); +} + +bool BlockLimits::fits(unsigned cls, td::uint64 size, td::uint64 gas_value, ton::LogicalTime lt) const { + return bytes.fits(cls, size) && gas.fits(cls, gas_value) && lt_delta.fits(cls, lt - start_lt); +} + +td::uint64 BlockLimitStatus::estimate_block_size(const vm::NewCellStorageStat::Stat* extra) const { + auto sum = st_stat.get_total_stat(); + if (extra) { + sum += *extra; + } + return 2000 + (sum.bits >> 3) + sum.cells * 12 + sum.internal_refs * 3 + sum.external_refs * 40 + accounts * 200 + + transactions * 200 + (extra ? 200 : 0); +} + +int BlockLimitStatus::classify() const { + return limits.classify(estimate_block_size(), gas_used, cur_lt); +} + +bool BlockLimitStatus::fits(unsigned cls) const { + return cls >= ParamLimits::limits_cnt || + (limits.gas.fits(cls, gas_used) && limits.lt_delta.fits(cls, cur_lt - limits.start_lt) && + limits.bytes.fits(cls, estimate_block_size())); +} + +bool BlockLimitStatus::would_fit(unsigned cls, ton::LogicalTime end_lt, td::uint64 more_gas, + const vm::NewCellStorageStat::Stat* extra) const { + return cls >= ParamLimits::limits_cnt || (limits.gas.fits(cls, gas_used + more_gas) && + limits.lt_delta.fits(cls, std::max(cur_lt, end_lt) - limits.start_lt) && + limits.bytes.fits(cls, estimate_block_size(extra))); +} + +// SETS: account_dict, shard_libraries_, mc_state_extra +// total_balance{,_extra}, total_validator_fees +// SETS: out_msg_queue, processed_upto_, ihr_pending (via unpack_out_msg_queue_info) +// SETS: utime_, lt_ +td::Status ShardState::unpack_state(ton::BlockIdExt blkid, Ref prev_state_root) { + if (!blkid.is_valid()) { + return td::Status::Error(-666, "invalid block id supplied to ShardState::unpack"); + } + if (prev_state_root.is_null()) { + return td::Status::Error(-666, "the root cell supplied for the shardchain state "s + blkid.to_str() + " is null"); + } + block::gen::ShardStateUnsplit::Record state; + if (!tlb::unpack_cell(prev_state_root, state)) { + return td::Status::Error(-666, "cannot unpack header of shardchain state "s + blkid.to_str()); + } + if ((unsigned)state.seq_no != blkid.seqno()) { + return td::Status::Error( + -666, PSTRING() << "shardchain state for " << blkid.to_str() << " has incorrect seqno " << state.seq_no); + } + auto shard1 = ton::ShardIdFull(block::ShardId{state.shard_id}); + if (shard1 != blkid.shard_full()) { + return td::Status::Error(-666, "shardchain state for "s + blkid.to_str() + + " corresponds to incorrect workchain or shard " + shard1.to_str()); + } + if (state.vert_seq_no) { + return td::Status::Error( + -666, "shardchain state for "s + blkid.to_str() + " has non-zero vert_seq_no, which is unsupported"); + } + id_ = blkid; + root_ = std::move(prev_state_root); + before_split_ = state.before_split; + account_dict_ = std::make_unique( + vm::load_cell_slice(std::move(state.accounts)).prefetch_ref(), 256, block::tlb::aug_ShardAccounts); + // check that all keys in account_dict have correct prefixes + td::BitArray<64> acc_pfx{(long long)shard1.shard}; + int acc_pfx_len = shard_prefix_length(shard1); + if (!account_dict_->has_common_prefix(acc_pfx.bits(), acc_pfx_len)) { + return td::Status::Error(-666, "account dictionary of previous state of "s + id_.to_str() + " does not have " + + acc_pfx.bits().to_hex(acc_pfx_len) + " as common key prefix"); + } + // get overload / underload history + overload_history_ = state.r1.overload_history; + underload_history_ = state.r1.underload_history; + // get shard libraries + shard_libraries_ = std::make_unique(state.r1.libraries->prefetch_ref(), 256); + if (!shard_libraries_->is_empty() && !shard1.is_masterchain()) { + return td::Status::Error(-666, + "shardchain state "s + id_.to_str() + + " has a non-trivial shard libraries collection, but it is not in the masterchain"); + } + mc_state_extra_ = state.custom->prefetch_ref(); + vm::CellSlice cs{*state.r1.master_ref}; // master_ref:(Maybe BlkMasterInfo) + if ((int)cs.fetch_ulong(1) == 1) { + if (!(block::tlb::t_ExtBlkRef.unpack(cs, mc_blk_ref_, &mc_blk_lt_) && cs.empty_ext())) { + return td::Status::Error(-666, "cannot unpack master_ref in shardchain state of "s + id_.to_str()); + } + mc_blk_seqno_ = mc_blk_ref_.seqno(); + } else { + mc_blk_seqno_ = 0; + mc_blk_lt_ = 0; + mc_blk_ref_.invalidate(); + } + min_ref_mc_seqno_ = state.min_ref_mc_seqno; + global_id_ = state.global_id; + utime_ = state.gen_utime; + lt_ = state.gen_lt; + if (!total_balance_.validate_unpack(state.r1.total_balance)) { + return td::Status::Error( + -666, "cannot unpack total_balance:CurrencyCollection from previous ShardState of "s + id_.to_str()); + } + auto accounts_extra = account_dict_->get_root_extra(); + CurrencyCollection old_total_balance; + if (!(accounts_extra.write().advance(5) && old_total_balance.fetch(accounts_extra.write()))) { + return td::Status::Error( + -666, + "cannot extract total account balance from ShardAccounts contained in previous ShardState of "s + id_.to_str()); + } + if (old_total_balance != total_balance_) { + return td::Status::Error(-666, "invalid previous ShardState for "s + id_.to_str() + ": declared total balance " + + total_balance_.to_str() + " differs from " + old_total_balance.to_str() + + " obtained by summing over all Accounts"); + } + if (!(total_validator_fees_.validate_unpack(state.r1.total_validator_fees) && !total_validator_fees_.has_extra())) { + return td::Status::Error( + -666, "cannot unpack total_validator_fees:CurrencyCollection from previous ShardState of "s + id_.to_str()); + } + if (is_masterchain()) { + if (mc_state_extra_.is_null()) { + return td::Status::Error(-666, "ShardState of "s + id_.to_str() + " does not contain McStateExtra"); + } + block::gen::McStateExtra::Record extra; + if (!tlb::unpack_cell(mc_state_extra_, extra)) { + return td::Status::Error(-666, "cannot unpack McStateExtra in ShardState of "s + id_.to_str()); + } + if (!global_balance_.validate_unpack(extra.global_balance)) { + return td::Status::Error(-666, "ShardState of "s + id_.to_str() + " does not contain a valid global_balance"); + } + } + return unpack_out_msg_queue_info(std::move(state.out_msg_queue_info)); +} + +// SETS: out_msg_queue, processed_upto_, ihr_pending +td::Status ShardState::unpack_out_msg_queue_info(Ref out_msg_queue_info) { + block::gen::OutMsgQueueInfo::Record qinfo; + if (!tlb::unpack_cell(std::move(out_msg_queue_info), qinfo)) { + return td::Status::Error(-666, "cannot unpack OutMsgQueueInfo in the state of "s + id_.to_str()); + } + out_msg_queue_ = + std::make_unique(std::move(qinfo.out_queue), 352, block::tlb::aug_OutMsgQueue); + if (verbosity >= 3 * 0) { + LOG(DEBUG) << "unpacking ProcessedUpto of our previous block " << id_.to_str(); + block::gen::t_ProcessedInfo.print(std::cerr, qinfo.proc_info); + } + if (!block::gen::t_ProcessedInfo.validate_csr(qinfo.proc_info)) { + return td::Status::Error( + -666, "ProcessedInfo in the state of "s + id_.to_str() + " is invalid according to automated validity checks"); + } + if (!block::gen::t_IhrPendingInfo.validate_csr(qinfo.ihr_pending)) { + return td::Status::Error( + -666, "IhrPendingInfo in the state of "s + id_.to_str() + " is invalid according to automated validity checks"); + } + processed_upto_ = block::MsgProcessedUptoCollection::unpack(ton::ShardIdFull(id_), std::move(qinfo.proc_info)); + ihr_pending_ = std::make_unique(std::move(qinfo.ihr_pending), 320); + auto shard1 = id_.shard_full(); + td::BitArray<64> pfx{(long long)shard1.shard}; + int pfx_len = shard_prefix_length(shard1); + if (!ihr_pending_->has_common_prefix(pfx.bits(), pfx_len)) { + return td::Status::Error(-666, "IhrPendingInfo in the state of "s + id_.to_str() + " does not have " + + pfx.bits().to_hex(pfx_len) + " as common key prefix"); + } + return td::Status::OK(); +} + +// UPDATES: prev_state_utime_, prev_state_lt_ +bool ShardState::update_prev_utime_lt(ton::UnixTime& prev_utime, ton::LogicalTime& prev_lt) const { + prev_utime = std::max(prev_utime, utime_); + prev_lt = std::max(prev_lt, lt_); + return true; +} + +td::Status ShardState::check_before_split(bool req_before_split) const { + CHECK(id_.is_valid()); + if (before_split_ != req_before_split) { + return td::Status::Error(PSTRING() << "previous state for " << id_.to_str() << " has before_split=" << before_split_ + << ", but we have after_split=" << req_before_split); + } + return td::Status::OK(); +} + +td::Status ShardState::check_global_id(int req_global_id) const { + if (global_id_ != req_global_id) { + return td::Status::Error(-666, PSTRING() << "global blockchain id mismatch in shard state of " << id_.to_str() + << ": expected " << req_global_id << ", found " << global_id_); + } + return td::Status::OK(); +} + +td::Status ShardState::check_mc_blk_seqno(ton::BlockSeqno last_mc_block_seqno) const { + if (mc_blk_seqno_ > last_mc_block_seqno) { + return td::Status::Error( + -666, PSTRING() << "previous block refers to masterchain block with seqno " << mc_blk_seqno_ + << " larger than the latest known masterchain block seqno " << last_mc_block_seqno); + } + return td::Status::OK(); +} + +td::Status ShardState::unpack_state_ext(ton::BlockIdExt id, Ref state_root, int global_id, + ton::BlockSeqno prev_mc_block_seqno, bool after_split, bool clear_history, + std::function for_each_mcseqno_func) { + TRY_STATUS(unpack_state(id, std::move(state_root))); + TRY_STATUS(check_global_id(global_id)); + TRY_STATUS(check_mc_blk_seqno(prev_mc_block_seqno)); + TRY_STATUS(check_before_split(after_split)); + clear_load_history_if(clear_history); + if (!for_each_mcseqno(std::move(for_each_mcseqno_func))) { + return td::Status::Error( + -666, "cannot perform necessary actions for each mc_seqno mentioned in ProcessedUpto of "s + id_.to_str()); + } + return td::Status::OK(); +} + +td::Status ShardState::merge_with(ShardState& sib) { + // 1. check that the two states are valid and belong to sibling shards + if (!is_valid() || !sib.is_valid()) { + return td::Status::Error(-666, "cannot merge invalid or uninitialized states"); + } + if (!ton::shard_is_sibling(id_.shard_full(), sib.id_.shard_full())) { + return td::Status::Error(-666, "cannot merge non-sibling states of "s + id_.to_str() + " and " + sib.id_.to_str()); + } + ton::ShardIdFull shard = ton::shard_parent(id_.shard_full()); + // 2. compute total_balance and total_validator_fees + total_balance_ += std::move(sib.total_balance_); + if (!total_balance_.is_valid()) { + return td::Status::Error(-667, "cannot add total_balance_extra of the two states being merged"); + } + total_validator_fees_ += std::move(sib.total_validator_fees_); + // 3. merge account_dict with sibling_account_dict + LOG(DEBUG) << "merging account dictionaries"; + if (!account_dict_->combine_with(*sib.account_dict_)) { + return td::Status::Error(-666, "cannot merge account dictionaries of the two ancestors"); + } + sib.account_dict_.reset(); + // 3.1. check that all keys in merged account_dict have correct prefixes + td::BitArray<64> pfx{(long long)shard.shard}; + int pfx_len = shard_prefix_length(shard); + if (!account_dict_->has_common_prefix(pfx.bits(), pfx_len)) { + return td::Status::Error(-666, "merged account dictionary of previous states of "s + shard.to_str() + + " does not have " + pfx.bits().to_hex(pfx_len) + " as common key prefix"); + } + // 3.2. check total balance of the new account_dict + auto accounts_extra = account_dict_->get_root_extra(); + CurrencyCollection old_total_balance; + if (!(accounts_extra.write().advance(5) && old_total_balance.fetch(accounts_extra.write()))) { + return td::Status::Error(-666, "cannot extract total account balance from merged accounts dictionary"); + } + if (old_total_balance != total_balance_) { + return td::Status::Error( + -666, + "invalid merged account dictionary: declared total balance differs from one obtained by summing over all " + "Accounts"); + } + // 4. merge shard libraries + CHECK(shard_libraries_->is_empty() && sib.shard_libraries_->is_empty()); + // 5. merge out_msg_queue + LOG(DEBUG) << "merging outbound message queues"; + if (!out_msg_queue_->combine_with(*sib.out_msg_queue_)) { + return td::Status::Error(-666, "cannot merge outbound message queues of the two ancestor states"); + } + sib.out_msg_queue_.reset(); + // 6. merge processed_upto + LOG(DEBUG) << "merging ProcessedUpto structures"; + if (!processed_upto_->combine_with(*sib.processed_upto_)) { + return td::Status::Error(-666, "cannot merge ProcessedUpto structures of the two ancestor states"); + } + sib.processed_upto_.reset(); + // 7. merge ihr_pending + LOG(DEBUG) << "merging IhrPendingInfo"; + if (!ihr_pending_->combine_with(*sib.ihr_pending_)) { + return td::Status::Error(-666, "cannot merge IhrPendingInfo of the two ancestors"); + } + sib.ihr_pending_.reset(); + // 7.1. check whether all keys of the new ihr_pending have correct prefix + if (!ihr_pending_->has_common_prefix(pfx.bits(), pfx_len)) { + return td::Status::Error(-666, "merged IhrPendingInfo of the two previous states of "s + shard.to_str() + + " does not have " + pfx.bits().to_hex(pfx_len) + " as common key prefix"); + } + // 8. compute merged utime_ and lt_ + utime_ = std::max(utime_, sib.utime_); + lt_ = std::max(lt_, sib.lt_); + // 9. compute underload & overload history + underload_history_ = overload_history_ = 0; + // Anything else? add here + // ... + + // 10. compute new root + if (!block::gen::t_ShardState.cell_pack_split_state(root_, std::move(root_), std::move(sib.root_))) { + return td::Status::Error(-667, "cannot construct a virtual split_state after a merge"); + } + // 11. invalidate sibling, change id_ to the (virtual) common parent + sib.invalidate(); + id_.id.shard = shard.shard; + id_.file_hash.set_zero(); + id_.root_hash.set_zero(); + return td::Status::OK(); +} + +td::Result> ShardState::compute_split_out_msg_queue( + ton::ShardIdFull subshard) { + auto shard = id_.shard_full(); + if (!ton::shard_is_parent(shard, subshard)) { + return td::Status::Error(-666, "cannot split subshard "s + subshard.to_str() + " from state of " + id_.to_str() + + " because it is not a parent"); + } + CHECK(out_msg_queue_); + auto subqueue = std::make_unique(*out_msg_queue_); + int res = block::filter_out_msg_queue(*subqueue, shard, subshard); + if (res < 0) { + return td::Status::Error(-666, "error splitting OutMsgQueue of "s + id_.to_str()); + } + LOG(DEBUG) << "OutMsgQueue split counter: " << res << " messages"; + return std::move(subqueue); +} + +td::Result> ShardState::compute_split_processed_upto( + ton::ShardIdFull subshard) { + if (!ton::shard_is_parent(id_.shard_full(), subshard)) { + return td::Status::Error(-666, "cannot split subshard "s + subshard.to_str() + " from state of " + id_.to_str() + + " because it is not a parent"); + } + CHECK(processed_upto_); + auto sub_processed_upto = std::make_shared(*processed_upto_); + if (!sub_processed_upto->split(subshard)) { + return td::Status::Error(-666, "error splitting ProcessedUpto of "s + id_.to_str()); + } + return std::move(sub_processed_upto); +} + +td::Status ShardState::split(ton::ShardIdFull subshard) { + if (!ton::shard_is_parent(id_.shard_full(), subshard)) { + return td::Status::Error(-666, "cannot split subshard "s + subshard.to_str() + " from state of " + id_.to_str() + + " because it is not a parent"); + } + // Have to split: + // 1. account_dict + LOG(DEBUG) << "splitting account dictionary"; + td::BitArray<64> pfx{(long long)subshard.shard}; + int pfx_len = shard_prefix_length(subshard); + CHECK(account_dict_); + CHECK(account_dict_->cut_prefix_subdict(pfx.bits(), pfx_len)); + CHECK(account_dict_->has_common_prefix(pfx.bits(), pfx_len)); + // 2. out_msg_queue + LOG(DEBUG) << "splitting OutMsgQueue"; + auto shard1 = id_.shard_full(); + CHECK(ton::shard_is_parent(shard1, subshard)); + CHECK(out_msg_queue_); + int res1 = block::filter_out_msg_queue(*out_msg_queue_, shard1, subshard); + if (res1 < 0) { + return td::Status::Error(-666, "error splitting OutMsgQueue of "s + id_.to_str()); + } + LOG(DEBUG) << "split counters: " << res1; + // 3. processed_upto + LOG(DEBUG) << "splitting ProcessedUpto"; + CHECK(processed_upto_); + if (!processed_upto_->split(subshard)) { + return td::Status::Error(-666, "error splitting ProcessedUpto of "s + id_.to_str()); + } + // 4. ihr_pending + LOG(DEBUG) << "splitting IhrPending"; + CHECK(ihr_pending_->cut_prefix_subdict(pfx.bits(), pfx_len)); + CHECK(ihr_pending_->has_common_prefix(pfx.bits(), pfx_len)); + // 5. adjust total_balance + LOG(DEBUG) << "splitting total_balance"; + auto old_total_balance = total_balance_; + auto accounts_extra = account_dict_->get_root_extra(); + if (!(accounts_extra.write().advance(5) && total_balance_.validate_unpack(accounts_extra))) { + LOG(ERROR) << "cannot unpack CurrencyCollection from the root of newly-split accounts dictionary"; + return td::Status::Error( + -666, "error splitting total balance in account dictionary of shardchain state "s + id_.to_str()); + } + LOG(DEBUG) << "split total balance from " << old_total_balance.to_str() << " to our share of " + << total_balance_.to_str(); + // 6. adjust total_fees + LOG(DEBUG) << "split total validator fees (current value is " << total_validator_fees_.to_str() << ")"; + total_validator_fees_.grams = (total_validator_fees_.grams + is_right_child(subshard)) >> 1; + LOG(DEBUG) << "new total_validator_fees is " << total_validator_fees_.to_str(); + // NB: if total_fees_extra will be allowed to be non-empty, split it here too + // 7. reset overload/underload history + overload_history_ = underload_history_ = 0; + // 999. anything else? + id_.id.shard = subshard.shard; + id_.file_hash.set_zero(); + id_.root_hash.set_zero(); + return td::Status::OK(); +} + +int filter_out_msg_queue(vm::AugmentedDictionary& out_queue, ton::ShardIdFull old_shard, ton::ShardIdFull subshard) { + return out_queue.filter([subshard, old_shard](vm::CellSlice& cs, td::ConstBitPtr key, int key_len) -> int { + CHECK(key_len == 352); + LOG(DEBUG) << "scanning OutMsgQueue entry with key " << key.to_hex(key_len); + block::tlb::MsgEnvelope::Record_std env; + block::gen::CommonMsgInfo::Record_int_msg_info info; + if (!(cs.size_ext() == 0x10080 // (uint64) enqueued_lt:uint64 out_msg:^MsgEnvelope + && tlb::unpack_cell(cs.prefetch_ref(), env) && tlb::unpack_cell_inexact(env.msg, info))) { + LOG(ERROR) << "cannot unpack OutMsgQueue entry with key " << key.to_hex(key_len); + return -1; + } + auto src_prefix = block::tlb::t_MsgAddressInt.get_prefix(info.src); + auto dest_prefix = block::tlb::t_MsgAddressInt.get_prefix(info.dest); + auto cur_prefix = block::interpolate_addr(src_prefix, dest_prefix, env.cur_addr); + if (!(src_prefix.is_valid() && dest_prefix.is_valid() && cur_prefix.is_valid())) { + LOG(ERROR) << "OutMsgQueue message with key " << key.to_hex(key_len) + << " has invalid source or destination address"; + return -1; + } + if (!ton::shard_contains(old_shard, cur_prefix)) { + LOG(ERROR) << "OutMsgQueue message with key " << key.to_hex(key_len) + << " does not contain current address belonging to shard " << old_shard.to_str(); + return -1; + } + return ton::shard_contains(subshard, cur_prefix); + }); +} + +bool CurrencyCollection::validate() const { + return is_valid() && td::sgn(grams) >= 0 && validate_extra(); +} + +bool CurrencyCollection::validate_extra() const { + if (extra.is_null()) { + return true; + } + vm::CellBuilder cb; + return cb.store_maybe_ref(extra) && block::tlb::t_ExtraCurrencyCollection.validate_ref(cb.finalize()); +} + +bool CurrencyCollection::add(const CurrencyCollection& a, const CurrencyCollection& b, CurrencyCollection& c) { + return (a.is_valid() && b.is_valid() && (c.grams = a.grams + b.grams).not_null() && c.grams->is_valid() && + add_extra_currency(a.extra, b.extra, c.extra)) || + c.invalidate(); +} + +bool CurrencyCollection::add(const CurrencyCollection& a, CurrencyCollection&& b, CurrencyCollection& c) { + return (a.is_valid() && b.is_valid() && (c.grams = a.grams + std::move(b.grams)).not_null() && c.grams->is_valid() && + add_extra_currency(a.extra, std::move(b.extra), c.extra)) || + c.invalidate(); +} + +CurrencyCollection& CurrencyCollection::operator+=(const CurrencyCollection& other) { + if (!is_valid()) { + return *this; + } + if (!(other.is_valid() && (grams += other.grams).not_null() && grams->is_valid() && + add_extra_currency(extra, other.extra, extra))) { + invalidate(); + } + return *this; +} + +CurrencyCollection& CurrencyCollection::operator+=(CurrencyCollection&& other) { + if (!is_valid()) { + return *this; + } + if (!(other.is_valid() && (grams += std::move(other.grams)).not_null() && grams->is_valid() && + add_extra_currency(extra, std::move(other.extra), extra))) { + invalidate(); + } + return *this; +} + +CurrencyCollection& CurrencyCollection::operator+=(td::RefInt256 other_grams) { + if (!is_valid()) { + return *this; + } + if (!(other_grams.not_null() && (grams += other_grams).not_null())) { + invalidate(); + } + return *this; +} + +CurrencyCollection CurrencyCollection::operator+(const CurrencyCollection& other) const { + CurrencyCollection res; + add(*this, other, res); + return res; +} + +CurrencyCollection CurrencyCollection::operator+(CurrencyCollection&& other) const { + CurrencyCollection res; + add(*this, std::move(other), res); + return res; +} + +CurrencyCollection CurrencyCollection::operator+(td::RefInt256 other_grams) { + if (!is_valid()) { + return *this; + } + auto sum = grams + other_grams; + if (sum.not_null()) { + return CurrencyCollection{std::move(sum), extra}; + } else { + return CurrencyCollection{}; + } +} + +bool CurrencyCollection::sub(const CurrencyCollection& a, const CurrencyCollection& b, CurrencyCollection& c) { + return (a.is_valid() && b.is_valid() && (c.grams = a.grams - b.grams).not_null() && c.grams->is_valid() && + td::sgn(c.grams) >= 0 && sub_extra_currency(a.extra, b.extra, c.extra)) || + c.invalidate(); +} + +bool CurrencyCollection::sub(const CurrencyCollection& a, CurrencyCollection&& b, CurrencyCollection& c) { + return (a.is_valid() && b.is_valid() && (c.grams = a.grams - std::move(b.grams)).not_null() && c.grams->is_valid() && + td::sgn(c.grams) >= 0 && sub_extra_currency(a.extra, std::move(b.extra), c.extra)) || + c.invalidate(); +} + +CurrencyCollection& CurrencyCollection::operator-=(const CurrencyCollection& other) { + if (!is_valid()) { + return *this; + } + if (!(other.is_valid() && (grams -= other.grams).not_null() && grams->is_valid() && td::sgn(grams) >= 0 && + sub_extra_currency(extra, other.extra, extra))) { + invalidate(); + } + return *this; +} + +CurrencyCollection& CurrencyCollection::operator-=(CurrencyCollection&& other) { + if (!is_valid()) { + return *this; + } + if (!(other.is_valid() && (grams -= std::move(other.grams)).not_null() && grams->is_valid() && td::sgn(grams) >= 0 && + sub_extra_currency(extra, std::move(other.extra), extra))) { + invalidate(); + } + return *this; +} + +CurrencyCollection& CurrencyCollection::operator-=(td::RefInt256 other_grams) { + if (!is_valid()) { + return *this; + } + if (!(other_grams.not_null() && (grams -= other_grams).not_null() && td::sgn(grams) >= 0)) { + invalidate(); + } + return *this; +} + +CurrencyCollection CurrencyCollection::operator-(const CurrencyCollection& other) const { + CurrencyCollection res; + sub(*this, other, res); + return res; +} + +CurrencyCollection CurrencyCollection::operator-(CurrencyCollection&& other) const { + CurrencyCollection res; + sub(*this, std::move(other), res); + return res; +} + +CurrencyCollection CurrencyCollection::operator-(td::RefInt256 other_grams) const { + if (!(is_valid() && other_grams.not_null())) { + return {}; + } + auto x = grams - other_grams; + if (td::sgn(x) >= 0) { + return CurrencyCollection{std::move(x), extra}; + } else { + return {}; + } +} + +bool CurrencyCollection::operator==(const CurrencyCollection& other) const { + return is_valid() && other.is_valid() && !td::cmp(grams, other.grams) && + (extra.not_null() == other.extra.not_null()) && + (extra.is_null() || extra->get_hash() == other.extra->get_hash()); +} + +bool CurrencyCollection::operator>=(const CurrencyCollection& other) const { + Ref tmp; + return is_valid() && other.is_valid() && td::cmp(grams, other.grams) >= 0 && + sub_extra_currency(extra, other.extra, tmp); +} + +bool CurrencyCollection::store(vm::CellBuilder& cb) const { + return is_valid() && store_CurrencyCollection(cb, grams, extra); +} + +bool CurrencyCollection::store_or_zero(vm::CellBuilder& cb) const { + return is_valid() ? store(cb) : cb.store_long_bool(0, 5); +} + +bool CurrencyCollection::fetch(vm::CellSlice& cs) { + return block::tlb::t_CurrencyCollection.unpack_special(cs, *this, true) || invalidate(); +} + +bool CurrencyCollection::fetch_exact(vm::CellSlice& cs) { + return block::tlb::t_CurrencyCollection.unpack_special(cs, *this, false) || invalidate(); +} + +bool CurrencyCollection::unpack(Ref csr) { + return unpack_CurrencyCollection(std::move(csr), grams, extra) || invalidate(); +} + +bool CurrencyCollection::validate_unpack(Ref csr) { + return (csr.not_null() && block::tlb::t_CurrencyCollection.validate(*csr) && + unpack_CurrencyCollection(std::move(csr), grams, extra)) || + invalidate(); +} + +Ref CurrencyCollection::pack() const { + vm::CellBuilder cb; + if (store(cb)) { + return vm::load_cell_slice_ref(cb.finalize()); + } else { + return {}; + } +} + +bool CurrencyCollection::show(std::ostream& os) const { + if (!is_valid()) { + os << ""; + return false; + } + if (extra.not_null()) { + os << '('; + } + os << grams << "ng"; + if (extra.not_null()) { + vm::Dictionary dict{extra, 32}; + if (!dict.check_for_each([&os](Ref csr, td::ConstBitPtr key, int n) { + CHECK(n == 32); + int x = (int)key.get_int(n); + auto val = block::tlb::t_VarUIntegerPos_32.as_integer_skip(csr.write()); + if (val.is_null() || !csr->empty_ext()) { + os << "+.$" << x << "...)"; + return false; + } + os << '+' << val << ".$" << x; + return true; + })) { + return false; + } + os << ')'; + } + return true; +} + +std::string CurrencyCollection::to_str() const { + std::ostringstream os; + show(os); + return os.str(); +} + +std::ostream& operator<<(std::ostream& os, const CurrencyCollection& cc) { + cc.show(os); + return os; +} + +bool ValueFlow::set_zero() { + return from_prev_blk.set_zero() && to_next_blk.set_zero() && imported.set_zero() && exported.set_zero() && + fees_collected.set_zero() && fees_imported.set_zero() && recovered.set_zero() && created.set_zero() && + minted.set_zero(); +} + +bool ValueFlow::validate() const { + return is_valid() && from_prev_blk + imported + fees_imported + created + minted + recovered == + to_next_blk + exported + fees_collected; +} + +bool ValueFlow::store(vm::CellBuilder& cb) const { + vm::CellBuilder cb2; + return cb.store_long_bool(block::gen::ValueFlow::cons_tag[0], 32) // value_flow ^[ + && from_prev_blk.store(cb2) // from_prev_blk:CurrencyCollection + && to_next_blk.store(cb2) // to_next_blk:CurrencyCollection + && imported.store(cb2) // imported:CurrencyCollection + && exported.store(cb2) // exported:CurrencyCollection + && cb.store_ref_bool(cb2.finalize()) // ] + && fees_collected.store(cb) // fees_collected:CurrencyCollection + && fees_imported.store(cb2) // ^[ fees_imported:CurrencyCollection + && recovered.store(cb2) // recovered:CurrencyCollection + && created.store(cb2) // created:CurrencyCollection + && minted.store(cb2) // minted:CurrencyCollection + && cb.store_ref_bool(cb2.finalize()); // ] = ValueFlow; +} + +bool ValueFlow::fetch(vm::CellSlice& cs) { + block::gen::ValueFlow::Record f; + if (!(tlb::unpack(cs, f) && from_prev_blk.validate_unpack(std::move(f.r1.from_prev_blk)) && + to_next_blk.validate_unpack(std::move(f.r1.to_next_blk)) && + imported.validate_unpack(std::move(f.r1.imported)) && exported.validate_unpack(std::move(f.r1.exported)) && + fees_collected.validate_unpack(std::move(f.fees_collected)) && + fees_imported.validate_unpack(std::move(f.r2.fees_imported)) && + recovered.validate_unpack(std::move(f.r2.recovered)) && created.validate_unpack(std::move(f.r2.created)) && + minted.validate_unpack(std::move(f.r2.minted)))) { + return invalidate(); + } + return true; +} + +bool ValueFlow::unpack(Ref csr) { + return (csr.not_null() && fetch(csr.write()) && csr->empty_ext()) || invalidate(); +} + +static inline bool say(std::ostream& os, const char* str) { + os << str; + return true; +} + +bool ValueFlow::show_one(std::ostream& os, const char* str, const CurrencyCollection& cc) const { + return say(os, str) && cc.show(os); +} + +bool ValueFlow::show(std::ostream& os) const { + if (!is_valid()) { + os << ""; + return false; + } + return (say(os, "(value-flow ") && show_one(os, "from_prev_blk:", from_prev_blk) && + show_one(os, " to_next_blk:", to_next_blk) && show_one(os, " imported:", imported) && + show_one(os, " exported:", exported) && show_one(os, " fees_collected:", fees_collected) && + show_one(os, " fees_imported:", fees_imported) && show_one(os, " recovered:", recovered) && + show_one(os, " created:", created) && show_one(os, " minted:", minted) && say(os, ")")) || + (say(os, "...)") && false); +} + +std::string ValueFlow::to_str() const { + std::ostringstream os; + show(os); + return os.str(); +} + +std::ostream& operator<<(std::ostream& os, const ValueFlow& vflow) { + vflow.show(os); + return os; +} + +/* + * + * Other block-related functions + * + */ + +bool store_UInt7(vm::CellBuilder& cb, unsigned long long value) { + return block::tlb::t_VarUInteger_7.store_long(cb, (long long)value); +} + +bool store_UInt7(vm::CellBuilder& cb, unsigned long long value1, unsigned long long value2) { + return store_UInt7(cb, value1) && store_UInt7(cb, value2); +} + +bool store_Maybe_Grams(vm::CellBuilder& cb, td::RefInt256 value) { + if (value.is_null()) { + return cb.store_long_bool(0, 1); + } else { + return cb.store_long_bool(1, 1) && block::tlb::t_Grams.store_integer_ref(cb, std::move(value)); + } +} + +bool store_Maybe_Grams_nz(vm::CellBuilder& cb, td::RefInt256 value) { + if (value.is_null() || !value->sgn()) { + return cb.store_long_bool(0, 1); + } else { + return cb.store_long_bool(1, 1) && block::tlb::t_Grams.store_integer_ref(cb, std::move(value)); + } +} + +bool store_CurrencyCollection(vm::CellBuilder& cb, td::RefInt256 value, Ref extra) { + return block::tlb::t_CurrencyCollection.pack_special(cb, std::move(value), std::move(extra)); +} + +bool fetch_CurrencyCollection(vm::CellSlice& cs, td::RefInt256& value, Ref& extra, bool inexact) { + return block::tlb::t_CurrencyCollection.unpack_special(cs, value, extra, inexact); +} + +bool unpack_CurrencyCollection(Ref csr, td::RefInt256& value, Ref& extra) { + if (csr.is_null()) { + return false; + } else if (csr->is_unique()) { + return block::tlb::t_CurrencyCollection.unpack_special(csr.write(), value, extra); + } else { + vm::CellSlice cs{*csr}; + return block::tlb::t_CurrencyCollection.unpack_special(cs, value, extra); + } +} + +bool check_one_library(Ref cs_ref, td::ConstBitPtr key, int n) { + assert(n == 256); + if (cs_ref->size_ext() != 0x10001) { + return false; + } + Ref cell = cs_ref->prefetch_ref(); + const auto& cell_hash = cell->get_hash(); + return !td::bitstring::bits_memcmp(cell_hash.bits(), key, n); +} + +bool valid_library_collection(Ref cell, bool catch_errors) { + if (cell.is_null()) { + return true; + } + if (!catch_errors) { + vm::Dictionary dict{std::move(cell), 256}; + return dict.check_for_each(check_one_library); + } + try { + vm::Dictionary dict{std::move(cell), 256}; + return dict.check_for_each(check_one_library); + } catch (vm::VmError&) { + return false; + } +} + +bool check_one_config_param(Ref cs_ref, td::ConstBitPtr key, td::ConstBitPtr addr, bool relax_par0) { + if (cs_ref->size_ext() != 0x10000) { + return false; + } + Ref cell = cs_ref->prefetch_ref(); + int idx = (int)key.get_int(32); + if (!idx) { + auto cs = load_cell_slice(std::move(cell)); + return cs.size_ext() == 256 && (relax_par0 || cs.fetch_bits(256) == addr); + } else if (idx < 0) { + return true; + } + bool ok = block::gen::ConfigParam{idx}.validate_ref(std::move(cell)); + if (!ok) { + LOG(ERROR) << "configuration parameter #" << idx << " is invalid"; + } + return ok; +} + +const int mandatory_config_params[] = {18, 20, 21, 22, 23, 24, 25, 28, 34}; + +bool valid_config_data(Ref cell, const td::BitArray<256>& addr, bool catch_errors, bool relax_par0) { + using namespace std::placeholders; + if (cell.is_null()) { + return false; + } + if (!catch_errors) { + vm::Dictionary dict{std::move(cell), 32}; + for (int x : mandatory_config_params) { + if (!dict.int_key_exists(x)) { + LOG(ERROR) << "mandatory configuration parameter #" << x << " is missing"; + return false; + } + } + return dict.check_for_each(std::bind(check_one_config_param, _1, _2, addr.cbits(), relax_par0)); + } + try { + vm::Dictionary dict{std::move(cell), 32}; + for (int x : mandatory_config_params) { + if (!dict.int_key_exists(x)) { + LOG(ERROR) << "mandatory configuration parameter #" << x << " is missing"; + return false; + } + } + return dict.check_for_each(std::bind(check_one_config_param, _1, _2, addr.cbits(), relax_par0)); + } catch (vm::VmError&) { + return false; + } +} + +bool add_extra_currency(Ref extra1, Ref extra2, Ref& res) { + if (extra2.is_null()) { + res = extra1; + return true; + } else if (extra1.is_null()) { + res = extra2; + return true; + } else { + return block::tlb::t_ExtraCurrencyCollection.add_values_ref(res, std::move(extra1), std::move(extra2)); + } +} + +bool sub_extra_currency(Ref extra1, Ref extra2, Ref& res) { + if (extra2.is_null()) { + res = extra1; + return true; + } else if (extra1.is_null()) { + res.clear(); + return false; + } else { + return block::tlb::t_ExtraCurrencyCollection.sub_values_ref(res, std::move(extra1), std::move(extra2)); + } +} + +// combine d bits from dest, remaining 64 - d bits from src +ton::AccountIdPrefixFull interpolate_addr(const ton::AccountIdPrefixFull& src, const ton::AccountIdPrefixFull& dest, + int d) { + if (d <= 0) { + return src; + } else if (d >= 96) { + return dest; + } else if (d >= 32) { + unsigned long long mask = (std::numeric_limits::max() >> (d - 32)); + return ton::AccountIdPrefixFull{dest.workchain, (dest.account_id_prefix & ~mask) | (src.account_id_prefix & mask)}; + } else { + int mask = (-1 >> d); + return ton::AccountIdPrefixFull{(dest.workchain & ~mask) | (src.workchain & mask), src.account_id_prefix}; + } +} + +bool interpolate_addr_to(const ton::AccountIdPrefixFull& src, const ton::AccountIdPrefixFull& dest, int d, + ton::AccountIdPrefixFull& res) { + res = interpolate_addr(src, dest, d); + return true; +} + +// result: (transit_addr_dest_bits, nh_addr_dest_bits) +std::pair perform_hypercube_routing(ton::AccountIdPrefixFull src, ton::AccountIdPrefixFull dest, + ton::ShardIdFull cur, int used_dest_bits) { + ton::AccountIdPrefixFull transit = interpolate_addr(src, dest, used_dest_bits); + if (!ton::shard_contains(cur, transit)) { + return {-1, -1}; + } + if (transit.account_id_prefix == dest.account_id_prefix || ton::shard_contains(cur, dest)) { + // if destination is already reached, or is in this shard, set cur:=next_hop:=dest + return {96, 96}; + } + if (transit.workchain == ton::masterchainId || dest.workchain == ton::masterchainId) { + return {used_dest_bits, 96}; // route messages to/from masterchain directly + } + if (transit.workchain != dest.workchain) { + return {used_dest_bits, 32}; + } + unsigned long long x = cur.shard & (cur.shard - 1), y = cur.shard | (cur.shard - 1); + unsigned long long t = transit.account_id_prefix, q = dest.account_id_prefix ^ t; + int i = (td::count_leading_zeroes64(q) & -4); // top i bits match, next 4 bits differ + unsigned long long m = (std::numeric_limits::max() >> i), h; + do { + m >>= 4; + h = t ^ (q & ~m); + i += 4; + } while (h >= x && h <= y); + return {28 + i, 32 + i}; +} + +bool compute_out_msg_queue_key(Ref msg_env, td::BitArray<352>& key) { + block::tlb::MsgEnvelope::Record_std env; + block::gen::CommonMsgInfo::Record_int_msg_info info; + if (!(tlb::unpack_cell(msg_env, env) && tlb::unpack_cell_inexact(env.msg, info))) { + return false; + } + auto src_prefix = block::tlb::t_MsgAddressInt.get_prefix(std::move(info.src)); + auto dest_prefix = block::tlb::t_MsgAddressInt.get_prefix(std::move(info.dest)); + auto next_hop = interpolate_addr(src_prefix, dest_prefix, env.next_addr); + key.bits().store_int(next_hop.workchain, 32); + (key.bits() + 32).store_int(next_hop.account_id_prefix, 64); + (key.bits() + 96).copy_from(env.msg->get_hash().bits(), 256); + return true; +} + +bool unpack_block_prev_blk(Ref block_root, const ton::BlockIdExt& id, std::vector& prev, + ton::BlockIdExt& mc_blkid, bool& after_split, ton::BlockIdExt* fetch_blkid) { + return unpack_block_prev_blk_ext(std::move(block_root), id, prev, mc_blkid, after_split, fetch_blkid).is_ok(); +} + +td::Status unpack_block_prev_blk_try(Ref block_root, const ton::BlockIdExt& id, + std::vector& prev, ton::BlockIdExt& mc_blkid, bool& after_split, + ton::BlockIdExt* fetch_blkid) { + try { + return unpack_block_prev_blk_ext(std::move(block_root), id, prev, mc_blkid, after_split, fetch_blkid); + } catch (vm::VmError err) { + return td::Status::Error(std::string{"error while processing Merkle proof: "} + err.get_msg()); + } catch (vm::VmVirtError err) { + return td::Status::Error(std::string{"error while processing Merkle proof: "} + err.get_msg()); + } +} + +td::Status unpack_block_prev_blk_ext(Ref block_root, const ton::BlockIdExt& id, + std::vector& prev, ton::BlockIdExt& mc_blkid, bool& after_split, + ton::BlockIdExt* fetch_blkid) { + block::gen::Block::Record blk; + block::gen::BlockInfo::Record info; + block::gen::ExtBlkRef::Record mcref; // _ ExtBlkRef = BlkMasterInfo; + ton::ShardIdFull shard; + if (!(tlb::unpack_cell(block_root, blk) && tlb::unpack_cell(blk.info, info) && !info.version && + block::tlb::t_ShardIdent.unpack(info.shard.write(), shard) && !info.vert_seq_no && + (!info.not_master || tlb::unpack_cell(info.master_ref, mcref)))) { + return td::Status::Error("cannot unpack block header"); + } + if (fetch_blkid) { + fetch_blkid->id = ton::BlockId{shard, (unsigned)info.seq_no}; + fetch_blkid->root_hash = block_root->get_hash().bits(); + fetch_blkid->file_hash.clear(); + } else { + ton::BlockId hdr_id{shard, (unsigned)info.seq_no}; + if (id.id != hdr_id) { + return td::Status::Error("block header contains block id "s + hdr_id.to_str() + ", expected " + id.id.to_str()); + } + if (id.root_hash != block_root->get_hash().bits()) { + return td::Status::Error("block header has incorrect root hash "s + block_root->get_hash().bits().to_hex(256) + + " instead of expected " + id.root_hash.to_hex()); + } + } + if (info.not_master != !shard.is_masterchain()) { + return td::Status::Error("block has invalid not_master flag in its (Merkelized) header"); + } + after_split = info.after_split; + block::gen::ExtBlkRef::Record prev1, prev2; + if (info.after_merge) { + auto cs = vm::load_cell_slice(std::move(info.prev_ref)); + CHECK(cs.size_ext() == 0x20000); // prev_blks_info$_ prev1:^ExtBlkRef prev2:^ExtBlkRef = BlkPrevInfo 1; + if (!(tlb::unpack_cell(cs.prefetch_ref(0), prev1) && tlb::unpack_cell(cs.prefetch_ref(1), prev2))) { + return td::Status::Error("cannot unpack two previous block references from block header"); + } + } else { + // prev_blk_info$_ prev:ExtBlkRef = BlkPrevInfo 0; + if (!(tlb::unpack_cell(std::move(info.prev_ref), prev1))) { + return td::Status::Error("cannot unpack previous block reference from block header"); + } + } + prev.clear(); + ton::BlockSeqno prev_seqno = prev1.seq_no; + if (!info.after_merge) { + prev.emplace_back(shard.workchain, info.after_split ? ton::shard_parent(shard.shard) : shard.shard, prev1.seq_no, + prev1.root_hash, prev1.file_hash); + if (info.after_split && !prev1.seq_no) { + return td::Status::Error("shardchains cannot be split immediately after initial state"); + } + } else { + if (info.after_split) { + return td::Status::Error("shardchains cannot be simultaneously split and merged at the same block"); + } + prev.emplace_back(shard.workchain, ton::shard_child(shard.shard, true), prev1.seq_no, prev1.root_hash, + prev1.file_hash); + prev.emplace_back(shard.workchain, ton::shard_child(shard.shard, false), prev2.seq_no, prev2.root_hash, + prev2.file_hash); + prev_seqno = std::max(prev1.seq_no, prev2.seq_no); + if (!prev1.seq_no || !prev2.seq_no) { + return td::Status::Error("shardchains cannot be merged immediately after initial state"); + } + } + if (id.id.seqno != prev_seqno + 1) { + return td::Status::Error("new block has invalid seqno (not equal to one plus maximum of seqnos of its ancestors)"); + } + if (shard.is_masterchain()) { + mc_blkid = prev.at(0); + } else { + mc_blkid = ton::BlockIdExt{ton::masterchainId, ton::shardIdAll, mcref.seq_no, mcref.root_hash, mcref.file_hash}; + } + return td::Status::OK(); +} + +std::unique_ptr get_prev_blocks_dict(Ref state_root) { + block::gen::ShardStateUnsplit::Record info; + block::gen::McStateExtra::Record extra_info; + if (!(::tlb::unpack_cell(std::move(state_root), info) && info.custom->size_refs() && + ::tlb::unpack_cell(info.custom->prefetch_ref(), extra_info))) { + return {}; + } + return std::make_unique(extra_info.r1.prev_blocks, 32, block::tlb::aug_OldMcBlocksInfo); +} + +bool get_old_mc_block_id(vm::AugmentedDictionary* prev_blocks_dict, ton::BlockSeqno seqno, ton::BlockIdExt& blkid, + ton::LogicalTime* end_lt) { + return prev_blocks_dict && get_old_mc_block_id(*prev_blocks_dict, seqno, blkid, end_lt); +} + +bool get_old_mc_block_id(vm::AugmentedDictionary& prev_blocks_dict, ton::BlockSeqno seqno, ton::BlockIdExt& blkid, + ton::LogicalTime* end_lt) { + return unpack_old_mc_block_id(prev_blocks_dict.lookup(td::BitArray<32>{seqno}), seqno, blkid, end_lt); +} + +bool unpack_old_mc_block_id(Ref old_blk_info, ton::BlockSeqno seqno, ton::BlockIdExt& blkid, + ton::LogicalTime* end_lt) { + return old_blk_info.not_null() && old_blk_info.write().advance(1) && + block::tlb::t_ExtBlkRef.unpack(std::move(old_blk_info), blkid, end_lt) && blkid.seqno() == seqno; +} + +bool check_old_mc_block_id(vm::AugmentedDictionary* prev_blocks_dict, const ton::BlockIdExt& blkid) { + return prev_blocks_dict && check_old_mc_block_id(*prev_blocks_dict, blkid); +} + +bool check_old_mc_block_id(vm::AugmentedDictionary& prev_blocks_dict, const ton::BlockIdExt& blkid) { + if (!blkid.id.is_masterchain_ext()) { + return false; + } + ton::BlockIdExt old_blkid; + return unpack_old_mc_block_id(prev_blocks_dict.lookup(td::BitArray<32>{blkid.id.seqno}), blkid.id.seqno, old_blkid) && + old_blkid == blkid; +} + +td::Result> get_block_transaction(Ref block_root, ton::WorkchainId workchain, + const ton::StdSmcAddress& addr, ton::LogicalTime lt) { + block::gen::Block::Record block; + block::gen::BlockInfo::Record info; + if (!(tlb::unpack_cell(std::move(block_root), block) && tlb::unpack_cell(std::move(block.info), info))) { + return td::Status::Error("cannot unpack block header"); + } + Ref trans_root; + if (lt > info.start_lt && lt < info.end_lt) { + // lt belongs to this block + block::gen::BlockExtra::Record extra; + if (!(tlb::unpack_cell(block.extra, extra))) { + return td::Status::Error("cannot unpack block extra information"); + } + vm::AugmentedDictionary account_blocks_dict{vm::load_cell_slice_ref(extra.account_blocks), 256, + block::tlb::aug_ShardAccountBlocks}; + auto ab_csr = account_blocks_dict.lookup(addr); + if (ab_csr.not_null()) { + // account block for this account exists + block::gen::AccountBlock::Record acc_block; + if (!(tlb::csr_unpack(std::move(ab_csr), acc_block) && acc_block.account_addr == addr)) { + return td::Status::Error("cannot unpack AccountBlock"); + } + vm::AugmentedDictionary trans_dict{vm::DictNonEmpty(), acc_block.transactions, 64, + block::tlb::aug_AccountTransactions}; + return trans_dict.lookup_ref(td::BitArray<64>{static_cast(lt)}); + } + } + return Ref{}; +} + +td::Result> get_block_transaction_try(Ref block_root, ton::WorkchainId workchain, + const ton::StdSmcAddress& addr, ton::LogicalTime lt) { + try { + return get_block_transaction(std::move(block_root), workchain, addr, lt); + } catch (vm::VmError err) { + return td::Status::Error(std::string{"error while extracting transaction from block : "} + err.get_msg()); + } catch (vm::VmVirtError err) { + return td::Status::Error(std::string{"virtualization error while traversing transaction proof : "} + err.get_msg()); + } +} + +bool get_transaction_in_msg(Ref trans_ref, Ref& in_msg) { + block::gen::Transaction::Record trans; + if (!tlb::unpack_cell(std::move(trans_ref), trans)) { + return false; + } else { + in_msg = trans.r1.in_msg->prefetch_ref(); + return true; + } +} + +bool is_transaction_in_msg(Ref trans_ref, Ref msg) { + Ref imsg; + return get_transaction_in_msg(std::move(trans_ref), imsg) && imsg.not_null() == msg.not_null() && + (imsg.is_null() || imsg->get_hash() == msg->get_hash()); +} + +bool is_transaction_out_msg(Ref trans_ref, Ref msg) { + block::gen::Transaction::Record trans; + vm::CellSlice cs; + unsigned long long created_lt; + if (!(trans_ref.not_null() && msg.not_null() && tlb::unpack_cell(std::move(trans_ref), trans) && cs.load_ord(msg) && + block::tlb::t_CommonMsgInfo.get_created_lt(cs, created_lt))) { + return false; + } + if (created_lt <= trans.lt || created_lt > trans.lt + trans.outmsg_cnt) { + return false; + } + try { + auto o_msg = + vm::Dictionary{trans.r1.out_msgs, 15}.lookup_ref(td::BitArray<15>{(long long)(created_lt - trans.lt - 1)}); + return o_msg.not_null() && o_msg->get_hash() == msg->get_hash(); + } catch (vm::VmError&) { + return false; + } +} + +// transaction$0111 account_addr:bits256 lt:uint64 ... +bool get_transaction_id(Ref trans_ref, ton::StdSmcAddress& account_addr, ton::LogicalTime& lt) { + if (trans_ref.is_null()) { + return false; + } + vm::CellSlice cs{vm::NoVmOrd(), trans_ref}; + return cs.fetch_ulong(4) == 7 // transaction$0111 + && cs.fetch_bits_to(account_addr) // account_addr:bits256 + && cs.fetch_uint_to(64, lt); // lt:uint64 +} + +bool get_transaction_owner(Ref trans_ref, ton::StdSmcAddress& addr) { + ton::LogicalTime lt; + return get_transaction_id(std::move(trans_ref), addr, lt); +} + +td::uint32 compute_validator_set_hash(ton::CatchainSeqno cc_seqno, ton::ShardIdFull from, + const std::vector& nodes) { + /* + std::vector> s_vec; + + for (auto& n : nodes) { + auto id = ValidatorFullId{n.key}.short_id(); + s_vec.emplace_back(create_tl_object(id, n.weight)); + } + + auto obj = create_tl_object(cc_seqno, std::move(s_vec)); + auto B = serialize_tl_object(obj, true); + return td::crc32c(B.as_slice()); + */ + CHECK(nodes.size() <= 0xffffffff); + auto tot_size = 1 + 1 + 1 + nodes.size() * (8 + 2 + 8); + auto buff = std::make_unique(tot_size); + td::TlStorerUnsafe storer(reinterpret_cast(buff.get())); + auto* begin = storer.get_buf(); + storer.store_int(-1877581587); // magic inherited from test0.validatorSet + storer.store_int(cc_seqno); + storer.store_binary((td::uint32)nodes.size()); + for (auto& n : nodes) { + storer.store_binary(n.key.as_bits256()); + storer.store_long(n.weight); + storer.store_binary(n.addr); + } + auto* end = storer.get_buf(); + CHECK(static_cast(end - begin) == 4 * tot_size); + return td::crc32c(td::Slice(begin, end)); +} + +td::Result> get_config_data_from_smc(Ref acc_root) { + if (acc_root.is_null()) { + return td::Status::Error("configuration smart contract not found or it has no state, cannot extract configuration"); + } + block::gen::Account::Record_account acc; + block::gen::AccountStorage::Record storage; + block::gen::StateInit::Record state; + if (!(tlb::unpack_cell(acc_root, acc) && tlb::csr_unpack(acc.storage, storage) && + storage.state.write().fetch_ulong(1) == 1 && tlb::csr_unpack(storage.state, state) && + state.data->have_refs(1))) { + return td::Status::Error("cannot extract persistent data from configuration smart contract state"); + } + Ref data_cell = state.data->prefetch_ref(); + auto res = vm::load_cell_slice(data_cell).prefetch_ref(); + if (res.is_null()) { + return td::Status::Error( + "configuration smart contract does not contain a valid configuration in the first reference of its persistent " + "data"); + } + return std::move(res); +} + +td::Result> get_config_data_from_smc(Ref acc_csr) { + if (acc_csr.is_null()) { + return td::Status::Error("configuration smart contract not found, cannot extract configuration"); + } + if (acc_csr->size_ext() != 0x10140) { + return td::Status::Error("configuration smart contract does not have a valid non-empty state"); + } + return get_config_data_from_smc(acc_csr->prefetch_ref()); +} + +// when these parameters change, the block must be marked as a key block +bool important_config_parameters_changed(Ref old_cfg_root, Ref new_cfg_root, bool coarse) { + if (old_cfg_root->get_hash() == new_cfg_root->get_hash()) { + return false; + } + if (coarse) { + return true; + } + // for now, all parameters are "important" + // at least the parameters affecting the computations of validator sets must be considered important + // ... + return true; +} + +bool is_public_library(td::ConstBitPtr key, Ref val) { + return val.not_null() && val->prefetch_ulong(1) == 1 && val->have_refs() && + !key.compare(val->prefetch_ref()->get_hash().bits(), 256); +} + +bool parse_hex_hash(const char* str, const char* end, td::Bits256& hash) { + if (end - str != 64) { + return false; + } + int y = 0; + for (int i = 0; i < 64; i++) { + int c = *str++, x = c - '0'; + if (x < 0) { + return false; + } else if (x > 10) { + x = (c | 0x20) - ('a' - 10); + if (x < 10 || x > 16) { + return false; + } + } + y = (y << 4) | x; + if (i & 1) { + hash.data()[i >> 1] = (unsigned char)y; + y = 0; + } + } + return true; +} + +bool parse_hex_hash(td::Slice str, td::Bits256& hash) { + return parse_hex_hash(str.begin(), str.end(), hash); +} + +bool parse_block_id_ext(const char* str, const char* end, ton::BlockIdExt& blkid) { + blkid.invalidate(); + if (!str || !end || str >= end || end - str > 255) { + return false; + } + if (*str != '(') { + return false; + } + if (!std::memchr(str, ')', end - str)) { + return false; + } + int wc, pos = 0; + unsigned seqno; + unsigned long long shard; + if (std::sscanf(str, "(%d,%llx,%u):%n", &wc, &shard, &seqno, &pos) < 3 || pos <= 0 || pos >= end - str) { + return false; + } + if (!shard || wc == ton::workchainInvalid) { + return false; + } + str += pos; + if (end - str != 64 * 2 + 1 || str[64] != ':') { + return false; + } + blkid.id = ton::BlockId{wc, shard, seqno}; + return (parse_hex_hash(str, str + 64, blkid.root_hash) && parse_hex_hash(str + 65, end, blkid.file_hash)) || + blkid.invalidate(); +} + +bool parse_block_id_ext(td::Slice str, ton::BlockIdExt& blkid) { + return parse_block_id_ext(str.begin(), str.end(), blkid); +} + +} // namespace block diff --git a/crypto/block/block.h b/crypto/block/block.h new file mode 100644 index 00000000..aa334ac4 --- /dev/null +++ b/crypto/block/block.h @@ -0,0 +1,569 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once +#include "common/refcnt.hpp" +#include "vm/cells.h" +#include "vm/cellslice.h" +#include "vm/dict.h" +#include "vm/boc.h" +#include "vm/stack.hpp" +#include +#include "tl/tlblib.hpp" +#include "td/utils/bits.h" +#include "td/utils/StringBuilder.h" +#include "ton/ton-types.h" + +namespace block { + +using td::Ref; + +struct StdAddress { + ton::WorkchainId workchain{ton::workchainInvalid}; + bool bounceable{true}; // addresses must be bounceable by default + bool testnet{false}; + ton::StdSmcAddress addr; + StdAddress() = default; + StdAddress(ton::WorkchainId _wc, const ton::StdSmcAddress& _addr, bool _bounce = true, bool _testnet = false) + : workchain(_wc), bounceable(_bounce), testnet(_testnet), addr(_addr) { + } + StdAddress(ton::WorkchainId _wc, td::ConstBitPtr _addr, bool _bounce = true, bool _testnet = false) + : workchain(_wc), bounceable(_bounce), testnet(_testnet), addr(_addr) { + } + explicit StdAddress(std::string serialized); + explicit StdAddress(td::Slice from); + bool is_valid() const { + return workchain != ton::workchainInvalid; + } + bool invalidate() { + workchain = ton::workchainInvalid; + return false; + } + std::string rserialize(bool base64_url = false) const; + bool rserialize_to(td::MutableSlice to, bool base64_url = false) const; + bool rserialize_to(char to[48], bool base64_url = false) const; + bool rdeserialize(td::Slice from); + bool rdeserialize(std::string from); + bool rdeserialize(const char from[48]); + bool parse_addr(td::Slice acc_string); + bool operator==(const StdAddress& other) const; + + static td::Result parse(td::Slice acc_string); +}; + +inline td::StringBuilder& operator<<(td::StringBuilder& sb, const StdAddress& addr) { + return sb << addr.workchain << " : " << addr.addr.to_hex(); +} + +bool parse_std_account_addr(td::Slice acc_string, ton::WorkchainId& wc, ton::StdSmcAddress& addr, + bool* bounceable = nullptr, bool* testnet_only = nullptr); + +struct ShardId { + ton::WorkchainId workchain_id; + int shard_pfx_len; + unsigned long long shard_pfx; + ShardId(ton::WorkchainId wc_id = ton::workchainInvalid) + : workchain_id(wc_id), shard_pfx_len(0), shard_pfx(1ULL << 63) { + } + ShardId(ton::WorkchainId wc_id, unsigned long long sh_pfx); + ShardId(ton::ShardIdFull ton_shard); + ShardId(ton::BlockId ton_block); + ShardId(const ton::BlockIdExt& ton_block); + ShardId(ton::WorkchainId wc_id, unsigned long long sh_pfx, int sh_pfx_len); + ShardId(vm::CellSlice& cs) { + deserialize(cs); + } + ShardId(Ref cs_ref) { + vm::CellSlice cs{*cs_ref}; + deserialize(cs); + } + explicit operator ton::ShardIdFull() const { + return ton::ShardIdFull{workchain_id, shard_pfx}; + } + bool operator==(const ShardId& other) const { + return workchain_id == other.workchain_id && shard_pfx == other.shard_pfx; + } + void invalidate() { + workchain_id = ton::workchainInvalid; + shard_pfx_len = 0; + } + bool is_valid() const { + return workchain_id != ton::workchainInvalid; + } + void show(std::ostream& os) const; + std::string to_str() const; + bool serialize(vm::CellBuilder& cb) const; + bool deserialize(vm::CellSlice& cs); + + private: + void init(); +}; + +struct EnqueuedMsgDescr { + ton::AccountIdPrefixFull src_prefix_, cur_prefix_, next_prefix_, dest_prefix_; + ton::LogicalTime lt_; + ton::LogicalTime enqueued_lt_; + ton::Bits256 hash_; + Ref msg_; + Ref msg_env_; + EnqueuedMsgDescr() = default; + EnqueuedMsgDescr(ton::AccountIdPrefixFull cur_pfx, ton::AccountIdPrefixFull next_pfx, ton::LogicalTime lt, + ton::LogicalTime enqueued_lt, td::ConstBitPtr hash) + : cur_prefix_(cur_pfx), next_prefix_(next_pfx), lt_(lt), enqueued_lt_(enqueued_lt), hash_(hash) { + } + bool is_valid() const { + return next_prefix_.is_valid(); + } + bool check_key(td::ConstBitPtr key) const; + bool invalidate() { + next_prefix_.workchain = cur_prefix_.workchain = ton::workchainInvalid; + return false; + } + bool unpack(vm::CellSlice& cs); +}; + +using compute_shard_end_lt_func_t = std::function; + +struct MsgProcessedUpto { + ton::ShardId shard; + ton::BlockSeqno mc_seqno; + ton::LogicalTime last_inmsg_lt; + ton::Bits256 last_inmsg_hash; + compute_shard_end_lt_func_t compute_shard_end_lt; + MsgProcessedUpto() = default; + MsgProcessedUpto(ton::ShardId _shard, ton::BlockSeqno _mcseqno, ton::LogicalTime _lt, td::ConstBitPtr _hash) + : shard(_shard), mc_seqno(_mcseqno), last_inmsg_lt(_lt), last_inmsg_hash(_hash) { + } + bool operator<(const MsgProcessedUpto& other) const & { + return shard < other.shard || (shard == other.shard && mc_seqno < other.mc_seqno); + } + bool contains(const MsgProcessedUpto& other) const &; + bool contains(ton::ShardId other_shard, ton::LogicalTime other_lt, td::ConstBitPtr other_hash, + ton::BlockSeqno other_mc_seqno) const &; + // NB: this is for checking whether we have already imported an internal message + bool already_processed(const EnqueuedMsgDescr& msg) const; +}; + +struct MsgProcessedUptoCollection { + ton::ShardIdFull owner; + bool valid{false}; + std::vector list; + MsgProcessedUptoCollection(ton::ShardIdFull _owner) : owner(_owner) { + } + MsgProcessedUptoCollection(ton::ShardIdFull _owner, Ref cs_ref); + static std::unique_ptr unpack(ton::ShardIdFull _owner, Ref cs_ref); + bool is_valid() const { + return valid; + } + bool insert(ton::BlockSeqno mc_seqno, ton::LogicalTime last_proc_lt, td::ConstBitPtr last_proc_hash); + bool insert_infty(ton::BlockSeqno mc_seqno, ton::LogicalTime last_proc_lt = ~0ULL); + bool compactify(); + bool pack(vm::CellBuilder& cb); + bool is_reduced() const; + bool contains(const MsgProcessedUpto& other) const; + bool contains(const MsgProcessedUptoCollection& other) const; + const MsgProcessedUpto* is_simple_update_of(const MsgProcessedUptoCollection& other, bool& ok) const; + ton::BlockSeqno min_mc_seqno() const; + bool split(ton::ShardIdFull new_owner); + bool combine_with(const MsgProcessedUptoCollection& other); + // NB: this is for checking whether we have already imported an internal message + bool already_processed(const EnqueuedMsgDescr& msg) const; + bool for_each_mcseqno(std::function) const; +}; + +struct ParamLimits { + enum { limits_cnt = 4 }; + enum { cl_underload = 0, cl_normal = 1, cl_soft = 2, cl_medium = 3, cl_hard = 4 }; + ParamLimits() = default; + ParamLimits(td::uint32 underload, td::uint32 soft_lim, td::uint32 hard_lim) + : limits_{underload, soft_lim, (soft_lim + hard_lim) / 2, hard_lim} { + } + td::uint32 underload() const { + return limits_[0]; + } + td::uint32 soft() const { + return limits_[1]; + } + td::uint32 hard() const { + return limits_[3]; + } + bool compute_medium_limit() { + limits_[2] = soft() + ((hard() - soft()) >> 1); + return true; + } + bool deserialize(vm::CellSlice& cs); + int classify(td::uint64 value) const; + bool fits(unsigned cls, td::uint64 value) const; + + private: + std::array limits_; +}; + +struct BlockLimits { + ParamLimits bytes, gas, lt_delta; + ton::LogicalTime start_lt{0}; + const vm::CellUsageTree* usage_tree{nullptr}; + bool deserialize(vm::CellSlice& cs); + int classify_size(td::uint64 size) const; + int classify_gas(td::uint64 gas) const; + int classify_lt(ton::LogicalTime lt) const; + int classify(td::uint64 size, td::uint64 gas, ton::LogicalTime lt) const; + bool fits(unsigned cls, td::uint64 size, td::uint64 gas, ton::LogicalTime lt) const; +}; + +struct BlockLimitStatus { + const BlockLimits& limits; + ton::LogicalTime cur_lt; + td::uint64 gas_used{}; + vm::NewCellStorageStat st_stat; + unsigned accounts{}, transactions{}; + BlockLimitStatus(const BlockLimits& limits_, ton::LogicalTime lt = 0) + : limits(limits_), cur_lt(std::max(limits_.start_lt, lt)) { + } + void reset() { + cur_lt = limits.start_lt; + st_stat.set_zero(); + transactions = accounts = 0; + gas_used = 0; + } + td::uint64 estimate_block_size(const vm::NewCellStorageStat::Stat* extra = nullptr) const; + int classify() const; + bool fits(unsigned cls) const; + bool would_fit(unsigned cls, ton::LogicalTime end_lt, td::uint64 more_gas, + const vm::NewCellStorageStat::Stat* extra = nullptr) const; + bool add_cell(Ref cell) { + st_stat.add_cell(std::move(cell)); + return true; + } + bool add_proof(Ref cell) { + st_stat.add_proof(std::move(cell), limits.usage_tree); + return true; + } + bool update_lt(ton::LogicalTime lt) { + cur_lt = std::max(lt, cur_lt); + return true; + } + bool update_gas(td::uint64 more_gas) { + gas_used += more_gas; + return true; + } + bool add_transaction(unsigned cnt = 1) { + transactions += cnt; + return true; + } + bool add_account(unsigned cnt = 1) { + accounts += cnt; + return true; + } +}; + +namespace tlb { +struct CurrencyCollection; +} // namespace tlb + +struct CurrencyCollection { + using type_class = block::tlb::CurrencyCollection; + td::RefInt256 grams; + Ref extra; + CurrencyCollection() = default; + explicit CurrencyCollection(td::RefInt256 _grams, Ref _extra = {}) + : grams(std::move(_grams)), extra(std::move(_extra)) { + } + explicit CurrencyCollection(long long _grams, Ref _extra = {}) + : grams(true, _grams), extra(std::move(_extra)) { + } + bool set_zero() { + grams = td::RefInt256{true, 0}; + extra.clear(); + return true; + } + static CurrencyCollection zero() { + return CurrencyCollection(td::RefInt256{true, 0}); + } + bool is_valid() const { + return grams.not_null(); + } + bool is_zero() const { + return is_valid() && extra.is_null() && !td::sgn(grams); + } + bool has_extra() const { + return extra.not_null(); + } + bool invalidate() { + extra.clear(); + grams.clear(); + return false; + } + bool validate() const; + bool validate_extra() const; + bool operator==(const CurrencyCollection& other) const; + bool operator!=(const CurrencyCollection& other) const { + return !operator==(other); + } + bool operator==(td::RefInt256 other_grams) const { + return is_valid() && !has_extra() && !td::cmp(grams, other_grams); + } + bool operator!=(td::RefInt256 other_grams) const { + return !operator==(std::move(other_grams)); + } + bool operator>=(const CurrencyCollection& other) const; + bool operator<=(const CurrencyCollection& other) const { + return other >= *this; + } + static bool add(const CurrencyCollection& a, const CurrencyCollection& b, CurrencyCollection& c); + static bool add(const CurrencyCollection& a, CurrencyCollection&& b, CurrencyCollection& c); + CurrencyCollection& operator+=(const CurrencyCollection& other); + CurrencyCollection& operator+=(CurrencyCollection&& other); + CurrencyCollection& operator+=(td::RefInt256 other_grams); + CurrencyCollection operator+(const CurrencyCollection& other) const; + CurrencyCollection operator+(CurrencyCollection&& other) const; + CurrencyCollection operator+(td::RefInt256 other_grams); + static bool sub(const CurrencyCollection& a, const CurrencyCollection& b, CurrencyCollection& c); + static bool sub(const CurrencyCollection& a, CurrencyCollection&& b, CurrencyCollection& c); + CurrencyCollection& operator-=(const CurrencyCollection& other); + CurrencyCollection& operator-=(CurrencyCollection&& other); + CurrencyCollection& operator-=(td::RefInt256 other_grams); + CurrencyCollection operator-(const CurrencyCollection& other) const; + CurrencyCollection operator-(CurrencyCollection&& other) const; + CurrencyCollection operator-(td::RefInt256 other_grams) const; + bool store(vm::CellBuilder& cb) const; + bool store_or_zero(vm::CellBuilder& cb) const; + bool fetch(vm::CellSlice& cs); + bool fetch_exact(vm::CellSlice& cs); + bool unpack(Ref csr); + bool validate_unpack(Ref csr); + Ref pack() const; + bool pack_to(Ref& csr) const { + return (csr = pack()).not_null(); + } + Ref as_vm_tuple() const { + if (is_valid()) { + return vm::make_tuple_ref(grams, vm::StackEntry::maybe(extra)); + } else { + return {}; + } + } + bool show(std::ostream& os) const; + std::string to_str() const; +}; + +std::ostream& operator<<(std::ostream& os, const CurrencyCollection& cc); + +struct ShardState { + enum { verbosity = 0 }; + ton::BlockIdExt id_; + Ref root_; + int global_id_; + ton::UnixTime utime_; + ton::LogicalTime lt_; + ton::BlockSeqno mc_blk_seqno_, min_ref_mc_seqno_; + ton::BlockIdExt mc_blk_ref_; + ton::LogicalTime mc_blk_lt_; + bool before_split_{false}; + std::unique_ptr account_dict_; + std::unique_ptr shard_libraries_; + Ref mc_state_extra_; + td::uint64 overload_history_{0}, underload_history_{0}; + CurrencyCollection total_balance_, total_validator_fees_, global_balance_; + std::unique_ptr out_msg_queue_; + std::unique_ptr ihr_pending_; + std::shared_ptr processed_upto_; + + bool is_valid() const { + return id_.is_valid(); + } + bool is_masterchain() const { + return id_.is_masterchain(); + } + bool invalidate() { + id_.invalidate(); + return false; + } + td::Status unpack_state(ton::BlockIdExt id, Ref state_root); + td::Status unpack_state_ext(ton::BlockIdExt id, Ref state_root, int global_id, + ton::BlockSeqno prev_mc_block_seqno, bool after_split, bool clear_history, + std::function for_each_mcseqno); + td::Status merge_with(ShardState& sib); + td::Result> compute_split_out_msg_queue(ton::ShardIdFull subshard); + td::Result> compute_split_processed_upto( + ton::ShardIdFull subshard); + td::Status split(ton::ShardIdFull subshard); + td::Status unpack_out_msg_queue_info(Ref out_msg_queue_info); + bool clear_load_history() { + overload_history_ = underload_history_ = 0; + return true; + } + bool clear_load_history_if(bool cond) { + return !cond || clear_load_history(); + } + td::Status check_before_split(bool before_split) const; + td::Status check_global_id(int req_global_id) const; + td::Status check_mc_blk_seqno(ton::BlockSeqno last_mc_block_seqno) const; + bool update_prev_utime_lt(ton::UnixTime& prev_utime, ton::LogicalTime& prev_lt) const; + + bool for_each_mcseqno(std::function func) const { + return processed_upto_ && processed_upto_->for_each_mcseqno(std::move(func)); + } +}; + +struct ValueFlow { + struct SetZero {}; + CurrencyCollection from_prev_blk, to_next_blk, imported, exported, fees_collected, fees_imported, recovered, created, + minted; + ValueFlow() = default; + ValueFlow(SetZero) + : from_prev_blk{0} + , to_next_blk{0} + , imported{0} + , exported{0} + , fees_collected{0} + , fees_imported{0} + , recovered{0} + , created{0} + , minted{0} { + } + bool is_valid() const { + return from_prev_blk.is_valid() && minted.is_valid(); + } + bool validate() const; + bool invalidate() { + return from_prev_blk.invalidate(); + } + bool set_zero(); + bool store(vm::CellBuilder& cb) const; + bool fetch(vm::CellSlice& cs); + bool unpack(Ref csr); + bool show(std::ostream& os) const; + std::string to_str() const; + + private: + bool show_one(std::ostream& os, const char* str, const CurrencyCollection& cc) const; +}; + +std::ostream& operator<<(std::ostream& os, const ValueFlow& vflow); + +struct BlkProofLink { + ton::BlockIdExt from, to; + bool is_key{false}, is_fwd{false}; + Ref dest_proof, shard_proof, proof; + ton::CatchainSeqno cc_seqno{0}; + td::uint32 validator_set_hash{0}; + std::vector signatures; + BlkProofLink(ton::BlockIdExt _from, ton::BlockIdExt _to, bool _iskey = false) + : from(_from), to(_to), is_key(_iskey), is_fwd(to.seqno() > from.seqno()) { + } +}; + +struct BlkProofChain { + ton::BlockIdExt from, to; + int mode; + std::vector links; + std::size_t link_count() const { + return links.size(); + } + BlkProofChain(ton::BlockIdExt _from, ton::BlockIdExt _to, int _mode) : from(_from), to(_to), mode(_mode) { + } +}; + +int filter_out_msg_queue(vm::AugmentedDictionary& out_queue, ton::ShardIdFull old_shard, ton::ShardIdFull subshard); + +std::ostream& operator<<(std::ostream& os, const ShardId& shard_id); + +bool pack_std_smc_addr_to(char result[48], bool base64_url, ton::WorkchainId wc, const ton::StdSmcAddress& addr, + bool bounceable, bool testnet); +std::string pack_std_smc_addr(bool base64_url, ton::WorkchainId wc, const ton::StdSmcAddress& addr, bool bounceable, + bool testnet); +bool unpack_std_smc_addr(const char packed[48], ton::WorkchainId& wc, ton::StdSmcAddress& addr, bool& bounceable, + bool& testnet); +bool unpack_std_smc_addr(td::Slice packed, ton::WorkchainId& wc, ton::StdSmcAddress& addr, bool& bounceable, + bool& testnet); +bool unpack_std_smc_addr(std::string packed, ton::WorkchainId& wc, ton::StdSmcAddress& addr, bool& bounceable, + bool& testnet); + +bool store_UInt7(vm::CellBuilder& cb, unsigned long long value); +bool store_UInt7(vm::CellBuilder& cb, unsigned long long value1, unsigned long long value2); +bool store_Maybe_Grams(vm::CellBuilder& cb, td::RefInt256 value); +bool store_Maybe_Grams_nz(vm::CellBuilder& cb, td::RefInt256 value); +bool store_CurrencyCollection(vm::CellBuilder& cb, td::RefInt256 value, Ref extra); +bool fetch_CurrencyCollection(vm::CellSlice& cs, td::RefInt256& value, Ref& extra, bool inexact = false); +bool unpack_CurrencyCollection(Ref csr, td::RefInt256& value, Ref& extra); + +bool valid_library_collection(Ref cell, bool catch_errors = true); + +bool valid_config_data(Ref cell, const td::BitArray<256>& addr, bool catch_errors = true, + bool relax_par0 = false); + +bool add_extra_currency(Ref extra1, Ref extra2, Ref& res); +bool sub_extra_currency(Ref extra1, Ref extra2, Ref& res); + +ton::AccountIdPrefixFull interpolate_addr(const ton::AccountIdPrefixFull& src, const ton::AccountIdPrefixFull& dest, + int used_dest_bits); +bool interpolate_addr_to(const ton::AccountIdPrefixFull& src, const ton::AccountIdPrefixFull& dest, int used_dest_bits, + ton::AccountIdPrefixFull& res); +// result: (transit_addr_dest_bits, nh_addr_dest_bits) +std::pair perform_hypercube_routing(ton::AccountIdPrefixFull src, ton::AccountIdPrefixFull dest, + ton::ShardIdFull cur, int used_dest_bits = 0); + +bool compute_out_msg_queue_key(Ref msg_env, td::BitArray<352>& key); + +bool unpack_block_prev_blk(Ref block_root, const ton::BlockIdExt& id, std::vector& prev, + ton::BlockIdExt& mc_blkid, bool& after_split, ton::BlockIdExt* fetch_blkid = nullptr); +td::Status unpack_block_prev_blk_ext(Ref block_root, const ton::BlockIdExt& id, + std::vector& prev, ton::BlockIdExt& mc_blkid, bool& after_split, + ton::BlockIdExt* fetch_blkid = nullptr); +td::Status unpack_block_prev_blk_try(Ref block_root, const ton::BlockIdExt& id, + std::vector& prev, ton::BlockIdExt& mc_blkid, bool& after_split, + ton::BlockIdExt* fetch_blkid = nullptr); + +std::unique_ptr get_prev_blocks_dict(Ref state_root); +bool get_old_mc_block_id(vm::AugmentedDictionary* prev_blocks_dict, ton::BlockSeqno seqno, ton::BlockIdExt& blkid, + ton::LogicalTime* end_lt = nullptr); +bool get_old_mc_block_id(vm::AugmentedDictionary& prev_blocks_dict, ton::BlockSeqno seqno, ton::BlockIdExt& blkid, + ton::LogicalTime* end_lt = nullptr); +bool unpack_old_mc_block_id(Ref old_blk_info, ton::BlockSeqno seqno, ton::BlockIdExt& blkid, + ton::LogicalTime* end_lt = nullptr); +bool check_old_mc_block_id(vm::AugmentedDictionary* prev_blocks_dict, const ton::BlockIdExt& blkid); +bool check_old_mc_block_id(vm::AugmentedDictionary& prev_blocks_dict, const ton::BlockIdExt& blkid); + +td::Result> get_block_transaction(Ref block_root, ton::WorkchainId workchain, + const ton::StdSmcAddress& addr, ton::LogicalTime lt); +td::Result> get_block_transaction_try(Ref block_root, ton::WorkchainId workchain, + const ton::StdSmcAddress& addr, ton::LogicalTime lt); + +bool get_transaction_in_msg(Ref trans_ref, Ref& in_msg); +bool is_transaction_in_msg(Ref trans_ref, Ref msg); +bool is_transaction_out_msg(Ref trans_ref, Ref msg); +bool get_transaction_id(Ref trans_ref, ton::StdSmcAddress& account_addr, ton::LogicalTime& lt); +bool get_transaction_owner(Ref trans_ref, ton::StdSmcAddress& addr); + +td::uint32 compute_validator_set_hash(ton::CatchainSeqno cc_seqno, ton::ShardIdFull from, + const std::vector& nodes); + +td::Result> get_config_data_from_smc(Ref acc_root); +td::Result> get_config_data_from_smc(Ref acc_csr); +bool important_config_parameters_changed(Ref old_cfg_root, Ref new_cfg_root, bool coarse = false); + +bool is_public_library(td::ConstBitPtr key, Ref val); + +bool parse_hex_hash(const char* str, const char* end, td::Bits256& hash); +bool parse_hex_hash(td::Slice str, td::Bits256& hash); + +bool parse_block_id_ext(const char* str, const char* end, ton::BlockIdExt& blkid); +bool parse_block_id_ext(td::Slice str, ton::BlockIdExt& blkid); + +} // namespace block diff --git a/crypto/block/block.tlb b/crypto/block/block.tlb new file mode 100644 index 00000000..bac766be --- /dev/null +++ b/crypto/block/block.tlb @@ -0,0 +1,662 @@ +unit$_ = Unit; +true$_ = True; +// EMPTY False; +bool_false$0 = Bool; +bool_true$1 = Bool; +bool_false$0 = BoolFalse; +bool_true$1 = BoolTrue; +nothing$0 {X:Type} = Maybe X; +just$1 {X:Type} value:X = Maybe X; +left$0 {X:Type} {Y:Type} value:X = Either X Y; +right$1 {X:Type} {Y:Type} value:Y = Either X Y; +pair$_ {X:Type} {Y:Type} first:X second:Y = Both X Y; + +bit$_ (## 1) = Bit; +/* + * + * FROM hashmap.tlb + * + */ +// ordinary Hashmap / HashmapE, with fixed length keys +// +hm_edge#_ {n:#} {X:Type} {l:#} {m:#} label:(HmLabel ~l n) + {n = (~m) + l} node:(HashmapNode m X) = Hashmap n X; + +hmn_leaf#_ {X:Type} value:X = HashmapNode 0 X; +hmn_fork#_ {n:#} {X:Type} left:^(Hashmap n X) + right:^(Hashmap n X) = HashmapNode (n + 1) X; + +hml_short$0 {m:#} {n:#} len:(Unary ~n) {n <= m} s:(n * Bit) = HmLabel ~n m; +hml_long$10 {m:#} n:(#<= m) s:(n * Bit) = HmLabel ~n m; +hml_same$11 {m:#} v:Bit n:(#<= m) = HmLabel ~n m; + +unary_zero$0 = Unary ~0; +unary_succ$1 {n:#} x:(Unary ~n) = Unary ~(n + 1); + +hme_empty$0 {n:#} {X:Type} = HashmapE n X; +hme_root$1 {n:#} {X:Type} root:^(Hashmap n X) = HashmapE n X; + +// true#_ = True; +_ {n:#} _:(Hashmap n True) = BitstringSet n; + +// HashmapAug, hashmap with an extra value +// (augmentation) of type Y at every node +// +ahm_edge#_ {n:#} {X:Type} {Y:Type} {l:#} {m:#} + label:(HmLabel ~l n) {n = (~m) + l} + node:(HashmapAugNode m X Y) = HashmapAug n X Y; +ahmn_leaf#_ {X:Type} {Y:Type} extra:Y value:X = HashmapAugNode 0 X Y; +ahmn_fork#_ {n:#} {X:Type} {Y:Type} left:^(HashmapAug n X Y) + right:^(HashmapAug n X Y) extra:Y = HashmapAugNode (n + 1) X Y; + +ahme_empty$0 {n:#} {X:Type} {Y:Type} extra:Y + = HashmapAugE n X Y; +ahme_root$1 {n:#} {X:Type} {Y:Type} root:^(HashmapAug n X Y) + extra:Y = HashmapAugE n X Y; + +// VarHashmap / VarHashmapE, with variable-length keys +// +vhm_edge#_ {n:#} {X:Type} {l:#} {m:#} label:(HmLabel ~l n) + {n = (~m) + l} node:(VarHashmapNode m X) + = VarHashmap n X; +vhmn_leaf$00 {n:#} {X:Type} value:X = VarHashmapNode n X; +vhmn_fork$01 {n:#} {X:Type} left:^(VarHashmap n X) + right:^(VarHashmap n X) value:(Maybe X) + = VarHashmapNode (n + 1) X; +vhmn_cont$1 {n:#} {X:Type} branch:Bit child:^(VarHashmap n X) + value:X = VarHashmapNode (n + 1) X; + +// nothing$0 {X:Type} = Maybe X; +// just$1 {X:Type} value:X = Maybe X; + +vhme_empty$0 {n:#} {X:Type} = VarHashmapE n X; +vhme_root$1 {n:#} {X:Type} root:^(VarHashmap n X) + = VarHashmapE n X; + +// +// PfxHashmap / PfxHashmapE, with variable-length keys +// constituting a prefix code +// + +phm_edge#_ {n:#} {X:Type} {l:#} {m:#} label:(HmLabel ~l n) + {n = (~m) + l} node:(PfxHashmapNode m X) + = PfxHashmap n X; + +phmn_leaf$0 {n:#} {X:Type} value:X = PfxHashmapNode n X; +phmn_fork$1 {n:#} {X:Type} left:^(PfxHashmap n X) + right:^(PfxHashmap n X) = PfxHashmapNode (n + 1) X; + +phme_empty$0 {n:#} {X:Type} = PfxHashmapE n X; +phme_root$1 {n:#} {X:Type} root:^(PfxHashmap n X) + = PfxHashmapE n X; +/* + * + * END hashmap.tlb + * + */ +// +// TON BLOCK LAYOUT +// +addr_none$00 = MsgAddressExt; +addr_extern$01 len:(## 9) external_address:(bits len) + = MsgAddressExt; +anycast_info$_ depth:(#<= 30) { depth >= 1 } + rewrite_pfx:(bits depth) = Anycast; +addr_std$10 anycast:(Maybe Anycast) + workchain_id:int8 address:bits256 = MsgAddressInt; +addr_var$11 anycast:(Maybe Anycast) addr_len:(## 9) + workchain_id:int32 address:(bits addr_len) = MsgAddressInt; +_ _:MsgAddressInt = MsgAddress; +_ _:MsgAddressExt = MsgAddress; +// +var_uint$_ {n:#} len:(#< n) value:(uint (len * 8)) + = VarUInteger n; +var_int$_ {n:#} len:(#< n) value:(int (len * 8)) + = VarInteger n; +nanograms$_ amount:(VarUInteger 16) = Grams; +// +extra_currencies$_ dict:(HashmapE 32 (VarUInteger 32)) + = ExtraCurrencyCollection; +currencies$_ grams:Grams other:ExtraCurrencyCollection + = CurrencyCollection; +// +int_msg_info$0 ihr_disabled:Bool bounce:Bool bounced:Bool + src:MsgAddressInt dest:MsgAddressInt + value:CurrencyCollection ihr_fee:Grams fwd_fee:Grams + created_lt:uint64 created_at:uint32 = CommonMsgInfo; +ext_in_msg_info$10 src:MsgAddressExt dest:MsgAddressInt + import_fee:Grams = CommonMsgInfo; +ext_out_msg_info$11 src:MsgAddressInt dest:MsgAddressExt + created_lt:uint64 created_at:uint32 = CommonMsgInfo; + +int_msg_info$0 ihr_disabled:Bool bounce:Bool bounced:Bool + src:MsgAddress dest:MsgAddressInt + value:CurrencyCollection ihr_fee:Grams fwd_fee:Grams + created_lt:uint64 created_at:uint32 = CommonMsgInfoRelaxed; +ext_out_msg_info$11 src:MsgAddress dest:MsgAddressExt + created_lt:uint64 created_at:uint32 = CommonMsgInfoRelaxed; + +tick_tock$_ tick:Bool tock:Bool = TickTock; + +_ split_depth:(Maybe (## 5)) special:(Maybe TickTock) + code:(Maybe ^Cell) data:(Maybe ^Cell) + library:(HashmapE 256 SimpleLib) = StateInit; + +simple_lib$_ public:Bool root:^Cell = SimpleLib; + +message$_ {X:Type} info:CommonMsgInfo + init:(Maybe (Either StateInit ^StateInit)) + body:(Either X ^X) = Message X; + +message$_ {X:Type} info:CommonMsgInfoRelaxed + init:(Maybe (Either StateInit ^StateInit)) + body:(Either X ^X) = MessageRelaxed X; +// +interm_addr_regular$0 use_dest_bits:(#<= 96) + = IntermediateAddress; +interm_addr_simple$10 workchain_id:int8 addr_pfx:uint64 + = IntermediateAddress; +interm_addr_ext$11 workchain_id:int32 addr_pfx:uint64 + = IntermediateAddress; +msg_envelope#4 cur_addr:IntermediateAddress + next_addr:IntermediateAddress fwd_fee_remaining:Grams + msg:^(Message Any) = MsgEnvelope; +// +msg_import_ext$000 msg:^(Message Any) transaction:^Transaction + = InMsg; +msg_import_ihr$010 msg:^(Message Any) transaction:^Transaction + ihr_fee:Grams proof_created:^Cell = InMsg; +msg_import_imm$011 in_msg:^MsgEnvelope + transaction:^Transaction fwd_fee:Grams = InMsg; +msg_import_fin$100 in_msg:^MsgEnvelope + transaction:^Transaction fwd_fee:Grams = InMsg; +msg_import_tr$101 in_msg:^MsgEnvelope out_msg:^MsgEnvelope + transit_fee:Grams = InMsg; +msg_discard_fin$110 in_msg:^MsgEnvelope transaction_id:uint64 + fwd_fee:Grams = InMsg; +msg_discard_tr$111 in_msg:^MsgEnvelope transaction_id:uint64 + fwd_fee:Grams proof_delivered:^Cell = InMsg; +// +import_fees$_ fees_collected:Grams + value_imported:CurrencyCollection = ImportFees; + +_ (HashmapAugE 256 InMsg ImportFees) = InMsgDescr; + +msg_export_ext$000 msg:^(Message Any) + transaction:^Transaction = OutMsg; +msg_export_imm$010 out_msg:^MsgEnvelope + transaction:^Transaction reimport:^InMsg = OutMsg; +msg_export_new$001 out_msg:^MsgEnvelope + transaction:^Transaction = OutMsg; +msg_export_tr$011 out_msg:^MsgEnvelope + imported:^InMsg = OutMsg; +msg_export_deq$110 out_msg:^MsgEnvelope // out_msg_hash:bits256 ? + import_block_lt:uint64 = OutMsg; +msg_export_tr_req$111 out_msg:^MsgEnvelope + imported:^InMsg = OutMsg; +msg_export_deq_imm$100 out_msg:^MsgEnvelope + reimport:^InMsg = OutMsg; + +_ enqueued_lt:uint64 out_msg:^MsgEnvelope = EnqueuedMsg; + +_ (HashmapAugE 256 OutMsg CurrencyCollection) = OutMsgDescr; + +_ (HashmapAugE 352 EnqueuedMsg uint64) = OutMsgQueue; + +processed_upto$_ last_msg_lt:uint64 last_msg_hash:bits256 = ProcessedUpto; +// key is [ shard:uint64 mc_seqno:uint32 ] +_ (HashmapE 96 ProcessedUpto) = ProcessedInfo; + +ihr_pending$_ import_lt:uint64 = IhrPendingSince; +_ (HashmapE 320 IhrPendingSince) = IhrPendingInfo; + +_ out_queue:OutMsgQueue proc_info:ProcessedInfo + ihr_pending:IhrPendingInfo = OutMsgQueueInfo; +// +storage_used$_ cells:(VarUInteger 7) bits:(VarUInteger 7) + public_cells:(VarUInteger 7) = StorageUsed; + +storage_used_short$_ cells:(VarUInteger 7) + bits:(VarUInteger 7) = StorageUsedShort; + +storage_info$_ used:StorageUsed last_paid:uint32 + due_payment:(Maybe Grams) = StorageInfo; + +account_none$0 = Account; +account$1 addr:MsgAddressInt storage_stat:StorageInfo + storage:AccountStorage = Account; + +account_storage$_ last_trans_lt:uint64 + balance:CurrencyCollection state:AccountState + = AccountStorage; + +account_uninit$00 = AccountState; +account_active$1 _:StateInit = AccountState; +account_frozen$01 state_hash:bits256 = AccountState; + +acc_state_uninit$00 = AccountStatus; +acc_state_frozen$01 = AccountStatus; +acc_state_active$10 = AccountStatus; +acc_state_nonexist$11 = AccountStatus; + +/* duplicates +tick_tock$_ tick:Bool tock:Bool = TickTock; + +_ split_depth:(Maybe (## 5)) special:(Maybe TickTock) + code:(Maybe ^Cell) data:(Maybe ^Cell) + library:(Maybe ^Cell) = StateInit; +*/ + +account_descr$_ account:^Account last_trans_hash:bits256 + last_trans_lt:uint64 = ShardAccount; + +depth_balance$_ split_depth:(#<= 30) balance:CurrencyCollection = DepthBalanceInfo; + +_ (HashmapAugE 256 ShardAccount DepthBalanceInfo) = ShardAccounts; + +transaction$0111 account_addr:bits256 lt:uint64 + prev_trans_hash:bits256 prev_trans_lt:uint64 now:uint32 + outmsg_cnt:uint15 + orig_status:AccountStatus end_status:AccountStatus + ^[ in_msg:(Maybe ^(Message Any)) out_msgs:(HashmapE 15 ^(Message Any)) ] + total_fees:CurrencyCollection state_update:^(HASH_UPDATE Account) + description:^TransactionDescr = Transaction; + +!merkle_update#02 {X:Type} old_hash:bits256 new_hash:bits256 + old:^X new:^X = MERKLE_UPDATE X; +update_hashes#72 {X:Type} old_hash:bits256 new_hash:bits256 + = HASH_UPDATE X; + +acc_trans#5 account_addr:bits256 + transactions:(HashmapAug 64 ^Transaction CurrencyCollection) + state_update:^(HASH_UPDATE Account) + = AccountBlock; + +_ (HashmapAugE 256 AccountBlock CurrencyCollection) = ShardAccountBlocks; +// +tr_phase_storage$_ storage_fees_collected:Grams + storage_fees_due:(Maybe Grams) + status_change:AccStatusChange + = TrStoragePhase; + +acst_unchanged$0 = AccStatusChange; // x -> x +acst_frozen$10 = AccStatusChange; // init -> frozen +acst_deleted$11 = AccStatusChange; // frozen -> deleted + +tr_phase_credit$_ due_fees_collected:(Maybe Grams) + credit:CurrencyCollection = TrCreditPhase; + +tr_phase_compute_skipped$0 reason:ComputeSkipReason + = TrComputePhase; +tr_phase_compute_vm$1 success:Bool msg_state_used:Bool + account_activated:Bool gas_fees:Grams + ^[ gas_used:(VarUInteger 7) + gas_limit:(VarUInteger 7) gas_credit:(Maybe (VarUInteger 3)) + mode:int8 exit_code:int32 exit_arg:(Maybe int32) + vm_steps:uint32 + vm_init_state_hash:bits256 vm_final_state_hash:bits256 ] + = TrComputePhase; +cskip_no_state$00 = ComputeSkipReason; +cskip_bad_state$01 = ComputeSkipReason; +cskip_no_gas$10 = ComputeSkipReason; + +tr_phase_action$_ success:Bool valid:Bool no_funds:Bool + status_change:AccStatusChange + total_fwd_fees:(Maybe Grams) total_action_fees:(Maybe Grams) + result_code:int32 result_arg:(Maybe int32) tot_actions:uint16 + spec_actions:uint16 skipped_actions:uint16 msgs_created:uint16 + action_list_hash:bits256 tot_msg_size:StorageUsedShort + = TrActionPhase; + +tr_phase_bounce_negfunds$00 = TrBouncePhase; +tr_phase_bounce_nofunds$01 msg_size:StorageUsedShort + req_fwd_fees:Grams = TrBouncePhase; +tr_phase_bounce_ok$1 msg_size:StorageUsedShort + msg_fees:Grams fwd_fees:Grams = TrBouncePhase; +// +trans_ord$0000 credit_first:Bool + storage_ph:(Maybe TrStoragePhase) + credit_ph:(Maybe TrCreditPhase) + compute_ph:TrComputePhase action:(Maybe ^TrActionPhase) + aborted:Bool bounce:(Maybe TrBouncePhase) + destroyed:Bool + = TransactionDescr; + +trans_storage$0001 storage_ph:TrStoragePhase + = TransactionDescr; + +trans_tick_tock$001 is_tock:Bool storage:TrStoragePhase + compute_ph:TrComputePhase action:(Maybe ^TrActionPhase) + aborted:Bool destroyed:Bool = TransactionDescr; +// +split_merge_info$_ cur_shard_pfx_len:(## 6) + acc_split_depth:(## 6) this_addr:bits256 sibling_addr:bits256 + = SplitMergeInfo; +trans_split_prepare$0100 split_info:SplitMergeInfo + compute_ph:TrComputePhase action:(Maybe ^TrActionPhase) + aborted:Bool destroyed:Bool + = TransactionDescr; +trans_split_install$0101 split_info:SplitMergeInfo + prepare_transaction:^Transaction + installed:Bool = TransactionDescr; + +trans_merge_prepare$0110 split_info:SplitMergeInfo + storage_ph:TrStoragePhase aborted:Bool + = TransactionDescr; +trans_merge_install$0111 split_info:SplitMergeInfo + prepare_transaction:^Transaction + credit_ph:(Maybe TrCreditPhase) + compute_ph:TrComputePhase action:(Maybe ^TrActionPhase) + aborted:Bool destroyed:Bool + = TransactionDescr; + +smc_info#076ef1ea actions:uint16 msgs_sent:uint16 + unixtime:uint32 block_lt:uint64 trans_lt:uint64 + rand_seed:bits256 balance_remaining:CurrencyCollection + myself:MsgAddressInt = SmartContractInfo; +// +// +out_list_empty$_ = OutList 0; +out_list$_ {n:#} prev:^(OutList n) action:OutAction + = OutList (n + 1); +action_send_msg#0ec3c86d mode:(## 8) + out_msg:^(MessageRelaxed Any) = OutAction; +action_set_code#ad4de08e new_code:^Cell = OutAction; +action_reserve_currency#36e6b809 mode:(## 8) + currency:CurrencyCollection = OutAction; + +out_list_node$_ prev:^Cell action:OutAction = OutListNode; +// +// +shard_ident$00 shard_pfx_bits:(#<= 60) + workchain_id:int32 shard_prefix:uint64 = ShardIdent; + +ext_blk_ref$_ end_lt:uint64 + seq_no:uint32 root_hash:bits256 file_hash:bits256 + = ExtBlkRef; + +block_id_ext$_ shard_id:ShardIdent seq_no:uint32 + root_hash:bits256 file_hash:bits256 = BlockIdExt; + +master_info$_ master:ExtBlkRef = BlkMasterInfo; + +shard_state#9023afe2 global_id:int32 + shard_id:ShardIdent + seq_no:uint32 vert_seq_no:# + gen_utime:uint32 gen_lt:uint64 + min_ref_mc_seqno:uint32 + out_msg_queue_info:^OutMsgQueueInfo + before_split:(## 1) + accounts:^ShardAccounts + ^[ overload_history:uint64 underload_history:uint64 + total_balance:CurrencyCollection + total_validator_fees:CurrencyCollection + libraries:(HashmapE 256 LibDescr) + master_ref:(Maybe BlkMasterInfo) ] + custom:(Maybe ^McStateExtra) + = ShardStateUnsplit; + +_ ShardStateUnsplit = ShardState; +split_state#5f327da5 left:^ShardStateUnsplit right:^ShardStateUnsplit = ShardState; + +shared_lib_descr$00 lib:^Cell publishers:(Hashmap 256 True) + = LibDescr; + +block_info#9bc7a987 version:uint32 + not_master:(## 1) + after_merge:(## 1) before_split:(## 1) + after_split:(## 1) + want_split:Bool want_merge:Bool + key_block:Bool vert_seqno_incr:(## 1) + flags:(## 8) + seq_no:# vert_seq_no:# { vert_seq_no >= vert_seqno_incr } + { prev_seq_no:# } { ~prev_seq_no + 1 = seq_no } + shard:ShardIdent gen_utime:uint32 + start_lt:uint64 end_lt:uint64 + gen_validator_list_hash_short:uint32 + gen_catchain_seqno:uint32 + min_ref_mc_seqno:uint32 + prev_key_block_seqno:uint32 + master_ref:not_master?^BlkMasterInfo + prev_ref:^(BlkPrevInfo after_merge) + prev_vert_ref:vert_seq_no?^(BlkPrevInfo 0) + = BlockInfo; + +prev_blk_info$_ prev:ExtBlkRef = BlkPrevInfo 0; +prev_blks_info$_ prev1:^ExtBlkRef prev2:^ExtBlkRef = BlkPrevInfo 1; + +block#11ef55aa global_id:int32 + info:^BlockInfo value_flow:^ValueFlow + state_update:^(MERKLE_UPDATE ShardState) + extra:^BlockExtra = Block; + +block_extra in_msg_descr:^InMsgDescr + out_msg_descr:^OutMsgDescr + account_blocks:^ShardAccountBlocks + rand_seed:bits256 + created_by:bits256 + custom:(Maybe ^McBlockExtra) = BlockExtra; +// +value_flow ^[ from_prev_blk:CurrencyCollection + to_next_blk:CurrencyCollection + imported:CurrencyCollection + exported:CurrencyCollection ] + fees_collected:CurrencyCollection + ^[ + fees_imported:CurrencyCollection + recovered:CurrencyCollection + created:CurrencyCollection + minted:CurrencyCollection + ] = ValueFlow; + +// +// +bt_leaf$0 {X:Type} leaf:X = BinTree X; +bt_fork$1 {X:Type} left:^(BinTree X) right:^(BinTree X) + = BinTree X; + +fsm_none$0 = FutureSplitMerge; +fsm_split$10 split_utime:uint32 interval:uint32 = FutureSplitMerge; +fsm_merge$11 merge_utime:uint32 interval:uint32 = FutureSplitMerge; + +shard_descr#b seq_no:uint32 reg_mc_seqno:uint32 + start_lt:uint64 end_lt:uint64 + root_hash:bits256 file_hash:bits256 + before_split:Bool before_merge:Bool + want_split:Bool want_merge:Bool + nx_cc_updated:Bool flags:(## 3) { flags = 0 } + next_catchain_seqno:uint32 next_validator_shard:uint64 + min_ref_mc_seqno:uint32 gen_utime:uint32 + split_merge_at:FutureSplitMerge + fees_collected:CurrencyCollection + funds_created:CurrencyCollection = ShardDescr; + +_ (HashmapE 32 ^(BinTree ShardDescr)) = ShardHashes; + +bta_leaf$0 {X:Type} {Y:Type} extra:Y leaf:X = BinTreeAug X Y; +bta_fork$1 {X:Type} {Y:Type} left:^(BinTreeAug X Y) + right:^(BinTreeAug X Y) extra:Y = BinTreeAug X Y; + +_ fees:CurrencyCollection create:CurrencyCollection = ShardFeeCreated; +_ (HashmapAugE 96 ShardFeeCreated ShardFeeCreated) = ShardFees; + +_ config_addr:bits256 config:^(Hashmap 32 ^Cell) + = ConfigParams; + +validator_info$_ + validator_list_hash_short:uint32 + catchain_seqno:uint32 + nx_cc_updated:Bool += ValidatorInfo; + +validator_base_info$_ + validator_list_hash_short:uint32 + catchain_seqno:uint32 += ValidatorBaseInfo; + +_ key:Bool max_end_lt:uint64 = KeyMaxLt; +_ key:Bool blk_ref:ExtBlkRef = KeyExtBlkRef; + +_ (HashmapAugE 32 KeyExtBlkRef KeyMaxLt) = OldMcBlocksInfo; + +masterchain_state_extra#cc26 + shard_hashes:ShardHashes + config:ConfigParams + ^[ flags:(## 16) { flags = 0 } + validator_info:ValidatorInfo + prev_blocks:OldMcBlocksInfo + after_key_block:Bool + last_key_block:(Maybe ExtBlkRef) ] + global_balance:CurrencyCollection += McStateExtra; + +ed25519_pubkey#8e81278a pubkey:bits256 = SigPubKey; // 288 bits +ed25519_signature#5 R:bits256 s:bits256 = CryptoSignatureSimple; // 516 bits +_ CryptoSignatureSimple = CryptoSignature; +sig_pair$_ node_id_short:bits256 sign:CryptoSignature = CryptoSignaturePair; // 256+x ~ 772 bits + +certificate#4 temp_key:SigPubKey valid_since:uint32 valid_until:uint32 = Certificate; // 356 bits +certificate_env#a419b7d certificate:Certificate = CertificateEnv; // 384 bits +signed_certificate$_ certificate:Certificate certificate_signature:CryptoSignature + = SignedCertificate; // 356+516 = 872 bits +// certificate_signature is the signature of CertificateEnv (with embedded certificate) with persistent key +chained_signature#f signed_cert:^SignedCertificate temp_key_signature:CryptoSignatureSimple + = CryptoSignature; // 4+(356+516)+516 = 520 bits+ref (1392 bits total) +// temp_key_signature is the signature of whatever was originally intended to be signed with temp_key from certificate + +masterchain_block_extra#cca5 + key_block:(## 1) + shard_hashes:ShardHashes + shard_fees:ShardFees + ^[ prev_blk_signatures:(HashmapE 16 CryptoSignaturePair) + recover_create_msg:(Maybe ^InMsg) + mint_msg:(Maybe ^InMsg) ] + config:key_block?ConfigParams += McBlockExtra; + +// +// CONFIGURATION PARAMETERS +// + +validator#53 public_key:SigPubKey weight:uint64 = ValidatorDescr; +validator_addr#73 public_key:SigPubKey weight:uint64 adnl_addr:bits256 = ValidatorDescr; +validators#11 utime_since:uint32 utime_until:uint32 + total:(## 16) main:(## 16) { main <= total } { main >= 1 } + list:(Hashmap 16 ValidatorDescr) = ValidatorSet; + +_ config_addr:bits256 = ConfigParam 0; +_ elector_addr:bits256 = ConfigParam 1; +_ minter_addr:bits256 = ConfigParam 2; // ConfigParam 0 is used if absent +_ fee_collector_addr:bits256 = ConfigParam 3; // ConfigParam 1 is used if absent +_ dns_root_addr:bits256 = ConfigParam 4; // root TON DNS resolver + +_ mint_new_price:Grams mint_add_price:Grams = ConfigParam 6; +_ to_mint:ExtraCurrencyCollection = ConfigParam 7; + +capabilities#c4 version:uint32 capabilities:uint64 = GlobalVersion; +_ GlobalVersion = ConfigParam 8; // all zero if absent +_ mandatory_params:(Hashmap 32 True) = ConfigParam 9; + +wfmt_basic#1 vm_version:int32 vm_mode:uint64 = WorkchainFormat 1; +wfmt_ext#0 min_addr_len:(## 12) max_addr_len:(## 12) addr_len_step:(## 12) + { min_addr_len >= 64 } { min_addr_len <= max_addr_len } + { max_addr_len <= 1023 } { addr_len_step <= 1023 } + workchain_type_id:(## 32) { workchain_type_id >= 1 } + = WorkchainFormat 0; + +workchain#a6 enabled_since:uint32 actual_min_split:(## 8) + min_split:(## 8) max_split:(## 8) { actual_min_split <= min_split } +//workchain#a5 enabled_since:uint32 min_split:(## 8) max_split:(## 8) +// { min_split <= max_split } { max_split <= 60 } + basic:(## 1) active:Bool accept_msgs:Bool flags:(## 13) { flags = 0 } + zerostate_root_hash:bits256 zerostate_file_hash:bits256 + version:uint32 format:(WorkchainFormat basic) + = WorkchainDescr; + +_ workchains:(HashmapE 32 WorkchainDescr) = ConfigParam 12; + +block_grams_created#6b masterchain_block_fee:Grams basechain_block_fee:Grams + = BlockCreateFees; +_ BlockCreateFees = ConfigParam 14; + +_ validators_elected_for:uint32 elections_start_before:uint32 + elections_end_before:uint32 stake_held_for:uint32 + = ConfigParam 15; + +_ max_validators:(## 16) max_main_validators:(## 16) min_validators:(## 16) + { max_validators >= max_main_validators } + { max_main_validators >= min_validators } + { min_validators >= 1 } + = ConfigParam 16; + +_ min_stake:Grams max_stake:Grams min_total_stake:Grams max_stake_factor:uint32 = ConfigParam 17; + +_#cc utime_since:uint32 bit_price_ps:uint64 cell_price_ps:uint64 + mc_bit_price_ps:uint64 mc_cell_price_ps:uint64 = StoragePrices; +_ (Hashmap 32 StoragePrices) = ConfigParam 18; + +gas_prices#dd gas_price:uint64 gas_limit:uint64 gas_credit:uint64 + block_gas_limit:uint64 freeze_due_limit:uint64 delete_due_limit:uint64 + = GasLimitsPrices; + +config_mc_gas_prices#_ GasLimitsPrices = ConfigParam 20; +config_gas_prices#_ GasLimitsPrices = ConfigParam 21; + +param_limits#c3 underload:# soft_limit:# { underload <= soft_limit } + hard_limit:# { soft_limit <= hard_limit } = ParamLimits; +block_limits#5d bytes:ParamLimits gas:ParamLimits lt_delta:ParamLimits + = BlockLimits; + +config_mc_block_limits#_ BlockLimits = ConfigParam 22; +config_block_limits#_ BlockLimits = ConfigParam 23; + +// msg_fwd_fees = (lump_price + ceil((bit_price * msg.bits + cell_price * msg.cells)/2^16)) nanograms +// ihr_fwd_fees = ceil((msg_fwd_fees * ihr_price_factor)/2^16) nanograms +// bits in the root cell of a message are not included in msg.bits (lump_price pays for them) +msg_forward_prices#ea lump_price:uint64 bit_price:uint64 cell_price:uint64 + ihr_price_factor:uint32 first_frac:uint16 next_frac:uint16 = MsgForwardPrices; + +// used for messages to/from masterchain +config_mc_fwd_prices#_ MsgForwardPrices = ConfigParam 24; +// used for all other messages +config_fwd_prices#_ MsgForwardPrices = ConfigParam 25; + +catchain_config#c1 mc_catchain_lifetime:uint32 shard_catchain_lifetime:uint32 + shard_validators_lifetime:uint32 shard_validators_num:uint32 = CatchainConfig; +consensus_config#d6 round_candidates:# { round_candidates >= 1 } + next_candidate_delay_ms:uint32 consensus_timeout_ms:uint32 + fast_attempts:uint32 attempt_duration:uint32 catchain_max_deps:uint32 + max_block_bytes:uint32 max_collated_bytes:uint32 = ConsensusConfig; + +_ CatchainConfig = ConfigParam 28; +_ ConsensusConfig = ConfigParam 29; + +_ fundamental_smc_addr:(HashmapE 256 True) = ConfigParam 31; +_ prev_validators:ValidatorSet = ConfigParam 32; +_ prev_temp_validators:ValidatorSet = ConfigParam 33; +_ cur_validators:ValidatorSet = ConfigParam 34; +_ cur_temp_validators:ValidatorSet = ConfigParam 35; +_ next_validators:ValidatorSet = ConfigParam 36; +_ next_temp_validators:ValidatorSet = ConfigParam 37; + +validator_temp_key#3 adnl_addr:bits256 temp_public_key:SigPubKey seqno:# valid_until:uint32 = ValidatorTempKey; +signed_temp_key#4 key:^ValidatorTempKey signature:CryptoSignature = ValidatorSignedTempKey; +_ (HashmapE 256 ValidatorSignedTempKey) = ConfigParam 39; + +// +// PROOFS +// +block_signatures_pure#_ sig_count:uint32 sig_weight:uint64 + signatures:(HashmapE 16 CryptoSignaturePair) = BlockSignaturesPure; +block_signatures#11 validator_info:ValidatorBaseInfo pure_signatures:BlockSignaturesPure = BlockSignatures; +block_proof#c3 proof_for:BlockIdExt root:^Cell signatures:(Maybe ^BlockSignatures) = BlockProof; + +chain_empty$_ = ProofChain 0; +chain_link$_ {n:#} root:^Cell prev:n?^(ProofChain n) = ProofChain (n + 1); +top_block_descr#d5 proof_for:BlockIdExt signatures:(Maybe ^BlockSignatures) + len:(## 8) { len >= 1 } { len <= 8 } chain:(ProofChain len) = TopBlockDescr; + +// +// COLLATED DATA +// +top_block_descr_set#4ac789f3 collection:(HashmapE 96 ^TopBlockDescr) = TopBlockDescrSet; diff --git a/crypto/block/check-proof.cpp b/crypto/block/check-proof.cpp new file mode 100644 index 00000000..c1ea8ad3 --- /dev/null +++ b/crypto/block/check-proof.cpp @@ -0,0 +1,294 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "check-proof.h" +#include "block/block.h" +#include "block/block-parse.h" +#include "block/block-auto.h" +#include "block/mc-config.h" + +#include "ton/ton-shard.h" + +#include "vm/cells/MerkleProof.h" + +namespace block { +td::Status check_block_header_proof(td::Ref root, ton::BlockIdExt blkid, ton::Bits256* store_shard_hash_to, + bool check_state_hash) { + ton::RootHash vhash{root->get_hash().bits()}; + if (vhash != blkid.root_hash) { + return td::Status::Error(PSTRING() << " block header for block " << blkid.to_str() << " has incorrect root hash " + << vhash.to_hex() << " instead of " << blkid.root_hash.to_hex()); + } + std::vector prev; + ton::BlockIdExt mc_blkid; + bool after_split; + TRY_STATUS(block::unpack_block_prev_blk_try(root, blkid, prev, mc_blkid, after_split)); + block::gen::Block::Record blk; + block::gen::BlockInfo::Record info; + if (!(tlb::unpack_cell(root, blk) && tlb::unpack_cell(blk.info, info))) { + return td::Status::Error(std::string{"cannot unpack header for block "} + blkid.to_str()); + } + if (store_shard_hash_to) { + vm::CellSlice upd_cs{vm::NoVmSpec(), blk.state_update}; + if (!(upd_cs.is_special() && upd_cs.prefetch_long(8) == 4 // merkle update + && upd_cs.size_ext() == 0x20228)) { + return td::Status::Error("invalid Merkle update in block header"); + } + auto upd_hash = upd_cs.prefetch_ref(1)->get_hash(0); + if (!check_state_hash) { + *store_shard_hash_to = upd_hash.bits(); + } else if (store_shard_hash_to->compare(upd_hash.bits())) { + return td::Status::Error(PSTRING() << "state hash mismatch in block header of " << blkid.to_str() + << " : header declares " << upd_hash.bits().to_hex(256) << " expected " + << store_shard_hash_to->to_hex()); + } + } + return td::Status::OK(); +} + +td::Result check_state_proof(ton::BlockIdExt blkid, td::Slice proof) { + TRY_RESULT(proof_root, vm::std_boc_deserialize(proof)); + auto virt_root = vm::MerkleProof::virtualize(std::move(proof_root), 1); + if (virt_root.is_null()) { + return td::Status::Error("account state proof is invalid"); + } + td::Bits256 state_hash; + TRY_STATUS(check_block_header_proof(std::move(virt_root), blkid, &state_hash)); + return state_hash; +} + +td::Result> check_extract_state_proof(ton::BlockIdExt blkid, td::Slice proof, td::Slice data) { + try { + TRY_RESULT(state_hash, check_state_proof(blkid, proof)); + TRY_RESULT(state_root, vm::std_boc_deserialize(data)); + auto state_virt_root = vm::MerkleProof::virtualize(std::move(state_root), 1); + if (state_virt_root.is_null()) { + return td::Status::Error("account state proof is invalid"); + } + if (state_hash != state_virt_root->get_hash().bits()) { + return td::Status::Error("root hash mismatch in the shardchain state proof"); + } + return std::move(state_virt_root); + } catch (vm::VmError& err) { + return td::Status::Error(PSLICE() << "error scanning shard state proof: " << err.get_msg()); + } catch (vm::VmVirtError& err) { + return td::Status::Error(PSLICE() << "virtualization error scanning shard state proof: " << err.get_msg()); + } +} + +td::Status check_shard_proof(ton::BlockIdExt blk, ton::BlockIdExt shard_blk, td::Slice shard_proof) { + if (blk == shard_blk) { + if (!shard_proof.empty()) { + LOG(WARNING) << "Unexpected non-empty shard proof"; + } + return td::Status::OK(); + } + if (!blk.is_masterchain() || !blk.is_valid_full()) { + return td::Status::Error(PSLICE() << "reference block " << blk.to_str() + << " for a getAccountState query must belong to the masterchain"); + } + TRY_RESULT_PREFIX(P_roots, vm::std_boc_deserialize_multi(std::move(shard_proof)), + "cannot deserialize shard configuration proof"); + if (P_roots.size() != 2) { + return td::Status::Error("shard configuration proof must have exactly two roots"); + } + try { + auto mc_state_root = vm::MerkleProof::virtualize(std::move(P_roots[1]), 1); + if (mc_state_root.is_null()) { + return td::Status::Error("shard configuration proof is invalid"); + } + ton::Bits256 mc_state_hash = mc_state_root->get_hash().bits(); + TRY_STATUS_PREFIX( + check_block_header_proof(vm::MerkleProof::virtualize(std::move(P_roots[0]), 1), blk, &mc_state_hash, true), + "error in shard configuration block header proof :"); + block::gen::ShardStateUnsplit::Record sstate; + if (!(tlb::unpack_cell(mc_state_root, sstate))) { + return td::Status::Error("cannot unpack masterchain state header"); + } + auto shards_dict = block::ShardConfig::extract_shard_hashes_dict(std::move(mc_state_root)); + if (!shards_dict) { + return td::Status::Error("cannot extract shard configuration dictionary from proof"); + } + vm::CellSlice cs; + ton::ShardIdFull true_shard; + if (!block::ShardConfig::get_shard_hash_raw_from(*shards_dict, cs, shard_blk.shard_full(), true_shard)) { + return td::Status::Error(PSLICE() << "masterchain state contains no information for shard " + << shard_blk.shard_full().to_str()); + } + auto shard_info = block::McShardHash::unpack(cs, true_shard); + if (shard_info.is_null()) { + return td::Status::Error(PSLICE() << "cannot unpack information for shard " << shard_blk.shard_full().to_str() + << " from masterchain state"); + } + if (shard_info->top_block_id() != shard_blk) { + return td::Status::Error(PSLICE() << "shard configuration mismatch: expected to find block " << shard_blk.to_str() + << " , found " << shard_info->top_block_id().to_str()); + } + } catch (vm::VmError err) { + return td::Status::Error(PSLICE() << "error while traversing shard configuration proof : " << err.get_msg()); + } catch (vm::VmVirtError err) { + return td::Status::Error(PSLICE() << "virtualization error while traversing shard configuration proof : " + << err.get_msg()); + } + return td::Status::OK(); +} + +td::Status check_account_proof(td::Slice proof, ton::BlockIdExt shard_blk, const block::StdAddress& addr, + td::Ref root, ton::LogicalTime* last_trans_lt, ton::Bits256* last_trans_hash) { + TRY_RESULT_PREFIX(Q_roots, vm::std_boc_deserialize_multi(std::move(proof)), "cannot deserialize account proof"); + if (Q_roots.size() != 2) { + return td::Status::Error(PSLICE() << "account state proof must have exactly two roots"); + } + + if (last_trans_lt) { + last_trans_hash->set_zero(); + } + + try { + auto state_root = vm::MerkleProof::virtualize(std::move(Q_roots[1]), 1); + if (state_root.is_null()) { + return td::Status::Error("account state proof is invalid"); + } + ton::Bits256 state_hash = state_root->get_hash().bits(); + TRY_STATUS_PREFIX( + check_block_header_proof(vm::MerkleProof::virtualize(std::move(Q_roots[0]), 1), shard_blk, &state_hash, true), + "error in account shard block header proof : "); + block::gen::ShardStateUnsplit::Record sstate; + if (!(tlb::unpack_cell(std::move(state_root), sstate))) { + return td::Status::Error("cannot unpack state header"); + } + vm::AugmentedDictionary accounts_dict{vm::load_cell_slice(sstate.accounts).prefetch_ref(), 256, + block::tlb::aug_ShardAccounts}; + auto acc_csr = accounts_dict.lookup(addr.addr); + if (acc_csr.not_null()) { + if (root.is_null()) { + return td::Status::Error(PSLICE() << "account state proof shows that account state for " << addr + << " must be non-empty, but it actually is empty"); + } + block::gen::ShardAccount::Record acc_info; + if (!tlb::csr_unpack(std::move(acc_csr), acc_info)) { + return td::Status::Error("cannot unpack ShardAccount from proof"); + } + if (acc_info.account->get_hash().bits().compare(root->get_hash().bits(), 256)) { + return td::Status::Error(PSLICE() << "account state hash mismatch: Merkle proof expects " + << acc_info.account->get_hash().bits().to_hex(256) + << " but received data has " << root->get_hash().bits().to_hex(256)); + } + if (last_trans_hash) { + *last_trans_hash = acc_info.last_trans_hash; + } + if (last_trans_lt) { + *last_trans_lt = acc_info.last_trans_lt; + } + } else if (root.not_null()) { + return td::Status::Error(PSLICE() << "account state proof shows that account state for " << addr + << " must be empty, but it is not"); + } + } catch (vm::VmError err) { + return td::Status::Error(PSLICE() << "error while traversing account proof : " << err.get_msg()); + } catch (vm::VmVirtError err) { + return td::Status::Error(PSLICE() << "virtualization error while traversing account proof : " << err.get_msg()); + } + return td::Status::OK(); +} + +td::Result AccountState::validate(ton::BlockIdExt ref_blk, block::StdAddress addr) const { + TRY_RESULT_PREFIX(root, vm::std_boc_deserialize(state.as_slice(), true), "cannot deserialize account state"); + + LOG(INFO) << "got account state for " << addr << " with respect to blocks " << blk.to_str() + << (shard_blk == blk ? "" : std::string{" and "} + shard_blk.to_str()); + if (blk != ref_blk && ref_blk.id.seqno != ~0U) { + return td::Status::Error(PSLICE() << "obtained getAccountState() for a different reference block " << blk.to_str() + << " instead of requested " << ref_blk.to_str()); + } + + if (!shard_blk.is_valid_full()) { + return td::Status::Error(PSLICE() << "shard block id " << shard_blk.to_str() << " in answer is invalid"); + } + + if (!ton::shard_contains(shard_blk.shard_full(), ton::extract_addr_prefix(addr.workchain, addr.addr))) { + return td::Status::Error(PSLICE() << "received data from shard block " << shard_blk.to_str() + << " that cannot contain requested account"); + } + + TRY_STATUS(block::check_shard_proof(blk, shard_blk, shard_proof.as_slice())); + + Info res; + TRY_STATUS( + block::check_account_proof(proof.as_slice(), shard_blk, addr, root, &res.last_trans_lt, &res.last_trans_hash)); + res.root = std::move(root); + + return res; +} + +td::Result Transaction::validate() { + if (root.is_null()) { + return td::Status::Error("transactions are expected to be non-empty"); + } + if (hash != root->get_hash().bits()) { + return td::Status::Error(PSLICE() << "transaction hash mismatch: expected " << hash.to_hex() << ", found " + << root->get_hash().bits().to_hex(256)); + } + block::gen::Transaction::Record trans; + if (!tlb::unpack_cell(root, trans)) { + return td::Status::Error("cannot unpack transaction #"); + } + + if (trans.lt != lt) { + return td::Status::Error(PSLICE() << "transaction lt mismatch: expected " << lt << ", found " << trans.lt); + } + Info res; + res.blkid = blkid; + res.prev_trans_lt = trans.prev_trans_lt; + res.prev_trans_hash = trans.prev_trans_hash; + res.transaction = root; + return std::move(res); +} + +td::Result TransactionList::validate() const { + if (blkids.empty()) { + return td::Status::Error("transaction list must be non-empty"); + } + auto R = vm::std_boc_deserialize_multi(std::move(transactions_boc)); + if (R.is_error()) { + return td::Status::Error("cannot deserialize transactions BoC"); + } + auto list = R.move_as_ok(); + if (list.size() != blkids.size()) { + return td::Status::Error(PSLICE() << "transaction list size " << list.size() + << " must be equal to the size of block id list " << blkids.size()); + } + size_t c = 0; + Info res; + auto current_lt = lt; + auto current_hash = hash; + for (auto& root : list) { + const auto& blkid = blkids[c++]; + Transaction transaction; + transaction.blkid = std::move(blkid); + transaction.lt = current_lt; + transaction.hash = current_hash; + transaction.root = root; + TRY_RESULT(info, transaction.validate()); + current_lt = info.prev_trans_lt; + current_hash = info.prev_trans_hash; + res.transactions.push_back(std::move(info)); + } + return std::move(res); +} +} // namespace block diff --git a/crypto/block/check-proof.h b/crypto/block/check-proof.h new file mode 100644 index 00000000..2bd0049a --- /dev/null +++ b/crypto/block/check-proof.h @@ -0,0 +1,80 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +#include "vm/cells.h" +#include "block/block.h" + +namespace block { +using td::Ref; + +td::Status check_block_header_proof(td::Ref root, ton::BlockIdExt blkid, + ton::Bits256* store_shard_hash_to = nullptr, bool check_state_hash = false); +td::Status check_shard_proof(ton::BlockIdExt blk, ton::BlockIdExt shard_blk, td::Slice shard_proof); +td::Status check_account_proof(td::Slice proof, ton::BlockIdExt shard_blk, const block::StdAddress& addr, + td::Ref root, ton::LogicalTime* last_trans_lt = nullptr, + ton::Bits256* last_trans_hash = nullptr); +td::Result check_state_proof(ton::BlockIdExt blkid, td::Slice proof); +td::Result> check_extract_state_proof(ton::BlockIdExt blkid, td::Slice proof, td::Slice data); + +struct AccountState { + ton::BlockIdExt blk; + ton::BlockIdExt shard_blk; + td::BufferSlice shard_proof; + td::BufferSlice proof; + td::BufferSlice state; + + struct Info { + td::Ref root; + ton::LogicalTime last_trans_lt = 0; + ton::Bits256 last_trans_hash; + }; + + td::Result validate(ton::BlockIdExt ref_blk, block::StdAddress addr) const; +}; + +struct Transaction { + ton::BlockIdExt blkid; + ton::LogicalTime lt; + ton::Bits256 hash; + td::Ref root; + + struct Info { + ton::BlockIdExt blkid; + ton::LogicalTime prev_trans_lt; + ton::Bits256 prev_trans_hash; + td::Ref transaction; + }; + td::Result validate(); +}; + +struct TransactionList { + ton::LogicalTime lt; + ton::Bits256 hash; + std::vector blkids; + td::BufferSlice transactions_boc; + + struct Info { + std::vector transactions; + }; + + td::Result validate() const; +}; + +} // namespace block diff --git a/crypto/block/create-state.cpp b/crypto/block/create-state.cpp new file mode 100644 index 00000000..8b92a24f --- /dev/null +++ b/crypto/block/create-state.cpp @@ -0,0 +1,780 @@ +/* + This file is part of TON Blockchain source code. + + TON Blockchain is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + TON Blockchain is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with TON Blockchain. If not, see . + + In addition, as a special exception, the copyright holders give permission + to link the code of portions of this program with the OpenSSL library. + You must obey the GNU General Public License in all respects for all + of the code used other than OpenSSL. If you modify file(s) with this + exception, you may extend this exception to your version of the file(s), + but you are not obligated to do so. If you do not wish to do so, delete this + exception statement from your version. If you delete this exception statement + from all source files in the program, then also delete it here. + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "vm/stack.hpp" +#include "vm/boc.h" + +#include "fift/Fift.h" +#include "fift/Dictionary.h" +#include "fift/SourceLookup.h" +#include "fift/words.h" + +#include "td/utils/logging.h" +#include "td/utils/misc.h" +#include "td/utils/Parser.h" +#include "td/utils/port/path.h" +#include "td/utils/port/signals.h" + +#include "block.h" +#include "block-parse.h" +#include "block-auto.h" +#include "mc-config.h" + +#define PDO(__op) \ + if (!(__op)) { \ + ok = false; \ + } +#define THRERR(__msg) \ + if (!ok) { \ + throw fift::IntError{__msg}; \ + } +#define RETERR \ + if (!ok) { \ + return false; \ + } + +using td::Ref; + +int verbosity; + +enum { wc_master = -1, wc_base = 0 }; +constexpr int wc_undef = std::numeric_limits::min(); + +int workchain_id = wc_undef; +int global_id = 0; + +typedef td::BitArray<256> hash_t; + +struct SmcDescr { + hash_t addr; + int split_depth; + bool preinit_only; + td::RefInt256 gram_balance; + Ref state_init; // StateInit + Ref account; // Account + SmcDescr(const hash_t& _addr) : addr(_addr), split_depth(0), preinit_only(false) { + } +}; + +std::map smart_contracts; +td::RefInt256 total_smc_balance{true, 0}, max_total_smc_balance; + +struct PublicLibDescr { + Ref root; + std::set publishers; + PublicLibDescr(Ref _root) : root(std::move(_root)) { + } +}; + +std::map public_libraries; + +hash_t config_addr; +Ref config_param_root; +bool config_addr_set; +vm::Dictionary config_dict{32}; + +ton::UnixTime now; + +bool set_config_smc(const SmcDescr& smc) { + if (config_addr_set || smc.preinit_only || workchain_id != wc_master || smc.split_depth) { + return false; + } + vm::CellSlice cs = load_cell_slice(smc.state_init); + bool ok = true; + PDO(block::gen::t_Maybe_natwidth_5.skip(cs) && block::gen::t_Maybe_TickTock.skip(cs) && + block::gen::t_Maybe_Ref_Cell.skip(cs)); + RETERR; + Ref data; + PDO(cs.fetch_ulong(1) == 1 && cs.fetch_ref_to(data)); + THRERR("config smart contract must have non-empty data"); + vm::CellSlice cs2 = load_cell_slice(data); + PDO(cs2.fetch_ref_to(data)); + THRERR("first reference in config smart contract data must point to initial configuration"); + PDO(block::valid_config_data(data, smc.addr)); + THRERR("invalid smart contract configuration data"); + config_addr = smc.addr; + config_param_root = std::move(data); + config_addr_set = true; + if (verbosity > 2) { + std::cerr << "set smart contract " << config_addr << " as the configuration smart contract with configuration:\n"; + load_cell_slice(config_param_root).print_rec(std::cerr); + } + return true; +} + +void interpret_set_workchain(vm::Stack& stack) { + workchain_id = stack.pop_smallint_range(0x7fffffff, -0x7fffffff); +} + +void interpret_get_workchain(vm::Stack& stack) { + stack.push_smallint(workchain_id); +} + +void interpret_set_global_id(vm::Stack& stack) { + global_id = stack.pop_smallint_range(0x7fffffff, -0x7fffffff); +} + +void interpret_get_global_id(vm::Stack& stack) { + stack.push_smallint(global_id); +} + +void interpret_get_verbosity(vm::Stack& stack) { + stack.push_smallint(GET_VERBOSITY_LEVEL()); +} + +void interpret_set_verbosity(vm::Stack& stack) { + int x = stack.pop_smallint_range(15); + SET_VERBOSITY_LEVEL(x); +} + +void interpret_set_config_smartcontract(vm::Stack& stack) { + if (workchain_id != wc_master) { + throw fift::IntError{"configuration smart contract may be selected in masterchain only"}; + } + if (config_addr_set) { + throw fift::IntError{"configuration smart contract already selected"}; + } + td::RefInt256 int_addr = stack.pop_int_finite(); + hash_t addr; + if (!int_addr->export_bits(addr.bits(), 256, false)) { + throw fift::IntError{"not a valid smart-contract address"}; + } + auto it = smart_contracts.find(addr); + if (it == smart_contracts.end()) { + throw fift::IntError{"unknown smart contract"}; + } + const SmcDescr& smc = it->second; + assert(smc.addr == addr); + if (smc.preinit_only) { + throw fift::IntError{"configuration smart contract must be completely initialized"}; + } + if (!set_config_smc(smc)) { + throw fift::IntError{"invalid configuration smart contract"}; + } +} + +bool is_empty_cell(Ref cell) { + bool is_special; + auto cs = load_cell_slice_special(std::move(cell), is_special); + return !is_special && cs.empty_ext(); +} + +bool add_public_library(hash_t lib_addr, hash_t smc_addr, Ref lib_root) { + if (lib_root.is_null() || lib_root->get_hash().as_array() != lib_addr.as_array()) { + return false; + } + auto ins = public_libraries.emplace(lib_addr, lib_root); + PublicLibDescr& lib = ins.first->second; + lib.publishers.insert(smc_addr); + if (verbosity > 2) { + std::cerr << "added " << (ins.second ? "new " : "") << "public library " << lib_addr << " with publisher " + << smc_addr << std::endl; + } + return true; +} + +td::RefInt256 create_smartcontract(td::RefInt256 smc_addr, Ref code, Ref data, + Ref library, td::RefInt256 balance, int special, int split_depth, + int mode) { + if (is_empty_cell(code)) { + code.clear(); + } + if (is_empty_cell(data)) { + data.clear(); + } + if (is_empty_cell(library)) { + library.clear(); + } + bool ok = true; + if (library.not_null()) { + PDO(block::valid_library_collection(library, false)); + THRERR("not a valid library collection"); + } + vm::CellBuilder cb; + if (!split_depth) { + PDO(cb.store_long_bool(0, 1)); + } else { + PDO(cb.store_long_bool(1, 1) && cb.store_ulong_rchk_bool(split_depth, 5)); + } + THRERR("invalid split_depth for a smart contract"); + if (!special) { + PDO(cb.store_long_bool(0, 1)); + } else { + PDO(cb.store_long_bool(1, 1) && cb.store_ulong_rchk_bool(special, 2)); + } + THRERR("invalid special TickTock argument for a smart contract"); + PDO(cb.store_maybe_ref(std::move(code)) && cb.store_maybe_ref(std::move(data)) && cb.store_maybe_ref(library)); + THRERR("cannot store smart-contract code, data or library"); + Ref state_init = cb.finalize(); + hash_t addr; + if (smc_addr.is_null()) { + addr = state_init->get_hash().as_array(); + smc_addr = td::RefInt256{true}; + PDO(smc_addr.write().import_bits(addr.data(), 0, 256, false)); + } else if (mode == 1) { + throw fift::IntError{"cannot create uninitialized smart contracts with specified addresses"}; + } else { + PDO(smc_addr->export_bits(addr.data(), 0, 256, false)); + } + THRERR("cannot initialize smart-contract address"); + if (verbosity > 2) { + std::cerr << "smart-contract address is "; + std::cerr << addr << " = " << smc_addr << std::endl; + } + PDO(mode || !sgn(balance)); + THRERR("cannot set non-zero balance to smart contract unless it is initialized"); + PDO(sgn(balance) >= 0); + THRERR("balance cannot be negative"); + if (!mode) { + return smc_addr; // compute address only + } + auto it = smart_contracts.find(addr); + if (it != smart_contracts.end()) { + std::cerr << "smart contract " << addr << " already defined\n"; + throw fift::IntError{"smart contract already exists"}; + } + auto ins = smart_contracts.emplace(addr, addr); + assert(ins.second); + SmcDescr& smc = ins.first->second; + smc.split_depth = split_depth; + smc.preinit_only = (mode == 1); + smc.gram_balance = balance; + total_smc_balance += balance; + if (mode > 1) { + smc.state_init = std::move(state_init); + } + if (max_total_smc_balance.not_null() && total_smc_balance > max_total_smc_balance) { + throw fift::IntError{"total smart-contract balance exceeds limit"}; + } + cb.reset(); + PDO(cb.store_long_bool(0, 64) // account_storage$_ last_trans_lt:uint64 + && block::tlb::t_Grams.store_integer_value(cb, *balance) // balance.grams:Grams + && cb.store_long_bool(0, 1)); // balance.other:ExtraCurrencyCollection + if (mode == 1) { + PDO(block::gen::t_AccountState.pack_account_uninit(cb)); + } else { + PDO(block::gen::t_AccountState.pack_account_active(cb, vm::load_cell_slice_ref(smc.state_init))); + } + THRERR("cannot create smart-contract AccountStorage"); + Ref storage = cb.finalize(); + vm::CellStorageStat stats; + PDO(stats.compute_used_storage(Ref(storage))); + if (verbosity > 2) { + std::cerr << "storage is:\n"; + vm::load_cell_slice(storage).print_rec(std::cerr); + std::cerr << "stats: bits=" << stats.bits << ", cells=" << stats.cells << std::endl; + std::cerr << "block::gen::AccountStorage.validate_ref() = " << block::gen::t_AccountStorage.validate_ref(storage) + << std::endl; + std::cerr << "block::tlb::AccountStorage.validate_ref() = " << block::tlb::t_AccountStorage.validate_ref(storage) + << std::endl; + } + PDO(block::gen::t_AccountStorage.validate_ref(storage)); + THRERR("AccountStorage of created smart-contract is invalid (?)"); + cb.reset(); // build Account + PDO(cb.store_long_bool(1, 1)); // account$1 + int ctor = 3; // addr_var$11 + if (workchain_id >= -128 && workchain_id <= 127) { + ctor = 2; // addr_std$10 + } + PDO(cb.store_long_bool(ctor, 2)); // addr_std$10 or addr_var$11 + if (split_depth) { + PDO(cb.store_long_bool(1, 1) // just$1 + && cb.store_ulong_rchk_bool(split_depth, 5) // depth:(## 5) + && cb.store_bits_bool(addr.cbits(), split_depth)); // rewrite pfx:(depth * Bit) + } else { + PDO(cb.store_long_bool(0, 1)); // nothing$0 + } + PDO(cb.store_long_rchk_bool(workchain_id, ctor == 2 ? 8 : 32) && cb.store_bits_bool(addr.cbits(), 256)); + THRERR("Cannot serialize addr:MsgAddressInt of the new smart contract"); + // storage_stat:StorageInfo -> storage_stat.used:StorageUsed + PDO(block::store_UInt7(cb, stats.cells) // cells:(VarUInteger 7) + && block::store_UInt7(cb, stats.bits) // bits:(VarUInteger 7) + && block::store_UInt7(cb, stats.public_cells)); // public_cells:(VarUInteger 7) + THRERR("Cannot serialize used:StorageUsed of the new smart contract"); + PDO(cb.store_long_bool(0, 33)); // last_paid:uint32 due_payment:(Maybe Grams) + PDO(cb.append_data_cell_bool(storage)); // storage:AccountStorage + THRERR("Cannot create Account of the new smart contract"); + smc.account = cb.finalize(); + if (verbosity > 2) { + std::cerr << "account is:\n"; + vm::load_cell_slice(smc.account).print_rec(std::cerr); + std::cerr << "block::gen::Account.validate_ref() = " << block::gen::t_Account.validate_ref(smc.account) + << std::endl; + std::cerr << "block::tlb::Account.validate_ref() = " << block::tlb::t_Account.validate_ref(smc.account) + << std::endl; + } + PDO(block::gen::t_Account.validate_ref(smc.account)); + THRERR("Account of created smart contract is invalid (?)"); + if (library.not_null()) { + vm::Dictionary dict{std::move(library), 256}; + ok &= dict.check_for_each([addr](Ref cs, td::ConstBitPtr key, int n) -> bool { + return !cs->prefetch_ulong(1) || add_public_library(key, addr, cs->prefetch_ref()); + }); + THRERR("Error processing libraries published by new smart contract"); + } + return smc_addr; +} + +// stores accounts:ShardAccounts +bool store_accounts(vm::CellBuilder& cb) { + vm::AugmentedDictionary dict{256, block::tlb::aug_ShardAccounts}; + for (const auto& smc_pair : smart_contracts) { + const SmcDescr& smc = smc_pair.second; + CHECK(smc_pair.first == smc.addr); + vm::CellBuilder cb; + bool ok = cb.store_ref_bool(smc.account) // account_descr$_ acc:^Account + && cb.store_zeroes_bool(256 + 64) // last_trans_hash:bits256 last_trans_lt:uint64 + && dict.set_builder(smc.addr.cbits(), 256, cb, vm::Dictionary::SetMode::Add); + CHECK(ok); + } + return std::move(dict).append_dict_to_bool(cb); +} + +// stores libraries:(HashmapE 256 LibDescr) +bool store_public_libraries(vm::CellBuilder& cb) { + vm::Dictionary dict{256}; + bool ok = true; + vm::CellBuilder empty_cb; + for (const auto& lib_pair : public_libraries) { + const PublicLibDescr pl = lib_pair.second; + PDO(pl.root->get_hash().as_array() == lib_pair.first.as_array()); + vm::Dictionary publishers{256}; + for (const auto& publisher : pl.publishers) { + PDO(publishers.set_builder(publisher.cbits(), 256, empty_cb, vm::Dictionary::SetMode::Add)); + } + Ref root = std::move(publishers).extract_root_cell(); + PDO(root.not_null()); + THRERR("public library has an empty or invalid set of publishers"); + vm::CellBuilder value_cb; // LibDescr + PDO(value_cb.store_long_bool(0, 2) && value_cb.store_ref_bool(pl.root) && + value_cb.append_cellslice_bool(vm::load_cell_slice(std::move(root)))); + THRERR("cannot create LibDescr for a public library"); + PDO(dict.set_builder(lib_pair.first.cbits(), 256, value_cb, vm::Dictionary::SetMode::Add)); + THRERR("cannot insert LibDescr of a public library into the public library collection"); + } + PDO(std::move(dict).append_dict_to_bool(cb)); + return ok; +} + +// stores config:ConfigParams +bool store_config_params(vm::CellBuilder& cb) { + return config_addr_set && config_param_root.not_null() && + cb.store_bits_bool(config_addr.cbits(), 256) // _ config_addr:bits256 + && cb.store_ref_bool(config_param_root); // config:^(Hashmap 32 ^Cell) +} + +// stores hash of initial masterchain validator set computed from configuration parameter 34 +bool store_validator_list_hash(vm::CellBuilder& cb) { + Ref vset_cell = config_dict.lookup_ref(td::BitArray<32>{34}); + auto res = block::Config::unpack_validator_set(std::move(vset_cell)); + if (res.is_error()) { + LOG(ERROR) << "cannot unpack current validator set: " << res.move_as_error().to_string(); + return false; + } + auto vset = res.move_as_ok(); + LOG_CHECK(vset) << "unpacked validator set is empty"; + auto ccvc = block::Config::unpack_catchain_validators_config(config_dict.lookup_ref(td::BitArray<32>{28})); + ton::ShardIdFull shard{ton::masterchainId}; + auto nodes = block::Config::do_compute_validator_set(ccvc, shard, *vset, now, 0); + LOG_CHECK(!nodes.empty()) << "validator node list in unpacked validator set is empty"; + auto vset_hash = block::compute_validator_set_hash(0, shard, std::move(nodes)); + LOG(DEBUG) << "initial validator set hash is " << vset_hash; + return cb.store_long_bool(vset_hash, 32); +} + +// stores custom:(Maybe ^McStateExtra) +bool store_custom(vm::CellBuilder& cb) { + if (workchain_id != wc_master) { + return cb.store_long_bool(0, 1); // nothing + } + vm::CellBuilder cb2, cb3; + bool ok = true; + PDO(cb2.store_long_bool(0xcc26, 16) // masterchain_state_extra#cc26 + && cb2.store_long_bool(0, 1) // shard_hashes:ShardHashes = (HashmapE 32 ^(BinTree ShardDescr)) + && store_config_params(cb2) // config:ConfigParams + && cb3.store_long_bool(0, 16) // ^[ flags:(## 16) { flags = 0 } + && store_validator_list_hash(cb3) // validator_list_hash_short:uint32 + && cb3.store_long_bool(0, 32) // catchain_seqno:uint32 + && cb3.store_bool_bool(true) // nx_cc_updated:Bool + && cb3.store_zeroes_bool(1 + 65) // prev_blocks:OldMcBlocksInfo + && cb3.store_long_bool(2, 1 + 1) // after_key_block:Bool last_key_block:(Maybe ...) + && cb2.store_ref_bool(cb3.finalize()) // ] + && block::CurrencyCollection{total_smc_balance}.store(cb2) // global_balance:CurrencyCollection + && cb.store_long_bool(1, 1) // just + && cb.store_ref_bool(cb2.finalize())); + return ok; +} + +Ref create_state() { + vm::CellBuilder cb, cb2; + now = static_cast(time(0)); + bool ok = true; + PDO(workchain_id != wc_undef); + THRERR("workchain_id is unset, cannot generate state"); + PDO(workchain_id != wc_master || config_addr_set); + THRERR("configuration smart contract must be selected"); + PDO(cb.store_long_bool(0x9023afe2, 32) // shard_state#9023afe2 + && cb.store_long_bool(global_id, 32)); // global_id:int32 + PDO(cb.store_long_bool(0, 8) && cb.store_long_bool(workchain_id, 32) && + cb.store_long_bool(0, 64) // shard_id:ShardIdent + && cb.store_long_bool(0, 32) // seq_no:# + && cb.store_zeroes_bool(32) // vert_seq_no:# + && cb.store_long_bool(now, 32) // gen_utime:uint32 + && cb.store_zeroes_bool(64) // gen_lt:uint64 + && cb.store_ones_bool(32) // min_ref_mc_seqno:uint32 + && cb2.store_zeroes_bool(1 + 64 + 2) // OutMsgQueueInfo + && cb.store_ref_bool(cb2.finalize()) // out_msg_queue_info:^OutMsgQueueInfo + && cb.store_long_bool(0, 1) // before_split:Bool + && store_accounts(cb2) // accounts:^ShardAccounts + && cb.store_ref_bool(cb2.finalize()) // ... + && cb2.store_zeroes_bool(128) // ^[ overload_history:uint64 underload_history:uint64 + && block::CurrencyCollection{total_smc_balance}.store(cb2) // total_balance:CurrencyCollection + && block::tlb::t_CurrencyCollection.null_value(cb2) // total_validator_fees:CurrencyCollection + && store_public_libraries(cb2) // libraries:(Hashmap 256 LibDescr) + && cb2.store_long_bool(0, 1) // master_ref:(Maybe BlkMasterInfo) + && cb.store_ref_bool(cb2.finalize()) // ] + && store_custom(cb)); // custom:(Maybe ^McStateExtra) + THRERR("cannot create blockchain state"); + Ref cell = cb.finalize(); + if (verbosity > 2) { + std::cerr << "shard_state is:\n"; + vm::load_cell_slice(cell).print_rec(std::cerr); + std::cerr << "pretty-printed shard_state is:\n"; + block::gen::t_ShardState.print_ref(std::cerr, cell); + std::cerr << "\n"; + std::cerr << "block::gen::ShardState.validate_ref() = " << block::gen::t_ShardState.validate_ref(cell) << std::endl; + std::cerr << "block::tlb::ShardState.validate_ref() = " << block::tlb::t_ShardState.validate_ref(cell) << std::endl; + block::gen::ShardStateUnsplit::Record data; + bool ok1 = tlb::unpack_cell(cell, data); + std::cerr << "block::gen::ShardState.unpack_cell() = " << ok1 << std::endl; + if (ok1) { + std::cerr << "shard_id = " << data.shard_id + << "; out_msg_queue_info = " << load_cell_slice(data.out_msg_queue_info) + << "; total_balance = " << data.r1.total_balance << std::endl; + } + } + PDO(block::gen::t_ShardState.validate_ref(cell)); + PDO(block::tlb::t_ShardState.validate_ref(cell)); + THRERR("created an invalid ShardState record"); + return cell; +} + +// code (cell) +// data (cell) +// library (cell) +// balance (int) +// split_depth (int 0..32) +// special (int 0..3, +2 = tick, +1 = tock) +// [ address (uint256) ] +// mode (0 = compute address only, 1 = create uninit, 2 = create complete; +4 = with specified address) +// --> 256-bit address +void interpret_register_smartcontract(vm::Stack& stack) { + if (workchain_id == wc_undef) { + throw fift::IntError{"cannot register a smartcontract unless the workchain is specified first"}; + } + td::RefInt256 spec_addr; + int mode = stack.pop_smallint_range(2 + 4); // allowed modes: 0 1 2 4 5 6 + if (mode == 3) { + throw fift::IntError{"invalid mode"}; + } + if (mode & 4) { + spec_addr = stack.pop_int_finite(); + mode &= ~4; + } + int special = stack.pop_smallint_range(3); + if (special && workchain_id != wc_master) { + throw fift::IntError{"cannot create special smartcontracts outside of the masterchain"}; + } + int split_depth = stack.pop_smallint_range(32); + td::RefInt256 balance = stack.pop_int_finite(); + if (sgn(balance) < 0) { + throw fift::IntError{"initial balance of a smartcontract cannot be negative"}; + } + if (sgn(balance) > 0 && !mode) { + throw fift::IntError{"cannot set non-zero balance if an account is not created"}; + } + Ref library = stack.pop_cell(); + Ref data = stack.pop_cell(); + Ref code = stack.pop_cell(); + td::RefInt256 addr = create_smartcontract(std::move(spec_addr), std::move(code), std::move(data), std::move(library), + std::move(balance), special, split_depth, mode); + if (addr.is_null()) { + throw fift::IntError{"internal error while creating smartcontract"}; + } + stack.push(std::move(addr)); +} + +void interpret_create_state(vm::Stack& stack) { + if (!global_id) { + throw fift::IntError{ + "(global) blockchain id must be set to a non-zero value: negative for test chains, positive for production"}; + } + Ref state = create_state(); + if (state.is_null()) { + throw fift::IntError{"could not create blockchain state"}; + } + stack.push(std::move(state)); +} + +void interpret_get_config_dict(vm::Stack& stack) { + Ref value = config_dict.get_root_cell(); + if (value.is_null()) { + stack.push_bool(false); + } else { + stack.push_cell(std::move(value)); + stack.push_bool(true); + } +} + +void interpret_get_config_param(vm::Stack& stack) { + int x = stack.pop_smallint_range(0x7fffffff, 0x80000000); + Ref value = config_dict.lookup_ref(td::BitArray<32>{x}); + if (value.is_null()) { + stack.push_bool(false); + } else { + stack.push_cell(std::move(value)); + stack.push_bool(true); + } +} + +void interpret_set_config_param(vm::Stack& stack) { + int x = stack.pop_smallint_range(0x7fffffff, 0x80000000); + Ref value = stack.pop_cell(); + if (verbosity > 2 && x >= 0) { + std::cerr << "setting configuration parameter #" << x << " to "; + // vm::load_cell_slice(value).print_rec(std::cerr); + block::gen::ConfigParam{x}.print_ref(std::cerr, value); + std::cerr << std::endl; + } + if (x >= 0 && !block::gen::ConfigParam{x}.validate_ref(value)) { + throw fift::IntError{"invalid value for indicated configuration parameter"}; + } + if (!config_dict.set_ref(td::BitArray<32>{x}, std::move(value))) { + throw fift::IntError{"cannot set value of configuration parameter (value too long?)"}; + } +} + +void interpret_is_shard_state(vm::Stack& stack) { + Ref cell = stack.pop_cell(); + if (verbosity > 4) { + std::cerr << "custom shard state is:\n"; + vm::load_cell_slice(cell).print_rec(std::cerr); + std::cerr << "pretty-printed custom shard state is:\n"; + block::gen::t_ShardState.print_ref(std::cerr, cell); + } + stack.push_bool(block::gen::t_ShardState.validate_ref(std::move(cell))); +} + +void interpret_is_workchain_descr(vm::Stack& stack) { + Ref cell = stack.pop_cell(); + if (verbosity > 4) { + std::cerr << "WorkchainDescr is:\n"; + vm::load_cell_slice(cell).print_rec(std::cerr); + std::cerr << "pretty-printed WorkchainDescr is:\n"; + block::gen::t_WorkchainDescr.print_ref(std::cerr, cell); + } + stack.push_bool(block::gen::t_WorkchainDescr.validate_ref(std::move(cell))); +} + +void init_words_custom(fift::Dictionary& d) { + d.def_stack_word("verb@ ", interpret_get_verbosity); + d.def_stack_word("verb! ", interpret_set_verbosity); + d.def_stack_word("wcid@ ", interpret_get_workchain); + d.def_stack_word("wcid! ", interpret_set_workchain); + d.def_stack_word("globalid@ ", interpret_get_global_id); + d.def_stack_word("globalid! ", interpret_set_global_id); + d.def_stack_word("config@ ", interpret_get_config_param); + d.def_stack_word("config! ", interpret_set_config_param); + d.def_stack_word("(configdict) ", interpret_get_config_dict); + d.def_stack_word("register_smc ", interpret_register_smartcontract); + d.def_stack_word("set_config_smc ", interpret_set_config_smartcontract); + d.def_stack_word("create_state ", interpret_create_state); + d.def_stack_word("isShardState? ", interpret_is_shard_state); + d.def_stack_word("isWorkchainDescr? ", interpret_is_workchain_descr); +} + +void usage(const char* progname) { + std::cerr + << "Creates initial state for a TON blockchain, using configuration defined by Fift-language source files\n"; + std::cerr + << "usage: " << progname + << " [-i] [-n] [-I ] {-L } ...\n"; + std::cerr << "\t-n\tDo not preload preamble files `Fift.fif` and `CreateState.fif`\n" + "\t-i\tForce interactive mode even if explicit source file names are indicated\n" + "\t-I\tSets colon-separated library source include path. If not indicated, " + "$FIFTPATH is used instead.\n" + "\t-L\tPre-loads a library source file\n" + "\t-v\tSet verbosity level\n"; + std::exit(2); +} + +void parse_include_path_set(std::string include_path_set, std::vector& res) { + td::Parser parser(include_path_set); + while (!parser.empty()) { + auto path = parser.read_till_nofail(':'); + if (!path.empty()) { + res.push_back(path.str()); + } + parser.skip_nofail(':'); + } +} + +void preload_preamble(fift::Fift& fift, std::string filename, bool standard = true) { + auto status = fift.interpret_file(filename, ""); + if (status.is_error()) { + LOG(ERROR) << "Error interpreting " << (standard ? "standard" : "application-specific") << " preamble file `" + << filename << "`: " << status.error().message() + << "\nCheck that correct include path is set by -I or by FIFTPATH environment variable, or disable " + "standard preamble by -n.\n"; + std::exit(2); + } +} + +int main(int argc, char* const argv[]) { + td::set_default_failure_signal_handler().ensure(); + bool interactive = false; + bool fift_preload = true, no_env = false, script_mode = false; + std::vector library_source_files, source_list; + std::vector source_include_path; + std::string ton_db_path; + + fift::Fift::Config config; + + int i; + int new_verbosity_level = VERBOSITY_NAME(INFO); + while (!script_mode && (i = getopt(argc, argv, "hinsI:L:v:")) != -1) { + switch (i) { + case 'i': + interactive = true; + break; + case 'n': + fift_preload = false; + break; + case 'I': + LOG(ERROR) << source_include_path; + parse_include_path_set(optarg, source_include_path); + no_env = true; + break; + case 's': + script_mode = true; + break; + case 'L': + library_source_files.emplace_back(optarg); + break; + case 'v': + new_verbosity_level = VERBOSITY_NAME(FATAL) + (verbosity = td::to_integer(td::Slice(optarg))); + break; + case 'h': + default: + usage(argv[0]); + } + } + SET_VERBOSITY_LEVEL(new_verbosity_level); + + while (optind < argc) { + source_list.emplace_back(argv[optind++]); + if (script_mode) { + break; + } + } + + if (!no_env) { + const char* path = std::getenv("FIFTPATH"); + if (path) { + parse_include_path_set(path ? path : "/usr/lib/fift", source_include_path); + } + } + std::string current_dir; + auto r_current_dir = td::realpath("."); + if (r_current_dir.is_ok()) { + current_dir = r_current_dir.move_as_ok(); + source_include_path.push_back(current_dir); + } + config.source_lookup = fift::SourceLookup(std::make_unique()); + for (auto& path : source_include_path) { + config.source_lookup.add_include_path(path); + } + + fift::init_words_common(config.dictionary); + fift::init_words_vm(config.dictionary); + fift::init_words_ton(config.dictionary); + init_words_custom(config.dictionary); + + if (script_mode) { + fift::import_cmdline_args(config.dictionary, source_list.empty() ? "" : source_list[0], argc - optind, + argv + optind); + } + + fift::Fift fift(std::move(config)); + + if (fift_preload) { + preload_preamble(fift, "Fift.fif", true); + preload_preamble(fift, "CreateState.fif", false); + } + + for (auto source : library_source_files) { + auto status = fift.interpret_file(source, ""); + if (status.is_error()) { + std::cerr << "Error interpreting preloaded file `" << source << "`: " << status.error().to_string() << std::endl; + std::exit(2); + } + } + + if (source_list.empty() && !interactive) { + std::cerr << "No Fift source files specified" << std::endl; + std::exit(2); + } + + for (const auto& source : source_list) { + auto status = fift.interpret_file(source, current_dir); + if (status.is_error()) { + std::cerr << "Error interpreting file `" << source << "`: " << status.error().to_string() << std::endl; + std::exit(2); + } + } + + if (interactive) { + fift.interpret_istream(std::cin, current_dir).ensure(); + } + // show_total_cells(); +} diff --git a/crypto/block/mc-config.cpp b/crypto/block/mc-config.cpp new file mode 100644 index 00000000..254b10e3 --- /dev/null +++ b/crypto/block/mc-config.cpp @@ -0,0 +1,1817 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + In addition, as a special exception, the copyright holders give permission + to link the code of portions of this program with the OpenSSL library. + You must obey the GNU General Public License in all respects for all + of the code used other than OpenSSL. If you modify file(s) with this + exception, you may extend this exception to your version of the file(s), + but you are not obligated to do so. If you do not wish to do so, delete this + exception statement from your version. If you delete this exception statement + from all source files in the program, then also delete it here. + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "mc-config.h" +#include "block/block.h" +#include "block/block-parse.h" +#include "block/block-auto.h" +#include "common/bitstring.h" +#include "vm/dict.h" +#include "td/utils/bits.h" +#include "td/utils/uint128.h" +#include "ton/ton-types.h" +#include "ton/ton-shard.h" +#include "crypto/openssl/digest.h" +#include +#include + +namespace block { +using td::Ref; + +Config::Config(Ref config_root, const td::Bits256& config_addr, int _mode) + : mode(_mode), config_addr(config_addr), config_root(std::move(config_root)) { +} + +td::Result> Config::unpack_config(Ref config_root, const td::Bits256& config_addr, + int mode) { + std::unique_ptr ptr{new Config(std::move(config_root), config_addr, mode)}; + TRY_STATUS(ptr->unpack_wrapped()); + return std::move(ptr); +} + +td::Result> Config::unpack_config(Ref config_csr, int mode) { + std::unique_ptr ptr{new Config(mode)}; + TRY_STATUS(ptr->unpack_wrapped(std::move(config_csr))); + return std::move(ptr); +} + +td::Result> Config::extract_from_key_block(Ref key_block_root, int mode) { + block::gen::Block::Record blk; + block::gen::BlockExtra::Record extra; + block::gen::McBlockExtra::Record mc_extra; + if (!(tlb::unpack_cell(key_block_root, blk) && tlb::unpack_cell(std::move(blk.extra), extra) && + tlb::unpack_cell(extra.custom->prefetch_ref(), mc_extra) && mc_extra.key_block && mc_extra.config.not_null())) { + return td::Status::Error(-400, "cannot unpack extra header of key block to extract configuration"); + } + return block::Config::unpack_config(std::move(mc_extra.config)); +} + +td::Result> Config::extract_from_state(Ref mc_state_root, int mode) { + gen::ShardStateUnsplit::Record state; + gen::McStateExtra::Record extra; + if (!(tlb::unpack_cell(mc_state_root, state) && state.global_id && + tlb::unpack_cell(state.custom->prefetch_ref(), extra))) { + return td::Status::Error("cannot extract configuration from masterchain state extra information"); + } + return unpack_config(std::move(extra.config), mode); +} + +td::Result> ConfigInfo::extract_config(std::shared_ptr static_boc, + int mode) { + TRY_RESULT(rc, static_boc->get_root_count()); + if (rc != 1) { + return td::Status::Error(-668, "Masterchain state BoC is invalid"); + } + TRY_RESULT(root, static_boc->get_root_cell(0)); + return extract_config(std::move(root), mode); +} + +td::Result> ConfigInfo::extract_config(Ref mc_state_root, int mode) { + if (mc_state_root.is_null()) { + return td::Status::Error("configuration state root cell is null"); + } + auto config = std::unique_ptr{new ConfigInfo(std::move(mc_state_root), mode)}; + TRY_STATUS(config->unpack_wrapped()); + return std::move(config); +} + +ConfigInfo::ConfigInfo(Ref mc_state_root, int _mode) : Config(_mode), state_root(std::move(mc_state_root)) { + block_id.root_hash.set_zero(); + block_id.file_hash.set_zero(); +} + +td::Status ConfigInfo::unpack_wrapped() { + try { + return unpack(); + } catch (vm::VmError& err) { + return td::Status::Error(PSLICE() << "error unpacking block state header and configuration: " << err.get_msg()); + } catch (vm::VmVirtError& err) { + return td::Status::Error(PSLICE() << "virtualization error while unpacking block state header and configuration: " + << err.get_msg()); + } +} + +td::Status ConfigInfo::unpack() { + gen::ShardStateUnsplit::Record root_info; + if (!tlb::unpack_cell(state_root, root_info) || !root_info.global_id) { + return td::Status::Error("configuration state root cannot be deserialized"); + } + global_id_ = root_info.global_id; + block::ShardId shard_id{root_info.shard_id}; + block_id.id = ton::BlockId{ton::ShardIdFull(shard_id), (unsigned)root_info.seq_no}; + block_id.root_hash.set_zero(); + block_id.file_hash.set_zero(); + vert_seqno = root_info.vert_seq_no; + utime = root_info.gen_utime; + lt = root_info.gen_lt; + min_ref_mc_seqno_ = root_info.min_ref_mc_seqno; + if (!root_info.custom->size_refs()) { + return td::Status::Error("state does not have a `custom` field with masterchain configuration"); + } + if (mode & needLibraries) { + lib_root_ = root_info.r1.libraries->prefetch_ref(); + libraries_dict_ = std::make_unique(lib_root_, 256); + } + if (mode & needAccountsRoot) { + accounts_root = vm::load_cell_slice_ref(root_info.accounts); + LOG(DEBUG) << "requested accounts dictionary"; + accounts_dict = std::make_unique(accounts_root, 256, block::tlb::aug_ShardAccounts); + LOG(DEBUG) << "accounts dictionary created"; + } + state_extra_root_ = root_info.custom->prefetch_ref(); + if (!is_masterchain()) { + if (mode & (needShardHashes | needValidatorSet | needSpecialSmc | needPrevBlocks | needWorkchainInfo)) { + return td::Status::Error("cannot extract masterchain-specific configuration data from a non-masterchain state"); + } + cleanup(); + return td::Status::OK(); + } + gen::McStateExtra::Record extra_info; + if (!tlb::unpack_cell(state_extra_root_, extra_info)) { + vm::load_cell_slice(state_extra_root_).print_rec(std::cerr); + block::gen::t_McStateExtra.print_ref(std::cerr, state_extra_root_); + return td::Status::Error("state extra information is invalid"); + } + gen::ValidatorInfo::Record validator_info; + if (!tlb::csr_unpack(extra_info.r1.validator_info, validator_info)) { + return td::Status::Error("validator_info in state extra information is invalid"); + } + cc_seqno_ = validator_info.catchain_seqno; + nx_cc_updated = validator_info.nx_cc_updated; + if ((mode & needShardHashes) && !ShardConfig::unpack(extra_info.shard_hashes)) { + return td::Status::Error("cannot unpack Shard configuration"); + } + is_key_state_ = extra_info.r1.after_key_block; + if (extra_info.r1.last_key_block->size() > 1) { + auto& cs = extra_info.r1.last_key_block.write(); + block::gen::ExtBlkRef::Record ext_ref; + if (!(cs.advance(1) && tlb::unpack_exact(cs, ext_ref))) { + return td::Status::Error("cannot unpack last_key_block from masterchain state"); + } + last_key_block_.id = ton::BlockId{ton::masterchainId, ton::shardIdAll, ext_ref.seq_no}; + last_key_block_.root_hash = ext_ref.root_hash; + last_key_block_.file_hash = ext_ref.file_hash; + last_key_block_lt_ = ext_ref.end_lt; + } else { + last_key_block_.invalidate(); + last_key_block_.id.seqno = 0; + last_key_block_lt_ = 0; + } + // unpack configuration + TRY_STATUS(Config::unpack_wrapped(std::move(extra_info.config))); + // unpack previous masterchain block collection + std::unique_ptr prev_blocks_dict = + std::make_unique(extra_info.r1.prev_blocks, 32, block::tlb::aug_OldMcBlocksInfo); + if (block_id.id.seqno) { + block::gen::ExtBlkRef::Record extref = {}; + auto ref = prev_blocks_dict->lookup(td::BitArray<32>::zero()); + if (!(ref.not_null() && ref.write().advance(1) && tlb::csr_unpack(ref, extref) && !extref.seq_no)) { + return td::Status::Error("OldMcBlocks in masterchain state does not contain a valid zero state reference"); + } + zerostate_id_.root_hash = extref.root_hash; + zerostate_id_.file_hash = extref.file_hash; + } else { + zerostate_id_.root_hash.set_zero(); + zerostate_id_.file_hash.set_zero(); + } + zerostate_id_.workchain = ton::masterchainId; + if (mode & needPrevBlocks) { + prev_blocks_dict_ = std::move(prev_blocks_dict); + } + // ... + cleanup(); + return td::Status::OK(); +} + +td::Status Config::unpack_wrapped(Ref config_csr) { + try { + return unpack(std::move(config_csr)); + } catch (vm::VmError err) { + return td::Status::Error(PSLICE() << "error unpacking masterchain configuration: " << err.get_msg()); + } +} + +td::Status Config::unpack_wrapped() { + try { + return unpack(); + } catch (vm::VmError err) { + return td::Status::Error(PSLICE() << "error unpacking masterchain configuration: " << err.get_msg()); + } +} + +td::Status Config::unpack(Ref config_cs) { + gen::ConfigParams::Record config_params; + if (!tlb::csr_unpack(std::move(config_cs), config_params)) { + return td::Status::Error("cannot unpack ConfigParams"); + } + config_addr = config_params.config_addr; + config_root = std::move(config_params.config); + return unpack(); +} + +td::Status Config::unpack() { + if (config_root.is_null()) { + return td::Status::Error("configuration root not set"); + } + config_dict = std::make_unique(config_root, 32); + if (mode & needValidatorSet) { + auto vset_res = unpack_validator_set(get_config_param(35, 34)); + if (vset_res.is_error()) { + return vset_res.move_as_error(); + } + cur_validators_ = vset_res.move_as_ok(); + } + if (mode & needSpecialSmc) { + LOG(DEBUG) << "needSpecialSmc flag set"; + auto param = get_config_param(31); + if (param.is_null()) { + special_smc_dict = std::make_unique(256); + } else { + special_smc_dict = std::make_unique(vm::load_cell_slice_ref(std::move(param)), 256); + LOG(DEBUG) << "smc dictionary created"; + } + } + if (mode & needWorkchainInfo) { + TRY_RESULT(pair, unpack_workchain_list_ext(get_config_param(12))); + workchains_ = std::move(pair.first); + workchains_dict_ = std::move(pair.second); + } + // ... + return td::Status::OK(); +} + +td::Status Config::visit_validator_params() const { + { + // current validator set + TRY_RESULT(vset, unpack_validator_set(get_config_param(34))); + } + for (int i = 32; i < 38; i++) { + // prev/current/next persistent and temporary validator sets + auto vs = get_config_param(i); + if (vs.not_null()) { + TRY_RESULT(vset, unpack_validator_set(std::move(vs))); + } + } + get_catchain_validators_config(); + return td::Status::OK(); +} + +ton::ValidatorSessionConfig Config::get_consensus_config() const { + auto cc = get_config_param(29); + ton::ValidatorSessionConfig c; + block::gen::ConsensusConfig::Record r; + if (cc.not_null() && tlb::unpack_cell(cc, r)) { + c.catchain_idle_timeout = r.consensus_timeout_ms * 0.001; + c.catchain_max_deps = r.catchain_max_deps; + c.round_candidates = r.round_candidates; + c.next_candidate_delay = r.next_candidate_delay_ms * 0.001; + c.round_attempt_duration = r.attempt_duration; + c.max_round_attempts = r.fast_attempts; + c.max_block_size = r.max_block_bytes; + c.max_collated_data_size = r.max_collated_bytes; + } + return c; +} + +bool Config::foreach_config_param(std::function)> scan_func) const { + if (!config_dict) { + return false; + } + return config_dict->check_for_each([scan_func](Ref cs_ref, td::ConstBitPtr key, int n) { + return n == 32 && cs_ref.not_null() && cs_ref->size_ext() == 0x10000 && + scan_func((int)key.get_int(n), cs_ref->prefetch_ref()); + }); +} + +std::unique_ptr ShardConfig::extract_shard_hashes_dict(Ref mc_state_root) { + gen::ShardStateUnsplit::Record root_info; + gen::McStateExtra::Record extra_info; + if (mc_state_root.not_null() // + && tlb::unpack_cell(mc_state_root, root_info) // + && tlb::unpack_cell(root_info.custom->prefetch_ref(), extra_info)) { + return std::make_unique(std::move(extra_info.shard_hashes), 32); + } else { + return {}; + } +} + +std::unique_ptr ConfigInfo::create_accounts_dict() const { + if (mode & needAccountsRoot) { + return std::make_unique(accounts_root, 256, block::tlb::aug_ShardAccounts); + } else { + return nullptr; + } +} + +const vm::AugmentedDictionary& ConfigInfo::get_accounts_dict() const { + return *accounts_dict; +} + +bool ConfigInfo::get_last_key_block(ton::BlockIdExt& blkid, ton::LogicalTime& blklt, bool strict) const { + if (strict || !is_key_state_) { + blkid = last_key_block_; + blklt = last_key_block_lt_; + } else { + blkid = block_id; + blklt = lt; + } + return blkid.is_valid() && blkid.seqno(); +} + +td::Result>> Config::unpack_workchain_list_ext( + Ref root) { + if (root.is_null()) { + LOG(DEBUG) << "workchain description dictionary is empty (no configuration parameter #12)"; + return std::make_pair(WorkchainSet{}, std::make_unique(32)); + } else { + auto wc_dict = std::make_unique(vm::load_cell_slice_ref(std::move(root)), 32); + WorkchainSet wc_list; + LOG(DEBUG) << "workchain description dictionary created"; + if (!(wc_dict->check_for_each([&wc_list](Ref cs_ref, td::ConstBitPtr key, int n) -> bool { + ton::WorkchainId wc = ton::WorkchainId(key.get_int(32)); + Ref wc_info{true}; + return wc_info.unique_write().unpack(wc, cs_ref.write()) && wc_list.emplace(wc, std::move(wc_info)).second; + }))) { + return td::Status::Error("cannot unpack WorkchainDescr from masterchain configuration"); + } + return std::make_pair(std::move(wc_list), std::move(wc_dict)); + } +} + +td::Result Config::unpack_workchain_list(Ref root) { + TRY_RESULT(pair, unpack_workchain_list_ext(std::move(root))); + return std::move(pair.first); +} + +td::Result> Config::unpack_validator_set(Ref vset_root) { + if (vset_root.is_null()) { + return td::Status::Error("validator set is absent"); + } + gen::ValidatorSet::Record rec; + if (!tlb::unpack_cell(std::move(vset_root), rec)) { + return td::Status::Error("validator set is invalid"); + } + vm::Dictionary dict{vm::Dictionary::construct_root_from(*rec.list), 16}; + td::BitArray<16> key_buffer; + auto last = dict.get_minmax_key(key_buffer.bits(), 16, true); + if (last.is_null() || (int)key_buffer.to_ulong() != rec.total - 1) { + return td::Status::Error( + "maximal index in a validator set dictionary must be one less than the total number of validators"); + } + auto ptr = std::make_unique(rec.utime_since, rec.utime_until, rec.total, rec.main); + for (int i = 0; i < rec.total; i++) { + key_buffer.store_ulong(i); + auto descr_cs = dict.lookup(key_buffer.bits(), 16); + if (descr_cs.is_null()) { + return td::Status::Error("indices in a validator set dictionary must be integers 0..total-1"); + } + gen::ValidatorDescr::Record_validator_addr descr; + if (!tlb::csr_unpack(descr_cs, descr)) { + descr.adnl_addr.set_zero(); + if (!(gen::t_ValidatorDescr.unpack_validator(descr_cs.write(), descr.public_key, descr.weight) && + descr_cs->empty_ext())) { + return td::Status::Error(PSLICE() << "validator #" << i + << " has an invalid ValidatorDescr record in the validator set dictionary"); + } + } + gen::SigPubKey::Record sig_pubkey; + if (!tlb::csr_unpack(std::move(descr.public_key), sig_pubkey)) { + return td::Status::Error(PSLICE() << "validator #" << i + << " has no public key or its public key is in unsupported format"); + } + if (!descr.weight) { + return td::Status::Error(PSLICE() << "validator #" << i << " has zero weight"); + } + if (descr.weight > ~(ptr->total_weight)) { + return td::Status::Error("total weight of all validators in validator set exceeds 2^64"); + } + ptr->list.emplace_back(sig_pubkey.pubkey, descr.weight, ptr->total_weight, descr.adnl_addr); + ptr->total_weight += descr.weight; + } + return std::move(ptr); +} + +bool Config::set_block_id_ext(const ton::BlockIdExt& block_id_ext) { + if (block_id.id == block_id_ext.id) { + block_id = block_id_ext; + return true; + } else { + return false; + } +} + +bool ConfigInfo::set_block_id_ext(const ton::BlockIdExt& block_id_ext) { + if (!Config::set_block_id_ext(block_id_ext)) { + return false; + } + if (!block_id.seqno()) { + zerostate_id_.workchain = ton::masterchainId; + zerostate_id_.root_hash = block_id_ext.root_hash; + zerostate_id_.file_hash = block_id_ext.file_hash; + } + reset_mc_hash(); + return true; +} + +void ConfigInfo::cleanup() { + if (!(mode & needStateRoot)) { + state_root.clear(); + } + if (!(mode & needStateExtraRoot)) { + state_extra_root_.clear(); + } +} + +Ref Config::get_config_param(int idx) const { + if (!config_dict) { + return {}; + } + return config_dict->lookup_ref(td::BitArray<32>{idx}); +} + +Ref Config::get_config_param(int idx, int idx2) const { + if (!config_dict) { + return {}; + } + auto res = config_dict->lookup_ref(td::BitArray<32>{idx}); + if (res.not_null()) { + return res; + } else { + return config_dict->lookup_ref(td::BitArray<32>{idx2}); + } +} + +td::Result> Config::get_block_limits(bool is_masterchain) const { + int param = (is_masterchain ? 22 : 23); + auto cell = get_config_param(param); + if (cell.is_null()) { + return td::Status::Error(PSTRING() << "configuration parameter " << param << " with block limits is absent"); + } + auto cs = vm::load_cell_slice(std::move(cell)); + auto ptr = std::make_unique(); + if (!ptr->deserialize(cs) || cs.size_ext()) { + return td::Status::Error(PSTRING() << "cannot deserialize BlockLimits obtained from configuration parameter " + << param); + } + return std::move(ptr); +} + +td::Result> Config::get_storage_prices() const { + auto cell = get_config_param(18); + std::vector res; + if (cell.is_null()) { + return td::Status::Error("configuration parameter 18 with storage prices dictionary is absent"); + } + vm::Dictionary dict{std::move(cell), 32}; + if (!dict.check_for_each([&res](Ref cs_ref, td::ConstBitPtr key, int n) -> bool { + block::gen::StoragePrices::Record data; + if (!tlb::csr_unpack(std::move(cs_ref), data) || data.utime_since != key.get_uint(n)) { + return false; + } + res.emplace_back(data.utime_since, data.bit_price_ps, data.cell_price_ps, data.mc_bit_price_ps, + data.mc_cell_price_ps); + return true; + })) { + return td::Status::Error("invalid storage prices dictionary in configuration parameter 18"); + } + return std::move(res); +} + +CatchainValidatorsConfig Config::unpack_catchain_validators_config(Ref cell) { + block::gen::CatchainConfig::Record cfg; + if (cell.is_null() || !tlb::unpack_cell(std::move(cell), cfg)) { + return {default_mc_catchain_lifetime, default_shard_catchain_lifetime, default_shard_validators_lifetime, + default_shard_validators_num}; + } else { + return {cfg.mc_catchain_lifetime, cfg.shard_catchain_lifetime, cfg.shard_validators_lifetime, + cfg.shard_validators_num}; + } +} + +CatchainValidatorsConfig Config::get_catchain_validators_config() const { + return unpack_catchain_validators_config(get_config_param(28)); +} + +// compares all fields except fsm*, before_merge_, nx_cc_updated_, next_catchain_seqno_ +bool McShardHash::basic_info_equal(const McShardHash& other, bool compare_fees, bool compare_reg_seqno) const { + return blk_ == other.blk_ && start_lt_ == other.start_lt_ && end_lt_ == other.end_lt_ && + (!compare_reg_seqno || reg_mc_seqno_ == other.reg_mc_seqno_) && gen_utime_ == other.gen_utime_ && + min_ref_mc_seqno_ == other.min_ref_mc_seqno_ && before_split_ == other.before_split_ && + want_split_ == other.want_split_ && want_merge_ == other.want_merge_ && + (!compare_fees || (fees_collected_ == other.fees_collected_ && funds_created_ == other.funds_created_)); +} + +void McShardHash::set_fsm(FsmState fsm, ton::UnixTime fsm_utime, ton::UnixTime fsm_interval) { + fsm_ = fsm; + fsm_utime_ = fsm_utime; + fsm_interval_ = fsm_interval; +} + +Ref McShardHash::unpack(vm::CellSlice& cs, ton::ShardIdFull id) { + gen::ShardDescr::Record descr; + CurrencyCollection fees_collected, funds_created; + if (!(tlb::unpack_exact(cs, descr) && fees_collected.unpack(descr.fees_collected) && + funds_created.unpack(descr.funds_created))) { + return {}; // throw ? + } + auto res = Ref(true, ton::BlockId{id, (unsigned)descr.seq_no}, descr.start_lt, descr.end_lt, + descr.gen_utime, descr.root_hash, descr.file_hash, fees_collected, funds_created, + descr.reg_mc_seqno, descr.min_ref_mc_seqno, descr.next_catchain_seqno, + descr.next_validator_shard, /* descr.nx_cc_updated */ false, descr.before_split, + descr.before_merge, descr.want_split, descr.want_merge); + McShardHash& sh = res.unique_write(); + switch (gen::t_FutureSplitMerge.get_tag(*(descr.split_merge_at))) { + case gen::FutureSplitMerge::fsm_none: + return res; + case gen::FutureSplitMerge::fsm_split: + if (gen::t_FutureSplitMerge.unpack_fsm_split(descr.split_merge_at.write(), sh.fsm_utime_, sh.fsm_interval_)) { + sh.fsm_ = FsmState::fsm_split; + return res; + } + break; + case gen::FutureSplitMerge::fsm_merge: + if (gen::t_FutureSplitMerge.unpack_fsm_merge(descr.split_merge_at.write(), sh.fsm_utime_, sh.fsm_interval_)) { + sh.fsm_ = FsmState::fsm_merge; + return res; + } + break; + default: + break; + } + return {}; +} + +bool McShardHash::pack(vm::CellBuilder& cb) const { + if (!(is_valid() // (validate) + && cb.store_long_bool(11, 4) // shard_descr#b + && cb.store_long_bool(blk_.id.seqno, 32) // seq_no:uint32 + && cb.store_long_bool(reg_mc_seqno_, 32) // reg_mc_seqno:uint32 + && cb.store_long_bool(start_lt_, 64) // start_lt:uint64 + && cb.store_long_bool(end_lt_, 64) // end_lt:uint64 + && cb.store_bits_bool(blk_.root_hash) // root_hash:bits256 + && cb.store_bits_bool(blk_.file_hash) // file_hash:bits256 + && cb.store_bool_bool(before_split_) // before_split:Bool + && cb.store_bool_bool(before_merge_) // before_merge:Bool + && cb.store_bool_bool(want_split_) // want_split:Bool + && cb.store_bool_bool(want_merge_) // want_merge:Bool + && cb.store_bool_bool(false) // nx_cc_updated:Bool + && cb.store_long_bool(0, 3) // flags:(## 3) { flags = 0 } + && cb.store_long_bool(next_catchain_seqno_, 32) // next_catchain_seqno:uint32 + && cb.store_long_bool(next_validator_shard_, 64) // next_validator_shard:uint64 + && cb.store_long_bool(min_ref_mc_seqno_, 32) // min_ref_mc_seqno:uint32 + && cb.store_long_bool(gen_utime_, 32) // gen_utime:uint32 + )) { + return false; + } + bool ok; + switch (fsm_) { // split_merge_at:FutureSplitMerge + case FsmState::fsm_none: + ok = gen::t_FutureSplitMerge.pack_fsm_none(cb); + break; + case FsmState::fsm_split: + ok = gen::t_FutureSplitMerge.pack_fsm_split(cb, fsm_utime_, fsm_interval_); + break; + case FsmState::fsm_merge: + ok = gen::t_FutureSplitMerge.pack_fsm_merge(cb, fsm_utime_, fsm_interval_); + break; + default: + return false; + } + return ok // split_merge_at:FutureSplitMerge + && fees_collected_.store_or_zero(cb) // fees_collected:CurrencyCollection + && funds_created_.store_or_zero(cb); // funds_created:CurrencyCollection = ShardDescr; +} + +Ref McShardHash::from_block(Ref block_root, const ton::FileHash& fhash, bool init_fees) { + if (block_root.is_null()) { + return {}; + } + block::gen::Block::Record rec; + block::gen::BlockInfo::Record info; + block::ShardId shard; + if (!(tlb::unpack_cell(block_root, rec) && tlb::unpack_cell(rec.info, info) && + shard.deserialize(info.shard.write()))) { + return {}; + } + ton::RootHash rhash = block_root->get_hash().bits(); + CurrencyCollection fees_collected, funds_created; + if (init_fees) { + block::gen::ValueFlow::Record flow; + if (!(tlb::unpack_cell(rec.value_flow, flow) && fees_collected.unpack(flow.fees_collected) && + funds_created.unpack(flow.r2.created))) { + return {}; + } + } + return Ref(true, ton::BlockId{ton::ShardIdFull(shard), (unsigned)info.seq_no}, info.start_lt, + info.end_lt, info.gen_utime, rhash, fhash, fees_collected, funds_created, ~0U, + info.min_ref_mc_seqno, info.gen_catchain_seqno, shard.shard_pfx, false, info.before_split, + false, info.want_split, info.want_merge); +} + +McShardDescr::McShardDescr(const McShardDescr& other) + : McShardHash(other) + , block_root(other.block_root) + , state_root(other.state_root) + , processed_upto(other.processed_upto) { + set_queue_root(other.outmsg_root); +} + +McShardDescr& McShardDescr::operator=(const McShardDescr& other) { + McShardHash::operator=(other); + block_root = other.block_root; + outmsg_root = other.outmsg_root; + processed_upto = other.processed_upto; + set_queue_root(other.outmsg_root); + return *this; +} + +Ref McShardDescr::from_block(Ref block_root, Ref state_root, + const ton::FileHash& fhash, bool init_fees) { + if (block_root.is_null()) { + return {}; + } + block::gen::Block::Record rec; + block::gen::BlockInfo::Record info; + block::ShardId shard; + if (!(tlb::unpack_cell(block_root, rec) && tlb::unpack_cell(rec.info, info) && + shard.deserialize(info.shard.write()))) { + return {}; + } + // TODO: use a suitable vm::MerkleUpdate method here + vm::CellSlice cs(vm::NoVm(), rec.state_update); + if (cs.special_type() != vm::Cell::SpecialType::MerkleUpdate) { + LOG(ERROR) << "state update in a block is not a Merkle update"; + return {}; + } + if (cs.size_refs() != 2 || cs.prefetch_ref(1)->get_hash(0) != state_root->get_hash()) { + LOG(ERROR) << "invalid Merkle update for block state : resulting state hash mismatch"; + return {}; + } + ton::RootHash rhash = block_root->get_hash().bits(); + CurrencyCollection fees_collected, funds_created; + if (init_fees) { + block::gen::ValueFlow::Record flow; + if (!(tlb::unpack_cell(rec.value_flow, flow) && fees_collected.unpack(flow.fees_collected) && + funds_created.unpack(flow.r2.created))) { + return {}; + } + } + auto res = Ref(true, ton::BlockId{ton::ShardIdFull(shard), (unsigned)info.seq_no}, info.start_lt, + info.end_lt, info.gen_utime, rhash, fhash, fees_collected, funds_created, ~0U, + info.min_ref_mc_seqno, info.gen_catchain_seqno, shard.shard_pfx, false, + info.before_split, false, info.want_split, info.want_merge); + auto& descr = res.unique_write(); + descr.block_root = std::move(block_root); + descr.state_root = std::move(state_root); + return res; +} + +Ref McShardDescr::from_state(ton::BlockIdExt blkid, Ref state_root) { + if (state_root.is_null()) { + return {}; + } + block::gen::ShardStateUnsplit::Record info; + block::gen::OutMsgQueueInfo::Record qinfo; + block::ShardId shard; + if (!(tlb::unpack_cell(state_root, info) && shard.deserialize(info.shard_id.write()) && + tlb::unpack_cell(info.out_msg_queue_info, qinfo))) { + LOG(DEBUG) << "cannot create McShardDescr from a shardchain state"; + return {}; + } + if (ton::ShardIdFull(shard) != ton::ShardIdFull(blkid) || info.seq_no != blkid.seqno()) { + LOG(DEBUG) << "shard id mismatch, cannot construct McShardDescr"; + return {}; + } + auto res = Ref(true, blkid.id, info.gen_lt, info.gen_lt, info.gen_utime, blkid.root_hash, + blkid.file_hash, CurrencyCollection{}, CurrencyCollection{}, ~0U, info.min_ref_mc_seqno, + 0, shard.shard_pfx, false, info.before_split); + res.unique_write().state_root = state_root; + res.unique_write().set_queue_root(qinfo.out_queue->prefetch_ref(0)); + return res; +} + +bool McShardDescr::set_queue_root(Ref queue_root) { + outmsg_root = std::move(queue_root); + out_msg_queue = std::make_unique(outmsg_root, 352, block::tlb::aug_OutMsgQueue); + return true; +} + +void McShardDescr::disable() { + block_root.clear(); + state_root.clear(); + outmsg_root.clear(); + out_msg_queue.reset(); + processed_upto.reset(); + McShardHash::disable(); +} + +void ConfigInfo::reset_mc_hash() { + if (block_id.is_masterchain() && !block_id.root_hash.is_zero()) { + // TODO: use block_start_lt instead of lt if available + set_mc_hash(Ref(true, block_id.id, lt, lt, utime, block_id.root_hash, block_id.file_hash)); + } else { + set_mc_hash(Ref{}); + } +} + +Ref ShardConfig::get_root_csr() const { + if (!shard_hashes_dict_) { + return {}; + } + return shard_hashes_dict_->get_root(); +} + +bool ShardConfig::unpack(Ref shard_hashes, Ref mc_shard_hash) { + shard_hashes_ = std::move(shard_hashes); + mc_shard_hash_ = std::move(mc_shard_hash); + return init(); +} + +bool ShardConfig::unpack(Ref shard_hashes, Ref mc_shard_hash) { + shard_hashes_ = shard_hashes->prefetch_ref(); + mc_shard_hash_ = std::move(mc_shard_hash); + return init(); +} + +bool ShardConfig::init() { + shard_hashes_dict_ = std::make_unique(shard_hashes_, 32); + valid_ = true; + return true; +} + +ShardConfig::ShardConfig(const ShardConfig& other) + : shard_hashes_(other.shard_hashes_), mc_shard_hash_(other.mc_shard_hash_) { + init(); +} + +bool ShardConfig::get_shard_hash_raw_from(vm::Dictionary& dict, vm::CellSlice& cs, ton::ShardIdFull id, + ton::ShardIdFull& true_id, bool exact, Ref* leaf) { + if (id.is_masterchain() || !id.is_valid()) { + return false; + } + auto root = dict.lookup_ref(td::BitArray<32>{id.workchain}); + if (root.is_null()) { + return false; + } + unsigned long long z = id.shard, m = std::numeric_limits::max(); + int len = id.pfx_len(); + while (true) { + cs.load(vm::NoVmOrd{}, leaf ? root : std::move(root)); + int t = (int)cs.fetch_ulong(1); + if (t < 0) { + return false; // throw DictError ? + } else if (!t) { + if (len && exact) { + return false; + } + true_id = ton::ShardIdFull{id.workchain, (id.shard | m) - (m >> 1)}; + if (leaf) { + *leaf = std::move(root); + } + return true; + } + if (!len || cs.size_ext() != 0x20000) { + return false; // throw DictError in the second case? + } + root = cs.prefetch_ref((unsigned)(z >> 63)); + z <<= 1; + --len; + m >>= 1; + } +} + +bool ShardConfig::get_shard_hash_raw(vm::CellSlice& cs, ton::ShardIdFull id, ton::ShardIdFull& true_id, + bool exact) const { + return shard_hashes_dict_ && get_shard_hash_raw_from(*shard_hashes_dict_, cs, id, true_id, exact); +} + +Ref ShardConfig::get_shard_hash(ton::ShardIdFull id, bool exact) const { + if (id.is_masterchain()) { + return (!exact || id.shard == ton::shardIdAll) ? get_mc_hash() : Ref{}; + } + ton::ShardIdFull true_id; + vm::CellSlice cs; + if (get_shard_hash_raw(cs, id, true_id, exact)) { + // block::gen::t_ShardDescr.print(std::cerr, vm::CellSlice{cs}); + return McShardHash::unpack(cs, true_id); + } else { + return {}; + } +} + +ton::CatchainSeqno ShardConfig::get_shard_cc_seqno(ton::ShardIdFull shard) const { + if (shard.is_masterchain() || !shard.is_valid()) { + return std::numeric_limits::max(); + } + ton::ShardIdFull true_id; + gen::ShardDescr::Record info; + vm::CellSlice cs; + if (!(get_shard_hash_raw(cs, shard - 1, true_id, false) && + (ton::shard_is_ancestor(true_id, shard) || ton::shard_is_parent(shard, true_id)) && + tlb::unpack_exact(cs, info))) { + return std::numeric_limits::max(); + } + ton::CatchainSeqno cc_seqno = info.next_catchain_seqno; + if (ton::shard_is_ancestor(true_id, shard)) { + return cc_seqno; + } + if (!(get_shard_hash_raw(cs, shard + 1, true_id, false) && ton::shard_is_parent(shard, true_id) && + tlb::unpack_exact(cs, info))) { + return std::numeric_limits::max(); + } + return std::max(cc_seqno, info.next_catchain_seqno) + 1; +} + +ton::LogicalTime ShardConfig::get_shard_end_lt_ext(ton::AccountIdPrefixFull acc, ton::ShardIdFull& actual_shard) const { + if (!acc.is_valid()) { + actual_shard.workchain = ton::workchainInvalid; + return 0; + } + if (acc.is_masterchain()) { + actual_shard = ton::ShardIdFull(ton::masterchainId); + CHECK(mc_shard_hash_.not_null()); + return mc_shard_hash_->end_lt_; + } + vm::CellSlice cs; + unsigned long long end_lt; + return get_shard_hash_raw(cs, acc.as_leaf_shard(), actual_shard, false) // lookup ShardDescr containing acc + && cs.advance(4 + 128) // shard_descr#b seq_no:uint32 reg_mc_seqno:uint32 start_lt:uint64 + && cs.fetch_ulong_bool(64, end_lt) // end_lt:uint64 + ? end_lt + : 0; +} + +ton::LogicalTime ShardConfig::get_shard_end_lt(ton::AccountIdPrefixFull acc) const { + ton::ShardIdFull tmp; + return get_shard_end_lt_ext(acc, tmp); +} + +bool ShardConfig::contains(ton::BlockIdExt blkid) const { + auto entry = get_shard_hash(blkid.shard_full()); + return entry.not_null() && entry->blk_ == blkid; +} + +static int process_workchain_shard_hashes(Ref& branch, ton::ShardIdFull shard, + std::function& func) { + auto cs = vm::load_cell_slice(branch); + int f = (int)cs.fetch_ulong(1); + if (f == 1) { + if ((shard.shard & 1) || cs.size_ext() != 0x20000) { + return false; + } + auto left = cs.prefetch_ref(0), right = cs.prefetch_ref(1); + int r = process_workchain_shard_hashes(left, ton::shard_child(shard, true), func); + if (r < 0) { + return r; + } + r |= process_workchain_shard_hashes(right, ton::shard_child(shard, false), func); + if (r <= 0) { + return r; + } + vm::CellBuilder cb; + return cb.store_bool_bool(true) && cb.store_ref_bool(std::move(left)) && cb.store_ref_bool(std::move(right)) && + cb.finalize_to(branch) + ? r + : -1; + } else if (!f) { + auto shard_info = McShardHash::unpack(cs, shard); + if (shard_info.is_null()) { + return -1; + } + int r = func(shard_info.write()); + if (r <= 0) { + return r; + } + vm::CellBuilder cb; + return cb.store_bool_bool(false) && shard_info->pack(cb) && cb.finalize_to(branch) ? r : -1; + } else { + return -1; + } +} + +bool ShardConfig::process_shard_hashes(std::function func) { + if (!shard_hashes_dict_) { + return false; + } + bool ok = true; + shard_hashes_dict_->map( + [&ok, &func](vm::CellBuilder& cb, Ref csr, td::ConstBitPtr key, int n) -> bool { + Ref root; + ok = ok && (n == 32) && csr->size_ext() == 0x10000 && std::move(csr)->prefetch_ref_to(root) && + process_workchain_shard_hashes(root, ton::ShardIdFull{(int)key.get_int(32)}, func) >= 0 && + cb.store_ref_bool(std::move(root)); + return true; + }); + return ok; +} + +static int process_workchain_sibling_shard_hashes(Ref& branch, Ref sibling, ton::ShardIdFull shard, + std::function& func) { + auto cs = vm::load_cell_slice(branch); + int f = (int)cs.fetch_ulong(1); + if (f == 1) { + if ((shard.shard & 1) || cs.size_ext() != 0x20000) { + return false; + } + auto left = cs.prefetch_ref(0), right = cs.prefetch_ref(1); + auto orig_left = left; + int r = process_workchain_sibling_shard_hashes(left, right, ton::shard_child(shard, true), func); + if (r < 0) { + return r; + } + r |= process_workchain_sibling_shard_hashes(right, std::move(orig_left), ton::shard_child(shard, false), func); + if (r <= 0) { + return r; + } + vm::CellBuilder cb; + return cb.store_bool_bool(true) && cb.store_ref_bool(std::move(left)) && cb.store_ref_bool(std::move(right)) && + cb.finalize_to(branch) + ? r + : -1; + } else if (!f) { + auto shard_info = McShardHash::unpack(cs, shard); + if (shard_info.is_null()) { + return -1; + } + Ref sibling_info; + if (sibling.not_null()) { + auto cs2 = vm::load_cell_slice(sibling); + if (!cs2.fetch_ulong(1)) { + sibling_info = McShardHash::unpack(cs2, ton::shard_sibling(shard)); + if (sibling_info.is_null()) { + return -1; + } + } + } + int r = func(shard_info.write(), sibling_info.get()); + if (r <= 0) { + return r; + } + vm::CellBuilder cb; + return cb.store_bool_bool(false) && shard_info->pack(cb) && cb.finalize_to(branch) ? r : -1; + } else { + return -1; + } +} + +bool ShardConfig::process_sibling_shard_hashes(std::function func) { + if (!shard_hashes_dict_) { + return false; + } + bool ok = true; + shard_hashes_dict_->map([&ok, &func](vm::CellBuilder& cb, Ref csr, td::ConstBitPtr key, + int n) -> bool { + Ref root; + ok = ok && (n == 32) && csr->size_ext() == 0x10000 && std::move(csr)->prefetch_ref_to(root) && + process_workchain_sibling_shard_hashes(root, Ref{}, ton::ShardIdFull{(int)key.get_int(32)}, func) >= + 0 && + cb.store_ref_bool(std::move(root)); + return true; + }); + return ok; +} + +std::vector ShardConfig::get_shard_hash_ids( + const std::function& filter) const { + if (!shard_hashes_dict_) { + return {}; + } + std::vector res; + bool mcout = mc_shard_hash_.is_null() || !mc_shard_hash_->seqno(); // include masterchain as a shard if seqno > 0 + bool ok = shard_hashes_dict_->check_for_each( + [&res, &mcout, mc_shard_hash_ = mc_shard_hash_, &filter](Ref cs_ref, td::ConstBitPtr key, + int n) -> bool { + int workchain = (int)key.get_int(n); + if (workchain >= 0 && !mcout) { + if (filter(ton::ShardIdFull{ton::masterchainId}, true)) { + res.emplace_back(mc_shard_hash_->blk_.id); + } + mcout = true; + } + if (!cs_ref->have_refs()) { + return false; + } + std::stack, unsigned long long>> stack; + stack.emplace(cs_ref->prefetch_ref(), ton::shardIdAll); + while (!stack.empty()) { + vm::CellSlice cs{vm::NoVm{}, std::move(stack.top().first)}; + unsigned long long shard = stack.top().second; + stack.pop(); + int t = (int)cs.fetch_ulong(1); + if (t < 0) { + return false; + } + if (!filter(ton::ShardIdFull{workchain, shard}, !t)) { + continue; + } + if (!t) { + if (!(cs.advance(4) && cs.have(32))) { + return false; + } + res.emplace_back(workchain, shard, (int)cs.prefetch_ulong(32)); + continue; + } + unsigned long long delta = (td::lower_bit64(shard) >> 1); + if (!delta || cs.size_ext() != 0x20000) { + return false; + } + stack.emplace(cs.prefetch_ref(1), shard + delta); + stack.emplace(cs.prefetch_ref(0), shard - delta); + } + return true; + }, + true); + if (!ok) { + return {}; + } + if (!mcout && filter(ton::ShardIdFull{ton::masterchainId}, true)) { + res.emplace_back(mc_shard_hash_->blk_.id); + } + return res; +} + +std::vector ShardConfig::get_shard_hash_ids(bool skip_mc) const { + return get_shard_hash_ids( + [skip_mc](ton::ShardIdFull shard, bool) -> bool { return !(skip_mc && shard.is_masterchain()); }); +} + +std::vector ShardConfig::get_intersecting_shard_hash_ids(ton::ShardIdFull myself) const { + return get_shard_hash_ids( + [myself](ton::ShardIdFull shard, bool) -> bool { return ton::shard_intersects(myself, shard); }); +} + +std::vector ShardConfig::get_neighbor_shard_hash_ids(ton::ShardIdFull myself) const { + return get_shard_hash_ids([myself](ton::ShardIdFull shard, bool) -> bool { return is_neighbor(myself, shard); }); +} + +std::vector ShardConfig::get_proper_neighbor_shard_hash_ids(ton::ShardIdFull myself) const { + return get_shard_hash_ids([myself](ton::ShardIdFull shard, bool leaf) -> bool { + return is_neighbor(myself, shard) && !(leaf && ton::shard_intersects(myself, shard)); + }); +} + +bool ShardConfig::is_neighbor(ton::ShardIdFull x, ton::ShardIdFull y) { + if (x.is_masterchain() || y.is_masterchain()) { + return true; + } + unsigned long long xs = x.shard, ys = y.shard; + unsigned long long xl = td::lower_bit64(xs), yl = td::lower_bit64(ys); + unsigned long long z = (xs ^ ys) & td::bits_negate64(std::max(xl, yl) << 1); + if (!z) { + return true; + } + if (x.workchain != y.workchain) { + return false; + } + int c1 = (td::count_leading_zeroes_non_zero64(z) >> 2); + int c2 = (td::count_trailing_zeroes_non_zero64(z) >> 2); + return c1 + c2 == 15; +} + +bool ShardConfig::has_workchain(ton::WorkchainId workchain) const { + return shard_hashes_dict_ && shard_hashes_dict_->key_exists(td::BitArray<32>{workchain}); +} + +std::vector ShardConfig::get_workchains() const { + if (!shard_hashes_dict_) { + return {}; + } + std::vector res; + if (!shard_hashes_dict_->check_for_each([&res](Ref val, td::ConstBitPtr key, int n) { + CHECK(n == 32); + ton::WorkchainId w = (int)key.get_int(32); + res.push_back(w); + return true; + })) { + return {}; + } + return res; +} + +bool ShardConfig::new_workchain(ton::WorkchainId workchain, ton::BlockSeqno reg_mc_seqno, + const ton::RootHash& zerostate_root_hash, const ton::FileHash& zerostate_file_hash) { + if (!shard_hashes_dict_ || has_workchain(workchain)) { + return false; + } + vm::CellBuilder cb; + Ref cell; + return cb.store_long_bool(11, 1 + 4) // bt_leaf$0 ; shard_descr#b + && cb.store_zeroes_bool(32) // seq_no:uint32 + && cb.store_long_bool(reg_mc_seqno, 32) // reg_mc_seqno:uint32 + && cb.store_zeroes_bool(64 * 2) // start_lt:uint64 end_lt:uint64 + && cb.store_bits_bool(zerostate_root_hash) // root_hash:bits256 + && cb.store_bits_bool(zerostate_file_hash) // file_hash:bits256 + && cb.store_long_bool(0, 8) // ... nx_cc_updated:Bool ... + && cb.store_long_bool(0, 32) // next_catchain_seqno:uint32 + && cb.store_long_bool(1ULL << 63, 64) // next_validator_shard:uint64 + && cb.store_long_bool(~0U, 32) // min_ref_mc_seqno:uint32 + && cb.store_long_bool(0, 32) // gen_utime:uint32 + && + cb.store_zeroes_bool( + 1 + 5 + + 5) // split_merge_at:FutureSplitMerge fees_collected:CurrencyCollection funds_created:CurrencyCollection + && cb.finalize_to(cell) && block::gen::t_BinTree_ShardDescr.validate_ref(cell) && + shard_hashes_dict_->set_ref(td::BitArray<32>{workchain}, std::move(cell), vm::Dictionary::SetMode::Add); +} + +td::Result ShardConfig::may_update_shard_block_info(Ref new_info, + const std::vector& old_blkids, + ton::LogicalTime lt_limit, Ref* ancestor) const { + if (!shard_hashes_dict_) { + return td::Status::Error(-666, "no shard top block dictionary present"); + } + if (new_info.is_null()) { + return td::Status::Error(-666, "suggested new top shard block info is empty"); + } + if (!new_info->is_valid()) { + return td::Status::Error(-666, "new top shard block description is invalid"); + } + auto wc = new_info->shard().workchain; + if (wc == ton::workchainInvalid || wc == ton::masterchainId) { + return td::Status::Error(-666, "new top shard block description belongs to an invalid workchain"); + } + if (!has_workchain(wc)) { + return td::Status::Error(-666, "new top shard block belongs to an unknown or disabled workchain"); + } + if (old_blkids.size() != 1 && old_blkids.size() != 2) { + return td::Status::Error(-666, "must have either one or two start blocks in a top shard block update"); + } + bool before_split = ton::shard_is_parent(old_blkids[0].shard_full(), new_info->shard()); + bool before_merge = (old_blkids.size() == 2); + if (before_merge) { + if (!ton::shard_is_sibling(old_blkids[0].shard_full(), old_blkids[1].shard_full())) { + return td::Status::Error(-666, "the two start blocks of a top shard block update must be siblings"); + } + if (!ton::shard_is_parent(new_info->shard(), old_blkids[0].shard_full())) { + return td::Status::Error( + -666, + std::string{"the two start blocks of a top shard block update do not merge into expected final shard "} + + old_blkids[0].shard_full().to_str()); + } + } else if (new_info->shard() != old_blkids[0].shard_full() && !before_split) { + return td::Status::Error( + -666, "the start block of a top shard block update must either coincide with the final shard or be its parent"); + } + if (ancestor) { + ancestor->clear(); + } + ton::CatchainSeqno old_cc_seqno = 0; + for (const auto& ob : old_blkids) { + auto odef = get_shard_hash(ob.shard_full()); + if (odef.is_null() || odef->blk_ != ob) { + return td::Status::Error(-666, + std::string{"the start block "} + ob.to_str() + + " of a top shard block update is not contained in the previous shard configuration"); + } + old_cc_seqno = std::max(old_cc_seqno, odef->next_catchain_seqno_); + if (shards_updated_.find(ob.shard_full()) != shards_updated_.end()) { + return td::Status::Error( + -666, std::string{"the shard of the start block "} + ob.to_str() + + " of a top shard block update has been already updated in the current shard configuration"); + } + if (odef->before_split_ != before_split) { + return td::Status::Error( + -666, PSTRING() << "the shard of the start block " << ob.to_str() + << " had before_split=" << odef->before_split_ + << " but the top shard block update is valid only if before_split=" << before_split); + } + if (odef->before_merge_ != before_merge) { + return td::Status::Error( + -666, PSTRING() << "the shard of the start block " << ob.to_str() + << " had before_merge=" << odef->before_merge_ + << " but the top shard block update is valid only if before_merge=" << before_merge); + } + if (new_info->before_split_) { + if (before_merge || before_split) { + return td::Status::Error( + -666, PSTRING() << "cannot register a before-split block " << new_info->top_block_id().to_str() + << " at the end of a chain that itself starts with a split/merge event"); + } + if (odef->fsm_state() != block::McShardHash::FsmState::fsm_split) { + return td::Status::Error(-666, PSTRING() << "cannot register a before-split block " + << new_info->top_block_id().to_str() + << " because fsm_split state was not set for this shard beforehand"); + } + if (new_info->gen_utime_ < odef->fsm_utime_ || new_info->gen_utime_ >= odef->fsm_utime_ + odef->fsm_interval_) { + return td::Status::Error(-666, PSTRING() << "cannot register a before-split block " + << new_info->top_block_id().to_str() + << " because fsm_split state was enabled for unixtime " + << odef->fsm_utime_ << " .. " << odef->fsm_utime_ + odef->fsm_interval_ + << " but the block is generated at " << new_info->gen_utime_); + } + } + if (before_merge) { + if (odef->fsm_state() != block::McShardHash::FsmState::fsm_merge) { + return td::Status::Error(-666, PSTRING() << "cannot register merged block " << new_info->top_block_id().to_str() + << " because fsm_merge state was not set for shard " + << odef->top_block_id().shard_full().to_str() << " beforehand"); + } + if (new_info->gen_utime_ < odef->fsm_utime_ || new_info->gen_utime_ >= odef->fsm_utime_ + odef->fsm_interval_) { + return td::Status::Error(-666, PSTRING() << "cannot register merged block " << new_info->top_block_id().to_str() + << " because fsm_merge state was enabled for shard " + << odef->top_block_id().shard_full().to_str() << " for unixtime " + << odef->fsm_utime_ << " .. " << odef->fsm_utime_ + odef->fsm_interval_ + << " but the block is generated at " << new_info->gen_utime_); + } + } + if (ancestor && !before_merge && !before_split) { + *ancestor = odef; + } + } + if (old_cc_seqno + before_merge != new_info->next_catchain_seqno_) { + return td::Status::Error(-666, PSTRING() + << "the top shard block update is generated with catchain_seqno=" + << new_info->next_catchain_seqno_ << " but previous shard configuration expects " + << old_cc_seqno + before_merge); + } + if (new_info->end_lt_ >= lt_limit) { + return td::Status::Error(-666, PSTRING() << "the top shard block update has end_lt " << new_info->end_lt_ + << " which is larger than the current limit " << lt_limit); + } + return !before_split; +} + +td::Result ShardConfig::update_shard_block_info(Ref new_info, + const std::vector& old_blkids) { + Ref ancestor; + auto res = may_update_shard_block_info(new_info, old_blkids, ~0ULL, &ancestor); + if (res.is_error()) { + return res; + } + if (!res.move_as_ok()) { + return td::Status::Error(-666, std::string{"cannot apply the after-split update for "} + new_info->blk_.to_str() + + " without a corresponding sibling update"); + } + if (ancestor.not_null() && ancestor->fsm_ != McShardHash::FsmState::fsm_none) { + new_info.write().set_fsm(ancestor->fsm_, ancestor->fsm_utime_, ancestor->fsm_interval_); + } + auto blk = new_info->blk_; + bool ok = do_update_shard_info(std::move(new_info)); + if (!ok) { + return td::Status::Error( + -666, + std::string{ + "unknown serialization error for (BinTree ShardDescr) while updating shard configuration to include "} + + blk.to_str()); + } else { + return true; + } +} + +td::Result ShardConfig::update_shard_block_info2(Ref new_info1, Ref new_info2, + const std::vector& old_blkids) { + auto res1 = may_update_shard_block_info(new_info1, old_blkids); + if (res1.is_error()) { + return res1; + } + auto res2 = may_update_shard_block_info(new_info2, old_blkids); + if (res2.is_error()) { + return res2; + } + if (res1.move_as_ok() || res2.move_as_ok()) { + return td::Status::Error(-666, "the two updates in update_shard_block_info2 must follow a shard split event"); + } + if (new_info1->blk_.id.shard > new_info2->blk_.id.shard) { + std::swap(new_info1, new_info2); + } + auto blk1 = new_info1->blk_, blk2 = new_info2->blk_; + bool ok = do_update_shard_info2(std::move(new_info1), std::move(new_info2)); + if (!ok) { + return td::Status::Error( + -666, + std::string{ + "unknown serialization error for (BinTree ShardDescr) while updating shard configuration to include "} + + blk1.to_str() + " and " + blk2.to_str()); + } else { + return true; + } +} + +bool ShardConfig::do_update_shard_info(Ref new_info) { + vm::CellBuilder cb; + Ref ref; + return new_info.not_null() && cb.store_bool_bool(false) // bt_leaf$0 + && new_info->pack(cb) // leaf:ShardDescr + && cb.finalize_to(ref) && set_shard_info(new_info->shard(), std::move(ref)); +} + +bool ShardConfig::do_update_shard_info2(Ref new_info1, Ref new_info2) { + if (new_info1.is_null() || new_info2.is_null() || !ton::shard_is_sibling(new_info1->shard(), new_info2->shard())) { + return false; + } + if (new_info1->blk_.id.shard > new_info2->blk_.id.shard) { + std::swap(new_info1, new_info2); + } + vm::CellBuilder cb, cb1; + Ref ref; + return cb.store_bool_bool(true) // bt_node$1 + && cb1.store_bool_bool(false) // ( bt_leaf$0 + && new_info1->pack(cb1) // leaf:ShardDescr + && cb1.finalize_to(ref) // ) -> ref + && cb.store_ref_bool(std::move(ref)) // left:^(BinTree ShardDescr) + && cb1.store_bool_bool(false) // ( bt_leaf$0 + && new_info2->pack(cb1) // leaf:ShardDescr + && cb1.finalize_to(ref) // ) -> ref + && cb.store_ref_bool(std::move(ref)) // right:^(BinTree ShardDescr) + && cb.finalize_to(ref) && set_shard_info(ton::shard_parent(new_info1->shard()), std::move(ref)); +} + +static bool btree_set(Ref& root, ton::ShardId shard, Ref value) { + if (!shard) { + return false; + } + if (shard == ton::shardIdAll) { + root = value; + return true; + } + auto cs = vm::load_cell_slice(std::move(root)); + if (cs.size_ext() != 0x20001 || cs.prefetch_ulong(1) != 1) { + return false; // branch does not exist + } + Ref left = cs.prefetch_ref(0), right = cs.prefetch_ref(1); + if (!(btree_set(shard & (1ULL << 63) ? right : left, shard << 1, std::move(value)))) { + return false; + } + vm::CellBuilder cb; + return cb.store_bool_bool(true) // bt_node$1 + && cb.store_ref_bool(std::move(left)) // left:^(BinTree ShardDescr) + && cb.store_ref_bool(std::move(right)) // right:^(BinTree ShardDescr) + && cb.finalize_to(root); // = BinTree ShardDescr +} + +bool ShardConfig::set_shard_info(ton::ShardIdFull shard, Ref value) { + if (!gen::t_BinTree_ShardDescr.validate_ref(value)) { + LOG(ERROR) << "attempting to store an invalid (BinTree ShardDescr) at shard configuration position " + << shard.to_str(); + gen::t_BinTree_ShardDescr.print_ref(std::cerr, value); + vm::load_cell_slice(value).print_rec(std::cerr); + return false; + } + auto root = shard_hashes_dict_->lookup_ref(td::BitArray<32>{shard.workchain}); + if (root.is_null()) { + LOG(ERROR) << "attempting to store a new ShardDescr for shard " << shard.to_str() << " in an undefined workchain"; + return false; + } + if (!btree_set(root, shard.shard, value)) { + LOG(ERROR) << "error while storing a new ShardDescr for shard " << shard.to_str() << " into shard configuration"; + return false; + } + if (!shard_hashes_dict_->set_ref(td::BitArray<32>{shard.workchain}, std::move(root), + vm::Dictionary::SetMode::Replace)) { + return false; + } + auto ins = shards_updated_.insert(shard); + CHECK(ins.second); + return true; +} + +bool Config::is_special_smartcontract(const ton::StdSmcAddress& addr) const { + CHECK(special_smc_dict); + return special_smc_dict->lookup(addr).not_null() || addr == config_addr; +} + +td::Result> Config::get_special_smartcontracts(bool without_config) const { + if (!special_smc_dict) { + return td::Status::Error(-666, "configuration loaded without fundamental smart contract list"); + } + std::vector res; + if (!special_smc_dict->check_for_each([&res, &without_config, conf_addr = config_addr.bits()]( + Ref cs_ref, td::ConstBitPtr key, int n) { + if (cs_ref->size_ext() || n != 256) { + return false; + } + res.emplace_back(key); + if (!without_config && key.equals(conf_addr, 256)) { + without_config = true; + } + return true; + })) { + return td::Status::Error(-666, "invalid fundamental smart contract set in configuration parameter 31"); + } + if (!without_config) { + res.push_back(config_addr); + } + return std::move(res); +} + +td::Result>> ConfigInfo::get_special_ticktock_smartcontracts( + int tick_tock) const { + if (!special_smc_dict) { + return td::Status::Error(-666, "configuration loaded without fundamental smart contract list"); + } + if (!accounts_dict) { + return td::Status::Error(-666, "state loaded without accounts information"); + } + std::vector> res; + if (!special_smc_dict->check_for_each( + [this, &res, tick_tock](Ref cs_ref, td::ConstBitPtr key, int n) -> bool { + if (cs_ref->size_ext() || n != 256) { + return false; + } + int tt = get_smc_tick_tock(key); + if (tt < -1) { + return false; + } + if (tt >= 0 && (tt & tick_tock) != 0) { + res.emplace_back(key, tt); + } + return true; + })) { + return td::Status::Error(-666, + "invalid fundamental smart contract set in configuration parameter 31, or unable to " + "recover tick-tock value from one of them"); + } + return std::move(res); +} + +int ConfigInfo::get_smc_tick_tock(td::ConstBitPtr smc_addr) const { + if (!accounts_dict) { + return -2; + } + auto acc_csr = accounts_dict->lookup(smc_addr, 256); + Ref acc_cell; + if (acc_csr.is_null() || !acc_csr->prefetch_ref_to(acc_cell)) { + return -1; + } + auto acc_cs = vm::load_cell_slice(std::move(acc_cell)); + if (block::gen::t_Account.get_tag(acc_cs) == block::gen::Account::account_none) { + return 0; + } + block::gen::Account::Record_account acc; + block::gen::AccountStorage::Record storage; + int ticktock; + return (tlb::unpack_exact(acc_cs, acc) && tlb::csr_unpack(acc.storage, storage) && + block::tlb::t_AccountState.get_ticktock(storage.state.write(), ticktock)) + ? ticktock + : -2; +} + +ton::CatchainSeqno ConfigInfo::get_shard_cc_seqno(ton::ShardIdFull shard) const { + return shard.is_masterchain() ? cc_seqno_ : ShardConfig::get_shard_cc_seqno(shard); +} + +std::vector Config::compute_validator_set(ton::ShardIdFull shard, const block::ValidatorSet& vset, + ton::UnixTime time, ton::CatchainSeqno cc_seqno) const { + return do_compute_validator_set(get_catchain_validators_config(), shard, vset, time, cc_seqno); +} + +std::vector Config::compute_validator_set(ton::ShardIdFull shard, ton::UnixTime time, + ton::CatchainSeqno cc_seqno) const { + if (!cur_validators_) { + return {}; + } else { + return compute_validator_set(shard, *cur_validators_, time, cc_seqno); + } +} + +std::vector ConfigInfo::compute_validator_set_cc(ton::ShardIdFull shard, + const block::ValidatorSet& vset, + ton::UnixTime time, + ton::CatchainSeqno* cc_seqno_delta) const { + if (cc_seqno_delta && (*cc_seqno_delta & -2)) { + return {}; + } + ton::CatchainSeqno cc_seqno = get_shard_cc_seqno(shard); + if (cc_seqno == ~0U) { + return {}; + } + if (cc_seqno_delta) { + cc_seqno = *cc_seqno_delta += cc_seqno; + } + return do_compute_validator_set(get_catchain_validators_config(), shard, vset, time, cc_seqno); +} + +std::vector ConfigInfo::compute_validator_set_cc(ton::ShardIdFull shard, ton::UnixTime time, + ton::CatchainSeqno* cc_seqno_delta) const { + auto vset = get_cur_validator_set(); + if (!vset) { + return {}; + } else { + return compute_validator_set_cc(shard, *vset, time, cc_seqno_delta); + } +} + +void validator_set_descr::incr_seed() { + for (int i = 31; i >= 0 && !++(seed[i]); --i) { + } +} + +void validator_set_descr::hash_to(unsigned char hash_buffer[64]) const { + digest::hash_str(hash_buffer, (const void*)this, sizeof(*this)); +} + +td::uint64 ValidatorSetPRNG::next_ulong() { + if (pos < limit) { + return td::bswap64(hash_longs[pos++]); + } + data.hash_to(hash); + data.incr_seed(); + pos = 1; + limit = 8; + return td::bswap64(hash_longs[0]); +} + +td::uint64 ValidatorSetPRNG::next_ranged(td::uint64 range) { + td::uint64 y = next_ulong(); + return td::uint128(range).mult(y).hi(); +} + +inline bool operator<(td::uint64 pos, const ValidatorDescr& descr) { + return pos < descr.cum_weight; +} + +const ValidatorDescr& ValidatorSet::at_weight(td::uint64 weight_pos) const { + CHECK(weight_pos < total_weight); + auto it = std::upper_bound(list.begin(), list.end(), weight_pos); + CHECK(it != list.begin()); + return *--it; +} + +std::vector ValidatorSet::export_validator_set() const { + std::vector l; + l.reserve(list.size()); + for (const auto& node : list) { + l.emplace_back(node.pubkey, node.weight, node.adnl_addr); + } + return l; +} + +std::vector Config::do_compute_validator_set(const block::CatchainValidatorsConfig& ccv_conf, + ton::ShardIdFull shard, + const block::ValidatorSet& vset, ton::UnixTime time, + ton::CatchainSeqno cc_seqno) { + LOG(DEBUG) << "in Config::do_compute_validator_set() for " << shard.to_str() << " ; cc_seqno=" << cc_seqno; + std::vector nodes; + bool is_mc = shard.is_masterchain(); + unsigned count = std::min(is_mc ? vset.main : ccv_conf.shard_val_num, vset.total); + CHECK((unsigned)vset.total == vset.list.size()); + if (!count) { + return {}; // no validators? + } + nodes.reserve(count); + if (is_mc) { + // simply take needed number of validators from the head of the list + // TODO: write a more clever validator list selection algorithm + // (if we really need one for the masterchain) + for (unsigned i = 0; i < count; i++) { + const auto& v = vset.list[i]; + nodes.emplace_back(v.pubkey, v.weight, v.adnl_addr); + } + return nodes; + } + // this is the "true" algorithm for generating shardchain validator subgroups + ValidatorSetPRNG gen{shard, cc_seqno}; // use zero seed (might use a non-trivial seed from ccv_conf in the future) + std::vector> holes; + holes.reserve(count); + td::uint64 total_wt = vset.total_weight; + for (unsigned i = 0; i < count; i++) { + CHECK(total_wt > 0); + auto p = gen.next_ranged(total_wt); // generate a pseudo-random number 0 .. total_wt-1 + // auto op = p; + for (auto& hole : holes) { + if (p < hole.first) { + break; + } + p += hole.second; + } + auto& entry = vset.at_weight(p); + // LOG(DEBUG) << "vset entry #" << i << ": rem_wt=" << total_wt << ", total_wt=" << vset.total_weight << ", op=" << op << ", p=" << p << "; entry.cum_wt=" << entry.cum_weight << ", entry.wt=" << entry.weight << " " << entry.cum_weight / entry.weight; + nodes.emplace_back(entry.pubkey, 1, entry.adnl_addr); // NB: shardchain validator lists have all weights = 1 + CHECK(total_wt >= entry.weight); + total_wt -= entry.weight; + std::pair new_hole{entry.cum_weight, entry.weight}; + auto it = std::upper_bound(holes.begin(), holes.end(), new_hole); + CHECK(it == holes.begin() || *(it - 1) < new_hole); + holes.insert(it, new_hole); + } + return nodes; +} + +std::vector Config::compute_total_validator_set(int next) const { + auto res = unpack_validator_set(get_config_param(next < 0 ? 32 : (next ? 36 : 34))); + if (res.is_error()) { + return {}; + } + return res.move_as_ok()->export_validator_set(); +} + +bool WorkchainInfo::unpack(ton::WorkchainId wc, vm::CellSlice& cs) { + workchain = ton::workchainInvalid; + if (wc == ton::workchainInvalid) { + return false; + } + block::gen::WorkchainDescr::Record info; + if (!tlb::unpack(cs, info)) { + return false; + } + enabled_since = info.enabled_since; + actual_min_split = info.actual_min_split; + min_split = info.min_split; + max_split = info.max_split; + basic = info.basic; + active = info.active; + accept_msgs = info.accept_msgs; + flags = info.flags; + zerostate_root_hash = info.zerostate_root_hash; + zerostate_file_hash = info.zerostate_file_hash; + version = info.version; + if (basic) { + min_addr_len = max_addr_len = addr_len_step = 256; + } else { + block::gen::WorkchainFormat::Record_wfmt_ext ext; + if (!tlb::type_unpack(cs, block::gen::WorkchainFormat{basic}, ext)) { + return false; + } + min_addr_len = ext.min_addr_len; + max_addr_len = ext.max_addr_len; + addr_len_step = ext.addr_len_step; + } + workchain = wc; + LOG(DEBUG) << "unpacked info for workchain " << wc << ": basic=" << basic << ", active=" << active + << ", accept_msgs=" << accept_msgs << ", min_split=" << min_split << ", max_split=" << max_split; + return true; +} + +Ref Config::get_workchain_info(ton::WorkchainId workchain_id) const { + if (!workchains_dict_) { + return {}; + } + auto it = workchains_.find(workchain_id); + if (it == workchains_.end()) { + return {}; + } else { + return it->second; + } +} + +bool ConfigInfo::get_old_mc_block_id(ton::BlockSeqno seqno, ton::BlockIdExt& blkid, ton::LogicalTime* end_lt) const { + if (block_id.is_valid() && seqno == block_id.id.seqno) { + blkid = block_id; + if (end_lt) { + *end_lt = lt; + } + return true; + } else { + return block::get_old_mc_block_id(prev_blocks_dict_.get(), seqno, blkid, end_lt); + } +} + +bool ConfigInfo::check_old_mc_block_id(const ton::BlockIdExt& blkid, bool strict) const { + return (!strict && blkid.id.seqno == block_id.id.seqno && block_id.is_valid()) + ? blkid == block_id + : block::check_old_mc_block_id(prev_blocks_dict_.get(), blkid); +} + +// returns block with min block.seqno and req_lt <= block.end_lt +bool ConfigInfo::get_mc_block_by_lt(ton::LogicalTime req_lt, ton::BlockIdExt& blkid, ton::LogicalTime* end_lt) const { + if (req_lt > lt) { + return false; + } + td::BitArray<32> key; + auto found = prev_blocks_dict_->traverse_extra( + key.bits(), 32, + [req_lt](td::ConstBitPtr key_prefix, int key_pfx_len, Ref extra, Ref value) { + unsigned long long found_lt; + if (!(extra.write().advance(1) && extra.write().fetch_ulong_bool(64, found_lt))) { + return -1; + } + if (found_lt < req_lt) { + return 0; // all leaves in subtree have end_lt <= found_lt < req_lt, skip + } + return 6; // visit left subtree, then right subtree; for leaf: accept and return to the top + }); + if (found.first.not_null()) { + CHECK(unpack_old_mc_block_id(std::move(found.first), (unsigned)key.to_ulong(), blkid, end_lt)); + return true; + } + if (block_id.is_valid()) { + blkid = block_id; + if (end_lt) { + *end_lt = lt; + } + return true; + } else { + return false; + } +} + +// returns key block with max block.seqno and block.seqno <= req_seqno +bool ConfigInfo::get_prev_key_block(ton::BlockSeqno req_seqno, ton::BlockIdExt& blkid, ton::LogicalTime* end_lt) const { + if (block_id.is_valid() && is_key_state_ && block_id.seqno() <= req_seqno) { + blkid = block_id; + if (end_lt) { + *end_lt = lt; + } + return true; + } + td::BitArray<32> key; + auto found = + prev_blocks_dict_->traverse_extra(key.bits(), 32, + [req_seqno](td::ConstBitPtr key_prefix, int key_pfx_len, + Ref extra, Ref value) -> int { + if (extra->prefetch_ulong(1) != 1) { + return 0; // no key blocks in subtree, skip + } + unsigned x = (unsigned)key_prefix.get_uint(key_pfx_len); + unsigned d = 32 - key_pfx_len; + if (!d) { + return x <= req_seqno; + } + unsigned y = req_seqno >> (d - 1); + if (y < 2 * x) { + // (x << d) > req_seqno <=> x > (req_seqno >> d) = (y >> 1) <=> 2 * x > y + return 0; // all nodes in subtree have block.seqno > req_seqno => skip + } + return y == 2 * x ? 1 /* visit only left */ : 5 /* visit right, then left */; + }); + if (found.first.not_null()) { + CHECK(unpack_old_mc_block_id(std::move(found.first), (unsigned)key.to_ulong(), blkid, end_lt)); + CHECK(blkid.seqno() <= req_seqno); + return true; + } else { + blkid.invalidate(); + return false; + } +} + +// returns key block with min block.seqno and block.seqno >= req_seqno +bool ConfigInfo::get_next_key_block(ton::BlockSeqno req_seqno, ton::BlockIdExt& blkid, ton::LogicalTime* end_lt) const { + td::BitArray<32> key; + auto found = prev_blocks_dict_->traverse_extra( + key.bits(), 32, + [req_seqno](td::ConstBitPtr key_prefix, int key_pfx_len, Ref extra, + Ref value) -> int { + if (extra->prefetch_ulong(1) != 1) { + return 0; // no key blocks in subtree, skip + } + unsigned x = (unsigned)key_prefix.get_uint(key_pfx_len); + unsigned d = 32 - key_pfx_len; + if (!d) { + return x >= req_seqno; + } + unsigned y = req_seqno >> (d - 1); + if (y > 2 * x + 1) { + // ((x + 1) << d) <= req_seqno <=> (x+1) <= (req_seqno >> d) = (y >> 1) <=> 2*x+2 <= y <=> y > 2*x+1 + return 0; // all nodes in subtree have block.seqno < req_seqno => skip + } + return y == 2 * x + 1 ? 2 /* visit only right */ : 6 /* visit left, then right */; + }); + if (found.first.not_null()) { + CHECK(unpack_old_mc_block_id(std::move(found.first), (unsigned)key.to_ulong(), blkid, end_lt)); + CHECK(blkid.seqno() >= req_seqno); + return true; + } + if (block_id.is_valid() && is_key_state_ && block_id.seqno() >= req_seqno) { + blkid = block_id; + if (end_lt) { + *end_lt = lt; + } + return true; + } else { + blkid.invalidate(); + return false; + } +} + +Ref ConfigInfo::lookup_library(td::ConstBitPtr root_hash) const { + if (!libraries_dict_) { + return {}; + } + auto csr = libraries_dict_->lookup(root_hash, 256); + if (csr.is_null() || csr->prefetch_ulong(8) != 0 || !csr->have_refs()) { // shared_lib_descr$00 lib:^Cell + return {}; + } + auto lib = csr->prefetch_ref(); + if (lib->get_hash().bits().compare(root_hash, 256)) { + LOG(ERROR) << "public library hash mismatch: expected " << root_hash.to_hex(256) << " , found " + << lib->get_hash().bits().to_hex(256); + return {}; + } + return lib; +} + +} // namespace block diff --git a/crypto/block/mc-config.h b/crypto/block/mc-config.h new file mode 100644 index 00000000..4ed8e9d8 --- /dev/null +++ b/crypto/block/mc-config.h @@ -0,0 +1,617 @@ +/* + This file is part of TON Blockchain source code. + + TON Blockchain is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + TON Blockchain is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with TON Blockchain. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once +#include "common/refcnt.hpp" +#include "vm/db/StaticBagOfCellsDb.h" +#include "vm/dict.h" +#include "ton/ton-types.h" +#include "ton/ton-shard.h" +#include "common/bitstring.h" +#include "block.h" + +#include +#include +#include +#include +#include + +namespace block { +using td::Ref; + +struct ValidatorDescr { + ton::Ed25519_PublicKey pubkey; + td::Bits256 adnl_addr; + td::uint64 weight; + td::uint64 cum_weight; + ValidatorDescr(const td::Bits256& _pubkey, td::uint64 _weight, td::uint64 _cum_weight) + : pubkey(_pubkey), weight(_weight), cum_weight(_cum_weight) { + adnl_addr.set_zero(); + } + ValidatorDescr(const td::Bits256& _pubkey, td::uint64 _weight, td::uint64 _cum_weight, const td::Bits256& _adnl_addr) + : pubkey(_pubkey), adnl_addr(_adnl_addr), weight(_weight), cum_weight(_cum_weight) { + } + ValidatorDescr(const ton::Ed25519_PublicKey& _pubkey, td::uint64 _weight, td::uint64 _cum_weight) + : pubkey(_pubkey), weight(_weight), cum_weight(_cum_weight) { + adnl_addr.set_zero(); + } + bool operator<(td::uint64 wt_pos) const & { + return cum_weight < wt_pos; + } +}; + +struct ValidatorSet { + unsigned utime_since; + unsigned utime_until; + int total; + int main; + td::uint64 total_weight; + std::vector list; + ValidatorSet() = default; + ValidatorSet(unsigned _since, unsigned _until, int _total, int _main = 0) + : utime_since(_since), utime_until(_until), total(_total), main(_main > 0 ? _main : _total), total_weight(0) { + } + const ValidatorDescr& operator[](unsigned i) const { + return list[i]; + } + const ValidatorDescr& at_weight(td::uint64 weight_pos) const; + std::vector export_validator_set() const; +}; + +#pragma pack(push, 1) +// this structure is hashed with SHA512 to produce pseudo-random bit stream in do_compute_validator_set() +// NB: all integers (including 256-bit seed) are actually big-endian +struct validator_set_descr { + unsigned char seed[32]; // seed for validator set computation, set to zero if none + td::uint64 shard; + td::int32 workchain; + td::uint32 cc_seqno; + validator_set_descr() = default; + validator_set_descr(ton::ShardIdFull shard_id, ton::CatchainSeqno cc_seqno_, bool flag) + : shard(td::bswap64(shard_id.shard)) + , workchain(td::bswap32(shard_id.workchain)) + , cc_seqno(td::bswap32(cc_seqno_)) { + } + validator_set_descr(ton::ShardIdFull shard_id, ton::CatchainSeqno cc_seqno_) + : validator_set_descr(shard_id, cc_seqno_, false) { + std::memset(seed, 0, 32); + } + validator_set_descr(ton::ShardIdFull shard_id, ton::CatchainSeqno cc_seqno_, const unsigned char seed_[32]) + : validator_set_descr(shard_id, cc_seqno_, false) { + std::memcpy(seed, seed_, 32); + } + validator_set_descr(ton::ShardIdFull shard_id, ton::CatchainSeqno cc_seqno_, td::ConstBitPtr seed_) + : validator_set_descr(shard_id, cc_seqno_, false) { + td::BitPtr{seed}.copy_from(seed_, 256); + } + validator_set_descr(ton::ShardIdFull shard_id, ton::CatchainSeqno cc_seqno_, const td::Bits256& seed_) + : validator_set_descr(shard_id, cc_seqno_, false) { + td::BitPtr{seed}.copy_from(seed_.cbits(), 256); + } + void incr_seed(); + void hash_to(unsigned char hash_buffer[64]) const; +}; +#pragma pack(pop) + +class ValidatorSetPRNG { + validator_set_descr data; + union { + unsigned char hash[64]; + td::uint64 hash_longs[8]; + }; + int pos{0}, limit{0}; + + public: + ValidatorSetPRNG() = default; + ValidatorSetPRNG(ton::ShardIdFull shard_id, ton::CatchainSeqno cc_seqno_) : data(shard_id, cc_seqno_) { + } + ValidatorSetPRNG(ton::ShardIdFull shard_id, ton::CatchainSeqno cc_seqno_, const unsigned char seed_[32]) + : data(shard_id, cc_seqno_, seed_) { + } + ValidatorSetPRNG(ton::ShardIdFull shard_id, ton::CatchainSeqno cc_seqno_, td::ConstBitPtr seed_) + : data(shard_id, cc_seqno_, std::move(seed_)) { + } + ValidatorSetPRNG(ton::ShardIdFull shard_id, ton::CatchainSeqno cc_seqno_, const td::Bits256& seed_) + : data(shard_id, cc_seqno_, seed_) { + } + td::uint64 next_ulong(); + td::uint64 next_ranged(td::uint64 range); // integer in 0 .. range-1 + ValidatorSetPRNG& operator>>(td::uint64& x) { + x = next_ulong(); + return *this; + } +}; + +class McShardHashI : public td::CntObject { + public: + enum class FsmState { fsm_none, fsm_split, fsm_merge }; + virtual ton::BlockIdExt top_block_id() const = 0; + virtual ton::LogicalTime start_lt() const = 0; + virtual ton::LogicalTime end_lt() const = 0; + virtual ton::UnixTime fsm_utime() const = 0; + virtual FsmState fsm_state() const = 0; + virtual ton::ShardIdFull shard() const = 0; + virtual bool before_split() const = 0; + virtual bool before_merge() const = 0; +}; + +struct McShardHash : public McShardHashI { + ton::BlockIdExt blk_; + ton::LogicalTime start_lt_, end_lt_; + ton::UnixTime gen_utime_{0}; + ton::UnixTime fsm_utime_{0}; + ton::UnixTime fsm_interval_{0}; + ton::BlockSeqno min_ref_mc_seqno_{std::numeric_limits::max()}; + ton::BlockSeqno reg_mc_seqno_{std::numeric_limits::max()}; + FsmState fsm_{FsmState::fsm_none}; + bool disabled_{false}; + bool before_split_{false}, before_merge_{false}, want_split_{false}, want_merge_{false}; + ton::CatchainSeqno next_catchain_seqno_{std::numeric_limits::max()}; + ton::ShardId next_validator_shard_{ton::shardIdAll}; + CurrencyCollection fees_collected_, funds_created_; + McShardHash(const ton::BlockId& id, ton::LogicalTime start_lt, ton::LogicalTime end_lt, ton::UnixTime gen_utime, + const ton::BlockHash& root_hash, const ton::FileHash& file_hash, CurrencyCollection fees_collected = {}, + CurrencyCollection funds_created = {}, + ton::BlockSeqno reg_mc_seqno = std::numeric_limits::max(), + ton::BlockSeqno min_ref_mc_seqno = std::numeric_limits::max(), + ton::CatchainSeqno cc_seqno = std::numeric_limits::max(), ton::ShardId val_shard = 0, + bool nx_cc_updated = false, bool before_split = false, bool before_merge = false, bool want_split = false, + bool want_merge = false) + : blk_(id, root_hash, file_hash) + , start_lt_(start_lt) + , end_lt_(end_lt) + , gen_utime_(gen_utime) + , min_ref_mc_seqno_(min_ref_mc_seqno) + , reg_mc_seqno_(reg_mc_seqno) + , before_split_(before_split) + , before_merge_(before_merge) + , want_split_(want_split) + , want_merge_(want_merge) + , next_catchain_seqno_(cc_seqno) + , next_validator_shard_(val_shard ? val_shard : id.shard) + , fees_collected_(fees_collected) + , funds_created_(funds_created) { + } + McShardHash(const ton::BlockIdExt& blk, ton::LogicalTime start_lt, ton::LogicalTime end_lt) + : blk_(blk), start_lt_(start_lt), end_lt_(end_lt) { + } + McShardHash(const McShardHash&) = default; + bool is_valid() const { + return blk_.is_valid(); + } + ton::BlockIdExt top_block_id() const override final { + return blk_; + } + // ZeroStateIdExt zero_state() const override; + ton::LogicalTime start_lt() const override final { + return start_lt_; + } + ton::LogicalTime end_lt() const override final { + return end_lt_; + } + ton::UnixTime fsm_utime() const override final { + return fsm_utime_; + } + ton::UnixTime fsm_utime_end() const { + return fsm_utime_ + fsm_interval_; + } + ton::UnixTime created_at() const { + return gen_utime_; + } + FsmState fsm_state() const override final { + return fsm_; + } + bool is_fsm_none() const { + return fsm_ == FsmState::fsm_none; + } + bool is_fsm_split() const { + return fsm_ == FsmState::fsm_split; + } + bool is_fsm_merge() const { + return fsm_ == FsmState::fsm_merge; + } + ton::ShardIdFull shard() const override final { + return ton::ShardIdFull(blk_); + } + ton::WorkchainId workchain() const { + return blk_.id.workchain; + } + bool contains(const ton::AccountIdPrefixFull& pfx) const { + return ton::shard_contains(shard(), pfx); + } + bool before_split() const override final { + return before_split_; + } + bool before_merge() const override final { + return before_merge_; + } + bool is_disabled() const { + return disabled_; + } + void disable() { + blk_.invalidate(); + disabled_ = true; + } + ton::BlockSeqno seqno() const { + return blk_.id.seqno; + } + bool set_reg_mc_seqno(ton::BlockSeqno reg_mc_seqno) { + reg_mc_seqno_ = reg_mc_seqno; + return true; + } + // compares all fields except fsm*, before_merge_, nx_cc_updated_, next_catchain_seqno_, fees_collected_ + bool basic_info_equal(const McShardHash& other, bool compare_fees = false, bool compare_reg_seqno = true) const; + void clear_fsm() { + fsm_ = FsmState::fsm_none; + } + void set_fsm(FsmState fsm, ton::UnixTime fsm_utime, ton::UnixTime fsm_interval); + void set_fsm_split(ton::UnixTime fsm_utime, ton::UnixTime fsm_interval) { + set_fsm(FsmState::fsm_split, fsm_utime, fsm_interval); + } + void set_fsm_merge(ton::UnixTime fsm_utime, ton::UnixTime fsm_interval) { + set_fsm(FsmState::fsm_merge, fsm_utime, fsm_interval); + } + bool fsm_equal(const McShardHash& other) const { + return fsm_ == other.fsm_ && + (is_fsm_none() || (fsm_utime_ == other.fsm_utime_ && fsm_interval_ == other.fsm_interval_)); + } + bool pack(vm::CellBuilder& cb) const; + static Ref unpack(vm::CellSlice& cs, ton::ShardIdFull id); + static Ref from_block(Ref block_root, const ton::FileHash& _fhash, bool init_fees = false); + McShardHash* make_copy() const override { + return new McShardHash(*this); + } +}; + +struct McShardDescr final : public McShardHash { + Ref block_root; + Ref state_root; + Ref outmsg_root; + std::unique_ptr out_msg_queue; + std::shared_ptr processed_upto; + McShardDescr(const ton::BlockId& id, ton::LogicalTime start_lt, ton::LogicalTime end_lt, ton::UnixTime gen_utime, + const ton::BlockHash& root_hash, const ton::FileHash& file_hash, CurrencyCollection fees_collected = {}, + CurrencyCollection funds_created = {}, + ton::BlockSeqno reg_mc_seqno = std::numeric_limits::max(), + ton::BlockSeqno min_ref_mc_seqno = std::numeric_limits::max(), + ton::CatchainSeqno cc_seqno = std::numeric_limits::max(), + ton::ShardId val_shard = ton::shardIdAll, bool nx_cc_updated = false, bool before_split = false, + bool before_merge = false, bool want_split = false, bool want_merge = false) + : McShardHash(id, start_lt, end_lt, gen_utime, root_hash, file_hash, fees_collected, funds_created, reg_mc_seqno, + min_ref_mc_seqno, cc_seqno, val_shard, nx_cc_updated, before_split, before_merge, want_split, + want_merge) { + } + McShardDescr(const ton::BlockIdExt& blk, ton::LogicalTime start_lt, ton::LogicalTime end_lt) + : McShardHash(blk, start_lt, end_lt) { + } + McShardDescr(const McShardHash& shard_hash) : McShardHash(shard_hash) { + } + McShardDescr(const McShardDescr& other); + McShardDescr(McShardDescr&& other) = default; + McShardDescr& operator=(const McShardDescr& other); + McShardDescr& operator=(McShardDescr&& other) = default; + bool set_queue_root(Ref queue_root); + void disable(); + static Ref from_block(Ref block_root, Ref state_root, const ton::FileHash& _fhash, + bool init_fees = false); + static Ref from_state(ton::BlockIdExt blkid, Ref state_root); +}; + +struct StoragePrices { + ton::UnixTime valid_since{0}; + td::uint64 bit_price{0}; + td::uint64 cell_price{0}; + td::uint64 mc_bit_price{0}; + td::uint64 mc_cell_price{0}; + StoragePrices() = default; + StoragePrices(ton::UnixTime _valid_since, td::uint64 _bprice, td::uint64 _cprice, td::uint64 _mc_bprice, + td::uint64 _mc_cprice) + : valid_since(_valid_since) + , bit_price(_bprice) + , cell_price(_cprice) + , mc_bit_price(_mc_bprice) + , mc_cell_price(_mc_cprice) { + } +}; + +struct CatchainValidatorsConfig { + td::uint32 mc_cc_lifetime, shard_cc_lifetime, shard_val_lifetime, shard_val_num; + CatchainValidatorsConfig(td::uint32 mc_cc_lt_, td::uint32 sh_cc_lt_, td::uint32 sh_val_lt_, td::uint32 sh_val_num_) + : mc_cc_lifetime(mc_cc_lt_) + , shard_cc_lifetime(sh_cc_lt_) + , shard_val_lifetime(sh_val_lt_) + , shard_val_num(sh_val_num_) { + } +}; + +struct WorkchainInfo : public td::CntObject { + ton::WorkchainId workchain{ton::workchainInvalid}; + ton::UnixTime enabled_since; + td::uint32 actual_min_split; + td::uint32 min_split, max_split; + bool basic; + bool active; + bool accept_msgs; + int flags; + td::uint32 version; + ton::RootHash zerostate_root_hash; + ton::FileHash zerostate_file_hash; + int min_addr_len, max_addr_len, addr_len_step; + bool is_valid() const { + return workchain != ton::workchainInvalid; + } + bool is_valid_addr_len(int addr_len) const { + return addr_len >= min_addr_len && addr_len <= max_addr_len && + (addr_len == min_addr_len || addr_len == max_addr_len || + (addr_len_step > 0 && !((addr_len - min_addr_len) % addr_len_step))); + } + bool unpack(ton::WorkchainId wc, vm::CellSlice& cs); +}; + +using WorkchainSet = std::map>; + +class ShardConfig { + Ref shard_hashes_; + Ref mc_shard_hash_; + std::unique_ptr shard_hashes_dict_; + std::set shards_updated_; + bool valid_{false}; + + public: + ShardConfig() = default; + ShardConfig(const ShardConfig& other); + ShardConfig(ShardConfig&& other) = default; + ShardConfig(Ref shard_hashes, Ref mc_shard_hash = {}) + : shard_hashes_(std::move(shard_hashes)), mc_shard_hash_(std::move(mc_shard_hash)) { + init(); + } + bool is_valid() const { + return valid_; + } + bool unpack(Ref shard_hashes, Ref mc_shard_hash = {}); + bool unpack(Ref shard_hashes, Ref mc_shard_hash = {}); + Ref get_root_csr() const; + bool has_workchain(ton::WorkchainId workchain) const; + std::vector get_workchains() const; + Ref get_shard_hash(ton::ShardIdFull id, bool exact = true) const; + bool contains(ton::BlockIdExt blkid) const; + bool get_shard_hash_raw(vm::CellSlice& cs, ton::ShardIdFull id, ton::ShardIdFull& true_id, bool exact = true) const; + ton::LogicalTime get_shard_end_lt(ton::AccountIdPrefixFull acc) const; + ton::LogicalTime get_shard_end_lt_ext(ton::AccountIdPrefixFull acc, ton::ShardIdFull& actual_shard) const; + static bool get_shard_hash_raw_from(vm::Dictionary& shard_hashes_dict, vm::CellSlice& cs, ton::ShardIdFull id, + ton::ShardIdFull& true_id, bool exact = true, Ref* leaf = nullptr); + std::vector get_shard_hash_ids(bool skip_mc = false) const; + std::vector get_shard_hash_ids(const std::function& filter) const; + std::vector get_intersecting_shard_hash_ids(ton::ShardIdFull myself) const; + std::vector get_neighbor_shard_hash_ids(ton::ShardIdFull myself) const; + std::vector get_proper_neighbor_shard_hash_ids(ton::ShardIdFull myself) const; + static std::unique_ptr extract_shard_hashes_dict(Ref mc_state_root); + bool process_shard_hashes(std::function func); + bool process_sibling_shard_hashes(std::function func); + // may become non-static const in the future + static bool is_neighbor(ton::ShardIdFull x, ton::ShardIdFull y); + Ref get_mc_hash() const { + return mc_shard_hash_; + } + void set_mc_hash(Ref mc_shard_hash) { + mc_shard_hash_ = std::move(mc_shard_hash); + } + ton::CatchainSeqno get_shard_cc_seqno(ton::ShardIdFull shard) const; + block::compute_shard_end_lt_func_t get_compute_shard_end_lt_func() const { + return std::bind(&ShardConfig::get_shard_end_lt, *this, std::placeholders::_1); + } + bool new_workchain(ton::WorkchainId workchain, ton::BlockSeqno reg_mc_seqno, const ton::RootHash& zerostate_root_hash, + const ton::FileHash& zerostate_file_hash); + td::Result update_shard_block_info(Ref new_info, const std::vector& old_blkids); + td::Result update_shard_block_info2(Ref new_info1, Ref new_info2, + const std::vector& old_blkids); + td::Result may_update_shard_block_info(Ref new_info, + const std::vector& old_blkids, + ton::LogicalTime lt_limit = std::numeric_limits::max(), + Ref* ancestor = nullptr) const; + + private: + bool init(); + bool do_update_shard_info(Ref new_info); + bool do_update_shard_info2(Ref new_info1, Ref new_info2); + bool set_shard_info(ton::ShardIdFull shard, Ref value); +}; + +class Config { + enum { + default_mc_catchain_lifetime = 200, + default_shard_catchain_lifetime = 200, + default_shard_validators_lifetime = 3000, + default_shard_validators_num = 7 + }; + + public: + enum { needValidatorSet = 16, needSpecialSmc = 32, needWorkchainInfo = 256 }; + int mode{0}; + ton::BlockIdExt block_id; + + private: + td::BitArray<256> config_addr; + Ref config_root; + std::unique_ptr config_dict; + std::unique_ptr cur_validators_; + std::unique_ptr workchains_dict_; + WorkchainSet workchains_; + + protected: + std::unique_ptr special_smc_dict; + + public: + static constexpr ton::LogicalTime get_lt_align() { + return 1000000; + } + static constexpr ton::LogicalTime get_max_lt_growth() { + return 10 * get_lt_align() - 1; + } + Ref get_config_param(int idx) const; + Ref get_config_param(int idx, int idx2) const; + Ref operator[](int idx) const { + return get_config_param(idx); + } + Ref get_root_cell() const { + return config_root; + } + bool is_masterchain() const { + return block_id.is_masterchain(); + } + bool set_block_id_ext(const ton::BlockIdExt& block_id_ext); + td::Result> get_special_smartcontracts(bool without_config = false) const; + bool is_special_smartcontract(const ton::StdSmcAddress& addr) const; + static td::Result> unpack_validator_set(Ref valset_root); + td::Result> get_storage_prices() const; + static CatchainValidatorsConfig unpack_catchain_validators_config(Ref cell); + CatchainValidatorsConfig get_catchain_validators_config() const; + td::Status visit_validator_params() const; + td::Result> get_block_limits(bool is_masterchain = false) const; + auto get_mc_block_limits() const { + return get_block_limits(true); + } + static td::Result>> unpack_workchain_list_ext( + Ref cell); + static td::Result unpack_workchain_list(Ref cell); + const WorkchainSet& get_workchain_list() const { + return workchains_; + } + const ValidatorSet* get_cur_validator_set() const { + return cur_validators_.get(); + } + ton::ValidatorSessionConfig get_consensus_config() const; + bool foreach_config_param(std::function)> scan_func) const; + Ref get_workchain_info(ton::WorkchainId workchain_id) const; + std::vector compute_validator_set(ton::ShardIdFull shard, const block::ValidatorSet& vset, + ton::UnixTime time, ton::CatchainSeqno cc_seqno) const; + std::vector compute_validator_set(ton::ShardIdFull shard, ton::UnixTime time, + ton::CatchainSeqno cc_seqno) const; + std::vector compute_total_validator_set(int next) const; + static std::vector do_compute_validator_set(const block::CatchainValidatorsConfig& ccv_conf, + ton::ShardIdFull shard, + const block::ValidatorSet& vset, ton::UnixTime time, + ton::CatchainSeqno cc_seqno); + + static td::Result> unpack_config(Ref config_root, + const td::Bits256& config_addr = td::Bits256::zero(), + int mode = 0); + static td::Result> unpack_config(Ref config_csr, int mode = 0); + static td::Result> extract_from_state(Ref mc_state_root, int mode = 0); + static td::Result> extract_from_key_block(Ref key_block_root, int mode = 0); + + protected: + Config(int _mode) : mode(_mode) { + config_addr.set_zero(); + } + Config(Ref config_root, const td::Bits256& config_addr = td::Bits256::zero(), int _mode = 0); + td::Status unpack_wrapped(Ref config_csr); + td::Status unpack(Ref config_csr); + td::Status unpack_wrapped(); + td::Status unpack(); +}; + +class ConfigInfo : public Config, public ShardConfig { + public: + enum { + needStateRoot = 1, + needLibraries = 2, + needStateExtraRoot = 4, + needShardHashes = 8, + needAccountsRoot = 64, + needPrevBlocks = 128 + }; + int vert_seqno{-1}; + int global_id_{0}; + ton::UnixTime utime{0}; + ton::LogicalTime lt{0}; + ton::BlockSeqno min_ref_mc_seqno_{std::numeric_limits::max()}; + ton::CatchainSeqno cc_seqno_{std::numeric_limits::max()}; + int shard_cc_updated{-1}; + bool nx_cc_updated; + bool is_key_state_{false}; + + private: + Ref state_root; + Ref lib_root_; + Ref state_extra_root_; + Ref accounts_root; + ton::ZeroStateIdExt zerostate_id_; + ton::BlockIdExt last_key_block_; + ton::LogicalTime last_key_block_lt_; + Ref shard_hashes; + std::unique_ptr shard_hashes_dict; + std::unique_ptr accounts_dict; + std::unique_ptr prev_blocks_dict_; + std::unique_ptr libraries_dict_; + + public: + bool set_block_id_ext(const ton::BlockIdExt& block_id_ext); + bool rotated_all_shards() const { + return nx_cc_updated; + } + int get_global_blockchain_id() const { + return global_id_; + } + ton::ZeroStateIdExt get_zerostate_id() const { + return zerostate_id_; + } + Ref lookup_library(const ton::Bits256& root_hash) const { + return lookup_library(root_hash.bits()); + } + Ref lookup_library(td::ConstBitPtr root_hash) const; + Ref get_libraries_root() const { + return lib_root_; + } + bool is_key_state() const { + return is_key_state_; + } + Ref get_state_extra_root() const { + return state_extra_root_; + } + ton::CatchainSeqno get_shard_cc_seqno(ton::ShardIdFull shard) const; + bool get_last_key_block(ton::BlockIdExt& blkid, ton::LogicalTime& blklt, bool strict = false) const; + bool get_old_mc_block_id(ton::BlockSeqno seqno, ton::BlockIdExt& blkid, ton::LogicalTime* end_lt = nullptr) const; + bool check_old_mc_block_id(const ton::BlockIdExt& blkid, bool strict = false) const; + // returns block with min seqno and req_lt <= block.end_lt + bool get_mc_block_by_lt(ton::LogicalTime lt, ton::BlockIdExt& blkid, ton::LogicalTime* end_lt = nullptr) const; + bool get_prev_key_block(ton::BlockSeqno req_seqno, ton::BlockIdExt& blkid, ton::LogicalTime* end_lt = nullptr) const; + bool get_next_key_block(ton::BlockSeqno req_seqno, ton::BlockIdExt& blkid, ton::LogicalTime* end_lt = nullptr) const; + td::Result>> get_special_ticktock_smartcontracts( + int tick_tock = 3) const; + int get_smc_tick_tock(td::ConstBitPtr smc_addr) const; + std::unique_ptr create_accounts_dict() const; + const vm::AugmentedDictionary& get_accounts_dict() const; + std::vector compute_validator_set_cc(ton::ShardIdFull shard, const block::ValidatorSet& vset, + ton::UnixTime time, + ton::CatchainSeqno* cc_seqno_delta = nullptr) const; + std::vector compute_validator_set_cc(ton::ShardIdFull shard, ton::UnixTime time, + ton::CatchainSeqno* cc_seqno_delta = nullptr) const; + static td::Result> extract_config(std::shared_ptr static_boc, + int mode = 0); + static td::Result> extract_config(Ref mc_state_root, int mode = 0); + + private: + ConfigInfo(Ref mc_state_root, int _mode = 0); + td::Status unpack_wrapped(); + td::Status unpack(); + void reset_mc_hash(); + void cleanup(); +}; + +} // namespace block diff --git a/crypto/block/output-queue-merger.cpp b/crypto/block/output-queue-merger.cpp new file mode 100644 index 00000000..981f1f95 --- /dev/null +++ b/crypto/block/output-queue-merger.cpp @@ -0,0 +1,221 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "output-queue-merger.h" + +namespace block { + +/* + * + * OUTPUT QUEUE MERGER + * + */ + +bool OutputQueueMerger::MsgKeyValue::operator<(const MsgKeyValue& other) const { + return lt < other.lt || + (lt == other.lt && td::bitstring::bits_memcmp(key.cbits() + 96, other.key.cbits() + 96, 256) < 0); +} + +bool OutputQueueMerger::MsgKeyValue::less(const std::unique_ptr& he1, + const std::unique_ptr& he2) { + return *he1 < *he2; +} + +bool OutputQueueMerger::MsgKeyValue::greater(const std::unique_ptr& he1, + const std::unique_ptr& he2) { + return *he2 < *he1; +} + +OutputQueueMerger::MsgKeyValue::MsgKeyValue(td::ConstBitPtr key_pfx, int key_pfx_len, int _src, Ref node) + : source(_src) { + unpack_node(key_pfx, key_pfx_len, std::move(node)); +} + +OutputQueueMerger::MsgKeyValue::MsgKeyValue(int _src, Ref node) : source(_src) { + unpack_node(td::ConstBitPtr{nullptr}, 0, std::move(node)); +} + +bool OutputQueueMerger::MsgKeyValue::invalidate() { + msg.clear(); + lt = 0; + source = -1; + return false; +} + +ton::LogicalTime OutputQueueMerger::MsgKeyValue::get_node_lt(Ref node, int key_pfx_len) { + if (node.is_null() || (unsigned)key_pfx_len > (unsigned)max_key_len) { + return std::numeric_limits::max(); + } + vm::dict::LabelParser label{std::move(node), max_key_len - key_pfx_len, vm::dict::LabelParser::chk_size}; + if (!label.is_valid()) { + return std::numeric_limits::max(); + } + label.skip_label(); + return label.remainder->prefetch_ulong(64); +} + +bool OutputQueueMerger::MsgKeyValue::unpack_node(td::ConstBitPtr key_pfx, int key_pfx_len, Ref node) { + if (node.is_null() || (unsigned)key_pfx_len >= (unsigned)max_key_len) { + return invalidate(); + } + if (!key_pfx.is_null()) { + td::bitstring::bits_memcpy(key.bits(), key_pfx, key_pfx_len); + } + vm::dict::LabelParser label{std::move(node), max_key_len - key_pfx_len, vm::dict::LabelParser::chk_size}; + if (!label.is_valid()) { + return invalidate(); + } + label.extract_label_to(key.bits() + key_pfx_len); + key_len = key_pfx_len + label.l_bits; + msg = std::move(label.remainder); + if (!msg.write().fetch_uint_to(64, lt)) { + return invalidate(); + } + if (is_fork() && msg->size_ext() != 0x20000) { + return invalidate(); + } + return true; +} + +bool OutputQueueMerger::MsgKeyValue::replace_with_child(bool child_idx) { + if (!is_fork() || msg.is_null() || msg->size_ext() != 0x20000) { + return false; + } + key[key_len] = child_idx; + return unpack_node(td::ConstBitPtr{nullptr}, key_len + 1, msg->prefetch_ref(child_idx)); +} + +bool OutputQueueMerger::MsgKeyValue::replace_by_prefix(td::ConstBitPtr req_pfx, int req_pfx_len) { + do { + if (td::bitstring::bits_memcmp(req_pfx, key.cbits(), std::min(req_pfx_len, key_len))) { + return false; + } + if (key_len >= req_pfx_len) { + return true; + } + } while (replace_with_child(req_pfx[key_len])); + return false; +} + +bool OutputQueueMerger::MsgKeyValue::split(MsgKeyValue& second) { + if (!is_fork() || msg.is_null()) { + return false; + } + unsigned long long keep_lt = lt; + unsigned long long left_lt = get_node_lt(msg->prefetch_ref(0), key_len + 1); + bool sw = (left_lt == lt); + second.source = source; + key[key_len] = sw; + if (!second.unpack_node(key.cbits(), key_len + 1, msg->prefetch_ref(sw))) { + return false; + } + key[key_len] = 1 - sw; + if (!unpack_node(td::ConstBitPtr{nullptr}, key_len + 1, msg->prefetch_ref(1 - sw))) { + return false; + } + if (lt != keep_lt || second.lt < keep_lt) { + return false; + } + return true; +} + +bool OutputQueueMerger::add_root(int src, Ref outmsg_root) { + if (outmsg_root.is_null()) { + return true; + } + //block::gen::HashmapAug{352, block::gen::t_EnqueuedMsg, block::gen::t_uint64}.print_ref(std::cerr, outmsg_root); + auto kv = std::make_unique(src, std::move(outmsg_root)); + if (kv->replace_by_prefix(common_pfx.cbits(), common_pfx_len)) { + heap.push_back(std::move(kv)); + } + return true; +} + +OutputQueueMerger::OutputQueueMerger(ton::ShardIdFull _queue_for, std::vector _neighbors) + : queue_for(_queue_for), neighbors(std::move(_neighbors)), eof(false), failed(false) { + init(); +} + +void OutputQueueMerger::init() { + common_pfx.bits().store_int(queue_for.workchain, 32); + int l = queue_for.pfx_len(); + td::bitstring::bits_store_long_top(common_pfx.bits() + 32, queue_for.shard, l); + common_pfx_len = 32 + l; + int i = 0; + for (block::McShardDescr& neighbor : neighbors) { + if (!neighbor.is_disabled()) { + LOG(DEBUG) << "adding " << (neighbor.outmsg_root.is_null() ? "" : "non-") << "empty output queue for neighbor #" + << i << " (" << neighbor.blk_.to_str() << ")"; + add_root(i++, neighbor.outmsg_root); + } else { + LOG(DEBUG) << "skipping output queue for disabled neighbor #" << i; + i++; + } + } + std::make_heap(heap.begin(), heap.end(), MsgKeyValue::greater); + eof = heap.empty(); + if (!eof) { + load(); + } +} + +OutputQueueMerger::MsgKeyValue* OutputQueueMerger::cur() { + return eof ? nullptr : msg_list.at(pos).get(); +} + +std::unique_ptr OutputQueueMerger::extract_cur() { + return eof ? std::unique_ptr{} : std::move(msg_list.at(pos)); +} + +bool OutputQueueMerger::next() { + if (eof) { + return false; + } else if (++pos < msg_list.size() || load()) { + return true; + } else { + eof = true; + return false; + } +} + +bool OutputQueueMerger::load() { + if (heap.empty() || failed) { + return false; + } + unsigned long long lt = heap[0]->lt; + std::size_t orig_size = msg_list.size(); + do { + while (heap[0]->is_fork()) { + auto other = std::make_unique(); + if (!heap[0]->split(*other)) { + failed = true; + return false; + } + heap.push_back(std::move(other)); + std::push_heap(heap.begin(), heap.end(), MsgKeyValue::greater); + } + assert(heap[0]->lt == lt); + std::pop_heap(heap.begin(), heap.end(), MsgKeyValue::greater); + msg_list.push_back(std::move(heap.back())); + heap.pop_back(); + } while (!heap.empty() && heap[0]->lt <= lt); + std::sort(msg_list.begin() + orig_size, msg_list.end(), MsgKeyValue::less); + return true; +} + +} // namespace block diff --git a/crypto/block/output-queue-merger.h b/crypto/block/output-queue-merger.h new file mode 100644 index 00000000..942ca7c8 --- /dev/null +++ b/crypto/block/output-queue-merger.h @@ -0,0 +1,80 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once +#include "ton/ton-types.h" +#include "vm/cells/CellSlice.h" +#include "block/mc-config.h" + +namespace block { +using td::Ref; + +struct OutputQueueMerger { + struct MsgKeyValue { + static constexpr int max_key_len = 32 + 64 + 256; + Ref msg; + unsigned long long lt; + int source; + int key_len{0}; + td::BitArray key; + MsgKeyValue() = default; + MsgKeyValue(int src, Ref node); + MsgKeyValue(td::ConstBitPtr key_pfx, int key_pfx_len, int src, Ref node); + bool operator<(const MsgKeyValue& other) const; + bool is_fork() const { + return key_len < max_key_len; + } + bool invalidate(); + static bool less(const std::unique_ptr& he1, const std::unique_ptr& he2); + static bool greater(const std::unique_ptr& he1, const std::unique_ptr& he2); + + protected: + friend struct OutputQueueMerger; + static ton::LogicalTime get_node_lt(Ref node, int key_pfx_len); + bool replace_with_child(bool child_idx); + bool replace_by_prefix(td::ConstBitPtr req_pfx, int req_pfx_len); + bool unpack_node(td::ConstBitPtr key_pfx, int key_pfx_len, Ref node); + bool split(MsgKeyValue& second); + }; + // + ton::ShardIdFull queue_for; + std::vector> msg_list; + std::vector neighbors; + + public: + OutputQueueMerger(ton::ShardIdFull _queue_for, std::vector _neighbors); + bool is_eof() const { + return eof; + } + MsgKeyValue* cur(); + std::unique_ptr extract_cur(); + bool next(); + + private: + td::BitArray<32 + 64> common_pfx; + int common_pfx_len; + std::vector> heap; + std::size_t pos{0}; + bool eof; + bool failed; + void init(); + bool add_root(int src, Ref outmsg_root); + bool load(); +}; + +} // namespace block diff --git a/crypto/block/test-block.cpp b/crypto/block/test-block.cpp new file mode 100644 index 00000000..6e10ebe9 --- /dev/null +++ b/crypto/block/test-block.cpp @@ -0,0 +1,248 @@ +/* + This file is part of TON Blockchain source code. + + TON Blockchain is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + TON Blockchain is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with TON Blockchain. If not, see . + + In addition, as a special exception, the copyright holders give permission + to link the code of portions of this program with the OpenSSL library. + You must obey the GNU General Public License in all respects for all + of the code used other than OpenSSL. If you modify file(s) with this + exception, you may extend this exception to your version of the file(s), + but you are not obligated to do so. If you do not wish to do so, delete this + exception statement from your version. If you delete this exception statement + from all source files in the program, then also delete it here. + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "block/block.h" +#include "vm/boc.h" +#include +#include "block-db.h" +#include "block-auto.h" +#include "block-parse.h" +#include "vm/cp0.h" +#include + +using td::Ref; + +int verbosity; + +struct IntError { + std::string err_msg; + IntError(std::string _msg) : err_msg(_msg) { + } + IntError(const char* _msg) : err_msg(_msg) { + } +}; + +td::Ref load_boc(std::string filename) { + std::cerr << "loading bag-of-cell file " << filename << std::endl; + auto bytes_res = block::load_binary_file(filename); + if (bytes_res.is_error()) { + throw IntError{PSTRING() << "cannot load file `" << filename << "` : " << bytes_res.move_as_error()}; + } + vm::BagOfCells boc; + auto res = boc.deserialize(bytes_res.move_as_ok()); + if (res.is_error()) { + throw IntError{PSTRING() << "cannot deserialize bag-of-cells " << res.move_as_error()}; + } + if (res.move_as_ok() <= 0 || boc.get_root_cell().is_null()) { + throw IntError{"cannot deserialize bag-of-cells "}; + } + return boc.get_root_cell(); +} + +void test1() { + block::ShardId id{ton::masterchainId}, id2{ton::basechainId, 0x11efULL << 48}; + std::cout << '[' << id << "][" << id2 << ']' << std::endl; + vm::CellBuilder cb; + cb << id << id2; + std::cout << "ShardIdent.pack() = " << block::tlb::t_ShardIdent.pack(cb, {12, 3, 0x3aeULL << 52}) << std::endl; + std::cout << cb << std::endl; + auto cref = cb.finalize(); + td::Ref cs{true, cref}, cs2; + block::ShardId id3{cs.write()}, id4, id5; + cs >> id4 >> id5; + std::cout << '[' << id3 << "][" << id4 << "][" << id5 << ']' << std::endl; + vm::CellSlice csl{std::move(cref)}; + std::cout << "ShardIdent.get_size() = " << block::tlb::t_ShardIdent.get_size(csl) << std::endl; + std::cout << "MsgAddress.get_size() = " << block::tlb::t_MsgAddress.get_size(csl) << std::endl; + std::cout << "Grams.get_size() = " << block::tlb::t_Grams.get_size(csl) << std::endl; + std::cout << "Grams.as_integer() = " << block::tlb::t_Grams.as_integer(csl) << std::endl; + (csl + 8).print_rec(std::cout); + std::cout << "Grams.get_size() = " << block::tlb::t_Grams.get_size(csl + 8) << std::endl; + std::cout << "Grams.as_integer() = " << block::tlb::t_Grams.as_integer(csl + 8) << std::endl; + + vm::CellSlice csl2{csl}; + block::gen::ShardIdent::Record sh_id; + for (int i = 0; i < 3; i++) { + std::cout << csl2 << std::endl; + bool ok = tlb::unpack(csl2, sh_id); + std::cout << "block::gen::ShardIdent.unpack() = " << ok << std::endl; + if (ok) { + std::cout << " (shard_ident shard_pfx_bits:" << sh_id.shard_pfx_bits << " workchain_id:" << sh_id.workchain_id + << " shard_prefix:" << std::hex << sh_id.shard_prefix << std::dec << ")" << std::endl; + } + } + + block::tlb::ShardIdent::Record shard_id; + for (int i = 0; i < 3; i++) { + std::cout << "ShardIdent.validate() = " << block::tlb::t_ShardIdent.validate(csl) << std::endl; + csl.print_rec(std::cerr); + csl.dump(std::cerr, 7); + std::cout << "ShardIdent.unpack() = " << block::tlb::t_ShardIdent.unpack(csl, shard_id) << std::endl; + if (shard_id.is_valid()) { + std::cout << "shard_pfx_bits:" << shard_id.shard_pfx_bits << " workchain_id:" << shard_id.workchain_id + << " shard_prefix:" << shard_id.shard_prefix << std::endl; + } + } + std::cout << "ShardIdent.skip_validate() = " << block::tlb::t_ShardIdent.validate_skip(csl) << std::endl; + std::cout << "ShardIdent.skip_validate() = " << block::tlb::t_ShardIdent.validate_skip(csl) << std::endl; + std::cout << "ShardIdent.skip_validate() = " << block::tlb::t_ShardIdent.validate_skip(csl) << std::endl; + using namespace td::literals; + std::cout << "Grams.store_intval(239) = " << block::tlb::t_Grams.store_integer_value(cb, "239"_i256) << std::endl; + std::cout << "Grams.store_intval(17239) = " << block::tlb::t_Grams.store_integer_value(cb, "17239"_i256) << std::endl; + std::cout << "Grams.store_intval(-17) = " << block::tlb::t_Grams.store_integer_value(cb, "-17"_i256) << std::endl; + std::cout << "Grams.store_intval(0) = " << block::tlb::t_Grams.store_integer_value(cb, "0"_i256) << std::endl; + std::cout << cb << std::endl; + cs = td::Ref{true, cb.finalize()}; + std::cout << "Grams.store_intval(666) = " << block::tlb::t_Grams.store_integer_value(cb, "666"_i256) << std::endl; + std::cout << cb << std::endl; + cs2 = td::Ref{true, cb.finalize()}; + std::cout << "Grams.validate(cs) = " << block::tlb::t_Grams.validate(*cs) << std::endl; + std::cout << "Grams.validate(cs2) = " << block::tlb::t_Grams.validate(*cs2) << std::endl; + // + block::gen::SplitMergeInfo::Record data; + block::gen::Grams::Record data2; + std::cout << "block::gen::Grams.validate(cs) = " << block::gen::t_Grams.validate(*cs) << std::endl; + std::cout << "block::gen::Grams.validate(cs2) = " << block::gen::t_Grams.validate(*cs2) << std::endl; + std::cout << "[cs = " << cs << "]" << std::endl; + bool ok = tlb::csr_unpack_inexact(cs, data); + std::cout << "block::gen::SplitMergeInfo.unpack(cs, data) = " << ok << std::endl; + if (ok) { + std::cout << " cur_shard_pfx_len = " << data.cur_shard_pfx_len << "; acc_split_depth = " << data.acc_split_depth + << "; this_addr = " << data.this_addr << "; sibling_addr = " << data.sibling_addr << std::endl; + } + ok = tlb::csr_unpack_inexact(cs, data2); + std::cout << "block::gen::Grams.unpack(cs, data2) = " << ok << std::endl; + if (ok) { + std::cout << " amount = " << data2.amount << std::endl; + block::gen::VarUInteger::Record data3; + ok = tlb::csr_type_unpack(data2.amount, block::gen::t_VarUInteger_16, data3); + std::cout << " block::gen::VarUInteger16.unpack(amount, data3) = " << ok << std::endl; + if (ok) { + std::cout << " len = " << data3.len << "; value = " << data3.value << std::endl; + vm::CellBuilder cb; + std::cout << " block::gen::VarUInteger16.pack(cb, data3) = " + << tlb::type_pack(cb, block::gen::t_VarUInteger_16, data3) << std::endl; + std::cout << " cb = " << cb.finalize() << std::endl; + } + } + /* + { + vm::CellBuilder cb; + td::BitArray<256> hash; + std::memset(hash.data(), 0x69, 32); + bool ok = tlb::pack( + cb, block::gen::Test::Record{1000000000000, {170239, -888, {239017, "1000000000000000000"_ri256}, hash}, 17}); + std::cout << " block::gen::Test::pack(cb, {1000000000000, ...}) = " << ok << std::endl; + std::cout << " cb = " << cb << std::endl; + auto cell = cb.finalize(); + vm::CellSlice cs{cell}; + cs.print_rec(std::cout); + block::gen::Test::Record data; + std::cout << " block::gen::Test::validate_ref(cell) = " << block::gen::t_Test.validate_ref(cell) << std::endl; + ok = tlb::unpack(cs, data); + std::cout << " block::gen::Test::unpack(cs, data) = " << ok << std::endl; + if (ok) { + std::cout << "a:" << data.a << " b:" << data.r1.b << " c:" << data.r1.c << " d:" << data.r1.r1.d + << " e:" << data.r1.r1.e << " f:" << data.r1.f << " g:" << data.g << std::endl; + } + std::cout << " block::gen::Test::print_ref(cell) = "; + block::gen::t_Test.print_ref(std::cout, cell, 2); + block::gen::t_CurrencyCollection.print_ref(std::cout, cell, 2); + std::cout << std::endl; + } + */ + std::cout << "Grams.add_values() = " << block::tlb::t_Grams.add_values(cb, cs.write(), cs2.write()) << std::endl; + std::cout << cb << std::endl; + std::cout << "block::gen::t_HashmapAug_64_...print_type() = " + << block::gen::t_HashmapAug_64_Ref_Transaction_CurrencyCollection << std::endl; +} + +void test2(vm::CellSlice& cs) { + std::cout << "Bool.validate() = " << block::tlb::t_Bool.validate(cs) << std::endl; + std::cout << "UInt16.validate() = " << block::tlb::t_uint16.validate(cs) << std::endl; + std::cout << "HashmapE(32,UInt16).validate() = " << block::tlb::HashmapE(32, block::tlb::t_uint16).validate(cs) + << std::endl; + std::cout << "block::gen::HashmapE(32,UInt16).validate() = " + << block::gen::HashmapE{32, block::gen::t_uint16}.validate(cs) << std::endl; +} + +void usage() { + std::cout << "usage: test-block []\n\tor test-block -h\n"; + std::exit(2); +} + +int main(int argc, char* const argv[]) { + int i; + int new_verbosity_level = VERBOSITY_NAME(INFO); + auto zerostate = std::make_unique(); + while ((i = getopt(argc, argv, "hv:")) != -1) { + switch (i) { + case 'v': + new_verbosity_level = VERBOSITY_NAME(FATAL) + (verbosity = td::to_integer(td::Slice(optarg))); + break; + case 'h': + usage(); + std::exit(2); + default: + usage(); + std::exit(2); + } + } + SET_VERBOSITY_LEVEL(new_verbosity_level); + try { + bool done = false; + while (optind < argc) { + auto boc = load_boc(argv[optind++]); + if (boc.is_null()) { + std::cerr << "(invalid boc)" << std::endl; + std::exit(2); + } else { + done = true; + vm::CellSlice cs{vm::NoVm(), boc}; + cs.print_rec(std::cout); + std::cout << std::endl; + block::gen::t_Block.print_ref(std::cout, boc); + std::cout << std::endl; + if (!block::gen::t_Block.validate_ref(boc)) { + std::cout << "(invalid Block)" << std::endl; + } else { + std::cout << "(valid Block)" << std::endl; + } + } + } + if (!done) { + test1(); + } + } catch (IntError& err) { + std::cerr << "caught internal error " << err.err_msg << std::endl; + return 1; + } catch (vm::VmError& err) { + std::cerr << "caught vm error " << err.get_msg() << std::endl; + return 1; + } + return 0; +} diff --git a/crypto/block/transaction.cpp b/crypto/block/transaction.cpp new file mode 100644 index 00000000..8e96c5da --- /dev/null +++ b/crypto/block/transaction.cpp @@ -0,0 +1,2089 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "block/transaction.h" +#include "block/block.h" +#include "block/block-parse.h" +#include "block/block-auto.h" +#include "td/utils/bits.h" +#include "td/utils/uint128.h" +#include "ton/ton-shard.h" +#include "vm/continuation.h" + +namespace block { +using td::Ref; + +Ref ComputePhaseConfig::lookup_library(td::ConstBitPtr key) const { + return libraries ? vm::lookup_library_in(key, libraries->get_root_cell()) : Ref{}; +} + +/* + * + * ACCOUNTS + * + */ + +bool Account::set_address(ton::WorkchainId wc, td::ConstBitPtr new_addr) { + workchain = wc; + addr = new_addr; + return true; +} + +bool Account::set_split_depth(int new_split_depth) { + if (new_split_depth < 0 || new_split_depth > 30) { + return false; // invalid value for split_depth + } + if (split_depth_set_) { + return split_depth_ == new_split_depth; + } else { + split_depth_ = (unsigned char)new_split_depth; + split_depth_set_ = true; + return true; + } +} + +bool Account::check_split_depth(int split_depth) const { + return split_depth_set_ ? (split_depth == split_depth_) : (split_depth >= 0 && split_depth <= 30); +} + +// initializes split_depth and addr_rewrite +bool Account::parse_maybe_anycast(vm::CellSlice& cs) { + int t = (int)cs.fetch_ulong(1); + if (t < 0) { + return false; + } else if (!t) { + return set_split_depth(0); + } + int depth; + return cs.fetch_uint_leq(30, depth) // anycast_info$_ depth:(#<= 30) + && depth // { depth >= 1 } + && cs.fetch_bits_to(addr_rewrite.bits(), depth) // rewrite_pfx:(bits depth) + && set_split_depth(depth); +} + +bool Account::store_maybe_anycast(vm::CellBuilder& cb) const { + if (!split_depth_set_ || !split_depth_) { + return cb.store_bool_bool(false); + } + return cb.store_bool_bool(true) // just$1 + && cb.store_uint_leq(30, split_depth_) // depth:(#<= 30) + && cb.store_bits_bool(addr_rewrite.cbits(), split_depth_); // rewrite_pfx:(bits depth) +} + +bool Account::unpack_address(vm::CellSlice& addr_cs) { + int addr_tag = block::gen::t_MsgAddressInt.get_tag(addr_cs); + int new_wc = ton::workchainInvalid; + switch (addr_tag) { + case block::gen::MsgAddressInt::addr_std: + if (!(addr_cs.advance(2) && parse_maybe_anycast(addr_cs) && addr_cs.fetch_int_to(8, new_wc) && + addr_cs.fetch_bits_to(addr_orig.bits(), 256) && addr_cs.empty_ext())) { + return false; + } + break; + case block::gen::MsgAddressInt::addr_var: + // cannot appear in masterchain / basechain + return false; + default: + return false; + } + addr_cs.clear(); + if (new_wc == ton::workchainInvalid) { + return false; + } + if (workchain == ton::workchainInvalid) { + workchain = new_wc; + addr = addr_orig; + addr.bits().copy_from(addr_rewrite.cbits(), split_depth_); + } else if (split_depth_) { + ton::StdSmcAddress new_addr = addr_orig; + new_addr.bits().copy_from(addr_rewrite.cbits(), split_depth_); + if (new_addr != addr) { + LOG(ERROR) << "error unpacking account " << workchain << ":" << addr.to_hex() + << " : account header contains different address " << new_addr.to_hex() << " (with splitting depth " + << (int)split_depth_ << ")"; + return false; + } + } else if (addr != addr_orig) { + LOG(ERROR) << "error unpacking account " << workchain << ":" << addr.to_hex() + << " : account header contains different address " << addr_orig.to_hex(); + return false; + } + if (workchain != new_wc) { + LOG(ERROR) << "error unpacking account " << workchain << ":" << addr.to_hex() + << " : account header contains different workchain " << new_wc; + return false; + } + addr_rewrite = addr.bits(); // initialize all 32 bits of addr_rewrite + if (!split_depth_) { + my_addr_exact = my_addr; + } + return true; +} + +bool Account::unpack_storage_info(vm::CellSlice& cs) { + block::gen::StorageInfo::Record info; + block::gen::StorageUsed::Record used; + if (!tlb::unpack_exact(cs, info) || !tlb::csr_unpack(info.used, used)) { + return false; + } + last_paid = info.last_paid; + if (info.due_payment->prefetch_ulong(1) == 1) { + vm::CellSlice& cs2 = info.due_payment.write(); + cs2.advance(1); + due_payment = block::tlb::t_Grams.as_integer_skip(cs2); + if (due_payment.is_null() || !cs2.empty_ext()) { + return false; + } + } else { + due_payment = td::RefInt256{true, 0}; + } + unsigned long long u = 0; + u |= storage_stat.cells = block::tlb::t_VarUInteger_7.as_uint(*used.cells); + u |= storage_stat.bits = block::tlb::t_VarUInteger_7.as_uint(*used.bits); + u |= storage_stat.public_cells = block::tlb::t_VarUInteger_7.as_uint(*used.public_cells); + LOG(DEBUG) << "last_paid=" << last_paid << "; cells=" << storage_stat.cells << " bits=" << storage_stat.bits + << " public_cells=" << storage_stat.public_cells; + return (u != std::numeric_limits::max()); +} + +// initializes split_depth (from account state - StateInit) +bool Account::unpack_state(vm::CellSlice& cs) { + block::gen::StateInit::Record state; + if (!tlb::unpack_exact(cs, state)) { + return false; + } + int sd = 0; + if (state.split_depth->size() == 6) { + sd = (int)state.split_depth->prefetch_ulong(6) - 32; + } + if (!set_split_depth(sd)) { + return false; + } + if (state.special->size() > 1) { + int z = (int)state.special->prefetch_ulong(3); + if (z < 0) { + return false; + } + tick = z & 2; + tock = z & 1; + LOG(DEBUG) << "tick=" << tick << ", tock=" << tock; + } + code = state.code->prefetch_ref(); + data = state.data->prefetch_ref(); + library = orig_library = state.library->prefetch_ref(); + return true; +} + +bool Account::compute_my_addr(bool force) { + if (!force && my_addr.not_null() && my_addr_exact.not_null()) { + return true; + } + if (workchain == ton::workchainInvalid) { + my_addr.clear(); + return false; + } + vm::CellBuilder cb; + Ref cell, cell2; + if (workchain >= -128 && workchain < 127) { + if (!(cb.store_long_bool(2, 2) // addr_std$10 + && store_maybe_anycast(cb) // anycast:(Maybe Anycast) + && cb.store_long_rchk_bool(workchain, 8) // workchain_id:int8 + && cb.store_bits_bool(addr_orig) // addr:bits256 + && cb.finalize_to(cell) && cb.store_long_bool(4, 3) // addr_std$10 anycast:(Maybe Anycast) + && cb.store_long_rchk_bool(workchain, 8) // workchain_id:int8 + && cb.store_bits_bool(addr) // addr:bits256 + && cb.finalize_to(cell2))) { + return false; + } + } else { + if (!(cb.store_long_bool(3, 2) // addr_var$11 + && store_maybe_anycast(cb) // anycast:(Maybe Anycast) + && cb.store_long_bool(256, 9) // addr_len:(## 9) + && cb.store_long_rchk_bool(workchain, 32) // workchain_id:int32 + && cb.store_bits_bool(addr_orig) // addr:(bits addr_len) + && cb.finalize_to(cell) && cb.store_long_bool(6, 3) // addr_var$11 anycast:(Maybe Anycast) + && cb.store_long_bool(256, 9) // addr_len:(## 9) + && cb.store_long_rchk_bool(workchain, 32) // workchain_id:int32 + && cb.store_bits_bool(addr) // addr:(bits addr_len) + && cb.finalize_to(cell2))) { + return false; + } + } + my_addr = load_cell_slice_ref(std::move(cell)); + my_addr_exact = load_cell_slice_ref(std::move(cell2)); + return true; +} + +bool Account::recompute_tmp_addr(Ref& tmp_addr, int split_depth, + td::ConstBitPtr orig_addr_rewrite) const { + if (!split_depth && my_addr_exact.not_null()) { + tmp_addr = my_addr_exact; + return true; + } + if (split_depth == split_depth_ && my_addr.not_null()) { + tmp_addr = my_addr; + return true; + } + if (split_depth < 0 || split_depth > 30) { + return false; + } + vm::CellBuilder cb; + bool std = (workchain >= -128 && workchain < 128); + if (!cb.store_long_bool(std ? 2 : 3, 2)) { // addr_std$10 or addr_var$11 + return false; + } + if (!split_depth) { + if (!cb.store_bool_bool(false)) { // anycast:(Maybe Anycast) + return false; + } + } else if (!(cb.store_bool_bool(true) // just$1 + && cb.store_long_bool(split_depth, 5) // depth:(#<= 30) + && cb.store_bits_bool(addr.bits(), split_depth))) { // rewrite_pfx:(bits depth) + return false; + } + if (std) { + if (!cb.store_long_rchk_bool(workchain, 8)) { // workchain:int8 + return false; + } + } else if (!(cb.store_long_bool(256, 9) // addr_len:(## 9) + && cb.store_long_bool(workchain, 32))) { // workchain:int32 + return false; + } + Ref cell; + return cb.store_bits_bool(orig_addr_rewrite, split_depth) // address:(bits addr_len) or bits256 + && cb.store_bits_bool(addr.bits() + split_depth, 256 - split_depth) && cb.finalize_to(cell) && + (tmp_addr = vm::load_cell_slice_ref(std::move(cell))).not_null(); +} + +bool Account::init_rewrite_addr(int split_depth, td::ConstBitPtr orig_addr_rewrite) { + if (split_depth_set_ || !created || !set_split_depth(split_depth)) { + return false; + } + addr_orig = addr; + addr_rewrite = addr.bits(); + addr_orig.bits().copy_from(orig_addr_rewrite, split_depth); + return compute_my_addr(true); +} + +// used to unpack previously existing accounts +bool Account::unpack(Ref shard_account, Ref extra, ton::UnixTime now, bool special) { + LOG(DEBUG) << "unpacking " << (special ? "special " : "") << "account " << addr.to_hex(); + if (shard_account.is_null()) { + LOG(ERROR) << "account " << addr.to_hex() << " does not have a valid ShardAccount to unpack"; + return false; + } + if (verbosity > 2) { + shard_account->print_rec(std::cerr, 2); + block::gen::t_ShardAccount.print(std::cerr, *shard_account); + } + block::gen::ShardAccount::Record acc_info; + if (!(block::gen::t_ShardAccount.validate_csr(shard_account) && + block::tlb::t_ShardAccount.validate_csr(shard_account) && tlb::unpack_exact(shard_account.write(), acc_info))) { + LOG(ERROR) << "account " << addr.to_hex() << " state is invalid"; + return false; + } + last_trans_lt_ = acc_info.last_trans_lt; + last_trans_hash_ = acc_info.last_trans_hash; + now_ = now; + auto account = std::move(acc_info.account); + total_state = orig_total_state = account; + auto acc_cs = load_cell_slice(std::move(account)); + if (block::gen::t_Account.get_tag(acc_cs) == block::gen::Account::account_none) { + status = acc_nonexist; + last_paid = 0; + last_trans_end_lt_ = 0; + is_special = special; + if (workchain != ton::workchainInvalid) { + addr_orig = addr; + addr_rewrite = addr.cbits(); + } + return compute_my_addr() && acc_cs.size_ext() == 1; + } + block::gen::Account::Record_account acc; + block::gen::AccountStorage::Record storage; + if (!(tlb::unpack_exact(acc_cs, acc) && (my_addr = acc.addr).not_null() && unpack_address(acc.addr.write()) && + compute_my_addr() && unpack_storage_info(acc.storage_stat.write()) && + tlb::csr_unpack(std::move(acc.storage), storage) && + std::max(storage.last_trans_lt, 1ULL) > acc_info.last_trans_lt && balance.unpack(std::move(storage.balance)))) { + return false; + } + is_special = special; + last_trans_end_lt_ = storage.last_trans_lt; + switch (block::gen::t_AccountState.get_tag(*storage.state)) { + case block::gen::AccountState::account_uninit: + status = orig_status = acc_uninit; + state_hash = addr; + break; + case block::gen::AccountState::account_frozen: + status = orig_status = acc_frozen; + if (!storage.state->have(2 + 256)) { + return false; + } + state_hash = storage.state->data_bits() + 2; + break; + case block::gen::AccountState::account_active: + status = orig_status = acc_active; + if (storage.state.write().fetch_ulong(1) != 1) { + return false; + } + inner_state = storage.state; + if (!unpack_state(storage.state.write())) { + return false; + } + state_hash.clear(); + break; + default: + return false; + } + LOG(DEBUG) << "end of Account.unpack() for " << workchain << ":" << addr.to_hex() + << " (balance = " << balance.to_str() << " ; last_trans_lt = " << last_trans_lt_ << ".." + << last_trans_end_lt_ << ")"; + return true; +} + +// used to initialize new accounts +bool Account::init_new(ton::UnixTime now) { + // only workchain and addr are initialized at this point + if (workchain == ton::workchainInvalid) { + return false; + } + addr_orig = addr; + addr_rewrite = addr.cbits(); + last_trans_lt_ = last_trans_end_lt_ = 0; + last_trans_hash_.set_zero(); + now_ = now; + last_paid = 0; + storage_stat.clear(); + due_payment = td::RefInt256{true, 0}; + balance.set_zero(); + if (my_addr_exact.is_null()) { + vm::CellBuilder cb; + if (workchain >= -128 && workchain < 128) { + CHECK(cb.store_long_bool(4, 3) // addr_std$10 anycast:(Maybe Anycast) + && cb.store_long_rchk_bool(workchain, 8) // workchain:int8 + && cb.store_bits_bool(addr)); // address:bits256 + } else { + CHECK(cb.store_long_bool(0xd00, 12) // addr_var$11 anycast:(Maybe Anycast) addr_len:(## 9) + && cb.store_long_rchk_bool(workchain, 32) // workchain:int32 + && cb.store_bits_bool(addr)); // address:(bits addr_len) + } + my_addr_exact = load_cell_slice_ref(cb.finalize()); + } + if (my_addr.is_null()) { + my_addr = my_addr_exact; + } + if (total_state.is_null()) { + vm::CellBuilder cb; + CHECK(cb.store_long_bool(0, 1) // account_none$0 = Account + && cb.finalize_to(total_state)); + orig_total_state = total_state; + } + state_hash = addr_orig; + status = orig_status = acc_nonexist; + created = true; + return true; +} + +bool Account::belongs_to_shard(ton::ShardIdFull shard) const { + return workchain == shard.workchain && ton::shard_is_ancestor(shard.shard, addr); +} + +void add_partial_storage_payment(td::BigInt256& payment, ton::UnixTime delta, const block::StoragePrices& prices, + const vm::CellStorageStat& storage, bool is_mc) { + td::BigInt256 c{(long long)storage.cells}, b{(long long)storage.bits}; + if (is_mc) { + // storage.cells * prices.mc_cell_price + storage.bits * prices.mc_bit_price; + c.mul_short(prices.mc_cell_price); + b.mul_short(prices.mc_bit_price); + } else { + // storage.cells * prices.cell_price + storage.bits * prices.bit_price; + c.mul_short(prices.cell_price); + b.mul_short(prices.bit_price); + } + b += c; + b.mul_short(delta); + CHECK(b.sgn() >= 0); + payment += b; +} + +td::RefInt256 Account::compute_storage_fees(ton::UnixTime now, const std::vector& pricing) const { + if (now <= last_paid || !last_paid || is_special || pricing.empty() || now <= pricing[0].valid_since) { + return {}; + } + std::size_t n = pricing.size(), i = n; + while (i && pricing[i - 1].valid_since > last_paid) { + --i; + } + if (i) { + --i; + } + ton::UnixTime upto = std::max(last_paid, pricing[0].valid_since); + td::RefInt256 total{true, 0}; + for (; i < n && upto < now; i++) { + ton::UnixTime valid_until = (i < n - 1 ? std::min(now, pricing[i + 1].valid_since) : now); + if (upto < valid_until) { + assert(upto >= pricing[i].valid_since); + add_partial_storage_payment(total.unique_write(), valid_until - upto, pricing[i], storage_stat, is_masterchain()); + } + upto = valid_until; + } + total.unique_write().rshift(16, 1); // divide by 2^16 with ceil rounding to obtain nanograms + return total; +} + +Transaction::Transaction(const Account& _account, int ttype, ton::LogicalTime req_start_lt, ton::UnixTime _now, + Ref _inmsg) + : trans_type(ttype) + , is_first(_account.transactions.empty()) + , new_tick(_account.tick) + , new_tock(_account.tock) + , now(_now) + , account(_account) + , my_addr(_account.my_addr) + , my_addr_exact(_account.my_addr_exact) + , balance(_account.balance) + , due_payment(_account.due_payment) + , last_paid(_account.last_paid) + , new_code(_account.code) + , new_data(_account.data) + , new_library(_account.library) + , in_msg(std::move(_inmsg)) { + start_lt = std::max(req_start_lt, account.last_trans_end_lt_); + end_lt = start_lt + 1; + acc_status = (account.status == Account::acc_nonexist ? Account::acc_uninit : account.status); +} + +bool Transaction::unpack_input_msg(bool ihr_delivered, const ActionPhaseConfig* cfg) { + if (in_msg.is_null() || in_msg_type) { + return false; + } + if (verbosity > 2) { + fprintf(stderr, "unpacking inbound message for a new transaction: "); + block::gen::t_Message_Any.print_ref(std::cerr, in_msg); + load_cell_slice(in_msg).print_rec(std::cerr); + } + auto cs = vm::load_cell_slice(in_msg); + int tag = block::gen::t_CommonMsgInfo.get_tag(cs); + Ref src_addr, dest_addr; + switch (tag) { + case block::gen::CommonMsgInfo::int_msg_info: { + block::gen::CommonMsgInfo::Record_int_msg_info info; + if (!(tlb::unpack(cs, info) && msg_balance_remaining.unpack(std::move(info.value)))) { + return false; + } + if (info.ihr_disabled && ihr_delivered) { + return false; + } + bounce_enabled = info.bounce; + src_addr = std::move(info.src); + dest_addr = std::move(info.dest); + in_msg_type = 1; + td::RefInt256 ihr_fee = block::tlb::t_Grams.as_integer(std::move(info.ihr_fee)); + if (ihr_delivered) { + in_fwd_fee = std::move(ihr_fee); + } else { + in_fwd_fee = td::RefInt256{true, 0}; + msg_balance_remaining += std::move(ihr_fee); + } + if (info.created_lt >= start_lt) { + start_lt = info.created_lt + 1; + end_lt = start_lt + 1; + } + // ... + break; + } + case block::gen::CommonMsgInfo::ext_in_msg_info: { + block::gen::CommonMsgInfo::Record_ext_in_msg_info info; + if (!tlb::unpack(cs, info)) { + return false; + } + src_addr = std::move(info.src); + dest_addr = std::move(info.dest); + in_msg_type = 2; + in_msg_extern = true; + // compute forwarding fees for this external message + vm::CellStorageStat sstat; // for message size + sstat.compute_used_storage(cs); // message body + sstat.bits -= cs.size(); // bits in the root cells are free + sstat.cells--; // the root cell itself is not counted as a cell + LOG(DEBUG) << "storage paid for a message: " << sstat.cells << " cells, " << sstat.bits << " bits"; + if (sstat.bits > max_msg_bits || sstat.cells > max_msg_cells) { + LOG(DEBUG) << "inbound external message too large, invalid"; + return false; + } + // fetch message pricing info + CHECK(cfg); + const MsgPrices& msg_prices = cfg->fetch_msg_prices(account.is_masterchain()); + // compute forwarding fees + auto fees_c = msg_prices.compute_fwd_ihr_fees(sstat.cells, sstat.bits, true); + LOG(DEBUG) << "computed fwd fees = " << fees_c.first << " + " << fees_c.second; + + if (account.is_special) { + LOG(DEBUG) << "computed fwd fees set to zero for special account"; + fees_c.first = fees_c.second = 0; + } + in_fwd_fee = td::RefInt256{true, fees_c.first}; + if (balance.grams < in_fwd_fee) { + LOG(DEBUG) << "cannot pay for importing this external message"; + return false; + } + // (tentatively) debit account for importing this external message + balance -= in_fwd_fee; + msg_balance_remaining.set_zero(); // external messages cannot carry value + // ... + break; + } + default: + return false; + } + // init:(Maybe (Either StateInit ^StateInit)) + switch ((int)cs.prefetch_ulong(2)) { + case 2: { // (just$1 (left$0 _:StateInit )) + Ref state_init; + vm::CellBuilder cb; + if (!(cs.advance(2) && block::gen::t_StateInit.fetch_to(cs, state_init) && + cb.append_cellslice_bool(std::move(state_init)) && cb.finalize_to(in_msg_state) && + block::gen::t_StateInit.validate_ref(in_msg_state))) { + LOG(DEBUG) << "cannot parse StateInit in inbound message"; + return false; + } + break; + } + case 3: { // (just$1 (right$1 _:^StateInit )) + if (!(cs.advance(2) && cs.fetch_ref_to(in_msg_state) && block::gen::t_StateInit.validate_ref(in_msg_state))) { + LOG(DEBUG) << "cannot parse ^StateInit in inbound message"; + return false; + } + break; + } + default: // nothing$0 + if (!cs.advance(1)) { + LOG(DEBUG) << "invalid init field in an inbound message"; + return false; + } + } + // body:(Either X ^X) + switch ((int)cs.fetch_ulong(1)) { + case 0: // left$0 _:X + in_msg_body = Ref{true, cs}; + break; + case 1: // right$1 _:^X + if (cs.size_ext() != 0x10000) { + LOG(DEBUG) << "body of an inbound message is not represented by exactly one reference"; + return false; + } + in_msg_body = load_cell_slice_ref(cs.prefetch_ref()); + break; + default: + LOG(DEBUG) << "invalid body field in an inbound message"; + return false; + } + total_fees += in_fwd_fee; + return true; +} + +bool Transaction::prepare_storage_phase(const StoragePhaseConfig& cfg, bool force_collect) { + if (now < account.last_paid) { + return false; + } + auto to_pay = account.compute_storage_fees(now, *(cfg.pricing)); + if (to_pay.not_null() && sgn(to_pay) < 0) { + return false; + } + auto res = std::make_unique(); + res->is_special = account.is_special; + last_paid = res->last_paid_updated = (res->is_special ? 0 : now); + if (to_pay.is_null() || sgn(to_pay) == 0) { + res->fees_collected = res->fees_due = td::RefInt256{true, 0}; + } else if (to_pay <= balance.grams) { + res->fees_collected = to_pay; + res->fees_due = td::RefInt256{true, 0}; + balance -= std::move(to_pay); + } else if (acc_status == Account::acc_frozen && !force_collect && to_pay + due_payment < cfg.delete_due_limit) { + // do not collect fee + res->last_paid_updated = (res->is_special ? 0 : account.last_paid); + res->fees_collected = res->fees_due = td::RefInt256{true, 0}; + } else { + res->fees_collected = balance.grams; + res->fees_due = std::move(to_pay) - std::move(balance.grams); + balance.grams = td::RefInt256{true, 0}; + if (!res->is_special) { + auto total_due = res->fees_due + due_payment; + switch (acc_status) { + case Account::acc_uninit: + case Account::acc_frozen: + if (total_due > cfg.delete_due_limit) { + res->deleted = true; + acc_status = Account::acc_deleted; + if (balance.extra.not_null()) { + // collect extra currencies as a fee + total_fees += block::CurrencyCollection{0, std::move(balance.extra)}; + balance.extra.clear(); + } + } + break; + case Account::acc_active: + if (total_due > cfg.freeze_due_limit) { + res->frozen = true; + was_frozen = true; + acc_status = Account::acc_frozen; + } + break; + } + } + } + total_fees += res->fees_collected; + storage_phase = std::move(res); + return true; +} + +bool Transaction::prepare_credit_phase() { + credit_phase = std::make_unique(); + auto collected = std::min(msg_balance_remaining.grams, due_payment); + credit_phase->due_fees_collected = collected; + due_payment -= collected; + credit_phase->credit = msg_balance_remaining -= collected; + if (!msg_balance_remaining.is_valid()) { + LOG(ERROR) << "cannot compute the amount to be credited in the credit phase of transaction"; + return false; + } + // NB: msg_balance_remaining may be deducted from balance later during bounce phase + balance += msg_balance_remaining; + if (!balance.is_valid()) { + LOG(ERROR) << "cannot credit currency collection to account"; + return false; + } + total_fees += std::move(collected); + return true; +} + +void ComputePhaseConfig::compute_threshold() { + gas_price256 = td::RefInt256{true, gas_price}; + max_gas_threshold = td::rshift(gas_price256 * gas_limit, 16, 1); +} + +td::uint64 ComputePhaseConfig::gas_bought_for(td::RefInt256 nanograms) const { + if (nanograms.is_null() || sgn(nanograms) < 0) { + return 0; + } + if (nanograms >= max_gas_threshold) { + return gas_limit; + } + auto res = td::div(std::move(nanograms) << 16, gas_price256); + return res->to_long(); +} + +td::RefInt256 ComputePhaseConfig::compute_gas_price(td::uint64 gas_used) const { + return td::rshift(gas_price256 * gas_used, 16, 1); +} + +bool Transaction::compute_gas_limits(ComputePhase& cp, const ComputePhaseConfig& cfg) { + // Compute gas limits + if (account.is_special) { + cp.gas_max = cfg.gas_limit; // TODO: introduce special gas limits? + } else { + cp.gas_max = cfg.gas_bought_for(balance.grams); + } + cp.gas_credit = 0; + if (trans_type != tr_ord) { + // may use all gas that can be bought using remaining balance + cp.gas_limit = cp.gas_max; + } else { + // originally use only gas bought using remaining message balance + // if the message is "accepted" by the smart contract, the gas limit will be set to gas_max + cp.gas_limit = cfg.gas_bought_for(msg_balance_remaining.grams); + if (!block::tlb::t_Message.is_internal(in_msg)) { + // external messages carry no balance, give them some credit to check whether they are accepted + cp.gas_credit = std::min(cfg.gas_credit, cp.gas_max); + } + } + LOG(DEBUG) << "gas limits: max=" << cp.gas_max << ", limit=" << cp.gas_limit << ", credit=" << cp.gas_credit; + return true; +} + +Ref Transaction::prepare_vm_stack(ComputePhase& cp) { + Ref stack_ref{true}; + td::RefInt256 acc_addr{true}; + CHECK(acc_addr.write().import_bits(account.addr.cbits(), 256)); + vm::Stack& stack = stack_ref.write(); + switch (trans_type) { + case tr_tick: + case tr_tock: + stack.push_int(balance.grams); + stack.push_int(std::move(acc_addr)); + stack.push_bool(trans_type == tr_tock); + stack.push_smallint(-2); + return stack_ref; + case tr_ord: + stack.push_int(balance.grams); + stack.push_int(msg_balance_remaining.grams); + stack.push_cell(in_msg); + stack.push_cellslice(in_msg_body); + stack.push_bool(in_msg_extern); + return stack_ref; + default: + LOG(ERROR) << "cannot initialize stack for a transaction of type " << trans_type; + return {}; + } +} + +bool Transaction::prepare_rand_seed(td::BitArray<256>& rand_seed, const ComputePhaseConfig& cfg) const { + // we might use SHA256(block_rand_seed . addr . trans_lt) + // instead, we use SHA256(block_rand_seed . addr) + // if the smart contract wants to randomize further, it can use RANDOMIZE instruction + td::BitArray<256 + 256> data; + data.bits().copy_from(cfg.block_rand_seed.cbits(), 256); + (data.bits() + 256).copy_from(account.addr_rewrite.cbits(), 256); + rand_seed.clear(); + data.compute_sha256(rand_seed); + return true; +} + +Ref Transaction::prepare_vm_c7(const ComputePhaseConfig& cfg) const { + // TODO: fix initialization of c7 + td::BitArray<256> rand_seed; + td::RefInt256 rand_seed_int{true}; + if (!(prepare_rand_seed(rand_seed, cfg) && rand_seed_int.unique_write().import_bits(rand_seed.cbits(), 256, false))) { + LOG(ERROR) << "cannot compute rand_seed for transaction"; + throw CollatorError{"cannot generate valid SmartContractInfo"}; + return {}; + } + auto tuple = vm::make_tuple_ref( + td::make_refint(0x076ef1ea), // [ magic:0x076ef1ea + td::make_refint(0), // actions:Integer + td::make_refint(0), // msgs_sent:Integer + td::make_refint(now), // unixtime:Integer + td::make_refint(account.block_lt), // block_lt:Integer + td::make_refint(start_lt), // trans_lt:Integer + std::move(rand_seed_int), // rand_seed:Integer + balance.as_vm_tuple(), // balance_remaining:[Integer (Maybe Cell)] + my_addr, // myself:MsgAddressInt + vm::StackEntry::maybe(cfg.global_config)); // global_config:(Maybe Cell) ] = SmartContractInfo; + LOG(DEBUG) << "SmartContractInfo initialized with " << vm::StackEntry(tuple).to_string(); + return vm::make_tuple_ref(std::move(tuple)); +} + +int output_actions_count(Ref list) { + int i = -1; + do { + ++i; + list = load_cell_slice(std::move(list)).prefetch_ref(); + } while (list.not_null()); + return i; +} + +bool Transaction::unpack_msg_state(bool lib_only) { + block::gen::StateInit::Record state; + if (in_msg_state.is_null() || !tlb::unpack_cell(in_msg_state, state)) { + LOG(ERROR) << "cannot unpack StateInit from an inbound message"; + return false; + } + if (lib_only) { + in_msg_library = state.library->prefetch_ref(); + return true; + } + if (state.split_depth->size() == 6) { + new_split_depth = (signed char)(state.split_depth->prefetch_ulong(6) - 32); + } else { + new_split_depth = 0; + } + if (state.special->size() > 1) { + int z = (int)state.special->prefetch_ulong(3); + if (z < 0) { + return false; + } + new_tick = z & 2; + new_tock = z & 1; + LOG(DEBUG) << "tick=" << new_tick << ", tock=" << new_tock; + } + new_code = state.code->prefetch_ref(); + new_data = state.data->prefetch_ref(); + new_library = state.library->prefetch_ref(); + return true; +} + +std::vector> Transaction::compute_vm_libraries(const ComputePhaseConfig& cfg) { + std::vector> lib_set; + if (in_msg_library.not_null()) { + lib_set.push_back(in_msg_library); + } + if (new_library.not_null()) { + lib_set.push_back(new_library); + } + auto global_libs = cfg.get_lib_root(); + if (global_libs.not_null()) { + lib_set.push_back(std::move(global_libs)); + } + return lib_set; +} + +bool Transaction::check_in_msg_state_hash() { + CHECK(in_msg_state.not_null()); + CHECK(new_split_depth >= 0 && new_split_depth < 32); + td::Bits256 in_state_hash = in_msg_state->get_hash().bits(); + int d = new_split_depth; + if ((in_state_hash.bits() + d).compare(account.addr.bits() + d, 256 - d)) { + return false; + } + orig_addr_rewrite = in_state_hash.bits(); + orig_addr_rewrite_set = true; + return account.recompute_tmp_addr(my_addr, d, orig_addr_rewrite.bits()); +} + +bool Transaction::prepare_compute_phase(const ComputePhaseConfig& cfg) { + // TODO: add more skip verifications + sometimes use state from in_msg to re-activate + // ... + compute_phase = std::make_unique(); + ComputePhase& cp = *(compute_phase.get()); + if (td::sgn(balance.grams) <= 0) { + // no gas + cp.skip_reason = ComputePhase::sk_no_gas; + return true; + } + if (in_msg_state.not_null()) { + LOG(DEBUG) << "HASH(in_msg_state) = " << in_msg_state->get_hash().bits().to_hex(256) + << ", account_state_hash = " << account.state_hash.to_hex(); + // vm::load_cell_slice(in_msg_state).print_rec(std::cerr); + } else { + LOG(DEBUG) << "in_msg_state is null"; + } + if (in_msg_state.not_null() && + (acc_status == Account::acc_uninit || + (acc_status == Account::acc_frozen && account.state_hash == in_msg_state->get_hash().bits()))) { + use_msg_state = true; + if (!(unpack_msg_state() && account.check_split_depth(new_split_depth))) { + LOG(DEBUG) << "cannot unpack in_msg_state, or it has bad split_depth; cannot init account state"; + cp.skip_reason = ComputePhase::sk_bad_state; + return false; + } + if (acc_status == Account::acc_uninit && !check_in_msg_state_hash()) { + LOG(DEBUG) << "in_msg_state hash mismatch, cannot init account state"; + cp.skip_reason = ComputePhase::sk_bad_state; + return false; + } + } else if (acc_status != Account::acc_active) { + // no state, cannot perform transactions + cp.skip_reason = in_msg_state.not_null() ? ComputePhase::sk_bad_state : ComputePhase::sk_no_state; + return true; + } else if (in_msg_state.not_null()) { + unpack_msg_state(true); // use only libraries + } + // Compute gas limits + if (!compute_gas_limits(cp, cfg)) { + compute_phase.reset(); + return false; + } + // initialize VM + Ref stack = prepare_vm_stack(cp); + if (stack.is_null()) { + compute_phase.reset(); + return false; + } + // OstreamLogger ostream_logger(error_stream); + // auto log = create_vm_log(error_stream ? &ostream_logger : nullptr); + vm::GasLimits gas{(long long)cp.gas_limit, (long long)cp.gas_max, (long long)cp.gas_credit}; + LOG(DEBUG) << "creating VM"; + + vm::VmState vm{new_code, std::move(stack), gas, 1, new_data, vm::VmLog(), compute_vm_libraries(cfg)}; + vm.set_c7(prepare_vm_c7(cfg)); // tuple with SmartContractInfo + // vm.incr_stack_trace(1); // enable stack dump after each step + + LOG(DEBUG) << "starting VM"; + cp.vm_init_state_hash = vm.get_state_hash(); + cp.exit_code = ~vm.run(); + LOG(DEBUG) << "VM terminated with exit code " << cp.exit_code; + cp.out_of_gas = (cp.exit_code == ~(int)vm::Excno::out_of_gas); + cp.vm_final_state_hash = vm.get_final_state_hash(cp.exit_code); + stack = vm.get_stack_ref(); + cp.vm_steps = (int)vm.get_steps_count(); + gas = vm.get_gas_limits(); + cp.gas_used = std::min(gas.gas_consumed(), gas.gas_limit); + cp.accepted = (gas.gas_credit == 0); + cp.success = (cp.accepted && (unsigned)cp.exit_code <= 1); + if (cp.accepted & use_msg_state) { + was_activated = true; + acc_status = Account::acc_active; + } + LOG(INFO) << "steps: " << vm.get_steps_count() << " gas: used=" << gas.gas_consumed() << ", max=" << gas.gas_max + << ", limit=" << gas.gas_limit << ", credit=" << gas.gas_credit; + LOG(INFO) << "out_of_gas=" << cp.out_of_gas << ", accepted=" << cp.accepted << ", success=" << cp.success; + if (cp.success) { + cp.new_data = vm.get_c4(); // c4 -> persistent data + cp.actions = vm.get_d(5); // c5 -> action list + int out_act_num = output_actions_count(cp.actions); + if (verbosity > 2) { + std::cerr << "new smart contract data: "; + load_cell_slice(cp.new_data).print_rec(std::cerr); + std::cerr << "output actions: "; + block::gen::OutList{out_act_num}.print_ref(std::cerr, cp.actions); + } + } + cp.mode = 0; + cp.exit_arg = 0; + if (!cp.success && stack->depth() > 0) { + td::RefInt256 tos = stack->tos().as_int(); + if (tos.not_null() && tos->signed_fits_bits(32)) { + cp.exit_arg = (int)tos->to_long(); + } + } + if (cp.accepted) { + if (account.is_special) { + cp.gas_fees = td::RefInt256{true, 0}; + } else { + cp.gas_fees = cfg.compute_gas_price(cp.gas_used); + total_fees += cp.gas_fees; + balance -= cp.gas_fees; + } + if (verbosity > 2) { + std::cerr << "gas fees: " << cp.gas_fees << " = " << cfg.gas_price256 << " * " << cp.gas_used + << " /2^16 ; price=" << cfg.gas_price << "; remaining balance=" << balance << std::endl; + } + CHECK(td::sgn(balance.grams) >= 0); + } + return true; +} + +bool Transaction::prepare_action_phase(const ActionPhaseConfig& cfg) { + if (!compute_phase || !compute_phase->success) { + return false; + } + action_phase = std::make_unique(); + ActionPhase& ap = *(action_phase.get()); + ap.result_code = -1; + ap.result_arg = 0; + ap.tot_actions = ap.spec_actions = ap.skipped_actions = ap.msgs_created = 0; + Ref list = compute_phase->actions; + assert(list.not_null()); + ap.action_list_hash = list->get_hash().bits(); + ap.remaining_balance = balance; + ap.end_lt = end_lt; + ap.total_fwd_fees = td::RefInt256{true, 0}; + ap.total_action_fees = td::RefInt256{true, 0}; + ap.reserved_balance.set_zero(); + + int n = 0; + while (true) { + ap.action_list.push_back(list); + auto cs = load_cell_slice(std::move(list)); + if (!cs.size_ext()) { + break; + } + if (!cs.have_refs()) { + ap.result_code = 32; // action list invalid + ap.result_arg = n; + ap.action_list_invalid = true; + LOG(DEBUG) << "action list invalid: entry found with data but no next reference"; + return true; + } + list = cs.prefetch_ref(); + n++; + if (n > cfg.max_actions) { + ap.result_code = 33; // too many actions + ap.result_arg = n; + ap.action_list_invalid = true; + LOG(DEBUG) << "action list too long: more than " << cfg.max_actions << " actions"; + return true; + } + } + + ap.tot_actions = n; + ap.spec_actions = ap.skipped_actions = 0; + for (int i = n - 1; i >= 0; --i) { + ap.result_arg = n - 1 - i; + if (!block::gen::t_OutListNode.validate_ref(ap.action_list[i])) { + ap.result_code = 34; // action #i invalid or unsupported + ap.action_list_invalid = true; + LOG(DEBUG) << "invalid action " << ap.result_arg << " found while preprocessing action list: error code " + << ap.result_code; + return true; + } + } + ap.valid = true; + for (int i = n - 1; i >= 0; --i) { + ap.result_arg = n - 1 - i; + vm::CellSlice cs = load_cell_slice(ap.action_list[i]); + CHECK(cs.fetch_ref().not_null()); + int tag = block::gen::t_OutAction.get_tag(cs); + CHECK(tag >= 0); + int err_code = 34; + switch (tag) { + case block::gen::OutAction::action_set_code: + err_code = try_action_set_code(cs, ap, cfg); + break; + case block::gen::OutAction::action_send_msg: + err_code = try_action_send_msg(cs, ap, cfg); + break; + case block::gen::OutAction::action_reserve_currency: + err_code = try_action_reserve_currency(cs, ap, cfg); + break; + } + if (err_code) { + ap.result_code = (err_code == -1 ? 34 : err_code); + ap.end_lt = end_lt; + if (err_code == -1 || err_code == 34) { + ap.action_list_invalid = true; + } + if (err_code == 37 || err_code == 38) { + ap.no_funds = true; + } + LOG(DEBUG) << "invalid action " << ap.result_arg << " in action list: error code " << ap.result_code; + return true; + } + } + ap.result_arg = 0; + ap.result_code = 0; + CHECK(ap.remaining_balance.grams->sgn() >= 0); + CHECK(ap.reserved_balance.grams->sgn() >= 0); + ap.remaining_balance += ap.reserved_balance; + CHECK(ap.remaining_balance.is_valid()); + if (ap.acc_delete_req) { + CHECK(ap.remaining_balance.is_zero()); + ap.acc_status_change = ActionPhase::acst_deleted; + acc_status = Account::acc_deleted; + was_deleted = true; + } + ap.success = true; + end_lt = ap.end_lt; + out_msgs = std::move(ap.out_msgs); + if (ap.new_code.not_null()) { + new_code = ap.new_code; + } + new_data = compute_phase->new_data; // tentative persistent data update applied + total_fees += + ap.total_action_fees; // NB: forwarding fees are not accounted here (they are not collected by the validators in this transaction) + balance = ap.remaining_balance; + return true; +} + +int Transaction::try_action_set_code(vm::CellSlice& cs, ActionPhase& ap, const ActionPhaseConfig& cfg) { + block::gen::OutAction::Record_action_set_code rec; + if (!tlb::unpack_exact(cs, rec)) { + return -1; + } + ap.new_code = std::move(rec.new_code); + ap.code_changed = true; + ap.spec_actions++; + return 0; +} + +// msg_fwd_fees = (lump_price + ceil((bit_price * msg.bits + cell_price * msg.cells)/2^16)) nanograms +// ihr_fwd_fees = ceil((msg_fwd_fees * ihr_price_factor)/2^16) nanograms +// bits in the root cell of a message are not included in msg.bits (lump_price pays for them) +td::uint64 MsgPrices::compute_fwd_fees(td::uint64 cells, td::uint64 bits) const { + return lump_price + td::uint128(bit_price) + .mult(bits) + .add(td::uint128(cell_price).mult(cells)) + .add(td::uint128(0xffffu)) + .shr(16) + .lo(); +} + +std::pair MsgPrices::compute_fwd_ihr_fees(td::uint64 cells, td::uint64 bits, + bool ihr_disabled) const { + td::uint64 fwd = compute_fwd_fees(cells, bits); + if (ihr_disabled) { + return std::pair(fwd, 0); + } + return std::pair(fwd, td::uint128(fwd).mult(ihr_factor).shr(16).lo()); +} + +td::RefInt256 MsgPrices::get_first_part(td::RefInt256 total) const { + return (std::move(total) * first_frac) >> 16; +} + +td::uint64 MsgPrices::get_first_part(td::uint64 total) const { + return td::uint128(total).mult(first_frac).shr(16).lo(); +} + +td::RefInt256 MsgPrices::get_next_part(td::RefInt256 total) const { + return (std::move(total) * next_frac) >> 16; +} + +bool Transaction::check_replace_src_addr(Ref& src_addr) const { + int t = (int)src_addr->prefetch_ulong(2); + if (!t && src_addr->size_ext() == 2) { + // addr_none$00 --> replace with the address of current smart contract + src_addr = my_addr; + return true; + } + if (t != 2) { + // invalid address (addr_extern and addr_var cannot be source addresses) + return false; + } + if (src_addr->contents_equal(*my_addr) || src_addr->contents_equal(*my_addr_exact)) { + // source address matches that of the current account + return true; + } + // only one valid case remaining: rewritten source address used, replace with the complete one + // (are we sure we want to allow this?) + return false; +} + +bool Transaction::check_rewrite_dest_addr(Ref& dest_addr, const ActionPhaseConfig& cfg, + bool* is_mc) const { + if (!dest_addr->prefetch_ulong(1)) { + // all external addresses allowed + if (is_mc) { + *is_mc = false; + } + return true; + } + bool repack = false; + int tag = block::gen::t_MsgAddressInt.get_tag(*dest_addr); + + block::gen::MsgAddressInt::Record_addr_var rec; + + if (tag == block::gen::MsgAddressInt::addr_var) { + if (!tlb::csr_unpack(dest_addr, rec)) { + // cannot unpack addr_var + LOG(DEBUG) << "cannot unpack addr_var in a destination address"; + return false; + } + if (rec.addr_len == 256 && rec.workchain_id >= -128 && rec.workchain_id < 128) { + LOG(DEBUG) << "destination address contains an addr_var to be repacked into addr_std"; + repack = true; + } + } else if (tag == block::gen::MsgAddressInt::addr_std) { + block::gen::MsgAddressInt::Record_addr_std recs; + if (!tlb::csr_unpack(dest_addr, recs)) { + // cannot unpack addr_std + LOG(DEBUG) << "cannot unpack addr_std in a destination address"; + return false; + } + rec.anycast = std::move(recs.anycast); + rec.addr_len = 256; + rec.workchain_id = recs.workchain_id; + rec.address = td::make_bitstring_ref(recs.address); + } else { + // unknown address format (not a MsgAddressInt) + LOG(DEBUG) << "destination address does not have a MsgAddressInt tag"; + return false; + } + if (rec.workchain_id != ton::masterchainId) { + // recover destination workchain info from configuration + auto it = cfg.workchains->find(rec.workchain_id); + if (it == cfg.workchains->end()) { + // undefined destination workchain + LOG(DEBUG) << "destination address contains unknown workchain_id " << rec.workchain_id; + return false; + } + if (!it->second->accept_msgs) { + // workchain does not accept new messages + LOG(DEBUG) << "destination address belongs to workchain " << rec.workchain_id << " not accepting new messages"; + return false; + } + if (!it->second->is_valid_addr_len(rec.addr_len)) { + // invalid address length for specified workchain + LOG(DEBUG) << "destination address has length " << rec.addr_len << " invalid for destination workchain " + << rec.workchain_id; + return false; + } + } + if (rec.anycast->size() > 1) { + // destination address is an anycast + if (rec.workchain_id == ton::masterchainId) { + // anycast addresses disabled in masterchain + LOG(DEBUG) << "masterchain destination address has an anycast field"; + return false; + } + vm::CellSlice cs{*rec.anycast}; + int d = (int)cs.fetch_ulong(6) - 32; + if (d <= 0 || d > 30) { + // invalid anycast prefix length + return false; + } + unsigned pfx = (unsigned)cs.fetch_ulong(d); + unsigned my_pfx = (unsigned)account.addr.cbits().get_uint(d); + if (pfx != my_pfx) { + // rewrite destination address + vm::CellBuilder cb; + CHECK(cb.store_long_bool(32 + d, 6) // just$1 depth:(#<= 30) + && cb.store_long_bool(my_pfx, d) // rewrite_pfx:(bits depth) + && (rec.anycast = load_cell_slice_ref(cb.finalize())).not_null()); + repack = true; + } + } + if (is_mc) { + *is_mc = (rec.workchain_id == ton::masterchainId); + } + if (!repack) { + return true; + } + if (rec.addr_len == 256 && rec.workchain_id >= -128 && rec.workchain_id < 128) { + // repack as an addr_std + vm::CellBuilder cb; + CHECK(cb.store_long_bool(2, 2) // addr_std$10 + && cb.append_cellslice_bool(std::move(rec.anycast)) // anycast:(Maybe Anycast) ... + && cb.store_long_bool(rec.workchain_id, 8) // workchain_id:int8 + && cb.append_bitstring(std::move(rec.address)) // address:bits256 + && (dest_addr = load_cell_slice_ref(cb.finalize())).not_null()); + } else { + // repack as an addr_var + CHECK(tlb::csr_pack(dest_addr, std::move(rec))); + } + CHECK(block::gen::t_MsgAddressInt.validate_csr(dest_addr)); + return true; +} + +int Transaction::try_action_send_msg(vm::CellSlice& cs, ActionPhase& ap, const ActionPhaseConfig& cfg) { + block::gen::OutAction::Record_action_send_msg act_rec; + // mode: +128 = attach all remaining balance, +64 = attach all remaining balance of the inbound message, +1 = pay message fees, +2 = skip if message cannot be sent + if (!tlb::unpack_exact(cs, act_rec) || (act_rec.mode & ~0xc3) || (act_rec.mode & 0xc0) == 0xc0) { + return -1; + } + bool skip_invalid = (act_rec.mode & 2); + auto cs2 = vm::load_cell_slice(act_rec.out_msg); + // try to parse suggested message in cs2 + td::RefInt256 fwd_fee, ihr_fee; + bool ext_msg = cs2.prefetch_ulong(1); + block::gen::CommonMsgInfoRelaxed::Record_int_msg_info info; + if (ext_msg) { + // ext_out_msg_info$11 constructor of CommonMsgInfoRelaxed + block::gen::CommonMsgInfoRelaxed::Record_ext_out_msg_info erec; + if (!tlb::unpack(cs2, erec)) { + return -1; + } + info.src = std::move(erec.src); + info.dest = std::move(erec.dest); + // created_lt and created_at are ignored + info.ihr_disabled = true; + info.bounce = false; + info.bounced = false; + fwd_fee = ihr_fee = td::RefInt256{true, 0}; + } else { + // int_msg_info$0 constructor + if (!tlb::unpack(cs2, info) || !block::tlb::t_CurrencyCollection.validate_csr(info.value)) { + return -1; + } + fwd_fee = block::tlb::t_Grams.as_integer(info.fwd_fee); + ihr_fee = block::tlb::t_Grams.as_integer(info.ihr_fee); + } + // set created_at and created_lt to correct values + info.created_at = now; + info.created_lt = ap.end_lt; + // always clear bounced flag + info.bounced = false; + // have to check source address + // it must be either our source address, or empty + if (!check_replace_src_addr(info.src)) { + LOG(DEBUG) << "invalid source address in a proposed outbound message"; + return 35; // invalid source address + } + bool to_mc = false; + if (!check_rewrite_dest_addr(info.dest, cfg, &to_mc)) { + LOG(DEBUG) << "invalid destination address in a proposed outbound message"; + return skip_invalid ? 0 : 36; // invalid destination address + } + + // fetch message pricing info + const MsgPrices& msg_prices = cfg.fetch_msg_prices(to_mc || account.is_masterchain()); + // compute size of message + vm::CellStorageStat sstat; // for message size + // preliminary storage estimation of the resulting message + sstat.compute_used_storage(cs2); // message body + if (!ext_msg) { + sstat.add_used_storage(info.value->prefetch_ref()); + } + sstat.bits -= cs2.size(); // bits in the root cells are free + sstat.cells--; // the root cell itself is not counted as a cell + LOG(DEBUG) << "storage paid for a message: " << sstat.cells << " cells, " << sstat.bits << " bits"; + if (sstat.bits > max_msg_bits || sstat.cells > max_msg_cells) { + LOG(DEBUG) << "message too large, invalid"; + return skip_invalid ? 0 : 40; + } + + // compute forwarding fees + auto fees_c = msg_prices.compute_fwd_ihr_fees(sstat.cells, sstat.bits, info.ihr_disabled); + LOG(DEBUG) << "computed fwd fees = " << fees_c.first << " + " << fees_c.second; + + if (account.is_special) { + LOG(DEBUG) << "computed fwd fees set to zero for special account"; + fees_c.first = fees_c.second = 0; + } + + // set fees to computed values + if (fwd_fee->unsigned_fits_bits(63) && fwd_fee->to_long() < (long long)fees_c.first) { + fwd_fee = td::RefInt256{true, fees_c.first}; + } + if (fees_c.second && ihr_fee->unsigned_fits_bits(63) && ihr_fee->to_long() < (long long)fees_c.second) { + ihr_fee = td::RefInt256{true, fees_c.second}; + } + + Ref new_msg; + td::RefInt256 fees_collected, fees_total; + unsigned new_msg_bits; + + if (!ext_msg) { + // Process outbound internal message + // check value, check/compute ihr_fees, fwd_fees + // ... + if (!block::tlb::t_CurrencyCollection.validate_csr(info.value)) { + LOG(DEBUG) << "invalid value:CurrencyCollection in proposed outbound message"; + return skip_invalid ? 0 : 37; + } + if (info.ihr_disabled) { + // if IHR is disabled, IHR fees will be always zero + ihr_fee = td::RefInt256{true, 0}; + } + // extract value to be carried by the message + block::CurrencyCollection req; + CHECK(req.unpack(info.value)); + CHECK(req.grams.not_null()); + + if (act_rec.mode & 0x80) { + // attach all remaining balance to this message + req = ap.remaining_balance; + act_rec.mode &= ~1; // pay fees from attached value + } else if (act_rec.mode & 0x40) { + // attach all remaining balance of the inbound message (in addition to the original value) + req += msg_balance_remaining; + if (!(act_rec.mode & 1) && compute_phase) { + req -= compute_phase->gas_fees; + if (!req.is_valid()) { + LOG(DEBUG) + << "not enough value to transfer with the message: all of the inbound message value has been consumed"; + return skip_invalid ? 0 : 37; + } + } + } + + // compute req_grams + fees + td::RefInt256 req_grams_brutto = req.grams; + fees_total = fwd_fee + ihr_fee; + if (act_rec.mode & 1) { + // we are going to pay the fees + req_grams_brutto += fees_total; + } else if (req.grams < fees_total) { + // receiver pays the fees (but cannot) + LOG(DEBUG) << "not enough value attached to the message to pay forwarding fees : have " << req.grams << ", need " + << fees_total; + return skip_invalid ? 0 : 37; // not enough grams + } else { + // decrease message value + req.grams -= fees_total; + } + + // check that we have at least the required value + if (ap.remaining_balance.grams < req_grams_brutto) { + LOG(DEBUG) << "not enough grams to transfer with the message : remaining balance is " + << ap.remaining_balance.to_str() << ", need " << req_grams_brutto << " (including forwarding fees)"; + return skip_invalid ? 0 : 37; // not enough grams + } + + Ref new_extra; + + if (!block::sub_extra_currency(ap.remaining_balance.extra, req.extra, new_extra)) { + LOG(DEBUG) << "not enough extra currency to send with the message"; + return skip_invalid ? 0 : 38; // not enough (extra) funds + } + + auto fwd_fee_mine = msg_prices.get_first_part(fwd_fee); + auto fwd_fee_remain = fwd_fee - fwd_fee_mine; + + // re-pack message value + CHECK(req.pack_to(info.value)); + vm::CellBuilder cb; + CHECK(block::tlb::t_Grams.store_integer_ref(cb, fwd_fee_remain) && + (info.fwd_fee = load_cell_slice_ref(cb.finalize())).not_null()); + CHECK(block::tlb::t_Grams.store_integer_ref(cb, ihr_fee) && + (info.ihr_fee = load_cell_slice_ref(cb.finalize())).not_null()); + + // serialize message + CHECK(tlb::pack(cb, info)); + if (!cb.append_cellslice_bool(cs2)) { + LOG(DEBUG) << "outbound message does not fit into a cell after rewriting"; + return 39; + } + + new_msg_bits = cb.size(); + new_msg = cb.finalize(); + + // clear msg_balance_remaining if it has been used + if (act_rec.mode & 0xc0) { + msg_balance_remaining.set_zero(); + } + + // update balance + ap.remaining_balance -= req_grams_brutto; + ap.remaining_balance.extra = std::move(new_extra); + CHECK(ap.remaining_balance.is_valid()); + CHECK(ap.remaining_balance.grams->sgn() >= 0); + fees_total = fwd_fee + ihr_fee; + fees_collected = fwd_fee_mine; + } else { + // external messages also have forwarding fees + if (ap.remaining_balance.grams < fwd_fee) { + LOG(DEBUG) << "not enough funds to pay for an outbound external message"; + return skip_invalid ? 0 : 37; // not enough grams + } + // repack message + // ext_out_msg_info$11 constructor of CommonMsgInfo + block::gen::CommonMsgInfo::Record_ext_out_msg_info erec; + erec.src = info.src; + erec.dest = info.dest; + erec.created_at = info.created_at; + erec.created_lt = info.created_lt; + vm::CellBuilder cb; + CHECK(tlb::pack(cb, erec)); + if (!cb.append_cellslice_bool(cs2)) { + LOG(DEBUG) << "outbound message does not fit into a cell after rewriting"; + return 39; + } + + new_msg_bits = cb.size(); + new_msg = cb.finalize(); + + // update balance + ap.remaining_balance -= fwd_fee; + CHECK(ap.remaining_balance.is_valid()); + CHECK(td::sgn(ap.remaining_balance.grams) >= 0); + fees_collected = fees_total = fwd_fee; + } + + if (!block::tlb::t_Message.validate_ref(new_msg)) { + LOG(ERROR) << "generated outbound message is not a valid (Message Any) according to hand-written check"; + return -1; + } + if (!block::gen::t_Message_Any.validate_ref(new_msg)) { + LOG(ERROR) << "generated outbound message is not a valid (Message Any) according to automated check"; + return -1; + } + if (verbosity > 2) { + std::cerr << "converted outbound message: "; + block::gen::t_Message_Any.print_ref(std::cerr, new_msg); + } + + ap.msgs_created++; + ap.end_lt++; + + ap.out_msgs.push_back(std::move(new_msg)); + ap.total_action_fees += fees_collected; + ap.total_fwd_fees += fees_total; + + if (act_rec.mode & 0x80) { + CHECK(ap.remaining_balance.is_zero()); + ap.acc_delete_req = ap.reserved_balance.is_zero(); + } + + ap.tot_msg_bits += sstat.bits + new_msg_bits; + ap.tot_msg_cells += sstat.cells + 1; + + return 0; +} + +int Transaction::try_action_reserve_currency(vm::CellSlice& cs, ActionPhase& ap, const ActionPhaseConfig& cfg) { + block::gen::OutAction::Record_action_reserve_currency rec; + if (!tlb::unpack_exact(cs, rec) || (rec.mode & ~3)) { + return -1; + } + int mode = rec.mode; + LOG(INFO) << "in try_action_reserve_currency(" << mode << ")"; + CurrencyCollection reserve, newc; + if (!reserve.validate_unpack(std::move(rec.currency))) { + LOG(DEBUG) << "cannot parse currency field in action_reserve_currency"; + return -1; + } + LOG(DEBUG) << "action_reserve_currency: mode=" << mode << ", reserve=" << reserve.to_str() + << ", balance=" << ap.remaining_balance.to_str(); + if (reserve.grams > ap.remaining_balance.grams) { + if (mode & 2) { + reserve.grams = ap.remaining_balance.grams; + } else { + LOG(DEBUG) << "cannot reserve " << reserve.grams << " nanograms : only " << ap.remaining_balance.grams + << " available"; + return 37; // not enough grams + } + } + if (!block::sub_extra_currency(ap.remaining_balance.extra, reserve.extra, newc.extra)) { + LOG(DEBUG) << "not enough extra currency to reserve"; + if (mode & 2) { + // TODO: process (mode & 2) correctly by setting res_extra := inf (reserve.extra, ap.remaining_balance.extra) + } + return 38; // not enough (extra) funds + } + newc.grams = ap.remaining_balance.grams - reserve.grams; + if (mode & 1) { + // leave only res_grams, reserve everything else + std::swap(newc, reserve); + } + // set remaining_balance to new_grams and new_extra + ap.remaining_balance = std::move(newc); + // increase reserved_balance by res_grams and res_extra + ap.reserved_balance += std::move(reserve); + CHECK(ap.reserved_balance.is_valid()); + CHECK(ap.remaining_balance.is_valid()); + LOG(INFO) << "changed remaining balance to " << ap.remaining_balance.to_str() << ", reserved balance to " + << ap.reserved_balance.to_str(); + ap.spec_actions++; + return 0; +} + +bool Transaction::prepare_bounce_phase(const ActionPhaseConfig& cfg) { + if (in_msg.is_null() || !bounce_enabled) { + return false; + } + bounce_phase = std::make_unique(); + BouncePhase& bp = *bounce_phase; + block::gen::CommonMsgInfo::Record_int_msg_info info; + if (!tlb::unpack_cell_inexact(in_msg, info)) { + bounce_phase.reset(); + return false; + } + info.ihr_disabled = true; + info.bounce = false; + info.bounced = true; + std::swap(info.src, info.dest); + bool to_mc = false; + if (!check_rewrite_dest_addr(info.dest, cfg, &to_mc)) { + LOG(DEBUG) << "invalid destination address in a bounced message"; + bounce_phase.reset(); + return false; + } + // fetch message pricing info + const MsgPrices& msg_prices = cfg.fetch_msg_prices(to_mc || account.is_masterchain()); + // compute size of message + vm::CellStorageStat sstat; // for message size + // preliminary storage estimation of the resulting message + sstat.compute_used_storage(info.value->prefetch_ref()); + bp.msg_bits = sstat.bits; + bp.msg_cells = sstat.cells; + // compute forwarding fees + bp.fwd_fees = msg_prices.compute_fwd_fees(sstat.cells, sstat.bits); + // check whether the message has enough funds + auto msg_balance = msg_balance_remaining; + if (compute_phase && compute_phase->gas_fees.not_null()) { + msg_balance.grams -= compute_phase->gas_fees; + } + if ((msg_balance.grams < 0) || + (msg_balance.grams->signed_fits_bits(64) && msg_balance.grams->to_long() < (long long)bp.fwd_fees)) { + // not enough funds + bp.nofunds = true; + return true; + } + // debit msg_balance_remaining from account's (tentative) balance + balance -= msg_balance; + CHECK(balance.is_valid()); + // debit total forwarding fees from the message's balance, then split forwarding fees into our part and remaining part + msg_balance -= td::RefInt256{true, bp.fwd_fees}; + bp.fwd_fees_collected = msg_prices.get_first_part(bp.fwd_fees); + bp.fwd_fees -= bp.fwd_fees_collected; + total_fees += td::RefInt256{true, bp.fwd_fees_collected}; + // serialize outbound message + info.created_lt = end_lt++; + info.created_at = now; + vm::CellBuilder cb; + CHECK(cb.store_long_bool(5, 4) // int_msg_info$0 ihr_disabled:Bool bounce:Bool bounced:Bool + && cb.append_cellslice_bool(info.src) // src:MsgAddressInt + && cb.append_cellslice_bool(info.dest) // dest:MsgAddressInt + && msg_balance.store(cb) // value:CurrencyCollection + && block::tlb::t_Grams.store_long(cb, 0) // ihr_fee:Grams + && block::tlb::t_Grams.store_long(cb, bp.fwd_fees) // fwd_fee:Grams + && cb.store_long_bool(info.created_lt, 64) // created_lt:uint64 + && cb.store_long_bool(info.created_at, 32) // created_at:uint32 + && cb.store_long_bool(0, 2) // init:(Maybe ...) state:(Either ..) + && cb.finalize_to(bp.out_msg)); + if (verbosity > 2) { + std::cerr << "generated bounced message: "; + block::gen::t_Message_Any.print_ref(std::cerr, bp.out_msg); + } + out_msgs.push_back(bp.out_msg); + bp.ok = true; + return true; +} + +/* + * + * SERIALIZE PREPARED TRANSACTION + * + */ + +bool Account::store_acc_status(vm::CellBuilder& cb, int acc_status) const { + int v; + switch (acc_status) { + case acc_nonexist: + case acc_deleted: + v = 3; // acc_state_nonexist$11 + break; + case acc_uninit: + v = 0; // acc_state_uninit$00 + break; + case acc_frozen: + v = 1; // acc_state_frozen$01 + break; + case acc_active: + v = 2; // acc_state_active$10 + break; + default: + return false; + } + return cb.store_long_bool(v, 2); +} + +bool Transaction::compute_state() { + if (new_total_state.not_null()) { + return true; + } + if (acc_status == Account::acc_uninit && !was_activated && balance.is_zero()) { + LOG(DEBUG) << "account is uninitialized and has zero balance, deleting it back"; + acc_status = Account::acc_nonexist; + was_created = false; + } + if (acc_status == Account::acc_nonexist || acc_status == Account::acc_deleted) { + CHECK(balance.is_zero()); + vm::CellBuilder cb; + CHECK(cb.store_long_bool(0, 1) // account_none$0 + && cb.finalize_to(new_total_state)); + return true; + } + vm::CellBuilder cb; + CHECK(cb.store_long_bool(end_lt, 64) // account_storage$_ last_trans_lt:uint64 + && balance.store(cb)); // balance:CurrencyCollection + int ticktock = new_tick * 2 + new_tock; + unsigned si_pos = 0; + if (acc_status == Account::acc_uninit) { + CHECK(cb.store_long_bool(0, 2)); // account_uninit$00 = AccountState + } else if (acc_status == Account::acc_frozen) { + if (was_frozen) { + vm::CellBuilder cb2; + CHECK(account.split_depth_ ? cb2.store_long_bool(account.split_depth_ + 32, 6) // _ ... = StateInit + : cb2.store_long_bool(0, 1)); // ... split_depth:(Maybe (## 5)) + CHECK(ticktock ? cb2.store_long_bool(ticktock | 4, 3) : cb2.store_long_bool(0, 1)); // special:(Maybe TickTock) + CHECK(cb2.store_maybe_ref(new_code) && cb2.store_maybe_ref(new_data) && cb2.store_maybe_ref(new_library)); + // code:(Maybe ^Cell) data:(Maybe ^Cell) library:(HashmapE 256 SimpleLib) + auto frozen_state = cb2.finalize(); + frozen_hash = frozen_state->get_hash().bits(); + if (verbosity >= 3 * 0) { // !!!DEBUG!!! + std::cerr << "freezing state of smart contract: "; + block::gen::t_StateInit.print_ref(std::cerr, frozen_state); + CHECK(block::gen::t_StateInit.validate_ref(frozen_state)); + CHECK(block::tlb::t_StateInit.validate_ref(frozen_state)); + std::cerr << "with hash " << frozen_hash.to_hex() << std::endl; + } + } + new_code.clear(); + new_data.clear(); + new_library.clear(); + if (frozen_hash == account.addr_orig) { + // if frozen_hash equals account's "original" address (before rewriting), do not need storing hash + CHECK(cb.store_long_bool(0, 2)); // account_uninit$00 = AccountState + } else { + CHECK(cb.store_long_bool(1, 2) // account_frozen$01 + && cb.store_bits_bool(frozen_hash)); // state_hash:bits256 + } + } else { + CHECK(acc_status == Account::acc_active && !was_frozen && !was_deleted); + si_pos = cb.size_ext() + 1; + CHECK(account.split_depth_ ? cb.store_long_bool(account.split_depth_ + 96, 7) // account_active$1 _:StateInit + : cb.store_long_bool(2, 2)); // ... split_depth:(Maybe (## 5)) + CHECK(ticktock ? cb.store_long_bool(ticktock | 4, 3) : cb.store_long_bool(0, 1)); // special:(Maybe TickTock) + CHECK(cb.store_maybe_ref(new_code) && cb.store_maybe_ref(new_data) && cb.store_maybe_ref(new_library)); + // code:(Maybe ^Cell) data:(Maybe ^Cell) library:(HashmapE 256 SimpleLib) + } + auto storage = cb.finalize(); + if (si_pos) { + auto cs_ref = load_cell_slice_ref(storage); + CHECK(cs_ref.unique_write().skip_ext(si_pos)); + new_inner_state = std::move(cs_ref); + } else { + new_inner_state.clear(); + } + vm::CellStorageStat& stats = new_storage_stat; + CHECK(stats.compute_used_storage(Ref(storage))); + CHECK(cb.store_long_bool(1, 1) // account$1 + && cb.append_cellslice_bool(account.my_addr) // addr:MsgAddressInt + && block::store_UInt7(cb, stats.cells) // storage_used$_ cells:(VarUInteger 7) + && block::store_UInt7(cb, stats.bits) // bits:(VarUInteger 7) + && block::store_UInt7(cb, stats.public_cells) // public_cells:(VarUInteger 7) + && cb.store_long_bool(last_paid, 32)); // last_paid:uint32 + if (due_payment.not_null() && td::sgn(due_payment) != 0) { + CHECK(cb.store_long_bool(1, 1) && block::tlb::t_Grams.store_integer_ref(cb, due_payment)); + // due_payment:(Maybe Grams) + } else { + CHECK(cb.store_long_bool(0, 1)); + } + CHECK(cb.append_data_cell_bool(std::move(storage))); + new_total_state = cb.finalize(); + if (verbosity > 2) { + std::cerr << "new account state: "; + block::gen::t_Account.print_ref(std::cerr, new_total_state); + } + CHECK(block::gen::t_Account.validate_ref(new_total_state)); + CHECK(block::tlb::t_Account.validate_ref(new_total_state)); + return true; +} + +bool Transaction::serialize() { + if (root.not_null()) { + return true; + } + if (!compute_state()) { + return false; + } + vm::Dictionary dict{15}; + for (unsigned i = 0; i < out_msgs.size(); i++) { + td::BitArray<15> key{i}; + if (!dict.set_ref(key, out_msgs[i], vm::Dictionary::SetMode::Add)) { + return false; + } + } + vm::CellBuilder cb, cb2; + if (!(cb.store_long_bool(7, 4) // transaction$0111 + && cb.store_bits_bool(account.addr) // account_addr:bits256 + && cb.store_long_bool(start_lt) // lt:uint64 + && cb.store_bits_bool(account.last_trans_hash_) // prev_trans_hash:bits256 + && cb.store_long_bool(account.last_trans_lt_, 64) // prev_trans_lt:uint64 + && cb.store_long_bool(account.now_, 32) // now:uint32 + && cb.store_ulong_rchk_bool(out_msgs.size(), 15) // outmsg_cnt:uint15 + && account.store_acc_status(cb) // orig_status:AccountStatus + && account.store_acc_status(cb, acc_status) // end_status:AccountStatus + && cb2.store_maybe_ref(in_msg) // ^[ in_msg:(Maybe ^(Message Any)) ... + && std::move(dict).append_dict_to_bool(cb2) // out_msgs:(HashmapE 15 ^(Message Any)) + && cb.store_ref_bool(cb2.finalize()) // ] + && total_fees.store(cb) // total_fees:CurrencyCollection + && cb2.store_long_bool(0x72, 8) // update_hashes#72 + && cb2.store_bits_bool(account.total_state->get_hash().bits(), 256) // old_hash:bits256 + && cb2.store_bits_bool(new_total_state->get_hash().bits(), 256) // new_hash:bits256 + && cb.store_ref_bool(cb2.finalize()))) { // state_update:^(HASH_UPDATE Account) + return false; + } + + switch (trans_type) { + case tr_tick: // fallthrough + case tr_tock: { + vm::CellBuilder cb3; + bool act = compute_phase->success; + bool act_ok = act && action_phase->success; + CHECK(cb2.store_long_bool(trans_type == tr_tick ? 2 : 3, 4) // trans_tick_tock$000 is_tock:Bool + && serialize_storage_phase(cb2) // storage:TrStoragePhase + && serialize_compute_phase(cb2) // compute_ph:TrComputePhase + && cb2.store_bool_bool(act) // action:(Maybe + && (!act || (serialize_action_phase(cb3) // ^TrActionPhase) + && cb2.store_ref_bool(cb3.finalize()))) && + cb2.store_bool_bool(!act_ok) // aborted:Bool + && cb2.store_bool_bool(was_deleted) // destroyed:Bool + && cb.store_ref_bool(cb2.finalize()) && cb.finalize_to(root)); + break; + } + case tr_ord: { + vm::CellBuilder cb3; + bool have_storage = (bool)storage_phase; + bool have_credit = (bool)credit_phase; + bool have_bounce = (bool)bounce_phase; + bool act = compute_phase->success; + bool act_ok = act && action_phase->success; + CHECK(cb2.store_long_bool(0, 4) // trans_ord$0000 + && cb2.store_long_bool(!bounce_enabled, 1) // credit_first:Bool + && cb2.store_bool_bool(have_storage) // storage_ph:(Maybe + && (!have_storage || serialize_storage_phase(cb2)) // TrStoragePhase) + && cb2.store_bool_bool(have_credit) // credit_ph:(Maybe + && (!have_credit || serialize_credit_phase(cb2)) // TrCreditPhase) + && serialize_compute_phase(cb2) // compute_ph:TrComputePhase + && cb2.store_bool_bool(act) // action:(Maybe + && (!act || (serialize_action_phase(cb3) && cb2.store_ref_bool(cb3.finalize()))) // ^TrActionPhase) + && cb2.store_bool_bool(!act_ok) // aborted:Bool + && cb2.store_bool_bool(have_bounce) // bounce:(Maybe + && (!have_bounce || serialize_bounce_phase(cb2)) // TrBouncePhase + && cb2.store_bool_bool(was_deleted) // destroyed:Bool + && cb.store_ref_bool(cb2.finalize()) && cb.finalize_to(root)); + break; + } + default: + return false; + } + if (verbosity >= 3 * 1) { + std::cerr << "new transaction: "; + block::gen::t_Transaction.print_ref(std::cerr, root); + vm::load_cell_slice(root).print_rec(std::cerr); + } + + if (!block::gen::t_Transaction.validate_ref(root)) { + LOG(ERROR) << "newly-generated transaction failed to pass automated validation:"; + vm::load_cell_slice(root).print_rec(std::cerr); + block::gen::t_Transaction.print_ref(std::cerr, root); + root.clear(); + return false; + } + if (!block::tlb::t_Transaction.validate_ref(root)) { + LOG(ERROR) << "newly-generated transaction failed to pass hand-written validation:"; + vm::load_cell_slice(root).print_rec(std::cerr); + block::gen::t_Transaction.print_ref(std::cerr, root); + root.clear(); + return false; + } + + return true; +} + +bool Transaction::serialize_storage_phase(vm::CellBuilder& cb) { + if (!storage_phase) { + return false; + } + StoragePhase& sp = *storage_phase; + bool ok; + // tr_phase_storage$_ storage_fees_collected:Grams + if (sp.fees_collected.not_null()) { + ok = block::tlb::t_Grams.store_integer_ref(cb, sp.fees_collected); + } else { + ok = block::tlb::t_Grams.null_value(cb); + } + // storage_fees_due:(Maybe Grams) + ok &= block::store_Maybe_Grams_nz(cb, sp.fees_due); + // status_change:AccStatusChange + if (sp.deleted || sp.frozen) { + ok &= cb.store_long_bool(sp.deleted ? 3 : 2, 2); // acst_frozen$10 acst_deleted$11 + } else { + ok &= cb.store_long_bool(0, 1); // acst_unchanged$0 = AccStatusChange + } + return ok; +} + +bool Transaction::serialize_credit_phase(vm::CellBuilder& cb) { + if (!credit_phase) { + return false; + } + CreditPhase& cp = *credit_phase; + // tr_phase_credit$_ due_fees_collected:(Maybe Grams) credit:CurrencyCollection + return block::store_Maybe_Grams_nz(cb, cp.due_fees_collected) && cp.credit.store(cb); +} + +bool Transaction::serialize_compute_phase(vm::CellBuilder& cb) { + if (!compute_phase) { + return false; + } + ComputePhase& cp = *compute_phase; + switch (cp.skip_reason) { + // tr_compute_phase_skipped$0 reason:ComputeSkipReason; + case ComputePhase::sk_no_state: + return cb.store_long_bool(0, 3); // cskip_no_state$00 = ComputeSkipReason; + case ComputePhase::sk_bad_state: + return cb.store_long_bool(1, 3); // cskip_bad_state$01 = ComputeSkipReason; + case ComputePhase::sk_no_gas: + return cb.store_long_bool(2, 3); // cskip_no_gas$10 = ComputeSkipReason; + case ComputePhase::sk_none: + break; + default: + return false; + } + vm::CellBuilder cb2; + bool ok, credit = (cp.gas_credit != 0), exarg = (cp.exit_arg != 0); + ok = cb.store_long_bool(1, 1) // tr_phase_compute_vm$1 + && cb.store_long_bool(cp.success, 1) // success:Bool + && cb.store_long_bool(cp.msg_state_used, 1) // msg_state_used:Bool + && cb.store_long_bool(cp.account_activated, 1) // account_activated:Bool + && block::tlb::t_Grams.store_integer_ref(cb, cp.gas_fees) // gas_fees:Grams + && block::store_UInt7(cb2, cp.gas_used) // ^[ gas_used:(VarUInteger 7) + && block::store_UInt7(cb2, cp.gas_limit) // gas_limit:(VarUInteger 7) + && cb2.store_long_bool(credit, 1) // gas_credit:(Maybe (VarUInteger 3)) + && (!credit || block::tlb::t_VarUInteger_3.store_long(cb2, cp.gas_credit)) && + cb2.store_long_rchk_bool(cp.mode, 8) // mode:int8 + && cb2.store_long_bool(cp.exit_code, 32) // exit_code:int32 + && cb2.store_long_bool(exarg, 1) // exit_arg:(Maybe int32) + && (!exarg || cb2.store_long_bool(cp.exit_arg, 32)) && + cb2.store_ulong_rchk_bool(cp.vm_steps, 32) // vm_steps:uint32 + && cb2.store_bits_bool(cp.vm_init_state_hash) // vm_init_state_hash:bits256 + && cb2.store_bits_bool(cp.vm_final_state_hash) // vm_final_state_hash:bits256 + && cb.store_ref_bool(cb2.finalize()); // ] = TrComputePhase + return ok; +} + +bool Transaction::serialize_action_phase(vm::CellBuilder& cb) { + if (!action_phase) { + return false; + } + ActionPhase& ap = *action_phase; + bool ok, arg = (ap.result_arg != 0); + ok = cb.store_long_bool(ap.success, 1) // tr_phase_action$_ success:Bool + && cb.store_long_bool(ap.valid, 1) // valid:Bool + && cb.store_long_bool(ap.no_funds, 1) // no_funds:Bool + && cb.store_long_bool(ap.acc_status_change, (ap.acc_status_change >> 1) + 1) // status_change:AccStatusChange + && block::store_Maybe_Grams_nz(cb, ap.total_fwd_fees) // total_fwd_fees:(Maybe Grams) + && block::store_Maybe_Grams_nz(cb, ap.total_action_fees) // total_action_fees:(Maybe Grams) + && cb.store_long_bool(ap.result_code, 32) // result_code:int32 + && cb.store_long_bool(arg, 1) // result_arg:(Maybe + && (!arg || cb.store_long_bool(ap.result_arg, 32)) // uint32) + && cb.store_ulong_rchk_bool(ap.tot_actions, 16) // tot_actions:uint16 + && cb.store_ulong_rchk_bool(ap.spec_actions, 16) // spec_actions:uint16 + && cb.store_ulong_rchk_bool(ap.skipped_actions, 16) // skipped_actions:uint16 + && cb.store_ulong_rchk_bool(ap.msgs_created, 16) // msgs_created:uint16 + && cb.store_bits_bool(ap.action_list_hash) // action_list_hash:bits256 + && block::store_UInt7(cb, ap.tot_msg_cells, ap.tot_msg_bits); // tot_msg_size:StorageUsed + return ok; +} + +bool Transaction::serialize_bounce_phase(vm::CellBuilder& cb) { + if (!bounce_phase) { + return false; + } + BouncePhase& bp = *bounce_phase; + if (!(bp.ok ^ bp.nofunds)) { + return false; + } + if (bp.nofunds) { + return cb.store_long_bool(1, 2) // tr_phase_bounce_nofunds$01 + && block::store_UInt7(cb, bp.msg_cells, bp.msg_bits) // msg_size:StorageUsed + && block::tlb::t_Grams.store_long(cb, bp.fwd_fees); // req_fwd_fees:Grams + } else { + return cb.store_long_bool(1, 1) // tr_phase_bounce_ok$1 + && block::store_UInt7(cb, bp.msg_cells, bp.msg_bits) // msg_size:StorageUsed + && block::tlb::t_Grams.store_long(cb, bp.fwd_fees_collected) // msg_fees:Grams + && block::tlb::t_Grams.store_long(cb, bp.fwd_fees); // fwd_fees:Grams + } +} + +td::Result Transaction::estimate_block_storage_profile_incr( + const vm::NewCellStorageStat& store_stat, const vm::CellUsageTree* usage_tree) const { + if (root.is_null()) { + return td::Status::Error("Cannot estimate the size profile of a transaction before it is serialized"); + } + if (new_total_state.is_null()) { + return td::Status::Error("Cannot estimate the size profile of a transaction before its new state is computed"); + } + return store_stat.tentative_add_proof(new_total_state, usage_tree) + store_stat.tentative_add_cell(root); +} + +bool Transaction::update_block_storage_profile(vm::NewCellStorageStat& store_stat, + const vm::CellUsageTree* usage_tree) const { + if (root.is_null() || new_total_state.is_null()) { + return false; + } + store_stat.add_proof(new_total_state, usage_tree); + store_stat.add_cell(root); + return true; +} + +bool Transaction::would_fit(unsigned cls, const block::BlockLimitStatus& blimst) const { + auto res = estimate_block_storage_profile_incr(blimst.st_stat, blimst.limits.usage_tree); + if (res.is_error()) { + LOG(ERROR) << res.move_as_error(); + return false; + } + auto extra = res.move_as_ok(); + return blimst.would_fit(cls, end_lt, gas_used(), &extra); +} + +bool Transaction::update_limits(block::BlockLimitStatus& blimst) const { + return blimst.update_lt(end_lt) && blimst.update_gas(gas_used()) && blimst.add_proof(new_total_state) && + blimst.add_cell(root) && blimst.add_transaction() && blimst.add_account(is_first); +} + +/* + * + * COMMIT TRANSACTION + * + */ + +Ref Transaction::commit(Account& acc) { + CHECK(account.last_trans_end_lt_ <= start_lt && start_lt < end_lt); + CHECK(root.not_null()); + CHECK(new_total_state.not_null()); + CHECK((const void*)&acc == (const void*)&account); + // export all fields modified by the Transaction into original account + // NB: this is the only method that modifies account + if (orig_addr_rewrite_set && new_split_depth >= 0 && acc.status == Account::acc_nonexist && + acc_status == Account::acc_active) { + LOG(DEBUG) << "setting address rewriting info for newly-activated account " << acc.addr.to_hex() + << " with split_depth=" << new_split_depth + << ", orig_addr_rewrite=" << orig_addr_rewrite.bits().to_hex(new_split_depth); + CHECK(acc.init_rewrite_addr(new_split_depth, orig_addr_rewrite.bits())); + } + acc.status = (acc_status == Account::acc_deleted ? Account::acc_nonexist : acc_status); + acc.last_trans_lt_ = start_lt; + acc.last_trans_end_lt_ = end_lt; + acc.last_trans_hash_ = root->get_hash().bits(); + acc.last_paid = last_paid; + acc.storage_stat = new_storage_stat; + acc.balance = std::move(balance); + acc.due_payment = std::move(due_payment); + acc.total_state = std::move(new_total_state); + acc.inner_state = std::move(new_inner_state); + if (was_frozen) { + acc.state_hash = frozen_hash; + } + acc.my_addr = std::move(my_addr); + // acc.my_addr_exact = std::move(my_addr_exact); + acc.code = std::move(new_code); + acc.data = std::move(new_data); + acc.library = std::move(new_library); + if (acc.status == Account::acc_active) { + acc.tick = new_tick; + acc.tock = new_tock; + } else { + acc.tick = acc.tock = false; + } + end_lt = 0; + acc.push_transaction(root, start_lt); + return root; +} + +LtCellRef Transaction::extract_out_msg(unsigned i) { + return {start_lt + i + 1, std::move(out_msgs.at(i))}; +} + +NewOutMsg Transaction::extract_out_msg_ext(unsigned i) { + return {start_lt + i + 1, std::move(out_msgs.at(i)), root}; +} + +void Transaction::extract_out_msgs(std::vector& list) { + for (unsigned i = 0; i < out_msgs.size(); i++) { + list.emplace_back(start_lt + i + 1, std::move(out_msgs[i])); + } +} + +void Account::push_transaction(Ref trans_root, ton::LogicalTime trans_lt) { + transactions.emplace_back(trans_lt, std::move(trans_root)); +} + +bool Account::create_account_block(vm::CellBuilder& cb) { + if (transactions.empty()) { + return false; + } + if (!(cb.store_long_bool(5, 4) // acc_trans#5 + && cb.store_bits_bool(addr))) { // account_addr:bits256 + return false; + } + vm::AugmentedDictionary dict{64, block::tlb::aug_AccountTransactions}; + for (auto& z : transactions) { + if (!dict.set_ref(td::BitArray<64>{(long long)z.first}, z.second, vm::Dictionary::SetMode::Add)) { + LOG(ERROR) << "error creating the list of transactions for account " << addr.to_hex() + << " : cannot add transaction with lt=" << z.first; + return false; + } + } + Ref dict_root = std::move(dict).extract_root_cell(); + // transactions:(HashmapAug 64 ^Transaction Grams) + if (dict_root.is_null() || !cb.append_cellslice_bool(vm::load_cell_slice(std::move(dict_root)))) { + return false; + } + vm::CellBuilder cb2; + return cb2.store_long_bool(0x72, 8) // update_hashes#72 + && cb2.store_bits_bool(orig_total_state->get_hash().bits(), 256) // old_hash:bits256 + && cb2.store_bits_bool(total_state->get_hash().bits(), 256) // new_hash:bits256 + && cb.store_ref_bool(cb2.finalize()); // state_update:^(HASH_UPDATE Account) +} + +bool Account::libraries_changed() const { + bool s = orig_library.not_null(); + bool t = library.not_null(); + if (s & t) { + return orig_library->get_hash() != library->get_hash(); + } else { + return s != t; + } +} + +} // namespace block diff --git a/crypto/block/transaction.h b/crypto/block/transaction.h new file mode 100644 index 00000000..c06f1525 --- /dev/null +++ b/crypto/block/transaction.h @@ -0,0 +1,401 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once +#include "common/refcnt.hpp" +#include "common/refint.h" +#include "vm/cells.h" +#include "vm/cellslice.h" +#include "vm/dict.h" +#include "vm/boc.h" +#include +#include "tl/tlblib.hpp" +#include "td/utils/bits.h" +#include "ton/ton-types.h" +#include "block/block.h" +#include "block/mc-config.h" + +namespace block { +using td::Ref; +using LtCellRef = std::pair>; + +struct Account; +struct Transaction; + +struct CollatorError { + std::string msg; + CollatorError(std::string _msg) : msg(_msg) { + } + CollatorError(const char* _msg) : msg(_msg) { + } + std::string get_msg() const { + return msg; + } +}; + +static inline bool operator<(const LtCellRef& a, const LtCellRef& b) { + return a.first < b.first; +} + +struct LtCellCompare { + bool operator()(const LtCellRef& a, const LtCellRef& b) { + return a.first < b.first; + } +}; + +struct NewOutMsg { + ton::LogicalTime lt; + Ref msg; + Ref trans; + NewOutMsg(ton::LogicalTime _lt, Ref _msg, Ref _trans) + : lt(_lt), msg(std::move(_msg)), trans(std::move(_trans)) { + } + bool operator<(const NewOutMsg& other) const & { + return lt < other.lt || (lt == other.lt && msg->get_hash() < other.msg->get_hash()); + } + bool operator>(const NewOutMsg& other) const & { + return lt > other.lt || (lt == other.lt && other.msg->get_hash() < msg->get_hash()); + } +}; + +struct StoragePhaseConfig { + const std::vector* pricing{nullptr}; + td::RefInt256 freeze_due_limit; + td::RefInt256 delete_due_limit; + StoragePhaseConfig() = default; + StoragePhaseConfig(const std::vector* _pricing, td::RefInt256 freeze_limit = {}, + td::RefInt256 delete_limit = {}) + : pricing(_pricing), freeze_due_limit(freeze_limit), delete_due_limit(delete_limit) { + } +}; + +struct StoragePhase { + td::RefInt256 fees_collected; + td::RefInt256 fees_due; + ton::UnixTime last_paid_updated; + bool frozen{false}; + bool deleted{false}; + bool is_special{false}; +}; + +struct ComputePhaseConfig { + td::uint64 gas_price; + td::uint64 gas_limit; + td::uint64 gas_credit; + static constexpr td::uint64 gas_infty = (1ULL << 63) - 1; + td::RefInt256 gas_price256; + td::RefInt256 max_gas_threshold; + std::unique_ptr libraries; + Ref global_config; + td::BitArray<256> block_rand_seed; + ComputePhaseConfig(td::uint64 _gas_price = 0, td::uint64 _gas_limit = 0, td::uint64 _gas_credit = 0) + : gas_price(_gas_price), gas_limit(_gas_limit), gas_credit(_gas_credit) { + compute_threshold(); + } + void compute_threshold(); + td::uint64 gas_bought_for(td::RefInt256 nanograms) const; + td::RefInt256 compute_gas_price(td::uint64 gas_used) const; + void set_gas_price(td::uint64 _gas_price) { + gas_price = _gas_price; + compute_threshold(); + } + Ref lookup_library(td::ConstBitPtr key) const; + Ref lookup_library(const td::Bits256& key) const { + return lookup_library(key.bits()); + } + Ref get_lib_root() const { + return libraries ? libraries->get_root_cell() : Ref{}; + } +}; + +// msg_fwd_fees = (lump_price + ceil((bit_price * msg.bits + cell_price * msg.cells)/2^16)) nanograms +// ihr_fwd_fees = ceil((msg_fwd_fees * ihr_price_factor)/2^16) nanograms +// bits in the root cell of a message are not included in msg.bits (lump_price pays for them) + +struct MsgPrices { + td::uint64 lump_price; + td::uint64 bit_price; + td::uint64 cell_price; + td::uint32 ihr_factor; + td::uint32 first_frac; + td::uint32 next_frac; + td::uint64 compute_fwd_fees(td::uint64 cells, td::uint64 bits) const; + std::pair compute_fwd_ihr_fees(td::uint64 cells, td::uint64 bits, + bool ihr_disabled = false) const; + MsgPrices() = default; + MsgPrices(td::uint64 lump, td::uint64 bitp, td::uint64 cellp, td::uint32 ihrf, td::uint32 firstf, td::uint32 nextf) + : lump_price(lump), bit_price(bitp), cell_price(cellp), ihr_factor(ihrf), first_frac(firstf), next_frac(nextf) { + } + td::RefInt256 get_first_part(td::RefInt256 total) const; + td::uint64 get_first_part(td::uint64 total) const; + td::RefInt256 get_next_part(td::RefInt256 total) const; +}; + +struct ActionPhaseConfig { + int max_actions{255}; + MsgPrices fwd_std; + MsgPrices fwd_mc; // from/to masterchain + const WorkchainSet* workchains{nullptr}; + const MsgPrices& fetch_msg_prices(bool is_masterchain) const { + return is_masterchain ? fwd_mc : fwd_std; + } +}; + +struct CreditPhase { + td::RefInt256 due_fees_collected; + block::CurrencyCollection credit; + // td::RefInt256 credit; + // Ref credit_extra; +}; + +struct ComputePhase { + enum { sk_none, sk_no_state, sk_bad_state, sk_no_gas }; + int skip_reason{sk_none}; + bool success{false}; + bool msg_state_used{false}; + bool account_activated{false}; + bool out_of_gas{false}; + bool accepted{false}; + td::RefInt256 gas_fees; + td::uint64 gas_used, gas_max, gas_limit, gas_credit; + int mode; + int exit_code; + int exit_arg; + int vm_steps; + ton::Bits256 vm_init_state_hash, vm_final_state_hash; + Ref in_msg; + Ref new_data; + Ref actions; +}; + +struct ActionPhase { + bool success{false}; + bool valid{false}; + bool no_funds{false}; + bool code_changed{false}; + bool action_list_invalid{false}; + bool acc_delete_req{false}; + enum { acst_unchanged = 0, acst_frozen = 2, acst_deleted = 3 }; + int acc_status_change{acst_unchanged}; + td::RefInt256 total_fwd_fees; // all fees debited from the account + td::RefInt256 total_action_fees; // fees credited to validators in this action phase + int result_code; + int result_arg; + int tot_actions; + int spec_actions; + int skipped_actions; + int msgs_created; + Ref new_code; + td::BitArray<256> action_list_hash; + block::CurrencyCollection remaining_balance, reserved_balance; + // td::RefInt256 remaining_balance; + // Ref remaining_extra; + // td::RefInt256 reserved_balance; + // Ref reserved_extra; + std::vector> action_list; // processed in reverse order + std::vector> out_msgs; + ton::LogicalTime end_lt; + unsigned long long tot_msg_bits{0}, tot_msg_cells{0}; +}; + +struct BouncePhase { + bool ok{false}; + bool nofunds{false}; + unsigned long long msg_bits, msg_cells; + unsigned long long fwd_fees, fwd_fees_collected; + Ref out_msg; +}; + +struct Account { + enum { acc_nonexist = 0, acc_uninit = 1, acc_frozen = 2, acc_active = 3, acc_deleted = 4 }; + int status{acc_nonexist}, orig_status{acc_nonexist}; + bool is_special{false}; + bool tick{false}; + bool tock{false}; + bool created{false}; + bool split_depth_set_{false}; + unsigned char split_depth_{0}; + int verbosity{3 * 0}; + ton::UnixTime now_{0}; + ton::WorkchainId workchain{ton::workchainInvalid}; + td::BitArray<32> addr_rewrite; // rewrite (anycast) data, split_depth bits + ton::StdSmcAddress addr; // rewritten address (by replacing a prefix of `addr_orig` with `addr_rewrite`) + ton::StdSmcAddress addr_orig; // address indicated in smart-contract data + Ref my_addr; // address as stored in the smart contract (MsgAddressInt) + Ref my_addr_exact; // exact address without anycast info + ton::LogicalTime last_trans_end_lt_; + ton::LogicalTime last_trans_lt_; + ton::Bits256 last_trans_hash_; + ton::LogicalTime block_lt; + ton::UnixTime last_paid; + vm::CellStorageStat storage_stat; + block::CurrencyCollection balance; + // td::RefInt256 balance; + // Ref extra_balance; + td::RefInt256 due_payment; + Ref orig_total_state; // ^Account + Ref total_state; // ^Account + Ref inner_state; // StateInit + ton::Bits256 state_hash; // hash of StateInit for frozen accounts + Ref code, data, library, orig_library; + std::vector transactions; + Account() = default; + Account(ton::WorkchainId wc, td::ConstBitPtr _addr) : workchain(wc), addr(_addr) { + } + Account(ton::WorkchainId wc, td::ConstBitPtr _addr, int depth) + : split_depth_set_(true), split_depth_((unsigned char)depth), workchain(wc), addr(_addr) { + } + block::CurrencyCollection get_balance() const { + return balance; + } + bool set_address(ton::WorkchainId wc, td::ConstBitPtr new_addr); + bool unpack(Ref account, Ref extra, ton::UnixTime now, bool special = false); + bool init_new(ton::UnixTime now); + bool recompute_tmp_addr(Ref& tmp_addr, int split_depth, td::ConstBitPtr orig_addr_rewrite) const; + td::RefInt256 compute_storage_fees(ton::UnixTime now, const std::vector& pricing) const; + bool is_masterchain() const { + return workchain == ton::masterchainId; + } + bool belongs_to_shard(ton::ShardIdFull shard) const; + bool store_acc_status(vm::CellBuilder& cb, int status) const; + bool store_acc_status(vm::CellBuilder& cb) const { + return store_acc_status(cb, status); + } + void push_transaction(Ref trans_root, ton::LogicalTime trans_lt); + bool libraries_changed() const; + bool create_account_block(vm::CellBuilder& cb); // stores an AccountBlock with all transactions + + protected: + friend struct Transaction; + bool set_split_depth(int split_depth); + bool check_split_depth(int split_depth) const; + bool init_rewrite_addr(int split_depth, td::ConstBitPtr orig_addr_rewrite); + + private: + bool unpack_address(vm::CellSlice& addr_cs); + bool unpack_storage_info(vm::CellSlice& cs); + bool unpack_state(vm::CellSlice& cs); + bool parse_maybe_anycast(vm::CellSlice& cs); + bool store_maybe_anycast(vm::CellBuilder& cb) const; + bool compute_my_addr(bool force = false); +}; + +struct Transaction { + static constexpr unsigned max_msg_bits = (1 << 21), max_msg_cells = (1 << 13); + enum { + tr_none, + tr_ord, + tr_storage, + tr_tick, + tr_tock, + tr_split_prepare, + tr_split_install, + tr_merge_prepare, + tr_merge_install + }; + int trans_type{tr_none}; + bool was_deleted{false}; + bool was_frozen{false}; + bool was_activated{false}; + bool was_created{false}; + bool bounce_enabled{false}; + bool in_msg_extern{false}; + bool use_msg_state{false}; + bool is_first{false}; + bool orig_addr_rewrite_set{false}; + bool new_tick; + bool new_tock; + signed char new_split_depth{-1}; + ton::UnixTime now; + int acc_status; + int verbosity{3 * 0}; + int in_msg_type{0}; + const Account& account; // only `commit` method modifies the account + Ref my_addr, my_addr_exact; // almost the same as in account.* + ton::LogicalTime start_lt, end_lt; + block::CurrencyCollection balance; + block::CurrencyCollection msg_balance_remaining; + td::RefInt256 due_payment; + td::RefInt256 in_fwd_fee, msg_fwd_fees; + block::CurrencyCollection total_fees{0}; + ton::UnixTime last_paid; + Ref root; + Ref new_total_state; + Ref new_inner_state; + // Ref extra_balance; + // Ref msg_extra; + Ref new_code, new_data, new_library; + Ref in_msg, in_msg_state; + Ref in_msg_body; + Ref in_msg_library; + td::BitArray<256> frozen_hash; + td::BitArray<32> orig_addr_rewrite; + std::vector> out_msgs; + std::unique_ptr storage_phase; + std::unique_ptr credit_phase; + std::unique_ptr compute_phase; + std::unique_ptr action_phase; + std::unique_ptr bounce_phase; + vm::CellStorageStat new_storage_stat; + Transaction(const Account& _account, int ttype, ton::LogicalTime req_start_lt, ton::UnixTime _now, + Ref _inmsg = {}); + bool unpack_input_msg(bool ihr_delivered, const ActionPhaseConfig* cfg); + bool check_in_msg_state_hash(); + bool prepare_storage_phase(const StoragePhaseConfig& cfg, bool force_collect = true); + bool prepare_credit_phase(); + bool compute_gas_limits(ComputePhase& cp, const ComputePhaseConfig& cfg); + Ref prepare_vm_stack(ComputePhase& cp); + std::vector> compute_vm_libraries(const ComputePhaseConfig& cfg); + bool prepare_compute_phase(const ComputePhaseConfig& cfg); + bool prepare_action_phase(const ActionPhaseConfig& cfg); + bool prepare_bounce_phase(const ActionPhaseConfig& cfg); + bool compute_state(); + bool serialize(); + td::uint64 gas_used() const { + return compute_phase ? compute_phase->gas_used : 0; + } + + td::Result estimate_block_storage_profile_incr( + const vm::NewCellStorageStat& store_stat, const vm::CellUsageTree* usage_tree) const; + bool update_block_storage_profile(vm::NewCellStorageStat& store_stat, const vm::CellUsageTree* usage_tree) const; + bool would_fit(unsigned cls, const block::BlockLimitStatus& blk_lim_st) const; + bool update_limits(block::BlockLimitStatus& blk_lim_st) const; + + Ref commit(Account& _account); // _account should point to the same account + LtCellRef extract_out_msg(unsigned i); + NewOutMsg extract_out_msg_ext(unsigned i); + void extract_out_msgs(std::vector& list); + + private: + Ref prepare_vm_c7(const ComputePhaseConfig& cfg) const; + bool prepare_rand_seed(td::BitArray<256>& rand_seed, const ComputePhaseConfig& cfg) const; + int try_action_set_code(vm::CellSlice& cs, ActionPhase& ap, const ActionPhaseConfig& cfg); + int try_action_send_msg(vm::CellSlice& cs, ActionPhase& ap, const ActionPhaseConfig& cfg); + int try_action_reserve_currency(vm::CellSlice& cs, ActionPhase& ap, const ActionPhaseConfig& cfg); + bool check_replace_src_addr(Ref& src_addr) const; + bool check_rewrite_dest_addr(Ref& dest_addr, const ActionPhaseConfig& cfg, + bool* is_mc = nullptr) const; + bool serialize_storage_phase(vm::CellBuilder& cb); + bool serialize_credit_phase(vm::CellBuilder& cb); + bool serialize_compute_phase(vm::CellBuilder& cb); + bool serialize_action_phase(vm::CellBuilder& cb); + bool serialize_bounce_phase(vm::CellBuilder& cb); + bool unpack_msg_state(bool lib_only = false); +}; + +} // namespace block diff --git a/crypto/common/AtomicRef.h b/crypto/common/AtomicRef.h new file mode 100644 index 00000000..91ad7334 --- /dev/null +++ b/crypto/common/AtomicRef.h @@ -0,0 +1,200 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +#include "td/utils/SpinLock.h" +#include "common/refcnt.hpp" + +#include + +namespace td { +template +class AtomicRefSpinlock { + public: + AtomicRefSpinlock() = default; + AtomicRefSpinlock(Ref&& ref) : ref_(ref.release()) { + } + ~AtomicRefSpinlock() { + Ref(ref_.load(std::memory_order_relaxed), typename Ref::acquire_t{}); + } + AtomicRefSpinlock(AtomicRefSpinlock&&) = delete; + AtomicRefSpinlock& operator=(AtomicRefSpinlock&&) = delete; + AtomicRefSpinlock(const AtomicRefSpinlock&) = delete; + AtomicRefSpinlock& operator=(const AtomicRefSpinlock&) = delete; + + Ref load() const { + auto guard = spin_lock_.lock(); + return Ref(ref_.load(std::memory_order_relaxed)); + } + Ref extract() const { + auto guard = spin_lock_.lock(); + return Ref(ref_.exchange(nullptr, std::memory_order_release), typename Ref::acquire_t{}); + } + + Ref load_unsafe() const { + return Ref(get_unsafe()); + } + const T* get_unsafe() const { + return ref_.load(std::memory_order_acquire); + } + bool store_if_empty(Ref& desired) { + auto guard = spin_lock_.lock(); + if (ref_.load(std::memory_order_relaxed) == nullptr) { + ref_.store(desired.release(), std::memory_order_release); + return true; + } + return false; + } + + void store(Ref&& ref) { + auto guard = spin_lock_.lock(); + Ref(ref_.exchange(ref.release(), std::memory_order_acq_rel), typename Ref::acquire_t{}); + } + + private: + mutable SpinLock spin_lock_; + std::atomic ref_{nullptr}; +}; + +template +class AtomicRefLockfree { + public: + AtomicRefLockfree() = default; + static constexpr int BATCH_SIZE = 100; + AtomicRefLockfree(Ref&& ref) : ptr_(Ptr(ref.release(), BATCH_SIZE)) { + Ref::acquire_shared(ptr_.load(std::memory_order_relaxed).ptr(), BATCH_SIZE); + } + ~AtomicRefLockfree() { + auto ptr = ptr_.load(std::memory_order_relaxed); + if (ptr.ptr()) { + Ref::release_shared(ptr.ptr(), ptr.ref_cnt() + 1); + } + } + AtomicRefLockfree(AtomicRefLockfree&&) = delete; + AtomicRefLockfree& operator=(AtomicRefLockfree&&) = delete; + AtomicRefLockfree(const AtomicRefLockfree&) = delete; + AtomicRefLockfree& operator=(const AtomicRefLockfree&) = delete; + + Ref load() const { + auto ptr = ptr_.load(); + while (ptr.ptr()) { + if (ptr.ref_cnt() == 0) { + td::this_thread::yield(); + ptr = ptr_.load(); + continue; + } + auto new_ptr = Ptr(ptr.ptr(), ptr.ref_cnt() - 1); + if (ptr_.compare_exchange_weak(ptr, new_ptr)) { + if (new_ptr.ref_cnt() < BATCH_SIZE / 2) { + try_reserve(ptr.ptr()); + } + return Ref(ptr.ptr(), typename Ref::acquire_t{}); + } + } + return {}; + } + void try_reserve(T* raw_ptr) const { + int reserve_cnt = BATCH_SIZE; + Ref::acquire_shared(raw_ptr, reserve_cnt); + auto ptr = ptr_.load(); + while (ptr.ptr() == raw_ptr && ptr.ref_cnt() < BATCH_SIZE / 2) { + auto new_ptr = Ptr(ptr.ptr(), ptr.ref_cnt() + reserve_cnt); + if (ptr_.compare_exchange_weak(ptr, new_ptr)) { + return; + } + } + Ref::release_shared(raw_ptr, reserve_cnt); + } + Ref extract() { + auto ptr = ptr_.exchange({}); + if (ptr.ref_cnt() != 0) { + Ref::release_shared(ptr.ptr(), ptr.ref_cnt()); + } + + return Ref(ptr.ptr(), typename Ref::acquire_t{}); + } + + Ref load_unsafe() const { + return load(); + } + T* get_unsafe() const { + return ptr_.load().ptr(); + } + bool store_if_empty(Ref& desired) { + auto raw_ptr = desired.get(); + Ref::acquire_shared(raw_ptr, BATCH_SIZE + 1); + + Ptr new_ptr{const_cast(raw_ptr), BATCH_SIZE}; + auto ptr = ptr_.load(); + while (ptr.ptr() == nullptr) { + if (ptr_.compare_exchange_weak(ptr, new_ptr)) { + return true; + } + } + Ref::release_shared(raw_ptr, BATCH_SIZE + 1); + return false; + } + + void store(Ref&& ref) { + Ptr new_ptr = [&]() -> Ptr { + if (ref.is_null()) { + return {}; + } + auto raw_ptr = ref.release(); + Ref::acquire_shared(raw_ptr, BATCH_SIZE); + return {raw_ptr, BATCH_SIZE}; + }(); + + auto ptr = ptr_.load(); + while (!ptr_.compare_exchange_weak(ptr, new_ptr)) { + } + + if (ptr.ptr()) { + Ref::release_shared(ptr.ptr(), ptr.ref_cnt() + 1); + } + } + + private: + struct Ptr { + public: + Ptr() = default; + Ptr(T* ptr, int ref_cnt) { + data_ = reinterpret_cast(ptr); + CHECK((data_ >> 48) == 0); + data_ |= static_cast(ref_cnt) << 48; + } + T* ptr() const { + return reinterpret_cast(data_ & (std::numeric_limits::max() >> 16)); + } + int ref_cnt() const { + return static_cast(data_ >> 48); + } + + private: + td::uint64 data_{0}; + }; + static_assert(sizeof(Ptr) == 8, "sizeof(Ptr) must be 8 for atomic to work fine"); + static_assert(std::is_trivially_copyable::value, "Ptr must be tribially copyable"); + + mutable std::atomic ptr_{Ptr()}; +}; + +template +using AtomicRef = AtomicRefLockfree; +} // namespace td diff --git a/crypto/common/bigint.cpp b/crypto/common/bigint.cpp new file mode 100644 index 00000000..3229e681 --- /dev/null +++ b/crypto/common/bigint.cpp @@ -0,0 +1,41 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "common/bigint.hpp" + +namespace td { + +template class AnyIntView; +template class BigIntG<257, BigIntInfo>; + +namespace literals { +BigInt256 operator""_i256(const char* str, std::size_t str_len) { + BigInt256 x; + x.enforce(x.parse_dec(str, (int)str_len) == (int)str_len); + return x; +} + +BigInt256 operator""_x256(const char* str, std::size_t str_len) { + BigInt256 x; + x.enforce(x.parse_hex(str, (int)str_len) == (int)str_len); + return x; +} + +} // namespace literals + +} // namespace td diff --git a/crypto/common/bigint.hpp b/crypto/common/bigint.hpp new file mode 100644 index 00000000..63bfe464 --- /dev/null +++ b/crypto/common/bigint.hpp @@ -0,0 +1,2525 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once +#include +#include +#include +#include +#include +#include +#include +#include +#include "common/bitstring.h" + +#include "td/utils/bits.h" +#include "td/utils/Span.h" +#include "td/utils/uint128.h" + +/************************************** + * + * BIGINT256 + * + ************************************** +**/ + +namespace td { + +struct BigIntInfo { + typedef long long word_t; + typedef unsigned long long uword_t; + enum { word_bits = 64, word_shift = 52, words_for_256bit = 1 + 256 / word_shift, max_pow10_exp = 18 }; + static const word_t Base = (1LL << word_shift); + static const word_t Half = (1LL << (word_shift - 1)); + static const word_t MaxDenorm = (1LL << (word_bits - word_shift - 1)); + static const word_t max_pow10 = 1000000000000000000LL; + static constexpr double InvBase = 1.0 / (double)Base; + + static void set_mul(word_t* hi, word_t* lo, word_t x, word_t y); + static void add_mul(word_t* hi, word_t* lo, word_t x, word_t y); + static void sub_mul(word_t* hi, word_t* lo, word_t x, word_t y); + static void dbl_divmod(word_t* quot, word_t* rem, word_t hi, word_t lo, word_t y); +}; + +inline void BigIntInfo::set_mul(word_t* hi, word_t* lo, word_t x, word_t y) { + auto z = uint128::from_signed(x).mult_signed(y); + *lo = static_cast(z.lo()) & (Base - 1); + *hi = static_cast(z.shr(word_shift).lo()); +} + +inline void BigIntInfo::add_mul(word_t* hi, word_t* lo, word_t x, word_t y) { + auto z = uint128::from_signed(x).mult_signed(y); + *lo += static_cast(z.lo()) & (Base - 1); + *hi += static_cast(z.shr(word_shift).lo()); +} + +inline void BigIntInfo::sub_mul(word_t* hi, word_t* lo, word_t x, word_t y) { + auto z = uint128::from_signed(x).mult_signed(y); + *lo -= static_cast(z.lo()) & (Base - 1); + *hi -= static_cast(z.shr(word_shift).lo()); +} + +inline void BigIntInfo::dbl_divmod(word_t* quot, word_t* rem, word_t hi, word_t lo, word_t y) { + auto x = uint128::from_signed(hi).shl(word_shift).add(uint128::from_signed(lo)); + int64 a, b; + x.divmod_signed(y, &a, &b); + *quot = a; + *rem = b; +} + +namespace { + +template +struct LogOpAnd { + static T op(T x, T y) { + return x & y; + } + static const T neutral = -1; + static const T zero = 0; + static const bool has_zero = true; +}; + +template +struct LogOpOr { + static T op(T x, T y) { + return x | y; + } + static const T neutral = 0; + static const T zero = -1; + static const bool has_zero = true; +}; + +template +struct LogOpXor { + static T op(T x, T y) { + return x ^ y; + } + static const T neutral = 0; + static const T zero = -1; + static const bool has_zero = false; +}; + +template +struct TransformId { + static T eval(T x) { + return x; + } +}; + +template +struct TransformNegate { + static T eval(T x) { + return -x; + } +}; + +template +struct TransformMul { + static T eval(T x) { + return x * factor; + } +}; + +} // namespace + +struct IntOverflow {}; + +template +class PropagateConstSpan { + public: + PropagateConstSpan() = default; + PropagateConstSpan(T* ptr, size_t size) : ptr_(ptr), size_(size) { + } + + T& operator[](size_t i) { + DCHECK(i < size_); + return ptr_[i]; + } + const T& operator[](size_t i) const { + DCHECK(i < size_); + return ptr_[i]; + } + T* data() { + return ptr_; + } + const T* data() const { + return ptr_; + } + size_t size() const { + return size_; + } + + private: + T* ptr_{nullptr}; + size_t size_{0}; +}; + +template +class AnyIntView { + public: + enum { word_bits = Tr::word_bits, word_shift = Tr::word_shift }; + typedef typename Tr::word_t word_t; + int& n_; + PropagateConstSpan digits; + + int max_size() const { + return static_cast(digits.size()); + } + int size() const { + return n_; + } + void set_size(int new_size) { + n_ = new_size; + } + int inc_size() { + return n_++; + } + void dec_size() { + n_--; + } + + bool is_valid() const { + return size() > 0; + } + void enforce_valid() const { + enforce(is_valid()); + } + void invalidate() { + set_size(0); + } + bool invalidate_bool() { + set_size(0); + return false; + } + void enforce(bool f) const { + if (!f) { + throw IntOverflow(); + } + } + void operator=(word_t y) { + set_size(1); + digits[0] = y; + } + + bool normalize_bool_any(); + bool set_pow2_any(int exponent); + bool set_any(const AnyIntView& yp); + bool add_pow2_any(int exponent, int factor); + bool add_any(const AnyIntView& yp); + bool sub_any(const AnyIntView& yp); + + template + bool log_op_any(const AnyIntView& yp); + + int cmp_any(const AnyIntView& yp) const; + int cmp_any(word_t y) const; + + template > + int cmp_un_any(const AnyIntView& yp) const; + + void negate_any(); + + int sgn_un_any() const; + bool get_bit_any(unsigned bit) const; + bool eq_any(const AnyIntView& yp) const; + bool eq_any(word_t y) const; + void mul_tiny_any(int y); + int divmod_tiny_any(int y); + word_t divmod_short_any(word_t y); + bool mul_add_short_any(word_t y, word_t z); + bool add_mul_any(const AnyIntView& yp, const AnyIntView& zp); + void add_mul_trunc_any(const AnyIntView& yp, const AnyIntView& zp); + bool mod_div_any(const AnyIntView& yp, AnyIntView& quot, int round_mode); + bool mod_pow2_any(int exponent); + bool mod_pow2_any(int exponent, int round_mode); + bool rshift_any(int exponent, int round_mode = -1); + bool lshift_any(int exponent); + bool unsigned_fits_bits_any(int nbits) const; + bool signed_fits_bits_any(int nbits) const; + int bit_size_any(bool sgnd = true) const; + bool export_bytes_any(unsigned char* buff, std::size_t size, bool sgnd = true) const; + bool export_bytes_lsb_any(unsigned char* buff, std::size_t size, bool sgnd = true) const; + bool import_bytes_any(const unsigned char* buff, std::size_t size, bool sgnd = true); + bool import_bytes_lsb_any(const unsigned char* buff, std::size_t size, bool sgnd = true); + bool export_bits_any(unsigned char* buff, int offs, unsigned bits, bool sgnd = true) const; + bool import_bits_any(const unsigned char* buff, int offs, unsigned bits, bool sgnd = true); + word_t top_word() const { + return digits[size() - 1]; + } + double top_double() const { + return size() > 1 ? (double)digits[size() - 1] + (double)digits[size() - 2] * (1.0 / Tr::Base) + : (double)digits[size() - 1]; + } + word_t to_long_any() const; + int parse_hex_any(const char* str, int str_len, int* frac = nullptr); + int parse_binary_any(const char* str, int str_len, int* frac = nullptr); + std::string to_dec_string_destroy_any(); + std::string to_dec_string_slow_destroy_any(); + std::string to_hex_string_any(bool upcase = false) const; + std::string to_hex_string_slow_destroy_any(); + std::string to_binary_string_any() const; + + int sgn() const { + return is_valid() ? (top_word() > 0 ? 1 : (top_word() < 0 ? -1 : 0)) : 0x80000000; + } +}; + +template +class BigIntG { + public: + enum { word_bits = Tr::word_bits, word_shift = Tr::word_shift, max_bits = len, word_cnt = len / word_shift + 1 }; + typedef typename Tr::word_t word_t; + typedef Tr Traits; + typedef BigIntG DoubleInt; + + AnyIntView as_any_int() { + return AnyIntView{n, PropagateConstSpan(digits, word_cnt)}; + } + + const AnyIntView as_any_int() const { + return AnyIntView{const_cast(n), PropagateConstSpan(const_cast(digits), word_cnt)}; + } + + private: + template + friend class BigIntG; + + int n; + word_t digits[word_cnt]; + + public: + BigIntG() : n(0) { + } + explicit BigIntG(word_t x) : n(1) { + digits[0] = x; + } + BigIntG(const BigIntG& x) : n(x.n) { + std::memcpy(digits, x.digits, n * sizeof(word_t)); + ///std::cout << "(BiCC " << (const void*)&x << "->" << (void*)this << ")"; + } + template + BigIntG(const BigIntG& y) : n(0) { + *this = y; + } + void enforce(bool f) const { + if (!f) { + throw IntOverflow(); + } + } + void ignore(bool f) const { + } + bool is_valid() const { + return n > 0; + } + void enforce_valid() const { + enforce(is_valid()); + } + BigIntG& invalidate() { + n = 0; + return *this; + } + bool invalidate_bool() { + n = 0; + return false; + } + BigIntG& invalidate_unless(bool f) { + if (!f) { + n = 0; + } + return *this; + } + bool normalize_bool() { + return as_any_int().normalize_bool_any(); + } + BigIntG& normalize() { + ignore(normalize_bool()); + return *this; + } + BigIntG& denormalize(); + int sgn() const { + return is_valid() ? (top_word() > 0 ? 1 : (top_word() < 0 ? -1 : 0)) : 0x80000000; + } + int sgn_un() const { + return as_any_int().sgn_un_any(); + } + bool get_bit(unsigned bit) const { + return as_any_int().get_bit_any(bit); + } + BigIntG& negate() { + as_any_int().negate_any(); + return *this; + } + BigIntG& logical_not(); + + BigIntG& operator=(const BigIntG& y) { + n = y.n; + std::memcpy(digits, y.digits, n * sizeof(word_t)); + ///std::cout << "(BiC=)"; + return *this; + } + + template + BigIntG& operator=(const BigIntG& y) { + return invalidate_unless(as_any_int().set_any(y.as_any_int())); + } + + template + BigIntG& operator+=(const BigIntG& y) { + ignore(as_any_int().add_any(y.as_any_int())); + return *this; + } + + template + BigIntG& operator-=(const BigIntG& y) { + ignore(as_any_int().sub_any(y.as_any_int())); + return *this; + } + + template + BigIntG& operator&=(const BigIntG& y) { + ignore(as_any_int().template log_op_any>(y.as_any_int())); + return *this; + } + + template + BigIntG& operator|=(const BigIntG& y) { + ignore(as_any_int().template log_op_any>(y.as_any_int())); + return *this; + } + + template + BigIntG& operator^=(const BigIntG& y) { + ignore(as_any_int().template log_op_any>(y.as_any_int())); + return *this; + } + + BigIntG& operator>>=(int shift) { + as_any_int().rshift_any(shift); + return *this; + } + + BigIntG& operator<<=(int shift) { + as_any_int().lshift_any(shift); + return *this; + } + + BigIntG& rshift(int shift, int round_mode = -1) { + as_any_int().rshift_any(shift, round_mode); + return *this; + } + + template + int cmp(const BigIntG& y) const { + return as_any_int().cmp_any(y.as_any_int()); + } + + template + int cmp_un(const BigIntG& y) const { + return as_any_int().template cmp_un_any>(y.as_any_int()); + } + + template + bool operator==(const BigIntG& y) const { + return as_any_int().eq_any(y.as_any_int()); + } + + template + bool operator!=(const BigIntG& y) const { + return !(*this == y); + } + + int cmp(word_t y) const { + return as_any_int().cmp_any(y); + } + + bool operator==(word_t y) const { + return as_any_int().eq_any(y); + } + + bool operator!=(word_t y) const { + return !(*this == y); + } + bool mul_add_short_bool(word_t y, word_t z) { + return as_any_int().mul_add_short_any(y, z); + } + + template + bool add_mul_bool(const BigIntG& y, const BigIntG& z) { + return as_any_int().add_mul_any(y.as_any_int(), z.as_any_int()); + } + + template + BigIntG& add_mul(const BigIntG& y, const BigIntG& z) { + ignore(add_mul_bool(y, z)); + return *this; + } + + template + void add_mul_trunc(const BigIntG& y, const BigIntG& z) { + as_any_int().add_mul_trunc_any(y.as_any_int(), z.as_any_int()); + } + + BigIntG& mul_short(word_t y) { + return invalidate_unless(mul_add_short_bool(y, 0)); + } + + BigIntG& mul_tiny(int y) { + as_any_int().mul_tiny_any(y); + return *this; + } + + BigIntG& add_tiny(word_t y) { + digits[0] += y; + return *this; + } + + BigIntG& sub_tiny(word_t y) { + digits[0] -= y; + return *this; + } + + BigIntG& mul_short_opt(word_t y) { + if (y <= Tr::MaxDenorm && y >= -Tr::MaxDenorm) { + return mul_tiny(static_cast(y)); + } else { + return mul_short(y); + } + } + + template + bool mod_div_bool(const BigIntG& y, BigIntG& quot, int round_mode = -1) { + auto q = quot.as_any_int(); + return as_any_int().mod_div_any(y.as_any_int(), q, round_mode); + } + + template + BigIntG& mod_div(const BigIntG& y, BigIntG& quot, int round_mode = -1) { + return invalidate_unless(mod_div_bool(y, quot, round_mode)); + } + + int divmod_tiny(int y) { + return as_any_int().divmod_tiny_any(y); + } + + word_t divmod_short(word_t y) { + return as_any_int().divmod_short_any(y); + } + + BigIntG& operator=(word_t y) { + n = 1; + digits[0] = y; + return *this; + } + + BigIntG& set_zero() { + n = 1; + digits[0] = 0; + return *this; + } + + BigIntG& set_pow2(int exponent) { + return invalidate_unless(as_any_int().set_pow2_any(exponent)); + } + + bool set_pow2_bool(int exponent) { + return as_any_int().set_pow2_any(exponent); + } + + BigIntG& mod_pow2(int exponent) { + return invalidate_unless(as_any_int().mod_pow2_any(exponent)); + } + + BigIntG& mod_pow2(int exponent, int round_mode) { + return invalidate_unless(as_any_int().mod_pow2_any(exponent, round_mode)); + } + + BigIntG& add_pow2(int exponent) { + return invalidate_unless(as_any_int().add_pow2_any(exponent, 1)); + } + + BigIntG& sub_pow2(int exponent) { + return invalidate_unless(as_any_int().add_pow2_any(exponent, -1)); + } + + bool unsigned_fits_bits(int nbits) const { + return as_any_int().unsigned_fits_bits_any(nbits); + } + + bool signed_fits_bits(int nbits) const { + return as_any_int().signed_fits_bits_any(nbits); + } + + bool fits_bits(int nbits, bool sgnd = true) const { + return sgnd ? signed_fits_bits(nbits) : unsigned_fits_bits(nbits); + } + + int bit_size(bool sgnd = true) const { + return as_any_int().bit_size_any(sgnd); + } + + bool export_bytes(unsigned char* buff, std::size_t size, bool sgnd = true) const { + return as_any_int().export_bytes_any(buff, size, sgnd); + } + + bool export_bytes_lsb(unsigned char* buff, std::size_t size, bool sgnd = true) const { + return as_any_int().export_bytes_lsb_any(buff, size, sgnd); + } + + bool import_bytes(const unsigned char* buff, std::size_t size, bool sgnd = true) { + return as_any_int().import_bytes_any(buff, size, sgnd); + } + + bool import_bytes_lsb(const unsigned char* buff, std::size_t size, bool sgnd = true) { + return as_any_int().import_bytes_lsb_any(buff, size, sgnd); + } + + bool export_bits(unsigned char* buff, int offs, unsigned bits, bool sgnd = true) const { + return as_any_int().export_bits_any(buff, offs, bits, sgnd); + } + + bool export_bits(td::BitPtr bp, unsigned bits, bool sgnd = true) const { + return as_any_int().export_bits_any(bp.ptr, bp.offs, bits, sgnd); + } + + template + bool export_bits(T& bs, bool sgnd = true) const { + return export_bits(bs.bits(), bs.size(), sgnd); + } + + bool export_bits(const BitSliceWrite& bs, bool sgnd = true) const { + return export_bits(bs.bits(), bs.size(), sgnd); + } + + bool import_bits(const unsigned char* buff, int offs, unsigned bits, bool sgnd = true) { + return as_any_int().import_bits_any(buff, offs, bits, sgnd); + } + + bool import_bits(td::ConstBitPtr bp, unsigned bits, bool sgnd = true) { + return as_any_int().import_bits_any(bp.ptr, bp.offs, bits, sgnd); + } + + template + bool import_bits(const T& bs, bool sgnd = true) { + return import_bits(bs.bits(), bs.size(), sgnd); + } + + std::ostream& dump(std::ostream& os, bool nl = true) const; + std::string dump() const; + int parse_dec(const char* str, int str_len, int* frac = nullptr); + int parse_dec(const std::string str, int* frac = nullptr) { + return parse_dec(str.c_str(), (int)str.size(), frac); + } + int parse_dec_slow(const char* str, int str_len); + int parse_dec_slow(const std::string str) { + return parse_dec_slow(str.c_str(), (int)str.size()); + } + int parse_hex(const char* str, int str_len, int* frac = nullptr) { + return as_any_int().parse_hex_any(str, str_len, frac); + } + int parse_hex(const std::string str, int* frac = nullptr) { + return parse_hex(str.c_str(), (int)str.size(), frac); + } + int parse_binary(const char* str, int str_len, int* frac = nullptr) { + return as_any_int().parse_binary_any(str, str_len, frac); + } + int parse_binary(const std::string str, int* frac = nullptr) { + return parse_binary(str.c_str(), (int)str.size(), frac); + } + std::string to_dec_string() const; + std::string to_dec_string_destroy(); + std::string to_dec_string_slow() const; + std::string to_hex_string_slow() const; + std::string to_hex_string(bool upcase = false) const; + std::string to_binary_string() const; + double to_double() const { + return is_valid() ? ldexp(top_double(), (n - 1) * word_shift) : NAN; + } + word_t to_long() const { + return as_any_int().to_long_any(); + } + + private: + word_t top_word() const { + return digits[n - 1]; + } + double top_double() const { + return n > 1 ? (double)digits[n - 1] + (double)digits[n - 2] * (1.0 / Tr::Base) : (double)digits[n - 1]; + } +}; + +template +bool AnyIntView::normalize_bool_any() { + word_t val = 0; + int i; + if (!is_valid()) { + return false; + } + for (i = 0; i < size() && digits[i] < Tr::Half && digits[i] >= -Tr::Half; i++) { + } + for (; i < size(); i++) { + val += digits[i] + Tr::Half; + digits[i] = (val & (Tr::Base - 1)) - Tr::Half; + val >>= word_shift; + } + if (val) { + do { + if (size() == max_size()) { + return invalidate_bool(); + } + val += Tr::Half; + digits[inc_size()] = (val & (Tr::Base - 1)) - Tr::Half; + val >>= word_shift; + } while (val); + } + while (size() > 1 && !digits[size() - 1]) { + dec_size(); + } + return true; +} + +template +bool AnyIntView::set_pow2_any(int exponent) { + if (exponent < 0 || exponent >= max_size() * word_shift) { + invalidate(); + return false; + } + auto dm = std::div(exponent, word_shift); + int k = dm.quot; + std::memset(digits.data(), 0, k * sizeof(word_t)); + if (dm.rem == word_shift - 1 && k + 1 < max_size()) { + digits[k] = -Tr::Half; + digits[k + 1] = 1; + set_size(k + 2); + return true; + } + digits[k] = ((word_t)1 << dm.rem); + set_size(k + 1); + return true; +} + +template +bool AnyIntView::set_any(const AnyIntView& yp) { + if (yp.size() <= max_size()) { + set_size(yp.size()); + std::memcpy(digits.data(), yp.digits.data(), size() * sizeof(word_t)); + return true; + } else { + set_size(max_size()); + std::memcpy(digits.data(), yp.digits.data(), size() * sizeof(word_t)); + return false; + } +} + +template +bool AnyIntView::add_pow2_any(int exponent, int factor) { + if (exponent < 0 || exponent >= max_size() * word_shift) { + invalidate(); + return false; + } + if (!is_valid()) { + return false; + } + auto dm = std::div(exponent, word_shift); + int k = dm.quot; + while (size() <= k) { + digits[inc_size()] = 0; + } + digits[k] += (factor << dm.rem); + return true; +} + +template +bool AnyIntView::add_any(const AnyIntView& yp) { + if (yp.size() <= size()) { + if (!yp.is_valid()) { + return invalidate_bool(); + } + for (int i = 0; i < yp.size(); i++) { + digits[i] += yp.digits[i]; + } + return true; + } else { + if (!is_valid()) { + return false; + } + if (yp.size() > max_size()) { + return invalidate_bool(); + } + for (int i = 0; i < size(); i++) { + digits[i] += yp.digits[i]; + } + for (int i = size(); i < yp.size(); i++) { + digits[i] = yp.digits[i]; + } + set_size(yp.size()); + return true; + } +} + +template +bool AnyIntView::sub_any(const AnyIntView& yp) { + if (yp.size() <= size()) { + if (!yp.is_valid()) { + return invalidate_bool(); + } + for (int i = 0; i < yp.size(); i++) { + digits[i] -= yp.digits[i]; + } + return true; + } else { + if (!is_valid()) { + return false; + } + if (yp.size() > max_size()) { + return invalidate_bool(); + } + for (int i = 0; i < size(); i++) { + digits[i] -= yp.digits[i]; + } + for (int i = size(); i < yp.size(); i++) { + digits[i] = -yp.digits[i]; + } + set_size(yp.size()); + return true; + } +} + +template +template +bool AnyIntView::log_op_any(const AnyIntView& yp) { + word_t cx = 0, cy = 0, cz = 0; + int i = 0; + const int shift = Tr::word_shift; + if (size() == 1) { + if (LogOp::has_zero && digits[0] == LogOp::zero) { + return true; + } else if (digits[0] == LogOp::neutral) { + if (yp.size() <= max_size()) { + set_size(yp.size()); + std::memcpy(digits.data(), yp.digits.data(), size() * sizeof(word_t)); + return true; + } else { + return invalidate_bool(); + } + } + } + if (yp.size() == 1) { + if (LogOp::has_zero && yp.digits[0] == LogOp::zero) { + set_size(1); + digits[0] = LogOp::zero; + return true; + } else if (yp.digits[0] == LogOp::neutral) { + return true; + } + } + if (yp.size() <= size()) { + if (!yp.is_valid()) { + return invalidate_bool(); + } + for (; i < yp.size(); i++) { + cx += digits[i]; + cy += yp.digits[i]; + cz += (LogOp::op(cx, cy) & (Tr::Base - 1)) + Tr::Half; + cx >>= shift; + cy >>= shift; + digits[i] = (cz & (Tr::Base - 1)) - Tr::Half; + cz >>= shift; + } + for (; i < size(); i++) { + cx += digits[i]; + cz += (LogOp::op(cx, cy) & (Tr::Base - 1)) + Tr::Half; + cx >>= shift; + cy >>= shift; + digits[i] = (cz & (Tr::Base - 1)) - Tr::Half; + cz >>= shift; + } + cz += LogOp::op(cx, cy); + if (cz) { + if (size() >= max_size()) { + return invalidate_bool(); + } + digits[inc_size()] = cz; + } else { + while (size() > 1 && !digits[size() - 1]) { + dec_size(); + } + } + return true; + } else { + if (!is_valid()) { + return false; + } + for (; i < size(); i++) { + cx += digits[i]; + cy += yp.digits[i]; + cz += (LogOp::op(cx, cy) & (Tr::Base - 1)) + Tr::Half; + cx >>= shift; + cy >>= shift; + digits[i] = (cz & (Tr::Base - 1)) - Tr::Half; + cz >>= shift; + } + set_size(std::min(yp.size(), max_size())); + for (; i < size(); i++) { + cy += yp.digits[i]; + cz += (LogOp::op(cx, cy) & (Tr::Base - 1)) + Tr::Half; + cx >>= shift; + cy >>= shift; + digits[i] = (cz & (Tr::Base - 1)) - Tr::Half; + cz >>= shift; + } + if (yp.size() > size()) { + for (; i < yp.size(); i++) { + cy += yp.digits[i]; + cz += (LogOp::op(cx, cy) & (Tr::Base - 1)); + cx >>= shift; + cy >>= shift; + if ((cz & (Tr::Base - 1)) != 0) { + return invalidate_bool(); + } + cz >>= shift; + } + } + cz += LogOp::op(cx, cy); + if (cz) { + return invalidate_bool(); + } + while (size() > 1 && !digits[size() - 1]) { + dec_size(); + } + return true; + } +} + +template +bool AnyIntView::add_mul_any(const AnyIntView& yp, const AnyIntView& zp) { + int yn = yp.size(), zn = zp.size(), rn = yn + zn; + if (!yp.is_valid() || !zp.is_valid() || !is_valid()) { + return invalidate_bool(); + } + if (rn > max_size() + 1) { + return invalidate_bool(); + } else if (rn < max_size() + 1) { + while (size() < rn) { + digits[inc_size()] = 0; + } + for (int i = 0; i < yn; i++) { + word_t yv = yp.digits[i]; + for (int j = 0; j < zn; j++) { + Tr::add_mul(&digits[i + j + 1], &digits[i + j], yv, zp.digits[j]); + } + } + } else { + while (size() < rn - 1) { + digits[inc_size()] = 0; + } + int i; + for (i = 0; i < yn - 1; i++) { + word_t yv = yp.digits[i]; + for (int j = 0; j < zn; j++) { + Tr::add_mul(&digits[i + j + 1], &digits[i + j], yv, zp.digits[j]); + } + } + word_t yv = yp.digits[i]; + int j; + for (j = 0; j < zn - 1; j++) { + Tr::add_mul(&digits[i + j + 1], &digits[i + j], yv, zp.digits[j]); + } + word_t hi = 0; + Tr::add_mul(&hi, &digits[i + j], yv, zp.digits[j]); + if (hi && hi != -1) { + return invalidate_bool(); + } + digits[size() - 1] += (hi << word_shift); + } + return true; +} + +template +void AnyIntView::add_mul_trunc_any(const AnyIntView& yp, const AnyIntView& zp) { + int yn = yp.size(), zn = zp.size(); + if (!yp.is_valid() || !zp.is_valid() || !is_valid()) { + invalidate(); + return; + } + int xn = std::min(yn + zn, max_size()); + while (size() < xn) { + digits[inc_size()] = 0; + } + xn = size(); + for (int i = 0; i < yn && i < xn; i++) { + word_t yv = yp.digits[i]; + for (int j = 0; j < zn; j++) { + if (i + j + 1 < xn) { + Tr::add_mul(&digits[i + j + 1], &digits[i + j], yv, zp.digits[j]); + } else { + word_t hi = 0; + Tr::add_mul(&hi, &digits[i + j], yv, zp.digits[j]); + break; + } + } + } +} + +template +int AnyIntView::sgn_un_any() const { + if (!is_valid()) { + return 0; + } + word_t v = digits[size() - 1]; + if (size() >= 2) { + if (v >= Tr::MaxDenorm) { + return 1; + } else if (v <= -Tr::MaxDenorm) { + return -1; + } + int i = size() - 2; + do { + v <<= word_shift; + word_t w = digits[i]; + if (w >= -v + Tr::MaxDenorm) { + return 1; + } else if (w <= -v - Tr::MaxDenorm) { + return -1; + } + v += w; + } while (--i >= 0); + } + return (v > 0 ? 1 : (v < 0 ? -1 : 0)); +} + +template +bool AnyIntView::get_bit_any(unsigned bit) const { + if (!is_valid()) { + return 0; + } + if (bit >= (unsigned)size() * word_shift) { + return sgn() < 0; + } + if (bit < word_shift) { + return (digits[0] >> bit) & 1; + } + auto q = std::div(bit, word_shift); + int i = q.quot; + word_t x = digits[i]; + while (--i >= 0) { + if (digits[i] < 0) { + --x; + break; + } else if (digits[i] > 0) { + break; + } + } + return (x >> q.rem) & 1; +} + +template +typename Tr::word_t AnyIntView::to_long_any() const { + if (!is_valid()) { + return (~0ULL << 63); + } else if (size() == 1) { + return digits[0]; + } else { + word_t v = digits[0] + (digits[1] << word_shift); // approximation mod 2^64 + word_t w = (v & (Tr::Base - 1)) - digits[0]; + w >>= word_shift; + w += (v >> word_shift); // excess of approximation divided by Tr::Base + int n = size() - 1; + for (int i = 1; i < n; i++) { + w -= digits[i]; + if (w & (Tr::Base - 1)) { + return (~0ULL << 63); + } + w >>= word_shift; + } + return w != digits[n] ? (~0ULL << 63) : v; + } +} + +template +int AnyIntView::cmp_any(const AnyIntView& yp) const { + if (yp.size() < size()) { + return top_word() < 0 ? -1 : 1; + } else if (yp.size() > size()) { + return yp.top_word() > 0 ? -1 : 1; + } + for (int i = size() - 1; i >= 0; i--) { + if (digits[i] < yp.digits[i]) { + return -1; + } else if (digits[i] > yp.digits[i]) { + return 1; + } + } + return 0; +} + +template +int AnyIntView::cmp_any(word_t y) const { + if (size() > 1) { + return top_word() < 0 ? -1 : 1; + } else if (size() == 1) { + return digits[0] < y ? -1 : (digits[0] > y ? 1 : 0); + } else { + return 0x80000000; + } +} + +template +template +int AnyIntView::cmp_un_any(const AnyIntView& yp) const { + int xn = size(), yn = yp.size(); + word_t v; + if (yn < xn) { + v = T::eval(digits[--xn]); + if (v >= Tr::MaxDenorm) { + return 1; + } else if (v <= -Tr::MaxDenorm) { + return -1; + } + while (xn > yn) { + v <<= word_shift; + word_t w = T::eval(digits[--xn]); + if (w >= -v + Tr::MaxDenorm) { + return 1; + } else if (w <= -v + Tr::MaxDenorm) { + return -1; + } + v += w; + } + } else if (yn > xn) { + v = -yp.digits[--yn]; + if (v >= Tr::MaxDenorm) { + return 1; + } else if (v <= -Tr::MaxDenorm) { + return -1; + } + while (yn > xn) { + v <<= word_shift; + word_t w = yp.digits[--yn]; + if (w <= v - Tr::MaxDenorm) { + return 1; + } else if (w >= v + Tr::MaxDenorm) { + return -1; + } + v -= w; + } + } else { + v = 0; + } + while (--xn >= 0) { + v <<= word_shift; + word_t w = T::eval(digits[xn]) - yp.digits[xn]; + if (w >= -v + Tr::MaxDenorm) { + return 1; + } else if (w <= -v - Tr::MaxDenorm) { + return -1; + } + v += w; + } + return (v > 0 ? 1 : (v < 0 ? -1 : 0)); +} + +template +bool AnyIntView::eq_any(const AnyIntView& yp) const { + if (yp.size() != size()) { + return false; + } + return !std::memcmp(digits.data(), yp.digits.data(), size() * sizeof(word_t)); +} + +template +bool AnyIntView::eq_any(word_t y) const { + return (size() == 1 && digits[0] == y); +} + +template +void AnyIntView::negate_any() { + for (int i = 0; i < size(); i++) { + digits[i] = -digits[i]; + } +} + +template +void AnyIntView::mul_tiny_any(int y) { + for (int i = 0; i < size(); i++) { + digits[i] *= y; + } +} + +template +int AnyIntView::divmod_tiny_any(int y) { + if (!y) { + invalidate(); + return 0; + } + int rem = 0; + for (int i = size() - 1; i >= 0; i--) { + auto divmod = std::div(digits[i] + ((word_t)rem << word_shift), (word_t)y); + digits[i] = divmod.quot; + rem = (int)divmod.rem; + if ((rem ^ y) < 0 && rem) { + rem += y; + digits[i]--; + } + } + while (size() > 1 && !digits[size() - 1]) { + dec_size(); + } + return rem; +} + +template +typename Tr::word_t AnyIntView::divmod_short_any(word_t y) { + if (!y || !is_valid()) { + invalidate(); + throw IntOverflow{}; + } + word_t rem = 0; + int i = size() - 1; + if (!i) { + auto divmod = std::div(digits[0], y); + digits[0] = divmod.quot; + rem = divmod.rem; + if ((rem ^ y) < 0 && rem) { + rem += y; + digits[0]--; + } + return rem; + } + if (std::abs(digits[i]) * 2 < std::abs(y)) { + rem = digits[i--]; + dec_size(); + } + do { + Tr::dbl_divmod(&digits[i], &rem, rem, digits[i], y); + } while (--i >= 0); + if ((rem ^ y) < 0 && rem) { + rem += y; + digits[0]--; + } + while (size() > 1 && !digits[size() - 1]) { + dec_size(); + } + return rem; +} + +template +bool AnyIntView::mul_add_short_any(word_t y, word_t z) { + if (!is_valid()) { + return false; + } + for (int i = 0; i < size(); i++) { + word_t newc; + Tr::set_mul(&newc, digits.data() + i, y, digits[i]); + digits[i] += z; + z = newc; + } + if (!z) { + return true; + } + if (size() < max_size()) { + digits[inc_size()] = z; + return true; + } + z += (digits[size() - 1] >> word_shift); + digits[size() - 1] &= Tr::Base - 1; + if (!z || z == -1) { + digits[size() - 1] += (z << word_shift); + return true; + } else { + return false; + } +} + +template +bool AnyIntView::mod_div_any(const AnyIntView& yp, AnyIntView& quot, int round_mode) { + quot.invalidate(); + if (!is_valid()) { + return false; + } + if (yp.size() == 1) { + word_t yv = yp.digits[0]; + if (!yv) { + return false; + } + word_t rem = divmod_short_any(yv); + if (!round_mode) { + if ((yv > 0 && rem * 2 >= yv) || (yv < 0 && rem * 2 <= yv)) { + rem -= yv; + digits[0]++; + } + } else if (round_mode > 0 && rem) { + rem -= yv; + digits[0]++; + } + if (!normalize_bool_any()) { + return false; + } + if (size() > quot.max_size()) { + return false; + } + quot.set_size(size()); + std::memcpy(quot.digits.data(), digits.data(), size() * sizeof(word_t)); + *this = rem; + return true; + } + if (!yp.is_valid()) { + return invalidate_bool(); + } + + double y_top = yp.top_double(); + if (y_top == 0) { + // division by zero + return invalidate_bool(); + } + double y_inv = (double)Tr::Base / y_top; + + int k = size() - yp.size(); + if (k >= 0) { + if (std::abs(top_word()) * 2 <= std::abs(yp.top_word())) { + if (k > quot.max_size()) { + return invalidate_bool(); + } + quot.set_size(k); + } else { + if (k >= quot.max_size()) { + return invalidate_bool(); + } + quot.set_size(k + 1); + double x_top = top_double(); + word_t q = std::llrint(x_top * y_inv * Tr::InvBase); + quot.digits[k] = q; + int i = yp.size() - 1; + word_t hi = 0; + Tr::sub_mul(&hi, &digits[k + i], q, yp.digits[i]); + while (--i >= 0) { + Tr::sub_mul(&digits[k + i + 1], &digits[k + i], q, yp.digits[i]); + } + digits[size() - 1] += (hi << word_shift); + } + } else { + quot.set_size(1); + quot.digits[0] = 0; + } + while (--k >= 0) { + double x_top = top_double(); + word_t q = std::llrint(x_top * y_inv); + quot.digits[k] = q; + for (int i = yp.size() - 1; i >= 0; --i) { + Tr::sub_mul(&digits[k + i + 1], &digits[k + i], q, yp.digits[i]); + } + dec_size(); + digits[size() - 1] += (digits[size()] << word_shift); + } + if (size() >= yp.size()) { + assert(size() == yp.size()); + double x_top = top_double(); + double t = x_top * y_inv * Tr::InvBase; + if (round_mode >= 0) { + t += (round_mode ? 1 : 0.5); + } + word_t q = std::llrint(std::floor(t)); + if (q) { + for (int i = 0; i < size(); i++) { + digits[i] -= q * yp.digits[i]; + } + quot.digits[0] += q; + } + } + + int q_adj = 0, sy = (y_inv > 0 ? 1 : -1); + if (round_mode < 0) { + // floor: must have 0 <= rem < y or 0 >= rem > y + int sr = sgn_un_any(); + if (sr * sy < 0) { + q_adj = -1; + } else { + sr = cmp_un_any>(yp); + if (sr * sy >= 0) { + q_adj = 1; + } + } + } else if (round_mode > 0) { + // ceil: must have -y < rem <= 0 or -y > rem >= 0 + int sr = sgn_un_any(); + if (sr * sy > 0) { + q_adj = 1; + } else { + sr = cmp_un_any>(yp); // -rem ?? y + if (sr * sy >= 0) { + q_adj = -1; + } + } + } else { + // round: must have -y <= 2*rem < y or -y >= 2*rem > y + int sr = sgn_un_any(); + if (sr * sy > 0) { + // y and rem same sign, check 2*rem < y or 2*rem > y + sr = cmp_un_any>(yp); + if (sr * sy >= 0) { + q_adj = 1; + } + } else { + // y and rem different sign, check 2*rem >= -y or 2*rem <= -y + sr = cmp_un_any>(yp); + if (sr * sy > 0) { + q_adj = -1; + } + } + } + if (q_adj) { + quot.digits[0] += q_adj; + if (q_adj < 0 ? !add_any(yp) : !sub_any(yp)) { + return invalidate_bool(); + } + } + return normalize_bool_any(); +} + +template +bool AnyIntView::mod_pow2_any(int exponent) { + if (!is_valid()) { + return false; + } + if (exponent <= 0) { + *this = 0; + return true; + } + int q = exponent - (size() - 1) * word_shift; + if (q >= word_bits) { + if (sgn() >= 0) { + return true; + } + if (exponent >= max_size() * word_shift) { + return invalidate_bool(); + } + while (q >= word_shift) { + digits[inc_size()] = 0; + q -= word_shift; + } + if (q == word_shift - 1 && size() < max_size()) { + digits[size() - 1] = -Tr::Half; + digits[inc_size()] = 1; + } else { + digits[size() - 1] = ((word_t)1 << q); + } + return true; + } + while (q < 0) { + dec_size(); + q += word_shift; + } + word_t pow = ((word_t)1 << q); + word_t v = digits[size() - 1] & (pow - 1); + if (!v) { + int k = size() - 1; + while (k > 0 && !digits[k - 1]) { + --k; + } + if (!k) { + *this = 0; + return true; + } + if (digits[k - 1] > 0) { + set_size(k); + return true; + } + if (exponent >= max_size() * word_shift) { + return invalidate_bool(); + } + if (q - word_shift >= 0) { + digits[size() - 1] = 0; + digits[inc_size()] = ((word_t)1 << (q - word_shift)); + } + if (q - word_shift == -1 && size() < max_size() - 1) { + digits[size() - 1] = -Tr::Half; + digits[inc_size()] = 1; + } else { + digits[size() - 1] = pow; + } + return true; + } else if (v >= Tr::Half) { + if (size() == max_size() - 1) { + return invalidate_bool(); + } else { + digits[size() - 1] = v | -Tr::Half; + digits[inc_size()] = ((word_t)1 << (q - word_shift)); + return true; + } + } else { + digits[size() - 1] = v; + return true; + } +} + +template +bool AnyIntView::mod_pow2_any(int exponent, int round_mode) { + if (round_mode < 0) { + return mod_pow2_any(exponent); + } + if (!is_valid()) { + return false; + } + if (exponent <= 0) { + *this = 0; + return true; + } + if (round_mode > 0) { + negate_any(); + bool res = mod_pow2_any(exponent); + negate_any(); + return res; + } + if (signed_fits_bits_any(exponent)) { + return true; + } + if (!mod_pow2_any(exponent)) { + return false; + } + if (!unsigned_fits_bits_any(exponent - 1)) { + return add_pow2_any(exponent, -1); + } + return true; +} + +template +bool AnyIntView::rshift_any(int exponent, int round_mode) { + if (exponent < 0) { + return invalidate_bool(); + } + if (!exponent) { + return true; + } + if (exponent > size() * word_shift + word_bits - word_shift) { + if (!round_mode) { + *this = 0; + } else if (round_mode < 0) { + *this = (sgn() < 0 ? -1 : 0); + } else { + *this = (sgn() > 0 ? 1 : 0); + } + return true; + } + int q = exponent / word_shift, r = exponent % word_shift; + assert(q <= size()); + + if (!round_mode && !r) { + digits[q - 1] += Tr::Half; + round_mode = -1; + } + + word_t v = (round_mode > 0 ? -1 : 0); + for (int i = 0; i < q; i++) { + v += digits[i]; + v >>= word_shift; + } + + set_size(size() - q); + if (!size()) { + if (!round_mode) { + *this = (((v >> (r - 1)) + 1) >> 1); + } else { + *this = (v >> r) + (round_mode > 0); + } + return true; + } + + if (!r) { + std::memmove(digits.data(), digits.data() + q, size() * sizeof(word_t)); + digits[0] += v + (round_mode > 0); + return true; + } + + v += digits[q]; + if (!round_mode) { + v = (((v >> (r - 1)) + 1) >> 1); + } else { + v >>= r; + v += (round_mode > 0); + } + + word_t mask = ((word_t)1 << r) - 1; + for (int i = 1; i < size(); i++) { + word_t w = digits[q + i]; + v += ((w & mask) << (word_shift - r)); + digits[i - 1] = v; + v = (w >> r); + } + digits[size() - 1] = v; + return true; +} + +template +bool AnyIntView::lshift_any(int exponent) { + if (exponent < 0) { + return invalidate_bool(); + } + if (!exponent) { + return true; + } + int q = exponent / word_shift, r = exponent % word_shift; + if (size() + q > max_size()) { + return invalidate_bool(); + } + if (!r) { + std::memmove(digits.data() + q, digits.data(), size() * sizeof(word_t)); + std::memset(digits.data(), 0, q * sizeof(word_t)); + set_size(size() + q); + return true; + } + + word_t v = 0, mask = (Tr::Base >> r) - 1; + for (int i = 0; i < size(); i++) { + word_t w = digits[i]; + v += ((w & mask) << r); + digits[i] = v; + v = (w >> (word_shift - r)); + } + if (v) { + if (size() + q < max_size()) { + digits[inc_size()] = v; + } else if (v != -1) { + return invalidate_bool(); + } else { + digits[size() - 1] += (v << word_shift); + } + } + if (q) { + std::memmove(digits.data() + q, digits.data(), size() * sizeof(word_t)); + std::memset(digits.data(), 0, q * sizeof(word_t)); + set_size(size() + q); + } + return true; +} + +template +bool AnyIntView::unsigned_fits_bits_any(int nbits) const { + if (!is_valid()) { + return false; + } + if (sgn() < 0) { + return false; + } + if (!sgn()) { + return true; + } + if (nbits >= size() * word_shift) { + return true; + } + if (nbits < 0) { + return false; + } + auto dm = std::div(nbits, word_shift); + int k = dm.quot; + if (size() >= k + 2) { + if (!(size() == k + 2 && dm.rem == word_shift - 1)) { + return false; + } + if (digits[k + 1] != 1) { + return false; + } + if (digits[k] > -Tr::Half) { + return false; + } else if (digits[k] < -Tr::Half) { + return true; + } + } else { + if (size() <= k) { + return true; + } + word_t pow = ((word_t)1 << dm.rem); + if (digits[k] > pow) { + return false; + } else if (digits[k] < pow) { + return true; + } + } + while (--k >= 0) { + if (digits[k] < 0) { + return true; + } else if (digits[k] > 0) { + return false; + } + } + return false; +} + +template +bool AnyIntView::signed_fits_bits_any(int nbits) const { + if (!is_valid()) { + return false; + } + if (nbits > size() * word_shift) { + return true; + } + int s = sgn(); + if (!s) { + return true; + } + if (nbits <= 0) { + return false; + } + auto dm = std::div(nbits - 1, word_shift); + int k = dm.quot; + if (size() <= k) { + return true; + } + if (size() >= k + 2) { + if (!(size() == k + 2 && dm.rem == word_shift - 1)) { + return false; + } + if (digits[k + 1] != s) { + return false; + } + word_t val = (s > 0 ? digits[k] : -digits[k]); + if (val > -Tr::Half) { + return false; + } else if (val < -Tr::Half) { + return true; + } + } else { + word_t val = (s > 0 ? digits[k] : -digits[k]); + word_t pow = ((word_t)1 << dm.rem); + if (val > pow) { + return false; + } else if (val < pow) { + return true; + } + } + while (--k >= 0) { + if (digits[k] < 0) { + return s > 0; + } else if (digits[k] > 0) { + return s < 0; + } + } + return s < 0; +} + +template +int AnyIntView::bit_size_any(bool sgnd) const { + if (!is_valid()) { + return 0x7fffffff; + } + int sg = sgn(); + if (!sg) { + return 0; + } else if (sg >= 0) { + int k = size() - 1; + word_t q = digits[k]; + if (k > 0 && q < Tr::MaxDenorm / 2) { + q <<= word_shift; + q += digits[--k]; + } + if (!k) { + int s = 64 - td::count_leading_zeroes64(q); + return s + sgnd; + } + int s = 64 - td::count_leading_zeroes64(q - Tr::MaxDenorm / 4); + q -= (word_t)1 << s; + s += k * word_shift + sgnd; + while (k > 0) { + if (q >= Tr::MaxDenorm / 2) { + return s + 1; + } else if (q <= -Tr::MaxDenorm / 2) { + return s; + } + q <<= word_shift; + q += digits[--k]; + } + return q >= 0 ? s + 1 : s; + } else if (sgnd) { + int k = size() - 1; + word_t q = digits[k]; + if (k > 0 && q > -Tr::MaxDenorm / 2) { + q <<= word_shift; + q += digits[--k]; + } + if (!k) { + int s = 64 - td::count_leading_zeroes64(~q); + return s + 1; + } + int s = 64 - td::count_leading_zeroes64(-q - Tr::MaxDenorm / 4); + q += (word_t)1 << s; + s += k * word_shift + 1; + while (k > 0) { + if (q >= Tr::MaxDenorm / 2) { + return s; + } else if (q <= -Tr::MaxDenorm / 2) { + return s + 1; + } + q <<= word_shift; + q += digits[--k]; + } + return q >= 0 ? s : s + 1; + } else { + return 0x7fffffff; + } +} + +template +bool AnyIntView::export_bytes_any(unsigned char* buff, std::size_t buff_size, bool sgnd) const { + if (!is_valid()) { + return false; + } + if (!buff_size) { + return sgn_un_any() == 0; + } + int k = 0; + word_t v = 0; + unsigned char* ptr = buff + buff_size; + unsigned char s = (sgn_un_any() < 0 ? 0xff : 0); + if (s && !sgnd) { + return false; + } + for (int i = 0; i < size(); i++) { + if ((word_shift & 7) && word_shift + 8 >= word_bits && k >= word_bits - word_shift - 1) { + int k1 = 8 - k; + v += (digits[i] << k) & 0xff; + if (ptr > buff) { + *--ptr = (unsigned char)(v & 0xff); + } else if ((unsigned char)(v & 0xff) != s) { + return false; + } + v >>= 8; + v += (digits[i] >> k1); + k += word_shift - 8; + } else { + v += (digits[i] << k); + k += word_shift; + } + while (k >= 8) { + if (ptr > buff) { + *--ptr = (unsigned char)(v & 0xff); + } else if ((unsigned char)(v & 0xff) != s) { + return false; + } + v >>= 8; + k -= 8; + } + } + while (ptr > buff) { + *--ptr = (unsigned char)(v & 0xff); + v >>= 8; + } + if (v != -(s & 1)) { + return false; + } + return !sgnd ? true : !((*ptr ^ s) & 0x80); +} + +template +bool AnyIntView::export_bytes_lsb_any(unsigned char* buff, std::size_t buff_size, bool sgnd) const { + if (!is_valid()) { + return false; + } + if (!buff_size) { + return sgn_un_any() == 0; + } + int k = 0; + word_t v = 0; + unsigned char* end = buff + buff_size; + unsigned char s = (sgn_un_any() < 0 ? 0xff : 0); + if (s && !sgnd) { + return false; + } + for (int i = 0; i < size(); i++) { + if ((word_shift & 7) && word_shift + 8 >= word_bits && k >= word_bits - word_shift - 1) { + int k1 = 8 - k; + v += (digits[i] << k) & 0xff; + if (buff < end) { + *buff++ = (unsigned char)(v & 0xff); + } else if ((unsigned char)(v & 0xff) != s) { + return false; + } + v >>= 8; + v += (digits[i] >> k1); + k += word_shift - 8; + } else { + v += (digits[i] << k); + k += word_shift; + } + while (k >= 8) { + if (buff < end) { + *buff++ = (unsigned char)(v & 0xff); + } else if ((unsigned char)(v & 0xff) != s) { + return false; + } + v >>= 8; + k -= 8; + } + } + while (buff < end) { + *buff++ = (unsigned char)(v & 0xff); + v >>= 8; + } + if (v != -(s & 1)) { + return false; + } + return !sgnd ? true : !((buff[-1] ^ s) & 0x80); +} + +template +bool AnyIntView::export_bits_any(unsigned char* buff, int offs, unsigned bits, bool sgnd) const { + if (!is_valid()) { + return false; + } + if (!bits) { + return sgn_un_any() == 0; + } + if (size() == 1 || bits < 64) { + word_t v = to_long_any(); + if (bits < 64) { + if (!sgnd) { + if (v < 0 || (unsigned long long)v >= (1ULL << bits)) { + return false; + } + } else { + word_t pw = (1LL << (bits - 1)); + if (v < -pw || v >= pw) { + return false; + } + } + td::bitstring::bits_store_long_top(buff, offs, v << (64 - bits), bits); + } else { + if (!sgnd && v < 0) { + return false; + } + td::bitstring::bits_memset(buff, offs, v < 0, bits - 64); + td::bitstring::bits_store_long_top(buff, offs + bits - 64, v, 64); + } + return true; + } + buff += (offs >> 3); + offs &= 7; + unsigned char s = (sgn_un_any() < 0 ? 0xff : 0); + if (s && !sgnd) { + return false; + } + unsigned end_offs = offs + bits; + unsigned char* ptr = buff + (end_offs >> 3); + int k = td::bits_negate32(end_offs) & 7; + word_t v = k ? (*ptr++ & ((1 << k) - 1)) : 0; + for (int i = 0; i < size(); i++) { + if (word_shift + 8 >= word_bits && k >= word_bits - word_shift - 1) { + int k1 = 8 - k; + v += (digits[i] << k) & 0xff; + if (ptr > buff) { + if (--ptr > buff) { + *ptr = (unsigned char)(v & 0xff); + } else { + int mask = (-0x100 >> offs) & 0xff; + if (((unsigned char)v ^ s) & mask) { + return false; + } + *ptr = (unsigned char)((*ptr & mask) | ((int)v & ~mask)); + } + } else if ((unsigned char)(v & 0xff) != s) { + return false; + } + v >>= 8; + v += (digits[i] >> k1); + k += word_shift - 8; + } else { + v += (digits[i] << k); + k += word_shift; + } + while (k >= 8) { + if (ptr > buff) { + if (--ptr > buff) { + *ptr = (unsigned char)(v & 0xff); + } else { + int mask = (-0x100 >> offs) & 0xff; + if (((unsigned char)v ^ s) & mask) { + return false; + } + *ptr = (unsigned char)((*ptr & mask) | ((int)v & ~mask)); + } + } else if ((unsigned char)(v & 0xff) != s) { + return false; + } + v >>= 8; + k -= 8; + } + } + if (ptr > buff) { + while (--ptr > buff) { + *ptr = (unsigned char)(v & 0xff); + v >>= 8; + } + int mask = (-0x100 >> offs) & 0xff; + if (((unsigned char)v ^ s) & mask) { + return false; + } + *ptr = (unsigned char)((*ptr & mask) | ((int)v & ~mask)); + v >>= 8; + } + if (v != -(s & 1)) { + return false; + } + return !sgnd ? true : !((*ptr ^ s) & (0x80 >> offs)); +} + +template +bool AnyIntView::import_bytes_any(const unsigned char* buff, std::size_t buff_size, bool sgnd) { + if (!buff_size) { + *this = 0; + return true; + } + unsigned char s = (sgnd && (buff[0] & 0x80)) ? 0xff : 0; + const unsigned char* ptr = buff + buff_size; + while (buff < ptr && *buff == s) { + buff++; + } + int k = 0; + word_t v = 0; + set_size(1); + assert(word_bits - word_shift >= 8); + while (ptr > buff) { + if (k >= word_shift) { + if (size() < max_size()) { + digits[size() - 1] = v; + inc_size(); + v = 0; + k -= word_shift; + } else if (k >= word_bits - 8) { + return invalidate_bool(); + } + } + v |= (((word_t) * --ptr) << k); + k += 8; + } + if (s) { + v -= ((word_t)1 << k); + } + digits[size() - 1] = v; + return normalize_bool_any(); +} + +template +bool AnyIntView::import_bits_any(const unsigned char* buff, int offs, unsigned bits, bool sgnd) { + if (bits < word_shift) { + set_size(1); + unsigned long long val = td::bitstring::bits_load_long_top(buff, offs, bits); + if (sgnd) { + digits[0] = ((long long)val >> (64 - bits)); + } else { + digits[0] = (val >> (64 - bits)); + } + return true; + } + buff += (offs >> 3); + offs &= 7; + unsigned char s = (sgnd && (buff[0] & (0x80 >> offs))) ? 0xff : 0; + unsigned end_offs = (unsigned)offs + bits; + const unsigned char* ptr = buff + (end_offs >> 3); + if (buff < ptr && !((*buff ^ s) & (0xff >> offs))) { + buff++; + offs = 0; + while (buff < ptr && *buff == s) { + buff++; + } + } + int k = end_offs & 7; + word_t v = k ? (*ptr >> (8 - k)) : 0; + set_size(1); + assert(word_bits - word_shift >= 8); + while (ptr > buff) { + if (k >= word_shift) { + if (size() < max_size()) { + digits[size() - 1] = v; + inc_size(); + v = 0; + k -= word_shift; + } else if (k >= word_bits - 8) { + return invalidate_bool(); + } + } + v |= (((word_t) * --ptr) << k); + k += 8; + } + k -= offs; + word_t pw = ((word_t)1 << k); + v &= pw - 1; + if (s) { + v -= pw; + } + digits[size() - 1] = v; + return normalize_bool_any(); +} + +template +bool AnyIntView::import_bytes_lsb_any(const unsigned char* buff, std::size_t buff_size, bool sgnd) { + if (!buff_size) { + *this = 0; + return true; + } + const unsigned char* end = buff + buff_size; + unsigned char s = (sgnd && (end[-1] & 0x80)) ? 0xff : 0; + while (end > buff && end[-1] == s) { + --end; + } + int k = 0; + word_t v = 0; + set_size(1); + assert(word_bits - word_shift >= 8); + while (buff < end) { + if (k >= word_shift) { + if (size() < max_size()) { + digits[size() - 1] = v; + inc_size(); + v = 0; + k -= word_shift; + } else if (k >= word_bits - 8) { + return invalidate_bool(); + } + } + v |= (((word_t)*buff++) << k); + k += 8; + } + if (s) { + v -= ((word_t)1 << k); + } + digits[size() - 1] = v; + return normalize_bool_any(); +} + +template +int AnyIntView::parse_hex_any(const char* str, int str_len, int* frac) { + invalidate(); + bool sgn = (str[0] == '-'); + int i = sgn, j; + int p = (frac ? -1 : 0); + while (i < str_len && str[i] == '0') { + i++; + } + for (j = i; j < str_len; j++) { + int c = str[j]; + if (c == '.' && p < 0) { + p = j + 1; + continue; + } + if (!((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f'))) { + break; + } + } + if (j == sgn + (p > 0)) { + return 0; + } + if ((j - i - (p > 0)) * 4 > (max_size() - 1) * word_shift + word_bits - 2) { + return 0; + } + int j_len = j, k = 0; + word_t v = 0; + set_size(0); + while (j > i) { + if (j == p) { + --j; + continue; + } + if (k >= word_shift && size() < max_size() - 1) { + digits[inc_size()] = (sgn ? -v : v); + v = 0; + k -= word_shift; + } + int c = str[--j]; + if (c <= '9') { + c -= '0'; + } else { + c = (c | 0x20) - ('a' - 10); + } + v += ((word_t)c << k); + k += 4; + enforce(k < word_bits - 1); + } + digits[inc_size()] = (sgn ? -v : v); + if (!normalize_bool_any()) { + invalidate(); + return 0; + } + if (p) { + *frac = (p > 0 ? j_len - p : -1); + } + return j_len; +} + +template +int AnyIntView::parse_binary_any(const char* str, int str_len, int* frac) { + invalidate(); + bool sgn = (str[0] == '-'); + int i = sgn, j, p = (frac ? -1 : 0); + while (i < str_len && str[i] == '0') { + i++; + } + for (j = i; j < str_len; j++) { + int c = str[j]; + if (c != '0' && c != '1') { + if (c == '.' && p < 0) { + p = j + 1; + continue; + } + break; + } + } + if (j == sgn + (p > 0)) { + return 0; + } + if (j - i - (p > 0) > (max_size() - 1) * word_shift + word_bits - 2) { + return 0; + } + int j_len = j, k = 0; + word_t v = 0; + set_size(0); + while (j > i) { + if (j == p) { + --j; + continue; + } + if (k >= word_shift && size() < max_size() - 1) { + digits[inc_size()] = (sgn ? -v : v); + v = 0; + k -= word_shift; + } + v += ((word_t)(str[--j] & 1) << k); + k++; + enforce(k < word_bits - 1); + } + digits[inc_size()] = (sgn ? -v : v); + if (!normalize_bool_any()) { + invalidate(); + return 0; + } + if (p) { + *frac = (p > 0 ? j_len - p : -1); + } + return j_len; +} + +template +std::string AnyIntView::to_dec_string_slow_destroy_any() { + if (!is_valid()) { + return "NaN"; + } + std::string x; + x.reserve((size() * word_shift + word_bits) * 97879 / 325147 + 2); + int s = sgn(); + if (s < 0) { + negate_any(); + } + do { + x += (char)('0' + divmod_short_any(10)); + } while (sgn()); + if (s < 0) { + x += '-'; + } + std::reverse(x.begin(), x.end()); + return x; +} + +template +std::string AnyIntView::to_dec_string_destroy_any() { + if (!is_valid()) { + return "NaN"; + } + std::string s; + std::vector stack; + int l10 = (size() * word_shift + word_bits) * 97879 / 325147; + s.reserve(l10 + 2); + stack.reserve(l10 / Tr::max_pow10_exp + 1); + if (sgn() < 0) { + negate_any(); + s += '-'; + } + do { + stack.push_back(divmod_short_any(Tr::max_pow10)); + } while (sgn()); + char slice[word_bits * 97879 / 325147 + 2]; + std::sprintf(slice, "%lld", stack.back()); + s += slice; + stack.pop_back(); + while (stack.size()) { + std::sprintf(slice, "%018lld", stack.back()); + s += slice; + stack.pop_back(); + } + return s; +} + +static const char hex_digits[] = "0123456789abcdef"; +static const char HEX_digits[] = "0123456789ABCDEF"; + +template +std::string AnyIntView::to_hex_string_slow_destroy_any() { + if (!is_valid()) { + return "NaN"; + } + std::string x; + x.reserve(((size() * word_shift + word_bits) >> 2) + 2); + int s = sgn(); + if (s < 0) { + negate_any(); + } + do { + x += hex_digits[divmod_short_any(16)]; + } while (sgn()); + if (s < 0) { + x += '-'; + } + std::reverse(x.begin(), x.end()); + return x; +} + +template +std::string AnyIntView::to_hex_string_any(bool upcase) const { + if (!is_valid()) { + return "NaN"; + } + int s = sgn(), k = 0; + if (!s) { + return "0"; + } + std::string x; + x.reserve(((size() * word_shift + word_bits) >> 2) + 2); + assert(word_shift < word_bits - 4); + const char* hex_digs = (upcase ? HEX_digits : hex_digits); + word_t v = 0; + for (int i = 0; i < size(); i++) { + v += ((s >= 0 ? digits[i] : -digits[i]) << k); + k += word_shift; + while (k >= 4 && (v || i < size() - 1)) { + x += hex_digs[v & 15]; + v >>= 4; + k -= 4; + } + } + assert(v >= 0); + while (v > 0) { + x += hex_digs[v & 15]; + v >>= 4; + } + if (s < 0) { + x += '-'; + } + std::reverse(x.begin(), x.end()); + return x; +} + +template +std::string AnyIntView::to_binary_string_any() const { + if (!is_valid()) { + return "NaN"; + } + int s = sgn(); + if (!s) { + return "0"; + } + std::string x; + x.reserve(size() * word_shift + word_bits + 2); + assert(word_shift < word_bits - 1); + word_t v = 0; + for (int i = 0; i < size(); i++) { + v += (s >= 0 ? digits[i] : -digits[i]); + int k = word_shift; + while (--k >= 0 && (v || i < size() - 1)) { + x += (v & 1 ? '1' : '0'); + v >>= 1; + } + } + assert(v >= 0); + while (v > 0) { + x += (v & 1 ? '1' : '0'); + v >>= 1; + } + if (s < 0) { + x += '-'; + } + std::reverse(x.begin(), x.end()); + return x; +} + +template +BigIntG& BigIntG::denormalize() { + word_t val = 0; + for (int i = 0; i < n; i++) { + val += digits[i]; + digits[i] = (val & (Tr::Base - 1)); + val >>= word_shift; + } + while (n < word_cnt) { + digits[n++] = (val & (Tr::Base - 1)); + val >>= word_shift; + } + return *this; +} + +template +BigIntG& BigIntG::logical_not() { + digits[0] = ~digits[0]; + for (int i = 1; i < n; i++) { + digits[i] = -digits[i]; + } + return *this; +} + +template +std::ostream& BigIntG::dump(std::ostream& os, bool nl) const { + os << "{"; + //auto f = os.flags(); + //os.flags(std::ios::hex | std::ios::showbase); + //os.width(16); + for (int i = n - 1; i >= 0; i--) { + os << digits[i] << (i ? ' ' : '}'); + } + if (!n) { + os << "nan}"; + } + if (nl) { + os << std::endl; + } + //os.flags(f); + return os; +} + +template +std::string BigIntG::dump() const { + std::ostringstream os; + dump(os); + return os.str(); +} + +template +int BigIntG::parse_dec_slow(const char* str, int str_len) { + *this = 0; + int i; + bool sgn = (str[0] == '-'); + if (str_len <= static_cast(sgn)) { + return 0; + } + for (i = sgn; i < str_len; i++) { + if (str[i] < '0' || str[i] > '9') { + return i; + } + mul_tiny(10); + add_tiny(sgn ? '0' - str[i] : str[i] - '0'); + if (!normalize_bool()) { + *this = 0; + return 0; + } + } + return i; +} + +template +int BigIntG::parse_dec(const char* str, int str_len, int* frac) { + *this = 0; + int i; + int p = frac ? -1 : 0; + bool sgn = (str[0] == '-'), ok = false; + word_t q = 1, a = 0; + for (i = sgn; i < str_len; i++) { + if (str[i] == '.') { + if (p >= 0) { + break; + } + p = i + 1; + continue; + } + int digit = (int)str[i] - '0'; + if ((unsigned)digit >= 10) { + break; + } + ok = true; + if (q >= Tr::Half / 10) { + if (!mul_add_short_bool(q, a)) { + return 0; + } + q = 1; + a = 0; + } + q *= 10; + a *= 10; + a += (sgn ? -digit : digit); + } + if (!ok || !mul_add_short_bool(q, a) || !normalize_bool()) { + return 0; + } + if (frac) { + *frac = (p > 0 ? i - p : -1); + } + return i; +} + +template +std::string BigIntG::to_dec_string_slow() const { + BigIntG copy(*this); + copy.normalize_bool(); + return copy.as_any_int().to_dec_string_slow_destroy_any(); +} + +template +std::string BigIntG::to_dec_string() const { + BigIntG copy(*this); + copy.normalize_bool(); + //std::cout << "(tds " << (const void*)this << "->" << (void*)© << ")"; + return copy.as_any_int().to_dec_string_destroy_any(); +} + +template +std::string BigIntG::to_dec_string_destroy() { + normalize_bool(); + return as_any_int().to_dec_string_destroy_any(); +} + +template +std::string BigIntG::to_hex_string_slow() const { + BigIntG copy(*this); + copy.normalize_bool(); + return copy.as_any_int().to_hex_string_slow_destroy_any(); +} + +template +std::string BigIntG::to_hex_string(bool upcase) const { + return as_any_int().to_hex_string_any(upcase); +} + +template +std::string BigIntG::to_binary_string() const { + return as_any_int().to_binary_string_any(); +} + +template +std::ostream& operator<<(std::ostream& os, const BigIntG& x) { + return os << x.to_dec_string(); +} + +template +std::ostream& operator<<(std::ostream& os, BigIntG&& x) { + return os << x.to_dec_string_destroy(); +} + +extern template class AnyIntView; +extern template class BigIntG<257, BigIntInfo>; +typedef BigIntG<257, BigIntInfo> BigInt256; + +namespace literals { + +extern BigInt256 operator""_i256(const char* str, std::size_t str_len); +extern BigInt256 operator""_x256(const char* str, std::size_t str_len); + +} // namespace literals + +} // namespace td diff --git a/crypto/common/bitstring.cpp b/crypto/common/bitstring.cpp new file mode 100644 index 00000000..6a23f072 --- /dev/null +++ b/crypto/common/bitstring.cpp @@ -0,0 +1,668 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "common/bitstring.h" +#include +#include +#include "td/utils/as.h" +#include "td/utils/bits.h" +#include "td/utils/misc.h" +#include "crypto/openssl/digest.h" + +namespace td { + +template class Ref; + +BitString::BitString(const BitSlice& bs, unsigned reserve_bits) { + if (!bs.size() && !reserve_bits) { + ptr = 0; + offs = len = bytes_alloc = 0; + } else { + offs = bs.get_offs(); + len = bs.size(); + bytes_alloc = (bs.get_offs() + bs.size() + reserve_bits + 7) >> 3; + ptr = static_cast(std::malloc(bytes_alloc)); + CHECK(ptr); + if (bs.size()) { + std::memcpy(ptr, bs.get_ptr(), bs.byte_size()); + } + } +} + +BitString::BitString(unsigned reserve_bits) { + if (!reserve_bits) { + ptr = 0; + offs = len = bytes_alloc = 0; + } else { + bytes_alloc = (reserve_bits + 7) >> 3; + ptr = static_cast(std::malloc(bytes_alloc)); + CHECK(ptr); + offs = len = 0; + } +} + +BitString::operator BitSlice() const { + return BitSlice(BitStringRef{this}, ptr, offs, len); +} + +BitString* BitString::make_copy() const { + if (!ptr) { + return new BitString(64); // reserve 64 bits + } else { + return new BitString(operator BitSlice(), 64); + } +} + +BitString& BitString::reserve_bits(unsigned req_bits) { + req_bits += offs + len; + if (req_bits > bytes_alloc * 8) { + bytes_alloc = (req_bits + 7) >> 3; + ptr = (unsigned char*)std::realloc(ptr, bytes_alloc); + CHECK(ptr); + } + return *this; +} + +BitSliceWrite BitString::reserve_bitslice(unsigned req_bits) { + reserve_bits(req_bits); + unsigned pos = offs + len; + len += req_bits; + return BitSliceWrite(Ref(this), ptr, pos, req_bits); +} + +BitString& BitString::append(const BitSlice& bs) { + reserve_bitslice(bs.size()) = bs; + return *this; +} + +BitSlice BitString::subslice(unsigned from, unsigned bits) const { + return BitSlice{BitStringRef{this}, ptr, static_cast(offs + from), bits}; +} + +BitSliceWrite BitString::subslice_write(unsigned from, unsigned bits) { + return BitSliceWrite{BitStringRef{this}, ptr, offs + from, bits}; +} + +const BitSliceWrite& BitSliceWrite::operator=(const BitSlice& bs) const { + if (size() != bs.size()) { + throw LengthMismatch(); + } + bitstring::bits_memcpy(get_ptr(), get_offs(), bs.get_ptr(), bs.get_offs(), size()); + return *this; +} + +const BitSliceWrite& BitSliceWrite::operator=(bool val) const { + bitstring::bits_memset(get_ptr(), get_offs(), val, size()); + return *this; +} + +std::ostream& operator<<(std::ostream& os, const BitString& bs) { + return os << bs.to_hex(); +} + +std::ostream& operator<<(std::ostream& os, Ref bs_ref) { + return os << (bs_ref.is_null() ? "(null-bs)" : bs_ref->to_hex()); +} + +namespace bitstring { + +void bits_memcpy(unsigned char* to, int to_offs, const unsigned char* from, int from_offs, std::size_t bit_count) { + if (bit_count <= 0) { + return; + } + from += (from_offs >> 3); + to += (to_offs >> 3); + from_offs &= 7; + to_offs &= 7; + //fprintf(stderr, "bits_memcpy: from=%p (%02x) to=%p (%02x) from_offs=%d to_offs=%d count=%lu\n", from, *from, to, *to, from_offs, to_offs, bit_count); + int sz = (int)bit_count; + bit_count += from_offs; + if (from_offs == to_offs) { + if (bit_count < 8) { + int mask = (-0x100 >> bit_count) & (0xff >> to_offs); + *to = (unsigned char)((*to & ~mask) | (*from & mask)); + return; + } + std::size_t l = (bit_count >> 3); + if (!to_offs) { + std::memcpy(to, from, l); + } else { + int mask = (0xff >> to_offs); + *to = (unsigned char)((*to & ~mask) | (*from & mask)); + std::memcpy(to + 1, from + 1, l - 1); + } + if ((bit_count &= 7) != 0) { + int mask = (-0x100 >> bit_count); + to[l] = (unsigned char)((to[l] & ~mask) | (from[l] & mask)); + } + } else { + int b = (int)to_offs; + unsigned long long acc = (b ? *to >> (8 - b) : 0); + if (bit_count < 8) { + acc <<= sz; + acc |= ((*from & (0xff >> from_offs)) >> (8 - bit_count)); + b += sz; + } else { + unsigned ld = 8 - from_offs; + acc <<= ld; + acc |= (*from++ & (0xff >> from_offs)); + b += ld; + bit_count -= 8; + // b <= 15 here + while (bit_count >= 32) { + acc <<= 32; + acc |= td::bswap32(as(from)); + from += 4; + as(to) = td::bswap32((unsigned)(acc >> b)); + to += 4; + bit_count -= 32; + } + // bit_count <= 31, b <= 15 + while (bit_count >= 8) { + acc <<= 8; + acc |= *from++; + bit_count -= 8; + b += 8; + } + // b + bit_count = const <= 46 + if (bit_count > 0) { + acc <<= bit_count; + acc |= (*from >> (8 - bit_count)); + b += (int)bit_count; + } + } + while (b >= 8) { + b -= 8; + *to++ = (unsigned char)(acc >> b); + } + if (b > 0) { + *to = (unsigned char)((*to & (0xff >> b)) | ((int)acc << (8 - b))); + } + } +} + +void bits_memcpy(BitPtr to, ConstBitPtr from, std::size_t bit_count) { + bits_memcpy(to.ptr, to.offs, from.ptr, from.offs, bit_count); +} + +void bits_memset(unsigned char* to, int to_offs, bool val, std::size_t bit_count) { + if (bit_count <= 0) { + return; + } + to += (to_offs >> 3); + to_offs &= 7; + int sz = (int)bit_count; + bit_count += to_offs; + int c = *to; + if (bit_count <= 8) { + int mask = (((-0x100 >> sz) & 0xff) >> to_offs); + if (val) { + *to = (unsigned char)(c | mask); + } else { + *to = (unsigned char)(c & ~mask); + } + return; + } + if (val) { + *to = (unsigned char)(c | (0xff >> to_offs)); + } else { + *to = (unsigned char)(c & (-0x100 >> to_offs)); + } + std::size_t l = (bit_count >> 3); + std::memset(to + 1, val ? 0xff : 0, l - 1); + if ((bit_count &= 7) != 0) { + if (val) { + to[l] = (unsigned char)(to[l] | (-0x100 >> bit_count)); + } else { + to[l] = (unsigned char)(to[l] & (0xff >> bit_count)); + } + } +} + +void bits_memset(BitPtr to, bool val, std::size_t bit_count) { + bits_memset(to.ptr, to.offs, val, bit_count); +} + +std::size_t bits_memscan_rev(const unsigned char* ptr, int offs, std::size_t bit_count, bool cmp_to) { + if (!bit_count) { + return 0; + } + int xor_val = (cmp_to ? -1 : 0); + ptr += ((offs + bit_count) >> 3); + offs = (int)((offs + bit_count) & 7); + std::size_t res = offs; + if (offs) { + unsigned v = (*ptr >> (8 - offs)) ^ xor_val; + unsigned c = td::count_trailing_zeroes32(v); + if (c < (unsigned)offs || res >= bit_count) { + return std::min(c, (unsigned)bit_count); + } + } + bit_count -= res; + while (bit_count >= 32) { + ptr -= 4; + unsigned v = td::bswap32(as(ptr)) ^ xor_val; + if (v) { + return td::count_trailing_zeroes_non_zero32(v) + res; + } + res += 32; + bit_count -= 32; + } + xor_val &= 0xff; + while (bit_count >= 8) { + unsigned v = *--ptr ^ xor_val; + if (v) { + return td::count_trailing_zeroes_non_zero32(v) + res; + } + res += 8; + bit_count -= 8; + } + if (bit_count > 0) { + unsigned v = *--ptr ^ xor_val; + return std::min((unsigned)td::count_trailing_zeroes32(v), (unsigned)bit_count) + res; + } else { + return res; + } +} + +std::size_t bits_memscan(const unsigned char* ptr, int offs, std::size_t bit_count, bool cmp_to) { + if (!bit_count) { + return 0; + } + int xor_val = -static_cast(cmp_to); + ptr += (offs >> 3); + offs &= 7; + std::size_t rem = bit_count; + unsigned v, c; + if (offs) { + v = ((unsigned)(ptr[0] ^ xor_val) << (24 + offs)); + // std::cerr << "[A] rem=" << rem << " ptr=" << (const void*)ptr << " v=" << std::hex << v << std::dec << std::endl; + c = td::count_leading_zeroes32(v); + unsigned l = (unsigned)(8 - offs); + if (c < l || bit_count <= l) { + return std::min(c, bit_count); + } + rem -= l; + ptr++; + } + while (rem >= 8 && !td::is_aligned_pointer<8>(ptr)) { + v = ((*ptr++ ^ xor_val) << 24); + // std::cerr << "[B] rem=" << rem << " ptr=" << (const void*)(ptr - 1) << " v=" << std::hex << v << std::dec << std::endl; + if (v) { + return bit_count - rem + td::count_leading_zeroes_non_zero32(v); + } + rem -= 8; + } + td::uint64 xor_val_l = (cmp_to ? ~0LL : 0LL); + while (rem >= 64) { + td::uint64 z = td::bswap64(as(ptr)) ^ xor_val_l; + // std::cerr << "[C] rem=" << rem << " ptr=" << (const void*)ptr << " z=" << std::hex << z << std::dec << std::endl; + if (z) { + return bit_count - rem + td::count_leading_zeroes_non_zero64(z); + } + ptr += 8; + rem -= 64; + } + while (rem >= 8) { + v = ((*ptr++ ^ xor_val) << 24); + // std::cerr << "[D] rem=" << rem << " ptr=" << (const void*)(ptr - 1) << " v=" << std::hex << v << std::dec << std::endl; + if (v) { + return bit_count - rem + td::count_leading_zeroes_non_zero32(v); + } + rem -= 8; + } + if (rem > 0) { + v = ((*ptr ^ xor_val) << 24); + // std::cerr << "[E] rem=" << rem << " ptr=" << (const void*)ptr << " v=" << std::hex << v << std::dec << std::endl; + c = td::count_leading_zeroes32(v); + return c < rem ? bit_count - rem + c : bit_count; + } else { + return bit_count; + } +} + +std::size_t bits_memscan(ConstBitPtr bs, std::size_t bit_count, bool cmp_to) { + return bits_memscan(bs.ptr, bs.offs, bit_count, cmp_to); +} + +std::size_t bits_memscan_rev(ConstBitPtr bs, std::size_t bit_count, bool cmp_to) { + return bits_memscan_rev(bs.ptr, bs.offs, bit_count, cmp_to); +} + +int bits_memcmp(const unsigned char* bs1, int bs1_offs, const unsigned char* bs2, int bs2_offs, std::size_t bit_count, + std::size_t* same_upto) { + if (!bit_count) { + return 0; + } + bs1 += (bs1_offs >> 3); + bs2 += (bs2_offs >> 3); + bs1_offs &= 7; + bs2_offs &= 7; + //fprintf(stderr, "bits_memcmp: bs1=%02x%02x offs=%d bs2=%02x%02x offs=%d cnt=%lu\n", bs1[0], bs1[1], bs1_offs, bs2[0], bs2[1], bs2_offs, bit_count); + unsigned long long acc1 = (((unsigned long long)*bs1++) << (56 + bs1_offs)); + int z1 = 8 - bs1_offs; + unsigned long long acc2 = (((unsigned long long)*bs2++) << (56 + bs2_offs)); + int z2 = 8 - bs2_offs; + std::size_t processed = 0; + while (bit_count >= 40) { + acc1 |= ((unsigned long long)td::bswap32(as(bs1)) << (32 - z1)); + bs1 += 4; + acc2 |= ((unsigned long long)td::bswap32(as(bs2)) << (32 - z2)); + bs2 += 4; + if ((acc1 ^ acc2) & (~0ULL << 32)) { + if (same_upto) { + *same_upto = processed + td::count_leading_zeroes64(acc1 ^ acc2); + } + return acc1 < acc2 ? -1 : 1; + } + acc1 <<= 32; + acc2 <<= 32; + processed += 32; + bit_count -= 32; + } + // now 0 <= bit_count <= 39 + + bs1_offs += (int)bit_count - 8; // = bit_count - z1, bits to load from bs1 + while (bs1_offs >= 8) { + acc1 |= ((unsigned long long)(*bs1++) << (56 - z1)); + z1 += 8; + bs1_offs -= 8; + } + if (bs1_offs > 0) { + acc1 |= ((unsigned long long)(*bs1) << (56 - z1)); + } + z1 += bs1_offs; // NB: bs1_offs may be negative + + bs2_offs += (int)bit_count - 8; // bits to load from bs2 + while (bs2_offs >= 8) { + acc2 |= ((unsigned long long)(*bs2++) << (56 - z2)); + z2 += 8; + bs2_offs -= 8; + } + if (bs2_offs > 0) { + acc2 |= ((unsigned long long)(*bs2) << (56 - z2)); + } + z2 += bs2_offs; + + CHECK(z1 == z2); + CHECK(z1 < 64); + //fprintf(stderr, "acc1=%016llx acc2=%016llx z1=z2=%d\n", acc1, acc2, z1); + if (z1) { + if ((acc1 ^ acc2) & (~0ULL << (64 - z1))) { + if (same_upto) { + *same_upto = processed + td::count_leading_zeroes64(acc1 ^ acc2); + } + return acc1 < acc2 ? -1 : 1; + } + } + if (same_upto) { + *same_upto = processed + bit_count; + } + return 0; +} + +int bits_memcmp(ConstBitPtr bs1, ConstBitPtr bs2, std::size_t bit_count, std::size_t* same_upto) { + return bits_memcmp(bs1.ptr, bs1.offs, bs2.ptr, bs2.offs, bit_count, same_upto); +} + +int bits_lexcmp(const unsigned char* bs1, int bs1_offs, std::size_t bs1_bit_count, const unsigned char* bs2, + int bs2_offs, std::size_t bs2_bit_count) { + int res = bits_memcmp(bs1, bs1_offs, bs2, bs2_offs, std::min(bs1_bit_count, bs2_bit_count), 0); + if (res || bs1_bit_count == bs2_bit_count) { + return res; + } + return bs1_bit_count < bs2_bit_count ? -1 : 1; +} + +int bits_lexcmp(ConstBitPtr bs1, std::size_t bs1_bit_count, ConstBitPtr bs2, std::size_t bs2_bit_count) { + return bits_lexcmp(bs1.ptr, bs1.offs, bs1_bit_count, bs2.ptr, bs2.offs, bs2_bit_count); +} + +void bits_store_long_top(unsigned char* to, int to_offs, unsigned long long val, unsigned top_bits) { + CHECK(top_bits <= 64); + if (top_bits <= 0) { + return; + } + to += (to_offs >> 3); + to_offs &= 7; + if (!to_offs && !(top_bits & 7)) { + // good only on little-endian machines! + unsigned long long tmp = td::bswap64(val); + std::memcpy(to, &tmp, top_bits >> 3); + return; + } + unsigned long long z = (unsigned long long)(*to & (-0x100 >> to_offs)) << 56; + z |= (val >> to_offs); + top_bits += to_offs; + if (top_bits > 64) { + as(to) = td::bswap64(z); + z = (val << (8 - to_offs)); + int mask = (0xff >> (top_bits - 64)); + to[8] = (unsigned char)((to[8] & mask) | ((int)z & ~mask)); + } else { + int p = 56, q = 64 - top_bits; + if (q <= 32) { + as(to) = td::bswap32((unsigned)(z >> 32)); + to += 4; + p -= 32; + } + while (p >= q) { + *to++ = (unsigned char)(z >> p); + p -= 8; + } + top_bits = p + 8 - q; + if (top_bits > 0) { + int mask = (0xff >> top_bits); + *to = (unsigned char)((*to & mask) | ((z >> p) & ~mask)); + } + } +} + +void bits_store_long_top(BitPtr to, unsigned long long val, unsigned top_bits) { + bits_store_long_top(to.ptr, to.offs, val, top_bits); +} + +void bits_store_long(BitPtr to, unsigned long long val, unsigned bits) { + bits_store_long_top(to, val << (64 - bits), bits); +} + +unsigned long long bits_load_long_top(const unsigned char* from, int from_offs, unsigned top_bits) { + CHECK(top_bits <= 64); + if (!top_bits) { + return 0; + } + from += (from_offs >> 3); + from_offs &= 7; + if ((unsigned)from_offs + top_bits <= 64) { + unsigned long long tmp; + std::memcpy(&tmp, from, (from_offs + top_bits + 7) >> 3); + return (td::bswap64(tmp) << from_offs) & (std::numeric_limits::max() << (64 - top_bits)); + } else { + unsigned long long z = td::bswap64(as(from)); + z <<= from_offs; + z |= (from[8] >> (8 - from_offs)); + return z & (std::numeric_limits::max() << (64 - top_bits)); + } +} + +unsigned long long bits_load_long_top(ConstBitPtr from, unsigned top_bits) { + return bits_load_long_top(from.ptr, from.offs, top_bits); +} + +unsigned long long bits_load_ulong(ConstBitPtr from, unsigned bits) { + return bits_load_long_top(from, bits) >> (64 - bits); +} + +long long bits_load_long(ConstBitPtr from, unsigned bits) { + return (long long)bits_load_long_top(from, bits) >> (64 - bits); +} + +std::string bits_to_binary(const unsigned char* ptr, int offs, std::size_t len) { + if (!len) { + return ""; + } + std::string s; + s.reserve(len); + ptr += (offs >> 3); + unsigned mask = (0x80 >> (offs & 7)); + unsigned value = *ptr++; + do { + s.push_back(value & mask ? '1' : '0'); + if (!(mask >>= 1)) { + value = *ptr++; + mask = 0x80; + } + } while (--len > 0); + return s; +} + +std::string bits_to_binary(ConstBitPtr bs, std::size_t len) { + return bits_to_binary(bs.ptr, bs.offs, len); +} + +static const char hex_digits[] = "0123456789ABCDEF"; + +std::string bits_to_hex(const unsigned char* ptr, int offs, std::size_t len) { + if (!len) { + return ""; + } + std::string s; + s.reserve((len + 7) >> 2); + ptr += (offs >> 3); + offs &= 7; + unsigned long long acc = *ptr++ & (0xff >> offs); + unsigned bits = 8 - offs; + if (bits > len) { + acc >>= bits - (unsigned)len; + bits = (unsigned)len; + } else { + len -= bits; + while (len >= 8) { + while (len >= 8 && bits <= 56) { + acc <<= 8; + acc |= *ptr++; + bits += 8; + len -= 8; + } + while (bits >= 4) { + bits -= 4; + s.push_back(hex_digits[(acc >> bits) & 15]); + } + } + if (len > 0) { + acc <<= len; + acc |= (*ptr >> (8 - len)); + bits += (unsigned)len; + } + } + int f = bits & 3; + if (f) { + acc = (2 * acc + 1) << (3 - f); + bits += 4 - f; + } + while (bits >= 4) { + bits -= 4; + s.push_back(hex_digits[(acc >> bits) & 15]); + } + CHECK(!bits); + if (f) { + s.push_back('_'); + } + return s; +} + +std::string bits_to_hex(ConstBitPtr bs, std::size_t len) { + return bits_to_hex(bs.ptr, bs.offs, len); +} + +long parse_bitstring_hex_literal(unsigned char* buff, std::size_t buff_size, const char* str, const char* str_end) { + std::size_t hex_digits_count = 0; + bool cmpl = false; + unsigned char* ptr = buff; + const char* rptr = str; + while (rptr < str_end) { + int c = *rptr++; + if (c == ' ' || c == '\t') { + continue; + } + if (cmpl) { + return td::narrow_cast(str - rptr); + } + if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')) { + int val = (c <= '9') ? c - '0' : ((c | 0x20) - 'a' + 10); + if (hex_digits_count >= 2 * buff_size) { + return td::narrow_cast(str - rptr); + } + if (!(hex_digits_count & 1)) { + *ptr = (unsigned char)(val << 4); + } else { + *ptr = (unsigned char)(*ptr | val); + ptr++; + } + hex_digits_count++; + continue; + } + if (c == '_') { + cmpl = true; + } else { + return td::narrow_cast(str - rptr); + } + } + std::size_t bits = 4 * hex_digits_count; + if (cmpl && bits) { + int t = (hex_digits_count & 1) ? (0x100 + *ptr) >> 4 : (0x100 + *--ptr); + while (bits > 0) { + --bits; + if (t & 1) { + break; + } + t >>= 1; + if (t == 1) { + t = 0x100 + *--ptr; + } + } + } + return bits; +} + +long parse_bitstring_binary_literal(BitPtr buff, std::size_t buff_size, const char* str, const char* str_end) { + const char* ptr = str; + while (ptr < str_end && buff_size && (*ptr == '0' || *ptr == '1')) { + *buff++ = (bool)(*ptr++ & 1); + --buff_size; + } + return td::narrow_cast(ptr == str_end ? ptr - str : str - ptr - 1); +} + +void bits_sha256(BitPtr to, ConstBitPtr from, std::size_t size) { + if (from.byte_aligned() && !(size & 7)) { + if (to.byte_aligned()) { + digest::hash_str(to.get_byte_ptr(), from.get_byte_ptr(), size >> 3); + } else { + unsigned char buffer[32]; + digest::hash_str(buffer, from.get_byte_ptr(), size >> 3); + to.copy_from(BitPtr{buffer}, 256); + } + } else { + throw BitstringError{}; + } +} + +} // namespace bitstring + +} // namespace td diff --git a/crypto/common/bitstring.h b/crypto/common/bitstring.h new file mode 100644 index 00000000..a5f0c5db --- /dev/null +++ b/crypto/common/bitstring.h @@ -0,0 +1,662 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once +#include "common/refcnt.hpp" +#include +#include +#include +#include +#include +#include "td/utils/bits.h" + +namespace td { +template +struct BitPtrGen; + +typedef BitPtrGen BitPtr; +typedef BitPtrGen ConstBitPtr; + +struct BitstringError {}; + +namespace bitstring { + +void bits_memcpy(unsigned char* to, int to_offs, const unsigned char* from, int from_offs, std::size_t bit_count); +void bits_memcpy(BitPtr to, ConstBitPtr from, std::size_t bit_count); +void bits_memset(unsigned char* to, int to_offs, bool val, std::size_t bit_count); +void bits_memset(BitPtr to, bool val, std::size_t bit_count); +int bits_memcmp(const unsigned char* bs1, int bs1_offs, const unsigned char* bs2, int bs2_offs, std::size_t bit_count, + std::size_t* same_upto = 0); +int bits_memcmp(ConstBitPtr bs1, ConstBitPtr bs2, std::size_t bit_count, std::size_t* same_upto = 0); +int bits_lexcmp(const unsigned char* bs1, int bs1_offs, std::size_t bs1_bit_count, const unsigned char* bs2, + int bs2_offs, std::size_t bs2_bit_count); +int bits_lexcmp(ConstBitPtr bs1, std::size_t bs1_bit_count, ConstBitPtr bs2, std::size_t bs2_bit_count); +std::size_t bits_memscan(const unsigned char* ptr, int offs, std::size_t bit_count, bool cmp_to); +std::size_t bits_memscan_rev(const unsigned char* ptr, int offs, std::size_t bit_count, bool cmp_to); +std::size_t bits_memscan(ConstBitPtr bs, std::size_t bit_count, bool cmp_to); +std::size_t bits_memscan_rev(ConstBitPtr bs, std::size_t bit_count, bool cmp_to); +void bits_store_long_top(unsigned char* to, int to_offs, unsigned long long val, unsigned top_bits); +void bits_store_long_top(BitPtr to, unsigned long long val, unsigned top_bits); +void bits_store_long(BitPtr to, unsigned long long val, unsigned bits); +unsigned long long bits_load_long_top(const unsigned char* from, int from_offs, unsigned top_bits); +unsigned long long bits_load_long_top(ConstBitPtr from, unsigned top_bits); +long long bits_load_long(ConstBitPtr from, unsigned bits); +unsigned long long bits_load_ulong(ConstBitPtr from, unsigned bits); +long parse_bitstring_hex_literal(unsigned char* buff, std::size_t buff_size, const char* str, const char* str_end); +long parse_bitstring_binary_literal(BitPtr buff, std::size_t buff_size, const char* str, const char* str_end); + +void bits_sha256(BitPtr to, ConstBitPtr from, std::size_t size); + +std::string bits_to_binary(const unsigned char* ptr, int offs, std::size_t len); +std::string bits_to_binary(ConstBitPtr bs, std::size_t len); +std::string bits_to_hex(const unsigned char* ptr, int offs, std::size_t len); +std::string bits_to_hex(ConstBitPtr bs, std::size_t len); + +} // namespace bitstring + +template +struct BitPtrGen { + Pt* ptr; + int offs; + BitPtrGen(Pt* _ptr, int _offs = 0) : ptr(_ptr), offs(_offs) { + } + template + BitPtrGen(BitPtrGen val) : ptr(val.ptr), offs(val.offs) { + } + BitPtrGen& operator+=(int _offs) { + offs += _offs; + return *this; + } + BitPtrGen& operator-=(int _offs) { + offs -= _offs; + return *this; + } + void advance(int _offs) { + offs += _offs; + } + bool byte_aligned() const { + return !(offs & 7); + } + Pt* get_byte_ptr() const { + return ptr + (offs >> 3); + } + void copy_from(BitPtrGen from, unsigned size) const { + bitstring::bits_memcpy(*this, from, size); + } + BitPtrGen& concat(BitPtrGen from, unsigned size) { + bitstring::bits_memcpy(*this, from, size); + offs += size; + return *this; + } + template + void copy_from(const T& from) const { + copy_from(from.bits(), from.size()); + } + template + BitPtrGen& concat(const T& from) { + return concat(from.bits(), from.size()); + } + void fill(bool bit, unsigned size) { + bitstring::bits_memset(*this, bit, size); + } + BitPtrGen& concat_same(bool bit, unsigned size) { + bitstring::bits_memset(*this, bit, size); + offs += size; + return *this; + } + int compare(BitPtrGen other, std::size_t size, std::size_t* same_upto = nullptr) const { + return bitstring::bits_memcmp(*this, other, size, same_upto); + } + bool equals(BitPtrGen other, std::size_t size) const { + return !bitstring::bits_memcmp(*this, other, size); + } + std::size_t scan(bool value, std::size_t len) const { + return bitstring::bits_memscan(*this, len, value); + } + long long get_int(unsigned bits) const { + return bitstring::bits_load_long(*this, bits); + } + unsigned long long get_uint(unsigned bits) const { + return bitstring::bits_load_ulong(*this, bits); + } + void store_uint(unsigned long long val, unsigned n) const { + bitstring::bits_store_long(*this, val, n); + } + void store_int(long long val, unsigned n) const { + bitstring::bits_store_long(*this, val, n); + } + BitPtrGen operator+(int _offs) const { + return BitPtrGen{ptr, offs + _offs}; + } + BitPtrGen operator-(int _offs) const { + return BitPtrGen{ptr, offs - _offs}; + } + BitPtrGen operator++() { + ++offs; + return *this; + } + BitPtrGen operator--() { + --offs; + return *this; + } + BitPtrGen operator++(int) { + return BitPtrGen{ptr, offs++}; + } + BitPtrGen operator--(int) { + return BitPtrGen{ptr, offs--}; + } + bool is_null() const { + return !ptr; + } + bool not_null() const { + return ptr; + } + class BitSelector { + Pt* ptr; + unsigned char mask; + + public: + BitSelector(Pt* _ptr, int offs) { + ptr = _ptr + (offs >> 3); + mask = (unsigned char)(0x80 >> (offs & 7)); + } + bool clear() { + *ptr = (unsigned char)(*ptr & ~mask); + return false; + } + bool set() { + *ptr = (unsigned char)(*ptr | mask); + return true; + } + bool operator=(bool val) { + return val ? set() : clear(); + } + operator bool() const { + return *ptr & mask; + } + }; + BitSelector operator*() const { + return BitSelector{ptr, offs}; + } + BitSelector operator[](int i) const { + return BitSelector{ptr, offs + i}; + } + std::string to_hex(std::size_t len) const { + return bitstring::bits_to_hex(*this, len); + } + std::string to_binary(std::size_t len) const { + return bitstring::bits_to_binary(*this, len); + } +}; + +template +class BitSliceGen { + Rf ref; + Pt* ptr; + unsigned offs, len; + + public: + struct BitSliceError {}; + BitSliceGen() : ref(), ptr(0), offs(0), len(0) { + } + BitSliceGen(Rf _ref, Pt* _ptr, int _offs, unsigned _len) + : ref(std::move(_ref)), ptr(_ptr + (_offs >> 3)), offs(_offs & 7), len(_len) { + } + BitSliceGen(const BitSliceGen& bs, unsigned _offs, unsigned _len); + BitSliceGen(BitSliceGen&& bs, unsigned _offs, unsigned _len); + BitSliceGen(Pt* _ptr, unsigned _len) : ref(), ptr(_ptr), offs(0), len(_len) { + } + ~BitSliceGen() { + } + Pt* get_ptr() const { + return ptr; + } + BitPtrGen bits() const { + return BitPtrGen{ptr, (int)offs}; + } + bool is_valid() const { + return ptr != 0; + } + unsigned char cur_byte() const { + return *ptr; + } + unsigned get_offs() const { + return offs; + } + unsigned size() const { + return len; + } + unsigned byte_size() const { + return (offs + len + 7) >> 3; + } + void ensure_throw(bool cond) const { + if (!cond) { + throw BitSliceError{}; + } + } + BitSliceGen& assign(Rf _ref, Pt* _ptr, unsigned _offs, unsigned _len); + void forget() { + ref.clear(); + ptr = 0; + offs = len = 0; + } + bool operator[](unsigned i) const { + i += offs; + return ptr[i >> 3] & (0x80 >> (i & 7)); + } + bool at(unsigned i) const { + ensure_throw(i < len); + return operator[](i); + } + bool advance_bool(unsigned bits); + bool set_size_bool(unsigned bits) { + if (bits > len) { + return false; + } + len = bits; + return true; + } + BitSliceGen& advance(unsigned bits) { + ensure_throw(advance_bool(bits)); + return *this; + } + BitSliceGen& set_size(unsigned bits) { + ensure_throw(set_size_bool(bits)); + return *this; + } + BitSliceGen subslice(unsigned from, unsigned bits) const & { + return BitSliceGen(*this, from, bits); + } + BitSliceGen subslice(unsigned from, unsigned bits) && { + return BitSliceGen(*this, from, bits); + } + void copy_to(BitPtr to) const { + bitstring::bits_memcpy(to, bits(), size()); + } + int compare(ConstBitPtr other) const { + return bitstring::bits_memcmp(bits(), other, size()); + } + bool operator==(ConstBitPtr other) const { + return !compare(other); + } + bool operator!=(ConstBitPtr other) const { + return compare(other); + } + std::string to_binary() const { + return bitstring::bits_to_binary(ptr, offs, len); + } + std::string to_hex() const { + return bitstring::bits_to_hex(ptr, offs, len); + } + void dump(std::ostream& stream, bool nocr = false) const { + stream << "[" << offs << "," << len << "]"; + if (!nocr) { + stream << std::endl; + } + } + + protected: + inline Pt& cur_byte_w() const { + return *ptr; + } +}; + +template +BitSliceGen::BitSliceGen(const BitSliceGen& bs, unsigned from, unsigned bits) : ref() { + if (from >= bs.size() || bits > bs.size() - from) { + ptr = 0; + offs = len = 0; + return; + } + //bs.dump(std::cout, true); + //std::cout << ".subslice(" << from << "," << bits << ") = "; + ref = bs.ref; + offs = bs.offs + from; + ptr = bs.ptr + (offs >> 3); + offs &= 7; + len = bits; + //dump(std::cout); +} + +template +BitSliceGen::BitSliceGen(BitSliceGen&& bs, unsigned from, unsigned bits) : ref() { + if (from >= bs.size() || bits > bs.size() - from) { + ptr = 0; + offs = len = 0; + return; + } + ref = std::move(bs.ref); + offs = bs.offs + from; + ptr = bs.ptr + (offs >> 3); + offs &= 7; + len = bits; +} + +template +BitSliceGen& BitSliceGen::assign(Rf _ref, Pt* _ptr, unsigned _offs, unsigned _len) { + ref = std::move(_ref); + ptr = _ptr + (_offs >> 3); + offs = (_offs & 7); + len = _len; + return *this; +} + +template +inline bool BitSliceGen::advance_bool(unsigned bits) { + if (len < bits) { + return false; + } + len -= bits; + offs += bits; + ptr += (offs >> 3); + offs &= 7; + return true; +} + +typedef BitSliceGen BitSlice; + +static inline std::string to_hex(const BitSlice& bs) { + return bs.to_hex(); +} + +static inline std::string to_binary(const BitSlice& bs) { + return bs.to_binary(); +} + +class BitSliceWrite : public BitSliceGen { + public: + struct LengthMismatch {}; + BitSliceWrite(RefAny _ref, unsigned char* _ptr, unsigned _offs, unsigned _len) + : BitSliceGen(_ref, _ptr, _offs, _len) { + } + BitSliceWrite() : BitSliceGen() { + } + BitSliceWrite(unsigned char* _ptr, unsigned _len) : BitSliceGen(_ptr, _len) { + } + operator BitSlice&() { + return *reinterpret_cast(this); + } + operator const BitSlice&() const { + return *reinterpret_cast(this); + } + const BitSliceWrite& operator=(const BitSlice& bs) const; + const BitSliceWrite& operator=(bool val) const; +}; + +class BitString : public CntObject { + unsigned char* ptr; + unsigned offs, len, bytes_alloc; + + public: + BitString() : ptr(0), offs(0), len(0), bytes_alloc(0) { + } + explicit BitString(const BitSlice& bs, unsigned reserve_bits = 0); + explicit BitString(unsigned reserve_bits); + + BitString(const BitString&) = delete; + BitString& operator=(const BitString&) = delete; + BitString(BitString&&) = delete; + BitString& operator=(BitString&&) = delete; + ~BitString() { + if (ptr) { + std::free(ptr); + } + } + operator BitSlice() const; + BitString* make_copy() const override; + unsigned size() const { + return len; + } + unsigned byte_size() const { + return (offs + len + 7) >> 3; + } + ConstBitPtr cbits() const { + return ConstBitPtr{ptr, (int)offs}; + } + ConstBitPtr bits() const { + return ConstBitPtr{ptr, (int)offs}; + } + BitPtr bits() { + return BitPtr{ptr, (int)offs}; + } + BitString& reserve_bits(unsigned req_bits); + BitSliceWrite reserve_bitslice(unsigned req_bits); + BitString& append(const BitSlice& bs); + BitSlice subslice(unsigned from, unsigned bits) const; + BitSliceWrite subslice_write(unsigned from, unsigned bits); + std::string to_hex() const { + return bitstring::bits_to_hex(cbits(), size()); + } + std::string to_binary() const { + return bitstring::bits_to_binary(cbits(), size()); + } +}; + +extern std::ostream& operator<<(std::ostream& stream, const BitString& bs); +extern std::ostream& operator<<(std::ostream& stream, Ref bs_ref); + +extern template class Ref; +typedef Ref BitStringRef; + +template +class BitArray { + static constexpr unsigned m = (n + 7) >> 3; + typedef std::array byte_array_t; + typedef unsigned char raw_byte_array_t[m]; + byte_array_t bytes; + + public: + const unsigned char* data() const { + return bytes.data(); + } + unsigned char* data() { + return bytes.data(); + } + unsigned size() const { + return n; + } + const byte_array_t& as_array() const { + return bytes; + } + byte_array_t& as_array() { + return bytes; + } + Slice as_slice() const { + return Slice{data(), m}; + } + MutableSlice as_slice() { + return MutableSlice{data(), m}; + } + ConstBitPtr cbits() const { + return ConstBitPtr{data()}; + } + ConstBitPtr bits() const { + return ConstBitPtr{data()}; + } + BitPtr bits() { + return BitPtr{data()}; + } + BitArray() = default; + BitArray(const BitArray&) = default; + BitArray(const byte_array_t& init_bytes) : bytes(init_bytes) { + } + explicit BitArray(const raw_byte_array_t init_bytes) { + std::memcpy(data(), init_bytes, m); + } + BitArray(ConstBitPtr from) { + bitstring::bits_memcpy(bits(), from, n); + } + template > + explicit BitArray(long long val) { + bitstring::bits_store_long(bits(), val, n); + } + BitArray& operator=(const BitArray&) = default; + BitArray& operator=(const byte_array_t& set_bytes) { + bytes = set_bytes; + return *this; + } + BitArray& operator=(ConstBitPtr from) { + bitstring::bits_memcpy(bits(), from, n); + return *this; + } + BitArray& operator=(const raw_byte_array_t set_byte_array) { + std::memcpy(data(), set_byte_array, m); + return *this; + } + BitSliceWrite write_bitslice() { + return BitSliceWrite{data(), n}; + } + BitSlice as_bitslice() const { + return BitSlice{data(), n}; + } + //operator BitString() const { + //return BitString{as_bitslice()}; + //} + Ref make_bitstring_ref() const { + return td::make_ref(as_bitslice()); + } + unsigned long long to_ulong() const { + return bitstring::bits_load_ulong(bits(), n); + } + long long to_long() const { + return bitstring::bits_load_long(bits(), n); + } + void store_ulong(unsigned long long val) { + bitstring::bits_store_long(bits(), val, n); + } + void store_long(long long val) { + bitstring::bits_store_long(bits(), val, n); + } + void set_same(bool v) { + bytes.fill(static_cast(v ? -1 : 0)); + } + void set_zero() { + set_same(0); + } + void set_zero_s() { + volatile uint8* p = data(); + auto x = m; + while (x--) { + *p++ = 0; + } + } + void set_ones() { + set_same(1); + } + void clear() { + set_zero(); + } + bool is_zero() const { + return bitstring::bits_memscan(cbits(), n, 0) == n; + } + std::string to_hex() const { + return bitstring::bits_to_hex(cbits(), size()); + } + std::string to_binary() const { + return bitstring::bits_to_binary(cbits(), size()); + } + int compare(const BitArray& other) const { + return (n % 8 == 0) ? std::memcmp(data(), other.data(), n / 8) : bitstring::bits_memcmp(bits(), other.bits(), n); + } + bool operator==(const BitArray& other) const { + return (n % 8 == 0) ? (bytes == other.bytes) : !bitstring::bits_memcmp(bits(), other.bits(), n); + } + bool operator!=(const BitArray& other) const { + return (n % 8 == 0) ? (bytes != other.bytes) : bitstring::bits_memcmp(bits(), other.bits(), n); + } + bool operator<(const BitArray& other) const { + return (n % 8 == 0) ? (bytes < other.bytes) : (bitstring::bits_memcmp(bits(), other.bits(), n) < 0); + } + int compare(ConstBitPtr other) const { + return bitstring::bits_memcmp(bits(), other, n); + } + bool operator==(ConstBitPtr other) const { + return !compare(other); + } + bool operator!=(ConstBitPtr other) const { + return compare(other); + } + BitPtr::BitSelector operator[](int i) { + return bits()[i]; + } + bool operator[](int i) const { + return cbits()[i]; + } + void compute_sha256(BitPtr to) const { + bitstring::bits_sha256(to, cbits(), n); + } + void compute_sha256(BitArray<256>& to) const { + bitstring::bits_sha256(to.bits(), cbits(), n); + } + static inline BitArray zero() { + BitArray x; + x.set_zero(); + return x; + } + static inline BitArray ones() { + BitArray x; + x.set_ones(); + return x; + } + BitArray operator^(const BitArray& with) const { + BitArray res; + for (unsigned i = 0; i < m; i++) { + res.bytes[i] = bytes[i] ^ with.bytes[i]; + } + return res; + } + unsigned scan(bool value) const { + return (unsigned)cbits().scan(value, n); + } + unsigned count_leading_zeroes() const { + return scan(false); + } + unsigned count_matching(ConstBitPtr other) const { + std::size_t cnt; + bitstring::bits_memcmp(cbits(), other, n, &cnt); + return (unsigned)cnt; + } + unsigned count_matching(const BitArray& other) const { + return count_matching(other.bits()); + } +}; + +using Bits256 = BitArray<256>; +using Bits128 = BitArray<128>; + +template +std::ostream& operator<<(std::ostream& stream, BitArray bits) { + return stream << bits.to_hex(); +} + +template +Slice as_slice(const BitArray& value) { + return value.as_slice(); +} + +template +MutableSlice as_slice(BitArray& value) { + return value.as_slice(); +} + +template +Ref make_bitstring_ref(const BitArray& value) { + return value.make_bitstring_ref(); +} + +} // namespace td diff --git a/crypto/common/refcnt.cpp b/crypto/common/refcnt.cpp new file mode 100644 index 00000000..5b95c8f3 --- /dev/null +++ b/crypto/common/refcnt.cpp @@ -0,0 +1,55 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "refcnt.hpp" + +#include "td/utils/ScopeGuard.h" + +namespace td { +namespace detail { +struct SafeDeleter { + public: + void retire(const CntObject *ptr) { + if (is_active_) { + to_delete_.push_back(ptr); + return; + } + is_active_ = true; + SCOPE_EXIT { + is_active_ = false; + }; + delete ptr; + while (!to_delete_.empty()) { + auto *ptr = to_delete_.back(); + to_delete_.pop_back(); + delete ptr; + } + } + + private: + std::vector to_delete_; + bool is_active_{false}; +}; + +TD_THREAD_LOCAL SafeDeleter *deleter; +void safe_delete(const CntObject *ptr) { + init_thread_local(deleter); + deleter->retire(ptr); +} +} // namespace detail +} // namespace td diff --git a/crypto/common/refcnt.hpp b/crypto/common/refcnt.hpp new file mode 100644 index 00000000..08a58412 --- /dev/null +++ b/crypto/common/refcnt.hpp @@ -0,0 +1,474 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +#include +#include +#include +#include + +#include "td/utils/StringBuilder.h" +#include "td/utils/logging.h" + +namespace td { + +template +class Ref; + +class CntObject { + private: + mutable std::atomic cnt_; + template + friend class Ref; + + void inc() const { + cnt_.fetch_add(1, std::memory_order_relaxed); + } + bool dec() const { + return cnt_.fetch_sub(1, std::memory_order_acq_rel) == 1; + } + void inc(int cnt) const { + cnt_.fetch_add(cnt, std::memory_order_relaxed); + } + bool dec(int cnt) const { + return cnt_.fetch_sub(cnt, std::memory_order_acq_rel) == cnt; + } + + public: + struct WriteError {}; + CntObject() : cnt_(1) { + } + CntObject(const CntObject& other) : CntObject() { + } + CntObject(CntObject&& other) : CntObject() { + } + CntObject& operator=(const CntObject& other) { + return *this; + } + CntObject& operator=(CntObject&& other) { + return *this; + } + virtual ~CntObject() { + auto cnt = cnt_.load(std::memory_order_relaxed); + (void)cnt; + //TODO: assert(cnt == 0) will fail if object is allocated on stack + assert(cnt == 0 || cnt == 1); + } + virtual CntObject* make_copy() const { + throw WriteError(); + } + bool is_unique() const { + return cnt_.load(std::memory_order_acquire) == 1; + } + int get_refcnt() const { + // use std::memory_order_acquire + return cnt_.load(std::memory_order_acquire); + } + void assert_unique() const { + assert(is_unique()); + } +}; + +typedef Ref RefAny; + +template +class Cnt : public CntObject { + T value; + + public: + template + Cnt(Args&&... args) : value(std::forward(args)...) { + ///std::cout << "(N " << (void*)this << ")"; + } + Cnt(const Cnt& x) : CntObject(), value(x.value) { + ///std::cout << "(C)"; + } + virtual ~Cnt() { + ///std::cout << "(D " << (void*)this << ")"; + } + T* operator->() { + return &value; + } + const T* operator->() const { + return &value; + } + T& operator*() { + return value; + } + const T& operator*() const { + return value; + } + Cnt* make_copy() const override { + ///std::cout << "(c " << (const void*)this << ")"; + return new Cnt{value}; + } +}; + +template +struct RefValue { + using Type = T; + static Type& make_ref(T* ptr) { + return *ptr; + } + static const Type& make_const_ref(const T* ptr) { + return *ptr; + } + static Type* make_ptr(T* ptr) { + return ptr; + } + static const Type* make_const_ptr(const T* ptr) { + return ptr; + } +}; + +template +struct RefValue> { + using Type = T; + static Type& make_ref(Cnt* ptr) { + return **ptr; + } + static const Type& make_const_ref(const Cnt* ptr) { + return **ptr; + } + static Type* make_ptr(Cnt* ptr) { + return &(**ptr); + } + static const Type* make_const_ptr(const Cnt* ptr) { + return &(**ptr); + } +}; + +struct static_cast_ref {}; + +namespace detail { +void safe_delete(const CntObject* ptr); +} +template +class Ref { + T* ptr; + + template + friend class Ref; + + public: + struct NullRef {}; + Ref() : ptr(0) { + } + //explicit Ref(bool init) : ptr(init ? new T : 0) { + //} + template + explicit Ref(bool init, Args&&... args) : ptr(0) { + //assert(init); + ptr = new T(std::forward(args)...); + } + /* + explicit Ref(const T& c) : ptr(&c) { + ptr.inc(); + } + */ + explicit Ref(T* pc) : ptr(pc) { + if (ptr) { + acquire_shared(ptr); + } + } + explicit Ref(const T* pc) : ptr(const_cast(pc)) { + if (ptr) { + acquire_shared(ptr); + } + } + explicit Ref(const T& obj) : ptr(obj.make_copy()) { + } + Ref(const Ref& r) : ptr(r.ptr) { + if (ptr) { + acquire_shared(ptr); + ///std::cout << "(rc+ " << (const void*)ptr << ")"; + } + } + Ref(Ref&& r) noexcept : ptr(std::move(r.ptr)) { + r.ptr = 0; + } + + T* release() { + auto res = ptr; + ptr = nullptr; + return res; + } + struct acquire_t {}; + Ref(T* ptr, acquire_t) : ptr(ptr) { + } + + template + Ref(const Ref& r, std::enable_if_t::value, int> t = 0) : ptr(static_cast(r.ptr)) { + static_assert(std::is_base_of::value, "Invalid static Ref conversion"); + if (ptr) { + acquire_shared(ptr); + } + } + + template + explicit Ref(const Ref& r, + std::enable_if_t::value && std::is_base_of::value, int> t = 0) + : ptr(dynamic_cast(r.ptr)) { + static_assert(std::is_base_of::value, "Invalid dynamic Ref conversion"); + if (ptr) { + acquire_shared(ptr); + //std::cout << "(rv+ " << (const void*)ptr << ")"; + } else { + //std::cout << "(error converting " << (const void*)r.ptr << ")"; + } + } + + template + Ref(static_cast_ref, const Ref& r, std::enable_if_t::value, int> t = 0) + : ptr(static_cast(r.ptr)) { + static_assert(std::is_base_of::value, "Invalid static Ref downcast"); + if (r.ptr) { + acquire_shared(ptr); + } else { + ptr = nullptr; + } + } + + template + Ref(Ref&& r, std::enable_if_t::value, int> t = 0) : ptr(static_cast(r.ptr)) { + static_assert(std::is_base_of::value, "Invalid static Ref conversion"); + r.ptr = nullptr; + } + + template + explicit Ref(Ref&& r, std::enable_if_t::value && std::is_base_of::value, int> t = 0) + : ptr(dynamic_cast(r.ptr)) { + static_assert(std::is_base_of::value, "Invalid dynamic Ref conversion"); + if (!ptr && r.ptr) { + release_shared(r.ptr); + } + r.ptr = nullptr; + } + + template + Ref(static_cast_ref, Ref&& r, std::enable_if_t::value, int> t = 0) noexcept + : ptr(static_cast(r.ptr)) { + static_assert(std::is_base_of::value, "Invalid static Ref downcast"); + if (r.ptr) { + r.ptr = nullptr; + } else { + ptr = nullptr; + } + } + + ~Ref() { + clear(); + } + Ref& operator=(const Ref& r); + template + Ref& operator=(const Ref& r); + Ref& operator=(Ref&& r); + template + Ref& operator=(Ref&& r); + const typename RefValue::Type* operator->() const { + if (!ptr) { + CHECK(ptr && "deferencing null Ref"); + throw NullRef{}; + } + return RefValue::make_const_ptr(ptr); + } + const typename RefValue::Type& operator*() const { + if (!ptr) { + CHECK(ptr && "deferencing null Ref"); + throw NullRef{}; + } + return RefValue::make_const_ref(ptr); + } + const T* get() const { + return ptr; + } + bool is_null() const { + return ptr == 0; + } + bool not_null() const { + return ptr != 0; + } + bool is_unique() const { + if (!ptr) { + CHECK(ptr && "defererencing null Ref"); + throw NullRef{}; + } + return ptr->is_unique(); + } + void clear() { + if (ptr) { + ///std::cout << "(r- " << (const void*)ptr << ")"; + release_shared(ptr); + ptr = 0; + } + } + void swap(Ref& r) { + std::swap(ptr, r.ptr); + } + Ref& operator^=(const Ref& r); + Ref& operator^=(Ref&& r); + Ref& operator&=(bool retain); + bool operator==(const Ref& r) const; + bool operator!=(const Ref& r) const; + typename RefValue::Type& write(); + typename RefValue::Type& unique_write() const; + + public: + template + static void release_shared(S* obj, int cnt = 1) { + if (obj->dec(cnt)) { + detail::safe_delete(obj); + } + } + template + static void acquire_shared(S* obj, int cnt = 1) { + obj->inc(cnt); + } + + private: + void assign(T* p) { + ptr = p; + if (p) { + acquire_shared(p); + ///std::cout << "(r+ " << (const void*)ptr << ")"; + } + } +}; + +template +Ref make_ref(Args&&... args) { + return Ref{true, std::forward(args)...}; +} + +template +Ref> make_cnt_ref(Args&&... args) { + return Ref>{true, std::forward(args)...}; +} + +template +td::StringBuilder& operator<<(td::StringBuilder& sb, const Ref& ref) { + if (ref.is_null()) { + return sb << "nullptr"; + } + return sb << *ref; +} + +template +Ref& Ref::operator=(const Ref& r) { + if (ptr != r.ptr) { + clear(); + assign(r.ptr); + } + return *this; +} + +template +template +Ref& Ref::operator=(const Ref& r) { + if (ptr != static_cast(r.ptr)) { + clear(); + assign(r.ptr); + } + return *this; +} + +template +Ref& Ref::operator=(Ref&& r) { + clear(); + ptr = r.ptr; + r.ptr = 0; + return *this; +} + +template +template +Ref& Ref::operator=(Ref&& r) { + clear(); + ptr = r.ptr; + r.ptr = 0; + return *this; +} + +template +typename RefValue::Type& Ref::write() { + if (!ptr) { + throw CntObject::WriteError(); + } + if (!ptr->is_unique()) { + T* copy = dynamic_cast(ptr->make_copy()); + if (!copy) { + throw CntObject::WriteError(); + } + release_shared(ptr); + ptr = copy; + } + return RefValue::make_ref(ptr); +} + +template +typename RefValue::Type& Ref::unique_write() const { + if (!ptr || !ptr->is_unique()) { + throw CntObject::WriteError(); + } + return RefValue::make_ref(ptr); +} + +template +Ref& Ref::operator^=(const Ref& r) { + if (r.ptr && r.ptr != ptr) { + clear(); + assign(r.ptr); + } + return *this; +} + +template +Ref& Ref::operator^=(Ref&& r) { + if (r.ptr && r.ptr != ptr) { + clear(); + ptr = r.ptr; + r.ptr = 0; + } + return *this; +} + +template +Ref& Ref::operator&=(bool retain) { + if (!retain && ptr) { + clear(); + } + return *this; +} + +template +bool Ref::operator==(const Ref& r) const { + return ptr == r.ptr; +} + +template +bool Ref::operator!=(const Ref& r) const { + return ptr != r.ptr; +} + +template +void swap(Ref& r1, Ref& r2) { + r1.swap(r2); +} + +} // namespace td diff --git a/crypto/common/refint.cpp b/crypto/common/refint.cpp new file mode 100644 index 00000000..662322c6 --- /dev/null +++ b/crypto/common/refint.cpp @@ -0,0 +1,321 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "common/refint.h" +#include +#include + +#include "td/utils/StringBuilder.h" +#include "td/utils/Slice.h" + +namespace td { + +template class Cnt; +template class Ref>; + +RefInt256 operator+(RefInt256 x, RefInt256 y) { + (x.write() += *y).normalize(); + return x; +} + +RefInt256 operator+(RefInt256 x, long long y) { + x.write().add_tiny(y).normalize(); + return x; +} + +RefInt256 operator-(RefInt256 x, RefInt256 y) { + (x.write() -= *y).normalize(); + return x; +} + +RefInt256 operator-(RefInt256 x, long long y) { + x.write().add_tiny(-y).normalize(); + return x; +} + +RefInt256 operator-(RefInt256 x) { + x.write().negate().normalize(); + return x; +} + +RefInt256 operator~(RefInt256 x) { + x.write().logical_not().normalize(); + return x; +} + +RefInt256 operator*(RefInt256 x, RefInt256 y) { + RefInt256 z{true, 0}; + z.write().add_mul(*x, *y).normalize(); + return z; +} + +RefInt256 operator*(RefInt256 x, long long y) { + x.write().mul_short_opt(y).normalize(); + return x; +} + +RefInt256 operator/(RefInt256 x, RefInt256 y) { + RefInt256 quot{true}; + x.write().mod_div(*y, quot.write()); + quot.write().normalize(); + return quot; +} + +RefInt256 div(RefInt256 x, RefInt256 y, int round_mode) { + RefInt256 quot{true}; + x.write().mod_div(*y, quot.write(), round_mode); + quot.write().normalize(); + return quot; +} + +RefInt256 operator%(RefInt256 x, RefInt256 y) { + BigInt256 quot; + x.write().mod_div(*y, quot); + return x; +} + +RefInt256 mod(RefInt256 x, RefInt256 y, int round_mode) { + BigInt256 quot; + x.write().mod_div(*y, quot, round_mode); + return x; +} + +std::pair divmod(RefInt256 x, RefInt256 y, int round_mode) { + RefInt256 quot{true}; + x.write().mod_div(*y, quot.write(), round_mode); + quot.write().normalize(); + return std::make_pair(std::move(quot), std::move(x)); +} + +RefInt256 operator&(RefInt256 x, RefInt256 y) { + x.write() &= *y; + return x; +} + +RefInt256 operator|(RefInt256 x, RefInt256 y) { + x.write() |= *y; + return x; +} + +RefInt256 operator^(RefInt256 x, RefInt256 y) { + x.write() ^= *y; + return x; +} + +RefInt256 operator<<(RefInt256 x, int y) { + (x.write() <<= y).normalize(); + return x; +} + +RefInt256 operator>>(RefInt256 x, int y) { + (x.write() >>= y).normalize(); + return x; +} + +RefInt256 rshift(RefInt256 x, int y, int round_mode) { + x.write().rshift(y, round_mode).normalize(); + return x; +} + +RefInt256& operator+=(RefInt256& x, RefInt256 y) { + (x.write() += *y).normalize(); + return x; +} + +RefInt256& operator+=(RefInt256& x, long long y) { + x.write().add_tiny(y).normalize(); + return x; +} + +RefInt256& operator-=(RefInt256& x, RefInt256 y) { + (x.write() -= *y).normalize(); + return x; +} + +RefInt256& operator-=(RefInt256& x, long long y) { + x.write().add_tiny(-y).normalize(); + return x; +} + +RefInt256& operator*=(RefInt256& x, RefInt256 y) { + RefInt256 z{true, 0}; + z.write().add_mul(*x, *y).normalize(); + return x = z; +} + +RefInt256& operator*=(RefInt256& x, long long y) { + x.write().mul_short_opt(y).normalize(); + return x; +} + +RefInt256& operator/=(RefInt256& x, RefInt256 y) { + RefInt256 quot{true}; + x.write().mod_div(*y, quot.write()); + quot.write().normalize(); + return x = quot; +} + +RefInt256& operator%=(RefInt256& x, RefInt256 y) { + BigInt256 quot; + x.write().mod_div(*y, quot); + return x; +} + +RefInt256& operator&=(RefInt256& x, RefInt256 y) { + x.write() &= *y; + return x; +} + +RefInt256& operator|=(RefInt256& x, RefInt256 y) { + x.write() |= *y; + return x; +} + +RefInt256& operator^=(RefInt256& x, RefInt256 y) { + x.write() ^= *y; + return x; +} + +RefInt256& operator<<=(RefInt256& x, int y) { + (x.write() <<= y).normalize(); + return x; +} + +RefInt256& operator>>=(RefInt256& x, int y) { + (x.write() >>= y).normalize(); + return x; +} + +int cmp(RefInt256 x, RefInt256 y) { + return x->cmp(*y); +} + +int cmp(RefInt256 x, long long y) { + return x->cmp(y); +} + +int sgn(RefInt256 x) { + return x->sgn(); +} + +extern RefInt256 make_refint(long long x) { + auto xx = td::RefInt256{true, x}; + xx.unique_write().normalize(); + return xx; +} + +std::string dec_string(RefInt256 x) { + return x.is_null() ? "(null)" : (x.is_unique() ? x.unique_write().to_dec_string_destroy() : x->to_dec_string()); +} + +std::string dec_string2(RefInt256&& x) { + return x.is_null() ? "(null)" : (x.is_unique() ? x.unique_write().to_dec_string_destroy() : x->to_dec_string()); +} + +std::string hex_string(RefInt256 x, bool upcase) { + return x.is_null() ? "(null)" : x->to_hex_string(upcase); +} + +std::string binary_string(RefInt256 x) { + return x.is_null() ? "(null)" : x->to_binary_string(); +} + +std::ostream& operator<<(std::ostream& os, const RefInt256& x) { + //std::cout << ""; + //return os; +} + +std::ostream& operator<<(std::ostream& os, RefInt256&& x) { + //std::cout << ""; + //return os; +} + +StringBuilder& operator<<(StringBuilder& sb, const RefInt256& x) { + return sb << dec_string(x); +} + +RefInt256 dec_string_to_int256(const std::string& s) { + return dec_string_to_int256(td::Slice{s}); +} + +RefInt256 dec_string_to_int256(td::Slice s) { + if (s.size() > 255) { + return {}; + } + RefInt256 x{true}; + if (x.unique_write().parse_dec(s.begin(), (int)s.size()) == (int)s.size()) { + return x; + } else { + return {}; + } +} + +RefInt256 hex_string_to_int256(const std::string& s) { + return hex_string_to_int256(td::Slice{s}); +} + +RefInt256 hex_string_to_int256(td::Slice s) { + if (s.size() > 255) { + return {}; + } + RefInt256 x{true}; + if (x.unique_write().parse_hex(s.begin(), (int)s.size()) == (int)s.size()) { + return x; + } else { + return {}; + } +} + +RefInt256 string_to_int256(const std::string& s) { + return string_to_int256(td::Slice{s}); +} + +RefInt256 string_to_int256(td::Slice s) { + if (s.size() >= 3 && s[0] == '-' && s[1] == '0' && s[2] == 'x') { + auto x = hex_string_to_int256(td::Slice(s.begin() + 3, s.end())); + if (x.not_null()) { + x.write().negate(); + } + return x; + } else if (s.size() >= 2 && s[0] == '0' && s[1] == 'x') { + return hex_string_to_int256(td::Slice(s.begin() + 2, s.end())); + } else { + return dec_string_to_int256(s); + } +} + +namespace literals { + +RefInt256 operator""_ri256(const char* str, std::size_t str_len) { + RefInt256 x{true}; + x->enforce(x.unique_write().parse_dec(str, (int)str_len) == (int)str_len); + return x; +} + +RefInt256 operator""_rx256(const char* str, std::size_t str_len) { + RefInt256 x{true}; + x->enforce(x.unique_write().parse_hex(str, (int)str_len) == (int)str_len); + return x; +} + +} // namespace literals +} // namespace td diff --git a/crypto/common/refint.h b/crypto/common/refint.h new file mode 100644 index 00000000..a828caf1 --- /dev/null +++ b/crypto/common/refint.h @@ -0,0 +1,127 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +#include "common/refcnt.hpp" +#include "common/bigint.hpp" +#include +#include + +namespace td { +class StringBuilder; + +extern template class Cnt; +extern template class Ref>; +typedef Cnt CntInt256; +typedef Ref RefInt256; + +extern RefInt256 operator+(RefInt256 x, RefInt256 y); +extern RefInt256 operator+(RefInt256 x, long long y); +extern RefInt256 operator-(RefInt256 x, RefInt256 y); +extern RefInt256 operator-(RefInt256 x, long long y); +extern RefInt256 operator*(RefInt256 x, RefInt256 y); +extern RefInt256 operator*(RefInt256 x, long long y); +extern RefInt256 operator/(RefInt256 x, RefInt256 y); +extern RefInt256 operator%(RefInt256 x, RefInt256 y); +extern RefInt256 div(RefInt256 x, RefInt256 y, int round_mode = -1); +extern RefInt256 mod(RefInt256 x, RefInt256 y, int round_mode = -1); +extern std::pair divmod(RefInt256 x, RefInt256 y, int round_mode = -1); +extern RefInt256 operator-(RefInt256 x); +extern RefInt256 operator&(RefInt256 x, RefInt256 y); +extern RefInt256 operator|(RefInt256 x, RefInt256 y); +extern RefInt256 operator^(RefInt256 x, RefInt256 y); +extern RefInt256 operator~(RefInt256 x); +extern RefInt256 operator<<(RefInt256 x, int y); +extern RefInt256 operator>>(RefInt256 x, int y); +extern RefInt256 rshift(RefInt256 x, int y, int round_mode = -1); + +extern RefInt256& operator+=(RefInt256& x, RefInt256 y); +extern RefInt256& operator+=(RefInt256& x, long long y); +extern RefInt256& operator-=(RefInt256& x, RefInt256 y); +extern RefInt256& operator-=(RefInt256& x, long long y); +extern RefInt256& operator*=(RefInt256& x, RefInt256 y); +extern RefInt256& operator*=(RefInt256& x, long long y); +extern RefInt256& operator/=(RefInt256& x, RefInt256 y); +extern RefInt256& operator%=(RefInt256& x, RefInt256 y); + +extern RefInt256& operator&=(RefInt256& x, RefInt256 y); +extern RefInt256& operator|=(RefInt256& x, RefInt256 y); +extern RefInt256& operator^=(RefInt256& x, RefInt256 y); +extern RefInt256& operator<<=(RefInt256& x, int y); +extern RefInt256& operator>>=(RefInt256& x, int y); + +template +bool operator==(RefInt256 x, T y) { + return cmp(x, y) == 0; +} + +template +bool operator!=(RefInt256 x, T y) { + return cmp(x, y) != 0; +} + +template +bool operator<(RefInt256 x, T y) { + return cmp(x, y) < 0; +} + +template +bool operator>(RefInt256 x, T y) { + return cmp(x, y) > 0; +} + +template +bool operator<=(RefInt256 x, T y) { + return cmp(x, y) <= 0; +} + +template +bool operator>=(RefInt256 x, T y) { + return cmp(x, y) >= 0; +} + +extern int cmp(RefInt256 x, RefInt256 y); +extern int cmp(RefInt256 x, long long y); +extern int sgn(RefInt256 x); + +extern RefInt256 make_refint(long long x); + +extern std::string dec_string(RefInt256 x); +extern std::string dec_string2(RefInt256&& x); +extern std::string hex_string(RefInt256 x, bool upcase = false); +extern std::string binary_string(RefInt256 x); + +extern RefInt256 dec_string_to_int256(const std::string& s); +extern RefInt256 dec_string_to_int256(td::Slice s); +extern RefInt256 hex_string_to_int256(const std::string& s); +extern RefInt256 hex_string_to_int256(td::Slice s); +extern RefInt256 string_to_int256(const std::string& s); +extern RefInt256 string_to_int256(td::Slice s); + +extern std::ostream& operator<<(std::ostream& os, const RefInt256& x); +extern std::ostream& operator<<(std::ostream& os, RefInt256&& x); +extern td::StringBuilder& operator<<(td::StringBuilder& os, const RefInt256& x); + +namespace literals { + +extern RefInt256 operator""_ri256(const char* str, std::size_t str_len); +extern RefInt256 operator""_rx256(const char* str, std::size_t str_len); + +} // namespace literals +} // namespace td diff --git a/crypto/common/util.cpp b/crypto/common/util.cpp new file mode 100644 index 00000000..72118152 --- /dev/null +++ b/crypto/common/util.cpp @@ -0,0 +1,212 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "util.h" + +#include + +namespace td { + +std::size_t compute_base64_encoded_size(size_t bindata_size) { + return ((bindata_size + 2) / 3) << 2; +} + +const char base64_table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; +const char base64_url_table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"; +const unsigned char base64_dec_table[256] = { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0x7e, 0, 0xbe, 0, 0x7f, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, + 0xfd, 0, 0, 0, 1, 0, 0, 0, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, + 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0, 0, 0, 0, + 0xbf, 0, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, + 0xeb, 0xec, 0xed, 0xee, 0xef, 0xf0, 0xf1, 0xf2, 0xf3, 0, 0, 0, 0, 0}; + +std::size_t buff_base64_encode(td::MutableSlice buffer, td::Slice raw, bool base64_url) { + std::size_t orig_size = raw.size(), res_size = compute_base64_encoded_size(orig_size); + if (res_size > buffer.size()) { + return 0; + } + const char *table = base64_url ? base64_url_table : base64_table; + char *wptr = buffer.data(); + unsigned x; + std::size_t i; + for (i = 0; i < orig_size - 2; i += 3) { + x = (((unsigned)(unsigned char)raw[i]) << 16) | (((unsigned)(unsigned char)raw[i + 1]) << 8) | + ((unsigned)(unsigned char)raw[i + 2]); + *wptr++ = table[(x >> 18) & 0x3f]; + *wptr++ = table[(x >> 12) & 0x3f]; + *wptr++ = table[(x >> 6) & 0x3f]; + *wptr++ = table[x & 0x3f]; + } + switch (orig_size - i) { + case 1: + x = (((unsigned)(unsigned char)raw[i]) << 16); + *wptr++ = table[(x >> 18) & 0x3f]; + *wptr++ = table[(x >> 12) & 0x3f]; + *wptr++ = '='; + *wptr++ = '='; + break; + case 2: + x = (((unsigned)(unsigned char)raw[i]) << 16) | (((unsigned)(unsigned char)raw[i + 1]) << 8); + *wptr++ = table[(x >> 18) & 0x3f]; + *wptr++ = table[(x >> 12) & 0x3f]; + *wptr++ = table[(x >> 6) & 0x3f]; + *wptr++ = '='; + } + CHECK(wptr == buffer.data() + res_size); + return res_size; +} + +std::string str_base64_encode(std::string raw, bool base64_url) { + return str_base64_encode(td::Slice{raw}, base64_url); +} + +std::string str_base64_encode(td::Slice raw, bool base64_url) { + std::size_t res_size = compute_base64_encoded_size(raw.size()); + std::string s; + s.resize(res_size); + if (res_size) { + buff_base64_encode(td::MutableSlice{const_cast(s.data()), s.size()}, raw, base64_url); + } + return s; +} + +bool is_valid_base64(std::string encoded, bool allow_base64_url) { + return is_valid_base64(td::Slice{encoded}, allow_base64_url); +} + +bool is_valid_base64(td::Slice encoded, bool allow_base64_url) { + const unsigned char *ptr = (const unsigned char *)encoded.data(), *end = ptr + encoded.size(); + if (encoded.size() & 3) { + return false; + } + unsigned mode = (allow_base64_url ? 0xc0 : 0x40); + while (ptr < end && (base64_dec_table[*ptr] & mode)) { + ptr++; + } + std::size_t d = end - ptr; + if (d > 2) { + return false; + } + while (ptr < end && *ptr == '=') { + ptr++; + } + return ptr == end; +} + +td::int32 decoded_base64_size(std::string encoded, bool allow_base64_url) { + return decoded_base64_size(td::Slice{encoded}, allow_base64_url); +} + +td::int32 decoded_base64_size(td::Slice encoded, bool allow_base64_url) { + const unsigned char *ptr = (const unsigned char *)encoded.data(), *end = ptr + encoded.size(); + if (encoded.size() & 3) { + return -1; + } + if (encoded.size() > static_cast(std::numeric_limits::max())) { + return -1; + } + if (end == ptr) { + return 0; + } + auto s = static_cast((encoded.size() >> 2) * 3); + if (end[-1] == '=') { + s--; + if (end[-2] == '=') { + s--; + } + } + return s; +} + +std::size_t buff_base64_decode(td::MutableSlice buffer, td::Slice encoded, bool allow_base64_url) { + if ((encoded.size() & 3) || !encoded.size()) { + return 0; + } + std::size_t n = (encoded.size() >> 2); + const unsigned char *ptr = (const unsigned char *)encoded.data(), *end = ptr + encoded.size(); + unsigned q = (end[-1] == '=' ? (end[-2] == '=' ? 2 : 1) : 0); + if (buffer.size() + q < n * 3) { + return 0; + } + unsigned char *wptr = (unsigned char *)buffer.data(), *wend = wptr + buffer.size(); + unsigned mode = (allow_base64_url ? 0xc0 : 0x40); + for (std::size_t i = 0; i < n; i++) { + unsigned x = 0; + for (std::size_t j = 0; j < 4; j++) { + unsigned z = base64_dec_table[ptr[4 * i + j]]; + if (!(z & mode) && z != 1 && (i < n - 1 || j + q < 4)) { + return 0; + } + x = (x << 6) | (z & 0x3f); + } + if (i < n - 1) { + *wptr++ = (unsigned char)(x >> 16); + *wptr++ = (unsigned char)(x >> 8); + *wptr++ = (unsigned char)x; + } else { + for (; q < 3; q++) { + *wptr++ = (unsigned char)(x >> 16); + x <<= 8; + } + break; + } + } + CHECK(wptr <= wend); + return wptr - (unsigned char *)buffer.data(); +} + +td::BufferSlice base64_decode(std::string encoded, bool allow_base64_url) { + return base64_decode(td::Slice{encoded}, allow_base64_url); +} + +td::BufferSlice base64_decode(td::Slice encoded, bool allow_base64_url) { + auto s = decoded_base64_size(encoded, allow_base64_url); + if (s <= 0) { + return td::BufferSlice{}; + } + td::BufferSlice res{static_cast(s)}; + auto r = buff_base64_decode(res.as_slice(), encoded, allow_base64_url); + if (!r) { + return td::BufferSlice{}; + } + CHECK(r == static_cast(s)); + return res; +} + +std::string str_base64_decode(std::string encoded, bool allow_base64_url) { + return str_base64_decode(td::Slice{encoded}, allow_base64_url); +} + +std::string str_base64_decode(td::Slice encoded, bool allow_base64_url) { + auto s = decoded_base64_size(encoded, allow_base64_url); + if (s <= 0) { + return std::string{}; + } + std::string res; + res.resize(static_cast(s)); + auto r = buff_base64_decode(td::MutableSlice{const_cast(res.data()), res.size()}, encoded, allow_base64_url); + if (!r) { + return std::string{}; + } + CHECK(r == static_cast(s)); + return res; +} + +} // namespace td diff --git a/crypto/common/util.h b/crypto/common/util.h new file mode 100644 index 00000000..2a594efc --- /dev/null +++ b/crypto/common/util.h @@ -0,0 +1,42 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once +#include +#include "td/utils/Slice.h" +#include "td/utils/buffer.h" + +namespace td { + +std::size_t compute_base64_encoded_size(size_t bindata_size); +std::size_t buff_base64_encode(td::MutableSlice buffer, td::Slice raw, bool base64_url = false); +std::string str_base64_encode(std::string raw, bool base64_url = false); +std::string str_base64_encode(td::Slice raw, bool base64_url = false); + +bool is_valid_base64(std::string encoded, bool allow_base64_url = true); +bool is_valid_base64(td::Slice encoded, bool allow_base64_url = true); +td::int32 decoded_base64_size(std::string encoded, bool allow_base64_url = true); +td::int32 decoded_base64_size(td::Slice encoded, bool allow_base64_url = true); + +std::size_t buff_base64_decode(td::MutableSlice buffer, td::Slice data, bool allow_base64_url = true); +td::BufferSlice base64_decode(std::string encoded, bool allow_base64_url = true); +td::BufferSlice base64_decode(td::Slice encoded, bool allow_base64_url = true); +std::string str_base64_decode(std::string encoded, bool allow_base64_url = true); +std::string str_base64_decode(td::Slice encoded, bool allow_base64_url = true); + +} // namespace td diff --git a/crypto/ellcurve/Ed25519.cpp b/crypto/ellcurve/Ed25519.cpp new file mode 100644 index 00000000..dca882cf --- /dev/null +++ b/crypto/ellcurve/Ed25519.cpp @@ -0,0 +1,280 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "Ed25519.h" + +#include "td/utils/Random.h" + +namespace crypto { +namespace Ed25519 { + +bool all_bytes_same(const unsigned char *str, std::size_t size) { + unsigned char c = str[0]; + for (std::size_t i = 0; i < size; i++) { + if (str[i] != c) { + return false; + } + } + return true; +} + +void PublicKey::clear(void) { + if (inited != pk_empty) { + std::memset(pubkey, 0, pubkey_bytes); + PubKey.zeroize(); + PubKey_xz.zeroize(); + } + inited = pk_empty; +} + +PublicKey::PublicKey(const unsigned char pub_key[pubkey_bytes]) + : inited(pk_empty), PubKey(ellcurve::Fp25519()), PubKey_xz(ellcurve::Fp25519()) { + import_public_key(pub_key); +} + +PublicKey::PublicKey(const ellcurve::TwEdwardsCurve::SegrePoint &Pub_Key) + : inited(pk_empty), PubKey(ellcurve::Fp25519()), PubKey_xz(ellcurve::Fp25519()) { + import_public_key(Pub_Key); +} + +bool PublicKey::import_public_key(const unsigned char pub_key[pubkey_bytes]) { + clear(); + if (all_bytes_same(pub_key, pubkey_bytes)) { + return false; + } + bool ok = false; + PubKey = ellcurve::Ed25519().import_point(pub_key, ok); + if (!ok) { + clear(); + return false; + } + std::memcpy(pubkey, pub_key, pubkey_bytes); + PubKey_xz.X = PubKey.Z + PubKey.Y; + PubKey_xz.Z = PubKey.Z - PubKey.Y; + inited = pk_init; + return true; +} + +bool PublicKey::import_public_key(const ellcurve::TwEdwardsCurve::SegrePoint &Pub_Key) { + clear(); + if (!Pub_Key.is_valid()) { + return false; + } + PubKey = Pub_Key; + PubKey_xz.X = PubKey.Z + PubKey.Y; + PubKey_xz.Z = PubKey.Z - PubKey.Y; + inited = pk_init; + + if (!PubKey.export_point(pubkey)) { + clear(); + return false; + } + return true; +} + +bool PublicKey::export_public_key(unsigned char pubkey_buffer[pubkey_bytes]) const { + if (inited != pk_init) { + std::memset(pubkey_buffer, 0, pubkey_bytes); + return false; + } else { + std::memcpy(pubkey_buffer, pubkey, pubkey_bytes); + return true; + } +} + +bool PublicKey::check_message_signature(const unsigned char signature[sign_bytes], const unsigned char *message, + std::size_t msg_size) { + if (inited != pk_init) { + return false; + } + unsigned char hash[64]; + { + digest::SHA512 hasher(signature, 32); + hasher.feed(pubkey, 32); + hasher.feed(message, msg_size); + hasher.extract(hash); + } + auto &E = ellcurve::Ed25519(); + const arith::Bignum &L = E.get_ell(); + arith::Bignum H, S; + S.import_lsb(signature + 32, 32); + H.import_lsb(hash, 64); + H %= L; + H = L - H; + auto sG = E.power_gen(S); + auto hA = E.power_point(PubKey, H); + auto pR1 = E.add_points(sG, hA); + unsigned char pR1_bytes[32]; + if (!pR1.export_point(pR1_bytes)) { + return false; + } + return !std::memcmp(pR1_bytes, signature, 32); +} + +// --------------------- +class PrivateKey; + +bool PrivateKey::random_private_key(bool strong) { + inited = false; + if (!prng::rand_gen().rand_bytes(privkey, privkey_bytes, strong)) { + clear(); + return false; + } + return process_private_key(); +} + +void PrivateKey::clear(void) { + std::memset(privkey, 0, privkey_bytes); + std::memset(priv_salt, 0, sizeof(priv_salt)); + priv_exp.clear(); + PubKey.clear(); + inited = false; +} + +bool PrivateKey::import_private_key(const unsigned char pk[privkey_bytes]) { + clear(); + if (all_bytes_same(pk, privkey_bytes)) { + return false; + } + std::memcpy(privkey, pk, privkey_bytes); + return process_private_key(); +} + +bool PrivateKey::export_private_key(unsigned char pk[privkey_bytes]) const { // careful! + if (!inited) { + std::memset(pk, 0, privkey_bytes); + return false; + } else { + std::memcpy(pk, privkey, privkey_bytes); + return true; + } +} + +bool PrivateKey::process_private_key() { + unsigned char buff[64]; + digest::hash_str(buff, privkey, privkey_bytes); + std::memcpy(priv_salt, buff + 32, 32); + buff[0] = (unsigned char)(buff[0] & -8); + buff[31] = (unsigned char)((buff[31] | 0x40) & ~0x80); + priv_exp.import_lsb(buff, 32); + PubKey = ellcurve::Ed25519().power_gen(priv_exp, true); // uniform + inited = PubKey.ok(); + if (!inited) { + clear(); + } + return inited; +} + +bool PrivateKey::compute_shared_secret(unsigned char secret[shared_secret_bytes], const PublicKey &Pub) { + if (!inited || !Pub.ok()) { + std::memset(secret, 0, shared_secret_bytes); + *(long *)secret = static_cast(td::Random::fast_uint64()); + return false; + } + // uniform power! + auto P = ellcurve::Curve25519().power_xz(Pub.get_point_xz(), priv_exp); + if (P.is_infty()) { + std::memset(secret, 0, shared_secret_bytes); + *(long *)secret = static_cast(td::Random::fast_uint64()); + return false; + } + P.export_point_u(secret); + return true; +} + +bool PrivateKey::compute_temp_shared_secret(unsigned char secret[shared_secret_bytes], + const unsigned char temp_pub_key[pubkey_bytes]) { + PublicKey tempPubkey(temp_pub_key); + if (!tempPubkey.ok()) { + return false; + } + return compute_shared_secret(secret, tempPubkey); +} + +bool PrivateKey::sign_message(unsigned char signature[sign_bytes], const unsigned char *message, std::size_t msg_size) { + if (!inited) { + std::memset(signature, 0, sign_bytes); + return false; + } + unsigned char r_bytes[64]; + digest::hash_two_str(r_bytes, priv_salt, 32, message, msg_size); + const arith::Bignum &L = ellcurve::Ed25519().get_ell(); + arith::Bignum eR; + eR.import_lsb(r_bytes, 64); + eR %= L; + std::memset(r_bytes, 0, sizeof(r_bytes)); + + // uniform power + auto pR = ellcurve::Ed25519().power_gen(eR, true); + + auto ok = pR.export_point(signature, true); + (void)ok; + assert(ok); + { + digest::SHA512 hasher(signature, 32); + hasher.feed(PubKey.get_pubkey_ptr(), 32); + hasher.feed(message, msg_size); + hasher.extract(r_bytes); + } + arith::Bignum S; + S.import_lsb(r_bytes, 64); + S %= L; + S *= priv_exp; + S += eR; + S %= L; + eR.clear(); + S.export_lsb(signature + 32, 32); + return true; +} + +// --------------------------------- +class TempKeyGenerator; + +unsigned char *TempKeyGenerator::get_temp_private_key(unsigned char *to, const unsigned char *message, std::size_t size, + const unsigned char *rand, + std::size_t rand_size) { // rand may be 0 + digest::SHA256 hasher(message, size); + hasher.feed(random_salt, salt_size); + if (rand && rand_size) { + hasher.feed(rand, rand_size); + } + if (!to) { + to = buffer; + } + hasher.extract(to); + //++ *((long *)random_salt); + return to; +} + +void TempKeyGenerator::create_temp_private_key(PrivateKey &pk, const unsigned char *message, std::size_t size, + const unsigned char *rand, std::size_t rand_size) { + pk.import_private_key(get_temp_private_key(buffer, message, size, rand, rand_size)); + std::memset(buffer, 0, privkey_bytes); +} + +bool TempKeyGenerator::create_temp_shared_secret(unsigned char temp_pub_key[pubkey_bytes], + unsigned char shared_secret[shared_secret_bytes], + const PublicKey &recipientPubKey, const unsigned char *message, + std::size_t size, const unsigned char *rand, std::size_t rand_size) { + PrivateKey tmpPk; + create_temp_private_key(tmpPk, message, size, rand, rand_size); + return tmpPk.export_public_key(temp_pub_key) && tmpPk.compute_shared_secret(shared_secret, recipientPubKey); +} + +} // namespace Ed25519 +} // namespace crypto diff --git a/crypto/ellcurve/Ed25519.h b/crypto/ellcurve/Ed25519.h new file mode 100644 index 00000000..cb899cc1 --- /dev/null +++ b/crypto/ellcurve/Ed25519.h @@ -0,0 +1,188 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once +#include "ellcurve/Montgomery.h" +#include "ellcurve/TwEdwards.h" +#include "openssl/digest.h" +#include "openssl/rand.hpp" +#include +#include + +#include "td/utils/buffer.h" + +namespace crypto { +namespace Ed25519 { + +const int privkey_bytes = 32; +const int pubkey_bytes = 32; +const int sign_bytes = 64; +const int shared_secret_bytes = 32; + +class PublicKey { + enum { pk_empty, pk_xz, pk_init } inited; + unsigned char pubkey[pubkey_bytes]; + ellcurve::TwEdwardsCurve::SegrePoint PubKey; + ellcurve::MontgomeryCurve::PointXZ PubKey_xz; + + public: + PublicKey() : inited(pk_empty), PubKey(ellcurve::Fp25519()), PubKey_xz(ellcurve::Fp25519()) { + } + PublicKey(const unsigned char pub_key[pubkey_bytes]); + PublicKey(td::Slice pub_key) : PublicKey(pub_key.ubegin()) { + CHECK(pub_key.size() == pubkey_bytes); + } + PublicKey(const ellcurve::TwEdwardsCurve::SegrePoint &Pub_Key); + + bool import_public_key(const unsigned char pub_key[pubkey_bytes]); + bool import_public_key(td::Slice pub_key) { + CHECK(pub_key.size() == pubkey_bytes); + return import_public_key(pub_key.ubegin()); + } + bool import_public_key(const ellcurve::TwEdwardsCurve::SegrePoint &Pub_Key); + bool export_public_key(unsigned char pubkey_buffer[pubkey_bytes]) const; + bool export_public_key(td::MutableSlice pubk) const { + CHECK(pubk.size() == pubkey_bytes); + return export_public_key(pubk.ubegin()); + } + bool check_message_signature(const unsigned char signature[sign_bytes], const unsigned char *message, + std::size_t msg_size); + bool check_message_signature(td::Slice signature, td::Slice message) { + CHECK(signature.size() == sign_bytes); + return check_message_signature(signature.ubegin(), message.ubegin(), message.size()); + } + + void clear(); + bool ok() const { + return inited == pk_init; + } + + const unsigned char *get_pubkey_ptr() const { + return inited == pk_init ? pubkey : 0; + } + const ellcurve::TwEdwardsCurve::SegrePoint &get_point() const { + return PubKey; + } + const ellcurve::MontgomeryCurve::PointXZ &get_point_xz() const { + return PubKey_xz; + } +}; + +class PrivateKey { + public: + struct priv_key_no_copy {}; + PrivateKey() : inited(false) { + memset(privkey, 0, privkey_bytes); + } + PrivateKey(const unsigned char pk[privkey_bytes]) : inited(false) { + memset(privkey, 0, privkey_bytes); + import_private_key(pk); + } + PrivateKey(td::Slice pk) : inited(false) { + CHECK(pk.size() == privkey_bytes); + memset(privkey, 0, privkey_bytes); + import_private_key(pk.ubegin()); + } + ~PrivateKey() { + clear(); + } + bool random_private_key(bool strong = false); + bool import_private_key(const unsigned char pk[privkey_bytes]); + bool import_private_key(td::Slice pk) { + CHECK(pk.size() == privkey_bytes); + return import_private_key(pk.ubegin()); + } + bool export_private_key(unsigned char pk[privkey_bytes]) const; // careful! + bool export_private_key(td::MutableSlice pk) const { // careful! + return export_private_key(pk.ubegin()); + } + bool export_public_key(unsigned char pubk[pubkey_bytes]) const { + return PubKey.export_public_key(pubk); + } + bool export_public_key(td::MutableSlice pubk) const { + return PubKey.export_public_key(pubk); + } + void clear(); + bool ok() const { + return inited; + } + + // used for EdDSA (sign) + bool sign_message(unsigned char signature[sign_bytes], const unsigned char *message, std::size_t msg_size); + bool sign_message(td::MutableSlice signature, td::Slice message) { + CHECK(signature.size() == sign_bytes); + return sign_message(signature.ubegin(), message.ubegin(), message.size()); + } + // used for ECDH (encrypt / decrypt) + bool compute_shared_secret(unsigned char secret[shared_secret_bytes], const PublicKey &Pub); + bool compute_shared_secret(td::MutableSlice secret, const PublicKey &Pub) { + CHECK(secret.size() == shared_secret_bytes); + return compute_shared_secret(secret.ubegin(), Pub); + } + // used for EC asymmetric decryption + bool compute_temp_shared_secret(unsigned char secret[shared_secret_bytes], + const unsigned char temp_pub_key[pubkey_bytes]); + + const PublicKey &get_public_key() const { + return PubKey; + } + + private: + bool inited; + unsigned char privkey[privkey_bytes]; + unsigned char priv_salt[32]; + arith::Bignum priv_exp; + PublicKey PubKey; + + bool process_private_key(); + PrivateKey(const PrivateKey &) { + throw priv_key_no_copy(); + } + PrivateKey &operator=(const PrivateKey &) { + throw priv_key_no_copy(); + } +}; + +// use one TempKeyGenerator object a lot of times +class TempKeyGenerator { + enum { salt_size = 64 }; + unsigned char random_salt[salt_size]; + unsigned char buffer[privkey_bytes]; + + public: + TempKeyGenerator() { + prng::rand_gen().strong_rand_bytes(random_salt, salt_size); + } + ~TempKeyGenerator() { + memset(random_salt, 0, salt_size); + memset(buffer, 0, privkey_bytes); + } + + unsigned char *get_temp_private_key(unsigned char *to, const unsigned char *message, std::size_t size, + const unsigned char *rand = 0, std::size_t rand_size = 0); // rand may be 0 + void create_temp_private_key(PrivateKey &pk, const unsigned char *message, std::size_t size, + const unsigned char *rand = 0, std::size_t rand_size = 0); + + // sets temp_pub_key and shared_secret for one-time asymmetric encryption of message + bool create_temp_shared_secret(unsigned char temp_pub_key[pubkey_bytes], unsigned char secret[shared_secret_bytes], + const PublicKey &recipientPubKey, const unsigned char *message, std::size_t size, + const unsigned char *rand = 0, std::size_t rand_size = 0); +}; + +} // namespace Ed25519 +} // namespace crypto diff --git a/crypto/ellcurve/Fp25519.cpp b/crypto/ellcurve/Fp25519.cpp new file mode 100644 index 00000000..393da408 --- /dev/null +++ b/crypto/ellcurve/Fp25519.cpp @@ -0,0 +1,33 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "ellcurve/Fp25519.h" + +namespace ellcurve { +using namespace arith; + +const Bignum& P25519() { + static const Bignum P25519 = (Bignum(1) << 255) - 19; + return P25519; +} + +td::Ref Fp25519() { + static const td::Ref Fp25519(true, P25519()); + return Fp25519; +} +} // namespace ellcurve diff --git a/crypto/ellcurve/Fp25519.h b/crypto/ellcurve/Fp25519.h new file mode 100644 index 00000000..3f17a540 --- /dev/null +++ b/crypto/ellcurve/Fp25519.h @@ -0,0 +1,32 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once +#include "common/refcnt.hpp" +#include "openssl/residue.h" + +namespace ellcurve { +using namespace arith; + +// returns 2^255-19 +const Bignum& P25519(); + +// residue ring modulo P25519 +td::Ref Fp25519(); + +} // namespace ellcurve diff --git a/crypto/ellcurve/Montgomery.cpp b/crypto/ellcurve/Montgomery.cpp new file mode 100644 index 00000000..c523f654 --- /dev/null +++ b/crypto/ellcurve/Montgomery.cpp @@ -0,0 +1,138 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "ellcurve/Montgomery.h" + +#include +#include + +namespace ellcurve { +using namespace arith; + +class MontgomeryCurve; + +void MontgomeryCurve::init() { + assert(!((a_short + 2) & 3) && a_short >= 0); +} + +void MontgomeryCurve::set_order_cofactor(const Bignum& order, int cof) { + assert(order > 0); + assert(cof >= 0); + assert(cof == 0 || (order % cof) == 0); + Order = order; + cofactor = cofactor_short = cof; + if (cof > 0) { + L = order / cof; + assert(is_prime(L)); + } + assert(!power_gen_xz(1).is_infty()); + assert(power_gen_xz(Order).is_infty()); +} + +// computes u(P+Q)*u(P-Q) as X/Z +MontgomeryCurve::PointXZ MontgomeryCurve::add_xz(const MontgomeryCurve::PointXZ& P, + const MontgomeryCurve::PointXZ& Q) const { + Residue u = (P.X + P.Z) * (Q.X - Q.Z); + Residue v = (P.X - P.Z) * (Q.X + Q.Z); + return MontgomeryCurve::PointXZ(sqr(u + v), sqr(u - v)); +} + +// computes u(2P) as X/Z +MontgomeryCurve::PointXZ MontgomeryCurve::double_xz(const MontgomeryCurve::PointXZ& P) const { + Residue u = sqr(P.X + P.Z); + Residue v = sqr(P.X - P.Z); + Residue w = u - v; + return PointXZ(u * v, w * (v + Residue(a_short, ring) * w)); +} + +MontgomeryCurve::PointXZ MontgomeryCurve::power_gen_xz(const Bignum& n) const { + return power_xz(Gu, n); +} + +MontgomeryCurve::PointXZ MontgomeryCurve::power_xz(const Residue& u, const Bignum& n) const { + return power_xz(PointXZ(u), n); +} + +// computes u([n]P) in form X/Z +MontgomeryCurve::PointXZ MontgomeryCurve::power_xz(const PointXZ& A, const Bignum& n) const { + assert(n >= 0); + if (n == 0) { + return PointXZ(ring); + } + + int k = n.num_bits(); + PointXZ P(A); + PointXZ Q(double_xz(P)); + for (int i = k - 2; i >= 0; --i) { + PointXZ PQ(add_xz(P, Q)); + PQ.X *= A.Z; + PQ.Z *= A.X; + if (n[i]) { + P = PQ; + Q = double_xz(Q); + } else { + Q = PQ; + P = double_xz(P); + } + } + return P; +} + +bool MontgomeryCurve::PointXZ::export_point_y(unsigned char buffer[32]) const { + if ((X + Z).is_zero()) { + std::memset(buffer, 0xff, 32); + return false; + } else { + get_y().extract().export_lsb(buffer, 32); + return true; + } +} + +bool MontgomeryCurve::PointXZ::export_point_u(unsigned char buffer[32]) const { + if (Z.is_zero()) { + std::memset(buffer, 0xff, 32); + return false; + } else { + get_u().extract().export_lsb(buffer, 32); + return true; + } +} + +MontgomeryCurve::PointXZ MontgomeryCurve::import_point_u(const unsigned char point[32]) const { + Bignum u; + u.import_lsb(point, 32); + u[255] = 0; + return PointXZ(Residue(u, ring)); +} + +MontgomeryCurve::PointXZ MontgomeryCurve::import_point_y(const unsigned char point[32]) const { + Bignum y; + y.import_lsb(point, 32); + y[255] = 0; + return PointXZ(Residue(y, ring), true); +} + +const MontgomeryCurve& Curve25519() { + static const MontgomeryCurve Curve25519 = [] { + MontgomeryCurve res(486662, 9, Fp25519()); + res.set_order_cofactor(hex_string{"80000000000000000000000000000000a6f7cef517bce6b2c09318d2e7ae9f68"}, 8); + return res; + }(); + return Curve25519; +} +} // namespace ellcurve diff --git a/crypto/ellcurve/Montgomery.h b/crypto/ellcurve/Montgomery.h new file mode 100644 index 00000000..d40e8f8d --- /dev/null +++ b/crypto/ellcurve/Montgomery.h @@ -0,0 +1,123 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once +#include +#include + +#include "openssl/bignum.h" +#include "openssl/residue.h" +#include "ellcurve/Fp25519.h" + +namespace ellcurve { +using namespace arith; + +class MontgomeryCurve { + td::Ref ring; + int A_short; // v^2 = u^2 + Au + 1 + int Gu_short; // u(G) + int a_short; // (A+2)/4 + Residue A_; + Residue Gu; + Bignum P_; + Bignum L; + Bignum Order; + Bignum cofactor; + int cofactor_short; + + void init(); + + public: + MontgomeryCurve(int _A, int _Gu, td::Ref _R) + : ring(_R) + , A_short(_A) + , Gu_short(_Gu) + , a_short((_A + 2) / 4) + , A_(_A, _R) + , Gu(_Gu, _R) + , P_(_R->get_modulus()) + , cofactor_short(0) { + init(); + } + + const Residue& get_gen_u() const { + return Gu; + } + const Bignum& get_ell() const { + return L; + } + const Bignum& get_order() const { + return Order; + } + td::Ref get_base_ring() const { + return ring; + } + const Bignum& get_p() const { + return P_; + } + + void set_order_cofactor(const Bignum& order, int cof); + + struct PointXZ { + Residue X, Z; + PointXZ(Residue x, Residue z) : X(x), Z(z) { + x.same_ring(z); + } + PointXZ(td::Ref r) : X(r->one()), Z(r->zero()) { + } + explicit PointXZ(Residue u) : X(u), Z(u.ring_of().one()) { + } + explicit PointXZ(Residue y, bool) : X(y.ring_of().one() + y), Z(y.ring_of().one() - y) { + } + PointXZ(const PointXZ& P) : X(P.X), Z(P.Z) { + } + PointXZ& operator=(const PointXZ& P) { + X = P.X; + Z = P.Z; + return *this; + } + Residue get_u() const { + return X * inverse(Z); + } + Residue get_v(bool sign_v = false) const; + bool is_infty() const { + return Z.is_zero(); + } + Residue get_y() const { + return (X - Z) * inverse(X + Z); + } + bool export_point_y(unsigned char buffer[32]) const; + bool export_point_u(unsigned char buffer[32]) const; + void zeroize() { + X = Z = Z.ring_of().zero(); + } + }; + + PointXZ power_gen_xz(const Bignum& n) const; + PointXZ power_xz(const Residue& u, const Bignum& n) const; + PointXZ power_xz(const PointXZ& P, const Bignum& n) const; + PointXZ add_xz(const PointXZ& P, const PointXZ& Q) const; + PointXZ double_xz(const PointXZ& P) const; + + PointXZ import_point_u(const unsigned char point[32]) const; + PointXZ import_point_y(const unsigned char point[32]) const; +}; + +const MontgomeryCurve& Curve25519(); + +} // namespace ellcurve diff --git a/crypto/ellcurve/TwEdwards.cpp b/crypto/ellcurve/TwEdwards.cpp new file mode 100644 index 00000000..6bcb42c2 --- /dev/null +++ b/crypto/ellcurve/TwEdwards.cpp @@ -0,0 +1,255 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "ellcurve/TwEdwards.h" +#include +#include + +namespace ellcurve { +using namespace arith; + +class TwEdwardsCurve; + +TwEdwardsCurve::TwEdwardsCurve(const Residue& _D, const Residue& _Gy, td::Ref _R) + : ring(_R) + , D(_D) + , D2(_D + _D) + , Gy(_Gy) + , P_(_R->get_modulus()) + , cofactor_short(0) + , G(_R) + , O(_R) + , table_lines(0) + , table() { + init(); +} + +TwEdwardsCurve::~TwEdwardsCurve() { +} + +void TwEdwardsCurve::init() { + assert(D != ring->zero() && D != ring->convert(-1)); + O.X = O.Z = ring->one(); + G = SegrePoint(*this, Gy, 0); + assert(!G.XY.is_zero()); +} + +void TwEdwardsCurve::set_order_cofactor(const Bignum& order, int cof) { + assert(order > 0); + assert(cof >= 0); + assert(cof == 0 || (order % cof) == 0); + Order = order; + cofactor = cofactor_short = cof; + if (cof > 0) { + L = order / cof; + assert(is_prime(L)); + assert(!power_gen(1).is_zero()); + assert(power_gen(L).is_zero()); + } +} + +TwEdwardsCurve::SegrePoint::SegrePoint(const TwEdwardsCurve& E, const Residue& y, bool x_sign) + : XY(y), X(E.get_base_ring()), Y(y), Z(E.get_base_ring()->one()) { + Residue x(y.ring_ref()); + if (E.recover_x(x, y, x_sign)) { + XY *= x; + X = x; + } else { + XY = Y = Z = E.get_base_ring()->zero(); + } +} + +bool TwEdwardsCurve::recover_x(Residue& x, const Residue& y, bool x_sign) const { + // recovers x from equation -x^2+y^2 = 1+d*x^2*y^2 + Residue z = inverse(ring->one() + D * sqr(y)); + if (z.is_zero()) { + return false; + } + z *= sqr(y) - ring->one(); + Residue t = sqrt(z); + if (sqr(t) == z) { + x = (t.extract().odd() == x_sign) ? t : -t; + //std::cout << "x=" << x << ", y=" << y << std::endl; + return true; + } else { + return false; + } +} + +void TwEdwardsCurve::add_points(SegrePoint& Res, const SegrePoint& P, const SegrePoint& Q) const { + Residue a((P.X + P.Y) * (Q.X + Q.Y)); + Residue b((P.X - P.Y) * (Q.X - Q.Y)); + Residue c(P.Z * Q.Z * ring->convert(2)); + Residue d(P.XY * Q.XY * D2); + Residue x_num(a - b); // 2(x1y2+x2y1) + Residue y_num(a + b); // 2(x1x2+y1y2) + Residue x_den(c + d); // 2(1+dx1x2y1y2) + Residue y_den(c - d); // 2(1-dx1x2y1y2) + Res.X = x_num * y_den; // x = x_num/x_den, y = y_num/y_den + Res.Y = y_num * x_den; + Res.XY = x_num * y_num; + Res.Z = x_den * y_den; +} + +TwEdwardsCurve::SegrePoint TwEdwardsCurve::add_points(const SegrePoint& P, const SegrePoint& Q) const { + SegrePoint Res(ring); + add_points(Res, P, Q); + return Res; +} + +void TwEdwardsCurve::double_point(SegrePoint& Res, const SegrePoint& P) const { + add_points(Res, P, P); +} + +TwEdwardsCurve::SegrePoint TwEdwardsCurve::double_point(const SegrePoint& P) const { + SegrePoint Res(ring); + double_point(Res, P); + return Res; +} + +// computes u([n]P) in form (xy,x,y,1)*Z +TwEdwardsCurve::SegrePoint TwEdwardsCurve::power_point(const SegrePoint& A, const Bignum& n, bool uniform) const { + assert(n >= 0); + if (n == 0) { + return O; + } + + int k = n.num_bits(); + SegrePoint P(A); + + if (uniform) { + SegrePoint Q(double_point(A)); + + for (int i = k - 2; i >= 0; --i) { + if (n[i]) { + add_points(P, P, Q); + double_point(Q, Q); + } else { + // we do more operations than necessary for uniformicity + add_points(Q, P, Q); + double_point(P, P); + } + } + } else { + for (int i = k - 2; i >= 0; --i) { + double_point(P, P); + if (n[i]) { + add_points(P, P, A); // may optimize further if A.z = 1 + } + } + } + return P; +} + +int TwEdwardsCurve::build_table() { + if (table.size()) { + return -1; + } + table_lines = (P_.num_bits() >> 2) + 2; + table.reserve(table_lines * 15 + 1); + table.emplace_back(get_base_point()); + for (int i = 0; i < table_lines; i++) { + for (int j = 0; j < 15; j++) { + table.emplace_back(add_points(table[15 * i + j], table[15 * i])); + } + } + return 1; +} + +int get_nibble(const Bignum& n, int idx) { + return n[idx * 4 + 3] * 8 + n[idx * 4 + 2] * 4 + n[idx * 4 + 1] * 2 + n[idx * 4]; +} + +TwEdwardsCurve::SegrePoint TwEdwardsCurve::power_gen(const Bignum& n, bool uniform) const { + if (uniform || n.num_bits() > table_lines * 4) { + return power_point(G, n, uniform); + } else if (n.is_zero()) { + return O; + } else { + int k = (n.num_bits() + 3) >> 2; + assert(k > 0 && k <= table_lines); + int x = get_nibble(n, k - 1); + assert(x > 0 && x < 16); + SegrePoint P(table[15 * (k - 1) + x - 1]); + for (int i = k - 2; i >= 0; i--) { + x = get_nibble(n, i); + assert(x >= 0 && x < 16); + if (x > 0) { + add_points(P, P, table[15 * i + x - 1]); + } + } + return P; + } +} + +bool TwEdwardsCurve::SegrePoint::export_point(unsigned char buffer[32], bool need_x) const { + if (!is_normalized()) { + if (Z.is_zero()) { + std::memset(buffer, 0xff, 32); + return false; + } + Residue f(inverse(Z)); + Bignum y((Y * f).extract()); + assert(!y[255]); + if (need_x) { + y[255] = (X * f).extract().odd(); + } + y.export_lsb(buffer, 32); + } else { + Bignum y(Y.extract()); + assert(!y[255]); + if (need_x) { + y[255] = X.extract().odd(); + } + y.export_lsb(buffer, 32); + } + return true; +} + +bool TwEdwardsCurve::SegrePoint::export_point_u(unsigned char buffer[32]) const { + if (Z == Y) { + std::memset(buffer, 0xff, 32); + return false; + } + Residue f(inverse(Z - Y)); + ((Z + Y) * f).extract().export_lsb(buffer, 32); + assert(!(buffer[31] & 0x80)); + return true; +} + +TwEdwardsCurve::SegrePoint TwEdwardsCurve::import_point(const unsigned char point[32], bool& ok) const { + Bignum y; + y.import_lsb(point, 32); + bool x_sign = y[255]; + y[255] = 0; + Residue yr(y, ring); + Residue xr(ring); + ok = recover_x(xr, yr, x_sign); + return ok ? SegrePoint(xr, yr) : SegrePoint(ring); +} + +const TwEdwardsCurve& Ed25519() { + static const TwEdwardsCurve Ed25519 = [] { + TwEdwardsCurve res(Fp25519()->frac(-121665, 121666), Fp25519()->frac(4, 5), Fp25519()); + res.set_order_cofactor(hex_string{"80000000000000000000000000000000a6f7cef517bce6b2c09318d2e7ae9f68"}, 8); + res.build_table(); + return res; + }(); + return Ed25519; +} +} // namespace ellcurve diff --git a/crypto/ellcurve/TwEdwards.h b/crypto/ellcurve/TwEdwards.h new file mode 100644 index 00000000..b226c291 --- /dev/null +++ b/crypto/ellcurve/TwEdwards.h @@ -0,0 +1,145 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once +#include +#include "common/refcnt.hpp" +#include "openssl/residue.h" +#include "ellcurve/Fp25519.h" + +namespace ellcurve { +using namespace arith; + +class TwEdwardsCurve { + public: + struct SegrePoint { + Residue XY, X, Y, Z; // if x=X/Z and y=Y/T, stores (xy,x,y,1)*Z*T + SegrePoint(td::Ref R) : XY(R), X(R), Y(R), Z(R) { + } + SegrePoint(const Residue& x, const Residue& y) : XY(x * y), X(x), Y(y), Z(y.ring_of().one()) { + } + SegrePoint(const TwEdwardsCurve& E, const Residue& y, bool x_sign); + SegrePoint(const SegrePoint& P) : XY(P.XY), X(P.X), Y(P.Y), Z(P.Z) { + } + SegrePoint& operator=(const SegrePoint& P) { + XY = P.XY; + X = P.X; + Y = P.Y; + Z = P.Z; + return *this; + } + bool is_zero() const { + return X.is_zero() && (Y == Z); + } + bool is_valid() const { + return (XY * Z == X * Y) && !(XY.is_zero() && X.is_zero() && Y.is_zero() && Z.is_zero()); + } + bool is_finite() const { + return !Z.is_zero(); + } + bool is_normalized() const { + return Z == Z.ring_of().one(); + } + SegrePoint& normalize() { + auto f = inverse(Z); + XY *= f; + X *= f; + Y *= f; + Z = Z.ring_of().one(); + return *this; + } + SegrePoint& zeroize() { + XY = X = Y = Z = Z.ring_of().zero(); + return *this; + } + bool export_point(unsigned char buffer[32], bool need_x = true) const; + bool export_point_y(unsigned char buffer[32]) const { + return export_point(buffer, false); + } + bool export_point_u(unsigned char buffer[32]) const; + Residue get_y() const { + return Y * inverse(Z); + } + Residue get_x() const { + return X * inverse(Z); + } + Residue get_u() const { + return (Z + Y) * inverse(Z - Y); + } + void negate() { + XY.negate(); + X.negate(); + } + }; + + private: + td::Ref ring; + Residue D; + Residue D2; + Residue Gy; + Bignum P_; + Bignum L; + Bignum Order; + Bignum cofactor; + int cofactor_short; + SegrePoint G; + SegrePoint O; + int table_lines; + std::vector table; + + void init(); + + public: + TwEdwardsCurve(const Residue& _D, const Residue& _Gy, td::Ref _R); + ~TwEdwardsCurve(); + const Residue& get_gen_y() const { + return Gy; + } + const Bignum& get_ell() const { + return L; + } + const Bignum& get_order() const { + return Order; + } + td::Ref get_base_ring() const { + return ring; + } + const Bignum& get_p() const { + return P_; + } + const SegrePoint& get_base_point() const { + return G; + } + + void set_order_cofactor(const Bignum& order, int cof); + bool recover_x(Residue& x, const Residue& y, bool x_sign) const; + + void add_points(SegrePoint& R, const SegrePoint& P, const SegrePoint& Q) const; + SegrePoint add_points(const SegrePoint& P, const SegrePoint& Q) const; + void double_point(SegrePoint& R, const SegrePoint& P) const; + SegrePoint double_point(const SegrePoint& P) const; + SegrePoint power_point(const SegrePoint& A, const Bignum& n, bool uniform = false) const; + SegrePoint power_gen(const Bignum& n, bool uniform = false) const; + int build_table(); + + SegrePoint import_point(const unsigned char point[32], bool& ok) const; +}; + +std::ostream& operator<<(std::ostream& os, const TwEdwardsCurve::SegrePoint& P); +const TwEdwardsCurve& Ed25519(); +} // namespace ellcurve diff --git a/crypto/fift/Dictionary.cpp b/crypto/fift/Dictionary.cpp new file mode 100644 index 00000000..3298132a --- /dev/null +++ b/crypto/fift/Dictionary.cpp @@ -0,0 +1,200 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "Dictionary.h" + +namespace fift { + +// +// WordDef +// +void WordDef::run(IntCtx& ctx) const { + auto next = run_tail(ctx); + while (next.not_null()) { + next = next->run_tail(ctx); + } +} + +// +// StackWord +// +Ref StackWord::run_tail(IntCtx& ctx) const { + f(ctx.stack); + return {}; +} + +// +// CtxWord +// +Ref CtxWord::run_tail(IntCtx& ctx) const { + f(ctx); + return {}; +} + +// +// CtxTailWord +// +Ref CtxTailWord::run_tail(IntCtx& ctx) const { + return f(ctx); +} + +// +// WordList +// +WordList::WordList(std::vector>&& _list) : list(std::move(_list)) { +} + +WordList::WordList(const std::vector>& _list) : list(_list) { +} + +WordList& WordList::push_back(Ref word_def) { + list.push_back(std::move(word_def)); + return *this; +} + +WordList& WordList::push_back(WordDef& wd) { + list.emplace_back(&wd); + return *this; +} + +Ref WordList::run_tail(IntCtx& ctx) const { + if (list.empty()) { + return {}; + } + auto it = list.cbegin(), it2 = list.cend() - 1; + while (it < it2) { + (*it)->run(ctx); + ++it; + } + return *it; +} + +void WordList::close() { + list.shrink_to_fit(); +} + +WordList& WordList::append(const std::vector>& other) { + list.insert(list.end(), other.begin(), other.end()); + return *this; +} + +// +// WordRef +// + +WordRef::WordRef(Ref _def, bool _act) : def(std::move(_def)), active(_act) { +} + +WordRef::WordRef(StackWordFunc func) : def(Ref{true, std::move(func)}), active(false) { +} + +WordRef::WordRef(CtxWordFunc func, bool _act) : def(Ref{true, std::move(func)}), active(_act) { +} + +WordRef::WordRef(CtxTailWordFunc func, bool _act) : def(Ref{true, std::move(func)}), active(_act) { +} + +Ref WordRef::get_def() const & { + return def; +} + +Ref WordRef::get_def() && { + return std::move(def); +} + +void WordRef::operator()(IntCtx& ctx) const { + def->run(ctx); +} + +bool WordRef::is_active() const { + return active; +} + +// +// Dictionary +// +WordRef* Dictionary::lookup(td::Slice name) { + auto it = words_.find(name); + if (it == words_.end()) { + return nullptr; + } + return &it->second; +} + +void Dictionary::def_ctx_word(std::string name, CtxWordFunc func) { + def_word(std::move(name), std::move(func)); +} + +void Dictionary::def_active_word(std::string name, CtxWordFunc func) { + Ref wdef = Ref{true, std::move(func)}; + def_word(std::move(name), {std::move(wdef), true}); +} + +void Dictionary::def_stack_word(std::string name, StackWordFunc func) { + def_word(std::move(name), std::move(func)); +} + +void Dictionary::def_ctx_tail_word(std::string name, CtxTailWordFunc func) { + def_word(std::move(name), std::move(func)); +} + +void Dictionary::def_word(std::string name, WordRef word) { + auto res = words_.emplace(name, std::move(word)); + LOG_IF(FATAL, !res.second) << "Cannot redefine word: " << name; +} + +void Dictionary::undef_word(td::Slice name) { + auto it = words_.find(name); + if (it == words_.end()) { + return; + } + words_.erase(it); +} + +void interpret_nop(vm::Stack& stack) { +} + +Ref Dictionary::nop_word_def = Ref{true, interpret_nop}; + +// +// functions for wordef +// +Ref pop_exec_token(vm::Stack& stack) { + stack.check_underflow(1); + auto wd_ref = stack.pop().as_object(); + if (wd_ref.is_null()) { + throw IntError{"execution token expected"}; + } + return wd_ref; +} + +Ref pop_word_list(vm::Stack& stack) { + stack.check_underflow(1); + auto wl_ref = stack.pop().as_object(); + if (wl_ref.is_null()) { + throw IntError{"word list expected"}; + } + return wl_ref; +} + +void push_argcount(vm::Stack& stack, int args) { + stack.push_smallint(args); + stack.push({vm::from_object, Dictionary::nop_word_def}); +} + +} // namespace fift diff --git a/crypto/fift/Dictionary.h b/crypto/fift/Dictionary.h new file mode 100644 index 00000000..34cdb832 --- /dev/null +++ b/crypto/fift/Dictionary.h @@ -0,0 +1,182 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +#include +#include + +#include "IntCtx.h" + +namespace fift { +using td::Ref; +/* + * + * WORD CLASSES + * + */ + +typedef std::function StackWordFunc; +typedef std::function CtxWordFunc; + +class WordDef : public td::CntObject { + public: + WordDef() = default; + virtual ~WordDef() override = default; + virtual Ref run_tail(IntCtx& ctx) const = 0; + void run(IntCtx& ctx) const; + virtual bool is_list() const { + return false; + } + virtual long long list_size() const { + return -1; + } + virtual const std::vector>* get_list() const { + return nullptr; + } +}; + +class StackWord : public WordDef { + StackWordFunc f; + + public: + StackWord(StackWordFunc _f) : f(std::move(_f)) { + } + ~StackWord() override = default; + Ref run_tail(IntCtx& ctx) const override; +}; + +class CtxWord : public WordDef { + CtxWordFunc f; + + public: + CtxWord(CtxWordFunc _f) : f(std::move(_f)) { + } + ~CtxWord() override = default; + Ref run_tail(IntCtx& ctx) const override; +}; + +typedef std::function(IntCtx&)> CtxTailWordFunc; + +class CtxTailWord : public WordDef { + CtxTailWordFunc f; + + public: + CtxTailWord(CtxTailWordFunc _f) : f(std::move(_f)) { + } + ~CtxTailWord() override = default; + Ref run_tail(IntCtx& ctx) const override; +}; + +class WordList : public WordDef { + std::vector> list; + + public: + ~WordList() override = default; + WordList() = default; + WordList(std::vector>&& _list); + WordList(const std::vector>& _list); + WordList& push_back(Ref word_def); + WordList& push_back(WordDef& wd); + Ref run_tail(IntCtx& ctx) const override; + void close(); + bool is_list() const override { + return true; + } + long long list_size() const override { + return (long long)list.size(); + } + const std::vector>* get_list() const override { + return &list; + } + WordList& append(const std::vector>& other); + WordList* make_copy() const override { + return new WordList(list); + } +}; + +class WordRef { + Ref def; + bool active; + + public: + WordRef() = delete; + WordRef(const WordRef& ref) = default; + WordRef(WordRef&& ref) = default; + WordRef(Ref _def, bool _act = false); + WordRef(StackWordFunc func); + WordRef(CtxWordFunc func, bool _act = false); + WordRef(CtxTailWordFunc func, bool _act = false); + //WordRef(const std::vector>& word_list); + //WordRef(std::vector>&& word_list); + WordRef& operator=(const WordRef&) = default; + WordRef& operator=(WordRef&&) = default; + Ref get_def() const &; + Ref get_def() &&; + void operator()(IntCtx& ctx) const; + bool is_active() const; + ~WordRef() = default; +}; + +/* +WordRef::WordRef(const std::vector>& word_list) : def(Ref{true, word_list}) { +} + +WordRef::WordRef(std::vector>&& word_list) : def(Ref{true, std::move(word_list)}) { +} +*/ + +/* + * + * DICTIONARIES + * + */ + +class Dictionary { + public: + WordRef* lookup(td::Slice name); + void def_ctx_word(std::string name, CtxWordFunc func); + void def_ctx_tail_word(std::string name, CtxTailWordFunc func); + void def_active_word(std::string name, CtxWordFunc func); + void def_stack_word(std::string name, StackWordFunc func); + void def_word(std::string name, WordRef word); + void undef_word(td::Slice name); + + auto begin() const { + return words_.begin(); + } + auto end() const { + return words_.end(); + } + + static Ref nop_word_def; + + private: + std::map> words_; +}; + +/* + * + * AUX FUNCTIONS FOR WORD DEFS + * + */ + +Ref pop_exec_token(vm::Stack& stack); +Ref pop_word_list(vm::Stack& stack); +void push_argcount(vm::Stack& stack, int args); +} // namespace fift diff --git a/crypto/fift/Fift.cpp b/crypto/fift/Fift.cpp new file mode 100644 index 00000000..5a4439ef --- /dev/null +++ b/crypto/fift/Fift.cpp @@ -0,0 +1,76 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "Fift.h" + +#include "words.h" + +#include "td/utils/PathView.h" + +namespace fift { + +Fift::Fift(Config config) : config_(std::move(config)) { +} + +Fift::Config& Fift::config() { + return config_; +} + +td::Result Fift::interpret_file(std::string fname, std::string current_dir, bool is_interactive) { + auto r_file = config_.source_lookup.lookup_source(fname, current_dir); + if (r_file.is_error()) { + return td::Status::Error("cannot locate file `" + fname + "`"); + } + auto file = r_file.move_as_ok(); + IntCtx ctx; + std::stringstream ss(file.data); + ctx.input_stream = &ss; + ctx.filename = td::PathView(file.path).file_name().str(); + ctx.currentd_dir = td::PathView(file.path).parent_dir().str(); + ctx.include_depth = is_interactive ? 0 : 1; + return do_interpret(ctx); +} + +td::Result Fift::interpret_istream(std::istream& stream, std::string current_dir, bool is_interactive) { + IntCtx ctx; + ctx.input_stream = &stream; + ctx.filename = "stdin"; + ctx.currentd_dir = current_dir; + ctx.include_depth = is_interactive ? 0 : 1; + return do_interpret(ctx); +} + +td::Result Fift::do_interpret(IntCtx& ctx) { + ctx.ton_db = &config_.ton_db; + ctx.source_lookup = &config_.source_lookup; + ctx.dictionary = &config_.dictionary; + ctx.output_stream = config_.output_stream; + ctx.error_stream = config_.error_stream; + if (!ctx.output_stream) { + return td::Status::Error("Cannot run interpreter without output_stream"); + } + try { + return funny_interpret_loop(ctx); + } catch (fift::IntError ab) { + return td::Status::Error(ab.msg); + } catch (fift::Quit q) { + return q.res; + } + return 0; +} +} // namespace fift diff --git a/crypto/fift/Fift.h b/crypto/fift/Fift.h new file mode 100644 index 00000000..bd3c8023 --- /dev/null +++ b/crypto/fift/Fift.h @@ -0,0 +1,53 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +#include "SourceLookup.h" +#include "vm/db/TonDb.h" +#include "Dictionary.h" + +#include "td/utils/Status.h" + +namespace fift { +struct IntCtx; +int funny_interpret_loop(IntCtx& ctx); + +struct Fift { + public: + struct Config { + fift::SourceLookup source_lookup; + vm::TonDb ton_db; + fift::Dictionary dictionary; + std::ostream* output_stream{&std::cout}; + std::ostream* error_stream{&std::cerr}; + }; + // Fift must own ton_db and dictionary, no concurrent access is allowed + explicit Fift(Config config); + + td::Result interpret_file(std::string fname, std::string current_dir, bool interactive = false); + td::Result interpret_istream(std::istream& stream, std::string current_dir, bool interactive = true); + + Config& config(); + + private: + Config config_; + + td::Result do_interpret(IntCtx& ctx); +}; +} // namespace fift diff --git a/crypto/fift/IntCtx.cpp b/crypto/fift/IntCtx.cpp new file mode 100644 index 00000000..f2da3409 --- /dev/null +++ b/crypto/fift/IntCtx.cpp @@ -0,0 +1,192 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "IntCtx.h" + +namespace fift { + +td::StringBuilder& operator<<(td::StringBuilder& os, const IntCtx& ctx) { + if (ctx.include_depth) { + return os << ctx.filename << ":" << ctx.line_no << ": "; + } else { + return os; + } +} + +std::ostream& operator<<(std::ostream& os, const IntCtx& ctx) { + return os << (PSLICE() << ctx).c_str(); +} + +void CharClassifier::import_from_string(td::Slice str, int space_cls) { + set_char_class(' ', space_cls); + set_char_class('\t', space_cls); + int cls = 3; + for (char c : str) { + if (c == ' ') { + cls--; + } else { + set_char_class(c, cls); + } + } +} + +void CharClassifier::import_from_string(std::string str, int space_cls) { + import_from_string(td::Slice{str}, space_cls); +} + +void CharClassifier::import_from_string(const char* str, int space_cls) { + import_from_string(td::Slice{str}, space_cls); +} + +CharClassifier CharClassifier::from_string(td::Slice str, int space_cls) { + return CharClassifier{str, space_cls}; +} + +void CharClassifier::set_char_class(int c, int cl) { + c &= 0xff; + cl &= 3; + int offs = (c & 3) * 2; + int mask = (3 << offs); + cl <<= offs; + unsigned char* p = data_ + (c >> 2); + *p = static_cast((*p & ~mask) | cl); +} + +IntCtx::Savepoint::Savepoint(IntCtx& _ctx, std::string new_filename, std::string new_current_dir, + std::istream* new_input_stream) + : ctx(_ctx) + , old_line_no(_ctx.line_no) + , old_filename(_ctx.filename) + , old_current_dir(_ctx.currentd_dir) + , old_input_stream(_ctx.input_stream) + , old_curline(_ctx.str) + , old_curpos(_ctx.input_ptr - _ctx.str.c_str()) { + ctx.line_no = 0; + ctx.filename = new_filename; + ctx.currentd_dir = new_current_dir; + ctx.input_stream = new_input_stream; + ctx.str = ""; + ctx.input_ptr = 0; + ++(ctx.include_depth); +} + +IntCtx::Savepoint::~Savepoint() { + ctx.line_no = old_line_no; + ctx.filename = old_filename; + ctx.currentd_dir = old_current_dir; + ctx.input_stream = old_input_stream; + ctx.str = old_curline; + ctx.input_ptr = ctx.str.c_str() + old_curpos; + --(ctx.include_depth); +} + +bool IntCtx::load_next_line() { + if (!std::getline(*input_stream, str)) { + return false; + } + if (!str.empty() && str.back() == '\r') { + str.pop_back(); + } + set_input(str); + return true; +} + +bool IntCtx::is_sb() const { + return !eof() && line_no == 1 && *input_ptr == '#' && input_ptr[1] == '!'; +} + +td::Slice IntCtx::scan_word_to(char delim, bool err_endl) { + auto ptr = input_ptr; + while (*ptr && *ptr != delim) { + ptr++; + } + if (*ptr) { + std::swap(ptr, input_ptr); + return td::Slice{ptr, input_ptr++}; + } else if (err_endl && delim) { + throw IntError{std::string{"end delimiter `"} + delim + "` not found"}; + } else { + std::swap(ptr, input_ptr); + return td::Slice{ptr, input_ptr}; + } +} + +td::Slice IntCtx::scan_word() { + skipspc(true); + auto ptr = input_ptr; + while (*ptr && *ptr != ' ' && *ptr != '\t' && *ptr != '\r') { + ptr++; + } + auto ptr2 = ptr; + std::swap(ptr, input_ptr); + skipspc(); + return td::Slice{ptr, ptr2}; +} + +td::Slice IntCtx::scan_word_ext(const CharClassifier& classifier) { + skipspc(true); + auto ptr = input_ptr; + while (*ptr && *ptr != '\r' && *ptr != '\n') { + int c = classifier.classify(*ptr); + if ((c & 1) && ptr != input_ptr) { + break; + } + ptr++; + if (c & 2) { + break; + } + } + std::swap(ptr, input_ptr); + return td::Slice{ptr, input_ptr}; +} + +void IntCtx::skipspc(bool skip_eol) { + do { + while (*input_ptr == ' ' || *input_ptr == '\t' || *input_ptr == '\r') { + ++input_ptr; + } + if (!skip_eol || *input_ptr) { + break; + } + } while (load_next_line()); +} + +void check_compile(const IntCtx& ctx) { + if (ctx.state <= 0) { + throw IntError{"compilation mode only"}; + } +} + +void check_execute(const IntCtx& ctx) { + if (ctx.state != 0) { + throw IntError{"interpret mode only"}; + } +} + +void check_not_int_exec(const IntCtx& ctx) { + if (ctx.state < 0) { + throw IntError{"not allowed in internal interpret mode"}; + } +} + +void check_int_exec(const IntCtx& ctx) { + if (ctx.state >= 0) { + throw IntError{"internal interpret mode only"}; + } +} +} // namespace fift diff --git a/crypto/fift/IntCtx.h b/crypto/fift/IntCtx.h new file mode 100644 index 00000000..753d2cb1 --- /dev/null +++ b/crypto/fift/IntCtx.h @@ -0,0 +1,147 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +#include "crypto/vm/db/TonDb.h" // FIXME +#include "crypto/vm/stack.hpp" +#include "crypto/common/bitstring.h" + +#include +#include +#include +#include + +namespace fift { +class Dictionary; +class SourceLookup; + +struct IntError { + std::string msg; + IntError(std::string _msg) : msg(_msg) { + } +}; + +class CharClassifier { + unsigned char data_[64]; + + public: + CharClassifier() { + std::memset(data_, 0, sizeof(data_)); + } + CharClassifier(td::Slice str, int space_cls = 3) : CharClassifier() { + import_from_string(str, space_cls); + } + CharClassifier(std::string str, int space_cls = 3) : CharClassifier(td::Slice{str}, space_cls) { + } + CharClassifier(const char* str, int space_cls = 3) : CharClassifier(td::Slice{str}, space_cls) { + } + void import_from_string(td::Slice str, int space_cls = 3); + void import_from_string(std::string str, int space_cls = 3); + void import_from_string(const char* str, int space_cls = 3); + static CharClassifier from_string(td::Slice str, int space_cls = 3); + void set_char_class(int c, int cl); + int classify(int c) const { + c &= 0xff; + int offs = (c & 3) * 2; + return (data_[(unsigned)c >> 2] >> offs) & 3; + } +}; + +struct IntCtx { + vm::Stack stack; + int state{0}; + int include_depth{0}; + int line_no{0}; + std::string filename; + std::string currentd_dir; + std::istream* input_stream{nullptr}; + std::ostream* output_stream{nullptr}; + std::ostream* error_stream{nullptr}; + + vm::TonDb* ton_db{nullptr}; + Dictionary* dictionary{nullptr}; + SourceLookup* source_lookup{nullptr}; + + private: + std::string str; + const char* input_ptr; + + public: + IntCtx() = default; + + operator vm::Stack&() { + return stack; + } + + td::Slice scan_word_to(char delim, bool err_endl = true); + td::Slice scan_word(); + td::Slice scan_word_ext(const CharClassifier& classifier); + void skipspc(bool skip_eol = false); + + bool eof() const { + return !*input_stream; + } + + bool not_eof() const { + return !eof(); + } + + void set_input(std::string input_str) { + str = input_str; + input_ptr = str.c_str(); + ++line_no; + } + void set_input(const char* ptr) { + input_ptr = ptr; + } + const char* get_input() const { + return input_ptr; + } + + bool load_next_line(); + + bool is_sb() const; + + void clear() { + state = 0; + stack.clear(); + } + class Savepoint { + IntCtx& ctx; + int old_line_no; + std::string old_filename; + std::string old_current_dir; + std::istream* old_input_stream; + std::string old_curline; + std::ptrdiff_t old_curpos; + + public: + Savepoint(IntCtx& _ctx, std::string new_filename, std::string new_current_dir, std::istream* new_input_stream); + ~Savepoint(); + }; +}; + +void check_compile(const IntCtx& ctx); +void check_execute(const IntCtx& ctx); +void check_not_int_exec(const IntCtx& ctx); +void check_int_exec(const IntCtx& ctx); + +td::StringBuilder& operator<<(td::StringBuilder& os, const IntCtx& ctx); +std::ostream& operator<<(std::ostream& os, const IntCtx& ctx); +} // namespace fift diff --git a/crypto/fift/SourceLookup.cpp b/crypto/fift/SourceLookup.cpp new file mode 100644 index 00000000..50cd8c31 --- /dev/null +++ b/crypto/fift/SourceLookup.cpp @@ -0,0 +1,89 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "SourceLookup.h" + +#include "td/utils/PathView.h" +#include "td/utils/PathView.h" +#include "td/utils/port/path.h" +#include "td/utils/filesystem.h" + +#include + +namespace fift { +td::Result OsFileLoader::read_file(td::CSlice filename) { + File res; + TRY_RESULT(data, td::read_file_str(filename)); + res.data = std::move(data); + TRY_RESULT(path, td::realpath(filename)); + res.path = std::move(path); + return std::move(res); +} + +td::Status OsFileLoader::write_file(td::CSlice filename, td::Slice data) { + return td::write_file(filename, data); +} + +td::Result OsFileLoader::read_file_part(td::CSlice filename, td::int64 size, td::int64 offset) { + File res; + TRY_RESULT(data, td::read_file_str(filename, size, offset)); + res.data = std::move(data); + TRY_RESULT(path, td::realpath(filename)); + res.path = std::move(path); + return std::move(res); +} + +bool OsFileLoader::is_file_exists(td::CSlice filename) { + return td::stat(filename).is_ok(); +} + +void SourceLookup::add_include_path(td::string path) { + if (path.empty()) { + return; + } + if (!td::PathView(path).is_dir()) { + path += TD_DIR_SLASH; + } + + source_include_path_.push_back(std::move(path)); +} + +td::Result SourceLookup::lookup_source(std::string filename, std::string current_dir) { + CHECK(file_loader_); + if (!current_dir.empty() && !td::PathView(current_dir).is_dir()) { + current_dir += TD_DIR_SLASH; + } + if (td::PathView(filename).is_absolute()) { + return read_file(filename); + } + if (!current_dir.empty()) { + auto res = read_file(current_dir + filename); + if (res.is_ok()) { + return res; + } + } + for (auto& dir : source_include_path_) { + auto res = read_file(dir + filename); + if (res.is_ok()) { + return res; + } + } + + return td::Status::Error(PSLICE() << "failed to lookup file: " << filename); +} +} // namespace fift diff --git a/crypto/fift/SourceLookup.h b/crypto/fift/SourceLookup.h new file mode 100644 index 00000000..73d2cd91 --- /dev/null +++ b/crypto/fift/SourceLookup.h @@ -0,0 +1,71 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once +#include + +#include "td/utils/Status.h" + +namespace fift { +class FileLoader { + public: + virtual ~FileLoader() = default; + struct File { + std::string data; + std::string path; + }; + virtual td::Result read_file(td::CSlice filename) = 0; + virtual td::Status write_file(td::CSlice filename, td::Slice data) = 0; + virtual td::Result read_file_part(td::CSlice filename, td::int64 size, td::int64 offset) = 0; + virtual bool is_file_exists(td::CSlice filename) = 0; +}; + +class OsFileLoader : public FileLoader { + public: + td::Result read_file(td::CSlice filename) override; + td::Status write_file(td::CSlice filename, td::Slice data) override; + td::Result read_file_part(td::CSlice filename, td::int64 size, td::int64 offset) override; + bool is_file_exists(td::CSlice filename) override; +}; + +class SourceLookup { + public: + SourceLookup() = default; + explicit SourceLookup(std::unique_ptr file_loader) : file_loader_(std::move(file_loader)) { + } + void add_include_path(td::string path); + td::Result lookup_source(std::string filename, std::string current_dir); + + td::Result read_file(td::CSlice path) { + return file_loader_->read_file(path); + } + td::Status write_file(td::CSlice path, td::Slice data) { + return file_loader_->write_file(path, data); + } + td::Result read_file_part(td::CSlice filename, td::int64 size, td::int64 offset) { + return file_loader_->read_file_part(filename, size, offset); + } + bool is_file_exists(td::CSlice filename) { + return file_loader_->is_file_exists(filename); + } + + protected: + std::unique_ptr file_loader_; + std::vector source_include_path_; +}; +} // namespace fift diff --git a/crypto/fift/fift-main.cpp b/crypto/fift/fift-main.cpp new file mode 100644 index 00000000..e040b6e8 --- /dev/null +++ b/crypto/fift/fift-main.cpp @@ -0,0 +1,221 @@ +/* + This file is part of TON Blockchain source code. + + TON Blockchain is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + TON Blockchain is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with TON Blockchain. If not, see . + + In addition, as a special exception, the copyright holders give permission + to link the code of portions of this program with the OpenSSL library. + You must obey the GNU General Public License in all respects for all + of the code used other than OpenSSL. If you modify file(s) with this + exception, you may extend this exception to your version of the file(s), + but you are not obligated to do so. If you do not wish to do so, delete this + exception statement from your version. If you delete this exception statement + from all source files in the program, then also delete it here. + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "vm/stack.hpp" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "Fift.h" +#include "Dictionary.h" +#include "SourceLookup.h" +#include "words.h" + +#include "vm/db/TonDb.h" + +#include "td/utils/logging.h" +#include "td/utils/misc.h" +#include "td/utils/Parser.h" +#include "td/utils/port/path.h" + +void usage(const char* progname) { + std::cerr << "A simple Fift interpreter. Type `bye` to quit, or `words` to get a list of all commands\n"; + std::cerr + << "usage: " << progname + << " [-i] [-n] [-I ] {-L } ...\n"; + std::cerr << "\t-n\tDo not preload standard preamble file `Fift.fif`\n" + "\t-i\tForce interactive mode even if explicit source file names are indicated\n" + "\t-I\tSets colon-separated library source include path. If not indicated, " + "$FIFTPATH is used instead.\n" + "\t-L\tPre-loads a library source file\n" + "\t-d\tUse a ton database\n" + "\t-s\tScript mode: use first argument as a fift source file and import remaining arguments as $n)\n" + "\t-v\tSet verbosity level\n"; + std::exit(2); +} + +void parse_include_path_set(std::string include_path_set, std::vector& res) { + td::Parser parser(include_path_set); + while (!parser.empty()) { + auto path = parser.read_till_nofail(':'); + if (!path.empty()) { + res.push_back(path.str()); + } + parser.skip_nofail(':'); + } +} + +int main(int argc, char* const argv[]) { + bool interactive = false; + bool fift_preload = true, no_env = false; + bool script_mode = false; + std::vector library_source_files, source_list; + std::vector source_include_path; + std::string ton_db_path; + + fift::Fift::Config config; + + int i; + int new_verbosity_level = VERBOSITY_NAME(INFO); + while (!script_mode && (i = getopt(argc, argv, "hinI:L:d:sv:")) != -1) { + switch (i) { + case 'i': + interactive = true; + break; + case 'n': + fift_preload = false; + break; + case 'I': + parse_include_path_set(optarg, source_include_path); + no_env = true; + break; + case 'L': + library_source_files.emplace_back(optarg); + break; + case 'd': + ton_db_path = optarg; + break; + case 's': + script_mode = true; + break; + case 'v': + new_verbosity_level = VERBOSITY_NAME(FATAL) + td::to_integer(td::Slice(optarg)); + break; + case 'h': + default: + usage(argv[0]); + } + } + SET_VERBOSITY_LEVEL(new_verbosity_level); + + while (optind < argc) { + source_list.emplace_back(argv[optind++]); + if (script_mode) { + break; + } + } + + if (!no_env) { + const char* path = std::getenv("FIFTPATH"); + if (path) { + parse_include_path_set(path ? path : "/usr/lib/fift", source_include_path); + } + } + std::string current_dir; + auto r_current_dir = td::realpath("."); + if (r_current_dir.is_ok()) { + current_dir = r_current_dir.move_as_ok(); + source_include_path.push_back(current_dir); + } + config.source_lookup = fift::SourceLookup(std::make_unique()); + for (auto& path : source_include_path) { + config.source_lookup.add_include_path(path); + } + + if (!ton_db_path.empty()) { + auto r_ton_db = vm::TonDbImpl::open(ton_db_path); + if (r_ton_db.is_error()) { + LOG(ERROR) << "Error opening ton database: " << r_ton_db.error().to_string(); + std::exit(2); + } + config.ton_db = r_ton_db.move_as_ok(); + // FIXME //std::atexit([&] { config.ton_db.reset(); }); + } + + fift::init_words_common(config.dictionary); + fift::init_words_vm(config.dictionary); + fift::init_words_ton(config.dictionary); + + if (script_mode) { + fift::import_cmdline_args(config.dictionary, source_list.empty() ? "" : source_list[0], argc - optind, + argv + optind); + } + + fift::Fift fift(std::move(config)); + + if (fift_preload) { + auto status = fift.interpret_file("Fift.fif", ""); + if (status.is_error()) { + LOG(ERROR) << "Error interpreting standard preamble file `Fift.fif`: " << status.error().message() + << "\nCheck that correct include path is set by -I or by FIFTPATH environment variable, or disable " + "standard preamble by -n.\n"; + std::exit(2); + } + } + + for (auto source : library_source_files) { + auto status = fift.interpret_file(source, ""); + if (status.is_error()) { + LOG(ERROR) << "Error interpreting preloaded file `" << source << "`: " << status.error().message(); + std::exit(2); + } + } + + if (source_list.empty()) { + interactive = true; + } + for (const auto& source : source_list) { + bool is_stdin = (source.empty() || source == "-"); + auto status = + !is_stdin ? fift.interpret_file(source, current_dir) : fift.interpret_istream(std::cin, current_dir, false); + if (status.is_error()) { + if (!is_stdin) { + LOG(ERROR) << "Error interpreting file `" << source << "`: " << status.error().message(); + } else { + LOG(ERROR) << "Error interpreting stdin: " << status.error().message(); + } + std::exit(2); + } + auto res = status.move_as_ok(); + if (res) { + std::exit(~res); + } + } + if (interactive) { + auto status = fift.interpret_istream(std::cin, current_dir); + if (status.is_error()) { + LOG(ERROR) << status.error().message(); + std::exit(2); + } else { + int res = status.move_as_ok(); + if (res) { + std::exit(~res); + } + } + } +} diff --git a/crypto/fift/lib/Asm.fif b/crypto/fift/lib/Asm.fif new file mode 100644 index 00000000..69adeeea --- /dev/null +++ b/crypto/fift/lib/Asm.fif @@ -0,0 +1,1101 @@ +library TVM_Asm +// simple TVM Assembler +variable @atend +'nop @atend ! +{ `normal eq? not abort"must be terminated by }>" } : @normal? +{ @atend @ 1 { @atend ! @normal? } does @atend ! } : @pushatend +{ @pushatend +{ }> b> } : }>c +{ }>c s +{ @atend @ 2 { @atend ! rot b> ref, swap @endblk } does @atend ! = -rot <= and } : 2x<= +{ 2 pick brembitrefs 1- 2x<= } : @havebitrefs +{ @havebits not ' @| if } : @ensurebits +{ @havebitrefs not ' @| if } : @ensurebitrefs +{ rot over @ensurebits -rot u, } : @simpleuop +{ tuck sbitrefs @ensurebitrefs swap s, } : @addop +{ tuck bbitrefs @ensurebitrefs swap b+ } : @addopb +' @addopb : @inline +{ 1 ' @addop does create } : @Defop +{ 1 { } : si() +// x mi ma -- ? +{ rot tuck >= -rot <= and } : @range +{ rot tuck < -rot > or } : @-range +{ @-range abort"Out of range" } : @rangechk +{ dup 0 < over 255 > or abort"Invalid stack register number" si() } : s() +{ si() constant } : @Sreg +-2 @Sreg s(-2) +-1 @Sreg s(-1) +0 @Sreg s0 +1 @Sreg s1 +2 @Sreg s2 +3 @Sreg s3 +4 @Sreg s4 +5 @Sreg s5 +6 @Sreg s6 +7 @Sreg s7 +8 @Sreg s8 +9 @Sreg s9 +10 @Sreg s10 +11 @Sreg s11 +12 @Sreg s12 +13 @Sreg s13 +14 @Sreg s14 +15 @Sreg s15 +{ dup 0 < over 7 > or abort"Invalid control register number" } : c() +{ c() constant } : @Creg +0 @Creg c0 +1 @Creg c1 +2 @Creg c2 +3 @Creg c3 +4 @Creg c4 +5 @Creg c5 +7 @Creg c7 +{ abort"not a stack register" 12 i@+ s> } : @bigsridx +{ @bigsridx dup 16 >= over 0< or abort"stack register s0..s15 expected" } : @sridx +{ rot @bigsridx tuck < -rot tuck > rot or abort"stack register out of range" } : @sridxrange +{ swap @bigsridx + dup 16 >= over 0< or abort"stack register out of range" } : @sridx+ +{ swap 0xcc <> over 7 > or over 6 = or abort"not a control register c0..c5 or c7" } : @cridx +{ = + { tuck 16 >= + { = and + { 15 and abort"integer too large" 8 + 2dup fits } until + > 2- 5 u, -rot i, + } cond + } cond + } cond + @addopb } dup : PUSHINT : INT +{ = or abort"invalid slice padding" + swap 1 1 u, 0 rot u, } : @scomplete +{ tuck sbitrefs swap 17 + swap @havebitrefs not + { PUSHREFSLICE } + { over sbitrefs 2dup 123 0 2x<= + { drop tuck 4 + 3 >> swap x{8B} s, over 4 u, 3 roll s, + -rot 3 << 4 + swap - @scomplete } + { 2dup 1 >= swap 248 <= and + { rot x{8C} s, swap 1- 2 u, over 7 + 3 >> tuck 5 u, 3 roll s, + -rot 3 << 1 + swap - @scomplete } + { rot x{8D} s, swap 3 u, over 2 + 3 >> tuck 7 u, 3 roll s, + -rot 3 << 6 + swap - @scomplete + } cond + } cond + } cond +} dup : PUSHSLICE : SLICE +{ tuck bbitrefs swap 16 + dup 7 and 3 -roll swap @havebitrefs + not rot or + { swap b> PUSHREFCONT } + { over bbitrefs 2dup 120 0 2x<= + { drop swap x{9} s, swap 3 >> 4 u, swap b+ } + { rot x{8F_} s, swap 2 u, swap 3 >> 7 u, swap b+ } cond + } cond +} dup : PUSHCONT : CONT +{ }> PUSHCONT } : }>CONT +{ { @normal? PUSHCONT } @doafter<{ } : CONT:<{ + +// arithmetic operations +{ 2 { rot dup 8 fits + { nip > tuck 3 u, 3 roll s, + -rot 3 << 2 + swap - @scomplete } + { 2drop swap PUSHSLICE STSLICER } cond + } cond +} : STSLICECONST +x{CF81} @Defop STZERO +x{CF83} @Defop STONE +// cell deserialization (CellSlice primitives) +x{D0} @Defop CTOS +x{D1} @Defop ENDS +x{D2} @Defop(8u+1) LDI +x{D3} @Defop(8u+1) LDU +x{D4} @Defop LDREF +x{D5} @Defop LDREFRTOS +x{D6} @Defop(8u+1) LDSLICE +x{D700} @Defop LDIX +x{D701} @Defop LDUX +x{D702} @Defop PLDIX +x{D703} @Defop PLDUX +x{D704} @Defop LDIXQ +x{D705} @Defop LDUXQ +x{D706} @Defop PLDIXQ +x{D707} @Defop PLDUXQ +x{D708} @Defop(8u+1) LDI_l +x{D709} @Defop(8u+1) LDU_l +x{D70A} @Defop(8u+1) PLDI +x{D70B} @Defop(8u+1) PLDU +x{D70C} @Defop(8u+1) LDIQ +x{D70D} @Defop(8u+1) LDUQ +x{D70E} @Defop(8u+1) PLDIQ +x{D70F} @Defop(8u+1) PLDUQ +{ dup 31 and abort"argument must be a multiple of 32" 5 >> 1- + > swap x{D72A_} s, over 7 u, 3 roll s, + -rot 3 << 3 + swap - @scomplete } : SDBEGINS:imm +{ tuck sbitrefs abort"no references allowed in slice" dup 26 <= + { drop > swap x{D72E_} s, over 7 u, 3 roll s, + -rot 3 << 3 + swap - @scomplete } : SDBEGINSQ:imm +{ tuck sbitrefs abort"no references allowed in slice" dup 26 <= + { drop PUSHCONT IF } : }>IF +x{DF} @Defop IFNOT +{ }> PUSHCONT IFNOT } : }>IFNOT +' IFNOTRET : IF: +' IFRET : IFNOT: +x{E0} @Defop IFJMP +{ }> PUSHCONT IFJMP } : }>IFJMP +{ { @normal? PUSHCONT IFJMP } @doafter<{ } : IFJMP:<{ +x{E1} @Defop IFNOTJMP +{ }> PUSHCONT IFNOTJMP } : }>IFNOTJMP +{ { @normal? PUSHCONT IFNOTJMP } @doafter<{ } : IFNOTJMP:<{ +x{E2} @Defop IFELSE +{ `else @endblk } : }>ELSE<{ +{ `else: @endblk } : }>ELSE: +{ PUSHCONT { @normal? PUSHCONT IFELSE } @doafter<{ } : @doifelse +{ 1 { swap @normal? -rot PUSHCONT swap PUSHCONT IFELSE } does @doafter<{ } : @doifnotelse +{ + { dup `else eq? + { drop @doifelse } + { dup `else: eq? + { drop PUSHCONT IFJMP } + { @normal? PUSHCONT IF + } cond + } cond + } @doafter<{ +} : IF:<{ +{ + { dup `else eq? + { drop @doifnotelse } + { dup `else: eq? + { drop PUSHCONT IFNOTJMP } + { @normal? PUSHCONT IFNOT + } cond + } cond + } @doafter<{ +} : IFNOT:<{ +x{E300} @Defop(ref) IFREF +x{E301} @Defop(ref) IFNOTREF +x{E302} @Defop(ref) IFJMPREF +x{E303} @Defop(ref) IFNOTJMPREF +x{E304} @Defop CONDSEL +x{E305} @Defop CONDSELCHK +x{E308} @Defop IFRETALT +x{E309} @Defop IFNOTRETALT +{ PUSHCONT REPEAT } : }>REPEAT +{ { @normal? PUSHCONT REPEAT } @doafter<{ } : REPEAT:<{ +x{E5} dup @Defop REPEATEND @Defop REPEAT: +x{E6} @Defop UNTIL +{ }> PUSHCONT UNTIL } : }>UNTIL +{ { @normal? PUSHCONT UNTIL } @doafter<{ } : UNTIL:<{ +x{E7} dup @Defop UNTILEND @Defop UNTIL: +x{E8} @Defop WHILE +x{E9} @Defop WHILEEND +{ `do @endblk } : }>DO<{ +{ `do: @endblk } : }>DO: +{ PUSHCONT { @normal? PUSHCONT WHILE } @doafter<{ } : @dowhile +{ + { dup `do eq? + { drop @dowhile } + { `do: eq? not abort"`}>DO<{` expected" PUSHCONT WHILEEND + } cond + } @doafter<{ +} : WHILE:<{ +x{EA} @Defop AGAIN +{ }> PUSHCONT AGAIN } : }>AGAIN +{ { @normal? PUSHCONT AGAIN } @doafter<{ } : AGAIN:<{ +x{EB} dup @Defop AGAINEND @Defop AGAIN: +// +// continuation stack manipulation and continuation creation +// +{ PUSHCONT ATEXIT } : }>ATEXIT +{ { @normal? PUSHCONT ATEXIT } @doafter<{ } : ATEXIT:<{ +x{EDF4} @Defop ATEXITALT +{ }> PUSHCONT ATEXITALT } : }>ATEXITALT +{ { @normal? PUSHCONT ATEXITALT } @doafter<{ } : ATEXITALT:<{ +x{EDF5} @Defop SETEXITALT +{ }> PUSHCONT SETEXITALT } : }>SETEXITALT +{ { @normal? PUSHCONT SETEXITALT } @doafter<{ } : SETEXITALT:<{ +x{EDF6} @Defop THENRET +x{EDF7} @Defop THENRETALT +x{EDF8} @Defop INVERT +x{EDF9} @Defop BOOLEVAL +// x{EE} is BLESSARGS +// +// dictionary subroutine call/jump primitives +{ c3 PUSH EXECUTE } : CALLVAR +{ c3 PUSH JMPX } : JMPVAR +{ c3 PUSH } : PREPAREVAR +{ dup 14 ufits { + dup 8 ufits { + CATCH<{ +{ PUSHCONT { @normal? PUSHCONT TRY } @doafter<{ } : @trycatch +{ + { `catch eq? not abort"`}>CATCH<{` expected" @trycatch + } @doafter<{ +} : TRY:<{ +// +// dictionary manipulation +' NULL : NEWDICT +' ISNULL : DICTEMPTY +' STSLICE : STDICTS +x{F400} dup @Defop STDICT @Defop STOPTREF +x{F401} dup @Defop SKIPDICT @Defop SKIPOPTREF +x{F402} @Defop LDDICTS +x{F403} @Defop PLDDICTS +x{F404} dup @Defop LDDICT @Defop LDOPTREF +x{F405} dup @Defop PLDDICT @Defop PLDOPTREF +x{F406} @Defop LDDICTQ +x{F407} @Defop PLDDICTQ + +x{F40A} @Defop DICTGET +x{F40B} @Defop DICTGETREF +x{F40C} @Defop DICTIGET +x{F40D} @Defop DICTIGETREF +x{F40E} @Defop DICTUGET +x{F40F} @Defop DICTUGETREF + +x{F412} @Defop DICTSET +x{F413} @Defop DICTSETREF +x{F414} @Defop DICTISET +x{F415} @Defop DICTISETREF +x{F416} @Defop DICTUSET +x{F417} @Defop DICTUSETREF +x{F41A} @Defop DICTSETGET +x{F41B} @Defop DICTSETGETREF +x{F41C} @Defop DICTISETGET +x{F41D} @Defop DICTISETGETREF +x{F41E} @Defop DICTUSETGET +x{F41F} @Defop DICTUSETGETREF + +x{F422} @Defop DICTREPLACE +x{F423} @Defop DICTREPLACEREF +x{F424} @Defop DICTIREPLACE +x{F425} @Defop DICTIREPLACEREF +x{F426} @Defop DICTUREPLACE +x{F427} @Defop DICTUREPLACEREF +x{F42A} @Defop DICTREPLACEGET +x{F42B} @Defop DICTREPLACEGETREF +x{F42C} @Defop DICTIREPLACEGET +x{F42D} @Defop DICTIREPLACEGETREF +x{F42E} @Defop DICTUREPLACEGET +x{F42F} @Defop DICTUREPLACEGETREF + +x{F432} @Defop DICTADD +x{F433} @Defop DICTADDREF +x{F434} @Defop DICTIADD +x{F435} @Defop DICTIADDREF +x{F436} @Defop DICTUADD +x{F437} @Defop DICTUADDREF +x{F43A} @Defop DICTADDGET +x{F43B} @Defop DICTADDGETREF +x{F43C} @Defop DICTIADDGET +x{F43D} @Defop DICTIADDGETREF +x{F43E} @Defop DICTUADDGET +x{F43F} @Defop DICTUADDGETREF + +x{F441} @Defop DICTSETB +x{F442} @Defop DICTISETB +x{F443} @Defop DICTUSETB +x{F445} @Defop DICTSETGETB +x{F446} @Defop DICTISETGETB +x{F447} @Defop DICTUSETGETB + +x{F449} @Defop DICTREPLACEB +x{F44A} @Defop DICTIREPLACEB +x{F44B} @Defop DICTUREPLACEB +x{F44D} @Defop DICTREPLACEGETB +x{F44E} @Defop DICTIREPLACEGETB +x{F44F} @Defop DICTUREPLACEGETB + +x{F451} @Defop DICTADDB +x{F452} @Defop DICTIADDB +x{F453} @Defop DICTUADDB +x{F455} @Defop DICTADDGETB +x{F456} @Defop DICTIADDGETB +x{F457} @Defop DICTUADDGETB + +x{F459} @Defop DICTDEL +x{F45A} @Defop DICTIDEL +x{F45B} @Defop DICTUDEL + +x{F462} @Defop DICTDELGET +x{F463} @Defop DICTDELGETREF +x{F464} @Defop DICTIDELGET +x{F465} @Defop DICTIDELGETREF +x{F466} @Defop DICTUDELGET +x{F467} @Defop DICTUDELGETREF + +x{F469} @Defop DICTGETOPTREF +x{F46A} @Defop DICTIGETOPTREF +x{F46B} @Defop DICTUGETOPTREF +x{F46D} @Defop DICTSETGETOPTREF +x{F46E} @Defop DICTISETGETOPTREF +x{F46F} @Defop DICTUSETGETOPTREF + +x{F470} @Defop PFXDICTSET +x{F471} @Defop PFXDICTREPLACE +x{F472} @Defop PFXDICTADD +x{F473} @Defop PFXDICTDEL + +x{F474} @Defop DICTGETNEXT +x{F475} @Defop DICTGETNEXTEQ +x{F476} @Defop DICTGETPREV +x{F477} @Defop DICTGETPREVEQ +x{F478} @Defop DICTIGETNEXT +x{F479} @Defop DICTIGETNEXTEQ +x{F47A} @Defop DICTIGETPREV +x{F47B} @Defop DICTIGETPREVEQ +x{F47C} @Defop DICTUGETNEXT +x{F47D} @Defop DICTUGETNEXTEQ +x{F47E} @Defop DICTUGETPREV +x{F47F} @Defop DICTUGETPREVEQ + +x{F482} @Defop DICTMIN +x{F483} @Defop DICTMINREF +x{F484} @Defop DICTIMIN +x{F485} @Defop DICTIMINREF +x{F486} @Defop DICTUMIN +x{F487} @Defop DICTUMINREF +x{F48A} @Defop DICTMAX +x{F48B} @Defop DICTMAXREF +x{F48C} @Defop DICTIMAX +x{F48D} @Defop DICTIMAXREF +x{F48E} @Defop DICTUMAX +x{F48F} @Defop DICTUMAXREF + +x{F492} @Defop DICTREMMIN +x{F493} @Defop DICTREMMINREF +x{F494} @Defop DICTIREMMIN +x{F495} @Defop DICTIREMMINREF +x{F496} @Defop DICTUREMMIN +x{F497} @Defop DICTUREMMINREF +x{F49A} @Defop DICTREMMAX +x{F49B} @Defop DICTREMMAXREF +x{F49C} @Defop DICTIREMMAX +x{F49D} @Defop DICTIREMMAXREF +x{F49E} @Defop DICTUREMMAX +x{F49F} @Defop DICTUREMMAXREF + +x{F4A0} @Defop DICTIGETJMP +x{F4A1} @Defop DICTUGETJMP +x{F4A2} @Defop DICTIGETEXEC +x{F4A3} @Defop DICTUGETEXEC +{ dup sbitrefs tuck 1 > swap 1 <> or abort"not a dictionary" swap 1 u@ over <> abort"not a dictionary" } : @chkdicts +{ dup null? tuck { abort"method redefined with different id" } + { swap @declproc } + cond } : DECLMETHOD + "main" @proclistadd + dictnew +} : PROGRAM{ +{ dup sbits 1000 > { s>c } : }END> +{ }END> b> } : }END>c +{ }END>c s + +0 constant recv_internal +-1 constant recv_external +-2 constant run_ticktock +-3 constant split_prepare +-4 constant split_install + +// ( c -- ) add vm library for later use with runvmcode +{ spec } : hash>libref +// ( c -- c' ) +{ hash hash>libref } : >libref diff --git a/crypto/fift/lib/Fift.fif b/crypto/fift/lib/Fift.fif new file mode 100644 index 00000000..292711e7 --- /dev/null +++ b/crypto/fift/lib/Fift.fif @@ -0,0 +1,83 @@ +{ 0 word drop 0 'nop } :: // +{ char " word 1 { swap { abort } if drop } } ::_ abort" +{ { bl word dup "" $= abort"comment extends after end of file" "*/" $= } until 0 'nop } :: /* +// { bl word 1 2 ' (create) } "::" 1 (create) +// { bl word 0 2 ' (create) } :: : +// { bl word 2 2 ' (create) } :: :_ +// { bl word 3 2 ' (create) } :: ::_ +// { bl word 0 (create) } : create +// { bl word (forget) } : forget +{ bl word 1 ' (forget) } :: [forget] +{ char " word 1 ' type } ::_ ." +{ swap ({) over 2+ -roll swap (compile) (}) } : does +{ 1 'nop does create } : constant +{ 2 'nop does create } : 2constant +{ hole constant } : variable +10 constant ten +{ bl word 1 { find 0= abort"word not found" } } :: (') +{ bl word find not abort"-?" 0 swap } :: [compile] +{ bl word 1 { + dup find { " -?" $+ abort } ifnot nip execute +} } :: @' +{ bl word 1 { swap 1 'nop does swap 0 (create) } +} :: =: +{ bl word 1 { -rot 2 'nop does swap 0 (create) } +} :: 2=: +{ } : s>c +{ s>c hash } : shash +// to be more efficiently re-implemented in C++ in the future +{ dup 0< ' negate if } : abs +{ 2dup > ' swap if } : minmax +{ minmax drop } : min +{ minmax nip } : max +"" constant <# +' $reverse : #> +{ swap 10 /mod char 0 + rot swap hold } : # +{ { # over 0<= } until } : #s +{ 0< { char - hold } if } : sign +// { dup abs <# #s rot sign #> nip } : (.) +// { (.) type } : ._ +// { ._ space } : . +{ bl (-trailing) } : -trailing +{ char 0 (-trailing) } : -trailing0 +{ char " word 1 ' $+ } ::_ +" +{ find 0<> dup ' nip if } : (def?) +{ bl word 1 ' (def?) } :: def? +{ bl word 1 { (def?) not } } :: undef? +{ def? ' skip-to-eof if } : skip-ifdef +{ bl word dup (def?) { drop skip-to-eof } { 'nop swap 0 (create) } cond } : library +{ bl word dup (def?) { 2drop skip-to-eof } { swap 1 'nop does swap 0 (create) } cond } : library-version +{ char ) word "$" swap $+ 1 { find 0= abort"undefined parameter" execute } } ::_ $( +// b s -- ? +{ sbitrefs rot brembitrefs rot >= -rot <= and } : s-fits? +{ 0 swap ! } : 0! +{ tuck @ + swap ! } : +! +{ tuck @ swap - swap ! } : -! +{ 1 swap +! } : 1+! +{ -1 swap +! } : 1-! +{ null swap ! } : null! +0 tuple constant nil +{ 1 tuple } : single +{ 2 tuple } : pair +{ 3 tuple } : triple +{ 1 untuple } : unsingle +{ 2 untuple } : unpair +{ 3 untuple } : untriple +{ over tuple? { swap count = } { 2drop false } cond } : tuple-len? +{ 0 tuple-len? } : nil? +{ 1 tuple-len? } : single? +{ 2 tuple-len? } : pair? +{ 3 tuple-len? } : triple? +{ 0 [] } : first +{ 1 [] } : second +{ 2 [] } : third +' pair : cons +' unpair : uncons +{ 0 [] } : car +{ 1 [] } : cdr +{ cdr car } : cadr +{ cdr cdr } : cddr +{ cdr cdr car } : caddr +{ null ' cons rot times } : list +{ true (atom) drop } : atom +{ bl word atom 1 'nop } ::_ ` diff --git a/crypto/fift/lib/Lisp.fif b/crypto/fift/lib/Lisp.fif new file mode 100644 index 00000000..fb91408d --- /dev/null +++ b/crypto/fift/lib/Lisp.fif @@ -0,0 +1,436 @@ +library Lisp // tiny Lisp (or rather Scheme) interpreter +"Lists.fif" include +variable lisp-dict +{ hole dup 1 { @ execute } does create } : recursive +{ atom>$ +" undefined" abort } : report-not-found +// a l -- d -1 or a 0 Look up definition d of atom a in dictionary l +{ { dup null? { drop false true } + { uncons -rot unpair -rot over eq? + { drop nip true true } { nip swap false } cond + } cond + } until +} : lookup-in +// a dict -- def +{ lookup-in ' report-not-found ifnot } : lookup-or-fail +{ lisp-dict @ lookup-or-fail } : lisp-dict-lookup +// a d -- Defines a with definition d in dictionary lisp-dict +{ pair lisp-dict @ cons lisp-dict ! } : lisp-dict-int-define +{ box lisp-dict-int-define } : lisp-dict-define +// a d -- Defines new a with defininition d +{ over lisp-dict @ lookup-in { 2drop atom>$ +" already defined" abort } + { drop lisp-dict-int-define } cond +} : lisp-dict-int-define-new +{ box lisp-dict-int-define-new } : lisp-dict-define-new +// a e -- Defines a with executable definition given by e +{ single lisp-dict-define-new } : lisp-dict-define-exec +// expr ctx def -- val +{ dup first execute } : run-definition +// expr ctx -- val +recursive lisp-ctx-eval { + over tuple? + { over first over lisp-ctx-eval run-definition } + { over atom? { lookup-or-fail @ } { drop } cond } + cond +} swap ! +// exp -- value +{ lisp-dict @ lisp-ctx-eval } : lisp-eval +// (exprs) ctx -- (vals) +recursive lisp-ctx-eval-list +{ over null? { drop } { + swap uncons -rot over lisp-ctx-eval -rot lisp-ctx-eval-list cons + } cond +} swap ! +// (exprs) ctx -- val +{ null rot { + dup null? { drop nip true } { + nip uncons swap 2 pick lisp-ctx-eval swap false + } cond } until +} : lisp-ctx-eval-list-last +// l c -- (args) +{ swap uncons nip swap lisp-ctx-eval-list } : extract-eval-arg-list +{ drop uncons nip } : extract-arg-list +// (x1 .. xn) e n -- x1 .. xn e +{ { swap uncons rot } swap times + swap null? not abort"invalid number of arguments" +} : unpack-list +// l c n e -- v +{ swap 2swap extract-eval-arg-list // e n (args) + -rot unpack-list execute +} : eval-exec-fixed +// l c n e -- v +{ 2 pick pair + swap 2swap extract-arg-list // [e c] n (args) + -rot unpack-list unpair swap execute +} : exec-fixed +// l c e -- v +{ -rot extract-eval-arg-list // e (args) + swap execute +} : eval-exec-list +{ -rot tuck extract-arg-list // e c (args) + swap rot execute +} : exec-list +// e a n -- +{ rot 2 { // expr ctx def n e + rot drop eval-exec-fixed } does + lisp-dict-define-exec +} : lisp-fixed-primitive +{ rot 2 { rot drop exec-fixed } does lisp-dict-define-exec +} : lisp-fixed-lazy-primitive +// e a -- +{ swap 1 { nip eval-exec-list } does lisp-dict-define-exec +} : lisp-primitive +{ swap 1 { nip exec-list } does lisp-dict-define-exec +} : lisp-lazy-primitive + +// Uncomment next line for Fift booleans +// false constant #f true constant #t null constant no-answer +// Uncomment next line for Scheme booleans +`#f constant #f `#t constant #t #f constant no-answer +{ #f eq? } : lisp-false? +{ lisp-false? 0= } : lisp-true? +{ ' #t ' #f cond } : lisp-bool + +// temp for defining a lot of primitives +{ bl word atom lisp-primitive } : L: +{ bl word atom swap lisp-dict-define } : L=: +{ bl word atom swap lisp-fixed-primitive } : #L: +{ 0 #L: } : 0L: +{ 1 #L: } : 1L: +{ 2 #L: } : 2L: + +// basic primitives +{ sum-list } L: + +{ - } 2L: - +{ dup null? { drop 1 } { ' * foldl-ne } cond } L: * +{ / } 2L: / +{ mod } 2L: modulo +{ abs } 1L: abs +{ ' min foldl-ne } L: min +{ ' max foldl-ne } L: max +{ true ' and foldl } L: integer-and +{ false ' or foldl } L: integer-or +{ 0 ' xor foldl } L: integer-xor +{ not } 1L: integer-not +{ = lisp-bool } 2L: = +{ <> lisp-bool } 2L: <> +{ < lisp-bool } 2L: < +{ <= lisp-bool } 2L: <= +{ > lisp-bool } 2L: > +{ >= lisp-bool } 2L: >= +{ eq? lisp-bool } 2L: eq? +{ eqv? lisp-bool } 2L: eqv? +{ equal? lisp-bool } 2L: equal? +{ cons } 2L: cons +{ car } 1L: car +{ cdr } 1L: cdr +{ cadr } 1L: cadr +{ cddr } 1L: cddr +{ caddr } 1L: caddr +{ cdr cddr } 1L: cdddr +{ concat-list-lists } L: append +{ list-reverse } 1L: reverse +{ list-tail } 2L: list-tail +{ list-ref } 2L: list-ref +{ list-member-eq } 2L: memq +{ list-member-eqv } 2L: memv +{ list-member-equal } 2L: member +{ assq ' #f ifnot } 2L: assq +{ assv ' #f ifnot } 2L: assv +{ assoc ' #f ifnot } 2L: assoc +{ list? lisp-bool } 1L: list? +{ pair? lisp-bool } 1L: pair? +{ tuple? lisp-bool } 1L: tuple? +{ string? lisp-bool } 1L: string? +{ integer? lisp-bool } 1L: integer? +{ integer? lisp-bool } 1L: number? +{ count } 1L: width +{ list-length } 1L: length +{ [] } 2L: tuple-ref +{ first } 1L: first +{ second } 1L: second +{ third } 1L: third +{ 3 [] } 1L: fourth +{ list>tuple } 1L: list->tuple +{ explode list } 1L: tuple->list +null L=: null +{ atom? lisp-bool } 1L: symbol? +{ atom } 1L: string->symbol +{ atom>$ } 1L: symbol->string +{ dup #f eq? swap #t eq? or lisp-bool } 1L: boolean? +#t L=: else +#f L=: #f +#t L=: #t +{ null? lisp-bool } 1L: null? +{ 0= lisp-bool } 1L: zero? +{ 0> lisp-bool } 1L: positive? +{ 0< lisp-bool } 1L: negative? +{ 1 and 0= lisp-bool } 1L: even? +{ 1 and 0<> lisp-bool } 1L: odd? +{ bye } 0L: exit +{ .l null } 1L: write +{ lisp-eval } 1L: eval +{ drop } `quote 1 lisp-fixed-lazy-primitive +'nop L: list +{ list>tuple } L: tuple +{ list-last } L: begin +{ $len } 1L: string-length +{ concat-string-list } L: string-append +{ $= lisp-bool } 2L: string=? +{ $cmp 0< lisp-bool } 2L: string lisp-bool } 2L: string>? +{ $cmp 0>= lisp-bool } 2L: string>=? +{ (number) dup 1 = { drop } { ' 2drop if no-answer } cond +} 1L: string->number +{ (.) } 1L: number->string +{ box? lisp-bool } 1L: box? +{ box } 1L: box +{ hole } 0L: new-box +{ @ } 1L: unbox +{ tuck swap ! } 2L: set-box! +{ abort } 1L: error +{ dup find { nip execute } { +" -?" abort } cond } : find-execute +{ explode-list 1- roll find-execute } L: fift-exec +{ explode-list dup 1- swap roll find-execute } L: fift-exec-cnt +{ uncons swap find-execute } L: fift-exec-list +// end of basic primitives +forget L: forget #L: forget L=: +forget 0L: forget 1L: forget 2L: + +{ { dup tuple? ' do-quote if } list-map } : map-quote +{ uncons ' cons foldr-ne map-quote + null swap cons lisp-dict @ rot run-definition +} `apply lisp-primitive // bad: should have preserved original context +// e1 e2 e3 ctx +{ 3 exch 3 pick lisp-ctx-eval lisp-true? ' swap if nip swap lisp-ctx-eval } +`if 3 lisp-fixed-lazy-primitive +// (e) ctx +{ #t -rot + { over null? { 2drop true } { + swap uncons swap 2 pick lisp-ctx-eval dup lisp-true? // v' c t v ? + { swap 2swap nip false } { -rot 2drop nip true } cond + } cond } until +} `and lisp-lazy-primitive +{ #f -rot + { over null? { 2drop true } { + swap uncons swap 2 pick lisp-ctx-eval dup lisp-false? // v' c t v ? + { swap 2swap nip false } { -rot 2drop nip true } cond + } cond } until +} `or lisp-lazy-primitive +{ lisp-false? lisp-bool } `not 1 lisp-fixed-primitive +// cond-clause ctx -- v -1 or 0 +{ swap uncons -rot dup `else eq? { + drop lisp-ctx-eval-list-last true } { + over lisp-ctx-eval lisp-true? { + lisp-ctx-eval-list-last true } { + 2drop false + } cond } cond +} : eval-cond-clause +// (clauses) ctx -- v +{ { over null? { no-answer true } { + swap uncons -rot over eval-cond-clause } cond + } until -rot 2drop +} `cond lisp-lazy-primitive +{ lisp-dict @ lookup-in { hole tuck lisp-dict-int-define } ifnot +} : lisp-create-global-var +// a e ctx -- old (simple) define +{ drop over atom? not abort"only a variable can be define'd" + over lisp-create-global-var swap lisp-eval swap ! +} drop // `define 2 lisp-fixed-lazy-primitive +{ tuck lisp-ctx-eval rot dup atom? not abort"only a variable can be set" + rot lookup-or-fail dup @ -rot ! +} `set! 2 lisp-fixed-lazy-primitive +// define lambda +{ { dup null? { drop true true } + { uncons swap atom? { false } { drop false true } cond } cond + } until +} : var-list? +{ { dup null? over atom? or { drop true true } + { uncons swap atom? { false } { drop false true } cond } cond + } until +} : lambda-var-list? +// (quote x) -- x -1 ; else 0 +{ dup pair? { uncons swap `quote eq? { car true } { drop false } cond } + { drop false } cond +} : is-quote? +recursive match-arg-list-acc +// l (vars) (args) -- ((var . arg) ...)+l -1 or ? 0 +{ over atom? { over `_ eq? { 2drop } { pair swap cons } cond true } { + over null? { nip null? } { // (vars) (args) + over tuple? not { 2drop false } { + over is-quote? { eq? nip } { // (v) (a) + dup tuple? not { 2drop false } { + over count over count over <> { drop 2drop false } { // l [v] [a] n + 3 roll 0 rot { // [v] [a] l i + dup 0< { + 3 pick over [] swap // [v] [a] l vi i + 3 pick over [] 2swap rot // [v] [a] i l vi ai + match-arg-list-acc { // [v] [a] i l' + swap 1+ } { nip -1 } cond + } ifnot + } swap times + 2swap 2drop 0>= + } cond } cond } cond } cond } cond } cond +} swap ! +{ null -rot match-arg-list-acc } : match-arg-list +// ((var . arg)...) ctx -- ctx' +{ { over null? not } + { swap uncons swap unpair box pair rot cons } while + nip +} : extend-ctx-by-list +// ((vars) body) ctx +{ swap uncons -rot + dup lambda-var-list? not abort"invalid formal parameter list" + { // l-expr ctx' [_ body ctx (vars)] + -rot 2 pick 3 [] swap rot // [_ body ...] (vars) ctx' l-expr + uncons nip swap lisp-ctx-eval-list // [_ body ...] (vars) (arg-vals) + match-arg-list not abort"invalid arguments to lambda" // [_ body ...] ((var arg)...) + over third extend-ctx-by-list // [_ body ctx (vars)] ctx'' + swap second swap lisp-ctx-eval-list-last + } 3 -roll 4 tuple +} : make-lambda +{ make-lambda } `lambda lisp-lazy-primitive +// (a e) ctx -- more sophisticated (define a e) +{ drop uncons swap dup atom? { // (e) a + tuck lisp-create-global-var + swap lisp-dict @ lisp-ctx-eval-list-last swap ! + } { // (e) (a v..) + uncons over atom? not abort"only variables can be define'd" // (e) a (v..) + rot cons over lisp-create-global-var // a ((v..) (e)) h + swap lisp-dict @ make-lambda swap ! + } cond +} `define lisp-lazy-primitive +// ((x e) ..) ctx -- ((x.v) ..) +recursive eval-assign-list +{ over null? { drop } { + swap uncons swap uncons // ctx t x (e) + over atom? not abort"invalid variable name in assignment list" + 3 pick lisp-ctx-eval-list-last // ctx t x v + pair swap rot eval-assign-list cons + } cond +} swap ! +// (((x v) ..) body) ctx -- let construct +{ swap uncons swap 2 pick eval-assign-list // ctx body ((x v)...) + rot extend-ctx-by-list lisp-ctx-eval-list-last +} `let lisp-lazy-primitive +// ((x e) ..) ctx -- ctx' +{ swap { + dup null? { drop true } { + uncons swap uncons // ctx t x (e) + over atom? not abort"invalid variable name in assignment list" + 3 pick lisp-ctx-eval-list-last // ctx t x v + box pair rot cons swap false + } cond } until +} : compute-let*-ctx +// (((x v) ..) body) ctx -- let* construct +{ swap uncons swap rot compute-let*-ctx lisp-ctx-eval-list-last +} `let* lisp-lazy-primitive +// ((x e) ..) ctx -- ((h e) ..) ctx' , with x bound to h in ctx' +recursive prepare-letrec-ctx { + over null? { + swap uncons swap uncons swap // ctx t (e) x + hole tuck pair swap rot cons // ctx t (x.h) (h e) + 3 -roll rot cons prepare-letrec-ctx // (h e) t ctx' + -rot cons swap + } ifnot +} swap ! +// (((x v) ..) body) ctx -- letrec construct +{ swap uncons swap rot prepare-letrec-ctx swap { // body ctx' ((h e)..) + dup null? { drop true } { + uncons -rot uncons 2 pick lisp-ctx-eval-list-last // body t ctx' h v + swap ! swap false + } cond } until + lisp-ctx-eval-list-last +} `letrec lisp-lazy-primitive +// (e (p e)...) ctx -- match construct +{ swap uncons swap 2 pick lisp-ctx-eval swap { // ctx v ((p e)..) + dup null? { drop 2drop no-answer true } { + uncons swap uncons swap 3 pick // ctx v t e p v + match-arg-list { // ctx v t e ((x' . v')...) + 2swap 2drop rot extend-ctx-by-list lisp-ctx-eval-list-last true } { + 2drop false + } cond } cond } until +} `match lisp-lazy-primitive +// +lisp-dict @ constant original-lisp-dict +{ original-lisp-dict lisp-dict ! } : reset-lisp +{ ' drop { lisp-eval .l cr } List-generic( } :_ LISP-EVAL-PRINT( +// LISP-EVAL-PRINT((+ 3 4) (* 5 6)) computes and prints 12 and 30 +{ hole dup 1 { @ nip } does swap + 1 { swap lisp-eval swap ! } does + List-generic( +} :_ LISP-EVAL( +// LISP-EVAL((+ 3 4) (* 5 6)) computes 12 and 30, returns only 30 +// /* +LISP-EVAL-PRINT( + (define succ (lambda (x) (+ x 1))) + (define (twice f) (lambda (x) (f (f x)))) + (define (fact n) (if (= n 0) 1 (* n (fact (- n 1))))) + (fact ((twice succ) 5)) + (define compare (lambda (x y) (cond ((< x y) 'less) ((= x y) 'equal) (else 'greater)))) + (compare 2 3) + (compare 7 (+ 2 3)) + (define next (let ((cnt 0)) (lambda () (set! cnt (+ cnt 1)) cnt))) + (list (next) (next)) + (define new-counter (lambda () (let ((x 0)) (lambda () (set! x (+ x 1)) x)))) + (define c1 (new-counter)) + (define c2 (new-counter)) + (list (c1) (c1) (c2) (c1) (c2) (c1) (c1) (c2) (c2)) + (let* ((x (+ 2 3)) (y (* x x)) (z (+ x y))) (list x y z)) + (letrec ((even? (lambda (n) (if (= n 0) #t (odd? (- n 1))))) + (odd? (lambda (n) (if (= n 0) #f (even? (- n 1)))))) + (even? 88)) + (define (len l) (if (null? l) 0 (+ 1 (len (cdr l))))) + (len '(2 3 9)) + (define (len2 l) (match l (() 0) ((x . t) (+ 1 (len2 t))))) + (len2 '(2 3 9)) + (define (foo x) (match x + (('zero) 0) + (('succ x) (+ (foo x) 1)) + (('plus x y) (+ (foo x) (foo y))) + (('minus x y) (- (foo x) (foo y))) + (x x))) + (foo '(plus (succ (zero)) (minus (succ (succ 5)) 3))) + (define (bar x) (match x + (['zero] 0) + (['succ x] (+ (bar x) 1)) + (['plus x y] (+ (bar x) (bar y))) + (['minus x y] (- (bar x) (bar y))) + (['const x] x))) + (bar '[plus [succ [zero]] [minus [succ [succ [const 5]]] [const 3]]]) + (define (map f l) (letrec + ((map-f (lambda (l) (match l + (() ()) + ((h . t) (cons (f h) (map-f t))))))) + (map-f l))) + (map (lambda (x) (* x (+ 2 x))) '(2 3 9)) + (define (make-promise proc) (let ((result-ready? #f) (result #f)) + (lambda () + (if result-ready? result + (let ((x (proc))) + (if result-ready? result + (begin (set! result x) (set! result-ready? #t) result))))))) + (define (force promise) (promise)) +) +// */ +// words for invoking Lisp definitions from Fift +// (args) def -- val +{ null rot map-quote cons lisp-dict @ rot run-definition +} : invoke-lisp-definition +{ atom lisp-dict-lookup 1 { @ invoke-lisp-definition } +} : (invoke-lisp) +{ bl word (invoke-lisp) } :: invoke-lisp +// ( 2 3 ) invoke-lisp compare .l +{ atom lisp-dict-lookup 2 { @ mklist-1 invoke-lisp-definition } +} : (invoke-lisp-fixed) +{ bl word (invoke-lisp-fixed) } :: invoke-lisp-fixed +// 9 8 2 invoke-lisp-fixed compare .l +{ bl word (invoke-lisp) does } : make-lisp-invoker +{ bl word (invoke-lisp-fixed) does } : make-lisp-fixed-invoker +// 2 make-lisp-fixed-invoker compare : compare +// 3 9 compare +// import Lisp definitions as Fift words +{ bl word dup (invoke-lisp) does swap 0 (create) } : import-lisp +{ bl word tuck (invoke-lisp-fixed) does swap 0 (create) } : import-lisp-fixed +// 1 import-lisp-fixed fact +// 7 fact . diff --git a/crypto/fift/lib/Lists.fif b/crypto/fift/lib/Lists.fif new file mode 100644 index 00000000..3d50ae0f --- /dev/null +++ b/crypto/fift/lib/Lists.fif @@ -0,0 +1,184 @@ +library Lists // List utilities +// +{ hole dup 1 { @ execute } does create } : recursive +// x x' -- ? recursively compares two S-expressions +recursive equal? { + dup tuple? { + over tuple? { + over count over count over = { // t t' l ? + 0 { dup 0>= { 2dup [] 3 pick 2 pick [] equal? { 1+ } { drop -1 } cond + } if } rot times + nip nip 0>= + } { drop 2drop false } cond + } { 2drop false } cond + } { eqv? } cond +} swap ! +// (a1 .. an) -- (an .. a1) +{ null swap { dup null? not } { uncons swap rot cons swap } while drop } : list-reverse +// (a1 .. an) -- an Computes last element of non-empty list l +{ { uncons dup null? { drop true } { nip false } cond } until } : list-last +// l l' -- l++l' Concatenates two lists +recursive list+ { + over null? { nip } { swap uncons rot list+ cons } cond +} swap ! +// l l' -- l'' -1 or 0, where l = l' ++ l'' +// Removes prefix from list +{ { dup null? { drop true true } { + swap dup null? { 2drop false true } { // l' l + uncons swap rot uncons -rot equal? { false } { + 2drop false true + } cond } cond } cond } until +} : list- +// (a1 .. an) -- a1 .. an n Explodes a list +{ 0 { over null? not } { swap uncons rot 1+ } while nip } : explode-list +// (a1 .. an) x -- a1 .. an n x Explodes a list under the topmost element +{ swap explode-list dup 1+ roll } : explode-list-1 +// l -- t Transforms a list into a tuple with the same elements +{ explode-list tuple } : list>tuple +// a1 ... an n x -- (a1 .. an) x +{ null swap rot { -rot cons swap } swap times } : mklist-1 +// (s1 ... sn) -- s1+...+sn Concatenates a list of strings +{ "" { over null? not } { swap uncons -rot $+ } while nip +} : concat-string-list +// (x1 ... xn) -- x1+...+xn Sums a list of integers +{ 0 { over null? not } { swap uncons -rot + } while nip +} : sum-list +// (a1 ... an) a e -- e(...e(e(a,a1),a2),...),an) +{ -rot { over null? not } { swap uncons -rot 3 pick execute } while nip nip +} : foldl +// (a1 ... an) e -- e(...e(e(a1,a2),a3),...),an) +{ swap uncons swap rot foldl } : foldl-ne +// (a1 ... an) a e -- e(a1,e(a2,...,e(an,a)...)) +recursive foldr { + rot dup null? { 2drop } { + uncons -rot 2swap swap 3 pick foldr rot execute + } cond +} swap ! +// (a1 ... an) e -- e(a1,e(a2,...,e(a[n-1],an)...)) +recursive foldr-ne { + over cdr null? { drop car } { + swap uncons 2 pick foldr-ne rot execute + } cond +} swap ! +// (l1 ... ln) -- l1++...++ln Concatenates a list of lists +{ dup null? { ' list+ foldr-ne } ifnot } : concat-list-lists +// (a1 .. an . t) n -- t Computes the n-th tail of a list +{ ' cdr swap times } : list-tail +// (a0 .. an ..) n -- an Computes the n-th element of a list +{ list-tail car } : list-ref +// l -- ? +{ { dup null? { drop true true } { + dup pair? { cdr false } { + drop false true + } cond } cond } until +} : list? +// l -- n +{ 0 { over null? not } { 1+ swap uncons nip swap } while nip +} : list-length +// l e -- t // returns tail of l after first member that satisfies e +{ swap { + dup null? { nip true } { + tuck car over execute { drop true } { + swap cdr false + } cond } cond } until +} : list-tail-from +// a l -- t // tail of l after first occurence of a using eq? +{ swap 1 ' eq? does list-tail-from } : list-member-eq +{ swap 1 ' eqv? does list-tail-from } : list-member-eqv +{ swap 1 ' equal? does list-tail-from } : list-member-equal +// a l -- ? +{ list-member-eq null? not } : list-member? +{ list-member-eqv null? not } : list-member-eqv? +// l -- a -1 or 0 // returns car l if l is non-empty +{ dup null? { drop false } { car true } cond +} : safe-car +{ dup null? { drop false } { car second true } cond +} : get-first-value +// l e -- v -1 or 0 +{ list-tail-from safe-car } : assoc-gen +{ list-tail-from get-first-value } : assoc-gen-x +// a l -- (a.v) -1 or 0 -- returns first entry (a . v) in l +{ swap 1 { swap first eq? } does assoc-gen } : assq +{ swap 1 { swap first eqv? } does assoc-gen } : assv +{ swap 1 { swap first equal? } does assoc-gen } : assoc +// a l -- v -1 or 0 -- returns v from first entry (a . v) in l +{ swap 1 { swap first eq? } does assoc-gen-x } : assq-val +{ swap 1 { swap first eqv? } does assoc-gen-x } : assv-val +{ swap 1 { swap first equal? } does assoc-gen-x } : assoc-val +// (a1 .. an) e -- (e(a1) .. e(an)) +recursive list-map { + over null? { drop } { + swap uncons -rot over execute -rot list-map cons + } cond +} swap ! +// +// create Lisp-style lists using words "(" and ")" +// +variable ') +'nop box constant ', +{ ") without (" abort } ') ! +{ ') @ execute } : ) +anon constant dot-marker +// m x1 ... xn t m -- (x1 ... xn . t) +{ swap + { -rot 2dup eq? not } + { over dot-marker eq? abort"invalid dotted list" + swap rot cons } while 2drop +} : list-tail-until-marker +// m x1 ... xn m -- (x1 ... xn) +{ null swap list-tail-until-marker } : list-until-marker +{ over dot-marker eq? { nip 2dup eq? abort"invalid dotted list" } + { null swap } cond + list-tail-until-marker +} : list-until-marker-ext +{ ') @ ', @ } : ops-get +{ ', ! ') ! } : ops-set +{ anon dup ops-get 3 { ops-set list-until-marker-ext } does ') ! 'nop ', ! +} : ( +// test of Lisp-style lists +// ( 42 ( `+ 9 ( `* 3 4 ) ) "test" ) .l cr +// ( `eq? ( `* 3 4 ) 3 4 * ) .l cr +// `alpha ( `beta `gamma `delta ) cons .l cr +// { ( `eq? ( `* 3 5 pick ) 3 4 roll * ) } : 3*sample +// 17 3*sample .l cr + +// similar syntax _( x1 .. xn ) for tuples +{ 2 { 1+ 2dup pick eq? } until 3 - nip } : count-to-marker +{ count-to-marker tuple nip } : tuple-until-marker +{ anon dup ops-get 3 { ops-set tuple-until-marker } does ') ! 'nop ', ! } : _( +// test of tuples +// _( _( 2 "two" ) _( 3 "three" ) _( 4 "four" ) ) .dump cr + +// pseudo-Lisp tokenizer +"()[]'" 34 hold constant lisp-delims +{ lisp-delims 11 (word) } : lisp-token +{ null cons `quote swap cons } : do-quote +{ 1 { ', @ 2 { 2 { ', ! execute ', @ execute } does ', ! } + does ', ! } does +} : postpone-prefix +{ ', @ 1 { ', ! } does ', ! } : postpone-', +( `( ' ( pair + `) ' ) pair + `[ ' _( pair + `] ' ) pair + `' ' do-quote postpone-prefix pair + `. ' dot-marker postpone-prefix pair + `" { char " word } pair + `;; { 0 word drop postpone-', } pair +) constant lisp-token-dict +variable eol +{ eol @ eol 0! anon dup ') @ 'nop 3 + { ops-set list-until-marker-ext true eol ! } does ') ! rot ', ! + { lisp-token dup (number) dup { roll drop } { + drop atom dup lisp-token-dict assq { nip second execute } if + } cond + ', @ execute + eol @ + } until + -rot eol ! execute +} :_ List-generic( +{ 'nop 'nop List-generic( } :_ LIST( +// LIST((lambda (x) (+ x 1)) (* 3 4)) +// LIST('(+ 3 4)) +// LIST(2 3 "test" . 9) +// LIST((process '[plus 3 4])) diff --git a/crypto/fift/lib/Stack.fif b/crypto/fift/lib/Stack.fif new file mode 100644 index 00000000..c7ef6f8e --- /dev/null +++ b/crypto/fift/lib/Stack.fif @@ -0,0 +1,266 @@ +library Stack // advanced stack manupulation library +"Lists.fif" include +// S(a b c - a c 2 a b) would compile to code performing the requested stack manipulation + +// interface to low-level stack manipulation primitives +{ (number) 1- abort"index expected" dup 0 < over 255 > or + abort"index 0..255 expected" +} : (idx) +// push(n) : a0 .. an - a0 .. an a0 equivalent to "n pick" +// push(0) = dup, push(1) = over +{ 0 char ) word (idx) } ::_ push( +// pop(n) : a0 a1 .. a(n-1) an - an a1 .. a(n-1) +// pop(0) = drop, pop(1) = nip +{ 0 char ) word (idx) } ::_ pop( +// xchg(i,j) : equivalent to "i j exch2" +{ 0 char , word (idx) char ) word (idx) } ::_ xchg( +// xchg0(i) : equivalent to "i exch" or "xchg(0,i)" +// xchg0(1) = swap +{ 0 char ) word (idx) 0 } ::_ xchg0( +forget (idx) + +// parser for stack notation expressions +")" 34 hold +" -" constant stk-delims +anon constant stk-start +anon constant stk-to +variable stk-mode +{ stk-delims 11 (word) } : stk-token +'nop : mk-lit +// stk-start vn ... v0 -- stk-start ... v0 i where v[i]=v0 +{ 0 { + 1+ 2dup 2+ pick dup stk-start eq? { 2drop drop 0 true } { eqv? } cond + } until +} : stk-lookup +// stk-start a1 .. an stk-to b1 .. bm -- [a1 .. an] [b1 .. bm] +{ stk-mode @ 0= abort"identifier expected" } : chk-lit +{ stk-to list-until-marker stk-mode ! + stk-start list-until-marker stk-mode @ + stk-mode 0! +} : build-stk-effect +{ stk-start stk-mode 0! { + stk-token dup ")" $= { drop true } { + dup "-" $= { + drop stk-mode @ abort"duplicate -" true stk-mode ! stk-to false } { + dup 34 chr $= { chk-lit drop char " word mk-lit false } { + dup (number) ?dup { chk-lit 1- { swap mk-lit -rot } if mk-lit nip false } { + atom dup `_ eq? { stk-mode @ abort"identifier expected" false } { + stk-lookup 0= stk-mode @ = { + stk-mode @ { atom>$ +" -?" } { atom>$ +" redefined" } cond abort } { + false + } cond } cond } cond } cond } cond } cond } until + stk-mode @ 0= abort"'-' expected" + build-stk-effect +} :_ parse-stk-list( + +// stack operation list construction +variable op-rlist +{ op-rlist null! } : clear-op-list +{ op-rlist @ list-reverse } : get-op-list +{ op-rlist @ cons op-rlist ! } : issue-op +{ minmax `xchg -rot triple } : op-xchg +{ `push swap pair } : op-push +{ `lit swap pair } : op-lit +{ `pop swap pair } : op-pop +0 op-pop constant op-drop +{ 2dup <> { op-xchg issue-op } if } : issue-xchg +{ op-push issue-op } : issue-push +{ op-lit issue-op } : issue-lit +{ op-pop issue-op } : issue-pop +{ op-drop issue-op } : issue-drop +{ ' issue-drop swap times } : issue-drop-# + +// emulated stack contents +variable emul-stk +{ emul-stk @ count } : emul-depth +{ emul-depth 1- swap - } : adj-i +{ emul-depth 1- tuck swap - swap rot - swap } : adj-ij +// i j -- +{ adj-ij 2dup emul-stk @ tuck swap [] swap rot [] rot // i sj si j + emul-stk @ -rot []= swap rot []= emul-stk ! +} : emul-xchg +{ emul-stk @ tpop drop emul-stk ! } : emul-drop +// i -- +{ 0 emul-xchg emul-drop } : emul-pop +// i -- s[i] +{ emul-stk @ swap [] } : emul-stk[] +// i -- si +{ adj-i emul-stk[] } : emul-get +{ 0 emul-get } : emul-tos +// v i -- ? Check whether s[i]=v +{ dup emul-depth < { emul-stk[] eqv? } { 2drop false } cond } : emul[]-eq? +// v -- i or -1 Returns maximum i with s[i]=v +{ emul-stk @ dup count { // v s i + ?dup 0= { -1 true } { 1- 2dup [] 3 pick eqv? } cond // v s i' ? + } until nip nip +} : emul-stk-lookup-rev +// i -- +{ emul-get emul-stk @ swap , emul-stk ! } : emul-push +{ emul-stk @ swap , emul-stk ! } : emul-lit +// show emulated stack contents similarly to .s +{ emul-stk @ explode dup 1 reverse ' .l swap times cr } : .e + +// both issue an operation and emulate it +{ 2dup issue-xchg emul-xchg } : issue-emul-xchg +{ dup issue-push emul-push } : issue-emul-push +{ dup issue-lit emul-lit } : issue-emul-lit +{ dup issue-pop emul-pop } : issue-emul-pop +{ issue-drop emul-drop } : issue-emul-drop +{ ' issue-emul-drop swap times } : issue-emul-drop-# + +// b.. s -- b.. s moves tos value to stk[s] +{ dup emul-stk[] 2 pick cdr list-member-eqv? { + dup adj-i 0 issue-emul-xchg } { dup adj-i issue-emul-pop } cond +} : move-tos-to + +// new s -- ops registered +{ { over null? not } { + // .sl .e get-op-list .l cr + // get-op-list list-length 100 > abort"too long" + emul-depth over > + { over emul-tos swap list-member-eqv? not } { false } cond { + // b.. s tos unneeded + issue-emul-drop } { + over car // b.. s b1 + 2dup swap emul[]-eq? { drop swap cdr swap 1+ } { + dup emul-stk-lookup-rev // b.. s b1 i + dup 0< { // b.. s b1 i not found, must be a literal + drop dup atom? abort"unavailable value" + issue-emul-lit } { + dup 3 pick < { // b.. s b1 i found in bottom s stack values + nip adj-i issue-emul-push // b.. s + dup emul-depth 1- < { move-tos-to } if + } { + emul-depth 1- over = { // b.. s b1 i found in tos + 2drop move-tos-to + } { // b.. s b1 i + nip over adj-ij issue-emul-xchg + } cond } cond } cond } cond } cond } while + nip emul-depth swap - issue-emul-drop-# +} : generate-reorder-ops + +// old new -- op-list +{ emul-stk @ op-rlist @ 2swap + swap list>tuple emul-stk ! clear-op-list + 0 generate-reorder-ops get-op-list + -rot op-rlist ! emul-stk ! +} : generate-reorder +{ parse-stk-list( generate-reorder } :_ SG( + +// op-list rewriting according to a ruleset +// l f l1 l2 -- l' -1 or l f with l' = l2 + (l - l1) +{ push(3) rot list- { list+ nip nip true } { drop } cond +} : try-rule +// l f ll -- l' -1 or l f +{ { dup null? not } { uncons 3 -roll unpair try-rule rot } while drop +} : try-ruleset +// l ll -- l' +{ swap { over false swap try-ruleset 0= } until nip +} : try-ruleset* +// l ruleset -- l' +recursive try-ruleset*-everywhere { + tuck try-ruleset* dup null? { nip } { + uncons rot try-ruleset*-everywhere cons } cond +} swap ! +LIST( + [([xchg 0 1] [xchg 0 2]) ([rot])] + [([xchg 0 1] [xchg 1 2]) ([-rot])] + [([xchg 0 2] [xchg 1 2]) ([rot])] + [([xchg 0 2] [xchg 0 1]) ([-rot])] + [([xchg 1 2] [xchg 0 1]) ([rot])] + [([xchg 1 2] [xchg 0 2]) ([-rot])] + [([xchg 0 1] [rot]) ([xchg 0 2])] + [([-rot] [xchg 0 1]) ([xchg 0 2])] + [([xchg 0 2] [xchg 1 3]) ([2swap])] + [([xchg 1 3] [xchg 0 2]) ([2swap])] + [([push 1] [push 1]) ([2dup])] + [([push 3] [push 3]) ([2over])] + [([pop 0] [pop 0]) ([2drop])] + [([pop 1] [pop 0]) ([2drop])] + [([xchg 0 1] [push 1]) ([tuck])] + [([rot] [-rot]) ()] + [([-rot] [rot]) ()] +) constant fift-stack-ruleset +{ fift-stack-ruleset try-ruleset*-everywhere } : fift-ops-rewrite +{ SG( fift-ops-rewrite } :_ SGF( + +// helpers for creating Fift source strings for one fift-op +// i j -- s +{ minmax over { "xchg(" rot (.) $+ +"," swap (.) $+ +")" } + { nip dup 1 = { drop "swap" } { + ?dup { "xchg0(" swap (.) $+ +")" } { "" } cond + } cond } cond +} : source- +// i -- s +{ dup 1 = { drop "over" } { + ?dup { "push(" swap (.) $+ +")" } { "dup" } cond + } cond +} : source- +// i -- s +{ dup 1 = { drop "nip" } { + ?dup { "pop(" swap (.) $+ +")" } { "drop" } cond + } cond +} : source- +// lit -- s +{ dup string? { char " chr swap $+ char " hold } { (.) } cond +} : source- + +// dictionary with all fift op compilation/source creation +{ 0 swap (compile) } : fop-compile +( _( `xchg 2 { fop-compile } { source- swap cons } ) + _( `push 1 { fop-compile } { source- swap cons } ) + _( `pop 1 { fop-compile } { source- swap cons } ) + _( `lit 1 { 1 'nop (compile) } { source- swap cons } ) + _( `rot 0 { ' rot fop-compile } { "rot" swap cons } ) + _( `-rot 0 { ' -rot fop-compile } { "-rot" swap cons } ) + _( `tuck 0 { ' tuck fop-compile } { "tuck" swap cons } ) + _( `2swap 0 { ' 2swap fop-compile } { "2swap" swap cons } ) + _( `2drop 0 { ' 2drop fop-compile } { "2drop" swap cons } ) + _( `2dup 0 { ' 2dup fop-compile } { "2dup" swap cons } ) + _( `2over 0 { ' 2over fop-compile } { "2over" swap cons } ) +) box constant fift-op-dict + +{ dup atom? { atom>$ } { drop "" } cond + "unknown operation " swap $+ abort +} : report-unknown-op +variable 'fop-entry-exec +// process fift-op according to 'fop-entry-exec +// ... op - ... +{ dup first dup fift-op-dict @ assq { report-unknown-op } ifnot + dup second 1+ push(3) count <> abort"incorrect param count" + nip swap explode dup roll drop 1- roll // o2 .. on entry + 'fop-entry-exec @ execute +} : process-fift-op + +// compile op-list into Fift wordlist +// wl op-list -- wl' +{ { third execute } 'fop-entry-exec ! + swap ' process-fift-op foldl } : compile-fift-op* +// op-list -- e +{ fift-ops-rewrite ({) swap compile-fift-op* (}) } : ops>wdef + +// S( - ) compiles a "word" performing required action +{ SG( ops>wdef 0 swap } ::_ S( +// 1 2 3 S(a b c - c a b a) .s would print 3 1 2 1 + +// transform op-list into Fift source +// ls op -- ls' +{ fift-ops-rewrite + { 3 [] execute } 'fop-entry-exec ! + null ' process-fift-op foldl + dup null? { drop "" } { { +" " swap $+ } foldr-ne } cond +} : ops>$ +{ SG( ops>$ 1 'nop } ::_ $S( +{ SG( ops>$ type } :_ .$S( +// $S(a b c - b c a c a c) => string "rot 2dup over" +// S(a b c - b c a c a c) => compile/execute block { rot 2dup over } +// $S(_ x y _ - y x) => string "drop pop(2)" +// .$S(x1 x2 - 17 x1) => print string "drop 17 swap" + +// simplify/transform sequences of stack manipulation operations +LIST(. [a b c d e f g h i j]) constant std-stack +{ stk-start std-stack explode drop stk-to std-stack explode drop +} : simplify<{ +{ build-stk-effect generate-reorder ops>$ } : }>stack +// simplify<{ drop drop over over -13 }>stack => string "2drop 2dup -13" +// simplify<{ 17 rot }>stack => string "swap 17 swap" +// simplify<{ 5 1 reverse }>stack => string "xchg(1,5) xchg(2,4)" diff --git a/crypto/fift/lib/TonUtil.fif b/crypto/fift/lib/TonUtil.fif new file mode 100644 index 00000000..2e2484ad --- /dev/null +++ b/crypto/fift/lib/TonUtil.fif @@ -0,0 +1,125 @@ +library TonUtil // TON Blockchain Fift Library +"Lists.fif" include + +-1 constant Masterchain +0 constant Basechain + +// parse workchain id +// ( S -- workchain ) +{ (number) 1- abort"workchain id must be an integer" + dup 32 fits not abort"workchain id must fit in 32 bits" +} : parse-workchain-id + +{ (number) 1- abort"integer expected" } : parse-int + +// Private key load/generate +// ( fname -- pubkey privkey ) +{ dup ."Loading private key from file " type cr + file>B dup Blen 32 <> abort"Private key must be exactly 32 bytes long" + dup priv>pub swap +} : load-keypair +// ( fname -- pubkey privkey ) +{ dup file-exists? + { load-keypair } + { dup newkeypair swap rot over swap B>file + rot ."Saved new private key to file " type cr + } cond +} : load-generate-keypair + +// Parse smart-contract address +// ( S -- workchain addr bounce? ) +{ $>smca not abort"invalid smart-contract address" + 1 and 0= +} : parse-smc-addr + +// ( wc addr -- ) Show address in : form +{ swap ._ .":" x. } : .addr +// ( wc addr flags -- ) Show address in base64url form +{ smca>$ type } : .Addr +// ( wc addr fname -- ) Save address to file in 36-byte format +{ -rot 256 u>B swap 32 i>B B+ swap B>file } : save-address +// ( wc addr fname -- ) Save address and print message +{ dup ."(Saving address to file " type .")" cr save-address +} : save-address-verbose + +// ( fname -- wc addr ) Load address from file +{ file>B 32 B| + dup Blen { 32 B>i@ } { drop Basechain } cond + swap 256 B>u@ +} : load-address +// ( fname -- wc addr ) Load address from file and print message +{ dup ."(Loading address from file " type .")" cr load-address +} : load-address-verbose +// Parse string as address or load address from file (if string is prefixed by @) +// ( S default-bounce -- workchain addr bounce? ) +{ over $len 0= abort"empty smart-contract address" + swap dup 1 $| swap "@" $= + { nip load-address rot } { drop nip parse-smc-addr } cond +} : parse-load-address + +// ( hex-str -- addr ) Parses ADNL address +{ dup $len 64 <> abort"ADNL address must consist of exactly 64 hexadecimal characters" + (hex-number) 1 <> abort"ADNL address must consist of 64 hexadecimal characters" + dup 256 ufits not abort"invalid ADNL address" +} : parse-adnl-address + +// ( b wc addr -- b' ) Serializes address into Builder b +{ -rot 8 i, swap 256 u, } : addr, + +// Gram utilities +1000000000 constant Gram +{ Gram swap */r } : Gram*/ +{ Gram * } : Gram* +// ( S -- nanograms ) +{ (number) ?dup 0= abort"not a valid Gram amount" +1- ' Gram*/ ' Gram* cond +} : $>GR +{ bl word $>GR 1 'nop } ::_ GR$ +// ( nanograms -- S ) +{ dup abs <# ' # 9 times char . hold #s rot sign #> +nip -trailing0 } : (.GR) +{ (.GR) ."GR$" type space } : .GR + +// b x -- b' ( serializes a Gram amount ) +{ -1 { 1+ 2dup 8 * ufits } until + rot over 4 u, -rot 8 * u, } : Gram, +// s -- x s' ( deserializes a Gram amount ) +{ 4 u@+ swap 8 * u@+ } : Gram@+ +// s -- x +{ 4 u@+ swap 8 * u@ } : Gram@ + +// currency collections +// b x --> b' ( serializes a VarUInteger32 ) +{ -1 { 1+ 2dup 8 * ufits } until + rot over 5 u, -rot 8 * u, } : VarUInt32, +// s --> x ( deserializes a VarUInteger32 ) +{ 5 u@+ swap 8 * u@ } : VarUInt32@ +32 constant cc-key-bits +' VarUInt32, : val, +' VarUInt32@ : val@ +// d k v -- d' +{ cc +{ dup null? { ."(null) " drop } { val@ . } cond } dup : .maybeVarUInt32 : .val +{ cc-key-bits { swap 32 1<< rmod . ."-> " .val ."; " true } dictforeach drop cr } : .cc +{ cc-key-bits { rot . ."-> " swap .val .val ."; " true } dictdiff drop cr } : show-cc-diff +{ cc-key-bits { val@ swap val@ + val, true } dictmerge } : cc+ +{ null swap cc-key-bits { val@ pair swap cons true } dictforeach drop } : cc>list-rev +{ cc>list-rev list-reverse } : cc>list +forget val, forget val@ forget .val + +// Libraries +// ( -- D ) New empty library collection +' dictnew : Libs{ +// ( D -- D ) Return library collection as dictionary +'nop : }Libs +// ( D c x -- D' ) Add a public/private library c to collection D +{ . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "utils.h" +#include "words.h" +#include "td/utils/PathView.h" +#include "td/utils/filesystem.h" +#include "td/utils/port/path.h" + +namespace fift { +namespace { + +std::string fift_dir(std::string dir) { + return dir.size() > 0 ? dir : td::PathView(td::realpath(__FILE__).move_as_ok()).parent_dir().str() + "lib/"; +} +td::Result load_source(std::string name, std::string dir = "") { + return td::read_file_str(fift_dir(dir) + name); +} +td::Result load_Fift_fif(std::string dir = "") { + return load_source("Fift.fif", dir); +} +td::Result load_Asm_fif(std::string dir = "") { + return load_source("Asm.fif", dir); +} +td::Result load_TonUtil_fif(std::string dir = "") { + return load_source("TonUtil.fif", dir); +} +td::Result load_Lists_fif(std::string dir = "") { + return load_source("Lists.fif", dir); +} + +class MemoryFileLoader : public fift::FileLoader { + public: + td::Result read_file(td::CSlice filename) override { + auto it = files_.find(filename); + if (it == files_.end()) { + return td::Status::Error("File not found"); + } + fift::FileLoader::File res; + res.data = it->second; + res.path = it->first; + return std::move(res); + } + + td::Status write_file(td::CSlice filename, td::Slice data) override { + files_[filename.str()] = data.str(); + return td::Status::OK(); + } + + void add_file(std::string path, std::string data) { + files_[path] = std::move(data); + } + td::Result read_file_part(td::CSlice filename, td::int64 size, td::int64 offset) override { + auto it = files_.find(filename); + if (it == files_.end()) { + return td::Status::Error("File not found"); + } + fift::FileLoader::File res; + if (static_cast(it->second.size()) < offset) { + return td::Status::Error("Offset too large"); + } + if (size > static_cast(it->second.size())) { + size = static_cast(it->second.size()); + } + res.data = td::Slice(it->second).substr(td::narrow_cast(offset), td::narrow_cast(size)).str(); + res.path = it->first; + return std::move(res); + } + + bool is_file_exists(td::CSlice filename) override { + return files_.count(filename) != 0; + } + + private: + std::map> files_; +}; + +td::Result create_source_lookup(std::string main, bool need_preamble = true, bool need_asm = true, + bool need_ton_util = true, std::string dir = "") { + auto loader = std::make_unique(); + loader->add_file("/main.fif", std::move(main)); + if (need_preamble) { + TRY_RESULT(f, load_Fift_fif(dir)); + loader->add_file("/Fift.fif", std::move(f)); + } + if (need_asm) { + TRY_RESULT(f, load_Asm_fif(dir)); + loader->add_file("/Asm.fif", std::move(f)); + } + if (need_ton_util) { + { + TRY_RESULT(f, load_Lists_fif(dir)); + loader->add_file("/Lists.fif", std::move(f)); + } + { + TRY_RESULT(f, load_TonUtil_fif(dir)); + loader->add_file("/TonUtil.fif", std::move(f)); + } + } + auto res = fift::SourceLookup(std::move(loader)); + res.add_include_path("/"); + return std::move(res); +} + +td::Result run_fift(fift::SourceLookup source_lookup, std::ostream *stream, + bool preload_fift = true, std::vector args = {}) { + fift::Fift::Config config; + config.source_lookup = std::move(source_lookup); + fift::init_words_common(config.dictionary); + fift::init_words_vm(config.dictionary); + fift::init_words_ton(config.dictionary); + config.error_stream = stream; + config.output_stream = stream; + if (args.size() != 0) { + std::vector argv; + for (auto &arg : args) { + argv.push_back(arg.c_str()); + } + fift::import_cmdline_args(config.dictionary, argv[0], td::narrow_cast(argv.size() - 1), argv.data() + 1); + } + fift::Fift fift{std::move(config)}; + if (preload_fift) { + TRY_STATUS(fift.interpret_file("Fift.fif", "")); + } + TRY_STATUS(fift.interpret_file("main.fif", "")); + return std::move(fift.config().source_lookup); +} +} // namespace +td::Result mem_run_fift(std::string source, std::vector args, std::string fift_dir) { + std::stringstream ss; + TRY_RESULT(source_lookup, create_source_lookup(source, true, true, true, fift_dir)); + TRY_RESULT_ASSIGN(source_lookup, run_fift(std::move(source_lookup), &ss, true, std::move(args))); + FiftOutput res; + res.source_lookup = std::move(source_lookup); + res.output = ss.str(); + return std::move(res); +} +td::Result mem_run_fift(SourceLookup source_lookup, std::vector args) { + std::stringstream ss; + TRY_RESULT_ASSIGN(source_lookup, run_fift(std::move(source_lookup), &ss, true, std::move(args))); + FiftOutput res; + res.source_lookup = std::move(source_lookup); + res.output = ss.str(); + return std::move(res); +} +td::Result create_mem_source_lookup(std::string main, std::string fift_dir, bool need_preamble, + bool need_asm, bool need_ton_util) { + return create_source_lookup(main, need_preamble, need_asm, need_ton_util, fift_dir); +} + +td::Result> compile_asm(td::Slice asm_code, std::string fift_dir) { + std::stringstream ss; + TRY_RESULT(source_lookup, + create_source_lookup(PSTRING() << "\"Asm.fif\" include\n<{ " << asm_code << "\n}>c boc>B \"res\" B>file", + true, true, true, fift_dir)); + TRY_RESULT(res, run_fift(std::move(source_lookup), &ss)); + TRY_RESULT(boc, res.read_file("res")); + return vm::std_boc_deserialize(std::move(boc.data)); +} +} // namespace fift diff --git a/crypto/fift/utils.h b/crypto/fift/utils.h new file mode 100644 index 00000000..e6ed1452 --- /dev/null +++ b/crypto/fift/utils.h @@ -0,0 +1,35 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +#include "Fift.h" +#include + +namespace fift { +struct FiftOutput { + SourceLookup source_lookup; + std::string output; +}; +td::Result create_mem_source_lookup(std::string main, std::string fift_dir = "", + bool need_preamble = true, bool need_asm = true, + bool need_ton_util = true); +td::Result mem_run_fift(std::string source, std::vector args = {}, std::string fift_dir = ""); +td::Result mem_run_fift(SourceLookup source_lookup, std::vector args); +td::Result> compile_asm(td::Slice asm_code, std::string fift_dir = ""); +} // namespace fift diff --git a/crypto/fift/words.cpp b/crypto/fift/words.cpp new file mode 100644 index 00000000..7b73742b --- /dev/null +++ b/crypto/fift/words.cpp @@ -0,0 +1,2747 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "words.h" + +#include "Dictionary.h" +#include "IntCtx.h" +#include "SourceLookup.h" + +#include "common/refcnt.hpp" +#include "common/bigint.hpp" +#include "common/refint.h" +#include "common/bitstring.h" +#include "common/util.h" + +#include "Ed25519.h" + +#include "vm/cells.h" +#include "vm/cellslice.h" +#include "vm/continuation.h" +#include "vm/cp0.h" +#include "vm/dict.h" +#include "vm/boc.h" + +#include "vm/box.hpp" +#include "vm/atom.h" + +#include "block/block.h" + +#include "td/utils/filesystem.h" +#include "td/utils/optional.h" +#include "td/utils/PathView.h" +#include "td/utils/port/thread.h" +#include "td/utils/port/Stat.h" +#include "td/utils/Timer.h" +#include "td/utils/tl_helpers.h" + +#include + +namespace fift { + +void show_total_cells(std::ostream& stream) { + stream << "total cells = " << vm::DataCell::get_total_data_cells() << std::endl; +} + +void do_compile(vm::Stack& stack, Ref word_def); +void do_compile_literals(vm::Stack& stack, int count); + +void interpret_dot(IntCtx& ctx, bool space_after) { + *ctx.output_stream << dec_string2(ctx.stack.pop_int()) << (space_after ? " " : ""); +} + +void interpret_dothex(IntCtx& ctx, bool upcase, bool space_after) { + *ctx.output_stream << hex_string(ctx.stack.pop_int(), upcase) << (space_after ? " " : ""); +} + +void interpret_dotbinary(IntCtx& ctx, bool space_after) { + *ctx.output_stream << binary_string(ctx.stack.pop_int()) << (space_after ? " " : ""); +} + +void interpret_dot_cellslice_rec(IntCtx& ctx) { + auto cs = ctx.stack.pop_cellslice(); + cs->print_rec(*ctx.output_stream); +} + +void interpret_dotstack(IntCtx& ctx) { + for (int i = ctx.stack.depth(); i > 0; i--) { + ctx.stack[i - 1].dump(*ctx.output_stream); + *ctx.output_stream << ' '; + } + *ctx.output_stream << std::endl; +} + +void interpret_dotstack_list(IntCtx& ctx) { + for (int i = ctx.stack.depth(); i > 0; i--) { + ctx.stack[i - 1].print_list(*ctx.output_stream); + *ctx.output_stream << ' '; + } + *ctx.output_stream << std::endl; +} + +void interpret_dump(IntCtx& ctx) { + ctx.stack.pop_chk().dump(*ctx.output_stream); + *ctx.output_stream << ' '; +} + +void interpret_dump_internal(vm::Stack& stack) { + stack.push_string(stack.pop_chk().to_string()); +} + +void interpret_print_list(IntCtx& ctx) { + ctx.stack.pop_chk().print_list(*ctx.output_stream); + *ctx.output_stream << ' '; +} + +void interpret_dottc(IntCtx& ctx) { + show_total_cells(*ctx.output_stream); +} + +void interpret_dot_internal(vm::Stack& stack) { + stack.push_string(dec_string2(stack.pop_int())); +} + +void interpret_dothex_internal(vm::Stack& stack, bool upcase) { + stack.push_string(hex_string(stack.pop_int(), upcase)); +} + +void interpret_dotbinary_internal(vm::Stack& stack) { + stack.push_string(binary_string(stack.pop_int())); +} + +void interpret_plus(vm::Stack& stack) { + stack.push_int(stack.pop_int() + stack.pop_int()); +} + +void interpret_cond_dup(vm::Stack& stack) { + auto x = stack.pop_int(); + if (x->sgn()) { + stack.push_int(x); + } + stack.push_int(std::move(x)); +} + +void interpret_plus_tiny(vm::Stack& stack, long long y) { + stack.push_int(stack.pop_int() + y); +} + +void interpret_minus(vm::Stack& stack) { + auto y = stack.pop_int(); + stack.push_int(stack.pop_int() - y); +} + +void interpret_times(vm::Stack& stack) { + stack.push_int(stack.pop_int() * stack.pop_int()); +} + +void interpret_div(vm::Stack& stack, int round_mode) { + auto y = stack.pop_int(); + stack.push_int(td::div(stack.pop_int(), y, round_mode)); +} + +void interpret_mod(vm::Stack& stack, int round_mode) { + auto y = stack.pop_int(); + stack.push_int(td::mod(stack.pop_int(), y, round_mode)); +} + +void interpret_divmod(vm::Stack& stack, int round_mode) { + auto y = stack.pop_int(); + auto dm = td::divmod(stack.pop_int(), std::move(y), round_mode); + stack.push_int(std::move(dm.first)); + stack.push_int(std::move(dm.second)); +} + +void interpret_times_div(vm::Stack& stack, int round_mode) { + auto z = stack.pop_int(); + auto y = stack.pop_int(); + auto x = stack.pop_int(); + td::BigIntG<257 * 2> tmp{0}; + tmp.add_mul(*x, *y); + auto q = td::RefInt256{true}; + tmp.mod_div(*z, q.unique_write(), round_mode); + q.unique_write().normalize(); + stack.push_int(std::move(q)); +} + +void interpret_times_divmod(vm::Stack& stack, int round_mode) { + auto z = stack.pop_int(); + auto y = stack.pop_int(); + auto x = stack.pop_int(); + td::BigIntG<257 * 2> tmp{0}; + tmp.add_mul(*x, *y); + auto q = td::RefInt256{true}; + tmp.mod_div(*z, q.unique_write(), round_mode); + q.unique_write().normalize(); + auto r = td::RefInt256{true, tmp}; + stack.push_int(std::move(q)); + stack.push_int(std::move(r)); +} + +void interpret_times_mod(vm::Stack& stack, int round_mode) { + auto z = stack.pop_int(); + auto y = stack.pop_int(); + auto x = stack.pop_int(); + td::BigIntG<257 * 2> tmp{0}; + tmp.add_mul(*x, *y); + td::BigIntG<257 * 2> q; + tmp.mod_div(*z, q, round_mode); + auto r = td::RefInt256{true, tmp}; + stack.push_int(std::move(r)); +} + +void interpret_negate(vm::Stack& stack) { + stack.push_int(-stack.pop_int()); +} + +void interpret_const(vm::Stack& stack, long long val) { + stack.push_smallint(val); +} + +void interpret_big_const(vm::Stack& stack, td::RefInt256 val) { + stack.push_int(std::move(val)); +} + +void interpret_literal(vm::Stack& stack, vm::StackEntry se) { + stack.push(std::move(se)); +} + +void interpret_cmp(vm::Stack& stack, const char opt[3]) { + auto y = stack.pop_int(); + auto x = stack.pop_int(); + int r = x->cmp(*y); + assert((unsigned)(r + 1) <= 2); + stack.push_smallint(((const signed char*)opt)[r + 1]); +} + +void interpret_sgn(vm::Stack& stack, const char opt[3]) { + auto x = stack.pop_int(); + int r = x->sgn(); + assert((unsigned)(r + 1) <= 2); + stack.push_smallint(((const signed char*)opt)[r + 1]); +} + +void interpret_fits(vm::Stack& stack, bool sgnd) { + int n = stack.pop_smallint_range(1023); + auto x = stack.pop_int(); + stack.push_bool(x->fits_bits(n, sgnd)); +} + +void interpret_pow2(vm::Stack& stack) { + int x = stack.pop_smallint_range(255); + auto r = td::RefInt256{true}; + r.unique_write().set_pow2(x); + stack.push_int(r); +} + +void interpret_neg_pow2(vm::Stack& stack) { + int x = stack.pop_smallint_range(256); + auto r = td::RefInt256{true}; + r.unique_write().set_pow2(x).negate().normalize(); + stack.push_int(r); +} + +void interpret_pow2_minus1(vm::Stack& stack) { + int x = stack.pop_smallint_range(256); + auto r = td::RefInt256{true}; + r.unique_write().set_pow2(x).add_tiny(-1).normalize(); + stack.push_int(r); +} + +void interpret_mod_pow2(vm::Stack& stack) { + int y = stack.pop_smallint_range(256); + auto x = stack.pop_int(); + x.write().mod_pow2(y).normalize(); + stack.push_int(x); +} + +void interpret_lshift(vm::Stack& stack) { + int y = stack.pop_smallint_range(256); + stack.push_int(stack.pop_int() << y); +} + +void interpret_rshift(vm::Stack& stack, int round_mode) { + int y = stack.pop_smallint_range(256); + stack.push_int(rshift(stack.pop_int(), y, round_mode)); +} + +void interpret_lshift_const(vm::Stack& stack, int y) { + stack.push_int(stack.pop_int() << y); +} + +void interpret_rshift_const(vm::Stack& stack, int y) { + stack.push_int(stack.pop_int() >> y); +} + +void interpret_times_rshift(vm::Stack& stack, int round_mode) { + int z = stack.pop_smallint_range(256); + auto y = stack.pop_int(); + auto x = stack.pop_int(); + td::BigIntG<257 * 2> tmp{0}; + tmp.add_mul(*x, *y).rshift(z, round_mode).normalize(); + auto q = td::RefInt256{true, tmp}; + stack.push_int(std::move(q)); +} + +void interpret_lshift_div(vm::Stack& stack, int round_mode) { + int z = stack.pop_smallint_range(256); + auto y = stack.pop_int(); + auto x = stack.pop_int(); + td::BigIntG<257 * 2> tmp{*x}; + tmp <<= z; + auto q = td::RefInt256{true}; + tmp.mod_div(*y, q.unique_write(), round_mode); + q.unique_write().normalize(); + stack.push_int(std::move(q)); +} + +void interpret_not(vm::Stack& stack) { + stack.push_int(~stack.pop_int()); +} + +void interpret_and(vm::Stack& stack) { + stack.push_int(stack.pop_int() & stack.pop_int()); +} + +void interpret_or(vm::Stack& stack) { + stack.push_int(stack.pop_int() | stack.pop_int()); +} + +void interpret_xor(vm::Stack& stack) { + stack.push_int(stack.pop_int() ^ stack.pop_int()); +} + +void interpret_has_type(vm::Stack& stack, int t) { + stack.push_bool(stack.pop_chk().type() == t); +} + +void interpret_drop(vm::Stack& stack) { + stack.check_underflow(1); + stack.pop(); +} + +void interpret_2drop(vm::Stack& stack) { + stack.check_underflow(2); + stack.pop(); + stack.pop(); +} + +void interpret_dup(vm::Stack& stack) { + stack.check_underflow(1); + stack.push(stack.fetch(0)); +} + +void interpret_2dup(vm::Stack& stack) { + stack.check_underflow(2); + stack.push(stack.fetch(1)); + stack.push(stack.fetch(1)); +} + +void interpret_over(vm::Stack& stack) { + stack.check_underflow(2); + stack.push(stack.fetch(1)); +} + +void interpret_2over(vm::Stack& stack) { + stack.check_underflow(4); + stack.push(stack.fetch(3)); + stack.push(stack.fetch(3)); +} + +void interpret_swap(vm::Stack& stack) { + stack.check_underflow(2); + swap(stack[0], stack[1]); +} + +void interpret_2swap(vm::Stack& stack) { + stack.check_underflow(4); + swap(stack[0], stack[2]); + swap(stack[1], stack[3]); +} + +void interpret_tuck(vm::Stack& stack) { + stack.check_underflow(2); + swap(stack[0], stack[1]); + stack.push(stack.fetch(1)); +} + +void interpret_nip(vm::Stack& stack) { + stack.check_underflow(2); + stack.pop(stack[1]); +} + +void interpret_rot(vm::Stack& stack) { + stack.check_underflow(3); + swap(stack[1], stack[2]); + swap(stack[0], stack[1]); +} + +void interpret_rot_rev(vm::Stack& stack) { + stack.check_underflow(3); + swap(stack[0], stack[1]); + swap(stack[1], stack[2]); +} + +void interpret_pick(vm::Stack& stack) { + int n = stack.pop_smallint_range(255); + stack.check_underflow(n + 1); + stack.push(stack.fetch(n)); +} + +void interpret_roll(vm::Stack& stack) { + int n = stack.pop_smallint_range(255); + stack.check_underflow(n + 1); + for (int i = n; i > 0; i--) { + swap(stack[i], stack[i - 1]); + } +} + +void interpret_roll_rev(vm::Stack& stack) { + int n = stack.pop_smallint_range(255); + stack.check_underflow(n + 1); + for (int i = 0; i < n; i++) { + swap(stack[i], stack[i + 1]); + } +} + +void interpret_reverse(vm::Stack& stack) { + int m = stack.pop_smallint_range(255); + int n = stack.pop_smallint_range(255); + stack.check_underflow(n + m); + int s = 2 * m + n - 1; + for (int i = ((s - 1) >> 1); i >= m; i--) { + swap(stack[i], stack[s - i]); + } +} + +void interpret_exch(vm::Stack& stack) { + int n = stack.pop_smallint_range(255); + stack.check_underflow(n + 1); + swap(stack[0], stack[n]); +} + +void interpret_exch2(vm::Stack& stack) { + int n = stack.pop_smallint_range(255); + int m = stack.pop_smallint_range(255); + stack.check_underflow(std::max(m, n) + 1); + swap(stack[n], stack[m]); +} + +void interpret_depth(vm::Stack& stack) { + stack.push_smallint(stack.depth()); +} + +void interpret_xchg0(vm::Stack& stack, int x) { + stack.check_underflow_p(x); + std::swap(stack.tos(), stack.at(x)); +} + +void interpret_xchg(vm::Stack& stack, int x, int y) { + stack.check_underflow_p(x, y); + std::swap(stack.at(x), stack.at(y)); +} + +void interpret_push(vm::Stack& stack, int x) { + stack.check_underflow_p(x); + stack.push(stack.fetch(x)); +} + +void interpret_pop(vm::Stack& stack, int x) { + stack.check_underflow_p(x); + std::swap(stack.tos(), stack.at(x)); + stack.pop(); +} + +Ref dup_word_def{true, interpret_dup}, over_word_def{true, interpret_over}, + drop_word_def{true, interpret_drop}, nip_word_def{true, interpret_nip}, swap_word_def{true, interpret_swap}; + +void interpret_make_xchg(vm::Stack& stack) { + using namespace std::placeholders; + int y = stack.pop_smallint_range(255), x = stack.pop_smallint_range(255); + if (x > y) { + std::swap(x, y); + } + if (x) { + stack.push_object(td::Ref{true, std::bind(interpret_xchg, _1, x, y)}); + } else if (y <= 1) { + stack.push_object(y ? swap_word_def : Dictionary::nop_word_def); + } else { + stack.push_object(td::Ref{true, std::bind(interpret_xchg0, _1, y)}); + } +} + +void interpret_make_push(vm::Stack& stack) { + int x = stack.pop_smallint_range(255); + if (x <= 1) { + stack.push_object(x ? over_word_def : dup_word_def); + } else { + stack.push_object(td::Ref{true, std::bind(interpret_push, std::placeholders::_1, x)}); + } +} + +void interpret_make_pop(vm::Stack& stack) { + int x = stack.pop_smallint_range(255); + if (x <= 1) { + stack.push_object(x ? nip_word_def : drop_word_def); + } else { + stack.push_object(td::Ref{true, std::bind(interpret_pop, std::placeholders::_1, x)}); + } +} + +void interpret_is_string(vm::Stack& stack) { + stack.push_bool(stack.pop().type() == vm::StackEntry::t_string); +} + +int make_utf8_char(char buffer[4], int x) { + if (x < -0x80) { + return 0; + } else if (x < 0x80) { + buffer[0] = (char)x; + return 1; + } else if (x < 0x800) { + buffer[0] = (char)(0xc0 + (x >> 6)); + buffer[1] = (char)(0x80 + (x & 0x3f)); + return 2; + } else if (x < 0x10000) { + buffer[0] = (char)(0xe0 + (x >> 12)); + buffer[1] = (char)(0x80 + ((x >> 6) & 0x3f)); + buffer[2] = (char)(0x80 + (x & 0x3f)); + return 3; + } else if (x < 0x200000) { + buffer[0] = (char)(0xf0 + (x >> 18)); + buffer[1] = (char)(0x80 + ((x >> 12) & 0x3f)); + buffer[2] = (char)(0x80 + ((x >> 6) & 0x3f)); + buffer[3] = (char)(0x80 + (x & 0x3f)); + return 4; + } else { + return 0; + } +} + +void interpret_chr(vm::Stack& stack) { + char buffer[8]; + unsigned len = make_utf8_char(buffer, stack.pop_smallint_range(0x10ffff, -128)); + stack.push_string(std::string{buffer, len}); +} + +void interpret_hold(vm::Stack& stack) { + stack.check_underflow(2); + char buffer[8]; + unsigned len = make_utf8_char(buffer, stack.pop_smallint_range(0x10ffff, -128)); + std::string s = stack.pop_string() + std::string{buffer, len}; + stack.push_string(std::move(s)); +} + +void interpret_emit(IntCtx& ctx) { + char buffer[8]; + buffer[make_utf8_char(buffer, ctx.stack.pop_smallint_range(0x10ffff, -128))] = 0; + *ctx.output_stream << buffer; +} + +void interpret_emit_const(IntCtx& ctx, char c) { + *ctx.output_stream << c; +} + +void interpret_type(IntCtx& ctx) { + std::string s = ctx.stack.pop_string(); + *ctx.output_stream << s; +} + +void interpret_str_concat(vm::Stack& stack) { + std::string t = stack.pop_string(); + stack.push_string(stack.pop_string() + t); +} + +void interpret_str_equal(vm::Stack& stack) { + stack.check_underflow(2); + std::string t = stack.pop_string(), s = stack.pop_string(); + stack.push_bool(s == t); +} + +void interpret_str_cmp(vm::Stack& stack) { + stack.check_underflow(2); + std::string t = stack.pop_string(), s = stack.pop_string(); + int res = s.compare(std::move(t)); + stack.push_smallint((res > 0) - (res < 0)); +} + +void interpret_str_len(vm::Stack& stack) { + stack.push_smallint((long long)stack.pop_string().size()); +} + +void interpret_str_split(vm::Stack& stack) { + stack.check_underflow(2); + unsigned sz = stack.pop_smallint_range(0x7fffffff); + std::string str = stack.pop_string(); + if (sz > str.size()) { + throw IntError{"not enough bytes for cutting"}; + } + stack.push_string(std::string{str, 0, sz}); + stack.push_string(std::string{str, sz}); +} + +void interpret_str_reverse(vm::Stack& stack) { + std::string s = stack.pop_string(); + auto it = s.begin(); + while (it < s.end()) { + if ((*it & 0xc0) != 0xc0) { + ++it; + } else { + auto it0 = it++; + while (it < s.end() && (*it & 0xc0) == 0x80) { + ++it; + } + std::reverse(it0, it); + } + } + std::reverse(s.begin(), s.end()); + stack.push_string(std::move(s)); +} + +void interpret_str_remove_trailing_int(vm::Stack& stack, int arg) { + char x = (char)(arg ? arg : stack.pop_long_range(127)); + std::string s = stack.pop_string(); + s.resize(s.find_last_not_of(x) + 1); // if not found, this expression will be 0 + stack.push_string(std::move(s)); +} + +void interpret_bytes_len(vm::Stack& stack) { + stack.push_smallint((long long)stack.pop_bytes().size()); +} + +void interpret_bytes_hex_print_raw(IntCtx& ctx) { + const char hex_digits[] = "0123456789ABCDEF"; + std::string str = ctx.stack.pop_bytes(); + for (unsigned c : str) { + *ctx.output_stream << hex_digits[(c >> 4) & 15] << hex_digits[c & 15]; + } +} + +void interpret_bytes_split(vm::Stack& stack) { + stack.check_underflow(2); + unsigned sz = stack.pop_smallint_range(0x7fffffff); + std::string str = stack.pop_bytes(); + if (sz > str.size()) { + throw IntError{"not enough bytes for cutting"}; + } + stack.push_bytes(std::string{str, 0, sz}); + stack.push_bytes(std::string{str, sz}); +} + +void interpret_bytes_concat(vm::Stack& stack) { + std::string t = stack.pop_bytes(); + stack.push_bytes(stack.pop_bytes() + t); +} + +void interpret_bytes_equal(vm::Stack& stack) { + stack.check_underflow(2); + std::string t = stack.pop_bytes(), s = stack.pop_bytes(); + stack.push_bool(s == t); +} + +void interpret_bytes_cmp(vm::Stack& stack) { + stack.check_underflow(2); + std::string t = stack.pop_bytes(), s = stack.pop_bytes(); + int res = s.compare(std::move(t)); + stack.push_smallint((res > 0) - (res < 0)); +} + +void interpret_bytes_fetch_int(vm::Stack& stack, int mode) { + stack.check_underflow(2); + unsigned bits = (unsigned)stack.pop_smallint_range(256 + (mode & 1)); + std::string str = stack.pop_bytes(); + if ((bits & 7)) { + throw IntError{"can load only an integer number of bytes"}; + } + unsigned sz = bits >> 3; + if (str.size() < sz) { + throw IntError{"not enough bytes in the source"}; + } + td::RefInt256 x{true}; + bool ok; + const unsigned char* ptr = (const unsigned char*)(str.data()); + if (!(mode & 0x10)) { + ok = x.write().import_bytes(ptr, sz, mode & 1); + } else { + ok = x.write().import_bytes_lsb(ptr, sz, mode & 1); + } + if (!ok) { + throw IntError{"cannot load integer"}; + } + if (mode & 2) { + stack.push_bytes(std::string{str, sz}); + } + stack.push_int(std::move(x)); +} + +void interpret_int_to_bytes(vm::Stack& stack, bool sgnd, bool lsb) { + stack.check_underflow(2); + unsigned bits = (unsigned)stack.pop_smallint_range(sgnd ? 264 : 256, 1); + td::RefInt256 x = stack.pop_int(); + if ((bits & 7)) { + throw IntError{"can store only an integer number of bytes"}; + } + unsigned sz = bits >> 3; + unsigned char buffer[33]; + if (!(lsb ? x->export_bytes_lsb(buffer, sz, sgnd) : x->export_bytes(buffer, sz, sgnd))) { + throw IntError{"cannot store integer"}; + } + stack.push_bytes(std::string{(char*)buffer, sz}); +} + +void interpret_bytes_hash(vm::Stack& stack) { + std::string str = stack.pop_bytes(); + unsigned char buffer[32]; + digest::hash_str(buffer, str.c_str(), str.size()); + td::RefInt256 x{true}; + x.write().import_bytes(buffer, 32, false); + stack.push_int(std::move(x)); +} + +void interpret_empty(vm::Stack& stack) { + stack.push(td::Ref{true}); +} + +void interpret_store(vm::Stack& stack, bool sgnd) { + stack.check_underflow(3); + int bits = stack.pop_smallint_range(1023); + auto x = stack.pop_int(); + auto cell = stack.pop_builder(); + if (!cell.write().store_int256_bool(*x, bits, sgnd)) { + throw IntError{"integer does not fit into cell"}; + } + stack.push(cell); +} + +void interpret_store_str(vm::Stack& stack) { + stack.check_underflow(2); + auto str = stack.pop_string(); + auto cell = stack.pop_builder(); + cell.write().store_bytes(str); // may throw CellWriteError + stack.push(cell); +} + +void interpret_store_bytes(vm::Stack& stack) { + stack.check_underflow(2); + auto str = stack.pop_bytes(); + auto cell = stack.pop_builder(); + cell.write().store_bytes(str); // may throw CellWriteError + stack.push(cell); +} + +void interpret_string_to_cellslice(vm::Stack& stack) { + auto str = stack.pop_string(); + vm::CellBuilder cb; + cb.store_bytes(str); // may throw CellWriteError + stack.push_cellslice(td::Ref{true, cb.finalize()}); +} + +void interpret_store_cellslice(vm::Stack& stack) { + stack.check_underflow(2); + auto cs = stack.pop_cellslice(); + auto cb = stack.pop_builder(); + vm::cell_builder_add_slice(cb.write(), *cs); + stack.push(std::move(cb)); +} + +void interpret_store_cellslice_ref(vm::Stack& stack) { + stack.check_underflow(2); + auto cs = stack.pop_cellslice(); + vm::CellBuilder cs_cell_builder; + vm::cell_builder_add_slice(cs_cell_builder, *cs); + auto cb = stack.pop_builder(); + if (!cb.write().store_ref_bool(cs_cell_builder.finalize())) { + throw IntError{"cell reference list overflow"}; + } + stack.push(std::move(cb)); +} + +void interpret_concat_cellslice(vm::Stack& stack) { + stack.check_underflow(2); + auto cs2 = stack.pop_cellslice(); + auto cs1 = stack.pop_cellslice(); + vm::CellBuilder cb; + vm::cell_builder_add_slice(cb, *cs1); + vm::cell_builder_add_slice(cb, *cs2); + stack.push_cellslice(td::Ref{true, cb.finalize()}); +} + +void interpret_concat_cellslice_ref(vm::Stack& stack) { + stack.check_underflow(2); + auto cs2 = stack.pop_cellslice(); + auto cs1 = stack.pop_cellslice(); + vm::CellBuilder builder1, builder2; + vm::cell_builder_add_slice(builder1, *cs1); + vm::cell_builder_add_slice(builder2, *cs2); + if (!builder1.store_ref_bool(builder2.finalize())) { + throw IntError{"cell reference list overflow"}; + } + stack.push_cellslice(td::Ref{true, builder1.finalize()}); +} + +void interpret_concat_builders(vm::Stack& stack) { + stack.check_underflow(2); + auto cb2 = stack.pop_builder(); + auto cb1 = stack.pop_builder(); + cb1.write().append_builder(std::move(cb2)); + stack.push_builder(std::move(cb1)); +} + +void interpret_slice_bitrefs(vm::Stack& stack, int mode) { + auto cs = stack.pop_cellslice(); + if (mode & 1) { + stack.push_smallint(cs->size()); + } + if (mode & 2) { + stack.push_smallint(cs->size_refs()); + } +} + +void interpret_builder_bitrefs(vm::Stack& stack, int mode) { + auto cb = stack.pop_builder(); + if (mode & 1) { + stack.push_smallint(cb->size()); + } + if (mode & 2) { + stack.push_smallint(cb->size_refs()); + } +} + +void interpret_builder_remaining_bitrefs(vm::Stack& stack, int mode) { + auto cb = stack.pop_builder(); + if (mode & 1) { + stack.push_smallint(cb->remaining_bits()); + } + if (mode & 2) { + stack.push_smallint(cb->remaining_refs()); + } +} + +void interpret_cell_hash(vm::Stack& stack) { + auto cell = stack.pop_cell(); + td::RefInt256 hash{true}; + hash.write().import_bytes(cell->get_hash().as_slice().ubegin(), 32, false); + stack.push_int(std::move(hash)); +} + +void interpret_store_ref(vm::Stack& stack) { + auto ref = stack.pop_cell(); + auto cb = stack.pop_builder(); + if (!cb.write().store_ref_bool(ref)) { + throw IntError{"cell reference list overflow"}; + } + stack.push(std::move(cb)); +} + +void interpret_store_end(vm::Stack& stack, bool special) { + auto cell = stack.pop_builder()->finalize_copy(special); + if (cell.is_null()) { + throw IntError{"invalid special cell constructed"}; + } + stack.push_cell(std::move(cell)); +} + +void interpret_from_cell(vm::Stack& stack) { + auto cell = stack.pop_cell(); + Ref cs{true}; + if (!cs.unique_write().load(vm::NoVmOrd(), std::move(cell))) { + throw IntError{"deserializing a special cell as ordinary"}; + } + stack.push(cs); +} + +// cs n -- cs' x +// cs n -- cs' x -1 OR cs' 0 +// mode & 1 : signed +// mode & 2 : advance position +// mode & 4 : return error on stack +void interpret_fetch(vm::Stack& stack, int mode) { + auto n = stack.pop_smallint_range(256 + (mode & 1)); + auto cs = stack.pop_cellslice(); + if (!cs->have(n)) { + stack.push(std::move(cs)); + stack.push_bool(false); + if (!(mode & 4)) { + throw IntError{"end of data while reading integer from cell"}; + } + } else { + if (mode & 2) { + stack.push_int(cs.write().fetch_int256(n, mode & 1)); + stack.push(std::move(cs)); + } else { + stack.push_int(cs->prefetch_int256(n, mode & 1)); + } + if (mode & 4) { + stack.push_bool(true); + } + } +} + +// mode & 1 : return result as bytes (instead of string) +// mode & 2 : advance position +// mode & 4 : return error on stack +void interpret_fetch_bytes(vm::Stack& stack, int mode) { + unsigned n = stack.pop_smallint_range(127); + auto cs = stack.pop_cellslice(); + if (!cs->have(n * 8)) { + stack.push(std::move(cs)); + stack.push_bool(false); + if (!(mode & 4)) { + throw IntError{"end of data while reading byte string from cell"}; + } + } else { + // unfortunately, std::string's data() is writeable only in C++17 + unsigned char tmp[128]; + if (mode & 2) { + cs.write().fetch_bytes(tmp, n); + } else { + cs.write().prefetch_bytes(tmp, n); + } + std::string s{tmp, tmp + n}; + if (mode & 1) { + stack.push_bytes(std::move(s)); + } else { + stack.push_string(std::move(s)); + } + stack.push(std::move(cs)); + if (mode & 4) { + stack.push_bool(true); + } + } +} + +void interpret_cell_empty(vm::Stack& stack) { + auto cs = stack.pop_cellslice(); + stack.push_bool(cs->empty_ext()); +} + +void interpret_cell_check_empty(vm::Stack& stack) { + auto cs = stack.pop_cellslice(); + if (!cs->empty_ext()) { + throw IntError{"cell slice not empty"}; + } +} + +void interpret_cell_remaining(vm::Stack& stack) { + auto cs = stack.pop_cellslice(); + stack.push_smallint(cs->size()); + stack.push_smallint(cs->size_refs()); +} + +// mode & 1 : return result as slice (instead of cell) +// mode & 2 : advance position +// mode & 4 : return error on stack +void interpret_fetch_ref(vm::Stack& stack, int mode) { + auto cs = stack.pop_cellslice(); + if (!cs->have_refs(1)) { + stack.push(std::move(cs)); + stack.push_bool(false); + if (!(mode & 4)) { + throw IntError{"end of data while reading reference from cell"}; + } + } else { + auto cell = (mode & 2) ? cs.write().fetch_ref() : cs.write().prefetch_ref(); + if (mode & 2) { + stack.push(std::move(cs)); + } + if (mode & 1) { + Ref new_cs{true, vm::NoVm(), std::move(cell)}; + stack.push(std::move(new_cs)); + } else { + stack.push_cell(std::move(cell)); + } + if (mode & 4) { + stack.push_bool(true); + } + } +} + +// Box create/fetch/store operations + +void interpret_hole(vm::Stack& stack) { + stack.push_box(Ref{true}); +} + +void interpret_box(vm::Stack& stack) { + stack.push_box(Ref{true, stack.pop_chk()}); +} + +void interpret_box_fetch(vm::Stack& stack) { + stack.push(stack.pop_box()->get()); +} + +void interpret_box_store(vm::Stack& stack) { + stack.check_underflow(2); + auto box = stack.pop_box(); + box->set(stack.pop()); +} + +void interpret_push_null(vm::Stack& stack) { + stack.push({}); +} + +void interpret_is_null(vm::Stack& stack) { + stack.push_bool(stack.pop_chk().empty()); +} + +// Tuple/array operations + +void interpret_empty_tuple(vm::Stack& stack) { + stack.push_tuple(Ref{true}); +} + +void interpret_is_tuple(vm::Stack& stack) { + stack.push_bool(stack.pop_chk().type() == vm::StackEntry::t_tuple); +} + +void interpret_tuple_push(vm::Stack& stack) { + stack.check_underflow(2); + auto val = stack.pop(); + auto tuple = stack.pop_tuple(); + tuple.write().emplace_back(std::move(val)); + stack.push_tuple(std::move(tuple)); +} + +void interpret_tuple_pop(vm::Stack& stack) { + auto tuple = stack.pop_tuple(); + if (tuple->empty()) { + throw IntError{"empty tuple"}; + } + auto val = tuple->back(); + tuple.write().pop_back(); + stack.push_tuple(std::move(tuple)); + stack.push(std::move(val)); +} + +void interpret_tuple_len(vm::Stack& stack) { + stack.push_smallint(stack.pop_tuple()->size()); +} + +void interpret_tuple_index(vm::Stack& stack) { + auto idx = stack.pop_long_range(std::numeric_limits::max()); + auto tuple = stack.pop_tuple(); + if ((td::uint64)idx >= tuple->size()) { + throw vm::VmError{vm::Excno::range_chk, "array index out of range"}; + } + stack.push((*tuple)[idx]); +} + +void interpret_tuple_set(vm::Stack& stack) { + auto idx = stack.pop_long_range(std::numeric_limits::max()); + auto val = stack.pop_chk(); + auto tuple = stack.pop_tuple(); + if ((td::uint64)idx >= tuple->size()) { + throw vm::VmError{vm::Excno::range_chk, "array index out of range"}; + } + tuple.write()[idx] = std::move(val); + stack.push_tuple(std::move(tuple)); +} + +void interpret_make_tuple(vm::Stack& stack) { + int n = stack.pop_smallint_range(255); + stack.check_underflow(n); + Ref ref{true}; + auto& tuple = ref.unique_write(); + tuple.reserve(n); + for (int i = n - 1; i >= 0; i--) { + tuple.push_back(std::move(stack[i])); + } + stack.pop_many(n); + stack.push_tuple(std::move(ref)); +} + +void interpret_tuple_explode(vm::Stack& stack, bool pop_count) { + std::size_t n = pop_count ? (unsigned)stack.pop_smallint_range(255) : 0; + auto ref = stack.pop_tuple(); + const auto& tuple = *ref; + if (!pop_count) { + n = tuple.size(); + if (n > 255) { + throw IntError{"tuple too large to be exploded"}; + } + } else if (tuple.size() != n) { + throw IntError{"tuple size mismatch"}; + } + if (ref.is_unique()) { + auto& tuplew = ref.unique_write(); + for (auto& entry : tuplew) { + stack.push(std::move(entry)); + } + } else { + for (const auto& entry : tuple) { + stack.push(entry); + } + } + if (!pop_count) { + stack.push_smallint((td::int32)n); + } +} + +void interpret_allot(vm::Stack& stack) { + auto n = stack.pop_long_range(0xffffffff); + Ref ref{true}; + auto& tuple = ref.unique_write(); + tuple.reserve(n); + while (n-- > 0) { + tuple.emplace_back(Ref{true}); + } + stack.push(std::move(ref)); +} + +// Atoms + +void interpret_atom(vm::Stack& stack) { + bool create = stack.pop_bool(); + auto atom = vm::Atom::find(stack.pop_string(), create); + if (atom.is_null()) { + stack.push_bool(false); + } else { + stack.push_atom(std::move(atom)); + stack.push_bool(true); + } +} + +void interpret_atom_name(vm::Stack& stack) { + stack.push_string(stack.pop_atom()->name_ext()); +} + +void interpret_atom_anon(vm::Stack& stack) { + stack.push_atom(vm::Atom::anon()); +} + +void interpret_is_atom(vm::Stack& stack) { + stack.push_bool(stack.pop().is_atom()); +} + +bool are_eqv(vm::StackEntry x, vm::StackEntry y) { + if (x.type() != y.type()) { + return false; + } + switch (x.type()) { + case vm::StackEntry::t_null: + return true; + case vm::StackEntry::t_atom: + return std::move(x).as_atom() == std::move(y).as_atom(); + case vm::StackEntry::t_int: + return !td::cmp(std::move(x).as_int(), std::move(y).as_int()); + case vm::StackEntry::t_string: + return std::move(x).as_string() == std::move(y).as_string(); + default: + return false; + } +} + +void interpret_is_eqv(vm::Stack& stack) { + auto y = stack.pop(), x = stack.pop(); + stack.push_bool(are_eqv(std::move(x), std::move(y))); +} + +void interpret_is_eq(vm::Stack& stack) { + auto y = stack.pop(), x = stack.pop(); + stack.push_bool(x == y); +} + +// BoC (de)serialization + +void interpret_boc_serialize(vm::Stack& stack) { + vm::BagOfCells boc; + boc.add_root(stack.pop_cell()); + auto res = boc.import_cells(); + if (res.is_error()) { + throw IntError{(PSLICE() << "cannot serialize bag-of-cells " << res.error()).c_str()}; + } + stack.push_bytes(boc.serialize_to_string()); +} + +void interpret_boc_serialize_ext(vm::Stack& stack) { + int mode = stack.pop_smallint_range(vm::BagOfCells::Mode::max); + vm::BagOfCells boc; + boc.add_root(stack.pop_cell()); + auto res = boc.import_cells(); + if (res.is_error()) { + throw IntError{(PSLICE() << "cannot serialize bag-of-cells " << res.error()).c_str()}; + } + stack.push_bytes(boc.serialize_to_string(mode)); +} + +void interpret_boc_deserialize(vm::Stack& stack) { + std::string bytes = stack.pop_bytes(); + vm::BagOfCells boc; + auto res = boc.deserialize(td::Slice{bytes}); + if (res.is_error()) { + throw IntError{(PSLICE() << "cannot deserialize bag-of-cells " << res.error()).c_str()}; + } + if (res.ok() <= 0 || boc.get_root_cell().is_null()) { + throw IntError{"cannot deserialize bag-of-cells "}; + } + stack.push_cell(boc.get_root_cell()); +} + +void interpret_read_file(IntCtx& ctx) { + std::string filename = ctx.stack.pop_string(); + auto r_data = ctx.source_lookup->read_file(filename); + if (r_data.is_error()) { + throw IntError{PSTRING() << "error reading file `" << filename << "`: " << r_data.error()}; + } + ctx.stack.push_bytes(r_data.move_as_ok().data); +} + +void interpret_read_file_part(IntCtx& ctx) { + auto size = ctx.stack.pop_long_range(std::numeric_limits::max()); + auto offset = ctx.stack.pop_long_range(std::numeric_limits::max()); + std::string filename = ctx.stack.pop_string(); + auto r_data = ctx.source_lookup->read_file_part(filename, size, offset); + if (r_data.is_error()) { + throw IntError{PSTRING() << "error reading file `" << filename << "`: " << r_data.error()}; + } + ctx.stack.push_bytes(r_data.move_as_ok().data); +} + +void interpret_write_file(IntCtx& ctx) { + std::string filename = ctx.stack.pop_string(); + std::string str = ctx.stack.pop_bytes(); + auto status = ctx.source_lookup->write_file(filename, str); + if (status.is_error()) { + throw IntError{PSTRING() << "error writing file `" << filename << "`: " << status.error()}; + } +} + +void interpret_file_exists(IntCtx& ctx) { + std::string filename = ctx.stack.pop_string(); + auto res = ctx.source_lookup->is_file_exists(filename); + ctx.stack.push_bool(res); +} + +// custom and crypto + +void interpret_now(vm::Stack& stack) { + stack.push_smallint(std::time(nullptr)); +} + +void interpret_new_keypair(vm::Stack& stack) { + auto priv_key = td::Ed25519::generate_private_key(); + if (!priv_key.is_ok()) { + throw fift::IntError{priv_key.error().to_string()}; + } + auto pub_key = priv_key.ok().get_public_key(); + if (!pub_key.is_ok()) { + throw fift::IntError{pub_key.error().to_string()}; + } + stack.push_bytes(priv_key.ok().as_octet_string()); + stack.push_bytes(pub_key.ok().as_octet_string()); +} + +void interpret_priv_key_to_pub(vm::Stack& stack) { + std::string str = stack.pop_bytes(); + if (str.size() != 32) { + throw IntError{"Ed25519 private key must be exactly 32 bytes long"}; + } + td::Ed25519::PrivateKey priv_key{td::SecureString{str}}; + auto pub_key = priv_key.get_public_key(); + if (!pub_key.is_ok()) { + throw fift::IntError{pub_key.error().to_string()}; + } + stack.push_bytes(pub_key.ok().as_octet_string()); +} + +void interpret_ed25519_sign(vm::Stack& stack) { + stack.check_underflow(2); + std::string key = stack.pop_bytes(), data = stack.pop_bytes(); + if (key.size() != 32) { + throw IntError{"Ed25519 private key must be exactly 32 bytes long"}; + } + td::Ed25519::PrivateKey priv_key{td::SecureString{key}}; + auto signature = priv_key.sign(td::Slice{data}); + if (!signature.is_ok()) { + throw fift::IntError{signature.error().to_string()}; + } + stack.push_bytes(signature.move_as_ok()); +} + +void interpret_ed25519_sign_uint(vm::Stack& stack) { + stack.check_underflow(2); + std::string key = stack.pop_bytes(); + td::RefInt256 data_int = stack.pop_int(); + if (key.size() != 32) { + throw IntError{"Ed25519 private key must be exactly 32 bytes long"}; + } + unsigned char data[32]; + if (!data_int->export_bytes(data, 32, false)) { + throw IntError{"Ed25519 data to be signed must fit into 256 bits"}; + } + td::Ed25519::PrivateKey priv_key{td::SecureString{key}}; + auto signature = priv_key.sign(td::Slice{data, 32}); + if (!signature.is_ok()) { + throw fift::IntError{signature.error().to_string()}; + } + stack.push_bytes(signature.move_as_ok()); +} + +void interpret_ed25519_chksign(vm::Stack& stack) { + stack.check_underflow(3); + std::string key = stack.pop_bytes(), signature = stack.pop_bytes(), data = stack.pop_bytes(); + if (key.size() != 32) { + throw IntError{"Ed25519 public key must be exactly 32 bytes long"}; + } + if (signature.size() != 64) { + throw IntError{"Ed25519 signature must be exactly 64 bytes long"}; + } + td::Ed25519::PublicKey pub_key{td::SecureString{key}}; + auto res = pub_key.verify_signature(td::Slice{data}, td::Slice{signature}); + stack.push_bool(res.is_ok()); +} + +// vm dictionaries +void interpret_dict_new(vm::Stack& stack) { + stack.push({}); +} + +void interpret_dict_to_slice(vm::Stack& stack) { + vm::CellBuilder cb; + cb.store_maybe_ref(stack.pop_maybe_cell()); + stack.push_cellslice(vm::load_cell_slice_ref(cb.finalize())); +} + +void interpret_load_dict(vm::Stack& stack, bool fetch) { + auto cs = stack.pop_cellslice(); + Ref dict; + bool non_empty; + if (!(cs.write().fetch_bool_to(non_empty) && (!non_empty || cs.write().fetch_ref_to(dict)))) { + throw IntError{"cell underflow"}; + } + stack.push_maybe_cell(std::move(dict)); + if (fetch) { + stack.push_cellslice(std::move(cs)); + } +} + +void interpret_store_dict(vm::Stack& stack) { + auto cell = stack.pop_maybe_cell(); + auto cb = stack.pop_builder(); + if (!cb.write().store_maybe_ref(std::move(cell))) { + throw IntError{"cell overflow"}; + } + stack.push_builder(std::move(cb)); +} + +// val key dict keylen -- dict' ? +void interpret_dict_add_u(vm::Stack& stack, vm::Dictionary::SetMode mode, bool add_builder, bool sgnd) { + int n = stack.pop_smallint_range(vm::Dictionary::max_key_bits); + vm::Dictionary dict{stack.pop_maybe_cell(), n}; + unsigned char buffer[vm::Dictionary::max_key_bytes]; + vm::BitSlice key = dict.integer_key(stack.pop_int(), n, sgnd, buffer); + if (!key.is_valid()) { + throw IntError{"not enough bits for a dictionary key"}; + } + bool res; + if (add_builder) { + res = dict.set_builder(std::move(key), stack.pop_builder(), mode); + } else { + res = dict.set(std::move(key), stack.pop_cellslice(), mode); + } + stack.push_maybe_cell(std::move(dict).extract_root_cell()); + stack.push_bool(res); +} + +void interpret_dict_get_u(vm::Stack& stack, bool sgnd) { + int n = stack.pop_smallint_range(vm::Dictionary::max_key_bits); + vm::Dictionary dict{stack.pop_maybe_cell(), n}; + unsigned char buffer[vm::Dictionary::max_key_bytes]; + vm::BitSlice key = dict.integer_key(stack.pop_int(), n, sgnd, buffer); + if (!key.is_valid()) { + throw IntError{"not enough bits for a dictionary key"}; + } + auto res = dict.lookup(std::move(key)); + if (res.not_null()) { + stack.push_cellslice(std::move(res)); + stack.push_bool(true); + } else { + stack.push_bool(false); + } +} + +void interpret_dict_map(IntCtx& ctx) { + auto func = pop_exec_token(ctx); + int n = ctx.stack.pop_smallint_range(vm::Dictionary::max_key_bits); + vm::Dictionary dict{ctx.stack.pop_maybe_cell(), n}; + vm::Dictionary::simple_map_func_t simple_map = [&ctx, func](vm::CellBuilder& cb, Ref cs_ref) -> bool { + ctx.stack.push_builder(Ref(cb)); + ctx.stack.push_cellslice(std::move(cs_ref)); + func->run(ctx); + assert(cb.is_unique()); + if (!ctx.stack.pop_bool()) { + return false; + } + Ref cb_ref = ctx.stack.pop_builder(); + cb = *cb_ref; + return true; + }; + dict.map(std::move(simple_map)); + ctx.stack.push_maybe_cell(std::move(dict).extract_root_cell()); +} + +void interpret_dict_map_ext(IntCtx& ctx) { + auto func = pop_exec_token(ctx); + int n = ctx.stack.pop_smallint_range(vm::Dictionary::max_key_bits); + vm::Dictionary dict{ctx.stack.pop_maybe_cell(), n}; + vm::Dictionary::map_func_t map_func = [&ctx, func](vm::CellBuilder& cb, Ref cs_ref, + td::ConstBitPtr key, int key_len) -> bool { + ctx.stack.push_builder(Ref(cb)); + td::RefInt256 x{true}; + x.unique_write().import_bits(key, key_len, false); + ctx.stack.push_int(std::move(x)); + ctx.stack.push_cellslice(std::move(cs_ref)); + func->run(ctx); + assert(cb.is_unique()); + if (!ctx.stack.pop_bool()) { + return false; + } + Ref cb_ref = ctx.stack.pop_builder(); + cb = *cb_ref; + return true; + }; + dict.map(std::move(map_func)); + ctx.stack.push_maybe_cell(std::move(dict).extract_root_cell()); +} + +void interpret_dict_foreach(IntCtx& ctx) { + auto func = pop_exec_token(ctx); + int n = ctx.stack.pop_smallint_range(vm::Dictionary::max_key_bits); + vm::Dictionary dict{ctx.stack.pop_maybe_cell(), n}; + vm::Dictionary::foreach_func_t foreach_func = [&ctx, func](Ref cs_ref, td::ConstBitPtr key, + int key_len) -> bool { + td::RefInt256 x{true}; + x.unique_write().import_bits(key, key_len, false); + ctx.stack.push_int(std::move(x)); + ctx.stack.push_cellslice(std::move(cs_ref)); + func->run(ctx); + return ctx.stack.pop_bool(); + }; + ctx.stack.push_bool(dict.check_for_each(std::move(foreach_func))); +} + +void interpret_dict_merge(IntCtx& ctx) { + auto func = pop_exec_token(ctx); + int n = ctx.stack.pop_smallint_range(vm::Dictionary::max_key_bits); + vm::Dictionary dict2{ctx.stack.pop_maybe_cell(), n}; + vm::Dictionary dict1{ctx.stack.pop_maybe_cell(), n}; + vm::Dictionary::simple_combine_func_t simple_combine = [&ctx, func](vm::CellBuilder& cb, Ref cs1_ref, + Ref cs2_ref) -> bool { + ctx.stack.push_builder(Ref(cb)); + ctx.stack.push_cellslice(std::move(cs1_ref)); + ctx.stack.push_cellslice(std::move(cs2_ref)); + func->run(ctx); + assert(cb.is_unique()); + if (!ctx.stack.pop_bool()) { + return false; + } + Ref cb_ref = ctx.stack.pop_builder(); + cb = *cb_ref; + return true; + }; + if (!dict1.combine_with(dict2, std::move(simple_combine))) { + throw IntError{"cannot combine dictionaries"}; + } + ctx.stack.push_maybe_cell(std::move(dict1).extract_root_cell()); +} + +void interpret_dict_diff(IntCtx& ctx) { + auto func = pop_exec_token(ctx); + int n = ctx.stack.pop_smallint_range(vm::Dictionary::max_key_bits); + vm::Dictionary dict2{ctx.stack.pop_maybe_cell(), n}; + vm::Dictionary dict1{ctx.stack.pop_maybe_cell(), n}; + vm::Dictionary::scan_diff_func_t scan_value_pair = + [&ctx, func](td::ConstBitPtr key, int key_len, Ref cs1_ref, Ref cs2_ref) -> bool { + td::RefInt256 x{true}; + x.unique_write().import_bits(key, key_len, false); + ctx.stack.push_int(std::move(x)); + ctx.stack.push_maybe_cellslice(std::move(cs1_ref)); + ctx.stack.push_maybe_cellslice(std::move(cs2_ref)); + func->run(ctx); + return ctx.stack.pop_bool(); + }; + ctx.stack.push_bool(dict1.scan_diff(dict2, std::move(scan_value_pair))); +} + +void interpret_pfx_dict_add(vm::Stack& stack, vm::Dictionary::SetMode mode, bool add_builder) { + int n = stack.pop_smallint_range(vm::Dictionary::max_key_bits); + vm::PrefixDictionary dict{stack.pop_maybe_cell(), n}; + auto cs = stack.pop_cellslice(); + bool res; + if (add_builder) { + res = dict.set_builder(cs->data_bits(), cs->size(), stack.pop_builder(), mode); + } else { + res = dict.set(cs->data_bits(), cs->size(), stack.pop_cellslice(), mode); + } + stack.push_maybe_cell(std::move(dict).extract_root_cell()); + stack.push_bool(res); +} + +void interpret_pfx_dict_get(vm::Stack& stack) { + int n = stack.pop_smallint_range(vm::Dictionary::max_key_bits); + vm::PrefixDictionary dict{stack.pop_maybe_cell(), n}; + auto cs = stack.pop_cellslice(); + auto res = dict.lookup(cs->data_bits(), cs->size()); + if (res.not_null()) { + stack.push_cellslice(std::move(res)); + stack.push_bool(true); + } else { + stack.push_bool(false); + } +} + +void interpret_bytes_hex_literal(IntCtx& ctx) { + auto s = ctx.scan_word_to('}'); + std::string t; + t.reserve(s.size() >> 1); + int v = 1; + for (char c : s) { + if (c == ' ' || c == '\t') { + continue; + } + v <<= 4; + if (c >= '0' && c <= '9') { + v += c - '0'; + } else { + c |= 0x20; + if (c >= 'a' && c <= 'f') { + v += c - ('a' - 10); + } else { + v = -1; + break; + } + } + if (v & 0x100) { + t.push_back((char)v); + v = 1; + } + } + if (v != 1) { + throw IntError{"Invalid bytes hexstring constant"}; + } + ctx.stack.push_bytes(std::move(t)); + push_argcount(ctx.stack, 1); +} + +void interpret_bitstring_hex_literal(IntCtx& ctx) { + auto s = ctx.scan_word_to('}'); + unsigned char buff[128]; + int bits = (int)td::bitstring::parse_bitstring_hex_literal(buff, sizeof(buff), s.begin(), s.end()); + if (bits < 0) { + throw IntError{"Invalid hex bitstring constant"}; + } + auto cs = Ref{true, vm::CellBuilder().store_bits(td::ConstBitPtr{buff}, bits).finalize()}; + ctx.stack.push(std::move(cs)); + push_argcount(ctx.stack, 1); +} + +void interpret_bitstring_binary_literal(IntCtx& ctx) { + auto s = ctx.scan_word_to('}'); + unsigned char buff[128]; + int bits = (int)td::bitstring::parse_bitstring_binary_literal(buff, sizeof(buff), s.begin(), s.end()); + if (bits < 0) { + throw IntError{"Invalid binary bitstring constant"}; + } + auto cs = Ref{true, vm::CellBuilder().store_bits(td::ConstBitPtr{buff}, bits).finalize()}; + ctx.stack.push(std::move(cs)); + push_argcount(ctx.stack, 1); +} + +void interpret_word(IntCtx& ctx) { + char sep = (char)ctx.stack.pop_smallint_range(127); + auto word = (sep != ' ' ? ctx.scan_word_to(sep, true) : ctx.scan_word()); + ctx.stack.push_string(word); +} + +void interpret_word_ext(IntCtx& ctx) { + int mode = ctx.stack.pop_smallint_range(11); + auto delims = ctx.stack.pop_string(); + if (mode & 8) { + ctx.skipspc(mode & 4); + } + ctx.stack.push_string(ctx.scan_word_ext(CharClassifier{delims, mode & 3})); +} + +void interpret_skipspc(IntCtx& ctx) { + ctx.skipspc(); +} + +void interpret_wordlist_begin_aux(vm::Stack& stack) { + stack.push({vm::from_object, Ref{true}}); +} + +void interpret_wordlist_begin(IntCtx& ctx) { + check_not_int_exec(ctx); + interpret_wordlist_begin_aux(ctx.stack); + push_argcount(ctx, 0); + ++(ctx.state); +} + +void interpret_wordlist_end_aux(vm::Stack& stack) { + Ref wordlist_ref = pop_word_list(stack); + wordlist_ref.write().close(); + stack.push({vm::from_object, Ref{wordlist_ref}}); +} + +void interpret_wordlist_end(IntCtx& ctx) { + check_compile(ctx); + interpret_wordlist_end_aux(ctx.stack); + push_argcount(ctx, 1); + --(ctx.state); +} + +void interpret_internal_interpret_begin(IntCtx& ctx) { + check_compile(ctx); + push_argcount(ctx, 0); + ctx.state = -ctx.state; +} + +void interpret_internal_interpret_end(IntCtx& ctx) { + check_int_exec(ctx); + ctx.state = -ctx.state; + ctx.stack.push({vm::from_object, Dictionary::nop_word_def}); +} + +// (create) +// maybe need an extra argument to identify the vocabulary (namespace) to be edited +void interpret_create_aux(IntCtx& ctx, int mode) { + if (mode < 0) { + mode = ctx.stack.pop_smallint_range(3); + } + std::string word = ctx.stack.pop_string(); + if (!word.size()) { + throw IntError{"non-empty word name expected"}; + } + auto wd_ref = pop_exec_token(ctx.stack); + if (!(mode & 2)) { + word += ' '; + } + bool active = (mode & 1); + auto entry = ctx.dictionary->lookup(word); + if (entry) { + *entry = WordRef{wd_ref, active}; // redefine word + } else { + ctx.dictionary->def_word(std::move(word), {wd_ref, active}); + } +} + +// { bl word 0 (create) } : create +void interpret_create(IntCtx& ctx) { + auto word = ctx.scan_word(); + if (!word.size()) { + throw IntError{"non-empty word name expected"}; + } + ctx.stack.push_string(word); + interpret_create_aux(ctx, 0); +} + +Ref create_aux_wd{Ref{true, std::bind(interpret_create_aux, std::placeholders::_1, -1)}}; + +// { bl word 2 ' (create) } :: : +void interpret_colon(IntCtx& ctx, int mode) { + ctx.stack.push_string(ctx.scan_word()); + ctx.stack.push_smallint(mode); + ctx.stack.push_smallint(2); + ctx.stack.push({vm::from_object, create_aux_wd}); + //push_argcount(ctx, 2, create_wd); +} + +// (forget) +void interpret_forget_aux(IntCtx& ctx) { + std::string s = ctx.stack.pop_string(); + auto s_copy = s; + auto entry = ctx.dictionary->lookup(s); + if (!entry) { + s += " "; + entry = ctx.dictionary->lookup(s); + } + if (!entry) { + throw IntError{"`" + s_copy + "` not found"}; + } else { + ctx.dictionary->undef_word(s); + } +} + +// { bl word (forget) } : forget +void interpret_forget(IntCtx& ctx) { + ctx.stack.push_string(ctx.scan_word()); + interpret_forget_aux(ctx); +} + +void interpret_quote_str(IntCtx& ctx) { + ctx.stack.push_string(ctx.scan_word_to('"')); + push_argcount(ctx.stack, 1); +} + +int str_utf8_code(const char* str, int& len) { + if (len <= 0) { + return -1; + } + if (len >= 1 && (unsigned char)str[0] < 0x80) { + len = 1; + return str[0]; + } + if (len >= 2 && (str[0] & 0xe0) == 0xc0 && (str[1] & 0xc0) == 0x80) { + len = 2; + return ((str[0] & 0x1f) << 6) | (str[1] & 0x3f); + } + if (len >= 3 && (str[0] & 0xf0) == 0xe0 && (str[1] & 0xc0) == 0x80 && (str[2] & 0xc0) == 0x80) { + len = 3; + return ((str[0] & 0x0f) << 12) | ((str[1] & 0x3f) << 6) | (str[2] & 0x3f); + } + if (len >= 4 && (str[0] & 0xf8) == 0xf0 && (str[1] & 0xc0) == 0x80 && (str[2] & 0xc0) == 0x80 && + (str[3] & 0xc0) == 0x80) { + len = 4; + return ((str[0] & 7) << 18) | ((str[1] & 0x3f) << 12) | ((str[2] & 0x3f) << 6) | (str[3] & 0x3f); + } + return -1; +} + +void interpret_char(IntCtx& ctx) { + auto s = ctx.scan_word(); + int len = (s.size() < 10 ? (int)s.size() : 10); + int code = str_utf8_code(s.data(), len); + if (code < 0 || s.size() != (unsigned)len) { + throw IntError{"exactly one character expected"}; + } + ctx.stack.push_smallint(code); + push_argcount(ctx, 1); +} + +int parse_number(std::string s, td::RefInt256& num, td::RefInt256& denom, bool allow_frac = true, + bool throw_error = false) { + if (allow_frac) { + auto pos = s.find('/'); + if (pos != std::string::npos) { + return parse_number(std::string{s, 0, pos}, num, denom, false, throw_error) > 0 && + parse_number(std::string{s, pos + 1}, denom, num, false, throw_error) > 0 + ? 2 + : 0; + } + } + const char* str = s.c_str(); + int len = (int)s.size(); + int frac = -1, base, *frac_ptr = allow_frac ? &frac : nullptr; + num = td::RefInt256{true}; + auto& x = num.unique_write(); + if (len >= 4 && str[0] == '-' && str[1] == '0' && (str[2] == 'x' || str[2] == 'b')) { + if (str[2] == 'x') { + base = 16; + if (x.parse_hex(str + 3, len - 3, frac_ptr) != len - 3) { + return 0; + } + } else { + base = 2; + if (x.parse_binary(str + 3, len - 3, frac_ptr) != len - 3) { + return 0; + } + } + x.negate().normalize(); + } else if (len >= 3 && str[0] == '0' && (str[1] == 'x' || str[1] == 'b')) { + if (str[1] == 'x') { + base = 16; + if (x.parse_hex(str + 2, len - 2, frac_ptr) != len - 2) { + return 0; + } + } else { + base = 2; + if (x.parse_binary(str + 2, len - 2, frac_ptr) != len - 2) { + return 0; + } + } + } else { + base = 10; + if (!len || x.parse_dec(str, len, frac_ptr) != len) { + return 0; + } + } + if (!x.signed_fits_bits(257)) { + if (throw_error) { + throw IntError{"integer constant too large"}; + } + return 0; + } + if (frac < 0) { + return 1; + } else { + denom = td::RefInt256{true, 1}; + while (frac-- > 0) { + if (!denom.unique_write().mul_tiny(base).normalize_bool()) { + if (throw_error) { + throw IntError{"denominator in constant too large"}; + } + return 0; + } + } + if (!denom.unique_write().unsigned_fits_bits(256)) { + if (throw_error) { + throw IntError{"denominator in constant too large"}; + } + return 0; + } + return 2; + } +} + +void interpret_parse_number(vm::Stack& stack) { + td::RefInt256 num, denom; + int res = parse_number(stack.pop_string(), num, denom, true, false); + if (res >= 1) { + stack.push_int(std::move(num)); + } + if (res == 2) { + stack.push_int(std::move(denom)); + } + stack.push_smallint(res); +} + +void interpret_parse_hex_number(vm::Stack& stack) { + td::RefInt256 x{true}; + auto str = stack.pop_string(); + bool ok = (str.size() <= 65535) && x.unique_write().parse_hex(str.data(), (int)str.size()) == (int)str.size(); + if (ok) { + stack.push_int(std::move(x)); + } + stack.push_smallint(ok); +} + +void interpret_quit(IntCtx& ctx) { + throw Quit{0}; +} + +void interpret_bye(IntCtx& ctx) { + throw Quit{-1}; +} + +void interpret_halt(vm::Stack& stack) { + int code = stack.pop_smallint_range(255); + throw Quit{~code}; +} + +void interpret_abort(IntCtx& ctx) { + throw IntError{ctx.stack.pop_string()}; +} + +Ref interpret_execute(IntCtx& ctx) { + return pop_exec_token(ctx); +} + +Ref interpret_execute_times(IntCtx& ctx) { + int count = ctx.stack.pop_smallint_range(1000000000); + auto wd_ref = pop_exec_token(ctx); + if (!count) { + return {}; + } + while (--count > 0) { + wd_ref->run(ctx); + } + return wd_ref; +} + +Ref interpret_if(IntCtx& ctx) { + auto true_ref = pop_exec_token(ctx); + if (ctx.stack.pop_bool()) { + return true_ref; + } else { + return {}; + } +} + +Ref interpret_ifnot(IntCtx& ctx) { + auto false_ref = pop_exec_token(ctx); + if (ctx.stack.pop_bool()) { + return {}; + } else { + return false_ref; + } +} + +Ref interpret_cond(IntCtx& ctx) { + auto false_ref = pop_exec_token(ctx); + auto true_ref = pop_exec_token(ctx); + if (ctx.stack.pop_bool()) { + return true_ref; + } else { + return false_ref; + } +} + +void interpret_while(IntCtx& ctx) { + auto body_ref = pop_exec_token(ctx); + auto cond_ref = pop_exec_token(ctx); + while (true) { + cond_ref->run(ctx); + if (!ctx.stack.pop_bool()) { + break; + } + body_ref->run(ctx); + } +} + +void interpret_until(IntCtx& ctx) { + auto body_ref = pop_exec_token(ctx); + do { + body_ref->run(ctx); + } while (!ctx.stack.pop_bool()); +} + +void interpret_tick(IntCtx& ctx) { + std::string word = ctx.scan_word().str(); + auto entry = ctx.dictionary->lookup(word); + if (!entry) { + entry = ctx.dictionary->lookup(word + ' '); + if (!entry) { + throw IntError{"word `" + word + "` undefined"}; + } + } + ctx.stack.push({vm::from_object, entry->get_def()}); + push_argcount(ctx, 1); +} + +void interpret_find(IntCtx& ctx) { + std::string word = ctx.stack.pop_string(); + auto entry = ctx.dictionary->lookup(word); + if (!entry) { + entry = ctx.dictionary->lookup(word + ' '); + } + if (!entry) { + ctx.stack.push_bool(false); + } else { + ctx.stack.push({vm::from_object, entry->get_def()}); + ctx.stack.push_bool(true); + } +} + +void interpret_tick_nop(vm::Stack& stack) { + stack.push({vm::from_object, Dictionary::nop_word_def}); +} + +void interpret_include(IntCtx& ctx) { + auto fname = ctx.stack.pop_string(); + auto r_file = ctx.source_lookup->lookup_source(fname, ctx.currentd_dir); + if (r_file.is_error()) { + throw IntError{"cannot locate file `" + fname + "`"}; + } + auto file = r_file.move_as_ok(); + std::stringstream ss(std::move(file.data)); + IntCtx::Savepoint save{ctx, td::PathView(file.path).file_name().str(), td::PathView(file.path).parent_dir().str(), + &ss}; + funny_interpret_loop(ctx); +} + +void interpret_skip_source(vm::Stack& stack) { + throw SkipToEof(); +} + +void interpret_words(IntCtx& ctx) { + for (const auto& x : *ctx.dictionary) { + *ctx.output_stream << x.first << " "; + } + *ctx.output_stream << std::endl; +} + +void interpret_pack_std_smc_addr(vm::Stack& stack) { + block::StdAddress a; + stack.check_underflow(3); + int mode = stack.pop_smallint_range(7); + td::RefInt256 x = stack.pop_int_finite(); + if (td::sgn(x) < 0) { + throw IntError{"non-negative integer expected"}; + } + CHECK(x->export_bytes(a.addr.data(), 32, false)); + a.workchain = stack.pop_smallint_range(0x7f, -0x80); + a.testnet = mode & 2; + a.bounceable = !(mode & 1); + stack.push_string(a.rserialize(mode & 4)); +} + +void interpret_unpack_std_smc_addr(vm::Stack& stack) { + block::StdAddress a; + if (!a.parse_addr(stack.pop_string())) { + stack.push_bool(false); + } else { + stack.push_smallint(a.workchain); + td::RefInt256 x{true}; + CHECK(x.write().import_bytes(a.addr.data(), 32, false)); + stack.push_int(std::move(x)); + stack.push_smallint(a.testnet * 2 + 1 - a.bounceable); + stack.push_bool(true); + } +} + +void interpret_bytes_to_base64(vm::Stack& stack, bool base64_url) { + stack.push_string(td::str_base64_encode(stack.pop_bytes(), base64_url)); +} + +void interpret_base64_to_bytes(vm::Stack& stack, bool allow_base64_url, bool quiet) { + auto s = stack.pop_string(); + if (!td::is_valid_base64(s, allow_base64_url)) { + stack.push_bool(false); + if (!quiet) { + throw IntError{"invalid base64"}; + } + } else { + stack.push_bytes(td::str_base64_decode(s, allow_base64_url)); + if (quiet) { + stack.push_bool(true); + } + } +} + +vm::VmLog create_vm_log(td::LogInterface* logger) { + if (!logger) { + return {}; + } + auto options = td::LogOptions::plain(); + options.level = 4; + options.fix_newlines = true; + return {logger, options}; +} + +class StringLogger : public td::LogInterface { + public: + void append(td::CSlice slice) override { + res.append(slice.data(), slice.size()); + } + std::string res; +}; +class OstreamLogger : public td::LogInterface { + public: + explicit OstreamLogger(std::ostream* stream) : stream_(stream) { + } + void append(td::CSlice slice) override { + stream_->write(slice.data(), slice.size()); + } + + private: + std::ostream* stream_{nullptr}; +}; + +td::Ref vm_libraries{true}; + +std::vector> get_vm_libraries() { + if (vm_libraries->get().type() == vm::StackEntry::t_cell) { + return {vm_libraries->get().as_cell()}; + } else { + return {}; + } +} + +void interpret_run_vm_code(IntCtx& ctx, bool with_gas) { + long long gas_limit = with_gas ? ctx.stack.pop_long_range(vm::GasLimits::infty) : vm::GasLimits::infty; + auto cs = ctx.stack.pop_cellslice(); + OstreamLogger ostream_logger(ctx.error_stream); + auto log = create_vm_log(ctx.error_stream ? &ostream_logger : nullptr); + vm::GasLimits gas{gas_limit}; + int res = vm::run_vm_code(cs, ctx.stack, 0, nullptr, log, nullptr, &gas, get_vm_libraries()); + ctx.stack.push_smallint(res); + if (with_gas) { + ctx.stack.push_smallint(gas.gas_consumed()); + } +} + +void interpret_run_vm_dict(IntCtx& ctx, bool with_gas) { + long long gas_limit = with_gas ? ctx.stack.pop_long_range(vm::GasLimits::infty) : vm::GasLimits::infty; + auto cs = ctx.stack.pop_cellslice(); + OstreamLogger ostream_logger(ctx.error_stream); + auto log = create_vm_log(ctx.error_stream ? &ostream_logger : nullptr); + vm::GasLimits gas{gas_limit}; + int res = vm::run_vm_code(cs, ctx.stack, 3, nullptr, log, nullptr, &gas, get_vm_libraries()); + ctx.stack.push_smallint(res); + if (with_gas) { + ctx.stack.push_smallint(gas.gas_consumed()); + } +} + +void interpret_run_vm(IntCtx& ctx, bool with_gas) { + long long gas_limit = with_gas ? ctx.stack.pop_long_range(vm::GasLimits::infty) : vm::GasLimits::infty; + auto data = ctx.stack.pop_cell(); + auto cs = ctx.stack.pop_cellslice(); + OstreamLogger ostream_logger(ctx.error_stream); + auto log = create_vm_log(ctx.error_stream ? &ostream_logger : nullptr); + vm::GasLimits gas{gas_limit}; + int res = vm::run_vm_code(cs, ctx.stack, 3, &data, log, nullptr, &gas); + ctx.stack.push_smallint(res); + ctx.stack.push_cell(std::move(data)); + if (with_gas) { + ctx.stack.push_smallint(gas.gas_consumed()); + } +} + +void do_interpret_db_run_vm_parallel(std::ostream* stream, vm::Stack& stack, vm::TonDb* ton_db_ptr, int threads_n, + int tasks_n) { + if (!ton_db_ptr || !*ton_db_ptr) { + throw vm::VmError{vm::Excno::fatal, "Ton database is not available"}; + } + auto& ton_db = *ton_db_ptr; + auto txn = ton_db->begin_transaction(); + auto txn_abort = td::ScopeExit() + [&] { ton_db->abort_transaction(std::move(txn)); }; + + struct Task { + vm::Ref code; + vm::SmartContractDb smart; + td::optional diff; + td::unique_ptr guard; + Ref stack; + int res{0}; + Ref data; + std::string log; + }; + std::vector tasks(tasks_n); + std::vector threads(threads_n); + + for (auto& task : tasks) { + task.code = stack.pop_cellslice(); + auto smart_hash = td::serialize(stack.pop_smallint_range(1000000000)); + task.smart = txn->begin_smartcontract(smart_hash); + task.guard = td::create_lambda_guard([&] { txn->abort_smartcontract(std::move(task.smart)); }); + auto argsn = stack.pop_smallint_range(100); + task.stack = stack.split_top(argsn); + } + + std::atomic next_task_i{0}; + auto run_tasks = [&] { + while (true) { + auto task_i = next_task_i++; + if (task_i >= tasks_n) { + break; + } + auto& task = tasks[task_i]; + auto data = task.smart->get_root(); + + StringLogger logger; + vm::VmLog log = create_vm_log(stream ? &logger : nullptr); + + task.res = vm::run_vm_code(task.code, task.stack, 3, &data, std::move(log)); + task.smart->set_root(data); + task.diff = vm::SmartContractDiff(std::move(task.smart)); + task.data = std::move(data); + task.log = std::move(logger.res); + } + }; + + td::Timer timer; + for (auto& thread : threads) { + thread = td::thread(run_tasks); + } + run_tasks(); + for (auto& thread : threads) { + thread.join(); + } + + if (stream) { + int id = 0; + for (auto& task : tasks) { + id++; + *stream << "Task #" << id << " vm_log begin" << std::endl; + *stream << task.log; + *stream << "Task #" << id << " vm_log end" << std::endl; + } + } + + LOG(ERROR) << timer; + timer = {}; + + for (auto& task : tasks) { + auto retn = task.stack.write().pop_smallint_range(100, -1); + if (retn == -1) { + retn = task.stack->depth(); + } + stack.push_from_stack(std::move(*task.stack), retn); + stack.push_smallint(task.res); + stack.push_cell(std::move(task.data)); + task.guard->dismiss(); + if (task.diff) { + txn->commit_smartcontract(std::move(task.diff.value())); + } else { + txn->commit_smartcontract(std::move(task.smart)); + } + } + LOG(ERROR) << timer; + timer = {}; + + txn_abort.dismiss(); + ton_db->commit_transaction(std::move(txn)); + timer = {}; + LOG(INFO) << "TonDB stats: \n" << ton_db->stats(); +} + +void interpret_db_run_vm(IntCtx& ctx) { + do_interpret_db_run_vm_parallel(ctx.error_stream, ctx.stack, ctx.ton_db, 0, 1); +} + +void interpret_db_run_vm_parallel(IntCtx& ctx) { + auto threads_n = ctx.stack.pop_smallint_range(32, 0); + auto tasks_n = ctx.stack.pop_smallint_range(1000000000); + do_interpret_db_run_vm_parallel(ctx.error_stream, ctx.stack, ctx.ton_db, threads_n, tasks_n); +} + +// n -- executes $n +void interpret_get_cmdline_arg(IntCtx& ctx) { + int n = ctx.stack.pop_smallint_range(999999); + char buffer[14]; + sprintf(buffer, "$%d ", n); + auto entry = ctx.dictionary->lookup(std::string{buffer}); + if (!entry) { + throw IntError{"-?"}; + } else { + (*entry)(ctx); + } +} + +// x1 .. xn n 'w --> +void interpret_execute_internal(IntCtx& ctx) { + Ref word_def = pop_exec_token(ctx); + int count = ctx.stack.pop_smallint_range(255); + ctx.stack.check_underflow(count); + word_def->run(ctx); +} + +// wl x1 .. xn n 'w --> wl' +void interpret_compile_internal(vm::Stack& stack) { + Ref word_def = pop_exec_token(stack); + int count = stack.pop_smallint_range(255); + do_compile_literals(stack, count); + if (word_def != Dictionary::nop_word_def) { + do_compile(stack, word_def); + } +} + +void do_compile(vm::Stack& stack, Ref word_def) { + Ref wl_ref = pop_word_list(stack); + if (word_def != Dictionary::nop_word_def) { + if ((td::uint64)word_def->list_size() <= 1) { + // inline short definitions + wl_ref.write().append(*(word_def->get_list())); + } else { + wl_ref.write().push_back(word_def); + } + } + stack.push({vm::from_object, wl_ref}); +} + +void compile_one_literal(WordList& wlist, vm::StackEntry val) { + using namespace std::placeholders; + if (val.type() == vm::StackEntry::t_int) { + auto x = std::move(val).as_int(); + if (!x->signed_fits_bits(257)) { + throw IntError{"invalid numeric literal"}; + } else if (x->signed_fits_bits(64)) { + wlist.push_back(Ref{true, std::bind(interpret_const, _1, x->to_long())}); + } else { + wlist.push_back(Ref{true, std::bind(interpret_big_const, _1, std::move(x))}); + } + } else { + wlist.push_back(Ref{true, std::bind(interpret_literal, _1, std::move(val))}); + } +} + +void do_compile_literals(vm::Stack& stack, int count) { + if (count < 0) { + throw IntError{"cannot compile a negative number of literals"}; + } + stack.check_underflow(count + 1); + Ref wl_ref = std::move(stack[count]).as_object(); + if (wl_ref.is_null()) { + throw IntError{"list of words expected"}; + } + for (int i = count - 1; i >= 0; i--) { + compile_one_literal(wl_ref.write(), std::move(stack[i])); + } + stack.pop_many(count + 1); + stack.push({vm::from_object, wl_ref}); +} + +void init_words_common(Dictionary& d) { + using namespace std::placeholders; + d.def_word("nop ", Dictionary::nop_word_def); + // stack print/dump words + d.def_ctx_word(". ", std::bind(interpret_dot, _1, true)); + d.def_ctx_word("._ ", std::bind(interpret_dot, _1, false)); + d.def_ctx_word("x. ", std::bind(interpret_dothex, _1, false, true)); + d.def_ctx_word("x._ ", std::bind(interpret_dothex, _1, false, false)); + d.def_ctx_word("X. ", std::bind(interpret_dothex, _1, true, true)); + d.def_ctx_word("X._ ", std::bind(interpret_dothex, _1, true, false)); + d.def_ctx_word("b. ", std::bind(interpret_dotbinary, _1, true)); + d.def_ctx_word("b._ ", std::bind(interpret_dotbinary, _1, false)); + d.def_ctx_word("csr. ", interpret_dot_cellslice_rec); + d.def_ctx_word(".s ", interpret_dotstack); + d.def_ctx_word(".sl ", interpret_dotstack_list); + d.def_ctx_word(".dump ", interpret_dump); + d.def_ctx_word(".l ", interpret_print_list); + d.def_ctx_word(".tc ", interpret_dottc); + d.def_stack_word("(dump) ", interpret_dump_internal); + d.def_stack_word("(.) ", interpret_dot_internal); + d.def_stack_word("(x.) ", std::bind(interpret_dothex_internal, _1, false)); + d.def_stack_word("(X.) ", std::bind(interpret_dothex_internal, _1, true)); + d.def_stack_word("(b.) ", interpret_dotbinary_internal); + // stack manipulation + d.def_stack_word("drop ", interpret_drop); + d.def_stack_word("2drop ", interpret_2drop); + d.def_stack_word("dup ", interpret_dup); + d.def_stack_word("over ", interpret_over); + d.def_stack_word("2dup ", interpret_2dup); + d.def_stack_word("2over ", interpret_2over); + d.def_stack_word("swap ", interpret_swap); + d.def_stack_word("2swap ", interpret_2swap); + d.def_stack_word("tuck ", interpret_tuck); + d.def_stack_word("nip ", interpret_nip); + d.def_stack_word("rot ", interpret_rot); + d.def_stack_word("-rot ", interpret_rot_rev); + d.def_stack_word("pick ", interpret_pick); + d.def_stack_word("roll ", interpret_roll); + d.def_stack_word("-roll ", interpret_roll_rev); + d.def_stack_word("reverse ", interpret_reverse); + d.def_stack_word("exch ", interpret_exch); + d.def_stack_word("exch2 ", interpret_exch2); + d.def_stack_word("depth ", interpret_depth); + d.def_stack_word("?dup ", interpret_cond_dup); + // low-level stack manipulation + d.def_stack_word(" ", interpret_make_xchg); + d.def_stack_word(" ", interpret_make_push); + d.def_stack_word(" ", interpret_make_pop); + // arithmetic + d.def_stack_word("+ ", interpret_plus); + d.def_stack_word("- ", interpret_minus); + d.def_stack_word("negate ", interpret_negate); + d.def_stack_word("1+ ", std::bind(interpret_plus_tiny, _1, 1)); + d.def_stack_word("1- ", std::bind(interpret_plus_tiny, _1, -1)); + d.def_stack_word("2+ ", std::bind(interpret_plus_tiny, _1, 2)); + d.def_stack_word("2- ", std::bind(interpret_plus_tiny, _1, -2)); + d.def_stack_word("* ", interpret_times); + d.def_stack_word("/ ", std::bind(interpret_div, _1, -1)); + d.def_stack_word("/c ", std::bind(interpret_div, _1, 1)); + d.def_stack_word("/r ", std::bind(interpret_div, _1, 0)); + d.def_stack_word("mod ", std::bind(interpret_mod, _1, -1)); + d.def_stack_word("rmod ", std::bind(interpret_mod, _1, 0)); + d.def_stack_word("cmod ", std::bind(interpret_mod, _1, 1)); + d.def_stack_word("/mod ", std::bind(interpret_divmod, _1, -1)); + d.def_stack_word("/cmod ", std::bind(interpret_divmod, _1, 1)); + d.def_stack_word("/rmod ", std::bind(interpret_divmod, _1, 0)); + d.def_stack_word("*/ ", std::bind(interpret_times_div, _1, -1)); + d.def_stack_word("*/c ", std::bind(interpret_times_div, _1, 1)); + d.def_stack_word("*/r ", std::bind(interpret_times_div, _1, 0)); + d.def_stack_word("*/mod ", std::bind(interpret_times_divmod, _1, -1)); + d.def_stack_word("*/cmod ", std::bind(interpret_times_divmod, _1, 1)); + d.def_stack_word("*/rmod ", std::bind(interpret_times_divmod, _1, 0)); + d.def_stack_word("*mod ", std::bind(interpret_times_mod, _1, -1)); + d.def_stack_word("1<< ", interpret_pow2); + d.def_stack_word("-1<< ", interpret_neg_pow2); + d.def_stack_word("1<<1- ", interpret_pow2_minus1); + d.def_stack_word("%1<< ", interpret_mod_pow2); + d.def_stack_word("<< ", interpret_lshift); + d.def_stack_word(">> ", std::bind(interpret_rshift, _1, -1)); + d.def_stack_word(">>c ", std::bind(interpret_rshift, _1, 1)); + d.def_stack_word(">>r ", std::bind(interpret_rshift, _1, 0)); + d.def_stack_word("2* ", std::bind(interpret_lshift_const, _1, 1)); + d.def_stack_word("2/ ", std::bind(interpret_rshift_const, _1, 1)); + d.def_stack_word("*>> ", std::bind(interpret_times_rshift, _1, -1)); + d.def_stack_word("*>>c ", std::bind(interpret_times_rshift, _1, 1)); + d.def_stack_word("*>>r ", std::bind(interpret_times_rshift, _1, 0)); + d.def_stack_word("< ", std::bind(interpret_cmp, _1, "\xff\x00\xff")); + d.def_stack_word("<= ", std::bind(interpret_cmp, _1, "\xff\xff\x00")); + d.def_stack_word(">= ", std::bind(interpret_cmp, _1, "\x00\xff\xff")); + d.def_stack_word("< ", std::bind(interpret_cmp, _1, "\xff\x00\x00")); + d.def_stack_word("> ", std::bind(interpret_cmp, _1, "\x00\x00\xff")); + d.def_stack_word("sgn ", std::bind(interpret_sgn, _1, "\xff\x00\x01")); + d.def_stack_word("0= ", std::bind(interpret_sgn, _1, "\x00\xff\x00")); + d.def_stack_word("0<> ", std::bind(interpret_sgn, _1, "\xff\x00\xff")); + d.def_stack_word("0<= ", std::bind(interpret_sgn, _1, "\xff\xff\x00")); + d.def_stack_word("0>= ", std::bind(interpret_sgn, _1, "\x00\xff\xff")); + d.def_stack_word("0< ", std::bind(interpret_sgn, _1, "\xff\x00\x00")); + d.def_stack_word("0> ", std::bind(interpret_sgn, _1, "\x00\x00\xff")); + d.def_stack_word("fits ", std::bind(interpret_fits, _1, true)); + d.def_stack_word("ufits ", std::bind(interpret_fits, _1, false)); + // char/string manipulation + d.def_active_word("\"", interpret_quote_str); + d.def_active_word("char ", interpret_char); + d.def_ctx_word("emit ", interpret_emit); + d.def_ctx_word("space ", std::bind(interpret_emit_const, _1, ' ')); + d.def_ctx_word("cr ", std::bind(interpret_emit_const, _1, '\n')); + d.def_ctx_word("type ", interpret_type); + d.def_stack_word("string? ", interpret_is_string); + d.def_stack_word("chr ", interpret_chr); + d.def_stack_word("hold ", interpret_hold); + d.def_stack_word("(number) ", interpret_parse_number); + d.def_stack_word("(hex-number) ", interpret_parse_hex_number); + d.def_stack_word("$| ", interpret_str_split); + d.def_stack_word("$+ ", interpret_str_concat); + d.def_stack_word("$= ", interpret_str_equal); + d.def_stack_word("$cmp ", interpret_str_cmp); + d.def_stack_word("$reverse ", interpret_str_reverse); + d.def_stack_word("(-trailing) ", std::bind(interpret_str_remove_trailing_int, _1, 0)); + d.def_stack_word("-trailing ", std::bind(interpret_str_remove_trailing_int, _1, ' ')); + d.def_stack_word("-trailing0 ", std::bind(interpret_str_remove_trailing_int, _1, '0')); + d.def_stack_word("$len ", interpret_str_len); + d.def_stack_word("Blen ", interpret_bytes_len); + d.def_ctx_word("Bx. ", interpret_bytes_hex_print_raw); + d.def_stack_word("B| ", interpret_bytes_split); + d.def_stack_word("B+ ", interpret_bytes_concat); + d.def_stack_word("B= ", interpret_bytes_equal); + d.def_stack_word("Bcmp ", interpret_bytes_cmp); + d.def_stack_word("u>B ", std::bind(interpret_int_to_bytes, _1, false, false)); + d.def_stack_word("i>B ", std::bind(interpret_int_to_bytes, _1, true, false)); + d.def_stack_word("Lu>B ", std::bind(interpret_int_to_bytes, _1, false, true)); + d.def_stack_word("Li>B ", std::bind(interpret_int_to_bytes, _1, true, true)); + d.def_stack_word("B>u@ ", std::bind(interpret_bytes_fetch_int, _1, 0)); + d.def_stack_word("B>i@ ", std::bind(interpret_bytes_fetch_int, _1, 1)); + d.def_stack_word("B>u@+ ", std::bind(interpret_bytes_fetch_int, _1, 2)); + d.def_stack_word("B>i@+ ", std::bind(interpret_bytes_fetch_int, _1, 3)); + d.def_stack_word("B>Lu@ ", std::bind(interpret_bytes_fetch_int, _1, 0x10)); + d.def_stack_word("B>Li@ ", std::bind(interpret_bytes_fetch_int, _1, 0x11)); + d.def_stack_word("B>Lu@+ ", std::bind(interpret_bytes_fetch_int, _1, 0x12)); + d.def_stack_word("B>Li@+ ", std::bind(interpret_bytes_fetch_int, _1, 0x13)); + d.def_stack_word("Bhash ", interpret_bytes_hash); + // cell manipulation (create, write and modify cells) + d.def_stack_word(" ", std::bind(interpret_store_end, _1, false)); + d.def_stack_word("b>spec ", std::bind(interpret_store_end, _1, true)); + d.def_stack_word("$>s ", interpret_string_to_cellslice); + d.def_stack_word("|+ ", interpret_concat_cellslice); + d.def_stack_word("|_ ", interpret_concat_cellslice_ref); + d.def_stack_word("b+ ", interpret_concat_builders); + d.def_stack_word("bbits ", std::bind(interpret_builder_bitrefs, _1, 1)); + d.def_stack_word("brefs ", std::bind(interpret_builder_bitrefs, _1, 2)); + d.def_stack_word("bbitrefs ", std::bind(interpret_builder_bitrefs, _1, 3)); + d.def_stack_word("brembits ", std::bind(interpret_builder_remaining_bitrefs, _1, 1)); + d.def_stack_word("bremrefs ", std::bind(interpret_builder_remaining_bitrefs, _1, 2)); + d.def_stack_word("brembitrefs ", std::bind(interpret_builder_remaining_bitrefs, _1, 3)); + d.def_stack_word("hash ", interpret_cell_hash); + // cellslice manipulation (read from cells) + d.def_stack_word(" ", interpret_cell_check_empty); + d.def_stack_word("empty? ", interpret_cell_empty); + d.def_stack_word("remaining ", interpret_cell_remaining); + d.def_stack_word("sbits ", std::bind(interpret_slice_bitrefs, _1, 1)); + d.def_stack_word("srefs ", std::bind(interpret_slice_bitrefs, _1, 2)); + d.def_stack_word("sbitrefs ", std::bind(interpret_slice_bitrefs, _1, 3)); + // boc manipulation + d.def_stack_word("B>boc ", interpret_boc_deserialize); + d.def_stack_word("boc>B ", interpret_boc_serialize); + d.def_stack_word("boc+>B ", interpret_boc_serialize_ext); + d.def_ctx_word("file>B ", interpret_read_file); + d.def_ctx_word("filepart>B ", interpret_read_file_part); + d.def_ctx_word("B>file ", interpret_write_file); + d.def_ctx_word("file-exists? ", interpret_file_exists); + // custom & crypto + d.def_stack_word("now ", interpret_now); + d.def_stack_word("newkeypair ", interpret_new_keypair); + d.def_stack_word("priv>pub ", interpret_priv_key_to_pub); + d.def_stack_word("ed25519_sign ", interpret_ed25519_sign); + d.def_stack_word("ed25519_chksign ", interpret_ed25519_chksign); + d.def_stack_word("ed25519_sign_uint ", interpret_ed25519_sign_uint); + // vm dictionaries + d.def_stack_word("dictnew ", interpret_dict_new); + d.def_stack_word("dict>s ", interpret_dict_to_slice); + d.def_stack_word("dict, ", interpret_store_dict); + d.def_stack_word("dict@ ", std::bind(interpret_load_dict, _1, false)); + d.def_stack_word("dict@+ ", std::bind(interpret_load_dict, _1, true)); + d.def_stack_word("udict!+ ", std::bind(interpret_dict_add_u, _1, vm::Dictionary::SetMode::Add, false, false)); + d.def_stack_word("udict! ", std::bind(interpret_dict_add_u, _1, vm::Dictionary::SetMode::Set, false, false)); + d.def_stack_word("b>udict!+ ", std::bind(interpret_dict_add_u, _1, vm::Dictionary::SetMode::Add, true, false)); + d.def_stack_word("b>udict! ", std::bind(interpret_dict_add_u, _1, vm::Dictionary::SetMode::Set, true, false)); + d.def_stack_word("udict@ ", std::bind(interpret_dict_get_u, _1, false)); + d.def_stack_word("idict!+ ", std::bind(interpret_dict_add_u, _1, vm::Dictionary::SetMode::Add, false, true)); + d.def_stack_word("idict! ", std::bind(interpret_dict_add_u, _1, vm::Dictionary::SetMode::Set, false, true)); + d.def_stack_word("b>idict!+ ", std::bind(interpret_dict_add_u, _1, vm::Dictionary::SetMode::Add, true, true)); + d.def_stack_word("b>idict! ", std::bind(interpret_dict_add_u, _1, vm::Dictionary::SetMode::Set, true, true)); + d.def_stack_word("idict@ ", std::bind(interpret_dict_get_u, _1, true)); + d.def_stack_word("pfxdict!+ ", std::bind(interpret_pfx_dict_add, _1, vm::Dictionary::SetMode::Add, false)); + d.def_stack_word("pfxdict! ", std::bind(interpret_pfx_dict_add, _1, vm::Dictionary::SetMode::Set, false)); + d.def_stack_word("pfxdict@ ", interpret_pfx_dict_get); + d.def_ctx_word("dictmap ", interpret_dict_map); + d.def_ctx_word("dictmapext ", interpret_dict_map_ext); + d.def_ctx_word("dictforeach ", interpret_dict_foreach); + d.def_ctx_word("dictmerge ", interpret_dict_merge); + d.def_ctx_word("dictdiff ", interpret_dict_diff); + // slice/bitstring constants + d.def_active_word("B{", interpret_bytes_hex_literal); + d.def_active_word("x{", interpret_bitstring_hex_literal); + d.def_active_word("b{", interpret_bitstring_binary_literal); + // boxes/holes/variables + d.def_stack_word("hole ", interpret_hole); + d.def_stack_word("box ", interpret_box); + d.def_stack_word("@ ", interpret_box_fetch); + d.def_stack_word("! ", interpret_box_store); + d.def_stack_word("null ", interpret_push_null); + d.def_stack_word("null? ", interpret_is_null); + // tuples/arrays + d.def_stack_word("| ", interpret_empty_tuple); + d.def_stack_word(", ", interpret_tuple_push); + d.def_stack_word("tpop ", interpret_tuple_pop); + d.def_stack_word("[] ", interpret_tuple_index); + d.def_stack_word("[]= ", interpret_tuple_set); + d.def_stack_word("count ", interpret_tuple_len); + d.def_stack_word("tuple? ", interpret_is_tuple); + d.def_stack_word("tuple ", interpret_make_tuple); + d.def_stack_word("untuple ", std::bind(interpret_tuple_explode, _1, true)); + d.def_stack_word("explode ", std::bind(interpret_tuple_explode, _1, false)); + d.def_stack_word("allot ", interpret_allot); + // atoms + d.def_stack_word("anon ", interpret_atom_anon); + d.def_stack_word("(atom) ", interpret_atom); + d.def_stack_word("atom>$ ", interpret_atom_name); + d.def_stack_word("eq? ", interpret_is_eq); + d.def_stack_word("eqv? ", interpret_is_eqv); + d.def_stack_word("atom? ", interpret_is_atom); + // execution control + d.def_ctx_tail_word("execute ", interpret_execute); + d.def_ctx_tail_word("times ", interpret_execute_times); + d.def_ctx_tail_word("if ", interpret_if); + d.def_ctx_tail_word("ifnot ", interpret_ifnot); + d.def_ctx_tail_word("cond ", interpret_cond); + d.def_ctx_word("while ", interpret_while); + d.def_ctx_word("until ", interpret_until); + // compiler control + d.def_active_word("[ ", interpret_internal_interpret_begin); + d.def_active_word("] ", interpret_internal_interpret_end); + d.def_active_word("{ ", interpret_wordlist_begin); + d.def_active_word("} ", interpret_wordlist_end); + d.def_stack_word("({) ", interpret_wordlist_begin_aux); + d.def_stack_word("(}) ", interpret_wordlist_end_aux); + d.def_stack_word("(compile) ", interpret_compile_internal); + d.def_ctx_word("(execute) ", interpret_execute_internal); + d.def_active_word("' ", interpret_tick); + d.def_stack_word("'nop ", interpret_tick_nop); + // dictionary manipulation + d.def_ctx_word("find ", interpret_find); + d.def_ctx_word("create ", interpret_create); + d.def_ctx_word("(create) ", std::bind(interpret_create_aux, _1, -1)); + d.def_active_word(": ", std::bind(interpret_colon, _1, 0)); + d.def_active_word(":: ", std::bind(interpret_colon, _1, 1)); + d.def_active_word(":_ ", std::bind(interpret_colon, _1, 2)); + d.def_active_word("::_ ", std::bind(interpret_colon, _1, 3)); + d.def_ctx_word("(forget) ", interpret_forget_aux); + d.def_ctx_word("forget ", interpret_forget); + d.def_ctx_word("words ", interpret_words); + // input parse + d.def_ctx_word("word ", interpret_word); + d.def_ctx_word("(word) ", interpret_word_ext); + d.def_ctx_word("skipspc ", interpret_skipspc); + d.def_ctx_word("include ", interpret_include); + d.def_stack_word("skip-to-eof ", interpret_skip_source); + d.def_ctx_word("abort ", interpret_abort); + d.def_ctx_word("quit ", interpret_quit); + d.def_ctx_word("bye ", interpret_bye); + d.def_stack_word("halt ", interpret_halt); +} + +void init_words_ton(Dictionary& d) { + using namespace std::placeholders; + d.def_stack_word("smca>$ ", interpret_pack_std_smc_addr); + d.def_stack_word("$>smca ", interpret_unpack_std_smc_addr); + d.def_stack_word("B>base64 ", std::bind(interpret_bytes_to_base64, _1, false)); + d.def_stack_word("B>base64url ", std::bind(interpret_bytes_to_base64, _1, true)); + d.def_stack_word("base64>B ", std::bind(interpret_base64_to_bytes, _1, false, false)); + d.def_stack_word("base64url>B ", std::bind(interpret_base64_to_bytes, _1, true, false)); +} + +void init_words_vm(Dictionary& d) { + using namespace std::placeholders; + vm::init_op_cp0(); + // vm run + d.def_stack_word("vmlibs ", std::bind(interpret_literal, _1, vm::StackEntry{vm_libraries})); + d.def_ctx_word("runvmcode ", std::bind(interpret_run_vm_code, _1, false)); + d.def_ctx_word("gasrunvmcode ", std::bind(interpret_run_vm_code, _1, true)); + d.def_ctx_word("runvmdict ", std::bind(interpret_run_vm_dict, _1, false)); + d.def_ctx_word("gasrunvmdict ", std::bind(interpret_run_vm_dict, _1, true)); + d.def_ctx_word("runvm ", std::bind(interpret_run_vm, _1, false)); + d.def_ctx_word("gasrunvm ", std::bind(interpret_run_vm, _1, true)); + d.def_ctx_word("dbrunvm ", interpret_db_run_vm); + d.def_ctx_word("dbrunvm-parallel ", interpret_db_run_vm_parallel); +} + +void import_cmdline_args(Dictionary& d, std::string arg0, int n, const char* const argv[]) { + using namespace std::placeholders; + LOG(DEBUG) << "import_cmdlist_args(" << arg0 << "," << n << ")"; + d.def_stack_word("$0 ", std::bind(interpret_literal, _1, vm::StackEntry{arg0})); + for (int i = 0; i < n; i++) { + char buffer[14]; + sprintf(buffer, "$%d ", i + 1); + d.def_stack_word(buffer, std::bind(interpret_literal, _1, vm::StackEntry{argv[i]})); + } + d.def_stack_word("$# ", std::bind(interpret_const, _1, n)); + d.def_ctx_word("$() ", interpret_get_cmdline_arg); +} + +std::pair numeric_value_ext(std::string s, bool allow_frac = true) { + td::RefInt256 num, denom; + int res = parse_number(s, num, denom, allow_frac); + if (res <= 0) { + throw IntError{"-?"}; + } + return std::make_pair(std::move(num), res == 2 ? std::move(denom) : td::RefInt256{}); +} + +td::RefInt256 numeric_value(std::string s) { + td::RefInt256 num, denom; + int res = parse_number(s, num, denom, false); + if (res != 1) { + throw IntError{"-?"}; + } + return num; +} + +int funny_interpret_loop(IntCtx& ctx) { + while (ctx.load_next_line()) { + if (ctx.is_sb()) { + continue; + } + std::ostringstream errs; + bool ok = true; + while (ok) { + ctx.skipspc(); + const char* ptr = ctx.get_input(); + if (!*ptr) { + break; + } + std::string Word; + Word.reserve(128); + auto entry = ctx.dictionary->lookup(""); + std::string entry_word; + const char* ptr_end = ptr; + while (*ptr && *ptr != ' ' && *ptr != '\t') { + Word += *ptr++; + auto cur = ctx.dictionary->lookup(Word); + if (cur) { + entry = cur; + entry_word = Word; + ptr_end = ptr; + } + } + auto cur = ctx.dictionary->lookup(Word + " "); + if (cur || !entry) { + entry = std::move(cur); + ctx.set_input(ptr); + ctx.skipspc(); + } else { + Word = entry_word; + ctx.set_input(ptr_end); + } + try { + if (entry) { + if (entry->is_active()) { + (*entry)(ctx); + } else { + ctx.stack.push_smallint(0); + ctx.stack.push({vm::from_object, entry->get_def()}); + } + } else { + auto res = numeric_value_ext(Word); + ctx.stack.push(std::move(res.first)); + if (res.second.not_null()) { + ctx.stack.push(std::move(res.second)); + push_argcount(ctx, 2); + } else { + push_argcount(ctx, 1); + } + } + if (ctx.state > 0) { + interpret_compile_internal(ctx.stack); + } else { + interpret_execute_internal(ctx); + } + } catch (IntError& ab) { + errs << ctx << Word << ": " << ab.msg; + ok = false; + } catch (vm::VmError& ab) { + errs << ctx << Word << ": " << ab.get_msg(); + ok = false; + } catch (vm::CellBuilder::CellWriteError) { + errs << ctx << Word << ": Cell builder write error"; + ok = false; + } catch (vm::VmFatal) { + errs << ctx << Word << ": fatal vm error"; + ok = false; + } catch (Quit& q) { + if (ctx.include_depth) { + throw; + } + if (!q.res) { + ok = false; + } else { + return q.res; + } + } catch (SkipToEof) { + return 0; + } + }; + if (!ok) { + auto err_msg = errs.str(); + if (!err_msg.empty()) { + LOG(ERROR) << err_msg; + } + ctx.clear(); + if (ctx.include_depth) { + throw IntError{"error interpreting included file `" + ctx.filename + "` : " + err_msg}; + } + } else if (!ctx.state && !ctx.include_depth) { + *ctx.output_stream << " ok" << std::endl; + } + } + return 0; +} + +} // namespace fift diff --git a/crypto/fift/words.h b/crypto/fift/words.h new file mode 100644 index 00000000..eac182e4 --- /dev/null +++ b/crypto/fift/words.h @@ -0,0 +1,43 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once +#include "Dictionary.h" + +namespace fift { + +// thrown by 'quit', 'bye' and 'halt' for exiting to top level +struct Quit { + int res; + Quit() : res(0) { + } + Quit(int _res) : res(_res) { + } +}; + +struct SkipToEof {}; + +void init_words_common(Dictionary& dictionary); +void init_words_vm(Dictionary& dictionary); +void init_words_ton(Dictionary& dictionary); + +void import_cmdline_args(Dictionary& d, std::string arg0, int n, const char* const argv[]); + +int funny_interpret_loop(IntCtx& ctx); + +} // namespace fift diff --git a/crypto/func/abscode.cpp b/crypto/func/abscode.cpp new file mode 100644 index 00000000..1bc4f0d9 --- /dev/null +++ b/crypto/func/abscode.cpp @@ -0,0 +1,492 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "func.h" + +namespace funC { + +/* + * + * ABSTRACT CODE + * + */ + +TmpVar::TmpVar(var_idx_t _idx, int _cls, TypeExpr* _type, SymDef* sym, const SrcLocation* loc) + : v_type(_type), idx(_idx), cls(_cls), coord(0) { + if (sym) { + name = sym->sym_idx; + sym->value->idx = _idx; + } + if (loc) { + where = std::make_unique(*loc); + } + if (!_type) { + v_type = TypeExpr::new_hole(); + } +} + +void TmpVar::set_location(const SrcLocation& loc) { + if (where) { + *where = loc; + } else { + where = std::make_unique(loc); + } +} + +void TmpVar::dump(std::ostream& os) const { + show(os); + os << " : " << v_type << " (width "; + v_type->show_width(os); + os << ")"; + if (coord > 0) { + os << " = _" << (coord >> 8) << '.' << (coord & 255); + } else if (coord < 0) { + int n = (~coord >> 8), k = (~coord & 0xff); + if (k) { + os << " = (_" << n << ".._" << (n + k - 1) << ")"; + } else { + os << " = ()"; + } + } + os << std::endl; +} + +void TmpVar::show(std::ostream& os, int omit_idx) const { + if (cls & _Named) { + os << sym::symbols.get_name(name); + if (omit_idx && (omit_idx >= 2 || (cls & _UniqueName))) { + return; + } + } + os << '_' << idx; +} + +std::ostream& operator<<(std::ostream& os, const TmpVar& var) { + var.show(os); + return os; +} + +void VarDescr::show_value(std::ostream& os) const { + if (val & _Int) { + os << 'i'; + } + if (val & _Const) { + os << 'c'; + } + if (val & _Zero) { + os << '0'; + } + if (val & _NonZero) { + os << '!'; + } + if (val & _Pos) { + os << '>'; + } + if (val & _Neg) { + os << '<'; + } + if (val & _Bool) { + os << 'B'; + } + if (val & _Bit) { + os << 'b'; + } + if (val & _Even) { + os << 'E'; + } + if (val & _Odd) { + os << 'O'; + } + if (val & _Finite) { + os << 'f'; + } + if (val & _Nan) { + os << 'N'; + } + if (int_const.not_null()) { + os << '=' << int_const; + } +} + +void VarDescr::show(std::ostream& os, const char* name) const { + if (flags & _Last) { + os << '*'; + } + if (flags & _Unused) { + os << '?'; + } + if (name) { + os << name; + } + os << '_' << idx; + show_value(os); +} + +void VarDescr::set_const(long long value) { + return set_const(td::RefInt256{true, value}); +} + +void VarDescr::set_const(td::RefInt256 value) { + int_const = std::move(value); + if (!int_const->signed_fits_bits(257)) { + int_const.write().invalidate(); + } + val = _Const | _Int; + int s = sgn(int_const); + if (s < -1) { + val |= _Nan | _NonZero; + } else if (s < 0) { + val |= _NonZero | _Neg | _Finite; + if (*int_const == -1) { + val |= _Bool; + } + } else if (s > 0) { + val |= _NonZero | _Pos | _Finite; + } else if (!s) { + if (*int_const == 1) { + val |= _Bit; + } + val |= _Zero | _Neg | _Pos | _Finite | _Bool | _Bit; + } + if (val & _Finite) { + val |= int_const->get_bit(0) ? _Odd : _Even; + } +} + +void VarDescr::set_const_nan() { + set_const(td::RefInt256{true}); +} + +void VarDescr::operator|=(const VarDescr& y) { + val &= y.val; + if (is_int_const() && cmp(int_const, y.int_const) != 0) { + val &= ~_Const; + } + if (!(val & _Const)) { + int_const.clear(); + } +} + +void VarDescr::operator&=(const VarDescr& y) { + val |= y.val; + if (y.int_const.not_null() && int_const.is_null()) { + int_const = y.int_const; + } +} + +void VarDescr::set_value(const VarDescr& y) { + val = y.val; + int_const = y.int_const; +} + +void VarDescr::set_value(VarDescr&& y) { + val = y.val; + int_const = std::move(y.int_const); +} + +void VarDescr::clear_value() { + val = 0; + int_const.clear(); +} + +void VarDescrList::show(std::ostream& os) const { + os << "["; + for (const auto& v : list) { + os << ' ' << v; + } + os << " ]\n"; +} + +void Op::flags_set_clear(int set, int clear) { + flags = (flags | set) & ~clear; + for (auto& op : block0) { + op.flags_set_clear(set, clear); + } + for (auto& op : block1) { + op.flags_set_clear(set, clear); + } +} +void Op::split_vars(const std::vector& vars) { + split_var_list(left, vars); + split_var_list(right, vars); + for (auto& op : block0) { + op.split_vars(vars); + } + for (auto& op : block1) { + op.split_vars(vars); + } +} + +void Op::split_var_list(std::vector& var_list, const std::vector& vars) { + int new_size = 0, changes = 0; + for (var_idx_t v : var_list) { + int c = vars.at(v).coord; + if (c < 0) { + ++changes; + new_size += (~c & 0xff); + } else { + ++new_size; + } + } + if (!changes) { + return; + } + std::vector new_var_list; + new_var_list.reserve(new_size); + for (var_idx_t v : var_list) { + int c = vars.at(v).coord; + if (c < 0) { + int n = (~c >> 8), k = (~c & 0xff); + while (k-- > 0) { + new_var_list.push_back(n++); + } + } else { + new_var_list.push_back(v); + } + } + var_list = std::move(new_var_list); +} + +void Op::show(std::ostream& os, const std::vector& vars, std::string pfx, int mode) const { + if (mode & 2) { + os << pfx << " ["; + for (const auto& v : var_info.list) { + os << ' '; + if (v.flags & VarDescr::_Last) { + os << '*'; + } + if (v.flags & VarDescr::_Unused) { + os << '?'; + } + os << vars[v.idx]; + if (mode & 4) { + os << ':'; + v.show_value(os); + } + } + os << " ]\n"; + } + std::string dis = disabled() ? " " : ""; + if (noreturn()) { + dis += " "; + } + if (!is_pure()) { + dis += " "; + } + switch (cl) { + case _Undef: + os << pfx << dis << "???\n"; + break; + case _Nop: + os << pfx << dis << "NOP\n"; + break; + case _Call: + os << pfx << dis << "CALL: "; + show_var_list(os, left, vars); + os << " := " << (fun_ref ? fun_ref->name() : "(null)") << " "; + if ((mode & 4) && args.size() == right.size()) { + show_var_list(os, args, vars); + } else { + show_var_list(os, right, vars); + } + os << std::endl; + break; + case _CallInd: + os << pfx << dis << "CALLIND: "; + show_var_list(os, left, vars); + os << " := EXEC "; + show_var_list(os, right, vars); + os << std::endl; + break; + case _Let: + os << pfx << dis << "LET "; + show_var_list(os, left, vars); + os << " := "; + show_var_list(os, right, vars); + os << std::endl; + break; + case _IntConst: + os << pfx << dis << "CONST "; + show_var_list(os, left, vars); + os << " := " << int_const << std::endl; + break; + case _Import: + os << pfx << dis << "IMPORT "; + show_var_list(os, left, vars); + os << std::endl; + break; + case _Return: + os << pfx << dis << "RETURN "; + show_var_list(os, left, vars); + os << std::endl; + break; + case _GlobVar: + os << pfx << dis << "GLOBVAR "; + show_var_list(os, left, vars); + os << " := " << (fun_ref ? fun_ref->name() : "(null)") << std::endl; + break; + case _Repeat: + os << pfx << dis << "REPEAT "; + show_var_list(os, left, vars); + os << ' '; + show_block(os, block0.get(), vars, pfx, mode); + os << std::endl; + break; + case _If: + os << pfx << dis << "IF "; + show_var_list(os, left, vars); + os << ' '; + show_block(os, block0.get(), vars, pfx, mode); + os << " ELSE "; + show_block(os, block1.get(), vars, pfx, mode); + os << std::endl; + break; + case _While: + os << pfx << dis << "WHILE "; + show_var_list(os, left, vars); + os << ' '; + show_block(os, block0.get(), vars, pfx, mode); + os << " DO "; + show_block(os, block1.get(), vars, pfx, mode); + os << std::endl; + break; + case _Until: + os << pfx << dis << "UNTIL "; + show_var_list(os, left, vars); + os << ' '; + show_block(os, block0.get(), vars, pfx, mode); + os << std::endl; + break; + case _Again: + os << pfx << dis << "AGAIN "; + show_var_list(os, left, vars); + os << ' '; + show_block(os, block0.get(), vars, pfx, mode); + os << std::endl; + break; + default: + os << pfx << dis << " "; + show_var_list(os, left, vars); + os << " -- "; + show_var_list(os, right, vars); + os << std::endl; + break; + } +} + +void Op::show_var_list(std::ostream& os, const std::vector& idx_list, + const std::vector& vars) const { + if (!idx_list.size()) { + os << "()"; + } else if (idx_list.size() == 1) { + os << vars.at(idx_list[0]); + } else { + os << "(" << vars.at(idx_list[0]); + for (std::size_t i = 1; i < idx_list.size(); i++) { + os << "," << vars.at(idx_list[i]); + } + os << ")"; + } +} + +void Op::show_var_list(std::ostream& os, const std::vector& list, const std::vector& vars) const { + auto n = list.size(); + if (!n) { + os << "()"; + } else { + os << "( "; + for (std::size_t i = 0; i < list.size(); i++) { + if (i) { + os << ", "; + } + if (list[i].is_unused()) { + os << '?'; + } + os << vars.at(list[i].idx) << ':'; + list[i].show_value(os); + } + os << " )"; + } +} + +void Op::show_block(std::ostream& os, const Op* block, const std::vector& vars, std::string pfx, int mode) { + os << "{" << std::endl; + std::string pfx2 = pfx + " "; + for (const Op& op : block) { + op.show(os, vars, pfx2, mode); + } + os << pfx << "}"; +} + +void CodeBlob::flags_set_clear(int set, int clear) { + for (auto& op : ops) { + op.flags_set_clear(set, clear); + } +} + +std::ostream& operator<<(std::ostream& os, const CodeBlob& code) { + code.print(os); + return os; +} + +// flags: +1 = show variable definition locations; +2 = show vars after each op; +4 = show var abstract value info after each op; +8 = show all variables at start +void CodeBlob::print(std::ostream& os, int flags) const { + os << "CODE BLOB: " << var_cnt << " variables, " << in_var_cnt << " input\n"; + if ((flags & 8) != 0) { + for (const auto& var : vars) { + var.dump(os); + if (var.where && (flags & 1) != 0) { + var.where->show(os); + os << " defined here:\n"; + var.where->show_context(os); + } + } + } + os << "------- BEGIN --------\n"; + for (const auto& op : ops) { + op.show(os, vars, "", flags); + } + os << "-------- END ---------\n\n"; +} + +var_idx_t CodeBlob::create_var(int cls, TypeExpr* var_type, SymDef* sym, const SrcLocation* location) { + vars.emplace_back(var_cnt, cls, var_type, sym, location); + if (sym) { + sym->value->idx = var_cnt; + } + return var_cnt++; +} + +bool CodeBlob::import_params(FormalArgList arg_list) { + if (var_cnt || in_var_cnt || op_cnt) { + return false; + } + std::vector list; + for (const auto& par : arg_list) { + TypeExpr* arg_type; + SymDef* arg_sym; + SrcLocation arg_loc; + std::tie(arg_type, arg_sym, arg_loc) = par; + list.push_back(create_var(arg_sym ? (TmpVar::_In | TmpVar::_Named) : TmpVar::_In, arg_type, arg_sym, &arg_loc)); + } + emplace_back(loc, Op::_Import, list); + in_var_cnt = var_cnt; + return true; +} + +} // namespace funC diff --git a/crypto/func/analyzer.cpp b/crypto/func/analyzer.cpp new file mode 100644 index 00000000..207aa64b --- /dev/null +++ b/crypto/func/analyzer.cpp @@ -0,0 +1,846 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "func.h" + +namespace funC { + +/* + * + * ANALYZE AND PREPROCESS ABSTRACT CODE + * + */ + +void CodeBlob::simplify_var_types() { + for (TmpVar& var : vars) { + TypeExpr::remove_indirect(var.v_type); + } +} + +int CodeBlob::split_vars(bool strict) { + int n = var_cnt, changes = 0; + for (int j = 0; j < var_cnt; j++) { + TmpVar& var = vars[j]; + if (strict && var.v_type->minw != var.v_type->maxw) { + throw src::ParseError{var.where.get(), "variable does not have fixed width, cannot manipulate it"}; + } + std::vector comp_types; + int k = var.v_type->extract_components(comp_types); + assert(k <= 254 && n <= 0x7fff00); + assert((unsigned)k == comp_types.size()); + if (k != 1) { + var.coord = ~((n << 8) + k); + for (int i = 0; i < k; i++) { + auto v = create_var(vars[j].cls, comp_types[i], 0, vars[j].where.get()); + assert(v == n + i); + assert(vars[v].idx == v); + vars[v].name = vars[j].name; + vars[v].coord = ((int)j << 8) + i + 1; + } + n += k; + ++changes; + } else if (strict && var.v_type->minw != 1) { + throw src::ParseError{var.where.get(), + "cannot work with variable or variable component of width greater than one"}; + } + } + if (!changes) { + return 0; + } + for (auto& op : ops) { + op.split_vars(vars); + } + return changes; +} + +bool CodeBlob::compute_used_code_vars() { + VarDescrList empty_var_info; + return compute_used_code_vars(ops, empty_var_info, true); +} + +bool CodeBlob::compute_used_code_vars(std::unique_ptr& ops_ptr, const VarDescrList& var_info, bool edit) const { + assert(ops_ptr); + if (!ops_ptr->next) { + assert(ops_ptr->cl == Op::_Nop); + return ops_ptr->set_var_info(var_info); + } + return compute_used_code_vars(ops_ptr->next, var_info, edit) | ops_ptr->compute_used_vars(*this, edit); +} + +bool operator==(const VarDescrList& x, const VarDescrList& y) { + if (x.size() != y.size()) { + return false; + } + for (std::size_t i = 0; i < x.size(); i++) { + if (x.list[i].idx != y.list[i].idx || x.list[i].flags != y.list[i].flags) { + return false; + } + } + return true; +} + +bool same_values(const VarDescr& x, const VarDescr& y) { + if (x.val != y.val || x.int_const.is_null() != y.int_const.is_null()) { + return false; + } + if (x.int_const.not_null() && cmp(x.int_const, y.int_const) != 0) { + return false; + } + return true; +} + +bool same_values(const VarDescrList& x, const VarDescrList& y) { + if (x.size() != y.size()) { + return false; + } + for (std::size_t i = 0; i < x.size(); i++) { + if (x.list[i].idx != y.list[i].idx || !same_values(x.list[i], y.list[i])) { + return false; + } + } + return true; +} + +bool Op::set_var_info(const VarDescrList& new_var_info) { + if (var_info == new_var_info) { + return false; + } + var_info = new_var_info; + return true; +} + +bool Op::set_var_info(VarDescrList&& new_var_info) { + if (var_info == new_var_info) { + return false; + } + var_info = std::move(new_var_info); + return true; +} + +bool Op::set_var_info_except(const VarDescrList& new_var_info, const std::vector& var_list) { + if (!var_list.size()) { + return set_var_info(new_var_info); + } + VarDescrList tmp_info{new_var_info}; + tmp_info -= var_list; + return set_var_info(new_var_info); +} + +bool Op::set_var_info_except(VarDescrList&& new_var_info, const std::vector& var_list) { + if (var_list.size()) { + new_var_info -= var_list; + } + return set_var_info(std::move(new_var_info)); +} +std::vector sort_unique_vars(const std::vector& var_list) { + std::vector vars{var_list}, unique_vars; + std::sort(vars.begin(), vars.end()); + vars.erase(std::unique(vars.begin(), vars.end()), vars.end()); + return vars; +} + +VarDescr* VarDescrList::operator[](var_idx_t idx) { + auto it = std::lower_bound(list.begin(), list.end(), idx); + return it != list.end() && it->idx == idx ? &*it : nullptr; +} + +const VarDescr* VarDescrList::operator[](var_idx_t idx) const { + auto it = std::lower_bound(list.begin(), list.end(), idx); + return it != list.end() && it->idx == idx ? &*it : nullptr; +} + +std::size_t VarDescrList::count(const std::vector idx_list) const { + std::size_t res = 0; + for (var_idx_t idx : idx_list) { + if (operator[](idx)) { + ++res; + } + } + return res; +} + +std::size_t VarDescrList::count_used(const std::vector idx_list) const { + std::size_t res = 0; + for (var_idx_t idx : idx_list) { + auto v = operator[](idx); + if (v && !v->is_unused()) { + ++res; + } + } + return res; +} + +VarDescrList& VarDescrList::operator-=(var_idx_t idx) { + auto it = std::lower_bound(list.begin(), list.end(), idx); + if (it != list.end() && it->idx == idx) { + list.erase(it); + } + return *this; +} + +VarDescrList& VarDescrList::operator-=(const std::vector& idx_list) { + for (var_idx_t idx : idx_list) { + *this -= idx; + } + return *this; +} + +VarDescrList& VarDescrList::add_var(var_idx_t idx, bool unused) { + auto it = std::lower_bound(list.begin(), list.end(), idx); + if (it == list.end() || it->idx != idx) { + list.emplace(it, idx, VarDescr::_Last | (unused ? VarDescr::_Unused : 0)); + } else if (it->is_unused() && !unused) { + it->clear_unused(); + } + return *this; +} + +VarDescrList& VarDescrList::add_vars(const std::vector& idx_list, bool unused) { + for (var_idx_t idx : idx_list) { + add_var(idx, unused); + } + return *this; +} + +VarDescr& VarDescrList::add(var_idx_t idx) { + auto it = std::lower_bound(list.begin(), list.end(), idx); + if (it == list.end() || it->idx != idx) { + it = list.emplace(it, idx); + } + return *it; +} + +VarDescr& VarDescrList::add_newval(var_idx_t idx) { + auto it = std::lower_bound(list.begin(), list.end(), idx); + if (it == list.end() || it->idx != idx) { + return *list.emplace(it, idx); + } else { + it->clear_value(); + return *it; + } +} + +VarDescrList& VarDescrList::clear_last() { + for (auto& var : list) { + if (var.flags & VarDescr::_Last) { + var.flags &= ~VarDescr::_Last; + } + } + return *this; +} + +VarDescrList VarDescrList::operator+(const VarDescrList& y) const { + VarDescrList res; + auto it1 = list.cbegin(); + auto it2 = y.list.cbegin(); + while (it1 != list.cend() && it2 != y.list.cend()) { + if (it1->idx < it2->idx) { + res.list.push_back(*it1++); + } else if (it1->idx > it2->idx) { + res.list.push_back(*it2++); + } else { + res.list.push_back(*it1++); + res.list.back() += *it2++; + } + } + while (it1 != list.cend()) { + res.list.push_back(*it1++); + } + while (it2 != y.list.cend()) { + res.list.push_back(*it2++); + } + return res; +} + +VarDescrList& VarDescrList::operator+=(const VarDescrList& y) { + return *this = *this + y; +} + +VarDescrList VarDescrList::operator|(const VarDescrList& y) const { + VarDescrList res; + auto it1 = list.cbegin(); + auto it2 = y.list.cbegin(); + while (it1 != list.cend() && it2 != y.list.cend()) { + if (it1->idx < it2->idx) { + it1++; + } else if (it1->idx > it2->idx) { + it2++; + } else { + res.list.push_back(*it1++); + res.list.back() |= *it2++; + } + } + return res; +} + +VarDescrList& VarDescrList::operator|=(const VarDescrList& y) { + return *this = *this | y; +} + +VarDescrList& VarDescrList::operator&=(const VarDescrList& values) { + for (const VarDescr& vd : values.list) { + VarDescr* item = operator[](vd.idx); + if (item) { + *item &= vd; + } + } + return *this; +} + +VarDescrList& VarDescrList::import_values(const VarDescrList& values) { + for (const VarDescr& vd : values.list) { + VarDescr* item = operator[](vd.idx); + if (item) { + item->set_value(vd); + } + } + return *this; +} + +bool Op::std_compute_used_vars(bool disabled) { + // left = OP right + // var_info := (var_info - left) + right + VarDescrList new_var_info{next->var_info}; + new_var_info -= left; + new_var_info.clear_last(); + if (args.size() == right.size() && !disabled) { + for (const VarDescr& arg : args) { + new_var_info.add_var(arg.idx, arg.is_unused()); + } + } else { + new_var_info.add_vars(right, disabled); + } + return set_var_info(std::move(new_var_info)); +} + +bool Op::compute_used_vars(const CodeBlob& code, bool edit) { + assert(next); + const VarDescrList& next_var_info = next->var_info; + if (cl == _Nop) { + return set_var_info_except(next_var_info, left); + } + switch (cl) { + case _IntConst: + case _GlobVar: + case _Call: + case _CallInd: { + // left = EXEC right; + if (!next_var_info.count_used(left) && is_pure()) { + // all variables in `left` are not needed + if (edit) { + disable(); + } + return std_compute_used_vars(true); + } + return std_compute_used_vars(); + } + case _Let: { + // left = right + std::size_t cnt = next_var_info.count_used(left); + assert(left.size() == right.size()); + auto l_it = left.cbegin(), r_it = right.cbegin(); + VarDescrList new_var_info{next_var_info}; + new_var_info -= left; + new_var_info.clear_last(); + std::vector new_left, new_right; + for (; l_it < left.cend(); ++l_it, ++r_it) { + if (std::find(l_it + 1, left.cend(), *l_it) == left.cend()) { + auto p = next_var_info[*l_it]; + new_var_info.add_var(*r_it, !p || p->is_unused()); + new_left.push_back(*l_it); + new_right.push_back(*r_it); + } + } + if (new_left.size() < left.size()) { + left = std::move(new_left); + right = std::move(new_right); + } + if (!cnt && edit) { + // all variables in `left` are not needed + disable(); + } + return set_var_info(std::move(new_var_info)); + } + case _Return: { + // return left + if (var_info.count(left) == left.size()) { + return false; + } + std::vector unique_vars = sort_unique_vars(left); + var_info.list.clear(); + for (var_idx_t i : unique_vars) { + var_info.list.emplace_back(i, VarDescr::_Last); + } + return true; + } + case _Import: { + // import left + std::vector unique_vars = sort_unique_vars(left); + var_info.list.clear(); + for (var_idx_t i : unique_vars) { + var_info.list.emplace_back(i, next_var_info[i] ? 0 : VarDescr::_Last); + } + return true; + } + case _If: { + // if (left) then block0 else block1 + // VarDescrList nx_var_info = next_var_info; + // nx_var_info.clear_last(); + code.compute_used_code_vars(block0, next_var_info, edit); + VarDescrList merge_info; + if (block1) { + code.compute_used_code_vars(block1, next_var_info, edit); + merge_info = block0->var_info + block1->var_info; + } else { + merge_info = block0->var_info + next_var_info; + } + merge_info.clear_last(); + merge_info += left; + return set_var_info(std::move(merge_info)); + } + case _While: { + // while (block0 || left) block1; + // ... { block0 left block1 } block0 left next + VarDescrList after_cond_first{next_var_info}; + after_cond_first += left; + code.compute_used_code_vars(block0, after_cond_first, false); + VarDescrList new_var_info{block0->var_info}; + bool changes = false; + do { + code.compute_used_code_vars(block1, block0->var_info, changes); + VarDescrList after_cond{block1->var_info}; + after_cond += left; + code.compute_used_code_vars(block0, after_cond, changes); + std::size_t n = new_var_info.size(); + new_var_info += block0->var_info; + new_var_info.clear_last(); + if (changes) { + break; + } + changes = (new_var_info.size() == n); + } while (changes <= edit); + return set_var_info(std::move(new_var_info)); + } + case _Until: { + // until (block0 || left); + // .. { block0 left } block0 left next + VarDescrList after_cond_first{next_var_info}; + after_cond_first += left; + code.compute_used_code_vars(block0, after_cond_first, false); + VarDescrList new_var_info{block0->var_info}; + bool changes = false; + do { + VarDescrList after_cond{new_var_info}; + after_cond += next_var_info; + after_cond += left; + code.compute_used_code_vars(block0, after_cond, changes); + std::size_t n = new_var_info.size(); + new_var_info += block0->var_info; + new_var_info.clear_last(); + if (changes) { + break; + } + changes = (new_var_info.size() == n); + } while (changes <= edit); + return set_var_info(std::move(new_var_info) + next_var_info); + } + case _Repeat: { + // repeat (left) block0 + // left { block0 } next + VarDescrList new_var_info{next_var_info}; + bool changes = false; + do { + code.compute_used_code_vars(block0, new_var_info, changes); + std::size_t n = new_var_info.size(); + new_var_info += block0->var_info; + new_var_info.clear_last(); + if (changes) { + break; + } + changes = (new_var_info.size() == n); + } while (changes <= edit); + new_var_info += left; + return set_var_info(std::move(new_var_info)); + } + case _Again: { + // for(;;) block0 + // { block0 } + VarDescrList new_var_info; + bool changes = false; + do { + code.compute_used_code_vars(block0, new_var_info, changes); + std::size_t n = new_var_info.size(); + new_var_info += block0->var_info; + new_var_info.clear_last(); + if (changes) { + break; + } + changes = (new_var_info.size() == n); + } while (changes <= edit); + return set_var_info(std::move(new_var_info)); + } + default: + std::cerr << "fatal: unknown operation in compute_used_vars()\n"; + throw src::ParseError{where, "unknown operation"}; + } +} + +bool prune_unreachable(std::unique_ptr& ops) { + if (!ops) { + return true; + } + Op& op = *ops; + if (op.cl == Op::_Nop) { + if (op.next) { + ops = std::move(op.next); + return prune_unreachable(ops); + } + return true; + } + bool reach; + switch (op.cl) { + case Op::_IntConst: + case Op::_GlobVar: + case Op::_Call: + case Op::_CallInd: + case Op::_Import: + reach = true; + break; + case Op::_Let: { + reach = true; + break; + } + case Op::_Return: + reach = false; + break; + case Op::_If: { + // if left then block0 else block1; ... + VarDescr* c_var = op.var_info[op.left[0]]; + if (c_var && c_var->always_true()) { + op.block0->last().next = std::move(op.next); + ops = std::move(op.block0); + return prune_unreachable(ops); + } else if (c_var && c_var->always_false()) { + op.block1->last().next = std::move(op.next); + ops = std::move(op.block1); + return prune_unreachable(ops); + } else { + reach = prune_unreachable(op.block0) | prune_unreachable(op.block1); + } + break; + } + case Op::_While: { + // while (block0 || left) block1; + if (!prune_unreachable(op.block0)) { + // computation of block0 never returns + ops = std::move(op.block0); + return prune_unreachable(ops); + } + VarDescr* c_var = op.block0->last().var_info[op.left[0]]; + if (c_var && c_var->always_false()) { + // block1 never executed + op.block0->last().next = std::move(op.next); + ops = std::move(op.block0); + return false; + } else if (c_var && c_var->always_true()) { + if (!prune_unreachable(op.block1)) { + // block1 never returns + op.block0->last().next = std::move(op.block1); + ops = std::move(op.block0); + return false; + } + // infinite loop + op.cl = Op::_Again; + op.block0->last().next = std::move(op.block1); + op.left.clear(); + reach = false; + } else { + if (!prune_unreachable(op.block1)) { + // block1 never returns, while equivalent to block0 ; if left then block1 else next + op.cl = Op::_If; + std::unique_ptr new_op = std::move(op.block0); + op.block0 = std::move(op.block1); + op.block1 = std::make_unique(op.next->where, Op::_Nop); + new_op->last().next = std::move(ops); + ops = std::move(new_op); + } + reach = true; // block1 may be never executed + } + break; + } + case Op::_Repeat: { + // repeat (left) block0 + VarDescr* c_var = op.var_info[op.left[0]]; + if (c_var && c_var->always_nonpos()) { + // loop never executed + ops = std::move(op.next); + return prune_unreachable(ops); + } + if (c_var && c_var->always_pos()) { + if (!prune_unreachable(op.block0)) { + // block0 executed at least once, and it never returns + // replace code with block0 + ops = std::move(op.block0); + return false; + } + } else { + prune_unreachable(op.block0); + } + reach = true; + break; + } + case Op::_Until: + case Op::_Again: { + // do block0 until left; ... + if (!prune_unreachable(op.block0)) { + // block0 never returns, replace loop by block0 + ops = std::move(op.block0); + return false; + } + reach = true; + break; + } + default: + std::cerr << "fatal: unknown operation \n"; + throw src::ParseError{op.where, "unknown operation in prune_unreachable()"}; + } + if (reach) { + return prune_unreachable(op.next); + } else { + while (op.next->next) { + op.next = std::move(op.next->next); + } + return false; + } +} + +void CodeBlob::prune_unreachable_code() { + if (prune_unreachable(ops)) { + throw src::ParseError{loc, "control reaches end of function"}; + } +} + +void CodeBlob::fwd_analyze() { + VarDescrList values; + assert(ops && ops->cl == Op::_Import); + for (var_idx_t i : ops->left) { + values += i; + if (vars[i].v_type->is_int()) { + values[i]->val |= VarDescr::_Int; + } + } + ops->fwd_analyze(values); +} + +void Op::prepare_args(VarDescrList values) { + if (args.size() != right.size()) { + args.clear(); + for (var_idx_t i : right) { + args.emplace_back(i); + } + } + for (std::size_t i = 0; i < right.size(); i++) { + const VarDescr* val = values[right[i]]; + if (val) { + args[i].set_value(*val); + args[i].clear_unused(); + } + } +} + +VarDescrList Op::fwd_analyze(VarDescrList values) { + var_info.import_values(values); + switch (cl) { + case _Nop: + case _Import: + break; + case _Return: + values.list.clear(); + break; + case _IntConst: { + values.add_newval(left[0]).set_const(int_const); + break; + } + case _GlobVar: + case _Call: { + prepare_args(values); + auto func = dynamic_cast(fun_ref->value); + if (func) { + std::vector res; + res.reserve(left.size()); + for (var_idx_t i : left) { + res.emplace_back(i); + } + AsmOpList tmp; + func->compile(tmp, res, args); // abstract interpretation of res := f (args) + int j = 0; + for (var_idx_t i : left) { + values.add_newval(i).set_value(res[j++]); + } + } else { + for (var_idx_t i : left) { + values.add_newval(i); + } + } + break; + } + case _CallInd: { + for (var_idx_t i : left) { + values.add_newval(i); + } + break; + } + case _Let: { + std::vector old_val; + assert(left.size() == right.size()); + for (std::size_t i = 0; i < right.size(); i++) { + const VarDescr* ov = values[right[i]]; + if (!ov && verbosity >= 5) { + std::cerr << "FATAL: error in assignment at right component #" << i << " (no value for _" << right[i] << ")" + << std::endl; + for (auto x : left) { + std::cerr << '_' << x << " "; + } + std::cerr << "= "; + for (auto x : right) { + std::cerr << '_' << x << " "; + } + std::cerr << std::endl; + } + // assert(ov); + if (ov) { + old_val.push_back(*ov); + } else { + old_val.emplace_back(); + } + } + for (std::size_t i = 0; i < left.size(); i++) { + values.add_newval(left[i]).set_value(std::move(old_val[i])); + } + break; + } + case _If: { + VarDescrList val1 = block0->fwd_analyze(values); + VarDescrList val2 = block1 ? block1->fwd_analyze(std::move(values)) : std::move(values); + values = val1 | val2; + break; + } + case _Repeat: { + bool atl1 = (values[left[0]] && values[left[0]]->always_pos()); + VarDescrList next_values = block0->fwd_analyze(values); + while (true) { + VarDescrList new_values = values | next_values; + if (same_values(new_values, values)) { + break; + } + values = std::move(new_values); + next_values = block0->fwd_analyze(values); + } + if (atl1) { + values = std::move(next_values); + } + break; + } + case _While: { + values = block0->fwd_analyze(values); + if (values[left[0]] && values[left[0]]->always_false()) { + // block1 never executed + block1->fwd_analyze(values); + break; + } + while (true) { + VarDescrList next_values = values | block0->fwd_analyze(block1->fwd_analyze(values)); + if (same_values(next_values, values)) { + break; + } + values = std::move(next_values); + } + break; + } + case _Until: + case _Again: { + while (true) { + VarDescrList next_values = values | block0->fwd_analyze(values); + if (same_values(next_values, values)) { + break; + } + values = std::move(next_values); + } + values = block0->fwd_analyze(values); + break; + } + default: + std::cerr << "fatal: unknown operation \n"; + throw src::ParseError{where, "unknown operation in fwd_analyze()"}; + } + if (next) { + return next->fwd_analyze(std::move(values)); + } else { + return values; + } +} + +bool Op::set_noreturn(bool nr) { + if (nr) { + flags |= _NoReturn; + } else { + flags &= ~_NoReturn; + } + return nr; +} + +bool Op::mark_noreturn() { + switch (cl) { + case _Nop: + if (!next) { + return set_noreturn(false); + } + // fallthrough + case _Import: + case _IntConst: + case _Let: + case _GlobVar: + case _CallInd: + case _Call: + return set_noreturn(next->mark_noreturn()); + case _Return: + return set_noreturn(true); + case _If: + return set_noreturn((block0->mark_noreturn() & (block1 && block1->mark_noreturn())) | next->mark_noreturn()); + case _Again: + block0->mark_noreturn(); + return set_noreturn(false); + case _Until: + return set_noreturn(block0->mark_noreturn() | next->mark_noreturn()); + case _While: + block1->mark_noreturn(); + return set_noreturn(block0->mark_noreturn() | next->mark_noreturn()); + case _Repeat: + block0->mark_noreturn(); + return set_noreturn(next->mark_noreturn()); + default: + std::cerr << "fatal: unknown operation \n"; + throw src::ParseError{where, "unknown operation in mark_noreturn()"}; + } +} + +void CodeBlob::mark_noreturn() { + ops->mark_noreturn(); +} + +} // namespace funC diff --git a/crypto/func/asmops.cpp b/crypto/func/asmops.cpp new file mode 100644 index 00000000..6afdac84 --- /dev/null +++ b/crypto/func/asmops.cpp @@ -0,0 +1,329 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "parser/srcread.h" +#include "func.h" +#include + +namespace funC { + +/* + * + * ASM-OP LIST FUNCTIONS + * + */ + +int is_pos_pow2(td::RefInt256 x) { + if (sgn(x) > 0 && !sgn(x & (x - 1))) { + return x->bit_size(false) - 1; + } else { + return -1; + } +} + +int is_neg_pow2(td::RefInt256 x) { + return sgn(x) < 0 ? is_pos_pow2(-x) : 0; +} + +std::ostream& operator<<(std::ostream& os, AsmOp::SReg stack_reg) { + int i = stack_reg.idx; + if (i >= 0) { + if (i < 16) { + return os << 's' << i; + } else { + return os << i << " s()"; + } + } else if (i >= -2) { + return os << "s(" << i << ')'; + } else { + return os << i << " s()"; + } +} + +AsmOp AsmOp::Const(int arg, std::string push_op) { + std::ostringstream os; + os << arg << ' ' << push_op; + return AsmOp::Const(os.str()); +} + +AsmOp AsmOp::make_stk2(int a, int b, const char* str, int delta) { + std::ostringstream os; + os << SReg(a) << ' ' << SReg(b) << ' ' << str; + int c = std::max(a, b) + 1; + return AsmOp::Custom(os.str(), c, c + delta); +} + +AsmOp AsmOp::make_stk3(int a, int b, int c, const char* str, int delta) { + std::ostringstream os; + os << SReg(a) << ' ' << SReg(b) << ' ' << SReg(c) << ' ' << str; + int m = std::max(a, std::max(b, c)) + 1; + return AsmOp::Custom(os.str(), m, m + delta); +} + +AsmOp AsmOp::BlkSwap(int a, int b) { + std::ostringstream os; + if (a == 1 && b == 1) { + return AsmOp::Xchg(0, 1); + } else if (a == 1) { + if (b == 2) { + os << "ROT"; + } else { + os << b << " ROLL"; + } + } else if (b == 1) { + if (a == 2) { + os << "-ROT"; + } else { + os << a << " -ROLL"; + } + } else { + os << a << " " << b << " BLKSWAP"; + } + return AsmOp::Custom(os.str(), a + b, a + b); +} + +AsmOp AsmOp::BlkPush(int a, int b) { + std::ostringstream os; + if (a == 1) { + return AsmOp::Push(b); + } else if (a == 2 && b == 1) { + os << "2DUP"; + } else { + os << a << " " << b << " BLKPUSH"; + } + return AsmOp::Custom(os.str(), b + 1, a + b + 1); +} + +AsmOp AsmOp::BlkDrop(int a) { + std::ostringstream os; + if (a == 1) { + return AsmOp::Pop(); + } else if (a == 2) { + os << "2DROP"; + } else { + os << a << " BLKDROP"; + } + return AsmOp::Custom(os.str(), a, 0); +} + +AsmOp AsmOp::BlkReverse(int a, int b) { + std::ostringstream os; + os << a << " " << b << " REVERSE"; + return AsmOp::Custom(os.str(), a + b, a + b); +} + +AsmOp AsmOp::IntConst(td::RefInt256 x) { + if (x->signed_fits_bits(8)) { + return AsmOp::Const(dec_string(std::move(x)) + " PUSHINT"); + } + if (!x->is_valid()) { + return AsmOp::Const("PUSHNAN"); + } + int k = is_pos_pow2(x); + if (k >= 0) { + return AsmOp::Const(k, "PUSHPOW2"); + } + k = is_pos_pow2(x + 1); + if (k >= 0) { + return AsmOp::Const(k, "PUSHPOW2DEC"); + } + k = is_pos_pow2(-x); + if (k >= 0) { + return AsmOp::Const(k, "PUSHNEGPOW2"); + } + return AsmOp::Const(dec_string(std::move(x)) + " PUSHINT"); +} + +AsmOp AsmOp::Parse(std::string custom_op) { + if (custom_op == "NOP") { + return AsmOp::Nop(); + } else if (custom_op == "SWAP") { + return AsmOp::Xchg(1); + } else if (custom_op == "DROP") { + return AsmOp::Pop(0); + } else if (custom_op == "NIP") { + return AsmOp::Pop(1); + } else if (custom_op == "DUP") { + return AsmOp::Push(0); + } else if (custom_op == "OVER") { + return AsmOp::Push(1); + } else { + return AsmOp::Custom(custom_op); + } +} + +AsmOp AsmOp::Parse(std::string custom_op, int args, int retv) { + auto res = Parse(custom_op); + if (res.is_custom()) { + res.a = args; + res.b = retv; + } + return res; +} + +void AsmOp::out(std::ostream& os) const { + if (!op.empty()) { + os << op; + return; + } + switch (t) { + case a_none: + break; + case a_xchg: + if (!a && !(b & -2)) { + os << (b ? "SWAP" : "NOP"); + break; + } + os << "s" << a << " s" << b << " XCHG"; + break; + case a_push: + if (!(a & -2)) { + os << (a ? "OVER" : "DUP"); + break; + } + os << "s" << a << " PUSH"; + break; + case a_pop: + if (!(a & -2)) { + os << (a ? "NIP" : "DROP"); + break; + } + os << "s" << a << " POP"; + break; + default: + throw src::Fatal{"unknown assembler operation"}; + } +} + +void AsmOp::out_indent_nl(std::ostream& os, bool no_eol) const { + for (int i = 0; i < indent; i++) { + os << " "; + } + out(os); + if (!no_eol) { + os << std::endl; + } +} + +std::string AsmOp::to_string() const { + if (!op.empty()) { + return op; + } else { + std::ostringstream os; + out(os); + return os.str(); + } +} + +bool AsmOpList::append(const std::vector& ops) { + for (const auto& op : ops) { + if (!append(op)) { + return false; + } + } + return true; +} + +const_idx_t AsmOpList::register_const(Const new_const) { + if (new_const.is_null()) { + return not_const; + } + unsigned idx; + for (idx = 0; idx < constants_.size(); idx++) { + if (!td::cmp(new_const, constants_[idx])) { + return idx; + } + } + constants_.push_back(std::move(new_const)); + return (const_idx_t)idx; +} + +Const AsmOpList::get_const(const_idx_t idx) { + if ((unsigned)idx < constants_.size()) { + return constants_[idx]; + } else { + return {}; + } +} + +void AsmOpList::show_var(std::ostream& os, var_idx_t idx) const { + if (!var_names_ || (unsigned)idx >= var_names_->size()) { + os << '_' << idx; + } else { + var_names_->at(idx).show(os, 2); + } +} + +void AsmOpList::show_var_ext(std::ostream& os, std::pair idx_pair) const { + auto i = idx_pair.first; + auto j = idx_pair.second; + if (!var_names_ || (unsigned)i >= var_names_->size()) { + os << '_' << i; + } else { + var_names_->at(i).show(os, 2); + } + if ((unsigned)j < constants_.size() && constants_[j].not_null()) { + os << '=' << constants_[j]; + } +} + +void AsmOpList::out(std::ostream& os, int mode) const { + if (!(mode & 2)) { + for (const auto& op : list_) { + op.out_indent_nl(os); + } + } else { + std::size_t n = list_.size(); + for (std::size_t i = 0; i < n; i++) { + const auto& op = list_[i]; + if (!op.is_comment() && i + 1 < n && list_[i + 1].is_comment()) { + op.out_indent_nl(os, true); + os << '\t'; + do { + i++; + } while (i + 1 < n && list_[i + 1].is_comment()); + list_[i].out(os); + os << std::endl; + } else { + op.out_indent_nl(os, false); + } + } + } +} + +bool apply_op(StackTransform& trans, const AsmOp& op) { + if (!trans.is_valid()) { + return false; + } + switch (op.t) { + case AsmOp::a_none: + return true; + case AsmOp::a_xchg: + return trans.apply_xchg(op.a, op.b, true); + case AsmOp::a_push: + return trans.apply_push(op.a); + case AsmOp::a_pop: + return trans.apply_pop(op.a); + case AsmOp::a_const: + return !op.a && op.b == 1 && trans.apply_push_newconst(); + default: + return false; + } +} + +} // namespace funC diff --git a/crypto/func/builtins.cpp b/crypto/func/builtins.cpp new file mode 100644 index 00000000..2393e9c6 --- /dev/null +++ b/crypto/func/builtins.cpp @@ -0,0 +1,974 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "func.h" + +namespace funC { +using namespace std::literals::string_literals; + +/* + * + * SYMBOL VALUES + * + */ + +int glob_func_cnt, undef_func_cnt; +std::vector glob_func; + +SymDef* predefine_builtin_func(std::string name, TypeExpr* func_type) { + sym_idx_t name_idx = sym::symbols.lookup(name, 1); + if (sym::symbols.is_keyword(name_idx)) { + std::cerr << "fatal: global function `" << name << "` already defined as a keyword" << std::endl; + } + SymDef* def = sym::define_global_symbol(name_idx, true); + if (!def) { + std::cerr << "fatal: global function `" << name << "` already defined" << std::endl; + std::exit(1); + } + return def; +} + +template +void define_builtin_func(std::string name, TypeExpr* func_type, const T& func, bool impure = false) { + SymDef* def = predefine_builtin_func(name, func_type); + def->value = new SymValAsmFunc{func_type, func, impure}; +} + +template +void define_builtin_func_x(std::string name, TypeExpr* func_type, const T& func, std::initializer_list arg_order, + std::initializer_list ret_order = {}, bool impure = false) { + SymDef* def = predefine_builtin_func(name, func_type); + def->value = new SymValAsmFunc{func_type, func, arg_order, ret_order, impure}; +} + +void define_builtin_func_x(std::string name, TypeExpr* func_type, const AsmOp& macro, + std::initializer_list arg_order, std::initializer_list ret_order = {}, + bool impure = false) { + SymDef* def = predefine_builtin_func(name, func_type); + def->value = new SymValAsmFunc{func_type, make_simple_compile(macro), arg_order, ret_order, impure}; +} + +bool SymValAsmFunc::compile(AsmOpList& dest, std::vector& in, std::vector& out) const { + if (simple_compile) { + return dest.append(simple_compile(in, out)); + } else if (ext_compile) { + return ext_compile(dest, in, out); + } else { + return false; + } +} + +/* + * + * DEFINE BUILT-IN FUNCTIONS + * + */ + +int emulate_negate(int a) { + int f = VarDescr::_Pos | VarDescr::_Neg; + if ((a & f) && (~a & f)) { + a ^= f; + } + f = VarDescr::_Bit | VarDescr::_Bool; + if ((a & f) && (~a & f)) { + a ^= f; + } + return a; +} + +int emulate_add(int a, int b) { + if (b & VarDescr::_Zero) { + return a; + } else if (a & VarDescr::_Zero) { + return b; + } + int u = a & b, v = a | b; + int r = VarDescr::_Int; + int t = u & (VarDescr::_Pos | VarDescr::_Neg); + if (v & VarDescr::_Nan) { + return r | VarDescr::_Nan; + } + // non-quiet addition always returns finite results! + r |= t | VarDescr::_Finite; + if (t) { + r |= v & VarDescr::_NonZero; + } + r |= v & VarDescr::_Nan; + if (u & (VarDescr::_Odd | VarDescr::_Even)) { + r |= VarDescr::_Even; + } else if (!(~v & (VarDescr::_Odd | VarDescr::_Even))) { + r |= VarDescr::_Odd | VarDescr::_NonZero; + } + return r; +} + +int emulate_sub(int a, int b) { + return emulate_add(a, emulate_negate(b)); +} + +int emulate_mul(int a, int b) { + if ((b & (VarDescr::_NonZero | VarDescr::_Bit)) == (VarDescr::_NonZero | VarDescr::_Bit)) { + return a; + } else if ((a & (VarDescr::_NonZero | VarDescr::_Bit)) == (VarDescr::_NonZero | VarDescr::_Bit)) { + return b; + } + int u = a & b, v = a | b; + int r = VarDescr::_Int; + if (v & VarDescr::_Nan) { + return r | VarDescr::_Nan; + } + // non-quiet multiplication always yields finite results, if any + r |= VarDescr::_Finite; + if (v & VarDescr::_Zero) { + // non-quiet multiplication + // the result is zero, if any result at all + return VarDescr::ConstZero; + } + if (u & (VarDescr::_Pos | VarDescr::_Neg)) { + r |= VarDescr::_Pos; + } else if (!(~v & (VarDescr::_Pos | VarDescr::_Neg))) { + r |= VarDescr::_Neg; + } + if (u & (VarDescr::_Bit | VarDescr::_Bool)) { + r |= VarDescr::_Bit; + } else if (!(~v & (VarDescr::_Bit | VarDescr::_Bool))) { + r |= VarDescr::_Bool; + } + r |= v & VarDescr::_Even; + r |= u & (VarDescr::_Odd | VarDescr::_NonZero); + return r; +} + +int emulate_lshift(int a, int b) { + if (((a | b) & VarDescr::_Nan) || !(~b & (VarDescr::_Neg | VarDescr::_NonZero))) { + return VarDescr::_Int | VarDescr::_Nan; + } + if (b & VarDescr::_Zero) { + return a; + } + int t = ((b & VarDescr::_NonZero) ? VarDescr::_Even : 0); + t |= b & VarDescr::_Finite; + return emulate_mul(a, VarDescr::_Int | VarDescr::_Pos | VarDescr::_NonZero | VarDescr::_Even | t); +} + +int emulate_div(int a, int b) { + if ((b & (VarDescr::_NonZero | VarDescr::_Bit)) == (VarDescr::_NonZero | VarDescr::_Bit)) { + return a; + } else if ((b & (VarDescr::_NonZero | VarDescr::_Bool)) == (VarDescr::_NonZero | VarDescr::_Bool)) { + return emulate_negate(b); + } + if (b & VarDescr::_Zero) { + return VarDescr::_Int | VarDescr::_Nan; + } + int u = a & b, v = a | b; + int r = VarDescr::_Int; + if (v & VarDescr::_Nan) { + return r | VarDescr::_Nan; + } + // non-quiet division always yields finite results, if any + r |= VarDescr::_Finite; + if (a & VarDescr::_Zero) { + // non-quiet division + // the result is zero, if any result at all + return VarDescr::ConstZero; + } + if (u & (VarDescr::_Pos | VarDescr::_Neg)) { + r |= VarDescr::_Pos; + } else if (!(~v & (VarDescr::_Pos | VarDescr::_Neg))) { + r |= VarDescr::_Neg; + } + if (u & (VarDescr::_Bit | VarDescr::_Bool)) { + r |= VarDescr::_Bit; + } else if (!(~v & (VarDescr::_Bit | VarDescr::_Bool))) { + r |= VarDescr::_Bool; + } + return r; +} + +int emulate_rshift(int a, int b) { + if (((a | b) & VarDescr::_Nan) || !(~b & (VarDescr::_Neg | VarDescr::_NonZero))) { + return VarDescr::_Int | VarDescr::_Nan; + } + if (b & VarDescr::_Zero) { + return a; + } + int t = ((b & VarDescr::_NonZero) ? VarDescr::_Even : 0); + t |= b & VarDescr::_Finite; + return emulate_div(a, VarDescr::_Int | VarDescr::_Pos | VarDescr::_NonZero | VarDescr::_Even | t); +} + +int emulate_mod(int a, int b, int round_mode = -1) { + if ((b & (VarDescr::_NonZero | VarDescr::_Bit)) == (VarDescr::_NonZero | VarDescr::_Bit)) { + return VarDescr::ConstZero; + } else if ((b & (VarDescr::_NonZero | VarDescr::_Bool)) == (VarDescr::_NonZero | VarDescr::_Bool)) { + return VarDescr::ConstZero; + } + if (b & VarDescr::_Zero) { + return VarDescr::_Int | VarDescr::_Nan; + } + int r = VarDescr::_Int; + if ((a | b) & VarDescr::_Nan) { + return r | VarDescr::_Nan; + } + // non-quiet division always yields finite results, if any + r |= VarDescr::_Finite; + if (a & VarDescr::_Zero) { + // non-quiet division + // the result is zero, if any result at all + return VarDescr::ConstZero; + } + if (round_mode < 0) { + r |= b & (VarDescr::_Pos | VarDescr::_Neg); + } else if (round_mode > 0) { + r |= emulate_negate(b) & (VarDescr::_Pos | VarDescr::_Neg); + } + if (a & (VarDescr::_Bit | VarDescr::_Bool)) { + if (r & VarDescr::_Pos) { + r |= VarDescr::_Bit; + } + if (r & VarDescr::_Neg) { + r |= VarDescr::_Bool; + } + } + if (b & VarDescr::_Even) { + r |= a & (VarDescr::_Even | VarDescr::_Odd); + } + return r; +} + +bool VarDescr::always_less(const VarDescr& other) const { + if (is_int_const() && other.is_int_const()) { + return int_const < other.int_const; + } + return (always_nonpos() && other.always_pos()) || (always_neg() && other.always_nonneg()); +} + +bool VarDescr::always_leq(const VarDescr& other) const { + if (is_int_const() && other.is_int_const()) { + return int_const <= other.int_const; + } + return always_nonpos() && other.always_nonneg(); +} + +bool VarDescr::always_greater(const VarDescr& other) const { + return other.always_less(*this); +} + +bool VarDescr::always_geq(const VarDescr& other) const { + return other.always_leq(*this); +} + +bool VarDescr::always_equal(const VarDescr& other) const { + return is_int_const() && other.is_int_const() && *int_const == *other.int_const; +} + +bool VarDescr::always_neq(const VarDescr& other) const { + if (is_int_const() && other.is_int_const()) { + return *int_const != *other.int_const; + } + return always_greater(other) || always_less(other) || (always_even() && other.always_odd()) || + (always_odd() && other.always_even()); +} + +AsmOp exec_op(std::string op) { + return AsmOp::Custom(op); +} + +AsmOp exec_op(std::string op, int args, int retv = 1) { + return AsmOp::Custom(op, args, retv); +} + +AsmOp exec_arg_op(std::string op, long long arg) { + std::ostringstream os; + os << arg << ' ' << op; + return AsmOp::Custom(os.str()); +} + +AsmOp exec_arg_op(std::string op, long long arg, int args, int retv) { + std::ostringstream os; + os << arg << ' ' << op; + return AsmOp::Custom(os.str(), args, retv); +} + +AsmOp exec_arg_op(std::string op, td::RefInt256 arg) { + std::ostringstream os; + os << arg << ' ' << op; + return AsmOp::Custom(os.str()); +} + +AsmOp exec_arg_op(std::string op, td::RefInt256 arg, int args, int retv) { + std::ostringstream os; + os << arg << ' ' << op; + return AsmOp::Custom(os.str(), args, retv); +} + +AsmOp push_const(td::RefInt256 x) { + return AsmOp::IntConst(std::move(x)); +} + +AsmOp compile_add(std::vector& res, std::vector& args) { + assert(res.size() == 1 && args.size() == 2); + VarDescr &r = res[0], &x = args[0], &y = args[1]; + if (x.is_int_const() && y.is_int_const()) { + r.set_const(x.int_const + y.int_const); + x.unused(); + y.unused(); + return push_const(r.int_const); + } + r.val = emulate_add(x.val, y.val); + if (y.is_int_const() && y.int_const->signed_fits_bits(8)) { + y.unused(); + if (y.always_zero()) { + return AsmOp::Nop(); + } + if (*y.int_const == 1) { + return exec_op("INC", 1); + } + if (*y.int_const == -1) { + return exec_op("DEC", 1); + } + return exec_arg_op("ADDCONST", y.int_const, 1); + } + if (x.is_int_const() && x.int_const->signed_fits_bits(8)) { + x.unused(); + if (x.always_zero()) { + return AsmOp::Nop(); + } + if (*x.int_const == 1) { + return exec_op("INC", 1); + } + if (*x.int_const == -1) { + return exec_op("DEC", 1); + } + return exec_arg_op("ADDCONST", x.int_const, 1); + } + return exec_op("ADD", 2); +} + +AsmOp compile_sub(std::vector& res, std::vector& args) { + assert(res.size() == 1 && args.size() == 2); + VarDescr &r = res[0], &x = args[0], &y = args[1]; + if (x.is_int_const() && y.is_int_const()) { + r.set_const(x.int_const - y.int_const); + x.unused(); + y.unused(); + return push_const(r.int_const); + } + r.val = emulate_sub(x.val, y.val); + if (y.is_int_const() && (-y.int_const)->signed_fits_bits(8)) { + y.unused(); + if (y.always_zero()) { + return {}; + } + if (*y.int_const == 1) { + return exec_op("DEC", 1); + } + if (*y.int_const == -1) { + return exec_op("INC", 1); + } + return exec_arg_op("ADDCONST", -y.int_const, 1); + } + if (x.always_zero()) { + x.unused(); + return exec_op("NEGATE", 1); + } + return exec_op("SUB", 2); +} + +AsmOp compile_negate(std::vector& res, std::vector& args) { + assert(res.size() == 1 && args.size() == 1); + VarDescr &r = res[0], &x = args[0]; + if (x.is_int_const()) { + r.set_const(-x.int_const); + x.unused(); + return push_const(r.int_const); + } + r.val = emulate_negate(x.val); + return exec_op("NEGATE", 1); +} + +AsmOp compile_mul(std::vector& res, std::vector& args) { + assert(res.size() == 1 && args.size() == 2); + VarDescr &r = res[0], &x = args[0], &y = args[1]; + if (x.is_int_const() && y.is_int_const()) { + r.set_const(x.int_const * y.int_const); + x.unused(); + y.unused(); + return push_const(r.int_const); + } + r.val = emulate_mul(x.val, y.val); + if (y.is_int_const()) { + int k = is_pos_pow2(y.int_const); + if (y.int_const->signed_fits_bits(8) && k < 0) { + y.unused(); + if (y.always_zero() && x.always_finite()) { + // dubious optimization: NaN * 0 = ? + r.set_const(y.int_const); + return push_const(r.int_const); + } + if (*y.int_const == 1 && x.always_finite()) { + return AsmOp::Nop(); + } + if (*y.int_const == -1) { + return exec_op("NEGATE", 1); + } + return exec_arg_op("MULCONST", y.int_const, 1); + } + if (k >= 0) { + y.unused(); + return exec_arg_op("LSHIFT#", k, 1); + } + } + if (x.is_int_const()) { + int k = is_pos_pow2(x.int_const); + if (x.int_const->signed_fits_bits(8) && k < 0) { + x.unused(); + if (x.always_zero() && y.always_finite()) { + // dubious optimization: NaN * 0 = ? + r.set_const(x.int_const); + return push_const(r.int_const); + } + if (*x.int_const == 1 && y.always_finite()) { + return AsmOp::Nop(); + } + if (*x.int_const == -1) { + return exec_op("NEGATE", 1); + } + return exec_arg_op("MULCONST", x.int_const, 1); + } + if (k >= 0) { + x.unused(); + return exec_arg_op("LSHIFT#", k, 1); + } + } + return exec_op("MUL", 2); +} + +AsmOp compile_lshift(std::vector& res, std::vector& args) { + assert(res.size() == 1 && args.size() == 2); + VarDescr &r = res[0], &x = args[0], &y = args[1]; + if (y.is_int_const()) { + auto yv = y.int_const->to_long(); + if (yv < 0 || yv > 256) { + r.set_const_nan(); + x.unused(); + y.unused(); + return push_const(r.int_const); + } else if (x.is_int_const()) { + r.set_const(x.int_const << (int)yv); + x.unused(); + y.unused(); + return push_const(r.int_const); + } + } + r.val = emulate_lshift(x.val, y.val); + if (y.is_int_const()) { + int k = (int)(y.int_const->to_long()); + if (!k /* && x.always_finite() */) { + // dubious optimization: what if x=NaN ? + y.unused(); + return AsmOp::Nop(); + } + y.unused(); + return exec_arg_op("LSHIFT#", k, 1); + } + if (x.is_int_const()) { + auto xv = x.int_const->to_long(); + if (xv == 1) { + x.unused(); + return exec_op("POW2", 1); + } + if (xv == -1) { + x.unused(); + return exec_op("NEGPOW2", 1); + } + } + return exec_op("LSHIFT", 2); +} + +AsmOp compile_rshift(std::vector& res, std::vector& args, int round_mode) { + assert(res.size() == 1 && args.size() == 2); + VarDescr &r = res[0], &x = args[0], &y = args[1]; + if (y.is_int_const()) { + auto yv = y.int_const->to_long(); + if (yv < 0 || yv > 256) { + r.set_const_nan(); + x.unused(); + y.unused(); + return push_const(r.int_const); + } else if (x.is_int_const()) { + r.set_const(td::rshift(x.int_const, (int)yv, round_mode)); + x.unused(); + y.unused(); + return push_const(r.int_const); + } + } + r.val = emulate_rshift(x.val, y.val); + std::string rshift = (round_mode < 0 ? "RSHIFT" : (round_mode ? "RSHIFTC" : "RSHIFTR")); + if (y.is_int_const()) { + int k = (int)(y.int_const->to_long()); + if (!k /* && x.always_finite() */) { + // dubious optimization: what if x=NaN ? + y.unused(); + return AsmOp::Nop(); + } + y.unused(); + return exec_arg_op(rshift + "#", k, 1); + } + return exec_op(rshift, 2); +} + +AsmOp compile_div(std::vector& res, std::vector& args, int round_mode) { + assert(res.size() == 1 && args.size() == 2); + VarDescr &r = res[0], &x = args[0], &y = args[1]; + if (x.is_int_const() && y.is_int_const()) { + r.set_const(div(x.int_const, y.int_const, round_mode)); + x.unused(); + y.unused(); + return push_const(r.int_const); + } + r.val = emulate_div(x.val, y.val); + if (y.is_int_const()) { + if (*y.int_const == 0) { + x.unused(); + y.unused(); + r.set_const(div(y.int_const, y.int_const)); + return push_const(r.int_const); + } + if (*y.int_const == 1 && x.always_finite()) { + y.unused(); + return AsmOp::Nop(); + } + if (*y.int_const == -1) { + y.unused(); + return exec_op("NEGATE", 1); + } + int k = is_pos_pow2(y.int_const); + if (k > 0) { + y.unused(); + std::string op = "RSHIFT"; + if (round_mode >= 0) { + op += (round_mode > 0 ? 'C' : 'R'); + } + return exec_arg_op(op + '#', k, 1); + } + } + std::string op = "DIV"; + if (round_mode >= 0) { + op += (round_mode > 0 ? 'C' : 'R'); + } + return exec_op(op, 2); +} + +AsmOp compile_mod(std::vector& res, std::vector& args, int round_mode) { + assert(res.size() == 1 && args.size() == 2); + VarDescr &r = res[0], &x = args[0], &y = args[1]; + if (x.is_int_const() && y.is_int_const()) { + r.set_const(mod(x.int_const, y.int_const, round_mode)); + x.unused(); + y.unused(); + return push_const(r.int_const); + } + r.val = emulate_mod(x.val, y.val); + if (y.is_int_const()) { + if (*y.int_const == 0) { + x.unused(); + y.unused(); + r.set_const(mod(y.int_const, y.int_const)); + return push_const(r.int_const); + } + if ((*y.int_const == 1 || *y.int_const == -1) && x.always_finite()) { + x.unused(); + y.unused(); + r.set_const(td::RefInt256{true, 0}); + return push_const(r.int_const); + } + int k = is_pos_pow2(y.int_const); + if (k > 0) { + y.unused(); + std::string op = "MODPOW2"; + if (round_mode >= 0) { + op += (round_mode > 0 ? 'C' : 'R'); + } + return exec_arg_op(op + '#', k, 1); + } + } + std::string op = "MOD"; + if (round_mode >= 0) { + op += (round_mode > 0 ? 'C' : 'R'); + } + return exec_op(op, 2); +} + +int compute_compare(td::RefInt256 x, td::RefInt256 y, int mode) { + int s = td::cmp(x, y); + if (mode == 7) { + return s; + } else { + return (mode >> (1 - s)) & 1; + } +} + +// return value: +// 4 -> constant 1 +// 2 -> constant 0 +// 1 -> constant -1 +// 3 -> 0 or -1 +int compute_compare(const VarDescr& x, const VarDescr& y, int mode) { + switch (mode) { + case 1: // > + return x.always_greater(y) ? 1 : (x.always_leq(y) ? 2 : 3); + case 2: // = + return x.always_equal(y) ? 1 : (x.always_neq(y) ? 2 : 3); + case 3: // >= + return x.always_geq(y) ? 1 : (x.always_less(y) ? 2 : 3); + case 4: // < + return x.always_less(y) ? 1 : (x.always_geq(y) ? 2 : 3); + case 5: // <> + return x.always_neq(y) ? 1 : (x.always_equal(y) ? 2 : 3); + case 6: // >= + return x.always_geq(y) ? 1 : (x.always_less(y) ? 2 : 3); + case 7: // <=> + return x.always_less(y) + ? 1 + : (x.always_equal(y) + ? 2 + : (x.always_greater(y) + ? 4 + : (x.always_leq(y) ? 3 : (x.always_geq(y) ? 6 : (x.always_neq(y) ? 5 : 7))))); + default: + return 7; + } +} + +AsmOp compile_cmp_int(std::vector& res, std::vector& args, int mode) { + assert(mode >= 1 && mode <= 7); + assert(res.size() == 1 && args.size() == 2); + VarDescr &r = res[0], &x = args[0], &y = args[1]; + if (x.is_int_const() && y.is_int_const()) { + r.set_const(compute_compare(x.int_const, y.int_const, mode)); + x.unused(); + y.unused(); + return push_const(r.int_const); + } + int v = compute_compare(x, y, mode); + assert(v); + if (!(v & (v - 1))) { + r.set_const(v - (v >> 2) - 2); + x.unused(); + y.unused(); + return push_const(r.int_const); + } + r.val = ~0; + if (v & 1) { + r.val &= VarDescr::ConstTrue; + } + if (v & 2) { + r.val &= VarDescr::ConstZero; + } + if (v & 4) { + r.val &= VarDescr::ConstOne; + } + static const char* cmp_int_names[] = {"", "GTINT", "EQINT", "GTINT", "LESSINT", "NEQINT", "LESSINT"}; + static const char* cmp_names[] = {"", "GREATER", "EQUAL", "GEQ", "LESS", "NEQ", "LEQ", "CMP"}; + static int cmp_int_delta[] = {0, 0, 0, -1, 0, 0, 1}; + if (mode != 7) { + if (y.is_int_const() && y.int_const >= -128 && y.int_const <= 127) { + y.unused(); + return exec_arg_op(cmp_int_names[mode], y.int_const + cmp_int_delta[mode], 1); + } + if (x.is_int_const() && x.int_const >= -128 && x.int_const <= 127) { + x.unused(); + mode = ((mode & 4) >> 2) | (mode & 2) | ((mode & 1) << 2); + return exec_arg_op(cmp_int_names[mode], x.int_const + cmp_int_delta[mode], 1); + } + } + return exec_op(cmp_names[mode], 2); +} + +AsmOp compile_throw(std::vector& res, std::vector& args) { + assert(res.empty() && args.size() == 1); + VarDescr& x = args[0]; + if (x.is_int_const() && x.int_const->unsigned_fits_bits(11)) { + x.unused(); + return exec_arg_op("THROW", x.int_const, 0, 0); + } else { + return exec_op("THROWANY", 1, 0); + } +} + +AsmOp compile_cond_throw(std::vector& res, std::vector& args, bool mode) { + assert(res.empty() && args.size() == 2); + VarDescr &x = args[0], &y = args[1]; + std::string suff = (mode ? "IF" : "IFNOT"); + bool skip_cond = false; + if (y.always_true() || y.always_false()) { + y.unused(); + skip_cond = true; + if (y.always_true() != mode) { + x.unused(); + return AsmOp::Nop(); + } + } + if (x.is_int_const() && x.int_const->unsigned_fits_bits(11)) { + x.unused(); + return skip_cond ? exec_arg_op("THROW", x.int_const, 0, 0) : exec_arg_op("THROW"s + suff, x.int_const, 1, 0); + } else { + return skip_cond ? exec_op("THROWANY", 1, 0) : exec_arg_op("THROWANY"s + suff, 2, 0); + } +} + +AsmOp compile_bool_const(std::vector& res, std::vector& args, bool val) { + assert(res.size() == 1 && args.empty()); + VarDescr& r = res[0]; + r.set_const(val ? -1 : 0); + return AsmOp::Const(val ? "TRUE" : "FALSE"); +} + +// (slice, int) load_int(slice s, int len) asm(s len -> 1 0) "LDIX"; +// (slice, int) load_uint(slice s, int len) asm( -> 1 0) "LDUX"; +// int preload_int(slice s, int len) asm "PLDIX"; +// int preload_uint(slice s, int len) asm "PLDUX"; +AsmOp compile_fetch_int(std::vector& res, std::vector& args, bool fetch, bool sgnd) { + assert(args.size() == 2 && res.size() == 1 + (unsigned)fetch); + auto &y = args[1], &r = res.back(); + r.val = (sgnd ? VarDescr::FiniteInt : VarDescr::FiniteUInt); + int v = -1; + if (y.is_int_const() && y.int_const >= 0 && y.int_const <= 256) { + v = (int)y.int_const->to_long(); + if (!v) { + r.val = VarDescr::ConstZero; + } + if (v == 1) { + r.val = (sgnd ? VarDescr::ValBool : VarDescr::ValBit); + } + if (v > 0) { + y.unused(); + return exec_arg_op((fetch ? "LD"s : "PLD"s) + (sgnd ? 'I' : 'U'), v, 1, 1 + (unsigned)fetch); + } + } + return exec_op((fetch ? "LD"s : "PLD"s) + (sgnd ? "IX" : "UX"), 2, 1 + (unsigned)fetch); +} + +// builder store_uint(builder b, int x, int len) asm(x b len) "STUX"; +// builder store_int(builder b, int x, int len) asm(x b len) "STIX"; +AsmOp compile_store_int(std::vector& res, std::vector& args, bool sgnd) { + assert(args.size() == 3 && res.size() == 1); + auto& z = args[2]; + if (z.is_int_const() && z.int_const > 0 && z.int_const <= 256) { + z.unused(); + return exec_arg_op("ST"s + (sgnd ? 'I' : 'U'), z.int_const, 2, 1); + } + return exec_op("ST"s + (sgnd ? "IX" : "UX"), 3, 1); +} + +AsmOp compile_fetch_slice(std::vector& res, std::vector& args, bool fetch) { + assert(args.size() == 2 && res.size() == 1 + (unsigned)fetch); + auto& y = args[1]; + int v = -1; + if (y.is_int_const() && y.int_const > 0 && y.int_const <= 256) { + v = (int)y.int_const->to_long(); + if (v > 0) { + y.unused(); + return exec_arg_op(fetch ? "LDSLICE" : "PLDSLICE", v, 1, 1 + (unsigned)fetch); + } + } + return exec_op(fetch ? "LDSLICEX" : "PLDSLICEX", 2, 1 + (unsigned)fetch); +} + +// _at(tuple t, int index) asm "INDEXVAR"; +AsmOp compile_tuple_at(std::vector& res, std::vector& args) { + assert(args.size() == 2 && res.size() == 1); + auto& y = args[1]; + if (y.is_int_const() && y.int_const >= 0 && y.int_const < 16) { + y.unused(); + return exec_arg_op("INDEX", y.int_const, 1, 1); + } + return exec_op("INDEXVAR", 2, 1); +} + +// int null?(X arg) +AsmOp compile_is_null(std::vector& res, std::vector& args) { + assert(args.size() == 1 && res.size() == 1); + auto &x = args[0], &r = res[0]; + if (x.always_null() || x.always_not_null()) { + x.unused(); + r.set_const(x.always_null() ? -1 : 0); + return push_const(r.int_const); + } + res[0].val = VarDescr::ValBool; + return exec_op("ISNULL", 1, 1); +} + +bool compile_run_method(AsmOpList& code, std::vector& res, std::vector& args, int n, + bool has_value) { + assert(args.size() == (unsigned)n + 1 && res.size() == (unsigned)has_value); + auto& x = args[0]; + if (x.is_int_const() && x.int_const->unsigned_fits_bits(14)) { + x.unused(); + code << exec_arg_op("PREPAREDICT", x.int_const, 0, 2); + } else { + code << exec_op("c3 PUSH", 0, 1); + } + code << exec_arg_op(has_value ? "1 CALLXARGS" : "0 CALLXARGS", n, n + 2, (unsigned)has_value); + return true; +} + +void define_builtins() { + using namespace std::placeholders; + auto Unit = TypeExpr::new_unit(); + auto Int = TypeExpr::new_atomic(_Int); + auto Cell = TypeExpr::new_atomic(_Cell); + auto Slice = TypeExpr::new_atomic(_Slice); + auto Builder = TypeExpr::new_atomic(_Builder); + // auto Null = TypeExpr::new_atomic(_Null); + auto Tuple = TypeExpr::new_atomic(_Tuple); + auto Int2 = TypeExpr::new_tensor({Int, Int}); + auto Int3 = TypeExpr::new_tensor({Int, Int, Int}); + auto TupleInt = TypeExpr::new_tensor({Tuple, Int}); + auto SliceInt = TypeExpr::new_tensor({Slice, Int}); + auto X = TypeExpr::new_var(); + auto Y = TypeExpr::new_var(); + auto Z = TypeExpr::new_var(); + auto T = TypeExpr::new_var(); + auto XY = TypeExpr::new_tensor({X, Y}); + auto XYZ = TypeExpr::new_tensor({X, Y, Z}); + auto XYZT = TypeExpr::new_tensor({X, Y, Z, T}); + auto arith_bin_op = TypeExpr::new_map(Int2, Int); + auto arith_un_op = TypeExpr::new_map(Int, Int); + auto impure_bin_op = TypeExpr::new_map(Int2, Unit); + auto impure_un_op = TypeExpr::new_map(Int, Unit); + auto fetch_int_op = TypeExpr::new_map(SliceInt, SliceInt); + auto prefetch_int_op = TypeExpr::new_map(SliceInt, Int); + auto store_int_op = TypeExpr::new_map(TypeExpr::new_tensor({Builder, Int, Int}), Builder); + auto store_int_method = + TypeExpr::new_map(TypeExpr::new_tensor({Builder, Int, Int}), TypeExpr::new_tensor({Builder, Unit})); + auto fetch_slice_op = TypeExpr::new_map(SliceInt, TypeExpr::new_tensor({Slice, Slice})); + auto prefetch_slice_op = TypeExpr::new_map(SliceInt, Slice); + //auto arith_null_op = TypeExpr::new_map(TypeExpr::new_unit(), Int); + define_builtin_func("_+_", arith_bin_op, compile_add); + define_builtin_func("_-_", arith_bin_op, compile_sub); + define_builtin_func("-_", arith_un_op, compile_negate); + define_builtin_func("_*_", arith_bin_op, compile_mul); + define_builtin_func("_/_", arith_bin_op, std::bind(compile_div, _1, _2, -1)); + define_builtin_func("_/~_", arith_bin_op, std::bind(compile_div, _1, _2, 0)); + define_builtin_func("_/^_", arith_bin_op, std::bind(compile_div, _1, _2, 1)); + define_builtin_func("_%_", arith_bin_op, std::bind(compile_mod, _1, _2, -1)); + define_builtin_func("_%~_", arith_bin_op, std::bind(compile_mod, _1, _2, 0)); + define_builtin_func("_%^_", arith_bin_op, std::bind(compile_mod, _1, _2, -1)); + define_builtin_func("_/%_", TypeExpr::new_map(Int2, Int2), AsmOp::Custom("DIVMOD", 2, 2)); + define_builtin_func("_<<_", arith_bin_op, compile_lshift); + define_builtin_func("_>>_", arith_bin_op, std::bind(compile_rshift, _1, _2, -1)); + define_builtin_func("_>>~_", arith_bin_op, std::bind(compile_rshift, _1, _2, 0)); + define_builtin_func("_>>^_", arith_bin_op, std::bind(compile_rshift, _1, _2, 1)); + define_builtin_func("_&_", arith_bin_op, AsmOp::Custom("AND", 2)); + define_builtin_func("_|_", arith_bin_op, AsmOp::Custom("OR", 2)); + define_builtin_func("_^_", arith_bin_op, AsmOp::Custom("XOR", 2)); + define_builtin_func("~_", arith_un_op, AsmOp::Custom("NOT", 1)); + define_builtin_func("^_+=_", arith_bin_op, compile_add); + define_builtin_func("^_-=_", arith_bin_op, compile_sub); + define_builtin_func("^_*=_", arith_bin_op, compile_mul); + define_builtin_func("^_/=_", arith_bin_op, std::bind(compile_div, _1, _2, -1)); + define_builtin_func("^_/~=_", arith_bin_op, std::bind(compile_div, _1, _2, 0)); + define_builtin_func("^_/^=_", arith_bin_op, std::bind(compile_div, _1, _2, 1)); + define_builtin_func("^_%=_", arith_bin_op, std::bind(compile_mod, _1, _2, -1)); + define_builtin_func("^_%~=_", arith_bin_op, std::bind(compile_mod, _1, _2, 0)); + define_builtin_func("^_%^=_", arith_bin_op, std::bind(compile_mod, _1, _2, 1)); + define_builtin_func("^_<<=_", arith_bin_op, compile_lshift); + define_builtin_func("^_>>=_", arith_bin_op, std::bind(compile_rshift, _1, _2, -1)); + define_builtin_func("^_>>~=_", arith_bin_op, std::bind(compile_rshift, _1, _2, 0)); + define_builtin_func("^_>>^=_", arith_bin_op, std::bind(compile_rshift, _1, _2, 1)); + define_builtin_func("^_&=_", arith_bin_op, AsmOp::Custom("AND", 2)); + define_builtin_func("^_|=_", arith_bin_op, AsmOp::Custom("OR", 2)); + define_builtin_func("^_^=_", arith_bin_op, AsmOp::Custom("XOR", 2)); + define_builtin_func("muldivr", TypeExpr::new_map(Int3, Int), AsmOp::Custom("MULDIVR", 3)); + define_builtin_func("muldiv", TypeExpr::new_map(Int3, Int), AsmOp::Custom("MULDIV", 3)); + define_builtin_func("muldivmod", TypeExpr::new_map(Int3, Int2), AsmOp::Custom("MULDIVMOD", 3, 2)); + define_builtin_func("_==_", arith_bin_op, std::bind(compile_cmp_int, _1, _2, 2)); + define_builtin_func("_!=_", arith_bin_op, std::bind(compile_cmp_int, _1, _2, 5)); + define_builtin_func("_<_", arith_bin_op, std::bind(compile_cmp_int, _1, _2, 4)); + define_builtin_func("_>_", arith_bin_op, std::bind(compile_cmp_int, _1, _2, 1)); + define_builtin_func("_<=_", arith_bin_op, std::bind(compile_cmp_int, _1, _2, 6)); + define_builtin_func("_>=_", arith_bin_op, std::bind(compile_cmp_int, _1, _2, 3)); + define_builtin_func("_<=>_", arith_bin_op, std::bind(compile_cmp_int, _1, _2, 7)); + define_builtin_func("true", Int, /* AsmOp::Const("TRUE") */ std::bind(compile_bool_const, _1, _2, true)); + define_builtin_func("false", Int, /* AsmOp::Const("FALSE") */ std::bind(compile_bool_const, _1, _2, false)); + // define_builtin_func("null", Null, AsmOp::Const("PUSHNULL")); + define_builtin_func("nil", Tuple, AsmOp::Const("PUSHNULL")); + define_builtin_func("null?", TypeExpr::new_forall({X}, TypeExpr::new_map(X, Int)), compile_is_null); + define_builtin_func("cons", TypeExpr::new_forall({X}, TypeExpr::new_map(TypeExpr::new_tensor(X, Tuple), Tuple)), + AsmOp::Custom("CONS", 2, 1)); + define_builtin_func("uncons", TypeExpr::new_forall({X}, TypeExpr::new_map(Tuple, TypeExpr::new_tensor(X, Tuple))), + AsmOp::Custom("UNCONS", 1, 2)); + define_builtin_func_x("list_next", + TypeExpr::new_forall({X}, TypeExpr::new_map(Tuple, TypeExpr::new_tensor(Tuple, X))), + AsmOp::Custom("UNCONS", 1, 2), {}, {1, 0}); + define_builtin_func("car", TypeExpr::new_forall({X}, TypeExpr::new_map(Tuple, X)), AsmOp::Custom("CAR", 1, 1)); + define_builtin_func("cdr", TypeExpr::new_map(Tuple, Tuple), AsmOp::Custom("CDR", 1, 1)); + define_builtin_func("pair", TypeExpr::new_forall({X, Y}, TypeExpr::new_map(XY, Tuple)), AsmOp::Custom("PAIR", 2, 1)); + define_builtin_func("unpair", TypeExpr::new_forall({X, Y}, TypeExpr::new_map(Tuple, XY)), + AsmOp::Custom("UNPAIR", 1, 2)); + define_builtin_func("triple", TypeExpr::new_forall({X, Y, Z}, TypeExpr::new_map(XYZ, Tuple)), + AsmOp::Custom("TRIPLE", 3, 1)); + define_builtin_func("untriple", TypeExpr::new_forall({X, Y, Z}, TypeExpr::new_map(Tuple, XYZ)), + AsmOp::Custom("UNTRIPLE", 1, 3)); + define_builtin_func("tuple4", TypeExpr::new_forall({X, Y, Z, T}, TypeExpr::new_map(XYZT, Tuple)), + AsmOp::Custom("4 TUPLE", 4, 1)); + define_builtin_func("untuple4", TypeExpr::new_forall({X, Y, Z, T}, TypeExpr::new_map(Tuple, XYZT)), + AsmOp::Custom("4 UNTUPLE", 1, 4)); + define_builtin_func("throw", impure_un_op, compile_throw, true); + define_builtin_func("throw_if", impure_bin_op, std::bind(compile_cond_throw, _1, _2, true), true); + define_builtin_func("throw_unless", impure_bin_op, std::bind(compile_cond_throw, _1, _2, false), true); + define_builtin_func_x("load_int", fetch_int_op, std::bind(compile_fetch_int, _1, _2, true, true), {}, {1, 0}); + define_builtin_func_x("load_uint", fetch_int_op, std::bind(compile_fetch_int, _1, _2, true, false), {}, {1, 0}); + define_builtin_func("preload_int", prefetch_int_op, std::bind(compile_fetch_int, _1, _2, false, true)); + define_builtin_func("preload_uint", prefetch_int_op, std::bind(compile_fetch_int, _1, _2, false, false)); + define_builtin_func_x("store_int", store_int_op, std::bind(compile_store_int, _1, _2, true), {1, 0, 2}); + define_builtin_func_x("store_uint", store_int_op, std::bind(compile_store_int, _1, _2, false), {1, 0, 2}); + define_builtin_func_x("~store_int", store_int_method, std::bind(compile_store_int, _1, _2, true), {1, 0, 2}); + define_builtin_func_x("~store_uint", store_int_method, std::bind(compile_store_int, _1, _2, false), {1, 0, 2}); + define_builtin_func_x("load_bits", fetch_slice_op, std::bind(compile_fetch_slice, _1, _2, true), {}, {1, 0}); + define_builtin_func("preload_bits", prefetch_slice_op, std::bind(compile_fetch_slice, _1, _2, false)); + define_builtin_func("int_at", TypeExpr::new_map(TupleInt, Int), compile_tuple_at); + define_builtin_func("cell_at", TypeExpr::new_map(TupleInt, Cell), compile_tuple_at); + define_builtin_func("slice_at", TypeExpr::new_map(TupleInt, Cell), compile_tuple_at); + define_builtin_func("tuple_at", TypeExpr::new_map(TupleInt, Tuple), compile_tuple_at); + define_builtin_func("at", TypeExpr::new_forall({X}, TypeExpr::new_map(TupleInt, X)), compile_tuple_at); + define_builtin_func("touch", TypeExpr::new_forall({X}, TypeExpr::new_map(X, X)), AsmOp::Nop()); + define_builtin_func("~touch", TypeExpr::new_forall({X}, TypeExpr::new_map(X, TypeExpr::new_tensor({X, Unit}))), + AsmOp::Nop()); + define_builtin_func("touch2", TypeExpr::new_forall({X, Y}, TypeExpr::new_map(XY, XY)), AsmOp::Nop()); + define_builtin_func("~touch2", TypeExpr::new_forall({X, Y}, TypeExpr::new_map(XY, TypeExpr::new_tensor({XY, Unit}))), + AsmOp::Nop()); + define_builtin_func("~dump", TypeExpr::new_forall({X}, TypeExpr::new_map(X, TypeExpr::new_tensor({X, Unit}))), + AsmOp::Custom("s0 DUMP", 1, 1)); + define_builtin_func( + "run_method0", TypeExpr::new_map(Int, Unit), + [](auto a, auto b, auto c) { return compile_run_method(a, b, c, 0, false); }, true); + define_builtin_func_x( + "run_method1", TypeExpr::new_forall({X}, TypeExpr::new_map(TypeExpr::new_tensor({Int, X}), Unit)), + [](auto a, auto b, auto c) { return compile_run_method(a, b, c, 1, false); }, {1, 0}, {}, true); + define_builtin_func_x( + "run_method2", TypeExpr::new_forall({X, Y}, TypeExpr::new_map(TypeExpr::new_tensor({Int, X, Y}), Unit)), + [](auto a, auto b, auto c) { return compile_run_method(a, b, c, 2, false); }, {1, 2, 0}, {}, true); + define_builtin_func_x( + "run_method3", TypeExpr::new_forall({X, Y, Z}, TypeExpr::new_map(TypeExpr::new_tensor({Int, X, Y, Z}), Unit)), + [](auto a, auto b, auto c) { return compile_run_method(a, b, c, 3, false); }, {1, 2, 3, 0}, {}, true); +} + +} // namespace funC diff --git a/crypto/func/codegen.cpp b/crypto/func/codegen.cpp new file mode 100644 index 00000000..0efea4d6 --- /dev/null +++ b/crypto/func/codegen.cpp @@ -0,0 +1,689 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "func.h" + +namespace funC { + +/* + * + * GENERATE TVM STACK CODE + * + */ + +StackLayout Stack::vars() const { + StackLayout res; + res.reserve(s.size()); + for (auto x : s) { + res.push_back(x.first); + } + return res; +} + +int Stack::find(var_idx_t var, int from) const { + for (int i = from; i < depth(); i++) { + if (at(i).first == var) { + return i; + } + } + return -1; +} + +// finds var in [from .. to) +int Stack::find(var_idx_t var, int from, int to) const { + for (int i = from; i < depth() && i < to; i++) { + if (at(i).first == var) { + return i; + } + } + return -1; +} + +// finds var outside [from .. to) +int Stack::find_outside(var_idx_t var, int from, int to) const { + from = std::max(from, 0); + if (from >= to) { + return find(var); + } else { + int t = find(var, 0, from); + return t >= 0 ? t : find(var, to); + } +} + +int Stack::find_const(const_idx_t cst, int from) const { + for (int i = from; i < depth(); i++) { + if (at(i).second == cst) { + return i; + } + } + return -1; +} + +void Stack::forget_const() { + for (auto& vc : s) { + if (vc.second != not_const) { + vc.second = not_const; + } + } +} + +void Stack::issue_pop(int i) { + validate(i); + o << AsmOp::Pop(i); + at(i) = get(0); + s.pop_back(); + modified(); +} + +void Stack::issue_push(int i) { + validate(i); + o << AsmOp::Push(i); + s.push_back(get(i)); + modified(); +} + +void Stack::issue_xchg(int i, int j) { + validate(i); + validate(j); + if (i != j && get(i) != get(j)) { + o << AsmOp::Xchg(i, j); + std::swap(at(i), at(j)); + modified(); + } +} + +int Stack::drop_vars_except(const VarDescrList& var_info, int excl_var) { + int dropped = 0, changes; + do { + changes = 0; + int n = depth(); + for (int i = 0; i < n; i++) { + var_idx_t idx = at(i).first; + if (((!var_info[idx] || var_info[idx]->is_unused()) && idx != excl_var) || find(idx, 0, i - 1) >= 0) { + // unneeded + issue_pop(i); + changes = 1; + break; + } + } + dropped += changes; + } while (changes); + return dropped; +} + +void Stack::show(int flags) { + std::ostringstream os; + for (auto i : s) { + os << ' '; + o.show_var_ext(os, i); + } + o << AsmOp::Comment(os.str()); + mode |= _Shown; +} + +void Stack::forget_var(var_idx_t idx) { + for (auto& x : s) { + if (x.first == idx) { + x = std::make_pair(_Garbage, not_const); + modified(); + } + } +} + +void Stack::push_new_var(var_idx_t idx) { + forget_var(idx); + s.emplace_back(idx, not_const); + modified(); +} + +void Stack::push_new_const(var_idx_t idx, const_idx_t cidx) { + forget_var(idx); + s.emplace_back(idx, cidx); + modified(); +} + +void Stack::assign_var(var_idx_t new_idx, var_idx_t old_idx) { + int i = find(old_idx); + assert(i >= 0 && "variable not found in stack"); + if (new_idx != old_idx) { + at(i).first = new_idx; + modified(); + } +} + +void Stack::do_copy_var(var_idx_t new_idx, var_idx_t old_idx) { + int i = find(old_idx); + assert(i >= 0 && "variable not found in stack"); + if (find(old_idx, i + 1) < 0) { + issue_push(i); + assert(at(0).first == old_idx); + } + assign_var(new_idx, old_idx); +} + +void Stack::enforce_state(const StackLayout& req_stack) { + int k = (int)req_stack.size(); + for (int i = 0; i < k; i++) { + var_idx_t x = req_stack[i]; + if (i < depth() && s[i].first == x) { + continue; + } + int j = find(x); + if (j >= depth() - i) { + issue_push(j); + j = 0; + } + issue_xchg(j, depth() - i - 1); + assert(s[i].first == x); + } + while (depth() > k) { + issue_pop(0); + } + assert(depth() == k); + for (int i = 0; i < k; i++) { + assert(s[i].first == req_stack[i]); + } +} + +void Stack::merge_const(const Stack& req_stack) { + assert(s.size() == req_stack.s.size()); + for (std::size_t i = 0; i < s.size(); i++) { + assert(s[i].first == req_stack.s[i].first); + if (s[i].second != req_stack.s[i].second) { + s[i].second = not_const; + } + } +} + +void Stack::merge_state(const Stack& req_stack) { + enforce_state(req_stack.vars()); + merge_const(req_stack); +} + +void Stack::rearrange_top(const StackLayout& top, std::vector last) { + while (last.size() < top.size()) { + last.push_back(false); + } + int k = (int)top.size(); + for (int i = 0; i < k; i++) { + for (int j = i + 1; j < k; j++) { + if (top[i] == top[j]) { + last[i] = false; + break; + } + } + } + int ss = 0; + for (int i = 0; i < k; i++) { + if (last[i]) { + ++ss; + } + } + for (int i = 0; i < k; i++) { + var_idx_t x = top[i]; + // find s(j) containing x with j not in [ss, ss+i) + int j = find_outside(x, ss, ss + i); + if (last[i]) { + // rearrange x to be at s(ss-1) + issue_xchg(--ss, j); + assert(get(ss).first == x); + } else { + // create a new copy of x + issue_push(j); + issue_xchg(0, ss); + assert(get(ss).first == x); + } + } + assert(!ss); +} + +void Stack::rearrange_top(var_idx_t top, bool last) { + int i = find(top); + if (last) { + issue_xchg(0, i); + } else { + issue_push(i); + } + assert(get(0).first == top); +} + +bool Op::generate_code_step(Stack& stack) { + stack.opt_show(); + stack.drop_vars_except(var_info); + stack.opt_show(); + const auto& next_var_info = next->var_info; + switch (cl) { + case _Nop: + case _Import: + return true; + case _Return: { + stack.enforce_state(left); + stack.opt_show(); + return false; + } + case _IntConst: { + auto p = next_var_info[left[0]]; + if (!p || p->is_unused()) { + return true; + } + auto cidx = stack.o.register_const(int_const); + int i = stack.find_const(cidx); + if (i < 0) { + stack.o << push_const(int_const); + stack.push_new_const(left[0], cidx); + } else { + assert(stack.at(i).second == cidx); + stack.do_copy_var(left[0], stack[i]); + } + return true; + } + case _GlobVar: { + assert(left.size() == 1); + auto p = next_var_info[left[0]]; + if (!p || p->is_unused() || disabled()) { + return true; + } + auto func = dynamic_cast(fun_ref->value); + if (func) { + std::vector res; + res.reserve(left.size()); + for (var_idx_t i : left) { + res.emplace_back(i); + } + func->compile(stack.o, res, args); // compile res := f (args) + } else { + std::string name = sym::symbols.get_name(fun_ref->sym_idx); + stack.o << AsmOp::Custom(name + " CALLDICT", (int)right.size()); + } + stack.push_new_var(left[0]); + return true; + } + case _Let: { + assert(left.size() == right.size()); + int i = 0; + std::vector active; + active.reserve(left.size()); + for (std::size_t k = 0; k < left.size(); k++) { + var_idx_t y = left[k]; // "y" = "x" + auto p = next_var_info[y]; + active.push_back(p && !p->is_unused()); + } + for (std::size_t k = 0; k < left.size(); k++) { + if (!active[k]) { + continue; + } + var_idx_t x = right[k]; // "y" = "x" + bool is_last = true; + for (std::size_t l = k + 1; l < right.size(); l++) { + if (right[l] == x && active[l]) { + is_last = false; + } + } + if (is_last) { + auto info = var_info[x]; + is_last = (info && info->is_last()); + } + if (is_last) { + stack.assign_var(--i, x); + } else { + stack.do_copy_var(--i, x); + } + } + i = 0; + for (std::size_t k = 0; k < left.size(); k++) { + if (active[k]) { + stack.assign_var(left[k], --i); + } + } + return true; + } + case _Call: + case _CallInd: { + if (disabled()) { + return true; + } + SymValFunc* func = (fun_ref ? dynamic_cast(fun_ref->value) : nullptr); + auto arg_order = (func ? func->get_arg_order() : nullptr); + auto ret_order = (func ? func->get_ret_order() : nullptr); + assert(!arg_order || arg_order->size() == right.size()); + assert(!ret_order || ret_order->size() == left.size()); + std::vector right1; + if (args.size()) { + assert(args.size() == right.size()); + for (int i = 0; i < (int)right.size(); i++) { + int j = arg_order ? arg_order->at(i) : i; + const VarDescr& arg = args.at(j); + if (!arg.is_unused()) { + assert(var_info[arg.idx] && !var_info[arg.idx]->is_unused()); + right1.push_back(arg.idx); + } + } + } else if (arg_order) { + for (int i = 0; i < (int)right.size(); i++) { + right1.push_back(right.at(arg_order->at(i))); + } + } else { + right1 = right; + } + std::vector last; + for (var_idx_t x : right1) { + last.push_back(var_info[x] && var_info[x]->is_last()); + } + stack.rearrange_top(right1, std::move(last)); + stack.opt_show(); + int k = (int)stack.depth() - (int)right1.size(); + assert(k >= 0); + for (int i = 0; i < (int)right1.size(); i++) { + if (stack.s[k + i].first != right1[i]) { + std::cerr << stack.o; + } + assert(stack.s[k + i].first == right1[i]); + } + if (cl == _CallInd) { + stack.o << exec_arg_op("CALLARGS", (int)right.size() - 1, -1, (int)right.size() - 1); + } else { + auto func = dynamic_cast(fun_ref->value); + if (func) { + std::vector res; + res.reserve(left.size()); + for (var_idx_t i : left) { + res.emplace_back(i); + } + func->compile(stack.o, res, args); // compile res := f (args) + } else { + std::string name = sym::symbols.get_name(fun_ref->sym_idx); + stack.o << AsmOp::Custom(name + " CALLDICT", (int)right.size()); + } + } + stack.s.resize(k); + for (int i = 0; i < (int)left.size(); i++) { + int j = ret_order ? ret_order->at(i) : i; + stack.push_new_var(left.at(j)); + } + return true; + } + case _If: { + if (block0->is_empty() && block1->is_empty()) { + return true; + } + var_idx_t x = left[0]; + stack.rearrange_top(x, var_info[x] && var_info[x]->is_last()); + assert(stack[0] == x); + stack.opt_show(); + stack.s.pop_back(); + stack.modified(); + if (block1->is_empty()) { + // if (left) block0; ... + if (block0->noreturn()) { + stack.o << "IFJMP:<{"; + stack.o.indent(); + Stack stack_copy{stack}; + block0->generate_code_all(stack_copy); + stack.o.undent(); + stack.o << "}>"; + return true; + } + stack.o << "IF:<{"; + stack.o.indent(); + Stack stack_copy{stack}; + block0->generate_code_all(stack_copy); + stack_copy.drop_vars_except(var_info); + stack_copy.opt_show(); + if (stack_copy == stack) { + stack.o.undent(); + stack.o << "}>"; + return true; + } + stack_copy.drop_vars_except(next->var_info); + stack_copy.opt_show(); + if (stack_copy.vars() == stack.vars()) { + stack.o.undent(); + stack.o << "}>"; + stack.merge_const(stack_copy); + return true; + } + stack.o.undent(); + stack.o << "}>ELSE<{"; + stack.o.indent(); + stack.merge_state(stack_copy); + stack.opt_show(); + stack.o.undent(); + stack.o << "}>"; + return true; + } + if (block0->is_empty()) { + // if (!left) block1; ... + if (block1->noreturn()) { + stack.o << "IFNOTJMP:<{"; + stack.o.indent(); + Stack stack_copy{stack}; + block1->generate_code_all(stack_copy); + stack.o.undent(); + stack.o << "}>"; + return true; + } + stack.o << "IFNOT:<{"; + stack.o.indent(); + Stack stack_copy{stack}; + block1->generate_code_all(stack_copy); + stack_copy.drop_vars_except(var_info); + stack_copy.opt_show(); + if (stack_copy.vars() == stack.vars()) { + stack.o.undent(); + stack.o << "}>"; + stack.merge_const(stack_copy); + return true; + } + stack_copy.drop_vars_except(next->var_info); + stack_copy.opt_show(); + if (stack_copy.vars() == stack.vars()) { + stack.o.undent(); + stack.o << "}>"; + stack.merge_const(stack_copy); + return true; + } + stack.o.undent(); + stack.o << "}>ELSE<{"; + stack.o.indent(); + stack.merge_state(stack_copy); + stack.opt_show(); + stack.o.undent(); + stack.o << "}>"; + return true; + } + if (block0->noreturn()) { + stack.o << "IFJMP:<{"; + stack.o.indent(); + Stack stack_copy{stack}; + block0->generate_code_all(stack_copy); + stack.o.undent(); + stack.o << "}>"; + return block1->generate_code_all(stack); + } + if (block1->noreturn()) { + stack.o << "IFNOTJMP:<{"; + stack.o.indent(); + Stack stack_copy{stack}; + block1->generate_code_all(stack_copy); + stack.o.undent(); + stack.o << "}>"; + return block0->generate_code_all(stack); + } + stack.o << "IF:<{"; + stack.o.indent(); + Stack stack_copy{stack}; + block0->generate_code_all(stack_copy); + stack_copy.drop_vars_except(next->var_info); + stack_copy.opt_show(); + stack.o.undent(); + stack.o << "}>ELSE<{"; + stack.o.indent(); + block1->generate_code_all(stack); + stack.merge_state(stack_copy); + stack.opt_show(); + stack.o.undent(); + stack.o << "}>"; + return true; + } + case _Repeat: { + var_idx_t x = left[0]; + //stack.drop_vars_except(block0->var_info, x); + stack.rearrange_top(x, var_info[x] && var_info[x]->is_last()); + assert(stack[0] == x); + stack.opt_show(); + stack.s.pop_back(); + stack.modified(); + if (!next->is_empty()) { + stack.o << "REPEAT:<{"; + stack.o.indent(); + stack.forget_const(); + StackLayout layout1 = stack.vars(); + block0->generate_code_all(stack); + stack.enforce_state(std::move(layout1)); + stack.opt_show(); + stack.o.undent(); + stack.o << "}>"; + return true; + } else { + stack.o << "REPEATEND"; + stack.forget_const(); + StackLayout layout1 = stack.vars(); + block0->generate_code_all(stack); + stack.enforce_state(std::move(layout1)); + stack.opt_show(); + return false; + } + } + case _Again: { + stack.drop_vars_except(block0->var_info); + stack.opt_show(); + if (!next->is_empty()) { + stack.o << "AGAIN:<{"; + stack.o.indent(); + stack.forget_const(); + StackLayout layout1 = stack.vars(); + block0->generate_code_all(stack); + stack.enforce_state(std::move(layout1)); + stack.opt_show(); + stack.o.undent(); + stack.o << "}>"; + return true; + } else { + stack.o << "AGAINEND"; + stack.forget_const(); + StackLayout layout1 = stack.vars(); + block0->generate_code_all(stack); + stack.enforce_state(std::move(layout1)); + stack.opt_show(); + return false; + } + } + case _Until: { + // stack.drop_vars_except(block0->var_info); + // stack.opt_show(); + if (!next->is_empty()) { + stack.o << "UNTIL:<{"; + stack.o.indent(); + stack.forget_const(); + auto layout1 = stack.vars(); + block0->generate_code_all(stack); + layout1.push_back(left[0]); + stack.enforce_state(std::move(layout1)); + stack.opt_show(); + stack.o.undent(); + stack.o << "}>"; + stack.s.pop_back(); + stack.modified(); + return true; + } else { + stack.o << "UNTILEND"; + stack.forget_const(); + StackLayout layout1 = stack.vars(); + block0->generate_code_all(stack); + layout1.push_back(left[0]); + stack.enforce_state(std::move(layout1)); + stack.opt_show(); + return false; + } + } + case _While: { + // while (block0 | left) block1; ...next + var_idx_t x = left[0]; + stack.drop_vars_except(block0->var_info); + stack.opt_show(); + StackLayout layout1 = stack.vars(); + bool next_empty = next->is_empty(); + stack.o << (next_empty ? "WHILEEND:<{" : "WHILE:<{"); + stack.o.indent(); + stack.forget_const(); + block0->generate_code_all(stack); + stack.rearrange_top(x, !next->var_info[x] && !block1->var_info[x]); + stack.opt_show(); + stack.s.pop_back(); + stack.modified(); + stack.o.undent(); + Stack stack_copy{stack}; + stack.o << (next_empty ? "}>" : "}>DO<{"); + if (!next_empty) { + stack.o.indent(); + } + stack_copy.opt_show(); + block1->generate_code_all(stack_copy); + stack_copy.enforce_state(std::move(layout1)); + stack_copy.opt_show(); + if (!next_empty) { + stack.o.undent(); + stack.o << "}>"; + return true; + } else { + return false; + } + } + default: + std::cerr << "fatal: unknown operation \n"; + throw src::ParseError{where, "unknown operation in generate_code()"}; + } +} + +bool Op::generate_code_all(Stack& stack) { + if (generate_code_step(stack) && next) { + return next->generate_code_all(stack); + } else { + return false; + } +} + +void CodeBlob::generate_code(AsmOpList& out, int mode) { + Stack stack{out, mode}; + assert(ops && ops->cl == Op::_Import); + for (var_idx_t x : ops->left) { + stack.push_new_var(x); + } + ops->generate_code_all(stack); + if (!(mode & Stack::_DisableOpt)) { + optimize_code(out); + } +} + +void CodeBlob::generate_code(std::ostream& os, int mode, int indent) { + AsmOpList out_list(indent, &vars); + generate_code(out_list, mode); + out_list.out(os, mode); +} + +} // namespace funC diff --git a/crypto/func/func.cpp b/crypto/func/func.cpp new file mode 100644 index 00000000..223e6fcf --- /dev/null +++ b/crypto/func/func.cpp @@ -0,0 +1,248 @@ +/* + This file is part of TON Blockchain source code. + + TON Blockchain is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + TON Blockchain is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with TON Blockchain. If not, see . + + In addition, as a special exception, the copyright holders give permission + to link the code of portions of this program with the OpenSSL library. + You must obey the GNU General Public License in all respects for all + of the code used other than OpenSSL. If you modify file(s) with this + exception, you may extend this exception to your version of the file(s), + but you are not obligated to do so. If you do not wish to do so, delete this + exception statement from your version. If you delete this exception statement + from all source files in the program, then also delete it here. + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "func.h" +#include "parser/srcread.h" +#include "parser/lexer.h" +#include "parser/symtable.h" +#include +#include + +namespace funC { + +int verbosity, indent, opt_level = 2; +bool stack_layout_comments, op_rewrite_comments, program_envelope, asm_preamble; +std::ostream* outs = &std::cout; +std::string generated_from, boc_output_filename; + +/* + * + * OUTPUT CODE GENERATOR + * + */ + +void generate_output_func(SymDef* func_sym) { + SymValCodeFunc* func_val = dynamic_cast(func_sym->value); + assert(func_val); + std::string name = sym::symbols.get_name(func_sym->sym_idx); + if (verbosity >= 2) { + std::cerr << "\n\n=========================\nfunction " << name << " : " << func_val->get_type() << std::endl; + } + if (!func_val->code) { + std::cerr << "( function `" << name << "` undefined )\n"; + } else { + CodeBlob& code = *(func_val->code); + if (verbosity >= 3) { + code.print(std::cerr, 9); + } + code.simplify_var_types(); + // std::cerr << "after simplify_var_types: \n"; code.print(std::cerr, 0); + code.prune_unreachable_code(); + // std::cerr << "after prune_unreachable: \n"; code.print(std::cerr, 0); + code.split_vars(true); + // std::cerr << "after split_vars: \n"; code.print(std::cerr, 0); + for (int i = 0; i < 8; i++) { + code.compute_used_code_vars(); + if (verbosity >= 5) { + std::cerr << "after compute_used_vars: \n"; + code.print(std::cerr, 6); + } + code.fwd_analyze(); + // std::cerr << "after fwd_analyze: \n"; code.print(std::cerr, 6); + code.prune_unreachable_code(); + // std::cerr << "after prune_unreachable: \n"; code.print(std::cerr, 6); + } + code.mark_noreturn(); + if (verbosity >= 3) { + code.print(std::cerr, 15); + } + if (verbosity >= 2) { + std::cerr << "\n---------- resulting code for " << name << " -------------\n"; + } + *outs << std::string(indent * 2, ' ') << name << " PROC:<{\n"; + code.generate_code( + *outs, + (stack_layout_comments ? Stack::_StkCmt | Stack::_CptStkCmt : 0) | (opt_level < 2 ? Stack::_DisableOpt : 0), + indent + 1); + *outs << std::string(indent * 2, ' ') << "}>\n"; + if (verbosity >= 2) { + std::cerr << "--------------\n"; + } + } +} + +int generate_output() { + if (asm_preamble) { + *outs << "\"Asm.fif\" include\n"; + } + *outs << "// automatically generated from " << generated_from << std::endl; + if (program_envelope) { + *outs << "PROGRAM{\n"; + } + for (SymDef* func_sym : glob_func) { + SymValCodeFunc* func_val = dynamic_cast(func_sym->value); + assert(func_val); + std::string name = sym::symbols.get_name(func_sym->sym_idx); + *outs << std::string(indent * 2, ' '); + if (func_val->method_id.is_null()) { + *outs << "DECLPROC " << name << "\n"; + } else { + *outs << func_val->method_id << " DECLMETHOD " << name << "\n"; + } + } + int errors = 0; + for (SymDef* func_sym : glob_func) { + try { + generate_output_func(func_sym); + } catch (src::Error& err) { + std::cerr << "cannot generate code for function `" << sym::symbols.get_name(func_sym->sym_idx) << "`:\n" + << err << std::endl; + ++errors; + } + } + if (program_envelope) { + *outs << "}END>c\n"; + } + if (!boc_output_filename.empty()) { + *outs << "2 boc+>B \"" << boc_output_filename << "\" B>file\n"; + } + return errors; +} + +} // namespace funC + +void usage(const char* progname) { + std::cerr + << "usage: " << progname + << " [-vIAPSR][-O][-i][-o][-W] { ...}\n" + "\tGenerates Fift TVM assembler code from a funC source\n" + "-I\tEnables interactive mode (parse stdin)\n" + "-o\tWrites generated code into specified file instead of stdout\n" + "-v\tIncreases verbosity level (extra information output into stderr)\n" + "-i\tSets indentation for the output code (in two-space units)\n" + "-A\tPrefix code with `\"Asm.fif\" include` preamble\n" + "-O\tSets optimization level (2 by default)\n" + "-P\tEnvelope code into PROGRAM{ ... }END>c\n" + "-S\tInclude stack layout comments in the output code\n" + "-R\tInclude operation rewrite comments in the output code\n" + "-W\tInclude Fift code to serialize and save generated code into specified BoC file. Enables " + "-A and -P.\n"; + std::exit(2); +} + +std::string output_filename; + +int main(int argc, char* const argv[]) { + int i; + bool interactive = false; + while ((i = getopt(argc, argv, "Ahi:Io:O:PRSvW:")) != -1) { + switch (i) { + case 'A': + funC::asm_preamble = true; + break; + case 'I': + interactive = true; + break; + case 'i': + funC::indent = std::max(0, atoi(optarg)); + break; + case 'o': + output_filename = optarg; + break; + case 'O': + funC::opt_level = std::max(0, atoi(optarg)); + break; + case 'P': + funC::program_envelope = true; + break; + case 'R': + funC::op_rewrite_comments = true; + break; + case 'S': + funC::stack_layout_comments = true; + break; + case 'v': + ++funC::verbosity; + break; + case 'W': + funC::boc_output_filename = optarg; + funC::asm_preamble = funC::program_envelope = true; + break; + case 'h': + default: + usage(argv[0]); + } + } + + if (funC::program_envelope && !funC::indent) { + funC::indent = 1; + } + + funC::define_keywords(); + funC::define_builtins(); + + int ok = 0, proc = 0; + try { + while (optind < argc) { + funC::generated_from += std::string{"`"} + argv[optind] + "` "; + ok += funC::parse_source_file(argv[optind++]); + proc++; + } + if (interactive) { + funC::generated_from += "stdin "; + ok += funC::parse_source_stdin(); + proc++; + } + if (ok < proc) { + throw src::Fatal{"output code generation omitted because of errors"}; + } + if (!proc) { + throw src::Fatal{"no source files, no output"}; + } + std::unique_ptr fs; + if (!output_filename.empty()) { + fs = std::make_unique(output_filename, fs->trunc | fs->out); + if (!fs->is_open()) { + std::cerr << "failed to create output file " << output_filename << '\n'; + return 2; + } + funC::outs = fs.get(); + } + funC::generate_output(); + } catch (src::Fatal& fatal) { + std::cerr << "fatal: " << fatal << std::endl; + std::exit(1); + } catch (src::Error& error) { + std::cerr << error << std::endl; + std::exit(1); + } catch (funC::UnifyError& unif_err) { + std::cerr << "fatal: "; + unif_err.print_message(std::cerr); + std::cerr << std::endl; + std::exit(1); + } +} diff --git a/crypto/func/func.h b/crypto/func/func.h new file mode 100644 index 00000000..f4b66380 --- /dev/null +++ b/crypto/func/func.h @@ -0,0 +1,1445 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once +#include +#include +#include +#include +#include +#include +#include +#include "common/refcnt.hpp" +#include "common/bigint.hpp" +#include "common/refint.h" +#include "parser/srcread.h" +#include "parser/lexer.h" +#include "parser/symtable.h" + +namespace funC { + +extern int verbosity; +extern bool op_rewrite_comments; + +constexpr int optimize_depth = 12; + +enum Keyword { + _Eof = -1, + _Ident = 0, + _Number, + _Special, + _String, + _Return = 0x80, + _Var, + _Repeat, + _Do, + _While, + _Until, + _If, + _Ifnot, + _Then, + _Else, + _Elseif, + _Elseifnot, + _Eq, + _Neq, + _Leq, + _Geq, + _Spaceship, + _Lshift, + _Rshift, + _RshiftR, + _RshiftC, + _DivR, + _DivC, + _DivMod, + _PlusLet, + _MinusLet, + _TimesLet, + _DivLet, + _DivRLet, + _DivCLet, + _ModLet, + _LshiftLet, + _RshiftLet, + _RshiftRLet, + _RshiftCLet, + _Int, + _Cell, + _Slice, + _Builder, + _Cont, + _Tuple, + _Type, + _Mapsto, + _Asm, + _Impure, + _Extern, + _MethodId, + _Operator, + _Infix, + _Infixl, + _Infixr +}; + +void define_keywords(); + +class IdSc { + int cls; + + public: + enum { undef = 0, dotid = 1, tildeid = 2 }; + IdSc(int _cls = undef) : cls(_cls) { + } + operator int() { + return cls; + } +}; + +// symbol subclass: +// 1 = begins with . (a const method) +// 2 = begins with ~ (a non-const method) +// 0 = else + +/* + * + * TYPE EXPRESSIONS + * + */ + +struct TypeExpr { + enum te_type { te_Unknown, te_Indirect, te_Atomic, te_Tensor, te_Map, te_Type, te_ForAll } constr; + enum { + _Int = Keyword::_Int, + _Cell = Keyword::_Cell, + _Slice = Keyword::_Slice, + _Builder = Keyword::_Builder, + _Cont = Keyword::_Cont, + _Tuple = Keyword::_Tuple, + _Type = Keyword::_Type + }; + int value; + int minw, maxw; + static constexpr int w_inf = 1023; + std::vector args; + TypeExpr(te_type _constr, int _val = 0) : constr(_constr), value(_val), minw(0), maxw(w_inf) { + } + TypeExpr(te_type _constr, int _val, int width) : constr(_constr), value(_val), minw(width), maxw(width) { + } + TypeExpr(te_type _constr, std::vector list) + : constr(_constr), value((int)list.size()), args(std::move(list)) { + compute_width(); + } + TypeExpr(te_type _constr, std::initializer_list list) + : constr(_constr), value((int)list.size()), args(std::move(list)) { + compute_width(); + } + TypeExpr(te_type _constr, TypeExpr* elem0, std::vector list) + : constr(_constr), value((int)list.size() + 1), args{elem0} { + args.insert(args.end(), list.begin(), list.end()); + compute_width(); + } + TypeExpr(te_type _constr, TypeExpr* elem0, std::initializer_list list) + : constr(_constr), value((int)list.size() + 1), args{elem0} { + args.insert(args.end(), list.begin(), list.end()); + compute_width(); + } + bool is_atomic() const { + return constr == te_Atomic; + } + bool is_atomic(int v) const { + return constr == te_Atomic && value == v; + } + bool is_int() const { + return is_atomic(_Int); + } + bool has_fixed_width() const { + return minw == maxw; + } + int get_width() const { + return has_fixed_width() ? minw : -1; + } + void compute_width(); + bool recompute_width(); + void show_width(std::ostream& os); + std::ostream& print(std::ostream& os, int prio = 0); + void replace_with(TypeExpr* te2); + int extract_components(std::vector& comp_list); + static int holes, type_vars; + static TypeExpr* new_hole() { + return new TypeExpr{te_Unknown, ++holes}; + } + static TypeExpr* new_hole(int width) { + return new TypeExpr{te_Unknown, ++holes, width}; + } + static TypeExpr* new_unit() { + return new TypeExpr{te_Tensor, 0, 0}; + } + static TypeExpr* new_atomic(int value) { + return new TypeExpr{te_Atomic, value, 1}; + } + static TypeExpr* new_map(TypeExpr* from, TypeExpr* to); + static TypeExpr* new_func() { + return new_map(new_hole(), new_hole()); + } + static TypeExpr* new_tensor(std::vector list, bool red = true) { + return red && list.size() == 1 ? list[0] : new TypeExpr{te_Tensor, std::move(list)}; + } + static TypeExpr* new_tensor(std::initializer_list list) { + return new TypeExpr{te_Tensor, std::move(list)}; + } + static TypeExpr* new_tensor(TypeExpr* te1, TypeExpr* te2) { + return new_tensor({te1, te2}); + } + static TypeExpr* new_tensor(TypeExpr* te1, TypeExpr* te2, TypeExpr* te3) { + return new_tensor({te1, te2, te3}); + } + static TypeExpr* new_var() { + return new TypeExpr{te_Unknown, --type_vars, 1}; + } + static TypeExpr* new_forall(std::vector list, TypeExpr* body) { + return new TypeExpr{te_ForAll, body, std::move(list)}; + } + static TypeExpr* new_forall(std::initializer_list list, TypeExpr* body) { + return new TypeExpr{te_ForAll, body, std::move(list)}; + } + static bool remove_indirect(TypeExpr*& te, TypeExpr* forbidden = nullptr); + static bool remove_forall(TypeExpr*& te); + static bool remove_forall_in(TypeExpr*& te, TypeExpr* te2, const std::vector& new_vars); +}; + +std::ostream& operator<<(std::ostream& os, TypeExpr* type_expr); + +struct UnifyError { + TypeExpr* te1; + TypeExpr* te2; + std::string msg; + UnifyError(TypeExpr* _te1, TypeExpr* _te2, std::string _msg = "") : te1(_te1), te2(_te2), msg(_msg) { + } + void print_message(std::ostream& os) const; + std::string message() const; +}; + +std::ostream& operator<<(std::ostream& os, const UnifyError& ue); + +void unify(TypeExpr*& te1, TypeExpr*& te2); + +// extern int TypeExpr::holes; + +/* + * + * ABSTRACT CODE + * + */ + +using src::Lexem; +using src::SrcLocation; +using sym::SymDef; +using sym::sym_idx_t; +using sym::var_idx_t; +using const_idx_t = int; + +struct TmpVar { + TypeExpr* v_type; + var_idx_t idx; + enum { _In = 1, _Named = 2, _Tmp = 4, _UniqueName = 0x20 }; + int cls; + sym_idx_t name; + int coord; + std::unique_ptr where; + TmpVar(var_idx_t _idx, int _cls, TypeExpr* _type = 0, SymDef* sym = 0, const SrcLocation* loc = 0); + void show(std::ostream& os, int omit_idx = 0) const; + void dump(std::ostream& os) const; + void set_location(const SrcLocation& loc); +}; + +struct VarDescr { + var_idx_t idx; + enum { _Last = 1, _Unused = 2 }; + int flags; + enum { + _Const = 16, + _Int = 32, + _Zero = 64, + _NonZero = 128, + _Pos = 256, + _Neg = 512, + _Bool = 1024, + _Bit = 2048, + _Finite = 4096, + _Nan = 8192, + _Even = 16384, + _Odd = 32768, + _Null = (1 << 16), + _NotNull = (1 << 17) + }; + static constexpr int ConstZero = _Int | _Zero | _Pos | _Neg | _Bool | _Bit | _Finite | _Even | _NotNull; + static constexpr int ConstOne = _Int | _NonZero | _Pos | _Bit | _Finite | _Odd | _NotNull; + static constexpr int ConstTrue = _Int | _NonZero | _Neg | _Bool | _Finite | _Odd | _NotNull; + static constexpr int ValBit = ConstZero & ConstOne; + static constexpr int ValBool = ConstZero & ConstTrue; + static constexpr int FiniteInt = _Int | _Finite | _NotNull; + static constexpr int FiniteUInt = FiniteInt | _Pos; + int val; + td::RefInt256 int_const; + VarDescr(var_idx_t _idx = -1, int _flags = 0, int _val = 0) : idx(_idx), flags(_flags), val(_val) { + } + bool operator<(var_idx_t other_idx) const { + return idx < other_idx; + } + bool is_unused() const { + return flags & _Unused; + } + bool is_last() const { + return flags & _Last; + } + bool always_true() const { + return val & _NonZero; + } + bool always_false() const { + return val & _Zero; + } + bool always_nonzero() const { + return val & _NonZero; + } + bool always_zero() const { + return val & _Zero; + } + bool always_even() const { + return val & _Even; + } + bool always_odd() const { + return val & _Odd; + } + bool always_null() const { + return val & _Null; + } + bool always_not_null() const { + return val & _NotNull; + } + bool is_const() const { + return val & _Const; + } + bool is_int_const() const { + return (val & (_Int | _Const)) == (_Int | _Const); + } + bool always_nonpos() const { + return val & _Neg; + } + bool always_nonneg() const { + return val & _Pos; + } + bool always_pos() const { + return (val & (_Pos | _NonZero)) == (_Pos | _NonZero); + } + bool always_neg() const { + return (val & (_Neg | _NonZero)) == (_Neg | _NonZero); + } + bool always_finite() const { + return val & _Finite; + } + bool always_less(const VarDescr& other) const; + bool always_leq(const VarDescr& other) const; + bool always_greater(const VarDescr& other) const; + bool always_geq(const VarDescr& other) const; + bool always_equal(const VarDescr& other) const; + bool always_neq(const VarDescr& other) const; + void unused() { + flags |= _Unused; + } + void clear_unused() { + flags &= ~_Unused; + } + void set_const(long long value); + void set_const(td::RefInt256 value); + void set_const_nan(); + void operator+=(const VarDescr& y) { + flags &= y.flags; + } + void operator|=(const VarDescr& y); + void operator&=(const VarDescr& y); + void set_value(const VarDescr& y); + void set_value(VarDescr&& y); + void set_value(const VarDescr* y) { + if (y) { + set_value(*y); + } + } + void clear_value(); + void show_value(std::ostream& os) const; + void show(std::ostream& os, const char* var_name = nullptr) const; +}; + +inline std::ostream& operator<<(std::ostream& os, const VarDescr& vd) { + vd.show(os); + return os; +} + +struct VarDescrList { + std::vector list; + VarDescrList() : list() { + } + VarDescrList(const std::vector& _list) : list(_list) { + } + VarDescrList(std::vector&& _list) : list(std::move(_list)) { + } + std::size_t size() const { + return list.size(); + } + VarDescr* operator[](var_idx_t idx); + const VarDescr* operator[](var_idx_t idx) const; + VarDescrList operator+(const VarDescrList& y) const; + VarDescrList& operator+=(const VarDescrList& y); + VarDescrList& clear_last(); + VarDescrList& operator+=(var_idx_t idx) { + return add_var(idx); + } + VarDescrList& operator+=(const std::vector& idx_list) { + return add_vars(idx_list); + } + VarDescrList& add_var(var_idx_t idx, bool unused = false); + VarDescrList& add_vars(const std::vector& idx_list, bool unused = false); + VarDescrList& operator-=(const std::vector& idx_list); + VarDescrList& operator-=(var_idx_t idx); + std::size_t count(const std::vector idx_list) const; + std::size_t count_used(const std::vector idx_list) const; + VarDescr& add(var_idx_t idx); + VarDescr& add_newval(var_idx_t idx); + VarDescrList& operator&=(const VarDescrList& values); + VarDescrList& import_values(const VarDescrList& values); + VarDescrList operator|(const VarDescrList& y) const; + VarDescrList& operator|=(const VarDescrList& values); + void show(std::ostream& os) const; +}; + +inline std::ostream& operator<<(std::ostream& os, const VarDescrList& values) { + values.show(os); + return os; +} + +struct CodeBlob; + +template +class ListIterator { + T* ptr; + + public: + ListIterator() : ptr(nullptr) { + } + ListIterator(T* _ptr) : ptr(_ptr) { + } + ListIterator& operator++() { + ptr = ptr->next.get(); + return *this; + } + ListIterator& operator++(int) { + T* z = ptr; + ptr = ptr->next.get(); + return ListIterator{z}; + } + T& operator*() const { + return *ptr; + } + T* operator->() const { + return ptr; + } + bool operator==(const ListIterator& y) const { + return ptr == y.ptr; + } + bool operator!=(const ListIterator& y) const { + return ptr != y.ptr; + } +}; + +struct Stack; + +struct Op { + enum { + _Undef, + _Nop, + _Call, + _CallInd, + _Let, + _IntConst, + _GlobVar, + _Import, + _Return, + _If, + _While, + _Until, + _Repeat, + _Again + }; + int cl; + enum { _Disabled = 1, _Reachable = 2, _NoReturn = 4, _ImpureR = 8, _ImpureW = 16, _Impure = 24 }; + int flags; + std::unique_ptr next; + SymDef* fun_ref; + SrcLocation where; + VarDescrList var_info; + std::vector args; + std::vector left, right; + std::unique_ptr block0, block1; + td::RefInt256 int_const; + Op(const SrcLocation& _where = {}, int _cl = _Undef) : cl(_cl), flags(0), fun_ref(nullptr), where(_where) { + } + Op(const SrcLocation& _where, int _cl, const std::vector& _left) + : cl(_cl), flags(0), fun_ref(nullptr), where(_where), left(_left) { + } + Op(const SrcLocation& _where, int _cl, std::vector&& _left) + : cl(_cl), flags(0), fun_ref(nullptr), where(_where), left(std::move(_left)) { + } + Op(const SrcLocation& _where, int _cl, const std::vector& _left, td::RefInt256 _const) + : cl(_cl), flags(0), fun_ref(nullptr), where(_where), left(_left), int_const(_const) { + } + Op(const SrcLocation& _where, int _cl, const std::vector& _left, const std::vector& _right, + SymDef* _fun = nullptr) + : cl(_cl), flags(0), fun_ref(_fun), where(_where), left(_left), right(_right) { + } + Op(const SrcLocation& _where, int _cl, std::vector&& _left, std::vector&& _right, + SymDef* _fun = nullptr) + : cl(_cl), flags(0), fun_ref(_fun), where(_where), left(std::move(_left)), right(std::move(_right)) { + } + bool disabled() const { + return flags & _Disabled; + } + bool enabled() const { + return !disabled(); + } + void disable() { + flags |= _Disabled; + } + bool unreachable() { + return !(flags & _Reachable); + } + void flags_set_clear(int set, int clear); + void show(std::ostream& os, const std::vector& vars, std::string pfx = "", int mode = 0) const; + void show_var_list(std::ostream& os, const std::vector& idx_list, const std::vector& vars) const; + void show_var_list(std::ostream& os, const std::vector& list, const std::vector& vars) const; + static void show_block(std::ostream& os, const Op* block, const std::vector& vars, std::string pfx = "", + int mode = 0); + void split_vars(const std::vector& vars); + static void split_var_list(std::vector& var_list, const std::vector& vars); + bool compute_used_vars(const CodeBlob& code, bool edit); + bool std_compute_used_vars(bool disabled = false); + bool set_var_info(const VarDescrList& new_var_info); + bool set_var_info(VarDescrList&& new_var_info); + bool set_var_info_except(const VarDescrList& new_var_info, const std::vector& var_list); + bool set_var_info_except(VarDescrList&& new_var_info, const std::vector& var_list); + void prepare_args(VarDescrList values); + VarDescrList fwd_analyze(VarDescrList values); + bool set_noreturn(bool nr); + bool mark_noreturn(); + bool noreturn() const { + return flags & _NoReturn; + } + bool is_empty() const { + return cl == _Nop && !next; + } + bool is_pure() const { + return !(flags & _Impure); + } + bool generate_code_step(Stack& stack); + bool generate_code_all(Stack& stack); + Op& last() { + return next ? next->last() : *this; + } + const Op& last() const { + return next ? next->last() : *this; + } + ListIterator begin() { + return ListIterator{this}; + } + ListIterator end() const { + return ListIterator{}; + } + ListIterator cbegin() { + return ListIterator{this}; + } + ListIterator cend() const { + return ListIterator{}; + } +}; + +inline ListIterator begin(const std::unique_ptr& op_list) { + return ListIterator{op_list.get()}; +} + +inline ListIterator end(const std::unique_ptr& op_list) { + return ListIterator{}; +} + +inline ListIterator cbegin(const Op* op_list) { + return ListIterator{op_list}; +} + +inline ListIterator cend(const Op* op_list) { + return ListIterator{}; +} + +inline ListIterator begin(const Op* op_list) { + return ListIterator{op_list}; +} + +inline ListIterator end(const Op* op_list) { + return ListIterator{}; +} + +inline ListIterator begin(Op* op_list) { + return ListIterator{op_list}; +} + +inline ListIterator end(Op* op_list) { + return ListIterator{}; +} + +typedef std::tuple FormalArg; +typedef std::vector FormalArgList; + +struct AsmOpList; + +struct CodeBlob { + int var_cnt, in_var_cnt, op_cnt; + TypeExpr* ret_type; + std::string name; + SrcLocation loc; + std::vector vars; + std::unique_ptr ops; + std::unique_ptr* cur_ops; + std::stack*> cur_ops_stack; + CodeBlob(TypeExpr* ret = nullptr) : var_cnt(0), in_var_cnt(0), op_cnt(0), ret_type(ret), cur_ops(&ops) { + } + template + Op& emplace_back(const Args&... args) { + Op& res = *(*cur_ops = std::make_unique(args...)); + cur_ops = &(res.next); + return res; + } + bool import_params(FormalArgList arg_list); + var_idx_t create_var(int cls, TypeExpr* var_type = 0, SymDef* sym = 0, const SrcLocation* loc = 0); + int split_vars(bool strict = false); + bool compute_used_code_vars(); + bool compute_used_code_vars(std::unique_ptr& ops, const VarDescrList& var_info, bool edit) const; + void print(std::ostream& os, int flags = 0) const; + void push_set_cur(std::unique_ptr& new_cur_ops) { + cur_ops_stack.push(cur_ops); + cur_ops = &new_cur_ops; + } + void close_blk(const SrcLocation& location) { + *cur_ops = std::make_unique(location, Op::_Nop); + } + void pop_cur() { + cur_ops = cur_ops_stack.top(); + cur_ops_stack.pop(); + } + void close_pop_cur(const SrcLocation& location) { + close_blk(location); + pop_cur(); + } + void simplify_var_types(); + void flags_set_clear(int set, int clear); + void prune_unreachable_code(); + void fwd_analyze(); + void mark_noreturn(); + void generate_code(AsmOpList& out_list, int mode = 0); + void generate_code(std::ostream& os, int mode = 0, int indent = 0); +}; + +/* + * + * SYMBOL VALUES + * + */ + +struct SymVal : sym::SymValBase { + TypeExpr* sym_type; + td::RefInt256 method_id; + bool impure; + SymVal(int _type, int _idx, TypeExpr* _stype = nullptr, bool _impure = false) + : sym::SymValBase(_type, _idx), sym_type(_stype), impure(_impure) { + } + ~SymVal() override = default; + TypeExpr* get_type() const { + return sym_type; + } + virtual const std::vector* get_arg_order() const { + return nullptr; + } + virtual const std::vector* get_ret_order() const { + return nullptr; + } +}; + +struct SymValFunc : SymVal { + std::vector arg_order, ret_order; + ~SymValFunc() override = default; + SymValFunc(int val, TypeExpr* _ft, bool _impure = false) : SymVal(_Func, val, _ft, _impure) { + } + SymValFunc(int val, TypeExpr* _ft, std::initializer_list _arg_order, std::initializer_list _ret_order = {}, + bool _impure = false) + : SymVal(_Func, val, _ft, _impure), arg_order(_arg_order), ret_order(_ret_order) { + } + + const std::vector* get_arg_order() const override { + return arg_order.empty() ? nullptr : &arg_order; + } + const std::vector* get_ret_order() const override { + return ret_order.empty() ? nullptr : &ret_order; + } +}; + +struct SymValCodeFunc : SymValFunc { + CodeBlob* code; + ~SymValCodeFunc() override = default; + SymValCodeFunc(int val, TypeExpr* _ft, bool _impure = false) : SymValFunc(val, _ft, _impure), code(nullptr) { + } +}; + +extern int glob_func_cnt, undef_func_cnt; +extern std::vector glob_func; + +/* + * + * PARSE SOURCE + * + */ + +// defined in parse-func.cpp +bool parse_source(std::istream* is, const src::FileDescr* fdescr); +bool parse_source_file(const char* filename); +bool parse_source_stdin(); + +/* + * + * EXPRESSIONS + * + */ + +struct Expr { + enum { + _None, + _Apply, + _VarApply, + _TypeApply, + _Tuple, + _Const, + _Var, + _Glob, + _Letop, + _LetFirst, + _Hole, + _Type, + _CondExpr + }; + int cls; + int val{0}; + enum { _IsType = 1, _IsRvalue = 2, _IsLvalue = 4, _IsHole = 8, _IsNewVar = 16, _IsImpure = 32 }; + int flags{0}; + SrcLocation here; + td::RefInt256 intval; + SymDef* sym{nullptr}; + TypeExpr* e_type{nullptr}; + std::vector args; + Expr(int c = _None) : cls(c) { + } + Expr(int c, const SrcLocation& loc) : cls(c), here(loc) { + } + Expr(int c, std::vector _args) : cls(c), args(std::move(_args)) { + } + Expr(int c, std::initializer_list _arglist) : cls(c), args(std::move(_arglist)) { + } + Expr(int c, SymDef* _sym, std::initializer_list _arglist) : cls(c), sym(_sym), args(std::move(_arglist)) { + } + Expr(int c, SymDef* _sym, std::vector _arglist) : cls(c), sym(_sym), args(std::move(_arglist)) { + } + Expr(int c, sym_idx_t name_idx, std::initializer_list _arglist); + ~Expr() { + for (auto& arg_ptr : args) { + delete arg_ptr; + } + } + Expr* copy() const; + void pb_arg(Expr* expr) { + args.push_back(expr); + } + void set_val(int _val) { + val = _val; + } + bool is_rvalue() const { + return flags & _IsRvalue; + } + bool is_lvalue() const { + return flags & _IsLvalue; + } + bool is_type() const { + return flags & _IsType; + } + void chk_rvalue(const Lexem& lem) const; + void chk_lvalue(const Lexem& lem) const; + void chk_type(const Lexem& lem) const; + bool deduce_type(const Lexem& lem); + void set_location(const SrcLocation& loc) { + here = loc; + } + const SrcLocation& get_location() const { + return here; + } + int define_new_vars(CodeBlob& code); + int predefine_vars(); + std::vector pre_compile(CodeBlob& code) const; +}; + +/* + * + * GENERATE CODE + * + */ + +typedef std::vector StackLayout; +typedef std::pair var_const_idx_t; +typedef std::vector StackLayoutExt; +constexpr const_idx_t not_const = -1; +using Const = td::RefInt256; + +struct AsmOp { + enum Type { a_none, a_xchg, a_push, a_pop, a_const, a_custom, a_magic }; + int t{a_none}; + int indent{0}; + int a, b, c; + std::string op; + struct SReg { + int idx; + SReg(int _idx) : idx(_idx) { + } + }; + AsmOp() = default; + AsmOp(int _t) : t(_t) { + } + AsmOp(int _t, std::string _op) : t(_t), op(std::move(_op)) { + } + AsmOp(int _t, int _a) : t(_t), a(_a) { + } + AsmOp(int _t, int _a, std::string _op) : t(_t), a(_a), op(std::move(_op)) { + } + AsmOp(int _t, int _a, int _b) : t(_t), a(_a), b(_b) { + } + AsmOp(int _t, int _a, int _b, std::string _op) : t(_t), a(_a), b(_b), op(std::move(_op)) { + } + AsmOp(int _t, int _a, int _b, int _c) : t(_t), a(_a), b(_b), c(_c) { + } + AsmOp(int _t, int _a, int _b, int _c, std::string _op) : t(_t), a(_a), b(_b), c(_c), op(std::move(_op)) { + } + void out(std::ostream& os) const; + void out_indent_nl(std::ostream& os, bool no_nl = false) const; + std::string to_string() const; + bool is_nop() const { + return t == a_none && op.empty(); + } + bool is_comment() const { + return t == a_none && !op.empty(); + } + bool is_custom() const { + return t == a_custom; + } + bool is_very_custom() const { + return is_custom() && a >= 255; + } + bool is_push() const { + return t == a_push; + } + bool is_push(int x) const { + return is_push() && a == x; + } + bool is_push(int* x) const { + *x = a; + return is_push(); + } + bool is_pop() const { + return t == a_pop; + } + bool is_pop(int x) const { + return is_pop() && a == x; + } + bool is_xchg() const { + return t == a_xchg; + } + bool is_xchg(int x, int y) const { + return is_xchg() && b == y && a == x; + } + bool is_xchg(int* x, int* y) const { + *x = a; + *y = b; + return is_xchg(); + } + bool is_swap() const { + return is_xchg(0, 1); + } + bool is_const() const { + return t == a_const && !a && b == 1; + } + bool is_gconst() const { + return (t == a_const || t == a_custom) && !a && b == 1; + } + static AsmOp Nop() { + return AsmOp(a_none); + } + static AsmOp Xchg(int a, int b = 0) { + return a == b ? AsmOp(a_none) : (a < b ? AsmOp(a_xchg, a, b) : AsmOp(a_xchg, b, a)); + } + static AsmOp Push(int a) { + return AsmOp(a_push, a); + } + static AsmOp Pop(int a = 0) { + return AsmOp(a_pop, a); + } + static AsmOp Xchg2(int a, int b) { + return make_stk2(a, b, "XCHG2", 0); + } + static AsmOp XcPu(int a, int b) { + return make_stk2(a, b, "XCPU", 1); + } + static AsmOp PuXc(int a, int b) { + return make_stk2(a, b, "PUXC", 1); + } + static AsmOp Push2(int a, int b) { + return make_stk2(a, b, "PUSH2", 2); + } + static AsmOp Xchg3(int a, int b, int c) { + return make_stk3(a, b, c, "XCHG3", 0); + } + static AsmOp Xc2Pu(int a, int b, int c) { + return make_stk3(a, b, c, "XC2PU", 1); + } + static AsmOp XcPuXc(int a, int b, int c) { + return make_stk3(a, b, c, "XCPUXC", 1); + } + static AsmOp XcPu2(int a, int b, int c) { + return make_stk3(a, b, c, "XCPU2", 3); + } + static AsmOp PuXc2(int a, int b, int c) { + return make_stk3(a, b, c, "PUXC2", 3); + } + static AsmOp PuXcPu(int a, int b, int c) { + return make_stk3(a, b, c, "PUXCPU", 3); + } + static AsmOp Pu2Xc(int a, int b, int c) { + return make_stk3(a, b, c, "PU2XC", 3); + } + static AsmOp Push3(int a, int b, int c) { + return make_stk3(a, b, c, "PUSH3", 3); + } + static AsmOp BlkSwap(int a, int b); + static AsmOp BlkPush(int a, int b); + static AsmOp BlkDrop(int a); + static AsmOp BlkReverse(int a, int b); + static AsmOp make_stk2(int a, int b, const char* str, int delta); + static AsmOp make_stk3(int a, int b, int c, const char* str, int delta); + static AsmOp IntConst(td::RefInt256 value); + static AsmOp Const(std::string push_op) { + return AsmOp(a_const, 0, 1, std::move(push_op)); + } + static AsmOp Const(int arg, std::string push_op); + static AsmOp Comment(std::string comment) { + return AsmOp(a_none, std::string{"// "} + comment); + } + static AsmOp Custom(std::string custom_op) { + return AsmOp(a_custom, 255, 255, custom_op); + } + static AsmOp Parse(std::string custom_op); + static AsmOp Custom(std::string custom_op, int args, int retv = 1) { + return AsmOp(a_custom, args, retv, custom_op); + } + static AsmOp Parse(std::string custom_op, int args, int retv = 1); +}; + +inline std::ostream& operator<<(std::ostream& os, const AsmOp& op) { + op.out(os); + return os; +} + +std::ostream& operator<<(std::ostream& os, AsmOp::SReg stack_reg); + +struct AsmOpList { + std::vector list_; + int indent_{0}; + const std::vector* var_names_{nullptr}; + std::vector constants_; + void out(std::ostream& os, int mode = 0) const; + AsmOpList(int indent = 0, const std::vector* var_names = nullptr) : indent_(indent), var_names_(var_names) { + } + template + AsmOpList& add(Args&&... args) { + list_.emplace_back(std::forward(args)...); + adjust_last(); + return *this; + } + bool append(const AsmOp& op) { + list_.push_back(op); + adjust_last(); + return true; + } + bool append(const std::vector& ops); + bool append(std::initializer_list ops) { + return append(std::vector(std::move(ops))); + } + AsmOpList& operator<<(const AsmOp& op) { + return add(op); + } + AsmOpList& operator<<(AsmOp&& op) { + return add(std::move(op)); + } + AsmOpList& operator<<(std::string str) { + return add(AsmOp::Type::a_custom, 255, 255, str); + } + const_idx_t register_const(Const new_const); + Const get_const(const_idx_t idx); + void show_var(std::ostream& os, var_idx_t idx) const; + void show_var_ext(std::ostream& os, std::pair idx_pair) const; + void adjust_last() { + if (list_.back().is_nop()) { + list_.pop_back(); + } else { + list_.back().indent = indent_; + } + } + void indent() { + ++indent_; + } + void undent() { + --indent_; + } + void set_indent(int new_indent) { + indent_ = new_indent; + } +}; + +inline std::ostream& operator<<(std::ostream& os, const AsmOpList& op_list) { + op_list.out(os); + return os; +} + +class IndentGuard { + AsmOpList& aol_; + + public: + IndentGuard(AsmOpList& aol) : aol_(aol) { + aol.indent(); + } + ~IndentGuard() { + aol_.undent(); + } +}; + +struct AsmOpCons { + std::unique_ptr car; + std::unique_ptr cdr; + AsmOpCons(std::unique_ptr head, std::unique_ptr tail) : car(std::move(head)), cdr(std::move(tail)) { + } + static std::unique_ptr cons(std::unique_ptr head, std::unique_ptr tail) { + return std::make_unique(std::move(head), std::move(tail)); + } +}; + +using AsmOpConsList = std::unique_ptr; + +int is_pos_pow2(td::RefInt256 x); +int is_neg_pow2(td::RefInt256 x); + +/* + * + * STACK TRANSFORMS + * + */ + +/* +A stack transform is a map f:N={0,1,...} -> N, such that f(x) = x + d_f for almost all x:N and for a fixed d_f:N. +They form a monoid under composition: (fg)(x)=f(g(x)). +They act on stacks S on the right: Sf=S', such that S'[n]=S[f(n)]. + +A stack transform f is determined by d_f and the finite set A of all pairs (x,y), such that x>=d_f, f(x-d_f) = y and y<>x. They are listed in increasing order by x. +*/ +struct StackTransform { + enum { max_n = 16, inf_x = 0x7fffffff, c_start = -1000 }; + int d{0}, n{0}, dp{0}, c{0}; + bool invalid{false}; + std::array, max_n> A; + StackTransform() = default; + // list of f(0),f(1),...,f(s); assumes next values are f(s)+1,f(s)+2,... + StackTransform(std::initializer_list list); + StackTransform& operator=(std::initializer_list list); + bool assign(const StackTransform& other); + static StackTransform id() { + return {}; + } + bool invalidate() { + invalid = true; + return false; + } + bool is_valid() const { + return !invalid; + } + bool set_id() { + d = n = dp = c = 0; + invalid = false; + return true; + } + bool shift(int offs) { // post-composes with x -> x + offs + d += offs; + return offs <= 0 || remove_negative(); + } + bool remove_negative(); + bool touch(int i) { + dp = std::max(dp, i + d + 1); + return true; + } + bool is_permutation() const; // is f:N->N bijective ? + bool is_trivial_after(int x) const; // f(x') = x' + d for all x' >= x + int preimage_count(int y) const; // card f^{-1}(y) + std::vector preimage(int y) const; + bool apply_xchg(int i, int j, bool relaxed = false); + bool apply_push(int i); + bool apply_pop(int i = 0); + bool apply_push_newconst(); + bool apply(const StackTransform& other); // this = this * other + bool preapply(const StackTransform& other); // this = other * this + // c := a * b + static bool compose(const StackTransform& a, const StackTransform& b, StackTransform& c); + StackTransform& operator*=(const StackTransform& other); + StackTransform operator*(const StackTransform& b) const &; + bool equal(const StackTransform& other, bool relaxed = false) const; + bool almost_equal(const StackTransform& other) const { + return equal(other, true); + } + bool operator==(const StackTransform& other) const { + return dp == other.dp && almost_equal(other); + } + bool operator<=(const StackTransform& other) const { + return dp <= other.dp && almost_equal(other); + } + bool operator>=(const StackTransform& other) const { + return dp >= other.dp && almost_equal(other); + } + int get(int i) const; + int touch_get(int i, bool relaxed = false) { + if (!relaxed) { + touch(i); + } + return get(i); + } + bool set(int i, int v, bool relaxed = false); + int operator()(int i) const { + return get(i); + } + class Pos { + StackTransform& t_; + int p_; + + public: + Pos(StackTransform& t, int p) : t_(t), p_(p) { + } + Pos& operator=(const Pos& other) = delete; + operator int() const { + return t_.get(p_); + } + const Pos& operator=(int v) const { + t_.set(p_, v); + return *this; + } + }; + Pos operator[](int i) { + return Pos(*this, i); + } + static const StackTransform rot; + static const StackTransform rot_rev; + bool is_id() const { + return is_valid() && !d && !n; + } + bool is_xchg(int i, int j) const; + bool is_xchg(int* i, int* j) const; + bool is_push(int i) const; + bool is_push(int* i) const; + bool is_pop(int i) const; + bool is_pop(int* i) const; + bool is_rot() const; + bool is_rotrev() const; + bool is_xchg2(int i, int j) const; + bool is_xchg2(int* i, int* j) const; + bool is_xcpu(int i, int j) const; + bool is_xcpu(int* i, int* j) const; + bool is_puxc(int i, int j) const; + bool is_puxc(int* i, int* j) const; + bool is_push2(int i, int j) const; + bool is_push2(int* i, int* j) const; + bool is_xchg3(int* i, int* j, int* k) const; + bool is_xc2pu(int* i, int* j, int* k) const; + bool is_xcpuxc(int* i, int* j, int* k) const; + bool is_xcpu2(int* i, int* j, int* k) const; + bool is_puxc2(int i, int j, int k) const; + bool is_puxc2(int* i, int* j, int* k) const; + bool is_puxcpu(int* i, int* j, int* k) const; + bool is_pu2xc(int i, int j, int k) const; + bool is_pu2xc(int* i, int* j, int* k) const; + bool is_push3(int i, int j, int k) const; + bool is_push3(int* i, int* j, int* k) const; + bool is_blkswap(int i, int j) const; + bool is_blkswap(int* i, int* j) const; + bool is_blkpush(int i, int j) const; + bool is_blkpush(int* i, int* j) const; + bool is_blkdrop(int* i) const; + bool is_reverse(int i, int j) const; + bool is_reverse(int* i, int* j) const; + bool is_nip_seq(int i, int j = 0) const; + bool is_nip_seq(int* i) const; + bool is_nip_seq(int* i, int* j) const; + + void show(std::ostream& os, int mode = 0) const; + + static StackTransform Xchg(int i, int j, bool relaxed = false); + static StackTransform Push(int i); + static StackTransform Pop(int i); + + private: + int try_load(int& i, int offs = 0) const; // returns A[i++].first + offs or inf_x + bool try_store(int x, int y); // appends (x,y) to A +}; + +//extern const StackTransform StackTransform::rot, StackTransform::rot_rev; + +inline std::ostream& operator<<(std::ostream& os, const StackTransform& trans) { + trans.show(os); + return os; +} + +bool apply_op(StackTransform& trans, const AsmOp& op); + +/* + * + * STACK OPERATION OPTIMIZER + * + */ + +struct Optimizer { + enum { n = optimize_depth }; + AsmOpConsList code_; + int l_{0}, l2_{0}, p_, pb_, q_, indent_; + bool debug_{false}; + std::unique_ptr op_[n], oq_[n]; + AsmOpCons* op_cons_[n]; + int offs_[n]; + StackTransform tr_[n]; + Optimizer() { + } + Optimizer(bool debug) : debug_(debug) { + } + Optimizer(AsmOpConsList code, bool debug = false) : Optimizer(debug) { + set_code(std::move(code)); + } + void set_code(AsmOpConsList code_); + void unpack(); + void pack(); + void apply(); + bool find_at_least(int pb); + bool find(); + bool optimize(); + bool compute_stack_transforms(); + bool say(std::string str) const; + bool show_stack_transforms() const; + void show_head() const; + void show_left() const; + void show_right() const; + bool is_const_push_swap() const; + bool rewrite_const_push_swap(); + bool is_const_push_xchgs(); + bool rewrite_const_push_xchgs(); + bool simple_rewrite(int p, AsmOp&& new_op); + bool simple_rewrite(int p, AsmOp&& new_op1, AsmOp&& new_op2); + bool simple_rewrite(AsmOp&& new_op) { + return simple_rewrite(p_, std::move(new_op)); + } + bool simple_rewrite(AsmOp&& new_op1, AsmOp&& new_op2) { + return simple_rewrite(p_, std::move(new_op1), std::move(new_op2)); + } + bool simple_rewrite_nop(); + bool is_pred(const std::function& pred, int min_p = 2); + bool is_same_as(const StackTransform& trans, int min_p = 2); + bool is_rot(); + bool is_rotrev(); + bool is_tuck(); + bool is_2dup(); + bool is_2drop(); + bool is_2swap(); + bool is_2over(); + bool is_xchg(int* i, int* j); + bool is_push(int* i); + bool is_pop(int* i); + bool is_nop(); + bool is_xchg2(int* i, int* j); + bool is_xcpu(int* i, int* j); + bool is_puxc(int* i, int* j); + bool is_push2(int* i, int* j); + bool is_xchg3(int* i, int* j, int* k); + bool is_xc2pu(int* i, int* j, int* k); + bool is_xcpuxc(int* i, int* j, int* k); + bool is_xcpu2(int* i, int* j, int* k); + bool is_puxc2(int* i, int* j, int* k); + bool is_puxcpu(int* i, int* j, int* k); + bool is_pu2xc(int* i, int* j, int* k); + bool is_push3(int* i, int* j, int* k); + bool is_blkswap(int* i, int* j); + bool is_blkpush(int* i, int* j); + bool is_blkdrop(int* i); + bool is_reverse(int* i, int* j); + bool is_nip_seq(int* i, int* j); + AsmOpConsList extract_code(); +}; + +AsmOpConsList optimize_code_head(AsmOpConsList op_list); +AsmOpConsList optimize_code(AsmOpConsList op_list); +void optimize_code(AsmOpList& ops); + +struct Stack { + StackLayoutExt s; + AsmOpList& o; + enum { _StkCmt = 1, _CptStkCmt = 2, _DisableOpt = 4, _Shown = 256, _Garbage = -0x10000 }; + int mode; + Stack(AsmOpList& _o, int _mode = 0) : o(_o), mode(_mode) { + } + Stack(AsmOpList& _o, const StackLayoutExt& _s, int _mode = 0) : s(_s), o(_o), mode(_mode) { + } + Stack(AsmOpList& _o, StackLayoutExt&& _s, int _mode = 0) : s(std::move(_s)), o(_o), mode(_mode) { + } + int depth() const { + return (int)s.size(); + } + var_idx_t operator[](int i) const { + validate(i); + return s[depth() - i - 1].first; + } + var_const_idx_t& at(int i) { + validate(i); + return s[depth() - i - 1]; + } + var_const_idx_t at(int i) const { + validate(i); + return s[depth() - i - 1]; + } + var_const_idx_t get(int i) const { + return at(i); + } + StackLayout vars() const; + int find(var_idx_t var, int from = 0) const; + int find(var_idx_t var, int from, int to) const; + int find_const(const_idx_t cst, int from = 0) const; + int find_outside(var_idx_t var, int from, int to) const; + void forget_const(); + void validate(int i) const { + assert(i >= 0 && i < depth() && "invalid stack reference"); + } + void modified() { + mode &= ~_Shown; + } + void issue_pop(int i); + void issue_push(int i); + void issue_xchg(int i, int j); + int drop_vars_except(const VarDescrList& var_info, int excl_var = 0x80000000); + void forget_var(var_idx_t idx); + void push_new_var(var_idx_t idx); + void push_new_const(var_idx_t idx, const_idx_t cidx); + void assign_var(var_idx_t new_idx, var_idx_t old_idx); + void do_copy_var(var_idx_t new_idx, var_idx_t old_idx); + void enforce_state(const StackLayout& req_stack); + void rearrange_top(const StackLayout& top, std::vector last); + void rearrange_top(var_idx_t top, bool last); + void merge_const(const Stack& req_stack); + void merge_state(const Stack& req_stack); + void show(int _mode); + void show() { + show(mode); + } + void opt_show() { + if ((mode & (_StkCmt | _Shown)) == _StkCmt) { + show(mode); + } + } + bool operator==(const Stack& y) const & { + return s == y.s; + } +}; + +/* + * + * SPECIFIC SYMBOL VALUES, + * BUILT-IN FUNCTIONS AND OPERATIONS + * + */ + +typedef std::function&, std::vector&)> simple_compile_func_t; +typedef std::function&, std::vector&)> compile_func_t; + +inline simple_compile_func_t make_simple_compile(AsmOp op) { + return [op](std::vector& out, std::vector& in) -> AsmOp { return op; }; +} + +inline compile_func_t make_ext_compile(std::vector ops) { + return [ops = std::move(ops)](AsmOpList & dest, std::vector & out, std::vector & in)->bool { + return dest.append(ops); + }; +} + +inline compile_func_t make_ext_compile(AsmOp op) { + return + [op](AsmOpList& dest, std::vector& out, std::vector& in) -> bool { return dest.append(op); }; +} + +struct SymValAsmFunc : SymValFunc { + simple_compile_func_t simple_compile; + compile_func_t ext_compile; + ~SymValAsmFunc() override = default; + SymValAsmFunc(TypeExpr* ft, const AsmOp& _macro, bool impure = false) + : SymValFunc(-1, ft, impure), simple_compile(make_simple_compile(_macro)) { + } + SymValAsmFunc(TypeExpr* ft, std::vector _macro, bool impure = false) + : SymValFunc(-1, ft, impure), ext_compile(make_ext_compile(std::move(_macro))) { + } + SymValAsmFunc(TypeExpr* ft, simple_compile_func_t _compile, bool impure = false) + : SymValFunc(-1, ft, impure), simple_compile(std::move(_compile)) { + } + SymValAsmFunc(TypeExpr* ft, compile_func_t _compile, bool impure = false) + : SymValFunc(-1, ft, impure), ext_compile(std::move(_compile)) { + } + SymValAsmFunc(TypeExpr* ft, simple_compile_func_t _compile, std::initializer_list arg_order, + std::initializer_list ret_order = {}, bool impure = false) + : SymValFunc(-1, ft, arg_order, ret_order, impure), simple_compile(std::move(_compile)) { + } + SymValAsmFunc(TypeExpr* ft, compile_func_t _compile, std::initializer_list arg_order, + std::initializer_list ret_order = {}, bool impure = false) + : SymValFunc(-1, ft, arg_order, ret_order, impure), ext_compile(std::move(_compile)) { + } + bool compile(AsmOpList& dest, std::vector& in, std::vector& out) const; +}; + +// defined in builtins.cpp +AsmOp exec_arg_op(std::string op, long long arg); +AsmOp exec_arg_op(std::string op, long long arg, int args, int retv = 1); +AsmOp exec_arg_op(std::string op, td::RefInt256 arg); +AsmOp exec_arg_op(std::string op, td::RefInt256 arg, int args, int retv = 1); +AsmOp push_const(td::RefInt256 x); + +void define_builtins(); + +} // namespace funC diff --git a/crypto/func/gen-abscode.cpp b/crypto/func/gen-abscode.cpp new file mode 100644 index 00000000..ab2efed5 --- /dev/null +++ b/crypto/func/gen-abscode.cpp @@ -0,0 +1,325 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "func.h" + +namespace funC { + +/* + * + * EXPRESSIONS + * + */ + +Expr* Expr::copy() const { + auto res = new Expr{*this}; + for (auto& arg : res->args) { + arg = arg->copy(); + } + return res; +} + +Expr::Expr(int c, sym_idx_t name_idx, std::initializer_list _arglist) : cls(c), args(std::move(_arglist)) { + sym = sym::lookup_symbol(name_idx); + if (!sym) { + } +} + +void Expr::chk_rvalue(const Lexem& lem) const { + if (!is_rvalue()) { + lem.error_at("rvalue expected before `", "`"); + } +} + +void Expr::chk_lvalue(const Lexem& lem) const { + if (!is_lvalue()) { + lem.error_at("lvalue expected before `", "`"); + } +} + +void Expr::chk_type(const Lexem& lem) const { + if (!is_type()) { + lem.error_at("type expression expected before `", "`"); + } +} + +bool Expr::deduce_type(const Lexem& lem) { + if (e_type) { + return true; + } + switch (cls) { + case _Apply: { + if (!sym) { + return false; + } + SymVal* sym_val = dynamic_cast(sym->value); + if (!sym_val || !sym_val->get_type()) { + return false; + } + std::vector arg_types; + for (const auto& arg : args) { + arg_types.push_back(arg->e_type); + } + TypeExpr* fun_type = TypeExpr::new_map(TypeExpr::new_tensor(arg_types), TypeExpr::new_hole()); + try { + unify(fun_type, sym_val->sym_type); + } catch (UnifyError& ue) { + std::ostringstream os; + os << "cannot apply function " << sym->name() << " : " << sym_val->get_type() << " to arguments of type " + << fun_type->args[0] << ": " << ue; + lem.error(os.str()); + } + e_type = fun_type->args[1]; + TypeExpr::remove_indirect(e_type); + return true; + } + case _VarApply: { + assert(args.size() == 2); + TypeExpr* fun_type = TypeExpr::new_map(args[1]->e_type, TypeExpr::new_hole()); + try { + unify(fun_type, args[0]->e_type); + } catch (UnifyError& ue) { + std::ostringstream os; + os << "cannot apply expression of type " << args[0]->e_type << " to an expression of type " << args[1]->e_type + << ": " << ue; + lem.error(os.str()); + } + e_type = fun_type->args[1]; + TypeExpr::remove_indirect(e_type); + return true; + } + case _Letop: { + assert(args.size() == 2); + try { + // std::cerr << "in assignment: " << args[0]->e_type << " from " << args[1]->e_type << std::endl; + unify(args[0]->e_type, args[1]->e_type); + } catch (UnifyError& ue) { + std::ostringstream os; + os << "cannot assign an expression of type " << args[1]->e_type << " to a variable or pattern of type " + << args[0]->e_type << ": " << ue; + lem.error(os.str()); + } + e_type = args[0]->e_type; + TypeExpr::remove_indirect(e_type); + return true; + } + case _LetFirst: { + assert(args.size() == 2); + TypeExpr* rhs_type = TypeExpr::new_tensor({args[0]->e_type, TypeExpr::new_hole()}); + try { + // std::cerr << "in implicit assignment of a modifying method: " << rhs_type << " and " << args[1]->e_type << std::endl; + unify(rhs_type, args[1]->e_type); + } catch (UnifyError& ue) { + std::ostringstream os; + os << "cannot implicitly assign an expression of type " << args[1]->e_type + << " to a variable or pattern of type " << rhs_type << " in modifying method `" << sym::symbols.get_name(val) + << "` : " << ue; + lem.error(os.str()); + } + e_type = rhs_type->args[1]; + TypeExpr::remove_indirect(e_type); + // std::cerr << "result type is " << e_type << std::endl; + return true; + } + case _CondExpr: { + assert(args.size() == 3); + auto flag_type = TypeExpr::new_atomic(_Int); + try { + unify(args[0]->e_type, flag_type); + } catch (UnifyError& ue) { + std::ostringstream os; + os << "condition in a conditional expression has non-integer type " << args[0]->e_type << ": " << ue; + lem.error(os.str()); + } + try { + unify(args[1]->e_type, args[2]->e_type); + } catch (UnifyError& ue) { + std::ostringstream os; + os << "the two variants in a conditional expression have different types " << args[1]->e_type << " and " + << args[2]->e_type << " : " << ue; + lem.error(os.str()); + } + e_type = args[1]->e_type; + TypeExpr::remove_indirect(e_type); + return true; + } + } + return false; +} + +int Expr::define_new_vars(CodeBlob& code) { + switch (cls) { + case _Tuple: + case _TypeApply: { + int res = 0; + for (const auto& x : args) { + res += x->define_new_vars(code); + } + return res; + } + case _Var: + if (val < 0) { + val = code.create_var(TmpVar::_Named, e_type, sym, &here); + return 1; + } + break; + case _Hole: + if (val < 0) { + val = code.create_var(TmpVar::_Tmp, e_type, nullptr, &here); + } + break; + } + return 0; +} + +int Expr::predefine_vars() { + switch (cls) { + case _Tuple: + case _TypeApply: { + int res = 0; + for (const auto& x : args) { + res += x->predefine_vars(); + } + return res; + } + case _Var: + if (!sym) { + assert(val < 0 && here.defined()); + sym = sym::define_symbol(~val, false, here); + if (!sym) { + throw src::ParseError{here, std::string{"redefined variable `"} + sym::symbols.get_name(~val) + "`"}; + } + sym->value = new SymVal{SymVal::_Var, -1, e_type}; + return 1; + } + break; + } + return 0; +} + +std::vector Expr::pre_compile(CodeBlob& code) const { + switch (cls) { + case _Tuple: { + std::vector res; + for (const auto& x : args) { + auto add = x->pre_compile(code); + res.insert(res.end(), add.cbegin(), add.cend()); + } + return res; + } + case _Apply: { + assert(sym); + std::vector res; + auto func = dynamic_cast(sym->value); + if (func && func->arg_order.size() == args.size()) { + //std::cerr << "!!! reordering " << args.size() << " arguments of " << sym->name() << std::endl; + std::vector> add_list(args.size()); + for (int i : func->arg_order) { + add_list[i] = args[i]->pre_compile(code); + } + for (const auto& add : add_list) { + res.insert(res.end(), add.cbegin(), add.cend()); + } + } else { + for (const auto& x : args) { + auto add = x->pre_compile(code); + res.insert(res.end(), add.cbegin(), add.cend()); + } + } + var_idx_t rv = code.create_var(TmpVar::_Tmp, e_type, nullptr, &here); + std::vector rvect{rv}; + auto& op = code.emplace_back(here, Op::_Call, rvect, std::move(res), sym); + if (flags & _IsImpure) { + op.flags |= Op::_Impure; + } + return rvect; + } + case _TypeApply: + return args[0]->pre_compile(code); + case _Var: + case _Hole: + return {val}; + case _VarApply: + if (args[0]->cls == _Glob) { + std::vector res = args[1]->pre_compile(code); + var_idx_t rv = code.create_var(TmpVar::_Tmp, e_type, nullptr, &here); + std::vector rvect{rv}; + auto& op = code.emplace_back(here, Op::_Call, rvect, std::move(res), args[0]->sym); + if (args[0]->flags & _IsImpure) { + op.flags |= Op::_Impure; + } + return rvect; + } else { + std::vector res = args[1]->pre_compile(code); + std::vector tfunc = args[0]->pre_compile(code); + if (tfunc.size() != 1) { + throw src::Fatal{"stack tuple used as a function"}; + } + res.push_back(tfunc[0]); + var_idx_t rv = code.create_var(TmpVar::_Tmp, e_type, nullptr, &here); + std::vector rvect{rv}; + code.emplace_back(here, Op::_CallInd, rvect, std::move(res)); + return rvect; + } + case _Const: { + var_idx_t rv = code.create_var(TmpVar::_Tmp, e_type, nullptr, &here); + std::vector rvect{rv}; + code.emplace_back(here, Op::_IntConst, rvect, intval); + return rvect; + } + case _Glob: { + var_idx_t rv = code.create_var(TmpVar::_Tmp, e_type, nullptr, &here); + std::vector rvect{rv}; + code.emplace_back(here, Op::_GlobVar, rvect, std::vector{}, sym); + return rvect; + } + case _Letop: { + std::vector right = args[1]->pre_compile(code); + std::vector left = args[0]->pre_compile(code); + code.emplace_back(here, Op::_Let, left, std::move(right)); + return left; + } + case _LetFirst: { + var_idx_t rv = code.create_var(TmpVar::_Tmp, e_type, nullptr, &here); + std::vector right = args[1]->pre_compile(code); + std::vector left = args[0]->pre_compile(code); + left.push_back(rv); + code.emplace_back(here, Op::_Let, std::move(left), std::move(right)); + return std::vector{rv}; + } + case _CondExpr: { + auto cond = args[0]->pre_compile(code); + assert(cond.size() == 1); + var_idx_t rv = code.create_var(TmpVar::_Tmp, e_type, nullptr, &here); + std::vector rvect{rv}; + Op& if_op = code.emplace_back(here, Op::_If, cond); + code.push_set_cur(if_op.block0); + code.emplace_back(here, Op::_Let, rvect, args[1]->pre_compile(code)); + code.close_pop_cur(args[1]->here); + code.push_set_cur(if_op.block1); + code.emplace_back(here, Op::_Let, rvect, args[2]->pre_compile(code)); + code.close_pop_cur(args[2]->here); + return rvect; + } + default: + std::cerr << "expression constructor is " << cls << std::endl; + throw src::Fatal{"cannot compile expression with unknown constructor"}; + } +} + +} // namespace funC diff --git a/crypto/func/keywords.cpp b/crypto/func/keywords.cpp new file mode 100644 index 00000000..bab30d32 --- /dev/null +++ b/crypto/func/keywords.cpp @@ -0,0 +1,115 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "func.h" + +namespace src { + +int lexem_is_special(std::string str) { + return 0; // no special lexems +} + +} // namespace src + +namespace funC { + +/* + * + * KEYWORD DEFINITION + * + */ + +void define_keywords() { + sym::symbols.add_kw_char('+') + .add_kw_char('-') + .add_kw_char('*') + .add_kw_char('/') + .add_kw_char('%') + .add_kw_char('?') + .add_kw_char(':') + .add_kw_char(',') + .add_kw_char(';') + .add_kw_char('(') + .add_kw_char(')') + .add_kw_char('{') + .add_kw_char('}') + .add_kw_char('=') + .add_kw_char('_') + .add_kw_char('<') + .add_kw_char('>') + .add_kw_char('&') + .add_kw_char('|') + .add_kw_char('~'); + + using Kw = funC::Keyword; + sym::symbols.add_keyword("==", Kw::_Eq) + .add_keyword("!=", Kw::_Neq) + .add_keyword("<=", Kw::_Leq) + .add_keyword(">=", Kw::_Geq) + .add_keyword("<=>", Kw::_Spaceship) + .add_keyword("<<", Kw::_Lshift) + .add_keyword(">>", Kw::_Rshift) + .add_keyword(">>~", Kw::_RshiftR) + .add_keyword(">>^", Kw::_RshiftC) + .add_keyword("/~", Kw::_DivR) + .add_keyword("/^", Kw::_DivC) + .add_keyword("/%", Kw::_DivMod) + .add_keyword("+=", Kw::_PlusLet) + .add_keyword("-=", Kw::_MinusLet) + .add_keyword("*=", Kw::_TimesLet) + .add_keyword("/=", Kw::_DivLet) + .add_keyword("%=", Kw::_ModLet) + .add_keyword("/~=", Kw::_DivRLet) + .add_keyword("/^=", Kw::_DivCLet) + .add_keyword("<<=", Kw::_LshiftLet) + .add_keyword(">>=", Kw::_RshiftLet) + .add_keyword(">>~=", Kw::_RshiftRLet) + .add_keyword(">>^=", Kw::_RshiftCLet); + + sym::symbols.add_keyword("return", Kw::_Return) + .add_keyword("var", Kw::_Var) + .add_keyword("repeat", Kw::_Repeat) + .add_keyword("do", Kw::_Do) + .add_keyword("while", Kw::_While) + .add_keyword("until", Kw::_Until) + .add_keyword("if", Kw::_If) + .add_keyword("ifnot", Kw::_Ifnot) + .add_keyword("then", Kw::_Then) + .add_keyword("else", Kw::_Else) + .add_keyword("elseif", Kw::_Elseif) + .add_keyword("elseifnot", Kw::_Elseifnot); + + sym::symbols.add_keyword("int", Kw::_Int) + .add_keyword("cell", Kw::_Cell) + .add_keyword("slice", Kw::_Slice) + .add_keyword("builder", Kw::_Builder) + .add_keyword("cont", Kw::_Cont) + .add_keyword("tuple", Kw::_Tuple) + .add_keyword("->", Kw::_Mapsto); + + sym::symbols.add_keyword("extern", Kw::_Extern) + .add_keyword("asm", Kw::_Asm) + .add_keyword("impure", Kw::_Impure) + .add_keyword("method_id", Kw::_MethodId) + .add_keyword("operator", Kw::_Operator) + .add_keyword("infix", Kw::_Infix) + .add_keyword("infixl", Kw::_Infixl) + .add_keyword("infixr", Kw::_Infixr); +} + +} // namespace funC diff --git a/crypto/func/optimize.cpp b/crypto/func/optimize.cpp new file mode 100644 index 00000000..95b4403c --- /dev/null +++ b/crypto/func/optimize.cpp @@ -0,0 +1,539 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "func.h" + +namespace funC { + +/* + * + * PEEPHOLE OPTIMIZER + * + */ + +void Optimizer::set_code(AsmOpConsList code) { + code_ = std::move(code); + unpack(); +} + +void Optimizer::unpack() { + int i = 0, j = 0; + for (AsmOpCons *p = code_.get(); p && i < n; p = p->cdr.get(), ++j) { + if (p->car->is_very_custom()) { + break; + } + if (p->car->is_comment()) { + continue; + } + op_cons_[i] = p; + op_[i] = std::move(p->car); + offs_[i] = j; + ++i; + } + l_ = i; + indent_ = (i ? op_[0]->indent : 0); +} + +void Optimizer::pack() { + for (int i = 0; i < l_; i++) { + op_cons_[i]->car = std::move(op_[i]); + op_cons_[i] = nullptr; + } + l_ = 0; +} + +void Optimizer::apply() { + if (!p_ && !q_) { + return; + } + assert(p_ > 0 && p_ <= l_ && q_ >= 0 && q_ <= n && l_ <= n); + for (int i = p_; i < l_; i++) { + assert(op_[i]); + op_cons_[i]->car = std::move(op_[i]); + op_cons_[i] = nullptr; + } + for (int c = offs_[p_ - 1]; c >= 0; --c) { + code_ = std::move(code_->cdr); + } + for (int j = q_ - 1; j >= 0; j--) { + assert(oq_[j]); + oq_[j]->indent = indent_; + code_ = AsmOpCons::cons(std::move(oq_[j]), std::move(code_)); + } + l_ = 0; +} + +AsmOpConsList Optimizer::extract_code() { + pack(); + return std::move(code_); +} + +void Optimizer::show_head() const { + if (!debug_) { + return; + } + std::cerr << "optimizing"; + for (int i = 0; i < l_; i++) { + if (op_[i]) { + std::cerr << ' ' << *op_[i] << ' '; + } else { + std::cerr << " (null) "; + } + } + std::cerr << std::endl; +} + +void Optimizer::show_left() const { + if (!debug_) { + return; + } + std::cerr << "// *** rewriting"; + for (int i = 0; i < p_; i++) { + if (op_[i]) { + std::cerr << ' ' << *op_[i] << ' '; + } else { + std::cerr << " (null) "; + } + } +} + +void Optimizer::show_right() const { + if (!debug_) { + return; + } + std::cerr << "->"; + for (int i = 0; i < q_; i++) { + if (oq_[i]) { + std::cerr << ' ' << *oq_[i] << ' '; + } else { + std::cerr << " (null) "; + } + } + std::cerr << std::endl; +} + +bool Optimizer::say(std::string str) const { + if (debug_) { + std::cerr << str << std::endl; + } + return true; +} + +bool Optimizer::is_const_push_swap() const { + return l_ >= 3 && op_[0]->is_gconst() && op_[1]->is_push() && op_[1]->a >= 1 && op_[2]->is_swap(); +} + +// PUSHCONST c ; PUSH s(i+1) ; SWAP -> PUSH s(i) ; PUSHCONST c +bool Optimizer::rewrite_const_push_swap() { + p_ = 3; + q_ = 2; + show_left(); + oq_[1] = std::move(op_[0]); + oq_[0] = std::move(op_[1]); + (oq_[0]->a)--; + show_right(); + return true; +} + +bool Optimizer::is_const_push_xchgs() { + if (!(pb_ >= 2 && pb_ <= l2_ && op_[0]->is_gconst())) { + return false; + } + StackTransform t; + int pos = 0, i; + for (i = 1; i < pb_; i++) { + int a, b; + if (op_[i]->is_xchg(&a, &b)) { + if (pos == a) { + pos = b; + } else if (pos == b) { + pos = a; + } else { + t.apply_xchg(a - (a > pos), b - (b > pos)); + } + } else if (op_[i]->is_push(&a)) { + if (pos == a) { + return false; + } + t.apply_push(a - (a > pos)); + ++pos; + } else { + return false; + } + } + if (pos) { + return false; + } + t.apply_push_newconst(); + if (t <= tr_[i - 1]) { + p_ = i; + return true; + } else { + return false; + } +} + +bool Optimizer::rewrite_const_push_xchgs() { + if (!p_) { + return false; + } + show_left(); + auto c_op = std::move(op_[0]); + assert(c_op->is_gconst()); + StackTransform t; + q_ = 0; + int pos = 0; + for (int i = 1; i < p_; i++) { + int a, b; + if (op_[i]->is_xchg(&a, &b)) { + if (a == pos) { + pos = b; + } else if (b == pos) { + pos = a; + } else { + oq_[q_] = std::move(op_[i]); + if (a > pos) { + oq_[q_]->a = a - 1; + } + if (b > pos) { + oq_[q_]->b = b - 1; + } + assert(apply_op(t, *oq_[q_])); + ++q_; + } + } else { + assert(op_[i]->is_push(&a)); + assert(a != pos); + oq_[q_] = std::move(op_[i]); + if (a > pos) { + oq_[q_]->a = a - 1; + } + assert(apply_op(t, *oq_[q_])); + ++q_; + ++pos; + } + } + assert(!pos); + t.apply_push_newconst(); + assert(t <= tr_[p_ - 1]); + oq_[q_++] = std::move(c_op); + show_right(); + return true; +} + +bool Optimizer::simple_rewrite(int p, AsmOp&& new_op) { + assert(p > 0 && p <= l_); + p_ = p; + q_ = 1; + show_left(); + oq_[0] = std::move(op_[0]); + *oq_[0] = new_op; + show_right(); + return true; +} + +bool Optimizer::simple_rewrite(int p, AsmOp&& new_op1, AsmOp&& new_op2) { + assert(p > 1 && p <= l_); + p_ = p; + q_ = 2; + show_left(); + oq_[0] = std::move(op_[0]); + *oq_[0] = new_op1; + oq_[1] = std::move(op_[1]); + *oq_[1] = new_op2; + show_right(); + return true; +} + +bool Optimizer::simple_rewrite_nop() { + assert(p_ > 0 && p_ <= l_); + q_ = 0; + show_left(); + show_right(); + return true; +} + +bool Optimizer::is_pred(const std::function& pred, int min_p) { + min_p = std::max(min_p, pb_); + for (int p = l2_; p >= min_p; p--) { + if (pred(tr_[p - 1])) { + p_ = p; + return true; + } + } + return false; +} + +bool Optimizer::is_same_as(const StackTransform& trans, int min_p) { + return is_pred([&trans](const auto& t) { return t >= trans; }, min_p); +} + +// s1 s3 XCHG ; s0 s2 XCHG -> 2SWAP +bool Optimizer::is_2swap() { + static const StackTransform t_2swap{2, 3, 0, 1, 4}; + return is_same_as(t_2swap); +} + +// s3 PUSH ; s3 PUSH -> 2OVER +bool Optimizer::is_2over() { + static const StackTransform t_2over{2, 3, 0}; + return is_same_as(t_2over); +} + +bool Optimizer::is_2dup() { + static const StackTransform t_2dup{0, 1, 0}; + return is_same_as(t_2dup); +} + +bool Optimizer::is_tuck() { + static const StackTransform t_tuck{0, 1, 0, 2}; + return is_same_as(t_tuck); +} + +bool Optimizer::is_2drop() { + static const StackTransform t_2drop{2}; + return is_same_as(t_2drop); +} + +bool Optimizer::is_rot() { + return is_pred([](const auto& t) { return t.is_rot(); }); +} + +bool Optimizer::is_rotrev() { + return is_pred([](const auto& t) { return t.is_rotrev(); }); +} + +bool Optimizer::is_nop() { + return is_pred([](const auto& t) { return t.is_id(); }, 1); +} + +bool Optimizer::is_xchg(int* i, int* j) { + return is_pred([i, j](const auto& t) { return t.is_xchg(i, j) && ((*i < 16 && *j < 16) || (!*i && *j < 256)); }); +} + +bool Optimizer::is_push(int* i) { + return is_pred([i](const auto& t) { return t.is_push(i) && *i < 256; }); +} + +bool Optimizer::is_pop(int* i) { + return is_pred([i](const auto& t) { return t.is_pop(i) && *i < 256; }); +} + +bool Optimizer::is_xchg2(int* i, int* j) { + return is_pred([i, j](const auto& t) { return t.is_xchg2(i, j) && *i < 16 && *j < 16; }); +} + +bool Optimizer::is_xcpu(int* i, int* j) { + return is_pred([i, j](const auto& t) { return t.is_xcpu(i, j) && *i < 16 && *j < 16; }); +} + +bool Optimizer::is_puxc(int* i, int* j) { + return is_pred([i, j](const auto& t) { return t.is_puxc(i, j) && *i < 16 && *j < 15; }); +} + +bool Optimizer::is_push2(int* i, int* j) { + return is_pred([i, j](const auto& t) { return t.is_push2(i, j) && *i < 16 && *j < 16; }); +} + +bool Optimizer::is_xchg3(int* i, int* j, int* k) { + return is_pred([i, j, k](const auto& t) { return t.is_xchg3(i, j, k) && *i < 16 && *j < 16 && *k < 16; }); +} + +bool Optimizer::is_xc2pu(int* i, int* j, int* k) { + return is_pred([i, j, k](const auto& t) { return t.is_xc2pu(i, j, k) && *i < 16 && *j < 16 && *k < 16; }); +} + +bool Optimizer::is_xcpuxc(int* i, int* j, int* k) { + return is_pred([i, j, k](const auto& t) { return t.is_xcpuxc(i, j, k) && *i < 16 && *j < 16 && *k < 15; }); +} + +bool Optimizer::is_xcpu2(int* i, int* j, int* k) { + return is_pred([i, j, k](const auto& t) { return t.is_xcpu2(i, j, k) && *i < 16 && *j < 16 && *k < 16; }); +} + +bool Optimizer::is_puxc2(int* i, int* j, int* k) { + return is_pred([i, j, k](const auto& t) { return t.is_puxc2(i, j, k) && *i < 16 && *j < 15 && *k < 15; }); +} + +bool Optimizer::is_puxcpu(int* i, int* j, int* k) { + return is_pred([i, j, k](const auto& t) { return t.is_puxcpu(i, j, k) && *i < 16 && *j < 15 && *k < 15; }); +} + +bool Optimizer::is_pu2xc(int* i, int* j, int* k) { + return is_pred([i, j, k](const auto& t) { return t.is_pu2xc(i, j, k) && *i < 16 && *j < 15 && *k < 14; }); +} + +bool Optimizer::is_push3(int* i, int* j, int* k) { + return is_pred([i, j, k](const auto& t) { return t.is_push3(i, j, k) && *i < 16 && *j < 16 && *k < 16; }); +} + +bool Optimizer::is_blkswap(int* i, int* j) { + return is_pred([i, j](const auto& t) { return t.is_blkswap(i, j) && *i > 0 && *j > 0 && *i <= 16 && *j <= 16; }); +} + +bool Optimizer::is_blkpush(int* i, int* j) { + return is_pred([i, j](const auto& t) { return t.is_blkpush(i, j) && *i > 0 && *i < 16 && *j < 16; }); +} + +bool Optimizer::is_blkdrop(int* i) { + return is_pred([i](const auto& t) { return t.is_blkdrop(i) && *i > 0 && *i < 16; }); +} + +bool Optimizer::is_reverse(int* i, int* j) { + return is_pred([i, j](const auto& t) { return t.is_reverse(i, j) && *i >= 2 && *i <= 17 && *j < 16; }); +} + +bool Optimizer::is_nip_seq(int* i, int* j) { + return is_pred([i, j](const auto& t) { return t.is_nip_seq(i, j) && *i >= 3 && *i <= 15; }); +} + +bool Optimizer::compute_stack_transforms() { + StackTransform trans; + for (int i = 0; i < l_; i++) { + if (!apply_op(trans, *op_[i])) { + l2_ = i; + return true; + } + tr_[i] = trans; + } + l2_ = l_; + return true; +} + +bool Optimizer::show_stack_transforms() const { + show_head(); + // slow version + /* + StackTransform trans2; + std::cerr << "id = " << trans2 << std::endl; + for (int i = 0; i < l_; i++) { + StackTransform op; + if (!apply_op(op, *op_[i])) { + std::cerr << "* (" << *op_[i] << " = invalid)\n"; + break; + } + trans2 *= op; + std::cerr << "* " << *op_[i] << " = " << op << " -> " << trans2 << std::endl; + } + */ + // fast version + StackTransform trans; + for (int i = 0; i < l_; i++) { + std::cerr << trans << std::endl << *op_[i] << " -> "; + if (!apply_op(trans, *op_[i])) { + std::cerr << " " << std::endl; + return true; + } + } + std::cerr << trans << std::endl; + return true; +} + +bool Optimizer::find_at_least(int pb) { + p_ = q_ = 0; + pb_ = pb; + // show_stack_transforms(); + int i = -100, j = -100, k = -100; + return (is_const_push_swap() && 3 >= pb && rewrite_const_push_swap()) || (is_nop() && simple_rewrite_nop()) || + (is_const_push_xchgs() && rewrite_const_push_xchgs()) || + (is_xchg(&i, &j) && simple_rewrite(AsmOp::Xchg(i, j))) || (is_push(&i) && simple_rewrite(AsmOp::Push(i))) || + (is_pop(&i) && simple_rewrite(AsmOp::Pop(i))) || (is_rot() && simple_rewrite(AsmOp::Custom("ROT", 3, 3))) || + (is_rotrev() && simple_rewrite(AsmOp::Custom("-ROT", 3, 3))) || + (is_2dup() && simple_rewrite(AsmOp::Custom("2DUP", 2, 4))) || + (is_2swap() && simple_rewrite(AsmOp::Custom("2SWAP", 2, 4))) || + (is_2over() && simple_rewrite(AsmOp::Custom("2OVER", 2, 4))) || + (is_tuck() && simple_rewrite(AsmOp::Custom("TUCK", 2, 3))) || + (is_2drop() && simple_rewrite(AsmOp::Custom("2DROP", 2, 0))) || + (is_xchg2(&i, &j) && simple_rewrite(AsmOp::Xchg2(i, j))) || + (is_xcpu(&i, &j) && simple_rewrite(AsmOp::XcPu(i, j))) || + (is_puxc(&i, &j) && simple_rewrite(AsmOp::PuXc(i, j))) || + (is_push2(&i, &j) && simple_rewrite(AsmOp::Push2(i, j))) || + (is_blkswap(&i, &j) && simple_rewrite(AsmOp::BlkSwap(i, j))) || + (is_blkpush(&i, &j) && simple_rewrite(AsmOp::BlkPush(i, j))) || + (is_blkdrop(&i) && simple_rewrite(AsmOp::BlkDrop(i))) || + (is_reverse(&i, &j) && simple_rewrite(AsmOp::BlkReverse(i, j))) || + (is_nip_seq(&i, &j) && simple_rewrite(AsmOp::Xchg(i, j), AsmOp::BlkDrop(i))) || + (is_xchg3(&i, &j, &k) && simple_rewrite(AsmOp::Xchg3(i, j, k))) || + (is_xc2pu(&i, &j, &k) && simple_rewrite(AsmOp::Xc2Pu(i, j, k))) || + (is_xcpuxc(&i, &j, &k) && simple_rewrite(AsmOp::XcPuXc(i, j, k))) || + (is_xcpu2(&i, &j, &k) && simple_rewrite(AsmOp::XcPu2(i, j, k))) || + (is_puxc2(&i, &j, &k) && simple_rewrite(AsmOp::PuXc2(i, j, k))) || + (is_puxcpu(&i, &j, &k) && simple_rewrite(AsmOp::PuXcPu(i, j, k))) || + (is_pu2xc(&i, &j, &k) && simple_rewrite(AsmOp::Pu2Xc(i, j, k))) || + (is_push3(&i, &j, &k) && simple_rewrite(AsmOp::Push3(i, j, k))); +} + +bool Optimizer::find() { + if (!compute_stack_transforms()) { + return false; + } + for (int pb = l_; pb > 0; --pb) { + if (find_at_least(pb)) { + return true; + } + } + return false; +} + +bool Optimizer::optimize() { + bool f = false; + while (find()) { + f = true; + apply(); + unpack(); + } + return f; +} + +AsmOpConsList optimize_code_head(AsmOpConsList op_list) { + Optimizer opt(std::move(op_list), op_rewrite_comments); + opt.optimize(); + return opt.extract_code(); +} + +AsmOpConsList optimize_code(AsmOpConsList op_list) { + std::vector> v; + while (op_list) { + if (!op_list->car->is_comment()) { + op_list = optimize_code_head(std::move(op_list)); + } + if (op_list) { + v.push_back(std::move(op_list->car)); + op_list = std::move(op_list->cdr); + } + } + for (auto it = v.rbegin(); it < v.rend(); ++it) { + op_list = AsmOpCons::cons(std::move(*it), std::move(op_list)); + } + return std::move(op_list); +} + +void optimize_code(AsmOpList& ops) { + std::unique_ptr op_list; + for (auto it = ops.list_.rbegin(); it < ops.list_.rend(); ++it) { + op_list = AsmOpCons::cons(std::make_unique(std::move(*it)), std::move(op_list)); + } + op_list = optimize_code(std::move(op_list)); + ops.list_.clear(); + while (op_list) { + ops.list_.push_back(std::move(*(op_list->car))); + op_list = std::move(op_list->cdr); + } +} + +} // namespace funC diff --git a/crypto/func/parse-func.cpp b/crypto/func/parse-func.cpp new file mode 100644 index 00000000..f605bd83 --- /dev/null +++ b/crypto/func/parse-func.cpp @@ -0,0 +1,1105 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "func.h" +#include "td/utils/crypto.h" +#include + +namespace sym { + +int compute_symbol_subclass(std::string str) { + using funC::IdSc; + if (str.size() < 2) { + return IdSc::undef; + } else if (str[0] == '.') { + return IdSc::dotid; + } else if (str[0] == '~') { + return IdSc::tildeid; + } else { + return IdSc::undef; + } +} + +} // namespace sym + +namespace funC { +using namespace std::literals::string_literals; +using src::Lexer; +using sym::symbols; +using td::Ref; + +inline bool is_dot_ident(sym_idx_t idx) { + return symbols.get_subclass(idx) == IdSc::dotid; +} + +inline bool is_tilde_ident(sym_idx_t idx) { + return symbols.get_subclass(idx) == IdSc::tildeid; +} + +inline bool is_special_ident(sym_idx_t idx) { + return symbols.get_subclass(idx) != IdSc::undef; +} + +/* + * + * PARSE SOURCE + * + */ + +// TE ::= TA | TA -> TE +// TA ::= int | ... | cont | var | _ | () | ( TE { , TE } ) +TypeExpr* parse_type(Lexer& lex); + +TypeExpr* parse_type1(Lexer& lex) { + switch (lex.tp()) { + case _Int: + lex.next(); + return TypeExpr::new_atomic(_Int); + case _Cell: + lex.next(); + return TypeExpr::new_atomic(_Cell); + case _Slice: + lex.next(); + return TypeExpr::new_atomic(_Slice); + case _Builder: + lex.next(); + return TypeExpr::new_atomic(_Builder); + case _Cont: + lex.next(); + return TypeExpr::new_atomic(_Cont); + case _Tuple: + lex.next(); + return TypeExpr::new_atomic(_Tuple); + case _Var: + case '_': + lex.next(); + return TypeExpr::new_hole(); + } + lex.expect('('); + if (lex.tp() == ')') { + lex.next(); + return TypeExpr::new_unit(); + } + auto t1 = parse_type(lex); + if (lex.tp() != ',') { + lex.expect(')'); + return t1; + } + std::vector tlist{1, t1}; + while (lex.tp() == ',') { + lex.next(); + tlist.push_back(parse_type(lex)); + } + lex.expect(')'); + return TypeExpr::new_tensor(std::move(tlist)); +} + +TypeExpr* parse_type(Lexer& lex) { + auto res = parse_type1(lex); + if (lex.tp() == _Mapsto) { + lex.next(); + auto to = parse_type(lex); + return TypeExpr::new_map(res, to); + } else { + return res; + } +} + +FormalArg parse_formal_arg(Lexer& lex, int fa_idx) { + TypeExpr* arg_type = 0; + SrcLocation loc = lex.cur().loc; + if (lex.tp() == '_') { + lex.next(); + if (lex.tp() == ',' || lex.tp() == ')') { + return std::make_tuple(TypeExpr::new_hole(), (SymDef*)nullptr, loc); + } + arg_type = TypeExpr::new_hole(); + loc = lex.cur().loc; + } else if (lex.tp() != _Ident) { + arg_type = parse_type(lex); + } else { + arg_type = TypeExpr::new_hole(); + } + if (lex.tp() == '_' || lex.tp() == ',' || lex.tp() == ')') { + if (lex.tp() == '_') { + loc = lex.cur().loc; + lex.next(); + } + return std::make_tuple(arg_type, (SymDef*)nullptr, loc); + } + if (lex.tp() != _Ident) { + lex.expect(_Ident, "formal parameter name"); + } + loc = lex.cur().loc; + SymDef* new_sym_def = sym::define_symbol(lex.cur().val, true, loc); + if (new_sym_def->value) { + lex.cur().error_at("redefined formal parameter `", "`"); + } + new_sym_def->value = new SymVal{SymVal::_Param, fa_idx, arg_type}; + lex.next(); + return std::make_tuple(arg_type, new_sym_def, loc); +} + +FormalArgList parse_formal_args(Lexer& lex) { + FormalArgList args; + lex.expect('(', "formal argument list"); + if (lex.tp() == ')') { + lex.next(); + return args; + } + int fa_idx = 0; + args.push_back(parse_formal_arg(lex, fa_idx++)); + while (lex.tp() == ',') { + lex.next(); + args.push_back(parse_formal_arg(lex, fa_idx++)); + } + lex.expect(')'); + return args; +} + +TypeExpr* extract_total_arg_type(const FormalArgList& arg_list) { + if (arg_list.empty()) { + return TypeExpr::new_unit(); + } + if (arg_list.size() == 1) { + return std::get<0>(arg_list[0]); + } + std::vector type_list; + for (auto& x : arg_list) { + type_list.push_back(std::get<0>(x)); + } + return TypeExpr::new_tensor(std::move(type_list)); +} + +SymValCodeFunc* make_new_glob_func(SymDef* func_sym, TypeExpr* func_type, bool impure = false) { + SymValCodeFunc* res = new SymValCodeFunc{glob_func_cnt, func_type, impure}; + func_sym->value = res; + glob_func.push_back(func_sym); + glob_func_cnt++; + return res; +} + +bool check_global_func(const Lexem& cur, sym_idx_t func_name = 0) { + if (!func_name) { + func_name = cur.val; + } + SymDef* def = sym::lookup_symbol(func_name); + if (!def) { + cur.loc.show_error(std::string{"undefined function `"} + symbols.get_name(func_name) + + "`, defining a global function of unknown type"); + def = sym::define_global_symbol(func_name, 0, cur.loc); + assert(def && "cannot define global function"); + ++undef_func_cnt; + make_new_glob_func(def, TypeExpr::new_hole()); // was: ... ::new_func() + return true; + } + SymVal* val = dynamic_cast(def->value); + if (!val) { + cur.error(std::string{"symbol `"} + symbols.get_name(func_name) + "` has no value and no type"); + return false; + } else if (!val->get_type()) { + cur.error(std::string{"symbol `"} + symbols.get_name(func_name) + "` has no type, possibly not a function"); + return false; + } else { + return true; + } +} + +Expr* parse_expr(Lexer& lex, CodeBlob& code, bool nv = false); + +// parse ( E { , E } ) | () | id | num | _ +Expr* parse_expr100(Lexer& lex, CodeBlob& code, bool nv) { + if (lex.tp() == '(') { + SrcLocation loc{lex.cur().loc}; + lex.next(); + if (lex.tp() == ')') { + lex.next(); + Expr* res = new Expr{Expr::_Tuple, {}}; + res->flags = Expr::_IsRvalue; + res->here = loc; + res->e_type = TypeExpr::new_unit(); + return res; + } + Expr* res = parse_expr(lex, code, nv); + if (lex.tp() != ',') { + lex.expect(')'); + return res; + } + std::vector type_list; + type_list.push_back(res->e_type); + int f = res->flags; + res = new Expr{Expr::_Tuple, {res}}; + while (lex.tp() == ',') { + lex.next(); + auto x = parse_expr(lex, code, nv); + res->pb_arg(x); + if ((f ^ x->flags) & Expr::_IsType) { + lex.cur().error("mixing type and non-type expressions inside the same tuple"); + } + f &= x->flags; + type_list.push_back(x->e_type); + } + res->here = loc; + res->flags = f; + res->e_type = TypeExpr::new_tensor(std::move(type_list)); + lex.expect(')'); + return res; + } + int t = lex.tp(); + if (t == Lexem::Number) { + Expr* res = new Expr{Expr::_Const, lex.cur().loc}; + res->flags = Expr::_IsRvalue; + res->intval = td::string_to_int256(lex.cur().str); + if (res->intval.is_null()) { + lex.cur().error_at("invalid integer constant `", "`"); + } + res->e_type = TypeExpr::new_atomic(_Int); + lex.next(); + return res; + } + if (t == '_') { + Expr* res = new Expr{Expr::_Hole, lex.cur().loc}; + res->val = -1; + res->flags = (Expr::_IsLvalue | Expr::_IsHole | Expr::_IsNewVar); + res->e_type = TypeExpr::new_hole(); + lex.next(); + return res; + } + if (t == _Var) { + Expr* res = new Expr{Expr::_Type, lex.cur().loc}; + res->flags = Expr::_IsType; + res->e_type = TypeExpr::new_hole(); + lex.next(); + return res; + } + if (t == _Int || t == _Cell || t == _Slice || t == _Builder || t == _Cont || t == _Type) { + Expr* res = new Expr{Expr::_Type, lex.cur().loc}; + res->flags = Expr::_IsType; + res->e_type = TypeExpr::new_atomic(t); + lex.next(); + return res; + } + if (t == _Ident) { + Expr* res = new Expr{Expr::_Var, lex.cur().loc}; + if (nv) { + res->val = ~lex.cur().val; + res->e_type = TypeExpr::new_hole(); + res->flags = Expr::_IsLvalue | Expr::_IsNewVar; + // std::cerr << "defined new variable " << lex.cur().str << " : " << res->e_type << std::endl; + } else { + res->sym = sym::lookup_symbol(lex.cur().val); + if (!res->sym) { + check_global_func(lex.cur()); + res->sym = sym::lookup_symbol(lex.cur().val); + } + SymVal* val = nullptr; + if (res->sym) { + val = dynamic_cast(res->sym->value); + } + if (!val) { + lex.cur().error_at("undefined identifier `", "`"); + } else if (val->type == SymVal::_Func) { + res->e_type = val->get_type(); + res->cls = Expr::_Glob; + } else if (val->idx < 0) { + lex.cur().error_at("accessing variable `", "` being defined"); + } else { + res->val = val->idx; + res->e_type = val->get_type(); + // std::cerr << "accessing variable " << lex.cur().str << " : " << res->e_type << std::endl; + } + // std::cerr << "accessing symbol " << lex.cur().str << " : " << res->e_type << (val->impure ? " (impure)" : " (pure)") << std::endl; + res->flags = Expr::_IsLvalue | Expr::_IsRvalue | (val->impure ? Expr::_IsImpure : 0); + } + res->deduce_type(lex.cur()); + lex.next(); + return res; + } + lex.expect(Lexem::Ident); + return nullptr; +} + +Expr* make_func_apply(Expr* fun, Expr* x) { + Expr* res; + if (fun->cls == Expr::_Glob) { + if (x->cls == Expr::_Tuple) { + res = new Expr{Expr::_Apply, fun->sym, x->args}; + } else { + res = new Expr{Expr::_Apply, fun->sym, {x}}; + } + res->flags = Expr::_IsRvalue | (fun->flags & Expr::_IsImpure); + } else { + res = new Expr{Expr::_VarApply, {fun, x}}; + res->flags = Expr::_IsRvalue; + } + return res; +} + +// parse E { E } +Expr* parse_expr90(Lexer& lex, CodeBlob& code, bool nv) { + Expr* res = parse_expr100(lex, code, nv); + while (lex.tp() == '(' || (lex.tp() == _Ident && !is_special_ident(lex.cur().val))) { + if (res->is_type()) { + Expr* x = parse_expr100(lex, code, true); + x->chk_lvalue(lex.cur()); // chk_lrvalue() ? + TypeExpr* tp = res->e_type; + delete res; + res = new Expr{Expr::_TypeApply, {x}}; + res->e_type = tp; + res->here = lex.cur().loc; + try { + unify(res->e_type, x->e_type); + } catch (UnifyError& ue) { + std::ostringstream os; + os << "cannot transform expression of type " << x->e_type << " to explicitly requested type " << res->e_type + << ": " << ue; + lex.cur().error(os.str()); + } + res->flags = x->flags; + } else { + Expr* x = parse_expr100(lex, code, false); + x->chk_rvalue(lex.cur()); + res = make_func_apply(res, x); + res->here = lex.cur().loc; + res->deduce_type(lex.cur()); + } + } + return res; +} + +// parse E { .method E | ~method E } +Expr* parse_expr80(Lexer& lex, CodeBlob& code, bool nv) { + Expr* res = parse_expr90(lex, code, nv); + while (lex.tp() == _Ident && is_special_ident(lex.cur().val)) { + auto modify = is_tilde_ident(lex.cur().val); + auto obj = res; + if (modify) { + obj->chk_lvalue(lex.cur()); + } else { + obj->chk_rvalue(lex.cur()); + } + auto loc = lex.cur().loc; + auto name = lex.cur().val; + auto sym = sym::lookup_symbol(name); + if (!sym || !dynamic_cast(sym->value)) { + auto name1 = symbols.lookup(lex.cur().str.substr(1)); + if (name1) { + auto sym1 = sym::lookup_symbol(name1); + if (sym1 && dynamic_cast(sym1->value)) { + name = name1; + sym = sym1; + } + } + } + check_global_func(lex.cur(), name); + if (verbosity >= 2) { + std::cerr << "using symbol `" << symbols.get_name(name) << "` for method call of " << lex.cur().str << std::endl; + } + sym = sym::lookup_symbol(name); + SymValFunc* val = sym ? dynamic_cast(sym->value) : nullptr; + if (!val) { + lex.cur().error_at("undefined method identifier `", "`"); + } + lex.next(); + auto x = parse_expr100(lex, code, false); + x->chk_rvalue(lex.cur()); + if (x->cls == Expr::_Tuple) { + res = new Expr{Expr::_Apply, name, {obj}}; + res->args.insert(res->args.end(), x->args.begin(), x->args.end()); + } else { + res = new Expr{Expr::_Apply, name, {obj, x}}; + } + res->here = loc; + res->flags = Expr::_IsRvalue | (val->impure ? Expr::_IsImpure : 0); + res->deduce_type(lex.cur()); + if (modify) { + // FIXME (use _LetFirst instead of _Letop) + auto tmp = res; + res = new Expr{Expr::_LetFirst, {obj->copy(), tmp}}; + res->here = loc; + res->flags = tmp->flags; + res->set_val(name); + res->deduce_type(lex.cur()); + } + } + return res; +} + +// parse [ ~ ] E +Expr* parse_expr75(Lexer& lex, CodeBlob& code, bool nv) { + if (lex.tp() == '~') { + sym_idx_t name = symbols.lookup_add("~_"); + check_global_func(lex.cur(), name); + SrcLocation loc{lex.cur().loc}; + lex.next(); + auto x = parse_expr80(lex, code, false); + x->chk_rvalue(lex.cur()); + auto res = new Expr{Expr::_Apply, name, {x}}; + res->here = loc; + res->set_val('~'); + res->flags = Expr::_IsRvalue; + res->deduce_type(lex.cur()); + return res; + } else { + return parse_expr80(lex, code, nv); + } +} + +// parse E { (* | / | % | /% ) E } +Expr* parse_expr30(Lexer& lex, CodeBlob& code, bool nv) { + Expr* res = parse_expr75(lex, code, nv); + while (lex.tp() == '*' || lex.tp() == '/' || lex.tp() == '%' || lex.tp() == _DivMod || lex.tp() == _DivC || + lex.tp() == _DivR || lex.tp() == '&') { + res->chk_rvalue(lex.cur()); + int t = lex.tp(); + sym_idx_t name = symbols.lookup_add(std::string{"_"} + lex.cur().str + "_"); + SrcLocation loc{lex.cur().loc}; + check_global_func(lex.cur(), name); + lex.next(); + auto x = parse_expr75(lex, code, false); + x->chk_rvalue(lex.cur()); + res = new Expr{Expr::_Apply, name, {res, x}}; + res->here = loc; + res->set_val(t); + res->flags = Expr::_IsRvalue; + res->deduce_type(lex.cur()); + } + return res; +} + +// parse [-] E { (+ | - | `|` ) E } +Expr* parse_expr20(Lexer& lex, CodeBlob& code, bool nv) { + Expr* res; + int t = lex.tp(); + if (t == '-') { + sym_idx_t name = symbols.lookup_add("-_"); + check_global_func(lex.cur(), name); + SrcLocation loc{lex.cur().loc}; + lex.next(); + auto x = parse_expr30(lex, code, false); + x->chk_rvalue(lex.cur()); + res = new Expr{Expr::_Apply, name, {x}}; + res->here = loc; + res->set_val(t); + res->flags = Expr::_IsRvalue; + res->deduce_type(lex.cur()); + } else { + res = parse_expr30(lex, code, nv); + } + while (lex.tp() == '-' || lex.tp() == '+' || lex.tp() == '|') { + res->chk_rvalue(lex.cur()); + t = lex.tp(); + sym_idx_t name = symbols.lookup_add(std::string{"_"} + lex.cur().str + "_"); + check_global_func(lex.cur(), name); + SrcLocation loc{lex.cur().loc}; + lex.next(); + auto x = parse_expr30(lex, code, false); + x->chk_rvalue(lex.cur()); + res = new Expr{Expr::_Apply, name, {res, x}}; + res->here = loc; + res->set_val(t); + res->flags = Expr::_IsRvalue; + res->deduce_type(lex.cur()); + } + return res; +} + +// parse E { ( << | >> | >>~ | >>^ ) E } +Expr* parse_expr17(Lexer& lex, CodeBlob& code, bool nv) { + Expr* res = parse_expr20(lex, code, nv); + while (lex.tp() == _Lshift || lex.tp() == _Rshift || lex.tp() == _RshiftC || lex.tp() == _RshiftR) { + res->chk_rvalue(lex.cur()); + int t = lex.tp(); + sym_idx_t name = symbols.lookup_add(std::string{"_"} + lex.cur().str + "_"); + check_global_func(lex.cur(), name); + SrcLocation loc{lex.cur().loc}; + lex.next(); + auto x = parse_expr20(lex, code, false); + x->chk_rvalue(lex.cur()); + res = new Expr{Expr::_Apply, name, {res, x}}; + res->here = loc; + res->set_val(t); + res->flags = Expr::_IsRvalue; + res->deduce_type(lex.cur()); + } + return res; +} + +// parse E [ (== | < | > | <= | >= | != | <=> ) E ] +Expr* parse_expr15(Lexer& lex, CodeBlob& code, bool nv) { + Expr* res = parse_expr17(lex, code, nv); + if (lex.tp() == _Eq || lex.tp() == '<' || lex.tp() == '>' || lex.tp() == _Leq || lex.tp() == _Geq || + lex.tp() == _Neq || lex.tp() == _Spaceship) { + res->chk_rvalue(lex.cur()); + int t = lex.tp(); + sym_idx_t name = symbols.lookup_add(std::string{"_"} + lex.cur().str + "_"); + check_global_func(lex.cur(), name); + SrcLocation loc{lex.cur().loc}; + lex.next(); + auto x = parse_expr17(lex, code, false); + x->chk_rvalue(lex.cur()); + res = new Expr{Expr::_Apply, name, {res, x}}; + res->here = loc; + res->set_val(t); + res->flags = Expr::_IsRvalue; + res->deduce_type(lex.cur()); + } + return res; +} + +// parse E [ ? E : E ] +Expr* parse_expr13(Lexer& lex, CodeBlob& code, bool nv) { + Expr* res = parse_expr15(lex, code, nv); + if (lex.tp() == '?') { + res->chk_rvalue(lex.cur()); + SrcLocation loc{lex.cur().loc}; + lex.next(); + auto x = parse_expr(lex, code, false); + x->chk_rvalue(lex.cur()); + lex.expect(':'); + auto y = parse_expr13(lex, code, false); + y->chk_rvalue(lex.cur()); + res = new Expr{Expr::_CondExpr, {res, x, y}}; + res->here = loc; + res->flags = Expr::_IsRvalue; + res->deduce_type(lex.cur()); + } + return res; +} + +// parse LE1 (= | += | -= | ... ) E2 +Expr* parse_expr10(Lexer& lex, CodeBlob& code, bool nv) { + auto x = parse_expr13(lex, code, nv); + int t = lex.tp(); + if (t == _PlusLet || t == _MinusLet || t == _TimesLet || t == _DivLet || t == _DivRLet || t == _DivCLet || + t == _ModLet || t == _LshiftLet || t == _RshiftLet || t == _RshiftCLet || t == _RshiftRLet) { + x->chk_lvalue(lex.cur()); + x->chk_rvalue(lex.cur()); + sym_idx_t name = symbols.lookup_add(std::string{"^_"} + lex.cur().str + "_"); + check_global_func(lex.cur(), name); + SrcLocation loc{lex.cur().loc}; + lex.next(); + auto y = parse_expr10(lex, code, false); + y->chk_rvalue(lex.cur()); + Expr* z = new Expr{Expr::_Apply, name, {x, y}}; + z->here = loc; + z->set_val(t); + z->flags = Expr::_IsRvalue; + z->deduce_type(lex.cur()); + Expr* res = new Expr{Expr::_Letop, {x->copy(), z}}; + res->here = loc; + res->flags = (x->flags & ~Expr::_IsType) | Expr::_IsRvalue; + res->set_val(t); + res->deduce_type(lex.cur()); + return res; + } else if (t == '=') { + x->chk_lvalue(lex.cur()); + SrcLocation loc{lex.cur().loc}; + lex.next(); + auto y = parse_expr10(lex, code, false); + y->chk_rvalue(lex.cur()); + x->predefine_vars(); + x->define_new_vars(code); + Expr* res = new Expr{Expr::_Letop, {x, y}}; + res->here = loc; + res->flags = (x->flags & ~Expr::_IsType) | Expr::_IsRvalue; + res->set_val(t); + res->deduce_type(lex.cur()); + return res; + } else { + return x; + } +} + +Expr* parse_expr(Lexer& lex, CodeBlob& code, bool nv) { + return parse_expr10(lex, code, nv); +} + +namespace blk_fl { +enum { end = 1, ret = 2, empty = 4 }; +typedef int val; +constexpr val init = end | empty; +void combine(val& x, const val y) { + x |= y & ret; + x &= y | ~(end | empty); +} +void combine_parallel(val& x, const val y) { + x &= y | ~(ret | empty); + x |= y & end; +} +} // namespace blk_fl + +blk_fl::val parse_return_stmt(Lexer& lex, CodeBlob& code) { + auto expr = parse_expr(lex, code); + expr->chk_rvalue(lex.cur()); + try { + // std::cerr << "in return: "; + unify(expr->e_type, code.ret_type); + } catch (UnifyError& ue) { + std::ostringstream os; + os << "previous function return type " << code.ret_type + << " cannot be unified with return statement expression type " << expr->e_type << ": " << ue; + lex.cur().error(os.str()); + } + std::vector tmp_vars = expr->pre_compile(code); + code.emplace_back(lex.cur().loc, Op::_Return, std::move(tmp_vars)); + lex.expect(';'); + return blk_fl::ret; +} + +blk_fl::val parse_implicit_ret_stmt(Lexer& lex, CodeBlob& code) { + auto ret_type = TypeExpr::new_unit(); + try { + // std::cerr << "in implicit return: "; + unify(ret_type, code.ret_type); + } catch (UnifyError& ue) { + std::ostringstream os; + os << "previous function return type " << code.ret_type + << " cannot be unified with implicit end-of-block return type " << ret_type << ": " << ue; + lex.cur().error(os.str()); + } + code.emplace_back(lex.cur().loc, Op::_Return); + return blk_fl::ret; +} + +blk_fl::val parse_stmt(Lexer& lex, CodeBlob& code); + +blk_fl::val parse_block_stmt(Lexer& lex, CodeBlob& code, bool no_new_scope = false) { + lex.expect('{'); + if (!no_new_scope) { + sym::open_scope(lex); + } + blk_fl::val res = blk_fl::init; + bool warned = false; + while (lex.tp() != '}') { + if (!(res & blk_fl::end) && !warned) { + lex.cur().loc.show_warning("unreachable code"); + warned = true; + } + blk_fl::combine(res, parse_stmt(lex, code)); + } + if (!no_new_scope) { + sym::close_scope(lex); + } + lex.expect('}'); + return res; +} + +blk_fl::val parse_repeat_stmt(Lexer& lex, CodeBlob& code) { + SrcLocation loc{lex.cur().loc}; + lex.expect(_Repeat); + auto expr = parse_expr(lex, code); + expr->chk_rvalue(lex.cur()); + auto cnt_type = TypeExpr::new_atomic(_Int); + try { + unify(expr->e_type, cnt_type); + } catch (UnifyError& ue) { + std::ostringstream os; + os << "repeat count value of type " << expr->e_type << " is not an integer: " << ue; + lex.cur().error(os.str()); + } + std::vector tmp_vars = expr->pre_compile(code); + if (tmp_vars.size() != 1) { + lex.cur().error("repeat count value is not a singleton"); + } + Op& repeat_op = code.emplace_back(loc, Op::_Repeat, tmp_vars); + code.push_set_cur(repeat_op.block0); + blk_fl::val res = parse_block_stmt(lex, code); + code.close_pop_cur(lex.cur().loc); + return res | blk_fl::end; +} + +blk_fl::val parse_while_stmt(Lexer& lex, CodeBlob& code) { + SrcLocation loc{lex.cur().loc}; + lex.expect(_While); + auto expr = parse_expr(lex, code); + expr->chk_rvalue(lex.cur()); + auto cnt_type = TypeExpr::new_atomic(_Int); + try { + unify(expr->e_type, cnt_type); + } catch (UnifyError& ue) { + std::ostringstream os; + os << "while condition value of type " << expr->e_type << " is not an integer: " << ue; + lex.cur().error(os.str()); + } + Op& while_op = code.emplace_back(loc, Op::_While); + code.push_set_cur(while_op.block0); + while_op.left = expr->pre_compile(code); + code.close_pop_cur(lex.cur().loc); + if (while_op.left.size() != 1) { + lex.cur().error("while condition value is not a singleton"); + } + code.push_set_cur(while_op.block1); + blk_fl::val res1 = parse_block_stmt(lex, code); + code.close_pop_cur(lex.cur().loc); + return res1 | blk_fl::end; +} + +blk_fl::val parse_do_stmt(Lexer& lex, CodeBlob& code) { + Op& while_op = code.emplace_back(lex.cur().loc, Op::_Until); + lex.expect(_Do); + code.push_set_cur(while_op.block0); + sym::open_scope(lex); + blk_fl::val res = parse_block_stmt(lex, code, true); + lex.expect(_Until); + auto expr = parse_expr(lex, code); + expr->chk_rvalue(lex.cur()); + sym::close_scope(lex); + auto cnt_type = TypeExpr::new_atomic(_Int); + try { + unify(expr->e_type, cnt_type); + } catch (UnifyError& ue) { + std::ostringstream os; + os << "`until` condition value of type " << expr->e_type << " is not an integer: " << ue; + lex.cur().error(os.str()); + } + while_op.left = expr->pre_compile(code); + code.close_pop_cur(lex.cur().loc); + if (while_op.left.size() != 1) { + lex.cur().error("`until` condition value is not a singleton"); + } + return res & ~blk_fl::empty; +} + +blk_fl::val parse_if_stmt(Lexer& lex, CodeBlob& code, int first_lex = _If) { + SrcLocation loc{lex.cur().loc}; + lex.expect(first_lex); + auto expr = parse_expr(lex, code); + expr->chk_rvalue(lex.cur()); + auto flag_type = TypeExpr::new_atomic(_Int); + try { + unify(expr->e_type, flag_type); + } catch (UnifyError& ue) { + std::ostringstream os; + os << "`if` condition value of type " << expr->e_type << " is not an integer: " << ue; + lex.cur().error(os.str()); + } + std::vector tmp_vars = expr->pre_compile(code); + if (tmp_vars.size() != 1) { + lex.cur().error("condition value is not a singleton"); + } + Op& if_op = code.emplace_back(loc, Op::_If, tmp_vars); + code.push_set_cur(if_op.block0); + blk_fl::val res1 = parse_block_stmt(lex, code); + blk_fl::val res2 = blk_fl::init; + code.close_pop_cur(lex.cur().loc); + if (lex.tp() == _Else) { + lex.expect(_Else); + code.push_set_cur(if_op.block1); + res2 = parse_block_stmt(lex, code); + code.close_pop_cur(lex.cur().loc); + } else if (lex.tp() == _Elseif || lex.tp() == _Elseifnot) { + code.push_set_cur(if_op.block1); + res2 = parse_if_stmt(lex, code, lex.tp()); + code.close_pop_cur(lex.cur().loc); + } else { + if_op.block1 = std::make_unique(lex.cur().loc, Op::_Nop); + } + if (first_lex == _Ifnot || first_lex == _Elseifnot) { + std::swap(if_op.block0, if_op.block1); + } + blk_fl::combine_parallel(res1, res2); + return res1; +} + +blk_fl::val parse_stmt(Lexer& lex, CodeBlob& code) { + switch (lex.tp()) { + case _Return: { + lex.next(); + return parse_return_stmt(lex, code); + } + case '{': { + return parse_block_stmt(lex, code); + } + case ';': { + lex.next(); + return blk_fl::init; + } + case _Repeat: + return parse_repeat_stmt(lex, code); + case _If: + case _Ifnot: + return parse_if_stmt(lex, code, lex.tp()); + case _Do: + return parse_do_stmt(lex, code); + case _While: + return parse_while_stmt(lex, code); + default: { + auto expr = parse_expr(lex, code); + expr->chk_rvalue(lex.cur()); + expr->pre_compile(code); + lex.expect(';'); + return blk_fl::end; + } + } +} + +CodeBlob* parse_func_body(Lexer& lex, FormalArgList arg_list, TypeExpr* ret_type) { + lex.expect('{'); + CodeBlob* blob = new CodeBlob{ret_type}; + blob->import_params(std::move(arg_list)); + blk_fl::val res = blk_fl::init; + bool warned = false; + while (lex.tp() != '}') { + if (!(res & blk_fl::end) && !warned) { + lex.cur().loc.show_warning("unreachable code"); + warned = true; + } + blk_fl::combine(res, parse_stmt(lex, *blob)); + } + if (res & blk_fl::end) { + parse_implicit_ret_stmt(lex, *blob); + } + blob->close_blk(lex.cur().loc); + lex.expect('}'); + return blob; +} + +SymValAsmFunc* parse_asm_func_body(Lexer& lex, TypeExpr* func_type, const FormalArgList& arg_list, TypeExpr* ret_type, + bool impure = false) { + auto loc = lex.cur().loc; + lex.expect(_Asm); + int cnt = (int)arg_list.size(); + int width = ret_type->get_width(); + if (width < 0 || width > 16) { + throw src::ParseError{loc, "return type of an assembler built-in function must have a well-defined fixed width"}; + } + if (arg_list.size() > 16) { + throw src::ParseError{loc, "assembler built-in function must have at most 16 arguments"}; + } + std::vector cum_arg_width; + cum_arg_width.push_back(0); + int tot_width = 0; + for (auto& arg : arg_list) { + int arg_width = std::get(arg)->get_width(); + if (arg_width < 0 || arg_width > 16) { + throw src::ParseError{std::get(arg), + "parameters of an assembler built-in function must have a well-defined fixed width"}; + } + cum_arg_width.push_back(tot_width += arg_width); + } + std::vector asm_ops; + std::vector arg_order, ret_order; + if (lex.tp() == '(') { + lex.expect('('); + if (lex.tp() != _Mapsto) { + std::vector visited(cnt, false); + for (int i = 0; i < cnt; i++) { + if (lex.tp() != _Ident) { + lex.expect(_Ident); + } + auto sym = sym::lookup_symbol(lex.cur().val); + int j; + for (j = 0; j < cnt; j++) { + if (std::get(arg_list[j]) == sym) { + break; + } + } + if (j == cnt) { + lex.cur().error("formal argument name expected"); + } + if (visited[j]) { + lex.cur().error("formal argument listed twice"); + } + visited[j] = true; + int c1 = cum_arg_width[j], c2 = cum_arg_width[j + 1]; + while (c1 < c2) { + arg_order.push_back(c1++); + } + lex.next(); + } + assert(arg_order.size() == (unsigned)tot_width); + } + if (lex.tp() == _Mapsto) { + lex.expect(_Mapsto); + std::vector visited(width, false); + for (int i = 0; i < width; i++) { + if (lex.tp() != Lexem::Number || lex.cur().str.size() > 3) { + lex.expect(Lexem::Number); + } + int j = atoi(lex.cur().str.c_str()); + if (j < 0 || j >= width || visited[j]) { + lex.cur().error("expected integer return value index 0 .. width-1"); + } + visited[j] = true; + ret_order.push_back(j); + lex.next(); + } + } + lex.expect(')'); + } + while (lex.tp() == _String) { + asm_ops.push_back(AsmOp::Parse(lex.cur().str, cnt, width)); + lex.next(); + if (asm_ops.back().is_custom()) { + cnt = width; + } + } + if (asm_ops.empty()) { + throw src::ParseError{lex.cur().loc, "string with assembler instruction expected"}; + } + lex.expect(';'); + auto res = new SymValAsmFunc{func_type, asm_ops, impure}; + res->arg_order = std::move(arg_order); + res->ret_order = std::move(ret_order); + return res; +} + +void parse_func_def(Lexer& lex) { + SrcLocation loc{lex.cur().loc}; + sym::open_scope(lex); + auto ret_type = parse_type(lex); + if (lex.tp() != _Ident) { + throw src::ParseError{lex.cur().loc, "function name identifier expected"}; + } + Lexem func_name = lex.cur(); + lex.next(); + FormalArgList arg_list = parse_formal_args(lex); + bool impure = (lex.tp() == _Impure); + if (impure) { + lex.next(); + } + td::RefInt256 method_id; + std::string method_name; + if (lex.tp() == _MethodId) { + lex.next(); + if (lex.tp() == '(') { + lex.expect('('); + if (lex.tp() == Lexem::String) { + method_name = lex.cur().str; + } else if (lex.tp() == Lexem::Number) { + method_name = lex.cur().str; + method_id = td::string_to_int256(method_name); + if (method_id.is_null()) { + lex.cur().error_at("invalid integer constant `", "`"); + } + } else { + throw src::ParseError{lex.cur().loc, "integer or string method identifier expected"}; + } + lex.next(); + lex.expect(')'); + } else { + method_name = func_name.str; + } + if (method_id.is_null()) { + unsigned crc = td::crc16(method_name); + method_id = td::make_refint((crc & 0xffff) | 0x10000); + } + } + if (lex.tp() != ';' && lex.tp() != '{' && lex.tp() != _Asm) { + lex.expect('{', "function body block expected"); + } + TypeExpr* func_type = TypeExpr::new_map(extract_total_arg_type(arg_list), ret_type); + if (verbosity >= 1) { + std::cerr << "function " << func_name.str << " : " << func_type << std::endl; + } + SymDef* func_sym = sym::define_global_symbol(func_name.val, 0, loc); + assert(func_sym); + SymValFunc* func_sym_val = dynamic_cast(func_sym->value); + if (func_sym->value) { + if (func_sym->value->type != SymVal::_Func || !func_sym_val) { + lex.cur().error("was not defined as a function before"); + } + try { + unify(func_sym_val->sym_type, func_type); + } catch (UnifyError& ue) { + std::ostringstream os; + os << "previous type of function " << func_name.str << " : " << func_sym_val->sym_type + << " cannot be unified with new type " << func_type << ": " << ue; + lex.cur().error(os.str()); + } + } + if (lex.tp() == ';') { + make_new_glob_func(func_sym, func_type, impure); + lex.next(); + } else if (lex.tp() == '{') { + if (dynamic_cast(func_sym_val)) { + lex.cur().error("function `"s + func_name.str + "` has been already defined as an assembler built-in"); + } + SymValCodeFunc* func_sym_code; + if (func_sym_val) { + func_sym_code = dynamic_cast(func_sym_val); + if (!func_sym_code) { + lex.cur().error("function `"s + func_name.str + "` has been already defined in an yet-unknown way"); + } + } else { + func_sym_code = make_new_glob_func(func_sym, func_type, impure); + } + if (func_sym_code->code) { + lex.cur().error("redefinition of function `"s + func_name.str + "`"); + } + CodeBlob* code = parse_func_body(lex, arg_list, ret_type); + code->name = func_name.str; + code->loc = loc; + // code->print(std::cerr); // !!!DEBUG!!! + func_sym_code->code = code; + } else { + if (func_sym_val) { + if (dynamic_cast(func_sym_val)) { + lex.cur().error("function `"s + func_name.str + "` was already declared as an ordinary function"); + } + if (dynamic_cast(func_sym_val)) { + lex.cur().error("redefinition of built-in assembler function `"s + func_name.str + "`"); + } + lex.cur().error("redefinition of previously (somehow) defined function `"s + func_name.str + "`"); + } + func_sym->value = parse_asm_func_body(lex, func_type, arg_list, ret_type, impure); + } + if (method_id.not_null()) { + auto val = dynamic_cast(func_sym->value); + if (!val) { + lex.cur().error("cannot set method id for unknown function `"s + func_name.str + "`"); + } + if (val->method_id.is_null()) { + val->method_id = std::move(method_id); + } else if (val->method_id != method_id) { + lex.cur().error("integer method identifier for `"s + func_name.str + "` changed to a different value"); + } + } + if (verbosity >= 1) { + std::cerr << "new type of function " << func_name.str << " : " << func_type << std::endl; + } + sym::close_scope(lex); +} + +bool parse_source(std::istream* is, const src::FileDescr* fdescr) { + src::SourceReader reader{is, fdescr}; + Lexer lex{reader, true}; + while (lex.tp() != _Eof) { + parse_func_def(lex); + } + return true; +} + +bool parse_source_file(const char* filename) { + if (!filename || !*filename) { + throw src::Fatal{"source file name is an empty string"}; + } + src::FileDescr* cur_source = new src::FileDescr{filename}; + std::ifstream ifs{filename}; + if (ifs.fail()) { + throw src::Fatal{std::string{"cannot open source file `"} + filename + "`"}; + } + return parse_source(&ifs, cur_source); +} + +bool parse_source_stdin() { + return parse_source(&std::cin, new src::FileDescr{"stdin", true}); +} + +} // namespace funC diff --git a/crypto/func/stack-transform.cpp b/crypto/func/stack-transform.cpp new file mode 100644 index 00000000..f682b490 --- /dev/null +++ b/crypto/func/stack-transform.cpp @@ -0,0 +1,826 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "func.h" + +namespace funC { + +/* + * + * GENERIC STACK TRANSFORMATIONS + * + */ + +StackTransform::StackTransform(std::initializer_list list) { + *this = list; +} + +StackTransform &StackTransform::operator=(std::initializer_list list) { + if (list.size() > 255) { + invalidate(); + return *this; + } + set_id(); + if (!list.size()) { + return *this; + } + int m = (int)list.size(); + d = list.begin()[m - 1] - (m - 1); + if (d >= 128 || d < -128) { + invalidate(); + return *this; + } + for (int i = 0; i < m - 1; i++) { + int x = d + i; + int y = list.begin()[i]; + if (y != x) { + if (x != (short)x || y != (short)y || n == max_n) { + invalidate(); + return *this; + } + dp = std::max(dp, std::max(x, y) + 1); + A[n++] = std::make_pair((short)x, (short)y); + } + } + return *this; +} + +bool StackTransform::assign(const StackTransform &other) { + if (!other.is_valid() || (unsigned)other.n > max_n) { + return invalidate(); + } + d = other.d; + n = other.n; + dp = other.dp; + c = other.c; + invalid = false; + for (int i = 0; i < n; i++) { + A[i] = other.A[i]; + } + return true; +} + +int StackTransform::get(int x) const { + if (!is_valid()) { + return -1; + } + if (x <= c_start) { + return x - c; + } + x += d; + int i; + for (i = 0; i < n && A[i].first < x; i++) { + } + if (i < n && A[i].first == x) { + return A[i].second; + } else { + return x; + } +} + +bool StackTransform::set(int x, int y, bool relaxed) { + if (!is_valid()) { + return false; + } + if (x < 0) { + return (relaxed && y == x + d) || invalidate(); + } + if (!relaxed) { + touch(x); + } + x += d; + int i; + for (i = 0; i < n && A[i].first < x; i++) { + } + if (i < n && A[i].first == x) { + if (x != y) { + if (y != (short)y) { + return invalidate(); + } + A[i].second = (short)y; + } else { + --n; + for (; i < n; i++) { + A[i] = A[i + 1]; + } + } + } else { + if (x != y) { + if (x != (short)x || y != (short)y || n == max_n) { + return invalidate(); + } + for (int j = n++; j > i; j--) { + A[j] = A[j - 1]; + } + A[i].first = (short)x; + A[i].second = (short)y; + touch(x - d); + touch(y); + } + } + return true; +} + +// f(x') = x' + d for all x' >= x ? +bool StackTransform::is_trivial_after(int x) const { + return is_valid() && (!n || A[n - 1].first < x + d); +} + +// card f^{-1}(y) +int StackTransform::preimage_count(int y) const { + if (!is_valid()) { + return -1; + } + int count = (y >= d); + for (const auto &pair : A) { + if (pair.second == y) { + ++count; + } else if (pair.first == y) { + --count; + } + } + return count; +} + +// f^{-1}(y) +std::vector StackTransform::preimage(int y) const { + if (!is_valid()) { + return {}; + } + std::vector res; + bool f = (y >= d); + for (const auto &pair : A) { + if (pair.first > y && f) { + res.push_back(y - d); + f = false; + } + if (pair.first == y) { + f = false; + } else if (pair.second == y) { + res.push_back(pair.first - d); + } + } + return res; +} + +// is f:N->N bijective ? +bool StackTransform::is_permutation() const { + if (!is_valid() || d) { + return false; + } + assert(n <= max_n); + std::array X, Y; + for (int i = 0; i < n; i++) { + X[i] = A[i].first; + Y[i] = A[i].second; + if (Y[i] < 0) { + return false; + } + } + std::sort(Y.begin(), Y.begin() + n); + for (int i = 0; i < n; i++) { + if (X[i] != Y[i]) { + return false; + } + } + return true; +} + +bool StackTransform::remove_negative() { + int s = 0; + while (s < n && A[s].first < d) { + ++s; + } + if (s) { + n -= s; + for (int i = 0; i < n; i++) { + A[i] = A[i + s]; + } + } + return true; +} + +int StackTransform::try_load(int &i, int offs) const { + return i < n ? A[i++].first + offs : inf_x; +} + +bool StackTransform::try_store(int x, int y) { + if (x == y || x < d) { + return true; + } + if (n == max_n || x != (short)x || y != (short)y) { + return invalidate(); + } + A[n].first = (short)x; + A[n++].second = (short)y; + return true; +} + +// c := a * b +bool StackTransform::compose(const StackTransform &a, const StackTransform &b, StackTransform &c) { + if (!a.is_valid() || !b.is_valid()) { + return c.invalidate(); + } + c.d = a.d + b.d; + c.n = 0; + c.dp = std::max(a.dp, b.dp + a.d); + c.c = a.c + b.c; + c.invalid = false; + int i = 0, j = 0; + int x1 = a.try_load(i); + int x2 = b.try_load(j, a.d); + while (true) { + if (x1 < x2) { + int y = a.A[i - 1].second; + if (!c.try_store(x1, y)) { + return false; + } + x1 = a.try_load(i); + } else if (x2 < inf_x) { + if (x1 == x2) { + x1 = a.try_load(i); + } + int y = b.A[j - 1].second; + if (!c.try_store(x2, a(y))) { + return false; + } + x2 = b.try_load(j, a.d); + } else { + return true; + } + } +} + +// this = this * other +bool StackTransform::apply(const StackTransform &other) { + StackTransform res; + if (!compose(*this, other, res)) { + return invalidate(); + } + return assign(res); +} + +// this = other * this +bool StackTransform::preapply(const StackTransform &other) { + StackTransform res; + if (!compose(other, *this, res)) { + return invalidate(); + } + return assign(res); +} + +StackTransform StackTransform::operator*(const StackTransform &b) const & { + StackTransform res; + compose(*this, b, res); + return res; +} + +// this = this * other +StackTransform &StackTransform::operator*=(const StackTransform &other) { + StackTransform res; + (compose(*this, other, res) && assign(res)) || invalidate(); + return *this; +} + +bool StackTransform::apply_xchg(int i, int j, bool relaxed) { + if (!is_valid() || i < 0 || j < 0) { + return invalidate(); + } + if (i == j) { + return relaxed || touch(i); + } + int u = touch_get(i), v = touch_get(j); + return set(i, v) && set(j, u); +} + +bool StackTransform::apply_push(int i) { + if (!is_valid() || i < 0) { + return invalidate(); + } + int u = touch_get(i); + return shift(-1) && set(0, u); +} + +bool StackTransform::apply_push_newconst() { + if (!is_valid()) { + return false; + } + return shift(-1) && set(0, c_start - c++); +} + +bool StackTransform::apply_pop(int i) { + if (!is_valid() || i < 0) { + return invalidate(); + } + if (!i) { + return touch(0) && shift(1); + } else { + return set(i, get(0)) && shift(1); + } +} + +bool StackTransform::equal(const StackTransform &other, bool relaxed) const { + if (!is_valid() || !other.is_valid()) { + return false; + } + if (!(n == other.n && d == other.d)) { + return false; + } + for (int i = 0; i < n; i++) { + if (A[i] != other.A[i]) { + return false; + } + } + return relaxed || dp == other.dp; +} + +StackTransform StackTransform::Xchg(int i, int j, bool relaxed) { + StackTransform t; + t.apply_xchg(i, j, relaxed); + return t; +} + +StackTransform StackTransform::Push(int i) { + StackTransform t; + t.apply_push(i); + return t; +} + +StackTransform StackTransform::Pop(int i) { + StackTransform t; + t.apply_pop(i); + return t; +} + +bool StackTransform::is_xchg(int i, int j) const { + if (i == j) { + return is_id(); + } + return is_valid() && !d && n == 2 && i >= 0 && j >= 0 && get(i) == j && get(j) == i; +} + +bool StackTransform::is_xchg(int *i, int *j) const { + if (!is_valid() || d || n > 2 || !dp) { + return false; + } + if (!n) { + *i = *j = 0; + return true; + } + if (n != 2) { + return false; + } + int a = A[0].first, b = A[1].first; + if (A[0].second != b || A[1].second != a) { + return false; + } + *i = std::min(a, b); + *j = std::max(a, b); + return true; +} + +bool StackTransform::is_push(int i) const { + return is_valid() && d == -1 && n == 1 && A[0].first == -1 && A[0].second == i; +} + +bool StackTransform::is_push(int *i) const { + if (is_valid() && d == -1 && n == 1 && A[0].first == -1 && A[0].second >= 0) { + *i = A[0].second; + return true; + } else { + return false; + } +} + +// 1 2 3 4 .. = pop0 +// 0 2 3 4 .. = pop1 +// 1 0 3 4 .. = pop2 +// 1 2 0 4 .. = pop3 +bool StackTransform::is_pop(int i) const { + if (!is_valid() || d != 1 || n > 1 || i < 0) { + return false; + } + if (!i) { + return !n; + } + return n == 1 && A[0].first == i && !A[0].second; +} + +bool StackTransform::is_pop(int *i) const { + if (!is_valid() || d != 1 || n > 1) { + return false; + } + if (!n) { + *i = 0; + return true; + } + if (n == 1 && !A[0].second) { + *i = A[0].first; + return true; + } + return false; +} + +const StackTransform StackTransform::rot{2, 0, 1, 3}; +const StackTransform StackTransform::rot_rev{1, 2, 0, 3}; + +bool StackTransform::is_rot() const { + return equal(rot, true); +} + +bool StackTransform::is_rotrev() const { + return equal(rot_rev, true); +} + +// XCHG s1,s(i) ; XCHG s0,s(j) +bool StackTransform::is_xchg2(int i, int j) const { + StackTransform t; + return is_valid() && !d && t.apply_xchg(1, i) && t.apply_xchg(0, j) && t <= *this; +} + +bool StackTransform::is_xchg2(int *i, int *j) const { + if (!is_valid() || d || n > 4 || n == 1 || dp < 2) { + return false; + } + *i = get(1); + *j = get(0); + if (!n) { + return true; + } + if (*i < 0 || *j < 0) { + return false; + } + if (n != 3) { + return is_xchg2(*i, *j); + } + if (*i) { + // XCHG2 s(i),s(i) = XCHG s1,s(i) ; XCHG s0,s(i) : 0->1, 1->i + *j = *i; + } // XCHG2 s0,s(i) = XCHG s0,s1 ; XCHG s0,s(i) : 0->i, 1->0 + return is_xchg2(*i, *j); +} + +// XCHG s0,s(i) ; PUSH s(j) = PUSH s(j') ; XCHG s1,s(i+1) +// j'=j if j!=0, j!=i +// j'=0 if j=i +// j'=i if j=0 +bool StackTransform::is_xcpu(int i, int j) const { + StackTransform t; + return is_valid() && d == -1 && t.apply_xchg(0, i) && t.apply_push(j) && t <= *this; +} + +bool StackTransform::is_xcpu(int *i, int *j) const { + if (!is_valid() || d != -1 || n > 3 || dp < 1) { + return false; + } + *i = get(1); + *j = get(0); + if (!*j) { + *j = *i; + } else if (*j == *i) { + *j = 0; + } + return is_xcpu(*i, *j); +} + +// PUSH s(i) ; XCHG s0, s1 ; XCHG s0, s(j+1) +bool StackTransform::is_puxc(int i, int j) const { + StackTransform t; + return is_valid() && d == -1 && t.apply_push(i) && t.apply_xchg(0, 1) && t.apply_xchg(0, j + 1) && t <= *this; +} + +// j > 0 : 0 -> j, 1 -> i +// j = 0 : 0 -> i, 1 -> 0 ( PUSH s(i) ) +// j = -1 : 0 -> 0, 1 -> i ( PUSH s(i) ; XCHG s0, s1 ) +bool StackTransform::is_puxc(int *i, int *j) const { + if (!is_valid() || d != -1 || n > 3) { + return false; + } + *i = get(1); + *j = get(0); + if (!*i && is_push(*j)) { + std::swap(*i, *j); + return is_puxc(*i, *j); + } + if (!*j) { + --*j; + } + return is_puxc(*i, *j); +} + +// PUSH s(i) ; PUSH s(j+1) +bool StackTransform::is_push2(int i, int j) const { + StackTransform t; + return is_valid() && d == -2 && t.apply_push(i) && t.apply_push(j + 1) && t <= *this; +} + +bool StackTransform::is_push2(int *i, int *j) const { + if (!is_valid() || d != -2 || n > 2) { + return false; + } + *i = get(1); + *j = get(0); + return is_push2(*i, *j); +} + +// XCHG s2,s(i) ; XCHG s1,s(j) ; XCHG s0,s(k) +bool StackTransform::is_xchg3(int *i, int *j, int *k) const { + if (!is_valid() || d || dp < 3 || !is_permutation()) { + return false; + } + for (int s = 2; s >= 0; s--) { + *i = get(s); + StackTransform t = Xchg(2, *i) * *this; + if (t.is_xchg2(j, k)) { + return true; + } + } + return false; +} + +// XCHG s1,s(i) ; XCHG s0,s(j) ; PUSH s(k) +bool StackTransform::is_xc2pu(int *i, int *j, int *k) const { + if (!is_valid() || d != -1 || dp < 2) { + return false; + } + for (int s = 2; s >= 1; s--) { + *i = get(s); + StackTransform t = Xchg(1, *i) * *this; + if (t.is_xcpu(j, k)) { + return true; + } + } + return false; +} + +// XCHG s1,s(i) ; PUSH s(j) ; XCHG s0,s1 ; XCHG s0,s(k+1) +bool StackTransform::is_xcpuxc(int *i, int *j, int *k) const { + if (!is_valid() || d != -1 || dp < 2) { + return false; + } + for (int s = 2; s >= 0; s--) { + *i = get(s); + StackTransform t = Xchg(1, *i) * *this; + if (t.is_puxc(j, k)) { + return true; + } + } + return false; +} + +// XCHG s0,s(i) ; PUSH s(j) ; PUSH s(k+1) +bool StackTransform::is_xcpu2(int *i, int *j, int *k) const { + if (!is_valid() || d != -2 || dp < 1) { + return false; + } + *i = get(2); + StackTransform t = Xchg(0, *i) * *this; + return t.is_push2(j, k); +} + +// PUSH s(i) ; XCHG s0,s2 ; XCHG s1,s(j+1) ; XCHG s0,s(k+1) +// 0 -> i or 1 -> i or 2 -> i ; i has two preimages +// 0 -> k if k >= 2, k != j +// 1 -> j=k if j = k >= 2 +// 1 -> j if j >= 2, k != 0 +// 0 -> j if j >= 2, k = 0 +// => i in {f(0), f(1), f(2)} ; j in {-1, 0, 1, f(0), f(1)} ; k in {-1, 0, 1, f(0), f(1)} +bool StackTransform::is_puxc2(int *i, int *j, int *k) const { + if (!is_valid() || d != -1 || dp < 2) { + return false; + } + for (int s = 2; s >= 0; s--) { + *i = get(s); + if (preimage_count(*i) != 2) { + continue; + } + for (int u = -1; u <= 3; u++) { + *j = (u >= 2 ? get(u - 2) : u); + for (int v = -1; v <= 3; v++) { + *k = (v >= 2 ? get(v - 2) : v); + if (is_puxc2(*i, *j, *k)) { + return true; + } + } + } + } + return false; +} + +// PUSH s(i) ; XCHG s0,s2 ; XCHG s1,s(j+1) ; XCHG s0,s(k+1) +bool StackTransform::is_puxc2(int i, int j, int k) const { + StackTransform t; + return is_valid() && d == -1 && dp >= 2 // basic checks + && t.apply_push(i) && t.apply_xchg(0, 2) // PUSH s(i) ; XCHG s0,s2 + && t.apply_xchg(1, j + 1) // XCHG s1,s(j+1) + && t.apply_xchg(0, k + 1) && t <= *this; // XCHG s0,s(k+2) +} + +// PUSH s(i) ; XCHG s0,s1 ; XCHG s0,s(j+1) ; PUSH s(k+1) +bool StackTransform::is_puxcpu(int *i, int *j, int *k) const { + if (!is_valid() || d != -2 || dp < 1) { + return false; + } + StackTransform t = *this; + if (t.apply_pop() && t.is_puxc(i, j)) { + int y = get(0); + auto v = t.preimage(y); + if (!v.empty()) { + *k = v[0] - 1; + t.apply_push(*k + 1); + return t <= *this; + } + } + return false; +} + +// PUSH s(i) ; XCHG s0,s1 ; PUSH s(j+1) ; XCHG s0,s1 ; XCHG s0,s(k+2) +// 2 -> i; 1 -> j (if j >= 1, k != -1), 1 -> i (if j = 0, k != -1), 1 -> 0 (if j = -1, k != -1) +// 0 -> k (if k >= 1), 0 -> i (if k = 0), 0 -> j (if k = -1, j >= 1) +bool StackTransform::is_pu2xc(int *i, int *j, int *k) const { + if (!is_valid() || d != -2 || dp < 1) { + return false; + } + *i = get(2); + for (int v = -2; v <= 1; v++) { + *k = (v <= 0 ? v : get(0)); // one of -2, -1, 0, get(0) + for (int u = -1; u <= 1; u++) { + *j = (u <= 0 ? u : get(v != -1)); // one of -1, 0, get(0), get(1) + if (is_pu2xc(*i, *j, *k)) { + return true; + } + } + } + return false; +} + +bool StackTransform::is_pu2xc(int i, int j, int k) const { + StackTransform t; + return is_valid() && d == -2 && dp >= 1 // basic checks + && t.apply_push(i) && t.apply_xchg(0, 1) // PUSH s(i) ; XCHG s0,s1 + && t.apply_push(j + 1) && t.apply_xchg(0, 1) // PUSH s(j+1) ; XCHG s0,s1 + && t.apply_xchg(0, k + 2) && t <= *this; // XCHG s0,s(k+2) +} + +// PUSH s(i) ; PUSH s(j+1) ; PUSH s(k+2) +bool StackTransform::is_push3(int i, int j, int k) const { + StackTransform t; + return is_valid() && d == -3 && t.apply_push(i) && t.apply_push(j + 1) && t.apply_push(k + 2) && t <= *this; +} + +bool StackTransform::is_push3(int *i, int *j, int *k) const { + if (!is_valid() || d != -3 || n > 3) { + return false; + } + *i = get(2); + *j = get(1); + *k = get(0); + return is_push3(*i, *j, *k); +} + +bool StackTransform::is_blkswap(int *i, int *j) const { + if (!is_valid() || d || !is_permutation()) { + return false; + } + *j = get(0); + if (*j <= 0) { + return false; + } + auto v = preimage(0); + if (v.size() != 1) { + return false; + } + *i = v[0]; + return *i > 0 && is_blkswap(*i, *j); +} + +bool StackTransform::is_blkswap(int i, int j) const { + if (!is_valid() || d || i <= 0 || j <= 0 || dp < i + j || !is_trivial_after(i + j)) { + return false; + } + for (int s = 0; s < i; s++) { + if (get(s) != s + j) { + return false; + } + } + for (int s = 0; s < j; s++) { + if (get(s + i) != s) { + return false; + } + } + return true; +} + +// equivalent to i times DROP +bool StackTransform::is_blkdrop(int *i) const { + if (is_valid() && d > 0 && !n) { + *i = d; + return true; + } + return false; +} + +// equivalent to i times PUSH s(j) +bool StackTransform::is_blkpush(int *i, int *j) const { + if (!is_valid() || d >= 0) { + return false; + } + *i = -d; + *j = get(*i - 1); + return is_blkpush(*i, *j); +} + +bool StackTransform::is_blkpush(int i, int j) const { + if (!is_valid() || d >= 0 || d != -i || j < 0 || dp < i + j || !is_trivial_after(i)) { + return false; + } + StackTransform t; + for (int s = 0; s < i; s++) { + if (!t.apply_push(j)) { + return false; + } + } + return t <= *this; +} + +bool StackTransform::is_reverse(int *i, int *j) const { + if (!is_valid() || d || !is_permutation() || n < 2) { + return false; + } + *j = A[0].first; + *i = A[n - 1].first - A[0].first + 1; + return is_reverse(*i, *j); +} + +bool StackTransform::is_reverse(int i, int j) const { + if (!is_valid() || d || !is_trivial_after(i + j) || n < 2 || A[0].first != j || A[n - 1].first != j + i - 1) { + return false; + } + for (int s = 0; s < i; s++) { + if (get(j + s) != j + i - 1 - s) { + return false; + } + } + return true; +} + +// 0 i+1 i+2 ... == i*NIP +// j i+1 i+2 ... == XCHG s(i),s(j) ; BLKDROP i +bool StackTransform::is_nip_seq(int i, int j) const { + return is_valid() && d == i && i > j && j >= 0 && n == 1 && A[0].first == i && A[0].second == j; +} + +bool StackTransform::is_nip_seq(int *i) const { + *i = d; + return is_nip_seq(*i); +} + +bool StackTransform::is_nip_seq(int *i, int *j) const { + if (is_valid() && n > 0) { + *i = d; + *j = A[0].second; + return is_nip_seq(*i, *j); + } else { + return false; + } +} + +void StackTransform::show(std::ostream &os, int mode) const { + if (!is_valid()) { + os << ""; + return; + } + int mi = 0, ma = 0; + if (n > 0 && A[0].first < d) { + mi = A[0].first - d; + } + if (n > 0) { + ma = std::max(ma, A[n - 1].first - d + 1); + } + ma = std::max(ma + 1, dp - d); + os << '{'; + if (dp == d) { + os << '|'; + } + for (int i = mi; i < ma; i++) { + os << get(i) << (i == -1 ? '?' : (i == dp - d - 1 ? '|' : ' ')); + } + os << get(ma) << "..}"; +} + +} // namespace funC diff --git a/crypto/func/test/a6.fc b/crypto/func/test/a6.fc new file mode 100644 index 00000000..06eeed9f --- /dev/null +++ b/crypto/func/test/a6.fc @@ -0,0 +1,85 @@ +(int, int) f(int a, int b, int c, int d, int e, int f) { + ;; solve a 2x2 linear equation + int D = a * d - b * c; + int Dx = e * d - b * f; + int Dy = a * f - e * c; + return (Dx / D, Dy / D); +} + +int calc_phi() { + var n = 1; + repeat (70) { n *= 10; } + var p = var q = 1; + do { + (p, q) = (q, p + q); + } until (q > n); + return muldivr(p, n, q); +} + +int calc_sqrt2() { + var n = 1; + repeat (70) { n *= 10; } + var p = var q = 1; + do { + var t = p + q; + (p, q) = (q, t + q); + } until (q > n); + return muldivr(p, n, q); +} + +var calc_root(m) { + int base = 1; + repeat(70) { base *= 10; } + var (a, b, c) = (1, 0, - m); + var (p1, q1, p2, q2) = (1, 0, 0, 1); + do { + int k = -1; + var (a1, b1, c1) = (0, 0, 0); + do { + k += 1; + (a1, b1, c1) = (a, b, c); + c += b; + c += b += a; + } until (c > 0); + (a, b, c) = (- c1, - b1, - a1); + (p1, q1) = (k * p1 + q1, p1); + (p2, q2) = (k * p2 + q2, p2); + } until (p1 > base); + return (p1, q1, p2, q2); +} + +{- +operator _/%_ infix 20; + +(int, int) ((int x) /% (int y)) { + return (x / y, x % y); +} + +(int, int) _/%_ (int x, int y) { + return (x / y, x % y); +} +-} +{- +< type A, type B, type C > +(B, C, A) rot (A x, B y, C z) { + return (y, z, x); +} +-} +int ataninv(int base, int q) { ;; computes base*atan(1/q) + base /~= q; + q *= - q; + int sum = 0; + int n = 1; + do { + sum += base /~ n; + base /~= q; + n += 2; + } until base == 0; + return sum; +} + +int calc_pi() { + int base = 64; + repeat (70) { base *= 10; } + return (ataninv(base << 2, 5) - ataninv(base, 239)) >>~ 4; +} diff --git a/crypto/func/test/a6.fp b/crypto/func/test/a6.fp new file mode 100644 index 00000000..00c2e76e --- /dev/null +++ b/crypto/func/test/a6.fp @@ -0,0 +1,75 @@ +f(int a, int b, int c, int d, int e, int f) : (int, int) { + var D = a * d - b * c; + var Dx : int = e * d - b * f; + var Dy : int = a * f - e * c; + return (Dx / D, Dy / D); +} + +calc_phi() : int = { + var n : int = 1; + repeat (10) { + n *= 10; + } + var p = var q = 1; + do { + (p, q) = (q, p + q); + } until q > n; + return muldivr(p, n, q); +} + +calc_sqrt2() : int = { + var n = 1; + repeat (70) { n *= 10; } + var p = var q = 1; + do { + var t = p + q; + (p, q) = (q, t + q); + } until q > n; + return muldivr(p, n, q); +} + +calc_phi() : int = { + var n = 1; + repeat (70) { n *= 10; } + var p = var q = 1; + do { + (p, q) = (q, p + q); + } until q > n; + return muldivr(p, n, q); +} + +operator _/%_ infix 20; + +(x : int) /% (y : int) : (int, int) = { + return (x / y, x % y); +} + +{- +_/%_ (int x, int y) : (int, int) = { + return (x / y, x % y); +} +-} + +rot < A : type, B : type, C : type > + (x : A, y : B, z : C) : (B, C, A) { + return (y, z, x); +} + +ataninv(base : int, q : int) : int { ;; computes base*atan(1/q) + base /~= q; + q *= - q; + var sum : int = 0; + var n = 1; + do { + sum += base /~ n; + base /~= q; + n += 2; + } while base; + return sum; +} + +calc_pi() : int { + var base = 64; + repeat (70) { base *= 10; } + return (ataninv(base << 2, 5) - ataninv(base, 239)) >>~ 4; +} diff --git a/crypto/func/test/a6_1.fc b/crypto/func/test/a6_1.fc new file mode 100644 index 00000000..9af7f254 --- /dev/null +++ b/crypto/func/test/a6_1.fc @@ -0,0 +1,6 @@ +(int, int) f(int a, int b, int c, int d, int e, int f) { + int D = a * d - b * c; + int Dx = e * d - b * f; + int Dy = a * f - e * c; + return (Dx / D, Dy / D); +} diff --git a/crypto/func/test/a6_2.fc b/crypto/func/test/a6_2.fc new file mode 100644 index 00000000..970ad624 --- /dev/null +++ b/crypto/func/test/a6_2.fc @@ -0,0 +1,130 @@ +var f(a, b, c, d, e, f) { + var D = a * d - b * c; + var Dx = e * d - b * f; + var Dy = a * f - e * c; + return (Dx / D, Dy / D); +} + +var test8() { + return -4; +} + +var test3(a) { + int x = a * 10; + a += 1; + int x = x + 3; + { + int y = x + 2; + x = y + a; + } + int z = 5; + return x * (z + a); +} + +var test2(a) { + (var x, var y) = a /% 10; + return x + y; +} + +var test2a(a) { + var (x, y) = a /% 10; + return x + y; +} + +var test2b(a) { + var z = a /% 10; + return _+_ z; +} + +var test2c(a) { + return _+_ (a /% 10); +} + +var twice(f, x) { + return f (f x); +} + +var test() { + return f(1, 2, 3, 4, 7, 17); +} + +var rot(a, b, c) { + return (b, c, a); +} + +var rot_int(int a, int b, int c) { + return (b, c, a); +} + +(int, _) dup_int(x) { + return (x, x); +} + +var calc_phi() { + var n = 10; + n *= n; + n *= n; + n *= n; + n *= n; + n *= n; + n *= n; + var p = var q = 1; + (p, q) = (q, p + q); + (p, q) = (q, p + q); + (p, q) = (q, p + q); + (p, q) = (q, p + q); + (p, q) = (q, p + q); + (p, q) = (q, p + q); + (p, q) = (q, p + q); + return muldivr(p, n, q); +} + +var t0() { + var x = 1; + return x; +} + +var t1() { + return 2; +} + +var t2(int x) { + return 2; + return x += 1; +} + +var t3(int x, var y) { + int z = (x + y) * (x - y); +} + +var t3b(int x, var y) { + int z = (x + y) * (x - y); + return z; +} + +var t4((int, int, int) z) { + var (_, u, _) = z; + return u; +} + +int foo(int t); + +var t5(x) { + var f = t2; + return twice(f, x) * f(x); +} + +var proj1(a, b) { return a; } +var proj1a(a, b) { var c = a; return c; } +var proj1b(a, b) { int c = a; return c; } +var proj1c(a, b) { var c = a + 2; return c; } +var proj1d(a, b) { return a + 2; } +var proj1e(int a, _) { return a; } +int proj1f(_ a, int) { return a; } +int proj1g(int, a) { return a; } + +var test1(a) { + a = a + 1; + return a; +} + diff --git a/crypto/func/test/a7.fc b/crypto/func/test/a7.fc new file mode 100644 index 00000000..f86be30a --- /dev/null +++ b/crypto/func/test/a7.fc @@ -0,0 +1,12 @@ +int steps(int x) { + var n = 0; + while (x > 1) { + n += 1; + if (x & 1) { + x = 3 * x + 1; + } else { + x >>= 1; + } + } + return n; +} diff --git a/crypto/func/test/a8.fc b/crypto/func/test/a8.fc new file mode 100644 index 00000000..1cd18a33 --- /dev/null +++ b/crypto/func/test/a8.fc @@ -0,0 +1,28 @@ +int A(int a, int b, int c, int d, int e, int f) { + return muldiv(a, b, c) + muldiv(d, e, f); +} + +int B(int a, int b, int c, int d, int e, int f) { + return muldiv(a, b, c) + muldiv(b, d, e) + f; +} + +int C(int a, int b, int c, int d, int e, int f) { + return muldiv(a, b, c) + muldiv(b, d, c) + muldiv(d, d, f); +} + +int D(int a, int b, int c, int d, int e, int f) { + return muldiv(a, b, c) + muldiv(b, d, c) + muldiv(e, e, f); +} + +int E(int a, int b, int c, int d, int e, int f) { + return muldiv(a, b, c) + muldiv(c, d, e) + muldiv(c, c, f); +} + +int F(int a, int b, int c, int d, int e, int f) { + return muldiv(a, b, c) + muldiv(a, d, c) + muldiv(f, f, e); +} + +int G(int a, int b, int c, int d, int e, int f) { + return muldiv(a, b, c) + muldiv(b, c, d) + muldiv(b, c, e) + f; +} + diff --git a/crypto/func/test/a9.fc b/crypto/func/test/a9.fc new file mode 100644 index 00000000..e9cc1450 --- /dev/null +++ b/crypto/func/test/a9.fc @@ -0,0 +1,23 @@ +int f(int x) { + if (2 * x + 1 == 7) { + return x + 17; + } else { + return 239; + } +} + +int f2(int x) { + return 2 * x + 1 == 6 ? x + 17 : 239; +} + +int g(int x) { + return x & 1 ? 3 * x + 1 : x / 2; +} + +_ H(int x, int y) { + return (x < y, x <= y, x == y, x >= y, x > y, x != y, x <=> y); +} + +int q() { + return 4; +} diff --git a/crypto/func/test/a9_1.fc b/crypto/func/test/a9_1.fc new file mode 100644 index 00000000..0dc9de0c --- /dev/null +++ b/crypto/func/test/a9_1.fc @@ -0,0 +1,28 @@ +_ F(int x) { + x = 2; + return x + 1; +} + +_ G(x) { + var y = x + 1; + x = 2; + return (x - 1, y); +} + +_ H(x) { + var y = x + 1; + x = 2; + return (x * y, y); +} + +_ I(x) { + int y = 17; + int z = x - y; + return (z, y); +} + +_ J(x) { + int y = 239; + int z = x - y; + return (z, y); +} diff --git a/crypto/func/test/b1.fc b/crypto/func/test/b1.fc new file mode 100644 index 00000000..ce522934 --- /dev/null +++ b/crypto/func/test/b1.fc @@ -0,0 +1,63 @@ +int now() asm "NOW"; + +int cell_hash(cell c) +asm "HASHCU"; + +int slice_hash(slice s) +asm "HASHSU"; + +int check_signature(int hash, slice signature, int public_key) +asm "CHKSIGNU"; + +;; () throw_if(int excno, int cond) impure +;; asm "THROWARGIF"; + +cell get_data() asm "c4 PUSH"; +() set_data(cell c) impure asm "c4 POP"; +() accept_message() impure asm "ACCEPT"; + +slice begin_parse(cell c) asm "CTOS"; +() end_parse(slice s) impure asm "ENDS"; +(cell, slice) load_ref(slice s) asm "LDREF"; +(int, slice) zload_int(slice s, int len) asm "LDIX"; +(int, slice) zload_uint(slice s, int len) asm "LDUX"; +int zpreload_int(slice s, int len) asm "PLDIX"; +int zpreload_uint(slice s, int len) asm "PLDUX"; +(slice, slice) load_bits(slice s, int len) asm "LDSLICEX"; +slice preload_bits(slice s, int len) asm "PLDSLICEX"; +cell set_idict_ref(cell value, int index, cell dict, int key_len) asm "DICTISETREF"; +builder begin_cell() asm "NEWC"; +builder store_ref(cell c, builder b) asm "STREF"; +builder zstore_uint(int x, builder b, int len) asm "STUX"; +builder zstore_int(int x, builder b, int len) asm "STIX"; +cell end_cell(builder b) asm "ENDC"; + +;; Simple configuration smart contract + +() recv_internal(cell in_msg) impure { + ;; do nothing for internal messages +} + +() recv_external(cell in_msg) impure { + var (signature, cs0) = load_bits(begin_parse(in_msg), 512); + var (msg_seqno, cs) = zload_uint(cs0, 32); + (var valid_until, cs) = zload_uint(cs, 32); + throw_if(35, valid_until < now()); + var (cfg_dict, cs2) = load_ref(begin_parse(get_data())); + (var stored_seqno, cs2) = zload_uint(cs2, 32); + (var public_key, cs2) = zload_uint(cs2, 256); + end_parse(cs2); + throw_unless(33, msg_seqno == stored_seqno); + throw_unless(34, check_signature(slice_hash(cs0), signature, public_key)); + accept_message(); + (var param_index, cs) = zload_uint(cs, 32); + (var param_value, cs) = load_ref(cs); + end_parse(cs); +;; cfg_dict = set_idict_ref(param_value, param_index, cfg_dict, 32); +;; var cb = begin_cell(); +;; cb = store_ref(cfg_dict, cb); + var cb = store_ref(set_idict_ref(param_value, param_index, cfg_dict, 32), begin_cell()); + cb = zstore_uint(stored_seqno + 1, cb, 32); + cb = zstore_uint(public_key, cb, 256); + set_data(end_cell(cb)); +} diff --git a/crypto/func/test/b2.fc b/crypto/func/test/b2.fc new file mode 100644 index 00000000..90322ae0 --- /dev/null +++ b/crypto/func/test/b2.fc @@ -0,0 +1,60 @@ +int now() asm "NOW"; + +int cell_hash(cell c) +asm "HASHCU"; + +int slice_hash(slice s) +asm "HASHSU"; + +int check_signature(int hash, slice signature, int public_key) +asm "CHKSIGNU"; + +;; () throw_if(int excno, int cond) impure +;; asm "THROWARGIF"; + +cell get_data() asm "c4 PUSH"; +() set_data(cell c) impure asm "c4 POP"; +() accept_message() impure asm "ACCEPT"; + +slice begin_parse(cell c) asm "CTOS"; +() end_parse(slice s) impure asm "ENDS"; +(slice, cell) load_ref(slice s) asm( -> 1 0) "LDREF"; +;; (slice, int) load_int(slice s, int len) asm(s len -> 1 0) "LDIX"; +;; (slice, int) load_uint(slice s, int len) asm( -> 1 0) "LDUX"; +;; int preload_int(slice s, int len) asm "PLDIX"; +;; int preload_uint(slice s, int len) asm "PLDUX"; +(slice, slice) load_bits(slice s, int len) asm(s len -> 1 0) "LDSLICEX"; +slice preload_bits(slice s, int len) asm "PLDSLICEX"; +cell set_idict_ref(cell dict, int key_len, int index, cell value) asm(value index dict key_len) "DICTISETREF"; +builder begin_cell() asm "NEWC"; +builder store_ref(builder b, cell c) asm(c b) "STREF"; +;; builder store_uint(builder b, int x, int len) asm(x b len) "STUX"; +;; builder store_int(builder b, int x, int len) asm(x b len) "STIX"; +cell end_cell(builder b) asm "ENDC"; + +;; Simple configuration smart contract + +() recv_internal(cell in_msg) impure { + ;; do nothing for internal messages +} + +() recv_external(cell in_msg) impure { + var (cs0, signature) = load_bits(begin_parse(in_msg), 512); + var (cs, msg_seqno) = load_uint(cs0, 32); + (cs, var valid_until) = load_uint(cs, 32); + throw_if(35, valid_until < now()); + var (cs2, cfg_dict) = load_ref(begin_parse(get_data())); + (cs2, var stored_seqno) = load_uint(cs2, 32); + (cs2, var public_key) = load_uint(cs2, 256); + end_parse(cs2); + throw_unless(33, msg_seqno == stored_seqno); + throw_unless(34, check_signature(slice_hash(cs0), signature, public_key)); + accept_message(); + (cs, var param_index) = load_uint(cs, 32); + (cs, var param_value) = load_ref(cs); + end_parse(cs); + var cb = store_ref(begin_cell(), set_idict_ref(cfg_dict, 32, param_index, param_value)); + cb = store_uint(cb, stored_seqno + 1, 32); + cb = store_uint(cb, public_key, 256); + set_data(end_cell(cb)); +} diff --git a/crypto/func/test/b2_0.fc b/crypto/func/test/b2_0.fc new file mode 100644 index 00000000..c2783f86 --- /dev/null +++ b/crypto/func/test/b2_0.fc @@ -0,0 +1,63 @@ +int now() asm "NOW"; + +int cell_hash(cell c) +asm "HASHCU"; + +int slice_hash(slice s) +asm "HASHSU"; + +int check_signature(int hash, slice signature, int public_key) +asm "CHKSIGNU"; + +;; () throw_if(int excno, int cond) impure +;; asm "THROWARGIF"; + +cell get_data() asm "c4 PUSH"; +() set_data(cell c) impure asm "c4 POP"; +() accept_message() impure asm "ACCEPT"; + +slice begin_parse(cell c) asm "CTOS"; +() end_parse(slice s) impure asm "ENDS"; +(slice, cell) load_ref(slice s) asm( -> 1 0) "LDREF"; +(slice, int) zload_int(slice s, int len) asm(s len -> 1 0) "LDIX"; +(slice, int) zload_uint(slice s, int len) asm( -> 1 0) "LDUX"; +int zpreload_int(slice s, int len) asm "PLDIX"; +int zpreload_uint(slice s, int len) asm "PLDUX"; +(slice, slice) load_bits(slice s, int len) asm(s len -> 1 0) "LDSLICEX"; +slice preload_bits(slice s, int len) asm "PLDSLICEX"; +cell set_idict_ref(cell dict, int key_len, int index, cell value) asm(value index dict key_len) "DICTISETREF"; +builder begin_cell() asm "NEWC"; +builder store_ref(builder b, cell c) asm(c b) "STREF"; +builder zstore_uint(builder b, int x, int len) asm(x b len) "STUX"; +builder zstore_int(builder b, int x, int len) asm(x b len) "STIX"; +cell end_cell(builder b) asm "ENDC"; + +;; Simple configuration smart contract + +() recv_internal(cell in_msg) impure { + ;; do nothing for internal messages +} + +() recv_external(cell in_msg) impure { + var (cs0, signature) = load_bits(begin_parse(in_msg), 512); + var (cs, msg_seqno) = zload_uint(cs0, 32); + (cs, var valid_until) = zload_uint(cs, 32); + throw_if(35, valid_until < now()); + var (cs2, cfg_dict) = load_ref(begin_parse(get_data())); + (cs2, var stored_seqno) = zload_uint(cs2, 32); + (cs2, var public_key) = zload_uint(cs2, 256); + end_parse(cs2); + throw_unless(33, msg_seqno == stored_seqno); + throw_unless(34, check_signature(slice_hash(cs0), signature, public_key)); + accept_message(); + (cs, var param_index) = zload_uint(cs, 32); + (cs, var param_value) = load_ref(cs); + end_parse(cs); +;; cfg_dict = set_idict_ref(cfg_dict, 32, param_index, param_value); +;; var cb = begin_cell(); +;; cb = store_ref(cb, cfg_dict); + var cb = store_ref(begin_cell(), set_idict_ref(cfg_dict, 32, param_index, param_value)); + cb = zstore_uint(cb, stored_seqno + 1, 32); + cb = zstore_uint(cb, public_key, 256); + set_data(end_cell(cb)); +} diff --git a/crypto/func/test/b2_1.fc b/crypto/func/test/b2_1.fc new file mode 100644 index 00000000..1ff79d05 --- /dev/null +++ b/crypto/func/test/b2_1.fc @@ -0,0 +1,62 @@ +int now() asm "NOW"; + +int cell_hash(cell c) +asm "HASHCU"; + +int slice_hash(slice s) +asm "HASHSU"; + +int check_signature(int hash, slice signature, int public_key) +asm "CHKSIGNU"; + +;; () throw_if(int excno, int cond) impure +;; asm "THROWARGIF"; + +cell get_data() asm "c4 PUSH"; +() set_data(cell c) impure asm "c4 POP"; +() accept_message() impure asm "ACCEPT"; + +slice begin_parse(cell c) asm "CTOS"; +() end_parse(slice s) impure asm "ENDS"; +(slice, cell) load_ref(slice s) asm( -> 1 0) "LDREF"; +;; (slice, int) ~load_int(slice s, int len) asm(s len -> 1 0) "LDIX"; +;; (slice, int) ~load_uint(slice s, int len) asm( -> 1 0) "LDUX"; +;; int preload_int(slice s, int len) asm "PLDIX"; +;; int preload_uint(slice s, int len) asm "PLDUX"; +(slice, slice) load_bits(slice s, int len) asm(s len -> 1 0) "LDSLICEX"; +slice preload_bits(slice s, int len) asm "PLDSLICEX"; +cell set_idict_ref(cell dict, int key_len, int index, cell value) asm(value index dict key_len) "DICTISETREF"; +(cell, ()) ~set_idict_ref(cell dict, int key_len, int index, cell value) asm(value index dict key_len) "DICTISETREF"; +builder begin_cell() asm "NEWC"; +builder store_ref(builder b, cell c) asm(c b) "STREF"; +;; builder store_uint(builder b, int x, int len) asm(x b len) "STUX"; +;; builder store_int(builder b, int x, int len) asm(x b len) "STIX"; +cell end_cell(builder b) asm "ENDC"; + +;; Simple configuration smart contract + +() recv_internal(cell in_msg) impure { + ;; do nothing for internal messages +} + +() recv_external(cell in_msg) impure { + var cs = begin_parse(in_msg); + var signature = cs~load_bits(512); + var cs0 = cs; + int msg_seqno = cs~load_uint(32); + var valid_until = cs~load_uint(32); + throw_if(35, valid_until < now()); + var cs2 = begin_parse(get_data()); + var cfg_dict = cs2~load_ref(); + var stored_seqno = cs2~load_uint(32); + var public_key = cs2~load_uint(256); + cs2.end_parse(); + throw_unless(33, msg_seqno == stored_seqno); + throw_unless(34, check_signature(slice_hash(cs0), signature, public_key)); + accept_message(); + var param_index = cs~load_uint(32); + var param_value = cs~load_ref(); + cs.end_parse(); + cfg_dict~set_idict_ref(32, param_index, param_value); + set_data(begin_cell().store_ref(cfg_dict).store_uint(stored_seqno + 1, 32).store_uint(public_key, 256).end_cell()); +} diff --git a/crypto/func/test/b2_2.fc b/crypto/func/test/b2_2.fc new file mode 100644 index 00000000..d137ff68 --- /dev/null +++ b/crypto/func/test/b2_2.fc @@ -0,0 +1,58 @@ +int now() asm "NOW"; + +int cell_hash(cell c) +asm "HASHCU"; + +int slice_hash(slice s) +asm "HASHSU"; + +int check_signature(int hash, slice signature, int public_key) +asm "CHKSIGNU"; + +;; () throw_if(int excno, int cond) impure +;; asm "THROWARGIF"; + +cell get_data() asm "c4 PUSH"; +() set_data(cell c) impure asm "c4 POP"; +() accept_message() impure asm "ACCEPT"; + +slice begin_parse(cell c) asm "CTOS"; +() end_parse(slice s) impure asm "ENDS"; +(slice, cell) load_ref(slice s) asm( -> 1 0) "LDREF"; +;; (slice, int) load_int(slice s, int len) asm(s len -> 1 0) "LDIX"; +;; (slice, int) load_uint(slice s, int len) asm( -> 1 0) "LDUX"; +;; int .preload_int(slice s, int len) asm "PLDIX"; +;; int .preload_uint(slice s, int len) asm "PLDUX"; +(slice, slice) load_bits(slice s, int len) asm(s len -> 1 0) "LDSLICEX"; +;; slice .preload_bits(slice s, int len) asm "PLDSLICEX"; +cell set_idict_ref(cell dict, int key_len, int index, cell value) asm(value index dict key_len) "DICTISETREF"; +builder begin_cell() asm "NEWC"; +builder store_ref(builder b, cell c) asm(c b) "STREF"; +;;builder .store_uint(builder b, int x, int len) asm(x b len) "STUX"; +;;builder .store_int(builder b, int x, int len) asm(x b len) "STIX"; +cell .end_cell(builder b) asm "ENDC"; + +;; Simple configuration smart contract + +() recv_internal(cell in_msg) impure { + ;; do nothing for internal messages +} + +() recv_external(cell in_msg) impure { + var (cs0, signature) = load_bits(begin_parse(in_msg), 512); + var (cs, msg_seqno) = load_uint(cs0, 32); + (cs, var valid_until) = load_uint(cs, 32); + throw_if(35, valid_until < now()); + var (cs2, cfg_dict) = load_ref(begin_parse(get_data())); + (cs2, var stored_seqno) = load_uint(cs2, 32); + (cs2, var public_key) = load_uint(cs2, 256); + end_parse(cs2); + throw_unless(33, msg_seqno == stored_seqno); + throw_unless(34, check_signature(slice_hash(cs0), signature, public_key)); + accept_message(); + (cs, var param_index) = load_uint(cs, 32); + (cs, var param_value) = load_ref(cs); + end_parse(cs); + set_data(begin_cell().store_ref(cfg_dict.set_idict_ref(32, param_index, param_value)) + .store_uint(stored_seqno + 1, 32).store_uint(public_key, 256).end_cell()); +} diff --git a/crypto/func/unify-types.cpp b/crypto/func/unify-types.cpp new file mode 100644 index 00000000..fd841a0f --- /dev/null +++ b/crypto/func/unify-types.cpp @@ -0,0 +1,384 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "func.h" + +namespace funC { + +/* + * + * TYPE EXPRESSIONS + * + */ + +int TypeExpr::holes = 0, TypeExpr::type_vars = 0; // not thread safe, but it is ok for now + +void TypeExpr::compute_width() { + switch (constr) { + case te_Atomic: + case te_Map: + minw = maxw = 1; + break; + case te_Tensor: + minw = maxw = 0; + for (TypeExpr* arg : args) { + minw += arg->minw; + maxw += arg->maxw; + } + if (minw > w_inf) { + minw = w_inf; + } + if (maxw > w_inf) { + maxw = w_inf; + } + break; + case te_Indirect: + minw = args[0]->minw; + maxw = args[0]->maxw; + break; + default: + minw = 0; + maxw = w_inf; + break; + } +} + +bool TypeExpr::recompute_width() { + switch (constr) { + case te_Tensor: + case te_Indirect: { + int min = 0, max = 0; + for (TypeExpr* arg : args) { + min += arg->minw; + max += arg->maxw; + } + if (min > maxw || max < minw) { + return false; + } + if (min > w_inf) { + min = w_inf; + } + if (max > w_inf) { + max = w_inf; + } + if (minw < min) { + minw = min; + } + if (maxw > max) { + maxw = max; + } + return true; + } + default: + return false; + } +} + +int TypeExpr::extract_components(std::vector& comp_list) { + if (constr != te_Indirect && constr != te_Tensor) { + comp_list.push_back(this); + return 1; + } + int res = 0; + for (TypeExpr* arg : args) { + res += arg->extract_components(comp_list); + } + return res; +} + +TypeExpr* TypeExpr::new_map(TypeExpr* from, TypeExpr* to) { + return new TypeExpr{te_Map, std::vector{from, to}}; +} + +void TypeExpr::replace_with(TypeExpr* te2) { + if (te2 == this) { + return; + } + constr = te_Indirect; + value = 0; + minw = te2->minw; + maxw = te2->maxw; + args.clear(); + args.push_back(te2); +} + +bool TypeExpr::remove_indirect(TypeExpr*& te, TypeExpr* forbidden) { + assert(te); + while (te->constr == te_Indirect) { + te = te->args[0]; + } + if (te->constr == te_Unknown) { + return te != forbidden; + } + bool res = true; + for (auto& x : te->args) { + res &= remove_indirect(x, forbidden); + } + return res; +} + +bool TypeExpr::remove_forall(TypeExpr*& te) { + assert(te); + if (te->constr != te_ForAll) { + return false; + } + assert(te->args.size() >= 1); + std::vector new_vars; + for (std::size_t i = 1; i < te->args.size(); i++) { + new_vars.push_back(new_hole(1)); + } + TypeExpr* te2 = te; + // std::cerr << "removing universal quantifier in " << te << std::endl; + te = te->args[0]; + remove_forall_in(te, te2, new_vars); + // std::cerr << "-> " << te << std::endl; + return true; +} + +bool TypeExpr::remove_forall_in(TypeExpr*& te, TypeExpr* te2, const std::vector& new_vars) { + assert(te); + assert(te2 && te2->constr == te_ForAll); + if (te->constr == te_Unknown) { + for (std::size_t i = 0; i < new_vars.size(); i++) { + if (te == te2->args[i + 1]) { + te = new_vars[i]; + return true; + } + } + return false; + } + if (te->constr == te_ForAll) { + return false; + } + if (te->args.empty()) { + return false; + } + auto te1 = new TypeExpr(*te); + bool res = false; + for (auto& arg : te1->args) { + res |= remove_forall_in(arg, te2, new_vars); + } + if (res) { + te = te1; + } else { + delete te1; + } + return res; +} + +void TypeExpr::show_width(std::ostream& os) { + os << minw; + if (maxw != minw) { + os << ".."; + if (maxw < w_inf) { + os << maxw; + } + } +} + +std::ostream& operator<<(std::ostream& os, TypeExpr* type_expr) { + if (!type_expr) { + return os << "(null-type-ptr)"; + } + return type_expr->print(os); +} + +std::ostream& TypeExpr::print(std::ostream& os, int lex_level) { + switch (constr) { + case te_Unknown: + if (value >= 0) { + return os << "??" << value; + } else if (value >= -26) { + return os << (char)(64 - value); + } else { + return os << "TVAR" << -value; + } + case te_Indirect: + return os << args[0]; + case te_Atomic: { + switch (value) { + case _Int: + return os << "int"; + case _Cell: + return os << "cell"; + case _Slice: + return os << "slice"; + case _Builder: + return os << "builder"; + case _Cont: + return os << "cont"; + case _Tuple: + return os << "tuple"; + case _Type: + return os << "type"; + default: + return os << "atomic-type-" << value; + } + } + case te_Tensor: { + os << "("; + auto c = args.size(); + if (c) { + for (const auto& x : args) { + x->print(os); + if (--c) { + os << ", "; + } + } + } + return os << ")"; + } + case te_Map: { + assert(args.size() == 2); + if (lex_level > 0) { + os << "("; + } + args[0]->print(os, 1); + os << " -> "; + args[1]->print(os); + if (lex_level > 0) { + os << ")"; + } + return os; + } + case te_ForAll: { + assert(args.size() >= 1); + if (lex_level > 0) { + os << '('; + } + os << "Forall "; + for (std::size_t i = 1; i < args.size(); i++) { + os << (i > 1 ? ' ' : '('); + args[i]->print(os); + } + os << ") "; + args[0]->print(os); + if (lex_level > 0) { + os << ')'; + } + return os; + } + default: + return os << "unknown-type-expr-" << constr; + } +} + +void UnifyError::print_message(std::ostream& os) const { + os << "cannot unify type " << te1 << " with " << te2; + if (!msg.empty()) { + os << ": " << msg; + } +} + +std::ostream& operator<<(std::ostream& os, const UnifyError& ue) { + ue.print_message(os); + return os; +} + +std::string UnifyError::message() const { + std::ostringstream os; + UnifyError::print_message(os); + return os.str(); +} + +void check_width_compat(TypeExpr* te1, TypeExpr* te2) { + if (te1->minw > te2->maxw || te2->minw > te1->maxw) { + std::ostringstream os{"cannot unify types of widths "}; + te1->show_width(os); + os << " and "; + te2->show_width(os); + throw UnifyError{te1, te2, os.str()}; + } +} + +void check_update_widths(TypeExpr* te1, TypeExpr* te2) { + check_width_compat(te1, te2); + te1->minw = te2->minw = std::max(te1->minw, te2->minw); + te1->maxw = te2->maxw = std::min(te1->maxw, te2->maxw); + assert(te1->minw <= te2->minw); +} + +void unify(TypeExpr*& te1, TypeExpr*& te2) { + assert(te1 && te2); + // std::cerr << "unify( " << te1 << " , " << te2 << " )\n"; + while (te1->constr == TypeExpr::te_Indirect) { + te1 = te1->args[0]; + } + while (te2->constr == TypeExpr::te_Indirect) { + te2 = te2->args[0]; + } + if (te1 == te2) { + return; + } + if (te1->constr == TypeExpr::te_ForAll) { + TypeExpr* te = te1; + if (!TypeExpr::remove_forall(te)) { + throw UnifyError{te1, te2, "cannot remove universal type quantifier while performing type unification"}; + } + unify(te, te2); + return; + } + if (te2->constr == TypeExpr::te_ForAll) { + TypeExpr* te = te2; + if (!TypeExpr::remove_forall(te)) { + throw UnifyError{te2, te1, "cannot remove universal type quantifier while performing type unification"}; + } + unify(te1, te); + return; + } + if (te1->constr == TypeExpr::te_Unknown) { + if (te2->constr == TypeExpr::te_Unknown) { + assert(te1->value != te2->value); + } + if (!TypeExpr::remove_indirect(te2, te1)) { + throw UnifyError{te1, te2, "type unification results in an infinite cyclic type"}; + } + check_update_widths(te1, te2); + te1->replace_with(te2); + te1 = te2; + return; + } + if (te2->constr == TypeExpr::te_Unknown) { + if (!TypeExpr::remove_indirect(te1, te2)) { + throw UnifyError{te2, te1, "type unification results in an infinite cyclic type"}; + } + check_update_widths(te2, te1); + te2->replace_with(te1); + te2 = te1; + return; + } + if (te1->constr != te2->constr || te1->value != te2->value || te1->args.size() != te2->args.size()) { + throw UnifyError{te1, te2}; + } + for (std::size_t i = 0; i < te1->args.size(); i++) { + unify(te1->args[i], te2->args[i]); + } + if (te1->constr == TypeExpr::te_Tensor) { + if (!te1->recompute_width()) { + throw UnifyError{te1, te2, "type unification incompatible with known width of first type"}; + } + if (!te2->recompute_width()) { + throw UnifyError{te2, te1, "type unification incompatible with known width of first type"}; + } + check_update_widths(te1, te2); + } + te1->replace_with(te2); + te1 = te2; +} + +} // namespace funC diff --git a/crypto/openssl/bignum.cpp b/crypto/openssl/bignum.cpp new file mode 100644 index 00000000..a696cb5a --- /dev/null +++ b/crypto/openssl/bignum.cpp @@ -0,0 +1,256 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "openssl/bignum.h" + +// impl only +#include + +namespace arith { + +BN_CTX* get_ctx(void) { + thread_local BN_CTX* ctx = BN_CTX_new(); + return ctx; +} + +BignumBitref& BignumBitref::operator=(bool val) { + if (val) { + BN_set_bit(ptr, n); + } else { + BN_clear_bit(ptr, n); + } + return *this; +} + +const Bignum operator+(const Bignum& x, const Bignum& y) { + Bignum z; + bn_assert(BN_add(z.bn_ptr(), x.bn_ptr(), y.bn_ptr())); + return z; +} + +const Bignum operator+(const Bignum& x, long y) { + if (y > 0) { + Bignum z(x); + bn_assert(BN_add_word(z.bn_ptr(), y)); + return z; + } else if (y < 0) { + Bignum z(x); + bn_assert(BN_sub_word(z.bn_ptr(), -y)); + return z; + } else { + return x; + } +} + +/* + const Bignum operator+ (Bignum&& x, long y) { + if (y > 0) { + bn_assert (BN_add_word (x.bn_ptr(), y)); + } else if (y < 0) { + bn_assert (BN_sub_word (x.bn_ptr(), -y)); + } + return std::move (x); + } + */ + +/* + const Bignum operator+ (long y, Bignum&& x) { + return x + y; + } + */ + +const Bignum operator-(const Bignum& x, const Bignum& y) { + Bignum z; + bn_assert(BN_sub(z.bn_ptr(), x.bn_ptr(), y.bn_ptr())); + return z; +} + +/* + const Bignum operator- (Bignum&& x, long y) { + return x + (-y); + } + */ + +const Bignum operator*(const Bignum& x, const Bignum& y) { + Bignum z; + bn_assert(BN_mul(z.bn_ptr(), x.bn_ptr(), y.bn_ptr(), get_ctx())); + return z; +} + +const Bignum operator*(const Bignum& x, long y) { + if (y > 0) { + Bignum z(x); + bn_assert(BN_mul_word(z.bn_ptr(), y)); + return z; + } else if (y < 0) { + Bignum z(x); + z.negate(); + bn_assert(BN_mul_word(z.bn_ptr(), -y)); + return z; + } else { + Bignum z(0); + return z; + } +} + +/* + const Bignum operator* (Bignum&& x, long y) { + if (y > 0) { + bn_assert (BN_mul_word (x.bn_ptr(), y)); + } else if (y < 0) { + x.negate(); + bn_assert (BN_mul_word (x.bn_ptr(), -y)); + } else { + x = 0; + } + return std::move (x); + } + */ + +const Bignum operator/(const Bignum& x, const Bignum& y) { + Bignum z, w; + bn_assert(BN_div(z.bn_ptr(), w.bn_ptr(), x.bn_ptr(), y.bn_ptr(), get_ctx())); + return z; +} + +const Bignum Bignum::divmod(const Bignum& y) { + Bignum w; + bn_assert(BN_div(val, w.bn_ptr(), val, y.bn_ptr(), get_ctx())); + return w; +} + +const Bignum operator%(const Bignum& x, const Bignum& y) { + Bignum z; + bn_assert(BN_mod(z.bn_ptr(), x.bn_ptr(), y.bn_ptr(), get_ctx())); + return z; +} + +unsigned long operator%(const Bignum& x, unsigned long y) { + BN_ULONG rem = BN_mod_word(x.bn_ptr(), y); + bn_assert(rem != (BN_ULONG)(-1)); + return static_cast(rem); +} + +const Bignum operator<<(const Bignum& x, int r) { + Bignum z; + bn_assert(BN_lshift(z.bn_ptr(), x.bn_ptr(), r)); + return z; +} + +const Bignum operator>>(const Bignum& x, int r) { + Bignum z; + bn_assert(BN_rshift(z.bn_ptr(), x.bn_ptr(), r)); + return z; +} + +const Bignum abs(const Bignum& x) { + Bignum T(x); + if (T.sign() < 0) { + T.negate(); + } + return T; +} + +const Bignum sqr(const Bignum& x) { + Bignum z; + bn_assert(BN_sqr(z.bn_ptr(), x.bn_ptr(), get_ctx())); + return z; +} + +void Bignum::export_msb(unsigned char* buffer, std::size_t size) const { + bn_assert(size <= (1 << 20)); + bn_assert(sign() >= 0); + std::size_t n = BN_num_bytes(val); + bn_assert(n <= size); + bn_assert(BN_bn2bin(val, buffer + size - n) == static_cast(n)); + std::memset(buffer, 0, size - n); +} + +Bignum& Bignum::import_msb(const unsigned char* buffer, std::size_t size) { + bn_assert(size <= (1 << 20)); + std::size_t i = 0; + while (i < size && !buffer[i]) { + i++; + } + bn_assert(BN_bin2bn(buffer + i, static_cast(size - i), val) == val); + return *this; +} + +void Bignum::export_lsb(unsigned char* buffer, std::size_t size) const { + bn_assert(size <= (1 << 20)); + bn_assert(sign() >= 0); + std::size_t n = BN_num_bytes(val); + bn_assert(n <= size); + bn_assert(BN_bn2bin(val, buffer) == (int)n); + std::memset(buffer + n, 0, size - n); + for (std::size_t i = 0; 2 * i + 1 < n; i++) { + std::swap(buffer[i], buffer[n - 1 - i]); + } +} + +Bignum& Bignum::import_lsb(const unsigned char* buffer, std::size_t size) { + bn_assert(size <= (1 << 20)); + while (size > 0 && !buffer[size - 1]) { + size--; + } + if (!size) { + bn_assert(BN_zero(val)); + return *this; + } + unsigned char tmp_buff[1024]; + unsigned char* tmp = (size < 1024 ? tmp_buff : new unsigned char[size]); + unsigned char* ptr = tmp + size; + for (std::size_t i = 0; i < size; i++) { + *--ptr = buffer[i]; + } + bn_assert(BN_bin2bn(tmp, static_cast(size), val) == val); + if (tmp != tmp_buff) { + delete[] tmp; + } + return *this; +} + +std::string Bignum::to_str() const { + char* ptr = BN_bn2dec(val); + std::string z(ptr); + OPENSSL_free(ptr); + return z; +} + +std::string Bignum::to_hex() const { + char* ptr = BN_bn2hex(val); + std::string z(ptr); + OPENSSL_free(ptr); + return z; +} + +std::ostream& operator<<(std::ostream& os, const Bignum& x) { + return os << x.to_str(); +} + +std::istream& operator>>(std::istream& is, Bignum& x) { + std::string word; + is >> word; + x = dec_string(word); + return is; +} + +bool is_prime(const Bignum& p, int nchecks, bool trial_div) { + return BN_is_prime_fasttest_ex(p.bn_ptr(), BN_prime_checks, get_ctx(), trial_div, 0); +} +} // namespace arith diff --git a/crypto/openssl/bignum.h b/crypto/openssl/bignum.h new file mode 100644 index 00000000..74b6a180 --- /dev/null +++ b/crypto/openssl/bignum.h @@ -0,0 +1,368 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +#include +#include + +#include +#include "td/utils/bits.h" + +namespace arith { +struct dec_string { + std::string str; + explicit dec_string(const std::string& s) : str(s) { + } +}; + +struct hex_string { + std::string str; + explicit hex_string(const std::string& s) : str(s) { + } +}; +} // namespace arith + +namespace arith { + +inline void bn_assert(int cond); +BN_CTX* get_ctx(); + +class BignumBitref { + BIGNUM* ptr; + int n; + + public: + BignumBitref(BIGNUM* x, int _n) : ptr(x), n(_n){}; + operator bool() const { + return BN_is_bit_set(ptr, n); + } + BignumBitref& operator=(bool val); +}; + +class Bignum { + BIGNUM* val; + + public: + class bignum_error {}; + Bignum() { + val = BN_new(); + } + Bignum(long x) { + val = BN_new(); + set_long(x); + } + ~Bignum() { + BN_free(val); + } + Bignum(const dec_string& ds) { + val = BN_new(); + set_dec_str(ds.str); + } + Bignum(const hex_string& hs) { + val = BN_new(); + set_hex_str(hs.str); + } + Bignum(const Bignum& x) { + val = BN_new(); + BN_copy(val, x.val); + } + //Bignum (Bignum&& x) { val = x.val; } + void clear() { + BN_clear(val); + } // use this for sensitive data + Bignum& operator=(const Bignum& x) { + BN_copy(val, x.val); + return *this; + } + Bignum& operator=(Bignum&& x) { + swap(x); + return *this; + } + Bignum& operator=(long x) { + return set_long(x); + } + Bignum& operator=(const dec_string& ds) { + return set_dec_str(ds.str); + } + Bignum& operator=(const hex_string& hs) { + return set_hex_str(hs.str); + } + Bignum& swap(Bignum& x) { + BN_swap(val, x.val); + return *this; + } + BIGNUM* bn_ptr() { + return val; + } + const BIGNUM* bn_ptr() const { + return val; + } + bool is_zero() const { + return BN_is_zero(val); + } + int sign() const { + return BN_is_zero(val) ? 0 : (BN_is_negative(val) ? -1 : 1); + } + bool odd() const { + return BN_is_odd(val); + } + int num_bits() const { + return BN_num_bits(val); + } + int num_bytes() const { + return BN_num_bytes(val); + } + bool operator[](int n) const { + return BN_is_bit_set(val, n); + } + BignumBitref operator[](int n) { + return BignumBitref(val, n); + } + void export_msb(unsigned char* buffer, std::size_t size) const; + Bignum& import_msb(const unsigned char* buffer, std::size_t size); + Bignum& import_msb(const std::string& s) { + return import_msb((const unsigned char*)s.c_str(), s.size()); + } + void export_lsb(unsigned char* buffer, std::size_t size) const; + Bignum& import_lsb(const unsigned char* buffer, std::size_t size); + Bignum& import_lsb(const std::string& s) { + return import_lsb((const unsigned char*)s.c_str(), s.size()); + } + + Bignum& set_dec_str(std::string s) { + bn_assert(BN_dec2bn(&val, s.c_str())); + return *this; + } + + Bignum& set_hex_str(std::string s) { + bn_assert(BN_hex2bn(&val, s.c_str())); + return *this; + } + + Bignum& set_ulong(unsigned long x) { + bn_assert(BN_set_word(val, x)); + return *this; + } + + Bignum& set_long(long x) { + set_ulong(std::abs(x)); + return x < 0 ? negate() : *this; + } + + Bignum& negate() { + BN_set_negative(val, !BN_is_negative(val)); + return *this; + } + + Bignum& operator+=(const Bignum& y) { + bn_assert(BN_add(val, val, y.val)); + return *this; + } + + Bignum& operator+=(long y) { + bn_assert((y >= 0 ? BN_add_word : BN_sub_word)(val, std::abs(y))); + return *this; + } + + Bignum& operator-=(long y) { + bn_assert((y >= 0 ? BN_sub_word : BN_add_word)(val, std::abs(y))); + return *this; + } + + Bignum& operator*=(const Bignum& y) { + bn_assert(BN_mul(val, val, y.val, get_ctx())); + return *this; + } + + Bignum& operator*=(long y) { + if (y < 0) { + negate(); + } + bn_assert(BN_mul_word(val, std::abs(y))); + return *this; + } + + Bignum& operator<<=(int r) { + bn_assert(BN_lshift(val, val, r)); + return *this; + } + + Bignum& operator>>=(int r) { + bn_assert(BN_rshift(val, val, r)); + return *this; + } + + Bignum& operator/=(const Bignum& y) { + Bignum w; + bn_assert(BN_div(val, w.val, val, y.val, get_ctx())); + return *this; + } + + Bignum& operator/=(long y) { + bn_assert(BN_div_word(val, std::abs(y)) != (BN_ULONG)(-1)); + return y < 0 ? negate() : *this; + } + + Bignum& operator%=(const Bignum& y) { + bn_assert(BN_mod(val, val, y.val, get_ctx())); + return *this; + } + + Bignum& operator%=(long y) { + BN_ULONG rem = BN_mod_word(val, std::abs(y)); + bn_assert(rem != (BN_ULONG)(-1)); + return set_long(static_cast(y < 0 ? td::bits_negate64(rem) : rem)); + } + + unsigned long divmod(unsigned long y) { + BN_ULONG rem = BN_div_word(val, y); + bn_assert(rem != (BN_ULONG)(-1)); + return static_cast(rem); + } + + const Bignum divmod(const Bignum& y); + + std::string to_str() const; + std::string to_hex() const; +}; + +inline void bn_assert(int cond) { + if (!cond) { + throw Bignum::bignum_error(); + } +} + +BN_CTX* get_ctx(void); + +const Bignum operator+(const Bignum& x, const Bignum& y); +const Bignum operator+(const Bignum& x, long y); + +/* + const Bignum operator+ (Bignum&& x, long y) { + if (y > 0) { + bn_assert (BN_add_word (x.bn_ptr(), y)); + } else if (y < 0) { + bn_assert (BN_sub_word (x.bn_ptr(), -y)); + } + return std::move (x); + } + */ + +inline const Bignum operator+(long y, const Bignum& x) { + return x + y; +} + +/* + const Bignum operator+ (long y, Bignum&& x) { + return x + y; + } + */ + +const Bignum operator-(const Bignum& x, const Bignum& y); +inline const Bignum operator-(const Bignum& x, long y) { + return x + (-y); +} + +/* + const Bignum operator- (Bignum&& x, long y) { + return x + (-y); + } + */ + +const Bignum operator*(const Bignum& x, const Bignum& y); +const Bignum operator*(const Bignum& x, long y); + +/* + const Bignum operator* (Bignum&& x, long y) { + if (y > 0) { + bn_assert (BN_mul_word (x.bn_ptr(), y)); + } else if (y < 0) { + x.negate(); + bn_assert (BN_mul_word (x.bn_ptr(), -y)); + } else { + x = 0; + } + return std::move (x); + } + */ + +inline const Bignum operator*(long y, const Bignum& x) { + return x * y; +} + +const Bignum operator/(const Bignum& x, const Bignum& y); +const Bignum operator%(const Bignum& x, const Bignum& y); +unsigned long operator%(const Bignum& x, unsigned long y); + +const Bignum operator<<(const Bignum& x, int r); +const Bignum operator>>(const Bignum& x, int r); + +const Bignum abs(const Bignum& x); +const Bignum sqr(const Bignum& x); + +std::ostream& operator<<(std::ostream& os, const Bignum& x); +std::istream& operator>>(std::istream& is, Bignum& x); + +bool is_prime(const Bignum& p, int nchecks = 64, bool trial_div = true); + +inline int cmp(const Bignum& x, const Bignum& y) { + return BN_cmp(x.bn_ptr(), y.bn_ptr()); +} + +inline bool operator==(const Bignum& x, const Bignum& y) { + return cmp(x, y) == 0; +} + +inline bool operator!=(const Bignum& x, const Bignum& y) { + return cmp(x, y) != 0; +} + +inline bool operator<(const Bignum& x, const Bignum& y) { + return cmp(x, y) < 0; +} + +inline bool operator<=(const Bignum& x, const Bignum& y) { + return cmp(x, y) <= 0; +} + +inline bool operator>(const Bignum& x, const Bignum& y) { + return cmp(x, y) > 0; +} + +inline bool operator>=(const Bignum& x, const Bignum& y) { + return cmp(x, y) >= 0; +} + +inline bool operator==(const Bignum& x, long y) { + if (y >= 0) { + return BN_is_word(x.bn_ptr(), y); + } else { + return x == Bignum(y); + } +} + +inline bool operator!=(const Bignum& x, long y) { + if (y >= 0) { + return !BN_is_word(x.bn_ptr(), y); + } else { + return x != Bignum(y); + } +} + +} // namespace arith diff --git a/crypto/openssl/digest.h b/crypto/openssl/digest.h new file mode 100644 index 00000000..d0dfc4c0 --- /dev/null +++ b/crypto/openssl/digest.h @@ -0,0 +1,151 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once +#include + +#include +#include + +#include "td/utils/Slice.h" + +namespace digest { +struct OpensslEVP_SHA1 { + enum { digest_bytes = 20 }; + static const EVP_MD *get_evp() { + return EVP_sha1(); + } +}; + +struct OpensslEVP_SHA256 { + enum { digest_bytes = 32 }; + static const EVP_MD *get_evp() { + return EVP_sha256(); + } +}; + +struct OpensslEVP_SHA512 { + enum { digest_bytes = 64 }; + static const EVP_MD *get_evp() { + return EVP_sha512(); + } +}; + +template +class HashCtx { + EVP_MD_CTX *ctx{nullptr}; + void init(); + void clear(); + + public: + enum { digest_bytes = H::digest_bytes }; + HashCtx() { + init(); + } + HashCtx(const void *data, std::size_t len) { + init(); + feed(data, len); + } + ~HashCtx() { + clear(); + } + void reset(); + void feed(const void *data, std::size_t len); + void feed(td::Slice slice) { + feed(slice.data(), slice.size()); + } + std::size_t extract(unsigned char buffer[digest_bytes]); + std::size_t extract(td::MutableSlice slice); + std::string extract(); +}; + +template +void HashCtx::init() { + ctx = EVP_MD_CTX_create(); + reset(); +} + +template +void HashCtx::reset() { + EVP_DigestInit_ex(ctx, H::get_evp(), 0); +} + +template +void HashCtx::clear() { + EVP_MD_CTX_destroy(ctx); + ctx = nullptr; +} + +template +void HashCtx::feed(const void *data, std::size_t len) { + EVP_DigestUpdate(ctx, data, len); +} + +template +std::size_t HashCtx::extract(unsigned char buffer[digest_bytes]) { + unsigned olen = 0; + EVP_DigestFinal_ex(ctx, buffer, &olen); + assert(olen == digest_bytes); + return olen; +} + +template +std::size_t HashCtx::extract(td::MutableSlice slice) { + return extract(slice.ubegin()); +} + +template +std::string HashCtx::extract() { + unsigned char buffer[digest_bytes]; + unsigned olen = 0; + EVP_DigestFinal_ex(ctx, buffer, &olen); + assert(olen == digest_bytes); + return std::string((char *)buffer, olen); +} + +typedef HashCtx SHA1; +typedef HashCtx SHA256; +typedef HashCtx SHA512; + +template +std::size_t hash_str(unsigned char buffer[T::digest_bytes], const void *data, std::size_t size) { + T hasher(data, size); + return hasher.extract(buffer); +} + +template +std::size_t hash_two_str(unsigned char buffer[T::digest_bytes], const void *data1, std::size_t size1, const void *data2, + std::size_t size2) { + T hasher(data1, size1); + hasher.feed(data2, size2); + return hasher.extract(buffer); +} + +template +std::string hash_str(const void *data, std::size_t size) { + T hasher(data, size); + return hasher.extract(); +} + +template +std::string hash_two_str(const void *data1, std::size_t size1, const void *data2, std::size_t size2) { + T hasher(data1, size1); + hasher.feed(data2, size2); + return hasher.extract(); +} +} // namespace digest diff --git a/crypto/openssl/rand.cpp b/crypto/openssl/rand.cpp new file mode 100644 index 00000000..9e9c546d --- /dev/null +++ b/crypto/openssl/rand.cpp @@ -0,0 +1,122 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "openssl/rand.hpp" + +#include "td/utils/common.h" + +#include +#include + +namespace prng { +int os_get_random_bytes(void *buf, int n); + +bool RandomGen::ok() const { + return RAND_status(); +} + +void RandomGen::seed_add(const void *data, std::size_t size, double entropy) { + RAND_add(data, static_cast(size), entropy > 0 ? entropy : static_cast(size)); +} + +void RandomGen::randomize(bool force) { + if (!force && ok()) { + return; + } + unsigned char buffer[128]; + int n = os_get_random_bytes(buffer, 128); + seed_add(buffer, n); + assert(ok()); +} + +bool RandomGen::rand_bytes(void *data, std::size_t size, bool strong) { +#if OPENSSL_VERSION_NUMBER < 0x10101000L + int res = (strong ? RAND_bytes : RAND_pseudo_bytes)((unsigned char *)data, static_cast(size)); +#else + int res = RAND_bytes((unsigned char *)data, static_cast(size)); +#endif + if (res != 0 && res != 1) { + throw rand_error(); + } + return res; +} + +std::string RandomGen::rand_string(std::size_t size, bool strong) { + std::string result(size, '\0'); + if (size > 0 && !rand_bytes(&result[0], size, strong)) { + throw rand_error(); + } + return result; +} + +RandomGen &rand_gen() { + // RandomGen is stateless, OpenSSL will handle concurrent access + static RandomGen MainPRNG; + return MainPRNG; +} +} // namespace prng + +//------------------------- move to separate OS-dependent file? +#if TD_WINDOWS +namespace prng { +int os_get_random_bytes(void *buf, int n) { + return 0; +} +} // namespace prng +#else +#include +#include + +namespace prng { + +int os_get_random_bytes(void *buf, int n) { + using namespace std; + int r = 0; + int h = open("/dev/random", O_RDONLY | O_NONBLOCK); + if (h >= 0) { + r = static_cast(read(h, buf, n)); + if (r > 0) { + //std::cerr << "added " << r << " bytes of real entropy to secure random numbers seed" << std::endl; + } else { + r = 0; + } + close(h); + } + + if (r < n) { + h = open("/dev/urandom", O_RDONLY); + if (h < 0) { + return r; + } + int s = static_cast(read(h, (char *)buf + r, n - r)); + close(h); + if (s < 0) { + return r; + } + r += s; + } + + if (r >= 8) { + *(long *)buf ^= lrand48(); + srand48(*(long *)buf); + } + + return r; +} +} // namespace prng +#endif diff --git a/crypto/openssl/rand.hpp b/crypto/openssl/rand.hpp new file mode 100644 index 00000000..91064e07 --- /dev/null +++ b/crypto/openssl/rand.hpp @@ -0,0 +1,56 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once +#include +#include + +namespace prng { + +// use this generator unless need a separate one +class RandomGen; +RandomGen &rand_gen(); + +class RandomGen { + public: + struct rand_error {}; + void randomize(bool force = true); + void seed_add(const void *data, std::size_t size, double entropy = 0); + bool ok() const; + RandomGen() { + randomize(false); + } + RandomGen(const void *seed, std::size_t size) { + seed_add(seed, size); + randomize(false); + } + bool rand_bytes(void *data, std::size_t size, bool strong = false); + bool strong_rand_bytes(void *data, std::size_t size) { + return rand_bytes(data, size, true); + } + template + bool rand_obj(T &obj) { + return rand_bytes(&obj, sizeof(T)); + } + template + bool rand_objs(T *ptr, std::size_t count) { + return rand_bytes(ptr, sizeof(T) * count); + } + std::string rand_string(std::size_t size, bool strong = false); +}; +} // namespace prng diff --git a/crypto/openssl/residue.cpp b/crypto/openssl/residue.cpp new file mode 100644 index 00000000..809ccd5a --- /dev/null +++ b/crypto/openssl/residue.cpp @@ -0,0 +1,176 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "residue.h" + +// --- impl +#include + +namespace arith { +class Residue; +class ResidueRing; + +void ResidueRing::init() { + Zero = new Residue(0, td::Ref(this)); + One = new Residue(1, td::Ref(this)); +} + +ResidueRing::~ResidueRing() { + delete Zero; + delete One; + delete Img_i; + Zero = One = Img_i = 0; +} + +const Residue operator+(const Residue& x, const Residue& y) { + x.same_ring(y); + Residue z(x.ring_ref()); + bn_assert(BN_mod_add(z.val.bn_ptr(), x.val.bn_ptr(), y.val.bn_ptr(), x.modulus().bn_ptr(), get_ctx())); + return z; +} + +const Residue operator-(const Residue& x, const Residue& y) { + x.same_ring(y); + Residue z(x.ring_ref()); + bn_assert(BN_mod_sub(z.val.bn_ptr(), x.val.bn_ptr(), y.val.bn_ptr(), x.modulus().bn_ptr(), get_ctx())); + return z; +} + +const Residue operator*(const Residue& x, const Residue& y) { + x.same_ring(y); + Residue z(x.ring_ref()); + bn_assert(BN_mod_mul(z.val.bn_ptr(), x.val.bn_ptr(), y.val.bn_ptr(), x.modulus().bn_ptr(), get_ctx())); + return z; +} + +const Residue operator-(const Residue& x) { + Residue z(x); + z.val.negate(); + return z.reduce(); +} + +Residue& Residue::operator+=(const Residue& y) { + same_ring(y); + bn_assert(BN_mod_add(val.bn_ptr(), val.bn_ptr(), y.val.bn_ptr(), modulus().bn_ptr(), get_ctx())); + return *this; +} + +Residue& Residue::operator-=(const Residue& y) { + same_ring(y); + bn_assert(BN_mod_sub(val.bn_ptr(), val.bn_ptr(), y.val.bn_ptr(), modulus().bn_ptr(), get_ctx())); + return *this; +} + +Residue& Residue::operator*=(const Residue& y) { + same_ring(y); + bn_assert(BN_mod_mul(val.bn_ptr(), val.bn_ptr(), y.val.bn_ptr(), modulus().bn_ptr(), get_ctx())); + return *this; +} + +bool operator==(const Residue& x, const Residue& y) { + x.same_ring(y); + return x.extract() == y.extract(); +} + +bool operator!=(const Residue& x, const Residue& y) { + x.same_ring(y); + return x.extract() != y.extract(); +} + +Residue sqr(const Residue& x) { + Residue z(x.ring_ref()); + bn_assert(BN_mod_sqr(z.val.bn_ptr(), x.val.bn_ptr(), x.modulus().bn_ptr(), get_ctx())); + return z; +} + +Residue power(const Residue& x, const Bignum& y) { + Residue z(x.ring_ref()); + bn_assert(BN_mod_exp(z.val.bn_ptr(), x.val.bn_ptr(), y.bn_ptr(), x.modulus().bn_ptr(), get_ctx())); + return z; +} + +Residue inverse(const Residue& x) { + assert(x.ring_ref()->is_prime()); + return power(x, x.ring_ref()->get_modulus() - 2); +} + +const Residue& ResidueRing::img_i() const { + if (!Img_i) { + assert(is_prime()); + assert(modulus % 4 == 1); + int g = 2; + Bignum n = (modulus - 1) / 4; + while (true) { + Residue t = power(frac(g), n); + if (t != one() && t != frac(-1)) { + Img_i = new Residue(t); + break; + } + g++; + } + } + return *Img_i; +} + +Residue sqrt(const Residue& x) { + assert(x.ring_of().is_prime()); + const ResidueRing& R = x.ring_of(); + const Bignum& p = R.get_modulus(); + if (x.is_zero() || !p.odd()) { + return x; + } + if (p[1]) { // p=3 (mod 4) + return power(x, (p + 1) >> 2); + } else if (p[2]) { + // p=5 (mod 8) + Residue t = power(x, (p + 3) >> 3); + return (sqr(t) == x) ? t : R.img_i() * t; + } else { + assert(p[2]); + return R.zero(); + } +} + +Residue ResidueRing::frac(long num, long denom) const { + assert(denom); + if (denom < 0) { + num = -num; + denom = -denom; + } + if (!(num % denom)) { + return Residue(num / denom, self_ref()); + } else { + return Residue(num, self_ref()) * inverse(Residue(denom, self_ref())); + } +} + +std::string Residue::to_str() const { + return "Mod(" + val.to_str() + "," + modulus().to_str() + ")"; +} + +std::ostream& operator<<(std::ostream& os, const Residue& x) { + return os << x.to_str(); +} + +std::istream& operator>>(std::istream& is, Residue& x) { + std::string word; + is >> word; + x = dec_string(word); + return is; +} +} // namespace arith diff --git a/crypto/openssl/residue.h b/crypto/openssl/residue.h new file mode 100644 index 00000000..00670838 --- /dev/null +++ b/crypto/openssl/residue.h @@ -0,0 +1,189 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once +#include "bignum.h" +#include "common/refcnt.hpp" + +namespace arith { +class Residue; +class ResidueRing; + +class ResidueRing : public td::CntObject { + public: + struct bad_modulus {}; + + private: + const Bignum modulus; + bool prime; + Residue* Zero; + Residue* One; + mutable Residue* Img_i; + void init(); + + public: + typedef Residue element; + explicit ResidueRing(Bignum mod) : modulus(mod), prime(arith::is_prime(mod)), Zero(0), One(0), Img_i(0) { + init(); + } + ~ResidueRing(); + const Bignum& get_modulus() const { + return modulus; + } + bool is_prime() const { + return prime; + } + const Residue& zero() const { + return *Zero; + } + const Residue& one() const { + return *One; + } + const Residue& img_i() const; + Residue frac(long num, long denom = 1) const; + Residue convert(long num) const; + Residue convert(const Bignum& x) const; + + Bignum reduce(const Bignum& x) const { + Bignum r = x % modulus; + if (r.sign() < 0) { + r += modulus; + } + return r; + } + + Bignum& do_reduce(Bignum& x) const { + x %= modulus; + if (x.sign() < 0) { + x += modulus; + } + return x; + } + + private: + td::Ref self_ref() const { + return td::Ref{this}; + } +}; + +class Residue { + public: + struct not_same_ring {}; + + private: + td::Ref ring; + mutable Bignum val; + Residue& reduce() { + ring->do_reduce(val); + return *this; + } + + public: + explicit Residue(td::Ref R) : ring(R) { + } + Residue(const Bignum& x, td::Ref R) : ring(R), val(R->reduce(x)) { + } + ~Residue() { + } + Residue(const Residue& x) : ring(x.ring), val(x.val) { + } + Bignum extract() const { + return val; + } + const Bignum& extract_raw() const { + return val; + } + const Bignum& modulus() const { + return ring->get_modulus(); + } + void same_ring(const Residue& y) const { + if (ring != y.ring) { + throw not_same_ring(); + } + } + const ResidueRing& ring_of() const { + return *ring; + } + td::Ref ring_ref() const { + return ring; + } + bool is_zero() const { + return (val == 0); + } + Residue& operator=(const Residue& x) { + same_ring(x); + val = x.val; + return *this; + } + Residue& operator=(const Bignum& x) { + val = ring->reduce(x); + return *this; + } + Residue& operator+=(const Residue& y); + Residue& operator-=(const Residue& y); + Residue& operator*=(const Residue& y); + Residue& operator+=(long y) { + val += y; + return reduce(); + } + Residue& operator-=(long y) { + val -= y; + return reduce(); + } + Residue& operator*=(long y) { + val *= y; + return reduce(); + } + Residue& negate() { + val.negate(); + return reduce(); + } + friend const Residue operator+(const Residue& x, const Residue& y); + friend const Residue operator-(const Residue& x, const Residue& y); + friend const Residue operator*(const Residue& x, const Residue& y); + friend const Residue operator-(const Residue& x); + friend Residue sqr(const Residue& x); + friend Residue power(const Residue& x, const Bignum& y); + friend Residue inverse(const Residue& x); + std::string to_str() const; +}; + +const Residue operator+(const Residue& x, const Residue& y); +const Residue operator-(const Residue& x, const Residue& y); +const Residue operator*(const Residue& x, const Residue& y); +const Residue operator-(const Residue& x); + +bool operator==(const Residue& x, const Residue& y); +bool operator!=(const Residue& x, const Residue& y); + +Residue sqr(const Residue& x); +Residue power(const Residue& x, const Bignum& y); +Residue inverse(const Residue& x); +Residue sqrt(const Residue& x); + +inline Residue ResidueRing::convert(long x) const { + return Residue(x, td::Ref(this)); +} + +inline Residue ResidueRing::convert(const Bignum& x) const { + return Residue(x, td::Ref(this)); +} + +std::ostream& operator<<(std::ostream& os, const Residue& x); +std::istream& operator>>(std::istream& is, Residue& x); +} // namespace arith diff --git a/crypto/parser/lexer.cpp b/crypto/parser/lexer.cpp new file mode 100644 index 00000000..d8029340 --- /dev/null +++ b/crypto/parser/lexer.cpp @@ -0,0 +1,288 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "lexer.h" +#include "symtable.h" +#include +#include + +namespace src { + +/* + * + * LEXER + * + */ + +std::string Lexem::lexem_name_str(int idx) { + if (idx == Eof) { + return "end of file"; + } else if (idx == Ident) { + return "identifier"; + } else if (idx == Number) { + return "number"; + } else if (idx == String) { + return "string"; + } else if (idx == Special) { + return "special"; + } else if (sym::symbols.get_keyword(idx)) { + return "`" + sym::symbols.get_keyword(idx)->str + "`"; + } else { + std::ostringstream os{""; + return os.str(); + } +} + +std::string Lexem::name_str() const { + if (tp == Ident) { + return std::string{"identifier `"} + sym::symbols.get_name(val) + "`"; + } else if (tp == String) { + return std::string{"string \""} + str + '"'; + } else { + return lexem_name_str(tp); + } +} + +bool is_number(std::string str) { + auto st = str.begin(), en = str.end(); + if (st == en) { + return false; + } + if (*st == '-') { + st++; + } + bool hex = false; + if (st + 1 < en && *st == '0' && st[1] == 'x') { + st += 2; + hex = true; + } + if (st == en) { + return false; + } + while (st < en) { + int c = *st; + if (c >= '0' && c <= '9') { + ++st; + continue; + } + if (!hex) { + return false; + } + c |= 0x20; + if (c < 'a' || c > 'f') { + return false; + } + ++st; + } + return true; +} + +int Lexem::classify() { + if (tp != Unknown) { + return tp; + } + sym::sym_idx_t i = sym::symbols.lookup(str); + if (i) { + assert(str == sym::symbols[i]->str); + str = sym::symbols[i]->str; + sym::sym_idx_t idx = sym::symbols[i]->idx; + tp = (idx < 0 ? -idx : Ident); + val = i; + } else if (is_number(str)) { + tp = Number; + } else { + tp = lexem_is_special(str); + } + if (tp == Unknown) { + tp = Ident; + val = sym::symbols.lookup(str, 1); + } + return tp; +} + +int Lexem::set(std::string _str, const SrcLocation& _loc, int _tp, int _val) { + str = _str; + loc = _loc; + tp = _tp; + val = _val; + return classify(); +} + +Lexer::Lexer(SourceReader& _src, bool init, std::string active_chars, std::string eol_cmts, std::string open_cmts, + std::string close_cmts, std::string quote_chars) + : src(_src), eof(false), lexem("", src.here(), Lexem::Undefined), peek_lexem("", {}, Lexem::Undefined) { + std::memset(char_class, 0, sizeof(char_class)); + unsigned char activity = cc::active; + for (char c : active_chars) { + if (c == ' ') { + if (!--activity) { + activity = cc::allow_repeat; + } + } else if ((unsigned)c < 0x80) { + char_class[(unsigned)c] |= activity; + } + } + set_spec(eol_cmt, eol_cmts); + set_spec(cmt_op, open_cmts); + set_spec(cmt_cl, close_cmts); + for (int c : quote_chars) { + if (c > ' ' && c <= 0x7f) { + char_class[(unsigned)c] |= cc::quote_char; + } + } + if (init) { + next(); + } +} + +void Lexer::set_spec(std::array& arr, std::string setup) { + arr[0] = arr[1] = arr[2] = -0x100; + std::size_t n = setup.size(), i; + for (i = 0; i < n; i++) { + if (setup[i] == ' ') { + continue; + } + if (i == n - 1 || setup[i + 1] == ' ') { + arr[0] = setup[i]; + } else if (i == n - 2 || (i < n - 2 && setup[i + 2] == ' ')) { + arr[1] = setup[i]; + arr[2] = setup[++i]; + } else { + while (i < n && setup[i] != ' ') { + i++; + } + } + } +} + +void Lexer::expect(int exp_tp, const char* msg) { + if (tp() != exp_tp) { + throw ParseError{lexem.loc, (msg ? std::string{msg} : Lexem::lexem_name_str(exp_tp)) + " expected instead of " + + cur().name_str()}; + } + next(); +} + +const Lexem& Lexer::next() { + if (peek_lexem.valid()) { + lexem = std::move(peek_lexem); + peek_lexem.clear({}, Lexem::Undefined); + eof = (lexem.tp == Lexem::Eof); + return lexem; + } + if (eof) { + return lexem.clear(src.here(), Lexem::Eof); + } + long long comm = 1; + while (!src.seek_eof()) { + int cc = src.cur_char(), nc = src.next_char(); + if (cc == eol_cmt[0] || (cc == eol_cmt[1] && nc == eol_cmt[2])) { + src.load_line(); + } else if (cc == cmt_op[1] && nc == cmt_op[2]) { + src.advance(2); + comm = comm * 2 + 1; + } else if (cc == cmt_op[0]) { + src.advance(1); + comm *= 2; + } else if (comm == 1) { + break; + } else if (cc == cmt_cl[1] && nc == cmt_cl[2]) { + if (!(comm & 1)) { + src.error(std::string{"a `"} + (char)cmt_op[0] + "` comment closed by `" + (char)cmt_cl[1] + (char)cmt_cl[2] + + "`"); + } + comm >>= 1; + src.advance(2); + } else if (cc == cmt_cl[0]) { + if (!(comm & 1)) { + src.error(std::string{"a `"} + (char)cmt_op[1] + (char)cmt_op[2] + "` comment closed by `" + (char)cmt_cl[0] + + "`"); + } + comm >>= 1; + src.advance(1); + } else { + src.advance(1); + } + if (comm < 0) { + src.error("too many nested comments"); + } + } + if (src.seek_eof()) { + eof = true; + if (comm > 1) { + if (comm & 1) { + src.error(std::string{"`"} + (char)cmt_op[1] + (char)cmt_op[2] + "` comment extends past end of file"); + } else { + src.error(std::string{"`"} + (char)cmt_op[0] + "` comment extends past end of file"); + } + } + return lexem.clear(src.here(), Lexem::Eof); + } + int c = src.cur_char(); + const char* end = src.get_ptr(); + if (is_quote_char(c) || c == '`') { + int qc = c; + ++end; + while (end < src.get_end_ptr() && *end != qc) { + ++end; + } + if (*end != qc) { + src.error(qc == '`' ? "a `back-quoted` token extends past end of line" : "string extends past end of line"); + } + lexem.set(std::string{src.get_ptr() + 1, end}, src.here(), qc == '`' ? Lexem::Unknown : Lexem::String); + src.set_ptr(end + 1); + // std::cerr << lexem.name_str() << ' ' << lexem.str << std::endl; + return lexem; + } + int len = 0, pc = -0x100; + while (end < src.get_end_ptr()) { + c = *end; + bool repeated = (c == pc && is_repeatable(c)); + if (c == ' ' || c == 9 || (len && is_left_active(c) && !repeated)) { + break; + } + ++len; + ++end; + if (is_right_active(c) && !repeated) { + break; + } + pc = c; + } + lexem.set(std::string{src.get_ptr(), end}, src.here()); + src.set_ptr(end); + // std::cerr << lexem.name_str() << ' ' << lexem.str << std::endl; + return lexem; +} + +const Lexem& Lexer::peek() { + if (peek_lexem.valid()) { + return peek_lexem; + } + if (eof) { + return lexem.clear(src.here(), Lexem::Eof); + } + Lexem keep = std::move(lexem); + next(); + peek_lexem = std::move(lexem); + lexem = std::move(keep); + eof = false; + return peek_lexem; +} + +} // namespace src diff --git a/crypto/parser/lexer.h b/crypto/parser/lexer.h new file mode 100644 index 00000000..803560e1 --- /dev/null +++ b/crypto/parser/lexer.h @@ -0,0 +1,114 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once +#include "srcread.h" +#include +#include +#include + +namespace src { + +/* + * + * LEXER + * + */ + +int lexem_is_special(std::string str); // return 0 if no special lexems are needed + +struct Lexem { + enum { Undefined = -2, Eof = -1, Unknown = 0, Ident = 0, Number = 1, Special = 2, String = 3 }; + int tp; + int val; + std::string str; + SrcLocation loc; + int classify(); + Lexem(std::string _str = "", const SrcLocation& _loc = {}, int _tp = Unknown, int _val = 0) + : tp(_tp), val(_val), str(_str), loc(_loc) { + classify(); + } + int set(std::string _str = "", const SrcLocation& _loc = {}, int _tp = Unknown, int _val = 0); + Lexem& clear(const SrcLocation& _loc = {}, int _tp = Unknown, int _val = 0) { + tp = _tp; + val = _val; + loc = _loc; + str = ""; + return *this; + } + bool valid() const { + return tp != Undefined; + } + std::string name_str() const; + void error(std::string _str) const { + throw ParseError{loc, _str}; + } + void error_at(std::string str1, std::string str2) const { + error(str1 + str + str2); + } + + static std::string lexem_name_str(int idx); +}; + +class Lexer { + SourceReader& src; + bool eof; + Lexem lexem, peek_lexem; + unsigned char char_class[128]; + std::array eol_cmt, cmt_op, cmt_cl; + enum cc { left_active = 2, right_active = 1, active = 3, allow_repeat = 4, quote_char = 8 }; + + public: + bool eof_found() const { + return eof; + } + Lexer(SourceReader& _src, bool init = false, std::string active_chars = ";,() ~.", std::string eol_cmts = ";;", + std::string open_cmts = "{-", std::string close_cmts = "-}", std::string quote_chars = "\""); + const Lexem& next(); + const Lexem& cur() const { + return lexem; + } + const Lexem& peek(); + int tp() const { + return lexem.tp; + } + void expect(int exp_tp, const char* msg = 0); + int classify_char(unsigned c) const { + return c < 0x80 ? char_class[c] : 0; + } + bool is_active(int c) const { + return (classify_char(c) & cc::active) == cc::active; + } + bool is_left_active(int c) const { + return (classify_char(c) & cc::left_active); + } + bool is_right_active(int c) const { + return (classify_char(c) & cc::right_active); + } + bool is_repeatable(int c) const { + return (classify_char(c) & cc::allow_repeat); + } + bool is_quote_char(int c) const { + return (classify_char(c) & cc::quote_char); + } + + private: + void set_spec(std::array& arr, std::string setup); +}; + +} // namespace src diff --git a/crypto/parser/srcread.cpp b/crypto/parser/srcread.cpp new file mode 100644 index 00000000..ac031e1e --- /dev/null +++ b/crypto/parser/srcread.cpp @@ -0,0 +1,180 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "srcread.h" + +namespace src { + +/* + * + * SOURCE FILE READER + * + */ + +std::ostream& operator<<(std::ostream& os, const FileDescr* fdescr) { + return os << (fdescr ? (fdescr->is_stdin ? "stdin" : fdescr->filename) : "unknown-location"); +} + +std::ostream& operator<<(std::ostream& os, const Fatal& fatal) { + return os << fatal.get_msg(); +} + +void SrcLocation::show(std::ostream& os) const { + os << fdescr; + if (line_no > 0) { + os << ':' << line_no; + if (line_pos >= 0) { + os << ':' << (line_pos + 1); + } + } +} + +bool SrcLocation::show_context(std::ostream& os) const { + if (text.empty() || line_pos < 0 || (unsigned)line_pos > text.size()) { + return false; + } + bool skip_left = (line_pos > 200), skip_right = (line_pos + 200u < text.size()); + const char* start = skip_left ? text.c_str() + line_pos - 100 : text.c_str(); + const char* end = skip_right ? text.c_str() + line_pos + 100 : text.c_str() + text.size(); + const char* here = text.c_str() + line_pos; + os << " "; + if (skip_left) { + os << "... "; + } + for (const char* ptr = start; ptr < end; ptr++) { + os << (char)*ptr; + } + if (skip_right) { + os << " ..."; + } + os << std::endl; + os << " "; + if (skip_left) { + os << "... "; + } + for (const char* ptr = start; ptr < here; ptr++) { + char c = *ptr; + os << (c == 9 || c == 10 ? c : ' '); + } + os << '^' << std::endl; + return true; +} + +std::ostream& operator<<(std::ostream& os, const SrcLocation& loc) { + loc.show(os); + return os; +} + +void SrcLocation::show_gen_error(std::ostream& os, std::string message, std::string err_type) const { + show(os); + if (!err_type.empty()) { + os << ": " << err_type; + } + os << ": " << message << std::endl; + show_context(os); +} + +std::ostream& operator<<(std::ostream& os, const Error& error) { + error.show(os); + return os; +} + +void ParseError::show(std::ostream& os) const { + os << where << ": error: " << message << std::endl; + where.show_context(os); +} + +SourceReader::SourceReader(std::istream* _is, const FileDescr* _fdescr) + : ifs(_is), loc(_fdescr), eof(false), cur_line_len(0), start(0), cur(0), end(0) { + load_line(); +} + +void SourceReader::set_eof() { + if (!eof) { + eof = true; + start = cur = end = 0; + } +} + +int SourceReader::skip_spc() { + if (!cur) { + return 0; + } + const char* ptr = cur; + int res = 0; + while (*ptr == ' ' || *ptr == 9) { + ++ptr; + ++res; + } + set_ptr(ptr); + return res; +} + +bool SourceReader::seek_eof() { + while (seek_eoln()) { + if (!load_line()) { + return true; + } + } + return false; +} + +const char* SourceReader::set_ptr(const char* ptr) { + if (ptr != cur) { + if (ptr < cur || ptr > end) { + error("parsing position went outside of line"); + } + loc.line_pos = (int)(ptr - start); + cur = ptr; + } + return ptr; +} + +bool SourceReader::load_line() { + if (eof) { + return false; + } + if (ifs->eof()) { + set_eof(); + return false; + } + ++loc.line_no; + loc.line_pos = -1; + std::getline(*ifs, cur_line); + if (ifs->fail()) { + set_eof(); + if (!ifs->eof()) { + error("cannot read line from source stream"); + } + return false; + } + std::size_t len = cur_line.size(); + if (len > 0xffffff) { + set_eof(); + error("line too long"); + return false; + } + loc.text = cur_line; + cur_line_len = (int)len; + loc.line_pos = 0; + cur = start = cur_line.c_str(); + end = start + cur_line_len; + return true; +} + +} // namespace src diff --git a/crypto/parser/srcread.h b/crypto/parser/srcread.h new file mode 100644 index 00000000..05b79212 --- /dev/null +++ b/crypto/parser/srcread.h @@ -0,0 +1,150 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +#include +#include + +namespace src { + +/* + * + * SOURCE FILE READER + * + */ + +struct FileDescr { + std::string filename; + bool is_stdin; + FileDescr(std::string _fname, bool _stdin = false) : filename(std::move(_fname)), is_stdin(_stdin) { + } +}; + +struct Fatal { + std::string message; + Fatal(std::string _msg) : message(std::move(_msg)) { + } + std::string get_msg() const { + return message; + } +}; + +std::ostream& operator<<(std::ostream& os, const Fatal& fatal); + +struct SrcLocation { + const FileDescr* fdescr; + int line_no; + int line_pos; + std::string text; + SrcLocation() : fdescr(nullptr), line_no(0), line_pos(-1) { + } + SrcLocation(const FileDescr* _fdescr, int line = 0, int pos = -1) : fdescr(_fdescr), line_no(line), line_pos(pos) { + } + bool defined() const { + return fdescr; + } + void show(std::ostream& os) const; + bool show_context(std::ostream& os) const; + void show_gen_error(std::ostream& os, std::string message, std::string err_type = "") const; + void show_note(std::string err_msg) const { + show_gen_error(std::cerr, err_msg, "note"); + } + void show_warning(std::string err_msg) const { + show_gen_error(std::cerr, err_msg, "warning"); + } + void show_error(std::string err_msg) const { + show_gen_error(std::cerr, err_msg, "error"); + } +}; + +std::ostream& operator<<(std::ostream& os, const SrcLocation& loc); + +struct Error { + virtual ~Error() = default; + virtual void show(std::ostream& os) const = 0; +}; + +std::ostream& operator<<(std::ostream& os, const Error& error); + +struct ParseError : Error { + SrcLocation where; + std::string message; + ParseError(const SrcLocation& _where, std::string _msg) : where(_where), message(_msg) { + } + ParseError(const SrcLocation* _where, std::string _msg) : message(_msg) { + if (_where) { + where = *_where; + } + } + ~ParseError() override = default; + void show(std::ostream& os) const override; +}; + +class SourceReader { + std::istream* ifs; + SrcLocation loc; + bool eof; + std::string cur_line; + int cur_line_len; + void set_eof(); + const char *start, *cur, *end; + + public: + SourceReader(std::istream* _is, const FileDescr* _fdescr); + bool load_line(); + bool is_eof() const { + return eof; + } + int is_eoln() const { + return cur == end; + } + int skip_spc(); + bool seek_eoln() { + skip_spc(); + return is_eoln(); + } + bool seek_eof(); + const char* cur_line_cstr() const { + return cur_line.c_str(); + } + const SrcLocation& here() const { + return loc; + } + char cur_char() const { + return *cur; + } + char next_char() const { + return cur[1]; + } + const char* get_ptr() const { + return cur; + } + const char* get_end_ptr() const { + return end; + } + const char* set_ptr(const char* ptr); + void advance(int n) { + set_ptr(get_ptr() + n); + } + void error(std::string err_msg) { + throw ParseError{loc, err_msg}; + } +}; + +} // namespace src diff --git a/crypto/parser/symtable.cpp b/crypto/parser/symtable.cpp new file mode 100644 index 00000000..157b4a2b --- /dev/null +++ b/crypto/parser/symtable.cpp @@ -0,0 +1,181 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "symtable.h" +#include +#include + +namespace sym { + +/* + * + * SYMBOL VALUES (DECLARED) + * + */ + +int scope_level; + +SymTable<100003> symbols; + +SymDef* sym_def[symbols.hprime]; +SymDef* global_sym_def[symbols.hprime]; +std::vector> symbol_stack; +std::vector scope_opened_at; + +std::string Symbol::unknown_symbol_name(sym_idx_t i) { + if (!i) { + return "_"; + } else { + std::ostringstream os; + os << "SYM#" << i; + return os.str(); + } +} + +sym_idx_t SymTableBase::gen_lookup(std::string str, int mode, sym_idx_t idx) { + unsigned long long h1 = 1, h2 = 1; + for (char c : str) { + h1 = ((h1 * 239) + (unsigned char)(c)) % p; + h2 = ((h2 * 17) + (unsigned char)(c)) % (p - 1); + } + ++h2; + ++h1; + while (true) { + if (sym_table[h1]) { + if (sym_table[h1]->str == str) { + return (mode & 2) ? not_found : sym_idx_t(h1); + } + h1 += h2; + if (h1 > p) { + h1 -= p; + } + } else { + if (!(mode & 1)) { + return not_found; + } + if (def_sym >= ((long)p * 3) / 4) { + throw SymTableOverflow{def_sym}; + } + sym_table[h1] = std::make_unique(str, idx <= 0 ? sym_idx_t(h1) : -idx); + ++def_sym; + return sym_idx_t(h1); + } + } +} + +SymTableBase& SymTableBase::add_keyword(std::string str, sym_idx_t idx) { + if (idx <= 0) { + idx = ++def_kw; + } + sym_idx_t res = gen_lookup(str, -1, idx); + if (!res) { + throw SymTableKwRedef{str}; + } + if (idx < max_kw_idx) { + keywords[idx] = res; + } + return *this; +} + +void open_scope(src::Lexer& lex) { + ++scope_level; + scope_opened_at.push_back(lex.cur().loc); +} + +void close_scope(src::Lexer& lex) { + if (!scope_level) { + throw src::Fatal{"cannot close the outer scope"}; + } + while (!symbol_stack.empty() && symbol_stack.back().first == scope_level) { + SymDef old_def = symbol_stack.back().second; + auto idx = old_def.sym_idx; + symbol_stack.pop_back(); + SymDef* cur_def = sym_def[idx]; + assert(cur_def); + assert(cur_def->level == scope_level && cur_def->sym_idx == idx); + //std::cerr << "restoring local symbol `" << old_def.name << "` of level " << scope_level << " to its previous level " << old_def.level << std::endl; + if (cur_def->value) { + //std::cerr << "deleting value of symbol " << old_def.name << ":" << old_def.level << " at " << (const void*) it->second.value << std::endl; + delete cur_def->value; + } + if (!old_def.level && !old_def.value) { + delete cur_def; // ??? keep the definition always? + sym_def[idx] = nullptr; + } else { + cur_def->value = std::move(old_def.value); + cur_def->level = old_def.level; + } + old_def.value = nullptr; + } + --scope_level; + scope_opened_at.pop_back(); +} + +SymDef* lookup_symbol(sym_idx_t idx, int flags) { + if (!idx) { + return nullptr; + } + if ((flags & 1) && sym_def[idx]) { + return sym_def[idx]; + } + if ((flags & 2) && global_sym_def[idx]) { + return global_sym_def[idx]; + } + return nullptr; +} + +SymDef* lookup_symbol(std::string name, int flags) { + return lookup_symbol(symbols.lookup(name), flags); +} + +SymDef* define_global_symbol(sym_idx_t name_idx, bool force_new, const src::SrcLocation& loc) { + if (!name_idx) { + return nullptr; + } + auto found = global_sym_def[name_idx]; + if (found) { + return force_new && found->value ? nullptr : found; + } + return global_sym_def[name_idx] = new SymDef(0, name_idx, loc); +} + +SymDef* define_symbol(sym_idx_t name_idx, bool force_new, const src::SrcLocation& loc) { + if (!name_idx) { + return nullptr; + } + if (!scope_level) { + return define_global_symbol(name_idx, force_new, loc); + } + auto found = sym_def[name_idx]; + if (found) { + if (found->level < scope_level) { + symbol_stack.push_back(std::make_pair(scope_level, *found)); + found->level = scope_level; + } else if (found->value && force_new) { + return nullptr; + } + found->value = 0; + found->loc = loc; + return found; + } + found = sym_def[name_idx] = new SymDef(scope_level, name_idx, loc); + symbol_stack.push_back(std::make_pair(scope_level, SymDef{0, name_idx})); + return found; +} + +} // namespace sym diff --git a/crypto/parser/symtable.h b/crypto/parser/symtable.h new file mode 100644 index 00000000..6c77ca7b --- /dev/null +++ b/crypto/parser/symtable.h @@ -0,0 +1,177 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once +#include "srcread.h" +#include "lexer.h" +#include + +namespace sym { + +/* + * + * SYMBOL VALUES (DECLARED) + * + */ + +typedef int var_idx_t; + +struct SymValBase { + enum { _Param, _Var, _Func, _Typename }; + int type; + int idx; + SymValBase(int _type, int _idx) : type(_type), idx(_idx) { + } + virtual ~SymValBase() = default; +}; + +/* + * + * SYMBOL TABLE + * + */ + +// defined outside this module (by the end user) +int compute_symbol_subclass(std::string str); // return 0 if unneeded + +typedef int sym_idx_t; + +struct Symbol { + std::string str; + sym_idx_t idx; + int subclass; + Symbol(std::string _str, sym_idx_t _idx, int _sc) : str(_str), idx(_idx), subclass(_sc) { + } + Symbol(std::string _str, sym_idx_t _idx) : str(_str), idx(_idx) { + subclass = compute_symbol_subclass(std::move(_str)); + } + static std::string unknown_symbol_name(sym_idx_t i); +}; + +class SymTableBase { + unsigned p; + std::unique_ptr* sym_table; + sym_idx_t def_kw, def_sym; + static constexpr int max_kw_idx = 10000; + sym_idx_t keywords[max_kw_idx]; + + public: + SymTableBase(unsigned p_, std::unique_ptr* sym_table_) + : p(p_), sym_table(sym_table_), def_kw(0x100), def_sym(0) { + std::memset(keywords, 0, sizeof(keywords)); + } + static constexpr sym_idx_t not_found = 0; + SymTableBase& add_keyword(std::string str, sym_idx_t idx = 0); + SymTableBase& add_kw_char(char c) { + return add_keyword(std::string{c}, c); + } + sym_idx_t lookup(std::string str, int mode = 0) { + return gen_lookup(str, mode); + } + sym_idx_t lookup_add(std::string str) { + return gen_lookup(str, 1); + } + Symbol* operator[](sym_idx_t i) const { + return sym_table[i].get(); + } + bool is_keyword(sym_idx_t i) const { + return sym_table[i] && sym_table[i]->idx < 0; + } + std::string get_name(sym_idx_t i) const { + return sym_table[i] ? sym_table[i]->str : Symbol::unknown_symbol_name(i); + } + int get_subclass(sym_idx_t i) const { + return sym_table[i] ? sym_table[i]->subclass : 0; + } + Symbol* get_keyword(int i) const { + return ((unsigned)i < (unsigned)max_kw_idx) ? sym_table[keywords[i]].get() : nullptr; + } + + protected: + sym_idx_t gen_lookup(std::string str, int mode = 0, sym_idx_t idx = 0); +}; + +template +class SymTable : public SymTableBase { + public: + static constexpr int hprime = pp; + static int size() { + return pp + 1; + } + + private: + std::unique_ptr sym[pp + 1]; + + public: + SymTable() : SymTableBase(pp, sym) { + } + SymTable& add_keyword(std::string str, sym_idx_t idx = 0) { + SymTableBase::add_keyword(str, idx); + return *this; + } + SymTable& add_kw_char(char c) { + return add_keyword(std::string{c}, c); + } +}; + +struct SymTableOverflow { + int sym_def; + SymTableOverflow(int x) : sym_def(x) { + } +}; + +struct SymTableKwRedef { + std::string kw; + SymTableKwRedef(std::string _kw) : kw(_kw) { + } +}; + +extern SymTable<100003> symbols; + +extern int scope_level; + +struct SymDef { + int level; + sym_idx_t sym_idx; + SymValBase* value; + src::SrcLocation loc; + SymDef(int lvl, sym_idx_t idx, const src::SrcLocation& _loc = {}, SymValBase* val = 0) + : level(lvl), sym_idx(idx), value(val), loc(_loc) { + } + bool has_name() const { + return sym_idx; + } + std::string name() const { + return symbols.get_name(sym_idx); + } +}; + +extern SymDef* sym_def[symbols.hprime]; +extern SymDef* global_sym_def[symbols.hprime]; +extern std::vector> symbol_stack; +extern std::vector scope_opened_at; + +void open_scope(src::Lexer& lex); +void close_scope(src::Lexer& lex); +SymDef* lookup_symbol(sym_idx_t idx, int flags = 3); +SymDef* lookup_symbol(std::string name, int flags = 3); + +SymDef* define_global_symbol(sym_idx_t name_idx, bool force_new = false, const src::SrcLocation& loc = {}); +SymDef* define_symbol(sym_idx_t name_idx, bool force_new = false, const src::SrcLocation& loc = {}); + +} // namespace sym diff --git a/crypto/smartcont/CreateState.fif b/crypto/smartcont/CreateState.fif new file mode 100644 index 00000000..a2711475 --- /dev/null +++ b/crypto/smartcont/CreateState.fif @@ -0,0 +1,167 @@ +"Asm.fif" include + +31 -1<< constant wc_undef +0 constant wc_base +-1 constant wc_master + +{ wcid@ 2dup <> swap wc_undef <> and + abort"workchain_id already set to another value" + wcid! } : setworkchain +{ globalid@ 2dup <> swap and + abort"global_id already set to another value" + dup 0= abort"global_id cannot be zero" + globalid! } : setglobalid + + constant empty_cell + +// b x --> b' ( serializes a Gram amount ) +{ -1 { 1+ 2dup 8 * ufits } until + rot over 4 u, -rot 8 * u, } : Gram, + +// workchain-id -- wc-zero-state +{ ref, 0 1 u, + ref, + ref, 0 1 u, b> + dup isShardState? not abort"invalid ShardState created" +} : mkemptyShardState + +' dictnew : Libs{ +'nop : }Libs +{ 16 config! } : config.validator_num! + +// min-stake max-stake min-total-stake max-factor -- +{ 4 0 reverse + 17 config! +} : config.validator_stake_limits! + +// elected-for elections-begin-before elections-end-before stakes-frozen +{ 4 0 reverse 15 config! } : config.election_params! + +dictnew 0 2constant validator-dict +{ @' validator-dict } : validator-dict@ +{ validator-dict@ nip } : validator# +// val-pubkey weight -- +{ dup 0<= abort"validator weight must be non-negative" + dup 64 ufits not abort"validator weight must fit into 64 bits" + over Blen 32 <> abort"validator public key must be 32 bytes long" + + 34 config! +} : config.validators! + +dictnew constant workchain-dict +// root-hash file-hash enable-utime actual-min-split min-split max-split workchain-id -- +{ + dup isWorkchainDescr? not abort"invalid WorkchainDescr created" + s s>c 12 config! } : config.workchains! + +dictnew constant special-dict +// special-smc-addr -- +{ x{} swap @' special-dict 256 udict! not abort"cannot add a new special smart contract" + =: special-dict +} : make_special +{ @' special-dict dict>s s>c 31 config! } : config.special! + +// bit-pps cell-pps mc-bit-pps mc-cell-pps -- +{ udict! 0= abort"cannot create storage prices dictionary" + 18 config! +} : config.storage_prices! +{ 16 << } : sg* +{ 16 < +} : make-gas-prices +{ make-gas-prices 20 config! } : config.mc_gas_prices! +{ make-gas-prices 21 config! } : config.gas_prices! + +// lump_price bit_price cell_price ihr_factor first_frac next_frac +{ 6 0 reverse +} : make-msg-fwd-prices +{ make-msg-fwd-prices 24 config! } : config.mc_fwd_prices! +{ make-msg-fwd-prices 25 config! } : config.fwd_prices! + +// mc-cc-lifetime sh-cc-lifetime sh-val-lifetime sh-val-num +{ 4 0 reverse +} : make-catchain-params +{ make-catchain-params 28 config! } : config.catchain_params! +// round-candidates next-cand-delay-ms consensus-timeout-ms fast-attempts attempt-duration cc-max-deps max-block-size max-collated-size +{ 8 0 reverse } : make-vsession-params +{ make-vsession-params 29 config! } : config.consensus_params! + +// b [underload soft hard] -- b' +{ untriple 3 roll x{c3} s, 3 roll 32 u, rot 32 u, swap 32 u, } : param_limits, +// bytes-limits gas-limits lt-limits -- c +{ +} : make-block-limits +{ make-block-limits 22 config! } : config.mc_block_limits! +{ make-block-limits 23 config! } : config.block_limits! + +// mc-block-create-fee bc-block-create-fee +{ } : make-block-create-fees +{ make-block-create-fees 14 config! } : config.block_create_fees! + +{ } : make-smc-addr-cell +{ make-smc-addr-cell 0 config! } : config.config_smc! +{ make-smc-addr-cell 1 config! } : config.elector_smc! +{ make-smc-addr-cell 2 config! } : config.minter_smc! +{ make-smc-addr-cell 3 config! } : config.collector_smc! + +{ 7 config! } : config.to_mint! + + +1000000000 constant Gram +1000000 constant mGram +1000 constant uGram +1 constant nGram +{ Gram * } : Gram* +{ mGram * } : mGram* +{ uGram * } : uGram* +'nop : nGram* +{ Gram swap */r } : Gram*/ +{ mGram swap */r } : mGram*/ +{ uGram swap */r } : uGram*/ +{ /r } : nGram*/ +// GR$.17 is equivalent to 170000000 +{ bl word (number) ?dup 0= abort"not a valid Gram amount" + 1- { Gram swap */r } { Gram * } cond + 1 'nop +} ::_ GR$ + +{ 10 << } : *Ki +{ 20 << } : *Mi +{ 10 < 0); + throw_unless(9, total >= main); + return (utime_since, utime_until); +} + +() send_answer(addr, query_id, ans_tag, mode) impure { + ;; int_msg_info$0 ihr_disabled:Bool bounce:Bool bounced:Bool src:MsgAddress -> 011000 + send_raw_message(begin_cell().store_uint(0x18, 6).store_slice(addr).store_uint(0, 5 + 4 + 4 + 64 + 32 + 1 + 1).store_uint(ans_tag, 32).store_uint(query_id, 64).end_cell(), mode); +} + +() send_confirmation(addr, query_id, ans_tag) impure { + return send_answer(addr, query_id, ans_tag, 64); +} + +() send_error(addr, query_id, ans_tag) impure { + return send_answer(addr, query_id, ans_tag, 64); +} + +() recv_internal(cell in_msg_cell, slice in_msg) impure { + var cs = in_msg_cell.begin_parse(); + var flags = cs~load_uint(4); ;; int_msg_info$0 ihr_disabled:Bool bounce:Bool bounced:Bool + var s_addr = cs~load_msg_addr(); + (int src_wc, int src_addr) = s_addr.parse_std_addr(); + if ((src_wc + 1) | (flags & 1) | in_msg.slice_empty?()) { + ;; source not in masterchain, or a bounced message, or a simple transfer + return (); + } + int tag = in_msg~load_uint(32); + int query_id = in_msg~load_uint(64); + if (tag == 0x4e565354) { + ;; set next validator set + var vset = in_msg~load_ref(); + in_msg.end_parse(); + var elector_param = config_param(1); + var elector_addr = cell_null?(elector_param) ? -1 : elector_param.begin_parse().preload_uint(256); + var ok = false; + if (src_addr == elector_addr) { + ;; message from elector smart contract + ;; set next validator set + (var t_since, var t_until) = check_validator_set(vset); + var t = now(); + ok = (t_since > t) & (t_until > t_since); + } + if (ok) { + set_conf_param(36, vset); + ;; send confirmation + return send_confirmation(s_addr, query_id, 0xee764f4b); + } else { + return send_error(s_addr, query_id, 0xee764f6f); + } + } + ;; if tag is non-zero and its higher bit is zero, throw an exception (the message is an unsupported query) + ;; to bounce message back to sender + throw_unless(37, (tag == 0) | (tag & (1 << 31))); + ;; do nothing for other internal messages +} + +;; forward a message to elector smart contract to make it upgrade its code +() change_elector_code(slice cs) impure { + var dest_addr = config_param(1).begin_parse().preload_uint(256); + var query_id = now(); + send_raw_message(begin_cell() + .store_uint(0xc4ff, 17) + .store_uint(dest_addr, 256) + .store_grams(1 << 30) ;; ~ 1 Gram (will be returned back) + .store_uint(0, 1 + 4 + 4 + 64 + 32 + 1 + 1) + .store_uint(0x4e436f64, 32) ;; action + .store_uint(query_id, 64) + .store_slice(cs) + .end_cell(), 0); +} + +() recv_external(slice in_msg) impure { + var signature = in_msg~load_bits(512); + var cs = in_msg; + int action = cs~load_uint(32); + int msg_seqno = cs~load_uint(32); + var valid_until = cs~load_uint(32); + throw_if(35, valid_until < now()); + var cs2 = begin_parse(get_data()); + var cfg_dict = cs2~load_ref(); + var stored_seqno = cs2~load_uint(32); + var public_key = cs2~load_uint(256); + cs2.end_parse(); + throw_unless(33, msg_seqno == stored_seqno); + throw_unless(34, check_signature(slice_hash(in_msg), signature, public_key)); + accept_message(); + if (action == 0x43665021) { + ;; change one configuration parameter + var param_index = cs~load_uint(32); + var param_value = cs~load_ref(); + cs.end_parse(); + cfg_dict~idict_set_ref(32, param_index, param_value); + } elseif (action == 0x4e436f64) { + ;; change configuration smart contract code + var new_code = cs~load_ref(); + cs.end_parse(); + set_code(new_code); + } elseif (action == 0x50624b21) { + ;; change configuration master public key + public_key = cs~load_uint(256); + cs.end_parse(); + } elseif (action == 0x4e43ef05) { + ;; change election smart contract code + change_elector_code(cs); + } else { + throw_if(32, action); + } + set_data(begin_cell().store_ref(cfg_dict).store_uint(stored_seqno + 1, 32).store_uint(public_key, 256).end_cell()); +} + +() run_ticktock(int is_tock) impure { + var cs = begin_parse(get_data()); + var cfg_dict = cs~load_ref(); + int kl = 32; + cfg_dict~idict_set_ref(kl, -17, begin_cell().store_uint(now() >> 8, 32).end_cell()); + var next_vset = cfg_dict.idict_get_ref(kl, 36); + ifnot (next_vset.null?()) { + ;; check whether we have to set next_vset as the current validator set + var ds = next_vset.begin_parse(); + if (ds.slice_bits() >= 40) { + var tag = ds~load_uint(8); + var since = ds.preload_uint(32); + if ((tag == 0x11) & (since >= now())) { + ;; next validator set becomes active! + var cur_vset = cfg_dict~idict_set_get_ref(kl, 34, next_vset); ;; next_vset -> cur_vset + cfg_dict~idict_set_get_ref(kl, 32, cur_vset); ;; cur_vset -> prev_vset + cfg_dict~idict_delete?(kl, 36); ;; (null) -> next_vset + } + } + } + set_data(begin_cell().store_ref(cfg_dict).store_slice(cs).end_cell()); +} diff --git a/crypto/smartcont/elector-code.fc b/crypto/smartcont/elector-code.fc new file mode 100644 index 00000000..b97b0b3c --- /dev/null +++ b/crypto/smartcont/elector-code.fc @@ -0,0 +1,798 @@ +;; Elector smartcontract + +;; cur_elect credits past_elect grams active_id active_hash +(cell, cell, cell, int, int, int) load_data() { + var cs = get_data().begin_parse(); + var res = (cs~load_dict(), cs~load_dict(), cs~load_dict(), cs~load_grams(), cs~load_uint(32), cs~load_uint(256)); + cs.end_parse(); + return res; +} + +;; cur_elect credits past_elect grams active_id active_hash +() store_data(elect, credits, past_elect, grams, active_id, active_hash) impure { + set_data(begin_cell() + .store_dict(elect) + .store_dict(credits) + .store_dict(past_elect) + .store_grams(grams) + .store_uint(active_id, 32) + .store_uint(active_hash, 256) + .end_cell()); +} + +;; elect -> elect_at elect_close min_stake total_stake members failed finished +_ unpack_elect(elect) { + var es = elect.begin_parse(); + var res = (es~load_uint(32), es~load_uint(32), es~load_grams(), es~load_grams(), es~load_dict(), es~load_int(1), es~load_int(1)); + es.end_parse(); + return res; +} + +cell pack_elect(elect_at, elect_close, min_stake, total_stake, members, failed, finished) { + return begin_cell() + .store_uint(elect_at, 32) + .store_uint(elect_close, 32) + .store_grams(min_stake) + .store_grams(total_stake) + .store_dict(members) + .store_int(failed, 1) + .store_int(finished, 1) + .end_cell(); +} + +;; elected_for elections_begin_before elections_end_before stake_held_for +(int, int, int, int) get_validator_conf() { + var cs = config_param(15).begin_parse(); + return (cs~load_int(32), cs~load_int(32), cs~load_int(32), cs.preload_int(32)); +} + +() send_message_back(addr, ans_tag, query_id, body, grams, mode) impure { + ;; int_msg_info$0 ihr_disabled:Bool bounce:Bool bounced:Bool src:MsgAddress -> 011000 + var msg = begin_cell() + .store_uint(0x18, 6) + .store_slice(addr) + .store_grams(grams) + .store_uint(0, 1 + 4 + 4 + 64 + 32 + 1 + 1) + .store_uint(ans_tag, 32) + .store_uint(query_id, 64); + if (body >= 0) { + msg~store_uint(body, 32); + } + send_raw_message(msg.end_cell(), mode); +} + +() return_stake(addr, query_id, reason) impure { + return send_message_back(addr, 0xee6f454c, query_id, reason, 0, 64); +} + +() send_confirmation(addr, query_id, comment) impure { + return send_message_back(addr, 0xf374484c, query_id, comment, 1000000000, 2); +} + +() send_validator_set_to_config(config_addr, vset, query_id) impure { + var msg = begin_cell() + .store_uint(0xc4ff, 17) ;; 0 11000100 0xff + .store_uint(config_addr, 256) + .store_grams(1 << 30) ;; ~1 gram of value to process and obtain answer + .store_uint(0, 1 + 4 + 4 + 64 + 32 + 1 + 1) + .store_uint(0x4e565354, 32) + .store_uint(query_id, 64) + .store_ref(vset); + send_raw_message(msg.end_cell(), 1); +} + +;; credits 'amount' to 'addr' inside credit dictionary 'credits' +_ ~credit_to(credits, addr, amount) { + var (val, f) = credits.udict_get?(256, addr); + if (f) { + amount += val~load_grams(); + } + credits~udict_set_builder(256, addr, begin_cell().store_grams(amount)); + return (credits, ()); +} + +() process_new_stake(s_addr, msg_value, cs, query_id) impure { + var (src_wc, src_addr) = parse_std_addr(s_addr); + var ds = get_data().begin_parse(); + var elect = ds~load_dict(); + if (null?(elect) | (src_wc + 1)) { + ;; no elections active, or source is not in masterchain + ;; bounce message + return return_stake(s_addr, query_id, 0); + } + ;; parse the remainder of new stake message + var validator_pubkey = cs~load_uint(256); + var stake_at = cs~load_uint(32); + var max_factor = cs~load_uint(32); + var adnl_addr = cs~load_uint(256); + var signature = cs~load_ref().begin_parse().preload_bits(512); + cs.end_parse(); + ifnot (check_data_signature(begin_cell() + .store_uint(0x654c5074, 32) + .store_uint(stake_at, 32) + .store_uint(max_factor, 32) + .store_uint(src_addr, 256) + .store_uint(adnl_addr, 256) + .end_cell().begin_parse(), signature, validator_pubkey)) { + ;; incorrect signature, return stake + return return_stake(s_addr, query_id, 1); + } + if (max_factor < 0x10000) { + ;; factor must be >= 1. = 65536/65536 + return return_stake(s_addr, query_id, 6); + } + ;; parse current election data + var (elect_at, elect_close, min_stake, total_stake, members, failed, finished) = elect.unpack_elect(); + elect_at~dump(); + msg_value -= 1000000000; ;; deduct GR$1 for sending confirmation + if ((msg_value << 12) < total_stake) { + ;; stake smaller than 1/4096 of the total accumulated stakes, return + return return_stake(s_addr, query_id, 2); + } + total_stake += msg_value; ;; (provisionally) increase total stake + if (stake_at != elect_at) { + ;; stake for some other elections, return + return return_stake(s_addr, query_id, 3); + } + if (finished) { + ;; elections already finished, return stake + return return_stake(s_addr, query_id, 0); + } + var (mem, found) = members.udict_get?(256, validator_pubkey); + if (found) { + ;; entry found, merge stakes + msg_value += mem~load_grams(); + mem~load_uint(64); ;; skip timestamp and max_factor + found = (src_addr != mem~load_uint(256)); + } + if (found) { + ;; can make stakes for a public key from one address only + return return_stake(s_addr, query_id, 4); + } + if (msg_value < min_stake) { + ;; stake too small, return it + return return_stake(s_addr, query_id, 5); + } + throw_unless(44, msg_value); + accept_message(); + ;; store stake in the dictionary + members~udict_set_builder(256, validator_pubkey, begin_cell() + .store_grams(msg_value) + .store_uint(now(), 32) + .store_uint(max_factor, 32) + .store_uint(src_addr, 256) + .store_uint(adnl_addr, 256)); + ;; gather and save election data + elect = pack_elect(elect_at, elect_close, min_stake, total_stake, members, false, false); + set_data(begin_cell().store_dict(elect).store_slice(ds).end_cell()); + ;; return confirmation message + if (query_id) { + return send_confirmation(s_addr, query_id, 0); + } + return (); +} + +(cell, int) unfreeze_without_bonuses(credits, freeze_dict, tot_stakes) { + var total = var recovered = 0; + var pubkey = -1; + do { + (pubkey, var cs, var f) = freeze_dict.udict_get_next?(256, pubkey); + if (f) { + var (addr, weight, stake, banned) = (cs~load_uint(256), cs~load_uint(64), cs~load_grams(), cs~load_int(1)); + cs.end_parse(); + if (banned) { + recovered += stake; + } else { + credits~credit_to(addr, stake); + } + total += stake; + } + } until (~ f); + throw_unless(59, total == tot_stakes); + return (credits, recovered); +} + +(cell, int) unfreeze_with_bonuses(credits, freeze_dict, tot_stakes, tot_bonuses) { + var total = var recovered = var returned_bonuses = 0; + var pubkey = -1; + do { + (pubkey, var cs, var f) = freeze_dict.udict_get_next?(256, pubkey); + if (f) { + var (addr, weight, stake, banned) = (cs~load_uint(256), cs~load_uint(64), cs~load_grams(), cs~load_int(1)); + cs.end_parse(); + if (banned) { + recovered += stake; + } else { + var bonus = muldiv(tot_bonuses, stake, tot_stakes); + returned_bonuses += bonus; + credits~credit_to(addr, stake + bonus); + } + total += stake; + } + } until (~ f); + throw_unless(59, (total == tot_stakes) & (returned_bonuses <= tot_bonuses)); + return (credits, recovered + tot_bonuses - returned_bonuses); +} + +_ unfreeze_all(credits, past_elections, elect_id) { + var (fs, f) = past_elections~udict_delete_get?(32, elect_id); + ifnot (f) { + ;; no elections with this id + return (credits, past_elections, 0); + } + var (data1, vset_hash, fdict, tot_stakes, bonuses, complaints) = (fs~load_uint(64), fs~load_uint(256), fs~load_dict(), fs~load_grams(), fs~load_grams(), fs~load_dict()); + fs.end_parse(); + var unused_prizes = (bonuses > 0) ? + credits~unfreeze_with_bonuses(fdict, tot_stakes, bonuses) : + credits~unfreeze_without_bonuses(fdict, tot_stakes); + return (credits, past_elections, unused_prizes); +} + +() config_set_confirmed(s_addr, cs, query_id, ok) impure { + var (src_wc, src_addr) = parse_std_addr(s_addr); + var config_addr = config_param(0).begin_parse().preload_uint(256); + var ds = get_data().begin_parse(); + var elect = ds~load_dict(); + if ((src_wc + 1) | (src_addr != config_addr) | elect.null?()) { + ;; not from config smc, somebody's joke? + ;; or no elections active (or just completed) + return (); + } + var (elect_at, elect_close, min_stake, total_stake, members, failed, finished) = elect.unpack_elect(); + if ((elect_at != query_id) | ~ finished) { + ;; not these elections, or elections not finished yet + return (); + } + accept_message(); + ifnot (ok) { + ;; cancel elections, return stakes + var (credits, past_elections, grams) = (ds~load_dict(), ds~load_dict(), ds~load_grams()); + (credits, past_elections, var unused_prizes) = unfreeze_all(credits, past_elections, elect_at); + set_data(begin_cell() + .store_int(false, 1) + .store_dict(credits) + .store_dict(past_elections) + .store_grams(grams + unused_prizes) + .store_slice(ds) + .end_cell()); + } + ;; ... do not remove elect until we see this set as the next elected validator set +} + +() process_simple_transfer(s_addr, msg_value) impure { + var (elect, credits, past_elect, grams, active_id, active_hash) = load_data(); + (int src_wc, int src_addr) = parse_std_addr(s_addr); + if (src_addr | (src_wc + 1) | (active_id == 0)) { + ;; simple transfer to us (credit "nobody's" account) + ;; (or no known active validator set) + grams += msg_value; + return store_data(elect, credits, past_elect, grams, active_id, active_hash); + } + ;; zero source address -1:00..00 (collecting validator fees) + var (fs, f) = past_elect.udict_get?(32, active_id); + ifnot (f) { + ;; active validator set not found (?) + grams += msg_value; + } else { + ;; credit active validator set bonuses + var (data, hash, dict, total_stake, bonuses) = (fs~load_uint(64), fs~load_uint(256), fs~load_dict(), fs~load_grams(), fs~load_grams()); + bonuses += msg_value; + past_elect~udict_set_builder(32, active_id, begin_cell() + .store_uint(data, 64) + .store_uint(hash, 256) + .store_dict(dict) + .store_grams(total_stake) + .store_grams(bonuses) + .store_slice(fs)); + } + store_data(elect, credits, past_elect, grams, active_id, active_hash); + return (); +} + +() recover_stake(op, s_addr, cs, query_id) impure { + (int src_wc, int src_addr) = parse_std_addr(s_addr); + if (src_wc + 1) { + ;; not from masterchain, return error + return send_message_back(s_addr, 0xfffffffe, query_id, op, 0, 64); + } + var ds = get_data().begin_parse(); + var (elect, credits) = (ds~load_dict(), ds~load_dict()); + var (cs, f) = credits~udict_delete_get?(256, src_addr); + ifnot (f) { + ;; no credit for sender, return error + return send_message_back(s_addr, 0xfffffffe, query_id, op, 0, 64); + } + var amount = cs~load_grams(); + cs.end_parse(); + ;; save data + set_data(begin_cell().store_dict(elect).store_dict(credits).store_slice(ds).end_cell()); + ;; send amount to sender in a new message + send_raw_message(begin_cell() + .store_uint(0x18, 6) + .store_slice(s_addr) + .store_grams(amount) + .store_uint(0, 1 + 4 + 4 + 64 + 32 + 1 + 1) + .store_uint(0xf96f7324, 32) + .store_uint(query_id, 64) + .end_cell(), 64); +} + +() after_code_upgrade(slice s_addr, slice cs, int query_id) impure method_id(1666) { + var op = 0x4e436f64; + return send_message_back(s_addr, 0xce436f64, query_id, op, 0, 64); +} + +int upgrade_code(s_addr, cs, query_id) { + var c_addr = config_param(0); + if (c_addr.null?()) { + ;; no configuration smart contract known + return false; + } + var config_addr = c_addr.begin_parse().preload_uint(256); + var (src_wc, src_addr) = parse_std_addr(s_addr); + if ((src_wc + 1) | (src_addr != config_addr)) { + ;; not from configuration smart contract, return error + return false; + } + accept_message(); + var code = cs~load_ref(); + set_code(code); + ifnot(cs.slice_empty?()) { + set_c3(code); + ;; run_method3(1666, s_addr, cs, query_id); + after_code_upgrade(s_addr, cs, query_id); + throw(0); + } + return true; +} + +() recv_internal(int msg_value, cell in_msg_cell, slice in_msg) impure { + ;; do nothing for internal messages + var cs = in_msg_cell.begin_parse(); + var flags = cs~load_uint(4); ;; int_msg_info$0 ihr_disabled:Bool bounce:Bool bounced:Bool + if (flags & 1) { + ;; ignore all bounced messages + return (); + } + var s_addr = cs~load_msg_addr(); + if (in_msg.slice_empty?()) { + ;; inbound message has empty body + return process_simple_transfer(s_addr, msg_value); + } + int op = in_msg~load_uint(32); + if (op == 0) { + ;; simple transfer with comment, return + return process_simple_transfer(s_addr, msg_value); + } + int query_id = in_msg~load_uint(64); + if (op == 0x4e73744b) { + ;; new stake message + return process_new_stake(s_addr, msg_value, in_msg, query_id); + } + if (op == 0x47657424) { + ;; recover stake request + return recover_stake(op, s_addr, in_msg, query_id); + } + if (op == 0x4e436f64) { + ;; upgrade code (accepted only from configuration smart contract) + var ok = upgrade_code(s_addr, in_msg, query_id); + return send_message_back(s_addr, ok ? 0xce436f64 : 0xffffffff, query_id, op, 0, 64); + } + var cfg_ok = (op == 0xee764f4b); + if (cfg_ok | (op == 0xee764f6f)) { + ;; confirmation from configuration smart contract + return config_set_confirmed(s_addr, in_msg, query_id, cfg_ok); + } + ifnot (op & (1 << 31)) { + ;; unknown query, return error + return send_message_back(s_addr, 0xffffffff, query_id, op, 0, 64); + } + ;; unknown answer, ignore + return (); +} + +int postpone_elections() impure { + return false; +} + +;; computes the total stake out of the first n entries of list l +_ compute_total_stake(l, n, m_stake) { + int tot_stake = 0; + repeat (n) { + (var h, l) = uncons(l); + var stake = h.at(0); + var max_f = h.at(1); + stake = min(stake, (max_f * m_stake) >> 16); + tot_stake += stake; + } + return tot_stake; +} + +(cell, cell, cell, int, int) try_elect(credits, members, min_stake, max_stake, min_total_stake, max_stake_factor) { + var cs = 16.config_param().begin_parse(); + var (max_validators, _, min_validators) = (cs~load_uint(16), cs~load_uint(16), cs~load_uint(16)); + cs.end_parse(); + min_validators = max(min_validators, 1); + int n = 0; + var sdict = new_dict(); + var pubkey = -1; + do { + (pubkey, var cs, var f) = members.udict_get_next?(256, pubkey); + if (f) { + var (stake, time, max_factor, addr, adnl_addr) = (cs~load_grams(), cs~load_uint(32), cs~load_uint(32), cs~load_uint(256), cs~load_uint(256)); + cs.end_parse(); + var key = begin_cell() + .store_uint(stake, 128) + .store_int(- time, 32) + .store_uint(pubkey, 256) + .end_cell().begin_parse(); + sdict~dict_set_builder(128 + 32 + 256, key, begin_cell() + .store_uint(min(max_factor, max_stake_factor), 32) + .store_uint(addr, 256) + .store_uint(adnl_addr, 256)); + n += 1; + } + } until (~ f); + n = min(n, max_validators); + if (n < min_validators) { + return (credits, new_dict(), new_dict(), 0, 0); + } + var l = nil; + do { + var (key, cs, f) = sdict~dict::delete_get_min(128 + 32 + 256); + if (f) { + var (stake, _, pubkey) = (min(key~load_uint(128), max_stake), key~load_uint(32), key.preload_uint(256)); + var (max_f, _, adnl_addr) = (cs~load_uint(32), cs~load_uint(256), cs.preload_uint(256)); + l = cons(tuple4(stake, max_f, pubkey, adnl_addr), l); + } + } until (~ f); + ;; l is the list of all stakes in decreasing order + int i = min_validators - 1; + var l1 = l; + repeat (i) { + l1 = cdr(l1); + } + var (best_stake, m) = (0, 0); + do { + var stake = l1~list_next().at(0); + i += 1; + if (stake >= min_stake) { + var tot_stake = compute_total_stake(l, i, stake); + if (tot_stake > best_stake) { + (best_stake, m) = (tot_stake, i); + } + } + } until (i >= n); + if ((m == 0) | (best_stake < min_total_stake)) { + return (credits, new_dict(), new_dict(), 0, 0); + } + ;; we have to select first m validators from list l + l1 = touch(l); + l1~dump(); ;; DEBUG + repeat (m - 1) { + l1 = cdr(l1); + } + var m_stake = car(l1).at(0); ;; minimal stake + ;; create both the new validator set and the refund set + int i = 0; + var tot_stake = 0; + var vset = new_dict(); + var frozen = new_dict(); + do { + var (stake, max_f, pubkey, adnl_addr) = l~list_next().untuple4(); + ;; lookup source address first + var (val, f) = members.udict_get?(256, pubkey); + throw_unless(61, f); + (_, _, var src_addr) = (val~load_grams(), val~load_uint(64), val.preload_uint(256)); + if (i < m) { + ;; one of the first m members, include into validator set + var true_stake = min(stake, (max_f * m_stake) >> 16); + stake -= true_stake; + ;; ed25519_pubkey#8e81278a pubkey:bits256 = SigPubKey; // 288 bits + ;; validator_addr#73 public_key:SigPubKey weight:uint64 adnl_addr:bits256 = ValidatorDescr; + var weight = (true_stake << 60) / best_stake; + tot_stake += true_stake; + var vinfo = begin_cell() + .store_uint(adnl_addr ? 0x73 : 0x53, 8) ;; validator_addr#73 or validator#53 + .store_uint(0x8e81278a, 32) ;; ed25519_pubkey#8e81278a + .store_uint(pubkey, 256) ;; pubkey:bits256 + .store_uint(weight, 64); ;; weight:uint64 + if (adnl_addr) { + vinfo~store_uint(adnl_addr, 256); ;; adnl_addr:bits256 + } + vset~udict_set_builder(16, i, vinfo); + frozen~udict_set_builder(256, pubkey, begin_cell() + .store_uint(src_addr, 256) + .store_uint(weight, 64) + .store_grams(true_stake) + .store_int(false, 1)); + } + if (stake) { + ;; non-zero unused part of the stake, credit to the source address + credits~credit_to(src_addr, stake); + } + i += 1; + } until (l.null?()); + throw_unless(49, tot_stake == best_stake); + return (credits, vset, frozen, tot_stake, m); +} + +int conduct_elections(ds, elect, credits) impure { + var (elect_at, elect_close, min_stake, total_stake, members, failed, finished) = elect.unpack_elect(); + if (now() < elect_close) { + ;; elections not finished yet + return false; + } + if (config_param(0).null?()) { + ;; no configuration smart contract to send result to + return postpone_elections(); + } + var cs = config_param(17).begin_parse(); + min_stake = cs~load_grams(); + var max_stake = cs~load_grams(); + var min_total_stake = cs~load_grams(); + var max_stake_factor = cs~load_uint(32); + cs.end_parse(); + if (total_stake < min_total_stake) { + ;; insufficient total stake, postpone elections + return postpone_elections(); + } + if (failed) { + ;; do not retry failed elections until new stakes arrive + return postpone_elections(); + } + if (finished) { + ;; elections finished + return false; + } + (credits, var vdict, var frozen, var total_stakes, var cnt) = try_elect(credits, members, min_stake, max_stake, min_total_stake, max_stake_factor); + ;; pack elections; if cnt==0, set failed=true, finished=false. + failed = (cnt == 0); + finished = ~ failed; + elect = pack_elect(elect_at, elect_close, min_stake, total_stake, members, failed, finished); + ifnot (cnt) { + ;; elections failed, set elect_failed to true + set_data(begin_cell().store_dict(elect).store_dict(credits).store_slice(ds).end_cell()); + return postpone_elections(); + } + ;; serialize a query to the configuration smart contract + ;; to install the computed validator set as the next validator set + var (elect_for, elect_begin_before, elect_end_before, stake_held) = get_validator_conf(); + var start = max(now() + elect_end_before - 60, elect_at); + var main_validators = config_param(16).begin_parse().skip_bits(16).preload_uint(16); + var vset = begin_cell() + .store_uint(0x11, 8) ;; validators#11 + .store_uint(start, 32) ;; utime_since:uint32 + .store_uint(start + elect_for, 32) ;; utime_until:uint32 + .store_uint(cnt, 16) ;; total:(## 16) + .store_uint(min(cnt, main_validators), 16) ;; main:(## 16) + .store_slice(vdict.begin_parse()) ;; list:(Hashmap 16 ValidatorDescr) + .end_cell(); + var config_addr = config_param(0).begin_parse().preload_uint(256); + send_validator_set_to_config(config_addr, vset, elect_at); + ;; add frozen to the dictionary of past elections + var past_elect = ds~load_dict(); + past_elect~udict_set_builder(32, elect_at, begin_cell() + .store_uint(start + elect_for + stake_held, 32) + .store_uint(stake_held, 32) + .store_uint(cell_hash(vset), 256) + .store_dict(frozen) + .store_grams(total_stakes) + .store_grams(0) + .store_int(false, 1)); + ;; store credits and frozen until end + set_data(begin_cell() + .store_dict(elect) + .store_dict(credits) + .store_dict(past_elect) + .store_slice(ds) + .end_cell()); + return true; +} + +int update_active_vset_id() impure { + var (elect, credits, past_elect, grams, active_id, active_hash) = load_data(); + var cur_hash = config_param(34).cell_hash(); + if (cur_hash == active_hash) { + ;; validator set unchanged + return false; + } + if (active_id) { + ;; active_id becomes inactive + var (fs, f) = past_elect.udict_get?(32, active_id); + if (f) { + ;; adjust unfreeze time of this validator set + var unfreeze_time = fs~load_uint(32); + var fs0 = fs; + var (stake_held, hash) = (fs~load_uint(32), fs~load_uint(256)); + throw_unless(57, hash == active_hash); + unfreeze_time = now() + stake_held; + past_elect~udict_set_builder(32, active_id, begin_cell() + .store_uint(unfreeze_time, 32) + .store_slice(fs0)); + } + } + ;; look up new active_id by hash + var id = -1; + do { + (id, var fs, var f) = past_elect.udict_get_next?(32, id); + if (f) { + var (tm, hash) = (fs~load_uint(64), fs~load_uint(256)); + if (hash == cur_hash) { + ;; parse more of this record + var (dict, total_stake, bonuses) = (fs~load_dict(), fs~load_grams(), fs~load_grams()); + ;; transfer 1/8 of accumulated everybody's grams to this validator set as bonuses + var amount = (grams >> 3); + grams -= amount; + bonuses += amount; + ;; serialize back + past_elect~udict_set_builder(32, id, begin_cell() + .store_uint(tm, 64) + .store_uint(hash, 256) + .store_dict(dict) + .store_grams(total_stake) + .store_grams(bonuses) + .store_slice(fs)); + ;; found + f = false; + } + } + } until (~ f); + active_id = (id.null?() ? 0 : id); + active_hash = cur_hash; + store_data(elect, credits, past_elect, grams, active_id, active_hash); + return true; +} + +int cell_hash_eq?(cell vset, int expected_vset_hash) { + return vset.null?() ? false : cell_hash(vset) == expected_vset_hash; +} + +int validator_set_installed(ds, elect, credits) impure { + var (elect_at, elect_close, min_stake, total_stake, members, failed, finished) = elect.unpack_elect(); + ifnot (finished) { + ;; elections not finished yet + return false; + } + var past_elections = ds~load_dict(); + var (fs, f) = past_elections.udict_get?(32, elect_at); + ifnot (f) { + ;; no election data in dictionary + return false; + } + ;; recover validator set hash + var vset_hash = fs.skip_bits(64).preload_uint(256); + if (config_param(34).cell_hash_eq?(vset_hash) | config_param(36).cell_hash_eq?(vset_hash)) { + ;; this validator set has been installed, forget elections + set_data(begin_cell() + .store_int(false, 1) ;; forget current elections + .store_dict(credits) + .store_dict(past_elections) + .store_slice(ds) + .end_cell()); + update_active_vset_id(); + return true; + } + return false; +} + +int check_unfreeze() impure { + var (elect, credits, past_elect, grams, active_id, active_hash) = load_data(); + int id = -1; + do { + (id, var fs, var f) = past_elect.udict_get_next?(32, id); + if (f) { + var unfreeze_at = fs~load_uint(32); + if ((unfreeze_at <= now()) & (id != active_id)) { + ;; unfreeze! + (credits, past_elect, var unused_prizes) = unfreeze_all(credits, past_elect, id); + grams += unused_prizes; + ;; unfreeze only one at time, exit loop + store_data(elect, credits, past_elect, grams, active_id, active_hash); + ;; exit loop + f = false; + } + } + } until (~ f); + return ~ id.null?(); +} + +int announce_new_elections(ds, elect, credits) { + var next_vset = config_param(36); ;; next validator set + ifnot (next_vset.null?()) { + ;; next validator set exists, no elections needed + return false; + } + var elector_addr = config_param(1).begin_parse().preload_uint(256); + var (my_wc, my_addr) = my_address().parse_std_addr(); + if ((my_wc + 1) | (my_addr != elector_addr)) { + ;; this smart contract is not the elections smart contract anymore, no new elections + return false; + } + var cur_vset = config_param(34); ;; current validator set + if (cur_vset.null?()) { + return false; + } + var (elect_for, elect_begin_before, elect_end_before, stake_held) = get_validator_conf(); + var cur_valid_until = cur_vset.begin_parse().skip_bits(8 + 32).preload_uint(32); + var t = now(); + var t0 = cur_valid_until - elect_begin_before; + if (t < t0) { + ;; too early for the next elections + return false; + } + ;; less than elect_before_begin seconds left, create new elections + if (t - t0 < 60) { + ;; pretend that the elections started at t0 + t = t0; + } + ;; get stake parameters + (_, var min_stake) = config_param(17).begin_parse().load_grams(); + ;; announce new elections + var elect_at = t + elect_begin_before; + elect_at~dump(); + var elect_close = elect_at - elect_end_before; + elect = pack_elect(elect_at, elect_close, min_stake, 0, new_dict(), false, false); + set_data(begin_cell().store_dict(elect).store_dict(credits).store_slice(ds).end_cell()); + return true; +} + +() run_ticktock(int is_tock) impure { + ;; check whether an election is being conducted + var ds = get_data().begin_parse(); + var (elect, credits) = (ds~load_dict(), ds~load_dict()); + ifnot (elect.null?()) { + ;; have an active election + throw_if(0, conduct_elections(ds, elect, credits)); ;; elections conducted, exit + throw_if(0, validator_set_installed(ds, elect, credits)); ;; validator set installed, current elections removed + } else { + throw_if(0, announce_new_elections(ds, elect, credits)); ;; new elections announced, exit + } + throw_if(0, update_active_vset_id()); ;; active validator set id updated, exit + check_unfreeze(); +} + +;; Get methods + +;; returns active election id or 0 +int active_election_id() method_id { + var elect = get_data().begin_parse().preload_dict(); + return elect.null?() ? 0 : elect.begin_parse().preload_uint(32); +} + +;; checks whether a public key participates in current elections +int participates_in(int validator_pubkey) method_id { + var elect = get_data().begin_parse().preload_dict(); + if (elect.null?()) { + return 0; + } + var (elect_at, elect_close, min_stake, total_stake, members, failed, finished) = elect.unpack_elect(); + var (mem, found) = members.udict_get?(256, validator_pubkey); + return found ? mem~load_grams() : 0; +} + +;; returns the list of all participants of current elections with their stakes +_ participant_list() method_id { + var elect = get_data().begin_parse().preload_dict(); + if (elect.null?()) { + return nil; + } + var (elect_at, elect_close, min_stake, total_stake, members, failed, finished) = elect.unpack_elect(); + var l = nil; + var id = (1 << 255) + ((1 << 255) - 1); + do { + (id, var fs, var f) = members.udict_get_prev?(256, id); + if (f) { + l = cons(pair(id, fs~load_grams()), l); + } + } until (~ f); + return l; +} + +;; computes the return stake +int compute_returned_stake(int wallet_addr) method_id { + var cs = get_data().begin_parse(); + (_, var credits) = (cs~load_dict(), cs~load_dict()); + var (val, f) = credits.udict_get?(256, wallet_addr); + return f ? val~load_grams() : 0; +} diff --git a/crypto/smartcont/gen-zerostate-test.fif b/crypto/smartcont/gen-zerostate-test.fif new file mode 100644 index 00000000..58161a23 --- /dev/null +++ b/crypto/smartcont/gen-zerostate-test.fif @@ -0,0 +1,238 @@ +"TonUtil.fif" include +"Asm.fif" include + +def? $1 { @' $1 } { "" } cond constant suffix +{ suffix $+ } : +suffix + +wc_master setworkchain +-17 setglobalid // negative value means a test instance of the blockchain + +// Initial state of Workchain 0 (Basic workchain) + +0 mkemptyShardState + +cr ."initial basechain state is:" cr dup B dup Bx. cr +dup "basestate0" +suffix +".boc" tuck B>file +."(Initial basechain state saved to file " type .")" cr +Bhash dup =: basestate0_fhash +."file hash=" dup x. space 256 u>B dup B>base64url type cr +"basestate0" +suffix +".fhash" B>file +hash dup =: basestate0_rhash +."root hash=" dup x. space 256 u>B dup B>base64url type cr +"basestate0" +suffix +".rhash" B>file + +basestate0_rhash basestate0_fhash now 0 2 32 0 add-std-workchain +config.workchains! + +// SmartContract #1 (Simple wallet) + +<{ SETCP0 DUP IFNOTRET INC 32 THROWIF // return if recv_internal, fail unless recv_external + 512 INT LDSLICEX DUP 32 PLDU // sign cs cnt + c4 PUSHCTR CTOS 32 LDU 256 LDU ENDS // sign cs cnt cnt' pubk + s1 s2 XCPU // sign cs cnt pubk cnt' cnt + EQUAL 33 THROWIFNOT // ( seqno mismatch? ) + s2 PUSH HASHSU // sign cs cnt pubk hash + s0 s4 s4 XC2PU // pubk cs cnt hash sign pubk + CHKSIGNU // pubk cs cnt ? + 34 THROWIFNOT // signature mismatch + ACCEPT + SWAP 32 LDU NIP 8 LDU LDREF ENDS // pubk cnt mode msg + SWAP SENDRAWMSG // pubk cnt ; ( message sent ) + INC NEWC 32 STU 256 STU ENDC c4 POPCTR +}>c +// code + // data +Libs{ + x{ABACABADABACABA} s>c public_lib + x{1234} x{5678} |_ s>c private_lib +}Libs // libraries +GR$1700000000 // balance +0 // split_depth +0 // ticktock +2 // mode: create +register_smc +dup make_special dup constant smc1_addr +Masterchain over +2dup ."wallet address = " .addr cr 2dup 6 .Addr cr +"main-wallet" +suffix +".addr" save-address-verbose + +// SmartContract #2 (Simple money giver for test network) +<{ SETCP0 DUP IFNOTRET INC 32 THROWIF // return if recv_internal, fail unless recv_external + 32 LDU SWAP // cs cnt + c4 PUSHCTR CTOS 32 LDU ENDS // cs cnt cnt' + TUCK EQUAL 33 THROWIFNOT // ( seqno mismatch? ) + ACCEPT // cs cnt' + SWAP 8 LDU LDREF ENDS // cnt'' mode msg + GR$20 INT 3 INT RAWRESERVE // reserve all but 20 Grams from the balance + SWAP SENDRAWMSG + INC NEWC 32 STU ENDC c4 POPCTR // store cnt'' +}>c +// code + // data +empty_cell // libraries +GR$1000000 // initial balance (1m test Grams) +0 0 2 register_smc +dup make_special dup constant smc2_addr +Masterchain over +2dup ."free test gram giver address = " .addr cr 2dup 6 .Addr cr +"testgiver" +suffix +".addr" save-address-verbose + +// SmartContract #3 +PROGRAM{ + recv_internal x{} PROC + run_ticktock PROC:<{ + c4 PUSHCTR CTOS 32 LDU 256 LDU ENDS + NEWC ROT INC 32 STUR OVER 256 STUR ENDC + c4 POPCTR + // first 32 bits of persistent data have been increased + // remaining 256 bits with an address have been fetched + // create new empty message with 0.1 Grams to that address + NEWC b{00100010011111111} STSLICECONST TUCK 256 STU + 100000000 INT STGRAMS // store 0.1 Grams + 1 4 + 4 + 64 + 32 + 1+ 1+ INT STZEROES ENDC + // send raw message from Cell + ZERO SENDRAWMSG + -17 INT 256 STIR 130000000 INT STGRAMS + 107 INT STZEROES ENDC + ZERO // another message with 0.13 Grams to account -17 + NEWC b{11000100100000} "test" $>s |+ STSLICECONST + 123456789 INT STGRAMS + 107 INT STZEROES "Hello, world!" $>s STSLICECONST ENDC + ZERO SENDRAWMSG SENDRAWMSG // external message to address "test" + }> +}END>c +// code + // data +// empty_cell // libraries +Libs{ + x{ABACABADABACABA} s>c public_lib + x{1234} x{5678} |_ s>c public_lib +}Libs // libraries +0x333333333 // balance +0 // split_depth +3 // ticktock: tick +2 // mode: create +register_smc +dup make_special dup constant smc3_addr +."address = " x. cr + +// SmartContract #4 (elector) +"elector-code.fif" include // code in separate source file + // data +GR$10 // balance: 10 grams +0 // split_depth +1 // ticktock: tick +2 // mode: create +register_smc +dup make_special dup constant smc4_addr dup constant elector_addr + +// Configuration Parameters + +// max-validators max-main-validators min-validators +// 9 3 2 config.validator_num! +1000 100 5 config.validator_num! +// min-stake max-stake min-total-stake max-factor +GR$10000 GR$10000000 GR$1000000 sg~10 config.validator_stake_limits! +// elected-for elect-start-before elect-end-before stakes-frozen-for +400000 200000 4000 400000 config.election_params! +// elector-addr +elector_addr config.elector_smc! + +1 sg* 100 sg* 1000 sg* 1000000 sg* config.storage_prices! +config.special! + +// gas_price gas_limit gas_credit block_gas_limit freeze_due_limit delete_due_limit -- +1000 sg* 1000000 10000 10000000 GR$0.1 GR$1.0 config.gas_prices! +10000 sg* 1000000 10000 10000000 GR$0.1 GR$1.0 config.mc_gas_prices! +// lump_price bit_price cell_price ihr_factor first_frac next_frac +1000000 1000 sg* 100000 sg* 3/2 sg*/ 1/3 sg*/ 1/3 sg*/ config.fwd_prices! +10000000 10000 sg* 1000000 sg* 3/2 sg*/ 1/3 sg*/ 1/3 sg*/ config.mc_fwd_prices! +// mc-cc-lifetime sh-cc-lifetime sh-val-lifetime sh-val-num +250 250 1000 7 config.catchain_params! + +128 *Ki 512 *Ki 1 *Mi triple // [ underload soft hard ] : block bytes limit +100000 500000 1000000 triple // gas limits +1000 5000 10000 triple // lt limits +triple dup untriple config.mc_block_limits! +untriple config.block_limits! + +GR$1.7 GR$1 config.block_create_fees! +smc1_addr config.collector_smc! +smc1_addr config.minter_smc! + +1000000000000 -17 of-cc 666666666666 239 of-cc cc+ config.to_mint! + +"validator-keys" +suffix +".pub" file>B +{ dup Blen } { 32 B| swap dup ."Validator public key = " Bx. cr + 17 add-validator } while drop +// newkeypair nip dup ."Validator #1 public key = " Bx. cr +// 17 add-validator +// newkeypair nip dup ."Validator #2 public key = " Bx. cr +// 239 add-validator +now dup 1000000 + 0 config.validators! + +// SmartContract #5 (Config) +PROGRAM{ + recv_internal x{} PROC + recv_external PROC:<{ + 512 INT LDSLICEX DUP 32 LDU 32 PLDU // sign cs cnt valid-until + NOW LEQ 35 THROWIF // ( fail if now >= valid-until ) + c4 PUSH CTOS LDREF 32 LDU 256 LDU ENDS // sign cs cnt dict cnt' pubk + s1 s3 XCPU // sign cs cnt dict pubk cnt' cnt + EQUAL 33 THROWIFNOT // ( seqno mismatch? ) + s3 PUSH HASHSU // sign cs cnt dict pubk hash + s0 s5 s5 XC2PU // pubk cs cnt dict hash sign pubk + CHKSIGNU // pubk cs cnt dict ? + 34 THROWIFNOT // signature mismatch + ACCEPT + ROT 64 LDU NIP 32 LDI LDREF ENDS // pubk cnt dict index value + s0 s2 XCHG 32 INT // pubk cnt value index dict 32 + DICTISETREF ROTREV // dict' pubk cnt + INC NEWC 32 STU 256 STU STREF ENDC c4 POP + }> + run_ticktock PROC:<{ + // store (now >> 8) into config param #-17, if the new value is different + c4 PUSH CTOS LDREF SWAP // s' D + NOW 8 RSHIFT# TUCK // s' t D t + NEWC 32 STU ENDC // s' t D c + -17 INT ROT 32 INT // s' t c -17 D 32 + DICTISETGETREF // s' t D' c' -1 or s' t D' 0 + IF:<{ CTOS 32 LDU ENDS }>ELSE<{ ZERO }> // s' t D' y' + ROT EQUAL IFNOT: // s' D' + NEWC STREF STSLICE ENDC // c + c4 POP + }> +}END>c +// code + // data +empty_cell // libraries +1 // balance +0 1 2 register_smc // tock +dup set_config_smc +Masterchain swap +."config smart contract address = " 2dup .addr cr 2dup 7 .Addr cr +"config-master" +suffix +".addr" save-address-verbose +// Other data + +create_state +cr cr ."new state is:" cr dup B dup Bx. cr +dup "zerostate" +suffix +".boc" tuck B>file +."(Initial masterchain state saved to file " type .")" cr +Bhash dup =: zerostate_fhash +."file hash=" dup x. space 256 u>B dup B>base64url type cr +"zerostate" +suffix +".fhash" B>file +hash dup =: zerostate_rhash ."root hash=" dup x. space 256 u>B dup B>base64url type cr +"zerostate" +suffix +".rhash" B>file +basestate0_rhash ."Basestate0 root hash=" dup x. space 256 u>B B>base64url type cr +basestate0_fhash ."Basestate0 file hash=" dup x. space 256 u>B B>base64url type cr +zerostate_rhash ."Zerostate root hash=" dup x. space 256 u>B B>base64url type cr +zerostate_fhash ."Zerostate file hash=" dup x. space 256 u>B B>base64url type cr diff --git a/crypto/smartcont/gen-zerostate.fif b/crypto/smartcont/gen-zerostate.fif new file mode 100644 index 00000000..436f43bc --- /dev/null +++ b/crypto/smartcont/gen-zerostate.fif @@ -0,0 +1,231 @@ +"TonUtil.fif" include +"Asm.fif" include + +def? $1 { @' $1 } { "" } cond constant suffix +{ suffix $+ } : +suffix + +wc_master setworkchain +-17 setglobalid // negative value means a test instance of the blockchain + +// Initial state of Workchain 0 (Basic workchain) + +0 mkemptyShardState + +cr ."initial basechain state is:" cr dup B dup Bx. cr +dup "basestate0" +suffix +".boc" tuck B>file +."(Initial basechain state saved to file " type .")" cr +Bhash dup =: basestate0_fhash +."file hash=" dup x. space 256 u>B dup B>base64url type cr +"basestate0" +suffix +".fhash" B>file +hash dup =: basestate0_rhash +."root hash=" dup x. space 256 u>B dup B>base64url type cr +"basestate0" +suffix +".rhash" B>file + +basestate0_rhash basestate0_fhash now 0 2 32 0 add-std-workchain +config.workchains! + +// SmartContract #1 (Simple wallet) + +<{ SETCP0 DUP IFNOTRET INC 32 THROWIF // return if recv_internal, fail unless recv_external + 512 INT LDSLICEX DUP 32 PLDU // sign cs cnt + c4 PUSHCTR CTOS 32 LDU 256 LDU ENDS // sign cs cnt cnt' pubk + s1 s2 XCPU // sign cs cnt pubk cnt' cnt + EQUAL 33 THROWIFNOT // ( seqno mismatch? ) + s2 PUSH HASHSU // sign cs cnt pubk hash + s0 s4 s4 XC2PU // pubk cs cnt hash sign pubk + CHKSIGNU // pubk cs cnt ? + 34 THROWIFNOT // signature mismatch + ACCEPT + SWAP 32 LDU NIP 8 LDU LDREF ENDS // pubk cnt mode msg + SWAP SENDRAWMSG // pubk cnt ; ( message sent ) + INC NEWC 32 STU 256 STU ENDC c4 POPCTR +}>c +// code + // data +Libs{ + x{ABACABADABACABA} s>c public_lib + x{1234} x{5678} |_ s>c private_lib +}Libs // libraries +GR$1700000000 // balance +0 // split_depth +0 // ticktock +2 // mode: create +register_smc +dup make_special dup constant smc1_addr +Masterchain over +2dup ."wallet address = " .addr cr 2dup 6 .Addr cr +"main-wallet" +suffix +".addr" save-address-verbose + +// SmartContract #2 (Simple money giver for test network) +<{ SETCP0 DUP IFNOTRET INC 32 THROWIF // return if recv_internal, fail unless recv_external + 32 LDU SWAP // cs cnt + c4 PUSHCTR CTOS 32 LDU ENDS // cs cnt cnt' + TUCK EQUAL 33 THROWIFNOT // ( seqno mismatch? ) + ACCEPT // cs cnt' + SWAP 8 LDU LDREF ENDS // cnt'' mode msg + GR$20 INT 3 INT RAWRESERVE // reserve all but 20 Grams from the balance + SWAP SENDRAWMSG + INC NEWC 32 STU ENDC c4 POPCTR // store cnt'' +}>c +// code + // data +empty_cell // libraries +GR$1000000 // initial balance (1m test Grams) +0 0 2 register_smc +dup make_special dup constant smc2_addr +Masterchain over +2dup ."free test gram giver address = " .addr cr 2dup 6 .Addr cr +"testgiver" +suffix +".addr" save-address-verbose + +// SmartContract #3 +PROGRAM{ + recv_internal x{} PROC + run_ticktock PROC:<{ + c4 PUSHCTR CTOS 32 LDU 256 LDU ENDS + NEWC ROT INC 32 STUR OVER 256 STUR ENDC + c4 POPCTR + // first 32 bits of persistent data have been increased + // remaining 256 bits with an address have been fetched + // create new empty message with 0.1 Grams to that address + NEWC b{00100010011111111} STSLICECONST TUCK 256 STU + 100000000 INT STGRAMS // store 0.1 Grams + 1 4 + 4 + 64 + 32 + 1+ 1+ INT STZEROES ENDC + // send raw message from Cell + ZERO SENDRAWMSG + -17 INT 256 STIR 130000000 INT STGRAMS + 107 INT STZEROES ENDC + ZERO // another message with 0.13 Grams to account -17 + NEWC b{11000100100000} "test" $>s |+ STSLICECONST + 123456789 INT STGRAMS + 107 INT STZEROES "Hello, world!" $>s STSLICECONST ENDC + ZERO SENDRAWMSG SENDRAWMSG // external message to address "test" + }> +}END>c +// code + // data +// empty_cell // libraries +Libs{ + x{ABACABADABACABA} s>c public_lib + x{1234} x{5678} |_ s>c public_lib +}Libs // libraries +0x333333333 // balance +0 // split_depth +3 // ticktock: tick +2 // mode: create +register_smc +dup make_special dup constant smc3_addr +."address = " x. cr + +/* + * + * SmartContract #4 (elector) + * + */ +"elector-code.fif" include // code in separate source file + // data: dict dict dict grams uint32 uint256 +empty_cell // libraries +GR$10 // balance: 10 grams +0 // split_depth +2 // ticktock: tick +2 // mode: create +register_smc +dup make_special dup constant smc4_addr dup constant elector_addr +Masterchain swap +."elector smart contract address = " 2dup .addr cr 2dup 7 .Addr cr +"elector" +suffix +".addr" save-address-verbose + +/* + * + * Configuration Parameters + * + */ +// max-validators max-main-validators min-validators +// 9 4 1 config.validator_num! +1000 100 5 config.validator_num! +// min-stake max-stake min-total-stake max-factor +GR$10000 GR$10000000 GR$1000000 sg~10 config.validator_stake_limits! +// elected-for elect-start-before elect-end-before stakes-frozen-for +// 400000 200000 4000 400000 config.election_params! +4000 2000 500 1000 config.election_params! // DEBUG +// config-addr = -1:5555...5555 +256 1<<1- 3 / constant config_addr +config_addr config.config_smc! +// elector-addr +elector_addr config.elector_smc! + +1 sg* 100 sg* 1000 sg* 1000000 sg* config.storage_prices! +config.special! + +// gas_price gas_limit gas_credit block_gas_limit freeze_due_limit delete_due_limit -- +1000 sg* 1000000 10000 10000000 GR$0.1 GR$1.0 config.gas_prices! +10000 sg* 1000000 10000 10000000 GR$0.1 GR$1.0 config.mc_gas_prices! +// lump_price bit_price cell_price ihr_factor first_frac next_frac +1000000 1000 sg* 100000 sg* 3/2 sg*/ 1/3 sg*/ 1/3 sg*/ config.fwd_prices! +10000000 10000 sg* 1000000 sg* 3/2 sg*/ 1/3 sg*/ 1/3 sg*/ config.mc_fwd_prices! +// mc-cc-lifetime sh-cc-lifetime sh-val-lifetime sh-val-num +250 250 1000 7 config.catchain_params! +// round-candidates next-cand-delay-ms consensus-timeout-ms fast-attempts attempt-duration cc-max-deps max-block-size max-collated-size +3 2000 16000 3 8 4 2 *Mi 2 *Mi config.consensus_params! + + +128 *Ki 512 *Ki 1 *Mi triple // [ underload soft hard ] : block bytes limit +100000 500000 1000000 triple // gas limits +1000 5000 10000 triple // lt limits +triple dup untriple config.mc_block_limits! +untriple config.block_limits! + +GR$1.7 GR$1 config.block_create_fees! +// smc1_addr config.collector_smc! +smc1_addr config.minter_smc! + +1000000000000 -17 of-cc 666666666666 239 of-cc cc+ config.to_mint! + +"validator-keys" +suffix +".pub" file>B +{ dup Blen } { 32 B| swap dup ."Validator public key = " Bx. cr + 17 add-validator } while drop +// newkeypair nip dup ."Validator #1 public key = " Bx. cr +// 17 add-validator +// newkeypair nip dup ."Validator #2 public key = " Bx. cr +// 239 add-validator +// 100000 =: orig_vset_valid_for +100 =: orig_vset_valid_for // original validator set valid 100 seconds only (DEBUG) +now dup orig_vset_valid_for + 0 config.validators! + +/* + * + * SmartContract #5 (Configuration smart contract) + * + */ +"config-code.fif" include // code in separate source file + // data +empty_cell // libraries +GR$10 // balance +0 1 config_addr 6 register_smc // tock +dup set_config_smc +Masterchain swap +."config smart contract address = " 2dup .addr cr 2dup 7 .Addr cr +"config-master" +suffix +".addr" save-address-verbose +// Other data + +create_state +cr cr ."new state is:" cr dup B dup Bx. cr +dup "zerostate" +suffix +".boc" tuck B>file +."(Initial masterchain state saved to file " type .")" cr +Bhash dup =: zerostate_fhash +."file hash= " dup X. space 256 u>B dup B>base64url type cr +"zerostate" +suffix +".fhash" B>file +hash dup =: zerostate_rhash ."root hash= " dup X. space 256 u>B dup B>base64url type cr +"zerostate" +suffix +".rhash" B>file +basestate0_rhash ."Basestate0 root hash= " dup X. space 256 u>B B>base64url type cr +basestate0_fhash ."Basestate0 file hash= " dup X. space 256 u>B B>base64url type cr +zerostate_rhash ."Zerostate root hash= " dup X. space 256 u>B B>base64url type cr +zerostate_fhash ."Zerostate file hash= " dup X. space 256 u>B B>base64url type cr diff --git a/crypto/smartcont/new-pinger.fif b/crypto/smartcont/new-pinger.fif new file mode 100644 index 00000000..8d7b9155 --- /dev/null +++ b/crypto/smartcont/new-pinger.fif @@ -0,0 +1,53 @@ +#!/usr/bin/fift -s +"TonUtil.fif" include +"Asm.fif" include + +{ ."usage: " @' $0 type ." []" cr + ."Creates a new pinger in specified workchain, with destination address . " cr + ."Resulting initialization query is saved into -query.boc ('new-pinger-query.boc' by default)" cr 1 halt +} : usage +def? $# { @' $# dup 1 < swap 3 > or ' usage if } if + +Basechain 256 1<<1- 3 15 */ 2constant dest-addr + +Basechain constant wc // create a wallet in workchain 0 (basechain) +def? $1 { @' $1 parse-workchain-id =: wc } if // set workchain id from command line argument +def? $2 { @' $2 false parse-load-address drop 2=: dest-addr } if +def? $3 { @' $3 } { "new-pinger" } cond constant file-base + +."Creating new pinger in workchain " wc . cr + +// Create new simple pinger +<{ SETCP0 DUP INC 1 RSHIFT# 32 THROWIF // fail unless recv_internal or recv_external + c4 PUSHCTR CTOS 32 LDU TUCK 8 LDI 256 LDU ENDS // body sel s seqno wc addr : destination address loaded + s4 PUSH IF:<{ + s2 s5 XCHG2 // addr sel s wc seqno body + 32 PLDU OVER EQUAL 33 THROWIFNOT // seqno mismatch? + ACCEPT // addr sel s wc seqno + INC NEWC 32 STU // addr sel s wc b + s1 s2 XCHG STSLICE // addr sel wc b' + ENDC c4 POPCTR // addr sel wc ; persistent data updated + ROT // sel wc addr + }> + // create new empty message with 0.1 Grams to that address + SWAP NEWC b{001000100} STSLICECONST 8 STI 256 STU + GR$10/9 INT STGRAMS // store 1.111..1 Grams + 1 4 + 4 + 64 + 32 + 1+ 1+ INT STZEROES ENDC + // send raw message from Cell + ZERO SENDRAWMSG +}>c +// code + // data +// no libraries + // create StateInit +dup ."StateInit: " +dup ."External message for initialization is " B dup Bx. cr +file-base +"-query.boc" tuck B>file +."(Saved pinger creating query to file " type .")" cr diff --git a/crypto/smartcont/new-wallet.fif b/crypto/smartcont/new-wallet.fif new file mode 100644 index 00000000..7fd4aa18 --- /dev/null +++ b/crypto/smartcont/new-wallet.fif @@ -0,0 +1,58 @@ +#!/usr/bin/fift -s +"TonUtil.fif" include +"Asm.fif" include + +{ ."usage: " @' $0 type ." []" cr + ."Creates a new wallet in specified workchain, with private key saved to or loaded from .pk" cr + ."('new-wallet.pk' by default)" cr 1 halt +} : usage +def? $# { @' $# 1- -2 and ' usage if } if + +Basechain constant wc // create a wallet in workchain 0 (basechain) +def? $1 { @' $1 parse-workchain-id =: wc } if // set workchain id from command line argument +def? $2 { @' $2 } { "new-wallet" } cond constant file-base + +."Creating new wallet in workchain " wc . cr + +// Create new simple wallet +<{ SETCP0 DUP IFNOTRET INC 32 THROWIF // return if recv_internal, fail unless recv_external + 512 INT LDSLICEX DUP 32 PLDU // sign cs cnt + c4 PUSHCTR CTOS 32 LDU 256 LDU ENDS // sign cs cnt cnt' pubk + s1 s2 XCPU // sign cs cnt pubk cnt' cnt + EQUAL 33 THROWIFNOT // ( seqno mismatch? ) + s2 PUSH HASHSU // sign cs cnt pubk hash + s0 s4 s4 XC2PU // pubk cs cnt hash sign pubk + CHKSIGNU // pubk cs cnt ? + 34 THROWIFNOT // signature mismatch + ACCEPT + SWAP 32 LDU NIP + DUP SREFS IF:<{ + 8 LDU LDREF // pubk cnt mode msg cs + s0 s2 XCHG SENDRAWMSG // pubk cnt cs ; ( message sent ) + }> + ENDS + INC NEWC 32 STU 256 STU ENDC c4 POPCTR +}>c // >libref +// code + // data +null // no libraries +// Libs{ x{ABACABADABACABA} drop x{AAAA} s>c public_lib x{1234} x{5678} |_ s>c public_lib }Libs + // create StateInit +dup ."StateInit: " +dup ."signing message: " +dup ."External message for initialization is " B dup Bx. cr +file-base +"-query.boc" tuck B>file +."(Saved wallet creating query to file " type .")" cr diff --git a/crypto/smartcont/recover-stake.fif b/crypto/smartcont/recover-stake.fif new file mode 100644 index 00000000..871b3855 --- /dev/null +++ b/crypto/smartcont/recover-stake.fif @@ -0,0 +1,17 @@ +#!/usr/bin/fift -s +"TonUtil.fif" include + +{ ."usage: " @' $0 type ." []" cr + ."Creates the message body to be sent from a validator controlling smart contract (wallet) to recover its share of unfrozen stakes and bonuses." cr + ."The result is saved into (`recover-query.boc` by default) and output in hexadecimal form, to be sent later as the body of a message from the wallet to elections smart contract, along with a small value (say, one Gram) to cover forwarding and processing fees" cr 1 halt +} : usage + +$# dup 0 < swap 1 > or ' usage if +def? $1 { @' $1 } { "recover-query.boc" } cond constant output_fname +now constant query_id +."query_id for stake recovery message is set to " query_id . cr + + +cr ."Message body is " dup B output_fname tuck B>file ."Saved to file " type cr diff --git a/crypto/smartcont/show-addr.fif b/crypto/smartcont/show-addr.fif new file mode 100644 index 00000000..5d309464 --- /dev/null +++ b/crypto/smartcont/show-addr.fif @@ -0,0 +1,21 @@ +#!/usr/bin/fift -s +"TonUtil.fif" include + +{ ."usage: " @' $0 type ." " cr + ."Shows the address of a simple wallet created by new-wallet.fif, with address in .addr " + ."and private key in file .pk" cr 1 halt +} : usage +def? $# { @' $# 1 > ' usage if } if +def? $1 { @' $1 } { "new-wallet" } cond constant file-base + +file-base +".addr" dup ."Loading wallet address from " type cr file>B 32 B| +dup Blen { 32 B>i@ } { drop Basechain } cond constant wallet_wc +256 B>u@ dup constant wallet_addr +."Source wallet address = " wallet_wc ._ .":" x. cr +wallet_wc wallet_addr 2dup 7 smca>$ ."Non-bounceable address (for init only): " type cr +6 smca>$ ."Bounceable address (for later access): " type cr + +file-base +".pk" dup file-exists? { + dup file>B dup Blen 32 <> abort"Private key must be exactly 32 bytes long" + =: wallet_pk ."Private key available in file " type cr +} { ."Private key file " type ." not found" cr } cond diff --git a/crypto/smartcont/simple-wallet-code.fc b/crypto/smartcont/simple-wallet-code.fc new file mode 100644 index 00000000..266bc9ac --- /dev/null +++ b/crypto/smartcont/simple-wallet-code.fc @@ -0,0 +1,25 @@ +;; Simple wallet smart contract + +() recv_internal(slice in_msg) impure { + ;; do nothing for internal messages +} + +() recv_external(slice in_msg) impure { + var signature = in_msg~load_bits(512); + var cs = in_msg; + int msg_seqno = cs~load_uint(32); + var cs2 = begin_parse(get_data()); + var stored_seqno = cs2~load_uint(32); + var public_key = cs2~load_uint(256); + cs2.end_parse(); + throw_unless(33, msg_seqno == stored_seqno); + throw_unless(34, check_signature(slice_hash(in_msg), signature, public_key)); + accept_message(); + cs~touch_slice(); + if (cs.slice_refs()) { + var mode = cs~load_uint(8); + send_raw_message(cs~load_ref(), mode); + } + cs.end_parse(); + set_data(begin_cell().store_uint(stored_seqno + 1, 32).store_uint(public_key, 256).end_cell()); +} diff --git a/crypto/smartcont/stdlib.fc b/crypto/smartcont/stdlib.fc new file mode 100644 index 00000000..4fb30bdd --- /dev/null +++ b/crypto/smartcont/stdlib.fc @@ -0,0 +1,101 @@ +;; Standard library for funC +;; + +int now() asm "NOW"; +slice my_address() asm "MYADDR"; + +int cell_hash(cell c) asm "HASHCU"; +int slice_hash(slice s) asm "HASHSU"; +int string_hash(slice s) asm "SHA256U"; + +int check_signature(int hash, slice signature, int public_key) asm "CHKSIGNU"; +int check_data_signature(slice data, slice signature, int public_key) asm "CHKSIGNS"; + +;; () throw_if(int excno, int cond) impure asm "THROWARGIF"; + +() dump_stack() impure asm "DUMPSTK"; + +cell get_data() asm "c4 PUSH"; +() set_data(cell c) impure asm "c4 POP"; +() set_c3(cell c) impure asm "c3 POP"; +() accept_message() impure asm "ACCEPT"; + +int min(int x, int y) asm "MIN"; +int max(int x, int y) asm "MAX"; + +slice begin_parse(cell c) asm "CTOS"; +() end_parse(slice s) impure asm "ENDS"; +(slice, cell) load_ref(slice s) asm( -> 1 0) "LDREF"; +cell preload_ref(slice s) asm "PLDREF"; +;; (slice, int) ~load_int(slice s, int len) asm(s len -> 1 0) "LDIX"; +;; (slice, int) ~load_uint(slice s, int len) asm( -> 1 0) "LDUX"; +;; int preload_int(slice s, int len) asm "PLDIX"; +;; int preload_uint(slice s, int len) asm "PLDUX"; +;; (slice, slice) load_bits(slice s, int len) asm(s len -> 1 0) "LDSLICEX"; +;; slice preload_bits(slice s, int len) asm "PLDSLICEX"; +(slice, int) load_grams(slice s) asm( -> 1 0) "LDGRAMS"; +slice skip_bits(slice s, int len) asm "SDSKIPFIRST"; +slice first_bits(slice s, int len) asm "SDCUTFIRST"; +(slice, cell) load_dict(slice s) asm( -> 1 0) "LDDICT"; +cell preload_dict(slice s) asm "PLDDICT"; +slice skip_dict(slice s) asm "SKIPDICT"; + +int slice_refs(slice s) asm "SREFS"; +int slice_bits(slice s) asm "SBITS"; +(int, int) slice_bits_refs(slice s) asm "SBITREFS"; +int slice_empty?(slice s) asm "SEMPTY"; +int slice_data_empty?(slice s) asm "SDEMPTY"; +int slice_refs_empty?(slice s) asm "SREMPTY"; + +builder begin_cell() asm "NEWC"; +cell end_cell(builder b) asm "ENDC"; +builder store_ref(builder b, cell c) asm(c b) "STREF"; +;; builder store_uint(builder b, int x, int len) asm(x b len) "STUX"; +;; builder store_int(builder b, int x, int len) asm(x b len) "STIX"; +builder store_slice(builder b, slice s) asm "STSLICER"; +builder store_grams(builder b, int x) asm "STGRAMS"; +builder store_dict(builder b, cell c) asm(c b) "STDICT"; + +(slice, slice) load_msg_addr(slice s) asm( -> 1 0) "LDMSGADDR"; +tuple parse_addr(slice s) asm "PARSEMSGADDR"; +(int, int) parse_std_addr(slice s) asm "REWRITESTDADDR"; +(int, slice) parse_var_addr(slice s) asm "REWRITEVARADDR"; + +cell idict_set_ref(cell dict, int key_len, int index, cell value) asm(value index dict key_len) "DICTISETREF"; +(cell, ()) ~idict_set_ref(cell dict, int key_len, int index, cell value) asm(value index dict key_len) "DICTISETREF"; +cell idict_get_ref(cell dict, int key_len, int index) asm(index dict key_len) "DICTIGETOPTREF"; +(cell, cell) idict_set_get_ref(cell dict, int key_len, int index, cell value) asm(value index dict key_len) "DICTISETGETOPTREF"; +(cell, int) idict_delete?(cell dict, int key_len, int index) asm(index dict key_len) "DICTIDEL"; +(slice, int) idict_get?(cell dict, int key_len, int index) asm(index dict key_len) "DICTIGET" "NULLSWAPIFNOT"; +(slice, int) udict_get?(cell dict, int key_len, int index) asm(index dict key_len) "DICTUGET" "NULLSWAPIFNOT"; +(cell, slice, int) idict_delete_get?(cell dict, int key_len, int index) asm(index dict key_len) "DICTIDELGET" "NULLSWAPIFNOT"; +(cell, slice, int) udict_delete_get?(cell dict, int key_len, int index) asm(index dict key_len) "DICTUDELGET" "NULLSWAPIFNOT"; +(cell, (slice, int)) ~idict_delete_get?(cell dict, int key_len, int index) asm(index dict key_len) "DICTIDELGET" "NULLSWAPIFNOT"; +(cell, (slice, int)) ~udict_delete_get?(cell dict, int key_len, int index) asm(index dict key_len) "DICTUDELGET" "NULLSWAPIFNOT"; +cell udict_set_builder(cell dict, int key_len, int index, builder value) asm(value index dict key_len) "DICTUSETB"; +(cell, ()) ~udict_set_builder(cell dict, int key_len, int index, builder value) asm(value index dict key_len) "DICTUSETB"; +cell idict_set_builder(cell dict, int key_len, int index, builder value) asm(value index dict key_len) "DICTISETB"; +(cell, ()) ~idict_set_builder(cell dict, int key_len, int index, builder value) asm(value index dict key_len) "DICTISETB"; +cell dict_set_builder(cell dict, int key_len, slice index, builder value) asm(value index dict key_len) "DICTSETB"; +(cell, ()) ~dict_set_builder(cell dict, int key_len, slice index, builder value) asm(value index dict key_len) "DICTSETB"; +(cell, int) udict_add_builder?(cell dict, int key_len, int index, builder value) asm(value index dict key_len) "DICTUADDB"; +(cell, int) udict_replace_builder?(cell dict, int key_len, int index, builder value) asm(value index dict key_len) "DICTUREPLACEB"; +(cell, int, slice, int) udict_delete_get_min(cell dict, int key_len) asm(-> 0 2 1 3) "DICTUREMMIN" "NULLSWAPIFNOT" "NULLSWAPIFNOT"; +(cell, (int, slice, int)) ~udict::delete_get_min(cell dict, int key_len) asm(-> 0 2 1 3) "DICTUREMMIN" "NULLSWAPIFNOT" "NULLSWAPIFNOT"; +(cell, slice, slice, int) dict_delete_get_min(cell dict, int key_len) asm(-> 0 2 1 3) "DICTREMMIN" "NULLSWAPIFNOT" "NULLSWAPIFNOT"; +(cell, (slice, slice, int)) ~dict::delete_get_min(cell dict, int key_len) asm(-> 0 2 1 3) "DICTREMMIN" "NULLSWAPIFNOT" "NULLSWAPIFNOT"; +(int, slice, int) udict_get_next?(cell dict, int key_len, int pivot) asm(pivot dict key_len -> 1 0 2) "DICTUGETNEXT" "NULLSWAPIFNOT" "NULLSWAPIFNOT"; +(int, slice, int) udict_get_prev?(cell dict, int key_len, int pivot) asm(pivot dict key_len -> 1 0 2) "DICTUGETPREV" "NULLSWAPIFNOT" "NULLSWAPIFNOT"; +cell new_dict() asm "NEWDICT"; +int dict_empty?(cell c) asm "DICTEMPTY"; + +cell config_param(int x) asm "CONFIGOPTPARAM"; +int cell_null?(cell c) asm "ISNULL"; + +() raw_reserve(int amount, int mode) impure asm "RAWRESERVE"; +() raw_reserve_extra(slice currencies, int mode) impure asm "RAWRESERVEX"; +() send_raw_message(cell msg, int mode) impure asm "SENDRAWMSG"; +() set_code(cell new_code) impure asm "SETCODE"; + +slice touch_slice(slice s) asm "NOP"; +(slice,()) ~touch_slice(slice s) asm "NOP"; diff --git a/crypto/smartcont/testgiver.fif b/crypto/smartcont/testgiver.fif new file mode 100644 index 00000000..3d717d05 --- /dev/null +++ b/crypto/smartcont/testgiver.fif @@ -0,0 +1,43 @@ +#!/usr/bin/fift -s +"TonUtil.fif" include + +{ ."usage: " @' $0 type ." []" cr + ."Creates a request to TestGiver and saves it into .boc" cr + ."('testgiver-query.boc' by default)" cr 1 halt +} : usage +def? $# { @' $# 3 - -2 and ' usage if } if + +// "testgiver.addr" load-address +Masterchain 0x8156775b79325e5d62e742d9b96c30b6515a5cd2f1f64c5da4b193c03f070e0d +2constant giver_addr + ."Test giver address = " giver_addr 2dup .addr cr 6 .Addr cr + +Basechain 0x60c04141c6a7b96d68615e7a91d265ad0f3a9a922e9ae9c901d4fa83f5d3c0d0 +2constant dest_addr +false constant bounce + +0 constant seqno +GR$6.666 constant amount + +def? $3 { + @' $1 bounce parse-load-address =: bounce 2=: dest_addr + @' $2 parse-int =: seqno + @' $3 $>GR =: amount +} if +def? $4 { @' $4 } { "testgiver-query" } cond constant savefile + +."Requesting " amount .GR ."to account " +dest_addr 2dup bounce 7 + .Addr ." = " .addr +."seqno=0x" seqno x. ."bounce=" bounce . cr + +// create a message (NB: 01b00.., b = bounce) + + +dup ."enveloping message: " +dup ."resulting external message: " B dup Bx. cr +savefile +".boc" tuck B>file +."(Saved to file " type .")" cr diff --git a/crypto/smartcont/update-config-smc.fif b/crypto/smartcont/update-config-smc.fif new file mode 100644 index 00000000..3dd51c58 --- /dev/null +++ b/crypto/smartcont/update-config-smc.fif @@ -0,0 +1,43 @@ +#!/usr/bin/fift -s +"TonUtil.fif" include + +{ ."usage: " @' $0 type ." []" cr + ."Creates a request to simple configuration smart contract requesting to change configuration smart contract code to the one currently stored in config-code.fif, " + ."with private key loaded from file .pk, " + ."and saves it into .boc ('config-query.boc' by default)" cr 1 halt +} : usage +def? $# { @' $# dup 2 < swap 3 > or ' usage if } if + +"config-master" constant file-base +0 constant seqno +-1 constant idx +true constant bounce +"config-code.fif" constant config-source +100 constant interval // valid for 100 seconds + +def? $2 { + @' $1 =: file-base + @' $2 parse-int =: seqno +} if +def? $5 { @' $5 } { "config-query" } cond constant savefile + +file-base +".addr" load-address +2dup 2constant config_addr +."Configuration smart contract address = " 2dup .addr cr 6 .Addr cr +file-base +".pk" load-keypair nip constant config_pk + +."Loading new configuration smart contract code from file file " config-source type cr +"Asm.fif" include +config-source include +dup +dup ."signing message: " +dup ."resulting external message: " B dup Bx. cr +savefile +".boc" tuck B>file +."(Saved to file " type .")" cr diff --git a/crypto/smartcont/update-config.fif b/crypto/smartcont/update-config.fif new file mode 100644 index 00000000..662dec81 --- /dev/null +++ b/crypto/smartcont/update-config.fif @@ -0,0 +1,48 @@ +#!/usr/bin/fift -s +"TonUtil.fif" include + +{ ."usage: " @' $0 type ." []" cr + ."Creates a request to simple configuration smart contract created by mc0.fif requesting to change configuration parameter to , " + ."with private key loaded from file .pk, " + ."and saves it into .boc ('config-query.boc' by default)" cr 1 halt +} : usage +def? $# { @' $# dup 4 < swap 5 > or ' usage if } if + +"config-master" constant file-base +0 constant seqno +-1 constant idx +true constant bounce +"new-value.boc" constant boc-filename +100 constant interval // valid for 100 seconds + +def? $4 { + @' $1 =: file-base + @' $2 parse-int =: seqno + @' $3 parse-int =: idx + @' $4 =: boc-filename +} if +def? $5 { @' $5 } { "config-query" } cond constant savefile + +file-base +".addr" load-address +2dup 2constant config_addr +."Configuration smart contract address = " 2dup .addr cr 6 .Addr cr +file-base +".pk" load-keypair nip constant config_pk + +."Loading new value of configuration parameter " idx . ."from file " boc-filename type cr +boc-filename file>B B>boc +dup +dup ."signing message: " +dup ."resulting external message: " B dup Bx. cr +savefile +".boc" tuck B>file +."(Saved to file " type .")" cr diff --git a/crypto/smartcont/update-elector-smc.fif b/crypto/smartcont/update-elector-smc.fif new file mode 100644 index 00000000..e485729a --- /dev/null +++ b/crypto/smartcont/update-elector-smc.fif @@ -0,0 +1,43 @@ +#!/usr/bin/fift -s +"TonUtil.fif" include + +{ ."usage: " @' $0 type ." []" cr + ."Creates a request to simple configuration smart contract requesting to change elector smart contract code to the one currently stored in elector-code.fif, " + ."with private key loaded from file .pk, " + ."and saves it into .boc ('config-query.boc' by default)" cr 1 halt +} : usage +def? $# { @' $# dup 2 < swap 3 > or ' usage if } if + +"config-master" constant file-base +0 constant seqno +-1 constant idx +true constant bounce +"elector-code.fif" constant elector-source +100 constant interval // valid for 100 seconds + +def? $2 { + @' $1 =: file-base + @' $2 parse-int =: seqno +} if +def? $5 { @' $5 } { "config-query" } cond constant savefile + +file-base +".addr" load-address +2dup 2constant config_addr +."Configuration smart contract address = " 2dup .addr cr 6 .Addr cr +file-base +".pk" load-keypair nip constant config_pk + +."Loading new elector smart contract code from file file " elector-source type cr +"Asm.fif" include +elector-source include +dup +dup ."signing message: " +dup ."resulting external message: " B dup Bx. cr +savefile +".boc" tuck B>file +."(Saved to file " type .")" cr diff --git a/crypto/smartcont/validator-elect-req.fif b/crypto/smartcont/validator-elect-req.fif new file mode 100644 index 00000000..215fe9c1 --- /dev/null +++ b/crypto/smartcont/validator-elect-req.fif @@ -0,0 +1,30 @@ +#!/usr/bin/fift -s +"TonUtil.fif" include + +{ ."usage: " @' $0 type ." []" cr + ."Creates a request to participate in validator elections starting at on behalf of smart-contract with address (prefix with '@' to load address from file) and hexadecimal adnl address (empty string or '0' for none)." cr + ."The result is saved into and output in hexadecimal form, to be signed later by the validator public key" cr 1 halt +} : usage + +$# dup 3 < swap 5 > or ' usage if +$1 true parse-load-address drop swap 1+ abort"only masterchain smartcontracts may participate in validator elections" +constant src_addr +$2 (number) 1 <> { 0 } if dup 0<= abort" must be a positive integer" +constant elect_time +$3 (number) dup 0= abort" must be a real number 1..100" +1 = { 16 << } { 16 < or abort" must be a real number 1..100" +constant max_factor +def? $4 { @' $4 dup $len 1 > { parse-adnl-address } { drop 0 } cond } ' 0 cond +constant adnl_addr +def? $5 { @' $5 } { "validator-to-sign.bin" } cond constant output_fname + +."Creating a request to participate in validator elections at time " elect_time . +."from smart contract " -1 src_addr 2dup 1 .Addr ." = " .addr +." with maximal stake factor with respect to the minimal stake " max_factor ._ +."/65536 and validator ADNL address " adnl_addr x. cr + +B{654c5074} elect_time 32 u>B B+ max_factor 32 u>B B+ src_addr 256 u>B B+ adnl_addr 256 u>B B+ +dup Bx. cr +dup B>base64url type cr +output_fname tuck B>file ."Saved to file " type cr diff --git a/crypto/smartcont/validator-elect-signed.fif b/crypto/smartcont/validator-elect-signed.fif new file mode 100644 index 00000000..55e3d9dd --- /dev/null +++ b/crypto/smartcont/validator-elect-signed.fif @@ -0,0 +1,43 @@ +#!/usr/bin/fift -s +"TonUtil.fif" include + +{ ."usage: " @' $0 type ." []" cr + ."Creates a message body for participating in validator elections starting at on behalf of smart-contract with address (prefix with '@' to load address from file) and hexadecimal adnl address (empty string or '0' for none)." cr + ." is the main public key of the future validator (as a Base64 string), and is the signature of the previously created validator request by that key (also Base64)" cr + ."The result is saved into (`validator-query.boc` by default) and output in hexadecimal form, to be sent later as the body of a message from to elections smart contract, along with the desired stake" cr 1 halt +} : usage + +$# dup 6 < swap 7 > or ' usage if +$1 true parse-load-address drop swap 1+ abort"only masterchain smartcontracts may participate in validator elections" +constant src_addr +$2 (number) 1 <> { 0 } if dup 0<= abort" must be a positive integer" +constant elect_time +$3 (number) dup 0= abort" must be a real number 1..100" +1 = { 16 << } { 16 < or abort" must be a real number 1..100" +constant max_factor +$4 dup $len 1 > { parse-adnl-address } { drop 0 } cond constant adnl_addr +$5 base64>B dup Blen 36 <> abort"validator Ed25519 public key must be exactly 36 bytes long" + 32 B>u@+ 0xC6B41348 <> abort"invalid Ed25519 public key: unknown magic number" + constant pubkey +$6 base64>B dup Blen 64 <> abort"validator Ed25519 signature must be exactly 64 bytes long" +constant signature +def? $7 { @' $7 } { "validator-query.boc" } cond constant output_fname + +."Creating a request to participate in validator elections at time " elect_time . +."from smart contract " -1 src_addr 2dup 1 .Addr ." = " .addr +." with maximal stake factor with respect to the minimal stake " max_factor ._ +."/65536 and validator ADNL address " adnl_addr x. cr + +B{654c5074} elect_time 32 u>B B+ max_factor 32 u>B B+ src_addr 256 u>B B+ adnl_addr 256 u>B B+ +."String to sign is: " dup Bx. cr constant to_sign + +to_sign signature pubkey ed25519_chksign not abort"Ed25519 signature is invalid" +."Provided a valid Ed25519 signature " signature Bx. ." with validator public key " pubkey Bx. cr +now dup constant query_id ."query_id set to " . cr + + ref, b> +cr ."Message body is " dup B output_fname tuck B>file ."Saved to file " type cr diff --git a/crypto/smartcont/wallet-code.fc b/crypto/smartcont/wallet-code.fc new file mode 100644 index 00000000..e559b92b --- /dev/null +++ b/crypto/smartcont/wallet-code.fc @@ -0,0 +1,27 @@ +;; Simple wallet smart contract + +() recv_internal(slice in_msg) impure { + ;; do nothing for internal messages +} + +() recv_external(slice in_msg) impure { + var signature = in_msg~load_bits(512); + var cs = in_msg; + int msg_seqno = cs~load_uint(32); + var valid_until = cs~load_uint(32); + throw_if(35, valid_until < now()); + var cs2 = begin_parse(get_data()); + var stored_seqno = cs2~load_uint(32); + var public_key = cs2~load_uint(256); + cs2.end_parse(); + throw_unless(33, msg_seqno == stored_seqno); + throw_unless(34, check_signature(slice_hash(in_msg), signature, public_key)); + accept_message(); + cs~touch(); + while (cs.slice_refs()) { + var mode = cs~load_uint(8); + send_raw_message(cs~load_ref(), mode); + } + cs.end_parse(); + set_data(begin_cell().store_uint(stored_seqno + 1, 32).store_uint(public_key, 256).end_cell()); +} diff --git a/crypto/smartcont/wallet.fif b/crypto/smartcont/wallet.fif new file mode 100644 index 00000000..5938eb2e --- /dev/null +++ b/crypto/smartcont/wallet.fif @@ -0,0 +1,51 @@ +#!/usr/bin/fift -s +"TonUtil.fif" include + +{ ."usage: " @' $0 type ." [-B ] []" cr + ."Creates a request to simple wallet created by new-wallet.fif, with private key loaded from file .pk " + ."and address from .addr, and saves it into .boc ('wallet-query.boc' by default)" cr 1 halt +} : usage +def? $6 { @' $5 "-B" $= { @' $6 =: body-boc-file [forget] $6 def? $7 { @' $7 =: $5 [forget] $7 } { [forget] $5 } cond + @' $# 2- =: $# } if } if +def? $# { @' $# dup 4 < swap 5 > or ' usage if } if + +"new-wallet" constant file-base +Basechain 0x13CB612A00A7C092C7DFD2EA45D603A9B54591BA4C88F71E707E009B879F0FB2 2constant dest_addr +0 constant seqno +GR$.666 constant amount +true constant bounce + +def? $4 { + @' $1 =: file-base + @' $2 bounce parse-load-address =: bounce 2=: dest_addr + @' $3 parse-int =: seqno + @' $4 $>GR =: amount +} if +def? $5 { @' $5 } { "wallet-query" } cond constant savefile + +file-base +".addr" load-address +2dup 2constant wallet_addr +."Source wallet address = " 2dup .addr cr 6 .Addr cr +file-base +".pk" load-keypair nip constant wallet_pk + +def? body-boc-file { @' body-boc-file file>B B>boc } { } cond +constant body-cell + +."Transferring " amount .GR ."to account " +dest_addr 2dup bounce 7 + .Addr ." = " .addr +."seqno=0x" seqno x. ."bounce=" bounce . cr +."Body of transfer message is " body-cell + +dup ."signing message: " +dup ."resulting external message: " B dup Bx. cr +savefile +".boc" tuck B>file +."(Saved to file " type .")" cr diff --git a/crypto/test/Ed25519.cpp b/crypto/test/Ed25519.cpp new file mode 100644 index 00000000..4d943abb --- /dev/null +++ b/crypto/test/Ed25519.cpp @@ -0,0 +1,144 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "crypto/Ed25519.h" +#include "td/utils/logging.h" +#include "td/utils/misc.h" +#include "td/utils/Slice.h" +#include "td/utils/tests.h" + +#include + +unsigned char fixed_privkey[32] = "abacabadabacabaeabacabadabacaba"; +unsigned char fixed_pubkey[32] = {0x6f, 0x9e, 0x5b, 0xde, 0xce, 0x87, 0x21, 0xeb, 0x57, 0x37, 0xfb, + 0xb5, 0x92, 0x28, 0xba, 0x07, 0xf7, 0x88, 0x0f, 0x73, 0xce, 0x5b, + 0xfa, 0xa1, 0xb7, 0x15, 0x73, 0x03, 0xd4, 0x20, 0x1e, 0x74}; + +unsigned char rfc8032_secret_key1[32] = {0x9d, 0x61, 0xb1, 0x9d, 0xef, 0xfd, 0x5a, 0x60, 0xba, 0x84, 0x4a, + 0xf4, 0x92, 0xec, 0x2c, 0xc4, 0x44, 0x49, 0xc5, 0x69, 0x7b, 0x32, + 0x69, 0x19, 0x70, 0x3b, 0xac, 0x03, 0x1c, 0xae, 0x7f, 0x60}; + +unsigned char rfc8032_public_key1[32] = {0xd7, 0x5a, 0x98, 0x01, 0x82, 0xb1, 0x0a, 0xb7, 0xd5, 0x4b, 0xfe, + 0xd3, 0xc9, 0x64, 0x07, 0x3a, 0x0e, 0xe1, 0x72, 0xf3, 0xda, 0xa6, + 0x23, 0x25, 0xaf, 0x02, 0x1a, 0x68, 0xf7, 0x07, 0x51, 0x1a}; + +unsigned char rfc8032_signature1[64] = { + 0xe5, 0x56, 0x43, 0x00, 0xc3, 0x60, 0xac, 0x72, 0x90, 0x86, 0xe2, 0xcc, 0x80, 0x6e, 0x82, 0x8a, + 0x84, 0x87, 0x7f, 0x1e, 0xb8, 0xe5, 0xd9, 0x74, 0xd8, 0x73, 0xe0, 0x65, 0x22, 0x49, 0x01, 0x55, + 0x5f, 0xb8, 0x82, 0x15, 0x90, 0xa3, 0x3b, 0xac, 0xc6, 0x1e, 0x39, 0x70, 0x1c, 0xf9, 0xb4, 0x6b, + 0xd2, 0x5b, 0xf5, 0xf0, 0x59, 0x5b, 0xbe, 0x24, 0x65, 0x51, 0x41, 0x43, 0x8e, 0x7a, 0x10, 0x0b, +}; + +unsigned char rfc8032_secret_key2[32] = { + 0xc5, 0xaa, 0x8d, 0xf4, 0x3f, 0x9f, 0x83, 0x7b, 0xed, 0xb7, 0x44, 0x2f, 0x31, 0xdc, 0xb7, 0xb1, + 0x66, 0xd3, 0x85, 0x35, 0x07, 0x6f, 0x09, 0x4b, 0x85, 0xce, 0x3a, 0x2e, 0x0b, 0x44, 0x58, 0xf7, +}; + +unsigned char rfc8032_public_key2[32] = { + 0xfc, 0x51, 0xcd, 0x8e, 0x62, 0x18, 0xa1, 0xa3, 0x8d, 0xa4, 0x7e, 0xd0, 0x02, 0x30, 0xf0, 0x58, + 0x08, 0x16, 0xed, 0x13, 0xba, 0x33, 0x03, 0xac, 0x5d, 0xeb, 0x91, 0x15, 0x48, 0x90, 0x80, 0x25, +}; + +unsigned char rfc8032_message2[2] = {0xaf, 0x82}; + +unsigned char rfc8032_signature2[64] = { + 0x62, 0x91, 0xd6, 0x57, 0xde, 0xec, 0x24, 0x02, 0x48, 0x27, 0xe6, 0x9c, 0x3a, 0xbe, 0x01, 0xa3, + 0x0c, 0xe5, 0x48, 0xa2, 0x84, 0x74, 0x3a, 0x44, 0x5e, 0x36, 0x80, 0xd7, 0xdb, 0x5a, 0xc3, 0xac, + 0x18, 0xff, 0x9b, 0x53, 0x8d, 0x16, 0xf2, 0x90, 0xae, 0x67, 0xf7, 0x60, 0x98, 0x4d, 0xc6, 0x59, + 0x4a, 0x7c, 0x15, 0xe9, 0x71, 0x6e, 0xd2, 0x8d, 0xc0, 0x27, 0xbe, 0xce, 0xea, 0x1e, 0xc4, 0x0a, +}; + +TEST(Crypto, ed25519) { + td::Ed25519::generate_private_key().ensure(); + + auto PK1 = td::Ed25519::generate_private_key().move_as_ok(); + auto PK2 = td::Ed25519::PrivateKey(td::SecureString(td::Slice(fixed_privkey, 32))); + LOG(ERROR) << "PK1 = " << td::buffer_to_hex(PK1.as_octet_string()); + auto priv2_export = PK2.as_octet_string(); + LOG(ERROR) << "PK2 = " << td::buffer_to_hex(priv2_export); + auto PK3 = td::Ed25519::PrivateKey(std::move(priv2_export)); + + auto PubK1 = PK1.get_public_key().move_as_ok(); + LOG(ERROR) << "PubK1 = " << td::buffer_to_hex(PubK1.as_octet_string()); + auto PubK2 = PK2.get_public_key().move_as_ok(); + LOG(ERROR) << "PubK2 = " << td::buffer_to_hex(PubK2.as_octet_string()); + CHECK(td::Slice(fixed_pubkey, 32) == PubK2.as_octet_string()); + auto PubK3 = PK3.get_public_key().move_as_ok(); + LOG(ERROR) << "PubK3 = " << td::buffer_to_hex(PubK3.as_octet_string()); + CHECK(td::Slice(fixed_pubkey, 32) == PubK3.as_octet_string()); + LOG(ERROR) << "PubK1 = " << td::buffer_to_hex(PubK1.as_octet_string()); + + auto secret22 = td::Ed25519::compute_shared_secret(PubK3, PK2).move_as_ok(); + LOG(ERROR) << "secret(PK2, PubK2)=" << td::buffer_to_hex(secret22); + + auto secret12 = td::Ed25519::compute_shared_secret(PubK3, PK1).move_as_ok(); + LOG(ERROR) << "secret(PK1, PubK2)=" << td::buffer_to_hex(secret12); + auto secret21 = td::Ed25519::compute_shared_secret(PubK1, PK2).move_as_ok(); + LOG(ERROR) << "secret(PK2, PubK1)=" << td::buffer_to_hex(secret21); + CHECK(secret12 == secret21); + + // for (int i = 0; i < 1000; i++) { + // td::Ed25519::compute_shared_secret(PubK2, PK1).ensure(); + // td::Ed25519::compute_shared_secret(PubK1, PK2).ensure(); + // } + + auto signature = PK1.sign("abc").move_as_ok(); + LOG(ERROR) << "PK1.signature=" << td::buffer_to_hex(signature); + + // signature[63] ^= 1; + auto ok = PubK1.verify_signature("abc", signature); + LOG(ERROR) << "PubK1.check_signature=" << ok; + ok.ensure(); + + td::Ed25519::PrivateKey PK4(td::SecureString(td::Slice(rfc8032_secret_key1, 32))); + auto PubK4 = PK4.get_public_key().move_as_ok(); + LOG(ERROR) << "PK4.private_key = " << td::buffer_to_hex(PK4.as_octet_string()); + LOG(ERROR) << "PK4.public_key = " << td::buffer_to_hex(PubK4.as_octet_string()); + CHECK(td::Slice(rfc8032_public_key1, 32) == PubK4.as_octet_string()); + signature = PK4.sign("").move_as_ok(); + LOG(ERROR) << "PK4.signature('') = " << td::buffer_to_hex(signature); + CHECK(signature == td::Slice(rfc8032_signature1, 64)); + + td::Ed25519::PrivateKey PK5(td::SecureString(td::Slice(rfc8032_secret_key2, 32))); + auto PubK5 = PK5.get_public_key().move_as_ok(); + LOG(ERROR) << "PK5.private_key = " << td::buffer_to_hex(PK5.as_octet_string()); + LOG(ERROR) << "PK5.public_key = " << td::buffer_to_hex(PubK5.as_octet_string()); + CHECK(td::Slice(rfc8032_public_key2, 32) == PubK5.as_octet_string()); + signature = PK5.sign(td::Slice(rfc8032_message2, 2)).move_as_ok(); + LOG(ERROR) << "PK5.signature('') = " << td::buffer_to_hex(signature); + CHECK(signature == td::Slice(rfc8032_signature2, 64)); + + // for (int i = 0; i < 100000; i++) { + // PK5.sign(td::Slice(rfc8032_message2, 2)); + // } + // for (int i = 0; i < 1000; i++) { + // PubK5.verify_signature(td::Slice(rfc8032_message2, 2), signature).ensure(); + // } + + /* + unsigned char temp_pubkey[32]; + crypto::Ed25519::TempKeyGenerator TKG; // use one generator a lot of times + + TKG.create_temp_shared_secret(temp_pubkey, secret12, PubK1, (const unsigned char*)"abc", 3); + LOG(ERROR) << "secret12=" << td::buffer_to_hex(secret12) << "; temp_pubkey=" << td::buffer_to_hex(temp_pubkey); + + PK1.compute_temp_shared_secret(secret21, temp_pubkey); + LOG(ERROR) << "secret21=" << td::buffer_to_hex(secret21); + assert(!std::memcmp(secret12, secret21, 32)); +*/ +} diff --git a/crypto/test/fift.cpp b/crypto/test/fift.cpp new file mode 100644 index 00000000..59b912c3 --- /dev/null +++ b/crypto/test/fift.cpp @@ -0,0 +1,92 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "fift/words.h" +#include "fift/Fift.h" +#include "fift/utils.h" + +#include "td/utils/tests.h" +#include "td/utils/PathView.h" +#include "td/utils/port/path.h" +#include "td/utils/filesystem.h" + +std::string current_dir() { + return td::PathView(td::realpath(__FILE__).move_as_ok()).parent_dir().str(); +} + +std::string load_test(std::string name) { + return td::read_file_str(current_dir() + "fift/" + name).move_as_ok(); +} + +td::Status run_fift(std::string name, bool preload_fift = true) { + TRY_RESULT(res, fift::mem_run_fift(load_test(name))); + REGRESSION_VERIFY(res.output); + return td::Status::OK(); +} + +TEST(Fift, testvm) { + run_fift("testvm.fif"); +} +TEST(Fift, testvm2) { + run_fift("testvm2.fif"); +} +TEST(Fift, testvm3) { + run_fift("testvm3.fif"); +} +TEST(Fift, testvm4) { + run_fift("testvm2.fif"); +} +TEST(Fift, testvm4a) { + run_fift("testvm4a.fif"); +} +TEST(Fift, testvm4b) { + run_fift("testvm4b.fif"); +} +TEST(Fift, testvm4c) { + run_fift("testvm4c.fif"); +} +TEST(Fift, testvm4d) { + run_fift("testvm4d.fif"); +} +TEST(Fift, testvm5) { + run_fift("testvm5.fif"); +} +TEST(Fift, testvm6) { + run_fift("testvm6.fif"); +} +TEST(Fift, testvm7) { + run_fift("testvm7.fif"); +} +TEST(Fift, testvm8) { + run_fift("testvm8.fif"); +} +TEST(Fift, testvmprog) { + run_fift("testvmprog.fif"); +} +TEST(Fift, bug) { + run_fift("bug.fif"); +} +TEST(Fift, contfrac) { + run_fift("contfrac.fif"); +} +TEST(Fift, test) { + run_fift("test.fif"); +} +TEST(Fift, bug_div) { + run_fift("bug_div.fif"); +} diff --git a/crypto/test/fift/bug.fif b/crypto/test/fift/bug.fif new file mode 100644 index 00000000..24440fed --- /dev/null +++ b/crypto/test/fift/bug.fif @@ -0,0 +1,3 @@ +10000000000000000000000000000000000000000000000000000000 dup . +dup 1 */ . + diff --git a/crypto/test/fift/bug_div.fif b/crypto/test/fift/bug_div.fif new file mode 100644 index 00000000..27ad386a --- /dev/null +++ b/crypto/test/fift/bug_div.fif @@ -0,0 +1,12 @@ +"Asm.fif" include + +1000 1 /r + +x{7A71A905} runvmcode + +<{ 1000 INT 1 INT DIVR }>s +dup csr. +runvmcode + +.s + diff --git a/crypto/test/fift/cmdline.fif b/crypto/test/fift/cmdline.fif new file mode 100755 index 00000000..1194a428 --- /dev/null +++ b/crypto/test/fift/cmdline.fif @@ -0,0 +1,8 @@ +#!/usr/bin/fift -s +{ ."usage: " $0 type ." " cr + ."Computes the product of two integers." cr bye } : usage +{ ' usage if } : ?usage +$# 2 <> ?usage +$1 (number) 1- ?usage +$2 (number) 1- ?usage +* . cr diff --git a/crypto/test/fift/contfrac.fif b/crypto/test/fift/contfrac.fif new file mode 100644 index 00000000..585f9e18 --- /dev/null +++ b/crypto/test/fift/contfrac.fif @@ -0,0 +1,18 @@ +{ dup 2over rot } : 3dup +{ 2drop drop } : 3drop +{ { 5 roll } 3 times } : 3swap +{ -rot over + rot over + -rot over + rot } : step1 +{ negate -rot negate swap negate } : step2 +{ 0 3 -roll { 3dup step1 dup 0> } { 3swap 3drop 3 roll 1+ 3 -roll } while 3drop step2 } : nsteps1 +{ ' nsteps1 swap times 3drop } : qcontfrac +{ -1 0 2swap qcontfrac } : sqrtcontfrac +{ rot 2 pick * + swap } : revstep1 +{ 1 0 rot ' revstep1 swap times } : computecontfrac +1 { 10 * } 74 times constant One +{ 100 sqrtcontfrac 100 computecontfrac */r } : *sqrtint +{ One swap *sqrtint } : sqrtint +2 sqrtint . +3 sqrtint . +6 sqrtint . +7 sqrtint . +239 sqrtint . diff --git a/crypto/test/fift/fixed.fif b/crypto/test/fift/fixed.fif new file mode 100644 index 00000000..779920cc --- /dev/null +++ b/crypto/test/fift/fixed.fif @@ -0,0 +1,19 @@ +1 { 10 * } 70 times constant One +{ bl word (number) dup 0= abort"invalid fixed-point constant" + 1- { One swap */r } { One * } cond 1 'nop } ::_ F$ +' + : F+ +' - : F- +{ One */r } : F* +{ One swap */r } : F/ +{ One dup rot */r } : Finv +{ One { 2dup F/ F+ 2/ } 9 times nip } : Fsqrt +{ ' Fsqrt 128 times One F- 128 << } : Fln +{ dup abs <# ' # 70 times char . hold #s rot sign #> + nip -trailing0 type space } : .F +F$17/12 .F +F$3.14159265 .F +{ 1 0 rot { -rot over + swap rot 2dup >= } until drop +} : fib-gtr +One fib-gtr F/ .F +F$2 Fsqrt .F +F$2 Fln .F diff --git a/crypto/test/fift/sort.fif b/crypto/test/fift/sort.fif new file mode 100644 index 00000000..ac66cd60 --- /dev/null +++ b/crypto/test/fift/sort.fif @@ -0,0 +1,27 @@ +{ null null rot + { dup null? not } + { uncons swap rot cons -rot } while drop +} : split +variable 'sort variable 'merge variable 'compare +{ 'sort @ execute } : sort +{ 'merge @ execute } : merge +{ 'compare @ execute } : compare +' < 'compare ! +{ dup null? { drop } { + over null? { nip } { + over car over car compare ' swap if + uncons rot merge cons + } cond + } cond +} 'merge ! +{ dup null? { + dup cdr null? { + split sort swap sort merge + } ifnot + } ifnot +} 'sort ! +3 1 4 1 5 9 2 6 5 9 list +dup .l cr sort .l cr +"once" "upon" "a" "time" "there" "lived" "a" "very" "little" "kitten" 10 list +{ $cmp 0< } 'compare ! +dup .l cr sort .l cr diff --git a/crypto/test/fift/sort2.fif b/crypto/test/fift/sort2.fif new file mode 100644 index 00000000..5611b179 --- /dev/null +++ b/crypto/test/fift/sort2.fif @@ -0,0 +1,28 @@ +{ hole dup 1 { @ execute } does create 1 ' ! does create } : defvect +defvect sort :sort +defvect merge :merge +defvect less :less +' < :less +{ null null rot + { dup null? not } + { uncons swap rot cons -rot } while drop +} : split +{ dup null? { drop } { + over null? { nip } { + over car over car less ' swap if + uncons rot merge cons + } cond + } cond +} :merge +{ dup null? { + dup cdr null? { + split sort swap sort merge + } ifnot + } ifnot +} :sort +3 1 4 1 5 9 2 6 5 9 list +dup .l cr sort .l cr +"once" "upon" "a" "time" "there" "lived" "a" "very" "little" "kitten" 10 list +{ $cmp 0< } :less +dup .l cr sort .l cr + diff --git a/crypto/test/fift/test.fif b/crypto/test/fift/test.fif new file mode 100644 index 00000000..3221117c --- /dev/null +++ b/crypto/test/fift/test.fif @@ -0,0 +1,24 @@ +{ bl word 1 2 ' (create) } "::" 1 (create) +{ bl word 0 2 ' (create) } :: : +{ bl word 2 2 ' (create) } :: :_ +{ bl word 3 2 ' (create) } :: ::_ +{ bl word 0 (create) } : create +{ char " word 1 ' type } ::_ ." +{ char " word 1 { swap { abort } if drop } } ::_ abort" +{ dup * } : square +6 square . +{ swap ({) over 2+ -roll swap (compile) (}) } : does +{ 1 'nop does create } : constant +10 constant ten +ten . +ten ten * . +{ 1 ' emit does create } : emits +42 emits star +34 emits quote +30000 emits figna +star star quote figna quote +{ 0 word drop 0 'nop } ::_ // +star quote // end-to-line comment +{ 1 ' * does create } : mulby +3 mulby 3* +4 3* . diff --git a/crypto/test/fift/testdb.fif b/crypto/test/fift/testdb.fif new file mode 100644 index 00000000..cbaa9817 --- /dev/null +++ b/crypto/test/fift/testdb.fif @@ -0,0 +1,102 @@ +"Asm.fif" include + +PROGRAM{ + +NEWPROC load_dict +NEWPROC generate_dict +NEWPROC save_dict + +NEWPROC do_get +NEWPROC do_set +NEWPROC do_erase + +main PROC:<{ + DUP 1 INT EQUAL IF:<{ + DROP + do_get CALL + }>ELSE<{ + DUP 2 INT EQUAL IF:<{ + DROP + do_set CALL + }>ELSE<{ + DUP 3 INT EQUAL IF:<{ + DROP + do_erase CALL + }> }> }> + -1 INT +}> + +do_get PROC:<{ + load_dict CALL + 32 INT + DICTIGET +}> + +do_set PROC:<{ + load_dict CALL + 32 INT + DICTISET + save_dict CALL +}> + +do_erase PROC:<{ + load_dict CALL + 32 INT + DICTIDEL + DROP + save_dict CALL +}> + +generate_dict PROC:<{ + 4 INT 100 INT REPEAT:<{ + DUP 2DUP MUL ROT 617 INT ADD 1000 INT MOD + }> + DROP 100 INT + NEWDICT + SWAP REPEAT:<{ + s0 s2 XCHG + NEWC + 16 STU + s0 s2 XCHG + 32 INT + DICTISETB + }> +}> + +load_dict PROC:<{ + PUSHROOT + CTOS DUP SEMPTY IF:<{ + DROP + generate_dict CALL + }> +}> + +save_dict PROC:<{ + NEWC + STSLICE + ENDC + POPROOT +}> + +}END>s constant pmc_prog + +{ 1 2 rot pmc_prog } : task_pmc_get +{ 2 3 rot pmc_prog } : task_pmc_set +{ 3 2 rot pmc_prog } : task_pmc_erase + +{ task_pmc_get dbrunvm 2drop } : pmc_get +{ task_pmc_set dbrunvm 2drop } : pmc_set +{ task_pmc_erase dbrunvm 2drop } : pmc_erase + + " .val ."; " true } dictforeach drop cr } : show-dict +{ key-bits { rot . ."-> " swap .val .val ."; " true } dictdiff drop cr } : show-dict-diff +{ key-bits { val@ swap val@ + val, true } dictmerge } : dict-sum +{ null swap key-bits { val@ pair swap cons true } dictforeach drop } : dict>list-rev +{ dict>list-rev list-reverse } : dict>list +( _( 13 169 ) _( 17 289 ) _( 10 100 ) ) mkdict =: Dict +_( 4 16 ) _( 9 81 ) Dict +dictpair +dictpair =: Dict1 +_( 4 20 ) _( 101 10201 ) Dict +dictpair +dictpair =: Dict2 +."Dict1 = " Dict1 show-dict +Dict1 dict>list .l cr +Dict1 file +"dict1000.boc" file>B +B>boc .s diff --git a/crypto/test/fift/testvm4c.fif b/crypto/test/fift/testvm4c.fif new file mode 100644 index 00000000..7e32cb55 --- /dev/null +++ b/crypto/test/fift/testvm4c.fif @@ -0,0 +1,52 @@ +"Asm.fif" include +<{ NEWDICT SWAP REPEAT: + s2 XCHG0 NEWC 16 STU s2 XCHG0 + 16 INT DICTISETB }>s +dup csr. constant mkdict(16,16).code +{ mkdict(16,16).code runvmcode abort"cannot create 16->16 dictionary" } : mkdict(16,16) + +<{ ZERO WHILE:<{ SWAP 16 INT DICTIREMMAX }>DO<{ // n d' x i + SWAP 16 LDU ENDS // n d' i s + 2SWAP SWAP INC + }> DROP +}>s dup csr. constant explode_dict(16,16).code +{ explode_dict(16,16).code runvmcode abort"cannot explode 16->16 dictionary" } : xdict(16,16) +{ xdict(16,16) ."{ " { swap . ."-> " . ."; " } swap times ."}" cr } : .dict(16,16) +{ xdict(16,16) { dup 0< not { ."+" } if ._ 64 + emit } swap times space } : .v +169 13 +239 dup dup * swap +289 17 +3 +mkdict(16,16) constant dict + +{ 16 { 16 i@+ s> 1+ 16 i, true } dictmap } : dict:1+ +{ 16 { 16 i@+ s> swap 16 i@+ s> + 16 i, true } dictmerge } : dict:+ + +dict dict:1+ dup s +dup csr. constant mkdict(16,16).code + +{ mkdict(16,16).code runvmcode abort"cannot create 16->16 dictionary" } : mkdict(16,16) + +<{ ZERO WHILE:<{ SWAP 16 INT DICTIREMMAX }>DO<{ // n d' x i + SWAP 16 LDU ENDS // n d' i s + 2SWAP SWAP INC + }> DROP +}>s dup csr. constant explode_dict(16,16).code + +{ explode_dict(16,16).code runvmcode abort"cannot explode 16->16 dictionary" } : xdict(16,16) +{ xdict(16,16) ."{ " { swap . ."-> " . ."; " } swap times ."}" cr } : .dict(16,16) + +{ 16 { 16 i@+ s> 1+ 16 i, true } dictmap } : dict:1+ +{ 16 { 16 i@+ s> swap 16 i@+ s> + 16 i, true } dictmerge } : dict:+ + +{ 1 swap dup 1 { 2dup dup * + 2 swap 2swap 1+ } 100 times nip mkdict(16,16) } : mdisqpb + +0 mdisqpb 0 { 1+ tuck dup * mdisqpb tuck dict:+ dict:+ swap } 100 times drop +dup s +dup csr. constant mkdict(16,16).code + +{ mkdict(16,16).code runvmcode abort"cannot create 16->16 dictionary" } : mkdict(16,16) + +<{ ZERO WHILE:<{ SWAP 16 INT DICTUREMMAX }>DO<{ // n d' x i + SWAP 16 LDU ENDS // n d' i s + 2SWAP SWAP INC + }> DROP +}>s dup csr. constant explode_dict(16,16).code + +{ explode_dict(16,16).code runvmcode abort"cannot explode 16->16 dictionary" } : xdict(16,16) +{ xdict(16,16) ."{ " { swap x. ."-> " x. ."; " } swap times ."}" } : .dict(16,16) + +{ 16 { 16 u@+ s> 1+ 16 u, true } dictmap } : dict:1+ +{ 16 { 16 u@+ s> swap 16 u@+ s> + 16 u, true } dictmerge } : dict:+ + +0x1000 0x1000 +0x1234 0x1234 +0x1357 0x1357 +0xABCD 0xABCD +0xBFFF 0xBFFF +5 mkdict(16,16) constant D +D dup dict>s csr. .dict(16,16) ." <-- D" cr + +D <{ 0 INT 0 INT ROT 16 INT SUBDICTUGET }>s dup csr. runvmcode drop +dup dict>s csr. .dict(16,16) ." <-- SubDict(D,_)" cr + +D <{ 0x1 INT 4 INT ROT 16 INT SUBDICTUGET }>s runvmcode drop +dup dict>s csr. .dict(16,16) ." <-- SubDict(D,0001)" cr + +D <{ 0x10 INT 8 INT ROT 16 INT SUBDICTUGET }>s runvmcode drop +dup dict>s csr. .dict(16,16) ." <-- SubDict(D,00010000)" cr + +D <{ 0x12 INT 8 INT ROT 16 INT SUBDICTUGET }>s runvmcode drop +dup dict>s csr. .dict(16,16) ." <-- SubDict(D,00010010)" cr + +D <{ 0x1357 INT 16 INT ROT OVER SUBDICTUGET }>s runvmcode drop +dup dict>s csr. .dict(16,16) ." <-- SubDict(D,x1357)" cr + +D <{ 0 INT 2 INT ROT 16 INT SUBDICTUGET }>s runvmcode .s drop +dup dict>s csr. .dict(16,16) ." <-- SubDict(D,00)" cr + +D <{ 1 INT 2 INT ROT 16 INT SUBDICTUGET }>s runvmcode .s drop +dup dict>s csr. .dict(16,16) ." <-- SubDict(D,01)" cr + +D <{ 0 INT 1 INT ROT 16 INT SUBDICTUGET }>s runvmcode .s drop +dup dict>s csr. .dict(16,16) ." <-- SubDict(D,0)" cr + +D <{ 1 INT 1 INT ROT 16 INT SUBDICTUGET }>s runvmcode .s drop +dup dict>s csr. .dict(16,16) ." <-- SubDict(D,1)" cr + +D <{ 0xA INT 4 INT ROT 16 INT SUBDICTUGET }>s runvmcode .s drop +dup dict>s csr. .dict(16,16) ." <-- SubDict(D,1010)" cr + +D <{ 0xB INT 4 INT ROT 16 INT SUBDICTUGET }>s runvmcode .s drop +dup dict>s csr. .dict(16,16) ." <-- SubDict(D,1011)" cr diff --git a/crypto/test/fift/testvm5.fif b/crypto/test/fift/testvm5.fif new file mode 100644 index 00000000..9578fc55 --- /dev/null +++ b/crypto/test/fift/testvm5.fif @@ -0,0 +1,11 @@ +"Asm.fif" include +<{ 2SWAP ADD s2 POP FALSE }>s b{00} +<{ 2SWAP MUL s2 POP FALSE }>s b{1} +<{ 2SWAP SUB s2 POP FALSE }>s b{01} +3 +<{ NEWDICT SWAP REPEAT: 8 INT PFXDICTADD 11 THROWIFNOT }>s runvmcode +abort"cannot create prefix code dictionary" +dup dict>s csr. +<{ <{ rot 8 PFXDICTSWITCH TRUE }>UNTIL SDEMPTY 11 THROWIFNOT }>s +dup csr. +1 2 3 4 5 b{001011} 6 roll runvmcode .s diff --git a/crypto/test/fift/testvm6.fif b/crypto/test/fift/testvm6.fif new file mode 100644 index 00000000..6b57eea0 --- /dev/null +++ b/crypto/test/fift/testvm6.fif @@ -0,0 +1,66 @@ +"Asm.fif" include + +<{ 1 INT 70 INT + REPEAT:<{ 10 MULCONST }> + 1 INT DUP <{ TUCK ADD DUP s3 PUSH GREATER }>UNTIL + MULDIVR + }>s +dup csr. + +runvmcode .s + +<{ 1 INT 70 INT REPEAT:<{ 10 MULCONST }> + 1 INT DUP <{ TUCK ADD TUCK ADD SWAP DUP s3 PUSH GREATER }>UNTIL + MULDIVR + }>s +dup csr. + +runvmcode .s + +<{ WHILE:<{ + DUP DEC + }>DO<{ + DUP DUP 1 INT AND IF:<{ + 3 MULCONST INC + }>ELSE<{ + 1 RSHIFT# + }> + }> +}>s +dup csr. + +<{ WHILE:<{ + DUP DEC + }>DO: + DUP DUP 1 INT AND IF:<{ + 3 MULCONST INC + }>ELSE: + 1 RSHIFT# +}>s + +dup csr. +nip + +17 swap runvmcode .s + +<{ CONT:<{ + TUCK DIVR SWAP DUP MUL NEGATE SWAP ZERO ONE + <{ SWAP s2 s1 PUSH2 DIVR ADD SWAP 2 ADDCONST + 2SWAP OVER DIVR 2SWAP s2 PUSH ISZERO }>UNTIL + DROP s2 POP DROP + }> + 8 PUSHPOW2 70 INT REPEAT:<{ 10 MULCONST }> + DUP 2 LSHIFT# 5 INT s3 PUSH EXECUTE + s0 s2 XCHG 239 INT SWAP EXECUTE SUB + 6 RSHIFTR# +}>s dup csr. + +runvmcode .s + +<{ 8 PUSHPOW2 70 INT REPEAT:<{ 10 MULCONST }> + ZERO DUP + <{ s2 PUSH ADD -ROT INC TUCK DIVR -ROT SWAP s2 PUSH ISZERO }>UNTIL + NIP NIP 8 RSHIFTR# +}>s dup csr. + +runvmcode .s diff --git a/crypto/test/fift/testvm7.fif b/crypto/test/fift/testvm7.fif new file mode 100644 index 00000000..796c771c --- /dev/null +++ b/crypto/test/fift/testvm7.fif @@ -0,0 +1,47 @@ +"Asm.fif" include +<{ -1 INT ZERO ROT + 100 INT REPEAT:<{ + ZERO WHILE:<{ + 3 3 BLKPUSH + // -ROT OVER ADD ROT OVER ADD -ROT OVER ADD ROT + s2 s1 PUXC ADD TUCK ADD s2 s1 PUXC ADD SWAP + DUP ISPOS + }>DO<{ + s4 POP s4 POP s4 POP INC + }> + 3 BLKDROP + s3 XCHG0 NEGATE s2 XCHG0 NEGATE SWAP NEGATE s2 XCHG0 + }> + 3 BLKDROP +}>s dup csr. +// 61 swap runvmcode .s +drop + +<{ -1 INT ZERO ROT ONE ZERO s0 s1 PUSH2 + <{ 3 6 BLKPUSH // a b c m11 m12 m21 m22 a' b' c' + s2 s1 PUXC ADD TUCK ADD s2 s1 PUXC ADD SWAP + DUP ISPOS IF:<{ + s7 POP s7 POP s7 POP // a' b' c' m11 m12 m21 m22 + s2 PUSH ADD SWAP s3 PUSH ADD SWAP + }>ELSE<{ + 3 BLKDROP // a b c m11 m12 m21 m22 + 2SWAP + 3 4 BLKSWAP // m m m m a b c + NEGATE SWAP NEGATE // .. a -c -b + ROT NEGATE // -c -b -a + 4 3 BLKSWAP + }> + DUP 250 QFITS ISNAN + }>UNTIL + 2DROP s3 POP s3 POP DROP +}>s dup csr. +1 { 10 * } 75 times constant One +{ One swap */r } : frac*One +61 over runvmcode drop frac*One swap +2 over runvmcode drop frac*One swap +3 over runvmcode drop frac*One swap +5 over runvmcode drop frac*One swap +6 over runvmcode drop frac*One swap +7 over runvmcode drop frac*One swap +8 over runvmcode drop frac*One swap +.s diff --git a/crypto/test/fift/testvm8.fif b/crypto/test/fift/testvm8.fif new file mode 100644 index 00000000..fba2f51b --- /dev/null +++ b/crypto/test/fift/testvm8.fif @@ -0,0 +1,5 @@ +"Asm.fif" include +<{ 7 INT -4 INT NEWC 64 STI 4 STU ENDC + CTOS 64 LDI 4 LDU ENDS }>s +dup csr. +runvmcode .s diff --git a/crypto/test/fift/testvm9.fif b/crypto/test/fift/testvm9.fif new file mode 100644 index 00000000..81306b06 --- /dev/null +++ b/crypto/test/fift/testvm9.fif @@ -0,0 +1,7 @@ +"Asm.fif" include +<{ 2 INT 3 INT 9 INT NULL CONS CONS CONS }>s dup csr. runvmcode .s +drop =: L +<{ ZERO WHILE:<{ OVER ISNULL NOT }>DO<{ SWAP SECOND SWAP INC }> NIP }>s dup csr. =: ListLength +L ListLength runvmcode .s 2drop +<{ CONT:<{ DUP ISNULL IF:<{ 2DROP ZERO }>ELSE: CDR OVER EXECUTE INC }> TUCK JMPX }>s dup csr. =: ListLengthRec +L ListLengthRec runvmcode .s 2drop diff --git a/crypto/test/fift/testvmprog.fif b/crypto/test/fift/testvmprog.fif new file mode 100644 index 00000000..c2f19f77 --- /dev/null +++ b/crypto/test/fift/testvmprog.fif @@ -0,0 +1,40 @@ +"Asm.fif" include + +PROGRAM{ + +NEWPROC add +NEWPROC sub +NEWPROC mul + +sub <{ s3 s3 XCHG2 SUB s2 XCHG0 SUB }>s PROC + +// compute (5+i)^4 * (239-i) +main PROC:<{ + 5 INT 1 INT // 5+i + 2DUP + mul CALL + 2DUP + mul CALL + 239 INT -1 INT + mul JMP +}> + +add PROC:<{ + s1 s2 XCHG + ADD -ROT ADD SWAP +}> + +// a b c d -- ac-bd ad+bc : complex number multiplication +mul PROC:<{ + s3 s1 PUSH2 // a b c d a c + MUL // a b c d ac + s3 s1 PUSH2 // a b c d ac b d + MUL // a b c d ac bd + SUB // a b c d ac-bd + s4 s4 XCHG2 // ac-bd b c a d + MUL // ac-bd b c ad + -ROT MUL ADD +}> + +}END>s +dup csr. runvmdict .s diff --git a/crypto/test/test-cells.cpp b/crypto/test/test-cells.cpp new file mode 100644 index 00000000..0557395b --- /dev/null +++ b/crypto/test/test-cells.cpp @@ -0,0 +1,571 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "common/refcnt.hpp" +#include "common/bigint.hpp" +#include "common/refint.h" +#include "common/bitstring.h" +#include "common/util.h" +#include "vm/cells.h" +#include "vm/cellslice.h" + +#include "td/utils/tests.h" +#include "td/utils/crypto.h" +#include "td/utils/misc.h" + +static std::stringstream create_ss() { + std::stringstream ss; + ss.imbue(std::locale::classic()); + ss.setf(std::ios_base::fixed, std::ios_base::floatfield); + ss.precision(6); + return ss; +} +static std::stringstream os = create_ss(); + +void show_total_cells(std::ostream& stream) { + stream << "total cells = " << vm::DataCell::get_total_data_cells() << std::endl; +} + +TEST(Cells, simple) { + os = create_ss(); + using namespace td::literals; + vm::CellBuilder cb1, cb2; + cb1.store_bytes("Hello, ", 7).reserve_slice(48) = td::BitSlice{(const unsigned char*)"world!", 48}; + cb2.store_bits(td::BitSlice{(const unsigned char*)"\xd0", 4}) + .store_long(17239, 16) + .store_long(-17, 11) + .store_long(1000000239, 32) + .store_long(1000000239LL * 1000000239) + .store_int256("-1000000000000000000000000239"_i256, 91); + cb1.store_ref(cb2.finalize_copy()); + show_total_cells(os); + cb2.store_bytes("<->", 3); + td::Ref c1{cb1.finalize_copy()}, c2{cb2.finalize_copy()}; + unsigned char hbuff[vm::Cell::hash_bytes]; + os << "cb1 = " << cb1 << "; hash=" << td::buffer_to_hex(td::Slice(cb1.compute_hash(hbuff), 32)) << "; c1 = " << *c1 + << std::endl; + os << "cb2 = " << cb2 << "; hash=" << td::buffer_to_hex(td::Slice(cb2.compute_hash(hbuff), 32)) << "; c2 = " << *c2 + << std::endl; + show_total_cells(os); + + vm::CellSlice cr1(c1); + cr1.dump(os); + os << "fetch_octet() = " << cr1.fetch_octet() << std::endl; + cr1.dump(os); + os << "fetch_octet() = " << cr1.fetch_octet() << std::endl; + cr1.dump(os); + os << "fetch_octet() = " << cr1.fetch_octet() << std::endl; + cr1.dump(os); + os << "fetch_octet() = " << cr1.fetch_octet() << std::endl; + cr1.dump(os); + os << "fetch_ref()=" << td::buffer_to_hex(cr1.prefetch_ref()->get_hash().as_slice()) << std::endl; + + vm::CellSlice cr(vm::NoVm(), cr1.fetch_ref()); + cr.dump(os); + os << "prefetch_ulong(4)=" << cr.prefetch_ulong(4) << std::endl; + cr.dump(os); + os << "fetch_ulong(4)=" << cr.fetch_ulong(4) << std::endl; + cr.dump(os); + os << "fetch_long(16)=" << cr.fetch_long(16) << std::endl; + cr.dump(os); + os << "prefetch_long(11)=" << cr.prefetch_long(11) << std::endl; + cr.dump(os); + os << "fetch_int256(11)=" << cr.fetch_int256(11) << std::endl; + cr.dump(os); + os << "fetch_long(32)=" << cr.fetch_long(32) << std::endl; + cr.dump(os); + os << "prefetch_long(64)=" << cr.prefetch_long(64) << std::endl; + cr.dump(os); + os << "fetch_long(64)=" << cr.fetch_long(64) << std::endl; + cr.dump(os); + os << "prefetch_int256(91)=" << cr.prefetch_int256(91) << std::endl; + cr.dump(os); + os << "fetch_int256(91)=" << cr.fetch_int256(91) << std::endl; + cr.dump(os); + os << "fetch_long(24)=" << cr.fetch_long(24) << std::endl; + cr.dump(os); + cr.clear(); + + REGRESSION_VERIFY(os.str()); +} + +void test_two_bitstrings(const td::BitSlice& bs1, const td::BitSlice& bs2) { + using td::to_binary; + using td::to_hex; + os << "bs1 = " << bs1.to_binary() << " = " << bs1.to_hex() << std::endl; + os << "bs2 = " << to_binary(bs2) << " = " << to_hex(bs2) << std::endl; + td::BitString st{bs1}; + //td::BitString st; + //st.append(bs1); + os << "st = " << to_binary(st) << " = " << to_hex(st) << std::endl; + st.append(bs2); + os << "st = " << to_binary(st) << " = " << to_hex(st) << std::endl; + ASSERT_EQ(to_binary(st), to_binary(bs1) + to_binary(bs2)); + auto bs3 = st.subslice(bs1.size(), bs2.size()); + os << "bs3 = " << to_binary(bs3) << " = " << to_hex(bs3) << std::endl; + ASSERT_EQ(to_binary(bs3), to_binary(bs2)); + ASSERT_EQ(to_hex(bs3), to_hex(bs2)); + bs1.dump(os); + bs2.dump(os); + bs3.dump(os); + std::string bs2_bin = to_binary(bs2); + for (unsigned i = 0; i <= bs2.size(); i++) { + for (unsigned j = 0; j <= bs2.size() - i; j++) { + auto bs4 = bs2.subslice(i, j); + auto bs5 = bs3.subslice(i, j); + if (!(to_binary(bs4) == to_binary(bs5) && to_hex(bs4) == to_hex(bs5) && to_binary(bs4) == bs2_bin.substr(i, j))) { + bs4.dump(os); + bs5.dump(os); + os << "bs2.subslice(" << i << ", " << j << ") = " << to_binary(bs4) << " = " << to_hex(bs4) << std::endl; + os << "bs3.subslice(" << i << ", " << j << ") = " << to_binary(bs5) << " = " << to_hex(bs5) << std::endl; + } + ASSERT_EQ(to_binary(bs4), to_binary(bs5)); + ASSERT_EQ(to_hex(bs4), to_hex(bs5)); + ASSERT_EQ(to_binary(bs4), bs2_bin.substr(i, j)); + } + } +} + +void test_one_bitstring(const td::BitSlice& bs) { + std::string bs_bin = bs.to_binary(); + for (unsigned i1 = 0; i1 <= bs.size(); i1++) { + for (unsigned j1 = 0; j1 <= bs.size() - i1; j1++) { + auto bs1 = bs.subslice(i1, j1); + ASSERT_EQ(bs1.to_binary(), bs_bin.substr(i1, j1)); + for (unsigned i2 = 0; i2 <= bs.size() && i2 < 8; i2++) { + for (unsigned j2 = 0; j2 <= bs.size() - i2; j2++) { + os << "(" << i1 << "," << j1 << ")+(" << i2 << "," << j2 << ")" << std::endl; + auto bs2 = bs.subslice(i2, j2); + ASSERT_EQ(bs2.to_binary(), bs_bin.substr(i2, j2)); + test_two_bitstrings(bs1, bs2); + } + } + } + } +} + +void test_bitstring_fill(unsigned n, unsigned p, unsigned k) { + td::BitString bs{n * 2}; + std::string s; + auto sl1 = td::BitSlice{(const unsigned char*)"\x40", 2}; + for (unsigned i = 0; i < n; i++) { + bs.append(sl1); + s += "01"; + } + os << td::to_binary(bs) << " = " << td::to_hex(bs) << std::endl; + ASSERT_EQ(td::to_binary(bs), s); + unsigned q = k %= p; + for (unsigned i = 0; i < p; i++) { + unsigned a = (q * n * 2) / p; + unsigned b = ((q + 1) * n * 2) / p; + bs.subslice_write(a, b - a) = (q & 1); + std::fill(s.begin() + a, s.begin() + b, (q & 1) + '0'); + os << "Step " << i << " (" << a << "," << b << "): " << td::to_binary(bs) << " = " << td::to_hex(bs) << std::endl; + ASSERT_EQ(td::to_binary(bs), s); + q = (q + k) % p; + } + bs.subslice_write(4, 16) = td::BitSlice{(const unsigned char*)"\x69\x96", 16}; + os << td::to_binary(bs) << " = " << td::to_hex(bs) << std::endl; + std::string t = "0110100110010110"; + std::copy(t.begin(), t.end(), s.begin() + 4); + ASSERT_EQ(td::to_binary(bs), s); +} + +TEST(Bitstrings, main) { + os = create_ss(); + auto test = td::BitSlice{(const unsigned char*)"test", 32}; + ASSERT_EQ(test.to_hex(), "74657374"); + test_two_bitstrings({(const unsigned char*)"\xf1\xd0", 12}, test); + test_two_bitstrings({(const unsigned char*)"\x9f", 3}, {(const unsigned char*)"t", 3}); + test_bitstring_fill(17 * 3, 17, 4); + //test_one_bitstring({(const unsigned char*)"SuperTest", 72}); + REGRESSION_VERIFY(os.str()); +} + +void test_parse_dec(std::string s) { + td::BigInt256 x, y; + os << "s=\"" << s << "\"" << std::endl; + x.parse_dec_slow(s); + y.parse_dec(s); + x.dump(os); + y.dump(os); + ASSERT_TRUE(x == y); + std::string s1 = x.to_dec_string(); + os << s1 << std::endl; + ASSERT_EQ(s, s1); + std::string s2 = x.to_hex_string(); + os << s2 << std::endl; + std::string s3 = x.to_hex_string_slow(); + os << s3 << std::endl; + ASSERT_EQ(s2, s3); +} + +void test_pow2(int exponent) { + td::BigInt256 x; + x.set_pow2(exponent); + os << "2^" << exponent << " = " << x.to_dec_string() << " = 0x" << x.to_hex_string() << std::endl; + x.dump(os); +} + +void test_fits(const td::BigInt256& x) { + int m = 0, n = 0; + const int limit = 300; + os << "x=" << x.to_dec_string() << "; log2(|x|)=" << std::log2(std::abs(x.to_double())) << std::endl; + x.dump(os); + while (m < limit && !x.unsigned_fits_bits(m)) { + m++; + } + for (int i = m; i < limit; i++) { + ASSERT_TRUE(x.unsigned_fits_bits(i)); + } + int su = x.bit_size(false); + while (n < limit && !x.signed_fits_bits(n)) { + n++; + } + for (int i = n; i < limit; i++) { + ASSERT_TRUE(x.signed_fits_bits(i)); + } + int ss = x.bit_size(); + os << "x=" << x.to_dec_string() << "=0x" << x.to_hex_string() << "; x=" << x.to_double() + << "; log2(|x|)=" << std::log2(std::abs(x.to_double())) << "; unsigned: " << m << "=" << su + << " bits; signed: " << n << "=" << ss << " bits" << std::endl; + ASSERT_TRUE(su == m || (su == 0x7fffffff && m == limit)); + ASSERT_EQ(ss, n); + ASSERT_EQ(x.to_hex_string(), x.to_hex_string_slow()); + td::BigInt256 y, z; + ASSERT_TRUE(y.parse_hex(x.to_hex_string()) && y == x); + ASSERT_TRUE(z.parse_dec(x.to_dec_string()) && z == x); +} + +void test_divmod(const td::BigInt256& x, const td::BigInt256& y) { + td::BigInt256 q, r(x); + os << "x = " << x << " = "; + x.dump(os); + os << "y = " << y << " = "; + y.dump(os); + if (!r.mod_div_bool(y, q)) { + os << "division error!\n"; + ASSERT_TRUE(0); + } else { + q.dump(os); + r.dump(os); + if (!q.normalize_bool() || !r.normalize_bool()) { + os << "cannot normalize q or r!\n"; + ASSERT_TRUE(0); + } else { + os << "q = " << q << "; r = " << r << std::endl; + if (y.sgn() > 0) { + ASSERT_TRUE(r.sgn() >= 0); + ASSERT_TRUE(r.cmp(y) < 0); + } else { + ASSERT_TRUE(r.sgn() <= 0); + ASSERT_TRUE(r.cmp(y) > 0); + } + r.add_mul(q, y); + ASSERT_TRUE(r.normalize() == x); + } + } +} + +void test_export_int(const td::BigInt256& x, bool sgnd = true) { + os << "x = " << x.to_hex_string() << std::endl; + int bad = 0, ok = 0; + for (int i = 1; i <= 33; i++) { + unsigned char buff[33]; + std::memset(buff, 0xcc, sizeof(buff)); + if (!x.export_bytes(buff, i, sgnd)) { + ASSERT_EQ(bad, i - 1); + bad = i; + continue; + } else if (++ok < 5) { + if (bad == i - 1) { + os << "export(" << bad << ", " << sgnd << ") = (bad)" << std::endl; + } + os << "export(" << i << ", " << sgnd << ") ="; + char tmp[33 * 3 + 1]; + for (int j = 0; j < i; j++) { + sprintf(tmp + 3 * j, " %02x", buff[j]); + } + os << tmp << std::endl; + td::BigInt256 y; + ASSERT_TRUE(y.import_bytes(buff, i, sgnd)); + os << "import() = " << y.to_hex_string() << std::endl; + ASSERT_TRUE(!x.cmp_un(y)); + } + } + if (!ok) { + os << "export(" << bad << ", " << sgnd << ") = (bad)" << std::endl; + } +} + +TEST(Bigint, main) { + os = create_ss(); + using namespace td::literals; + td::BigInt256 x, y, z; + test_parse_dec("0"); + test_parse_dec("1"); + test_parse_dec("-1"); + test_parse_dec("123"); + test_parse_dec("-239"); + test_parse_dec("-115792089237316195423570985008687907853269984665640564039457584007913129639936"); + test_parse_dec("115792089237316195423570985008687907853269984665640564039457584007913129639935"); + test_parse_dec("143126893554044595713052252685501316785002612509329899766666973726012466208042"); + test_parse_dec("100000000000000000000000000000000000000000000000000000000000000000000000000001"); + x.parse_dec("11111111111111111111111111111111111111111111111111111111111111111111111111111"); + y.parse_dec("22222222222222222222222222222222222"); + x += y; + os << x.to_dec_string() << std::endl; + x -= y; + os << x.to_dec_string() << std::endl; + x -= y; + os << x.to_dec_string() << std::endl; + y -= x; + os << y.to_dec_string() << std::endl; + y += x; + os << x.to_dec_string() << std::endl; + x.parse_dec("10000000000000000000000000000001"); + y.parse_dec("11111111111111111111111111111111"); + z.add_mul(x, y); + os << x.to_dec_string() << " * " << y.to_dec_string() << " = " << z.to_dec_string() << std::endl; + test_pow2(0); + test_pow2(1); + test_pow2(54); + test_pow2(55); + test_pow2(56); + test_pow2(57); + test_pow2(4 * 56 - 2); + test_pow2(4 * 56 - 1); + test_pow2(4 * 56); + test_pow2(4 * 56 + 1); + test_pow2(255); + test_pow2(256); + test_fits("1111111111111111111111111111"_i256); + test_fits( + "0000000000000000000000000000000000000000000000000000000000000000000000000000000000ffffffffffffffffffffffffffff"_x256); + for (int i = 10; i >= -10; --i) { + test_fits(td::BigInt256(i)); + } + test_export_int("10000000000000000000000000000000000000"_i256); + for (int k = 127; k <= 129; k++) { + x.set_pow2(k).add_tiny(-1); + test_export_int(x, true); + test_export_int(x, false); + x.add_tiny(1); + test_export_int(x, true); + test_export_int(x, false); + x.add_tiny(1); + test_export_int(x, true); + test_export_int(x, false); + x.negate(); + test_export_int(x, true); + test_export_int(x, false); + x.add_tiny(1); + test_export_int(x, true); + test_export_int(x, false); + x.add_tiny(1); + test_export_int(x, true); + test_export_int(x, false); + } + for (x = 1, y.set_pow2(256).divmod_tiny(3); x.cmp(y) < 0; x.mul_tiny(3).normalize()) { + test_export_int(x, true); + x.negate(); + test_export_int(x, true); + x.negate(); + } + test_export_int("7fffffffffffffffffffffffffffffff"_x256); + test_export_int("ffffffffffffffffffffffffffffffff"_x256); + test_export_int("7fffffffffffffffffffffffffffffff"_x256); + test_export_int("ffffffffffffffffffffffffffffffff"_x256); + for (int i = 0; i <= 257; i++) { + x.set_pow2(i).add_tiny(-3); + for (int j = -3; j <= 3; j++) { + x.negate().normalize(); + os << "-2^" << i << "+" << -j << ": "; + test_fits(x); + x.negate().normalize(); + os << "2^" << i << "+" << j << ": "; + test_fits(x); + x.add_tiny(1); + } + } + + for (auto t : {"fffffffffffffffffffffffffffffffffffffff"_x256, td::BigInt256{-1}, + "123456789abcdef0123456789abcdef0123456789abcdef"_x256, "-8000000000000000000000000001"_x256}) { + for (int i = 0; i <= 256; i++) { + (x = t).mod_pow2(i).dump(os); + os << "mod 2^" << i << " : " << x.to_hex_string() << std::endl; + } + } + + test_divmod(x.set_pow2(224), "10000000000000"_i256); + test_divmod(x.set_pow2(256), "100000000000000000000000000000000000000000"_i256); + test_divmod(x.set_pow2(256), "100000000000000000000000000000000000000000000"_i256); + test_divmod(x.set_pow2(80), "-100000000000000000000000000000000000000000000"_i256); + test_divmod(x.set_pow2(256), y.set_pow2(128).add_tiny(-1)); + test_divmod(x.set_pow2(224), y.set_pow2(112).add_tiny(-1)); + test_divmod(x.set_pow2(222), y.set_pow2(111).add_tiny(-1)); + test_divmod(td::BigInt256(-1), y.set_pow2(256)); + test_divmod("10000000000000000000000000000000000000000000000000000000000000000"_i256, + "142857142857142857142857142857142857"_i256); + test_divmod("100000000"_i256, "-253"_i256); + test_divmod("-100000000"_i256, "-253"_i256); + test_divmod("-100000000"_i256, "253"_i256); + + test_divmod(x.set_pow2(222), td::BigInt256{std::numeric_limits::min()}); + test_divmod(x.set_pow2(222).negate(), td::BigInt256{std::numeric_limits::min()}); + REGRESSION_VERIFY(os.str()); +} + +TEST(RefInt, main) { + os = create_ss(); + using namespace td::literals; + auto x = "10000000000000000000000"_ri256; + td::RefInt256 y{true, -239}, z{false}; + auto v = x + y; + std::move(v); + os << x << " + " << y << " = " << x + y << std::endl; + os << x << " - " << y << " = " << x - y << std::endl; + os << x << " * " << y << " = " << x * y << std::endl; + os << x << " / " << y << " = " << x / y << std::endl; + os << x << " % " << y << " = " << x % y << std::endl; + os << x << " + " << y << " = " << x + y << std::endl; + os << "10000000000000000000000000000000000000000"_ri256 / "27182818284590"_ri256 << std::endl; + { + auto w(x + y); + z = w; + } + os << "(x-y)*(x+y) = " << (x - y) * (x + y) << std::endl; + os << "z = " << z << std::endl; + z = x; + x += y; + os << "new x = " << x << " = 0x" << hex_string(x) << std::endl; + os << "z = (old x) = " << std::move(z) << std::endl; + os << "x + y = " << std::move(x) + std::move(y) << std::endl; + z = "10000000000000000000000000000000000000000000000000000000000000000000000"_ri256; + //z = td::RefInt256{true} + //z.unique_write()->set_pow2(256); + x = td::RefInt256{true, 0}; + int i = 1; + while (z->sgn() > 0) { + x += z; + z.write().add_tiny(i >> 1).divmod_tiny(i); + ++i; + } + x.write().normalize(); + os << x << " = " << hex_string(x) << std::endl; + REGRESSION_VERIFY(os.str()); +} + +TEST(crc16, main) { + os = create_ss(); + std::string s = "EMSI_FCK"; + unsigned crc16 = td::crc16(td::Slice{s}); + os << "s = `" << s << "`; crc16 = " << std::hex << crc16 << std::dec << std::endl; + REGRESSION_VERIFY(os.str()); +} + +TEST(base64, main) { + os = create_ss(); + std::vector arr = {"TEST STRING NUMBER ONE", "TEST STRING NUMBER FOUR", "TEST STRING NUMBER THREE"}; + for (std::string s : arr) { + std::string t = td::str_base64_encode(s); + std::string u = td::str_base64_decode(t); + os << "`" << s << "` -> `" << t << "` -> `" << u << "`" << std::endl; + os << (s == u) << std::endl; + } + std::string s; + int k = 0; + for (int i = 0; i < 1024; i++) { + s.push_back((char)(k >> 8)); + k = 69069 * k + 1; + } + std::string t = td::str_base64_encode(s); + std::string u = td::str_base64_decode(t, true); + os << t << std::endl; + os << (s == u) << std::endl; + t = td::str_base64_encode(s, true); + u = td::str_base64_decode(t, true); + os << t << std::endl; + os << (s == u) << std::endl; + u = td::sha256(td::Slice{s}); + for (int i = 0; i < 32; i++) { + os << std::hex << ((u[i] >> 4) & 15) << (u[i] & 15); + } + os << std::dec << std::endl; + REGRESSION_VERIFY(os.str()); +} + +void check_bits256_scan(std::ostream& os, td::Bits256 a, td::Bits256 b) { + auto c = a ^ b; + auto bit = c.count_leading_zeroes(); + auto bit2 = a.count_matching(b); + // os << a.to_hex() << " and " << b.to_hex() << " match in " << bit << " or " << bit2 << " first bits" << std::endl; + // std::cerr << a.to_hex() << " and " << b.to_hex() << " match in " << bit << " or " << bit2 << " first bits (a XOR b = " << c.to_hex() << ")" << std::endl; + CHECK((int)bit >= 0 && bit <= 256); + for (td::uint32 i = 0; i < bit; i++) { + CHECK(a[i] == b[i]); + } + CHECK(bit == 256 || a[bit] != b[bit]); + CHECK(bit == bit2); +} + +void check_bits_scan(std::ostream& os, td::ConstBitPtr a, bool value) { + auto bit = (unsigned)a.scan(value, 256); + CHECK((int)bit >= 0 && bit <= 256); + for (td::uint32 i = 0; i < bit; i++) { + CHECK(a[i] == value); + } + CHECK(bit == 256 || a[bit] != value); +} + +TEST(bits256_scan, main) { + os = create_ss(); + td::Bits256 a, b; + int k = 0; + unsigned char r[1024]; + for (auto& c : r) { + c = (k & 0x80) ? (unsigned char)(k >> 8) : 0; + k = 69069 * k + 1; + } + for (k = 0; k < 32; k++) { + a = td::ConstBitPtr{r + 32 * k}; + for (int j = 0; j < 32; j++) { + b = td::ConstBitPtr{r + 32 * j}; + check_bits256_scan(os, a, b); + } + b = a; + unsigned i = r[7 + k]; + b[i] = b[i] ^ true; + check_bits256_scan(os, a, b); + } + for (k = 0; k < 256; k++) { + check_bits_scan(os, td::ConstBitPtr{r} + k, false); + check_bits_scan(os, td::ConstBitPtr{r} + k, true); + } + os << "bits256_scan test OK"; + REGRESSION_VERIFY(os.str()); +} diff --git a/crypto/test/test-db.cpp b/crypto/test/test-db.cpp new file mode 100644 index 00000000..28f05ff8 --- /dev/null +++ b/crypto/test/test-db.cpp @@ -0,0 +1,2008 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "vm/boc.h" +#include "vm/cellslice.h" +#include "vm/cells.h" +#include "common/AtomicRef.h" +#include "vm/cells/MerkleProof.h" +#include "vm/cells/MerkleUpdate.h" +#include "vm/db/BlobView.h" +#include "vm/db/CellStorage.h" +#include "vm/db/CellHashTable.h" +#include "vm/db/TonDb.h" +#include "vm/db/StaticBagOfCellsDb.h" + +#include "td/utils/base64.h" +#include "td/utils/benchmark.h" +#include "td/utils/crypto.h" +#include "td/utils/Random.h" +#include "td/utils/Slice.h" +#include "td/utils/Status.h" +#include "td/utils/Timer.h" +#include "td/utils/filesystem.h" +#include "td/utils/port/path.h" +#include "td/utils/format.h" +#include "td/utils/misc.h" +#include "td/utils/tests.h" +#include "td/utils/tl_parsers.h" +#include "td/utils/tl_helpers.h" + +#include "td/db/RocksDb.h" +#include "td/db/MemoryKeyValue.h" + +#include +#include + +#include + +struct Step { + std::function func; + td::uint32 weight; +}; +class RandomSteps { + public: + RandomSteps(std::vector steps) : steps_(std::move(steps)) { + for (auto &step : steps_) { + steps_sum_ += step.weight; + } + } + template + void step(Random &rnd) { + auto w = rnd() % steps_sum_; + for (auto &step : steps_) { + if (w < step.weight) { + step.func(); + break; + } + w -= step.weight; + } + } + + private: + std::vector steps_; + td::int32 steps_sum_ = 0; +}; + +namespace vm { + +std::vector do_get_serialization_modes() { + std::vector res; + for (int i = 0; i < 32; i++) { + if ((i & BagOfCells::Mode::WithCacheBits) && !(i & BagOfCells::Mode::WithIndex)) { + continue; + } + res.push_back(i); + } + return res; +} +const std::vector &get_serialization_modes() { + static auto modes = do_get_serialization_modes(); + return modes; +} + +template +int get_random_serialization_mode(T &rnd) { + auto &modes = get_serialization_modes(); + return modes[rnd.fast(0, (int)modes.size() - 1)]; +} + +class BenchSha256 : public td::Benchmark { + public: + std::string get_description() const override { + return "SHA256"; + } + + void run(int n) override { + int res = 0; + for (int i = 0; i < n; i++) { + digest::SHA256 hasher; + hasher.feed("abcd", 4); + unsigned char buf[32]; + hasher.extract(buf); + res += buf[0]; + } + td::do_not_optimize_away(res); + } +}; +class BenchSha256Reuse : public td::Benchmark { + public: + std::string get_description() const override { + return "SHA256 reuse"; + } + + void run(int n) override { + int res = 0; + digest::SHA256 hasher; + for (int i = 0; i < n; i++) { + hasher.reset(); + hasher.feed("abcd", 4); + unsigned char buf[32]; + hasher.extract(buf); + res += buf[0]; + } + td::do_not_optimize_away(res); + } +}; +class BenchSha256Low : public td::Benchmark { + public: + std::string get_description() const override { + return "SHA256 low level"; + } + + void run(int n) override { + int res = 0; + SHA256_CTX ctx; + for (int i = 0; i < n; i++) { + SHA256_Init(&ctx); + SHA256_Update(&ctx, "abcd", 4); + unsigned char buf[32]; + SHA256_Final(buf, &ctx); + res += buf[0]; + } + td::do_not_optimize_away(res); + } +}; +class BenchSha256Tdlib : public td::Benchmark { + public: + std::string get_description() const override { + return "SHA256 TDLib"; + } + + void run(int n) override { + int res = 0; + static TD_THREAD_LOCAL td::Sha256State *ctx; + for (int i = 0; i < n; i++) { + td::init_thread_local(ctx); + ctx->init(); + ctx->feed("abcd"); + unsigned char buf[32]; + ctx->extract(td::MutableSlice(buf, 32), false); + res += buf[0]; + } + td::do_not_optimize_away(res); + } +}; +TEST(Cell, sha_benchmark) { + bench(BenchSha256Tdlib()); + bench(BenchSha256Low()); + bench(BenchSha256Reuse()); + bench(BenchSha256()); +} + +std::string serialize_boc(Ref cell, int mode = 31) { + CHECK(cell.not_null()); + vm::BagOfCells boc; + boc.add_root(std::move(cell)); + boc.import_cells().ensure(); + auto res = boc.serialize_to_string(mode); + CHECK(res.size() != 0); + return res; +} +std::string serialize_boc(td::Span> cells, int mode = 31) { + CHECK(!cells.empty()); + vm::BagOfCells boc; + for (auto cell : cells) { + boc.add_root(std::move(cell)); + } + boc.import_cells().ensure(); + auto res = boc.serialize_to_string(mode); + CHECK(res.size() != 0); + return res; +} + +Ref deserialize_boc(td::Slice serialized) { + vm::BagOfCells boc; + boc.deserialize(serialized).ensure(); + return boc.get_root_cell(); +} +std::vector> deserialize_boc_multiple(td::Slice serialized) { + vm::BagOfCells boc; + boc.deserialize(serialized).ensure(); + std::vector> res; + for (int i = 0; i < boc.get_root_count(); i++) { + res.push_back(boc.get_root_cell(i)); + } + return res; +} + +class CellExplorer { + public: + struct Op { + enum { Pop, ReadCellSlice } type; + bool should_load; + int children_mask; + }; + struct Exploration { + std::vector ops; + std::string log; + std::set visited; + std::vector> visited_cells; + }; + + static Exploration explore(Ref root, std::vector ops) { + CellExplorer e(root); + for (auto op : ops) { + e.do_op(op); + } + return e.get_exploration(); + } + + template + static Exploration random_explore(Ref root, T &rnd) { + CellExplorer e(root); + int it = 0; + int cnt = rnd.fast(1, 100); + while (it++ < cnt && e.do_random_op(rnd)) { + } + return e.get_exploration(); + } + + private: + CellExplorer(Ref root) { + if (root.not_null()) { + cells_.push_back(std::move(root)); + } + } + + std::vector> cells_; + Ref cs_; + std::vector ops_; + std::set visited_; + std::map> visited_cells_; + td::StringBuilder log_{{}, true}; + + void do_op(Op op) { + ops_.push_back(op); + log_op(op); + switch (op.type) { + case op.Pop: { + CHECK(!cells_.empty()); + CHECK(cs_.is_null()); + auto cell = std::move(cells_.back()); + cells_.pop_back(); + visited_cells_.emplace(cell->get_hash(), cell); + log_cell(cell); + if (op.should_load) { + log_loaded_cell(cell); + visited_.insert(cell->get_hash()); + // It is ok to visit the same vertex multiple times + cs_ = Ref{true, NoVm(), std::move(cell)}; + } + break; + } + case op.ReadCellSlice: { + CHECK(cs_.not_null()); + log_cell_slice(cs_); + for (unsigned i = 0; i < cs_->size_refs(); i++) { + if ((op.children_mask >> i) % 2 != 0) { + cells_.push_back(cs_->prefetch_ref(i)); + } + } + cs_ = {}; + break; + } + } + } + + template + bool do_random_op(T &rnd) { + if (cs_.not_null()) { + int children_mask = 0; + if (cs_->size_refs() != 0 && rnd.fast(0, 3) != 0) { + //children_mask = rnd.fast(1, (1 << cs_->size_refs()) - 1); + children_mask = (1 << cs_->size_refs()) - 1; + } + do_op({Op::ReadCellSlice, false, children_mask}); + return true; + } + if (!cells_.empty()) { + do_op({Op::Pop, rnd.fast(0, 30) != 0, 0}); + return true; + } + return false; + } + + Exploration get_exploration() { + std::vector> visited_cells; + for (auto &it : visited_cells_) { + visited_cells.push_back(it.second); + } + return {std::move(ops_), log_.as_cslice().str(), std::move(visited_), std::move(visited_cells)}; + } + + void log_op(Op op) { + switch (op.type) { + case op.Pop: + log_ << "pop" << (op.should_load ? " and load" : "") << "\n"; + break; + case op.ReadCellSlice: + log_ << "read slice " << op.children_mask << "\n"; + break; + } + } + void log_cell(const Ref &cell) { + log_ << cell->get_level_mask().get_mask() << " " << cell->get_hash() << "\n"; + } + void log_loaded_cell(const Ref &cell) { + log_ << "depth: "; + for (unsigned i = 0; i <= cell->get_level(); i++) { + log_ << cell->get_depth(i) << " "; + } + log_ << "\n"; + } + void log_cell_slice(const Ref &cs) { + log_ << cs->special_type() << " " << cs->size() << " " << cs->size_refs() << " " + << td::bitstring::bits_to_hex(cs->data_bits(), cs->size()) << "\n"; + } +}; + +class RandomBagOfCells { + public: + template + RandomBagOfCells(size_t size, T &rnd, bool with_prunned_branches, std::vector> cells) { + std::map depth; + + for (auto &cell : cells) { + nodes_.emplace_back(cell, calc_depth(cell, depth)); + } + + for (size_t i = 0; i < size; i++) { + add_random_cell(rnd, with_prunned_branches); + } + } + + Ref get_root() { + CHECK(!nodes_.empty()); + // Fix root to be zero level + while (nodes_.back().cell->get_level() != 0) { + nodes_.emplace_back(CellBuilder::create_merkle_proof(nodes_.back().cell), nodes_.back().merkle_depth + 1); + } + return nodes_.back().cell; + } + template + std::vector> get_random_roots(size_t size, T &rnd) { + CHECK(!nodes_.empty()); + std::vector> res(size); + for (auto &c : res) { + c = nodes_[rnd.fast(0, static_cast(nodes_.size()) - 1)].cell; + } + return res; + } + + size_t get_size() const { + return nodes_.size(); + } + + template + void add_random_cell(T &rnd, bool with_prunned_branches = true) { + int cnt = 0; + while (true) { + CellBuilder cb; + int next_cnt = rnd.fast(0, Cell::max_refs); + int merkle_depth = 0; + for (int j = 0; j < next_cnt && !nodes_.empty(); j++) { + int to = rnd.fast(j == 0 && nodes_.size() > 3 ? (int)nodes_.size() - 3 : 0, (int)nodes_.size() - 1); + merkle_depth = td::max(merkle_depth, nodes_.at(to).merkle_depth); + cb.store_ref(nodes_[to].cell); + } + int size = rnd.fast(0, 4); + for (int j = 0; j < size; j++) { + cb.store_bytes(&"ab"[rnd.fast(0, 1)], 1); + } + if (rnd.fast(0, 4) == 4) { + cb.store_bits(rnd.fast(0, 1) ? "\xff" : "\x55", rnd.fast(1, 7)); + } + Ref cell = cb.finalize(); + auto cell_level = cell->get_level(); + if (with_prunned_branches) { + if (rnd.fast(0, 5) == 0 && cell_level + 1 < Cell::max_level) { + cell = CellBuilder::create_pruned_branch(std::move(cell), cell_level + 1); + } + if (merkle_depth + 1 + cell->get_level() < Cell::max_level && rnd.fast(0, 10) == 0) { + cell = CellBuilder::create_merkle_proof(std::move(cell)); + merkle_depth++; + } + } + if (merkle_depth + cell->get_level() >= Cell::max_level) { + cnt++; + CHECK(cnt < 1000); + continue; + } + CHECK(cell.not_null()); + nodes_.emplace_back(std::move(cell), merkle_depth); + break; + } + } + + private: + struct Node { + Node() = default; + Node(Ref cell, int merkle_depth) : cell(std::move(cell)), merkle_depth(merkle_depth) { + } + Ref cell; + int merkle_depth; + }; + std::vector nodes_; + + auto calc_depth(const Ref &root, std::map &depth) -> int { + auto it_flag = depth.emplace(root->get_hash(), 0); + if (!it_flag.second) { + return it_flag.first->second; + } + auto res = 0; + CellSlice cs(NoVm(), root); + for (unsigned i = 0; i < cs.size_refs(); i++) { + res = std::max(res, calc_depth(cs.prefetch_ref(i), depth)); + } + if (cs.special_type() == Cell::SpecialType::MerkleProof) { + res++; + } + depth[root->get_hash()] = res; + return res; + }; +}; + +template +void random_shuffle(td::MutableSpan v, td::Random::Xorshift128plus &rnd) { + for (std::size_t i = 1; i < v.size(); i++) { + auto pos = static_cast(rnd() % (i + 1)); + std::swap(v[i], v[pos]); + } +} +Ref gen_random_cell(int size, td::Random::Xorshift128plus &rnd, bool with_prunned_branches = true, + std::vector> cells = {}) { + if (!cells.empty()) { + random_shuffle(td::MutableSpan>(cells), rnd); + cells.resize(cells.size() % rnd()); + } + return RandomBagOfCells(size, rnd, with_prunned_branches, std::move(cells)).get_root(); +} +std::vector> gen_random_cells(int roots, int size, td::Random::Xorshift128plus &rnd, + bool with_prunned_branches = true, std::vector> cells = {}) { + if (!cells.empty()) { + random_shuffle(td::MutableSpan>(cells), rnd); + cells.resize(cells.size() % rnd()); + } + return RandomBagOfCells(size, rnd, with_prunned_branches, std::move(cells)).get_random_roots(roots, rnd); +} + +TEST(Cell, MerkleProof) { + td::Random::Xorshift128plus rnd{123}; + for (int t = 0; t < 1000; t++) { + bool with_prunned_branches = true; + auto cell = gen_random_cell(rnd.fast(1, 1000), rnd, with_prunned_branches); + auto exploration = CellExplorer::random_explore(cell, rnd); + + auto usage_tree = std::make_shared(); + auto usage_cell = UsageCell::create(cell, usage_tree->root_ptr()); + auto exploration2 = CellExplorer::explore(usage_cell, exploration.ops); + ASSERT_EQ(exploration.log, exploration2.log); + + auto is_prunned = [&](const Ref &cell) { return exploration.visited.count(cell->get_hash()) == 0; }; + auto proof = MerkleProof::generate(cell, is_prunned); + // CellBuilder::virtualize(proof, 1); + //ASSERT_EQ(1u, proof->get_level()); + auto virtualized_proof = MerkleProof::virtualize(proof, 1); + auto exploration3 = CellExplorer::explore(virtualized_proof, exploration.ops); + ASSERT_EQ(exploration.log, exploration3.log); + + auto proof2 = MerkleProof::generate(cell, usage_tree.get()); + CHECK(proof2->get_depth() == proof->get_depth()); + auto virtualized_proof2 = MerkleProof::virtualize(proof2, 1); + auto exploration4 = CellExplorer::explore(virtualized_proof2, exploration.ops); + ASSERT_EQ(exploration.log, exploration4.log); + } +}; + +TEST(Cell, MerkleProofCombine) { + td::Random::Xorshift128plus rnd{123}; + for (int t = 0; t < 1000; t++) { + bool with_prunned_branches = true; + auto cell = gen_random_cell(rnd.fast(1, 1000), rnd, with_prunned_branches); + auto exploration1 = CellExplorer::random_explore(cell, rnd); + auto exploration2 = CellExplorer::random_explore(cell, rnd); + + Ref proof1; + { + auto usage_tree = std::make_shared(); + auto usage_cell = UsageCell::create(cell, usage_tree->root_ptr()); + CellExplorer::explore(usage_cell, exploration1.ops); + proof1 = MerkleProof::generate(cell, usage_tree.get()); + + auto virtualized_proof = MerkleProof::virtualize(proof1, 1); + auto exploration = CellExplorer::explore(virtualized_proof, exploration1.ops); + ASSERT_EQ(exploration.log, exploration1.log); + } + + Ref proof2; + { + auto usage_tree = std::make_shared(); + auto usage_cell = UsageCell::create(cell, usage_tree->root_ptr()); + CellExplorer::explore(usage_cell, exploration2.ops); + proof2 = MerkleProof::generate(cell, usage_tree.get()); + + auto virtualized_proof = MerkleProof::virtualize(proof2, 1); + auto exploration = CellExplorer::explore(virtualized_proof, exploration2.ops); + ASSERT_EQ(exploration.log, exploration2.log); + } + + Ref proof12; + { + auto usage_tree = std::make_shared(); + auto usage_cell = UsageCell::create(cell, usage_tree->root_ptr()); + CellExplorer::explore(usage_cell, exploration1.ops); + CellExplorer::explore(usage_cell, exploration2.ops); + proof12 = MerkleProof::generate(cell, usage_tree.get()); + + auto virtualized_proof = MerkleProof::virtualize(proof12, 1); + auto exploration_a = CellExplorer::explore(virtualized_proof, exploration1.ops); + auto exploration_b = CellExplorer::explore(virtualized_proof, exploration2.ops); + ASSERT_EQ(exploration_a.log, exploration1.log); + ASSERT_EQ(exploration_b.log, exploration2.log); + } + + Ref proof_union; + { + proof_union = MerkleProof::combine(proof1, proof2); + ASSERT_EQ(proof_union->get_hash(), proof12->get_hash()); + + auto virtualized_proof = MerkleProof::virtualize(proof_union, 1); + auto exploration_a = CellExplorer::explore(virtualized_proof, exploration1.ops); + auto exploration_b = CellExplorer::explore(virtualized_proof, exploration2.ops); + ASSERT_EQ(exploration_a.log, exploration1.log); + ASSERT_EQ(exploration_b.log, exploration2.log); + } + { + auto cell = MerkleProof::virtualize(proof12, 1); + + auto usage_tree = std::make_shared(); + auto usage_cell = UsageCell::create(cell, usage_tree->root_ptr()); + CellExplorer::explore(usage_cell, exploration1.ops); + auto proof = MerkleProof::generate(cell, usage_tree.get()); + + auto virtualized_proof = MerkleProof::virtualize(proof, 2); + auto exploration = CellExplorer::explore(virtualized_proof, exploration1.ops); + ASSERT_EQ(exploration.log, exploration1.log); + if (proof->get_hash() != proof1->get_hash()) { + CellSlice(NoVm(), proof12).print_rec(std::cerr); + CellSlice(NoVm(), proof).print_rec(std::cerr); + CellSlice(NoVm(), proof1).print_rec(std::cerr); + LOG(ERROR) << proof->get_level() << " " << proof->get_hash().to_hex(); + LOG(ERROR) << proof->get_level() << " " << proof1->get_hash().to_hex(); + LOG(FATAL) << "?"; + } + } + } +}; + +int X = 20; +Ref gen_random_cell(int size, Ref from, td::Random::Xorshift128plus &rnd, + bool with_prunned_branches = true) { + auto exploration = CellExplorer::random_explore(from, rnd); + return gen_random_cell(size, rnd, with_prunned_branches, std::move(exploration.visited_cells)); +} +auto gen_merkle_update(Ref cell, td::Random::Xorshift128plus &rnd, bool with_prunned_branches) { + auto usage_tree = std::make_shared(); + auto usage_cell = UsageCell::create(cell, usage_tree->root_ptr()); + auto new_cell = gen_random_cell(rnd.fast(1, X), usage_cell, rnd, with_prunned_branches); + auto update = MerkleUpdate::generate(cell, new_cell, usage_tree.get()); + return std::make_tuple(new_cell, update, usage_tree); +}; + +void check_merkle_update(Ref A, Ref B, Ref AB) { + CHECK(AB.not_null()); + CHECK(A.not_null()); + MerkleUpdate::may_apply(A, AB).ensure(); + MerkleUpdate::validate(AB).ensure(); + auto got_B = MerkleUpdate::apply(A, AB); + ASSERT_EQ(B->get_hash(), got_B->get_hash()); +}; + +TEST(Cell, MerkleUpdate) { + td::Random::Xorshift128plus rnd{123}; + for (int t = 0; t < 1000; t++) { + bool with_prunned_branches = true; + auto A = gen_random_cell(rnd.fast(1, 1000), rnd, with_prunned_branches); + + Ref B; + Ref AB; + std::tie(B, AB, std::ignore) = gen_merkle_update(A, rnd, with_prunned_branches); + check_merkle_update(A, B, AB); + } +}; + +TEST(Cell, MerkleUpdateCombine) { + td::Random::Xorshift128plus rnd{123}; + for (int t = 0; t < 1000; t++) { + bool with_prunned_branches = true; + auto A = gen_random_cell(rnd.fast(1, X), rnd, with_prunned_branches); + + Ref B; + Ref AB; + std::tie(B, AB, std::ignore) = gen_merkle_update(A, rnd, with_prunned_branches); + check_merkle_update(A, B, AB); + + Ref C; + Ref BC; + std::tie(C, BC, std::ignore) = gen_merkle_update(B, rnd, with_prunned_branches); + check_merkle_update(B, C, BC); + + check_merkle_update(A, C, MerkleUpdate::combine(AB, BC)); + } +}; + +class BenchCellBuilder : public td::Benchmark { + public: + std::string get_description() const override { + return "BenchCellBuilder"; + } + + void run(int n) override { + td::Random::Xorshift128plus rnd(123); + std::string data(128, ' '); + for (auto &c : data) { + c = static_cast(rnd()); + } + + for (int i = 0; i < n; i++) { + CellBuilder cb; + cb.store_bytes(data.data(), rnd() & 127); + cb.finalize(false); + } + } +}; +TEST(TonDb, BenchCellBuilder) { + td::bench(BenchCellBuilder()); +} +class BenchCellBuilder2 : public td::Benchmark { + public: + std::string get_description() const override { + return "BenchCellBuilder"; + } + + void run(int n) override { + td::Random::Xorshift128plus rnd(123); + + for (int i = 0; i < n; i++) { + gen_random_cell(rnd.fast(1, 1000), rnd); + } + } +}; +TEST(TonDb, BenchCellBuilder2) { + td::bench(BenchCellBuilder2()); +} +class BenchCellBuilder3 : public td::Benchmark { + public: + std::string get_description() const override { + return "BenchCellBuilder"; + } + + void run(int n) override { + td::Random::Xorshift128plus rnd(123); + + for (int i = 0; i < n; i++) { + auto cell = gen_random_cell(rnd.fast(1, 1000), rnd, false); + auto cell_hash = cell->get_hash().to_hex(); + + int mode = get_random_serialization_mode(rnd); + + auto serialized = serialize_boc(std::move(cell), mode); + CHECK(serialized.size() != 0); + + auto loaded_cell = deserialize_boc(serialized); + ASSERT_EQ(cell_hash, loaded_cell->get_hash().to_hex()); + + auto new_serialized = serialize_boc(std::move(loaded_cell), mode); + ASSERT_EQ(serialized, new_serialized); + } + } +}; +TEST(TonDb, BenchCellBuilder3) { + td::bench(BenchCellBuilder3()); +} + +TEST(TonDb, Boc) { + td::Random::Xorshift128plus rnd{123}; + for (int t = 0; t < 1000; t++) { + auto cell = gen_random_cell(rnd.fast(1, 1000), rnd); + auto cell_hash = cell->get_hash(); + auto mode = get_random_serialization_mode(rnd); + + auto serialized = serialize_boc(std::move(cell), mode); + CHECK(serialized.size() != 0); + + auto loaded_cell = deserialize_boc(serialized); + ASSERT_EQ(cell_hash, loaded_cell->get_hash()); + + auto new_serialized = serialize_boc(std::move(loaded_cell), mode); + ASSERT_EQ(serialized, new_serialized); + } +}; +TEST(TonDb, BocMultipleRoots) { + td::Random::Xorshift128plus rnd{123}; + for (int t = 0; t < 200; t++) { + auto cells = gen_random_cells(rnd.fast(1, 10), rnd.fast(1, 1000), rnd); + std::vector cell_hashes; + for (size_t i = 0; i < cells.size(); i++) { + cell_hashes.push_back(cells[i]->get_hash()); + } + auto mode = get_random_serialization_mode(rnd); + auto serialized = serialize_boc(cells, mode); + CHECK(serialized.size() != 0); + + auto loaded_cells = deserialize_boc_multiple(serialized); + ASSERT_EQ(cell_hashes.size(), loaded_cells.size()); + + for (size_t i = 0; i < cell_hashes.size(); i++) { + ASSERT_EQ(cell_hashes[i], loaded_cells[i]->get_hash()); + } + auto new_serialized = serialize_boc(std::move(loaded_cells), mode); + ASSERT_EQ(serialized, new_serialized); + } +}; + +TEST(TonDb, DynamicBoc) { + td::Random::Xorshift128plus rnd{123}; + std::string old_root_hash; + std::string old_root_serialization; + auto kv = std::make_shared(); + auto dboc = DynamicBagOfCellsDb::create(); + dboc->set_loader(std::make_unique(kv)); + for (int t = 1000; t >= 0; t--) { + if (rnd() % 10 == 0) { + dboc = DynamicBagOfCellsDb::create(); + } + dboc->set_loader(std::make_unique(kv)); + Ref old_root; + if (!old_root_hash.empty()) { + old_root = dboc->load_cell(old_root_hash).move_as_ok(); + auto serialization = serialize_boc(old_root); + ASSERT_EQ(old_root_serialization, serialization); + } + + auto cell = gen_random_cell(rnd.fast(1, 1000), rnd); + old_root_hash = cell->get_hash().as_slice().str(); + old_root_serialization = serialize_boc(cell); + + // Check that DynamicBagOfCells properly loads cells + cell = vm::StaticBagOfCellsDbLazy::create(vm::BufferSliceBlobView::create(td::BufferSlice(old_root_serialization))) + .move_as_ok() + ->get_root_cell(0) + .move_as_ok(); + + dboc->dec(old_root); + if (t != 0) { + dboc->inc(cell); + } + dboc->prepare_commit(); + { + CellStorer cell_storer(*kv); + dboc->commit(cell_storer); + } + } + ASSERT_EQ(0u, kv->count("").ok()); +}; + +TEST(TonDb, DynamicBoc2) { + int VERBOSITY_NAME(boc) = VERBOSITY_NAME(DEBUG) + 10; + td::Random::Xorshift128plus rnd{123}; + int total_roots = 10000; + int max_roots = 20; + std::vector root_hashes(max_roots); + std::vector> roots(max_roots); + int last_commit_at = 0; + int first_root_id = 0; + int last_root_id = 0; + auto kv = std::make_shared(); + auto dboc = DynamicBagOfCellsDb::create(); + dboc->set_loader(std::make_unique(kv)); + + auto add_root = [&](Ref root) { + dboc->inc(root); + root_hashes[last_root_id % max_roots] = (root->get_hash().as_slice().str()); + roots[last_root_id % max_roots] = root; + last_root_id++; + }; + + auto get_root = [&](int root_id) { + VLOG(boc) << " from older root #" << root_id; + auto from_root = roots[root_id % max_roots]; + if (from_root.is_null()) { + VLOG(boc) << " from db"; + auto from_root_hash = root_hashes[root_id % max_roots]; + from_root = dboc->load_cell(from_root_hash).move_as_ok(); + } else { + VLOG(boc) << "FROM MEMORY"; + } + return from_root; + }; + auto new_root = [&] { + if (last_root_id == total_roots) { + return; + } + if (last_root_id - first_root_id >= max_roots) { + return; + } + VLOG(boc) << "add root"; + Ref from_root; + if (first_root_id != last_root_id) { + from_root = get_root(rnd.fast(first_root_id, last_root_id - 1)); + } + VLOG(boc) << " ..."; + add_root(gen_random_cell(rnd.fast(1, 20), from_root, rnd)); + VLOG(boc) << " OK"; + }; + + auto commit = [&] { + VLOG(boc) << "commit"; + dboc->prepare_commit(); + { + CellStorer cell_storer(*kv); + dboc->commit(cell_storer); + } + dboc->set_loader(std::make_unique(kv)); + for (int i = last_commit_at; i < last_root_id; i++) { + roots[i % max_roots].clear(); + } + last_commit_at = last_root_id; + }; + auto reset = [&] { + VLOG(boc) << "reset"; + commit(); + dboc = DynamicBagOfCellsDb::create(); + dboc->set_loader(std::make_unique(kv)); + }; + + auto delete_root = [&] { + VLOG(boc) << "Delete root"; + if (first_root_id == last_root_id) { + return; + } + dboc->dec(get_root(first_root_id)); + first_root_id++; + VLOG(boc) << " OK"; + }; + + RandomSteps steps({{new_root, 10}, {delete_root, 9}, {commit, 2}, {reset, 1}}); + while (first_root_id != total_roots) { + VLOG(boc) << first_root_id << " " << last_root_id << " " << kv->count("").ok(); + steps.step(rnd); + } + commit(); + ASSERT_EQ(0u, kv->count("").ok()); +} + +template +td::Status test_boc_deserializer(std::vector> cells, int mode) { + auto total_data_cells_before = vm::DataCell::get_total_data_cells(); + SCOPE_EXIT { + auto total_data_cells_after = vm::DataCell::get_total_data_cells(); + ASSERT_EQ(total_data_cells_before, total_data_cells_after); + }; + auto serialized = serialize_boc(cells, mode); + CHECK(serialized.size() != 0); + + TRY_RESULT(boc_deserializer, BocDeserializerT::create(serialized)); + TRY_RESULT(root_count, boc_deserializer->get_root_count()); + ASSERT_EQ(cells.size(), root_count); + + std::vector> loaded_cells; + for (size_t root_i = 0; root_i < root_count; root_i++) { + TRY_RESULT(loaded_cell, boc_deserializer->get_root_cell(root_i)); + auto cell = cells[root_i]; + ASSERT_EQ(cell->get_level(), loaded_cell->get_level()); + for (int i = 0; i <= (int)cell->get_level(); i++) { + ASSERT_EQ(cell->get_hash(i), loaded_cell->get_hash(i)); + } + ASSERT_EQ(loaded_cell->get_hash(cell->get_level()), loaded_cell->get_hash()); + loaded_cells.push_back(std::move(loaded_cell)); + } + + auto new_serialized = serialize_boc(std::move(loaded_cells), mode); + ASSERT_EQ(serialized, new_serialized); + return td::Status::OK(); +} + +template +td::Status test_boc_deserializer_threads(Ref cell, int mode, td::Random::Xorshift128plus &rnd, + size_t threads_n = 4) { + auto serialized = serialize_boc(cell, mode); + CHECK(serialized.size() != 0); + std::vector explorations; + for (size_t i = 0; i < threads_n; i++) { + explorations.push_back(CellExplorer::random_explore(cell, rnd)); + } + TRY_RESULT(boc_deserializer, BocDeserializerT::create(serialized)); + TRY_RESULT(root_count, boc_deserializer->get_root_count()); + ASSERT_EQ(1u, root_count); + TRY_RESULT(loaded_cell, boc_deserializer->get_root_cell(0)); + std::vector threads; + for (auto &exploration : explorations) { + threads.emplace_back([&] { + auto exploration2 = CellExplorer::explore(loaded_cell, exploration.ops); + ASSERT_EQ(exploration.log, exploration2.log); + }); + } + for (auto &thread : threads) { + thread.join(); + } + + return td::Status::OK(); +} + +td::Status test_boc_deserializer_full(std::vector> cells) { + for (auto mode : get_serialization_modes()) { + TRY_STATUS(vm::test_boc_deserializer(cells, mode)); + TRY_STATUS(vm::test_boc_deserializer(cells, mode)); + } + return td::Status::OK(); +} +td::Status test_boc_deserializer_full(Ref cell) { + return test_boc_deserializer_full(std::vector>{std::move(cell)}); +} + +template +void test_boc_deserializer() { + td::Random::Xorshift128plus rnd{123}; + for (int t = 0; t < 1000; t++) { + auto cells = gen_random_cells(rnd.fast(1, 10), static_cast(rnd() % 1000 + 1), rnd); + for (auto mode : get_serialization_modes()) { + test_boc_deserializer(cells, mode).ensure(); + } + } +} + +TEST(TonDb, BocDeserializerBaseline) { + test_boc_deserializer(); +} + +TEST(TonDb, BocDeserializerSimple) { + test_boc_deserializer(); +} + +template +void test_boc_deserializer_threads() { + td::Random::Xorshift128plus rnd{123}; + for (int t = 0; t < 20; t++) { + auto cell = gen_random_cell(static_cast(rnd() % 1000 + 1), rnd); + for (auto mode : get_serialization_modes()) { + test_boc_deserializer_threads(cell, mode, rnd).ensure(); + } + } +} + +TEST(TonDb, BocDeserializerSimpleThreads) { + test_boc_deserializer_threads(); +} + +class CompactArray { + public: + CompactArray(size_t size) { + root_ = create(size, 0); + size_ = size; + } + CompactArray(size_t size, Ref root) { + root_ = std::move(root); + size_ = size; + } + CompactArray(td::Span span) { + root_ = create(span); + size_ = span.size(); + } + CompactArray(CompactArray &&other) = default; + CompactArray &operator=(CompactArray &&other) = default; + + td::Slice hash() const { + return root()->get_hash().as_slice(); + } + void set(size_t pos, td::uint64 value) { + root_ = set(root_, size_, pos, value); + } + td::uint64 get(size_t pos) { + return get(root_, size_, pos, nullptr); + } + + const Ref &root() const { + return root_; + } + size_t size() const { + return size_; + } + + Ref merkle_proof(std::vector keys) { + std::set hashes; + for (auto key : keys) { + get(root_, size_, key, &hashes); + } + + auto is_prunned = [&](const Ref &cell) { return hashes.count(cell->get_hash()) == 0; }; + return MerkleProof::generate_raw(root_, is_prunned); + } + + private: + Ref root_; + size_t size_; + + static Ref create_list(td::uint64 value) { + CellBuilder cb; + cb.store_long(value, 64); + return cb.finalize(); + } + static Ref create_node(Ref left, Ref right) { + CellBuilder cb; + cb.store_ref(std::move(left)); + cb.store_ref(std::move(right)); + return cb.finalize(); + } + static Ref create(size_t size, td::uint64 value) { + if (size == 1) { + return create_list(value); + } + return create_node(create(size / 2, value), create((size + 1) / 2, value)); + } + static Ref create(td::Span value) { + if (value.size() == 1) { + return create_list(value[0]); + } + return create_node(create(value.substr(0, value.size() / 2)), create(value.substr(value.size() / 2))); + } + + static td::uint64 get(Ref any_cell, size_t size, size_t pos, std::set *hashes) { + if (hashes) { + hashes->insert(any_cell->get_hash()); + } + CellSlice cs(NoVm(), any_cell); + assert(pos < size); + if (size == 1) { + return cs.fetch_long(64); + } + auto left = cs.fetch_ref(); + if (pos < size / 2) { + return get(left, size / 2, pos, hashes); + } + pos -= size / 2; + auto right = cs.fetch_ref(); + return get(right, (size + 1) / 2, pos, hashes); + } + + static Ref set(Ref any_cell, size_t size, size_t pos, td::uint64 value) { + CellSlice cs(NoVm(), any_cell); + assert(pos < size); + if (size == 1) { + return create_list(value); + } + //LOG(ERROR) << cell->size_refs() << " " << cell->size_bits(); + auto left = cs.fetch_ref(); + auto right = cs.fetch_ref(); + if (pos < size / 2) { + left = set(left, size / 2, pos, value); + } else { + pos -= size / 2; + right = set(right, (size + 1) / 2, pos, value); + } + return create_node(left, right); + } +}; + +class FastCompactArray { + public: + FastCompactArray(size_t size) : v_(size) { + } + void set(size_t pos, td::uint64 value) { + v_.at(pos) = value; + } + td::uint64 get(size_t pos) { + return v_.at(pos); + } + td::Span as_span() const { + return v_; + } + + private: + std::vector v_; +}; + +TEST(Cell, BocHands) { + serialize_boc(CellBuilder{}.store_bytes("AAAAAAAA").finalize()); + auto a = CellBuilder{}.store_bytes("abcd").store_ref(CellBuilder{}.store_bytes("???").finalize()).finalize(); + a = CellBuilder{} + .store_bits("XXX", 3) + .store_ref(CellBuilder::create_pruned_branch(std::move(a), Cell::max_level)) + .finalize(); + auto serialized = serialize_boc(a); + deserialize_boc(serialized); + deserialize_boc(serialize_boc(std::vector>{a, a})); + + // CHECK backward compatibility with + // serialized_boc_idx and serialized_boc_idx_crc32c + //auto serialized_idx_crc_x = serialize_boc(a, BagOfCells::WithIndex | BagOfCells::WithCRC32C); + //LOG(ERROR) << td::format::escaped(serialized_idx_crc_x); + std::string serialized_idx_crc = + td::Slice( + "\254\303\247(\001\001\002\001\000*\004*\201\001P\001\210H\001\004\024\271\313\264\253\277\265\350dN\250{," + "\372\021\012:I\354\322|\255\245\330\204+&\345\214\026\300\064\000\001\032\231\063\274") + .str(); + //auto serialized_idx_x = serialize_boc(a, BagOfCells::WithIndex); + //LOG(ERROR) << td::format::escaped(serialized_idx_x); + std::string serialized_idx = + td::Slice( + "h\377e\363\001\001\002\001\000*\004*\201\001P\001\210H\001\004\024\271\313\264\253\277\265\350dN\250{," + "\372\021\012:I\354\322|\255\245\330\204+&\345\214\026\300\064\000\001") + .str(); + + ASSERT_EQ(serialized, serialize_boc(deserialize_boc(serialized_idx))); + ASSERT_EQ(serialized, serialize_boc(deserialize_boc(serialized_idx_crc))); +} + +TEST(Cell, MerkleProofHands) { + // data has a reference, because we do not prune lists + auto data = CellBuilder{}.store_bytes("pruned data").store_ref(CellBuilder{}.finalize()).finalize(); + auto prunned_data = CellBuilder::create_pruned_branch(data, data->get_level() + 1); + ASSERT_EQ(1u, prunned_data->get_level()); + ASSERT_EQ(prunned_data->get_hash(0), data->get_hash(0)); + ASSERT_EQ(data->get_hash(0), data->get_hash(1)); + ASSERT_TRUE(prunned_data->get_hash(1) != prunned_data->get_hash(0)); + + auto node = CellBuilder{}.store_bytes("protected data").store_ref(data).finalize(); + auto proof = CellBuilder{}.store_bits(node->get_data(), node->get_bits()).store_ref(prunned_data).finalize(); + ASSERT_EQ(0u, node->get_level()); + ASSERT_EQ(1u, proof->get_level()); + ASSERT_EQ(proof->get_hash(0), node->get_hash(0)); + ASSERT_TRUE(proof->get_hash(1) != node->get_hash(1)); + test_boc_deserializer_full(proof).ensure(); + + auto merkle_proof = CellBuilder::create_merkle_proof(proof); + ASSERT_EQ(0u, merkle_proof->get_level()); + test_boc_deserializer_full(merkle_proof).ensure(); + + { + auto virtual_node = proof->virtualize({0, 1}); + ASSERT_EQ(0u, virtual_node->get_level()); + ASSERT_EQ(1u, virtual_node->get_virtualization()); + CellSlice cs{NoVm(), virtual_node}; + auto virtual_data = cs.fetch_ref(); + ASSERT_EQ(0u, virtual_data->get_level()); + ASSERT_EQ(1u, virtual_data->get_virtualization()); + ASSERT_EQ(data->get_hash(), virtual_data->get_hash()); + + auto virtual_node_copy = + CellBuilder{}.store_bits(node->get_data(), node->get_bits()).store_ref(virtual_data).finalize(); + ASSERT_EQ(0u, virtual_node_copy->get_level()); + ASSERT_EQ(1u, virtual_node_copy->get_virtualization()); + ASSERT_EQ(virtual_node->get_hash(), virtual_node_copy->get_hash()); + + { + auto two_nodes = CellBuilder{}.store_ref(virtual_node).store_ref(node).finalize(); + ASSERT_EQ(0u, two_nodes->get_level()); + ASSERT_EQ(1u, two_nodes->get_virtualization()); + CellSlice cs2(NoVm(), two_nodes); + ASSERT_EQ(1u, cs2.prefetch_ref(0)->get_virtualization()); + ASSERT_EQ(0u, cs2.prefetch_ref(1)->get_virtualization()); + } + } + LOG(ERROR) << td::NamedThreadSafeCounter::get_default(); +} + +TEST(Cell, MerkleProofArrayHands) { + // create simple array + CompactArray arr(17); + for (size_t i = 0; i < arr.size(); i++) { + arr.set(i, i / 3); + } + + // create merke proof for 4 5 6 and 16th elements + std::vector keys = {4, 5, 6, 16}; + auto proof = arr.merkle_proof(keys); + + ASSERT_EQ(1u, proof->get_level()); + ASSERT_EQ(proof->get_hash(0), arr.root()->get_hash(0)); + ASSERT_TRUE(proof->get_hash(1) != arr.root()->get_hash(1)); + ASSERT_EQ(arr.root()->get_hash(0), arr.root()->get_hash(1)); + + CompactArray new_arr(arr.size(), proof->virtualize({0, 1})); + for (auto k : keys) { + ASSERT_EQ(arr.get(k), new_arr.get(k)); + } + test_boc_deserializer_full(proof).ensure(); + test_boc_deserializer_full(CellBuilder::create_merkle_proof(proof)).ensure(); +} +TEST(Cell, MerkleUpdateHands) { + auto data = CellBuilder{}.store_bytes("pruned data").store_ref(CellBuilder{}.finalize()).finalize(); + auto node = CellBuilder{}.store_bytes("protected data").store_ref(data).finalize(); + auto other_node = CellBuilder{}.store_bytes("other protected data").store_ref(data).finalize(); + auto usage_tree = std::make_shared(); + auto other_usage_tree = std::make_shared(); + auto usage_cell = UsageCell::create(node, usage_tree->root_ptr()); + auto child = CellSlice(vm::NoVm(), usage_cell).prefetch_ref(0); + auto new_node = CellBuilder{}.store_bytes("new data").store_ref(child).finalize(); + auto new_child = CellSlice(vm::NoVm(), new_node).prefetch_ref(0); + auto update = MerkleUpdate::generate(usage_cell, new_node, usage_tree.get()); + + MerkleUpdate::may_apply(node, update).ensure(); + MerkleUpdate::validate(update).ensure(); + auto x = MerkleUpdate::apply(node, update); + ASSERT_TRUE(serialize_boc(new_node) == serialize_boc(x)); + + MerkleUpdate::may_apply(other_node, update).ensure_error(); + ASSERT_TRUE(MerkleUpdate::apply(other_node, update).is_null()); + auto other_update = CellBuilder::create_merkle_update(CellBuilder::create_pruned_branch(other_node, 1), + CellBuilder::create_pruned_branch(new_node, 1)); + MerkleUpdate::may_apply(node, other_update).ensure_error(); + MerkleUpdate::validate(other_update).ensure_error(); + ASSERT_TRUE(MerkleUpdate::apply(other_node, other_update).is_null()); + auto bad_update = CellBuilder::create_merkle_update(CellBuilder::create_pruned_branch(new_node, 1), + CellBuilder::create_pruned_branch(other_node, 1)); + CHECK(MerkleUpdate::combine(update, bad_update).is_null()); +} + +TEST(Cell, MerkleUpdateArray) { + // create simple array + size_t n = 1 << 20; + std::vector data; + for (size_t i = 0; i < n; i++) { + data.push_back(i / 3); + } + CompactArray arr(data); + auto root = arr.root(); + auto usage_tree = std::make_shared(); + auto usage_cell = UsageCell::create(root, usage_tree->root_ptr()); + arr = CompactArray(n, usage_cell); + arr.set(n / 2, 0); + arr.set(n / 2 + 1, 1); + arr.set(n / 2 + 2, 2414221111); + arr.set(n / 2 + 3, 2); + + auto update = MerkleUpdate::generate(usage_cell, arr.root(), usage_tree.get()); + CellStorageStat stat; + stat.compute_used_storage(update, false); + ASSERT_EQ(stat.cells, 81u); + //CellSlice(NoVm(), update).print_rec(std::cerr); + + check_merkle_update(root, arr.root(), update); +} + +TEST(Cell, MerkleUpdateCombineArray) { + size_t n = 1 << 10; + std::vector data; + for (size_t i = 0; i < n; i++) { + data.push_back(i / 3); + } + CompactArray arr(data); + auto from = arr.root(); + std::shared_ptr usage_tree; + Ref usage_cell; + + std::vector> updates; + + auto apply_op = [&](auto op) { + auto A = arr.root(); + usage_tree = std::make_shared(); + usage_cell = UsageCell::create(arr.root(), usage_tree->root_ptr()); + arr = CompactArray(n, usage_cell); + op(); + updates.push_back(MerkleUpdate::generate(A, arr.root(), usage_tree.get())); + }; + + auto combine_all = [&]() { + while (updates.size() > 1) { + size_t i = updates.size() - 2; + updates[i] = MerkleUpdate::combine(updates[i], updates[i + 1]); + updates.pop_back(); + CellStorageStat stat; + stat.compute_used_storage(updates[i], false); + } + }; + auto validate = [&](size_t size) { + combine_all(); + check_merkle_update(from, arr.root(), updates.at(0)); + CellStorageStat stat; + stat.compute_used_storage(updates[0], false); + if (size != 0) { + ASSERT_EQ(size, stat.cells); + } + }; + apply_op([] {}); + validate(3); + apply_op([] {}); + apply_op([] {}); + apply_op([] {}); + validate(3); + + apply_op([&] { + for (size_t i = 0; i < n; i++) { + arr.set(i, i / 3 + 10); + } + }); + apply_op([&] { + for (size_t i = 0; i < n; i++) { + arr.set(i, i / 3); + } + }); + validate(3); + + for (size_t i = 0; i + 1 < n; i++) { + apply_op([&] { + arr.set(i, i / 3 + 1); + if (i != 0) { + arr.set(i - 1, (i - 1) / 3); + } + }); + } + + validate(41); +} + +} // namespace vm + +class BenchBocSerializerImport : public td::Benchmark { + public: + BenchBocSerializerImport() { + std::vector v(array_size); + td::Random::Xorshift128plus rnd{123}; + for (auto &x : v) { + x = rnd(); + } + arr = vm::CompactArray(v); + //serialization_ = td::BufferSlice(boc.serialize_to_string(15)); + } + std::string get_description() const override { + return "BenchBocSerializer"; + } + + void run(int n) override { + for (int i = 0; i < n; i++) { + vm::BagOfCells boc; + boc.add_root(arr.root()); + boc.import_cells().ensure(); + } + } + + private: + td::BufferSlice serialization_; + static constexpr td::uint32 array_size = 1024; + vm::CompactArray arr{1}; +}; + +class BenchBocSerializerSerialize : public td::Benchmark { + public: + BenchBocSerializerSerialize() { + std::vector v(array_size); + td::Random::Xorshift128plus rnd{123}; + for (auto &x : v) { + x = rnd(); + } + arr = vm::CompactArray(v); + boc.add_root(arr.root()); + boc.import_cells().ensure(); + } + std::string get_description() const override { + return "BenchBocSerializer"; + } + + void run(int n) override { + for (int i = 0; i < n; i++) { + boc.serialize_to_string(31); + } + } + + private: + td::BufferSlice serialization_; + static constexpr td::uint32 array_size = 1024; + vm::CompactArray arr{1}; + vm::BagOfCells boc; +}; + +struct BenchBocDeserializerConfig { + enum BlobType { File, Memory, FileMemoryMap, RocksDb } blob_type; + int k{100}; + enum Mode { Prefix, Range, Random } mode{Random}; + bool with_index{true}; + int threads_n{1}; +}; + +td::StringBuilder &operator<<(td::StringBuilder &sb, const BenchBocDeserializerConfig &config) { + sb << "load from "; + switch (config.blob_type) { + case BenchBocDeserializerConfig::File: + sb << "file"; + break; + case BenchBocDeserializerConfig::Memory: + sb << "memory"; + break; + case BenchBocDeserializerConfig::FileMemoryMap: + sb << "file mmap"; + break; + case BenchBocDeserializerConfig::RocksDb: + sb << "rocksdb"; + break; + } + sb << td::tag("k", config.k) << " "; + switch (config.mode) { + case BenchBocDeserializerConfig::Prefix: + sb << "prefix"; + break; + case BenchBocDeserializerConfig::Range: + sb << "range"; + break; + case BenchBocDeserializerConfig::Random: + sb << "random"; + break; + } + sb << " " << (config.with_index ? "with" : "without") << " index"; + sb << " " << config.threads_n << " threads"; + return sb; +} + +template +class BenchBocDeserializer : public td::Benchmark { + public: + BenchBocDeserializer(std::string name, BenchBocDeserializerConfig config) : name_(std::move(name)), config_(config) { + fast_array_ = vm::FastCompactArray(array_size); + td::Random::Xorshift128plus rnd{123}; + for (td::uint32 i = 0; i < array_size; i++) { + auto val = rnd(); + fast_array_.set(i, val); + } + vm::CompactArray arr(fast_array_.as_span()); + auto db_path = "serialization_rocksdb"; + if (config.blob_type == BenchBocDeserializerConfig::RocksDb) { + { + td::RocksDb::destroy(td::Slice(db_path)).ensure(); + auto db = vm::TonDbImpl::open(td::Slice(db_path)).move_as_ok(); + auto txn = db->begin_transaction(); + auto smt = txn->begin_smartcontract(); + SCOPE_EXIT { + db->commit_transaction(std::move(txn)); + }; + SCOPE_EXIT { + txn->commit_smartcontract(std::move(smt)); + }; + smt->set_root(arr.root()); + } + db_ = vm::TonDbImpl::open(td::Slice(db_path)).move_as_ok(); + } else { + serialization_ = td::BufferSlice(serialize_boc( + arr.root(), vm::BagOfCells::WithIntHashes | vm::BagOfCells::WithTopHash | + (config.with_index ? vm::BagOfCells::WithIndex | vm::BagOfCells::WithCacheBits : 0))); + + if (config.blob_type == BenchBocDeserializerConfig::File || + config.blob_type == BenchBocDeserializerConfig::FileMemoryMap) { + td::unlink("serialization").ignore(); + td::write_file("serialization", serialization_.as_slice()).ensure(); + } + } + root_ = arr.root(); + } + std::string get_description() const override { + return PSTRING() << "BocDeserializer " << name_ << " " << config_; + } + + vm::Ref load_root() { + if (config_.blob_type == BenchBocDeserializerConfig::RocksDb) { + auto txn = db_->begin_transaction(); + auto smt = txn->begin_smartcontract(); + SCOPE_EXIT { + db_->abort_transaction(std::move(txn)); + }; + SCOPE_EXIT { + txn->commit_smartcontract(std::move(smt)); + }; + LOG(ERROR) << "load root from rocksdb"; + return smt->get_root(); + } + auto blob = [&] { + switch (config_.blob_type) { + case BenchBocDeserializerConfig::File: + return vm::FileBlobView::create("serialization").move_as_ok(); + case BenchBocDeserializerConfig::Memory: + return vm::BufferSliceBlobView::create(serialization_.clone()); + case BenchBocDeserializerConfig::FileMemoryMap: + return vm::FileMemoryMappingBlobView::create("serialization").move_as_ok(); + default: + UNREACHABLE(); + } + UNREACHABLE(); + }(); + auto boc_deserializer = DeserializerT::create(std::move(blob)).move_as_ok(); + ASSERT_EQ(1u, boc_deserializer->get_root_count().move_as_ok()); + return boc_deserializer->get_root_cell(0).move_as_ok(); + } + + void run(int n) override { + td::Random::Xorshift128plus rnd{123}; + + std::vector threads; + //TODO: use config.k + auto K = config_.k == 0 ? n : config_.k; + td::Stage stage; + vm::Ref root; + for (int t = 0; t < config_.threads_n; t++) { + threads.emplace_back([&, seed = rnd(), thread_i = t] { + for (int round_i = 0; round_i < n / K; round_i++) { + if (thread_i == 0) { + root = load_root(); + } + stage.wait(config_.threads_n * (2 * round_i + 1)); + + vm::CompactArray array(array_size, root); + td::Random::Xorshift128plus rnd{seed}; + td::uint64 start_pos = + config_.mode == BenchBocDeserializerConfig::Range ? array_size / config_.threads_n * thread_i : 0; + for (int k = 0; k < K; k++) { + auto pos = start_pos; + switch (config_.mode) { + case BenchBocDeserializerConfig::Prefix: + case BenchBocDeserializerConfig::Range: + pos = (pos + k) % array_size; + break; + case BenchBocDeserializerConfig::Random: + pos = rnd() % array_size; + break; + } + ASSERT_EQ(fast_array_.get(td::narrow_cast(pos)), array.get(td::narrow_cast(pos))); + } + stage.wait(config_.threads_n * (2 * round_i + 2)); + } + }); + } + for (auto &thread : threads) { + thread.join(); + } + } + + private: + std::string name_; + td::BufferSlice serialization_; + BenchBocDeserializerConfig config_; + static constexpr td::uint32 array_size = 1024 * 1024; + vm::FastCompactArray fast_array_{array_size}; + vm::Ref root_; + vm::TonDb db_; +}; + +TEST(TonDb, BenchBocSerializerImport) { + if (0) { + BenchBocSerializerImport b; + while (true) { + td::bench_n(b, 1000000); + } + } + td::bench(BenchBocSerializerImport()); +} +TEST(TonDb, BenchBocSerializerSerialize) { + td::bench(BenchBocSerializerSerialize()); +} + +template +void bench_deserializer(std::string name, bool full) { + using Config = BenchBocDeserializerConfig; + if (full) { + for (auto k : {1, 10, 100}) { + for (auto with_index : {false, true}) { + for (auto mode : {Config::Prefix, Config::Range, Config::Random}) { + for (auto blob_type : {Config::Memory, Config::File, Config::FileMemoryMap}) { + BenchBocDeserializerConfig config; + config.k = k; + config.with_index = with_index; + config.mode = mode; + config.blob_type = blob_type; + td::bench(BenchBocDeserializer(name, config)); + } + } + } + } + } else { + td::bench(BenchBocDeserializer(name, BenchBocDeserializerConfig())); + } +} +template +void bench_deserializer_threads(std::string name) { + using Config = BenchBocDeserializerConfig; + for (auto threads_n : {1, 4, 16}) { + //for (auto threads_n : {16}) { + //for (auto with_index : {false, true}) { + //for (auto mode : {BenchBocDeserializerConfig::Prefix, BenchBocDeserializerConfig::Range, + //BenchBocDeserializerConfig::Random}) { + //for (auto from_file : {false, true}) { + BenchBocDeserializerConfig config; + config.threads_n = threads_n; + config.k = 0; + config.with_index = true; + config.mode = Config::Random; + config.mode = Config::Range; + config.mode = Config::Prefix; + config.blob_type = Config::Memory; + td::bench(BenchBocDeserializer(name, config)); + //td::bench_n(BenchBocThreadsDeserializer(name, config), 1000000); + //} + //} + //} + } +} + +TEST(TonDb, BenchBocThreadsDeserializerSimple) { + //td::bench_n(BenchBocDeserializer("simple", BenchBocDeserializerConfig()), 1000000); + //std::exit(0); + bench_deserializer_threads("simple"); +} +TEST(TonDb, BenchBocDeserializerSimple) { + //td::bench_n(BenchBocDeserializer("simple", BenchBocDeserializerConfig()), 1000000); + //std::exit(0); + bench_deserializer("simple", false); +} +TEST(TonDb, BenchBocDeserializerBaseline) { + //td::bench_n(BenchBocDeserializer("baseline", BenchBocDeserializerConfig()), 1000000); + //std::exit(0); + bench_deserializer("baseline", false); +} +TEST(TonDb, BenchBocDeserializerRocksDb) { + //td::bench_n(BenchBocDeserializer("baseline", BenchBocDeserializerConfig()), 1000000); + //std::exit(0); + auto config = BenchBocDeserializerConfig(); + config.blob_type = BenchBocDeserializerConfig::RocksDb; + config.threads_n = 4; + config.k = 0; + td::bench(BenchBocDeserializer("rockdb", config)); +} + +TEST(TonDb, CompactArray) { + SET_VERBOSITY_LEVEL(VERBOSITY_NAME(ERROR)); + td::Slice db_path = "compact_array_db"; + td::RocksDb::destroy(db_path).ensure(); + + td::Random::Xorshift128plus rnd(123); + + auto next_array_size = [&rnd]() { + static std::vector array_sizes = {1, 2, 4, 10, 37, 100, 1000, 10000}; + return array_sizes[rnd() % array_sizes.size()]; + }; + + vm::CompactArray array(2); + vm::FastCompactArray fast_array(2); + auto next_pos = [&] { return static_cast(rnd() % array.size()); }; + + auto db = vm::TonDbImpl::open(db_path).move_as_ok(); + auto txn = db->begin_transaction(); + auto smt = txn->begin_smartcontract(); + SCOPE_EXIT { + db->commit_transaction(std::move(txn)); + }; + SCOPE_EXIT { + txn->commit_smartcontract(std::move(smt)); + }; + + auto flush_to_db = [&] { + if (rnd() % 10 != 0) { + return; + } + bool restart_db = rnd() % 20 == 0; + bool reload_array = rnd() % 5 == 0; + smt->set_root(array.root()); + txn->commit_smartcontract(std::move(smt)); + db->commit_transaction(std::move(txn)); + if (restart_db) { + db->clear_cache(); + //db.reset(); + //db = vm::TonDbImpl::open(db_path).move_as_ok(); + } + txn = db->begin_transaction(); + smt = txn->begin_smartcontract(); + smt->validate_meta().ensure(); + ASSERT_EQ(smt->get_root()->get_hash(), array.root()->get_hash()); + if (reload_array) { + auto size = array.size(); + array = vm::CompactArray(size, smt->get_root()); + } + }; + + auto do_validate = [&](size_t pos) { ASSERT_EQ(array.get(pos), fast_array.get(pos)); }; + auto validate = [&] { do_validate(next_pos()); }; + auto validate_full = [&] { + for (size_t pos = 0; pos < array.size(); pos++) { + do_validate(pos); + } + }; + + auto set_value = [&] { + auto pos = static_cast(rnd() % array.size()); + auto value = rnd() % 3; + array.set(pos, value); + fast_array.set(pos, value); + }; + + auto reset_array = [&] { + auto size = next_array_size(); + array = vm::CompactArray(size); + fast_array = vm::FastCompactArray(size); + }; + + RandomSteps steps({{reset_array, 1}, {set_value, 1000}, {validate, 10}, {validate_full, 2}, {flush_to_db, 1}}); + for (size_t t = 0; t < 100000; t++) { + if (t % 10000 == 0) { + LOG(ERROR) << t; + } + steps.step(rnd); + } +}; + +TEST(TonDb, CompactArrayOld) { + SET_VERBOSITY_LEVEL(VERBOSITY_NAME(ERROR)); + using namespace vm; + //auto kv = std::make_unique(); + td::RocksDb::destroy("ttt").ensure(); + + auto ton_db = vm::TonDbImpl::open("ttt").move_as_ok(); + + //auto storage = std::make_unique(kv.get()); + + size_t array_size = 1000; + std::string hash; + td::Random::Xorshift128plus rnd(123); + FastCompactArray fast_array(array_size); + { + auto txn = ton_db->begin_transaction(); + SCOPE_EXIT { + ton_db->commit_transaction(std::move(txn)); + }; + auto smart = txn->begin_smartcontract(""); + SCOPE_EXIT { + txn->commit_smartcontract(std::move(smart)); + }; + CompactArray arr(array_size); + arr.set(array_size / 2, 124); + fast_array.set(array_size / 2, 124); + //for (size_t i = 0; i < array_size; i++) { + //int x = rnd() % 2; + //arr.set(i, x); + //fast_array.set(i, x); + //} + smart->set_root(arr.root()); + LOG(ERROR) << smart->get_root()->get_hash().to_hex(); + } + //LOG(ERROR) << "OK"; + + for (int i = 0; i < 100; i++) { + if (i % 10 == 9) { + //LOG(ERROR) << ton_db->stat(); + ton_db.reset(); + ton_db = vm::TonDbImpl::open("ttt").move_as_ok(); + } + auto txn = ton_db->begin_transaction(); + SCOPE_EXIT { + ton_db->commit_transaction(std::move(txn)); + }; + auto smart = txn->begin_smartcontract(""); + //smart->validate_meta(); + SCOPE_EXIT { + txn->commit_smartcontract(std::move(smart)); + }; + if (i % 1000 == 0) { + LOG(ERROR) << "i = " << i; + } + CompactArray arr(array_size, smart->get_root()); + auto key = static_cast(rnd() % array_size); + auto value = rnd() % 2; + arr.set(key, value); + fast_array.set(key, value); + smart->set_root(arr.root()); + //LOG(ERROR) << storage->size(); + } + { + auto txn = ton_db->begin_transaction(); + SCOPE_EXIT { + ton_db->abort_transaction(std::move(txn)); + }; + auto smart = txn->begin_smartcontract(""); + SCOPE_EXIT { + txn->abort_smartcontract(std::move(smart)); + }; + + CompactArray arr(array_size, smart->get_root()); + for (size_t i = 0; i < array_size; i++) { + ASSERT_EQ(fast_array.get(i), arr.get(i)); + } + } +} + +TEST(TonDb, StackOverflow) { + try { + td::Ref cell = vm::CellBuilder().finalize(); + for (int i = 0; i < 10000000; i++) { + vm::CellBuilder cb; + cb.store_ref(std::move(cell)); + cell = cb.finalize(); + } + LOG(ERROR) << "A"; + vm::test_boc_deserializer({cell}, 31); + LOG(ERROR) << "B"; + vm::test_boc_deserializer({cell}, 31); + LOG(ERROR) << "C"; + } catch (...) { + } + + struct A : public td::CntObject { + explicit A(td::Ref next) : next(next) { + } + td::Ref next; + }; + { + td::Ref head; + for (int i = 0; i < 10000000; i++) { + td::Ref new_head = td::Ref(true, std::move(head)); + head = std::move(new_head); + } + } +} + +TEST(TonDb, BocRespectsUsageCell) { + td::Random::Xorshift128plus rnd(123); + auto cell = vm::gen_random_cell(20, rnd, true); + auto usage_tree = std::make_shared(); + auto usage_cell = vm::UsageCell::create(cell, usage_tree->root_ptr()); + auto serialization = serialize_boc(usage_cell); + auto proof = vm::MerkleProof::generate(cell, usage_tree.get()); + auto virtualized_proof = vm::MerkleProof::virtualize(proof, 1); + auto serialization_of_virtualized_cell = serialize_boc(virtualized_proof); + ASSERT_STREQ(serialization, serialization_of_virtualized_cell); +} + +TEST(TonDb, DynamicBocRespectsUsageCell) { + td::Random::Xorshift128plus rnd(123); + auto cell = vm::gen_random_cell(20, rnd, true); + auto usage_tree = std::make_shared(); + auto usage_cell = vm::UsageCell::create(cell, usage_tree->root_ptr()); + + auto kv = std::make_shared(); + auto dboc = vm::DynamicBagOfCellsDb::create(); + dboc->set_loader(std::make_unique(kv)); + dboc->inc(usage_cell); + { + vm::CellStorer cell_storer(*kv); + dboc->commit(cell_storer); + } + + auto proof = vm::MerkleProof::generate(cell, usage_tree.get()); + auto virtualized_proof = vm::MerkleProof::virtualize(proof, 1); + auto serialization_of_virtualized_cell = serialize_boc(virtualized_proof); + auto serialization = serialize_boc(cell); + ASSERT_STREQ(serialization, serialization_of_virtualized_cell); +} + +TEST(TonDb, DoNotMakeListsPrunned) { + auto cell = vm::CellBuilder().store_bytes("abc").finalize(); + auto is_prunned = [&](const td::Ref &cell) { return true; }; + auto proof = vm::MerkleProof::generate(cell, is_prunned); + auto virtualized_proof = vm::MerkleProof::virtualize(proof, 1); + ASSERT_TRUE(virtualized_proof->get_virtualization() == 0); +} + +TEST(TonDb, CellStat) { + td::Random::Xorshift128plus rnd(123); + bool with_prunned_branches = true; + for (int i = 0; i < 1000; i++) { + auto A = vm::gen_random_cell(100, rnd, with_prunned_branches); + td::Ref B, AB, B_proof; + std::shared_ptr usage_tree; + std::tie(B, AB, usage_tree) = gen_merkle_update(A, rnd, with_prunned_branches); + B_proof = vm::CellSlice(vm::NoVm(), AB).prefetch_ref(1); + + vm::CellStorageStat stat; + stat.add_used_storage(B); + + vm::NewCellStorageStat new_stat; + new_stat.add_cell({}); + new_stat.add_cell(B); + ASSERT_EQ(stat.cells, new_stat.get_stat().cells); + ASSERT_EQ(stat.bits, new_stat.get_stat().bits); + + vm::CellStorageStat proof_stat; + proof_stat.add_used_storage(B_proof); + + vm::NewCellStorageStat new_proof_stat; + new_proof_stat.add_proof(B, usage_tree.get()); + CHECK(new_proof_stat.get_stat().cells == 0); + CHECK(new_proof_stat.get_proof_stat().cells <= proof_stat.cells); + //CHECK(new_proof_stat.get_proof_stat().cells + new_proof_stat.get_proof_stat().external_refs >= proof_stat.cells); + + vm::NewCellStorageStat new_all_stat; + new_all_stat.add_cell_and_proof(B, usage_tree.get()); + CHECK(new_proof_stat.get_proof_stat() == new_all_stat.get_proof_stat()); + CHECK(new_stat.get_stat() == new_all_stat.get_stat()); + + stat.add_used_storage(A); + auto AB_stat = new_stat.get_stat() + const_cast(new_stat).tentative_add_cell(A); + new_stat.add_cell(A); + CHECK(AB_stat == new_stat.get_stat()); + ASSERT_EQ(stat.cells, new_stat.get_stat().cells); + ASSERT_EQ(stat.bits, new_stat.get_stat().bits); + + CHECK(usage_tree.unique()); + usage_tree.reset(); + td::Ref C, BC, C_proof; + std::shared_ptr usage_tree_B; + std::tie(C, BC, usage_tree_B) = gen_merkle_update(B, rnd, with_prunned_branches); + C_proof = vm::CellSlice(vm::NoVm(), BC).prefetch_ref(1); + + auto BC_proof_stat = new_proof_stat.get_proof_stat() + new_proof_stat.tentative_add_proof(C, usage_tree_B.get()); + new_proof_stat.add_proof(C, usage_tree_B.get()); + CHECK(BC_proof_stat == new_proof_stat.get_proof_stat()); + } +} + +struct String { + String() { + total_strings.add(1); + } + String(std::string str) : str(std::move(str)) { + total_strings.add(1); + } + ~String() { + total_strings.add(-1); + } + static td::ThreadSafeCounter total_strings; + std::string str; +}; + +td::ThreadSafeCounter String::total_strings; +TEST(Ref, AtomicRef) { + struct Node { + td::AtomicRefLockfree> name_; + char pad[64]; + }; + + int threads_n = 10; + std::vector nodes(threads_n); + std::vector threads(threads_n); + int thread_id = 0; + for (auto &thread : threads) { + thread = td::thread([&] { + for (int i = 0; i < 1000000; i++) { + auto &node = nodes[td::Random::fast(0, threads_n / 3 - 1)]; + auto name = node.name_.load(); + if (name.not_null()) { + CHECK(name->str == "one" || name->str == "twotwo"); + } + if (td::Random::fast(0, 5) == 0) { + auto new_string = td::Ref>{true, td::Random::fast(0, 1) == 0 ? "one" : "twotwo"}; + node.name_.store(std::move(new_string)); + } + } + }); + thread_id++; + } + for (auto &thread : threads) { + thread.join(); + } + nodes.clear(); + LOG(ERROR) << String::total_strings.sum(); +} + +//TEST(Tmp, Boc) { +//LOG(ERROR) << "A"; +//auto data = td::read_file("boc"); +//LOG(ERROR) << "B"; +//auto cell = vm::deserialize_boc(data.move_as_ok().as_slice()); +//vm::CellStorageStat stat; +//stat.add_used_storage(cell, false); +//LOG(ERROR) << stat.cells; +////LOG(ERROR) << "C"; +////auto new_data = vm::serialize_boc(cell); +////LOG(ERROR) << "D"; +//vm::test_boc_deserializer({cell}, 31); +//} diff --git a/crypto/test/test-ed25519-crypto.cpp b/crypto/test/test-ed25519-crypto.cpp new file mode 100644 index 00000000..85ae5149 --- /dev/null +++ b/crypto/test/test-ed25519-crypto.cpp @@ -0,0 +1,314 @@ +/* + This file is part of TON Blockchain source code. + + TON Blockchain is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + TON Blockchain is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with TON Blockchain. If not, see . + + In addition, as a special exception, the copyright holders give permission + to link the code of portions of this program with the OpenSSL library. + You must obey the GNU General Public License in all respects for all + of the code used other than OpenSSL. If you modify file(s) with this + exception, you may extend this exception to your version of the file(s), + but you are not obligated to do so. If you do not wish to do so, delete this + exception statement from your version. If you delete this exception statement + from all source files in the program, then also delete it here. + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include +#include +#include +#include + +#include "crypto/ellcurve/Ed25519.h" + +static void my_assert_impl(bool cond, const char* str, const char* file, int line) { + if (!cond) { + std::cerr << "Failed " << str << " in " << file << " at " << line << ".\n"; + } +} +#define my_assert(x) my_assert_impl(x, #x, __FILE__, __LINE__) + +void print_buffer(const unsigned char buffer[32]) { + for (int i = 0; i < 32; i++) { + char buff[4]; + sprintf(buff, "%02x", buffer[i]); + std::cout << buff; + } +} + +std::string buffer_to_hex(const unsigned char* buffer, std::size_t size = 32) { + const char* hex = "0123456789ABCDEF"; + std::string res(2 * size, '\0'); + for (std::size_t i = 0; i < size; i++) { + auto c = buffer[i]; + res[2 * i] = hex[c & 15]; + res[2 * i + 1] = hex[c >> 4]; + } + return res; +} + +// export of (17/12)G on twisted Edwards curve +unsigned char test_vector1[32] = {0xfc, 0xb7, 0x42, 0x1e, 0x26, 0xad, 0x1b, 0x17, 0xf6, 0xb1, 0x52, + 0x0c, 0xdb, 0x8a, 0x64, 0x7d, 0x28, 0xa7, 0x56, 0x69, 0xd4, 0xb6, + 0x0c, 0xec, 0x63, 0x72, 0x5e, 0xe6, 0x32, 0x4d, 0xf7, 0xe6}; + +unsigned char rfc7748_output[32] = { + 0x95, 0xcb, 0xde, 0x94, 0x76, 0xe8, 0x90, 0x7d, 0x7a, 0xad, 0xe4, 0x5c, 0xb4, 0xb8, 0x73, 0xf8, + 0x8b, 0x59, 0x5a, 0x68, 0x79, 0x9f, 0xa1, 0x52, 0xe6, 0xf8, 0xf7, 0x64, 0x7a, 0xac, 0x79, 0x57, +}; + +bool test_ed25519_impl(void) { + std::cout << "************** Testing Curve25519 / Ed25519 operations ************\n"; + auto& E = ellcurve::Curve25519(); + auto& Edw = ellcurve::Ed25519(); + arith::Bignum L = E.get_ell(); + my_assert(arith::is_prime(L)); + my_assert(L == Edw.get_ell()); + arith::ResidueRing Fl(L); + arith::Bignum s = Fl.frac(17, 12).extract(); + arith::Bignum t = Fl.frac(12, 17).extract(); + std::cout << "l = " << L << std::endl; + std::cout << "s = 17/12 mod l = " << s << std::endl; + std::cout << "t = 12/17 mod l = " << t << std::endl; + auto sG = E.power_gen_xz(s); + auto u_sG = sG.get_u(); + std::cout << "Curve25519 u(sG) = " << sG.get_u().extract() << std::endl; + std::cout << "Curve25519 y(sG) = " << sG.get_y().extract() << std::endl; + auto sG1 = Edw.power_gen(s); + std::cout << "Ed25519 u(sG) = " << sG1.get_u().extract() << std::endl; + std::cout << "Ed25519 y(sG) = " << sG1.get_y().extract() << std::endl; + std::cout << "Ed25519 x(sG) = " << sG1.get_x().extract() << std::endl; + my_assert(sG1.get_x().extract() != sG1.get_y().extract()); + my_assert(sG.get_u() == sG1.get_u()); + my_assert(sG.get_y() == sG1.get_y()); + + my_assert( + sG1.get_x().extract() == + arith::Bignum(arith::dec_string{"9227429025021714590777223519505276506601225973596506606120015751301699519597"})); + my_assert(sG1.get_y().extract() == + arith::Bignum( + arith::dec_string{"46572854587220149033453000581008590225032365765275643343836649812808016508924"})); + + auto sG2 = Edw.power_gen(s, true); + my_assert(sG1.get_u() == sG2.get_u()); + my_assert(sG1.get_y() == sG2.get_y()); + unsigned char buff[32]; + std::memset(buff, 0, 32); + my_assert(sG1.export_point(buff)); + std::cout << "sG export = " << buffer_to_hex(buff) << std::endl; + bool ok; + auto sG3 = Edw.import_point(buff, ok); + my_assert(ok); + my_assert(!std::memcmp(buff, test_vector1, 32)); + my_assert(sG3.get_u() == sG1.get_u()); + my_assert(sG2.get_x() == sG2.get_x()); + my_assert(sG2.get_y() == sG2.get_y()); + + auto stG = E.power_xz(u_sG, t); + std::cout << "Curve25519 u(stG) = " << stG.get_u().extract() << std::endl; + my_assert(stG.get_u().extract() == 9); + auto stG1 = Edw.power_point(sG1, t); + std::cout << "Ed25519 u(stG) = " << stG1.get_u().extract() << std::endl; + my_assert(stG1.get_u().extract() == 9); + stG1.normalize(); + my_assert(stG1.XY == Edw.get_base_point().XY); + my_assert(stG1.X == Edw.get_base_point().X); + my_assert(stG1.Y == Edw.get_base_point().Y); + my_assert(stG1.Z == Edw.get_base_point().Z); + auto stG2 = Edw.power_point(sG2, t, true); + my_assert(stG2.get_u().extract() == 9); + stG2.normalize(); + my_assert(stG2.XY == stG1.XY && stG2.X == stG1.X && stG2.Y == stG1.Y); + auto stG3 = Edw.power_point(sG3, t).normalize(); + auto stG4 = Edw.power_point(sG3, t, true).normalize(); + my_assert(stG3.XY == stG1.XY && stG3.X == stG1.X && stG3.Y == stG1.Y); + my_assert(stG4.XY == stG1.XY && stG4.X == stG1.X && stG4.Y == stG1.Y); + + // RFC7748 test vector + auto u = + arith::Bignum(arith::dec_string{"8883857351183929894090759386610649319417338800022198945255395922347792736741"}); + //u[255] = 0; + auto n = + arith::Bignum(arith::dec_string{"35156891815674817266734212754503633747128614016119564763269015315466259359304"}); + //n[255] = 0; n[254] = 1; + //n[0] = n[1] = n[2] = 0; + auto umodp = arith::Residue(u, E.get_base_ring()); + auto nP = E.power_xz(umodp, n); + std::cout << "u(P) = " << u.to_hex() << std::endl; + std::cout << "n = " << n.to_hex() << std::endl; + std::cout << "u(nP) = " << nP.get_u().extract().to_hex() << std::endl; + unsigned char buffer[32]; + std::memset(buffer, 0, 32); + nP.export_point_u(buffer); + std::cout << "u(nP) export = " << buffer_to_hex(buffer) << std::endl; + my_assert(!std::memcmp(buffer, rfc7748_output, 32)); + + std::cout << "********* ok\n\n"; + return true; +} + +unsigned char fixed_privkey[32] = "abacabadabacabaeabacabadabacaba"; +unsigned char fixed_pubkey[32] = {0x6f, 0x9e, 0x5b, 0xde, 0xce, 0x87, 0x21, 0xeb, 0x57, 0x37, 0xfb, + 0xb5, 0x92, 0x28, 0xba, 0x07, 0xf7, 0x88, 0x0f, 0x73, 0xce, 0x5b, + 0xfa, 0xa1, 0xb7, 0x15, 0x73, 0x03, 0xd4, 0x20, 0x1e, 0x74}; + +unsigned char rfc8032_secret_key1[32] = {0x9d, 0x61, 0xb1, 0x9d, 0xef, 0xfd, 0x5a, 0x60, 0xba, 0x84, 0x4a, + 0xf4, 0x92, 0xec, 0x2c, 0xc4, 0x44, 0x49, 0xc5, 0x69, 0x7b, 0x32, + 0x69, 0x19, 0x70, 0x3b, 0xac, 0x03, 0x1c, 0xae, 0x7f, 0x60}; + +unsigned char rfc8032_public_key1[32] = {0xd7, 0x5a, 0x98, 0x01, 0x82, 0xb1, 0x0a, 0xb7, 0xd5, 0x4b, 0xfe, + 0xd3, 0xc9, 0x64, 0x07, 0x3a, 0x0e, 0xe1, 0x72, 0xf3, 0xda, 0xa6, + 0x23, 0x25, 0xaf, 0x02, 0x1a, 0x68, 0xf7, 0x07, 0x51, 0x1a}; + +unsigned char rfc8032_signature1[64] = { + 0xe5, 0x56, 0x43, 0x00, 0xc3, 0x60, 0xac, 0x72, 0x90, 0x86, 0xe2, 0xcc, 0x80, 0x6e, 0x82, 0x8a, + 0x84, 0x87, 0x7f, 0x1e, 0xb8, 0xe5, 0xd9, 0x74, 0xd8, 0x73, 0xe0, 0x65, 0x22, 0x49, 0x01, 0x55, + 0x5f, 0xb8, 0x82, 0x15, 0x90, 0xa3, 0x3b, 0xac, 0xc6, 0x1e, 0x39, 0x70, 0x1c, 0xf9, 0xb4, 0x6b, + 0xd2, 0x5b, 0xf5, 0xf0, 0x59, 0x5b, 0xbe, 0x24, 0x65, 0x51, 0x41, 0x43, 0x8e, 0x7a, 0x10, 0x0b, +}; + +unsigned char rfc8032_secret_key2[32] = { + 0xc5, 0xaa, 0x8d, 0xf4, 0x3f, 0x9f, 0x83, 0x7b, 0xed, 0xb7, 0x44, 0x2f, 0x31, 0xdc, 0xb7, 0xb1, + 0x66, 0xd3, 0x85, 0x35, 0x07, 0x6f, 0x09, 0x4b, 0x85, 0xce, 0x3a, 0x2e, 0x0b, 0x44, 0x58, 0xf7, +}; + +unsigned char rfc8032_public_key2[32] = { + 0xfc, 0x51, 0xcd, 0x8e, 0x62, 0x18, 0xa1, 0xa3, 0x8d, 0xa4, 0x7e, 0xd0, 0x02, 0x30, 0xf0, 0x58, + 0x08, 0x16, 0xed, 0x13, 0xba, 0x33, 0x03, 0xac, 0x5d, 0xeb, 0x91, 0x15, 0x48, 0x90, 0x80, 0x25, +}; + +unsigned char rfc8032_message2[2] = {0xaf, 0x82}; + +unsigned char rfc8032_signature2[64] = { + 0x62, 0x91, 0xd6, 0x57, 0xde, 0xec, 0x24, 0x02, 0x48, 0x27, 0xe6, 0x9c, 0x3a, 0xbe, 0x01, 0xa3, + 0x0c, 0xe5, 0x48, 0xa2, 0x84, 0x74, 0x3a, 0x44, 0x5e, 0x36, 0x80, 0xd7, 0xdb, 0x5a, 0xc3, 0xac, + 0x18, 0xff, 0x9b, 0x53, 0x8d, 0x16, 0xf2, 0x90, 0xae, 0x67, 0xf7, 0x60, 0x98, 0x4d, 0xc6, 0x59, + 0x4a, 0x7c, 0x15, 0xe9, 0x71, 0x6e, 0xd2, 0x8d, 0xc0, 0x27, 0xbe, 0xce, 0xea, 0x1e, 0xc4, 0x0a, +}; + +bool test_ed25519_crypto() { + std::cout << "************** Testing Curve25519 / Ed25519 cryptographic primitives ************\n"; + crypto::Ed25519::PrivateKey PK1, PK2, PK3, PK4, PK5; + PK1.random_private_key(); + PK2.import_private_key(fixed_privkey); + unsigned char priv2_export[32]; + bool ok = PK1.export_private_key(priv2_export); + std::cout << "PK1 = " << ok << " " << buffer_to_hex(priv2_export) << std::endl; + my_assert(ok); + ok = PK2.export_private_key(priv2_export); + std::cout << "PK2 = " << ok << " " << buffer_to_hex(priv2_export) << std::endl; + my_assert(ok); + PK3.import_private_key(priv2_export); + std::cout << "PK3 = " << PK3.ok() << std::endl; + my_assert(PK3.ok()); + + unsigned char pub_export[32]; + ok = PK1.export_public_key(pub_export); + std::cout << "PubK1 = " << ok << " " << buffer_to_hex(pub_export) << std::endl; + my_assert(ok); + crypto::Ed25519::PublicKey PubK1(pub_export); + ok = PK2.export_public_key(pub_export); + std::cout << "PubK2 = " << ok << " " << buffer_to_hex(pub_export) << std::endl; + my_assert(ok); + my_assert(!std::memcmp(pub_export, fixed_pubkey, 32)); + crypto::Ed25519::PublicKey PubK2(pub_export); + ok = PK3.export_public_key(pub_export); + std::cout << "PubK3 = " << ok << " " << buffer_to_hex(pub_export) << std::endl; + my_assert(ok); + my_assert(!std::memcmp(pub_export, fixed_pubkey, 32)); + crypto::Ed25519::PublicKey PubK3(pub_export); + ok = PubK1.export_public_key(pub_export); + std::cout << "PubK1 = " << ok << " " << buffer_to_hex(pub_export) << std::endl; + my_assert(ok); + + unsigned char secret22[32]; + ok = PK2.compute_shared_secret(secret22, PubK3); + std::cout << "secret(PK2,PubK2)=" << ok << " " << buffer_to_hex(secret22) << std::endl; + my_assert(ok); + + unsigned char secret12[32], secret21[32]; + ok = PK1.compute_shared_secret(secret12, PubK3); + std::cout << "secret(PK1,PubK2)=" << ok << " " << buffer_to_hex(secret12) << std::endl; + my_assert(ok); + ok = PK2.compute_shared_secret(secret21, PubK1); + std::cout << "secret(PK2,PubK1)=" << ok << " " << buffer_to_hex(secret21) << std::endl; + my_assert(ok); + my_assert(!std::memcmp(secret12, secret21, 32)); + + // for (int i = 0; i < 1000; i++) { + // ok = PK1.compute_shared_secret(secret12, PubK3); + // my_assert(ok); + // ok = PK2.compute_shared_secret(secret21, PubK1); + // my_assert(ok); + // } + + unsigned char signature[64]; + ok = PK1.sign_message(signature, (const unsigned char*)"abc", 3); + std::cout << "PK1.signature=" << ok << " " << buffer_to_hex(signature, 64) << std::endl; + my_assert(ok); + + // signature[63] ^= 1; + ok = PubK1.check_message_signature(signature, (const unsigned char*)"abc", 3); + std::cout << "PubK1.check_signature=" << ok << std::endl; + my_assert(ok); + + PK4.import_private_key(rfc8032_secret_key1); + PK4.export_public_key(pub_export); + std::cout << "PK4.private_key = " << buffer_to_hex(rfc8032_secret_key1) << std::endl; + std::cout << "PK4.public_key = " << buffer_to_hex(pub_export) << std::endl; + my_assert(!std::memcmp(pub_export, rfc8032_public_key1, 32)); + ok = PK4.sign_message(signature, (const unsigned char*)"", 0); + std::cout << "PK4.signature('') = " << buffer_to_hex(signature, 64) << std::endl; + my_assert(ok); + my_assert(!std::memcmp(signature, rfc8032_signature1, 32)); + + PK5.import_private_key(rfc8032_secret_key2); + PK5.export_public_key(pub_export); + std::cout << "PK5.private_key = " << buffer_to_hex(rfc8032_secret_key2) << std::endl; + std::cout << "PK5.public_key = " << buffer_to_hex(pub_export) << std::endl; + my_assert(!std::memcmp(pub_export, rfc8032_public_key2, 32)); + ok = PK5.sign_message(signature, rfc8032_message2, 2); + std::cout << "PK5.signature('') = " << buffer_to_hex(signature, 64) << std::endl; + my_assert(ok); + my_assert(!std::memcmp(signature, rfc8032_signature2, 32)); + crypto::Ed25519::PublicKey PubK5(pub_export); + + // for (int i = 0; i < 10000; i++) { + // ok = PK5.sign_message (signature, rfc8032_message2, 2); + // my_assert (ok); + // } + // for (int i = 0; i < 10000; i++) { + // ok = PubK5.check_message_signature (signature, rfc8032_message2, 2); + // my_assert (ok); + // } + + unsigned char temp_pubkey[32]; + crypto::Ed25519::TempKeyGenerator TKG; // use one generator a lot of times + + TKG.create_temp_shared_secret(temp_pubkey, secret12, PubK1, (const unsigned char*)"abc", 3); + std::cout << "secret12=" << buffer_to_hex(secret12) << "; temp_pubkey=" << buffer_to_hex(temp_pubkey) << std::endl; + + PK1.compute_temp_shared_secret(secret21, temp_pubkey); + std::cout << "secret21=" << buffer_to_hex(secret21) << std::endl; + my_assert(!std::memcmp(secret12, secret21, 32)); + + std::cout << "********* ok\n\n"; + return true; +} + +int main(void) { + test_ed25519_impl(); + test_ed25519_crypto(); + return 0; +} diff --git a/crypto/test/vm.cpp b/crypto/test/vm.cpp new file mode 100644 index 00000000..72e2f8c6 --- /dev/null +++ b/crypto/test/vm.cpp @@ -0,0 +1,427 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "vm/continuation.h" +#include "vm/cp0.h" +#include "vm/dict.h" +#include "fift/utils.h" +#include "common/bigint.hpp" + +#include "td/utils/tests.h" +#include "td/utils/ScopeGuard.h" +#include "td/utils/StringBuilder.h" + +std::string run_vm(td::Ref cell) { + vm::init_op_cp0(); + vm::DictionaryBase::get_empty_dictionary(); + + class Logger : public td::LogInterface { + public: + void append(td::CSlice slice) override { + res.append(slice.data(), slice.size()); + } + std::string res; + }; + static Logger logger; + logger.res = ""; + td::set_log_fatal_error_callback([](td::CSlice message) { td::default_log_interface->append(logger.res); }); + vm::VmLog log{&logger, td::LogOptions::plain()}; + log.log_options.level = 4; + log.log_options.fix_newlines = true; + log.log_mask |= vm::VmLog::DumpStack; + + auto total_data_cells_before = vm::DataCell::get_total_data_cells(); + SCOPE_EXIT { + auto total_data_cells_after = vm::DataCell::get_total_data_cells(); + ASSERT_EQ(total_data_cells_before, total_data_cells_after); + }; + + vm::Stack stack; + try { + vm::run_vm_code(vm::load_cell_slice_ref(cell), stack, 0 /*flags*/, nullptr /*data*/, std::move(log) /*VmLog*/); + } catch (...) { + LOG(FATAL) << "catch unhandled exception"; + } + return logger.res; // must be a copy +} + +td::Ref to_cell(const unsigned char *buff, int bits) { + return vm::CellBuilder().store_bits(buff, bits, 0).finalize(); +} +void test_run_vm(td::Ref code) { + auto a = run_vm(code); + auto b = run_vm(code); + ASSERT_EQ(a, b); + REGRESSION_VERIFY(a); +} + +void test_run_vm(td::Slice code_hex) { + unsigned char buff[128]; + int bits = (int)td::bitstring::parse_bitstring_hex_literal(buff, sizeof(buff), code_hex.begin(), code_hex.end()); + CHECK(bits >= 0); + test_run_vm(to_cell(buff, bits)); +} + +TEST(VM, simple) { + test_run_vm("ABCBABABABA"); +} + +TEST(VM, memory_leak_old) { + test_run_vm("90787FDB3B"); +} + +TEST(VM, memory_leak) { + test_run_vm("90707FDB3B"); +} + +TEST(VM, bug_div_short_any) { + test_run_vm("6883FF73A98D"); +} +TEST(VM, assert_pfx_dict_lookup) { + test_run_vm("778B04216D73F43E018B04591277F473"); +} + +TEST(VM, assert_lookup_prefix) { + test_run_vm("78E58B008B028B04010000016D90ED5272F43A755D77F4A8"); +} + +TEST(VM, assert_code_not_null) { + test_run_vm("76ED40DE"); +} + +TEST(VM, bug_exec_dict_getnear) { + test_run_vm("8B048B00006D72F47573655F6D656D6D656D8B007F"); +} + +TEST(VM, bug_stack_overflow) { + test_run_vm("72A93AF8"); +} + +TEST(VM, assert_extract_minmax_key) { + test_run_vm("6D6DEB21807AF49C2180EB21807AF41C"); +} + +TEST(VM, memory_leak_new) { + test_run_vm("72E5ED40DB3603"); +} + +TEST(VM, unhandled_exception_1) { + test_run_vm("70EDA2ED00"); +} + +TEST(VM, unhandled_exception_2) { + // infinite loop now + // test_run_vm("EBEDB4"); +} + +TEST(VM, unhandled_exception_3) { + // infinite loop now + // test_run_vm("EBEDC0"); +} + +TEST(VM, unhandled_exception_4) { + test_run_vm("7F853EA1C8CB3E"); +} + +TEST(VM, unhandled_exception_5) { + test_run_vm("738B04016D21F41476A721F49F"); +} + +TEST(VM, bigint) { + td::StringBuilder sb({}, true); + + using word_t = td::BigIntInfo::word_t; + std::vector numbers{1, + -1, + 2, + -2, + 100, + -100, + std::numeric_limits::max(), + std::numeric_limits::min(), + std::numeric_limits::max() - 1, + std::numeric_limits::min() + 1}; + for (auto x : numbers) { + for (auto y : numbers) { + word_t a; + word_t b; + td::BigIntInfo::set_mul(&a, &b, x, y); + sb << "set_mul " << x << " * " << y << " = " << a << " " << b << "\n"; + td::BigIntInfo::add_mul(&a, &b, x, y); + sb << "add_mul " << x << " " << y << " = " << a << " " << b << "\n"; + td::BigIntInfo::sub_mul(&a, &b, x, y); + sb << "sub_mul " << x << " " << y << " = " << a << " " << b << "\n"; + } + } + auto base = td::BigIntInfo::Base; + std::vector lo_numbers{1, -1, 2, -2, 100, -100, base - 1, base - 2, -base + 1, -base + 2}; + for (auto x : numbers) { + for (auto y : lo_numbers) { + for (auto z : numbers) { + word_t a; + word_t b; + td::BigIntInfo::dbl_divmod(&a, &b, x, y, z); + sb << "dbl_divmod " << x << " " << y << " / " << z << " = " << a << " " << b << "\n"; + } + } + } + + REGRESSION_VERIFY(sb.as_cslice()); +} + +TEST(VM, report3_1) { + //WA: expect (1, 2, 6, 3) + td::Slice test1 = + R"A( +CONT:<{ +DEPTH +}> +3 SETNUMARGS +c0 POPCTR +1 INT +2 INT +3 INT +4 INT +5 INT +6 INT +4 RETURNARGS +)A"; + test_run_vm(fift::compile_asm(test1).move_as_ok()); +} + +TEST(VM, report3_2) { + td::Slice test1 = + R"A( +CONT:<{ +DEPTH +}> +2 SETNUMARGS +c0 POPCTR +1 INT +2 INT +3 INT +4 INT +2 RETARGS +)A"; + test_run_vm(fift::compile_asm(test1).move_as_ok()); +} + +TEST(VM, report3_3) { + // WA: expect (9) + td::Slice test1 = + R"A( +CONT:<{ + 8 INT +}> +c0 POPCTR +CONT:<{ + 9 INT +}> +c1 POPCTR +0 INT +BRANCH +)A"; + test_run_vm(fift::compile_asm(test1).move_as_ok()); +} + +TEST(VM, report3_4) { + td::Slice test1 = + R"A( +CONT:<{ +1 INT +2 INT +3 INT +2 RETARGS +}> +CALLX +ADD +)A"; + test_run_vm(fift::compile_asm(test1).move_as_ok()); +} + +TEST(VM, report3_6) { + // WA: expect StackOverflow + td::Slice test1 = + R"A( +10 INT +20 INT +30 INT +CONT:<{ + DEPTH + 40 INT + SWAP +}> +2 SETNUMARGS +3 1 CALLXARGS +)A"; + test_run_vm(fift::compile_asm(test1).move_as_ok()); +} + +//TEST(VM, report3_ce) { +//td::Slice test1 = +//R"A( +//s16 POP +//s16 PUSH +//s0 s16 XCHG +//)A"; +//test_run_vm(fift::compile_asm(test1).move_as_ok()); +//} + +TEST(VM, report3_int_overflow_1) { + td::Slice test1 = + R"A( +4 INT +16 INT +-115792089237316195423570985008687907853269984665640564039457584007913129639936 INT +MULDIVMOD +)A"; + test_run_vm(fift::compile_asm(test1).move_as_ok()); +} +TEST(VM, report3_int_overflow_2) { + td::Slice test1 = + R"A( +4 INT +16 INT +-115792089237316195423570985008687907853269984665640564039457584007913129639936 INT +MULDIVR +)A"; + test_run_vm(fift::compile_asm(test1).move_as_ok()); +} + +TEST(VM, report3_qnot) { + td::Slice test1 = + R"A( +PUSHNAN +QNOT +)A"; + test_run_vm(fift::compile_asm(test1).move_as_ok()); +} + +TEST(VM, report3_loop_1) { + //WA + td::Slice test1 = + R"A( +CONT:<{ + 2 INT +}> +ATEXITALT +CONT:<{ + 1 INT + RETALT + -1 INT +}> +AGAIN +3 INT +)A"; + test_run_vm(fift::compile_asm(test1).move_as_ok()); +} + +TEST(VM, report3_loop_2) { + //WA + td::Slice test1 = + R"A( +CONT:<{ + 2 INT +}> +ATEXITALT +CONT:<{ + 1 INT + RETALT + -1 INT +}> +UNTIL +3 INT +)A"; + test_run_vm(fift::compile_asm(test1).move_as_ok()); +} + +TEST(VM, report3_loop_3) { + //WA + td::Slice test1 = + R"A( +1 INT +CONT:<{ + UNTILEND + RET + -1 INT +}> +CALLX +)A"; + test_run_vm(fift::compile_asm(test1).move_as_ok()); +} + +TEST(VM, report3_loop_4) { + //WA + td::Slice test1 = + R"A( +CONT:<{ + 2 INT +}> +ATEXITALT +CONT:<{ + 1 INT + RETALT + -1 PUSHINT +}> +CONT:<{ + -1 INT +}> +WHILE +3 INT +)A"; + test_run_vm(fift::compile_asm(test1).move_as_ok()); +} +TEST(VM, report3_loop_5) { + //WA + td::Slice test1 = + R"A( +CONT:<{ + 1 INT + 2 INT +}> +ATEXITALT +3 INT +AGAINEND +DEC +DUP +IFRET +DROP +RETALT +)A"; + test_run_vm(fift::compile_asm(test1).move_as_ok()); +} + +TEST(VM, report3_loop_6) { + //WA + td::Slice test1 = + R"A( +CONT:<{ + 1 INT + 2 INT +}> +3 INT +AGAINEND +DEC +DUP +IFRET +DROP +ATEXITALT +RETALT +)A"; + test_run_vm(fift::compile_asm(test1).move_as_ok()); +} diff --git a/crypto/tl/boc.tlb b/crypto/tl/boc.tlb new file mode 100644 index 00000000..c296cb6f --- /dev/null +++ b/crypto/tl/boc.tlb @@ -0,0 +1,45 @@ +// +// Bag-of-Cells (BoC) serialization formats +// + +serialized_boc_idx#68ff65f3 size:(## 8) { size <= 4 } + off_bytes:(## 8) { off_bytes <= 8 } + cells:(##(size * 8)) + roots:(##(size * 8)) { roots = 1 } + absent:(##(size * 8)) { roots + absent <= cells } + tot_cells_size:(##(off_bytes * 8)) + index:(cells * ##(off_bytes * 8)) + cell_data:(tot_cells_size * [ uint8 ]) + = BagOfCells; + +serialized_boc_idx_crc32c#acc3a728 size:(## 8) { size <= 4 } + off_bytes:(## 8) { off_bytes <= 8 } + cells:(##(size * 8)) + roots:(##(size * 8)) { roots = 1 } + absent:(##(size * 8)) { roots + absent <= cells } + tot_cells_size:(##(off_bytes * 8)) + index:(cells * ##(off_bytes * 8)) + cell_data:(tot_cells_size * [ uint8 ]) + crc32c:uint32 = BagOfCells; + +serialized_boc#b5ee9c72 has_idx:(## 1) has_crc32c:(## 1) + has_cache_bits:(## 1) flags:(## 2) { flags = 0 } + size:(## 3) { size <= 4 } + off_bytes:(## 8) { off_bytes <= 8 } + cells:(##(size * 8)) + roots:(##(size * 8)) { roots >= 1 } + absent:(##(size * 8)) { roots + absent <= cells } + tot_cells_size:(##(off_bytes * 8)) + root_list:(roots * ##(size * 8)) + index:(cells * ##(off_bytes * 8)) + cell_data:(tot_cells_size * [ uint8 ]) + = BagOfCells; + +compiled_smart_contract + compiled_at:uint32 code:^Cell data:^Cell + description:(Maybe ^TinyString) + ^[ source_file:(Maybe ^TinyString) + compiler_version:(Maybe ^TinyString) ] + = CompiledSmartContract; + +tiny_string#_ len:(#<= 126) str:(len * [ uint8 ]) = TinyString; diff --git a/crypto/tl/hashmap.tlb b/crypto/tl/hashmap.tlb new file mode 100644 index 00000000..0ec55a65 --- /dev/null +++ b/crypto/tl/hashmap.tlb @@ -0,0 +1,60 @@ +bit#_ _:(## 1) = Bit; + +// ordinary Hashmap / HashmapE, with fixed length keys +// +hm_edge#_ {n:#} {X:Type} {l:#} {m:#} label:(HmLabel ~l n) + {n = (~m) + l} node:(HashmapNode m X) = Hashmap n X; + +hmn_leaf#_ {X:Type} value:X = HashmapNode 0 X; +hmn_fork#_ {n:#} {X:Type} left:^(Hashmap n X) + right:^(Hashmap n X) = HashmapNode (n + 1) X; + +hml_short$0 {m:#} {n:#} len:(Unary ~n) s:(n * Bit) = HmLabel ~n m; +hml_long$10 {m:#} n:(#<= m) s:(n * Bit) = HmLabel ~n m; +hml_same$11 {m:#} v:Bit n:(#<= m) = HmLabel ~n m; + +unary_zero$0 = Unary ~0; +unary_succ$1 {n:#} x:(Unary ~n) = Unary ~(n + 1); + +hme_empty$0 {n:#} {X:Type} = HashmapE n X; +hme_root$1 {n:#} {X:Type} root:^(Hashmap n X) = HashmapE n X; + +true#_ = True; +_ {n:#} _:(Hashmap n True) = BitstringSet n; + + +// VarHashmap / VarHashmapE, with variable-length keys +// +vhm_edge#_ {n:#} {X:Type} {l:#} {m:#} label:(HmLabel ~l n) + {n = (~m) + l} node:(VarHashmapNode m X) + = VarHashmap n X; +vhmn_leaf$00 {n:#} {X:Type} value:X = VarHashmapNode n X; +vhmn_fork$01 {n:#} {X:Type} left:^(VarHashmap n X) + right:^(VarHashmap n X) value:(Maybe X) + = VarHashmapNode (n + 1) X; +vhmn_cont$1 {n:#} {X:Type} branch:Bit child:^(VarHashmap n X) + value:X = VarHashmapNode (n + 1) X; + +nothing$0 {X:Type} = Maybe X; +just$1 {X:Type} value:X = Maybe X; + +vhme_empty$0 {n:#} {X:Type} = VarHashmapE n X; +vhme_root$1 {n:#} {X:Type} root:^(VarHashmap n X) + = VarHashmapE n X; + +// +// PfxHashmap / PfxHashmapE, with variable-length keys +// constituting a prefix code +// + +phm_edge#_ {n:#} {X:Type} {l:#} {m:#} label:(HmLabel ~l n) + {n = (~m) + l} node:(PfxHashmapNode m X) + = PfxHashmap n X; + +phmn_leaf$0 {n:#} {X:Type} value:X = PfxHashmapNode n X; +phmn_fork$1 {n:#} {X:Type} left:^(PfxHashmap n X) + right:^(PfxHashmap n X) = PfxHashmapNode (n + 1) X; + +phme_empty$0 {n:#} {X:Type} = PfxHashmapE n X; +phme_root$1 {n:#} {X:Type} root:^(PfxHashmap n X) + = PfxHashmapE n X; diff --git a/crypto/tl/tlbc-aux.h b/crypto/tl/tlbc-aux.h new file mode 100644 index 00000000..54f72bcc --- /dev/null +++ b/crypto/tl/tlbc-aux.h @@ -0,0 +1,262 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once +#include +#include +#include +#include + +namespace tlbc { + +static constexpr unsigned long long All = 1ULL << 63; + +struct BitPfxCollection { + std::vector pfx; + void clear() { + pfx.clear(); + } + void all() { + pfx.clear(); + pfx.push_back(All); + } + BitPfxCollection() = default; + BitPfxCollection(unsigned long long one_pfx) { + if (one_pfx) { + pfx.push_back(one_pfx); + } + } + bool empty() const { + return pfx.empty(); + } + unsigned long long min() const { + return pfx.empty() ? 0 : pfx[0]; + } + bool is_all() const { + return pfx.size() == 1 && pfx[0] == All; + } + BitPfxCollection& operator*=(unsigned long long prepend); + BitPfxCollection operator*(unsigned long long prepend) const; + BitPfxCollection operator+(const BitPfxCollection& other) const; + bool operator+=(const BitPfxCollection& other); + bool operator==(const BitPfxCollection& other) const { + return pfx == other.pfx; + } + bool operator!=(const BitPfxCollection& other) const { + return pfx != other.pfx; + } + void merge_back(unsigned long long z); + void show(std::ostream& os) const; +}; + +std::ostream& operator<<(std::ostream& os, const BitPfxCollection& p); + +struct AdmissibilityInfo { + enum { side = 4 }; + std::vector info; + int dim; + AdmissibilityInfo() : info(1, false), dim(0) { + } + void extend(int dim1); + bool operator[](std::size_t i) { + return info[i & (info.size() - 1)]; + } + void operator|=(const AdmissibilityInfo& other); + void set_all(bool val = true); + void clear_all() { + set_all(false); + } + void set_by_pattern(int pdim, int pattern[]); + void show(std::ostream& os) const; + bool is_set_all() const { + return !dim && info[0]; + } + bool extract1(char A[side], char tag, int p1) const; + bool extract2(char A[side][side], char tag, int p1, int p2) const; + bool extract3(char A[side][side][side], char tag, int p1, int p2, int p3) const; + int conflicts_at(const AdmissibilityInfo& other) const; + bool conflicts_with(const AdmissibilityInfo& other) const { + return conflicts_at(other) >= 0; + } +}; + +std::ostream& operator<<(std::ostream& os, const AdmissibilityInfo& p); + +struct ConflictSet { + unsigned long long x; + explicit ConflictSet(unsigned long long _x = 0) : x(_x) { + } + bool operator[](int i) const { + return (x >> i) & 1; + } + ConflictSet& operator|=(ConflictSet other) { + x |= other.x; + return *this; + } + int size() const { + return td::count_bits64(x); + } + int min() const { + return x ? td::count_trailing_zeroes_non_zero64(x) : 0x7fffffff; + } + int max() const { + return x ? 63 - td::count_leading_zeroes_non_zero64(x) : -1; + } + void remove(int i) { + x &= ~(1ULL << i); + } + void insert(int i) { + x |= (1ULL << i); + } +}; + +struct ConflictGraph { + std::array g; + ConflictSet& operator[](int i) { + return g[i]; + } + const ConflictSet& operator[](int i) const { + return g[i]; + } + void set_clique(ConflictSet set); +}; + +struct BinTrie { + std::unique_ptr left, right; + unsigned long long tag, down_tag; + int useful_depth; + BinTrie(unsigned long long _tag = 0, std::unique_ptr _left = {}, std::unique_ptr _right = {}) + : left(std::move(_left)), right(std::move(_right)), tag(_tag), down_tag(0), useful_depth(0) { + } + void ins_path(unsigned long long path, unsigned long long new_tag); + unsigned long long lookup_tag(unsigned long long path) const; + const BinTrie* lookup_node_const(unsigned long long path) const; + BinTrie* lookup_node(unsigned long long path); + bool is_unique() const { + return !(down_tag & (down_tag - 1)); + } + int unique_value() const { + return down_tag ? td::count_trailing_zeroes_non_zero64(down_tag) : -1; + } + static std::unique_ptr insert_path(std::unique_ptr root, unsigned long long path, + unsigned long long tag); + static std::unique_ptr insert_paths(std::unique_ptr root, const BitPfxCollection& paths, + unsigned long long tag); + void set_conflict_graph(ConflictGraph& gr, unsigned long long colors = 0) const; + int compute_useful_depth(unsigned long long colors = 0); + unsigned long long find_conflict_path(unsigned long long colors = 0, unsigned long long mask = ~0ULL) const; + unsigned long long build_submap_at(int depth, unsigned long long A[], unsigned long long pfx) const; + unsigned long long build_submap(int depth, unsigned long long A[]) const; + void show(std::ostream& os, unsigned long long pfx = 1ULL << 63) const; +}; + +std::ostream& operator<<(std::ostream& os, const BinTrie& bt); + +struct MinMaxSize { + enum : unsigned long long { Any = 0x7ff07, OneRef = 0x100000001ULL, Impossible = (0x7ff07ULL << 32) }; + unsigned long long minmax_size; + unsigned min_size() const { + return (unsigned)(minmax_size >> 32); + } + unsigned max_size() const { + return (unsigned)(minmax_size & 0xffffffff); + } + unsigned long long get() const { + return minmax_size; + } + bool is_fixed() const { + return min_size() == max_size(); + } + int fixed_bit_size() const { + return is_fixed() && !(min_size() & 0xff) ? (min_size() >> 8) : -1; + } + bool fits_into_cell() const { + return !((0x3ff04 - min_size()) & 0x80000080U); + } + bool is_possible() const { + return !((max_size() - min_size()) & 0x80000080U); + } + void normalize(); + MinMaxSize& clear() { + minmax_size = 0; + return *this; + } + MinMaxSize& clear_min() { + minmax_size &= (1ULL << 32) - 1; + return *this; + } + MinMaxSize& infinite_max() { + minmax_size |= 0x3ff07; + return *this; + } + MinMaxSize(unsigned long long _size = Impossible, bool _normalize = false) : minmax_size(_size) { + if (_normalize) { + normalize(); + } + } + static unsigned convert_size(unsigned z) { + return ((z & 0xff) << 16) | (z >> 8); + } + unsigned convert_min_size() const { + return convert_size(min_size()); + } + unsigned convert_max_size() const { + return convert_size(max_size()); + } + MinMaxSize operator+(MinMaxSize y) { + return MinMaxSize(get() + y.get(), true); + } + MinMaxSize& operator+=(MinMaxSize y) { + minmax_size += y.get(); + normalize(); + return *this; + } + MinMaxSize& operator|=(MinMaxSize y); + bool operator==(MinMaxSize y) { + return get() == y.get(); + } + bool operator!=(MinMaxSize y) { + return get() != y.get(); + } + MinMaxSize& repeat(int count); + MinMaxSize& repeat_at_least(int count); + static MinMaxSize fixed_size(unsigned sz) { + return MinMaxSize(sz * 0x10000000100ULL); + } + static MinMaxSize size_range(unsigned min_sz, unsigned max_sz) { + return MinMaxSize((((unsigned long long)min_sz << 32) + max_sz) << 8); + } + void show(std::ostream& os) const; + struct unpacked { + unsigned min_bits, min_refs, max_bits, max_refs; + unpacked(MinMaxSize val); + MinMaxSize pack() const; + void show(std::ostream& os) const; + }; + + private: + void nrm(unsigned long long a, unsigned long long b) { + if (minmax_size & a) { + minmax_size = (minmax_size | (a | b)) - a; + } + } +}; + +std::ostream& operator<<(std::ostream& os, MinMaxSize t); + +} // namespace tlbc diff --git a/crypto/tl/tlbc-data.h b/crypto/tl/tlbc-data.h new file mode 100644 index 00000000..02900348 --- /dev/null +++ b/crypto/tl/tlbc-data.h @@ -0,0 +1,328 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once +#include +#include + +namespace tlbc { + +using src::Lexem; +using src::Lexer; +using sym::sym_idx_t; + +struct Type; +struct Constructor; + +struct TypeExpr { + enum { te_Unknown, te_Type, te_Param, te_Apply, te_Add, te_MulConst, te_IntConst, te_Tuple, te_Ref, te_CondType }; + enum { max_const_expr = 100000, const_htable_size = 170239 }; + int tp; + int value; + Type* type_applied; + bool is_nat; // we keep integer expressions in 'TypeExpr' as well + bool is_nat_subtype; // this is # or a subtype of # + bool negated; // is it linearly negative + bool tchk_only; // type to be used as RHS of : only + int is_constexpr; // if non-zero, it is an index in `const_type_expr`, the table of all constant type expressions + src::SrcLocation where; + std::vector args; + TypeExpr(const src::SrcLocation& loc, int _tp, int _value = 0, bool _pol = false) + : tp(_tp) + , value(_value) + , type_applied(nullptr) + , is_nat_subtype(false) + , negated(_pol) + , tchk_only(false) + , is_constexpr(0) + , where(loc) { + init_is_nat(); + } + TypeExpr(const src::SrcLocation& loc, int _tp, int _value, std::initializer_list _arglist, + bool _pol = false) + : tp(_tp) + , value(_value) + , type_applied(nullptr) + , is_nat_subtype(false) + , negated(_pol) + , tchk_only(false) + , is_constexpr(0) + , where(loc) + , args(std::move(_arglist)) { + init_is_nat(); + } + TypeExpr(const src::SrcLocation& loc, int _tp, int _value, std::vector _arglist, bool _pol = false) + : tp(_tp) + , value(_value) + , type_applied(nullptr) + , is_nat_subtype(false) + , negated(_pol) + , tchk_only(false) + , is_constexpr(0) + , where(loc) + , args(std::move(_arglist)) { + init_is_nat(); + } + void check_mode(const src::SrcLocation& loc, int mode); + bool no_tchk() const; + bool close(const src::SrcLocation& loc); + bool bind_value(bool value_negated, Constructor& cs, bool checking_type = false); + int abstract_interpret_nat() const; + MinMaxSize compute_size() const; + bool compute_any_bits() const; + bool detect_constexpr(); + int is_integer() const; + bool is_anon() const; + bool is_ref_to_anon() const; + bool equal(const TypeExpr& other) const; + void const_type_name(std::ostream& os) const; + static TypeExpr* mk_intconst(const src::SrcLocation& loc, std::string int_const); + static TypeExpr* mk_intconst(const src::SrcLocation& loc, unsigned int_const); + static TypeExpr* mk_apply_gen(const src::SrcLocation& loc, TypeExpr* expr1, TypeExpr* expr2); + static TypeExpr* mk_mulint(const src::SrcLocation& loc, TypeExpr* expr1, TypeExpr* expr2); + static TypeExpr* mk_cellref(const src::SrcLocation& loc, TypeExpr* expr1); + static TypeExpr* mk_apply(const src::SrcLocation& loc, int tp, TypeExpr* expr1, TypeExpr* expr2); + static TypeExpr* mk_apply_empty(const src::SrcLocation& loc, sym_idx_t name, Type* type_applied); + void show(std::ostream& os, const Constructor* cs = nullptr, int prio = 0, int mode = 0) const; + + private: + void init_is_nat() { + is_nat = (tp >= te_Add && tp <= te_IntConst); + } + unsigned long long compute_hash() const; + static TypeExpr* const_htable[const_htable_size]; +}; + +// extern TypeExpr* TypeExpr::const_htable[TypeExpr::const_htable_size]; +extern TypeExpr* const_type_expr[TypeExpr::max_const_expr]; +extern int const_type_expr_num; + +std::ostream& operator<<(std::ostream& os, const TypeExpr* te); + +struct Field { + int field_idx; + bool implicit; + bool known; + bool constraint; + bool used; + bool subrec; + sym_idx_t name; + TypeExpr* type; + const src::SrcLocation loc; + Field(const src::SrcLocation& where, bool impl, int idx, sym_idx_t fname = 0, TypeExpr* ftype = nullptr) + : field_idx(idx) + , implicit(impl) + , known(false) + , constraint(false) + , used(false) + , subrec(false) + , name(fname) + , type(ftype) + , loc(where) { + } + void register_sym() const; + std::string get_name() const; + bool isomorphic_to(const Field& f, bool allow_other_names = true) const; +}; + +struct Constructor { + sym_idx_t constr_name; + sym_idx_t type_name; + Type* type_defined; + src::SrcLocation where; + unsigned long long tag; + int tag_bits; + int fields_num; + int type_arity; + bool is_fwd; + bool is_enum; + bool is_simple_enum; + bool is_special; + bool has_fixed_size; + bool any_bits; + MinMaxSize size; + BitPfxCollection begins_with; + std::vector fields; + std::vector params; + std::vector param_negated; + std::vector param_const_val; // -1 -- not integer or not constant + AdmissibilityInfo admissible_params; + void set_tag(unsigned long long new_tag) { + tag = new_tag; + tag_bits = (tag ? 63 - td::count_trailing_zeroes_non_zero64(tag) : -1); + } + Constructor(const src::SrcLocation& _loc = {}, sym_idx_t cname = 0, sym_idx_t tname = 0, unsigned long long _tag = 0, + Type* type = nullptr) + : constr_name(cname) + , type_name(tname) + , type_defined(type) + , where(_loc) + , fields_num(0) + , type_arity(0) + , is_fwd(false) + , is_enum(false) + , is_simple_enum(false) + , is_special(false) + , has_fixed_size(false) + , any_bits(false) { + set_tag(_tag); + } + Field& new_field(const src::SrcLocation& where, bool implicit, sym_idx_t name); + void show(std::ostream& os, int mode = 0) const; + std::string get_name() const; + std::string get_qualified_name() const; + bool isomorphic_to(const Constructor& cs, bool allow_other_names) const; + unsigned long long compute_tag() const; + void check_assign_tag(); + bool compute_is_fwd(); + bool recompute_begins_with(); + bool recompute_minmax_size(); + bool recompute_any_bits(); + bool compute_admissible_params(); + int get_const_param(unsigned idx) const { + return idx < param_const_val.size() ? param_const_val[idx] : -1; + } +}; + +std::ostream& operator<<(std::ostream& os, const Constructor& cs); + +struct Type { + enum { _IsType = 1, _IsNat = 2, _IsPos = 4, _IsNeg = 8, _NonConst = 16 }; + sym_idx_t type_name; + int type_idx; + int parent_type_idx; + int constr_num; + int arity; + int used; + int last_declared; + static int last_declared_counter; + bool produces_nat; + bool is_final; + bool is_builtin; + bool is_enum; + bool is_simple_enum; + bool is_special; + bool is_pfx_determ; + bool is_param_determ; + bool is_const_param_determ; + bool is_const_param_pfx_determ; + bool is_param_pfx_determ; + bool is_determ; + bool has_fixed_size; + bool any_bits; + bool is_auto; + bool is_anon; + bool is_unit; + bool is_bool; + signed char is_integer; + int useful_depth; + int const_param_idx; + int conflict1, conflict2; + MinMaxSize size; + std::vector constructors; + std::vector args; + BitPfxCollection begins_with; + AdmissibilityInfo admissible_params; + std::unique_ptr cs_trie; + + Type(int idx, sym_idx_t _tname = 0, bool pnat = false, int _arity = -1, bool _final = false, bool _nonempty = false) + : type_name(_tname) + , type_idx(idx) + , parent_type_idx(-1) + , constr_num(0) + , arity(_arity) + , used(0) + , last_declared(0) + , produces_nat(pnat) + , is_final(_final) + , is_builtin(_final) + , is_enum(!_final) + , is_simple_enum(!_final) + , is_special(false) + , is_pfx_determ(false) + , is_param_determ(false) + , is_const_param_determ(false) + , is_const_param_pfx_determ(false) + , is_param_pfx_determ(false) + , is_determ(false) + , has_fixed_size(false) + , any_bits(false) + , is_auto(false) + , is_anon(false) + , is_unit(false) + , is_bool(false) + , is_integer(pnat) + , useful_depth(-1) + , const_param_idx(-1) + , conflict1(-1) + , conflict2(-1) { + if (arity > 0) { + args.resize(arity, 0); + } + if (_nonempty) { + begins_with.all(); + } + } + void bind_constructor(const src::SrcLocation& loc, Constructor* cs); + bool unique_constructor_equals(const Constructor& cs, bool allow_other_names = false) const; + void print_name(std::ostream& os) const; + std::string get_name() const; + bool recompute_begins_with(); + bool recompute_minmax_size(); + bool recompute_any_bits(); + bool compute_admissible_params(); + void compute_constructor_trie(); + int detect_const_params(); + bool check_conflicts(); + void show_constructor_conflict(); + void detect_basic_types(); + bool cons_all_exact() const; + int cons_common_len() const; + bool is_const_arg(int p) const; + std::vector get_all_param_values(int p) const; + std::vector get_constr_by_param_value(int p, int pv) const; + void renew_last_declared() { + last_declared = ++last_declared_counter; + } +}; + +extern TypeExpr type_Type; + +struct SymVal : sym::SymValBase { + TypeExpr* sym_type; + SymVal(int _type, int _idx, TypeExpr* _stype = nullptr) : sym::SymValBase(_type, _idx), sym_type(_stype) { + } + TypeExpr* get_type() const { + return sym_type; + } +}; + +struct SymValType : SymVal { + Type* type_ref; + explicit SymValType(Type* _type = nullptr) : SymVal(sym::SymValBase::_Typename, 0, &type_Type), type_ref(_type) { + } +}; + +extern sym_idx_t Nat_name, Eq_name, Less_name, Leq_name; +extern Type *Nat_type, *Eq_type; +extern Type *NatWidth_type, *NatLess_type, *NatLeq_type, *Int_type, *UInt_type; + +extern int types_num, builtin_types_num; +extern std::vector types; + +} // namespace tlbc diff --git a/crypto/tl/tlbc-gen-cpp.cpp b/crypto/tl/tlbc-gen-cpp.cpp new file mode 100644 index 00000000..945f8493 --- /dev/null +++ b/crypto/tl/tlbc-gen-cpp.cpp @@ -0,0 +1,3412 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "tlbc-gen-cpp.h" +#include "td/utils/bits.h" +#include "td/utils/filesystem.h" + +namespace tlbc { + +/* + * + * C++ CODE GENERATION + * + */ + +CppIdentSet global_cpp_ids; + +std::vector> cpp_type; + +bool add_type_members; + +std::set forbidden_cpp_idents, local_forbidden_cpp_idents; +std::vector const_type_expr_cpp_idents; +std::vector const_type_expr_simple; + +void init_forbidden_cpp_idents() { + std::set& f = forbidden_cpp_idents; + f.insert("true"); + f.insert("false"); + f.insert("int"); + f.insert("bool"); + f.insert("unsigned"); + f.insert("long"); + f.insert("short"); + f.insert("char"); + f.insert("void"); + f.insert("class"); + f.insert("struct"); + f.insert("enum"); + f.insert("union"); + f.insert("public"); + f.insert("private"); + f.insert("protected"); + f.insert("extern"); + f.insert("static"); + f.insert("final"); + f.insert("if"); + f.insert("else"); + f.insert("while"); + f.insert("do"); + f.insert("for"); + f.insert("break"); + f.insert("continue"); + f.insert("return"); + f.insert("virtual"); + f.insert("explicit"); + f.insert("override"); + f.insert("new"); + f.insert("delete"); + f.insert("operator"); + f.insert("Ref"); + f.insert("Cell"); + f.insert("CellSlice"); + f.insert("Anything"); + f.insert("RefAnything"); + f.insert("Nat"); + f.insert("t_Nat"); + f.insert("t_RefCell"); + f.insert("t_Anything"); + f.insert("TLB"); + f.insert("TLB_Complex"); + f.insert("PrettyPrinter"); + std::set& l = local_forbidden_cpp_idents; + l.insert("cons_len"); + l.insert("cons_len_exact"); + l.insert("cons_tag"); + l.insert("skip"); + l.insert("validate_skip"); + l.insert("get_size"); + l.insert("pack"); + l.insert("unpack"); + l.insert("cs"); + l.insert("cb"); + l.insert("cell_ref"); + l.insert("type_class"); + l.insert("pp"); + l.insert("weak"); +} + +std::string CppIdentSet::compute_cpp_ident(std::string orig_ident, int count) { + std::ostringstream os; + int a, r = 0, cnt = 0; + bool prev_skip = false; + for (int c : orig_ident) { + bool pp = prev_skip; + prev_skip = true; + if (c & 0x80) { + if ((c & 0xe0) == 0xc0) { + a = (c & 0x1f); + r = 1; + continue; + } else if ((c & 0xf0) == 0xe0) { + a = (c & 0x0f); + r = 2; + continue; + } + if ((c & 0xc0) != 0x80) { + continue; + } + if (!r) { + continue; + } + a = (a << 6) | (c & 0x3f); + if (--r) { + continue; + } + c = a; + } + prev_skip = false; + if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '_') { + os << (char)c; + cnt++; + continue; + } + if (c >= '0' && c <= '9') { + if (!cnt) { + os << '_'; + cnt++; + } + os << (char)c; + cnt++; + continue; + } + if (c >= 0x410 && c < 0x450) { + os << (char)(0xc0 + (c >> 6)) << (char)(0x80 + (c & 0x3f)); + cnt++; + continue; + } + prev_skip = true; + if (!pp) { + os << '_'; + } + } + if (!cnt) { + os << '_'; + prev_skip = true; + } + if (count) { + os << count; + } + return os.str(); +} + +bool CppIdentSet::is_good_ident(std::string ident) { + return !defined(ident) && !forbidden_cpp_idents.count(ident) && + !(extra_forbidden_idents && extra_forbidden_idents->count(ident)); +} + +std::string CppIdentSet::new_ident(std::string orig_ident, int count, std::string suffix) { + while (true) { + std::string ident = compute_cpp_ident(orig_ident, count) + suffix; + if (is_good_ident(ident)) { + cpp_idents.insert(ident); + return ident; + } + ++count; + } +} + +struct SizeWriter { + int sz; + explicit SizeWriter(int _sz) : sz(_sz) { + } + void write(std::ostream& os) const; +}; + +void SizeWriter::write(std::ostream& os) const { + if (sz < 0x10000) { + os << sz; + } else { + os << "0x" << std::hex << sz << std::dec; + } +} + +std::ostream& operator<<(std::ostream& os, SizeWriter w) { + w.write(os); + return os; +} + +unsigned long long CppTypeCode::compute_selector_mask() const { + unsigned long long z = 0, w = 1; + int c = 0; + for (int v : cons_tag_map) { + if (v > c) { + c = v; + z |= w; + } + w <<= 1; + } + return z; +} + +struct HexConstWriter { + unsigned long long mask; + explicit HexConstWriter(unsigned long long _mask) : mask(_mask){}; + void write(std::ostream& os) const; +}; + +void HexConstWriter::write(std::ostream& os) const { + if (mask < 32) { + os << mask; + } else { + os << "0x" << std::hex << mask << std::dec; + } + if (mask >= (1ULL << 31)) { + os << (mask >= (1ULL << 32) ? "ULL" : "U"); + } +} + +std::ostream& operator<<(std::ostream& os, HexConstWriter w) { + w.write(os); + return os; +} + +cpp_val_type detect_cpp_type(const TypeExpr* expr) { + if (expr->tp == TypeExpr::te_Ref) { + return ct_cell; + } + if (expr->is_nat) { + return ct_int32; + } + MinMaxSize sz = expr->compute_size(); + int l = sz.fixed_bit_size(); + if (expr->is_nat_subtype) { + return l == 1 ? ct_bool : ct_int32; + } + if (expr->tp == TypeExpr::te_CondType) { + cpp_val_type subtype = detect_cpp_type(expr->args.at(1)); + if (subtype == ct_slice || subtype == ct_cell || subtype == ct_integer || subtype == ct_bitstring || + subtype == ct_enum) { + return subtype; + } + if ((subtype == ct_int32 || subtype == ct_int64) && expr->args[1]->is_integer() > 0) { + return subtype; + } + return ct_slice; + } + int x = expr->is_integer(); + if (sz.max_size() & 0xff) { + return ct_slice; + } + if (!x) { + const Type* ta = expr->type_applied; + if (expr->tp == TypeExpr::te_Apply && ta && ta->is_simple_enum) { + return ct_enum; + } + if (expr->tp == TypeExpr::te_Apply && ta && ta->type_idx < builtin_types_num && + (ta == Bits_type || ta->get_name().at(0) == 'b')) { + return (l >= 0 && l <= 256) ? ct_bits : ct_bitstring; + } + if (expr->tp == TypeExpr::te_Tuple && expr->args[1]->tp == TypeExpr::te_Apply && + expr->args[1]->type_applied->is_bool) { + return (l >= 0 && l <= 256) ? ct_bits : ct_bitstring; + } + return ct_slice; + } + l = (sz.max_size() >> 8); + if (x > 0 && l == 1) { + return ct_bool; + } + if (l < 32) { + return ct_int32; + } + if (l == 32) { + return (x < 0 ? ct_int32 : ct_uint32); + } + if (l < 64) { + return ct_int64; + } + if (l == 64) { + return (x < 0 ? ct_int64 : ct_uint64); + } + return ct_integer; +} + +cpp_val_type detect_field_cpp_type(const Field& field) { + return field.subrec ? ct_subrecord : detect_cpp_type(field.type); +} + +void show_valtype(std::ostream& os, cpp_val_type x, int size = -1, bool pass_value = false) { + switch (x) { + case ct_void: + os << "void"; + break; + case ct_slice: + os << "Ref"; + break; + case ct_cell: + os << "Ref"; + break; + case ct_typeptr: + os << "const TLB*"; + break; + case ct_typeref: + os << "const TLB&"; + break; + case ct_bitstring: + os << "Ref"; + break; + case ct_bits: + if (pass_value) { + os << "const "; + } + os << "td::BitArray<" << size << ">"; + if (pass_value) { + os << "&"; + } + break; + case ct_integer: + os << "RefInt256"; + break; + case ct_bool: + os << "bool"; + break; + case ct_enum: + os << "char"; + break; + case ct_int32: + os << "int"; + break; + case ct_uint32: + os << "unsigned"; + break; + case ct_int64: + os << "long long"; + break; + case ct_uint64: + os << "unsigned long long"; + break; + case ct_subrecord: + if (pass_value) { + os << "const "; + } + os << "::Record"; + if (pass_value) { + os << "&"; + } + break; + default: + os << ""; + } +} + +bool CppValType::needs_move() const { + return (vt == ct_cell || vt == ct_slice || vt == ct_bitstring || vt == ct_integer); +} + +void CppValType::show(std::ostream& os, bool pass_value) const { + show_valtype(os, vt, size, pass_value); +} + +std::ostream& operator<<(std::ostream& os, CppValType cvt) { + cvt.show(os); + return os; +} + +void CppTypeCode::ConsField::print_type(std::ostream& os, bool pass_value) const { + if (ctype != ct_subrecord) { + get_cvt().show(os, pass_value); + } else { + assert(subrec); + if (pass_value) { + os << "const "; + } + subrec->print_full_name(os); + if (pass_value) { + os << "&"; + } + } +} + +void CppTypeCode::ConsRecord::print_full_name(std::ostream& os) const { + os << cpp_type.cpp_type_class_name << "::" << cpp_name; +} + +void CppTypeCode::assign_class_name() { + std::string type_name = type.get_name(); + sym_idx_t name = type.type_name; + if (!name && type.parent_type_idx >= 0) { + int i = type.parent_type_idx; + while (true) { + name = types.at(i).type_name; + if (name || types.at(i).parent_type_idx < 0) { + break; + } + i = types.at(i).parent_type_idx; + } + if (name) { + type_name = sym::symbols.get_name(name) + "_aux"; + } + } + cpp_type_class_name = global_cpp_ids.new_ident(type_name); + if (params) { + cpp_type_template_name = global_cpp_ids.new_ident(cpp_type_class_name + "T"); + } else { + cpp_type_var_name = global_cpp_ids.new_ident(std::string{"t_"} + cpp_type_class_name); + } +} + +void CppTypeCode::assign_cons_names() { + cons_enum_name.resize(cons_num); + for (int i = 0; i < cons_num; i++) { + sym_idx_t cons = type.constructors.at(i)->constr_name; + if (cons) { + cons_enum_name[i] = local_cpp_ids.new_ident(sym::symbols.get_name(cons)); + } else if (type.const_param_idx >= 0) { + int pv = type.constructors[i]->get_const_param(type.const_param_idx); + cons_enum_name[i] = local_cpp_ids.new_ident(pv ? "cons" : "cons0", pv); + } else { + cons_enum_name[i] = local_cpp_ids.new_ident("cons", i + 1); + } + } +} + +void CppTypeCode::assign_cons_values() { + std::vector> a; + a.reserve(cons_num); + for (int i = 0; i < cons_num; i++) { + a.emplace_back(type.constructors[i]->begins_with.min(), i); + } + std::sort(a.begin(), a.end()); + cons_enum_value.resize(cons_num); + cons_idx_by_enum.resize(cons_num); + int i = 0; + for (auto z : a) { + cons_enum_value[z.second] = i; + cons_idx_by_enum[i++] = z.second; + } +} + +std::vector std_field_names = {"x", "y", "z", "t", "u", "v", "w"}; + +void CppTypeCode::assign_record_cons_names() { + for (int i = 0; i < cons_num; i++) { + const Constructor& ctor = *type.constructors.at(i); + records.emplace_back(*this, ctor, i); + ConsRecord& record = records.back(); + record.has_trivial_name = (cons_num <= 1 || !ctor.constr_name); + record.declared = false; + record.cpp_name = local_cpp_ids.new_ident(cons_num <= 1 ? "Record" : std::string{"Record_"} + cons_enum_name[i]); + CppIdentSet rec_cpp_ids; + rec_cpp_ids.insert("type_class"); + rec_cpp_ids.insert(record.cpp_name); + // maybe : add field identifiers from type class context (?) + for (int j = 0; j < ctor.fields_num; j++) { + const Field& field = ctor.fields.at(j); + if (field.constraint) { + } else if (!field.implicit) { + MinMaxSize sz = field.type->compute_size(); + if (!sz.max_size()) { + continue; + } + std::string field_name; + const ConsRecord* subrec = nullptr; + if (field.name) { + field_name = rec_cpp_ids.new_ident(field.get_name()); + } else if (field.subrec) { + field_name = rec_cpp_ids.new_ident("r", 1); + subrec = &cpp_type.at(field.type->args.at(0)->type_applied->type_idx)->records.at(0); + } else if (field.type->tp == TypeExpr::te_Ref) { + field_name = rec_cpp_ids.new_ident("ref", 1); + } + record.cpp_fields.emplace_back(field, field_name, detect_field_cpp_type(field), sz.fixed_bit_size(), j, subrec); + } else if (field.used && (add_type_members || field.type->is_nat_subtype)) { + std::string field_name = rec_cpp_ids.new_ident(field.get_name()); + record.cpp_fields.emplace_back(field, field_name, field.type->is_nat_subtype ? ct_int32 : ct_typeptr, -1, j, + nullptr, true); + } + } + auto q = std_field_names.cbegin(); + for (auto& fi : record.cpp_fields) { + if (fi.name.empty()) { + bool is_ok = false; + while (q < std_field_names.cend()) { + if (!rec_cpp_ids.defined(*q)) { + fi.name = rec_cpp_ids.new_ident(*q++); + is_ok = true; + break; + } + } + if (!is_ok) { + fi.name = rec_cpp_ids.new_ident("f", 1); + } + } + } + record.is_trivial = (record.cpp_fields.size() <= 1); + record.is_small = (record.cpp_fields.size() <= 3); + record.inline_record = (record.cpp_fields.size() <= 2); + cpp_val_type t = ct_unknown; + if (record.is_trivial) { + t = (record.cpp_fields.size() == 1) ? record.cpp_fields.at(0).ctype : ct_void; + } + std::vector tv; + for (const auto& f : record.cpp_fields) { + if (f.ctype == ct_subrecord) { + record.is_trivial = record.is_small = false; + } else if (!f.implicit) { + tv.push_back(f.ctype); + } + } + record.equiv_cpp_type = t; + record.equiv_cpp_types = tv; + record.triv_conflict = false; + for (int j = 0; j < i; j++) { + if (records[j].equiv_cpp_types == tv) { + record.triv_conflict = records[j].triv_conflict = true; + break; + } + } + } +} + +bool CppTypeCode::ConsRecord::recover_idents(CppIdentSet& idents) const { + bool is_ok = idents.insert(cpp_name) && idents.insert("type_class"); + for (const auto& f : cpp_fields) { + is_ok &= idents.insert(f.name); + } + return is_ok; +} + +void CppTypeCode::assign_class_field_names() { + char cn = 'm', ct = 'X'; + int c = 0; + for (int z : type.args) { + bool f = z & Type::_IsNat; + bool neg = (z & Type::_IsNeg); + type_param_is_nat.push_back(f); + type_param_is_neg.push_back(neg); + std::string id; + if (!neg && !c++) { + template_args += ", "; + constructor_args += ", "; + } + if (f) { + id = local_cpp_ids.new_ident(std::string{cn}, 0, "_"); + if (cn != 't') { + ++cn; + } + if (!neg) { + template_args += "int "; + constructor_args += "int "; + } else { + skip_extra_args += ", int& "; + skip_extra_args_pass += ", "; + } + } else { + id = local_cpp_ids.new_ident(std::string{ct}, 0, "_"); + if (ct != 'Z') { + ++ct; + } else { + ct = 'T'; + } + assert(!neg); + template_args += "typename "; + constructor_args += "const TLB& "; + } + type_param_name.push_back(id); + if (!neg) { + template_args += id; + constructor_args += id; + } else { + skip_extra_args += id; + skip_extra_args_pass += id; + } + } +} + +bool CppTypeCode::compute_simple_cons_tags() { + if (!type.is_pfx_determ || type.useful_depth > 8) { + return false; + } + int d = type.useful_depth; + int n = (1 << d); + cons_tag_map.resize(n, 0); + //std::cerr << "compute_simple_cons_tags() for `" << type.get_name() << "` (d=" << d << ")\n"; + for (int i = 0; i < cons_num; i++) { + int t = cons_enum_value.at(i) + 1; + for (unsigned long long z : type.constructors[i]->begins_with.pfx) { + int l = std::min(63 - td::count_trailing_zeroes_non_zero64(z), d); + assert(l <= d); + int a = d ? (int)((z & (z - 1)) >> (64 - d)) : 0; + int b = (1 << (d - l)); + while (b-- > 0) { + assert(!cons_tag_map.at(a) || cons_tag_map[a] == t); + cons_tag_map[a++] = t; + } + } + } + int c = 0; + for (int v : cons_tag_map) { + if (v && v != c && v != ++c) { + return false; + } + } + return true; +} + +bool CppTypeCode::check_incremental_cons_tags() const { + if (!cons_num || common_cons_len < 0) { + return false; + } + int l = common_cons_len; + if (!l || l > 32) { + return true; + } + for (int i = 0; i < cons_num; i++) { + unsigned long long tag = (type.constructors.at(i)->tag >> (64 - l)); + if (tag != (unsigned)cons_enum_value.at(i)) { + return false; + } + } + return true; +} + +bool CppTypeCode::init() { + builtin = type.is_builtin; + cons_num = type.constr_num; + params = ret_params = tot_params = 0; + for (int z : type.args) { + if ((z & Type::_IsNeg)) { + ++ret_params; + } else { + ++params; + } + ++tot_params; + } + assign_class_name(); + assign_cons_names(); + assign_class_field_names(); + assign_cons_values(); + assign_record_cons_names(); + simple_get_size = type.has_fixed_size; + inline_skip = simple_get_size; + inline_validate_skip = (inline_skip && type.any_bits && !(type.size.min_size() & 0xff)); + inline_get_tag = (type.is_pfx_determ && type.useful_depth <= 6); + simple_cons_tags = compute_simple_cons_tags(); + common_cons_len = type.cons_common_len(); + incremental_cons_tags = check_incremental_cons_tags(); + return true; +} + +void CppTypeCode::generate_cons_enum(std::ostream& os) { + os << " enum { "; + for (int i = 0; i < cons_num; i++) { + if (i) { + os << ", "; + } + int k = cons_idx_by_enum.at(i); + os << cons_enum_name.at(k); + assert(cons_enum_value.at(k) == i); + } + os << " };\n"; +} + +void CppTypeCode::generate_cons_len_array(std::ostream& os, std::string nl, int options) { + bool f = (options & 2); + os << nl << (f ? "" : "static ") << ((options & 3) ? "constexpr " : "") << "char "; + if (f) { + os << cpp_type_class_name << "::"; + } + os << "cons_len[" << cons_num << "]"; + if (f) { + os << ";\n"; + return; + } + os << " = { "; + for (int i = 0; i < cons_num; i++) { + int k = cons_idx_by_enum.at(i); + const Constructor& constr = *type.constructors.at(k); + if (i > 0) { + os << ", "; + } + os << constr.tag_bits; + } + os << " };\n"; +} + +void CppTypeCode::generate_cons_tag_array(std::ostream& os, std::string nl, int options) { + int m = -1; + for (int i = 0; i < cons_num; i++) { + int k = cons_idx_by_enum.at(i); + const Constructor& constr = *type.constructors.at(k); + if (constr.tag_bits > m) { + m = constr.tag_bits; + } + } + bool f = (options & 2); + os << nl << (f ? "" : "static ") << ((options & 3) ? "constexpr " : ""); + if (m <= 8) { + os << "unsigned char "; + } else if (m <= 16) { + os << "unsigned short "; + } else if (m <= 32) { + os << "unsigned "; + } else { + os << "unsigned long long "; + } + if (f) { + os << cpp_type_class_name << "::"; + } + os << "cons_tag[" << cons_num << "]"; + if (f) { + os << ";\n"; + return; + } + os << " = { "; + for (int i = 0; i < cons_num; i++) { + int k = cons_idx_by_enum.at(i); + const Constructor& constr = *type.constructors.at(k); + if (i > 0) { + os << ", "; + } + os << HexConstWriter{constr.tag_bits ? (constr.tag >> (64 - constr.tag_bits)) : 0}; + } + os << " };\n"; +} + +void CppTypeCode::generate_cons_tag_info(std::ostream& os, std::string nl, int options) { + if (cons_num) { + if (common_cons_len == -1) { + generate_cons_len_array(os, nl, options); + } else if (options & 1) { + os << " static constexpr int cons_len_exact = " << common_cons_len << ";\n"; + } + if (common_cons_len != 0 && !incremental_cons_tags) { + generate_cons_tag_array(os, nl, options); + } + } +} + +void CppTypeCode::generate_get_tag_subcase(std::ostream& os, std::string nl, const BinTrie* trie, int depth) const { + if (!trie || !trie->down_tag) { + os << nl << "return -1; // ???"; + return; + } + if (trie->is_unique()) { + os << nl << "return " << cons_enum_name.at(trie->unique_value()) << ";"; + return; + } + if (!trie->useful_depth) { + generate_get_tag_param(os, nl, trie->down_tag); + return; + } + assert(trie->left || trie->right); + if (!trie->right) { + generate_get_tag_subcase(os, nl, trie->left.get(), depth + 1); + return; + } + if (!trie->left) { + generate_get_tag_subcase(os, nl, trie->right.get(), depth + 1); + return; + } + if (trie->left->is_unique() && trie->right->is_unique()) { + os << nl << "return cs.bit_at(" << depth << ") ? "; + int a = trie->right->unique_value(), b = trie->left->unique_value(); + os << (a >= 0 ? cons_enum_name.at(a) : "-1") << " : "; + os << (b >= 0 ? cons_enum_name.at(b) : "-1") << ";"; + return; + } + os << nl << "if (cs.bit_at(" << depth << ")) {"; + generate_get_tag_subcase(os, nl + " ", trie->right.get(), depth + 1); + os << nl << "} else {"; + generate_get_tag_subcase(os, nl + " ", trie->left.get(), depth + 1); + os << nl << "}"; +} + +void CppTypeCode::generate_get_tag_param(std::ostream& os, std::string nl, unsigned long long tag, + unsigned long long tag_params) const { + if (!tag) { + os << nl << "return -1; // ???"; + return; + } + if (!(tag & (tag - 1))) { + os << nl << "return " << cons_enum_name.at(td::count_trailing_zeroes64(tag)) << ";"; + return; + } + int cnt = td::count_bits64(tag); + DCHECK(cnt >= 2); + int mdim = 0, mmdim = 0; + for (int c = 0; c < 64; c++) { + if ((tag >> c) & 1) { + int dim = type.constructors.at(c)->admissible_params.dim; + if (dim > mdim) { + mmdim = mdim; + mdim = dim; + } else if (dim > mmdim) { + mmdim = dim; + } + } + } + assert(mmdim > 0); + for (int p1 = 0; p1 < mmdim; p1++) { + char A[4]; + std::memset(A, 0, sizeof(A)); + int c; + for (c = 0; c < 64; c++) { + if ((tag >> c) & 1) { + if (!type.constructors[c]->admissible_params.extract1(A, (char)(c + 1), p1)) { + break; + } + } + } + if (c == 64) { + std::string param_name = get_nat_param_name(p1); + generate_get_tag_param1(os, nl, A, ¶m_name); + return; + } + } + for (int p2 = 0; p2 < mmdim; p2++) { + for (int p1 = 0; p1 < p2; p1++) { + char A[4][4]; + std::memset(A, 0, sizeof(A)); + int c; + for (c = 0; c < 64; c++) { + if ((tag >> c) & 1) { + if (!type.constructors[c]->admissible_params.extract2(A, (char)(c + 1), p1, p2)) { + break; + } + } + } + if (c == 64) { + std::string param_names[2]; + param_names[0] = get_nat_param_name(p1); + param_names[1] = get_nat_param_name(p2); + generate_get_tag_param2(os, nl, A, param_names); + return; + } + } + } + for (int p3 = 0; p3 < mmdim; p3++) { + for (int p2 = 0; p2 < p3; p2++) { + for (int p1 = 0; p1 < p2; p1++) { + char A[4][4][4]; + std::memset(A, 0, sizeof(A)); + int c; + for (c = 0; c < 64; c++) { + if ((tag >> c) & 1) { + if (!type.constructors[c]->admissible_params.extract3(A, (char)(c + 1), p1, p2, p3)) { + break; + } + } + } + if (c == 64) { + std::string param_names[3]; + param_names[0] = get_nat_param_name(p1); + param_names[1] = get_nat_param_name(p2); + param_names[2] = get_nat_param_name(p3); + generate_get_tag_param3(os, nl, A, param_names); + return; + } + } + } + } + os << nl << "// ??? cannot distinguish constructors for this type using up to three parameters\n"; + throw src::Fatal{std::string{"cannot generate `"} + cpp_type_class_name + "::get_tag()` method for type `" + + type.get_name() + "`"}; +} + +bool CppTypeCode::match_param_pattern(std::ostream& os, std::string nl, const char A[4], int mask, std::string pattern, + std::string param_name) const { + int v = 0, w = 0; + for (int i = 0; i < 4; i++) { + if (A[i]) { + if ((mask >> i) & 1) { + v = (v && v != A[i] ? -1 : A[i]); + } else { + w = (w && w != A[i] ? -1 : A[i]); + } + } + } + if (v <= 0 || w <= 0) { + return false; + } + os << nl << "return "; + for (char c : pattern) { + if (c != '#') { + os << c; + } else { + os << param_name; + } + } + os << " ? " << cons_enum_name.at(v - 1) << " : " << cons_enum_name.at(w - 1) << ";"; + return true; +} + +void CppTypeCode::generate_get_tag_param1(std::ostream& os, std::string nl, const char A[4], + const std::string param_names[1]) const { + os << nl << "// distinguish by parameter `" << param_names[0] << "` using"; + for (int i = 0; i < 4; i++) { + os << ' ' << (int)A[i]; + } + if (match_param_pattern(os, nl, A, 14, "#", param_names[0]) || + match_param_pattern(os, nl, A, 2, "# == 1", param_names[0]) || + match_param_pattern(os, nl, A, 3, "# <= 1", param_names[0]) || + match_param_pattern(os, nl, A, 10, "(# & 1)", param_names[0]) || + match_param_pattern(os, nl, A, 4, "# && !(# & 1)", param_names[0]) || + match_param_pattern(os, nl, A, 8, "# > 1 && (# & 1)", param_names[0])) { + return; + } + os << nl << "static inline size_t nat_abs(int x) { return (x > 1) * 2 + (x & 1); }"; + os << nl << "static signed char ctab[4] = { "; + for (int i = 0; i < 4; i++) { + if (i > 0) { + os << ", "; + } + os << (A[i] ? cons_enum_name.at(A[i] - 1) : "-1"); + } + os << " };" << nl << "return ctab[nat_abs(" << param_names[0] << ")];"; +} + +void CppTypeCode::generate_get_tag_param2(std::ostream& os, std::string nl, const char A[4][4], + const std::string param_names[2]) const { + os << nl << "// distinguish by parameters `" << param_names[0] << "`, `" << param_names[1] << "` using"; + for (int i = 0; i < 4; i++) { + for (int j = 0; j < 4; j++) { + os << ' ' << (int)A[i][j]; + } + } + os << nl << "static inline size_t nat_abs(int x) { return (x > 1) * 2 + (x & 1); }"; + os << nl << "static signed char ctab[4][4] = { "; + for (int i = 0; i < 16; i++) { + if (i > 0) { + os << ", "; + } + int v = A[i >> 2][i & 3]; + os << (v ? cons_enum_name.at(v - 1) : "-1"); + } + os << " };" << nl << "return ctab[nat_abs(" << param_names[0] << ")][nat_abs(" << param_names[1] << ")];"; +} + +void CppTypeCode::generate_get_tag_param3(std::ostream& os, std::string nl, const char A[4][4][4], + const std::string param_names[3]) const { + os << nl << "// distinguish by parameters `" << param_names[0] << "`, `" << param_names[1] << "`, `" << param_names[2] + << "` using"; + for (int i = 0; i < 4; i++) { + for (int j = 0; j < 4; j++) { + for (int k = 0; k < 4; k++) { + os << ' ' << (int)A[i][j][k]; + } + } + } + os << nl << "static inline size_t nat_abs(int x) { return (x > 1) * 2 + (x & 1); }"; + os << nl << "static signed char ctab[4][4][4] = { "; + for (int i = 0; i < 64; i++) { + if (i > 0) { + os << ", "; + } + int v = A[i >> 4][(i >> 2) & 3][i & 3]; + os << (v ? cons_enum_name.at(v - 1) : "-1"); + } + os << " };" << nl << "return ctab[nat_abs(" << param_names[0] << ")][nat_abs(" << param_names[1] << ")][nat_abs(" + << param_names[2] << ")];"; +} + +std::string CppTypeCode::get_nat_param_name(int idx) const { + for (int i = 0; i < tot_params; i++) { + if (!type_param_is_neg.at(i) && type_param_is_nat.at(i) && !idx--) { + return type_param_name.at(i); + } + } + return "???"; +} + +void CppTypeCode::generate_tag_pfx_selector(std::ostream& os, std::string nl, const BinTrie& trie, int d, + int min_size) const { + assert(d >= 0 && d <= 6); + int n = (1 << d); + unsigned long long A[64]; + int c[65]; + unsigned long long mask = trie.build_submap(d, A); + int l = 1; + c[0] = -1; + for (int i = 0; i < n; i++) { + assert(!(A[i] & (A[i] - 1))); + if ((mask >> l) & 1) { + c[l++] = A[i] ? td::count_trailing_zeroes_non_zero64(A[i]) : -1; + } + } + bool simple = (l > n / 2); + if (simple) { + l = n + 1; + for (int i = 0; i < n; i++) { + c[i + 1] = A[i] ? td::count_trailing_zeroes_non_zero64(A[i]) : -1; + } + } + os << nl << "static signed char ctab[" << l << "] = {"; + for (int i = 0; i < l; i++) { + if (i > 0) { + os << ", "; + } + if (c[i] < 0) { + os << c[i]; + } else { + os << cons_enum_name.at(c[i]); + } + } + os << "};" << nl << "return ctab[1 + "; + if (simple) { + os << "(long)cs.prefetch_ulong(" << d << ")];"; + } else { + os << "(long)cs.bselect" << (d >= min_size ? "(" : "_ext(") << d << ", " << HexConstWriter{mask} << ")];"; + } +} + +bool CppTypeCode::generate_get_tag_pfx_distinguisher(std::ostream& os, std::string nl, + const std::vector& constr_list, bool in_block) const { + if (constr_list.empty()) { + os << nl << " return -1;"; + return false; + } + if (constr_list.size() == 1) { + os << nl << " return " << cons_enum_name.at(constr_list[0]) << ";"; + return false; + } + std::unique_ptr trie; + for (int i : constr_list) { + trie = BinTrie::insert_paths(std::move(trie), type.constructors.at(i)->begins_with, 1ULL << i); + } + if (!trie) { + os << nl << " return -1;"; + return false; + } + int d = trie->compute_useful_depth(); + bool is_pfx_determ = !trie->find_conflict_path(); + assert(is_pfx_determ); + if (!in_block) { + os << " {"; + } + generate_tag_pfx_selector(os, nl, *trie, d, (int)(type.size.min_size() >> 8)); + return !in_block; +} + +void CppTypeCode::generate_get_tag_body(std::ostream& os, std::string nl) { + int d = type.useful_depth; + cons_tag_exact.resize(cons_num, false); + if (type.is_pfx_determ) { + if (!cons_num) { + os << nl << "return -1;"; + return; + } + if (!d) { + assert(simple_cons_tags && cons_num == 1); + cons_tag_exact[0] = !(type.constructors.at(0)->tag_bits); + os << nl << "return 0;"; + return; + } + int min_size = (int)(type.size.min_size() >> 8); + bool always_has = (d <= min_size); + if (d <= 6 && simple_cons_tags) { + unsigned long long sm = compute_selector_mask(); + if (always_has && sm + 1 == (2ULL << ((1 << d) - 1))) { + for (int i = 0; i < cons_num; i++) { + cons_tag_exact[i] = (type.constructors.at(i)->tag_bits <= d); + } + os << nl << "return (int)cs.prefetch_ulong(" << d << ");"; + return; + } + for (int i = 0; i < cons_num; i++) { + unsigned long long tag = type.constructors.at(i)->tag; + int l = 63 - td::count_trailing_zeroes_non_zero64(tag); + if (l <= d) { + int a = (int)((tag & (tag - 1)) >> (64 - d)); + int b = a + (1 << (d - l)); + cons_tag_exact[i] = ((sm >> a) & 1) && (b == (1 << d) || ((sm >> b) & 1)); + } + } + os << nl << "return cs.bselect" << (always_has ? "(" : "_ext(") << d << ", " << HexConstWriter{sm} << ");"; + return; + } + if (d <= 6) { + generate_tag_pfx_selector(os, nl, *(type.cs_trie), d, min_size); + return; + } + } + if (type.is_const_param_determ || type.is_const_param_pfx_determ) { + int p = type.const_param_idx; + assert(p >= 0); + std::vector param_values = type.get_all_param_values(p); + assert(param_values.size() > 1 && param_values.at(0) >= 0); + os << nl << "switch (" << type_param_name.at(p) << ") {"; + for (int pv : param_values) { + assert(pv >= 0); + os << nl << "case " << pv << ":"; + std::vector constr_list = type.get_constr_by_param_value(p, pv); + assert(!constr_list.empty()); + if (constr_list.size() == 1) { + os << nl << " return " << cons_enum_name.at(constr_list[0]) << ";"; + continue; + } + bool opbr = generate_get_tag_pfx_distinguisher(os, nl + " ", constr_list, false); + if (opbr) { + os << nl << "}"; + } + } + os << nl << "default:" << nl << " return -1;" << nl << "}"; + return; + } + if (d) { + int d1 = std::min(6, d); + int n = (1 << d1); + bool always_has = (d1 <= (int)(type.size.min_size() >> 8)); + unsigned long long A[64], B[64]; + unsigned long long mask = type.cs_trie->build_submap(d1, A); + int l = td::count_bits64(mask); + bool simple = (l > n / 2 || n <= 8); + if (!simple) { + int j = 0; + for (int i = 0; i < n; i++) { + if ((mask >> i) & 1) { + //std::cerr << i << ',' << std::hex << A[i] << std::dec << std::endl; + B[j] = (2 * i + 1ULL) << (63 - d1); + A[j++] = A[i]; + } + } + assert(j == l); + } else { + for (int i = 0; i < n; i++) { + B[i] = (2 * i + 1ULL) << (63 - d1); + } + l = n; + } + os << nl << "switch ("; + if (simple) { + os << "(int)cs.prefetch_ulong(" << d1; + } else { + os << "cs.bselect" << (always_has ? "(" : "_ext(") << d1 << ", " << HexConstWriter{mask}; + } + os << ")) {"; + for (int i = 0; i < l; i++) { + if (A[i] != 0) { + if ((long)A[i] > 0) { + int j; + for (j = 0; j < i; j++) { + if (A[j] == A[i]) { + break; + } + } + if (j < i) { + continue; + } + } + os << nl << "case " << i << ":"; + if ((long)A[i] > 0) { + int j; + for (j = i + 1; j < l; j++) { + if (A[j] == A[i]) { + os << " case " << j << ":"; + } + } + if (!(A[i] & (A[i] - 1))) { + os << nl << " return " << cons_enum_name.at(td::count_trailing_zeroes_non_zero64(A[i])) << ";"; + } + } else { + generate_get_tag_subcase(os, nl + " ", type.cs_trie->lookup_node_const(B[i]), d1); + } + } + } + os << nl << "default:" << nl << " return -1;" << nl << "}"; + } else { + generate_get_tag_subcase(os, nl, type.cs_trie.get(), 0); + } +} + +void CppTypeCode::generate_type_fields(std::ostream& os, int options) { + int st = -1; + for (int i = 0; i < tot_params; i++) { + if (type_param_is_neg[i]) { + continue; + } + int nst = type_param_is_nat[i]; + if (st != nst) { + if (st >= 0) { + os << ";\n"; + } + os << (nst ? " int " : " const TLB "); + st = nst; + } else { + os << ", "; + } + if (!nst) { + os << '&'; + } + os << type_param_name[i]; + } + if (st >= 0) { + os << ";\n"; + } +} + +static std::string constr_arg_name(std::string type_field_name) { + if (type_field_name.size() <= 1 || type_field_name.back() != '_') { + return std::string{"_"} + type_field_name; + } else { + return {type_field_name, 0, type_field_name.size() - 1}; + } +} + +void CppTypeCode::generate_type_constructor(std::ostream& os, int options) { + os << " " << cpp_type_class_name << "("; + for (int i = 0, j = 0; i < tot_params; i++) { + if (type_param_is_neg[i]) { + continue; + } + if (j++ > 0) { + os << ", "; + } + os << (type_param_is_nat[i] ? "int " : "const TLB& "); + os << constr_arg_name(type_param_name[i]); + } + os << ")"; + for (int i = 0, j = 0; i < tot_params; i++) { + if (type_param_is_neg[i]) { + continue; + } + if (j++ > 0) { + os << ", "; + } else { + os << " : "; + } + os << type_param_name[i] << "(" << constr_arg_name(type_param_name[i]) << ")"; + } + os << " {}\n"; +} + +void Action::show(std::ostream& os) const { + if (fixed_size >= 0) { + if (!fixed_size) { + os << "true"; + } else if (fixed_size < 0x10000) { + os << "cs.advance(" << fixed_size << ")"; + } else if (!(fixed_size & 0xffff)) { + os << "cs.advance_refs(" << (fixed_size >> 16) << ")"; + } else { + os << "cs.advance_ext(0x" << std::hex << fixed_size << std::dec << ")"; + } + } else { + os << action; + } +} + +bool Action::may_combine(const Action& next) const { + return !fixed_size || !next.fixed_size || (fixed_size >= 0 && next.fixed_size >= 0); +} + +bool Action::operator+=(const Action& next) { + if (!next.fixed_size) { + return true; + } + if (!fixed_size) { + fixed_size = next.fixed_size; + action = next.action; + return true; + } + if (fixed_size >= 0 && next.fixed_size >= 0) { + fixed_size += next.fixed_size; + return true; + } + return false; +} + +void operator+=(std::vector& av, const Action& next) { + if (av.empty() || !(av.back() += next)) { + if (next.is_constraint && !av.empty() && av.back().fixed_size >= 0) { + Action last = av.back(); + av.pop_back(); + av.push_back(next); + av.push_back(last); + } else { + av.push_back(next); + } + } +} + +void CppTypeCode::clear_context() { + actions.clear(); + incomplete = 0; + tmp_ints = 0; + needs_tmp_cell = false; + tmp_vars.clear(); + field_vars.clear(); + field_var_set.clear(); + param_var_set.clear(); + param_constraint_used.clear(); + tmp_cpp_ids.clear(); + tmp_cpp_ids.new_ident("cs"); + tmp_cpp_ids.new_ident("cb"); + tmp_cpp_ids.new_ident("cell_ref"); + tmp_cpp_ids.new_ident("t"); +} + +std::string CppTypeCode::new_tmp_var() { + char buffer[16]; + while (true) { + sprintf(buffer, "t%d", ++tmp_ints); + if (tmp_cpp_ids.is_good_ident(buffer) && local_cpp_ids.is_good_ident(buffer)) { + break; + } + } + std::string s{buffer}; + s = tmp_cpp_ids.new_ident(s); + tmp_vars.push_back(s); + return s; +} + +std::string CppTypeCode::new_tmp_var(std::string hint) { + if (hint.empty() || hint == "_") { + return new_tmp_var(); + } + int count = 0; + while (true) { + std::string s = local_cpp_ids.compute_cpp_ident(hint, count++); + if (tmp_cpp_ids.is_good_ident(s) && local_cpp_ids.is_good_ident(s)) { + s = tmp_cpp_ids.new_ident(s); + tmp_vars.push_back(s); + return s; + } + } +} + +std::string compute_type_class_name(const Type* typ, int& fake_arg) { + fake_arg = -1; + int idx = typ->type_idx; + if (idx >= builtin_types_num) { + return cpp_type[idx]->cpp_type_class_name; + } else if (typ->produces_nat) { + if (typ == Nat_type) { + return "Nat"; + } else if (typ == NatWidth_type) { + return "NatWidth"; + } else if (typ == NatLeq_type) { + return "NatLeq"; + } else if (typ == NatLess_type) { + return "NatLess"; + } + // ... + } else if (typ == Any_type) { + return "Anything"; + } else if (typ->has_fixed_size) { + fake_arg = (typ->size.min_size() >> 8); + int c = typ->get_name()[0]; + return (c == 'b') ? "Bits" : ((c == 'u') ? "UInt" : "Int"); + } else if (typ == Int_type) { + return "Int"; + } else if (typ == UInt_type) { + return "UInt"; + } else if (typ == Bits_type) { + return "Bits"; + } + return ""; +} + +std::string compute_type_expr_class_name(const TypeExpr* expr, int& fake_arg) { + switch (expr->tp) { + case TypeExpr::te_Apply: + return compute_type_class_name(expr->type_applied, fake_arg); + case TypeExpr::te_Ref: + return "RefT"; + case TypeExpr::te_Tuple: + return "TupleT"; + case TypeExpr::te_CondType: + return "CondT"; + } + return ""; +} + +void CppTypeCode::output_cpp_expr(std::ostream& os, const TypeExpr* expr, int prio, bool allow_type_neg) const { + if (expr->negated) { + if (!allow_type_neg || expr->tp != TypeExpr::te_Apply) { + throw src::Fatal{static_cast(std::ostringstream{} << "cannot convert negated expression `" + << expr << "` into C++ code") + .str()}; + } + } + int pos_args = 0; + for (const TypeExpr* arg : expr->args) { + pos_args += !arg->negated; + } + switch (expr->tp) { + case TypeExpr::te_Param: { + int i = expr->value; + assert(field_var_set.at(i)); + std::string fv = field_vars.at(i); + assert(!fv.empty()); + os << fv; + return; + } + case TypeExpr::te_Apply: + if (!pos_args && expr->type_applied->type_idx >= builtin_types_num) { + int type_idx = expr->type_applied->type_idx; + const CppTypeCode& cc = *cpp_type.at(type_idx); + assert(!cc.cpp_type_var_name.empty()); + os << cc.cpp_type_var_name; + return; + } + // fall through + case TypeExpr::te_Ref: + case TypeExpr::te_CondType: + case TypeExpr::te_Tuple: + if (expr->is_constexpr > 0) { + os << const_type_expr_cpp_idents.at(expr->is_constexpr); + return; + } else { + int fake_arg = -1; + os << compute_type_expr_class_name(expr, fake_arg); + os << "{"; + int c = 0; + if (fake_arg >= 0) { + os << fake_arg; + c = 1; + } + for (const TypeExpr* arg : expr->args) { + if (!arg->negated) { + os << (c++ ? ", " : ""); + output_cpp_expr(os, arg); + } + } + os << '}'; + return; + } + case TypeExpr::te_Add: + if (prio > 10) { + os << "("; + } + output_cpp_expr(os, expr->args[0], 10); + os << " + "; + output_cpp_expr(os, expr->args[1], 10); + if (prio > 10) { + os << ")"; + } + return; + case TypeExpr::te_MulConst: + if (prio > 20) { + os << "("; + } + os << expr->value; + os << " * "; + output_cpp_expr(os, expr->args[0], 20); + if (prio > 20) { + os << ")"; + } + return; + case TypeExpr::te_IntConst: + os << expr->value; + return; + } + os << ""; +} + +bool CppTypeCode::can_compute_sizeof(const TypeExpr* expr) const { + if (expr->negated || expr->is_nat) { + return false; + } + MinMaxSize sz = expr->compute_size(); + if (sz.is_fixed()) { + return !(sz.min_size() & 0xff); + } + if (expr->tp == TypeExpr::te_Apply && (expr->type_applied == Int_type || expr->type_applied == UInt_type || + expr->type_applied == NatWidth_type || expr->type_applied == Bits_type)) { + return true; + } + if (expr->tp != TypeExpr::te_CondType && expr->tp != TypeExpr::te_Tuple) { + return false; + } + return can_compute_sizeof(expr->args[1]); +} + +void CppTypeCode::output_cpp_sizeof_expr(std::ostream& os, const TypeExpr* expr, int prio) const { + if (expr->negated) { + throw src::Fatal{static_cast(std::ostringstream{} + << "cannot compute size of negated type expression `" << expr + << "` in C++ code") + .str()}; + } + if (expr->is_nat) { + throw src::Fatal{static_cast(std::ostringstream{} + << "cannot compute size of non-type expression `" << expr + << "` in C++ code") + .str()}; + } + MinMaxSize sz = expr->compute_size(); + if (sz.is_fixed()) { + os << SizeWriter{(int)sz.convert_min_size()}; + return; + } + switch (expr->tp) { + case TypeExpr::te_CondType: + if (prio > 5) { + os << '('; + } + output_cpp_expr(os, expr->args[0], 5); + os << " ? "; + output_cpp_sizeof_expr(os, expr->args[1], 6); + os << " : 0"; + if (prio > 5) { + os << ')'; + } + return; + case TypeExpr::te_Tuple: + if (expr->args[0]->tp == TypeExpr::te_IntConst && expr->args[0]->value == 1) { + output_cpp_sizeof_expr(os, expr->args[1], prio); + return; + } + sz = expr->args[1]->compute_size(); + if (sz.is_fixed() && sz.convert_min_size() == 1) { + output_cpp_expr(os, expr->args[0], prio); + return; + } + if (prio > 20) { + os << '('; + } + output_cpp_expr(os, expr->args[0], 20); + os << " * "; + output_cpp_sizeof_expr(os, expr->args[1], 20); + if (prio > 20) { + os << ')'; + } + return; + case TypeExpr::te_Apply: + if (expr->type_applied == Int_type || expr->type_applied == UInt_type || expr->type_applied == NatWidth_type || + expr->type_applied == Bits_type) { + output_cpp_expr(os, expr->args[0], prio); + return; + } + // no break + } + os << ""; +} + +bool CppTypeCode::can_compute(const TypeExpr* expr) const { + if (expr->negated) { + return false; + } + if (expr->tp == TypeExpr::te_Param) { + return field_var_set.at(expr->value); + } + for (const TypeExpr* arg : expr->args) { + if (!can_compute(arg)) { + return false; + } + } + return true; +} + +bool CppTypeCode::can_use_to_compute(const TypeExpr* expr, int i) const { + if (!expr->negated || !expr->is_nat) { + return false; + } + if (expr->tp == TypeExpr::te_Param) { + return expr->value == i; + } + for (const TypeExpr* arg : expr->args) { + if (!(arg->negated ? can_use_to_compute(arg, i) : can_compute(arg))) { + return false; + } + } + return true; +} + +void CppTypeCode::add_compute_actions(const TypeExpr* expr, int i, std::string bind_to) { + assert(expr->negated && expr->is_nat); + switch (expr->tp) { + case TypeExpr::te_MulConst: { + assert(expr->args.size() == 1 && expr->value > 0); + const TypeExpr* x = expr->args[0]; + assert(x->negated); + std::string tmp; + if (x->tp != TypeExpr::te_Param || (x->value != i && i >= 0)) { + tmp = new_tmp_var(); + } else { + i = x->value; + tmp = field_vars.at(i); + assert(!tmp.empty()); + assert(!field_var_set[i]); + field_var_set[i] = true; + x = nullptr; + } + std::ostringstream ss; + ss << "mul_r1(" << tmp << ", " << expr->value << ", " << bind_to << ")"; + actions += Action{std::move(ss), true}; + if (x) { + add_compute_actions(x, i, tmp); + } + return; + } + case TypeExpr::te_Add: { + assert(expr->args.size() == 2); + const TypeExpr *x = expr->args[0], *y = expr->args[1]; + assert(x->negated ^ y->negated); + if (!x->negated) { + std::swap(x, y); + } + std::string tmp; + if (x->tp != TypeExpr::te_Param || (x->value != i && i >= 0)) { + tmp = new_tmp_var(); + } else { + i = x->value; + tmp = field_vars.at(i); + assert(!tmp.empty()); + assert(!field_var_set[i]); + field_var_set[i] = true; + x = nullptr; + } + std::ostringstream ss; + ss << "add_r1(" << tmp << ", "; + output_cpp_expr(ss, y); + ss << ", " << bind_to << ")"; + actions += Action{std::move(ss), true}; + if (x) { + add_compute_actions(x, i, tmp); + } + return; + } + case TypeExpr::te_Param: + assert(expr->value == i || i < 0); + i = expr->value; + assert(!field_vars.at(i).empty()); + if (!field_var_set.at(i)) { + actions += Action{std::string{"("} + field_vars.at(i) + " = " + bind_to + ") >= 0"}; + field_var_set[i] = true; + } else { + actions += Action{field_vars.at(i) + " == " + bind_to}; + } + return; + } + throw src::Fatal{static_cast(std::ostringstream{} << "cannot use expression `" << expr << "` = " + << bind_to << " to set field variable " + << (i >= 0 ? field_vars.at(i) : "")) + .str()}; +} + +bool CppTypeCode::is_self(const TypeExpr* expr, const Constructor& constr) const { + if (expr->tp != TypeExpr::te_Apply || expr->type_applied != &type || (int)expr->args.size() != tot_params) { + return false; + } + assert(constr.params.size() == expr->args.size()); + for (int i = 0; i < tot_params; i++) { + assert(type_param_is_neg[i] == expr->args[i]->negated); + assert(type_param_is_neg[i] == constr.param_negated[i]); + if (!type_param_is_neg[i] && !expr->args[i]->equal(*constr.params[i])) { + return false; + } + } + return true; +} + +void CppTypeCode::init_cons_context(const Constructor& constr) { + clear_context(); + field_vars.resize(constr.fields.size()); + field_var_set.resize(constr.fields.size(), false); + param_var_set.resize(params + ret_params, false); + param_constraint_used.resize(params + ret_params, false); +} + +void CppTypeCode::identify_cons_params(const Constructor& constr, int options) { + int j = 0; + for (const TypeExpr* pexpr : constr.params) { + if (pexpr->tp == TypeExpr::te_Param) { + if (!type_param_is_neg.at(j)) { + int i = pexpr->value; + if (field_var_set.at(i)) { + // field i and parameter j must be equal + actions += Action{type_param_name.at(j) + " == " + field_vars.at(i)}; + param_constraint_used[j] = true; + } else if (field_vars.at(i).empty()) { + // identify field i with parameter j + field_vars[i] = type_param_name.at(j); + field_var_set[i] = true; + param_constraint_used[j] = true; + } + } else if (!(options & 2)) { + tmp_vars.push_back(type_param_name.at(j)); + } + } + j++; + } +} + +void CppTypeCode::identify_cons_neg_params(const Constructor& constr, int options) { + int j = 0; + for (const TypeExpr* pexpr : constr.params) { + if (pexpr->tp == TypeExpr::te_Param && type_param_is_neg.at(j)) { + int i = pexpr->value; + if (!field_var_set.at(i) && field_vars.at(i).empty()) { + // identify field i with parameter j + field_vars[i] = type_param_name.at(j); + param_constraint_used[j] = true; + } + } + j++; + } +} + +void CppTypeCode::add_cons_tag_check(const Constructor& constr, int cidx, int options) { + if (constr.tag_bits) { + if ((options & 1) && ((options & 8) || cons_num == 1 || !cons_tag_exact.at(cidx))) { + std::ostringstream ss; + int l = constr.tag_bits; + unsigned long long tag = (constr.tag >> (64 - l)); + if (l < 64) { + ss << "cs.fetch_ulong(" << l << ") == " << HexConstWriter{tag}; + } else { + ss << "cs.begins_with_skip(" << l << ", " << HexConstWriter{tag} << ")"; + } + actions.emplace_back(std::move(ss)); + } else { + actions.emplace_back(constr.tag_bits); + } + } +} + +void CppTypeCode::add_cons_tag_store(const Constructor& constr, int cidx) { + if (constr.tag_bits) { + std::ostringstream ss; + int l = constr.tag_bits; + unsigned long long tag = (constr.tag >> (64 - l)); + ss << "cb.store_long_bool(" << HexConstWriter{tag} << ", " << l << ")"; + actions.emplace_back(std::move(ss)); + } +} + +void CppTypeCode::add_remaining_param_constraints_check(const Constructor& constr, int options) { + int j = 0; + for (const TypeExpr* pexpr : constr.params) { + if (!param_constraint_used.at(j)) { + std::ostringstream ss; + if (!type_param_is_neg.at(j)) { + ss << type_param_name.at(j) << " == "; + output_cpp_expr(ss, pexpr); + actions += Action{std::move(ss)}; + } else if (options & 2) { + ss << "(" << type_param_name.at(j) << " = "; + output_cpp_expr(ss, pexpr); + ss << ") >= 0"; + actions += Action{std::move(ss), true}; + } + } + ++j; + } +} + +void CppTypeCode::output_actions(std::ostream& os, std::string nl, int options) { + bool opbr = false; + if (tmp_vars.size() || needs_tmp_cell) { + if (!(options & 4)) { + opbr = true; + os << " {"; + } + if (tmp_vars.size()) { + os << nl << "int"; + int c = 0; + for (auto t : tmp_vars) { + if (c++) { + os << ","; + } + os << " " << t; + } + os << ";"; + } + if (needs_tmp_cell) { + os << nl << "Ref tmp_cell;"; + } + } + if (!actions.size()) { + os << nl << "return true;"; + } else { + for (std::size_t i = 0; i < actions.size(); i++) { + os << nl << (i ? " && " : "return "); + actions[i].show(os); + } + os << ";"; + } + if (incomplete) { + os << nl << "// ???"; + } + if (opbr) { + os << nl << "}"; + } +} + +void CppTypeCode::compute_implicit_field(const Constructor& constr, const Field& field, int options) { + int i = field.field_idx; + if (field_vars.at(i).empty()) { + assert(!field_var_set.at(i)); + assert(field.type->is_nat_subtype); + std::string id = new_tmp_var(field.get_name()); + field_vars[i] = id; + } + int j = -1; + for (const TypeExpr* pexpr : constr.params) { + ++j; + if (!param_constraint_used.at(j) && !type_param_is_neg.at(j)) { + // std::cerr << "can_use_to_compute(" << pexpr << ", " << i << ") = " << can_use_to_compute(pexpr, i) << std::endl; + if (!field_var_set.at(i) && pexpr->tp == TypeExpr::te_Param && pexpr->value == i) { + std::ostringstream ss; + if (field.type->is_nat_subtype) { + ss << "(" << field_vars[i] << " = " << type_param_name.at(j) << ") >= 0"; + } else { + ss << "(" << field_vars[i] << " = &" << type_param_name.at(j) << ")"; + } + actions += Action{std::move(ss)}; + field_vars[i] = type_param_name[j]; + field_var_set[i] = true; + param_constraint_used[j] = true; + } else if (can_compute(pexpr)) { + std::ostringstream ss; + ss << type_param_name.at(j) << " == "; + output_cpp_expr(ss, pexpr); + actions += Action{std::move(ss), true}; + param_constraint_used[j] = true; + } else if (!field_var_set.at(i) && can_use_to_compute(pexpr, i)) { + add_compute_actions(pexpr, i, type_param_name.at(j)); + param_constraint_used[j] = true; + } + } + } +} + +bool CppTypeCode::add_constraint_check(const Constructor& constr, const Field& field, int options) { + const TypeExpr* expr = field.type; + if (expr->tp == TypeExpr::te_Apply && + (expr->type_applied == Eq_type || expr->type_applied == Less_type || expr->type_applied == Leq_type)) { + assert(expr->args.size() == 2); + const TypeExpr *x = expr->args[0], *y = expr->args[1]; + if (x->negated || y->negated) { + assert(expr->type_applied == Eq_type); + assert(x->negated ^ y->negated); + if (!x->negated) { + std::swap(x, y); + } + std::ostringstream ss; + output_cpp_expr(ss, y); + add_compute_actions(x, -1, ss.str()); + } else { + std::ostringstream ss; + output_cpp_expr(ss, x); + ss << (expr->type_applied == Eq_type ? " == " : (expr->type_applied == Less_type ? " < " : " <= ")); + output_cpp_expr(ss, y); + actions += Action{std::move(ss), true}; + } + return true; + } else { + // ... + ++incomplete; + actions += Action{"check_constraint_incomplete"}; + return false; + } +} + +void CppTypeCode::output_negative_type_arguments(std::ostream& os, const TypeExpr* expr) { + assert(expr->tp == TypeExpr::te_Apply); + for (const TypeExpr* arg : expr->args) { + if (arg->negated) { + int j = arg->value; + if (arg->tp == TypeExpr::te_Param && !field_var_set.at(j)) { + assert(!field_vars.at(j).empty()); + os << ", " << field_vars.at(j); + field_var_set[j] = true; + } else { + std::string tmp = new_tmp_var(); + os << ", " << tmp; + postponed_equate.emplace_back(tmp, arg); + } + } + } +} + +void CppTypeCode::add_postponed_equate_actions() { + for (const auto& p : postponed_equate) { + add_compute_actions(p.second, -1, p.first); + } + postponed_equate.clear(); +} + +std::string CppTypeCode::add_fetch_nat_field(const Constructor& constr, const Field& field, int options) { + const TypeExpr* expr = field.type; + int i = field.field_idx; + std::string id = field_vars.at(i); + if (id.empty()) { + field_vars[i] = id = new_tmp_var(field.get_name()); + } + const Type* ta = expr->type_applied; + assert(expr->tp == TypeExpr::te_Apply && + (ta == Nat_type || ta == NatWidth_type || ta == NatLeq_type || ta == NatLess_type)); + std::ostringstream ss; + ss << "cs."; + if (ta == Nat_type) { + ss << "fetch_uint_to(32, " << id << ")"; + } else if (ta == NatWidth_type && expr->args.at(0)->tp == TypeExpr::te_IntConst && expr->args[0]->value == 1) { + ss << "fetch_bool_to(" << id << ")"; + } else { + if (ta == NatWidth_type) { + ss << "fetch_uint_to("; + } else if (ta == NatLeq_type) { + ss << "fetch_uint_leq("; + } else if (ta == NatLess_type) { + ss << "fetch_uint_less("; + } + output_cpp_expr(ss, expr->args[0]); + ss << ", " << id << ")"; + } + actions += Action{std::move(ss)}; + field_var_set[i] = true; + return id; +} + +void CppTypeCode::add_store_nat_field(const Constructor& constr, const Field& field, int options) { + const TypeExpr* expr = field.type; + int i = field.field_idx; + std::string id = field_vars.at(i); + assert(!id.empty()); + const Type* ta = expr->type_applied; + assert(expr->tp == TypeExpr::te_Apply && + (ta == Nat_type || ta == NatWidth_type || ta == NatLeq_type || ta == NatLess_type)); + std::ostringstream ss; + ss << "cb."; + if (ta == Nat_type) { + ss << "store_ulong_rchk_bool(" << id << ", 32)"; + } else if (ta == NatWidth_type) { + if (expr->args.at(0)->tp == TypeExpr::te_IntConst && expr->args[0]->value == 1) { + ss << "store_ulong_rchk_bool(" << id << ", 1)"; + } else { + ss << "store_ulong_rchk_bool(" << id << ", "; + output_cpp_expr(ss, expr->args[0]); + ss << ")"; + } + } else if (ta == NatLeq_type) { + ss << "store_uint_leq("; + output_cpp_expr(ss, expr->args[0]); + ss << ", " << id << ")"; + } else if (ta == NatLess_type) { + ss << "store_uint_less("; + output_cpp_expr(ss, expr->args[0]); + ss << ", " << id << ")"; + } else { + ss << "(" << id << ")"; + } + actions += Action{std::move(ss)}; + field_var_set[i] = true; +} + +void CppTypeCode::generate_skip_field(const Constructor& constr, const Field& field, int options) { + const TypeExpr* expr = field.type; + MinMaxSize sz = expr->compute_size(); + bool any_bits = expr->compute_any_bits(); + bool validating = (options & 1); + // std::cerr << "field `" << field.get_name() << "` size is " << sz << "; fixed=" << sz.is_fixed() << "; any=" << any_bits << std::endl; + if (field.used || (validating && expr->is_nat_subtype && !any_bits)) { + // an explicit field of type # or ## which is used later or its value is not arbitrary + // (must load the value into an integer variable and check) + assert(expr->is_nat_subtype && "cannot use fields of non-`#` type"); + add_fetch_nat_field(constr, field, options); + return; + } + if (sz.is_fixed() && (!validating || (!(sz.min_size() & 0xff) && any_bits))) { + // field has fixed size, and either its bits can have arbitrary values (and it has no references) + // ... or we are not validating + // simply skip the necessary amount of bits + // NB: if the field is a reference, and we are not validating, we arrive here + actions += Action{(int)sz.convert_min_size()}; + return; + } + if (expr->negated) { + // the field type has some "negative" parameters, which will be computed while checking this field + // must invoke the correct validate_skip or skip method for the type in question + std::ostringstream ss; + if (!is_self(expr, constr)) { + output_cpp_expr(ss, expr, 100, true); + ss << '.'; + } + ss << (validating ? "validate_skip(cs, weak" : "skip(cs"); + output_negative_type_arguments(ss, expr); + ss << ")"; + actions += Action{std::move(ss)}; + add_postponed_equate_actions(); + return; + } + // at this point, if the field type is a reference, we must be validating + if (expr->tp == TypeExpr::te_Ref && expr->args[0]->tp == TypeExpr::te_Apply && + (expr->args[0]->type_applied == Cell_type || expr->args[0]->type_applied == Any_type)) { + // field type is a reference to a cell with arbitrary contents + actions += Action{0x10000}; + return; + } + // remaining case: general positive type expression + std::ostringstream ss; + std::string tail; + while (expr->tp == TypeExpr::te_CondType) { + // optimization for (chains of) conditional types ( x?type ) + assert(expr->args.size() == 2); + ss << "(!"; + output_cpp_expr(ss, expr->args[0], 30); + ss << " || "; + expr = expr->args[1]; + tail = std::string{")"} + tail; + } + if ((!validating || any_bits) && can_compute_sizeof(expr)) { + // field size can be computed at run-time, and either the contents is arbitrary, or we are not validating + ss << "cs.advance("; + output_cpp_sizeof_expr(ss, expr, 0); + ss << ")" << tail; + actions += Action{std::move(ss)}; + return; + } + if (expr->tp != TypeExpr::te_Ref) { + // field type is not a reference, generate a type expression and invoke skip/validate_skip method + if (!is_self(expr, constr)) { + output_cpp_expr(ss, expr, 100); + ss << '.'; + } + ss << (validating ? "validate_skip(cs, weak)" : "skip(cs)") << tail; + actions += Action{std::move(ss)}; + return; + } + // the (remaining) field type is a reference + if (!validating || (expr->args[0]->tp == TypeExpr::te_Apply && + (expr->args[0]->type_applied == Cell_type || expr->args[0]->type_applied == Any_type))) { + // the subcase when the field type is either a reference to a cell with arbitrary contents + // or it is a reference, and we are not validating, so we simply skip the reference + ss << "cs.advance_refs(1)" << tail; + actions += Action{std::move(ss)}; + return; + } + // general reference type, invoke validate_skip_ref() + // (notice that we are necessarily validating at this point) + expr = expr->args[0]; + if (!is_self(expr, constr)) { + output_cpp_expr(ss, expr, 100); + ss << '.'; + } + ss << "validate_skip_ref(cs, weak)" << tail; + actions += Action{ss.str()}; +} + +void CppTypeCode::generate_skip_cons_method(std::ostream& os, std::string nl, int cidx, int options) { + const Constructor& constr = *(type.constructors.at(cidx)); + init_cons_context(constr); + identify_cons_params(constr, options); + identify_cons_neg_params(constr, options); + add_cons_tag_check(constr, cidx, options); + for (const Field& field : constr.fields) { + if (!field.implicit) { + generate_skip_field(constr, field, options); + } else if (!field.constraint) { + compute_implicit_field(constr, field, options); + } else { + add_constraint_check(constr, field, options); + } + } + add_remaining_param_constraints_check(constr, options); + output_actions(os, nl, options); + clear_context(); +} + +void CppTypeCode::generate_skip_method(std::ostream& os, int options) { + bool validate = options & 1; + bool ret_ext = options & 2; + os << "\nbool " << cpp_type_class_name << "::" << (validate ? "validate_" : "") << "skip(vm::CellSlice& cs" + << (validate ? ", bool weak" : ""); + if (ret_ext) { + os << skip_extra_args; + } + os << ") const {"; + if (cons_num > 1) { + os << "\n switch (get_tag(cs)) {\n"; + for (int i = 0; i < cons_num; i++) { + os << " case " << cons_enum_name[i] << ":"; + generate_skip_cons_method(os, "\n ", i, options & ~4); + os << "\n"; + } + os << " }\n return false;\n"; + } else if (cons_num == 1) { + generate_skip_cons_method(os, "\n ", 0, options | 4); + os << "\n"; + } else { + os << "\n return false;\n"; + } + os << "}\n"; +} + +void CppTypeCode::generate_cons_tag_check(std::ostream& os, std::string nl, int cidx, bool force) { + const Constructor& constr = *(type.constructors.at(cidx)); + if (!constr.tag_bits) { + os << nl << "return " << cons_enum_name[cidx] << ";"; + } else if (force || cons_num == 1 || !cons_tag_exact.at(cidx)) { + os << nl << "return "; + int l = constr.tag_bits; + unsigned long long tag = (constr.tag >> (64 - l)); + if (l < 64 || tag != ~0ULL) { + os << "cs.prefetch_ulong(" << l << ") == " << HexConstWriter{tag}; + } else { + os << "cs.begins_with(" << l << ", " << HexConstWriter{tag} << ")"; + } + os << " ? " << cons_enum_name[cidx] << " : -1;"; + } else { + os << nl << "return cs.have(" << constr.tag_bits << ") ? " << cons_enum_name[cidx] << " : -1;"; + } +} + +void CppTypeCode::generate_check_tag_method(std::ostream& os) { + os << "\nint " << cpp_type_class_name << "::check_tag(const vm::CellSlice& cs) const {"; + if (cons_num > 1) { + os << "\n switch (get_tag(cs)) {\n"; + for (int i = 0; i < cons_num; i++) { + os << " case " << cons_enum_name[i] << ":"; + generate_cons_tag_check(os, "\n ", i); + os << "\n"; + } + os << " }\n return -1;\n"; + } else if (cons_num == 1) { + generate_cons_tag_check(os, "\n ", 0); + os << "\n"; + } else { + os << "\n return -1;\n"; + } + os << "}\n"; +} + +bool CppTypeCode::output_print_simple_field(std::ostream& os, const Field& field, std::string field_name, + const TypeExpr* expr) { + cpp_val_type cvt = detect_cpp_type(expr); + MinMaxSize sz = expr->compute_size(); + int i = expr->is_integer(); + int l = (sz.is_fixed() ? sz.convert_min_size() : -1); + switch (cvt) { + case ct_bitstring: + case ct_bits: + assert(!(sz.max_size() & 0xff)); + os << "pp.fetch_bits_field(cs, "; + output_cpp_sizeof_expr(os, expr, 0); + if (!field_name.empty()) { + os << ", \"" << field_name << '"'; + } + os << ")"; + return true; + case ct_bool: + case ct_int32: + case ct_uint32: + case ct_int64: + case ct_uint64: + assert(i && l <= 64); + os << "pp.fetch_" << (i > 0 ? "u" : "") << "int_field(cs, "; + output_cpp_sizeof_expr(os, expr, 0); + if (!field_name.empty()) { + os << ", \"" << field_name << '"'; + } + os << ")"; + return true; + case ct_integer: + assert(i); + os << "pp.fetch_" << (i > 0 ? "u" : "") << "int256_field(cs, "; + output_cpp_sizeof_expr(os, expr, 0); + if (!field_name.empty()) { + os << ", \"" << field_name << '"'; + } + os << ")"; + return true; + default: + break; + } + return false; +} + +void CppTypeCode::generate_print_field(const Constructor& constr, const Field& field, int options) { + const TypeExpr* expr = field.type; + MinMaxSize sz = expr->compute_size(); + cpp_val_type cvt = detect_cpp_type(expr); + bool any_bits = expr->compute_any_bits(); + bool is_simple = (cvt >= ct_bits && cvt <= ct_uint64 && cvt != ct_enum); + // std::cerr << "field `" << field.get_name() << "` size is " << sz << "; fixed=" << sz.is_fixed() << "; any=" << any_bits << std::endl; + std::string field_name = field.name ? field.get_name() : ""; + if (field.used || expr->is_nat_subtype) { + // an explicit field of type # or ## + assert(expr->is_nat_subtype && "cannot use fields of non-`#` type"); + std::ostringstream ss; + ss << "pp.field_int(" << add_fetch_nat_field(constr, field, options); + if (field.name) { + ss << ", \"" << field_name << '"'; + } + ss << ')'; + actions += Action{std::move(ss)}; + return; + } + if (sz.is_fixed() && !(sz.min_size() & 0xff) && any_bits) { + // field has fixed size, and either its bits can have arbitrary values (and it has no references) + std::ostringstream ss; + if (output_print_simple_field(ss, field, field_name, expr)) { + actions += Action{std::move(ss)}; + return; + } + } + bool cond_chain = (expr->tp == TypeExpr::te_CondType); + if (!cond_chain && !is_simple) { + if (field.name) { + actions += Action{std::string{"pp.field(\""} + field_name + "\")"}; + } else { + actions += Action{"pp.field()"}; + } + } + if (expr->negated) { + // the field type has some "negative" parameters, which will be computed while checking this field + // must invoke the correct validate_skip or skip method for the type in question + assert(!cond_chain); + std::ostringstream ss; + if (!is_self(expr, constr)) { + output_cpp_expr(ss, expr, 100, true); + ss << '.'; + } + ss << "print_skip(pp, cs"; + output_negative_type_arguments(ss, expr); + ss << ")"; + actions += Action{std::move(ss)}; + add_postponed_equate_actions(); + return; + } + // remaining case: general positive type expression + std::ostringstream ss; + std::string tail; + while (expr->tp == TypeExpr::te_CondType) { + // optimization for (chains of) conditional types ( x?type ) + assert(expr->args.size() == 2); + ss << "(!"; + output_cpp_expr(ss, expr->args[0], 30); + ss << " || "; + expr = expr->args[1]; + tail += ')'; + } + if (output_print_simple_field(ss, field, field_name, expr)) { + ss << tail; + actions += Action{std::move(ss)}; + return; + } + if (cond_chain) { + if (field.name) { + ss << "(pp.field(\"" + field_name + "\") && "; + } else { + ss << "(pp.field() && "; + } + tail += ')'; + } + if (expr->tp != TypeExpr::te_Ref) { + // field type is not a reference, generate a type expression and invoke skip/validate_skip method + if (!is_self(expr, constr)) { + output_cpp_expr(ss, expr, 100); + ss << '.'; + } + ss << "print_skip(pp, cs)" << tail; + actions += Action{std::move(ss)}; + return; + } + // general reference type, invoke print_ref() + expr = expr->args[0]; + if (!is_self(expr, constr)) { + output_cpp_expr(ss, expr, 100); + ss << '.'; + } + ss << "print_ref(pp, cs.fetch_ref())" << tail; + actions += Action{ss.str()}; +} + +void CppTypeCode::generate_print_cons_method(std::ostream& os, std::string nl, int cidx, int options) { + const Constructor& constr = *(type.constructors.at(cidx)); + init_cons_context(constr); + identify_cons_params(constr, options); + identify_cons_neg_params(constr, options); + add_cons_tag_check(constr, cidx, options); + bool do_open = !constr.is_enum; + if (do_open) { + if (constr.constr_name) { + actions += Action{std::string{"pp.open(\""} + constr.get_name() + "\")"}; + } else { + actions += Action{"pp.open()"}; + } + } else { + actions += Action{std::string{"pp.cons(\""} + constr.get_name() + "\")"}; + } + for (const Field& field : constr.fields) { + if (!field.implicit) { + generate_print_field(constr, field, options); + } else if (!field.constraint) { + compute_implicit_field(constr, field, options); + } else { + add_constraint_check(constr, field, options); + } + } + add_remaining_param_constraints_check(constr, options); + if (do_open) { + actions += Action{"pp.close()"}; + } + output_actions(os, nl, options); + clear_context(); +} + +void CppTypeCode::generate_print_method(std::ostream& os, int options) { + bool ret_ext = options & 2; + os << "\nbool " << cpp_type_class_name << "::print_skip(PrettyPrinter& pp, vm::CellSlice& cs"; + if (ret_ext) { + os << skip_extra_args; + } + os << ") const {"; + if (cons_num > 1) { + os << "\n switch (get_tag(cs)) {\n"; + for (int i = 0; i < cons_num; i++) { + os << " case " << cons_enum_name[i] << ":"; + generate_print_cons_method(os, "\n ", i, options & ~4); + os << "\n"; + } + os << " }\n return pp.fail(\"unknown constructor for " << type.get_name() << "\");\n"; + } else if (cons_num == 1) { + generate_print_cons_method(os, "\n ", 0, options | 4); + os << "\n"; + } else { + os << "\n return pp.fail(\"no constructors for " << type.get_name() << "\");\n"; + } + os << "}\n"; +} + +void CppTypeCode::bind_record_fields(const CppTypeCode::ConsRecord& rec, int options) { + bool direct = options & 8; + bool read_only = options & 32; + for (const ConsField& fi : rec.cpp_fields) { + int i = fi.orig_idx; + assert(field_vars.at(i).empty() && !field_var_set.at(i)); + if (!read_only || !rec.constr.fields.at(i).implicit) { + field_vars[i] = direct ? fi.name : std::string{"data."} + fi.name; + field_var_set[i] = read_only; + } + } +} + +void CppTypeCode::output_fetch_field(std::ostream& os, std::string field_var, const TypeExpr* expr, cpp_val_type cvt) { + int i = expr->is_integer(); + MinMaxSize sz = expr->compute_size(); + int l = (sz.is_fixed() ? sz.convert_min_size() : -1); + switch (cvt) { + case ct_slice: + os << "cs.fetch_subslice_" << (sz.max_size() & 0xff ? "ext_" : "") << "to("; + output_cpp_sizeof_expr(os, expr, 0); + os << ", " << field_var << ")"; + return; + case ct_bitstring: + assert(!(sz.max_size() & 0xff)); + os << "cs.fetch_bitstring_to("; + output_cpp_sizeof_expr(os, expr, 0); + os << ", " << field_var << ")"; + return; + case ct_bits: + assert(l >= 0 && l < 0x10000); + os << "cs.fetch_bits_to(" << field_var << ".bits(), " << l << ")"; + return; + case ct_cell: + assert(l == 0x10000); + os << "cs.fetch_ref_to(" << field_var << ")"; + return; + case ct_bool: + assert(i > 0 && l == 1); + os << "cs.fetch_bool_to(" << field_var << ")"; + return; + case ct_int32: + case ct_uint32: + case ct_int64: + case ct_uint64: + assert(i && l <= 64); + os << "cs.fetch_" << (i > 0 ? "u" : "") << "int_to("; + output_cpp_sizeof_expr(os, expr, 0); + os << ", " << field_var << ")"; + return; + case ct_integer: + assert(i); + os << "cs.fetch_" << (i > 0 ? "u" : "") << "int256_to("; + output_cpp_sizeof_expr(os, expr, 0); + os << ", " << field_var << ")"; + return; + default: + break; + } + throw src::Fatal{"cannot fetch a field of unknown scalar type"}; +} + +void CppTypeCode::output_fetch_subrecord(std::ostream& os, std::string field_name, const ConsRecord* subrec) { + assert(subrec); + os << subrec->cpp_type.cpp_type_var_name << ".cell_unpack(cs.fetch_ref(), " << field_name << ")"; +} + +void CppTypeCode::generate_unpack_field(const CppTypeCode::ConsField& fi, const Constructor& constr, const Field& field, + int options) { + int i = field.field_idx; + const TypeExpr* expr = field.type; + MinMaxSize sz = expr->compute_size(); + bool any_bits = expr->compute_any_bits(); + bool validating = (options & 1); + cpp_val_type cvt = fi.ctype; + // std::cerr << "field `" << field.get_name() << "` size is " << sz << "; fixed=" << sz.is_fixed() << "; any=" << any_bits << std::endl; + if (field.used || expr->is_nat_subtype) { + assert(expr->is_nat_subtype && "cannot use fields of non-`#` type"); + assert(cvt == ct_int32 || cvt == ct_bool); + add_fetch_nat_field(constr, field, options); + return; + } + if (sz.is_fixed() && cvt != ct_enum && (!validating || (!(sz.min_size() & 0xff) && any_bits))) { + // field has fixed size, and either its bits can have arbitrary values (and it has no references) + // ... or we are not validating + // simply skip the necessary amount of bits + // NB: if the field is a reference, and we are not validating, we arrive here + if (cvt == ct_cell) { + assert(sz.min_size() == 1); + } + std::ostringstream ss; + if (cvt == ct_subrecord && field.subrec) { + output_fetch_subrecord(ss, field_vars.at(i), fi.subrec); + } else { + output_fetch_field(ss, field_vars.at(i), expr, cvt); + } + actions += Action{std::move(ss)}; + field_var_set[i] = true; + return; + } + if (expr->negated) { + // the field type has some "negative" parameters, which will be computed while checking this field + // must invoke the correct validate_skip or skip method for the type in question + std::ostringstream ss; + assert(cvt == ct_slice); + if (!is_self(expr, constr)) { + output_cpp_expr(ss, expr, 100, true); + ss << '.'; + } + ss << (validating ? "validate_fetch_to(cs, weak, " : "fetch_to(cs, ") << field_vars.at(i); + output_negative_type_arguments(ss, expr); + ss << ")"; + actions += Action{std::move(ss)}; + add_postponed_equate_actions(); + field_var_set[i] = true; + return; + } + // at this point, if the field type is a reference, we must be validating + if (expr->tp == TypeExpr::te_Ref && expr->args[0]->tp == TypeExpr::te_Apply && + (expr->args[0]->type_applied == Cell_type || expr->args[0]->type_applied == Any_type)) { + // field type is a reference to a cell with arbitrary contents + assert(cvt == ct_cell); + actions += Action{"cs.fetch_ref_to(" + field_vars.at(i) + ")"}; + field_var_set[i] = true; + return; + } + // remaining case: general positive type expression + std::ostringstream ss; + std::string tail; + while (expr->tp == TypeExpr::te_CondType) { + // optimization for (chains of) conditional types ( x?type ) + assert(expr->args.size() == 2); + ss << "(!"; + output_cpp_expr(ss, expr->args[0], 30); + ss << " || "; + expr = expr->args[1]; + tail = std::string{")"} + tail; + } + if ((!validating || any_bits) && can_compute_sizeof(expr) && cvt != ct_enum) { + // field size can be computed at run-time, and either the contents is arbitrary, or we are not validating + output_fetch_field(ss, field_vars.at(i), expr, cvt); + field_var_set[i] = true; + ss << tail; + actions += Action{std::move(ss)}; + return; + } + if (expr->tp != TypeExpr::te_Ref) { + // field type is not a reference, generate a type expression and invoke skip/validate_skip method + assert(cvt == ct_slice || cvt == ct_enum); + if (!is_self(expr, constr)) { + output_cpp_expr(ss, expr, 100); + ss << '.'; + } + ss << (validating ? "validate_" : "") << "fetch_" << (cvt == ct_enum ? "enum_" : "") << "to(cs, " + << (validating ? "weak, " : "") << field_vars.at(i) << ")" << tail; + field_var_set[i] = true; + actions += Action{std::move(ss)}; + return; + } + // the (remaining) field type is a reference + if (!validating || (expr->args[0]->tp == TypeExpr::te_Apply && + (expr->args[0]->type_applied == Cell_type || expr->args[0]->type_applied == Any_type))) { + // the subcase when the field type is either a reference to a cell with arbitrary contents + // or it is a reference, and we are not validating, so we simply skip the reference + assert(cvt == ct_cell); + ss << "cs.fetch_ref_to(" << field_vars.at(i) << ")" << tail; + field_var_set[i] = true; + actions += Action{std::move(ss)}; + return; + } + // general reference type, invoke validate_skip_ref() + // (notice that we are necessarily validating at this point) + expr = expr->args[0]; + assert(cvt == ct_cell); + ss << "(cs.fetch_ref_to(" << field_vars.at(i) << ") && "; + if (!is_self(expr, constr)) { + output_cpp_expr(ss, expr, 100); + ss << '.'; + } + ss << "validate_ref(" << field_vars.at(i) << "))" << tail; + actions += Action{ss.str()}; +} + +void CppTypeCode::generate_unpack_method(std::ostream& os, CppTypeCode::ConsRecord& rec, int options) { + std::ostringstream tmp; + if (!rec.declare_record_unpack(tmp, "", options)) { + return; + } + tmp.clear(); + os << "\n"; + bool res = rec.declare_record_unpack(os, "", options | 3072); + DCHECK(res); + if (options & 16) { + // cell unpack version + os << "\n if (cell_ref.is_null()) { return false; }" + << "\n auto cs = load_cell_slice(std::move(cell_ref));" + << "\n return " << (options & 1 ? "validate_" : "") << "unpack"; + if (!(options & 8)) { + os << "(cs, data"; + } else { + os << "_" << cons_enum_name.at(rec.cons_idx) << "(cs"; + for (const auto& f : rec.cpp_fields) { + os << ", " << f.name; + } + } + if (options & 2) { + os << skip_extra_args_pass; + } + os << ") && cs.empty_ext();\n}\n"; + return; + } + init_cons_context(rec.constr); + bind_record_fields(rec, options); + identify_cons_params(rec.constr, options); + identify_cons_neg_params(rec.constr, options); + add_cons_tag_check(rec.constr, rec.cons_idx, 9 /* (options & 1) | 8 */); + auto it = rec.cpp_fields.cbegin(), end = rec.cpp_fields.cend(); + for (const Field& field : rec.constr.fields) { + if (field.constraint) { + add_constraint_check(rec.constr, field, options); + continue; + } + if (!field.implicit) { + assert(it < end && it->orig_idx == field.field_idx); + generate_unpack_field(*it++, rec.constr, field, options); + } else { + if (it < end && it->orig_idx == field.field_idx) { + ++it; + } + compute_implicit_field(rec.constr, field, options); + } + } + assert(it == end); + add_remaining_param_constraints_check(rec.constr, options); + output_actions(os, "\n ", options | 4); + clear_context(); + os << "\n}\n"; +} + +void CppTypeCode::output_store_field(std::ostream& os, std::string field_var, const TypeExpr* expr, cpp_val_type cvt) { + int i = expr->is_integer(); + MinMaxSize sz = expr->compute_size(); + int l = (sz.is_fixed() ? sz.convert_min_size() : -1); + switch (cvt) { + case ct_slice: + os << "cb.append_cellslice_chk(" << field_var << ", "; + output_cpp_sizeof_expr(os, expr, 0); + os << ")"; + return; + case ct_bitstring: + assert(!(sz.max_size() & 0xff)); + os << "cb.append_bitstring_chk(" << field_var << ", "; + output_cpp_sizeof_expr(os, expr, 0); + os << ")"; + return; + case ct_bits: + assert(l >= 0 && l < 0x10000); + os << "cb.store_bits_bool(" << field_var << ".cbits(), " << l << ")"; + return; + case ct_cell: + assert(l == 0x10000); + os << "cb.store_ref_bool(" << field_var << ")"; + return; + case ct_bool: + assert(i > 0 && l == 1); + // os << "cb.store_bool(" << field_var << ")"; + // return; + // fall through + case ct_int32: + case ct_uint32: + case ct_int64: + case ct_uint64: + assert(i && l <= 64); + os << "cb.store_" << (i > 0 ? "u" : "") << "long_rchk_bool(" << field_var << ", "; + output_cpp_sizeof_expr(os, expr, 0); + os << ")"; + return; + case ct_integer: + assert(i); + os << "cb.store_int256_bool(" << field_var << ", "; + output_cpp_sizeof_expr(os, expr, 0); + os << (i > 0 ? ", false)" : ")"); + return; + default: + break; + } + throw src::Fatal{"cannot store a field of unknown scalar type"}; +} + +void CppTypeCode::add_store_subrecord(std::string field_name, const ConsRecord* subrec) { + assert(subrec); + needs_tmp_cell = true; + std::ostringstream ss; + ss << subrec->cpp_type.cpp_type_var_name << ".cell_pack(tmp_cell, " << field_name << ")"; + actions += Action{std::move(ss)}; + actions += Action{"cb.store_ref_bool(std::move(tmp_cell))"}; +} + +void CppTypeCode::generate_pack_field(const CppTypeCode::ConsField& fi, const Constructor& constr, const Field& field, + int options) { + int i = field.field_idx; + const TypeExpr* expr = field.type; + MinMaxSize sz = expr->compute_size(); + bool any_bits = expr->compute_any_bits(); + bool validating = (options & 1); + cpp_val_type cvt = fi.ctype; + // std::cerr << "field `" << field.get_name() << "` size is " << sz << "; fixed=" << sz.is_fixed() << "; any=" << any_bits << std::endl; + if (field.used || expr->is_nat_subtype) { + assert(expr->is_nat_subtype && "cannot use fields of non-`#` type"); + assert(cvt == ct_int32 || cvt == ct_bool); + add_store_nat_field(constr, field, options); + return; + } + if (sz.is_fixed() && cvt != ct_enum && (!validating || (!(sz.min_size() & 0xff) && any_bits))) { + // field has fixed size, and either its bits can have arbitrary values (and it has no references) + // ... or we are not validating + // simply skip the necessary amount of bits + // NB: if the field is a reference, and we are not validating, we arrive here + if (cvt == ct_cell) { + assert(sz.min_size() == 1); + } + if (cvt == ct_subrecord && field.subrec) { + add_store_subrecord(field_vars.at(i), fi.subrec); + } else { + std::ostringstream ss; + output_store_field(ss, field_vars.at(i), expr, cvt); + actions += Action{std::move(ss)}; + } + field_var_set[i] = true; + return; + } + if (expr->negated) { + // the field type has some "negative" parameters, which will be computed while checking this field + // must invoke the correct validate_skip or skip method for the type in question + std::ostringstream ss; + assert(cvt == ct_slice); + ss << "tlb::" << (validating ? "validate_" : "") << "store_from(cb, "; + if (!is_self(expr, constr)) { + output_cpp_expr(ss, expr, 5, true); + } else { + ss << "*this"; + } + ss << ", " << field_vars.at(i); + output_negative_type_arguments(ss, expr); + ss << ")"; + actions += Action{std::move(ss)}; + add_postponed_equate_actions(); + field_var_set[i] = true; + return; + } + // at this point, if the field type is a reference, we must be validating + if (expr->tp == TypeExpr::te_Ref && expr->args[0]->tp == TypeExpr::te_Apply && + (expr->args[0]->type_applied == Cell_type || expr->args[0]->type_applied == Any_type)) { + // field type is a reference to a cell with arbitrary contents + assert(cvt == ct_cell); + actions += Action{"cb.store_ref_bool(" + field_vars.at(i) + ")"}; + field_var_set[i] = true; + return; + } + // remaining case: general positive type expression + std::ostringstream ss; + std::string tail; + while (expr->tp == TypeExpr::te_CondType) { + // optimization for (chains of) conditional types ( x?type ) + assert(expr->args.size() == 2); + ss << "(!"; + output_cpp_expr(ss, expr->args[0], 30); + ss << " || "; + expr = expr->args[1]; + tail = std::string{")"} + tail; + } + if ((!validating || any_bits) && can_compute_sizeof(expr) && cvt != ct_enum) { + // field size can be computed at run-time, and either the contents is arbitrary, or we are not validating + output_store_field(ss, field_vars.at(i), expr, cvt); + field_var_set[i] = true; + ss << tail; + actions += Action{std::move(ss)}; + return; + } + if (expr->tp != TypeExpr::te_Ref) { + // field type is not a reference, generate a type expression and invoke skip/validate_skip method + assert(cvt == ct_slice || cvt == ct_enum); + if (!is_self(expr, constr)) { + output_cpp_expr(ss, expr, 100); + ss << '.'; + } + ss << (validating ? "validate_" : "") << "store_" << (cvt == ct_enum ? "enum_" : "") << "from(cb, " + << field_vars.at(i) << ")" << tail; + field_var_set[i] = true; + actions += Action{std::move(ss)}; + return; + } + // the (remaining) field type is a reference + if (!validating || (expr->args[0]->tp == TypeExpr::te_Apply && + (expr->args[0]->type_applied == Cell_type || expr->args[0]->type_applied == Any_type))) { + // the subcase when the field type is either a reference to a cell with arbitrary contents + // or it is a reference, and we are not validating, so we simply skip the reference + assert(cvt == ct_cell); + ss << "cb.store_ref_bool(" << field_vars.at(i) << ")" << tail; + field_var_set[i] = true; + actions += Action{std::move(ss)}; + return; + } + // general reference type, invoke validate_skip_ref() + // (notice that we are necessarily validating at this point) + expr = expr->args[0]; + assert(cvt == ct_cell); + ss << "(cb.store_ref_bool(" << field_vars.at(i) << ") && "; + if (!is_self(expr, constr)) { + output_cpp_expr(ss, expr, 100); + ss << '.'; + } + ss << "validate_ref(" << field_vars.at(i) << "))" << tail; + actions += Action{ss.str()}; +} + +void CppTypeCode::generate_pack_method(std::ostream& os, CppTypeCode::ConsRecord& rec, int options) { + std::ostringstream tmp; + if (!rec.declare_record_pack(tmp, "", options)) { + return; + } + tmp.clear(); + os << "\n"; + bool res = rec.declare_record_pack(os, "", options | 3072); + DCHECK(res); + if (options & 16) { + // cell pack version + os << "\n vm::CellBuilder cb;" + << "\n return " << (options & 1 ? "validate_" : "") << "pack"; + if (!(options & 8)) { + os << "(cb, data"; + } else { + os << "_" << cons_enum_name.at(rec.cons_idx) << "(cb"; + for (const auto& f : rec.cpp_fields) { + // skip SOME implicit fields ??? + if (f.implicit) { + } else if (f.get_cvt().needs_move()) { + os << ", std::move(" << f.name << ")"; + } else { + os << ", " << f.name; + } + } + } + if (options & 2) { + os << skip_extra_args_pass; + } + os << ") && std::move(cb).finalize_to(cell_ref);\n}\n"; + return; + } + init_cons_context(rec.constr); + bind_record_fields(rec, options | 32); + identify_cons_params(rec.constr, options); + identify_cons_neg_params(rec.constr, options); + add_cons_tag_store(rec.constr, rec.cons_idx); + auto it = rec.cpp_fields.cbegin(), end = rec.cpp_fields.cend(); + for (const Field& field : rec.constr.fields) { + if (field.constraint) { + add_constraint_check(rec.constr, field, options); + continue; + } + if (!field.implicit) { + assert(it < end && it->orig_idx == field.field_idx); + generate_pack_field(*it++, rec.constr, field, options); + } else { + if (it < end && it->orig_idx == field.field_idx) { + ++it; + } + compute_implicit_field(rec.constr, field, options); + } + } + assert(it == end); + add_remaining_param_constraints_check(rec.constr, options); + output_actions(os, "\n ", options | 4); + clear_context(); + os << "\n}\n"; +} + +void CppTypeCode::generate_ext_fetch_to(std::ostream& os, int options) { + std::string validate = (options & 1) ? "validate_" : ""; + os << "\nbool " << cpp_type_class_name << "::" << validate << "fetch_to(vm::CellSlice& cs, Ref& res" + << skip_extra_args << ") const {\n" + << " res = Ref{true, cs};\n" + << " return " << validate << "skip(cs" << skip_extra_args_pass << ") && res.unique_write().cut_tail(cs);\n" + << "}\n"; +} + +void CppTypeCode::ConsRecord::declare_record(std::ostream& os, std::string nl, int options) { + bool force = options & 1024; + if (declared) { + return; + } + if (!force) { + os << nl << "struct " << cpp_name; + if (!inline_record) { + os << ";\n"; + return; + } + } else { + os << "\n" << nl << "struct " << cpp_type.cpp_type_class_name << "::" << cpp_name; + } + os << " {\n"; + os << nl << " typedef " << cpp_type.cpp_type_class_name << " type_class;\n"; + CppIdentSet rec_cpp_ids; + recover_idents(rec_cpp_ids); + std::size_t n = cpp_fields.size(); + for (const ConsField& fi : cpp_fields) { + os << nl << " "; + fi.print_type(os); + os << " " << fi.name << "; \t// "; + if (fi.field.name) { + os << fi.field.get_name() << " : "; + } + fi.field.type->show(os, &constr); + os << std::endl; + } + if (n) { + os << nl << " " << cpp_name << "() = default;\n"; + std::vector ctor_args; + os << nl << " " << cpp_name << "("; + int i = 0, j = 0; + for (const ConsField& fi : cpp_fields) { + if (!fi.implicit) { + std::string arg = rec_cpp_ids.new_ident(std::string{"_"} + fi.name); + ctor_args.push_back(arg); + if (i++) { + os << ", "; + } + fi.print_type(os, true); + os << " " << arg; + } + } + os << ") : "; + i = 0; + for (const ConsField& fi : cpp_fields) { + if (i++) { + os << ", "; + } + os << fi.name << "("; + if (fi.implicit) { + os << (fi.ctype == ct_int32 ? "-1" : "nullptr"); + } else if (fi.get_cvt().needs_move()) { + os << "std::move(" << ctor_args.at(j++) << ")"; + } else { + os << ctor_args.at(j++); + } + os << ")"; + } + os << " {}\n"; + } + os << nl << "};\n"; + declared = true; +} + +bool CppTypeCode::ConsRecord::declare_record_unpack(std::ostream& os, std::string nl, int options) { + bool is_ok = false; + bool cell = options & 16; + std::string slice_arg = cell ? "Ref cell_ref" : "vm::CellSlice& cs"; + std::string fun_name = (options & 1) ? "validate_unpack" : "unpack"; + if (cell) { + fun_name = std::string{"cell_"} + fun_name; + } + std::string class_name; + if (options & 2048) { + class_name = cpp_type.cpp_type_class_name + "::"; + } + if (!(options & 8)) { + os << nl << "bool " << class_name << fun_name << "(" << slice_arg << ", " << class_name << cpp_name << "& data"; + is_ok = true; + } else if (is_small) { + os << nl << "bool " << class_name << fun_name << "_" << cpp_type.cons_enum_name.at(cons_idx) << "(" << slice_arg; + for (const auto& f : cpp_fields) { + os << ", " << f.get_cvt() << "& " << f.name; + } + is_ok = true; + } + if (is_ok) { + if (options & 2) { + os << cpp_type.skip_extra_args; + } + os << ") const" << (options & 1024 ? " {" : ";\n"); + } + return is_ok; +} + +bool CppTypeCode::ConsRecord::declare_record_pack(std::ostream& os, std::string nl, int options) { + bool is_ok = false; + bool cell = options & 16; + std::string builder_arg = cell ? "Ref& cell_ref" : "vm::CellBuilder& cb"; + std::string fun_name = (options & 1) ? "validate_pack" : "pack"; + if (cell) { + fun_name = std::string{"cell_"} + fun_name; + } + std::string class_name; + if (options & 2048) { + class_name = cpp_type.cpp_type_class_name + "::"; + } + if (!(options & 8)) { + os << nl << "bool " << class_name << fun_name << "(" << builder_arg << ", const " << class_name << cpp_name + << "& data"; + is_ok = true; + } else if (is_small) { + os << nl << "bool " << class_name << fun_name << "_" << cpp_type.cons_enum_name.at(cons_idx) << "(" << builder_arg; + for (const auto& f : cpp_fields) { + // skip SOME implicit fields ??? + if (!f.implicit) { + os << ", " << f.get_cvt() << " " << f.name; + } + } + is_ok = true; + } + if (is_ok) { + if (options & 2) { + os << cpp_type.skip_extra_args; + } + os << ") const" << (options & 1024 ? " {" : ";\n"); + } + return is_ok; +} + +void CppTypeCode::generate_fetch_enum_method(std::ostream& os, int options) { + int minl = type.size.convert_min_size(), maxl = type.size.convert_max_size(); + bool exact = type.cons_all_exact(); + std::string ctag = incremental_cons_tags ? "(unsigned)t" : "cons_tag[t]"; + os << "\nbool " << cpp_type_class_name << "::fetch_enum_to(vm::CellSlice& cs, char& value) const {\n"; + if (!cons_num) { + os << " value = -1;\n" + " return false;\n"; + } else if (!maxl) { + os << " value = 0;\n" + " return true;\n"; + } else if (cons_num == 1) { + const Constructor& constr = *type.constructors.at(0); + os << " value = (cs.fetch_ulong(" << minl << ") == " << HexConstWriter{constr.tag >> (64 - constr.tag_bits)} + << ") ? 0 : -1;\n"; + os << " return !value;\n"; + } else if (minl == maxl) { + if (exact) { + os << " value = (char)cs.fetch_ulong(" << minl << ");\n"; + os << " return value >= 0;\n"; + } else { + os << " int t = get_tag(cs);\n"; + os << " value = (char)t;\n"; + os << " return t >= 0 && cs.fetch_ulong(" << minl << ") == " << ctag << ";\n"; + } + } else if (exact) { + os << " int t = get_tag(cs);\n"; + os << " value = (char)t;\n"; + os << " return t >= 0 && cs.advance(cons_len[t]);\n"; + } else { + os << " int t = get_tag(cs);\n"; + os << " value = (char)t;\n"; + os << " return t >= 0 && cs.fetch_ulong(cons_len[t]) == " << ctag << ";\n"; + } + os << "}\n"; +} + +void CppTypeCode::generate_store_enum_method(std::ostream& os, int options) { + int minl = type.size.convert_min_size(), maxl = type.size.convert_max_size(); + bool exact = type.cons_all_exact(); + std::string ctag = incremental_cons_tags ? "value" : "cons_tag[value]"; + os << "\nbool " << cpp_type_class_name << "::store_enum_from(vm::CellBuilder& cb, int value) const {\n"; + if (!cons_num) { + os << " return false;\n"; + } else if (!maxl) { + os << " return !value;\n"; + } else if (cons_num == 1) { + const Constructor& constr = *type.constructors.at(0); + os << " return !value && cb.store_long_bool(" << HexConstWriter{constr.tag >> (64 - constr.tag_bits)} << ", " + << minl << ");\n"; + } else if (minl == maxl) { + if (exact) { + os << " return cb.store_long_rchk_bool(value, " << minl << ");\n"; + } else if (incremental_cons_tags && cons_num > (1 << (minl - 1))) { + os << " return cb.store_uint_less(" << cons_num << ", value);\n"; + } else { + os << " return (unsigned)value < " << cons_num << " && cb.store_long_bool(" << ctag << ", " << minl << ");\n"; + } + } else { + os << " return (unsigned)value < " << cons_num << " && cb.store_long_bool(" << ctag << ", cons_len[value]);\n"; + } + os << "}\n"; +} + +void CppTypeCode::generate_print_type_body(std::ostream& os, std::string nl) { + std::string name = type.type_name ? type.get_name() : cpp_type_class_name; + if (!tot_params) { + os << nl << "return os << \"" << name << "\";"; + return; + } + os << nl << "return os << \"(" << name; + for (int i = 0; i < tot_params; i++) { + if (type_param_is_neg[i]) { + os << " ~" << type_param_name[i]; + } else { + os << " \" << " << type_param_name[i] << " << \""; + } + } + os << ")\";"; +} + +void CppTypeCode::generate_header(std::ostream& os, int options) { + os << "\nstruct " << cpp_type_class_name << " final : TLB_Complex {\n"; + generate_cons_enum(os); + generate_cons_tag_info(os, " ", 1); + if (params) { + generate_type_fields(os, options); + generate_type_constructor(os, options); + } + for (int i = 0; i < cons_num; i++) { + records.at(i).declare_record(os, " ", options); + } + if (type.is_special) { + os << " bool always_special() const override {\n"; + os << " return true;\n }\n"; + } + int sz = type.size.min_size(); + sz = ((sz & 0xff) << 16) | (sz >> 8); + if (simple_get_size) { + os << " int get_size(const vm::CellSlice& cs) const override {\n"; + os << " return " << SizeWriter{sz} << ";\n }\n"; + } + os << " bool skip(vm::CellSlice& cs) const override"; + if (!inline_skip) { + os << ";\n"; + } else if (sz) { + os << " {\n return cs.advance" << (sz < 0x10000 ? "(" : "_ext(") << SizeWriter{sz} << ");\n }\n"; + } else { + os << " {\n return true;\n }\n"; + } + if (ret_params) { + os << " bool skip(vm::CellSlice& cs" << skip_extra_args << ") const;\n"; + } + os << " bool validate_skip(vm::CellSlice& cs, bool weak = false) const override"; + if (!inline_validate_skip) { + os << ";\n"; + } else if (sz) { + os << " {\n return cs.advance(" << SizeWriter{sz} << ");\n }\n"; + } else { + os << " {\n return true;\n }\n"; + } + if (ret_params) { + os << " bool validate_skip(vm::CellSlice& cs, bool weak" << skip_extra_args << ") const;\n"; + os << " bool fetch_to(vm::CellSlice& cs, Ref& res" << skip_extra_args << ") const;\n"; + } + if (type.is_simple_enum) { + os << " bool fetch_enum_to(vm::CellSlice& cs, char& value) const;\n"; + os << " bool store_enum_from(vm::CellBuilder& cb, int value) const;\n"; + } + for (int i = 0; i < cons_num; i++) { + records[i].declare_record_unpack(os, " ", 2); + records[i].declare_record_unpack(os, " ", 10); + records[i].declare_record_unpack(os, " ", 18); + records[i].declare_record_unpack(os, " ", 26); + records[i].declare_record_pack(os, " ", 2); + records[i].declare_record_pack(os, " ", 10); + records[i].declare_record_pack(os, " ", 18); + records[i].declare_record_pack(os, " ", 26); + } + os << " bool print_skip(PrettyPrinter& pp, vm::CellSlice& cs) const override;\n"; + if (ret_params) { + os << " bool print_skip(PrettyPrinter& pp, vm::CellSlice& cs" << skip_extra_args << ") const;\n"; + } + os << " std::ostream& print_type(std::ostream& os) const override {"; + generate_print_type_body(os, "\n "); + os << "\n }\n"; + os << " int check_tag(const vm::CellSlice& cs) const override;\n"; + os << " int get_tag(const vm::CellSlice& cs) const override"; + if (inline_get_tag) { + os << " {"; + generate_get_tag_body(os, "\n "); + os << "\n }\n"; + } else { + os << ";\n"; + } + os << "};\n"; + for (int i = 0; i < cons_num; i++) { + records.at(i).declare_record(os, "", options | 1024); + } + if (!cpp_type_var_name.empty()) { + os << "\nextern const " << cpp_type_class_name << " " << cpp_type_var_name << ";\n"; + } +} + +void CppTypeCode::generate_body(std::ostream& os, int options) { + generate_cons_tag_info(os, "", 2); + if (!inline_get_tag) { + os << "\nint " << cpp_type_class_name << "::get_tag(const vm::CellSlice& cs) const {"; + generate_get_tag_body(os, "\n "); + os << "\n}\n"; + } + generate_check_tag_method(os); + options &= -4; + if (!inline_skip) { + generate_skip_method(os, options); + } + if (ret_params) { + generate_skip_method(os, options + 2); + } + if (!inline_validate_skip) { + generate_skip_method(os, options + 1); + } + if (ret_params) { + generate_skip_method(os, options + 3); + generate_ext_fetch_to(os, options); + } + if (type.is_simple_enum) { + generate_fetch_enum_method(os, options); + generate_store_enum_method(os, options); + } + for (int i = 0; i < cons_num; i++) { + ConsRecord& rec = records.at(i); + generate_unpack_method(os, rec, 2); + generate_unpack_method(os, rec, 10); + generate_unpack_method(os, rec, 18); + generate_unpack_method(os, rec, 26); + } + for (int i = 0; i < cons_num; i++) { + ConsRecord& rec = records.at(i); + generate_pack_method(os, rec, 2); + generate_pack_method(os, rec, 10); + generate_pack_method(os, rec, 18); + generate_pack_method(os, rec, 26); + } + generate_print_method(os, options + 1); + if (ret_params) { + generate_print_method(os, options + 3); + } + if (!cpp_type_var_name.empty()) { + os << "\nconst " << cpp_type_class_name << " " << cpp_type_var_name << ";"; + } + os << std::endl; +} + +void CppTypeCode::generate(std::ostream& os, int options) { + std::string type_name = type.get_name(); + if (!type.type_name && type.is_auto) { + type_name = cpp_type_class_name; + } + if (options & 1) { + os << "\n//\n// headers for " << (type.is_auto ? "auxiliary " : "") << "type `" << type_name << "`\n//\n"; + generate_header(os, options); + } else if (options & 2) { + std::ostringstream tmp; + generate_header(tmp, options | 1); + } + if (options & 2) { + os << "\n//\n// code for " << (type.is_auto ? "auxiliary " : "") << "type `" << type_name << "`\n//\n"; + generate_body(os, options); + } +} + +void generate_type_constant(std::ostream& os, int i, TypeExpr* expr, std::string cpp_name, int mode) { + if (!mode) { + os << "// " << expr << std::endl; + os << "extern "; + } + std::string cls_name = "TLB"; + int fake_arg = -1; + cls_name = compute_type_expr_class_name(expr, fake_arg); + os << "const " << cls_name << ' ' << cpp_name; + if (!mode) { + os << ";\n"; + return; + } + int c = 0; + if (fake_arg >= 0) { + os << '{' << fake_arg; + c++; + } + for (const TypeExpr* arg : expr->args) { + if (!arg->negated) { + assert(arg->is_constexpr); + os << (c++ ? ", " : "{"); + if (arg->is_nat) { + os << arg->value; + } else { + os << const_type_expr_cpp_idents.at(arg->is_constexpr); + } + } + } + if (c) { + os << '}'; + } + os << ";\n"; +} + +void generate_type_constants(std::ostream& os, int mode) { + os << "\n// " << (mode ? "definitions" : "declarations") << " of constant types used\n\n"; + for (int i = 1; i <= const_type_expr_num; i++) { + TypeExpr* expr = const_type_expr[i]; + if (!expr->is_nat && !const_type_expr_simple[i]) { + generate_type_constant(os, i, expr, const_type_expr_cpp_idents[i], mode); + } + } +} + +void assign_const_type_cpp_idents() { + const_type_expr_cpp_idents.resize(const_type_expr_num + 1, ""); + const_type_expr_simple.resize(const_type_expr_num + 1, false); + for (int i = 1; i <= const_type_expr_num; i++) { + const TypeExpr* expr = const_type_expr[i]; + if (!expr->is_nat) { + if (expr->tp == TypeExpr::te_Ref && expr->args[0]->tp == TypeExpr::te_Apply && + (expr->args[0]->type_applied == Any_type || expr->args[0]->type_applied == Cell_type)) { + const_type_expr_cpp_idents[i] = "t_RefCell"; + const_type_expr_simple[i] = true; + continue; + } + if (expr->tp == TypeExpr::te_Apply) { + const Type* typ = expr->type_applied; + int idx = typ->type_idx; + if (typ == Any_type || typ == Cell_type || typ == Nat_type) { + const_type_expr_cpp_idents[i] = (typ == Nat_type ? "t_Nat" : "t_Anything"); + const_type_expr_simple[i] = true; + continue; + } + if (idx >= builtin_types_num && idx < types_num && !cpp_type[idx]->params) { + const_type_expr_cpp_idents[i] = cpp_type[idx]->cpp_type_var_name; + const_type_expr_simple[i] = true; + continue; + } + } + std::ostringstream ss; + ss << "t"; + expr->const_type_name(ss); + const_type_expr_cpp_idents[i] = global_cpp_ids.new_ident(ss.str()); + } + } +} + +std::string cpp_namespace = "tlb"; +std::vector cpp_namespace_list; +std::string tlb_library_header_name = "tl/tlblib.hpp"; + +void split_namespace_id() { + auto prev_it = cpp_namespace.cbegin(); + for (auto it = cpp_namespace.cbegin(); it != cpp_namespace.cend(); ++it) { + if (it[0] == ':' && it + 2 != cpp_namespace.cend() && it[1] == ':') { + if (prev_it != it) { + cpp_namespace_list.emplace_back(prev_it, it); + } + ++it; + prev_it = it + 1; + } + } + if (prev_it != cpp_namespace.cend()) { + cpp_namespace_list.emplace_back(prev_it, cpp_namespace.cend()); + } +} + +std::vector type_gen_order; + +void prepare_generate_cpp(int options = 0) { + std::vector> pairs; + pairs.reserve(types_num - builtin_types_num); + for (int i = builtin_types_num; i < types_num; i++) { + pairs.emplace_back(types.at(i).last_declared, i); + } + std::sort(pairs.begin(), pairs.end()); + type_gen_order.reserve(pairs.size()); + for (auto z : pairs) { + type_gen_order.push_back(z.second); + } + cpp_type.resize(types_num); + for (int i : type_gen_order) { + Type& type = types[i]; + cpp_type[i] = std::make_unique(type); + CppTypeCode& cc = *cpp_type[i]; + if (!cpp_type[i] || !cc.is_ok()) { + throw src::Fatal{std::string{"cannot generate c++ code for type `"} + type.get_name() + "`"}; + } + } + split_namespace_id(); + assign_const_type_cpp_idents(); +} + +bool generate_prepared; +bool gen_cpp; +bool gen_hpp; +bool append_suffix; + +void generate_cpp_output_to(std::ostream& os, int options = 0, std::vector include_files = {}) { + if (!generate_prepared) { + prepare_generate_cpp(options); + generate_prepared = true; + } + if (options & 1) { + os << "#pragma once\n"; + } + for (auto s : include_files) { + if (s.size() >= 10 && s.substr(s.size() - 10) == "tlblib.hpp") { + os << "#include <" << s << ">\n"; + } else { + os << "#include \"" << s << "\"\n"; + } + } + os << "/*\n *\n * AUTO-GENERATED FROM"; + for (auto s : source_list) { + if (s.empty()) { + os << " stdin"; + } else { + os << " `" << s << "`"; + } + } + os << "\n *\n */\n"; + for (int i = 0; i < builtin_types_num; i++) { + Type& type = types[i]; + if (type.used) { + os << "// uses built-in type `" << type.get_name() << "`\n"; + } + } + for (auto cpp_nsp : cpp_namespace_list) { + os << "\nnamespace " << cpp_nsp << " {" << std::endl; + }; + if (cpp_namespace != "tlb") { + os << "using namespace ::tlb;\n"; + } + os << "using td::Ref;\n" + << "using vm::CellSlice;\n" + << "using vm::Cell;\n" + << "using td::RefInt256;\n"; + for (int pass = 1; pass <= 2; pass++) { + if (options & pass) { + for (int i : type_gen_order) { + CppTypeCode& cc = *cpp_type[i]; + cc.generate(os, (options & -4) | pass); + } + generate_type_constants(os, pass - 1); + } + } + for (auto it = cpp_namespace_list.rbegin(); it != cpp_namespace_list.rend(); ++it) { + os << "\n} // namespace " << *it << std::endl; + } +} + +void generate_cpp_output_to(std::string filename, int options = 0, std::vector include_files = {}) { + std::stringstream ss; + generate_cpp_output_to(ss, options, std::move(include_files)); + auto new_content = ss.str(); + auto r_old_content = td::read_file_str(filename); + if (r_old_content.is_ok() && r_old_content.ok() == new_content) { + return; + } + std::ofstream os{filename}; + if (!os) { + throw src::Fatal{std::string{"cannot create output file `"} + filename + "`"}; + } + os << new_content; +} + +void generate_cpp_output(std::string filename = "", int options = 0) { + if (!gen_cpp && !gen_hpp) { + gen_cpp = gen_hpp = true; + } + options &= ~3; + options |= (gen_hpp ? 1 : 0) | (gen_cpp << 1); + if (filename.empty()) { + generate_cpp_output_to(std::cout, options, {tlb_library_header_name}); + } else if (!append_suffix) { + generate_cpp_output_to(filename, options, {tlb_library_header_name}); + } else { + if (gen_hpp) { + generate_cpp_output_to(filename + ".h", options & ~2, {tlb_library_header_name}); + } + if (gen_cpp) { + generate_cpp_output_to(filename + ".cpp", options & ~1, {filename + ".h"}); + } + } +} + +} // namespace tlbc diff --git a/crypto/tl/tlbc-gen-cpp.h b/crypto/tl/tlbc-gen-cpp.h new file mode 100644 index 00000000..c84d825d --- /dev/null +++ b/crypto/tl/tlbc-gen-cpp.h @@ -0,0 +1,290 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +namespace tlbc { + +extern std::set forbidden_cpp_idents, local_forbidden_cpp_idents; + +struct CppIdentSet { + std::set cpp_idents; + const std::set* extra_forbidden_idents; + CppIdentSet(const std::set* forbid = nullptr) : extra_forbidden_idents(forbid) { + } + static std::string compute_cpp_ident(std::string orig_ident, int count = 0); + std::string new_ident(std::string orig_ident, int count = 0, std::string suffix = ""); + bool insert(std::string ident) { + return cpp_idents.insert(ident).second; + } + bool defined(std::string ident) { + return cpp_idents.count(ident); + } + bool is_good_ident(std::string ident); + void clear() { + cpp_idents.clear(); + } +}; + +extern CppIdentSet global_cpp_ids; + +struct Action { + int fixed_size; + bool is_pure; + bool is_constraint; + std::string action; + Action(int _size) : fixed_size(_size), is_pure(false), is_constraint(false) { + } + Action(std::string _action, bool _cst = false) + : fixed_size(-1), is_pure(false), is_constraint(_cst), action(_action) { + } + Action(const std::ostringstream& ss, bool _cst = false) + : fixed_size(-1), is_pure(false), is_constraint(_cst), action(ss.str()) { + } + Action(std::ostringstream&& ss, bool _cst = false) + : fixed_size(-1), is_pure(false), is_constraint(_cst), action(std::move(ss).str()) { + } + void show(std::ostream& os) const; + bool may_combine(const Action& next) const; + bool operator+=(const Action& next); +}; + +enum cpp_val_type { + ct_unknown, + ct_void = 1, + ct_slice = 2, + ct_cell = 3, + ct_typeref = 4, + ct_typeptr = 5, + ct_bits = 6, + ct_bitstring = 7, + ct_integer = 8, + ct_bool = 10, + ct_enum = 11, + ct_int32 = 12, + ct_uint32 = 13, + ct_int64 = 14, + ct_uint64 = 15, + ct_subrecord = 16 +}; + +struct CppValType { + cpp_val_type vt; + int size; + CppValType(cpp_val_type _vt = ct_unknown, int _size = -1) : vt(_vt), size(_size) { + } + cpp_val_type get() const { + return vt; + } + void show(std::ostream& os, bool pass_value = false) const; + bool needs_move() const; +}; + +extern std::ostream& operator<<(std::ostream& os, CppValType cvt); + +class CppTypeCode { + Type& type; + bool ok; + bool builtin; + bool inline_get_tag; + bool inline_skip; + bool inline_validate_skip; + bool simple_get_size; + bool simple_cons_tags; + bool incremental_cons_tags; + + public: + int params; + int tot_params; + int ret_params; + int cons_num; + int common_cons_len; + std::vector cons_enum_name; + std::vector cons_enum_value; + std::vector cons_tag_map; + std::vector cons_tag_exact; + std::vector cons_idx_by_enum; + std::string cpp_type_var_name; + std::string cpp_type_class_name; + std::string cpp_type_template_name; + + struct ConsRecord; + + struct ConsField { + const Field& field; + const ConsRecord* subrec; + std::string name; + cpp_val_type ctype; + int size; + int orig_idx; + bool implicit; + ConsField(const Field& _field, std::string _name, cpp_val_type _ctype, int _size, int _idx, + const ConsRecord* _subrec = nullptr, bool _implicit = false) + : field(_field), subrec(_subrec), name(_name), ctype(_ctype), size(_size), orig_idx(_idx), implicit(_implicit) { + assert(ctype != ct_subrecord || subrec); + } + CppValType get_cvt() const { + return {ctype, size}; + } + void print_type(std::ostream& os, bool pass_value = false) const; + }; + + struct ConsRecord { + const CppTypeCode& cpp_type; + const Constructor& constr; + int cons_idx; + bool is_trivial; + bool is_small; + bool triv_conflict; + bool has_trivial_name; + bool inline_record; + bool declared; + cpp_val_type equiv_cpp_type; + std::vector equiv_cpp_types; + std::string cpp_name; + std::vector cpp_fields; + ConsRecord(const CppTypeCode& _cpp_type, const Constructor& _constr, int idx, bool _triv = false) + : cpp_type(_cpp_type), constr(_constr), cons_idx(idx), is_trivial(_triv), declared(false) { + } + bool recover_idents(CppIdentSet& idents) const; + void declare_record(std::ostream& os, std::string nl, int options); + bool declare_record_unpack(std::ostream& os, std::string nl, int options); + bool declare_record_pack(std::ostream& os, std::string nl, int options); + void print_full_name(std::ostream& os) const; + }; + std::vector records; + + private: + std::vector type_param_name; + std::vector type_param_is_nat; + std::vector type_param_is_neg; + std::string template_args; + std::string constructor_args; + std::string skip_extra_args; + std::string skip_extra_args_pass; + CppIdentSet local_cpp_ids; + bool init(); + + public: + CppTypeCode(Type& _type) : type(_type), local_cpp_ids(&local_forbidden_cpp_idents) { + ok = init(); + } + bool is_ok() const { + return ok; + } + void generate(std::ostream& os, int options = 0); + + private: + bool compute_simple_cons_tags(); + bool check_incremental_cons_tags() const; + unsigned long long compute_selector_mask() const; + void assign_class_name(); + void assign_cons_names(); + void assign_class_field_names(); + void assign_cons_values(); + void assign_record_cons_names(); + void generate_cons_enum(std::ostream& os); + void generate_type_constructor(std::ostream& os, int options); + void generate_type_fields(std::ostream& os, int options); + void generate_header(std::ostream& os, int options = 0); + void generate_body(std::ostream& os, int options = 0); + void generate_cons_len_array(std::ostream& os, std::string nl, int options = 0); + void generate_cons_tag_array(std::ostream& os, std::string nl, int options = 0); + void generate_cons_tag_info(std::ostream& os, std::string nl, int options = 0); + void generate_skip_method(std::ostream& os, int options = 0); + void generate_skip_cons_method(std::ostream& os, std::string nl, int cidx, int options); + void generate_cons_tag_check(std::ostream& os, std::string nl, int cidx, bool force = false); + void generate_check_tag_method(std::ostream& os); + void generate_unpack_method(std::ostream& os, ConsRecord& rec, int options); + void generate_pack_method(std::ostream& os, ConsRecord& rec, int options); + void generate_ext_fetch_to(std::ostream& os, int options); + void generate_fetch_enum_method(std::ostream& os, int options); + void generate_store_enum_method(std::ostream& os, int options); + void generate_print_type_body(std::ostream& os, std::string nl); + void generate_print_method(std::ostream& os, int options = 0); + void generate_print_cons_method(std::ostream& os, std::string nl, int cidx, int options); + void generate_get_tag_body(std::ostream& os, std::string nl); + void generate_get_tag_subcase(std::ostream& os, std::string nl, const BinTrie* trie, int depth) const; + void generate_get_tag_param(std::ostream& os, std::string nl, unsigned long long tag, + unsigned long long params = std::numeric_limits::max()) const; + void generate_get_tag_param1(std::ostream& os, std::string nl, const char A[4], + const std::string param_names[1]) const; + void generate_get_tag_param2(std::ostream& os, std::string nl, const char A[4][4], + const std::string param_names[2]) const; + void generate_get_tag_param3(std::ostream& os, std::string nl, const char A[4][4][4], + const std::string param_names[3]) const; + bool match_param_pattern(std::ostream& os, std::string nl, const char A[4], int mask, std::string pattern, + std::string param_name) const; + std::string get_nat_param_name(int idx) const; + void generate_tag_pfx_selector(std::ostream& os, std::string nl, const BinTrie& trie, int d, int min_size) const; + bool generate_get_tag_pfx_distinguisher(std::ostream& os, std::string nl, const std::vector& constr_list, + bool in_block) const; + + private: + std::vector actions; + int incomplete; + int tmp_ints; + bool needs_tmp_cell; + std::vector tmp_vars; + std::vector field_vars; + std::vector field_var_set; + std::vector param_var_set; + std::vector param_constraint_used; + std::vector> postponed_equate; + CppIdentSet tmp_cpp_ids; + void clear_context(); + void init_cons_context(const Constructor& constr); + std::string new_tmp_var(std::string hint); + std::string new_tmp_var(); + void add_action(const Action& act); + void output_actions(std::ostream& os, std::string nl, int options); + void output_cpp_expr(std::ostream& os, const TypeExpr* expr, int prio = 0, bool allow_type_neg = false) const; + void output_cpp_sizeof_expr(std::ostream& os, const TypeExpr* expr, int prio) const; + void output_negative_type_arguments(std::ostream& os, const TypeExpr* expr); + bool can_compute(const TypeExpr* expr) const; + bool can_use_to_compute(const TypeExpr* expr, int i) const; + bool can_compute_sizeof(const TypeExpr* expr) const; + bool is_self(const TypeExpr* expr, const Constructor& constr) const; + void add_compute_actions(const TypeExpr* expr, int i, std::string bind_to); + void identify_cons_params(const Constructor& constr, int options); + void identify_cons_neg_params(const Constructor& constr, int options); + void bind_record_fields(const ConsRecord& rec, int options); + void add_cons_tag_check(const Constructor& constr, int cidx, int options); + void add_cons_tag_store(const Constructor& constr, int cidx); + std::string add_fetch_nat_field(const Constructor& constr, const Field& field, int options); + void add_store_nat_field(const Constructor& constr, const Field& field, int options); + void add_remaining_param_constraints_check(const Constructor& constr, int options); + void compute_implicit_field(const Constructor& constr, const Field& field, int options); + bool add_constraint_check(const Constructor& constr, const Field& field, int options); + void add_postponed_equate_actions(); + void output_fetch_field(std::ostream& os, std::string field_name, const TypeExpr* expr, cpp_val_type cvt); + void output_fetch_subrecord(std::ostream& os, std::string field_name, const ConsRecord* subrec); + void output_store_field(std::ostream& os, std::string field_name, const TypeExpr* expr, cpp_val_type cvt); + void add_store_subrecord(std::string field_name, const ConsRecord* subrec); + void generate_skip_field(const Constructor& constr, const Field& field, int options); + void generate_print_field(const Constructor& constr, const Field& field, int options); + bool output_print_simple_field(std::ostream& os, const Field& field, std::string field_name, const TypeExpr* expr); + void generate_unpack_field(const ConsField& fi, const Constructor& constr, const Field& field, int options); + void generate_pack_field(const ConsField& fi, const Constructor& constr, const Field& field, int options); +}; + +extern std::vector> cpp_type; + +extern bool add_type_members; + +} // namespace tlbc diff --git a/crypto/tl/tlbc.cpp b/crypto/tl/tlbc.cpp new file mode 100644 index 00000000..81524ff5 --- /dev/null +++ b/crypto/tl/tlbc.cpp @@ -0,0 +1,3078 @@ +/* + This file is part of TON Blockchain source code. + + TON Blockchain is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + TON Blockchain is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with TON Blockchain. If not, see . + + In addition, as a special exception, the copyright holders give permission + to link the code of portions of this program with the OpenSSL library. + You must obey the GNU General Public License in all respects for all + of the code used other than OpenSSL. If you modify file(s) with this + exception, you may extend this exception to your version of the file(s), + but you are not obligated to do so. If you do not wish to do so, delete this + exception statement from your version. If you delete this exception statement + from all source files in the program, then also delete it here. + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "common/refcnt.hpp" +#include "common/bigint.hpp" +#include "common/refint.h" +#include "parser/srcread.h" +#include "parser/lexer.h" +#include "parser/symtable.h" +#include "td/utils/Slice-decl.h" +#include "td/utils/format.h" +#include "td/utils/crypto.h" +#include "tlbc-aux.h" +#include "tlbc-data.h" +#include "tlbc-gen-cpp.h" + +int verbosity; + +namespace src { + +/* + * + * KEYWORD DEFINITION + * + */ + +enum { _Eof = -1, _Ident = 0, _Number, _Special, _Eq = 0x80, _Leq, _Geq, _Neq, _Type, _EMPTY }; + +void define_keywords() { + sym::symbols.add_kw_char('+') + .add_kw_char('-') + .add_kw_char('*') + .add_kw_char(':') + .add_kw_char(';') + .add_kw_char('(') + .add_kw_char(')') + .add_kw_char('{') + .add_kw_char('}') + .add_kw_char('[') + .add_kw_char(']') + .add_kw_char('=') + .add_kw_char('_') + .add_kw_char('?') + .add_kw_char('~') + .add_kw_char('^'); + + sym::symbols.add_keyword("==", _Eq) + .add_keyword("<=", _Leq) + .add_keyword(">=", _Geq) + .add_keyword("!=", _Neq) + .add_keyword("Type", _Type) + .add_keyword("EMPTY", _EMPTY); +} + +// parses constant bitstrings in format \#[0-9a-f]*_? or \$[01]*_? +unsigned long long get_special_value(std::string str) { + std::size_t i = 1, n = str.size(); + if (n <= 1) { + return 0; + } + unsigned long long val = 0; + int bits = 0; + if (str[0] == '#') { + while (i < n) { + int c = str[i++]; + if (c == '_') { + break; + } + if (c >= '0' && c <= '9') { + c -= '0'; + } else if (c >= 'a' && c <= 'f') { + c -= 'a' - 10; + } else { + return 0; + } + if (bits > 60) { + return 0; + } + val |= (unsigned long long)c << (60 - bits); + bits += 4; + } + } else if (str[0] == '$') { + if (str[1] != '_') { + for (; i < n; i++) { + int c = str[i]; + c -= '0'; + if (c & -2) { + return 0; + } + if (bits > 63) { + return 0; + } + val |= (unsigned long long)c << (63 - bits); + bits++; + } + } + } else { + return 0; + } + if (i < n - 1) { + return 0; + } + if (i == n - 1 && bits) { + // trailing _ + while (bits && !((val >> (64 - bits)) & 1)) { + --bits; + } + } + if (bits == 64) { + return 0; + } + return val | (1ULL << (63 - bits)); +} + +int lexem_is_special(std::string str) { + return get_special_value(str) ? Lexem::Special : 0; +} + +} // namespace src + +namespace sym { + +enum class IdSc : char { undef = 0, lc = 1, uc = 2, blc = 3 }; +// subclass: +// 1 = first letter or first letter after last . is lowercase +// 2 = ... uppercase +// 3 = 1 + first character (after last ., if present) is a ! +// 0 = else +int compute_symbol_subclass(std::string str) { + IdSc res = IdSc::undef; + int t = 0, s = 0; + for (char c : str) { + if (c == '.') { + res = IdSc::undef; + s = t = 0; + } else if (res == IdSc::undef) { + if (!s) { + s = (c == '!' ? 1 : -1); + } + if ((c | 0x20) >= 'a' && (c | 0x20) <= 'z') { + res = (c & 0x20 ? IdSc::lc : IdSc::uc); + } + if (t && (((unsigned)c & 0xc0) == 0x80)) { + t = (t << 6) | ((unsigned)c & 0x3f); + if (t >= 0x410 && t < 0x450) { + res = (t < 0x430 ? IdSc::uc : IdSc::lc); + } + } + t = (((unsigned)c & 0xe0) == 0xc0 ? (c & 0x1f) : 0); + } + } + if (s == 1 && res == IdSc::lc) { + res = IdSc::blc; + } + return (int)res; +} + +inline bool is_lc_ident(sym_idx_t idx) { + auto sc = symbols.get_subclass(idx); + return sc == (int)IdSc::lc || sc == (int)IdSc::blc; +} + +inline bool is_spec_lc_ident(sym_idx_t idx) { + auto sc = symbols.get_subclass(idx); + return sc == (int)IdSc::blc; +} + +inline bool is_uc_ident(sym_idx_t idx) { + return symbols.get_subclass(idx) == (int)IdSc::uc; +} + +} // namespace sym + +namespace tlbc { + +/* + * + * AUXILIARY DATA TYPES + * + */ + +// headers are in tlbc-aux.h + +std::ostream& operator<<(std::ostream& os, const BitPfxCollection& p) { + p.show(os); + return os; +} + +void BitPfxCollection::show(std::ostream& os) const { + char first = '{'; + for (unsigned long long val : pfx) { + os << first; + while (val & (All - 1)) { + os << (val >> 63); + val <<= 1; + } + os << '*'; + first = ','; + } + if (first == '{') { + os << '{'; + } + os << '}'; +} + +void BitPfxCollection::merge_back(unsigned long long z) { + if (!pfx.size()) { + pfx.push_back(z); + return; + } + unsigned long long w = td::lower_bit64(z); + while (pfx.size()) { + unsigned long long t = z ^ pfx.back(); + if (!t) { + return; + } + if (t != (w << 1)) { + break; + } + z -= w; + w <<= 1; + pfx.pop_back(); + } + pfx.push_back(z); +} + +BitPfxCollection& BitPfxCollection::operator*=(unsigned long long prepend) { + if (!prepend) { + clear(); + return *this; + } + if (prepend == All) { + return *this; + } + int l = 63 - td::count_trailing_zeroes_non_zero64(prepend); + prepend &= prepend - 1; + std::size_t i, j = 0, n = pfx.size(); + for (i = 0; i < n; i++) { + unsigned long long z = pfx[i], zw = td::lower_bit64(z); + z >>= l; + z |= prepend; + if (!(zw >> l)) { + z |= 1; + } + if (!j || pfx[j - 1] != z) { + pfx[j++] = z; + } + } + pfx.resize(j); + return *this; +} + +BitPfxCollection BitPfxCollection::operator*(unsigned long long prepend) const { + if (!prepend) { + return BitPfxCollection{}; + } + if (prepend == All) { + return *this; + } + BitPfxCollection res; + res.pfx.reserve(pfx.size()); + int l = 63 - td::count_trailing_zeroes_non_zero64(prepend); + prepend &= prepend - 1; + std::size_t i, n = pfx.size(); + for (i = 0; i < n; i++) { + unsigned long long z = pfx[i], zw = td::lower_bit64(z); + z >>= l; + z |= prepend; + if (!(zw >> l)) { + z |= 1; + } + res.merge_back(z); + } + return res; +} + +BitPfxCollection BitPfxCollection::operator+(const BitPfxCollection& other) const { + if (!other.pfx.size()) { + return *this; + } + if (!pfx.size()) { + return other; + } + BitPfxCollection res; + res.pfx.reserve(pfx.size() + other.pfx.size()); + std::size_t i = 0, j = 0, m = pfx.size(), n = other.pfx.size(); + struct Interval { + unsigned long long z, a, b; + void operator=(unsigned long long _z) { + z = _z; + a = (_z & (_z - 1)); + b = (_z | (_z - 1)); + } + bool contains(const Interval& other) const { + return a <= other.a && other.b <= b; + } + }; + Interval U, V; + U = pfx[0]; + V = other.pfx[0]; + while (i < m && j < n) { + if (U.b < V.b || (U.b == V.b && U.a >= V.a)) { + if (U.a < V.a) { + res.merge_back(U.z); + } + if (++i == m) { + break; + } + U = pfx[i]; + } else { + if (V.a < U.a) { + res.merge_back(V.z); + } + if (++j == n) { + break; + } + V = other.pfx[j]; + } + } + while (i < m) { + res.merge_back(pfx[i++]); + } + while (j < n) { + res.merge_back(other.pfx[j++]); + } + return res; +} + +bool BitPfxCollection::operator+=(const BitPfxCollection& other) { + BitPfxCollection tmp = *this + other; + if (*this == tmp) { + return false; + } else { + *this = tmp; + return true; + } +} + +void AdmissibilityInfo::set_all(bool val) { + dim = 0; + info.clear(); + info.resize(1, val); +} + +std::ostream& operator<<(std::ostream& os, const AdmissibilityInfo& p) { + p.show(os); + return os; +} + +void AdmissibilityInfo::show(std::ostream& os) const { + os << '['; + for (bool x : info) { + os << (int)x; + } + os << ']'; +} + +void AdmissibilityInfo::extend(int dim1) { + if (dim < dim1) { + std::size_t i, n = info.size(), n1 = (size_t(1) << (2 * dim1)); + assert(n); + info.resize(n1); + for (i = n; i < n1; i++) { + info[i] = info[i - n]; + } + dim = dim1; + } +} + +void AdmissibilityInfo::operator|=(const AdmissibilityInfo& other) { + extend(other.dim); + std::size_t i, j, n = info.size(), n1 = other.info.size(); + assert(n1 && !(n1 & (n1 - 1))); + for (i = j = 0; i < n; i++) { + info[i] = info[i] | other.info[j]; + j = (j + 1) & (n1 - 1); + } +} + +void AdmissibilityInfo::set_by_pattern(int pdim, int pattern[]) { + extend(pdim); + std::size_t n = info.size(); + for (std::size_t x = 0; x < n; x++) { + std::size_t y = x; + bool f = true; + for (int i = 0; i < pdim; i++) { + if (!((pattern[i] >> (y & 3)) & 1)) { + f = false; + break; + } + y >>= 2; + } + if (f) { + info[x] = true; + } + } +} + +int AdmissibilityInfo::conflicts_at(const AdmissibilityInfo& other) const { + std::size_t i, n1 = info.size(), n2 = other.info.size(), n = std::max(n1, n2); + for (i = 0; i < n; i++) { + if (info[i & (n1 - 1)] && other.info[i & (n2 - 1)]) { + return (int)i; + } + } + return -1; +} + +bool AdmissibilityInfo::extract1(char A[4], char tag, int p1) const { + char B[4]; + std::memset(B, 0, sizeof(B)); + p1 <<= 1; + std::size_t n = info.size() - 1; + for (std::size_t x = 0; x <= n; x++) { + if (info[x]) { + B[(x >> p1) & 3] = 1; + } + } + int m1 = ((n >> p1) & 3); + for (int i = 0; i < 4; i++) { + if (B[i & m1]) { + if (A[i] && A[i] != tag) { + A[i] = -1; + return false; + } + A[i] = tag; + } + } + return true; +} + +bool AdmissibilityInfo::extract2(char A[4][4], char tag, int p1, int p2) const { + char B[4][4]; + std::memset(B, 0, sizeof(B)); + p1 <<= 1; + p2 <<= 1; + std::size_t n = info.size() - 1; + for (std::size_t x = 0; x <= n; x++) { + if (info[x]) { + B[(x >> p1) & 3][(x >> p2) & 3] = 1; + } + } + int m1 = ((n >> p1) & 3); + int m2 = ((n >> p2) & 3); + for (int i = 0; i < 4; i++) { + for (int j = 0; j < 4; j++) { + if (B[i & m1][j & m2]) { + if (A[i][j] && A[i][j] != tag) { + A[i][j] = -1; + return false; + } + A[i][j] = tag; + } + } + } + return true; +} + +bool AdmissibilityInfo::extract3(char A[4][4][4], char tag, int p1, int p2, int p3) const { + char B[4][4][4]; + std::memset(B, 0, sizeof(B)); + p1 <<= 1; + p2 <<= 1; + p3 <<= 1; + std::size_t n = info.size() - 1; + for (std::size_t x = 0; x <= n; x++) { + if (info[x]) { + B[(x >> p1) & 3][(x >> p2) & 3][(x >> p3) & 3] = 1; + } + } + int m1 = ((n >> p1) & 3); + int m2 = ((n >> p2) & 3); + int m3 = ((n >> p3) & 3); + for (int i = 0; i < 4; i++) { + for (int j = 0; j < 4; j++) { + for (int k = 0; k < 4; k++) { + if (B[i & m1][j & m2][k & m3]) { + if (A[i][j][k] && A[i][j][k] != tag) { + A[i][j][k] = -1; + return false; + } + A[i][j][k] = tag; + } + } + } + } + return true; +} + +void ConflictGraph::set_clique(ConflictSet set) { + if (set.x) { + for (int i = 0; i < 64; i++) { + if (set[i]) { + g[i] |= set; + } + } + } +} + +std::ostream& operator<<(std::ostream& os, const BinTrie& bt) { + bt.show(os); + return os; +} + +void BinTrie::show(std::ostream& os, unsigned long long pfx) const { + unsigned long long x = pfx, u = (td::lower_bit64(x) >> 1); + while (x & ((1ULL << 63) - 1)) { + os << (x >> 63); + x <<= 1; + } + os << " t=" << tag << "; dt=" << down_tag << "; ud=" << useful_depth << std::endl; + if (left) { + left->show(os, pfx - u); + } + if (right) { + right->show(os, pfx + u); + } +} + +void BinTrie::ins_path(unsigned long long path, unsigned long long new_tag) { + if (!path || !new_tag) { + return; + } + if (!(path & ((1ULL << 63) - 1))) { + tag |= new_tag; + return; + } else if ((long long)path >= 0) { + left = insert_path(std::move(left), path << 1, new_tag); + } else { + right = insert_path(std::move(right), path << 1, new_tag); + } + if (left && right) { + tag |= left->tag & right->tag; + } +} + +std::unique_ptr BinTrie::insert_path(std::unique_ptr root, unsigned long long path, + unsigned long long tag) { + if (!path || !tag) { + return root; + } + if (root) { + root->ins_path(path, tag); + return root; + } + if (!(path & ((1ULL << 63) - 1))) { + return std::make_unique(tag); + } + if ((long long)path >= 0) { + return std::make_unique(0, insert_path({}, path << 1, tag), std::unique_ptr{}); + } else { + return std::make_unique(0, std::unique_ptr{}, insert_path({}, path << 1, tag)); + } +} + +std::unique_ptr BinTrie::insert_paths(std::unique_ptr root, const BitPfxCollection& paths, + unsigned long long tag) { + if (tag) { + for (auto x : paths.pfx) { + root = insert_path(std::move(root), x, tag); + } + } + return root; +} + +unsigned long long BinTrie::lookup_tag(unsigned long long path) const { + const BinTrie* node = lookup_node_const(path); + return node ? node->tag : 0; +} + +BinTrie* BinTrie::lookup_node(unsigned long long path) { + if (!path) { + return nullptr; + } + if (!(path & ((1ULL << 63) - 1))) { + return this; + } + if ((long long)path >= 0) { + return left ? left->lookup_node(path << 1) : nullptr; + } else { + return right ? right->lookup_node(path << 1) : nullptr; + } +} + +const BinTrie* BinTrie::lookup_node_const(unsigned long long path) const { + if (!path) { + return nullptr; + } + if (!(path & ((1ULL << 63) - 1))) { + return this; + } + if ((long long)path >= 0) { + return left ? left->lookup_node_const(path << 1) : nullptr; + } else { + return right ? right->lookup_node_const(path << 1) : nullptr; + } +} + +void BinTrie::set_conflict_graph(ConflictGraph& gr, unsigned long long colors) const { + colors |= tag; + if (!left || !right) { + gr.set_clique(ConflictSet{colors}); + } + if (left) { + left->set_conflict_graph(gr, colors); + } + if (right) { + right->set_conflict_graph(gr, colors); + } +} + +int BinTrie::compute_useful_depth(unsigned long long colors) { + int res = 0; // useless; + down_tag = tag = colors |= tag; + if (left) { + res = left->compute_useful_depth(colors); + down_tag |= left->down_tag; + } + if (right) { + res = std::max(res, right->compute_useful_depth(colors)); + down_tag |= right->down_tag; + } + if (res > 0) { + return useful_depth = res + 1; + } + if (left && right && (left->down_tag & ~right->down_tag) != 0 && (right->down_tag & ~left->down_tag) != 0) { + return useful_depth = 1; + } + return useful_depth = 0; +} + +unsigned long long BinTrie::build_submap(int depth, unsigned long long A[]) const { + if (!depth) { + A[0] = down_tag | (useful_depth ? (1ULL << 63) : 0); + return down_tag != 0; + } + int n = (1 << (depth - 1)); + unsigned long long r1 = 0, r2 = 0; + if (left) { + r1 = left->build_submap(depth - 1, A); + } else { + std::memset(A, 0, n * 8); + } + if (right) { + r2 = right->build_submap(depth - 1, A + n); + } else { + std::memset(A + n, 0, n * 8); + } + if (A[n] != A[n - 1] || (long)A[n] < 0) { + r2 |= 1; + } else { + r2 &= ~1; + } + return r1 | (r2 << n); +} + +unsigned long long BinTrie::build_submap_at(int depth, unsigned long long A[], unsigned long long pfx) const { + const BinTrie* node = lookup_node_const(pfx); + if (!node) { + std::memset(A, 0, 8 << depth); + return 0; + } + return node->build_submap(depth, A); +} + +unsigned long long BinTrie::find_conflict_path(unsigned long long colors, unsigned long long mask) const { + colors |= tag & mask; + if (!left && !right) { + return colors & (colors - 1) ? (1ULL << 63) : 0; + } + if (!left) { + if (colors & (colors - 1)) { + return (1ULL << 62); // $0 + } else { + unsigned long long x = right->find_conflict_path(colors, mask); + return x ? ((x >> 1) | (1ULL << 63)) : 0; + } + } else if (!right) { + if (colors & (colors - 1)) { + return (3ULL << 62); // $1 + } else { + return left->find_conflict_path(colors, mask) >> 1; + } + } + unsigned long long x = left->find_conflict_path(colors, mask); + unsigned long long y = right->find_conflict_path(colors, mask); + if (td::lower_bit64(y) > td::lower_bit64(x)) { + return (y >> 1) | (1ULL << 63); + } else { + return x >> 1; + } +} + +std::ostream& operator<<(std::ostream& os, MinMaxSize t) { + t.show(os); + return os; +} + +void MinMaxSize::normalize() { + if (minmax_size & (0xfff800f8U * 0x100000001ULL)) { + nrm(0xf8, 0x7); + nrm(0xfff80000U, 0x7ff00); + nrm(0xf8ULL << 32, 7ULL << 32); + nrm(0xfff80000ULL << 32, 0x7ff00ULL << 32); + } +} + +MinMaxSize::unpacked::unpacked(MinMaxSize val) { + val.normalize(); + max_refs = val.minmax_size & 0xff; + max_bits = (val.minmax_size >> 8) & 0x7ff; + min_refs = (val.minmax_size >> 32) & 0xff; + min_bits = (val.minmax_size >> 40) & 0x7ff; +} + +MinMaxSize MinMaxSize::unpacked::pack() const { + unsigned long long t = ((unsigned long long)(min_bits * 0x100 + min_refs) << 32); + t += (max_bits * 0x100 + max_refs); + return MinMaxSize{t}; +} + +MinMaxSize& MinMaxSize::repeat(int count) { + if (count <= 0) { + return clear(); + } + if (count == 1) { + return *this; + } + unpacked z{*this}; + count = std::min(count, 1024); + z.max_refs = std::min(z.max_refs * count, 7U); + z.max_bits = std::min(z.max_bits * count, 0x7ffU); + z.min_refs = std::min(z.min_refs * count, 7U); + z.min_bits = std::min(z.min_bits * count, 0x7ffU); + return *this = z.pack(); +} + +MinMaxSize& MinMaxSize::repeat_at_least(int count) { + count = std::min(std::max(count, 0), 1024); + unpacked z{*this}; + if (z.max_refs) { + z.max_refs = 7; + } + if (z.max_bits) { + z.max_bits = 0x7ff; + } + z.min_refs = std::min(z.min_refs * count, 7U); + z.min_bits = std::min(z.min_bits * count, 0x7ffU); + return *this = z.pack(); +} + +MinMaxSize& MinMaxSize::operator|=(MinMaxSize y) { + unpacked z{*this}, w{y}; + z.min_refs = std::min(z.min_refs, w.min_refs); + z.min_bits = std::min(z.min_bits, w.min_bits); + z.max_refs = std::max(z.max_refs, w.max_refs); + z.max_bits = std::max(z.max_bits, w.max_bits); + return *this = z.pack(); +} + +void MinMaxSize::show(std::ostream& os) const { + unpacked z{*this}; + z.show(os); +} + +void MinMaxSize::unpacked::show(std::ostream& os) const { + bool fixed = (min_bits == max_bits && min_refs == max_refs); + if (fixed) { + os << '='; + } + if (min_bits >= 1024 && min_refs >= 7) { + os << "infty"; + } else { + os << min_bits; + if (min_refs) { + os << "+" << min_refs << "R"; + } + } + if (!fixed) { + os << ".."; + if (max_bits >= 1024 && max_refs >= 7) { + os << "infty"; + } else { + os << max_bits; + if (max_refs) { + os << "+" << max_refs << "R"; + } + } + } +} + +} // namespace tlbc + +namespace tlbc { + +using src::Lexem; +using src::Lexer; +using sym::sym_idx_t; + +/* + * + * DATA TYPES: Type Expressions, Types, Constructors + * + */ + +// headers in tlbc-data.h + +std::ostream& operator<<(std::ostream& os, const TypeExpr* te) { + if (te) { + te->show(os); + } else { + os << "(null-type)"; + } + return os; +} + +std::ostream& operator<<(std::ostream& os, const Constructor& cs) { + cs.show(os); + return os; +} + +TypeExpr type_Type{{}, TypeExpr::te_Type}; + +TypeExpr* const_type_expr[TypeExpr::max_const_expr]; +int const_type_expr_num; + +TypeExpr* TypeExpr::const_htable[TypeExpr::const_htable_size]; + +sym_idx_t Nat_name, Eq_name, Less_name, Leq_name; +Type* Nat_type; +Type *Eq_type, *Less_type, *Leq_type; +Type *NatWidth_type, *NatLess_type, *NatLeq_type, *Int_type, *UInt_type; +Type* Bits_type; +Type *Any_type, *Cell_type; + +int types_num, builtin_types_num; +std::vector types; + +int Type::last_declared_counter; + +void TypeExpr::check_mode(const src::SrcLocation& loc, int mode) { + if (!(mode & (1 << (is_nat ? 1 : 0)))) { + if (is_nat) { + throw src::ParseError{loc, "type expression required"}; + } else { + throw src::ParseError{loc, "integer expression required"}; + } + } + if (tchk_only && !(mode & 8)) { + throw src::ParseError{where, "type expression can be used only in a type-checking context"}; + } +} + +bool TypeExpr::no_tchk() const { + if (tchk_only) { + throw src::ParseError{where, "type expression can be used only in a type-checking context"}; + return false; + } + return true; +} + +TypeExpr* TypeExpr::mk_intconst(const src::SrcLocation& loc, unsigned int_const) { + return new TypeExpr{loc, te_IntConst, (int)int_const}; +} + +TypeExpr* TypeExpr::mk_intconst(const src::SrcLocation& loc, std::string int_const) { + char* end_ptr = 0; + long long value = -1; + if (!int_const.empty()) { + value = std::strtoll(int_const.c_str(), &end_ptr, 0); + } + if (value < 0 || value >= (1LL << 31) || end_ptr != int_const.c_str() + int_const.size()) { + throw src::ParseError{loc, "integer constant does not fit in an unsigned 31-bit integer"}; + } + return mk_intconst(loc, (unsigned)value); +} + +TypeExpr* TypeExpr::mk_apply_gen(const src::SrcLocation& loc, TypeExpr* expr1, TypeExpr* expr2) { + if (expr1->tp != te_Apply) { + throw src::ParseError{loc, "cannot apply one expression to the other"}; + } + expr1->args.push_back(expr2); + return expr1; +} + +TypeExpr* TypeExpr::mk_mulint(const src::SrcLocation& loc, TypeExpr* expr1, TypeExpr* expr2) { + if (expr1->tp != te_IntConst && expr2->tp != te_IntConst) { + throw src::ParseError{loc, "multiplication allowed only by constant values"}; + } + if (expr2->tp != te_IntConst) { + std::swap(expr1, expr2); + } + if (!expr1->is_nat) { + throw src::ParseError{expr1->where, "argument to integer multiplication should be a number"}; + } + if (expr1->tp == te_IntConst) { + long long product = (long long)expr1->value * expr2->value; + if (product < 0 || product >= (1LL << 31)) { + throw src::ParseError{loc, "product does not git in 31 bits"}; + } + return mk_intconst(loc, (unsigned)product); + } + int val = expr2->value; + if (!val) { + return expr2; + } + // delete expr2; + return new TypeExpr{loc, te_MulConst, val, {expr1}, expr1->negated}; +} + +TypeExpr* TypeExpr::mk_apply(const src::SrcLocation& loc, int tp, TypeExpr* expr1, TypeExpr* expr2) { + TypeExpr* expr = new TypeExpr{loc, tp, 0, {expr1, expr2}}; + return expr; +} + +TypeExpr* TypeExpr::mk_cellref(const src::SrcLocation& loc, TypeExpr* expr1) { + TypeExpr* expr = new TypeExpr{loc, te_Ref, 0, {expr1}}; + return expr; +} + +Field& Constructor::new_field(const src::SrcLocation& field_where, bool implicit, sym_idx_t name = 0) { + assert((long)fields_num == (long)fields.size()); + if (name) { + sym::SymDef* sym_def = sym::lookup_symbol(name, 1); + if (sym_def) { + throw src::ParseError{field_where, "redefined field or parameter"}; + } + } + fields.emplace_back(field_where, implicit, fields_num++, name); + return fields.back(); +} + +std::string Field::get_name() const { + return sym::symbols.get_name(name); +} + +// register symbol in local symbol table +void Field::register_sym() const { + if (name) { + sym::SymDef* sym_def = sym::lookup_symbol(name, 1); + if (sym_def) { + throw src::ParseError{loc, "redefined field or parameter"}; + } else { + sym_def = sym::define_symbol(name, true, loc); + if (!sym_def) { + throw src::ParseError{loc, "cannot register field"}; + } + } + delete sym_def->value; + sym_def->value = new SymVal((int)SymVal::_Param, field_idx, type); + } +} + +bool TypeExpr::close(const src::SrcLocation& loc) { + if (tp != te_Apply) { + return true; + } + Type* type = type_applied; + assert(type); + if (type->arity < 0) { + type->arity = (int)args.size(); + type->args.resize(type->arity, 0); + } else if (type->arity != (int)args.size()) { + throw src::ParseError{where, + std::string{"operator `"} + sym::symbols.get_name(type->type_name) + + "` applied with incorrect number of arguments, partial type applications not supported"}; + return false; + } + is_nat_subtype = type->produces_nat; + bool is_eq = (type == Eq_type); + int neg_cnt = 0; + for (int i = 0; i < type->arity; i++) { + TypeExpr* arg = args[i]; + int& x = type->args[i]; + if (arg->negated) { + ++neg_cnt; + negated = true; + if (!is_eq) { + if (x & Type::_IsPos) { + throw src::ParseError{arg->where, std::string{"passed an argument of incorrect polarity to `"} + + sym::symbols.get_name(type->type_name) + "`"}; + } + x |= Type::_IsNeg; + } else if (neg_cnt == 2) { + throw src::ParseError{loc, "cannot equate two expressions of negative polarity"}; + } + } + arg->no_tchk(); + if (arg->is_nat) { + x |= Type::_IsNat; + } else { + x |= Type::_IsType; + if (arg->negated) { + throw src::ParseError{arg->where, "cannot use negative types as arguments to other types"}; + } + } + } + tchk_only = negated = neg_cnt; + return true; +} + +TypeExpr* TypeExpr::mk_apply_empty(const src::SrcLocation& loc, sym_idx_t name, Type* type_applied) { + TypeExpr* expr = new TypeExpr{loc, te_Apply, name}; + expr->type_applied = type_applied; + expr->is_nat_subtype = (type_applied->produces_nat && !type_applied->arity); + return expr; +} + +void Type::print_name(std::ostream& os) const { + if (type_name) { + os << sym::symbols.get_name(type_name); + } else { + os << "TYPE_" << type_idx; + } +} + +std::string Type::get_name() const { + if (type_name) { + return sym::symbols.get_name(type_name); + } else { + std::ostringstream os; + os << "TYPE_" << type_idx; + return os.str(); + } +} + +std::string Constructor::get_name() const { + return sym::symbols.get_name(constr_name); +} + +std::string Constructor::get_qualified_name() const { + return type_defined->get_name() + "::" + get_name(); +} + +void TypeExpr::show(std::ostream& os, const Constructor* cs, int prio, int mode) const { + if (mode & 2) { + prio = 0; + } + switch (tp) { + case te_Type: + os << "Type"; + break; + case te_Param: { + int i = value; + sym_idx_t param_name = 0; + if (cs && i >= 0 && i < cs->fields_num) { + param_name = cs->fields.at(i).name; + } + if (negated ^ (mode & 1)) { + os << '~'; + } + if (param_name > 0) { + os << sym::symbols.get_name(param_name); + } else { + os << '_' << i + 1; + } + break; + } + case te_Apply: + if (!args.size() && !type_applied->type_name && type_applied->constr_num == 1 && + !type_applied->constructors.at(0)->constr_name && + !(type_applied->constructors[0]->tag & ((1ULL << 63) - 1))) { + type_applied->constructors[0]->show(os, mode | 4); + } else { + if (prio > 90 && args.size()) { + os << '('; + } + type_applied->print_name(os); + for (TypeExpr* arg : args) { + os << ' '; // priority 90 + arg->show(os, cs, 91, mode); + } + if (prio > 90 && args.size()) { + os << ')'; + } + } + break; + case te_Add: { + assert(args.size() == 2); + if (prio > 20) { + os << '('; + } + args[0]->show(os, cs, 20, mode); + os << " + "; // priority 20 + args[1]->show(os, cs, 21, mode); + if (prio > 20) { + os << ')'; + } + break; + } + case te_IntConst: { + assert(args.empty()); + os << value; + break; + } + case te_MulConst: { + if (prio > 30) { + os << '('; + } + assert(args.size() == 1); + os << value << " * "; // priority 30 + args[0]->show(os, cs, 31, mode); + if (prio > 30) { + os << ')'; + } + break; + } + case te_Tuple: { + assert(args.size() == 2); + if (prio > 30) { + os << '('; + } + args[0]->show(os, cs, 30, mode); + os << " * "; // priority 30 + args[1]->show(os, cs, 31, mode); + if (prio > 30) { + os << ')'; + } + break; + } + case te_CondType: { + assert(args.size() == 2); + if (prio > 95) { + os << '('; + } + args[0]->show(os, cs, 96, mode); + os << "?"; // priority 95 + args[1]->show(os, cs, 96, mode); + if (prio > 95) { + os << ')'; + } + break; + } + case te_Ref: { + assert(args.size() == 1); + os << '^'; // priority 100 + args[0]->show(os, cs, 100, mode); + break; + } + default: + os << "(unknown-type)"; + } +} + +bool TypeExpr::equal(const TypeExpr& other) const { + if (tp != other.tp || value != other.value || type_applied != other.type_applied || + args.size() != other.args.size()) { + return false; + } + for (std::size_t i = 0; i < args.size(); i++) { + if (!args[i]->equal(*other.args[i])) { + return false; + } + } + return true; +} + +// 0 = 0, 1 = 1, 2 = any even >= 2, 3 = any odd >= 3 +// (We work in N/(4~2), or in the free semilattice generated by it.) +int abstract_nat_const(int value) { + return 1 << ((value & 1) + (value >= 2 ? 2 : 0)); +} + +unsigned char abstract_add_base_table[4][4] = {{0, 1, 2, 3}, {1, 2, 3, 2}, {2, 3, 2, 3}, {3, 2, 3, 2}}; + +unsigned char abstract_mul_base_table[4][4] = {{0, 0, 0, 0}, {0, 1, 2, 3}, {0, 2, 2, 2}, {0, 3, 2, 3}}; + +unsigned char abstract_add_table[16][16]; +unsigned char abstract_mul_table[16][16]; + +void compute_semilat_table(unsigned char table[16][16], const unsigned char base_table[4][4]) { + for (int x = 0; x < 16; x++) { + for (int y = 0; y < 16; y++) { + int res = 0; + for (int i = 0; i < 4; i++) { + if ((x >> i) & 1) { + for (int j = 0; j < 4; j++) { + if ((y >> j) & 1) { + res |= 1 << base_table[i][j]; + } + } + } + } + table[x][y] = (unsigned char)res; + } + } +} + +void init_abstract_tables() { + compute_semilat_table(abstract_add_table, abstract_add_base_table); + compute_semilat_table(abstract_mul_table, abstract_mul_base_table); +} + +int abstract_add(int x, int y) { + return abstract_add_table[x & 15][y & 15]; +} + +int abstract_mul(int x, int y) { + return abstract_mul_table[x & 15][y & 15]; +} + +int TypeExpr::abstract_interpret_nat() const { + if (!is_nat || tchk_only) { + return 0; + } + switch (tp) { + case te_Param: + return 0xf; // for now, natural parameters can take arbitrary values + case te_Add: + assert(args.size() == 2); + return abstract_add(args[0]->abstract_interpret_nat(), args[1]->abstract_interpret_nat()); + case te_IntConst: + return abstract_nat_const(value); + case te_MulConst: + assert(args.size() == 1); + return abstract_mul(args[0]->abstract_interpret_nat(), abstract_nat_const(value)); + default: + return 0xf; + } +} + +MinMaxSize TypeExpr::compute_size() const { + if (is_nat) { + return {0}; + } + switch (tp) { + case te_Type: + return {0}; + case te_Param: + return {MinMaxSize::Any}; // any size possible for type parameters + case te_Ref: { + assert(args.size() == 1); + bool f = args[0]->compute_size().is_possible(); + return f ? MinMaxSize::OneRef : MinMaxSize::Impossible; + } + case te_CondType: { + assert(args.size() == 2); + int z = args[0]->abstract_interpret_nat(); + if (!(z & ~1)) { + return {0}; // always 0 + } else { + MinMaxSize t = args[1]->compute_size(); + if (z & 1) { + t.clear_min(); + } + return t; + } + } + case te_Tuple: { + assert(args.size() == 2); + int z = args[0]->abstract_interpret_nat(); + if (!(z & ~1)) { + return {0}; // always 0 + } else { + MinMaxSize t = args[1]->compute_size(); + if (args[0]->tp == te_IntConst) { + t.repeat(args[0]->value); + return t; + } + if (z & 1) { + t.clear_min(); // zero repetition count possible + } + if (z & 12) { + // may be repeated more than once + int n = ((z & 1) ? 0 : ((z & 2) ? 1 : 2)); // minimal value of repetition count + t.repeat_at_least(n); // repetition count >= n + } + return t; + } + } + case te_Apply: { + if (args.size() == 1 && args[0]->tp == te_IntConst) { + int n = args[0]->value; + if (type_applied == NatWidth_type || type_applied == Int_type || type_applied == UInt_type || + type_applied == Bits_type) { + return MinMaxSize::fixed_size(std::min(n, 2047)); + } else if (type_applied == NatLeq_type) { + return MinMaxSize::fixed_size(32 - td::count_leading_zeroes32(n)); + } else if (type_applied == NatLess_type) { + return MinMaxSize::fixed_size(n ? 32 - td::count_leading_zeroes32(n - 1) : 2047); + } + } + return type_applied->size; + } + } // end switch + return {}; +} + +bool TypeExpr::compute_any_bits() const { + if (is_nat) { + return true; + } + switch (tp) { + case te_Type: + return true; + case te_Param: + return false; + case te_Ref: + return true; + case te_Tuple: + case te_CondType: { + assert(args.size() == 2); + int z = args[0]->abstract_interpret_nat(); + if (!(z & ~1)) { + return true; // always 0 + } else { + return args[1]->compute_any_bits(); + } + } + case te_Apply: { + if (args.size() == 1 && args[0]->tp == te_IntConst) { + int n = args[0]->value; + if (type_applied == NatLeq_type) { + return !(n & (n + 1)); + } else if (type_applied == NatLess_type) { + return !(n & (n - 1)); + } + } + return type_applied->any_bits; + } + } // end switch + return false; +} + +int TypeExpr::is_integer() const { + if (is_nat) { + return 1; + } + if (tp != te_Apply) { + return 0; + } + const Type* ta = type_applied; + if (ta == Int_type) { + return -1; + } else if (ta == UInt_type) { + return 1; + } + if (ta->is_bool) { + return 1; + } + if (ta->is_builtin) { + return ta->is_integer; + } + return 0; +} + +bool TypeExpr::is_ref_to_anon() const { + return tp == te_Ref && args.at(0)->is_anon(); +} + +bool TypeExpr::is_anon() const { + return tp == te_Apply && args.empty() && type_applied->is_anon; +} + +unsigned long long TypeExpr::compute_hash() const { + unsigned long long h = tp * 17239ULL + value * 23917ULL + 1; + if (type_applied) { + h += 239017 * type_applied->type_idx; + } + for (const TypeExpr* arg : args) { + h *= 170239; + if (!arg->negated) { + h += arg->is_constexpr; + } + } + return h; +} + +bool TypeExpr::detect_constexpr() { + if (is_constexpr) { + return true; + } + bool c = !negated; + for (TypeExpr* arg : args) { + if (!arg->detect_constexpr() && !arg->negated) { + c = false; + } + } + if (!c || tp == te_Param) { + return false; + } + unsigned long long hash = compute_hash(); + unsigned long long h1 = hash % const_htable_size, h2 = 1 + hash % (const_htable_size + 1); + while (const_htable[h1]) { + TypeExpr* other = const_htable[h1]; + if (other->tp == tp && other->value == value && other->type_applied == type_applied && + other->args.size() == args.size()) { + bool match = true; + for (std::size_t i = 0; i < args.size(); i++) { + if (other->args[i]->negated != args[i]->negated || other->args[i]->is_constexpr != args[i]->is_constexpr) { + match = false; + break; + } + } + if (match) { + is_constexpr = other->is_constexpr; + assert(is_constexpr); + return true; + } + } + h1 += h2; + if (h1 >= const_htable_size) { + h1 -= const_htable_size; + } + } + assert(const_type_expr_num < max_const_expr - 1); + const_type_expr[is_constexpr = ++const_type_expr_num] = this; + const_htable[h1] = this; + return true; +} + +void TypeExpr::const_type_name(std::ostream& os) const { + if (negated) { + return; + } + switch (tp) { + case te_Type: + os << "_Type"; + return; + case te_Param: + return; + case te_Add: + args[0]->const_type_name(os); + os << "_plus"; + args[1]->const_type_name(os); + return; + case te_IntConst: + os << "_" << value; + return; + case te_MulConst: + os << "_mul" << value; + return; + case te_Ref: + os << "_Ref"; + args[0]->const_type_name(os); + return; + case te_Tuple: + os << "_tuple"; + args[0]->const_type_name(os); + args[1]->const_type_name(os); + return; + case te_CondType: + os << "_if"; + args[0]->const_type_name(os); + args[1]->const_type_name(os); + return; + case te_Apply: + os << '_'; + if (type_applied->produces_nat) { + if (type_applied == Nat_type) { + os << "nat"; + } else if (type_applied == NatWidth_type) { + os << "natwidth"; + } else if (type_applied == NatLeq_type) { + os << "natleq"; + } else if (type_applied == NatLess_type) { + os << "natless"; + } + } else { + os << type_applied->get_name(); + } + for (const TypeExpr* arg : args) { + arg->const_type_name(os); + } + return; + } +} + +bool TypeExpr::bind_value(bool value_negated, Constructor& cs, bool checking_type) { + // if checking_type == false: + // negated = false, value_negated = false: compute expression, compare to value (only for integers) + // negated = false, value_negated = true: compute expression, return it to the "value" + // negated = true, value_negated = false: assign the value to the expression to compute some of the variables present in the expression + // if checking_type == true: + // value_negated must be false + /* (debug output) + std::cerr << "binding " << (value_negated ? "negative" : "positive") << " value to expression " << (checking_type ? "of type " : ""); + show(std::cerr, &cs); + std::cerr << std::endl; + */ + if (!checking_type) { + no_tchk(); + } else { + if (is_nat) { + throw src::ParseError{where, "cannot use check a type against an integer expression"}; + } + if (value_negated) { + throw src::ParseError{where, "cannot compute a value knowing only its type"}; + } + } + if (negated && value_negated) { + // both the expression and the value cannot be negative + throw src::ParseError{where, "expression has wrong polarity"}; + } + if (!is_nat) { + // for type expressions: + if (value_negated) { + // cannot "return" values that are not integer (i.e. types) + // throw src::ParseError{where, "cannot assign or return type expressions"}; + // in reality, this check should be only when parsing type parameters after constructors + } + if (!checking_type) { + // "true" equality/assignment of type expressions + if (!negated && !value_negated) { + if (tp == te_Apply && args.empty()) { + throw src::ParseError{where, "use of a global type or an undeclared variable"}; + } else { + throw src::ParseError{where, "cannot check type expressions for equality"}; + } + } + // available only if the expression is a free variable + if (negated && tp != te_Param) { + throw src::ParseError{where, "types can be assigned only to free type variables"}; + } + } + } + switch (tp) { + case te_Add: { + assert(is_nat && args.size() == 2 && !(args[0]->negated && args[1]->negated)); + assert(negated == (args[0]->negated || args[1]->negated)); + int i = args[0]->negated; + args[i]->bind_value(negated, cs); + args[1 - i]->bind_value(false, cs); + return true; + } + case te_IntConst: { + assert(is_nat && !negated); + return true; + } + case te_MulConst: { + assert(is_nat && args.size() == 1 && value > 0); + assert(negated == args[0]->negated); + args[0]->bind_value(value_negated, cs); + return true; + } + case te_Type: { + assert(!is_nat && !negated); + return true; + } + case te_Param: { + assert(value >= 0 && value < cs.fields_num); + Field& field = cs.fields.at(value); + if (!negated || checking_type) { + if (!field.known) { + throw src::ParseError{where, + std::string{"variable `"} + field.get_name() + "` used before being assigned to"}; + } else { + field.used = true; + } + } else if (!field.known) { + field.known = true; + // where.show_note(std::string{"variable `"} + field.get_name() + "` is assigned a value here"); + } + return true; + } + case te_Apply: + if (type_applied == Eq_type) { + assert(args.size() == 2 && !(args[0]->negated && args[1]->negated)); + assert(negated == (args[0]->negated || args[1]->negated)); + int i = args[0]->negated; + args[i]->bind_value(negated, cs); + args[1 - i]->bind_value(false, cs); + return true; + } else { + assert(!negated || checking_type); + for (TypeExpr* arg : args) { + if (!arg->negated) { + arg->bind_value(true, cs); + } + } + for (TypeExpr* arg : args) { + if (arg->negated) { + arg->bind_value(false, cs); + } + } + return true; + } + case te_CondType: + case te_Tuple: { + assert(args.size() == 2); + assert(!negated && !args[0]->negated && !args[1]->negated); + assert(args[0]->is_nat); + assert(!args[1]->is_nat); + args[0]->bind_value(true, cs); + args[1]->bind_value(true, cs); + return true; + } + case te_Ref: { + assert(args.size() == 1); + return args[0]->bind_value(value_negated, cs, checking_type); + } + default: + throw src::ParseError{where, "cannot bind a value to an expression of unknown sort"}; + } + return true; +} + +void parse_implicit_param(Lexer& lex, Constructor& cs) { + // ident : # or ident : Type + if (lex.tp() != src::_Ident) { + lex.expect(src::_Ident); + } + Field& field = cs.new_field(lex.cur().loc, true, lex.cur().val); + lex.next(); + lex.expect(':'); + if (lex.tp() != src::_Type && (lex.tp() != src::_Ident || lex.cur().val != Nat_name)) { + throw src::ParseError{lex.cur().loc, "either `Type` or `#` implicit parameter type expected"}; + } + if (lex.tp() == src::_Type) { + field.type = &type_Type; + } else { + field.type = TypeExpr::mk_apply_empty(lex.cur().loc, Nat_name, Nat_type); + assert(Nat_type->produces_nat); + assert(!Nat_type->arity); + assert(field.type->is_nat_subtype); + } + lex.next(); + field.register_sym(); +} + +sym::SymDef* register_new_type(const src::SrcLocation& loc, sym_idx_t name) { + // unknown identifier, declare new type + if (!sym::is_uc_ident(name)) { + throw src::ParseError{loc, std::string{"implicitly defined type `"} + sym::symbols.get_name(name) + + "` must begin with an uppercase letter"}; + } + sym::SymDef* sym_def = sym::define_global_symbol(name, true, loc); + assert(sym_def); + types.emplace_back(types_num++, name); + sym_def->value = new SymValType{&types.back()}; + return sym_def; +} + +void show_tag(std::ostream& os, unsigned long long tag) { + if (!tag) { + return; + } + if (!(tag & ((1ULL << 59) - 1))) { + os << '$'; + int c = 0; + while (tag & ((1ULL << 63) - 1)) { + os << (tag >> 63); + tag <<= 1; + ++c; + } + if (!c) { + os << '_'; + } + } else { + os << '#'; + while (tag & ((1ULL << 63) - 1)) { + static const char hex_digits[] = "0123456789abcdef"; + os << hex_digits[tag >> 60]; + tag <<= 4; + } + if (!tag) { + os << '_'; + } + } +} + +void Constructor::show(std::ostream& os, int mode) const { + if (mode & 4) { + os << '['; + } else { + os << sym::symbols.get_name(constr_name); + } + if (!(mode & 8)) { + show_tag(os, tag); + } + for (const Field& field : fields) { + os << ' '; + if (field.implicit || field.constraint) { + if (!(mode & 2)) { + os << '{'; + } + if (field.name) { + os << field.get_name() << ':'; + } + field.type->show(os, this, 0, mode & ~1); + if (!(mode & 2)) { + os << '}'; + } + } else { + if (field.name) { + os << field.get_name() << ':'; + } + field.type->show(os, this, 95, mode & ~1); + } + } + if (mode & 4) { + os << " ]"; + return; + } + os << " = "; + if (type_defined) { + type_defined->print_name(os); + } else { + os << sym::symbols.get_name(type_name); + } + for (int i = 0; i < type_arity; i++) { + os << ' '; + if (param_negated.at(i)) { + os << '~'; + } + params.at(i)->show(os, this, 100, mode | 1); + } + if (!(mode & 2)) { + os << ';'; + } +} + +unsigned long long Constructor::compute_tag() const { + std::ostringstream os; + show(os, 10); + unsigned crc = td::crc32(td::Slice{os.str()}); + if (verbosity > 2) { + std::cerr << "crc32('" << os.str() << "') = " << std::hex << crc << std::dec << std::endl; + } + return ((unsigned long long)crc << 32) | 0x80000000; +} + +bool Constructor::compute_is_fwd() { + if (constr_name || tag_bits || type_arity || fields_num != 1) { + return is_fwd = false; + } + return is_fwd = (!fields[0].implicit && !fields[0].constraint); +} + +bool show_tag_warnings; + +void Constructor::check_assign_tag() { + if (constr_name && (!tag || (tag & (1ULL << 63)) == (1ULL << 63))) { + unsigned long long computed_tag = compute_tag(); + if (!tag) { + set_tag(computed_tag); + if (show_tag_warnings) { + std::ostringstream os; + os << "constructor `" << sym::symbols.get_name(type_name) << "::" << sym::symbols.get_name(constr_name) + << "` had no tag, assigned "; + show_tag(os, computed_tag); + where.show_warning(os.str()); + } + } else if (tag != computed_tag && show_tag_warnings) { + std::ostringstream os; + os << "constructor `" << sym::symbols.get_name(type_name) << "::" << sym::symbols.get_name(constr_name) + << "` has explicit tag "; + show_tag(os, tag); + os << " different from its computed tag "; + show_tag(os, computed_tag); + where.show_warning(os.str()); + } + } else if (!constr_name && !tag) { + set_tag(1ULL << 63); + } +} + +void Type::bind_constructor(const src::SrcLocation& loc, Constructor* cs) { + if (is_final) { + throw src::ParseError{loc, std::string{"cannot add new constructor `"} + sym::symbols.get_name(cs->constr_name) + + "` to a finalized type `" + sym::symbols.get_name(type_name) + "`"}; + } + if (arity < 0) { + arity = cs->type_arity; + assert(arity >= 0); + args.resize(arity, 0); + } else { + if (arity != cs->type_arity) { + throw src::ParseError{loc, std::string{"parametrized type `"} + sym::symbols.get_name(type_name) + + "` redefined with different arity"}; + } + } + assert(arity == cs->type_arity && arity == (int)cs->params.size() && cs->params.size() == cs->param_negated.size()); + int true_params = 0; + for (int i = 0; i < arity; i++) { + auto expr = cs->params.at(i); + bool negated = cs->param_negated.at(i); + int& x = args[i]; + x |= (expr->is_nat ? _IsNat : _IsType); + if ((x & (_IsNat | _IsType)) == (_IsNat | _IsType)) { + throw src::ParseError{expr->where, std::string{"formal parameter to type `"} + sym::symbols.get_name(type_name) + + "` has incorrect type"}; + } + x |= (negated ? _IsNeg : _IsPos); + if ((x & (_IsPos | _IsNeg)) == (_IsPos | _IsNeg)) { + throw src::ParseError{expr->where, std::string{"formal parameter to type `"} + sym::symbols.get_name(type_name) + + "` has incorrect polarity"}; + } + if (cs->param_const_val.at(i) < 0) { + x |= _NonConst; + } + true_params += !negated; + } + assert(cs->fields_num >= 0 && (long long)cs->fields_num == (long long)cs->fields.size()); + int explicit_fields = 0; + for (Field& field : cs->fields) { + if (field.constraint) { + field.type->bind_value(false, *cs, true); + field.known = true; + } else if (!field.implicit) { + ++explicit_fields; + field.type->bind_value(false, *cs, true); + if (!field.known) { + // field.loc.show_note(std::string{"variable `"} + field.get_name() + "` is assigned a value here"); + } + field.known = true; + } + } + cs->is_enum = !explicit_fields; + cs->is_simple_enum = (cs->is_enum && !true_params); + for (int i = 0; i < arity; i++) { + auto expr = cs->params[i]; + bool negated = cs->param_negated[i]; + if (negated) { + expr->bind_value(true, *cs); + } + } + for (Field& field : cs->fields) { + if (!field.known) { + throw src::ParseError{field.loc, std::string{"field `"} + field.get_name() + "` is left unbound"}; + } + } + if (cs->constr_name) { + for (auto c : constructors) { + if (c->constr_name == cs->constr_name) { + std::string tname = sym::symbols.get_name(type_name); + std::string cname = tname + "::" + sym::symbols.get_name(cs->constr_name); + c->where.show_note(std::string{"constructor `"} + cname + "` first defined here"); + throw src::ParseError{cs->where, std::string{"constructor `"} + cname + "` redefined"}; + } + } + } + if (!cs->type_defined && cs->type_name == type_name) { + cs->type_defined = this; + } + cs->check_assign_tag(); + cs->compute_is_fwd(); + is_enum &= cs->is_enum; + is_simple_enum &= cs->is_simple_enum; + if (constr_num && (is_special != cs->is_special)) { + throw src::ParseError{cs->where, std::string{"type `"} + sym::symbols.get_name(type_name) + + "` has mixed special and non-special constructors"}; + } + is_special = cs->is_special; + ++(constr_num); + constructors.push_back(cs); +} + +bool Type::unique_constructor_equals(const Constructor& cs, bool allow_other_names) const { + return constr_num == 1 && constructors.at(0)->isomorphic_to(cs, allow_other_names); +} + +bool Constructor::isomorphic_to(const Constructor& cs, bool allow_other_names) const { + if (constr_name != cs.constr_name || tag != cs.tag || fields_num != cs.fields_num || type_arity != cs.type_arity || + params.size() != cs.params.size()) { + return false; + } + for (int i = 0; i < fields_num; i++) { + if (!fields.at(i).isomorphic_to(cs.fields.at(i), allow_other_names)) { + return false; + } + } + for (std::size_t i = 0; i < params.size(); i++) { + if (!params.at(i)->equal(*cs.params.at(i))) { + return false; + } + } + return true; +} + +bool Field::isomorphic_to(const Field& f, bool allow_other_names) const { + if (f.field_idx != field_idx || f.implicit != implicit || f.constraint != constraint || + (!allow_other_names && f.name != name)) { + return false; + } + return f.type->equal(*type); +} + +/* + * + * TL-B SOURCE PARSER + * + */ + +void parse_field_list(Lexer& lex, Constructor& cs); + +TypeExpr* parse_anonymous_constructor(Lexer& lex, Constructor& cs) { + sym::open_scope(lex); + Constructor* cs2 = new Constructor(lex.cur().loc); // anonymous constructor + parse_field_list(lex, *cs2); + if (lex.tp() != ']') { + lex.expect(']'); + } + cs2->set_tag(1ULL << 63); + for (int i = builtin_types_num; i < types_num; i++) { + if (types.at(i).is_auto && types[i].is_final && types[i].unique_constructor_equals(*cs2)) { + sym::close_scope(lex); + if (types[i].parent_type_idx >= 0) { + types[i].parent_type_idx = -2; + } + delete cs2; + return TypeExpr::mk_apply_empty(lex.cur().loc, 0, &types[i]); + } + } + types.emplace_back(types_num++, 0); + Type* type = &types.back(); // anonymous type + type->bind_constructor(lex.cur().loc, cs2); + type->is_final = true; + type->is_auto = true; + type->is_anon = true; + type->renew_last_declared(); + sym::close_scope(lex); + return TypeExpr::mk_apply_empty(lex.cur().loc, 0, type); +} + +TypeExpr* parse_expr(Lexer& lex, Constructor& cs, int mode); + +// ( E ) | [ {field-def} ] | id | ~id | num | ^T +TypeExpr* parse_term(Lexer& lex, Constructor& cs, int mode) { + if (lex.tp() == '(') { + lex.next(); + TypeExpr* expr = parse_expr(lex, cs, mode); + expr->check_mode(lex.cur().loc, mode); + lex.expect(')'); + return expr; + } + if (lex.tp() == src::_Number) { + TypeExpr* expr = TypeExpr::mk_intconst(lex.cur().loc, lex.cur().str); + expr->check_mode(lex.cur().loc, mode); + lex.next(); + return expr; + } + if (lex.tp() == '[') { + lex.next(); + TypeExpr* expr = parse_anonymous_constructor(lex, cs); + expr->check_mode(lex.cur().loc, mode); + lex.expect(']'); + return expr; + } + if (lex.tp() == '^') { + src::SrcLocation loc = lex.cur().loc; + lex.next(); + TypeExpr* expr = parse_term(lex, cs, mode & ~2); + expr->close(lex.cur().loc); + if (expr->is_nat) { + throw src::ParseError{loc, "cannot create a cell reference type to a natural number"}; + } + return TypeExpr::mk_cellref(loc, expr); + } + bool negate = false; + if (lex.tp() == '~') { + lex.next(); + if (lex.tp() != src::_Ident) { + lex.expect(src::_Ident, "field identifier"); + } + negate = true; + } + if (lex.tp() == src::_Ident) { + sym_idx_t name = lex.cur().val; + sym::SymDef* sym_def = sym::lookup_symbol(name); + if (!sym_def) { + if (negate) { + throw src::ParseError{lex.cur().loc, "field identifier expected"}; + } + sym_def = register_new_type(lex.cur().loc, name); + if (verbosity > 2) { + std::cerr << "implicitly defined new type `" << sym::symbols.get_name(name) << "`" << std::endl; + } + } + if (!sym_def->value) { + throw src::ParseError{lex.cur().loc, "global symbol has no value"}; + } + if (sym_def->value->type == sym::SymValBase::_Typename) { + // found a global type identifier + if (negate) { + throw src::ParseError{lex.cur().loc, "cannot negate a type"}; + } + SymValType* svt = dynamic_cast(sym_def->value); + assert(svt && svt->type_ref); + (svt->type_ref->used)++; + auto res = TypeExpr::mk_apply_empty(lex.cur().loc, name, svt->type_ref); + lex.next(); + return res; + } + SymVal* sym_val = dynamic_cast(sym_def->value); + if (sym_def->value->type != sym::SymValBase::_Param || !sym_val) { + throw src::ParseError{lex.cur().loc, "field identifier expected"}; + } + if (sym_def->level != sym::scope_level) { + throw src::ParseError{lex.cur().loc, std::string{"cannot access field `"} + lex.cur().str + "` from outer scope"}; + } + int i = sym_val->idx; + assert(i >= 0 && i < cs.fields_num); + auto res = new TypeExpr{lex.cur().loc, TypeExpr::te_Param, i}; + auto field_type = cs.fields[i].type; + assert(field_type); + if ((mode & 4) && !cs.fields[i].known) { + // auto-negate, used for parsing type parameters in constructor RHS + negate = true; + } + res->is_nat = field_type->is_nat_subtype; + // std::cerr << "using field " << lex.cur().str << "; is_nat_subtype = " << res->is_nat << std::endl; + if (!res->is_nat && field_type->tp != TypeExpr::te_Type) { + throw src::ParseError{lex.cur().loc, + "cannot use a field in an expression unless it is either an integer or a type"}; + } + if (negate && !cs.fields[i].implicit) { + throw src::ParseError{lex.cur().loc, "cannot negate an explicit field"}; + } + res->negated = negate; + res->check_mode(lex.cur().loc, mode); + lex.next(); + return res; + } else { + lex.expect(src::_Ident, "type identifier"); + return nullptr; + } +} + +// E ? E [ : E ] + +TypeExpr* parse_expr95(Lexer& lex, Constructor& cs, int mode) { + TypeExpr* expr = parse_term(lex, cs, mode | 3); + if (lex.tp() != '?') { + expr->check_mode(lex.cur().loc, mode); + return expr; + } + src::SrcLocation where = lex.cur().loc; + expr->close(where); + if (!expr->is_nat) { + throw src::ParseError{where, "cannot apply `?` with non-integer selectors"}; + } + lex.next(); + TypeExpr* expr2 = parse_term(lex, cs, mode & ~10); + expr2->close(lex.cur().loc); + expr2->no_tchk(); + expr = TypeExpr::mk_apply(where, TypeExpr::te_CondType, expr, expr2); + expr->check_mode(lex.cur().loc, mode); + return expr; +} + +// E E +TypeExpr* parse_expr90(Lexer& lex, Constructor& cs, int mode) { + TypeExpr* expr = parse_expr95(lex, cs, mode | 3); + while (lex.tp() == '(' || lex.tp() == src::_Ident || lex.tp() == src::_Number || lex.tp() == '~' || lex.tp() == '^' || + lex.tp() == '[') { + TypeExpr* expr2 = parse_expr95(lex, cs, mode | 3); + expr2->close(lex.cur().loc); + expr = TypeExpr::mk_apply_gen(lex.cur().loc, expr, expr2); + } + expr->check_mode(lex.cur().loc, mode); + return expr; +} + +// E * E +TypeExpr* parse_expr30(Lexer& lex, Constructor& cs, int mode) { + TypeExpr* expr = parse_expr90(lex, cs, mode); + while (lex.tp() == '*') { + src::SrcLocation where = lex.cur().loc; + expr->close(lex.cur().loc); + if (!expr->is_nat) { + throw src::ParseError{where, "cannot apply `*` to types"}; + } + lex.next(); + TypeExpr* expr2 = parse_expr90(lex, cs, mode); + expr2->close(lex.cur().loc); + if (expr2->is_nat) { + expr = TypeExpr::mk_mulint(where, expr, expr2); + } else { + expr2->no_tchk(); + expr = TypeExpr::mk_apply(where, TypeExpr::te_Tuple, expr, expr2); + } + } + expr->check_mode(lex.cur().loc, mode); + return expr; +} + +// E + E +TypeExpr* parse_expr20(Lexer& lex, Constructor& cs, int mode) { + TypeExpr* expr = parse_expr30(lex, cs, mode); + while (lex.tp() == '+') { + src::SrcLocation where = lex.cur().loc; + expr->close(lex.cur().loc); + // std::cerr << "parse +, mode " << mode << std::endl; + if (!(mode & 2)) { + throw src::ParseError{where, "sum cannot be used instead of a type expression"}; + } + if (!expr->is_nat) { + throw src::ParseError{where, "cannot apply `+` to types"}; + } + lex.next(); + TypeExpr* expr2 = parse_expr30(lex, cs, mode & ~1); + expr2->close(lex.cur().loc); + if (expr->negated && expr2->negated) { + throw src::ParseError{where, "cannot add two values of negative polarity"}; + } + bool negated = expr->negated | expr2->negated; + expr = TypeExpr::mk_apply(where, TypeExpr::te_Add, expr, expr2); + expr->negated = negated; + } + expr->check_mode(lex.cur().loc, mode); + return expr; +} + +// E | E = E | E <= E | E < E | E >= E | E > E +TypeExpr* parse_expr10(Lexer& lex, Constructor& cs, int mode) { + TypeExpr* expr = parse_expr20(lex, cs, mode | 3); + int op = lex.tp(); + if (!(op == '=' || op == '<' || op == '>' || op == src::_Leq || op == src::_Geq)) { + expr->check_mode(lex.cur().loc, mode); + return expr; + } + // std::cerr << "parse <=>, mode " << mode << std::endl; + sym_idx_t op_name = lex.cur().val; + src::SrcLocation where = lex.cur().loc; + expr->close(where); + if (!(mode & 1)) { + throw src::ParseError{where, "comparison result used as an integer"}; + } + if (!expr->is_nat) { + throw src::ParseError{where, "cannot apply integer comparison to types"}; + } + lex.next(); + TypeExpr* expr2 = parse_expr20(lex, cs, (mode & ~1) | 2); + expr2->close(lex.cur().loc); + if (!expr2->is_nat) { + throw src::ParseError{lex.cur().loc, "cannot apply integer comparison to types"}; + } + if (op == '>') { + std::swap(expr, expr2); + op = '<'; + op_name = Less_name; + } else if (op == src::_Geq) { + std::swap(expr, expr2); + op = src::_Leq; + op_name = Leq_name; + } + auto sym_def = sym::lookup_symbol(op_name, 2); + assert(sym_def); + auto sym_val = dynamic_cast(sym_def->value); + assert(sym_val); + auto expr0 = TypeExpr::mk_apply_empty(where, op_name, sym_val->type_ref); + expr = TypeExpr::mk_apply_gen(where, std::move(expr0), expr); + expr = TypeExpr::mk_apply_gen(lex.cur().loc, expr, std::move(expr2)); + expr->check_mode(lex.cur().loc, mode); + return expr; +} + +TypeExpr* parse_expr(Lexer& lex, Constructor& cs, int mode) { + return parse_expr10(lex, cs, mode); +} + +void parse_param(Lexer& lex, Constructor& cs, bool named) { + // [ ( ident | _ ) : ] type-expr + src::SrcLocation loc = lex.cur().loc; + if (named && lex.tp() == '_') { + lex.next(); + lex.expect(':'); + named = false; + } + sym_idx_t param_name = 0; + if (named) { + if (lex.tp() != src::_Ident) { + lex.expect(src::_Ident); + } + param_name = lex.cur().val; + lex.next(); + lex.expect(':'); + } + Field& field = cs.new_field(loc, false, param_name); + field.type = parse_expr95(lex, cs, 9); // must be a type expression + field.type->close(lex.cur().loc); + field.type->detect_constexpr(); + field.subrec = field.type->is_ref_to_anon(); + CHECK(!field.name || !field.subrec); + field.register_sym(); +} + +void parse_constraint(Lexer& lex, Constructor& cs) { + Field& field = cs.new_field(lex.cur().loc, true, 0); + field.type = parse_expr(lex, cs, 9); // must be a type expression + field.type->close(lex.cur().loc); + field.type->detect_constexpr(); + field.constraint = true; + field.register_sym(); +} + +void parse_field_list(Lexer& lex, Constructor& cs) { + while (lex.tp() != '=' && lex.tp() != ']') { + if (lex.tp() == '{') { + // either an implicit parameter or a constraint + lex.next(); + if (lex.tp() == src::_Ident && lex.peek().tp == ':') { + parse_implicit_param(lex, cs); + } else { + parse_constraint(lex, cs); + } + lex.expect('}'); + } else if ((lex.tp() == src::_Ident || lex.tp() == '_') && lex.peek().tp == ':') { + parse_param(lex, cs, true); + } else { + parse_param(lex, cs, false); + } + } +} + +void parse_constructor_def(Lexer& lex) { + if (lex.tp() != '_' && (lex.tp() != src::_Ident || !sym::is_lc_ident(lex.cur().val))) { + throw src::ParseError{lex.cur().loc, "constructor name lowercase identifier expected"}; + } + bool is_special = sym::is_spec_lc_ident(lex.cur().val); + sym::open_scope(lex); + Lexem constr_lex = lex.cur(); + int orig_types_num = types_num; + sym_idx_t constr_name = (lex.tp() == src::_Ident ? lex.cur().val : 0); + src::SrcLocation where = lex.cur().loc; + lex.next(); + unsigned long long tag = 0; + if (lex.tp() == src::_Special) { + tag = src::get_special_value(lex.cur().str); + assert(tag); + lex.next(); + } + //std::cerr << "parsing constructor `" << sym::symbols.get_name(constr_name) << "` with tag " << std::hex << tag + // << std::dec << std::endl; + auto cs_ref = new Constructor(where, constr_name, 0, tag); + Constructor& cs = *cs_ref; + cs.is_special = is_special; + parse_field_list(lex, cs); + lex.expect('='); + if (lex.tp() != src::_Ident || !sym::is_uc_ident(lex.cur().val)) { + throw src::ParseError{lex.cur().loc, "type name uppercase identifier expected"}; + } + Lexem type_lex = lex.cur(); + sym_idx_t type_name = lex.cur().val; + sym::SymDef* sym_def = sym::lookup_symbol(type_name, 2); + if (!sym_def) { + sym_def = register_new_type(lex.cur().loc, type_name); + if (verbosity > 2) { + std::cerr << "defined new type `" << sym::symbols.get_name(type_name) << "`" << std::endl; + } + assert(sym_def); + } + if (!sym_def || !sym_def->value || sym_def->value->type != sym::SymValBase::_Typename) { + throw src::ParseError{lex.cur().loc, "parametrized type identifier expected"}; + } + SymValType* sym_val = dynamic_cast(sym_def->value); + assert(sym_val); + cs.type_name = type_name; + cs.type_arity = 0; + Type* type = cs.type_defined = sym_val->type_ref; + if (type->is_final) { + throw src::ParseError{lex.cur().loc, + std::string{"cannot add new constructor to a finalized type `"} + lex.cur().str + "`"}; + } + lex.next(); + while (lex.tp() != ';') { + bool negate = (lex.tp() == '~'); + if (negate) { + lex.next(); + } + TypeExpr* type_param = parse_term(lex, cs, negate ? 3 : 7); + type_param->close(lex.cur().loc); + int const_val = (!negate && type_param->tp == TypeExpr::te_IntConst) ? type_param->value : -1; + if (!negate) { + //std::cerr << "binding value to type parameter expression "; + //type_param->show(std::cerr, &cs); + //std::cerr << std::endl; + type_param->bind_value(negate, cs); + } else if (!type_param->is_nat) { + throw src::ParseError{type_param->where, "cannot return type expressions"}; + } + cs.params.push_back(type_param); + cs.param_negated.push_back(negate); + cs.param_const_val.push_back(const_val); + ++cs.type_arity; + } + if (lex.tp() != ';') { + lex.expect(';'); + } + type->bind_constructor(lex.cur().loc, cs_ref); + type->renew_last_declared(); + lex.expect(';'); + sym::close_scope(lex); + for (int i = orig_types_num; i < types_num; i++) { + if (types.at(i).is_auto && types[i].parent_type_idx == -1) { + types[i].parent_type_idx = type->type_idx; + } + } +} + +/* + * + * SOURCE PARSER (TOP LEVEL) + * + */ + +bool parse_source(std::istream* is, const src::FileDescr* fdescr) { + src::SourceReader reader{is, fdescr}; + src::Lexer lex{reader, true, "(){}:;? #$. ^~ #", "//", "/*", "*/"}; + while (lex.tp() != src::_Eof) { + parse_constructor_def(lex); + // std::cerr << lex.cur().str << '\t' << lex.cur().name_str() << std::endl; + } + return true; +} + +bool parse_source_file(const char* filename) { + if (!filename || !*filename) { + throw src::Fatal{"source file name is an empty string"}; + } + src::FileDescr* cur_source = new src::FileDescr{filename}; + std::ifstream ifs{filename}; + if (ifs.fail()) { + throw src::Fatal{std::string{"cannot open source file `"} + filename + "`"}; + } + return parse_source(&ifs, cur_source); +} + +bool parse_source_stdin() { + return parse_source(&std::cin, new src::FileDescr{"stdin", true}); +} + +/* + * + * BUILT-IN TYPE DEFINITIONS + * + */ + +Type* define_builtin_type(std::string name_str, std::string args, bool produces_nat, int size = -1, int min_size = -1, + bool any_bits = false, int is_int = 0) { + sym_idx_t name = sym::symbols.lookup_add(name_str); + assert(name_str.size() && name); + int arity = (int)args.size(); + types.emplace_back(types_num++, name, produces_nat, arity, true, true); + auto type = &types.back(); + type->args.resize(arity, 0); + int f = (name_str != "#" ? Type::_IsPos : 0); + for (int i = 0; i < arity; i++) { + type->args[i] = f | (args[i] == '#' ? Type::_IsNat : Type::_IsType); + } + if (is_int) { + type->is_integer = (char)is_int; + } + auto sym_def = sym::define_global_symbol(name, true); + assert(sym_def); + sym_def->value = new SymValType{type}; + if (size < 0) { + type->size = MinMaxSize::Any; + } else if (min_size >= 0 && min_size != size) { + type->size = MinMaxSize::size_range(min_size, size); + } else { + type->size = MinMaxSize::fixed_size(size); + type->has_fixed_size = true; + } + type->any_bits = any_bits; + return type; +} + +Type* lookup_type(std::string name_str) { + sym_idx_t name = sym::symbols.lookup(name_str); + if (name) { + auto sym_def = sym::lookup_symbol(name); + if (sym_def) { + auto sym_val = dynamic_cast(sym_def->value); + if (sym_val) { + return sym_val->type_ref; + } + } + } + return nullptr; +} + +void define_builtins() { + types.reserve(10000); + Nat_type = define_builtin_type("#", "", true, 32, 32, true); + NatWidth_type = define_builtin_type("##", "#", true, 32, 0, true); + NatLess_type = define_builtin_type("#<", "#", true, 32, 0); + NatLeq_type = define_builtin_type("#<=", "#", true, 32, 0); + Any_type = define_builtin_type("Any", "", false); + Cell_type = define_builtin_type("Cell", "", false); + Int_type = define_builtin_type("int", "#", false, 257, 0, true, -1); + UInt_type = define_builtin_type("uint", "#", false, 256, 0, true, 1); + Bits_type = define_builtin_type("bits", "#", false, 1023, 0, true, 0); + for (int i = 1; i <= 257; i++) { + char buff[8]; + sprintf(buff, "uint%d", i); + define_builtin_type(buff + 1, "", false, i, i, true, -1); + if (i < 257) { + define_builtin_type(buff, "", false, i, i, true, 1); + } + } + for (int i = 1; i <= 1023; i++) { + char buff[12]; + sprintf(buff, "bits%d", i); + define_builtin_type(buff, "", false, i, i, true, 0); + } + Eq_type = define_builtin_type("=", "##", false, 0, 0, true); + Less_type = define_builtin_type("<", "##", false, 0, 0, true); + Leq_type = define_builtin_type("<=", "##", false, 0, 0, true); + Nat_name = sym::symbols.lookup("#"); + Eq_name = sym::symbols.lookup("="); + Less_name = sym::symbols.lookup("<"); + Leq_name = sym::symbols.lookup("<="); + builtin_types_num = types_num; +} + +/* + * + * SCHEME PROCESSING AND CHECKING + * + */ + +bool Type::cons_all_exact() const { + unsigned long long sum = 0; + for (const auto& cons : constructors) { + sum += (1ULL << (63 - cons->tag_bits)); + } + return sum == (1ULL << 63); +} + +int Type::cons_common_len() const { + if (!constr_num) { + return -1; + } + int len = constructors.at(0)->tag_bits; + for (const auto cons : constructors) { + if (cons->tag_bits != len) { + return -1; + } + } + return len; +} + +bool Constructor::compute_admissible_params() { + int dim = 0; + int abs_param[4]; + for (std::size_t i = 0; i < params.size(); i++) { + if (!param_negated[i] && params[i]->is_nat) { + int t = params[i]->abstract_interpret_nat(); + assert(t >= 0 && t <= 15); + // std::cerr << "abstract_interpret( " << params[i] << " ) = " << t << std::endl; + abs_param[dim++] = t; + if (!t) { + admissible_params.clear_all(); + return false; + } + if (dim == 4) { + break; + } + } + } + while (dim > 0 && abs_param[dim - 1] == 15) { + --dim; + } + if (!dim) { + admissible_params.set_all(); + return true; + } + admissible_params.set_by_pattern(dim, abs_param); + return true; +} + +bool Type::compute_admissible_params() { + bool admissible = false; + for (Constructor* cs : constructors) { + admissible |= cs->compute_admissible_params(); + admissible_params |= cs->admissible_params; + } + return admissible; +} + +void compute_admissible_params() { + for (int i = builtin_types_num; i < types_num; i++) { + (void)types[i].compute_admissible_params(); + } +} + +bool Constructor::recompute_begins_with() { + for (const Field& field : fields) { + if (!field.implicit && !field.constraint) { + TypeExpr* expr = field.type; + if (expr->tp == TypeExpr::te_Ref) { + continue; + } + if (expr->tp != TypeExpr::te_Apply) { + break; + } + BitPfxCollection add = expr->type_applied->begins_with * tag; + return (begins_with += add); + } + } + BitPfxCollection add{tag}; + if (begins_with == add) { + return false; + } + begins_with += add; + return true; +} + +bool Type::recompute_begins_with() { + bool changes = false; + for (Constructor* cs : constructors) { + if (cs->recompute_begins_with()) { + changes |= (begins_with += cs->begins_with); + } + } + return changes; +} + +void compute_begins_with() { + bool changes = true; + while (changes) { + changes = false; + for (int i = builtin_types_num; i < types_num; i++) { + changes |= types[i].recompute_begins_with(); + } + } +} + +bool Constructor::recompute_minmax_size() { + MinMaxSize sz = MinMaxSize::fixed_size(tag_bits); + for (const Field& field : fields) { + if (!field.implicit && !field.constraint) { + sz += field.type->compute_size(); + } + } + if (sz == size) { + return false; + } + size = sz; + has_fixed_size = sz.is_fixed(); + return true; +} + +bool Type::recompute_minmax_size() { + MinMaxSize sz; + bool changes = false; + for (Constructor* cs : constructors) { + changes |= cs->recompute_minmax_size(); + sz |= cs->size; + } + if (sz == size) { + return changes; + } + size = sz; + has_fixed_size = sz.is_fixed(); + return true; +} + +void compute_minmax_sizes() { + bool changes = true; + while (changes) { + changes = false; + for (int i = builtin_types_num; i < types_num; i++) { + changes |= types[i].recompute_minmax_size(); + } + } +} + +bool Constructor::recompute_any_bits() { + bool res = true; + for (const Field& field : fields) { + if (!field.implicit && !field.constraint) { + res &= field.type->compute_any_bits(); + } + } + if (res == any_bits) { + return false; + } + any_bits = res; + return true; +} + +bool Type::recompute_any_bits() { + bool res = begins_with.is_all(); + bool changes = false; + for (Constructor* cs : constructors) { + changes |= cs->recompute_any_bits(); + res &= cs->any_bits; + } + if (res == any_bits) { + return changes; + } + any_bits = res; + return true; +} + +void compute_any_bits() { + bool changes = true; + while (changes) { + changes = false; + for (int i = builtin_types_num; i < types_num; i++) { + changes |= types[i].recompute_any_bits(); + } + } +} + +void Type::detect_basic_types() { + if (!arity && constr_num > 0 && size.is_fixed() && any_bits) { + is_unit = !size.min_size(); + is_bool = (size.min_size() == 0x100); + } +} + +void detect_basic_types() { + for (int i = builtin_types_num; i < types_num; i++) { + types[i].detect_basic_types(); + } +} + +int show_size_warnings() { + int errors = 0; + for (int i = builtin_types_num; i < types_num; i++) { + Type& type = types[i]; + if (!type.size.fits_into_cell() || !type.size.is_possible()) { + std::cerr << "error: type `" << type.get_name() << "`" + << (!type.size.is_possible() ? " cannot be instantiated" : " never fits into a cell") << " (size " + << type.size << ")\n"; + ++errors; + } + for (Constructor* cs : type.constructors) { + if (!cs->size.fits_into_cell() || !cs->size.is_possible()) { + std::cerr << "error: constructor `" << cs->get_qualified_name() << "`" + << (!cs->size.is_possible() ? " cannot be instantiated" : " never fits into a cell") << " (size " + << cs->size << ")\n"; + cs->show(std::cerr); + std::cerr << std::endl; + cs->where.show_note("defined here"); + ++errors; + } + } + } + return errors; +} + +bool Type::is_const_arg(int p) const { + return (args.at(p) & (_IsType | _IsNat | _IsPos | _IsNeg | _NonConst)) == (_IsNat | _IsPos); +} + +int Type::detect_const_params() { + for (int i = 0; i < arity; i++) { + if (is_const_arg(i)) { + return const_param_idx = i; + } + } + return const_param_idx = -1; +} + +std::vector Type::get_all_param_values(int p) const { + if (p < 0 || p >= arity) { + return {}; + } + std::vector res; + res.reserve(constr_num); + for (const Constructor* cs : constructors) { + res.push_back(cs->param_const_val.at(p)); + } + std::sort(res.begin(), res.end()); + res.erase(std::unique(res.begin(), res.end()), res.end()); + return res; +} + +std::vector Type::get_constr_by_param_value(int p, int pv) const { + std::vector res; + if (p < 0 || p >= arity) { + return res; + } + for (int i = 0; i < constr_num; i++) { + if (constructors[i]->param_const_val[p] == pv) { + res.push_back(i); + } + } + return res; +} + +void Type::compute_constructor_trie() { + if (cs_trie || !constr_num) { + return; + } + unsigned long long z = 1; + for (Constructor* cs : constructors) { + if (!z) { + throw src::ParseError{cs->where, + std::string{"cannot work with more than 64 constructors for type `"} + get_name() + "`"}; + } + cs_trie = BinTrie::insert_paths(std::move(cs_trie), cs->begins_with, z); + z <<= 1; + } + if (cs_trie) { + useful_depth = cs_trie->compute_useful_depth(); + is_pfx_determ = !cs_trie->find_conflict_path(); + } else { + useful_depth = 0; + is_pfx_determ = true; + } +} + +bool Type::check_conflicts() { + compute_constructor_trie(); + int cp = detect_const_params(); + is_param_pfx_determ = is_param_determ = is_determ = true; + is_const_param_determ = is_const_param_pfx_determ = (cp >= 0); + if (!constr_num || !cs_trie) { + return false; + } + assert(constr_num <= 64); + // std::cerr << "prefix trie for constructors of `" << get_name() << "`" << std::endl; + // cs_trie->show(std::cerr); + ConflictGraph pfx_cg; + cs_trie->set_conflict_graph(pfx_cg); + for (int i = 0; i < constr_num; i++) { + AdmissibilityInfo& ap1 = constructors[i]->admissible_params; + for (int j = 0; j < i; j++) { + bool cp_same = (constructors[i]->get_const_param(cp) == constructors[j]->get_const_param(cp)); + if (cp_same) { + is_const_param_determ = false; + if (pfx_cg[i][j]) { + is_const_param_pfx_determ = false; + } + } + if (ap1.conflicts_with(constructors[j]->admissible_params)) { + is_param_determ = false; + if (pfx_cg[i][j]) { + is_param_pfx_determ = false; + if (cp_same) { + conflict1 = j; + conflict2 = i; + is_determ = false; + } + } + } + } + } + return !is_determ; +} + +void Type::show_constructor_conflict() { + assert(cs_trie); + assert(conflict1 != conflict2); + int i = conflict1, j = conflict2; + assert(i >= 0 && i <= j && j < 64 && j < constr_num); + unsigned long long mask = (1ULL << i) | (1ULL << j); + unsigned long long pfx = cs_trie->find_conflict_path(0, mask); + assert(pfx); + ConflictSet cs_set{cs_trie->lookup_tag(pfx)}; + std::cerr << "found conflict between constructors of type `" << get_name() << "`: prefix "; + show_tag(std::cerr, pfx); + AdmissibilityInfo& info1 = constructors[i]->admissible_params; + AdmissibilityInfo& info2 = constructors[j]->admissible_params; + bool need_params = !(info1.is_set_all() && info2.is_set_all()); + int params = info1.conflicts_at(info2); + assert(params >= 0); + for (int s = 0; s < 64 && s < constr_num; s++) { + if (cs_set[s] && + !(need_params ? constructors[s]->admissible_params[params] : constructors[s]->admissible_params.is_set_all())) { + cs_set.remove(s); + } + } + assert(cs_set[i] && cs_set[j]); + std::cerr << " can be present in " << cs_set.size() << " constructors:" << std::endl; + for (int s = 0; s < 64 && s < constr_num; s++) { + if (cs_set[s]) { + std::cerr << "\t"; + constructors[s]->show(std::cerr); + std::cerr << std::endl; + constructors[s]->where.show_note("defined here"); + } + } + if (need_params) { + std::cerr << "when type parameters are instantiated as " << get_name(); + char nat = 'a', t = 'A'; + for (int x : args) { + if (x & _IsNeg) { + std::cerr << " ~" << (x & _IsNat ? nat++ : t++); + } else if (x & _IsType) { + std::cerr << ' ' << t++; + } else { + std::cerr << ' ' << (params & 3); + if (params & 2) { + std::cerr << "+2*" << nat++; + } + } + } + std::cerr << std::endl; + } +} + +int check_conflicts() { + int c = 0; + for (int i = builtin_types_num; i < types_num; i++) { + if (types[i].check_conflicts()) { + ++c; + types[i].show_constructor_conflict(); + } + } + return c; +} + +void check_scheme() { + compute_admissible_params(); + compute_begins_with(); + compute_minmax_sizes(); + compute_any_bits(); + detect_basic_types(); + if (show_size_warnings()) { + throw src::Fatal{"invalid scheme: some constructors or types cannot be instantiated or do not fit into cells"}; + } + if (check_conflicts()) { + throw src::Fatal{"invalid scheme: have conflicts between constructors of some types"}; + } +} + +void dump_all_types() { + std::cerr << types_num << " types defined, out of them " << builtin_types_num << " built-in, " + << types_num - builtin_types_num << " user-defined\n"; + for (int i = 0; i < builtin_types_num; i++) { + Type& type = types[i]; + if (type.used) { + std::cerr << "built-in type #" << i << ": `" << type.get_name() << "`, arity " << type.arity << "; prefixes " + << type.begins_with << "; size " << type.size; + if (type.is_unit) { + std::cerr << " (UNIT)"; + } + if (type.is_bool) { + std::cerr << " (BOOL)"; + } + std::cerr << std::endl; + } + } + for (int i = builtin_types_num; i < types_num; i++) { + Type& type = types[i]; + std::cerr << "type #" << i << ": `" << type.get_name() << "`, arity " << type.arity << ", " << type.constr_num + << " constructors\n"; + if (type.const_param_idx >= 0) { + std::cerr << " constant parameters:"; + for (int j = 0; j < type.arity; j++) { + std::cerr << (type.is_const_arg(j) ? " const" : " *"); + } + std::cerr << std::endl; + } + for (Constructor* cs : type.constructors) { + std::cerr << " constructor `" << cs->get_name() << "`" << (cs->is_fwd ? " (simple forwarder)\n\t" : "\n\t"); + cs->show(std::cerr); + std::cerr << "\n\tbegins with " << cs->begins_with << std::endl; + if (!cs->admissible_params.is_set_all()) { + std::cerr << "\tadmissibility " << cs->admissible_params << std::endl; + } + if (type.const_param_idx >= 0) { + std::cerr << "\tconstant parameter #" << type.const_param_idx + 1 << " = " + << cs->get_const_param(type.const_param_idx) << std::endl; + } + std::cerr << "\tsize " << cs->size << (cs->has_fixed_size ? " (fixed)" : "") + << (cs->any_bits ? " (any bits)" : "") << std::endl; + for (const Field& field : cs->fields) { + std::cerr << "\t\tfield `" << field.get_name() << "`: " << field.type << " (used=" << field.used + << ") (is_nat_subtype=" << field.type->is_nat_subtype << ")\n"; + } + } + if (type.is_unit) { + std::cerr << " (UNIT)\n"; + } + if (type.is_bool) { + std::cerr << " (BOOL)\n"; + } + if (type.is_enum) { + std::cerr << (type.is_simple_enum ? " (SIMPLE ENUM)" : " (ENUM)") << std::endl; + } + if (type.constr_num > 1) { + std::cerr << " constructor detection: "; + if (type.is_pfx_determ) { + std::cerr << "PFX(" << type.useful_depth << ") "; + } + if (type.is_param_determ) { + std::cerr << "PARAM "; + } + if (type.is_const_param_determ) { + std::cerr << "CONST_PARAM "; + } + if (type.is_const_param_pfx_determ && !type.is_pfx_determ && !type.is_const_param_determ) { + std::cerr << "PFX(" << type.useful_depth << ")+CONST_PARAM "; + } + if (type.is_param_pfx_determ && !type.is_pfx_determ && !type.is_param_determ && !type.is_const_param_pfx_determ) { + std::cerr << "PFX(" << type.useful_depth << ")+PARAM "; + } + if (type.is_determ && !type.is_const_param_pfx_determ && !type.is_param_pfx_determ) { + std::cerr << "PFX(" << type.useful_depth << ")+CONST_PARAM+PARAM "; + } + if (!type.is_determ) { + std::cerr << ""; + } + std::cerr << std::endl; + } + std::cerr << " type size " << type.size << (type.has_fixed_size ? " (fixed)" : "") + << (type.any_bits ? " (any bits)" : "") << std::endl; + std::cerr << " type begins with " << type.begins_with << std::endl; + if (!type.admissible_params.is_set_all()) { + std::cerr << " type admissibility " << type.admissible_params << std::endl; + } + std::cerr << std::endl; + if (!type.constr_num && !type.is_final) { + sym::SymDef* sym_def = sym::lookup_symbol(type.type_name); + assert(sym_def); + throw src::ParseError{ + sym_def ? sym_def->loc : src::SrcLocation{}, + std::string{"implicitly defined type `"} + sym::symbols.get_name(type.type_name) + "` has no constructors"}; + } + } +} + +void dump_all_constexpr() { + std::cerr << "****************\n" << const_type_expr_num << " constant expressions:\n"; + for (int i = 1; i <= const_type_expr_num; i++) { + std::cerr << "expr #" << i << ": " << const_type_expr[i] << std::endl; + } +} + +/* + * + * CODE GENERATION + * + */ + +std::vector source_list; + +void register_source(std::string source) { + source_list.push_back(source); +} + +} // namespace tlbc + +#include "tlbc-gen-cpp.cpp" + +/* + * + * TLBC MAIN + * + */ + +void usage(const char* progname) { + std::cerr << "usage: " << progname + << " [-v][-i][-h][-c][-z][-t][-T][-q][-n][-o] { ...}\n" + << "-v\tIncrease verbosity level\n" + << "-t\tShow tag mismatch warnings\n" + << "-q\tOmit code generation (TLB scheme check only)\n" + << "-h\tGenerate C++ header file only (usually .h or .hpp)\n" + << "-c\tGenerate C++ source file only (usually .cpp)\n" + << "-T\tAdd type pointer members into generated C++ data record classes\n" + << "-z\tAppend .cpp or .hpp to output filename\n" + << "-n\tPut generated code into specified namespace (default `tlb`)\n"; + std::exit(2); +} + +std::string output_filename; + +int main(int argc, char* const argv[]) { + int i; + bool interactive = false; + bool no_code_gen = false; + while ((i = getopt(argc, argv, "chin:o:qTtvz")) != -1) { + switch (i) { + case 'i': + interactive = true; + break; + case 'v': + ++verbosity; + break; + case 'o': + output_filename = optarg; + break; + case 'c': + tlbc::gen_cpp = true; + break; + case 'h': + tlbc::gen_hpp = true; + break; + case 'q': + no_code_gen = true; + break; + case 'n': + tlbc::cpp_namespace = optarg; + break; + case 'T': + tlbc::add_type_members = true; + break; + case 't': + tlbc::show_tag_warnings = true; + break; + case 'z': + tlbc::append_suffix = true; + break; + default: + usage(argv[0]); + } + } + if (verbosity >= 3) { + tlbc::show_tag_warnings = true; + } + + src::define_keywords(); + tlbc::init_abstract_tables(); + tlbc::define_builtins(); + + int ok = 0, proc = 0; + try { + while (optind < argc) { + tlbc::register_source(argv[optind]); + ok += tlbc::parse_source_file(argv[optind++]); + proc++; + } + if (interactive) { + tlbc::register_source(""); + ok += tlbc::parse_source_stdin(); + proc++; + } + if (ok < proc) { + throw src::Fatal{"output code generation omitted because of errors"}; + } + if (!proc) { + throw src::Fatal{"no source files, no output"}; + } + tlbc::check_scheme(); + if (verbosity > 0) { + tlbc::dump_all_types(); + tlbc::dump_all_constexpr(); + } + if (!no_code_gen) { + tlbc::init_forbidden_cpp_idents(); + tlbc::generate_cpp_output(output_filename); + } + } catch (src::Fatal& fatal) { + std::cerr << "fatal: " << fatal << std::endl; + std::exit(1); + } catch (src::Error& error) { + std::cerr << error << std::endl; + std::exit(1); + } +} diff --git a/crypto/tl/tlblib.cpp b/crypto/tl/tlblib.cpp new file mode 100644 index 00000000..40637c57 --- /dev/null +++ b/crypto/tl/tlblib.cpp @@ -0,0 +1,342 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include + +namespace tlb { + +const False t_False; +const True t_True; +const Unit t_Unit; + +const Bool t_Bool; + +const Int t_int8{8}, t_int16{16}, t_int24{24}, t_int32{32}, t_int64{64}, t_int128{128}, t_int256{256}, t_int257{257}; +const UInt t_uint8{8}, t_uint16{16}, t_uint24{24}, t_uint32{32}, t_uint64{64}, t_uint128{128}, t_uint256{256}; +const NatWidth t_Nat{32}; + +const Anything t_Anything; +const RefAnything t_RefCell; + +bool Bool::print_skip(PrettyPrinter& pp, vm::CellSlice& cs) const { + int t = get_tag(cs); + return cs.advance(1) && pp.out(t ? "bool_true" : "bool_false"); +} + +bool NatWidth::print_skip(PrettyPrinter& pp, vm::CellSlice& cs) const { + long long value = (long long)cs.fetch_ulong(32); + return value >= 0 && pp.out_int(value); +} + +bool NatLeq::print_skip(PrettyPrinter& pp, vm::CellSlice& cs) const { + long long value = (long long)as_uint(cs); + return value >= 0 && skip(cs) && pp.out_int(value); +} + +bool NatLess::print_skip(PrettyPrinter& pp, vm::CellSlice& cs) const { + long long value = (long long)as_uint(cs); + return value >= 0 && skip(cs) && pp.out_int(value); +} + +bool TupleT::print_skip(PrettyPrinter& pp, vm::CellSlice& cs) const { + pp.open("tuple "); + pp.os << n << " ["; + pp.mode_nl(); + int i = n; + for (; i > 0; --i) { + if (!X.print_skip(pp, cs)) { + return false; + } + pp.mode_nl(); + } + return pp.close("]"); +} + +bool CondT::print_skip(PrettyPrinter& pp, vm::CellSlice& cs) const { + return (n > 0 ? X.print_skip(pp, cs) : (!n && pp.out("()"))); +} + +bool Int::print_skip(PrettyPrinter& pp, vm::CellSlice& cs) const { + if (n <= 64) { + long long value; + return cs.fetch_int_to(n, value) && pp.out_int(value); + } else { + return pp.out_integer(cs.fetch_int256(n, true)); + } +} + +bool UInt::print_skip(PrettyPrinter& pp, vm::CellSlice& cs) const { + if (n <= 64) { + unsigned long long value; + return cs.fetch_uint_to(n, value) && pp.out_uint(value); + } else { + return pp.out_integer(cs.fetch_int256(n, false)); + } +} + +bool Bits::print_skip(PrettyPrinter& pp, vm::CellSlice& cs) const { + if (cs.have(n)) { + pp.os << 'x' << cs.fetch_bits(n).to_hex(); + return true; + } else { + return false; + } +} + +bool TupleT::skip(vm::CellSlice& cs) const { + int i = n; + for (; i > 0; --i) { + if (!X.skip(cs)) { + break; + } + } + return !i; +} + +bool TupleT::validate_skip(vm::CellSlice& cs, bool weak) const { + int i = n; + for (; i > 0; --i) { + if (!X.validate_skip(cs, weak)) { + break; + } + } + return !i; +} + +bool TLB::validate_ref_internal(Ref cell_ref, bool weak) const { + bool is_special; + auto cs = load_cell_slice_special(std::move(cell_ref), is_special); + return always_special() ? is_special : (is_special ? weak : (validate_skip(cs) && cs.empty_ext())); +} + +bool TLB::print_skip(PrettyPrinter& pp, vm::CellSlice& cs) const { + pp.open("raw@"); + pp << *this << ' '; + vm::CellSlice cs_copy{cs}; + if (!validate_skip(cs) || !cs_copy.cut_tail(cs)) { + return pp.fail("invalid value"); + } + pp.raw_nl(); + cs_copy.print_rec(pp.os, pp.indent); + return pp.mkindent() && pp.close(); +} + +bool TLB::print_special(PrettyPrinter& pp, vm::CellSlice& cs) const { + pp.open("raw@"); + pp << *this << ' '; + pp.raw_nl(); + cs.print_rec(pp.os, pp.indent); + return pp.mkindent() && pp.close(); +} + +bool TLB::print_ref(PrettyPrinter& pp, Ref cell_ref) const { + if (cell_ref.is_null()) { + return pp.fail("null cell reference"); + } + bool is_special; + auto cs = load_cell_slice_special(std::move(cell_ref), is_special); + if (is_special) { + return print_special(pp, cs); + } else { + return print_skip(pp, cs) && (cs.empty_ext() || pp.fail("extra data in cell")); + } +} + +bool TLB::print_skip(std::ostream& os, vm::CellSlice& cs, int indent) const { + PrettyPrinter pp{os, indent}; + return pp.fail_unless(print_skip(pp, cs)); +} + +bool TLB::print(std::ostream& os, const vm::CellSlice& cs, int indent) const { + PrettyPrinter pp{os, indent}; + return pp.fail_unless(print(pp, cs)); +} + +bool TLB::print_ref(std::ostream& os, Ref cell_ref, int indent) const { + PrettyPrinter pp{os, indent}; + return pp.fail_unless(print_ref(pp, std::move(cell_ref))); +} + +std::string TLB::as_string_skip(vm::CellSlice& cs, int indent) const { + std::ostringstream os; + print_skip(os, cs, indent); + return os.str(); +} + +std::string TLB::as_string(const vm::CellSlice& cs, int indent) const { + std::ostringstream os; + print(os, cs, indent); + return os.str(); +} + +std::string TLB::as_string_ref(Ref cell_ref, int indent) const { + std::ostringstream os; + print_ref(os, std::move(cell_ref), indent); + return os.str(); +} + +PrettyPrinter::~PrettyPrinter() { + if (failed || level) { + if (nl_used) { + nl(-2 * level); + } + os << "PRINTING FAILED"; + while (level > 0) { + os << ')'; + --level; + } + } + if (nl_used) { + os << std::endl; + } +} + +bool PrettyPrinter::fail(std::string msg) { + os << ""; + failed = true; + return false; +} + +bool PrettyPrinter::mkindent(int delta) { + indent += delta; + for (int i = 0; i < indent; i++) { + os << ' '; + } + nl_used = true; + return true; +} + +bool PrettyPrinter::nl(int delta) { + os << std::endl; + return mkindent(delta); +} +bool PrettyPrinter::raw_nl(int delta) { + os << std::endl; + indent += delta; + nl_used = true; + return true; +} + +bool PrettyPrinter::open(std::string msg) { + os << "(" << msg; + indent += 2; + level++; + return true; +} + +bool PrettyPrinter::close() { + return close(""); +} + +bool PrettyPrinter::close(std::string msg) { + if (level <= 0) { + return fail("cannot close scope"); + } + indent -= 2; + --level; + os << msg << ")"; + return true; +} + +bool PrettyPrinter::mode_nl() { + if (mode & 1) { + return nl(); + } else { + os << ' '; + return true; + } +} + +bool PrettyPrinter::field(std::string name) { + mode_nl(); + os << name << ':'; + return true; +} + +bool PrettyPrinter::field() { + mode_nl(); + return true; +} + +bool PrettyPrinter::field_int(long long x, std::string name) { + os << ' ' << name << ':' << x; + return true; +} + +bool PrettyPrinter::field_int(long long x) { + os << ' ' << x; + return true; +} + +bool PrettyPrinter::field_uint(unsigned long long x, std::string name) { + os << ' ' << name << ':' << x; + return true; +} + +bool PrettyPrinter::field_uint(unsigned long long x) { + os << ' ' << x; + return true; +} + +bool PrettyPrinter::fetch_bits_field(vm::CellSlice& cs, int n) { + os << " x"; + return cs.have(n) && out(cs.fetch_bits(n).to_hex()); +} + +bool PrettyPrinter::fetch_bits_field(vm::CellSlice& cs, int n, std::string name) { + os << ' ' << name << ":x"; + return cs.have(n) && out(cs.fetch_bits(n).to_hex()); +} + +bool PrettyPrinter::fetch_int_field(vm::CellSlice& cs, int n) { + return cs.have(n) && field_int(cs.fetch_long(n)); +} + +bool PrettyPrinter::fetch_int_field(vm::CellSlice& cs, int n, std::string name) { + return cs.have(n) && field_int(cs.fetch_long(n), name); +} + +bool PrettyPrinter::fetch_uint_field(vm::CellSlice& cs, int n) { + return cs.have(n) && field_uint(cs.fetch_ulong(n)); +} + +bool PrettyPrinter::fetch_uint_field(vm::CellSlice& cs, int n, std::string name) { + return cs.have(n) && field_uint(cs.fetch_ulong(n), name); +} + +bool PrettyPrinter::fetch_int256_field(vm::CellSlice& cs, int n) { + os << ' '; + return out_integer(cs.fetch_int256(n, true)); +} + +bool PrettyPrinter::fetch_int256_field(vm::CellSlice& cs, int n, std::string name) { + os << ' ' << name << ':'; + return out_integer(cs.fetch_int256(n, true)); +} + +bool PrettyPrinter::fetch_uint256_field(vm::CellSlice& cs, int n) { + os << ' '; + return out_integer(cs.fetch_int256(n, false)); +} + +bool PrettyPrinter::fetch_uint256_field(vm::CellSlice& cs, int n, std::string name) { + os << ' ' << name << ':'; + return out_integer(cs.fetch_int256(n, false)); +} + +} // namespace tlb diff --git a/crypto/tl/tlblib.hpp b/crypto/tl/tlblib.hpp new file mode 100644 index 00000000..1e4f458c --- /dev/null +++ b/crypto/tl/tlblib.hpp @@ -0,0 +1,981 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once +#include +#include "vm/cellslice.h" + +namespace tlb { + +using td::Ref; +using vm::CellSlice; + +struct PrettyPrinter; + +class TLB { + public: + virtual ~TLB() = default; + virtual int get_size(const vm::CellSlice& cs) const { + return -1; + } + virtual bool skip(vm::CellSlice& cs) const { + return cs.skip_ext(get_size(cs)); + } + virtual bool validate(const vm::CellSlice& cs, bool weak = false) const { + return cs.have_ext(get_size(cs)); + } + virtual bool validate_exact(const vm::CellSlice& cs, bool weak = false) const { + return (int)cs.size_ext() == get_size(cs); + } + bool validate_csr(Ref cs_ref, bool weak = false) const { + return cs_ref.not_null() && validate_skip_exact(cs_ref.write(), weak); + } + Ref fetch(vm::CellSlice& cs) const { + return cs.fetch_subslice_ext(get_size(cs)); + } + Ref prefetch(const vm::CellSlice& cs) const { + return cs.prefetch_subslice_ext(get_size(cs)); + } + virtual Ref validate_fetch(vm::CellSlice& cs, bool weak = false) const { + return validate(cs, weak) ? cs.fetch_subslice_ext(get_size(cs)) : Ref{}; + } + virtual Ref validate_prefetch(const vm::CellSlice& cs, bool weak = false) const { + return validate(cs, weak) ? cs.prefetch_subslice_ext(get_size(cs)) : Ref{}; + } + bool fetch_to(vm::CellSlice& cs, Ref& res) const { + return (res = fetch(cs)).not_null(); + } + bool validate_fetch_to(vm::CellSlice& cs, Ref& res, bool weak = false) const { + return (res = validate_fetch(cs, weak)).not_null(); + } + bool store_from(vm::CellBuilder& cb, Ref field) const { + return field.not_null() && get_size(*field) == (int)field->size_ext() && cb.append_cellslice_bool(std::move(field)); + } + bool validate_store_from(vm::CellBuilder& cb, Ref field, bool weak = false) const { + if (field.is_null()) { + return false; + } + vm::CellSlice cs{*field}; + return validate_skip(cs, weak) && cs.empty_ext() && cb.append_cellslice_bool(std::move(field)); + } + virtual bool extract(vm::CellSlice& cs) const { + return cs.only_ext(get_size(cs)); + } + virtual bool validate_extract(vm::CellSlice& cs, bool weak = false) const { + return validate(cs, weak) && extract(cs); + } + int get_size_by_skip(const vm::CellSlice& cs) const { + vm::CellSlice copy{cs}; + return skip(copy) ? copy.subtract_base_ext(cs) : -1; + } + virtual bool validate_skip(vm::CellSlice& cs, bool weak = false) const { + return validate(cs, weak) && skip(cs); + } + bool validate_skip_exact(vm::CellSlice& cs, bool weak = false) const { + return validate_skip(cs, weak) && cs.empty_ext(); + } + bool validate_by_skip(const vm::CellSlice& cs, bool weak = false) const { + vm::CellSlice copy{cs}; + return validate_skip(copy, weak); + } + bool validate_by_skip_exact(const vm::CellSlice& cs, bool weak = false) const { + vm::CellSlice copy{cs}; + return validate_skip_exact(copy, weak); + } + bool extract_by_skip(vm::CellSlice& cs) const { + vm::CellSlice copy{cs}; + return skip(copy) && cs.cut_tail(copy); + } + bool validate_extract_by_skip(vm::CellSlice& cs, bool weak = false) const { + vm::CellSlice copy{cs}; + return validate_skip(copy, weak) && cs.cut_tail(copy); + } + Ref validate_fetch_by_skip(vm::CellSlice& cs, bool weak = false) const { + Ref copy{true, cs}; + return validate_skip(cs, weak) && copy.unique_write().cut_tail(cs) ? copy : Ref{}; + } + Ref validate_prefetch_by_skip(const vm::CellSlice& cs, bool weak = false) const { + vm::CellSlice copy{cs}; + return validate_skip(copy, false) ? cs.prefetch_subslice_ext(copy.subtract_base_ext(cs)) : Ref{}; + } + virtual bool skip_copy(vm::CellBuilder& cb, vm::CellSlice& cs) const { + return cb.append_cellslice_bool(fetch(cs)); + } + virtual bool copy(vm::CellBuilder& cb, const vm::CellSlice& cs) const { + return cb.append_cellslice_bool(prefetch(cs)); + } + virtual bool always_special() const { + return false; + } + virtual int get_tag(const vm::CellSlice& cs) const { + return -1; + } + virtual int check_tag(const vm::CellSlice& cs) const { + return get_tag(cs); + } + bool has_valid_tag(const vm::CellSlice& cs) const { + return check_tag(cs) >= 0; + } + virtual long long as_int(const vm::CellSlice& cs) const { + return -1; + } + virtual unsigned long long as_uint(const vm::CellSlice& cs) const { + return static_cast(-1); + } + virtual td::RefInt256 as_integer(const vm::CellSlice& cs) const { + return {}; + } + virtual td::RefInt256 as_integer_skip(vm::CellSlice& cs) const { + return {}; + } + virtual td::RefInt256 as_integer(Ref cs) const { + return as_integer(*cs); + } + bool as_integer_skip_to(vm::CellSlice& cs, td::RefInt256& res) const { + return (res = as_integer_skip(cs)).not_null(); + } + bool as_integer_to(const vm::CellSlice& cs, td::RefInt256& res) const { + return (res = as_integer(cs)).not_null(); + } + bool as_integer_to(Ref cs_ref, td::RefInt256& res) const { + return (res = as_integer(std::move(cs_ref))).not_null(); + } + bool validate_ref(Ref cell_ref, bool weak = false) const { + return cell_ref.not_null() && validate_ref_internal(std::move(cell_ref), weak); + } + bool force_validate_ref(Ref cell_ref) const { + return cell_ref.not_null() && validate_ref_internal(std::move(cell_ref), false); + } + bool validate_skip_ref(vm::CellSlice& cs, bool weak = false) const { + return validate_ref(cs.fetch_ref(), weak); + } + virtual bool null_value(vm::CellBuilder& cb) const { + return false; + } + virtual bool store_integer_value(vm::CellBuilder& cb, const td::BigInt256& value) const { + return false; + } + virtual bool store_long(vm::CellBuilder& cb, long long value) const { + return store_integer_value(cb, td::BigInt256{value}); + } + virtual bool store_integer_ref(vm::CellBuilder& cb, td::RefInt256 value) const { + return value.not_null() && store_integer_value(cb, *value); + } + virtual bool add_values(vm::CellBuilder& cb, vm::CellSlice& cs1, vm::CellSlice& cs2) const { + td::RefInt256 x = as_integer_skip(cs1), y = as_integer_skip(cs2); + return x.not_null() && y.not_null() && store_integer_ref(cb, x += std::move(y)); + } + // result: -1 = error, 0 = ok (zero), 1 = ok + virtual int sub_values(vm::CellBuilder& cb, vm::CellSlice& cs1, vm::CellSlice& cs2) const { + td::RefInt256 x = as_integer_skip(cs1), y = as_integer_skip(cs2); + return x.not_null() && y.not_null() && store_integer_ref(cb, x -= std::move(y)) ? (td::sgn(x) ? 1 : 0) : -1; + } + template + bool unpack(Ref cs_ref, Args&... args) const { + return cs_ref.not_null() && unpack(cs_ref.write(), args...) && cs_ref->empty_ext(); + } + virtual std::ostream& print_type(std::ostream& os) const { + return os << ""; + } + virtual bool print_skip(PrettyPrinter& pp, vm::CellSlice& cs) const; + virtual bool print(PrettyPrinter& pp, const vm::CellSlice& cs) const { + vm::CellSlice cs_copy{cs}; + return print_skip(pp, cs_copy); + } + bool print_special(PrettyPrinter& pp, vm::CellSlice& cs) const; + bool print_ref(PrettyPrinter& pp, Ref cell_ref) const; + bool print(PrettyPrinter& pp, Ref cs_ref) const { + return print(pp, *cs_ref); + } + bool print_skip(std::ostream& os, vm::CellSlice& cs, int indent = 0) const; + bool print(std::ostream& os, const vm::CellSlice& cs, int indent = 0) const; + bool print(std::ostream& os, Ref cs_ref, int indent = 0) const { + return print(os, *cs_ref, indent); + } + bool print_ref(std::ostream& os, Ref cell_ref, int indent = 0) const; + std::string as_string_skip(vm::CellSlice& cs, int indent = 0) const; + std::string as_string(const vm::CellSlice& cs, int indent = 0) const; + std::string as_string(Ref cs_ref, int indent = 0) const { + return cs_ref.not_null() ? as_string(*cs_ref, indent) : ""; + } + std::string as_string_ref(Ref cell_ref, int indent = 0) const; + + protected: + bool validate_ref_internal(Ref cell_ref, bool weak = false) const; +}; + +static inline std::ostream& operator<<(std::ostream& os, const TLB& type) { + return type.print_type(os); +} + +struct TLB_Complex : TLB { + bool skip(vm::CellSlice& cs) const override { + return validate_skip(cs); + } + bool validate_skip(vm::CellSlice& cs, bool weak = false) const override = 0; + int get_size(const vm::CellSlice& cs) const override { + return get_size_by_skip(cs); + } + bool validate(const vm::CellSlice& cs, bool weak = false) const override { + return validate_by_skip(cs, weak); + } + bool validate_exact(const vm::CellSlice& cs, bool weak = false) const override { + return validate_by_skip_exact(cs, weak); + } + bool extract(vm::CellSlice& cs) const override { + return extract_by_skip(cs); + } + bool validate_extract(vm::CellSlice& cs, bool weak = false) const override { + return validate_extract_by_skip(cs, weak); + } + Ref validate_fetch(vm::CellSlice& cs, bool weak = false) const override { + return validate_fetch_by_skip(cs, weak); + } + Ref validate_prefetch(const vm::CellSlice& cs, bool weak = false) const override { + return validate_prefetch_by_skip(cs, weak); + } + td::RefInt256 as_integer(const vm::CellSlice& cs) const override { + vm::CellSlice copy{cs}; + auto res = as_integer_skip(copy); + return res.not_null() && copy.empty_ext() ? std::move(res) : td::RefInt256{}; + } + td::RefInt256 as_integer(Ref cs) const override { + auto res = as_integer_skip(cs.write()); + return res.not_null() && cs->empty_ext() ? std::move(res) : td::RefInt256{}; + } +}; + +static inline bool add_chk(int x, int y, int z) { + return x + y == z && z >= 0; +} + +static inline bool add_r1(int& x, int y, int z) { + return z >= y && (x = z - y) >= 0; +} + +static inline bool add_r3(int& x, int y, int& z) { + return (z = (x + y)) >= 0; +} + +static inline bool mul_chk(int x, int y, int z) { + return (long long)x * y == z; +} + +static inline bool mul_r1(int& x, int y, int z) { + return y && !(z % y) && (x = z / y) >= 0; +} + +static inline bool mul_r3(int x, int y, int& z) { + unsigned long long t = (unsigned long long)x * y; + if (t <= 0x7fffffff) { + z = (int)t; + return true; + } else { + return false; + } +} + +static inline int mul_bound(int x, int y) { + unsigned long long t = (unsigned long long)x * y; + return t <= 0x7fffffff ? (int)t : 0x7fffffff; +} + +// templatized unpack functions +template +bool unpack(vm::CellSlice& cs, R& rec, Args&... args) { + return (typename R::type_class{}).unpack(cs, rec, args...); +} + +template +bool unpack_exact(vm::CellSlice& cs, R& rec, Args&... args) { + return (typename R::type_class{}).unpack(cs, rec, args...) && cs.empty_ext(); +} + +template +bool type_unpack(vm::CellSlice& cs, const T& type, R& rec, Args&... args) { + return type.unpack(cs, rec, args...); +} + +template +bool type_unpack_exact(vm::CellSlice& cs, const T& type, R& rec, Args&... args) { + return type.unpack(cs, rec, args...) && cs.empty_ext(); +} + +template +bool csr_unpack(Ref csr, R& rec, Args&... args) { + return (typename R::type_class{}).unpack(csr.write(), rec, args...) && csr->empty_ext(); +} + +template +bool csr_unpack_safe(Ref csr, R& rec, Args&... args) { + return csr.not_null() && (typename R::type_class{}).unpack(csr.write(), rec, args...) && csr->empty_ext(); +} + +template +bool unpack_cell(Ref cell, R& rec, Args&... args) { + vm::CellSlice cs = vm::load_cell_slice(std::move(cell)); + return cs.is_valid() && (typename R::type_class{}).unpack(cs, rec, args...) && cs.empty_ext(); +} + +template +bool unpack_cell_inexact(Ref cell, R& rec, Args&... args) { + vm::CellSlice cs = vm::load_cell_slice(std::move(cell)); + return cs.is_valid() && (typename R::type_class{}).unpack(cs, rec, args...); +} + +template +bool type_unpack_cell(Ref cell, const T& type, R& rec, Args&... args) { + vm::CellSlice cs = vm::load_cell_slice(std::move(cell)); + return cs.is_valid() && type.unpack(cs, rec, args...) && cs.empty_ext(); +} + +template +bool csr_type_unpack(Ref csr, const T& type, R& rec, Args&... args) { + return type.unpack(csr.write(), rec, args...) && csr->empty_ext(); +} + +template +bool csr_unpack_inexact(Ref csr, R& rec, Args&... args) { + return (typename R::type_class{}).unpack(csr.write(), rec, args...); +} + +template +bool csr_type_unpack_inexact(Ref csr, const T& type, R& rec, Args&... args) { + return type.unpack(csr.write(), rec, args...); +} + +template +bool csr_unpack_skip(Ref& csr, R& rec, Args&... args) { + return (typename R::type_class{}).unpack(csr.write(), rec, args...); +} + +template +bool csr_type_unpack_skip(Ref& csr, const T& type, R& rec, Args&... args) { + return type.unpack(csr.write(), rec, args...); +} + +// templatized pack functions +template +bool pack(vm::CellBuilder& cb, const R& rec, Args&... args) { + return (typename R::type_class{}).pack(cb, rec, args...); +} + +template +bool type_pack(vm::CellBuilder& cb, const T& type, const R& rec, Args&... args) { + return type.pack(cb, rec, args...); +} + +template +bool pack_cell(Ref& cell, const R& rec, Args&... args) { + vm::CellBuilder cb; + return pack(cb, rec, args...) && cb.finalize_to(cell); +} + +template +bool type_pack_cell(Ref& cell, const T& type, const R& rec, Args&... args) { + vm::CellBuilder cb; + return type.pack(cb, rec, args...) && cb.finalize_to(cell); +} + +template +bool csr_pack(Ref& csr, const R& rec, Args&... args) { + vm::CellBuilder cb; + Ref cell; + return pack(cb, rec, args...) && cb.finalize_to(cell) && (csr = vm::load_cell_slice_ref(std::move(cell))).not_null(); +} + +template +bool csr_type_pack(Ref& csr, const T& type, const R& rec, Args&... args) { + vm::CellBuilder cb; + Ref cell; + return type.pack(cb, rec, args...) && cb.finalize_to(cell) && + (csr = vm::load_cell_slice_ref(std::move(cell))).not_null(); +} + +// templatized store_from function + +template +bool store_from(vm::CellBuilder& cb, const T& tlb_type, Ref field, Args&... args) { + if (field.is_null()) { + return false; + } + vm::CellSlice cs{*field}; + return tlb_type.skip(cs, args...) && cs.empty_ext() && cb.append_cellslice_bool(std::move(field)); +} + +} // namespace tlb + +namespace tlb { + +struct PrettyPrinter { + std::ostream& os; + int indent; + int level; + bool failed; + bool nl_used; + int mode; + PrettyPrinter(std::ostream& _os, int _indent = 0, int _mode = 1) + : os(_os), indent(_indent), level(0), failed(false), nl_used(false), mode(_mode) { + } + ~PrettyPrinter(); + bool ok() const { + return !failed && !level; + } + bool fail_unless(bool res) { + if (!res) { + failed = true; + } + return res; + } + bool fail(std::string msg); + bool nl(int delta = 0); + bool raw_nl(int delta = 0); + bool mkindent(int delta = 0); + bool mode_nl(); + bool open(std::string msg = ""); + bool close(); + bool close(std::string msg); + bool field(std::string name); + bool field(); + bool field_int(long long value); + bool field_int(long long value, std::string name); + bool field_uint(unsigned long long value); + bool field_uint(unsigned long long value, std::string name); + bool out(std::string str) { + os << str; + return true; + } + bool out_int(long long value) { + os << value; + return true; + } + bool out_uint(unsigned long long value) { + os << value; + return true; + } + bool out_integer(td::RefInt256 value) { + if (value.not_null()) { + os << std::move(value); + return true; + } else { + return false; + } + } + bool cons(std::string str) { + return out(str); + } + bool fetch_bits_field(vm::CellSlice& cs, int n); + bool fetch_bits_field(vm::CellSlice& cs, int n, std::string name); + bool fetch_int_field(vm::CellSlice& cs, int n); + bool fetch_int_field(vm::CellSlice& cs, int n, std::string name); + bool fetch_uint_field(vm::CellSlice& cs, int n); + bool fetch_uint_field(vm::CellSlice& cs, int n, std::string name); + bool fetch_int256_field(vm::CellSlice& cs, int n); + bool fetch_int256_field(vm::CellSlice& cs, int n, std::string name); + bool fetch_uint256_field(vm::CellSlice& cs, int n); + bool fetch_uint256_field(vm::CellSlice& cs, int n, std::string name); + template + PrettyPrinter& operator<<(const T& value) { + os << value; + return *this; + } +}; + +} // namespace tlb + +namespace tlb { + +struct False final : TLB { + int get_size(const vm::CellSlice& cs) const override { + return -1; + } + std::ostream& print_type(std::ostream& os) const override { + return os << "False"; + } + bool print_skip(PrettyPrinter& pp, vm::CellSlice& cs) const override { + return false; + } +}; + +extern const False t_False; + +struct True final : TLB { + int get_size(const vm::CellSlice& cs) const override { + return 0; + } + std::ostream& print_type(std::ostream& os) const override { + return os << "True"; + } + bool print_skip(PrettyPrinter& pp, vm::CellSlice& cs) const override { + return pp.out("true"); + } +}; + +extern const True t_True; + +struct Unit final : TLB { + int get_size(const vm::CellSlice& cs) const override { + return 0; + } + std::ostream& print_type(std::ostream& os) const override { + return os << "Unit"; + } + bool print_skip(PrettyPrinter& pp, vm::CellSlice& cs) const override { + return pp.out("()"); + } +}; + +struct FwdT final : TLB { + const TLB& X; + FwdT(const TLB& _X) : X(_X) { + } + int get_size(const vm::CellSlice& cs) const override { + return X.get_size(cs); + } + bool skip(vm::CellSlice& cs) const override { + return X.skip(cs); + } + bool validate(const vm::CellSlice& cs, bool weak = false) const override { + return X.validate(cs, weak); + } + Ref validate_fetch(vm::CellSlice& cs, bool weak = false) const override { + return X.validate_fetch(cs, weak); + } + Ref validate_prefetch(const vm::CellSlice& cs, bool weak = false) const override { + return X.validate_prefetch(cs, weak); + } + bool extract(vm::CellSlice& cs) const override { + return X.extract(cs); + } + bool validate_extract(vm::CellSlice& cs, bool weak = false) const override { + return X.validate_extract(cs, weak); + } + bool validate_skip(vm::CellSlice& cs, bool weak = false) const override { + return X.validate_skip(cs, weak); + } + bool skip_copy(vm::CellBuilder& cb, vm::CellSlice& cs) const override { + return X.skip_copy(cb, cs); + } + bool copy(vm::CellBuilder& cb, const vm::CellSlice& cs) const override { + return X.copy(cb, cs); + } + int get_tag(const vm::CellSlice& cs) const override { + return X.get_tag(cs); + } + long long as_int(const vm::CellSlice& cs) const override { + return X.as_int(cs); + } + unsigned long long as_uint(const vm::CellSlice& cs) const override { + return X.as_uint(cs); + } + td::RefInt256 as_integer(const vm::CellSlice& cs) const override { + return X.as_integer(cs); + } + td::RefInt256 as_integer_skip(vm::CellSlice& cs) const override { + return X.as_integer_skip(cs); + } + td::RefInt256 as_integer(Ref cs) const override { + return X.as_integer(std::move(cs)); + } + bool null_value(vm::CellBuilder& cb) const override { + return X.null_value(cb); + } + bool store_integer_value(vm::CellBuilder& cb, const td::BigInt256& value) const override { + return X.store_integer_value(cb, value); + } + bool store_integer_ref(vm::CellBuilder& cb, td::RefInt256 value) const override { + return X.store_integer_ref(cb, std::move(value)); + } + bool add_values(vm::CellBuilder& cb, vm::CellSlice& cs1, vm::CellSlice& cs2) const override { + return X.add_values(cb, cs1, cs2); + } + std::ostream& print_type(std::ostream& os) const override { + return X.print_type(os); + } + bool print_skip(PrettyPrinter& pp, vm::CellSlice& cs) const override { + return X.print_skip(pp, cs); + } +}; + +extern const Unit t_Unit; + +struct Bool final : TLB { + enum { bool_false = 0, bool_true = 1 }; + int get_size(const vm::CellSlice& cs) const override { + return 1; + } + int get_tag(const vm::CellSlice& cs) const override { + return (int)cs.prefetch_ulong(1); + } + std::ostream& print_type(std::ostream& os) const override { + return os << "Bool"; + } + bool print_skip(PrettyPrinter& pp, vm::CellSlice& cs) const override; +}; + +extern const Bool t_Bool; + +struct NatWidth final : TLB { + int n; + NatWidth(int _n) : n(_n) { + } + int get_size(const vm::CellSlice& cs) const override { + return n <= 32 ? n : -1; + } + td::RefInt256 as_integer(const vm::CellSlice& cs) const override { + return cs.prefetch_int256(n, false); + } + td::RefInt256 as_integer_skip(vm::CellSlice& cs) const override { + return cs.fetch_int256(n, false); + } + unsigned long long as_uint(const vm::CellSlice& cs) const override { + return n <= 32 ? cs.prefetch_ulong(n) : -1; + } + std::ostream& print_type(std::ostream& os) const override { + return os << "(## " << n << ')'; + } + bool print_skip(PrettyPrinter& pp, vm::CellSlice& cs) const override; +}; + +extern const NatWidth t_Nat; + +struct NatLess final : TLB { + int n, w; + NatLess(int _n) : n(_n - 1), w(32 - td::count_leading_zeroes32(_n - 1)) { + } + int get_size(const vm::CellSlice& cs) const override { + return n >= 0 ? w : -1; + } + bool validate(const vm::CellSlice& cs, bool weak = false) const override { + return n >= 0 && (unsigned)cs.prefetch_ulong(w) <= (unsigned)n; + } + bool validate_skip(vm::CellSlice& cs, bool weak = false) const override { + return n >= 0 && (unsigned)cs.fetch_ulong(w) <= (unsigned)n; + } + unsigned long long as_uint(const vm::CellSlice& cs) const override { + unsigned long long r = cs.prefetch_ulong(w); + return n >= 0 && (unsigned)r <= (unsigned)n ? r : std::numeric_limits::max(); + } + std::ostream& print_type(std::ostream& os) const override { + return os << "(#< " << n << ')'; + } + bool print_skip(PrettyPrinter& pp, vm::CellSlice& cs) const override; +}; + +struct NatLeq final : TLB { + int n, w; + NatLeq(int _n) : n(_n), w(32 - td::count_leading_zeroes32(_n)) { + } + int get_size(const vm::CellSlice& cs) const override { + return n >= 0 ? w : -1; + } + bool validate(const vm::CellSlice& cs, bool weak = false) const override { + return n >= 0 && (unsigned)cs.prefetch_ulong(w) <= (unsigned)n; + } + bool validate_skip(vm::CellSlice& cs, bool weak = false) const override { + return n >= 0 && (unsigned)cs.fetch_ulong(w) <= (unsigned)n; + } + unsigned long long as_uint(const vm::CellSlice& cs) const override { + unsigned long long r = cs.prefetch_ulong(w); + return n >= 0 && (unsigned)r <= (unsigned)n ? r : std::numeric_limits::max(); + } + std::ostream& print_type(std::ostream& os) const override { + return os << "(#<= " << n << ')'; + } + bool print_skip(PrettyPrinter& pp, vm::CellSlice& cs) const override; +}; + +struct TupleT final : TLB_Complex { + int n; + const TLB& X; + TupleT(int _n, const TLB& _X) : n(_n), X(_X) { + } + bool skip(vm::CellSlice& cs) const override; + bool validate_skip(vm::CellSlice& cs, bool weak = false) const override; + int get_tag(const vm::CellSlice& cs) const override { + return 0; + } + bool print_skip(PrettyPrinter& pp, vm::CellSlice& cs) const override; +}; + +struct CondT final : TLB_Complex { + int n; + const TLB& X; + CondT(int _n, const TLB& _X) : n(_n), X(_X) { + } + bool skip(vm::CellSlice& cs) const override { + return !n || X.skip(cs); + } + bool validate_skip(vm::CellSlice& cs, bool weak = false) const override { + return !n || (n > 0 && X.validate_skip(cs, weak)); + } + int get_tag(const vm::CellSlice& cs) const override { + return 0; + } + std::ostream& print_type(std::ostream& os) const override { + return os << "(CondT " << n << ' ' << X << ')'; + } + bool print_skip(PrettyPrinter& pp, vm::CellSlice& cs) const override; +}; + +template +struct Cond final : TLB_Complex { + int n; + T field_type; + template + Cond(int _n, Args... args) : n(_n), field_type(args...) { + } + bool skip(vm::CellSlice& cs) const override { + return !n || field_type.skip(cs); + } + bool validate_skip(vm::CellSlice& cs, bool weak = false) const override { + return !n || (n > 0 && field_type.validate_skip(cs, weak)); + } + int get_tag(const vm::CellSlice& cs) const override { + return 0; + } + std::ostream& print_type(std::ostream& os) const override { + return os << "(Cond " << n << ' ' << field_type << ')'; + } + bool print_skip(PrettyPrinter& pp, vm::CellSlice& cs) const override { + return (n > 0 ? field_type.print_skip(pp, cs) : (!n && pp.out("()"))); + } +}; + +struct Int final : TLB { + int n; + Int(int _n) : n(_n) { + } + int get_size(const vm::CellSlice& cs) const override { + return n; + } + td::RefInt256 as_integer(const vm::CellSlice& cs) const override { + return cs.prefetch_int256(n, true); + } + td::RefInt256 as_integer_skip(vm::CellSlice& cs) const override { + return cs.fetch_int256(n, true); + } + long long as_int(const vm::CellSlice& cs) const override { + return n <= 64 ? cs.prefetch_long(n) : (1ULL << 63); + } + bool null_value(vm::CellBuilder& cb) const override { + return cb.store_zeroes_bool(n); + } + bool store_integer_value(vm::CellBuilder& cb, const td::BigInt256& value) const override { + return cb.store_int256_bool(value, n, true); + } + std::ostream& print_type(std::ostream& os) const override { + return os << "int" << n; + } + bool print_skip(PrettyPrinter& pp, vm::CellSlice& cs) const override; +}; + +extern const Int t_int8, t_int16, t_int24, t_int32, t_int64, t_int128, t_int256, t_int257; + +struct UInt final : TLB { + int n; + UInt(int _n) : n(_n) { + } + int get_size(const vm::CellSlice& cs) const override { + return n; + } + td::RefInt256 as_integer(const vm::CellSlice& cs) const override { + return cs.prefetch_int256(n, false); + } + td::RefInt256 as_integer_skip(vm::CellSlice& cs) const override { + return cs.fetch_int256(n, false); + } + unsigned long long as_uint(const vm::CellSlice& cs) const override { + return n <= 64 ? cs.prefetch_ulong(n) : -1; + } + bool null_value(vm::CellBuilder& cb) const override { + return cb.store_zeroes_bool(n); + } + bool store_integer_value(vm::CellBuilder& cb, const td::BigInt256& value) const override { + return cb.store_int256_bool(value, n, false); + } + std::ostream& print_type(std::ostream& os) const override { + return os << "uint" << n; + } + bool print_skip(PrettyPrinter& pp, vm::CellSlice& cs) const override; +}; + +extern const UInt t_uint8, t_uint16, t_uint24, t_uint32, t_uint64, t_uint128, t_uint256; + +struct Bits final : TLB { + int n; + Bits(int _n) : n(_n) { + } + int get_size(const vm::CellSlice& cs) const override { + return n; + } + bool null_value(vm::CellBuilder& cb) const override { + return cb.store_zeroes_bool(n); + } + std::ostream& print_type(std::ostream& os) const override { + return os << "bits" << n; + } + bool print_skip(PrettyPrinter& pp, vm::CellSlice& cs) const override; +}; + +template +struct Maybe : TLB_Complex { + T field_type; + template + Maybe(Args... args) : field_type(args...) { + } + bool skip(vm::CellSlice& cs) const override; + bool validate_skip(vm::CellSlice& cs, bool weak = false) const override; + int get_tag(const vm::CellSlice& cs) const override { + return cs.have(1) ? (int)cs.prefetch_ulong(1) : -1; + } + std::ostream& print_type(std::ostream& os) const override { + return os << "(Maybe " << field_type << ')'; + } + bool print_skip(PrettyPrinter& pp, vm::CellSlice& cs) const override; +}; + +template +bool Maybe::skip(vm::CellSlice& cs) const { + int t = get_tag(cs); + if (t > 0) { + return cs.advance(1) && field_type.skip(cs); + } else if (!t) { + return cs.advance(1); + } else { + return false; + } +} + +template +bool Maybe::validate_skip(vm::CellSlice& cs, bool weak) const { + int t = get_tag(cs); + if (t > 0) { + return cs.advance(1) && field_type.validate_skip(cs, weak); + } else if (!t) { + return cs.advance(1); + } else { + return false; + } +} + +template +bool Maybe::print_skip(PrettyPrinter& pp, vm::CellSlice& cs) const { + if (!get_tag(cs)) { + return cs.advance(1) && pp.out("nothing"); + } else { + return cs.advance(1) && pp.open("just ") && field_type.print_skip(pp, cs) && pp.close(); + } +} + +struct RefAnything final : TLB { + int get_size(const vm::CellSlice& cs) const override { + return 0x10000; + } + std::ostream& print_type(std::ostream& os) const override { + return os << "^Cell"; + } +}; + +extern const RefAnything t_RefCell; + +struct Anything final : TLB { + int get_size(const vm::CellSlice& cs) const override { + return cs.size_ext(); + } + std::ostream& print_type(std::ostream& os) const override { + return os << "Any"; + } +}; + +extern const Anything t_Anything; + +template +struct RefTo final : TLB { + T ref_type; + template + RefTo(Args... args) : ref_type(args...) { + } + int get_size(const vm::CellSlice& cs) const override { + return 0x10000; + } + bool validate(const vm::CellSlice& cs, bool weak = false) const override { + return cs.size_refs() ? ref_type.validate_ref(cs.prefetch_ref(), weak) : false; + } + bool validate_skip(vm::CellSlice& cs, bool weak = false) const override { + return ref_type.validate_skip_ref(cs, weak); + } + std::ostream& print_type(std::ostream& os) const override { + return os << '^' << ref_type; + } + bool print_skip(PrettyPrinter& pp, vm::CellSlice& cs) const override { + return pp.out("^") && ref_type.print_ref(pp, cs.fetch_ref()); + } +}; + +struct RefT final : TLB { + const TLB& X; + RefT(const TLB& _X) : X(_X) { + } + int get_size(const vm::CellSlice& cs) const override { + return 0x10000; + } + bool validate(const vm::CellSlice& cs, bool weak = false) const override { + return X.validate_ref(cs.prefetch_ref(), weak); + } + bool validate_skip(vm::CellSlice& cs, bool weak = false) const override { + return X.validate_skip_ref(cs, weak); + } + std::ostream& print_type(std::ostream& os) const override { + return os << '^' << X; + } + bool print_skip(PrettyPrinter& pp, vm::CellSlice& cs) const override { + return pp.out("^") && X.print_ref(pp, cs.fetch_ref()); + } +}; + +template +struct Either final : TLB_Complex { + T1 left_type; + T2 right_type; + bool skip(vm::CellSlice& cs) const override { + return cs.have(1) ? (cs.fetch_ulong(1) ? right_type.skip(cs) : left_type.skip(cs)) : false; + } + bool validate_skip(vm::CellSlice& cs, bool weak = false) const override { + return cs.have(1) ? (cs.fetch_ulong(1) ? right_type.validate_skip(cs, weak) : left_type.validate_skip(cs, weak)) + : false; + } + int get_tag(const vm::CellSlice& cs) const override { + return (int)cs.prefetch_ulong(1); + } + std::ostream& print_type(std::ostream& os) const override { + return os << "(Either " << left_type << ' ' << right_type << ')'; + } + bool print_skip(PrettyPrinter& pp, vm::CellSlice& cs) const override; +}; + +template +bool Either::print_skip(PrettyPrinter& pp, vm::CellSlice& cs) const { + if (!get_tag(cs)) { + return cs.advance(1) && pp.open("left ") && left_type.print_skip(pp, cs) && pp.close(); + } else { + return cs.advance(1) && pp.open("right ") && right_type.print_skip(pp, cs) && pp.close(); + } +} + +} // namespace tlb diff --git a/crypto/vm/arithops.cpp b/crypto/vm/arithops.cpp new file mode 100644 index 00000000..19279cf1 --- /dev/null +++ b/crypto/vm/arithops.cpp @@ -0,0 +1,946 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include +#include "vm/arithops.h" +#include "vm/log.h" +#include "vm/opctable.h" +#include "vm/stack.hpp" +#include "vm/continuation.h" +#include "vm/excno.hpp" +#include "common/bigint.hpp" +#include "common/refint.h" + +namespace vm { + +int exec_push_tinyint4(VmState* st, unsigned args) { + int x = (int)((args + 5) & 15) - 5; + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute PUSHINT " << x; + stack.push_smallint(x); + return 0; +} + +std::string dump_push_tinyint4(CellSlice&, unsigned args) { + int x = (int)((args + 5) & 15) - 5; + std::ostringstream os{"PUSHINT "}; + os << x; + return os.str(); +} + +int exec_push_tinyint8(VmState* st, unsigned args) { + int x = (signed char)args; + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute PUSHINT " << x; + stack.push_smallint(x); + return 0; +} + +std::string dump_op_tinyint8(const char* op_prefix, CellSlice&, unsigned args) { + int x = (signed char)args; + std::ostringstream os{op_prefix}; + os << x; + return os.str(); +} + +int exec_push_smallint(VmState* st, unsigned args) { + int x = (short)args; + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute PUSHINT " << x; + stack.push_smallint(x); + return 0; +} + +std::string dump_push_smallint(CellSlice&, unsigned args) { + int x = (short)args; + std::ostringstream os{"PUSHINT "}; + os << x; + return os.str(); +} + +int exec_push_int(VmState* st, CellSlice& cs, unsigned args, int pfx_bits) { + int l = (int)(args & 31) + 2; + if (!cs.have(pfx_bits + 3 + l * 8)) { + throw VmError{Excno::inv_opcode, "not enough bits for integer constant in PUSHINT"}; + } + cs.advance(pfx_bits); + td::RefInt256 x = cs.fetch_int256(3 + l * 8); + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute PUSHINT " << x; + stack.push_int(std::move(x)); + return 0; +} + +std::string dump_push_int(CellSlice& cs, unsigned args, int pfx_bits) { + int l = (int)(args & 31) + 2; + if (!cs.have(pfx_bits + 3 + l * 8)) { + return ""; + } + cs.advance(pfx_bits); + td::RefInt256 x = cs.fetch_int256(3 + l * 8); + std::ostringstream os{"PUSHINT "}; + os << x; + return os.str(); +} + +int compute_len_push_int(const CellSlice& cs, unsigned args, int pfx_bits) { + int l = (int)(args & 31) + 2; + if (!cs.have(pfx_bits + 3 + l * 8)) { + return 0; + } else { + return pfx_bits + 3 + l * 8; + } +} + +int exec_push_pow2(VmState* st, unsigned args) { + int x = (args & 255) + 1; + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute PUSHPOW2 " << x; + td::RefInt256 r{true}; + r.unique_write().set_pow2(x); + stack.push(std::move(r)); + return 0; +} + +int exec_push_nan(VmState* st) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute PUSHNAN"; + td::RefInt256 r{true}; + r.unique_write().invalidate(); + stack.push(std::move(r)); + return 0; +} + +int exec_push_pow2dec(VmState* st, unsigned args) { + int x = (args & 255) + 1; + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute PUSHPOW2DEC " << x; + td::RefInt256 r{true}; + r.unique_write().set_pow2(x).add_tiny(-1).normalize(); + stack.push(std::move(r)); + return 0; +} + +int exec_push_negpow2(VmState* st, unsigned args) { + int x = (args & 255) + 1; + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute PUSHNEGPOW2 " << x; + td::RefInt256 r{true}; + r.unique_write().set_pow2(x).negate().normalize(); + stack.push(std::move(r)); + return 0; +} + +void register_int_const_ops(OpcodeTable& cp0) { + using namespace std::placeholders; + cp0.insert(OpcodeInstr::mkfixed(0x7, 4, 4, dump_push_tinyint4, exec_push_tinyint4)) + .insert(OpcodeInstr::mkfixed(0x80, 8, 8, std::bind(dump_op_tinyint8, "PUSHINT ", _1, _2), exec_push_tinyint8)) + .insert(OpcodeInstr::mkfixed(0x81, 8, 16, dump_push_smallint, exec_push_smallint)) + .insert(OpcodeInstr::mkextrange(0x82 << 5, (0x82 << 5) + 31, 13, 5, dump_push_int, exec_push_int, + compute_len_push_int)) + .insert(OpcodeInstr::mkfixedrange(0x8300, 0x83ff, 16, 8, instr::dump_1c_l_add(1, "PUSHPOW2 "), exec_push_pow2)) + .insert(OpcodeInstr::mksimple(0x83ff, 16, "PUSHNAN", exec_push_nan)) + .insert(OpcodeInstr::mkfixed(0x84, 8, 8, instr::dump_1c_l_add(1, "PUSHPOW2DEC "), exec_push_pow2dec)) + .insert(OpcodeInstr::mkfixed(0x85, 8, 8, instr::dump_1c_l_add(1, "PUSHNEGPOW2 "), exec_push_negpow2)); +} + +int exec_add(VmState* st, bool quiet) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute ADD"; + stack.check_underflow(2); + auto y = stack.pop_int(); + stack.push_int_quiet(stack.pop_int() + std::move(y), quiet); + return 0; +} + +int exec_sub(VmState* st, bool quiet) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute SUB"; + stack.check_underflow(2); + auto y = stack.pop_int(); + stack.push_int_quiet(stack.pop_int() - std::move(y), quiet); + return 0; +} + +int exec_subr(VmState* st, bool quiet) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute SUBR"; + stack.check_underflow(2); + auto y = stack.pop_int(); + stack.push_int_quiet(std::move(y) - stack.pop_int(), quiet); + return 0; +} + +int exec_negate(VmState* st, bool quiet) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute NEGATE"; + stack.check_underflow(1); + stack.push_int_quiet(-stack.pop_int(), quiet); + return 0; +} + +int exec_inc(VmState* st, bool quiet) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute INC"; + stack.check_underflow(1); + stack.push_int_quiet(stack.pop_int() + 1, quiet); + return 0; +} + +int exec_dec(VmState* st, bool quiet) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute DEC"; + stack.check_underflow(1); + stack.push_int_quiet(stack.pop_int() - 1, quiet); + return 0; +} + +int exec_add_tinyint8(VmState* st, unsigned args, bool quiet) { + int x = (signed char)args; + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute ADDINT " << x; + stack.check_underflow(1); + stack.push_int_quiet(stack.pop_int() + x, quiet); + return 0; +} + +int exec_mul_tinyint8(VmState* st, unsigned args, bool quiet) { + int x = (signed char)args; + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute MULINT " << x; + stack.check_underflow(1); + stack.push_int_quiet(stack.pop_int() * x, quiet); + return 0; +} + +int exec_mul(VmState* st, bool quiet) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute MUL"; + stack.check_underflow(2); + auto y = stack.pop_int(); + stack.push_int_quiet(stack.pop_int() * std::move(y), quiet); + return 0; +} + +void register_add_mul_ops(OpcodeTable& cp0) { + using namespace std::placeholders; + cp0.insert(OpcodeInstr::mksimple(0xa0, 8, "ADD", std::bind(exec_add, _1, false))) + .insert(OpcodeInstr::mksimple(0xa1, 8, "SUB", std::bind(exec_sub, _1, false))) + .insert(OpcodeInstr::mksimple(0xa2, 8, "SUBR", std::bind(exec_subr, _1, false))) + .insert(OpcodeInstr::mksimple(0xa3, 8, "NEGATE", std::bind(exec_negate, _1, false))) + .insert(OpcodeInstr::mksimple(0xa4, 8, "INC", std::bind(exec_inc, _1, false))) + .insert(OpcodeInstr::mksimple(0xa5, 8, "DEC", std::bind(exec_dec, _1, false))) + .insert(OpcodeInstr::mkfixed(0xa6, 8, 8, std::bind(dump_op_tinyint8, "ADDINT ", _1, _2), + std::bind(exec_add_tinyint8, _1, _2, false))) + .insert(OpcodeInstr::mkfixed(0xa7, 8, 8, std::bind(dump_op_tinyint8, "MULINT ", _1, _2), + std::bind(exec_mul_tinyint8, _1, _2, false))) + .insert(OpcodeInstr::mksimple(0xa8, 8, "MUL", std::bind(exec_mul, _1, false))); + cp0.insert(OpcodeInstr::mksimple(0xb7a0, 16, "QADD", std::bind(exec_add, _1, true))) + .insert(OpcodeInstr::mksimple(0xb7a1, 16, "QSUB", std::bind(exec_sub, _1, true))) + .insert(OpcodeInstr::mksimple(0xb7a2, 16, "QSUBR", std::bind(exec_subr, _1, true))) + .insert(OpcodeInstr::mksimple(0xb7a3, 16, "QNEGATE", std::bind(exec_negate, _1, true))) + .insert(OpcodeInstr::mksimple(0xb7a4, 16, "QINC", std::bind(exec_inc, _1, true))) + .insert(OpcodeInstr::mksimple(0xb7a5, 16, "QDEC", std::bind(exec_dec, _1, true))) + .insert(OpcodeInstr::mkfixed(0xb7a6, 16, 8, std::bind(dump_op_tinyint8, "QADDINT ", _1, _2), + std::bind(exec_add_tinyint8, _1, _2, true))) + .insert(OpcodeInstr::mkfixed(0xb7a7, 16, 8, std::bind(dump_op_tinyint8, "QMULINT ", _1, _2), + std::bind(exec_mul_tinyint8, _1, _2, true))) + .insert(OpcodeInstr::mksimple(0xb7a8, 16, "QMUL", std::bind(exec_mul, _1, true))); +} + +int exec_divmod(VmState* st, unsigned args, int quiet) { + int round_mode = (int)(args & 3) - 1; + if (!(args & 12) || round_mode == 2) { + throw VmError{Excno::inv_opcode}; + } + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute DIV/MOD " << (args & 15); + stack.check_underflow(2); + auto y = stack.pop_int(); + auto x = stack.pop_int(); + switch ((args >> 2) & 3) { + case 1: + stack.push_int_quiet(td::div(std::move(x), std::move(y), round_mode), quiet); + break; + case 2: + stack.push_int_quiet(td::mod(std::move(x), std::move(y), round_mode), quiet); + break; + case 3: { + auto dm = td::divmod(std::move(x), std::move(y), round_mode); + stack.push_int_quiet(std::move(dm.first), quiet); + stack.push_int_quiet(std::move(dm.second), quiet); + break; + } + } + return 0; +} + +std::string dump_divmod(CellSlice&, unsigned args, bool quiet) { + int round_mode = (int)(args & 3); + if (!(args & 12) || round_mode == 3) { + return ""; + } + std::string s = (args & 4) ? "DIV" : ""; + if (args & 8) { + s += "MOD"; + } + if (quiet) { + s = "Q" + s; + } + return s + "FRC"[round_mode]; +} + +int exec_shrmod(VmState* st, unsigned args, int mode) { + int y = -1; + if (mode & 2) { + y = (args & 0xff) + 1; + args >>= 8; + } + int round_mode = (int)(args & 3) - 1; + if (!(args & 12) || round_mode == 2) { + throw VmError{Excno::inv_opcode}; + } + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute SHR/MOD " << (args & 15) << ',' << y; + if (!(mode & 2)) { + stack.check_underflow(2); + y = stack.pop_smallint_range(256); + } else { + stack.check_underflow(1); + } + if (!y) { + round_mode = -1; + } + auto x = stack.pop_int(); + switch ((args >> 2) & 3) { + case 1: + stack.push_int_quiet(td::rshift(std::move(x), y, round_mode), mode & 1); + break; + case 3: + stack.push_int_quiet(td::rshift(x, y, round_mode), mode & 1); + // fallthrough + case 2: + x.write().mod_pow2(y, round_mode).normalize(); + stack.push_int_quiet(std::move(x), mode & 1); + break; + } + return 0; +} + +std::string dump_shrmod(CellSlice&, unsigned args, int mode) { + int y = -1; + if (mode & 2) { + y = (args & 0xff) + 1; + args >>= 8; + } + int round_mode = (int)(args & 3); + if (!(args & 12) || round_mode == 3) { + return ""; + } + std::string s; + switch (args & 12) { + case 4: + s = "RSHIFT"; + break; + case 8: + s = "MODPOW2"; + break; + case 12: + s = "RSHIFTMOD"; + break; + } + if (mode & 1) { + s = "Q" + s; + } + s += "FRC"[round_mode]; + if (mode & 2) { + char buff[8]; + sprintf(buff, " %d", y); + s += buff; + } + return s; +} + +int exec_muldivmod(VmState* st, unsigned args, int quiet) { + int round_mode = (int)(args & 3) - 1; + if (!(args & 12) || round_mode == 2) { + throw VmError{Excno::inv_opcode}; + } + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute MULDIV/MOD " << (args & 15); + stack.check_underflow(3); + auto z = stack.pop_int(); + auto y = stack.pop_int(); + auto x = stack.pop_int(); + typename td::BigInt256::DoubleInt tmp{0}; + tmp.add_mul(*x, *y); + auto q = td::RefInt256{true}; + tmp.mod_div(*z, q.unique_write(), round_mode); + switch ((args >> 2) & 3) { + case 1: + q.unique_write().normalize(); + stack.push_int_quiet(std::move(q), quiet); + break; + case 3: + q.unique_write().normalize(); + stack.push_int_quiet(std::move(q), quiet); + // fallthrough + case 2: + stack.push_int_quiet(td::RefInt256{true, tmp}, quiet); + break; + } + return 0; +} + +std::string dump_muldivmod(CellSlice&, unsigned args, bool quiet) { + int round_mode = (int)(args & 3); + if (!(args & 12) || round_mode == 3) { + return ""; + } + std::string s = (args & 4) ? "MULDIV" : "MUL"; + if (args & 8) { + s += "MOD"; + } + if (quiet) { + s = "Q" + s; + } + return s + "FRC"[round_mode]; +} + +int exec_mulshrmod(VmState* st, unsigned args, int mode) { + int z = -1; + if (mode & 2) { + z = (args & 0xff) + 1; + args >>= 8; + } + int round_mode = (int)(args & 3) - 1; + if (!(args & 12) || round_mode == 2) { + throw VmError{Excno::inv_opcode}; + } + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute MULSHR/MOD " << (args & 15) << ',' << z; + if (!(mode & 2)) { + stack.check_underflow(3); + z = stack.pop_smallint_range(256); + } else { + stack.check_underflow(2); + } + if (!z) { + round_mode = -1; + } + auto y = stack.pop_int(); + auto x = stack.pop_int(); + typename td::BigInt256::DoubleInt tmp{0}; + tmp.add_mul(*x, *y); + switch ((args >> 2) & 3) { + case 1: + tmp.rshift(z, round_mode).normalize(); + stack.push_int_quiet(td::RefInt256{true, tmp}, mode & 1); + break; + case 3: { + typename td::BigInt256::DoubleInt tmp2{tmp}; + tmp2.rshift(z, round_mode).normalize(); + stack.push_int_quiet(td::RefInt256{true, tmp2}, mode & 1); + } + // fallthrough + case 2: + tmp.mod_pow2(z, round_mode).normalize(); + stack.push_int_quiet(td::RefInt256{true, tmp}, mode & 1); + break; + } + return 0; +} + +std::string dump_mulshrmod(CellSlice&, unsigned args, int mode) { + int y = -1; + if (mode & 2) { + y = (args & 0xff) + 1; + args >>= 8; + } + int round_mode = (int)(args & 3); + if (!(args & 12) || round_mode == 3) { + return ""; + } + std::string s; + switch (args & 12) { + case 4: + s = "MULRSHIFT"; + break; + case 8: + s = "MULMODPOW2"; + break; + case 12: + s = "MULRSHIFTMOD"; + break; + } + if (mode & 1) { + s = "Q" + s; + } + s += "FRC"[round_mode]; + if (mode & 2) { + char buff[8]; + sprintf(buff, " %d", y); + s += buff; + } + return s; +} + +int exec_shldivmod(VmState* st, unsigned args, int mode) { + int y = -1; + if (mode & 2) { + y = (args & 0xff) + 1; + args >>= 8; + } + int round_mode = (int)(args & 3) - 1; + if (!(args & 12) || round_mode == 2) { + throw VmError{Excno::inv_opcode}; + } + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute SHLDIV/MOD " << (args & 15) << ',' << y; + if (!(mode & 2)) { + stack.check_underflow(3); + y = stack.pop_smallint_range(256); + } else { + stack.check_underflow(2); + } + auto z = stack.pop_int(); + auto x = stack.pop_int(); + typename td::BigInt256::DoubleInt tmp{*x}; + tmp <<= y; + switch ((args >> 2) & 3) { + case 1: { + auto q = td::RefInt256{true}; + tmp.mod_div(*z, q.unique_write(), round_mode); + q.unique_write().normalize(); + stack.push_int_quiet(std::move(q), mode & 1); + break; + } + case 3: { + auto q = td::RefInt256{true}; + tmp.mod_div(*z, q.unique_write(), round_mode); + q.unique_write().normalize(); + stack.push_int_quiet(std::move(q), mode & 1); + stack.push_int_quiet(td::RefInt256{true, tmp}, mode & 1); + break; + } + case 2: { + typename td::BigInt256::DoubleInt tmp2; + tmp.mod_div(*z, tmp2, round_mode); + stack.push_int_quiet(td::RefInt256{true, tmp}, mode & 1); + break; + } + } + return 0; +} + +std::string dump_shldivmod(CellSlice&, unsigned args, bool quiet) { + int round_mode = (int)(args & 3); + if (!(args & 12) || round_mode == 3) { + return ""; + } + std::string s = (args & 4) ? "LSHIFTDIV" : "LSHIFT"; + if (args & 8) { + s += "MOD"; + } + if (quiet) { + s = "Q" + s; + } + return s + "FRC"[round_mode]; +} + +void register_div_ops(OpcodeTable& cp0) { + using namespace std::placeholders; + cp0.insert(OpcodeInstr::mkfixed(0xa90, 12, 4, std::bind(dump_divmod, _1, _2, false), + std::bind(exec_divmod, _1, _2, false))) + .insert(OpcodeInstr::mkfixed(0xa92, 12, 4, std::bind(dump_shrmod, _1, _2, 0), std::bind(exec_shrmod, _1, _2, 0))) + .insert(OpcodeInstr::mkfixed(0xa93, 12, 12, std::bind(dump_shrmod, _1, _2, 2), std::bind(exec_shrmod, _1, _2, 2))) + .insert(OpcodeInstr::mkfixed(0xa98, 12, 4, std::bind(dump_muldivmod, _1, _2, false), + std::bind(exec_muldivmod, _1, _2, false))) + .insert(OpcodeInstr::mkfixed(0xa9a, 12, 4, std::bind(dump_mulshrmod, _1, _2, 0), + std::bind(exec_mulshrmod, _1, _2, 0))) + .insert(OpcodeInstr::mkfixed(0xa9b, 12, 12, std::bind(dump_mulshrmod, _1, _2, 2), + std::bind(exec_mulshrmod, _1, _2, 2))) + .insert(OpcodeInstr::mkfixed(0xa9c, 12, 4, std::bind(dump_shldivmod, _1, _2, 0), + std::bind(exec_shldivmod, _1, _2, 0))) + .insert(OpcodeInstr::mkfixed(0xa9d, 12, 12, std::bind(dump_shldivmod, _1, _2, 2), + std::bind(exec_shldivmod, _1, _2, 2))); + cp0.insert(OpcodeInstr::mkfixed(0xb7a90, 20, 4, std::bind(dump_divmod, _1, _2, true), + std::bind(exec_divmod, _1, _2, true))) + .insert( + OpcodeInstr::mkfixed(0xb7a92, 20, 4, std::bind(dump_shrmod, _1, _2, 1), std::bind(exec_shrmod, _1, _2, 1))) + // .insert(OpcodeInstr::mkfixed(0xb7a93, 20, 12, std::bind(dump_shrmod, _1, _2, 3), std::bind(exec_shrmod, _1, _2, 3))) + .insert(OpcodeInstr::mkfixed(0xb7a98, 20, 4, std::bind(dump_muldivmod, _1, _2, true), + std::bind(exec_muldivmod, _1, _2, true))) + .insert(OpcodeInstr::mkfixed(0xb7a9a, 20, 4, std::bind(dump_mulshrmod, _1, _2, 1), + std::bind(exec_mulshrmod, _1, _2, 1))) + // .insert(OpcodeInstr::mkfixed(0xb7a9b, 20, 12, std::bind(dump_mulshrmod, _1, _2, 3), std::bind(exec_mulshrmod, _1, _2, 3))) + .insert(OpcodeInstr::mkfixed(0xb7a9c, 20, 4, std::bind(dump_shldivmod, _1, _2, 1), + std::bind(exec_shldivmod, _1, _2, 1))) + // .insert(OpcodeInstr::mkfixed(0xb7a9d, 20, 12, std::bind(dump_shldivmod, _1, _2, 3), std::bind(exec_shldivmod, _1, _2, 3))) + ; +} + +int exec_lshift_tinyint8(VmState* st, unsigned args, bool quiet) { + int x = (args & 0xff) + 1; + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute LSHIFT " << x; + stack.check_underflow(1); + stack.push_int_quiet(stack.pop_int() << x, quiet); + return 0; +} + +int exec_rshift_tinyint8(VmState* st, unsigned args, bool quiet) { + int x = (args & 0xff) + 1; + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute RSHIFT " << x; + stack.check_underflow(1); + stack.push_int_quiet(stack.pop_int() >> x, quiet); + return 0; +} + +int exec_lshift(VmState* st, bool quiet) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute LSHIFT"; + stack.check_underflow(2); + int x = stack.pop_smallint_range(1023); + stack.push_int_quiet(stack.pop_int() << x, quiet); + return 0; +} + +int exec_rshift(VmState* st, bool quiet) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute RSHIFT"; + stack.check_underflow(2); + int x = stack.pop_smallint_range(1023); + stack.push_int_quiet(stack.pop_int() >> x, quiet); + return 0; +} + +int exec_pow2(VmState* st, bool quiet) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute POW2"; + stack.check_underflow(1); + int x = stack.pop_smallint_range(1023); + td::RefInt256 r{true}; + r.unique_write().set_pow2(x); + stack.push_int_quiet(std::move(r), quiet); + return 0; +} + +int exec_and(VmState* st, bool quiet) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute AND"; + stack.check_underflow(2); + auto y = stack.pop_int(); + stack.push_int_quiet(stack.pop_int() & std::move(y), quiet); + return 0; +} + +int exec_or(VmState* st, bool quiet) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute OR"; + stack.check_underflow(2); + auto y = stack.pop_int(); + stack.push_int_quiet(stack.pop_int() | std::move(y), quiet); + return 0; +} + +int exec_xor(VmState* st, bool quiet) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute XOR"; + stack.check_underflow(2); + auto y = stack.pop_int(); + stack.push_int_quiet(stack.pop_int() ^ std::move(y), quiet); + return 0; +} + +int exec_not(VmState* st, bool quiet) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute NOT"; + stack.check_underflow(1); + stack.push_int_quiet(~stack.pop_int(), quiet); + return 0; +} + +int exec_fits_tinyint8(VmState* st, unsigned args, bool quiet) { + int y = (args & 0xff) + 1; + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute FITS " << y; + stack.check_underflow(1); + auto x = stack.pop_int(); + if (!x->signed_fits_bits(y)) { + x.write().invalidate(); + } + stack.push_int_quiet(std::move(x), quiet); + return 0; +} + +int exec_ufits_tinyint8(VmState* st, unsigned args, bool quiet) { + int y = (args & 0xff) + 1; + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute UFITS " << y; + stack.check_underflow(1); + auto x = stack.pop_int(); + if (!x->unsigned_fits_bits(y)) { + x.write().invalidate(); + } + stack.push_int_quiet(std::move(x), quiet); + return 0; +} + +int exec_fits(VmState* st, bool quiet) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute FITSX"; + stack.check_underflow(2); + int y = stack.pop_smallint_range(1023); + auto x = stack.pop_int(); + if (!x->signed_fits_bits(y)) { + x.write().invalidate(); + } + stack.push_int_quiet(std::move(x), quiet); + return 0; +} + +int exec_ufits(VmState* st, bool quiet) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute UFITSX"; + stack.check_underflow(2); + int y = stack.pop_smallint_range(1023); + auto x = stack.pop_int(); + if (!x->unsigned_fits_bits(y)) { + x.write().invalidate(); + } + stack.push_int_quiet(std::move(x), quiet); + return 0; +} + +int exec_bitsize(VmState* st, bool sgnd, bool quiet) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute " << (sgnd ? "" : "U") << "BITSIZE"; + stack.check_underflow(1); + auto x = stack.pop_int(); + int y = x->bit_size(sgnd); + if (y < 0x7fffffff) { + stack.push_smallint(y); + } else if (!quiet) { + throw VmError{Excno::range_chk, "CHKSIZE for negative integer"}; + } else { + stack.push_int_quiet(td::RefInt256{true}, quiet); + } + return 0; +} + +void register_shift_logic_ops(OpcodeTable& cp0) { + using namespace std::placeholders; + cp0.insert(OpcodeInstr::mkfixed(0xaa, 8, 8, instr::dump_1c_l_add(1, "LSHIFT "), + std::bind(exec_lshift_tinyint8, _1, _2, false))) + .insert(OpcodeInstr::mkfixed(0xab, 8, 8, instr::dump_1c_l_add(1, "RSHIFT "), + std::bind(exec_rshift_tinyint8, _1, _2, false))) + .insert(OpcodeInstr::mksimple(0xac, 8, "LSHIFT", std::bind(exec_lshift, _1, false))) + .insert(OpcodeInstr::mksimple(0xad, 8, "RSHIFT", std::bind(exec_rshift, _1, false))) + .insert(OpcodeInstr::mksimple(0xae, 8, "POW2", std::bind(exec_pow2, _1, false))) + .insert(OpcodeInstr::mksimple(0xb0, 8, "AND", std::bind(exec_and, _1, false))) + .insert(OpcodeInstr::mksimple(0xb1, 8, "OR", std::bind(exec_or, _1, false))) + .insert(OpcodeInstr::mksimple(0xb2, 8, "XOR", std::bind(exec_xor, _1, false))) + .insert(OpcodeInstr::mksimple(0xb3, 8, "NOT", std::bind(exec_not, _1, false))) + .insert(OpcodeInstr::mkfixed(0xb4, 8, 8, instr::dump_1c_l_add(1, "FITS "), + std::bind(exec_fits_tinyint8, _1, _2, false))) + .insert(OpcodeInstr::mkfixed(0xb5, 8, 8, instr::dump_1c_l_add(1, "UFITS "), + std::bind(exec_ufits_tinyint8, _1, _2, false))) + .insert(OpcodeInstr::mksimple(0xb600, 16, "FITSX", std::bind(exec_fits, _1, false))) + .insert(OpcodeInstr::mksimple(0xb601, 16, "UFITSX", std::bind(exec_ufits, _1, false))) + .insert(OpcodeInstr::mksimple(0xb602, 16, "BITSIZE", std::bind(exec_bitsize, _1, true, false))) + .insert(OpcodeInstr::mksimple(0xb603, 16, "UBITSIZE", std::bind(exec_bitsize, _1, false, false))); + cp0.insert(OpcodeInstr::mkfixed(0xb7aa, 16, 8, instr::dump_1c_l_add(1, "QLSHIFT "), + std::bind(exec_lshift_tinyint8, _1, _2, true))) + .insert(OpcodeInstr::mkfixed(0xb7ab, 16, 8, instr::dump_1c_l_add(1, "QRSHIFT "), + std::bind(exec_rshift_tinyint8, _1, _2, true))) + .insert(OpcodeInstr::mksimple(0xb7ac, 16, "QLSHIFT", std::bind(exec_lshift, _1, true))) + .insert(OpcodeInstr::mksimple(0xb7ad, 16, "QRSHIFT", std::bind(exec_rshift, _1, true))) + .insert(OpcodeInstr::mksimple(0xb7ae, 16, "QPOW2", std::bind(exec_pow2, _1, true))) + .insert(OpcodeInstr::mksimple(0xb7b0, 16, "QAND", std::bind(exec_and, _1, true))) + .insert(OpcodeInstr::mksimple(0xb7b1, 16, "QOR", std::bind(exec_or, _1, true))) + .insert(OpcodeInstr::mksimple(0xb7b2, 16, "QXOR", std::bind(exec_xor, _1, true))) + .insert(OpcodeInstr::mksimple(0xb7b3, 16, "QNOT", std::bind(exec_not, _1, true))) + .insert(OpcodeInstr::mkfixed(0xb7b4, 16, 8, instr::dump_1c_l_add(1, "QFITS "), + std::bind(exec_fits_tinyint8, _1, _2, true))) + .insert(OpcodeInstr::mkfixed(0xb7b5, 16, 8, instr::dump_1c_l_add(1, "QUFITS "), + std::bind(exec_ufits_tinyint8, _1, _2, true))) + .insert(OpcodeInstr::mksimple(0xb7b600, 24, "QFITSX", std::bind(exec_fits, _1, true))) + .insert(OpcodeInstr::mksimple(0xb7b601, 24, "QUFITSX", std::bind(exec_ufits, _1, true))) + .insert(OpcodeInstr::mksimple(0xb7b602, 24, "QBITSIZE", std::bind(exec_bitsize, _1, true, true))) + .insert(OpcodeInstr::mksimple(0xb7b603, 24, "QUBITSIZE", std::bind(exec_bitsize, _1, false, true))); +} + +int exec_minmax(VmState* st, int mode) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute MINMAXOP " << mode; + stack.check_underflow(2); + auto x = stack.pop_int(); + auto y = stack.pop_int(); + if (!x->is_valid()) { + y = x; + } else if (!y->is_valid()) { + x = y; + } else if (cmp(x, y) > 0) { + swap(x, y); + } + if (mode & 2) { + stack.push_int_quiet(std::move(x), mode & 1); + } + if (mode & 4) { + stack.push_int_quiet(std::move(y), mode & 1); + } + return 0; +} + +int exec_abs(VmState* st, bool quiet) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute ABS"; + stack.check_underflow(1); + auto x = stack.pop_int(); + if (x->is_valid() && x->sgn() < 0) { + stack.push_int_quiet(-std::move(x), quiet); + } else { + stack.push_int_quiet(std::move(x), quiet); + } + return 0; +} + +void register_other_arith_ops(OpcodeTable& cp0) { + using namespace std::placeholders; + cp0.insert(OpcodeInstr::mksimple(0xb608, 16, "MIN", std::bind(exec_minmax, _1, 2))) + .insert(OpcodeInstr::mksimple(0xb609, 16, "MAX", std::bind(exec_minmax, _1, 4))) + .insert(OpcodeInstr::mksimple(0xb60a, 16, "MINMAX", std::bind(exec_minmax, _1, 6))) + .insert(OpcodeInstr::mksimple(0xb60b, 16, "ABS", std::bind(exec_abs, _1, false))); + cp0.insert(OpcodeInstr::mksimple(0xb7b608, 24, "QMIN", std::bind(exec_minmax, _1, 3))) + .insert(OpcodeInstr::mksimple(0xb7b609, 24, "QMAX", std::bind(exec_minmax, _1, 5))) + .insert(OpcodeInstr::mksimple(0xb7b60a, 24, "QMINMAX", std::bind(exec_minmax, _1, 7))) + .insert(OpcodeInstr::mksimple(0xb7b60b, 24, "QABS", std::bind(exec_abs, _1, true))); +} + +int exec_sgn(VmState* st, int mode, bool quiet, const char* name) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute " << name; + stack.check_underflow(1); + auto x = stack.pop_int(); + if (!x->is_valid()) { + stack.push_int_quiet(std::move(x), quiet); + } else { + int y = td::sgn(std::move(x)); + stack.push_smallint(((mode >> (4 + y * 4)) & 15) - 8); + } + return 0; +} + +int exec_cmp(VmState* st, int mode, bool quiet, const char* name) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute " << name; + stack.check_underflow(2); + auto y = stack.pop_int(); + auto x = stack.pop_int(); + if (!x->is_valid() || !y->is_valid()) { + stack.push_int_quiet(std::move(x), quiet); + } else { + int z = td::cmp(std::move(x), std::move(y)); + stack.push_smallint(((mode >> (4 + z * 4)) & 15) - 8); + } + return 0; +} + +int exec_cmp_int(VmState* st, unsigned args, int mode, bool quiet, const char* name) { + int y = (signed char)args; + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute " << name << "INT " << y; + stack.check_underflow(1); + auto x = stack.pop_int(); + if (!x->is_valid()) { + stack.push_int_quiet(std::move(x), quiet); + } else { + int z = td::cmp(std::move(x), y); + stack.push_smallint(((mode >> (4 + z * 4)) & 15) - 8); + } + return 0; +} + +int exec_is_nan(VmState* st) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute ISNAN"; + stack.check_underflow(1); + auto x = stack.pop_int(); + stack.push_smallint(x->is_valid() ? 0 : -1); + return 0; +} + +int exec_chk_nan(VmState* st) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute CHKNAN"; + stack.check_underflow(1); + auto x = stack.pop_int(); + stack.push_int(std::move(x)); + return 0; +} + +void register_int_cmp_ops(OpcodeTable& cp0) { + using namespace std::placeholders; + cp0.insert(OpcodeInstr::mksimple(0xb8, 8, "SGN", std::bind(exec_sgn, _1, 0x987, false, "SGN"))) + .insert(OpcodeInstr::mksimple(0xb9, 8, "LESS", std::bind(exec_cmp, _1, 0x887, false, "LESS"))) + .insert(OpcodeInstr::mksimple(0xba, 8, "EQUAL", std::bind(exec_cmp, _1, 0x878, false, "EQUAL"))) + .insert(OpcodeInstr::mksimple(0xbb, 8, "LEQ", std::bind(exec_cmp, _1, 0x877, false, "LEQ"))) + .insert(OpcodeInstr::mksimple(0xbc, 8, "GREATER", std::bind(exec_cmp, _1, 0x788, false, "GREATER"))) + .insert(OpcodeInstr::mksimple(0xbd, 8, "NEQ", std::bind(exec_cmp, _1, 0x787, false, "NEQ"))) + .insert(OpcodeInstr::mksimple(0xbe, 8, "GEQ", std::bind(exec_cmp, _1, 0x778, false, "GEQ"))) + .insert(OpcodeInstr::mksimple(0xbf, 8, "CMP", std::bind(exec_cmp, _1, 0x987, false, "CMP"))) + .insert(OpcodeInstr::mkfixed(0xc0, 8, 8, std::bind(dump_op_tinyint8, "EQINT ", _1, _2), + std::bind(exec_cmp_int, _1, _2, 0x878, false, "EQ"))) + .insert(OpcodeInstr::mkfixed(0xc1, 8, 8, std::bind(dump_op_tinyint8, "LESSINT ", _1, _2), + std::bind(exec_cmp_int, _1, _2, 0x887, false, "LESS"))) + .insert(OpcodeInstr::mkfixed(0xc2, 8, 8, std::bind(dump_op_tinyint8, "GTINT ", _1, _2), + std::bind(exec_cmp_int, _1, _2, 0x788, false, "GT"))) + .insert(OpcodeInstr::mkfixed(0xc3, 8, 8, std::bind(dump_op_tinyint8, "NEQINT ", _1, _2), + std::bind(exec_cmp_int, _1, _2, 0x787, false, "NEQ"))) + .insert(OpcodeInstr::mksimple(0xc4, 8, "ISNAN", exec_is_nan)) + .insert(OpcodeInstr::mksimple(0xc5, 8, "CHKNAN", exec_chk_nan)); + cp0.insert(OpcodeInstr::mksimple(0xb7b8, 16, "QSGN", std::bind(exec_sgn, _1, 0x987, true, "QSGN"))) + .insert(OpcodeInstr::mksimple(0xb7b9, 16, "QLESS", std::bind(exec_cmp, _1, 0x887, true, "QLESS"))) + .insert(OpcodeInstr::mksimple(0xb7ba, 16, "QEQUAL", std::bind(exec_cmp, _1, 0x878, true, "QEQUAL"))) + .insert(OpcodeInstr::mksimple(0xb7bb, 16, "QLEQ", std::bind(exec_cmp, _1, 0x877, true, "QLEQ"))) + .insert(OpcodeInstr::mksimple(0xb7bc, 16, "QGREATER", std::bind(exec_cmp, _1, 0x788, true, "QGREATER"))) + .insert(OpcodeInstr::mksimple(0xb7bd, 16, "QNEQ", std::bind(exec_cmp, _1, 0x787, true, "QNEQ"))) + .insert(OpcodeInstr::mksimple(0xb7be, 16, "QGEQ", std::bind(exec_cmp, _1, 0x778, true, "QGEQ"))) + .insert(OpcodeInstr::mksimple(0xb7bf, 16, "QCMP", std::bind(exec_cmp, _1, 0x987, true, "QCMP"))) + .insert(OpcodeInstr::mkfixed(0xb7c0, 16, 8, std::bind(dump_op_tinyint8, "QEQINT ", _1, _2), + std::bind(exec_cmp_int, _1, _2, 0x878, true, "QEQ"))) + .insert(OpcodeInstr::mkfixed(0xb7c1, 16, 8, std::bind(dump_op_tinyint8, "QLESSINT ", _1, _2), + std::bind(exec_cmp_int, _1, _2, 0x887, true, "QLESS"))) + .insert(OpcodeInstr::mkfixed(0xb7c2, 16, 8, std::bind(dump_op_tinyint8, "QGTINT ", _1, _2), + std::bind(exec_cmp_int, _1, _2, 0x788, true, "QGT"))) + .insert(OpcodeInstr::mkfixed(0xb7c3, 16, 8, std::bind(dump_op_tinyint8, "QNEQINT ", _1, _2), + std::bind(exec_cmp_int, _1, _2, 0x787, true, "QNEQ"))); +} + +void register_arith_ops(OpcodeTable& cp0) { + register_int_const_ops(cp0); + register_add_mul_ops(cp0); + register_div_ops(cp0); + register_shift_logic_ops(cp0); + register_other_arith_ops(cp0); + register_int_cmp_ops(cp0); +} + +} // namespace vm diff --git a/crypto/vm/arithops.h b/crypto/vm/arithops.h new file mode 100644 index 00000000..1395d8b4 --- /dev/null +++ b/crypto/vm/arithops.h @@ -0,0 +1,27 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +namespace vm { + +class OpcodeTable; + +void register_arith_ops(OpcodeTable& cp0); + +} // namespace vm diff --git a/crypto/vm/atom.cpp b/crypto/vm/atom.cpp new file mode 100644 index 00000000..1b654fa1 --- /dev/null +++ b/crypto/vm/atom.cpp @@ -0,0 +1,97 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "atom.h" + +namespace vm { +using td::Ref; + +std::atomic Atom::hashtable[hashtable_size] = {}; +std::atomic Atom::atoms_defined{0}; +std::atomic Atom::anon_atoms{0}; + +void Atom::print_to(std::ostream& os) const { + if (name_.empty()) { + os << "atom#" << index_; + } else { + os << name_; + } +} + +std::string Atom::make_name() const { + char buffer[16]; + sprintf(buffer, "atom#%d", index_); + return buffer; +} + +std::ostream& operator<<(std::ostream& os, const Atom& atom) { + atom.print_to(os); + return os; +} + +std::ostream& operator<<(std::ostream& os, Ref atom_ref) { + atom_ref->print_to(os); + return os; +} + +std::pair Atom::compute_hash(td::Slice name) { + unsigned h1 = 1, h2 = 1; + for (std::size_t i = 0; i < name.size(); i++) { + h1 = (239 * h1 + (unsigned char)name[i]) % hashtable_size; + h2 = (17 * h2 + (unsigned char)name[i]) % (hashtable_size - 1); + } + return std::make_pair(h1, h2 + 1); +} + +Ref Atom::find(td::Slice name, bool create) { + auto hash = compute_hash(name); + while (true) { + auto& pos = hashtable[hash.first]; + Atom* ptr = pos.load(std::memory_order_acquire); + if (ptr) { + if (ptr->name_as_slice() == name) { + return Ref(ptr); + } + } else if (!create) { + return {}; + } else { + Atom* p2 = new Atom(name.str(), hash.first); + Atom* p1 = nullptr; + if (pos.compare_exchange_strong(p1, p2)) { + atoms_defined.fetch_add(1, std::memory_order_relaxed); + return Ref(p2); + } + delete p2; + CHECK(p1); + if (p1->name_as_slice() == name) { + return Ref(p1); + } + } + hash.first += hash.second; + if (hash.first >= hashtable_size) { + hash.first -= hashtable_size; + } + } +} + +Ref Atom::anon() { + int c = anon_atoms.fetch_add(1, std::memory_order_relaxed); + return Ref{true, "", ~c}; +} + +} // namespace vm diff --git a/crypto/vm/atom.h b/crypto/vm/atom.h new file mode 100644 index 00000000..08e40632 --- /dev/null +++ b/crypto/vm/atom.h @@ -0,0 +1,70 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once +#include +#include "vm/stack.hpp" +#include "td/utils/Slice.h" + +namespace vm { +using td::Ref; + +class Atom : public td::CntObject { + std::string name_; + int index_; + static constexpr unsigned hashtable_size = 170239; + static std::atomic hashtable[hashtable_size]; + static std::atomic atoms_defined; + static std::atomic anon_atoms; + static std::pair compute_hash(td::Slice name); + + public: + Atom(const Atom&) = delete; + Atom &operator=(const Atom&) = delete; + Atom(Atom&&) = delete; + Atom &operator=(Atom&&) = delete; + ~Atom() override = default; + Atom(std::string name, int index) : name_(name), index_(index) { + } + static Ref anon(); + static Ref find(td::Slice name, bool create = false); + static Ref create(td::Slice name) { + return find(std::move(name), true); + } + std::string name() const { + return name_; + } + std::string name_ext() const { + return name_.empty() ? make_name() : name_; + } + td::Slice name_as_slice() const { + return td::Slice{name_}; + } + int index() const { + return index_; + } + void print_to(std::ostream& os) const; + + private: + std::string make_name() const; +}; + +std::ostream& operator<<(std::ostream& os, const Atom& atom); +std::ostream& operator<<(std::ostream& os, Ref atom_ref); + +} // namespace vm diff --git a/crypto/vm/boc.cpp b/crypto/vm/boc.cpp new file mode 100644 index 00000000..46c8fe31 --- /dev/null +++ b/crypto/vm/boc.cpp @@ -0,0 +1,1133 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include +#include +#include +#include "vm/boc.h" +#include "vm/cells.h" +#include "vm/cellslice.h" +#include "td/utils/bits.h" +#include "td/utils/Slice-decl.h" +#include "td/utils/format.h" +#include "td/utils/crypto.h" + +namespace vm { +using td::Ref; + +td::Status CellSerializationInfo::init(td::Slice data, int ref_byte_size) { + if (data.size() < 2) { + return td::Status::Error(PSLICE() << "Not enough bytes " << td::tag("got", data.size()) + << td::tag("expected", "at least 2")); + } + TRY_STATUS(init(data.ubegin()[0], data.ubegin()[1], ref_byte_size)); + if (data.size() < end_offset) { + return td::Status::Error(PSLICE() << "Not enough bytes " << td::tag("got", data.size()) + << td::tag("expected", end_offset)); + } + return td::Status::OK(); +} + +td::Status CellSerializationInfo::init(td::uint8 d1, td::uint8 d2, int ref_byte_size) { + refs_cnt = d1 & 7; + level_mask = Cell::LevelMask(d1 >> 5); + special = (d1 & 8) != 0; + with_hashes = (d1 & 16) != 0; + + if (refs_cnt > 4) { + if (refs_cnt != 7 || !with_hashes) { + return td::Status::Error("Invalid first byte"); + } + refs_cnt = 0; + // ... + // do not deserialize absent cells! + return td::Status::Error("TODO: absent cells"); + } + + hashes_offset = 2; + auto n = level_mask.get_hashes_count(); + depth_offset = hashes_offset + (with_hashes ? n * Cell::hash_bytes : 0); + data_offset = depth_offset + (with_hashes ? n * Cell::depth_bytes : 0); + data_len = (d2 >> 1) + (d2 & 1); + data_with_bits = (d2 & 1) != 0; + refs_offset = data_offset + data_len; + end_offset = refs_offset + refs_cnt * ref_byte_size; + + return td::Status::OK(); +} + +td::Result CellSerializationInfo::get_bits(td::Slice cell) const { + if (data_with_bits) { + DCHECK(data_len != 0); + int last = cell[data_offset + data_len - 1]; + if (!last || last == 0x80) { + return td::Status::Error("overlong encoding"); + } + return td::narrow_cast((data_len - 1) * 8 + 7 - td::count_trailing_zeroes_non_zero32(last)); + } else { + return td::narrow_cast(data_len * 8); + } +} + +// TODO: check usage when result is empty +td::Result> CellSerializationInfo::create_data_cell(td::Slice cell_slice, + td::Span> refs) const { + CellBuilder cb; + TRY_RESULT(bits, get_bits(cell_slice)); + cb.store_bits(cell_slice.ubegin() + data_offset, bits); + DCHECK(refs_cnt == (td::int64)refs.size()); + for (int k = 0; k < refs_cnt; k++) { + cb.store_ref(std::move(refs[k])); + } + auto res = cb.finalize_novm(special); + if (res.is_null()) { + return td::Status::Error("CellBuilder::finalize failed"); + } + if (res->is_special() != special) { + return td::Status::Error("is_special mismatch"); + } + if (res->get_level_mask() != level_mask) { + return td::Status::Error("level mask mismatch"); + } + //return res; + if (with_hashes) { + auto hash_n = level_mask.get_hashes_count(); + if (res->get_hash().as_slice() != + cell_slice.substr(hashes_offset + Cell::hash_bytes * (hash_n - 1), Cell::hash_bytes)) { + return td::Status::Error("representation hash mismatch"); + } + if (res->get_depth() != + DataCell::load_depth( + cell_slice.substr(depth_offset + Cell::depth_bytes * (hash_n - 1), Cell::depth_bytes).ubegin())) { + return td::Status::Error("depth mismatch"); + } + + bool check_all_hashes = true; + for (unsigned level_i = 0, hash_i = 0, level = level_mask.get_level(); check_all_hashes && level_i < level; + level_i++) { + if (!level_mask.is_significant(level_i)) { + continue; + } + if (cell_slice.substr(hashes_offset + Cell::hash_bytes * hash_i, Cell::hash_bytes) != + res->get_hash(level_i).as_slice()) { + // hash mismatch + return td::Status::Error("lower hash mismatch"); + } + if (res->get_depth(level_i) != + DataCell::load_depth( + cell_slice.substr(depth_offset + Cell::depth_bytes * hash_i, Cell::depth_bytes).ubegin())) { + return td::Status::Error("lower depth mismatch"); + } + hash_i++; + } + } + return res; +} + +void BagOfCells::clear() { + cells_clear(); + roots.clear(); + root_count = 0; + serialized.clear(); +} + +int BagOfCells::set_roots(const std::vector>& new_roots) { + clear(); + return add_roots(new_roots); +} + +int BagOfCells::set_root(td::Ref new_root) { + clear(); + return add_root(std::move(new_root)); +} + +int BagOfCells::add_roots(const std::vector>& add_roots) { + int res = 0; + for (td::Ref root : add_roots) { + res += add_root(std::move(root)); + } + return res; +} + +int BagOfCells::add_root(td::Ref add_root) { + if (add_root.is_null()) { + return 0; + } + LOG_CHECK(add_root->get_virtualization() == 0) << "TODO: support serialization of virtualized cells"; + //const Cell::Hash& hash = add_root->get_hash(); + //for (const auto& root_info : roots) { + //if (root_info.cell->get_hash() == hash) { + //return 0; + //} + //} + roots.emplace_back(std::move(add_root), -1); + ++root_count; + cells_clear(); + return 1; +} + +td::Status BagOfCells::import_cells() { + cells_clear(); + for (auto& root : roots) { + auto res = import_cell(root.cell, 0); + if (res.is_error()) { + return res.move_as_error(); + } + root.idx = res.move_as_ok(); + } + //LOG(INFO) << "[cells: " << cell_count << ", refs: " << int_refs << ", bytes: " << data_bytes << "]"; + reorder_cells(); + //LOG(INFO) << "[cells: " << cell_count << ", refs: " << int_refs << ", bytes: " << data_bytes + //<< ", internal hashes: " << int_hashes << ", top hashes: " << top_hashes << "]"; + CHECK(cell_count != 0); + return td::Status::OK(); +} + +td::Result BagOfCells::import_cell(td::Ref cell, int depth) { + if (depth > max_depth) { + return td::Status::Error("error while importing a cell into a bag of cells: cell depth too large"); + } + if (cell.is_null()) { + return td::Status::Error("error while importing a cell into a bag of cells: cell is null"); + } + auto it = cells.find(cell->get_hash()); + if (it != cells.end()) { + auto pos = it->second; + cell_list_[pos].should_cache = true; + return pos; + } + if (cell->get_virtualization() != 0) { + return td::Status::Error( + "error while importing a cell into a bag of cells: cell has non-zero virtualization level"); + } + auto r_loaded_dc = cell->load_cell(); + if (r_loaded_dc.is_error()) { + return td::Status::Error("error while importing a cell into a bag of cells: " + + r_loaded_dc.move_as_error().to_string()); + } + auto loaded_dc = r_loaded_dc.move_as_ok(); + CellSlice cs(std::move(loaded_dc)); + std::array refs{-1}; + DCHECK(cs.size_refs() <= 4); + unsigned sum_child_wt = 1; + for (unsigned i = 0; i < cs.size_refs(); i++) { + auto ref = import_cell(cs.prefetch_ref(i), depth + 1); + if (ref.is_error()) { + return ref.move_as_error(); + } + refs[i] = ref.move_as_ok(); + sum_child_wt += cell_list_[refs[i]].wt; + ++int_refs; + } + DCHECK(cell_list_.size() == static_cast(cell_count)); + auto dc = cs.move_as_loaded_cell().data_cell; + auto res = cells.emplace(dc->get_hash(), cell_count); + DCHECK(res.second); + cell_list_.emplace_back(dc, dc->size_refs(), refs); + CellInfo& dc_info = cell_list_.back(); + dc_info.hcnt = static_cast(dc->get_level_mask().get_hashes_count()); + dc_info.wt = static_cast(std::min(0xffU, sum_child_wt)); + dc_info.new_idx = -1; + data_bytes += dc->get_serialized_size(); + return cell_count++; +} + +void BagOfCells::reorder_cells() { + int_hashes = 0; + for (int i = cell_count - 1; i >= 0; --i) { + CellInfo& dci = cell_list_[i]; + int s = dci.ref_num, c = s, sum = max_cell_whs - 1, mask = 0; + for (int j = 0; j < s; ++j) { + CellInfo& dcj = cell_list_[dci.ref_idx[j]]; + int limit = (max_cell_whs - 1 + j) / s; + if (dcj.wt <= limit) { + sum -= dcj.wt; + --c; + mask |= (1 << j); + } + } + if (c) { + for (int j = 0; j < s; ++j) { + if (!(mask & (1 << j))) { + CellInfo& dcj = cell_list_[dci.ref_idx[j]]; + int limit = sum++ / c; + if (dcj.wt > limit) { + dcj.wt = static_cast(limit); + } + } + } + } + } + for (int i = 0; i < cell_count; i++) { + CellInfo& dci = cell_list_[i]; + int s = dci.ref_num, sum = 1; + for (int j = 0; j < s; ++j) { + sum += cell_list_[dci.ref_idx[j]].wt; + } + DCHECK(sum <= max_cell_whs); + if (sum <= dci.wt) { + dci.wt = static_cast(sum); + } else { + dci.wt = 0; + int_hashes += dci.hcnt; + } + } + top_hashes = 0; + for (auto& root_info : roots) { + auto& cell_info = cell_list_[root_info.idx]; + if (cell_info.is_root_cell) { + cell_info.is_root_cell = true; + if (cell_info.wt) { + top_hashes += cell_info.hcnt; + } + } + } + if (cell_count > 0) { + rv_idx = 0; + cell_list_tmp.clear(); + cell_list_tmp.reserve(cell_count); + + for (const auto& root_info : roots) { + revisit(root_info.idx, 0); + revisit(root_info.idx, 1); + } + for (const auto& root_info : roots) { + revisit(root_info.idx, 2); + } + for (auto& root_info : roots) { + root_info.idx = cell_list_[root_info.idx].new_idx; + } + + DCHECK(rv_idx == cell_count); + //DCHECK(cell_list.back().new_idx == cell_count - 1); + DCHECK(cell_list_.size() == cell_list_tmp.size()); + cell_list_ = std::move(cell_list_tmp); + cell_list_tmp.clear(); + } +} + +// force=0 : previsit (recursively until special cells are found; then visit them) +// force=1 : visit (allocate and process all children) +// force=2 : allocate (assign a new index; can be run only after visiting) +int BagOfCells::revisit(int cell_idx, int force) { + DCHECK(cell_idx >= 0 && cell_idx < cell_count); + CellInfo& dci = cell_list_[cell_idx]; + if (dci.new_idx >= 0) { + return dci.new_idx; + } + if (!force) { + // previsit + if (dci.new_idx != -1) { + // already previsited or visited + return dci.new_idx; + } + int n = dci.ref_num; + for (int j = n - 1; j >= 0; --j) { + int child_idx = dci.ref_idx[j]; + // either previsit or visit child, depending on whether it is special + revisit(dci.ref_idx[j], cell_list_[child_idx].is_special()); + } + return dci.new_idx = -2; // mark as previsited + } + if (force > 1) { + // time to allocate + auto i = dci.new_idx = rv_idx++; + cell_list_tmp.emplace_back(std::move(dci)); + return i; + } + if (dci.new_idx == -3) { + // already visited + return dci.new_idx; + } + if (dci.is_special()) { + // if current cell is special, previsit it first + revisit(cell_idx, 0); + } + // visit children + int n = dci.ref_num; + for (int j = n - 1; j >= 0; --j) { + revisit(dci.ref_idx[j], 1); + } + // allocate children + for (int j = n - 1; j >= 0; --j) { + dci.ref_idx[j] = revisit(dci.ref_idx[j], 2); + } + return dci.new_idx = -3; // mark as visited (and all children processed) +} + +td::uint64 BagOfCells::compute_sizes(int mode, int& r_size, int& o_size) { + int rs = 0, os = 0; + if (!root_count || !data_bytes) { + r_size = o_size = 0; + return 0; + } + while (cell_count >= (1 << (rs << 3))) { + rs++; + } + td::uint64 hashes = + (((mode & Mode::WithTopHash) ? top_hashes : 0) + ((mode & Mode::WithIntHashes) ? int_hashes : 0)) * + (Cell::hash_bytes + Cell::depth_bytes); + td::uint64 data_bytes_adj = data_bytes + (unsigned long long)int_refs * rs + hashes; + td::uint64 max_offset = (mode & Mode::WithCacheBits) ? data_bytes_adj * 2 : data_bytes_adj; + while (max_offset >= (1ULL << (os << 3))) { + os++; + } + if (rs > 4 || os > 8) { + r_size = o_size = 0; + return 0; + } + if (!(mode & Mode::WithIndex)) { + if (rs > 2 || os > 4) { + rs = std::max(3, rs); + os = 8; + data_bytes_adj = data_bytes + (unsigned long long)int_refs * rs + hashes; + } else { + os = 4; + } + } + r_size = rs; + o_size = os; + return data_bytes_adj; +} + +std::size_t BagOfCells::estimate_serialized_size(int mode) { + if ((mode & Mode::WithCacheBits) && !(mode & Mode::WithIndex)) { + info.invalidate(); + return 0; + } + auto data_bytes_adj = compute_sizes(mode, info.ref_byte_size, info.offset_byte_size); + if (!data_bytes_adj) { + info.invalidate(); + return 0; + } + info.valid = true; + info.has_crc32c = mode & Mode::WithCRC32C; + info.has_index = mode & Mode::WithIndex; + info.has_cache_bits = mode & Mode::WithCacheBits; + info.root_count = root_count; + info.cell_count = cell_count; + info.absent_count = dangle_count; + int crc_size = info.has_crc32c ? 4 : 0; + info.roots_offset = 4 + 1 + 1 + 3 * info.ref_byte_size + info.offset_byte_size; + info.index_offset = info.roots_offset + info.root_count * info.ref_byte_size; + info.data_offset = info.index_offset; + if (info.has_index) { + info.data_offset += (long long)cell_count * info.offset_byte_size; + } + info.magic = Info::boc_generic; + info.data_size = data_bytes_adj; + info.total_size = info.data_offset + data_bytes_adj + crc_size; + auto res = td::narrow_cast_safe(info.total_size); + if (res.is_error()) { + return 0; + } + return res.ok(); +} + +BagOfCells& BagOfCells::serialize(int mode) { + std::size_t size_est = estimate_serialized_size(mode); + if (!size_est) { + serialized.clear(); + return *this; + } + serialized.resize(size_est); + if (serialize_to(const_cast(serialized.data()), serialized.size(), mode) != size_est) { + serialized.clear(); + } + return *this; +} + +std::string BagOfCells::serialize_to_string(int mode) { + std::size_t size_est = estimate_serialized_size(mode); + if (!size_est) { + return {}; + } + std::string res; + res.resize(size_est, 0); + if (serialize_to(const_cast(reinterpret_cast(res.data())), res.size(), mode) == + res.size()) { + return res; + } else { + return {}; + } +} + +td::Result BagOfCells::serialize_to_slice(int mode) { + std::size_t size_est = estimate_serialized_size(mode); + if (!size_est) { + return td::Status::Error("no cells to serialize to this bag of cells"); + } + td::BufferSlice res(size_est); + if (serialize_to(const_cast(reinterpret_cast(res.data())), res.size(), mode) == + res.size()) { + return std::move(res); + } else { + return td::Status::Error("error while serializing a bag of cells: actual serialized size differs from estimated"); + } +} + +std::string BagOfCells::extract_string() const { + return std::string{serialized.data(), serialized.data() + serialized.size()}; +} + +void BagOfCells::store_uint(unsigned long long value, unsigned bytes) { + unsigned char* ptr = store_ptr += bytes; + store_chk(); + while (bytes) { + *--ptr = value & 0xff; + value >>= 8; + --bytes; + } + DCHECK(!bytes); +} + +//serialized_boc#672fb0ac has_idx:(## 1) has_crc32c:(## 1) +// has_cache_bits:(## 1) flags:(## 2) { flags = 0 } +// size:(## 3) { size <= 4 } +// off_bytes:(## 8) { off_bytes <= 8 } +// cells:(##(size * 8)) +// roots:(##(size * 8)) +// absent:(##(size * 8)) { roots + absent <= cells } +// tot_cells_size:(##(off_bytes * 8)) +// index:(cells * ##(off_bytes * 8)) +// cell_data:(tot_cells_size * [ uint8 ]) +// = BagOfCells; +std::size_t BagOfCells::serialize_to(unsigned char* buffer, std::size_t buff_size, int mode) { + std::size_t size_est = estimate_serialized_size(mode); + if (!size_est || size_est > buff_size) { + return 0; + } + init_store(buffer, buffer + size_est); + store_uint(info.magic, 4); + + td::uint8 byte{0}; + if (info.has_index) { + byte |= 1 << 7; + } + if (info.has_crc32c) { + byte |= 1 << 6; + } + if (info.has_cache_bits) { + byte |= 1 << 5; + } + // 3, 4 - flags + if (info.ref_byte_size < 1 || info.ref_byte_size > 7) { + return 0; + } + byte |= static_cast(info.ref_byte_size); + store_uint(byte, 1); + + store_uint(info.offset_byte_size, 1); + store_ref(cell_count); + store_ref(root_count); + store_ref(0); + store_offset(info.data_size); + for (const auto& root_info : roots) { + int k = cell_count - 1 - root_info.idx; + DCHECK(k >= 0 && k < cell_count); + store_ref(k); + } + DCHECK(store_ptr - buffer == (long long)info.index_offset); + DCHECK((unsigned)cell_count == cell_list_.size()); + if (info.has_index) { + std::size_t offs = 0; + for (int i = cell_count - 1; i >= 0; --i) { + const Ref& dc = cell_list_[i].dc_ref; + bool with_hash = (mode & Mode::WithIntHashes) && !cell_list_[i].wt; + if (cell_list_[i].is_root_cell && (mode & Mode::WithTopHash)) { + with_hash = true; + } + offs += dc->get_serialized_size(with_hash) + dc->size_refs() * info.ref_byte_size; + auto fixed_offset = offs; + if (info.has_cache_bits) { + fixed_offset = offs * 2 + cell_list_[i].should_cache; + } + store_offset(fixed_offset); + } + DCHECK(offs == info.data_size); + } + DCHECK(store_ptr - buffer == (long long)info.data_offset); + unsigned char* keep_ptr = store_ptr; + for (int i = 0; i < cell_count; ++i) { + const auto& dc_info = cell_list_[cell_count - 1 - i]; + const Ref& dc = dc_info.dc_ref; + bool with_hash = (mode & Mode::WithIntHashes) && !dc_info.wt; + if (dc_info.is_root_cell && (mode & Mode::WithTopHash)) { + with_hash = true; + } + int s = dc->serialize(store_ptr, 256, with_hash); + store_ptr += s; + store_chk(); + DCHECK(dc->size_refs() == dc_info.ref_num); + // std::cerr << (dc_info.is_special() ? '*' : ' ') << i << '<' << (int)dc_info.wt << ">:"; + for (unsigned j = 0; j < dc_info.ref_num; ++j) { + int k = cell_count - 1 - dc_info.ref_idx[j]; + DCHECK(k > i && k < cell_count); + store_ref(k); + // std::cerr << ' ' << k; + } + // std::cerr << std::endl; + } + store_chk(); + DCHECK(store_ptr - keep_ptr == (long long)info.data_size); + DCHECK(store_end - store_ptr == (info.has_crc32c ? 4 : 0)); + if (info.has_crc32c) { + // compute crc32c of buffer .. store_ptr + unsigned crc = td::crc32c(td::Slice{buffer, store_ptr}); + store_uint(td::bswap32(crc), 4); + } + DCHECK(store_empty()); + return store_ptr - buffer; +} + +unsigned long long BagOfCells::Info::read_int(const unsigned char* ptr, unsigned bytes) { + unsigned long long res = 0; + while (bytes > 0) { + res = (res << 8) + *ptr++; + --bytes; + } + return res; +} + +void BagOfCells::Info::write_int(unsigned char* ptr, unsigned long long value, int bytes) { + ptr += bytes; + while (bytes) { + *--ptr = value & 0xff; + value >>= 8; + --bytes; + } + DCHECK(!bytes); +} + +long long BagOfCells::Info::parse_serialized_header(const td::Slice& slice) { + invalidate(); + int sz = static_cast(std::min(slice.size(), static_cast(0xffff))); + if (sz < 4) { + return -10; // want at least 10 bytes + } + const unsigned char* ptr = slice.ubegin(); + magic = (unsigned)read_int(ptr, 4); + has_crc32c = false; + has_index = false; + has_cache_bits = false; + ref_byte_size = 0; + offset_byte_size = 0; + root_count = cell_count = absent_count = -1; + index_offset = data_offset = data_size = total_size = 0; + if (magic != boc_generic && magic != boc_idx && magic != boc_idx_crc32c) { + magic = 0; + return 0; + } + if (sz < 5) { + return -10; + } + td::uint8 byte = ptr[4]; + if (magic == boc_generic) { + has_index = (byte >> 7) % 2 == 1; + has_crc32c = (byte >> 6) % 2 == 1; + has_cache_bits = (byte >> 5) % 2 == 1; + } else { + has_index = true; + has_crc32c = magic == boc_idx_crc32c; + } + if (has_cache_bits && !has_index) { + return 0; + } + ref_byte_size = byte & 7; + if (ref_byte_size > 4 || ref_byte_size < 1) { + return 0; + } + if (sz < 6) { + return -7 - 3 * ref_byte_size; + } + offset_byte_size = ptr[5]; + if (offset_byte_size > 8 || offset_byte_size < 1) { + return 0; + } + roots_offset = 6 + 3 * ref_byte_size + offset_byte_size; + ptr += 6; + sz -= 6; + if (sz < ref_byte_size) { + return -(int)roots_offset; + } + cell_count = (int)read_ref(ptr); + if (cell_count < 0) { + cell_count = -1; + return 0; + } + if (sz < 2 * ref_byte_size) { + return -static_cast(roots_offset); + } + root_count = (int)read_ref(ptr + ref_byte_size); + if (root_count <= 0) { + root_count = -1; + return 0; + } + index_offset = roots_offset; + if (magic == boc_generic) { + index_offset += root_count * ref_byte_size; + has_roots = true; + } else { + if (root_count != 1) { + return 0; + } + } + data_offset = index_offset; + if (has_index) { + data_offset += (long long)cell_count * offset_byte_size; + } + if (sz < 3 * ref_byte_size) { + return -static_cast(roots_offset); + } + absent_count = (int)read_ref(ptr + 2 * ref_byte_size); + if (absent_count < 0 || absent_count > cell_count) { + return 0; + } + if (sz < 3 * ref_byte_size + offset_byte_size) { + return -(int)roots_offset; + } + data_size = read_offset(ptr + 3 * ref_byte_size); + if (data_size > ((unsigned long long)cell_count << 10)) { + return 0; + } + valid = true; + total_size = data_offset + data_size + (has_crc32c ? 4 : 0); + return total_size; +} + +td::Result BagOfCells::get_cell_slice(int idx, td::Slice data) { + unsigned long long offs = get_idx_entry(idx - 1); + unsigned long long offs_end = get_idx_entry(idx); + if (offs > offs_end || offs_end > data.size()) { + return td::Status::Error(PSLICE() << "invalid index entry [" << offs << "; " << offs_end << "], " + << td::tag("data.size()", data.size())); + } + return data.substr(offs, offs_end - offs); +} + +td::Result> BagOfCells::deserialize_cell(int idx, td::Slice cells_slice, + td::Span> cells_span, + std::vector* cell_should_cache) { + TRY_RESULT(cell_slice, get_cell_slice(idx, cells_slice)); + std::array, 4> refs_buf; + + CellSerializationInfo cell_info; + TRY_STATUS(cell_info.init(cell_slice, info.ref_byte_size)); + if (cell_info.end_offset != cell_slice.size()) { + return td::Status::Error("unused space in cell serialization"); + } + + auto refs = td::MutableSpan>(refs_buf).substr(0, cell_info.refs_cnt); + for (int k = 0; k < cell_info.refs_cnt; k++) { + int ref_idx = (int)info.read_ref(cell_slice.ubegin() + cell_info.refs_offset + k * info.ref_byte_size); + if (ref_idx <= idx) { + return td::Status::Error(PSLICE() << "bag-of-cells error: reference #" << k << " of cell #" << idx + << " is to cell #" << ref_idx << " with smaller index"); + } + if (ref_idx >= cell_count) { + return td::Status::Error(PSLICE() << "bag-of-cells error: reference #" << k << " of cell #" << idx + << " is to non-existent cell #" << ref_idx << ", only " << cell_count + << " cells are defined"); + } + refs[k] = cells_span[cell_count - ref_idx - 1]; + if (cell_should_cache) { + auto& cnt = (*cell_should_cache)[ref_idx]; + if (cnt < 2) { + cnt++; + } + } + } + + return cell_info.create_data_cell(cell_slice, refs); +} + +td::Result BagOfCells::deserialize(const td::Slice& data) { + clear(); + long long size_est = info.parse_serialized_header(data); + //LOG(INFO) << "estimated size " << size_est << ", true size " << data.size(); + if (size_est == 0) { + return td::Status::Error(PSLICE() << "cannot deserialize bag-of-cells: invalid header, error " << size_est); + } + + if (size_est < 0) { + //LOG(ERROR) << "cannot deserialize bag-of-cells: not enough bytes (" << data.size() << " present, " << -size_est + //<< " required)"; + return size_est; + } + + if (size_est > (long long)data.size()) { + //LOG(ERROR) << "cannot deserialize bag-of-cells: not enough bytes (" << data.size() << " present, " << size_est + //<< " required)"; + return -size_est; + } + //LOG(INFO) << "estimated size " << size_est << ", true size " << data.size(); + if (info.has_crc32c) { + unsigned crc_computed = td::crc32c(td::Slice{data.ubegin(), data.uend() - 4}); + unsigned crc_stored = td::as(data.uend() - 4); + if (crc_computed != crc_stored) { + return td::Status::Error(PSLICE() << "bag-of-cells CRC32C mismatch: expected " << td::format::as_hex(crc_computed) + << ", found " << td::format::as_hex(crc_stored)); + } + } + + cell_count = info.cell_count; + std::vector cell_should_cache; + if (info.has_cache_bits) { + cell_should_cache.resize(cell_count, 0); + } + roots.clear(); + roots.resize(info.root_count); + auto* roots_ptr = data.substr(info.roots_offset).ubegin(); + for (int i = 0; i < info.root_count; i++) { + int idx = 0; + if (info.has_roots) { + idx = (int)info.read_ref(roots_ptr + i * info.ref_byte_size); + } + if (idx < 0 || idx >= info.cell_count) { + return td::Status::Error(PSLICE() << "bag-of-cells invalid root index " << idx); + } + roots[i].idx = info.cell_count - idx - 1; + if (info.has_cache_bits) { + auto& cnt = cell_should_cache[idx]; + if (cnt < 2) { + cnt++; + } + } + } + if (info.has_index) { + index_ptr = data.substr(info.index_offset).ubegin(); + // TODO: should we validate index here + } else { + index_ptr = nullptr; + unsigned long long cur = 0; + custom_index.reserve(info.cell_count); + + auto cells_slice = data.substr(info.data_offset, info.data_size); + + for (int i = 0; i < info.cell_count; i++) { + CellSerializationInfo cell_info; + auto status = cell_info.init(cells_slice, info.ref_byte_size); + if (status.is_error()) { + return td::Status::Error(PSLICE() + << "invalid bag-of-cells failed to deserialize cell #" << i << " " << status.error()); + } + cells_slice = cells_slice.substr(cell_info.end_offset); + cur += cell_info.end_offset; + custom_index.push_back(cur); + } + if (!cells_slice.empty()) { + return td::Status::Error(PSLICE() << "invalid bag-of-cells last cell #" << info.cell_count - 1 << ": end offset " + << cur << " is different from total data size " << info.data_size); + } + } + auto cells_slice = data.substr(info.data_offset, info.data_size); + std::vector> cell_list; + cell_list.reserve(cell_count); + std::array, 4> refs_buf; + for (int i = 0; i < cell_count; i++) { + // reconstruct cell with index cell_count - 1 - i + int idx = cell_count - 1 - i; + auto r_cell = deserialize_cell(idx, cells_slice, cell_list, info.has_cache_bits ? &cell_should_cache : nullptr); + if (r_cell.is_error()) { + return td::Status::Error(PSLICE() << "invalid bag-of-cells failed to deserialize cell #" << idx << " " + << r_cell.error()); + } + cell_list.push_back(r_cell.move_as_ok()); + DCHECK(cell_list.back().not_null()); + } + if (info.has_cache_bits) { + for (int idx = 0; idx < cell_count; idx++) { + auto should_cache = cell_should_cache[idx] > 1; + auto stored_should_cache = get_cache_entry(idx); + if (should_cache != stored_should_cache) { + return td::Status::Error(PSLICE() << "invalid bag-of-cells cell #" << idx << " has wrong cache flag " + << stored_should_cache); + } + } + } + custom_index.clear(); + index_ptr = nullptr; + root_count = info.root_count; + dangle_count = info.absent_count; + for (auto& root_info : roots) { + root_info.cell = cell_list[root_info.idx]; + } + cell_list.clear(); + return size_est; +} + +unsigned long long BagOfCells::get_idx_entry(int index) { + auto raw = get_idx_entry_raw(index); + if (info.has_cache_bits) { + raw /= 2; + } + return raw; +} + +bool BagOfCells::get_cache_entry(int index) { + if (!info.has_cache_bits) { + return true; + } + if (!info.has_index) { + return true; + } + auto raw = get_idx_entry_raw(index); + return raw % 2 == 1; +} + +unsigned long long BagOfCells::get_idx_entry_raw(int index) { + if (index < 0) { + return 0; + } + if (!info.has_index) { + return custom_index.at(index); + } else if (index < info.cell_count && index_ptr) { + return info.read_offset(index_ptr + (long)index * info.offset_byte_size); + } else { + // throw ? + return 0; + } +} + +/* + * + * Simple BoC serialization/deserialization functions + * + */ + +td::Result> std_boc_deserialize(td::Slice data, bool can_be_empty) { + if (data.empty() && can_be_empty) { + return Ref(); + } + BagOfCells boc; + auto res = boc.deserialize(data); + if (res.is_error()) { + return res.move_as_error(); + } + if (boc.get_root_count() != 1) { + return td::Status::Error("bag of cells is expected to have exactly one root"); + } + auto root = boc.get_root_cell(); + if (root.is_null()) { + return td::Status::Error("bag of cells has null root cell (?)"); + } + if (root->get_level() != 0) { + return td::Status::Error("bag of cells has a root with non-zero level"); + } + return std::move(root); +} + +td::Result>> std_boc_deserialize_multi(td::Slice data) { + if (data.empty()) { + return std::vector>{}; + } + BagOfCells boc; + auto res = boc.deserialize(data); + if (res.is_error()) { + return res.move_as_error(); + } + int n = boc.get_root_count(); + std::vector> roots; + for (int i = 0; i < n; i++) { + auto root = boc.get_root_cell(i); + if (root.is_null()) { + return td::Status::Error("bag of cells has a null root cell (?)"); + } + if (root->get_level() != 0) { + return td::Status::Error("bag of cells has a root with non-zero level"); + } + roots.emplace_back(std::move(root)); + } + return std::move(roots); +} + +td::Result std_boc_serialize(Ref root, int mode) { + if (root.is_null()) { + return td::Status::Error("cannot serialize a null cell reference into a bag of cells"); + } + BagOfCells boc; + boc.add_root(std::move(root)); + auto res = boc.import_cells(); + if (res.is_error()) { + return res.move_as_error(); + } + return boc.serialize_to_slice(mode); +} + +td::Result std_boc_serialize_multi(std::vector> roots, int mode) { + if (roots.empty()) { + return td::BufferSlice{}; + } + BagOfCells boc; + boc.add_roots(std::move(roots)); + auto res = boc.import_cells(); + if (res.is_error()) { + return res.move_as_error(); + } + return boc.serialize_to_slice(mode); +} + +/* + * + * Cell storage statistics + * + */ + +bool CellStorageStat::compute_used_storage(Ref cs_ref, bool kill_dup, bool skip_count_root) { + clear(); + return add_used_storage(std::move(cs_ref), kill_dup, skip_count_root) && clear_seen(); +} + +bool CellStorageStat::compute_used_storage(const CellSlice& cs, bool kill_dup, bool skip_count_root) { + clear(); + return add_used_storage(cs, kill_dup, skip_count_root) && clear_seen(); +} + +bool CellStorageStat::compute_used_storage(CellSlice&& cs, bool kill_dup, bool skip_count_root) { + clear(); + return add_used_storage(std::move(cs), kill_dup, skip_count_root) && clear_seen(); +} + +bool CellStorageStat::compute_used_storage(Ref cell, bool kill_dup, bool skip_count_root) { + clear(); + return add_used_storage(std::move(cell), kill_dup, skip_count_root) && clear_seen(); +} + +bool CellStorageStat::add_used_storage(Ref cs_ref, bool kill_dup, bool skip_count_root) { + if (cs_ref->is_unique()) { + return add_used_storage(std::move(cs_ref.unique_write()), kill_dup, skip_count_root); + } else { + return add_used_storage(*cs_ref, kill_dup, skip_count_root); + } +} + +bool CellStorageStat::add_used_storage(const CellSlice& cs, bool kill_dup, bool skip_count_root) { + if (!skip_count_root) { + ++cells; + } + bits += cs.size(); + for (unsigned i = 0; i < cs.size_refs(); i++) { + if (!add_used_storage(cs.prefetch_ref(i), kill_dup)) { + return false; + } + } + return true; +} + +bool CellStorageStat::add_used_storage(CellSlice&& cs, bool kill_dup, bool skip_count_root) { + if (!skip_count_root) { + ++cells; + } + bits += cs.size(); + while (cs.size_refs()) { + if (!add_used_storage(cs.fetch_ref(), kill_dup)) { + return false; + } + } + return true; +} + +bool CellStorageStat::add_used_storage(Ref cell, bool kill_dup, bool skip_count_root) { + if (cell.is_null()) { + return false; + } + if (kill_dup) { + auto ins = seen.insert(cell->get_hash()); + if (!ins.second) { + return true; + } + } + vm::CellSlice cs{vm::NoVm{}, std::move(cell)}; + return add_used_storage(std::move(cs), kill_dup, skip_count_root); +} + +void NewCellStorageStat::add_cell(Ref cell) { + dfs(std::move(cell), true, false); +} +void NewCellStorageStat::add_proof(Ref cell, const CellUsageTree* usage_tree) { + CHECK(usage_tree); + usage_tree_ = usage_tree; + dfs(std::move(cell), false, true); +} +void NewCellStorageStat::add_cell_and_proof(Ref cell, const CellUsageTree* usage_tree) { + CHECK(usage_tree); + usage_tree_ = usage_tree; + dfs(std::move(cell), true, true); +} + +NewCellStorageStat::Stat NewCellStorageStat::tentative_add_cell(Ref cell) const { + NewCellStorageStat stat; + stat.parent_ = this; + stat.add_cell(std::move(cell)); + return stat.get_stat(); +} + +NewCellStorageStat::Stat NewCellStorageStat::tentative_add_proof(Ref cell, + const CellUsageTree* usage_tree) const { + NewCellStorageStat stat; + stat.parent_ = this; + stat.add_proof(std::move(cell), usage_tree); + return stat.get_proof_stat(); +} + +void NewCellStorageStat::dfs(Ref cell, bool need_stat, bool need_proof_stat) { + if (cell.is_null()) { + // FIXME: save error flag? + return; + } + if (need_stat) { + stat_.internal_refs++; + if ((parent_ && parent_->seen_.count(cell->get_hash()) != 0) || !seen_.insert(cell->get_hash()).second) { + need_stat = false; + } else { + stat_.cells++; + } + } + + if (need_proof_stat) { + auto tree_node = cell->get_tree_node(); + if (!tree_node.empty() && tree_node.is_from_tree(usage_tree_)) { + proof_stat_.external_refs++; + need_proof_stat = false; + } else { + proof_stat_.internal_refs++; + if ((parent_ && parent_->proof_seen_.count(cell->get_hash()) != 0) || + !proof_seen_.insert(cell->get_hash()).second) { + need_proof_stat = false; + } else { + proof_stat_.cells++; + } + } + } + + if (!need_proof_stat && !need_stat) { + return; + } + vm::CellSlice cs{vm::NoVm{}, std::move(cell)}; + if (need_stat) { + stat_.bits += cs.size(); + } + if (need_proof_stat) { + proof_stat_.bits += cs.size(); + } + while (cs.size_refs()) { + dfs(cs.fetch_ref(), need_stat, need_proof_stat); + } +} + +} // namespace vm diff --git a/crypto/vm/boc.h b/crypto/vm/boc.h new file mode 100644 index 00000000..5b4e4e2c --- /dev/null +++ b/crypto/vm/boc.h @@ -0,0 +1,314 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once +#include +#include "vm/cells.h" +#include "td/utils/Status.h" +#include "td/utils/buffer.h" +#include "td/utils/HashMap.h" + +namespace vm { +using td::Ref; + +td::Result> std_boc_deserialize(td::Slice data, bool can_be_empty = false); +td::Result std_boc_serialize(Ref root, int mode = 0); + +td::Result>> std_boc_deserialize_multi(td::Slice data); +td::Result std_boc_serialize_multi(std::vector> root, int mode = 0); + +class NewCellStorageStat { + public: + NewCellStorageStat() { + } + + struct Stat { + Stat() { + } + Stat(td::uint64 cells_, td::uint64 bits_, td::uint64 internal_refs_ = 0, td::uint64 external_refs_ = 0) + : cells(cells_), bits(bits_), internal_refs(internal_refs_), external_refs(external_refs_) { + } + td::uint64 cells{0}; + td::uint64 bits{0}; + td::uint64 internal_refs{0}; + td::uint64 external_refs{0}; + + auto key() const { + return std::make_tuple(cells, bits, internal_refs, external_refs); + } + bool operator==(const Stat& other) const { + return key() == other.key(); + } + Stat& operator=(const Stat& other) = default; + Stat& operator+=(const Stat& other) { + cells += other.cells; + bits += other.bits; + internal_refs += other.internal_refs; + external_refs += other.external_refs; + return *this; + } + Stat operator+(const Stat& other) const { + return Stat{cells + other.cells, bits + other.bits, internal_refs + other.internal_refs, + external_refs + other.external_refs}; + } + bool fits_uint32() const { + return !((cells | bits | internal_refs | external_refs) >> 32); + } + void set_zero() { + cells = bits = internal_refs = external_refs = 0; + } + }; + + Stat get_stat() const { + return stat_; + } + + Stat get_proof_stat() const { + return proof_stat_; + } + + Stat get_total_stat() const { + return stat_ + proof_stat_; + } + + void add_cell(Ref cell); + void add_proof(Ref cell, const CellUsageTree* usage_tree); + void add_cell_and_proof(Ref cell, const CellUsageTree* usage_tree); + Stat tentative_add_cell(Ref cell) const; + Stat tentative_add_proof(Ref cell, const CellUsageTree* usage_tree) const; + void set_zero() { + stat_.set_zero(); + proof_stat_.set_zero(); + } + + private: + const CellUsageTree* usage_tree_; + std::set seen_; + Stat stat_; + std::set proof_seen_; + Stat proof_stat_; + const NewCellStorageStat* parent_{nullptr}; + + void dfs(Ref cell, bool need_stat, bool need_proof_stat); +}; + +struct CellStorageStat { + unsigned long long cells; + unsigned long long bits; + unsigned long long public_cells; + std::set seen; + CellStorageStat() : cells(0), bits(0), public_cells(0) { + } + bool clear_seen() { + seen.clear(); + return true; + } + void clear() { + cells = bits = public_cells = 0; + clear_seen(); + } + bool compute_used_storage(Ref cs_ref, bool kill_dup = true, bool skip_count_root = false); + bool compute_used_storage(const CellSlice& cs, bool kill_dup = true, bool skip_count_root = false); + bool compute_used_storage(CellSlice&& cs, bool kill_dup = true, bool skip_count_root = false); + bool compute_used_storage(Ref cell, bool kill_dup = true, bool skip_count_root = false); + + bool add_used_storage(Ref cs_ref, bool kill_dup = true, bool skip_count_root = false); + bool add_used_storage(const CellSlice& cs, bool kill_dup = true, bool skip_count_root = false); + bool add_used_storage(CellSlice&& cs, bool kill_dup = true, bool skip_count_root = false); + bool add_used_storage(Ref cell, bool kill_dup = true, bool skip_count_root = false); +}; + +struct CellSerializationInfo { + bool special; + Cell::LevelMask level_mask; + + bool with_hashes; + size_t hashes_offset; + size_t depth_offset; + + size_t data_offset; + size_t data_len; + bool data_with_bits; + + size_t refs_offset; + int refs_cnt; + + size_t end_offset; + + td::Status init(td::Slice data, int ref_byte_size); + td::Status init(td::uint8 d1, td::uint8 d2, int ref_byte_size); + td::Result get_bits(td::Slice cell) const; + + td::Result> create_data_cell(td::Slice data, td::Span> refs) const; +}; + +class BagOfCells { + public: + enum { hash_bytes = vm::Cell::hash_bytes }; + enum Mode { WithIndex = 1, WithCRC32C = 2, WithTopHash = 4, WithIntHashes = 8, WithCacheBits = 16, max = 31 }; + enum { max_cell_whs = 64 }; + using Hash = Cell::Hash; + struct Info { + enum : td::uint32 { boc_idx = 0x68ff65f3, boc_idx_crc32c = 0xacc3a728, boc_generic = 0xb5ee9c72 }; + + unsigned magic; + int root_count; + int cell_count; + int absent_count; + int ref_byte_size; + int offset_byte_size; + bool valid; + bool has_index; + bool has_roots{false}; + bool has_crc32c; + bool has_cache_bits; + unsigned long long roots_offset, index_offset, data_offset, data_size, total_size; + Info() : magic(0), valid(false) { + } + void invalidate() { + valid = false; + } + long long parse_serialized_header(const td::Slice& slice); + unsigned long long read_int(const unsigned char* ptr, unsigned bytes); + unsigned long long read_ref(const unsigned char* ptr) { + return read_int(ptr, ref_byte_size); + } + unsigned long long read_offset(const unsigned char* ptr) { + return read_int(ptr, offset_byte_size); + } + void write_int(unsigned char* ptr, unsigned long long value, int bytes); + void write_ref(unsigned char* ptr, unsigned long long value) { + write_int(ptr, value, ref_byte_size); + } + void write_offset(unsigned char* ptr, unsigned long long value) { + write_int(ptr, value, offset_byte_size); + } + }; + + private: + int cell_count{0}, root_count{0}, dangle_count{0}, int_refs{0}; + int int_hashes{0}, top_hashes{0}; + int max_depth{1024}; + Info info; + unsigned long long data_bytes{0}; + unsigned char* store_ptr{nullptr}; + unsigned char* store_end{nullptr}; + td::HashMap cells; + struct CellInfo { + Ref dc_ref; + std::array ref_idx; + unsigned char ref_num; + unsigned char wt; + unsigned char hcnt; + int new_idx; + bool should_cache{false}; + bool is_root_cell{false}; + CellInfo() : ref_num(0) { + } + CellInfo(Ref _dc) : dc_ref(std::move(_dc)), ref_num(0) { + } + CellInfo(Ref _dc, int _refs, const std::array& _ref_list) + : dc_ref(std::move(_dc)), ref_idx(_ref_list), ref_num(static_cast(_refs)) { + } + bool is_special() const { + return !wt; + } + }; + std::vector cell_list_; + struct RootInfo { + RootInfo() = default; + RootInfo(Ref cell, int idx) : cell(std::move(cell)), idx(idx) { + } + Ref cell; + int idx{-1}; + }; + std::vector cell_list_tmp; + std::vector roots; + std::vector serialized; + const unsigned char* index_ptr{nullptr}; + const unsigned char* data_ptr{nullptr}; + std::vector custom_index; + + public: + void clear(); + int set_roots(const std::vector>& new_roots); + int set_root(td::Ref new_root); + int add_roots(const std::vector>& add_roots); + int add_root(td::Ref add_root); + td::Status import_cells() TD_WARN_UNUSED_RESULT; + BagOfCells() = default; + std::size_t estimate_serialized_size(int mode = 0); + BagOfCells& serialize(int mode = 0); + std::string serialize_to_string(int mode = 0); + td::Result serialize_to_slice(int mode = 0); + std::size_t serialize_to(unsigned char* buffer, std::size_t buff_size, int mode = 0); + std::string extract_string() const; + + td::Result deserialize(const td::Slice& data); + td::Result deserialize(const unsigned char* buffer, std::size_t buff_size) { + return deserialize(td::Slice{buffer, buff_size}); + } + int get_root_count() const { + return root_count; + } + Ref get_root_cell(int idx = 0) const { + return (idx >= 0 && idx < root_count) ? roots.at(idx).cell : Ref{}; + } + + static int precompute_cell_serialization_size(const unsigned char* cell, std::size_t len, int ref_size, + int* refs_num_ptr = nullptr); + + private: + int rv_idx; + td::Result import_cell(td::Ref cell, int depth); + void cells_clear() { + cell_count = 0; + int_refs = 0; + data_bytes = 0; + cells.clear(); + cell_list_.clear(); + } + td::uint64 compute_sizes(int mode, int& r_size, int& o_size); + void init_store(unsigned char* from, unsigned char* to) { + store_ptr = from; + store_end = to; + } + void store_chk() const { + DCHECK(store_ptr <= store_end); + } + bool store_empty() const { + return store_ptr == store_end; + } + void store_uint(unsigned long long value, unsigned bytes); + void store_ref(unsigned long long value) { + store_uint(value, info.ref_byte_size); + } + void store_offset(unsigned long long value) { + store_uint(value, info.offset_byte_size); + } + void reorder_cells(); + int revisit(int cell_idx, int force = 0); + unsigned long long get_idx_entry_raw(int index); + unsigned long long get_idx_entry(int index); + bool get_cache_entry(int index); + td::Result get_cell_slice(int index, td::Slice data); + td::Result> deserialize_cell(int index, td::Slice data, td::Span> cells, + std::vector* cell_should_cache); +}; + +} // namespace vm diff --git a/crypto/vm/box.hpp b/crypto/vm/box.hpp new file mode 100644 index 00000000..a448c5d8 --- /dev/null +++ b/crypto/vm/box.hpp @@ -0,0 +1,63 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once +#include "vm/stack.hpp" + +namespace vm { +using td::Ref; + +class Box : public td::CntObject { + mutable StackEntry data_; + + public: + Box() = default; + Box(const Box&) = default; + Box(Box&&) = default; + template + Box(Args... args) : data_{std::move(args...)} { + } + ~Box() override = default; + Box(const StackEntry& data) : data_(data) { + } + Box(StackEntry&& data) : data_(std::move(data)) { + } + void operator=(const StackEntry& data) const { + data_ = data; + } + void operator=(StackEntry&& data) const { + data_ = data; + } + void set(const StackEntry& data) const { + data_ = data; + } + void set(StackEntry&& data) const { + data_ = data; + } + const StackEntry& get() const { + return data_; + } + void clear() const { + data_.clear(); + } + bool empty() const { + return data_.empty(); + } +}; + +} // namespace vm diff --git a/crypto/vm/cellops.cpp b/crypto/vm/cellops.cpp new file mode 100644 index 00000000..5346e168 --- /dev/null +++ b/crypto/vm/cellops.cpp @@ -0,0 +1,1420 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include +#include "vm/cellops.h" +#include "vm/log.h" +#include "vm/opctable.h" +#include "vm/stack.hpp" +#include "vm/continuation.h" +#include "vm/excno.hpp" +#include "vm/vmstate.h" +#include "common/bigint.hpp" +#include "common/refint.h" + +namespace vm { + +int exec_push_ref(VmState* st, CellSlice& cs, int mode, int pfx_bits) { + if (!cs.have_refs(1)) { + throw VmError{Excno::inv_opcode, "no references left for a PUSHREF instruction"}; + } + cs.advance(pfx_bits); + auto cell = cs.fetch_ref(); + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute PUSHREF" << (mode == 2 ? "CONT" : (mode == 1 ? "SLICE" : "")) << " (" + << cell->get_hash().to_hex() << ")"; + switch (mode) { + default: + case 0: + stack.push_cell(std::move(cell)); + break; + case 1: + stack.push_cellslice(load_cell_slice_ref(std::move(cell))); + break; + case 2: + stack.push_cont(Ref{true, load_cell_slice_ref(std::move(cell)), st->get_cp()}); + break; + } + return 0; +} + +std::string dump_push_ref(CellSlice& cs, unsigned args, int pfx_bits, std::string name) { + if (!cs.have_refs(1)) { + return ""; + } + cs.advance(pfx_bits); + cs.advance_refs(1); + return name; +} + +int compute_len_push_ref(const CellSlice& cs, unsigned args, int pfx_bits) { + return cs.have_refs(1) ? (0x10000 + pfx_bits) : 0; +} + +int exec_push_slice_common(VmState* st, CellSlice& cs, unsigned data_bits, unsigned refs, int pfx_bits) { + if (!cs.have(pfx_bits + data_bits)) { + throw VmError{Excno::inv_opcode, "not enough data bits for a PUSHSLICE instruction"}; + } + if (!cs.have_refs(refs)) { + throw VmError{Excno::inv_opcode, "not enough references for a PUSHSLICE instruction"}; + } + Stack& stack = st->get_stack(); + cs.advance(pfx_bits); + auto slice = cs.fetch_subslice(data_bits, refs); + slice.unique_write().remove_trailing(); + VM_LOG(st) << "execute PUSHSLICE " << slice; + stack.push(std::move(slice)); + return 0; +} + +std::string dump_push_slice_common(CellSlice& cs, unsigned data_bits, unsigned refs, int pfx_bits, + const char* name = "PUSHSLICE ") { + if (!cs.have(pfx_bits + data_bits) || !cs.have_refs(refs)) { + return ""; + } + cs.advance(pfx_bits); + auto slice = cs.fetch_subslice(data_bits, refs); + slice.unique_write().remove_trailing(); + std::ostringstream os{name}; + slice->dump_hex(os, 1, false); + return os.str(); +} + +int compute_len_push_slice_common(const CellSlice& cs, unsigned data_bits, unsigned refs, int pfx_bits) { + unsigned bits = pfx_bits + data_bits; + return cs.have(bits) && cs.have_refs(refs) ? (refs << 16) + bits : 0; +} + +int exec_push_slice(VmState* st, CellSlice& cs, unsigned args, int pfx_bits) { + return exec_push_slice_common(st, cs, (args & 15) * 8 + 4, 0, pfx_bits); +} + +std::string dump_push_slice(CellSlice& cs, unsigned args, int pfx_bits) { + return dump_push_slice_common(cs, (args & 15) * 8 + 4, 0, pfx_bits); +} + +int compute_len_push_slice(const CellSlice& cs, unsigned args, int pfx_bits) { + return compute_len_push_slice_common(cs, (args & 15) * 8 + 4, 0, pfx_bits); +} + +int exec_push_slice_r(VmState* st, CellSlice& cs, unsigned args, int pfx_bits) { + unsigned refs = ((args >> 5) & 3) + 1; + unsigned data_bits = (args & 31) * 8 + 1; + return exec_push_slice_common(st, cs, data_bits, refs, pfx_bits); +} + +std::string dump_push_slice_r(CellSlice& cs, unsigned args, int pfx_bits) { + unsigned refs = ((args >> 5) & 3) + 1; + unsigned data_bits = (args & 31) * 8 + 1; + return dump_push_slice_common(cs, data_bits, refs, pfx_bits); +} + +int compute_len_push_slice_r(const CellSlice& cs, unsigned args, int pfx_bits) { + unsigned refs = ((args >> 5) & 3) + 1; + unsigned data_bits = (args & 31) * 8 + 1; + return compute_len_push_slice_common(cs, data_bits, refs, pfx_bits); +} + +int exec_push_slice_r2(VmState* st, CellSlice& cs, unsigned args, int pfx_bits) { + unsigned refs = (args >> 7) & 7; + unsigned data_bits = (args & 127) * 8 + 6; + return exec_push_slice_common(st, cs, data_bits, refs, pfx_bits); +} + +std::string dump_push_slice_r2(CellSlice& cs, unsigned args, int pfx_bits) { + unsigned refs = (args >> 7) & 7; + unsigned data_bits = (args & 127) * 8 + 6; + return dump_push_slice_common(cs, data_bits, refs, pfx_bits); +} + +int compute_len_push_slice_r2(const CellSlice& cs, unsigned args, int pfx_bits) { + unsigned refs = (args >> 7) & 7; + unsigned data_bits = (args & 127) * 8 + 6; + return compute_len_push_slice_common(cs, data_bits, refs, pfx_bits); +} + +int exec_push_cont(VmState* st, CellSlice& cs, unsigned args, int pfx_bits) { + unsigned refs = (args >> 7) & 3; + unsigned data_bits = (args & 127) * 8; + if (!cs.have(pfx_bits + data_bits)) { + throw VmError{Excno::inv_opcode, "not enough data bits for a PUSHCONT instruction"}; + } + if (!cs.have_refs(refs)) { + throw VmError{Excno::inv_opcode, "not enough references for a PUSHCONT instruction"}; + } + Stack& stack = st->get_stack(); + cs.advance(pfx_bits); + auto slice = cs.fetch_subslice(data_bits, refs); + + VM_LOG(st) << "execute PUSHCONT " << slice; + + stack.push_cont(Ref{true, std::move(slice), st->get_cp()}); + return 0; +} + +std::string dump_push_cont(CellSlice& cs, unsigned args, int pfx_bits) { + unsigned refs = (args >> 7) & 3; + unsigned data_bits = (args & 127) * 8; + if (!cs.have(pfx_bits + data_bits) || !cs.have_refs(refs)) { + return ""; + } + cs.advance(pfx_bits); + auto slice = cs.fetch_subslice(data_bits, refs); + std::ostringstream os{"PUSHCONT "}; + slice->dump_hex(os, 1, false); + return os.str(); +} + +int compute_len_push_cont(const CellSlice& cs, unsigned args, int pfx_bits) { + unsigned refs = (args >> 7) & 3; + unsigned data_bits = (args & 127) * 8; + return compute_len_push_slice_common(cs, data_bits, refs, pfx_bits); +} + +int exec_push_cont_simple(VmState* st, CellSlice& cs, unsigned args, int pfx_bits) { + unsigned data_bits = (args & 15) * 8; + if (!cs.have(pfx_bits + data_bits)) { + throw VmError{Excno::inv_opcode, "not enough data bits for a PUSHCONT instruction"}; + } + Stack& stack = st->get_stack(); + cs.advance(pfx_bits); + auto slice = cs.fetch_subslice(data_bits); + VM_LOG(st) << "execute PUSHCONT " << slice; + stack.push_cont(Ref{true, std::move(slice), st->get_cp()}); + return 0; +} + +std::string dump_push_cont_simple(CellSlice& cs, unsigned args, int pfx_bits) { + unsigned data_bits = (args & 15) * 8; + if (!cs.have(pfx_bits + data_bits)) { + return ""; + } + cs.advance(pfx_bits); + auto slice = cs.fetch_subslice(data_bits); + std::ostringstream os{"PUSHCONT "}; + slice->dump_hex(os, 1, false); + return os.str(); +} + +int compute_len_push_cont_simple(const CellSlice& cs, unsigned args, int pfx_bits) { + unsigned data_bits = (args & 15) * 8; + return compute_len_push_slice_common(cs, data_bits, 0, pfx_bits); +} + +void register_cell_const_ops(OpcodeTable& cp0) { + using namespace std::placeholders; + cp0.insert(OpcodeInstr::mkext(0x88, 8, 0, std::bind(dump_push_ref, _1, _2, _3, "PUSHREF"), + std::bind(exec_push_ref, _1, _2, 0, _4), compute_len_push_ref)) + .insert(OpcodeInstr::mkext(0x89, 8, 0, std::bind(dump_push_ref, _1, _2, _3, "PUSHREFSLICE"), + std::bind(exec_push_ref, _1, _2, 1, _4), compute_len_push_ref)) + .insert(OpcodeInstr::mkext(0x8a, 8, 0, std::bind(dump_push_ref, _1, _2, _3, "PUSHREFCONT"), + std::bind(exec_push_ref, _1, _2, 2, _4), compute_len_push_ref)) + .insert(OpcodeInstr::mkext(0x8b, 8, 4, dump_push_slice, exec_push_slice, compute_len_push_slice)) + .insert(OpcodeInstr::mkext(0x8c, 8, 7, dump_push_slice_r, exec_push_slice_r, compute_len_push_slice_r)) + .insert(OpcodeInstr::mkextrange((0x8d * 8) << 7, (0x8d * 8 + 5) << 7, 18, 10, dump_push_slice_r2, + exec_push_slice_r2, compute_len_push_slice_r2)) + .insert(OpcodeInstr::mkext(0x8e / 2, 7, 9, dump_push_cont, exec_push_cont, compute_len_push_cont)) + .insert(OpcodeInstr::mkext(9, 4, 4, dump_push_cont_simple, exec_push_cont_simple, compute_len_push_cont_simple)); +} + +int exec_un_cs_cmp(VmState* st, const char* name, const std::function)>& func) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute " << name; + stack.check_underflow(1); + stack.push_smallint(func(stack.pop_cellslice()) ? -1 : 0); + return 0; +} + +int exec_iun_cs_cmp(VmState* st, const char* name, const std::function)>& func) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute " << name; + stack.check_underflow(1); + stack.push_smallint(func(stack.pop_cellslice())); + return 0; +} + +int exec_bin_cs_cmp(VmState* st, const char* name, const std::function, Ref)>& func) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute " << name; + stack.check_underflow(2); + auto cs2 = stack.pop_cellslice(); + auto cs1 = stack.pop_cellslice(); + stack.push_smallint(func(cs1, cs2) ? -1 : 0); + return 0; +} + +int exec_ibin_cs_cmp(VmState* st, const char* name, const std::function, Ref)>& func) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute " << name; + stack.check_underflow(2); + auto cs2 = stack.pop_cellslice(); + auto cs1 = stack.pop_cellslice(); + stack.push_smallint(func(cs1, cs2)); + return 0; +} + +namespace { + +using namespace std::placeholders; + +void reg_un_cs_cmp(OpcodeTable& cp, unsigned code, unsigned len, const char* name, + std::function)> func) { + cp.insert(OpcodeInstr::mksimple(code, len, name, std::bind(exec_un_cs_cmp, _1, name, std::move(func)))); +} + +void reg_iun_cs_cmp(OpcodeTable& cp, unsigned code, unsigned len, const char* name, + std::function)> func) { + cp.insert(OpcodeInstr::mksimple(code, len, name, std::bind(exec_iun_cs_cmp, _1, name, std::move(func)))); +} + +void reg_bin_cs_cmp(OpcodeTable& cp, unsigned code, unsigned len, const char* name, + std::function, Ref)> func) { + cp.insert(OpcodeInstr::mksimple(code, len, name, std::bind(exec_bin_cs_cmp, _1, name, std::move(func)))); +} + +void reg_ibin_cs_cmp(OpcodeTable& cp, unsigned code, unsigned len, const char* name, + std::function, Ref)> func) { + cp.insert(OpcodeInstr::mksimple(code, len, name, std::bind(exec_ibin_cs_cmp, _1, name, std::move(func)))); +} + +} // namespace + +void register_cell_cmp_ops(OpcodeTable& cp0) { + reg_un_cs_cmp(cp0, 0xc700, 16, "SEMPTY", [](auto cs) { return cs->empty() && !cs->size_refs(); }); + reg_un_cs_cmp(cp0, 0xc701, 16, "SDEMPTY", [](auto cs) { return cs->empty(); }); + reg_un_cs_cmp(cp0, 0xc702, 16, "SREMPTY", [](auto cs) { return !cs->size_refs(); }); + reg_un_cs_cmp(cp0, 0xc703, 16, "SDFIRST", [](auto cs) { return cs->prefetch_long(1) == -1; }); + reg_ibin_cs_cmp(cp0, 0xc704, 16, "SDLEXCMP", [](auto cs1, auto cs2) { return cs1->lex_cmp(*cs2); }); + reg_bin_cs_cmp(cp0, 0xc705, 16, "SDEQ", [](auto cs1, auto cs2) { return !cs1->lex_cmp(*cs2); }); + reg_bin_cs_cmp(cp0, 0xc708, 16, "SDPFX", [](auto cs1, auto cs2) { return cs1->is_prefix_of(*cs2); }); + reg_bin_cs_cmp(cp0, 0xc709, 16, "SDPFXREV", [](auto cs1, auto cs2) { return cs2->is_prefix_of(*cs1); }); + reg_bin_cs_cmp(cp0, 0xc70a, 16, "SDPPFX", [](auto cs1, auto cs2) { return cs1->is_proper_prefix_of(*cs2); }); + reg_bin_cs_cmp(cp0, 0xc70b, 16, "SDPPFXREV", [](auto cs1, auto cs2) { return cs2->is_proper_prefix_of(*cs1); }); + reg_bin_cs_cmp(cp0, 0xc70c, 16, "SDSFX", [](auto cs1, auto cs2) { return cs1->is_suffix_of(*cs2); }); + reg_bin_cs_cmp(cp0, 0xc70d, 16, "SDSFXREV", [](auto cs1, auto cs2) { return cs2->is_suffix_of(*cs1); }); + reg_bin_cs_cmp(cp0, 0xc70e, 16, "SDPSFX", [](auto cs1, auto cs2) { return cs1->is_proper_suffix_of(*cs2); }); + reg_bin_cs_cmp(cp0, 0xc70f, 16, "SDPSFXREV", [](auto cs1, auto cs2) { return cs2->is_proper_suffix_of(*cs1); }); + reg_iun_cs_cmp(cp0, 0xc710, 16, "SDCNTLEAD0", [](auto cs) { return cs->count_leading(0); }); + reg_iun_cs_cmp(cp0, 0xc711, 16, "SDCNTLEAD1", [](auto cs) { return cs->count_leading(1); }); + reg_iun_cs_cmp(cp0, 0xc712, 16, "SDCNTTRAIL0", [](auto cs) { return cs->count_trailing(0); }); + reg_iun_cs_cmp(cp0, 0xc713, 16, "SDCNTTRAIL1", [](auto cs) { return cs->count_trailing(1); }); +} + +int exec_new_builder(VmState* st) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute NEWC"; + stack.push_builder(Ref{true}); + return 0; +} + +int exec_builder_to_cell(VmState* st) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute ENDC"; + stack.check_underflow(1); + stack.push_cell(stack.pop_builder()->finalize_copy()); + return 0; +} + +int exec_builder_to_special_cell(VmState* st) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute ENDXC"; + stack.check_underflow(2); + bool special = stack.pop_bool(); + stack.push_cell(stack.pop_builder()->finalize_copy(special)); + return 0; +} + +inline void check_space(const CellBuilder& builder, unsigned bits, unsigned refs = 0) { + if (!builder.can_extend_by(bits, refs)) { + throw VmError{Excno::cell_ov}; + } +} + +int store_int_common_fail(int code, Stack& stack, Ref builder, td::RefInt256 x, unsigned args) { + if (!(args & 2)) { + stack.push_int_quiet(std::move(x), true); + stack.push_builder(std::move(builder)); + } else { + stack.push_builder(std::move(builder)); + stack.push_int_quiet(std::move(x), true); + } + stack.push_smallint(code); + return 0; +} + +int exec_store_int_common(Stack& stack, unsigned bits, unsigned args) { + bool sgnd = !(args & 1); + Ref builder; + td::RefInt256 x; + if (!(args & 2)) { + builder = stack.pop_builder(); + x = stack.pop_int(); + } else { + x = stack.pop_int(); + builder = stack.pop_builder(); + } + if (!builder->can_extend_by(bits)) { + if (args & 4) { + return store_int_common_fail(-1, stack, std::move(builder), std::move(x), args); + } + throw VmError{Excno::cell_ov}; + } + if (!x->fits_bits(bits, sgnd)) { + if (args & 4) { + return store_int_common_fail(1, stack, std::move(builder), std::move(x), args); + } + throw VmError{Excno::range_chk}; + } + builder.write().store_int256(*x, bits, sgnd); + stack.push_builder(std::move(builder)); + if (args & 4) { + stack.push_smallint(0); + } + return 0; +} + +int exec_store_int(VmState* st, unsigned args, bool sgnd) { + unsigned bits = (args & 0xff) + 1; + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute ST" << (sgnd ? 'I' : 'U') << ' ' << bits; + stack.check_underflow(2); + return exec_store_int_common(stack, bits, !sgnd); +} + +int exec_store_ref(VmState* st, bool quiet) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute STREF" << (quiet ? "Q\n" : "\n"); + stack.check_underflow(2); + auto builder = stack.pop_builder(); + auto cell = stack.pop_cell(); + if (!builder->can_extend_by(0, 1)) { + if (!quiet) { + throw VmError{Excno::cell_ov}; + } + stack.push_cell(std::move(cell)); + stack.push_builder(std::move(builder)); + stack.push_smallint(-1); + return 0; + } + builder.write().store_ref(std::move(cell)); + stack.push_builder(std::move(builder)); + if (quiet) { + stack.push_smallint(0); + } + return 0; +} + +int exec_store_ref_rev(VmState* st, bool quiet) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute STREFR" << (quiet ? "Q\n" : "\n"); + stack.check_underflow(2); + auto cell = stack.pop_cell(); + auto builder = stack.pop_builder(); + if (!builder->can_extend_by(0, 1)) { + if (!quiet) { + throw VmError{Excno::cell_ov}; + } + stack.push_builder(std::move(builder)); + stack.push_cell(std::move(cell)); + stack.push_smallint(-1); + return 0; + } + builder.write().store_ref(std::move(cell)); + stack.push_builder(std::move(builder)); + if (quiet) { + stack.push_smallint(0); + } + return 0; +} + +int exec_store_builder_as_ref(VmState* st, bool quiet) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute STBREF\n"; + stack.check_underflow(2); + auto builder = stack.pop_builder(); + auto builder2 = stack.pop_builder(); + if (!builder->can_extend_by(0, 1)) { + if (!quiet) { + throw VmError{Excno::cell_ov}; + } + stack.push_builder(std::move(builder2)); + stack.push_builder(std::move(builder)); + stack.push_smallint(-1); + return 0; + } + builder.write().store_ref(builder2->finalize_copy()); + stack.push_builder(std::move(builder)); + if (quiet) { + stack.push_smallint(0); + } + return 0; +} + +int exec_store_builder_as_ref_rev(VmState* st, bool quiet) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute STBREFR\n"; + stack.check_underflow(2); + auto builder2 = stack.pop_builder(); + auto builder = stack.pop_builder(); + if (!builder->can_extend_by(0, 1)) { + if (!quiet) { + throw VmError{Excno::cell_ov}; + } + stack.push_builder(std::move(builder)); + stack.push_builder(std::move(builder2)); + stack.push_smallint(-1); + return 0; + } + builder.write().store_ref(builder2->finalize_copy()); + stack.push_builder(std::move(builder)); + if (quiet) { + stack.push_smallint(0); + } + return 0; +} + +int exec_store_slice(VmState* st, bool quiet) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute STSLICE\n"; + stack.check_underflow(2); + auto builder = stack.pop_builder(); + auto cs = stack.pop_cellslice(); + if (!builder->can_extend_by(cs->size(), cs->size_refs())) { + if (!quiet) { + throw VmError{Excno::cell_ov}; + } + stack.push_cellslice(std::move(cs)); + stack.push_builder(std::move(builder)); + stack.push_smallint(-1); + return 0; + } + cell_builder_add_slice(builder.write(), *cs); + stack.push_builder(std::move(builder)); + if (quiet) { + stack.push_smallint(0); + } + return 0; +} + +int exec_store_slice_rev(VmState* st, bool quiet) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute STSLICER\n"; + stack.check_underflow(2); + auto cs = stack.pop_cellslice(); + auto builder = stack.pop_builder(); + if (!builder->can_extend_by(cs->size(), cs->size_refs())) { + if (!quiet) { + throw VmError{Excno::cell_ov}; + } + stack.push_builder(std::move(builder)); + stack.push_cellslice(std::move(cs)); + stack.push_smallint(-1); + return 0; + } + cell_builder_add_slice(builder.write(), *cs); + stack.push_builder(std::move(builder)); + if (quiet) { + stack.push_smallint(0); + } + return 0; +} + +int exec_store_builder(VmState* st, bool quiet) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute STB\n"; + stack.check_underflow(2); + auto builder = stack.pop_builder(); + auto cb2 = stack.pop_builder(); + if (!builder->can_extend_by(cb2->size(), cb2->size_refs())) { + if (!quiet) { + throw VmError{Excno::cell_ov}; + } + stack.push_builder(std::move(cb2)); + stack.push_builder(std::move(builder)); + stack.push_smallint(-1); + return 0; + } + builder.write().append_builder(std::move(cb2)); + stack.push_builder(std::move(builder)); + if (quiet) { + stack.push_smallint(0); + } + return 0; +} + +int exec_store_builder_rev(VmState* st, bool quiet) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute STBR\n"; + stack.check_underflow(2); + auto cb2 = stack.pop_builder(); + auto builder = stack.pop_builder(); + if (!builder->can_extend_by(cb2->size(), cb2->size_refs())) { + if (!quiet) { + throw VmError{Excno::cell_ov}; + } + stack.push_builder(std::move(builder)); + stack.push_builder(std::move(cb2)); + stack.push_smallint(-1); + return 0; + } + builder.write().append_builder(std::move(cb2)); + stack.push_builder(std::move(builder)); + if (quiet) { + stack.push_smallint(0); + } + return 0; +} + +int exec_store_int_var(VmState* st, unsigned args) { + bool sgnd = !(args & 1); + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute ST" << (sgnd ? 'I' : 'U') << 'X' << ((args & 2) ? "R" : "") << ((args & 4) ? "Q\n" : "\n"); + stack.check_underflow(3); + unsigned bits = stack.pop_smallint_range(256 + sgnd); + return exec_store_int_common(stack, bits, args); +} + +std::string dump_store_int_var(CellSlice& cs, unsigned args) { + bool sgnd = !(args & 1); + std::string s = "ST"; + s += sgnd ? 'I' : 'U'; + s += 'X'; + if (args & 2) { + s += 'R'; + } + if (args & 4) { + s += 'Q'; + } + return s; +} + +int exec_store_int_fixed(VmState* st, unsigned args) { + unsigned bits = (args & 0xff) + 1; + args >>= 8; + bool sgnd = !(args & 1); + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute ST" << (sgnd ? 'I' : 'U') << ((args & 2) ? "R" : "") << ((args & 4) ? "Q " : " ") << bits; + stack.check_underflow(2); + return exec_store_int_common(stack, bits, args); +} + +std::string dump_store_int_fixed(CellSlice& cs, unsigned args) { + unsigned bits = (args & 0xff) + 1; + bool sgnd = !(args & 0x100); + std::ostringstream s{"ST"}; + s << (sgnd ? 'I' : 'U'); + if (args & 0x200) { + s << 'R'; + } + if (args & 0x400) { + s << 'Q'; + } + s << ' ' << bits; + return s.str(); +} + +int exec_store_const_ref(VmState* st, CellSlice& cs, unsigned args, int pfx_bits) { + unsigned refs = (args & 1) + 1; + if (!cs.have_refs(refs)) { + throw VmError{Excno::inv_opcode, "no references left for a STREFCONST instruction"}; + } + cs.advance(pfx_bits); + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute STREF" << refs << "CONST\n"; + stack.check_underflow(1); + auto builder = stack.pop_builder(); + check_space(*builder, 0, refs); + do { + builder.write().store_ref(cs.fetch_ref()); + } while (--refs > 0); + stack.push_builder(std::move(builder)); + return 0; +} + +std::string dump_store_const_ref(CellSlice& cs, unsigned args, int pfx_bits) { + unsigned refs = (args & 1) + 1; + if (!cs.have_refs(refs)) { + return ""; + } + cs.advance(pfx_bits); + cs.advance_refs(refs); + return refs > 1 ? (std::string{"STREF"} + (char)('0' + refs) + "CONST") : "STREFCONST"; +} + +int compute_len_store_const_ref(const CellSlice& cs, unsigned args, int pfx_bits) { + unsigned refs = (args & 1) + 1; + return cs.have_refs(refs) ? ((refs << 16) + pfx_bits) : 0; +} + +int exec_store_le_int(VmState* st, unsigned args) { + unsigned bits = (args & 2) ? 64 : 32; + bool sgnd = !(args & 1); + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute ST" << (sgnd ? 'I' : 'U') << "LE" << bits / 8; + stack.check_underflow(2); + auto builder = stack.pop_builder(); + auto x = stack.pop_int(); + check_space(*builder, bits); + if (!(sgnd ? x->signed_fits_bits(bits) : x->unsigned_fits_bits(bits))) { + throw VmError{Excno::range_chk}; + } + unsigned char buff[8]; + st->ensure_throw(x->export_bytes_lsb(buff, bits >> 3, sgnd)); + builder.write().store_bytes(buff, bits >> 3); + stack.push_builder(std::move(builder)); + return 0; +} + +std::string dump_store_le_int(CellSlice& cs, unsigned args) { + bool sgnd = !(args & 1); + return std::string{"ST"} + (sgnd ? 'I' : 'U') + "LE" + ((args & 2) ? '8' : '4'); +} + +int exec_int_builder_func(VmState* st, std::string name, const std::function)>& func) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute " << name; + stack.check_underflow(1); + stack.push_smallint(func(stack.pop_builder())); + return 0; +} + +int exec_2int_builder_func(VmState* st, std::string name, + const std::function(Ref)>& func) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute " << name; + stack.check_underflow(1); + std::pair res = func(stack.pop_builder()); + stack.push_smallint(res.first); + stack.push_smallint(res.second); + return 0; +} + +int exec_builder_chk_bits(VmState* st, unsigned args, bool quiet) { + unsigned bits = (args & 0xff) + 1; + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute BCHKBITS" << (quiet ? "Q " : " ") << bits; + stack.check_underflow(1); + auto builder = stack.pop_builder(); + if (quiet) { + stack.push_smallint(builder->can_extend_by(bits) ? -1 : 0); + } else { + check_space(*builder, bits); + } + return 0; +} + +int exec_builder_chk_bits_refs(VmState* st, unsigned mode) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute BCHK" << ((mode & 1) ? "BIT" : "") << ((mode & 2) ? "REFS" : "S") << ((mode & 4) ? "Q" : ""); + stack.check_underflow(1 + (mode & 1) + ((mode & 2) >> 1)); + unsigned refs = (mode & 2) ? stack.pop_smallint_range(7) : 0; + unsigned bits = (mode & 1) ? stack.pop_smallint_range(1023) : 0; + auto builder = stack.pop_builder(); + if (mode & 4) { + stack.push_smallint(builder->can_extend_by(bits, refs) ? -1 : 0); + } else { + check_space(*builder, bits, refs); + } + return 0; +} + +int exec_store_same(VmState* st, const char* name, int val) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute " << name; + stack.check_underflow(2 + (val < 0)); + if (val < 0) { + val = stack.pop_smallint_range(1); + } + unsigned bits = stack.pop_smallint_range(1023); + auto builder = stack.pop_builder(); + check_space(*builder, bits); + builder.write().reserve_slice(bits) = (bool)val; + stack.push_builder(std::move(builder)); + return 0; +} + +int exec_store_const_slice(VmState* st, CellSlice& cs, unsigned args, int pfx_bits) { + unsigned refs = (args >> 3) & 3; + unsigned data_bits = (args & 7) * 8 + 2; + if (!cs.have(pfx_bits + data_bits)) { + throw VmError{Excno::inv_opcode, "not enough data bits for a STSLICECONST instruction"}; + } + if (!cs.have_refs(refs)) { + throw VmError{Excno::inv_opcode, "not enough references for a STSLICECONST instruction"}; + } + Stack& stack = st->get_stack(); + cs.advance(pfx_bits); + auto slice = cs.fetch_subslice(data_bits, refs); + slice.unique_write().remove_trailing(); + VM_LOG(st) << "execute STSLICECONST " << slice; + auto builder = stack.pop_builder(); + check_space(*builder, slice->size(), slice->size_refs()); + cell_builder_add_slice(builder.write(), *slice); + stack.push_builder(std::move(builder)); + return 0; +} + +std::string dump_store_const_slice(CellSlice& cs, unsigned args, int pfx_bits) { + unsigned refs = (args >> 3) & 3; + unsigned data_bits = (args & 7) * 8 + 2; + return dump_push_slice_common(cs, data_bits, refs, pfx_bits, "STSLICECONST "); +} + +int compute_len_store_const_slice(const CellSlice& cs, unsigned args, int pfx_bits) { + unsigned refs = (args >> 3) & 3; + unsigned data_bits = (args & 7) * 8 + 2; + return compute_len_push_slice_common(cs, data_bits, refs, pfx_bits); +} + +void register_cell_serialize_ops(OpcodeTable& cp0) { + using namespace std::placeholders; + cp0.insert(OpcodeInstr::mksimple(0xc8, 8, "NEWC", exec_new_builder)) + .insert(OpcodeInstr::mksimple(0xc9, 8, "ENDC", exec_builder_to_cell)) + .insert( + OpcodeInstr::mkfixed(0xca, 8, 8, instr::dump_1c_l_add(1, "STI "), std::bind(exec_store_int, _1, _2, true))) + .insert( + OpcodeInstr::mkfixed(0xcb, 8, 8, instr::dump_1c_l_add(1, "STU "), std::bind(exec_store_int, _1, _2, false))) + .insert(OpcodeInstr::mksimple(0xcc, 8, "STREF", std::bind(exec_store_ref, _1, false))) + .insert(OpcodeInstr::mksimple(0xcd, 8, "ENDCST", std::bind(exec_store_builder_as_ref_rev, _1, false))) + .insert(OpcodeInstr::mksimple(0xce, 8, "STSLICE", std::bind(exec_store_slice, _1, false))) + .insert(OpcodeInstr::mkfixed(0xcf00 >> 3, 13, 3, dump_store_int_var, exec_store_int_var)) + .insert(OpcodeInstr::mkfixed(0xcf08 >> 3, 13, 11, dump_store_int_fixed, exec_store_int_fixed)) + .insert(OpcodeInstr::mksimple(0xcf10, 16, "STREF", std::bind(exec_store_ref, _1, false))) + .insert(OpcodeInstr::mksimple(0xcf11, 16, "STBREF", std::bind(exec_store_builder_as_ref, _1, false))) + .insert(OpcodeInstr::mksimple(0xcf12, 16, "STSLICE", std::bind(exec_store_slice, _1, false))) + .insert(OpcodeInstr::mksimple(0xcf13, 16, "STB", std::bind(exec_store_builder, _1, false))) + .insert(OpcodeInstr::mksimple(0xcf14, 16, "STREFR", std::bind(exec_store_ref_rev, _1, false))) + .insert(OpcodeInstr::mksimple(0xcf15, 16, "STBREFR", std::bind(exec_store_builder_as_ref_rev, _1, false))) + .insert(OpcodeInstr::mksimple(0xcf16, 16, "STSLICER", std::bind(exec_store_slice_rev, _1, false))) + .insert(OpcodeInstr::mksimple(0xcf17, 16, "STBR", std::bind(exec_store_builder_rev, _1, false))) + .insert(OpcodeInstr::mksimple(0xcf18, 16, "STREFQ", std::bind(exec_store_ref, _1, true))) + .insert(OpcodeInstr::mksimple(0xcf19, 16, "STBREFQ", std::bind(exec_store_builder_as_ref, _1, true))) + .insert(OpcodeInstr::mksimple(0xcf1a, 16, "STSLICEQ", std::bind(exec_store_slice, _1, true))) + .insert(OpcodeInstr::mksimple(0xcf1b, 16, "STBQ", std::bind(exec_store_builder, _1, true))) + .insert(OpcodeInstr::mksimple(0xcf1c, 16, "STREFRQ", std::bind(exec_store_ref_rev, _1, true))) + .insert(OpcodeInstr::mksimple(0xcf1d, 16, "STBREFRQ", std::bind(exec_store_builder_as_ref_rev, _1, true))) + .insert(OpcodeInstr::mksimple(0xcf1e, 16, "STSLICERQ", std::bind(exec_store_slice_rev, _1, true))) + .insert(OpcodeInstr::mksimple(0xcf1f, 16, "STBRQ", std::bind(exec_store_builder_rev, _1, true))) + .insert(OpcodeInstr::mkextrange(0xcf20, 0xcf22, 16, 1, dump_store_const_ref, exec_store_const_ref, + compute_len_store_const_ref)) + .insert(OpcodeInstr::mksimple(0xcf23, 16, "ENDXC", exec_builder_to_special_cell)) + .insert(OpcodeInstr::mkfixed(0xcf28 >> 2, 14, 2, dump_store_le_int, exec_store_le_int)) + .insert(OpcodeInstr::mksimple( + 0xcf31, 16, "BBITS", + std::bind(exec_int_builder_func, _1, "BBITS", [](Ref b) { return b->size(); }))) + .insert(OpcodeInstr::mksimple( + 0xcf32, 16, "BREFS", + std::bind(exec_int_builder_func, _1, "BREFS", [](Ref b) { return b->size_refs(); }))) + .insert(OpcodeInstr::mksimple( + 0xcf33, 16, "BBITREFS", + std::bind(exec_2int_builder_func, _1, "BBITSREFS", + [](Ref b) { return std::make_pair(b->size(), b->size_refs()); }))) + .insert(OpcodeInstr::mksimple( + 0xcf35, 16, "BREMBITS", + std::bind(exec_int_builder_func, _1, "BREMBITS", [](Ref b) { return b->remaining_bits(); }))) + .insert(OpcodeInstr::mksimple( + 0xcf36, 16, "BREMREFS", + std::bind(exec_int_builder_func, _1, "BREMREFS", [](Ref b) { return b->remaining_refs(); }))) + .insert(OpcodeInstr::mksimple( + 0xcf37, 16, "BREMBITREFS", + std::bind(exec_2int_builder_func, _1, "BREMBITSREFS", + [](Ref b) { return std::make_pair(b->remaining_bits(), b->remaining_refs()); }))) + .insert(OpcodeInstr::mkfixed(0xcf38, 16, 8, instr::dump_1c_l_add(1, "BCHKBITS "), + std::bind(exec_builder_chk_bits, _1, _2, false))) + .insert(OpcodeInstr::mksimple(0xcf39, 16, "BCHKBITS", std::bind(exec_builder_chk_bits_refs, _1, 1))) + .insert(OpcodeInstr::mksimple(0xcf3a, 16, "BCHKREFS", std::bind(exec_builder_chk_bits_refs, _1, 2))) + .insert(OpcodeInstr::mksimple(0xcf3b, 16, "BCHKBITREFS", std::bind(exec_builder_chk_bits_refs, _1, 3))) + .insert(OpcodeInstr::mkfixed(0xcf3c, 16, 8, instr::dump_1c_l_add(1, "BCHKBITSQ "), + std::bind(exec_builder_chk_bits, _1, _2, true))) + .insert(OpcodeInstr::mksimple(0xcf3d, 16, "BCHKBITSQ", std::bind(exec_builder_chk_bits_refs, _1, 5))) + .insert(OpcodeInstr::mksimple(0xcf3e, 16, "BCHKREFSQ", std::bind(exec_builder_chk_bits_refs, _1, 6))) + .insert(OpcodeInstr::mksimple(0xcf3f, 16, "BCHKBITREFSQ", std::bind(exec_builder_chk_bits_refs, _1, 7))) + .insert(OpcodeInstr::mksimple(0xcf40, 16, "STZEROES", std::bind(exec_store_same, _1, "STZEROES", 0))) + .insert(OpcodeInstr::mksimple(0xcf41, 16, "STONES", std::bind(exec_store_same, _1, "STONES", 1))) + .insert(OpcodeInstr::mksimple(0xcf42, 16, "STSAME", std::bind(exec_store_same, _1, "STSAME", -1))) + .insert(OpcodeInstr::mkext(0xcf80 >> 7, 9, 5, dump_store_const_slice, exec_store_const_slice, + compute_len_store_const_slice)); +} + +int exec_cell_to_slice(VmState* st) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute CTOS"; + auto cell = stack.pop_cell(); + stack.push_cellslice(load_cell_slice_ref(std::move(cell))); + return 0; +} + +int exec_cell_to_slice_maybe_special(VmState* st) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute XCTOS"; + bool is_special; + auto cell = stack.pop_cell(); + stack.push_cellslice(load_cell_slice_ref_special(std::move(cell), is_special)); + stack.push_bool(is_special); + return 0; +} + +int exec_load_special_cell(VmState* st, bool quiet) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute XLOAD" << (quiet ? "Q" : ""); + auto cell = stack.pop_cell(); + stack.push_cell(cell); + if (quiet) { + stack.push_bool(true); + } + return 0; +} + +int exec_slice_chk_empty(VmState* st) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute ENDS"; + auto cs = stack.pop_cellslice(); + if (cs->size() || cs->size_refs()) { + throw VmError{Excno::cell_und, "extra data remaining in deserialized cell"}; + } + return 0; +} + +int exec_load_int_common(Stack& stack, unsigned bits, unsigned mode) { + auto cs = stack.pop_cellslice(); + if (!cs->have(bits)) { + if (!(mode & 4)) { + throw VmError{Excno::cell_und}; + } + if (!(mode & 2)) { + stack.push_cellslice(std::move(cs)); + } + stack.push_smallint(0); + return 0; + } + bool sgnd = !(mode & 1); + if (mode & 2) { + stack.push_int(cs->prefetch_int256(bits, sgnd)); + } else { + stack.push_int(cs.write().fetch_int256(bits, sgnd)); + stack.push_cellslice(std::move(cs)); + } + if (mode & 4) { + stack.push_smallint(-1); + } + return 0; +} + +int exec_load_int_fixed(VmState* st, unsigned args, unsigned mode) { + unsigned bits = (args & 0xff) + 1; + bool sgnd = !(mode & 1); + VM_LOG(st) << "execute " << (mode & 2 ? "P" : "") << "LD" << (sgnd ? 'I' : 'U') << (mode & 4 ? "Q " : " ") << bits; + return exec_load_int_common(st->get_stack(), bits, mode); +} + +int exec_preload_ref_fixed(VmState* st, unsigned args) { + unsigned idx = args & 3; + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute PLDREFIDX " << idx; + auto cs = stack.pop_cellslice(); + if (!cs->have_refs(idx + 1)) { + throw VmError{Excno::cell_und}; + } + stack.push_cell(cs->prefetch_ref(idx)); + return 0; +} + +int exec_preload_ref(VmState* st) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute PLDREFVAR"; + stack.check_underflow(2); + unsigned idx = stack.pop_smallint_range(3); + auto cs = stack.pop_cellslice(); + if (!cs->have_refs(idx + 1)) { + throw VmError{Excno::cell_und}; + } + stack.push_cell(cs->prefetch_ref(idx)); + return 0; +} + +int exec_load_ref(VmState* st, unsigned mode) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute " << (mode & 2 ? "P" : "") << "LDREF" << (mode & 4 ? "Q" : ""); + auto cs = stack.pop_cellslice(); + if (!cs->have_refs()) { + if (!(mode & 4)) { + throw VmError{Excno::cell_und}; + } + stack.push_smallint(0); + return 0; + } + if (mode & 2) { + stack.push_cell(cs->prefetch_ref()); + } else { + stack.push_cell(cs.write().fetch_ref()); + stack.push_cellslice(std::move(cs)); + } + if (mode & 4) { + stack.push_smallint(-1); + } + return 0; +} + +int exec_load_ref_rev_to_slice(VmState* st, unsigned mode) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute " << (mode & 2 ? "P" : "") << "LDREFRTOS" << (mode & 4 ? "Q" : ""); + auto cs = stack.pop_cellslice(); + if (!cs->have_refs()) { + if (!(mode & 4)) { + throw VmError{Excno::cell_und}; + } + stack.push_smallint(0); + return 0; + } + if (mode & 2) { + stack.push_cellslice(load_cell_slice_ref(cs->prefetch_ref())); + } else { + auto cell = cs.write().fetch_ref(); + stack.push_cellslice(std::move(cs)); + stack.push_cellslice(load_cell_slice_ref(std::move(cell))); + } + if (mode & 4) { + stack.push_smallint(-1); + } + return 0; +} + +int exec_load_slice_common(Stack& stack, unsigned bits, unsigned mode) { + auto cs = stack.pop_cellslice(); + if (!cs->have(bits)) { + if (!(mode & 2)) { + throw VmError{Excno::cell_und}; + } + if (!(mode & 1)) { + stack.push_cellslice(std::move(cs)); + } + stack.push_smallint(0); + return 0; + } + if (mode & 1) { + stack.push_cellslice(cs->prefetch_subslice(bits)); + } else { + stack.push_cellslice(cs.write().fetch_subslice(bits)); + stack.push_cellslice(std::move(cs)); + } + if (mode & 2) { + stack.push_smallint(-1); + } + return 0; +} + +int exec_load_slice_fixed(VmState* st, unsigned args) { + unsigned bits = (args & 0xff) + 1; + VM_LOG(st) << "execute LDSLICE " << bits; + return exec_load_slice_common(st->get_stack(), bits, 0); +} + +int exec_load_int_var(VmState* st, unsigned args) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute " << (args & 2 ? "PLD" : "LD") << (args & 1 ? "UX" : "IX") << (args & 4 ? "Q\n" : "\n"); + stack.check_underflow(2); + unsigned bits = stack.pop_smallint_range(257 - (args & 1)); + return exec_load_int_common(stack, bits, args & 7); +} + +std::string dump_load_int_var(CellSlice&, unsigned args) { + return (args & 2 ? std::string{"PLD"} : std::string{"LD"}) + (args & 1 ? "UX" : "IX") + (args & 4 ? "Q" : ""); +} + +int exec_load_int_fixed2(VmState* st, unsigned args) { + unsigned bits = (args & 0xff) + 1; + args >>= 8; + VM_LOG(st) << "execute " << (args & 2 ? "PLD" : "LD") << (args & 1 ? "U" : "I") << (args & 4 ? "Q " : " ") << bits; + return exec_load_int_common(st->get_stack(), bits, args & 7); +} + +std::string dump_load_int_fixed2(CellSlice&, unsigned args) { + std::ostringstream os{args & 0x200 ? "PLD" : "LD"}; + os << (args & 0x100 ? 'U' : 'I'); + if (args & 0x400) { + os << 'Q'; + } + os << ' ' << ((args & 0xff) + 1); + return os.str(); +} + +int exec_preload_uint_fixed_0e(VmState* st, unsigned args) { + unsigned bits = ((args & 7) + 1) << 5; + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute PLDUZ " << bits; + auto cs = stack.pop_cellslice(); + auto x = cs->prefetch_int256_zeroext(bits, false); + stack.push_cellslice(std::move(cs)); + stack.push_int(std::move(x)); + return 0; +} + +std::string dump_preload_uint_fixed_0e(CellSlice&, unsigned args) { + std::ostringstream os{"PLDUZ "}; + unsigned bits = ((args & 7) + 1) << 5; + os << bits; + return os.str(); +} + +int exec_load_slice(VmState* st, unsigned args) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute " << (args & 1 ? "PLDSLICEX" : "LDSLICEX") << (args & 2 ? "Q\n" : "\n"); + stack.check_underflow(2); + unsigned bits = stack.pop_smallint_range(1023); + return exec_load_slice_common(stack, bits, args); +} + +std::string dump_load_slice(CellSlice&, unsigned args) { + return std::string{args & 1 ? "P" : ""} + "LDSLICEX" + (args & 2 ? "Q" : ""); +} + +int exec_load_slice_fixed2(VmState* st, unsigned args) { + unsigned bits = (args & 0xff) + 1; + args >>= 8; + VM_LOG(st) << "execute " << (args & 1 ? "PLDSLICE" : "LDSLICE") << (args & 2 ? "Q " : " ") << bits; + return exec_load_slice_common(st->get_stack(), bits, args); +} + +std::string dump_load_slice_fixed2(CellSlice&, unsigned args) { + unsigned bits = (args & 0xff) + 1; + std::ostringstream os{args & 0x100 ? "PLDSLICE" : "LDSLICE"}; + if (args & 0x200) { + os << 'Q'; + } + os << ' ' << bits; + return os.str(); +} + +int exec_slice_op_args(VmState* st, const char* name, unsigned max_arg1, + const std::function& func) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute " << name; + stack.check_underflow(2); + unsigned x = stack.pop_smallint_range(max_arg1); + auto cs = stack.pop_cellslice(); + if (!func(cs.write(), x)) { + throw VmError{Excno::cell_und}; + } + stack.push_cellslice(std::move(cs)); + return 0; +} + +int exec_slice_op_args2(VmState* st, const char* name, unsigned max_arg1, unsigned max_arg2, + const std::function& func) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute " << name; + stack.check_underflow(3); + unsigned y = stack.pop_smallint_range(max_arg2); + unsigned x = stack.pop_smallint_range(max_arg1); + auto cs = stack.pop_cellslice(); + if (!func(cs.write(), x, y)) { + throw VmError{Excno::cell_und}; + } + stack.push_cellslice(std::move(cs)); + return 0; +} + +int exec_slice_begins_with_common(VmState* st, Ref cs2, bool quiet) { + Stack& stack = st->get_stack(); + auto cs = stack.pop_cellslice(); + if (!cs->has_prefix(*cs2)) { + if (!quiet) { + throw VmError{Excno::cell_und, "slice does not begin with expected data bits"}; + } + stack.push_cellslice(std::move(cs)); + stack.push_smallint(0); + return 0; + } + cs.write().advance(cs2->size()); + stack.push_cellslice(std::move(cs)); + if (quiet) { + stack.push_smallint(-1); + } + return 0; +} + +int exec_slice_begins_with(VmState* st, bool quiet) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute SDBEGINSX" << (quiet ? "Q\n" : "\n"); + stack.check_underflow(2); + return exec_slice_begins_with_common(st, stack.pop_cellslice(), quiet); +} + +int exec_slice_begins_with_const(VmState* st, CellSlice& cs, unsigned args, int pfx_bits) { + bool quiet = args & 128; + unsigned data_bits = (args & 127) * 8 + 3; + if (!cs.have(pfx_bits + data_bits)) { + throw VmError{Excno::inv_opcode, "not enough data bits for a SDBEGINS instruction"}; + } + cs.advance(pfx_bits); + auto slice = cs.fetch_subslice(data_bits); + slice.unique_write().remove_trailing(); + VM_LOG(st) << "execute SDBEGINS" << (quiet ? "Q " : " ") << slice; + return exec_slice_begins_with_common(st, slice, quiet); +} + +std::string dump_slice_begins_with_const(CellSlice& cs, unsigned args, int pfx_bits) { + bool quiet = args & 128; + unsigned data_bits = (args & 127) * 8 + 3; + return dump_push_slice_common(cs, data_bits, 0, pfx_bits, quiet ? "SDBEGINSQ " : "SDBEGINS "); +} + +int compute_len_slice_begins_with_const(const CellSlice& cs, unsigned args, int pfx_bits) { + unsigned data_bits = (args & 127) * 8 + 3; + return compute_len_push_slice_common(cs, data_bits, 0, pfx_bits); +} + +int exec_subslice(VmState* st) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute SUBSLICE\n"; + stack.check_underflow(5); + unsigned r2 = stack.pop_smallint_range(4), l2 = stack.pop_smallint_range(1023); + unsigned r1 = stack.pop_smallint_range(4), l1 = stack.pop_smallint_range(1023); + auto cs = stack.pop_cellslice(); + if (!cs.write().skip_first(l1, r1) || !cs.unique_write().only_first(l2, r2)) { + throw VmError{Excno::cell_und}; + } + stack.push_cellslice(std::move(cs)); + return 0; +} + +int exec_split(VmState* st, bool quiet) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute SPLIT" << (quiet ? "Q\n" : "\n"); + stack.check_underflow(3); + unsigned refs = stack.pop_smallint_range(4), bits = stack.pop_smallint_range(1023); + auto cs = stack.pop_cellslice(); + if (!cs->have(bits) || !cs->have_refs(refs)) { + if (!quiet) { + throw VmError{Excno::cell_und}; + } + stack.push_cellslice(std::move(cs)); + stack.push_smallint(0); + return 0; + } + auto cs2 = cs; + cs2.write().only_first(bits, refs); + cs.write().skip_first(bits, refs); + stack.push_cellslice(std::move(cs2)); + stack.push_cellslice(std::move(cs)); + if (quiet) { + stack.push_smallint(-1); + } + return 0; +} + +int exec_slice_chk_op_args(VmState* st, const char* name, unsigned max_arg1, bool quiet, + const std::function& func) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute " << name; + stack.check_underflow(2); + unsigned x = stack.pop_smallint_range(max_arg1); + auto cs = stack.pop_cellslice(); + bool res = func(*cs, x); + if (quiet) { + stack.push_smallint(res ? -1 : 0); + } else if (!res) { + throw VmError{Excno::cell_und}; + } + return 0; +} + +int exec_slice_chk_op_args2(VmState* st, const char* name, unsigned max_arg1, unsigned max_arg2, bool quiet, + const std::function& func) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute " << name; + stack.check_underflow(3); + unsigned y = stack.pop_smallint_range(max_arg2); + unsigned x = stack.pop_smallint_range(max_arg1); + auto cs = stack.pop_cellslice(); + bool res = func(*cs, x, y); + if (quiet) { + stack.push_smallint(res ? -1 : 0); + } else if (!res) { + throw VmError{Excno::cell_und}; + } + return 0; +} + +int exec_slice_bits_refs(VmState* st, unsigned mode) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute S" << (mode & 1 ? "BIT" : "") << (mode & 2 ? "REF" : "") << "S\n"; + stack.check_underflow(1); + auto cs = stack.pop_cellslice(); + if (mode & 1) { + stack.push_smallint(cs->size()); + } + if (mode & 2) { + stack.push_smallint(cs->size_refs()); + } + return 0; +} + +int exec_load_le_int(VmState* st, unsigned args) { + unsigned len = (args & 2) ? 8 : 4; + bool sgnd = !(args & 1); + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute " << (args & 4 ? "PLD" : "LD") << (sgnd ? 'I' : 'U') << "LE" << len + << (args & 8 ? "Q\n" : "\n"); + stack.check_underflow(1); + auto cs = stack.pop_cellslice(); + if (!cs->have(len << 3)) { + if (args & 8) { + if (!(args & 4)) { + stack.push_cellslice(std::move(cs)); + } + stack.push_smallint(0); + return 0; + } + throw VmError{Excno::cell_und}; + } + unsigned char buff[8]; + st->ensure_throw(cs->prefetch_bytes(buff, len)); + td::RefInt256 x{true}; + st->ensure_throw(x.unique_write().import_bytes_lsb(buff, len, sgnd)); + stack.push_int(std::move(x)); + if (!(args & 4)) { + st->ensure_throw(cs.write().advance(len << 3)); + stack.push_cellslice(std::move(cs)); + } + if (args & 8) { + stack.push_smallint(-1); + } + return 0; +} + +std::string dump_load_le_int(CellSlice& cs, unsigned args) { + bool sgnd = !(args & 1); + return std::string{args & 4 ? "P" : ""} + "LD" + (sgnd ? 'I' : 'U') + "LE" + ((args & 2) ? '8' : '4') + + (args & 8 ? "Q" : ""); +} + +int exec_load_same(VmState* st, const char* name, int x) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute " << name; + stack.check_underflow(1 + (x < 0)); + if (x < 0) { + x = stack.pop_smallint_range(1); + } + auto cs = stack.pop_cellslice(); + unsigned n = cs->count_leading(x); + if (n > 0) { + cs.write().advance(n); + } + stack.push_smallint(n); + stack.push_cellslice(std::move(cs)); + return 0; +} + +void register_cell_deserialize_ops(OpcodeTable& cp0) { + using namespace std::placeholders; + cp0.insert(OpcodeInstr::mksimple(0xd0, 8, "CTOS", exec_cell_to_slice)) + .insert(OpcodeInstr::mksimple(0xd1, 8, "ENDS", exec_slice_chk_empty)) + .insert( + OpcodeInstr::mkfixed(0xd2, 8, 8, instr::dump_1c_l_add(1, "LDI "), std::bind(exec_load_int_fixed, _1, _2, 0))) + .insert( + OpcodeInstr::mkfixed(0xd3, 8, 8, instr::dump_1c_l_add(1, "LDU "), std::bind(exec_load_int_fixed, _1, _2, 1))) + .insert(OpcodeInstr::mksimple(0xd4, 8, "LDREF", std::bind(exec_load_ref, _1, 0))) + .insert(OpcodeInstr::mksimple(0xd5, 8, "LDREFRTOS", std::bind(exec_load_ref_rev_to_slice, _1, 0))) + .insert(OpcodeInstr::mkfixed(0xd6, 8, 8, instr::dump_1c_l_add(1, "LDSLICE "), exec_load_slice_fixed)) + .insert(OpcodeInstr::mkfixed(0xd700 >> 3, 13, 3, dump_load_int_var, exec_load_int_var)) + .insert(OpcodeInstr::mkfixed(0xd708 >> 3, 13, 11, dump_load_int_fixed2, exec_load_int_fixed2)) + .insert(OpcodeInstr::mkfixed(0xd710 >> 3, 13, 3, dump_preload_uint_fixed_0e, exec_preload_uint_fixed_0e)) + .insert(OpcodeInstr::mkfixed(0xd718 >> 2, 14, 2, dump_load_slice, exec_load_slice)) + .insert(OpcodeInstr::mkfixed(0xd71c >> 2, 14, 10, dump_load_slice_fixed2, exec_load_slice_fixed2)) + .insert(OpcodeInstr::mksimple(0xd720, 16, "SDCUTFIRST", + std::bind(exec_slice_op_args, _1, "SDCUTFIRST", 1023, + [](auto& cs, unsigned bits) { return cs.only_first(bits); }))) + .insert(OpcodeInstr::mksimple(0xd721, 16, "SDSKIPFIRST", + std::bind(exec_slice_op_args, _1, "SDSKIPFIRST", 1023, + [](auto& cs, unsigned bits) { return cs.skip_first(bits); }))) + .insert(OpcodeInstr::mksimple(0xd722, 16, "SDCUTLAST", + std::bind(exec_slice_op_args, _1, "SDCUTLAST", 1023, + [](auto& cs, unsigned bits) { return cs.only_last(bits); }))) + .insert(OpcodeInstr::mksimple(0xd723, 16, "SDSKIPLAST", + std::bind(exec_slice_op_args, _1, "SDSKIPLAST", 1023, + [](auto& cs, unsigned bits) { return cs.skip_last(bits); }))) + .insert(OpcodeInstr::mksimple( + 0xd724, 16, "SDSUBSTR", + std::bind(exec_slice_op_args2, _1, "SDSUBSTR", 1023, 1023, + [](auto& cs, unsigned offs, unsigned bits) { return cs.skip_first(offs) && cs.only_first(bits); }))) + .insert(OpcodeInstr::mksimple(0xd726, 16, "SDBEGINSX", std::bind(exec_slice_begins_with, _1, false))) + .insert(OpcodeInstr::mksimple(0xd727, 16, "SDBEGINSXQ", std::bind(exec_slice_begins_with, _1, true))) + .insert(OpcodeInstr::mkext(0xd728 >> 3, 13, 8, dump_slice_begins_with_const, exec_slice_begins_with_const, + compute_len_slice_begins_with_const)) + .insert(OpcodeInstr::mksimple( + 0xd730, 16, "SCUTFIRST", + std::bind(exec_slice_op_args2, _1, "SCUTFIRST", 1023, 4, + [](auto& cs, unsigned bits, unsigned refs) { return cs.only_first(bits, refs); }))) + .insert(OpcodeInstr::mksimple( + 0xd731, 16, "SSKIPFIRST", + std::bind(exec_slice_op_args2, _1, "SSKIPFIRST", 1023, 4, + [](auto& cs, unsigned bits, unsigned refs) { return cs.skip_first(bits, refs); }))) + .insert(OpcodeInstr::mksimple( + 0xd732, 16, "SCUTLAST", + std::bind(exec_slice_op_args2, _1, "SCUTLAST", 1023, 4, + [](auto& cs, unsigned bits, unsigned refs) { return cs.only_last(bits, refs); }))) + .insert(OpcodeInstr::mksimple( + 0xd733, 16, "SSKIPLAST", + std::bind(exec_slice_op_args2, _1, "SSKIPLAST", 1023, 4, + [](auto& cs, unsigned bits, unsigned refs) { return cs.skip_last(bits, refs); }))) + .insert(OpcodeInstr::mksimple(0xd734, 16, "SUBSLICE", exec_subslice)) + .insert(OpcodeInstr::mksimple(0xd736, 16, "SPLIT", std::bind(exec_split, _1, false))) + .insert(OpcodeInstr::mksimple(0xd737, 16, "SPLITQ", std::bind(exec_split, _1, true))) + .insert(OpcodeInstr::mksimple(0xd739, 16, "XCTOS", exec_cell_to_slice_maybe_special)) + .insert(OpcodeInstr::mksimple(0xd73a, 16, "XLOAD", std::bind(exec_load_special_cell, _1, false))) + .insert(OpcodeInstr::mksimple(0xd73b, 16, "XLOADQ", std::bind(exec_load_special_cell, _1, true))) + .insert(OpcodeInstr::mksimple(0xd741, 16, "SCHKBITS", + std::bind(exec_slice_chk_op_args, _1, "SCHKBITS", 1023, false, + [](auto cs, unsigned bits) { return cs.have(bits); }))) + .insert(OpcodeInstr::mksimple(0xd742, 16, "SCHKREFS", + std::bind(exec_slice_chk_op_args, _1, "SCHKREFS", 1023, false, + [](auto cs, unsigned refs) { return cs.have_refs(refs); }))) + .insert(OpcodeInstr::mksimple( + 0xd743, 16, "SCHKBITREFS", + std::bind(exec_slice_chk_op_args2, _1, "SCHKBITREFS", 1023, 4, false, + [](auto cs, unsigned bits, unsigned refs) { return cs.have(bits) && cs.have_refs(refs); }))) + .insert(OpcodeInstr::mksimple(0xd745, 16, "SCHKBITSQ", + std::bind(exec_slice_chk_op_args, _1, "SCHKBITSQ", 1023, true, + [](auto cs, unsigned bits) { return cs.have(bits); }))) + .insert(OpcodeInstr::mksimple(0xd746, 16, "SCHKREFSQ", + std::bind(exec_slice_chk_op_args, _1, "SCHKREFSQ", 1023, true, + [](auto cs, unsigned refs) { return cs.have_refs(refs); }))) + .insert(OpcodeInstr::mksimple( + 0xd747, 16, "SCHKBITREFSQ", + std::bind(exec_slice_chk_op_args2, _1, "SCHKBITREFSQ", 1023, 4, true, + [](auto cs, unsigned bits, unsigned refs) { return cs.have(bits) && cs.have_refs(refs); }))) + .insert(OpcodeInstr::mksimple(0xd748, 16, "PLDREFVAR", exec_preload_ref)) + .insert(OpcodeInstr::mksimple(0xd749, 16, "SBITS", std::bind(exec_slice_bits_refs, _1, 1))) + .insert(OpcodeInstr::mksimple(0xd74a, 16, "SREFS", std::bind(exec_slice_bits_refs, _1, 2))) + .insert(OpcodeInstr::mksimple(0xd74b, 16, "SBITREFS", std::bind(exec_slice_bits_refs, _1, 3))) + .insert(OpcodeInstr::mkfixed(0xd74c >> 2, 14, 2, instr::dump_1c_and(3, "PLDREFIDX "), exec_preload_ref_fixed)) + .insert(OpcodeInstr::mkfixed(0xd75, 12, 4, dump_load_le_int, exec_load_le_int)) + .insert(OpcodeInstr::mksimple(0xd760, 16, "LDZEROES", std::bind(exec_load_same, _1, "LDZEROES", 0))) + .insert(OpcodeInstr::mksimple(0xd761, 16, "LDONES", std::bind(exec_load_same, _1, "LDONES", 1))) + .insert(OpcodeInstr::mksimple(0xd762, 16, "LDSAME", std::bind(exec_load_same, _1, "LDSAME", -1))); +} + +void register_cell_ops(OpcodeTable& cp0) { + register_cell_const_ops(cp0); + register_cell_cmp_ops(cp0); + register_cell_serialize_ops(cp0); + register_cell_deserialize_ops(cp0); +} + +} // namespace vm diff --git a/crypto/vm/cellops.h b/crypto/vm/cellops.h new file mode 100644 index 00000000..cc0f5ab8 --- /dev/null +++ b/crypto/vm/cellops.h @@ -0,0 +1,31 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once +#include "vm/cellslice.h" + +namespace vm { + +class OpcodeTable; + +void register_cell_ops(OpcodeTable &cp0); + +std::string dump_push_ref(CellSlice &cs, unsigned args, int pfx_bits, std::string name); +int compute_len_push_ref(const CellSlice &cs, unsigned args, int pfx_bits); + +} // namespace vm diff --git a/crypto/vm/cellparse.hpp b/crypto/vm/cellparse.hpp new file mode 100644 index 00000000..9e411632 --- /dev/null +++ b/crypto/vm/cellparse.hpp @@ -0,0 +1,105 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once +#include "vm/cells.h" +#include "vm/cellslice.h" + +namespace vm { + +class CellRefParser { + Ref cs; + bool state; + + public: + CellRefParser(Ref _cs) : cs(std::move(_cs)), state(cs.not_null()) { + } + bool ok() const { + return state; + } + operator bool() const { + return state; + } + template + CellRefParser& deserialize(T& val) { + state = (state && val.deserialize(cs.write())); + return *this; + } + //template + //CellRefParser& deserialize(T& val) { + // state = val.deserialize_ext(cs.write(), state); + // return *this; + //} + template + CellRefParser& deserialize(const T& val) { + state = (state && val.deserialize(cs.write())); + return *this; + } + //template + //CellRefParser& deserialize(const T& val) { + // state = val.deserialize_ext(cs.write(), state); + // return *this; + //} +}; + +class CellParser { + CellSlice& cs; + bool state; + + public: + CellParser(CellSlice& _cs) : cs(_cs), state(true) { + } + bool ok() const { + return state; + } + operator bool() const { + return state; + } + template + CellParser& deserialize(T& val) { + state = (state && val.deserialize(cs)); + return *this; + } + // template + // CellParser& deserialize(T& val) { + // state = val.deserialize_ext(cs, state); + // return *this; + // } + template + CellParser& deserialize(const T& val) { + state = (state && val.deserialize(cs)); + return *this; + } + // template + // CellParser& deserialize(const T& val) { + // state = val.deserialize_ext(cs, state); + // return *this; + // } +}; + +template +P& operator>>(P& cp, T& val) { + return cp.deserialize(val); +} + +template +P& operator>>(P& cp, const T& val) { + return cp.deserialize(val); +} + +} // namespace vm diff --git a/crypto/vm/cells.h b/crypto/vm/cells.h new file mode 100644 index 00000000..ea362597 --- /dev/null +++ b/crypto/vm/cells.h @@ -0,0 +1,73 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +#include +#include "common/refcnt.hpp" +#include "common/bitstring.h" +#include "common/bigint.hpp" +#include "common/refint.h" + +#include "vm/cells/Cell.h" +#include "vm/cells/CellBuilder.h" +#include "vm/cells/DataCell.h" +#include "vm/cells/UsageCell.h" +#include "vm/cells/VirtualCell.h" + +#include "td/utils/Slice.h" +#include "td/utils/StringBuilder.h" +#include "openssl/digest.h" + +// H_i(cell) = H(cell_i) +// cell.hash = sha256( +// d1, d2, +// (level == 0 || special_type == PrunnedBranch) ? data : cell_(level - 1).hash, +// for child in refs: +// child.depth, child.hash +// ) +// lower hashes of Prunned branch are calculated from its data +// cell_i.ref[j] = (special_type == MerkleProof || special_type == MerkleUpdate) ? cell.ref[j]_(i+1) : cell.ref[j]_i +// +// Ordinary cell: +// cell_i.data = cell.data +// cell_i.level_mask = cell.level_mask & ((1< == cell.level + 1 +// cell_i = if i < cell.level then prunned_cell_i +// prunned_cell.data = EXCEPTION +// prunned_cell_i.hash = +// prunned_cell.level_mask = cell.level_mask ^ (1 << cell.level) +// prunned_cell.level = +// prunned_cell_i.depth = +// +// Merkle proof +// cell.level_mask = proof_cell.level_mask >> 1 +// cell.level = == max(0, proof.cell.level - 1) +// cell_i.data = +// cell_i.level_mask = +// cell_i.ref[j] = cell.ref[j]_(i+1) +// cell_i.depth = max_j(1 + cell_i.ref[j].depth) + diff --git a/crypto/vm/cells/Cell.cpp b/crypto/vm/cells/Cell.cpp new file mode 100644 index 00000000..d9e392ed --- /dev/null +++ b/crypto/vm/cells/Cell.cpp @@ -0,0 +1,59 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "vm/cells/Cell.h" +#include "vm/cells/VirtualCell.h" +#include "vm/cells/DataCell.h" + +#include + +namespace vm { +td::Status Cell::check_equals_unloaded(const Ref& other) const { + auto level_mask = get_level_mask(); + if (level_mask != other->get_level_mask()) { + return td::Status::Error("level mismatch"); + } + auto level = level_mask.get_level(); + for (unsigned i = 0; i <= level; i++) { + if (!get_level_mask().is_significant(i)) { + continue; + } + if (get_hash(i) != other->get_hash(i)) { + return td::Status::Error("hash mismatch"); + } + } + for (unsigned i = 0; i <= level; i++) { + if (!get_level_mask().is_significant(i)) { + continue; + } + if (get_depth(i) != other->get_depth(i)) { + return td::Status::Error("depth mismatch"); + } + } + return td::Status::OK(); +} + +Ref Cell::virtualize(VirtualizationParameters virt) const { + return VirtualCell::create(virt, Ref(this)); +} + +std::ostream& operator<<(std::ostream& os, const Cell& c) { + return os << c.get_hash().to_hex(); +} + +} // namespace vm diff --git a/crypto/vm/cells/Cell.h b/crypto/vm/cells/Cell.h new file mode 100644 index 00000000..2f5d2cc7 --- /dev/null +++ b/crypto/vm/cells/Cell.h @@ -0,0 +1,89 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once +#include "common/refcnt.hpp" +#include "common/bitstring.h" + +#include "vm/cells/CellHash.h" +#include "vm/cells/CellTraits.h" +#include "vm/cells/CellUsageTree.h" +#include "vm/cells/LevelMask.h" +#include "vm/cells/VirtualizationParameters.h" + +#include "td/utils/Status.h" + +#include + +namespace vm { +using td::Ref; +class DataCell; + +class Cell : public CellTraits { + public: + using LevelMask = detail::LevelMask; + using VirtualizationParameters = detail::VirtualizationParameters; + struct LoadedCell { + Ref data_cell; + VirtualizationParameters virt; + CellUsageTree::NodePtr tree_node; // TODO: inline_vector? + }; + + using Hash = CellHash; + static_assert(std::is_standard_layout::value, "Cell::Hash is not a standard layout type"); + static_assert(sizeof(Hash) == hash_bytes, "Cell::Hash size is not equal to hash_bytes"); + //typedef td::BitArray hash_t; + + Cell* make_copy() const final { + throw WriteError(); + } + + // load interface + virtual td::Result load_cell() const = 0; + virtual Ref virtualize(VirtualizationParameters virt) const; + virtual td::uint32 get_virtualization() const = 0; + virtual CellUsageTree::NodePtr get_tree_node() const = 0; + virtual bool is_loaded() const = 0; + + // hash and level + virtual LevelMask get_level_mask() const = 0; + + // level helper function + td::uint32 get_level() const { + return get_level_mask().get_level(); + } + + // hash helper functions + const Hash get_hash(int level = max_level) const { + return do_get_hash(level); + } + + // depth helper function + td::uint16 get_depth(int level = max_level) const { + return do_get_depth(level); + } + + td::Status check_equals_unloaded(const Ref& other) const; + + private: + virtual td::uint16 do_get_depth(td::uint32 level) const = 0; + virtual const Hash do_get_hash(td::uint32 level) const = 0; +}; + +std::ostream& operator<<(std::ostream& os, const Cell& c); +} // namespace vm diff --git a/crypto/vm/cells/CellBuilder.cpp b/crypto/vm/cells/CellBuilder.cpp new file mode 100644 index 00000000..ec933c07 --- /dev/null +++ b/crypto/vm/cells/CellBuilder.cpp @@ -0,0 +1,593 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "vm/cells/CellBuilder.h" + +#include "vm/cells/CellSlice.h" +#include "vm/cells/DataCell.h" + +#include "td/utils/misc.h" +#include "td/utils/format.h" + +#include "openssl/digest.h" + +namespace vm { + +using td::Ref; +using td::RefAny; + +/* + * + * CELL BUILDERS + * + */ + +CellBuilder::~CellBuilder() { + get_thread_safe_counter().add(-1); +} + +CellBuilder::CellBuilder() : bits(0), refs_cnt(0) { + get_thread_safe_counter().add(+1); +} + +Ref CellBuilder::finalize_copy(bool special) const { + auto* vm_state_interface = VmStateInterface::get(); + if (vm_state_interface) { + vm_state_interface->register_cell_create(); + } + auto res = DataCell::create(data, size(), td::span(refs.data(), size_refs()), special); + if (res.is_error()) { + LOG(ERROR) << res.error(); + throw CellWriteError{}; + } + CHECK(res.ok().not_null()); + return res.move_as_ok(); +} + +Ref CellBuilder::finalize_novm(bool special) { + auto res = DataCell::create(data, size(), td::mutable_span(refs.data(), size_refs()), special); + bits = refs_cnt = 0; + if (res.is_error()) { + LOG(ERROR) << res.error(); + throw CellWriteError{}; + } + CHECK(res.ok().not_null()); + return res.move_as_ok(); +} + +Ref CellBuilder::finalize(bool special) { + auto* vm_state_interface = VmStateInterface::get(); + if (vm_state_interface) { + vm_state_interface->register_cell_create(); + } + return finalize_novm(special); +} + +Ref CellBuilder::create_pruned_branch(Ref cell, td::uint32 new_level, td::uint32 virt_level) { + if (cell->is_loaded() && cell->get_level() <= virt_level && cell->get_virtualization() == 0) { + CellSlice cs(NoVm{}, cell); + if (cs.size_refs() == 0) { + return cell; + } + } + return do_create_pruned_branch(std::move(cell), new_level, virt_level); +} +Ref CellBuilder::do_create_pruned_branch(Ref cell, td::uint32 new_level, td::uint32 virt_level) { + auto level_mask = cell->get_level_mask().apply(virt_level); + auto level = level_mask.get_level(); + if (new_level < level + 1) { + throw CellWriteError(); + } + CellBuilder cb; + cb.store_long(static_cast(Cell::SpecialType::PrunnedBranch), 8); + cb.store_long(level_mask.apply_or(Cell::LevelMask::one_level(new_level)).get_mask(), 8); + for (td::uint32 i = 0; i <= level; i++) { + if (level_mask.is_significant(i)) { + cb.store_bytes(cell->get_hash(i).as_slice()); + } + } + for (td::uint32 i = 0; i <= level; i++) { + if (level_mask.is_significant(i)) { + cb.store_long(cell->get_depth(i), 16); + } + } + return cb.finalize(true); +} + +Ref CellBuilder::create_merkle_proof(Ref cell_proof) { + CellBuilder cb; + cb.store_long(static_cast(Cell::SpecialType::MerkleProof), 8); + cb.store_bytes(cell_proof->get_hash(0).as_slice()); + cb.store_long(cell_proof->get_depth(0), Cell::depth_bytes * 8); + cb.store_ref(cell_proof); + return cb.finalize(true); +} + +Ref CellBuilder::create_merkle_update(Ref from_proof, Ref to_proof) { + CellBuilder cb; + cb.store_long(static_cast(Cell::SpecialType::MerkleUpdate), 8); + cb.store_bytes(from_proof->get_hash(0).as_slice()); + cb.store_bytes(to_proof->get_hash(0).as_slice()); + cb.store_long(from_proof->get_depth(0), Cell::depth_bytes * 8); + cb.store_long(to_proof->get_depth(0), Cell::depth_bytes * 8); + cb.store_ref(from_proof); + cb.store_ref(to_proof); + return cb.finalize(true); +} + +void CellBuilder::reset(void) { + while (refs_cnt > 0) { + refs[--refs_cnt].clear(); + } + bits = 0; +} + +CellBuilder& CellBuilder::operator=(const CellBuilder& other) { + bits = other.bits; + refs_cnt = other.refs_cnt; + refs = other.refs; + std::memcpy(data, other.data, (bits + 7) >> 3); + return *this; +} + +CellBuilder& CellBuilder::operator=(CellBuilder&& other) { + bits = other.bits; + refs_cnt = other.refs_cnt; + refs = std::move(other.refs); + other.refs_cnt = 0; + std::memcpy(data, other.data, (bits + 7) >> 3); + return *this; +} + +bool CellBuilder::can_extend_by(std::size_t new_bits, unsigned new_refs) const { + return (new_bits <= Cell::max_bits - bits && new_refs <= (unsigned)Cell::max_refs - refs_cnt); +} + +bool CellBuilder::can_extend_by(std::size_t new_bits) const { + return new_bits <= Cell::max_bits - bits; +} + +CellBuilder& CellBuilder::store_bytes(const unsigned char* str, std::size_t len) { + ensure_throw(len <= Cell::max_bytes); + return store_bits(str, len * 8); +} + +CellBuilder& CellBuilder::store_bytes(const unsigned char* str, const unsigned char* end) { + ensure_throw(end >= str && end <= str + Cell::max_bytes); + return store_bits(str, (end - str) * 8); +} + +CellBuilder& CellBuilder::store_bytes(const char* str, std::size_t len) { + return store_bytes((const unsigned char*)(str), len); +} + +CellBuilder& CellBuilder::store_bytes(const char* str, const char* end) { + return store_bytes((const unsigned char*)(str), (const unsigned char*)(end)); +} + +CellBuilder& CellBuilder::store_bytes(td::Slice s) { + return store_bytes((const unsigned char*)(s.data()), (const unsigned char*)(s.data() + s.size())); +} + +bool CellBuilder::store_bytes_bool(const unsigned char* str, std::size_t len) { + return len <= Cell::max_bytes && store_bits_bool(str, len * 8); +} + +bool CellBuilder::store_bytes_bool(const char* str, std::size_t len) { + return len <= Cell::max_bytes && store_bits_bool((const unsigned char*)str, len * 8); +} + +bool CellBuilder::store_bytes_bool(td::Slice s) { + return store_bytes_bool((const unsigned char*)s.data(), s.size()); +} + +bool CellBuilder::store_bits_bool(const unsigned char* str, std::size_t bit_count, int bit_offset) { + unsigned pos = bits; + if (!prepare_reserve(bit_count)) { + return false; + } + td::bitstring::bits_memcpy(data, pos, str, bit_offset, bit_count); + return true; +} + +CellBuilder& CellBuilder::store_bits(const unsigned char* str, std::size_t bit_count, int bit_offset) { + unsigned pos = bits; + if (prepare_reserve(bit_count)) { + td::bitstring::bits_memcpy(data, pos, str, bit_offset, bit_count); + } + return *this; +} + +CellBuilder& CellBuilder::store_bits(const td::BitSlice& bs) { + return store_bits(bs.get_ptr(), bs.size(), bs.get_offs()); +} + +CellBuilder& CellBuilder::store_bits(const char* str, std::size_t bit_count, int bit_offset) { + return store_bits((const unsigned char*)str, bit_count, bit_offset); +} + +CellBuilder& CellBuilder::store_bits(td::ConstBitPtr bs, std::size_t bit_count) { + return store_bits(bs.ptr, bit_count, bs.offs); +} + +bool CellBuilder::store_bits_bool(td::ConstBitPtr bs, std::size_t bit_count) { + return store_bits_bool(bs.ptr, bit_count, bs.offs); +} + +CellBuilder& CellBuilder::store_bits_same(std::size_t bit_count, bool val) { + unsigned pos = bits; + if (prepare_reserve(bit_count)) { + td::bitstring::bits_memset(data, pos, val, bit_count); + } + return *this; +} + +bool CellBuilder::store_bits_same_bool(std::size_t bit_count, bool val) { + unsigned pos = bits; + if (!prepare_reserve(bit_count)) { + return false; + } + td::bitstring::bits_memset(data, pos, val, bit_count); + return true; +} + +inline bool CellBuilder::prepare_reserve(std::size_t bit_count) { + if (!can_extend_by(bit_count)) { + return false; + } else { + bits += (unsigned)bit_count; + return true; + } +} + +td::BitSliceWrite CellBuilder::reserve_slice(std::size_t bit_count) { + unsigned offs = bits; + if (prepare_reserve(bit_count)) { + return td::BitSliceWrite{Ref{this}, data, offs, (unsigned)bit_count}; + } else { + return td::BitSliceWrite{}; + } +} + +CellBuilder& CellBuilder::reserve_slice(std::size_t bit_count, td::BitSliceWrite& bsw) { + unsigned offs = bits; + if (prepare_reserve(bit_count)) { + bsw.assign(Ref{this}, data, offs, (unsigned)bit_count); + } else { + bsw.forget(); + } + return *this; +} + +bool CellBuilder::store_bool_bool(bool val) { + if (can_extend_by_fast(1)) { + store_long(val, 1); + return true; + } else { + return false; + } +} + +bool CellBuilder::store_long_bool(long long val, unsigned val_bits) { + if (val_bits > 64 || !can_extend_by(val_bits)) { + return false; + } + store_long(val, val_bits); + return true; +} + +bool CellBuilder::store_long_rchk_bool(long long val, unsigned val_bits) { + if (val_bits > 64 || !can_extend_by(val_bits)) { + return false; + } + if (val_bits < 64 && (val < static_cast(std::numeric_limits::max() << (val_bits - 1)) || + val >= (1LL << (val_bits - 1)))) { + return false; + } + store_long(val, val_bits); + return true; +} + +bool CellBuilder::store_ulong_rchk_bool(unsigned long long val, unsigned val_bits) { + if (val_bits > 64 || !can_extend_by(val_bits)) { + return false; + } + if (val_bits < 64 && val >= (1ULL << val_bits)) { + return false; + } + store_long(val, val_bits); + return true; +} + +CellBuilder& CellBuilder::store_long(long long val, unsigned val_bits) { + return store_long_top(val << (64 - val_bits), val_bits); +} + +CellBuilder& CellBuilder::store_long_top(unsigned long long val, unsigned top_bits) { + unsigned pos = bits; + auto reserve_ok = prepare_reserve(top_bits); + ensure_throw(reserve_ok); + td::bitstring::bits_store_long_top(data, pos, val, top_bits); + return *this; +} + +bool CellBuilder::store_uint_less(unsigned upper_bound, unsigned long long val) { + return val < upper_bound && store_long_bool(val, 32 - td::count_leading_zeroes32(upper_bound - 1)); +} + +bool CellBuilder::store_uint_leq(unsigned upper_bound, unsigned long long val) { + return val <= upper_bound && store_long_bool(val, 32 - td::count_leading_zeroes32(upper_bound)); +} + +bool CellBuilder::store_int256_bool(const td::BigInt256& val, unsigned val_bits, bool sgnd) { + unsigned pos = bits; + if (!prepare_reserve(val_bits)) { + return false; + } + if (val.export_bits(data, pos, val_bits, sgnd)) { + return true; + } else { + bits = pos; + return false; + } +} + +CellBuilder& CellBuilder::store_int256(const td::BigInt256& val, unsigned val_bits, bool sgnd) { + return ensure_pass(store_int256_bool(val, val_bits, sgnd)); +} + +bool CellBuilder::store_int256_bool(td::RefInt256 val, unsigned val_bits, bool sgnd) { + return val.not_null() && store_int256_bool(*val, val_bits, sgnd); +} + +bool CellBuilder::store_builder_ref_bool(vm::CellBuilder&& cb) { + return store_ref_bool(cb.finalize()); +} + +bool CellBuilder::store_ref_bool(Ref ref) { + if (refs_cnt < Cell::max_refs && ref.not_null()) { + refs[refs_cnt++] = std::move(ref); + return true; + } else { + return false; + } +} + +CellBuilder& CellBuilder::store_ref(Ref ref) { + return ensure_pass(store_ref_bool(std::move(ref))); +} + +bool CellBuilder::append_data_cell_bool(const DataCell& cell) { + unsigned len = cell.size(); + if (can_extend_by(len, cell.size_refs())) { + unsigned pos = bits; + ensure_throw(prepare_reserve(len)); + td::bitstring::bits_memcpy(data, pos, cell.get_data(), 0, len); + for (unsigned i = 0; i < cell.size_refs(); i++) { + refs[refs_cnt++] = cell.get_ref(i); + } + return true; + } else { + return false; + } +} + +CellBuilder& CellBuilder::append_data_cell(const DataCell& cell) { + return ensure_pass(append_data_cell_bool(cell)); +} + +bool CellBuilder::append_data_cell_bool(Ref cell_ref) { + return append_data_cell_bool(*cell_ref); +} + +CellBuilder& CellBuilder::append_data_cell(Ref cell_ref) { + return ensure_pass(append_data_cell_bool(std::move(cell_ref))); +} + +bool CellBuilder::append_builder_bool(const CellBuilder& cb) { + unsigned len = cb.size(); + if (can_extend_by(len, cb.size_refs())) { + unsigned pos = bits; + ensure_throw(prepare_reserve(len)); + td::bitstring::bits_memcpy(data, pos, cb.get_data(), 0, len); + for (unsigned i = 0; i < cb.size_refs(); i++) { + refs[refs_cnt++] = cb.get_ref(i); + } + return true; + } else { + return false; + } +} + +CellBuilder& CellBuilder::append_builder(const CellBuilder& cb) { + return ensure_pass(append_builder_bool(cb)); +} + +bool CellBuilder::append_builder_bool(Ref cb_ref) { + return append_builder_bool(*cb_ref); +} + +CellBuilder& CellBuilder::append_builder(Ref cb_ref) { + return ensure_pass(append_builder_bool(std::move(cb_ref))); +} + +bool CellBuilder::append_cellslice_bool(const CellSlice& cs) { + unsigned len = cs.size(); + if (can_extend_by(len, cs.size_refs())) { + int pos = bits; + ensure_throw(prepare_reserve(len)); + td::bitstring::bits_memcpy(td::BitPtr{data, pos}, cs.data_bits(), len); + for (unsigned i = 0; i < cs.size_refs(); i++) { + refs[refs_cnt++] = cs.prefetch_ref(i); + } + return true; + } else { + return false; + } +} + +CellBuilder& CellBuilder::append_cellslice(const CellSlice& cs) { + return ensure_pass(append_cellslice_bool(cs)); +} + +bool CellBuilder::append_cellslice_bool(Ref cs_ref) { + return cs_ref.not_null() && append_cellslice_bool(*cs_ref); +} + +CellBuilder& CellBuilder::append_cellslice(Ref cs) { + return ensure_pass(append_cellslice_bool(cs)); +} + +bool CellBuilder::append_cellslice_chk(const CellSlice& cs, unsigned size_ext) { + return cs.size_ext() == size_ext && append_cellslice_bool(cs); +} + +bool CellBuilder::append_cellslice_chk(Ref cs_ref, unsigned size_ext) { + return cs_ref.not_null() && append_cellslice_chk(*cs_ref, size_ext); +} + +bool CellBuilder::append_bitstring(const td::BitString& bs) { + return store_bits_bool(bs.cbits(), bs.size()); +} + +bool CellBuilder::append_bitstring(Ref bs_ref) { + return bs_ref.not_null() && append_bitstring(*bs_ref); +} + +bool CellBuilder::append_bitstring_chk(const td::BitString& bs, unsigned size) { + return bs.size() == size && store_bits_bool(bs.cbits(), size); +} + +bool CellBuilder::append_bitstring_chk(Ref bs_ref, unsigned size) { + return bs_ref.not_null() && append_bitstring_chk(*bs_ref, size); +} + +bool CellBuilder::append_bitslice(const td::BitSlice& bs) { + return store_bits_bool(bs.bits(), bs.size()); +} + +bool CellBuilder::store_maybe_ref(Ref cell) { + if (cell.is_null()) { + return store_long_bool(0, 1); + } else { + return store_long_bool(1, 1) && store_ref_bool(std::move(cell)); + } +} + +void CellBuilder::flush(unsigned char d[2]) const { + assert(refs_cnt <= Cell::max_refs && bits <= Cell::max_bits); + + unsigned l = (bits >> 3); + if (bits & 7) { + int m = (0x80 >> (bits & 7)); + data[l] = static_cast((data[l] & -m) | m); + d[1] = static_cast(2 * l + 1); + } else { + d[1] = static_cast(2 * l); + } + d[0] = static_cast(refs_cnt); +} + +const unsigned char* CellBuilder::compute_hash(unsigned char buffer[Cell::hash_bytes]) const { + unsigned char tmp[2]; + flush(tmp); + digest::SHA256 hasher(tmp, 2); + hasher.feed(data, (bits + 7) >> 3); + for (unsigned i = 0; i < refs_cnt; i++) { + hasher.feed(refs[i]->get_hash().as_slice().data(), Cell::hash_bytes); + } + auto extracted_size = hasher.extract(buffer); + DCHECK(extracted_size == Cell::hash_bytes); + return buffer; +} + +int CellBuilder::serialize(unsigned char* buff, int buff_size) const { + int len = get_serialized_size(); + if (len > buff_size) { + return 0; + } + flush(buff); + std::memcpy(buff + 2, data, len - 2); + return len; +} + +CellBuilder* CellBuilder::make_copy() const { + CellBuilder* c = new CellBuilder(); + if (!c) { + throw CellWriteError(); + } + c->bits = bits; + std::memcpy(c->data, data, (bits + 7) >> 3); + c->refs_cnt = refs_cnt; + for (unsigned i = 0; i < refs_cnt; i++) { + c->refs[i] = refs[i]; + } + return c; +} + +CellSlice CellBuilder::as_cellslice() const& { + return CellSlice{finalize_copy()}; +} + +Ref CellBuilder::as_cellslice_ref() const& { + return Ref{true, finalize_copy()}; +} + +CellSlice CellBuilder::as_cellslice() && { + return CellSlice{finalize()}; +} + +Ref CellBuilder::as_cellslice_ref() && { + return Ref{true, finalize()}; +} + +bool CellBuilder::contents_equal(const CellSlice& cs) const { + if (size() != cs.size() || size_refs() != cs.size_refs()) { + return false; + } + if (td::bitstring::bits_memcmp(data_bits(), cs.data_bits(), size())) { + return false; + } + for (unsigned i = 0; i < size_refs(); i++) { + if (refs[i]->get_hash() != cs.prefetch_ref(i)->get_hash()) { + return false; + } + } + return true; +} + +std::string CellBuilder::serialize() const { + unsigned char buff[Cell::max_serialized_bytes]; + int len = serialize(buff, sizeof(buff)); + return std::string(buff, buff + len); +} + +std::string CellBuilder::to_hex() const { + unsigned char buff[Cell::max_serialized_bytes]; + int len = serialize(buff, sizeof(buff)); + char hex_buff[Cell::max_serialized_bytes * 2 + 1]; + for (int i = 0; i < len; i++) { + sprintf(hex_buff + 2 * i, "%02x", buff[i]); + } + return hex_buff; +} + +std::ostream& operator<<(std::ostream& os, const CellBuilder& cb) { + return os << cb.to_hex(); +} +} // namespace vm diff --git a/crypto/vm/cells/CellBuilder.h b/crypto/vm/cells/CellBuilder.h new file mode 100644 index 00000000..0095018e --- /dev/null +++ b/crypto/vm/cells/CellBuilder.h @@ -0,0 +1,238 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once +#include "vm/cells/DataCell.h" +#include "vm/cells/VirtualCell.h" +#include "vm/vmstate.h" +#include "common/refint.h" + +#include "td/utils/ThreadSafeCounter.h" + +namespace vm { + +class CellSlice; +class DataCell; + +class CellBuilder : public td::CntObject { + public: + struct CellWriteError {}; + struct CellCreateError {}; + + private: + unsigned bits; + unsigned refs_cnt; + std::array, Cell::max_refs> refs; + mutable unsigned char data[Cell::max_bytes]; + static td::NamedThreadSafeCounter::CounterRef get_thread_safe_counter() { + static auto res = td::NamedThreadSafeCounter::get_default().get_counter("CellBuilder"); + return res; + } + + public: + CellBuilder(); + virtual ~CellBuilder() override; + + static Ref create_pruned_branch(Ref cell, td::uint32 new_level, td::uint32 virt_level = Cell::max_level); + static Ref do_create_pruned_branch(Ref cell, td::uint32 new_level, + td::uint32 virt_level = Cell::max_level); + static Ref create_merkle_proof(Ref cell_proof); + static Ref create_merkle_update(Ref from_proof, Ref to_proof); + + unsigned get_refs_cnt() const { + return refs_cnt; + } + unsigned get_bits() const { + return bits; + } + unsigned size_refs() const { + return refs_cnt; + } + unsigned size() const { + return bits; + } + unsigned size_ext() const { + return (refs_cnt << 16) + bits; + } + unsigned remaining_bits() const { + return Cell::max_bits - bits; + } + unsigned remaining_refs() const { + return Cell::max_refs - refs_cnt; + } + const unsigned char* get_data() const { + return data; + } + td::ConstBitPtr data_bits() const { + return data; + } + Ref get_ref(unsigned idx) const { + return idx < refs_cnt ? refs[idx] : Ref{}; + } + void reset(); + CellBuilder& operator=(const CellBuilder&); + CellBuilder& operator=(CellBuilder&&); + CellBuilder& store_bytes(const char* str, std::size_t len); + CellBuilder& store_bytes(const char* str, const char* end); + CellBuilder& store_bytes(const unsigned char* str, std::size_t len); + CellBuilder& store_bytes(const unsigned char* str, const unsigned char* end); + CellBuilder& store_bytes(td::Slice s); + bool store_bytes_bool(const unsigned char* str, std::size_t len); + bool store_bytes_bool(const char* str, std::size_t len); + bool store_bytes_bool(td::Slice s); + CellBuilder& store_bits(const unsigned char* str, std::size_t bit_count, int bit_offset = 0); + CellBuilder& store_bits(const char* str, std::size_t bit_count, int bit_offset = 0); + CellBuilder& store_bits(td::ConstBitPtr bs, std::size_t bit_count); + CellBuilder& store_bits(const td::BitSlice& bs); + CellBuilder& store_bits_same(std::size_t bit_count, bool val); + bool store_bits_bool(const unsigned char* str, std::size_t bit_count, int bit_offset = 0); + bool store_bits_bool(td::ConstBitPtr bs, std::size_t bit_count); + template + bool store_bits_bool(const td::BitArray& ba) { + return store_bits_bool(ba.cbits(), n); + } + bool store_bits_same_bool(std::size_t bit_count, bool val); + CellBuilder& store_zeroes(std::size_t bit_count) { + return store_bits_same(bit_count, false); + } + CellBuilder& store_ones(std::size_t bit_count) { + return store_bits_same(bit_count, true); + } + bool store_zeroes_bool(std::size_t bit_count) { + return store_bits_same_bool(bit_count, false); + } + bool store_ones_bool(std::size_t bit_count) { + return store_bits_same_bool(bit_count, true); + } + td::BitSliceWrite reserve_slice(std::size_t bit_count); + CellBuilder& reserve_slice(std::size_t bit_count, td::BitSliceWrite& bsw); + bool store_long_bool(long long val, unsigned val_bits = 64); + bool store_long_rchk_bool(long long val, unsigned val_bits = 64); + bool store_ulong_rchk_bool(unsigned long long val, unsigned val_bits = 64); + bool store_uint_less(unsigned upper_bound, unsigned long long val); + bool store_uint_leq(unsigned upper_bound, unsigned long long val); + CellBuilder& store_long(long long val, unsigned val_bits = 64); + // bool store_long_top_bool(unsigned long long val, unsigned top_bits); + CellBuilder& store_long_top(unsigned long long val, unsigned top_bits); + bool store_bool_bool(bool val); + bool store_int256_bool(const td::BigInt256& val, unsigned val_bits, bool sgnd = true); + bool store_int256_bool(td::RefInt256 val, unsigned val_bits, bool sgnd = true); + bool store_uint256_bool(const td::BigInt256& val, unsigned val_bits) { + return store_int256_bool(val, val_bits, false); + } + bool store_uint256_bool(td::RefInt256 val, unsigned val_bits) { + return store_int256_bool(std::move(val), val_bits, false); + } + CellBuilder& store_int256(const td::BigInt256& val, unsigned val_bits, bool sgnd = true); + CellBuilder& store_uint256(const td::BigInt256& val, unsigned val_bits) { + return store_int256(val, val_bits, false); + } + bool store_builder_ref_bool(vm::CellBuilder&& cb); + bool store_ref_bool(Ref r); + CellBuilder& store_ref(Ref r); + bool append_data_cell_bool(const DataCell& cell); + CellBuilder& append_data_cell(const DataCell& cell); + bool append_data_cell_bool(Ref cell_ref); + CellBuilder& append_data_cell(Ref cell_ref); + bool append_builder_bool(const CellBuilder& cb); + CellBuilder& append_builder(const CellBuilder& cb); + bool append_builder_bool(Ref cb_ref); + CellBuilder& append_builder(Ref cb_ref); + bool append_cellslice_bool(const CellSlice& cs); + CellBuilder& append_cellslice(const CellSlice& cs); + bool append_cellslice_bool(Ref cs_ref); + CellBuilder& append_cellslice(Ref cs_ref); + bool append_cellslice_chk(const CellSlice& cs, unsigned size_ext); + bool append_cellslice_chk(Ref cs, unsigned size_ext); + bool append_bitstring(const td::BitString& bs); + bool append_bitstring(Ref bs_ref); + bool append_bitstring_chk(const td::BitString& bs, unsigned size); + bool append_bitstring_chk(Ref bs, unsigned size); + bool append_bitslice(const td::BitSlice& bs); + bool store_maybe_ref(Ref cell); + bool contents_equal(const CellSlice& cs) const; + CellBuilder* make_copy() const override; + bool can_extend_by(std::size_t bits) const; + bool can_extend_by(std::size_t bits, unsigned refs) const; + Ref finalize_copy(bool special = false) const; + Ref finalize(bool special = false); + Ref finalize_novm(bool special = false); + bool finalize_to(Ref& res, bool special = false) { + return (res = finalize(special)).not_null(); + } + CellSlice as_cellslice() const &; + CellSlice as_cellslice() &&; + Ref as_cellslice_ref() const &; + Ref as_cellslice_ref() &&; + static td::int64 get_total_cell_builders() { + return get_thread_safe_counter().sum(); + } + int get_serialized_size() const { + return ((bits + 23) >> 3); + } + int serialize(unsigned char* buff, int buff_size) const; + std::string serialize() const; + std::string to_hex() const; + const unsigned char* compute_hash(unsigned char buffer[Cell::hash_bytes]) const; + + const CellBuilder& ensure_pass(bool cond) const { + ensure_throw(cond); + return *this; + } + CellBuilder& ensure_pass(bool cond) { + ensure_throw(cond); + return *this; + } + void ensure_throw(bool cond) const { + if (!cond) { + throw CellCreateError{}; + } + } + + private: + void flush(unsigned char d[2]) const; + bool prepare_reserve(std::size_t bit_count); + bool can_extend_by_fast(unsigned bits_req) const { + return (int)bits <= (int)(Cell::max_bits - bits_req); + } + bool can_extend_by_fast(unsigned bits_req, unsigned refs_req) const { + return (int)bits <= (int)(Cell::max_bits - bits_req) && (int)refs_cnt <= (int)(Cell::max_refs - refs_req); + } + bool can_extend_by_fast2(unsigned bits_req) const { + return bits + bits_req <= Cell::max_bits; + } + bool can_extend_by_fast2(unsigned bits_req, unsigned refs_req) const { + return bits + bits_req <= Cell::max_bits && refs_cnt + refs_req <= Cell::max_refs; + } +}; + +std::ostream& operator<<(std::ostream& os, const CellBuilder& cb); + +template +CellBuilder& operator<<(CellBuilder& cb, const T& val) { + return cb.ensure_pass(val.serialize(cb)); +} + +template +Ref& operator<<(Ref& cb_ref, const T& val) { + bool res = val.serialize(cb_ref.write()); + cb_ref->ensure_throw(res); + return cb_ref; +} + +} // namespace vm diff --git a/crypto/vm/cells/CellHash.cpp b/crypto/vm/cells/CellHash.cpp new file mode 100644 index 00000000..f801e2e4 --- /dev/null +++ b/crypto/vm/cells/CellHash.cpp @@ -0,0 +1,28 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "vm/cells/CellHash.h" + +#include "td/utils/StringBuilder.h" +#include "td/utils/Slice.h" + +namespace vm { +td::StringBuilder &operator<<(td::StringBuilder &sb, const CellHash &hash) { + return sb << hash.to_hex(); +} +} // namespace vm diff --git a/crypto/vm/cells/CellHash.h b/crypto/vm/cells/CellHash.h new file mode 100644 index 00000000..0b5c122d --- /dev/null +++ b/crypto/vm/cells/CellHash.h @@ -0,0 +1,92 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once +#include "vm/cells/CellTraits.h" +#include "common/bitstring.h" + +#include "td/utils/as.h" + +#include +namespace td { +class StringBuilder; +} + +namespace vm { +struct CellHash { + public: + td::Slice as_slice() const { + return td::Slice(hash_.data(), hash_.size()); + } + td::MutableSlice as_slice() { + return td::MutableSlice(hash_.data(), hash_.size()); + } + bool operator==(const CellHash& other) const { + return hash_ == other.hash_; + } + bool operator<(const CellHash& other) const { + return hash_ < other.hash_; + } + bool operator!=(const CellHash& other) const { + return hash_ != other.hash_; + } + std::string to_hex() const { + return td::ConstBitPtr{hash_.data()}.to_hex(hash_.size() * 8); + } + friend td::StringBuilder& operator<<(td::StringBuilder& sb, const CellHash& hash); + td::ConstBitPtr bits() const { + return td::ConstBitPtr{hash_.data()}; + } + td::BitPtr bits() { + return td::BitPtr{hash_.data()}; + } + td::BitSlice as_bitslice() const { + return td::BitSlice{hash_.data(), (unsigned int)hash_.size() * 8}; + } + const std::array& as_array() const { + return hash_; + } + + static CellHash from_slice(td::Slice slice) { + CellHash res; + CHECK(slice.size() == res.hash_.size()); + td::MutableSlice(res.hash_.data(), res.hash_.size()).copy_from(slice); + return res; + } + + private: + std::array hash_; +}; +} // namespace vm + +namespace std { +template <> +struct hash { + typedef vm::CellHash argument_type; + typedef std::size_t result_type; + result_type operator()(argument_type const& s) const noexcept { + return td::as(s.as_slice().ubegin()); + } +}; +} // namespace std +namespace vm { +template +H AbslHashValue(H h, const CellHash& cell_hash) { + return H::combine(std::move(h), std::hash()(cell_hash)); +} +} // namespace vm diff --git a/crypto/vm/cells/CellSlice.cpp b/crypto/vm/cells/CellSlice.cpp new file mode 100644 index 00000000..3b2bb745 --- /dev/null +++ b/crypto/vm/cells/CellSlice.cpp @@ -0,0 +1,1066 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "vm/cells/CellSlice.h" +#include "vm/excno.hpp" +#include "td/utils/bits.h" + +namespace vm { + +/* +CellSlice::CellSlice(Ref&& ref) : cell(std::move(ref)), bits_st(0), refs_st(0), ptr(0), zd(0) { + init_bits_refs(); +} +*/ + +CellSlice::CellSlice(VirtualCell::LoadedCell loaded_cell) + : virt(loaded_cell.virt) + , cell(std::move(loaded_cell.data_cell)) + , tree_node(std::move(loaded_cell.tree_node)) + , bits_st(0) + , refs_st(0) + , ptr(0) + , zd(0) { + init_bits_refs(); +} + +CellSlice::CellSlice() : bits_st(0), refs_st(0), bits_en(0), refs_en(0), ptr(0), zd(0) { +} + +namespace { +Cell::LoadedCell load_cell_nothrow(const Ref& ref) { + auto res = ref->load_cell(); + if (res.is_ok()) { + auto ld = res.move_as_ok(); + CHECK(ld.virt.get_virtualization() == 0 || ld.data_cell->special_type() != Cell::SpecialType::PrunnedBranch); + return ld; + } + return {}; +} + +Cell::LoadedCell load_cell_nothrow(const Ref& ref, int mode) { + auto res = ref->load_cell(); + if (res.is_ok()) { + auto ld = res.move_as_ok(); + CHECK(ld.virt.get_virtualization() == 0 || ld.data_cell->special_type() != Cell::SpecialType::PrunnedBranch); + if ((mode >> (ld.data_cell->is_special() ? 1 : 0)) & 1) { + return ld; + } + } + return {}; +} + +} // namespace + +CellSlice::CellSlice(NoVm, Ref ref) : CellSlice(load_cell_nothrow(std::move(ref))) { +} +CellSlice::CellSlice(NoVmOrd, Ref ref) : CellSlice(load_cell_nothrow(std::move(ref), 1)) { +} +CellSlice::CellSlice(NoVmSpec, Ref ref) : CellSlice(load_cell_nothrow(std::move(ref), 2)) { +} +CellSlice::CellSlice(Ref ref) : CellSlice(VirtualCell::LoadedCell{std::move(ref), {}, {}}) { +} +CellSlice::CellSlice(const CellSlice& cs) = default; + +bool CellSlice::load(VirtualCell::LoadedCell loaded_cell) { + virt = loaded_cell.virt; + cell = std::move(loaded_cell.data_cell); + tree_node = std::move(loaded_cell.tree_node); + bits_st = 0; + refs_st = 0; + ptr = 0; + zd = 0; + init_bits_refs(); + return cell.not_null(); +} + +bool CellSlice::load(NoVm, Ref cell_ref) { + return load(load_cell_nothrow(std::move(cell_ref))); +} +bool CellSlice::load(NoVmOrd, Ref cell_ref) { + return load(load_cell_nothrow(std::move(cell_ref), 1)); +} +bool CellSlice::load(NoVmSpec, Ref cell_ref) { + return load(load_cell_nothrow(std::move(cell_ref), 2)); +} +bool CellSlice::load(Ref dc_ref) { + return load(VirtualCell::LoadedCell{std::move(dc_ref), {}, {}}); +} + +/* +CellSlice::CellSlice(Ref dc_ref, unsigned _bits_en, unsigned _refs_en, unsigned _bits_st, unsigned _refs_st) + : cell(std::move(dc_ref)) + , bits_st(_bits_st) + , refs_st(_refs_st) + , bits_en(_bits_en) + , refs_en(_refs_en) + , ptr(0) + , zd(0) { + assert(bits_st <= bits_en && refs_st <= refs_en); + if (cell.is_null()) { + assert(!bits_en && !refs_en); + } else { + assert(bits_en <= cell->get_bits() && refs_en <= cell->get_refs_cnt()); + if (bits_en) { + init_preload(); + } + } +} +*/ + +CellSlice::CellSlice(const CellSlice& cs, unsigned _bits_en, unsigned _refs_en, unsigned _bits_st, unsigned _refs_st) + : virt(cs.virt) + , cell(cs.cell) + , tree_node(cs.tree_node) + , bits_st(cs.bits_st + _bits_st) + , refs_st(cs.refs_st + _refs_st) + , bits_en(cs.bits_st + _bits_en) + , refs_en(cs.refs_st + _refs_en) + , ptr(0) + , zd(0) { + assert(_bits_st <= _bits_en && _refs_st <= _refs_en); + assert(_bits_en <= cs.size() && _refs_en <= cs.size_refs()); + if (_bits_en > _bits_st) { + init_preload(); + } +} + +CellSlice::CellSlice(const CellSlice& cs, unsigned _bits_en, unsigned _refs_en) + : virt(cs.virt) + , cell(cs.cell) + , tree_node(cs.tree_node) + , bits_st(cs.bits_st) + , refs_st(cs.refs_st) + , bits_en(cs.bits_st + _bits_en) + , refs_en(cs.refs_st + _refs_en) + , ptr(0) + , zd(0) { + assert(_bits_en <= cs.size() && _refs_en <= cs.size_refs()); + if (_bits_en) { + init_preload(); + } +} + +Cell::LoadedCell CellSlice::move_as_loaded_cell() { + Cell::LoadedCell res{std::move(cell), std::move(virt), std::move(tree_node)}; + clear(); + return res; +} + +void CellSlice::init_bits_refs() { + if (cell.is_null()) { + bits_en = 0; + refs_en = 0; + } else { + bits_en = cell->get_bits(); + refs_en = cell->get_refs_cnt(); + if (bits_en) { + init_preload(); + } + } +} + +void CellSlice::init_preload() const { + if (bits_st >= bits_en) { + zd = 0; + return; + } + ptr = cell->get_data() + (bits_st >> 3) + 1; + unsigned t = 8 - (bits_st & 7); + z = (((unsigned long long)ptr[-1]) << (64 - t)); + zd = std::min(t, size()); +} + +void CellSlice::clear() { + zd = 0; + bits_en = bits_st = 0; + refs_st = refs_en = 0; + ptr = 0; + cell.clear(); +} + +/* +void CellSlice::error() { + // maybe throw a different simple exception, and convert it at the upper level of the vm + throw VmError{Excno::cell_und, "cell deserialization error"}; +} +*/ + +unsigned CellSlice::get_cell_level() const { + return cell->get_level_mask().apply(virt.get_level()).get_level(); +} + +unsigned CellSlice::get_level() const { + unsigned l = 0; + for (unsigned i = refs_st; i < refs_en; i++) { + auto res = cell->get_ref(i)->virtualize(child_virt()); + unsigned l1 = res->get_level(); + // maybe l1 = cell->get_ref(i)->get_level_mask().apply(virt.get_level()).get_level(); + if (l1 > l) { + l = l1; + } + } + return l; +} + +bool CellSlice::advance(unsigned bits) { + if (have(bits)) { + bits_st += bits; + if (zd <= bits) { // NB: if we write here zd < bits, we obtain bug with z <<= 64 + init_preload(); + } else { + zd -= bits; + z <<= bits; + } + return true; + } else { + return false; + } +} + +bool CellSlice::advance_refs(unsigned refs = 1) { + if (have_refs(refs)) { + refs_st += refs; + return true; + } else { + return false; + } +} + +bool CellSlice::advance_ext(unsigned bits, unsigned refs) { + if (have(bits) && have_refs(refs)) { + refs_st += refs; + return advance(bits); + } else { + return false; + } +} + +bool CellSlice::advance_ext(unsigned bits_refs) { + return advance_ext(bits_refs >> 16, bits_refs & 0xffff); +} + +// (PRIVATE) +// assume: at least `req_bits` bits can be preloaded +void CellSlice::preload_at_least(unsigned req_bits) const { + assert(req_bits <= 64 && have(req_bits) && ptr); + if (req_bits <= zd) { + return; + } + int remain = bits_en - bits_st - zd; + if (zd <= 32 && remain > 24) { + z |= (((unsigned long long)td::bswap32(td::as(ptr))) << (32 - zd)); + ptr += 4; + if (remain <= 32) { + zd += remain; + return; + } + zd += 32; + remain -= 32; + } + while (zd < req_bits && remain > 0) { + if (zd > 56) { + z |= (*ptr >> (zd - 56)); + return; + } + z |= (((unsigned long long)*ptr++) << (56 - zd)); + if (remain <= 8) { + zd += remain; + return; + } + zd += 8; + remain -= 8; + } +} + +int CellSlice::prefetch_octet() const { + if (!have(8)) { + return -1; + } else { + preload_at_least(8); + return (int)(z >> 56); + } +} + +int CellSlice::fetch_octet() { + if (!have(8)) { + return -1; + } else { + preload_at_least(8); + int res = (int)(z >> 56); + z <<= 8; + zd -= 8; + return res; + } +} + +unsigned long long CellSlice::fetch_ulong(unsigned bits) { + if (!have(bits) || bits > 64) { + return fetch_ulong_eof; + } else if (!bits) { + return 0; + } else if (bits <= 56) { + preload_at_least(bits); + unsigned long long res = (z >> (64 - bits)); + z <<= bits; + assert(zd >= bits); + zd -= bits; + bits_st += bits; + return res; + } else { + preload_at_least(bits); + unsigned long long res = (z >> (64 - bits)); + advance(bits); + return res; + } +} + +unsigned long long CellSlice::prefetch_ulong(unsigned bits) const { + if (!have(bits) || bits > 64) { + return fetch_ulong_eof; + } else if (!bits) { + return 0; + } else { + preload_at_least(bits); + return (z >> (64 - bits)); + } +} + +unsigned long long CellSlice::prefetch_ulong_top(unsigned& bits) const { + if (bits > size()) { + bits = size(); + } + if (!bits) { + return 0; + } + preload_at_least(bits); + return z; +} + +long long CellSlice::fetch_long(unsigned bits) { + if (!have(bits) || bits > 64) { + return fetch_long_eof; + } else if (!bits) { + return 0; + } else if (bits <= 56) { + preload_at_least(bits); + long long res = ((long long)z >> (64 - bits)); + z <<= bits; + assert(zd >= bits); + zd -= bits; + bits_st += bits; + return res; + } else { + preload_at_least(bits); + long long res = ((long long)z >> (64 - bits)); + advance(bits); + return res; + } +} + +long long CellSlice::prefetch_long(unsigned bits) const { + if (!have(bits) || bits > 64) { + return fetch_long_eof; + } else if (!bits) { + return 0; + } else { + preload_at_least(bits); + return ((long long)z >> (64 - bits)); + } +} + +bool CellSlice::fetch_long_bool(unsigned bits, long long& res) { + if (bits > 64 || !have(bits)) { + return false; + } + res = fetch_long(bits); + return true; +} + +bool CellSlice::prefetch_long_bool(unsigned bits, long long& res) const { + if (bits > 64 || !have(bits)) { + return false; + } + res = prefetch_long(bits); + return true; +} + +bool CellSlice::fetch_ulong_bool(unsigned bits, unsigned long long& res) { + if (bits > 64 || !have(bits)) { + return false; + } + res = fetch_ulong(bits); + return true; +} + +bool CellSlice::prefetch_ulong_bool(unsigned bits, unsigned long long& res) const { + if (bits > 64 || !have(bits)) { + return false; + } + res = prefetch_ulong(bits); + return true; +} + +bool CellSlice::fetch_bool_to(bool& res) { + if (!have(1)) { + return false; + } else { + res = (bool)fetch_ulong(1); + return true; + } +} + +bool CellSlice::fetch_bool_to(int& res) { + if (!have(1)) { + return false; + } else { + res = (int)fetch_ulong(1); + return true; + } +} + +bool CellSlice::fetch_bool_to(int& res, int mask) { + if (!have(1)) { + return false; + } else if (fetch_ulong(1)) { + res |= mask; + } else { + res &= ~mask; + } + return true; +} + +bool CellSlice::fetch_uint_to(unsigned bits, unsigned long long& res) { + if (bits > 64 || !have(bits)) { + return false; + } else { + res = fetch_ulong(bits); + return true; + } +} + +bool CellSlice::fetch_uint_to(unsigned bits, long long& res) { + if (bits > 64 || !have(bits)) { + return false; + } else { + res = (long long)fetch_ulong(bits); + return res >= 0; + } +} + +bool CellSlice::fetch_uint_to(unsigned bits, unsigned long& res) { + if (bits > 8 * sizeof(unsigned long) || !have(bits)) { + return false; + } else { + res = static_cast(fetch_ulong(bits)); + return true; + } +} + +bool CellSlice::fetch_uint_to(unsigned bits, long& res) { + if (bits > 8 * sizeof(long) || !have(bits)) { + return false; + } else { + res = static_cast(fetch_ulong(bits)); + return res >= 0; + } +} + +bool CellSlice::fetch_uint_to(unsigned bits, unsigned& res) { + if (bits > 32 || !have(bits)) { + return false; + } else { + res = (unsigned)fetch_ulong(bits); + return true; + } +} + +bool CellSlice::fetch_uint_to(unsigned bits, int& res) { + if (bits > 32 || !have(bits)) { + return false; + } else { + res = (int)fetch_ulong(bits); + return res >= 0; + } +} + +bool CellSlice::fetch_int_to(unsigned bits, long long& res) { + if (bits > 64 || !have(bits)) { + return false; + } else { + res = fetch_long(bits); + return true; + } +} + +bool CellSlice::fetch_int_to(unsigned bits, int& res) { + if (bits > 32 || !have(bits)) { + return false; + } else { + res = (int)fetch_long(bits); + return true; + } +} + +bool CellSlice::fetch_uint_less(unsigned upper_bound, int& res) { + unsigned bits = 32 - td::count_leading_zeroes32(upper_bound - 1); + if (!upper_bound || bits > 31 || !have(bits)) { + return false; + } else { + res = (int)fetch_ulong(bits); + return (unsigned)res < upper_bound; + } +} + +bool CellSlice::fetch_uint_less(unsigned upper_bound, unsigned& res) { + unsigned bits = 32 - td::count_leading_zeroes32(upper_bound - 1); + if (!upper_bound || bits > 32 || !have(bits)) { + return false; + } else { + res = (unsigned)fetch_ulong(bits); + return res < upper_bound; + } +} + +bool CellSlice::fetch_uint_leq(unsigned upper_bound, int& res) { + unsigned bits = 32 - td::count_leading_zeroes32(upper_bound); + if (bits > 31 || !have(bits)) { + return false; + } else { + res = (int)fetch_ulong(bits); + return (unsigned)res <= upper_bound; + } +} + +bool CellSlice::fetch_uint_leq(unsigned upper_bound, unsigned& res) { + unsigned bits = 32 - td::count_leading_zeroes32(upper_bound); + if (bits > 32 || !have(bits)) { + return false; + } else { + res = (unsigned)fetch_ulong(bits); + return res <= upper_bound; + } +} + +int CellSlice::bselect(unsigned bits, unsigned long long mask) const { + if (bits > 6 || !have(bits)) { + return -1; + } else { + int n = (int)prefetch_ulong(bits); + return td::count_bits64(mask & ((2ULL << n) - 1)) - 1; + } +} + +int CellSlice::bselect_ext(unsigned bits, unsigned long long mask) const { + if (bits > 6) { + return -1; + } + int n; + if (have(bits)) { + n = (int)prefetch_ulong(bits); + } else { + n = (int)prefetch_ulong(size()) << (bits - size()); + } + return td::count_bits64(mask & ((2ULL << n) - 1)) - 1; +} + +td::RefInt256 CellSlice::fetch_int256(unsigned bits, bool sgnd) { + if (!have(bits)) { + return {}; + } else if (bits < td::BigInt256::word_shift) { + long long val = sgnd ? fetch_long(bits) : fetch_ulong(bits); + return td::RefInt256{true, val}; + } else { + td::RefInt256 res{true}; + res.unique_write().import_bits(data_bits(), bits, sgnd); + advance(bits); + return res; + } +} + +td::RefInt256 CellSlice::prefetch_int256(unsigned bits, bool sgnd) const { + if (!have(bits)) { + return {}; + } else if (bits < td::BigInt256::word_shift) { + long long val = sgnd ? prefetch_long(bits) : prefetch_ulong(bits); + return td::RefInt256{true, val}; + } else { + td::RefInt256 res{true}; + res.unique_write().import_bits(data_bits(), bits, sgnd); + return res; + } +} + +td::RefInt256 CellSlice::prefetch_int256_zeroext(unsigned bits, bool sgnd) const { + if (bits > 256u + sgnd) { + return td::RefInt256{false}; + } else { + unsigned ld_bits = std::min(bits, size()); + if (bits < td::BigInt256::word_shift) { + long long val = sgnd ? prefetch_long(ld_bits) : prefetch_ulong(ld_bits); + val <<= bits - ld_bits; + return td::RefInt256{true, val}; + } else { + td::RefInt256 res{true}; + res.unique_write().import_bits(data_bits(), ld_bits, sgnd); + res <<= bits - ld_bits; + return res; + } + } +} + +td::BitSlice CellSlice::fetch_bits(unsigned bits) { + if (!have(bits)) { + return {}; + } else { + td::BitSlice res{cell, data(), (int)bits_st, bits}; + advance(bits); + return res; + } +} + +td::BitSlice CellSlice::prefetch_bits(unsigned bits) const { + if (!have(bits)) { + return {}; + } else { + return td::BitSlice{cell, data(), (int)bits_st, bits}; + } +} + +bool CellSlice::fetch_bits_to(td::BitPtr buffer, unsigned bits) { + if (!have(bits)) { + return false; + } + fetch_bits(bits).copy_to(buffer); + return true; +} + +bool CellSlice::prefetch_bits_to(td::BitPtr buffer, unsigned bits) const { + if (!have(bits)) { + return false; + } + prefetch_bits(bits).copy_to(buffer); + return true; +} + +td::Ref CellSlice::fetch_subslice(unsigned bits, unsigned refs) { + if (!have(bits, refs)) { + return {}; + } else { + td::Ref res{true, *this, bits, refs}; + advance(bits); + advance_refs(refs); + return res; + } +} + +td::Ref CellSlice::prefetch_subslice(unsigned bits, unsigned refs) const { + if (!have(bits, refs)) { + return {}; + } else { + return td::Ref{true, *this, bits, refs}; + } +} + +td::Ref CellSlice::fetch_subslice_ext(unsigned size) { + return fetch_subslice(size & 0xffff, size >> 16); +} + +td::Ref CellSlice::prefetch_subslice_ext(unsigned size) const { + return prefetch_subslice(size & 0xffff, size >> 16); +} + +td::Ref CellSlice::prefetch_bitstring(unsigned bits) const { + if (!have(bits)) { + return {}; + } else { + return td::Ref{true, prefetch_bits(bits)}; + } +} + +td::Ref CellSlice::fetch_bitstring(unsigned bits) { + if (!have(bits)) { + return {}; + } else { + return td::Ref{true, fetch_bits(bits)}; + } +} + +bool CellSlice::prefetch_bytes(unsigned char* buffer, unsigned bytes) const { + if (!have(bytes * 8)) { + return false; + } else { + td::BitSliceWrite{buffer, bytes* 8} = prefetch_bits(bytes * 8); + return true; + } +} + +bool CellSlice::fetch_bytes(unsigned char* buffer, unsigned bytes) { + if (prefetch_bytes(buffer, bytes)) { + advance(bytes * 8); + return true; + } else { + return false; + } +} + +Ref CellSlice::prefetch_ref(unsigned offset) const { + if (offset < size_refs()) { + auto ref_id = refs_st + offset; + auto res = cell->get_ref(ref_id)->virtualize(child_virt()); + if (!tree_node.empty()) { + res = UsageCell::create(std::move(res), tree_node.create_child(ref_id)); + } + return res; + } else { + return Ref{}; + } +} + +Ref CellSlice::fetch_ref() { + if (have_refs()) { + auto ref_id = refs_st++; + auto res = cell->get_ref(ref_id)->virtualize(child_virt()); + if (!tree_node.empty()) { + res = UsageCell::create(std::move(res), tree_node.create_child(ref_id)); + } + return res; + } else { + return Ref{}; + } +} + +bool CellSlice::begins_with(unsigned bits, unsigned long long value) const { + return have(bits) && !((prefetch_ulong(bits) ^ value) & ((1ULL << bits) - 1)); +} + +bool CellSlice::begins_with(unsigned long long value) const { + return begins_with(63 - td::count_leading_zeroes_non_zero64(value), value); +} + +bool CellSlice::begins_with_skip(unsigned long long value) { + return begins_with_skip(63 - td::count_leading_zeroes_non_zero64(value), value); +} + +bool CellSlice::only_first(unsigned bits, unsigned refs) { + if (!have(bits, refs)) { + return false; + } + bits_en = bits_st + bits; + refs_en = refs_st + refs; + return true; +} + +bool CellSlice::only_ext(unsigned size) { + return only_first(size & 0xffff, size >> 16); +} + +bool CellSlice::skip_first(unsigned bits, unsigned refs) { + if (!have(bits, refs)) { + return false; + } + refs_st += refs; + return advance(bits); +} + +bool CellSlice::skip_ext(unsigned size) { + return skip_first(size & 0xffff, size >> 16); +} + +bool CellSlice::only_last(unsigned bits, unsigned refs) { + if (!have(bits, refs)) { + return false; + } + refs_st = refs_en - refs; + return advance(size() - bits); +} + +bool CellSlice::skip_last(unsigned bits, unsigned refs) { + if (!have(bits, refs)) { + return false; + } + bits_en -= bits; + refs_en -= refs; + return true; +} + +bool CellSlice::cut_tail(const CellSlice& tail_cs) { + return skip_last(tail_cs.size(), tail_cs.size_refs()); +} + +int CellSlice::lex_cmp(const CellSlice& cs2) const { + return td::bitstring::bits_lexcmp(data(), bits_st, size(), cs2.data(), cs2.bits_st, cs2.size()); +} + +bool CellSlice::contents_equal(const CellSlice& cs2) const { + if (size() != cs2.size() || size_refs() != cs2.size_refs()) { + return false; + } + if (td::bitstring::bits_memcmp(data_bits(), cs2.data_bits(), size())) { + return false; + } + for (unsigned i = 0; i < size_refs(); i++) { + if (prefetch_ref(i)->get_hash() != cs2.prefetch_ref(i)->get_hash()) { + return false; + } + } + return true; +} + +bool CellSlice::is_prefix_of(const CellSlice& cs2) const { + return size() <= cs2.size() && !td::bitstring::bits_memcmp(data_bits(), cs2.data_bits(), size(), 0); +} + +bool CellSlice::is_prefix_of(td::ConstBitPtr bs, unsigned len) const { + return size() <= len && !td::bitstring::bits_memcmp(data_bits(), bs, size(), 0); +} + +/* +bool CellSlice::is_prefix_of(const td::BitSlice& bs, unsigned offs, unsigned max_len) const { + max_len = std::min(max_len, size()); + return max_len + offs <= bs.size() && + !td::bitstring::bits_memcmp(data_bits(), bs.bits() + offs, max_len, 0); +} +*/ + +bool CellSlice::is_suffix_of(const CellSlice& cs2) const { + return size() <= cs2.size() && + !td::bitstring::bits_memcmp(data_bits(), cs2.data_bits() + (cs2.size() - size()), size(), 0); +} + +bool CellSlice::has_prefix(const CellSlice& cs2) const { + return size() >= cs2.size() && !td::bitstring::bits_memcmp(data_bits(), cs2.data_bits(), cs2.size(), 0); +} + +bool CellSlice::has_prefix(td::ConstBitPtr bs, unsigned len) const { + return size() >= len && !td::bitstring::bits_memcmp(data_bits(), bs, len, 0); +} + +bool CellSlice::has_suffix(const CellSlice& cs2) const { + return size() >= cs2.size() && + !td::bitstring::bits_memcmp(data_bits() + (size() - cs2.size()), cs2.data_bits(), cs2.size(), 0); +} + +bool CellSlice::is_proper_prefix_of(const CellSlice& cs2) const { + return size() < cs2.size() && !td::bitstring::bits_memcmp(data_bits(), cs2.data_bits(), size(), 0); +} + +bool CellSlice::is_proper_suffix_of(const CellSlice& cs2) const { + return size() < cs2.size() && + !td::bitstring::bits_memcmp(data_bits(), cs2.data_bits() + (cs2.size() - size()), size(), 0); +} + +int CellSlice::common_prefix_len(const CellSlice& cs2) const { + std::size_t same_upto = 0; + td::bitstring::bits_memcmp(data_bits(), cs2.data_bits(), std::min(size(), cs2.size()), &same_upto); + return (int)same_upto; +} + +/* +int CellSlice::common_prefix_len(const td::BitSlice& bs, unsigned offs, unsigned max_len) const { + return common_prefix_len(bs.bits() + offs, std::min(bs.size() - offs, max_len)); +} +*/ + +int CellSlice::common_prefix_len(td::ConstBitPtr bs, unsigned len) const { + std::size_t same_upto = 0; + td::bitstring::bits_memcmp(data_bits(), bs, std::min(size(), len), &same_upto); + return (int)same_upto; +} + +int CellSlice::count_leading(bool bit) const { + return (int)td::bitstring::bits_memscan(data_bits(), size(), bit); +} + +int CellSlice::count_trailing(bool bit) const { + return (int)td::bitstring::bits_memscan_rev(data_bits(), size(), bit); +} + +int CellSlice::remove_trailing() { + if (bits_st == bits_en) { + return 0; + } + unsigned bits = bits_en - bits_st; + unsigned trailing = (unsigned)td::bitstring::bits_memscan_rev(data(), bits_st, bits, 0); + assert(trailing <= bits); + if (trailing == bits) { + bits_en -= trailing; + } else { + bits_en -= ++trailing; + } + return trailing; +} + +bool cell_builder_add_slice_bool(CellBuilder& cb, const CellSlice& cs) { + if (!cb.can_extend_by(cs.size(), cs.size_refs())) { + return false; + } + for (unsigned cnt = 0; cnt < cs.size_refs(); cnt++) { + cb.store_ref(cs.prefetch_ref(cnt)); + } + cb.store_bits(cs.as_bitslice()); + return true; +} + +CellBuilder& cell_builder_add_slice(CellBuilder& cb, const CellSlice& cs) { + return cb.ensure_pass(cell_builder_add_slice_bool(cb, cs)); +} + +void CellSlice::dump(std::ostream& os, int level, bool endl) const { + os << "Cell"; + if (level > 0) { + os << "{" << cell->to_hex() << "}"; + } + os << " bits: " << bits_st << ".." << bits_en; + os << "; refs: " << refs_st << ".." << refs_en; + if (level > 2) { + char tmp[64]; + std::sprintf(tmp, "; ptr=data+%ld; z=%016llx", + static_cast(ptr && cell.not_null() ? ptr - cell->get_data() : -1), static_cast(z)); + os << tmp << " (have " << size() << " bits; " << zd << " preloaded)"; + } + if (endl) { + os << std::endl; + } +} + +void CellSlice::dump_hex(std::ostream& os, int mode, bool endl) const { + os << "x" << as_bitslice().to_hex(); + if (have_refs() && (mode & 1)) { + os << "," << size_refs(); + } + if (endl) { + os << std::endl; + } +} + +void CellSlice::print_rec(std::ostream& os, int indent) const { + for (int i = 0; i < indent; i++) { + os << ' '; + } + if (cell.is_null()) { + os << "NULL" << std::endl; + return; + } + if (is_special()) { + os << "SPECIAL "; + } + os << "x{" << as_bitslice().to_hex() << '}' << std::endl; + for (unsigned i = 0; i < size_refs(); i++) { + CellSlice cs{NoVm(), prefetch_ref(i)}; + cs.print_rec(os, indent + 1); + } +} + +td::StringBuilder& operator<<(td::StringBuilder& sb, const CellSlice& cs) { + std::ostringstream os; + cs.dump_hex(os, 1, false); + return sb << os.str(); +} + +std::ostream& operator<<(std::ostream& os, CellSlice cs) { + cs.dump_hex(os, 1, false); + return os; +} + +std::ostream& operator<<(std::ostream& os, Ref cs_ref) { + if (cs_ref.is_null()) { + os << "(null)"; + } else { + cs_ref->dump_hex(os, 1, false); + } + return os; +} + +// BEGIN (SLICE LOAD FUNCTIONS) +// (if these functions become more complicated, move them into a separate file) + +// If can_be_special is not null, then it is allowed to load special cell +// Flag whether loaded cell is actually special will be stored into can_be_special +VirtualCell::LoadedCell load_cell_slice_impl(const Ref& cell, bool* can_be_special) { + auto* vm_state_interface = VmStateInterface::get(); + if (vm_state_interface) { + vm_state_interface->register_cell_load(); + } + auto r_loaded_cell = cell->load_cell(); + if (r_loaded_cell.is_error()) { + throw VmError{Excno::cell_und, "failed to load cell"}; + } + auto loaded_cell = r_loaded_cell.move_as_ok(); + if (loaded_cell.data_cell->special_type() == DataCell::SpecialType::PrunnedBranch) { + auto virtualization = loaded_cell.virt.get_virtualization(); + if (virtualization != 0) { + throw VmVirtError{virtualization}; + } + } + if (can_be_special) { + *can_be_special = loaded_cell.data_cell->is_special(); + } else if (loaded_cell.data_cell->is_special()) { + if (loaded_cell.data_cell->special_type() == DataCell::SpecialType::Library) { + if (vm_state_interface) { + CellSlice cs(std::move(loaded_cell)); + DCHECK(cs.size() == Cell::hash_bits + 8); + auto library_cell = vm_state_interface->load_library(cs.data_bits() + 8); + if (library_cell.not_null()) { + //TODO: fix infinity loop + return load_cell_slice_impl(library_cell, nullptr); + } + throw VmError{Excno::cell_und, "failed to load library cell"}; + } + throw VmError{Excno::cell_und, "failed to load library cell (no vm_state_interface available)"}; + } else if (loaded_cell.data_cell->special_type() == DataCell::SpecialType::PrunnedBranch) { + CHECK(loaded_cell.virt.get_virtualization() == 0); + throw VmError{Excno::cell_und, "trying to load prunned cell"}; + } + throw VmError{Excno::cell_und, "unexpected special cell"}; + } + return loaded_cell; +} + +CellSlice load_cell_slice(const Ref& cell) { + return CellSlice{load_cell_slice_impl(cell, nullptr)}; +} + +CellSlice load_cell_slice_special(const Ref& cell, bool& special) { + return CellSlice{load_cell_slice_impl(cell, &special)}; +} + +Ref load_cell_slice_ref(const Ref& cell) { + return Ref{true, CellSlice(load_cell_slice_impl(cell, nullptr))}; +} + +Ref load_cell_slice_ref_special(const Ref& cell, bool& special) { + return Ref{true, CellSlice(load_cell_slice_impl(cell, &special))}; +} + +void print_load_cell(std::ostream& os, Ref cell, int indent) { + auto cs = load_cell_slice(cell); + cs.print_rec(os, indent); +} + +bool CellSlice::load(Ref cell) { + return load(load_cell_slice_impl(std::move(cell), nullptr)); +} + +bool CellSlice::load_ord(Ref cell) { + return load(load_cell_slice_impl(std::move(cell), nullptr)); +} + +// END (SLICE LOAD FUNCTIONS) + +} // namespace vm diff --git a/crypto/vm/cells/CellSlice.h b/crypto/vm/cells/CellSlice.h new file mode 100644 index 00000000..5d7f906b --- /dev/null +++ b/crypto/vm/cells/CellSlice.h @@ -0,0 +1,328 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +#include "common/refcnt.hpp" +#include "common/refint.h" +#include "vm/cells.h" + +namespace td { +class StringBuilder; +} +namespace vm { + +struct NoVm {}; +struct NoVmOrd {}; +struct NoVmSpec {}; + +class CellSlice : public td::CntObject { + Cell::VirtualizationParameters virt; + Ref cell; + CellUsageTree::NodePtr tree_node; + unsigned bits_st, refs_st; + unsigned bits_en, refs_en; + mutable const unsigned char* ptr{nullptr}; + mutable unsigned long long z; + mutable unsigned zd; + + public: + static constexpr long long fetch_long_eof = (static_cast(-1LL) << 63); + static constexpr unsigned long long fetch_ulong_eof = (unsigned long long)-1LL; + struct CellReadError {}; + + CellSlice(NoVm, Ref cell_ref); + CellSlice(NoVmOrd, Ref cell_ref); + CellSlice(NoVmSpec, Ref cell_ref); + CellSlice(Ref dc_ref); + CellSlice(VirtualCell::LoadedCell loaded_cell); + /* + CellSlice(Ref dc_ref, unsigned _bits_en, unsigned _refs_en, unsigned _bits_st = 0, unsigned _refs_st = 0);*/ + CellSlice(const CellSlice& cs, unsigned _bits_en, unsigned _refs_en); + CellSlice(const CellSlice& cs, unsigned _bits_en, unsigned _refs_en, unsigned _bits_st, unsigned _refs_st); + CellSlice(const CellSlice&); + CellSlice& operator=(const CellSlice& other) = default; + CellSlice(); + Cell::LoadedCell move_as_loaded_cell(); + td::CntObject* make_copy() const override { + return new CellSlice{*this}; + } + void clear(); + bool load(VirtualCell::LoadedCell loaded_cell); + bool load(NoVm, Ref cell_ref); + bool load(NoVmOrd, Ref cell_ref); + bool load(NoVmSpec, Ref cell_ref); + bool load(Ref dc_ref); + bool load(Ref cell); + bool load_ord(Ref cell); + unsigned size() const { + return bits_en - bits_st; + } + bool is_special() const { + return cell->is_special(); + } + bool is_valid() const { + return cell.not_null(); + } + Cell::SpecialType special_type() const { + return cell->special_type(); + } + int child_merkle_depth(int merkle_depth) const { + if (merkle_depth == Cell::VirtualizationParameters::max_level()) { + return merkle_depth; + } + if (cell->special_type() == Cell::SpecialType::MerkleProof || + cell->special_type() == Cell::SpecialType::MerkleUpdate) { + merkle_depth++; + } + return merkle_depth; + } + unsigned size_refs() const { + return refs_en - refs_st; + } + unsigned size_ext() const { + return size() + (size_refs() << 16); + } + bool have(unsigned bits) const { + return bits <= size(); + } + bool have(unsigned bits, unsigned refs) const { + return bits <= size() && refs <= size_refs(); + } + bool have_ext(unsigned ext_size) const { + return have(ext_size & 0xffff, ext_size >> 16); + } + bool empty() const { + return !size(); + } + bool empty_ext() const { + return !size() && !size_refs(); + } + bool have_refs(unsigned refs = 1) const { + return refs <= size_refs(); + } + bool advance(unsigned bits); + bool advance_refs(unsigned refs); + bool advance_ext(unsigned bits_refs); + bool advance_ext(unsigned bits, unsigned refs); + unsigned cur_pos() const { + return bits_st; + } + unsigned cur_ref() const { + return refs_st; + } + const unsigned char* data() const { + return cell->get_data(); + } + td::ConstBitPtr data_bits() const { + return td::ConstBitPtr{data(), (int)cur_pos()}; + } + int subtract_base_ext(const CellSlice& base) { + return (bits_st - base.bits_st) | ((refs_st - base.refs_st) << 16); + } + unsigned get_cell_level() const; + unsigned get_level() const; + int fetch_octet(); + int prefetch_octet() const; + unsigned long long prefetch_ulong_top(unsigned& bits) const; + unsigned long long fetch_ulong(unsigned bits); + unsigned long long prefetch_ulong(unsigned bits) const; + long long fetch_long(unsigned bits); + long long prefetch_long(unsigned bits) const; + bool fetch_long_bool(unsigned bits, long long& res); + bool prefetch_long_bool(unsigned bits, long long& res) const; + bool fetch_ulong_bool(unsigned bits, unsigned long long& res); + bool prefetch_ulong_bool(unsigned bits, unsigned long long& res) const; + bool fetch_bool_to(bool& res); + bool fetch_bool_to(int& res); + bool fetch_bool_to(int& res, int mask); + bool fetch_uint_to(unsigned bits, unsigned long long& res); + bool fetch_uint_to(unsigned bits, long long& res); + bool fetch_uint_to(unsigned bits, unsigned long& res); + bool fetch_uint_to(unsigned bits, long& res); + bool fetch_uint_to(unsigned bits, unsigned& res); + bool fetch_uint_to(unsigned bits, int& res); + bool fetch_uint_less(unsigned upper_bound, int& res); + bool fetch_uint_less(unsigned upper_bound, unsigned& res); + bool fetch_uint_leq(unsigned upper_bound, int& res); + bool fetch_uint_leq(unsigned upper_bound, unsigned& res); + bool fetch_int_to(unsigned bits, long long& res); + bool fetch_int_to(unsigned bits, int& res); + int bselect(unsigned bits, unsigned long long mask) const; + int bselect_ext(unsigned bits, unsigned long long mask) const; + int bit_at(unsigned i) const { + return have(i) ? data_bits()[i] : -1; + } + td::RefInt256 fetch_int256(unsigned bits, bool sgnd = true); + td::RefInt256 prefetch_int256(unsigned bits, bool sgnd = true) const; + td::RefInt256 prefetch_int256_zeroext(unsigned bits, bool sgnd = true) const; + bool fetch_int256_to(unsigned bits, td::RefInt256& res, bool sgnd = true) { + return (res = fetch_int256(bits, sgnd)).not_null(); + } + bool fetch_uint256_to(unsigned bits, td::RefInt256& res) { + return (res = fetch_int256(bits, false)).not_null(); + } + Ref prefetch_ref(unsigned offset = 0) const; + Ref fetch_ref(); + bool fetch_ref_to(Ref& ref) { + return (ref = fetch_ref()).not_null(); + } + bool prefetch_ref_to(Ref& ref, unsigned offset = 0) const { + return (ref = prefetch_ref(offset)).not_null(); + } + td::BitSlice fetch_bits(unsigned bits); + td::BitSlice prefetch_bits(unsigned bits) const; + td::Ref fetch_subslice(unsigned bits, unsigned refs = 0); + td::Ref prefetch_subslice(unsigned bits, unsigned refs = 0) const; + td::Ref fetch_subslice_ext(unsigned size); + td::Ref prefetch_subslice_ext(unsigned size) const; + td::Ref fetch_bitstring(unsigned size); + td::Ref prefetch_bitstring(unsigned size) const; + bool fetch_subslice_to(unsigned bits, td::Ref& res) { + return (res = fetch_subslice(bits)).not_null(); + } + bool fetch_subslice_ext_to(unsigned bits, td::Ref& res) { + return (res = fetch_subslice_ext(bits)).not_null(); + } + bool fetch_bitstring_to(unsigned bits, td::Ref& res) { + return (res = fetch_bitstring(bits)).not_null(); + } + bool fetch_bits_to(td::BitPtr buffer, unsigned bits); + bool prefetch_bits_to(td::BitPtr buffer, unsigned bits) const; + template + bool fetch_bits_to(td::BitArray& buffer) { + return fetch_bits_to(buffer.bits(), n); + } + template + bool prefetch_bits_to(td::BitArray& buffer) const { + return prefetch_bits_to(buffer.bits(), n); + } + bool fetch_bytes(unsigned char* buffer, unsigned bytes); + bool prefetch_bytes(unsigned char* buffer, unsigned bytes) const; + td::BitSlice as_bitslice() const { + return prefetch_bits(size()); + } + bool begins_with(unsigned bits, unsigned long long value) const; + bool begins_with(unsigned long long value) const; + bool begins_with_skip(unsigned bits, unsigned long long value) { + return begins_with(bits, value) && advance(bits); + } + bool begins_with_skip(unsigned long long value); + bool only_first(unsigned bits, unsigned refs = 0); + bool only_ext(unsigned size); + bool skip_first(unsigned bits, unsigned refs = 0); + bool skip_ext(unsigned size); + bool only_last(unsigned bits, unsigned refs = 0); + bool skip_last(unsigned bits, unsigned refs = 0); + bool cut_tail(const CellSlice& tail_cs); + int remove_trailing(); + int count_leading(bool bit) const; + int count_trailing(bool bit) const; + int lex_cmp(const CellSlice& cs2) const; + int common_prefix_len(const CellSlice& cs2) const; + bool is_prefix_of(const CellSlice& cs2) const; + bool is_prefix_of(td::ConstBitPtr bs, unsigned len) const; + bool is_suffix_of(const CellSlice& cs2) const; + bool has_prefix(const CellSlice& cs2) const; + bool has_prefix(td::ConstBitPtr bs, unsigned len) const; + bool has_suffix(const CellSlice& cs2) const; + bool is_proper_prefix_of(const CellSlice& cs2) const; + bool is_proper_suffix_of(const CellSlice& cs2) const; + // int common_prefix_len(const td::BitSlice& bs, unsigned offs = 0, unsigned max_len = 0xffffffffU) const; + int common_prefix_len(td::ConstBitPtr bs, unsigned len) const; + // bool is_prefix_of(const td::BitSlice& bs, unsigned offs = 0, unsigned max_len = 0xffffffffU) const; + bool contents_equal(const CellSlice& cs2) const; + void dump(std::ostream& os, int level = 0, bool endl = true) const; + void dump_hex(std::ostream& os, int mode = 0, bool endl = false) const; + void print_rec(std::ostream& os, int indent = 0) const; + void error() const { + throw CellReadError{}; + } + bool chk(bool cond) const { + if (!cond) { + error(); + } + return cond; + } + bool have_chk(unsigned bits) const { + return chk(have(bits)); + } + bool have_chk(unsigned bits, unsigned refs) const { + return chk(have(bits, refs)); + } + bool have_refs_chk(unsigned refs = 1) const { + return chk(have_refs(refs)); + } + CellSlice operator+(unsigned offs) const { + offs = std::min(offs, size()); + return CellSlice{*this, size() - offs, size_refs(), offs, 0}; + } + + private: + void init_bits_refs(); + void init_preload() const; + void preload_at_least(unsigned req_bits) const; + Cell::VirtualizationParameters child_virt() const { + return Cell::VirtualizationParameters(static_cast(child_merkle_depth(virt.get_level())), + virt.get_virtualization()); + } +}; + +td::StringBuilder& operator<<(td::StringBuilder& sb, const CellSlice& cs); + +bool cell_builder_add_slice_bool(CellBuilder& cb, const CellSlice& cs); +CellBuilder& cell_builder_add_slice(CellBuilder& cb, const CellSlice& cs); + +std::ostream& operator<<(std::ostream& os, CellSlice cs); +std::ostream& operator<<(std::ostream& os, Ref cs_ref); + +template +CellSlice& operator>>(CellSlice& cs, T& val) { + cs.chk(val.deserialize(cs)); + return cs; +} + +template +Ref& operator>>(Ref& cs_ref, T& val) { + bool res = val.deserialize(cs_ref.write()); + cs_ref->chk(res); + return cs_ref; +} + +template +CellSlice& operator>>(CellSlice& cs, const T& val) { + cs.chk(val.deserialize(cs)); + return cs; +} + +template +Ref& operator>>(Ref& cs_ref, const T& val) { + bool res = val.deserialize(cs_ref.write()); + cs_ref->chk(res); + return cs_ref; +} + +// If can_be_special is not null, then it is allowed to load special cell +// Flag whether loaded cell is actually special will be stored into can_be_special +CellSlice load_cell_slice(const Ref& cell); +Ref load_cell_slice_ref(const Ref& cell); +CellSlice load_cell_slice_special(const Ref& cell, bool& is_special); +Ref load_cell_slice_ref_special(const Ref& cell, bool& is_special); +void print_load_cell(std::ostream& os, Ref cell, int indent = 0); + +} // namespace vm diff --git a/crypto/vm/cells/CellTraits.cpp b/crypto/vm/cells/CellTraits.cpp new file mode 100644 index 00000000..b0ce7381 --- /dev/null +++ b/crypto/vm/cells/CellTraits.cpp @@ -0,0 +1,45 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "vm/cells/CellTraits.h" + +#include "td/utils/StringBuilder.h" +#include "td/utils/Slice.h" + +namespace vm { +td::StringBuilder& operator<<(td::StringBuilder& sb, CellTraits::SpecialType special_type) { + switch (special_type) { + case CellTraits::SpecialType::Ordinary: + sb << "Ordinary"; + break; + case CellTraits::SpecialType::MerkleProof: + sb << "MerkleProof"; + break; + case CellTraits::SpecialType::MerkleUpdate: + sb << "MerkleUpdate"; + break; + case CellTraits::SpecialType::PrunnedBranch: + sb << "PrunnedBranch"; + break; + case CellTraits::SpecialType::Library: + sb << "Library"; + break; + } + return sb; +} +} // namespace vm diff --git a/crypto/vm/cells/CellTraits.h b/crypto/vm/cells/CellTraits.h new file mode 100644 index 00000000..9e55dd67 --- /dev/null +++ b/crypto/vm/cells/CellTraits.h @@ -0,0 +1,52 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once +#include "td/utils/int_types.h" +#include "common/refcnt.hpp" + +namespace td { +class StringBuilder; +} + +namespace vm { +class CellTraits : public td::CntObject { + public: + enum class SpecialType : td::uint8 { + Ordinary = 0, + PrunnedBranch = 1, + Library = 2, + MerkleProof = 3, + MerkleUpdate = 4 + }; + enum { + max_refs = 4, + max_bytes = 128, + max_bits = 1023, + hash_bytes = 32, + hash_bits = hash_bytes * 8, + depth_bytes = 2, + depth_bits = depth_bytes * 8, + max_level = 3, + max_depth = 1024, + max_virtualization = 7, + max_serialized_bytes = 2 + max_bytes + (max_level + 1) * (hash_bytes + depth_bytes) + }; +}; +td::StringBuilder& operator<<(td::StringBuilder& sb, CellTraits::SpecialType special_type); +} // namespace vm diff --git a/crypto/vm/cells/CellUsageTree.cpp b/crypto/vm/cells/CellUsageTree.cpp new file mode 100644 index 00000000..117d1c32 --- /dev/null +++ b/crypto/vm/cells/CellUsageTree.cpp @@ -0,0 +1,136 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "vm/cells/CellUsageTree.h" + +namespace vm { +// +// CellUsageTree::NodePtr +// +bool CellUsageTree::NodePtr::on_load() const { + auto tree = tree_weak_.lock(); + if (!tree) { + return false; + } + tree->on_load(node_id_); + return true; +} + +CellUsageTree::NodePtr CellUsageTree::NodePtr::create_child(unsigned ref_id) const { + auto tree = tree_weak_.lock(); + if (!tree) { + return {}; + } + return {tree_weak_, tree->create_child(node_id_, ref_id)}; +} + +bool CellUsageTree::NodePtr::is_from_tree(const CellUsageTree* master_tree) const { + DCHECK(master_tree); + auto tree = tree_weak_.lock(); + if (tree.get() != master_tree) { + return false; + } + return true; +} + +bool CellUsageTree::NodePtr::mark_path(CellUsageTree* master_tree) const { + DCHECK(master_tree); + auto tree = tree_weak_.lock(); + if (tree.get() != master_tree) { + return false; + } + master_tree->mark_path(node_id_); + return true; +} + +// +// CellUsageTree +// +CellUsageTree::NodePtr CellUsageTree::root_ptr() { + return {shared_from_this(), 1}; +} + +CellUsageTree::NodeId CellUsageTree::root_id() const { + return 1; +}; + +bool CellUsageTree::is_loaded(NodeId node_id) const { + if (use_mark_) { + return nodes_[node_id].has_mark; + } + return nodes_[node_id].is_loaded; +} + +bool CellUsageTree::has_mark(NodeId node_id) const { + return nodes_[node_id].has_mark; +} + +void CellUsageTree::set_mark(NodeId node_id, bool mark) { + if (node_id == 0) { + return; + } + nodes_[node_id].has_mark = mark; +} + +void CellUsageTree::mark_path(NodeId node_id) { + auto cur_node_id = get_parent(node_id); + while (cur_node_id != 0) { + if (has_mark(cur_node_id)) { + break; + } + set_mark(cur_node_id); + cur_node_id = get_parent(cur_node_id); + } +} + +CellUsageTree::NodeId CellUsageTree::get_parent(NodeId node_id) { + return nodes_[node_id].parent; +} + +CellUsageTree::NodeId CellUsageTree::get_child(NodeId node_id, unsigned ref_id) { + DCHECK(ref_id < CellTraits::max_refs); + return nodes_[node_id].children[ref_id]; +} + +void CellUsageTree::set_use_mark_for_is_loaded(bool use_mark) { + use_mark_ = use_mark; +} + +void CellUsageTree::on_load(NodeId node_id) { + nodes_[node_id].is_loaded = true; +} + +CellUsageTree::NodeId CellUsageTree::create_child(NodeId node_id, unsigned ref_id) { + DCHECK(ref_id < CellTraits::max_refs); + NodeId res = nodes_[node_id].children[ref_id]; + if (res) { + return res; + } + res = create_node(node_id); + nodes_[node_id].children[ref_id] = res; + return res; +} + +CellUsageTree::NodeId CellUsageTree::create_node(NodeId parent) { + NodeId res = static_cast(nodes_.size()); + nodes_.emplace_back(); + nodes_.back().parent = parent; + return res; +} + +} // namespace vm diff --git a/crypto/vm/cells/CellUsageTree.h b/crypto/vm/cells/CellUsageTree.h new file mode 100644 index 00000000..b3383984 --- /dev/null +++ b/crypto/vm/cells/CellUsageTree.h @@ -0,0 +1,75 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +#include "vm/cells/CellTraits.h" + +#include "td/utils/int_types.h" +#include "td/utils/logging.h" + +namespace vm { +class CellUsageTree : public std::enable_shared_from_this { + public: + using NodeId = td::uint32; + + struct NodePtr { + public: + NodePtr() = default; + NodePtr(std::weak_ptr tree_weak, NodeId node_id) + : tree_weak_(std::move(tree_weak)), node_id_(node_id) { + } + bool empty() const { + return node_id_ == 0 || tree_weak_.expired(); + } + + bool on_load() const; + NodePtr create_child(unsigned ref_id) const; + bool mark_path(CellUsageTree* master_tree) const; + bool is_from_tree(const CellUsageTree* master_tree) const; + + private: + std::weak_ptr tree_weak_; + NodeId node_id_{0}; + }; + + NodePtr root_ptr(); + NodeId root_id() const; + bool is_loaded(NodeId node_id) const; + bool has_mark(NodeId node_id) const; + void set_mark(NodeId node_id, bool mark = true); + void mark_path(NodeId node_id); + NodeId get_parent(NodeId node_id); + NodeId get_child(NodeId node_id, unsigned ref_id); + void set_use_mark_for_is_loaded(bool use_mark = true); + NodeId create_child(NodeId node_id, unsigned ref_id); + + private: + struct Node { + bool is_loaded{false}; + bool has_mark{false}; + NodeId parent{0}; + std::array children{}; + }; + bool use_mark_{false}; + std::vector nodes_{2}; + + void on_load(NodeId node_id); + NodeId create_node(NodeId parent); +}; +} // namespace vm diff --git a/crypto/vm/cells/CellWithStorage.h b/crypto/vm/cells/CellWithStorage.h new file mode 100644 index 00000000..b9841e1c --- /dev/null +++ b/crypto/vm/cells/CellWithStorage.h @@ -0,0 +1,92 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +namespace vm { +namespace detail { +template +class CellWithArrayStorage : public CellT { + public: + template + CellWithArrayStorage(ArgsT&&... args) : CellT(std::forward(args)...) { + } + ~CellWithArrayStorage() { + CellT::destroy_storage(get_storage()); + } + template + static std::unique_ptr create(size_t storage_size, ArgsT&&... args) { + static_assert(CellT::max_storage_size <= 40 * 8, ""); + //size = 128 + 32 + 8; + auto size = (storage_size + 7) / 8; +#define CASE(size) \ + case (size): \ + return std::make_unique>(std::forward(args)...); +#define CASE2(offset) CASE(offset) CASE(offset + 1) +#define CASE8(offset) CASE2(offset) CASE2(offset + 2) CASE2(offset + 4) CASE2(offset + 6) +#define CASE32(offset) CASE8(offset) CASE8(offset + 8) CASE8(offset + 16) CASE8(offset + 24) + switch (size) { CASE32(0) CASE8(32) } +#undef CASE +#undef CASE2 +#undef CASE8 +#undef CASE32 + LOG(FATAL) << "TOO BIG " << storage_size; + UNREACHABLE(); + } + + private: + alignas(alignof(void*)) char storage_[Size]; + + const char* get_storage() const final { + return storage_; + } + char* get_storage() final { + return storage_; + } +}; + +template +class CellWithUniquePtrStorage : public CellT { + public: + template + CellWithUniquePtrStorage(size_t storage_size, ArgsT&&... args) + : CellT(std::forward(args)...), storage_(std::make_unique(storage_size)) { + } + ~CellWithUniquePtrStorage() { + CellT::destroy_storage(get_storage()); + } + + template + static std::unique_ptr create(size_t storage_size, ArgsT&&... args) { + return std::make_unique(storage_size, std::forward(args)...); + } + + private: + std::unique_ptr storage_; + + const char* get_storage() const final { + CHECK(storage_); + return storage_.get(); + } + char* get_storage() final { + CHECK(storage_); + return storage_.get(); + } +}; +} // namespace detail +} // namespace vm diff --git a/crypto/vm/cells/DataCell.cpp b/crypto/vm/cells/DataCell.cpp new file mode 100644 index 00000000..4a89a276 --- /dev/null +++ b/crypto/vm/cells/DataCell.cpp @@ -0,0 +1,368 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "vm/cells/DataCell.h" + +#include "openssl/digest.h" + +#include "td/utils/ScopeGuard.h" + +#include "vm/cells/CellWithStorage.h" + +namespace vm { +std::unique_ptr DataCell::create_empty_data_cell(Info info) { + return detail::CellWithUniquePtrStorage::create(info.get_storage_size(), info); +} + +DataCell::DataCell(Info info) : info_(std::move(info)) { + get_thread_safe_counter().add(1); +} +DataCell::~DataCell() { + get_thread_safe_counter().add(-1); +} + +void DataCell::destroy_storage(char* storage) { + auto* refs = info_.get_refs(storage); + for (size_t i = 0; i < get_refs_cnt(); i++) { + Ref(refs[i], Ref::acquire_t{}); // call destructor + } +} + +td::Result> DataCell::create(td::ConstBitPtr data, unsigned bits, td::Span> refs, + bool special) { + std::array, max_refs> copied_refs; + CHECK(refs.size() <= copied_refs.size()); + for (size_t i = 0; i < refs.size(); i++) { + copied_refs[i] = refs[i]; + } + return create(std::move(data), bits, td::MutableSpan>(copied_refs.data(), refs.size()), special); +} + +DataCell::SpecialType DataCell::special_type() const { + if (is_special()) { + return static_cast(td::bitstring::bits_load_ulong(get_data(), 8)); + } + return SpecialType::Ordinary; +} + +td::Result> DataCell::create(td::ConstBitPtr data, unsigned bits, td::MutableSpan> refs, + bool special) { + for (auto& ref : refs) { + if (ref.is_null()) { + return td::Status::Error("Has null cell reference"); + } + } + + SpecialType type = SpecialType::Ordinary; + if (special) { + if (bits < 8) { + return td::Status::Error("Not enough data for a special cell"); + } + type = static_cast(td::bitstring::bits_load_ulong(data, 8)); + if (type == SpecialType::Ordinary) { + return td::Status::Error("Special cell has Ordinary type"); + } + } + + LevelMask level_mask; + td::uint32 virtualization = 0; + switch (type) { + case SpecialType::Ordinary: { + for (auto& ref : refs) { + level_mask = level_mask.apply_or(ref->get_level_mask()); + virtualization = td::max(virtualization, ref->get_virtualization()); + } + break; + } + + case SpecialType::PrunnedBranch: { + if (refs.size() != 0) { + return td::Status::Error("PrunnedBranch special cell has a cell reference"); + } + if (bits < 16) { + return td::Status::Error("Not enough data for a PrunnedBranch special cell"); + } + level_mask = LevelMask((td::bitstring::bits_load_ulong(data + 8, 8)) & 0xff); + auto level = level_mask.get_level(); + if (level > max_level || level == 0) { + return td::Status::Error("Prunned Branch has an invalid level"); + } + if (bits != (2 + level_mask.apply(level - 1).get_hashes_count() * (hash_bytes + depth_bytes)) * 8) { + return td::Status::Error("Not enouch data for a PrunnedBranch special cell"); + } + // depth will be checked later! + break; + } + + case SpecialType::Library: { + if (bits != 8 + hash_bytes * 8) { + return td::Status::Error("Not enouch data for a Library special cell"); + } + break; + } + + case SpecialType::MerkleProof: { + if (bits != 8 + (hash_bytes + depth_bytes) * 8) { + return td::Status::Error("Not enouch data for a MerkleProof special cell"); + } + if (refs.size() != 1) { + return td::Status::Error("Wrong references count for a MerkleProof special cell"); + } + if (td::bitstring::bits_memcmp(data + 8, refs[0]->get_hash(0).as_bitslice().get_ptr(), hash_bits) != 0) { + return td::Status::Error("Hash mismatch in a MerkleProof special cell"); + } + if (td::bitstring::bits_load_ulong(data + 8 + hash_bits, depth_bytes * 8) != refs[0]->get_depth(0)) { + return td::Status::Error("Depth mismatch in a MerkleProof special cell"); + } + level_mask = refs[0]->get_level_mask().shift_right(); + virtualization = refs[0]->get_virtualization(); + break; + } + + case SpecialType::MerkleUpdate: { + if (bits != 8 + (hash_bytes + depth_bytes) * 8 * 2) { + return td::Status::Error("Not enouch data for a MerkleUpdate special cell"); + } + if (refs.size() != 2) { + return td::Status::Error("Wrong references count for a MerkleUpdate special cell"); + } + if (td::bitstring::bits_memcmp(data + 8, refs[0]->get_hash(0).as_bitslice().get_ptr(), hash_bits) != 0) { + return td::Status::Error("First hash mismatch in a MerkleProof special cell"); + } + if (td::bitstring::bits_memcmp(data + 8 + hash_bits, refs[1]->get_hash(0).as_bitslice().get_ptr(), hash_bits) != + 0) { + return td::Status::Error("Second hash mismatch in a MerkleProof special cell"); + } + if (td::bitstring::bits_load_ulong(data + 8 + 2 * hash_bits, depth_bytes * 8) != refs[0]->get_depth(0)) { + return td::Status::Error("First depth mismatch in a MerkleProof special cell"); + } + if (td::bitstring::bits_load_ulong(data + 8 + 2 * hash_bits + depth_bytes * 8, depth_bytes * 8) != + refs[1]->get_depth(0)) { + return td::Status::Error("Second depth mismatch in a MerkleProof special cell"); + } + + level_mask = refs[0]->get_level_mask().apply_or(refs[1]->get_level_mask()).shift_right(); + virtualization = td::max(refs[0]->get_virtualization(), refs[1]->get_virtualization()); + break; + } + + default: + return td::Status::Error("Unknown special cell type"); + } + + Info info; + if (td::unlikely(bits > max_bits)) { + return td::Status::Error("Too many bits"); + } + if (td::unlikely(refs.size() > max_refs)) { + return td::Status::Error("Too many cell references"); + } + if (td::unlikely(virtualization > max_virtualization)) { + return td::Status::Error("Too big virtualization"); + } + + CHECK(level_mask.get_level() <= max_level); + + auto hash_count = type == SpecialType::PrunnedBranch ? 1 : level_mask.get_hashes_count(); + DCHECK(hash_count <= max_level + 1); + + info.bits_ = bits; + info.refs_count_ = refs.size() & 7; + info.is_special_ = special; + info.level_mask_ = level_mask.get_mask() & 7; + info.hash_count_ = hash_count & 7; + info.virtualization_ = virtualization & 7; + + auto data_cell = create_empty_data_cell(info); + auto* storage = data_cell->get_storage(); + + // init data + auto* data_ptr = info.get_data(storage); + td::BitPtr{data_ptr}.copy_from(data, bits); + // prepare for serialization + if (bits & 7) { + int m = (0x80 >> (bits & 7)); + unsigned l = bits / 8; + data_ptr[l] = static_cast((data_ptr[l] & -m) | m); + } + + // init refs + auto refs_ptr = info.get_refs(storage); + for (size_t i = 0; i < refs.size(); i++) { + refs_ptr[i] = refs[i].release(); + } + + // init hashes and depth + auto* hashes_ptr = info.get_hashes(storage); + auto* depth_ptr = info.get_depth(storage); + + // NB: be careful with special cells + auto total_hash_count = level_mask.get_hashes_count(); + auto hash_i_offset = total_hash_count - hash_count; + for (td::uint32 level_i = 0, hash_i = 0, level = level_mask.get_level(); level_i <= level; level_i++) { + if (!level_mask.is_significant(level_i)) { + continue; + } + SCOPE_EXIT { + hash_i++; + }; + if (hash_i < hash_i_offset) { + continue; + } + unsigned char tmp[2]; + tmp[0] = info.d1(level_mask.apply(level_i)); + tmp[1] = info.d2(); + + static TD_THREAD_LOCAL digest::SHA256* hasher; + td::init_thread_local(hasher); + hasher->reset(); + + hasher->feed(td::Slice(tmp, 2)); + + if (hash_i == hash_i_offset) { + DCHECK(level_i == 0 || type == SpecialType::PrunnedBranch); + hasher->feed(td::Slice(data_ptr, (bits + 7) >> 3)); + } else { + DCHECK(level_i != 0 && type != SpecialType::PrunnedBranch); + hasher->feed(hashes_ptr[hash_i - hash_i_offset - 1].as_slice()); + } + + auto dest_i = hash_i - hash_i_offset; + + // calc depth + td::uint16 depth = 0; + for (int i = 0; i < info.refs_count_; i++) { + td::uint16 child_depth = 0; + if (type == SpecialType::MerkleProof || type == SpecialType::MerkleUpdate) { + child_depth = refs_ptr[i]->get_depth(level_i + 1); + } else { + child_depth = refs_ptr[i]->get_depth(level_i); + } + + // add depth into hash + td::uint8 child_depth_buf[depth_bytes]; + store_depth(child_depth_buf, child_depth); + hasher->feed(td::Slice(child_depth_buf, depth_bytes)); + + depth = std::max(depth, child_depth); + } + if (info.refs_count_ != 0) { + if (depth >= max_depth) { + return td::Status::Error("Depth is too big"); + } + depth++; + } + depth_ptr[dest_i] = depth; + + // children hash + for (int i = 0; i < info.refs_count_; i++) { + if (type == SpecialType::MerkleProof || type == SpecialType::MerkleUpdate) { + hasher->feed(refs_ptr[i]->get_hash(level_i + 1).as_slice()); + } else { + hasher->feed(refs_ptr[i]->get_hash(level_i).as_slice()); + } + } + auto extracted_size = hasher->extract(hashes_ptr[dest_i].as_slice()); + DCHECK(extracted_size == hash_bytes); + } + + return Ref(data_cell.release(), Ref::acquire_t{}); +} + +const DataCell::Hash DataCell::do_get_hash(td::uint32 level) const { + auto hash_i = get_level_mask().apply(level).get_hash_i(); + if (special_type() == SpecialType::PrunnedBranch) { + auto this_hash_i = get_level_mask().get_hash_i(); + if (hash_i != this_hash_i) { + return reinterpret_cast(info_.get_data(get_storage()) + 2)[hash_i]; + } + hash_i = 0; + } + return info_.get_hashes(get_storage())[hash_i]; +} + +td::uint16 DataCell::do_get_depth(td::uint32 level) const { + auto hash_i = get_level_mask().apply(level).get_hash_i(); + if (special_type() == SpecialType::PrunnedBranch) { + auto this_hash_i = get_level_mask().get_hash_i(); + if (hash_i != this_hash_i) { + return load_depth(info_.get_data(get_storage()) + 2 + hash_bytes * this_hash_i + hash_i * depth_bytes); + } + hash_i = 0; + } + return info_.get_depth(get_storage())[hash_i]; +} + +int DataCell::serialize(unsigned char* buff, int buff_size, bool with_hashes) const { + int len = get_serialized_size(with_hashes); + if (len > buff_size) { + return 0; + } + buff[0] = static_cast(info_.d1() | (with_hashes * 16)); + buff[1] = info_.d2(); + int hs = 0; + if (with_hashes) { + hs = (get_level_mask().get_hashes_count()) * (hash_bytes + depth_bytes); + assert(len >= 2 + hs); + std::memset(buff + 2, 0, hs); + auto dest = td::MutableSlice(buff + 2, hs); + auto level = get_level(); + // TODO: optimize for prunned brandh + for (unsigned i = 0; i <= level; i++) { + if (!get_level_mask().is_significant(i)) { + continue; + } + dest.copy_from(get_hash(i).as_slice()); + dest.remove_prefix(hash_bytes); + } + for (unsigned i = 0; i <= level; i++) { + if (!get_level_mask().is_significant(i)) { + continue; + } + store_depth(dest.ubegin(), get_depth(i)); + dest.remove_prefix(depth_bytes); + } + // buff[2] = 0; // for testing hash verification in deserialization + buff += hs; + len -= hs; + } + std::memcpy(buff + 2, get_data(), len - 2); + return len + hs; +} + +std::string DataCell::serialize() const { + unsigned char buff[max_serialized_bytes]; + int len = serialize(buff, sizeof(buff)); + return std::string(buff, buff + len); +} + +std::string DataCell::to_hex() const { + unsigned char buff[max_serialized_bytes]; + int len = serialize(buff, sizeof(buff)); + char hex_buff[max_serialized_bytes * 2 + 1]; + for (int i = 0; i < len; i++) { + sprintf(hex_buff + 2 * i, "%02x", buff[i]); + } + return hex_buff; +} + +std::ostream& operator<<(std::ostream& os, const DataCell& c) { + return os << c.to_hex(); +} + +} // namespace vm diff --git a/crypto/vm/cells/DataCell.h b/crypto/vm/cells/DataCell.h new file mode 100644 index 00000000..416d1835 --- /dev/null +++ b/crypto/vm/cells/DataCell.h @@ -0,0 +1,212 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once +#include "vm/cells/Cell.h" + +#include "td/utils/Span.h" + +#include "td/utils/ThreadSafeCounter.h" + +namespace vm { + +class DataCell : public Cell { + public: + DataCell(const DataCell& other) = delete; + ~DataCell() override; + + static void store_depth(td::uint8* dest, td::uint16 depth) { + td::bitstring::bits_store_long(dest, depth, depth_bits); + } + static td::uint16 load_depth(const td::uint8* src) { + return td::bitstring::bits_load_ulong(src, depth_bits) & 0xff; + } + + protected: + struct Info { + unsigned bits_; + + // d1 + unsigned char refs_count_ : 3; + bool is_special_ : 1; + unsigned char level_mask_ : 3; + + unsigned char hash_count_ : 3; + + unsigned char virtualization_ : 3; + + unsigned char d1() const { + return d1(LevelMask{level_mask_}); + } + unsigned char d1(LevelMask level_mask) const { + // d1 = refs_count + 8 * is_special + 32 * level + // + 16 * with_hashes - for seriazlization + // d1 = 7 + 16 + 32 * l - for absent cells + return static_cast(refs_count_ + 8 * is_special_ + 32 * level_mask.get_mask()); + } + unsigned char d2() const { + auto res = static_cast((bits_ / 8) * 2); + if ((bits_ & 7) != 0) { + return static_cast(res + 1); + } + return res; + } + size_t get_hashes_offset() const { + return 0; + } + size_t get_refs_offset() const { + return get_hashes_offset() + hash_bytes * hash_count_; + } + size_t get_depth_offset() const { + return get_refs_offset() + refs_count_ * sizeof(Cell*); + } + size_t get_data_offset() const { + return get_depth_offset() + sizeof(td::uint16) * hash_count_; + } + size_t get_storage_size() const { + return get_data_offset() + (bits_ + 7) / 8; + } + + const Hash* get_hashes(const char* storage) const { + return reinterpret_cast(storage + get_hashes_offset()); + } + + Hash* get_hashes(char* storage) const { + return reinterpret_cast(storage + get_hashes_offset()); + } + + const td::uint16* get_depth(const char* storage) const { + return reinterpret_cast(storage + get_depth_offset()); + } + + td::uint16* get_depth(char* storage) const { + return reinterpret_cast(storage + get_depth_offset()); + } + + const unsigned char* get_data(const char* storage) const { + return reinterpret_cast(storage + get_data_offset()); + } + unsigned char* get_data(char* storage) const { + return reinterpret_cast(storage + get_data_offset()); + } + + Cell* const* get_refs(const char* storage) const { + return reinterpret_cast(storage + get_refs_offset()); + } + Cell** get_refs(char* storage) const { + return reinterpret_cast(storage + get_refs_offset()); + } + }; + + Info info_; + virtual char* get_storage() = 0; + virtual const char* get_storage() const = 0; + // TODO: we may also save three different pointers + + void destroy_storage(char* storage); + + explicit DataCell(Info info); + Cell* get_ref_raw_ptr(unsigned idx) const { + DCHECK(idx < get_refs_cnt()); + return info_.get_refs(get_storage())[idx]; + } + + public: + td::Result load_cell() const override { + return LoadedCell{Ref{this}, {}, {}}; + } + unsigned get_refs_cnt() const { + return info_.refs_count_; + } + unsigned get_bits() const { + return info_.bits_; + } + unsigned size_refs() const { + return info_.refs_count_; + } + unsigned size() const { + return info_.bits_; + } + const unsigned char* get_data() const { + return info_.get_data(get_storage()); + } + Ref get_ref(unsigned idx) const { + if (idx >= get_refs_cnt()) { + return Ref{}; + } + return Ref(get_ref_raw_ptr(idx)); + } + + td::uint32 get_virtualization() const override { + return info_.virtualization_; + } + CellUsageTree::NodePtr get_tree_node() const override { + return {}; + } + bool is_loaded() const override { + return true; + } + LevelMask get_level_mask() const override { + return LevelMask{info_.level_mask_}; + } + + bool is_special() const { + return info_.is_special_; + } + SpecialType special_type() const; + int get_serialized_size(bool with_hashes = false) const { + return ((get_bits() + 23) >> 3) + + (with_hashes ? get_level_mask().get_hashes_count() * (hash_bytes + depth_bytes) : 0); + } + int serialize(unsigned char* buff, int buff_size, bool with_hashes = false) const; + std::string serialize() const; + std::string to_hex() const; + static td::int64 get_total_data_cells() { + return get_thread_safe_counter().sum(); + } + + template + void store(StorerT& storer) const { + storer.template store_binary(info_.d1()); + storer.template store_binary(info_.d2()); + storer.store_slice(td::Slice(get_data(), (get_bits() + 7) / 8)); + } + + protected: + static constexpr auto max_storage_size = max_refs * sizeof(void*) + (max_level + 1) * hash_bytes + max_bytes; + + private: + static td::NamedThreadSafeCounter::CounterRef get_thread_safe_counter() { + static auto res = td::NamedThreadSafeCounter::get_default().get_counter("DataCell"); + return res; + } + static std::unique_ptr create_empty_data_cell(Info info); + + const Hash do_get_hash(td::uint32 level) const override; + td::uint16 do_get_depth(td::uint32 level) const override; + + friend class CellBuilder; + static td::Result> create(td::ConstBitPtr data, unsigned bits, td::Span> refs, bool special); + static td::Result> create(td::ConstBitPtr data, unsigned bits, td::MutableSpan> refs, + bool special); +}; + +std::ostream& operator<<(std::ostream& os, const DataCell& c); + +} // namespace vm + diff --git a/crypto/vm/cells/ExtCell.h b/crypto/vm/cells/ExtCell.h new file mode 100644 index 00000000..72490677 --- /dev/null +++ b/crypto/vm/cells/ExtCell.h @@ -0,0 +1,140 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once +#include "vm/cells/Cell.h" +#include "vm/cells/PrunnedCell.h" +#include "common/AtomicRef.h" + +#include +#include "td/utils/port/thread_local.h" +#include "td/utils/HazardPointers.h" +#include "td/utils/optional.h" + +namespace vm { + +template +class ExtCell : public Cell { + private: + struct PrivateTag {}; + + public: + static td::Result>> create(const PrunnedCellInfo& prunned_cell_info, ExtraT&& extra) { + TRY_RESULT(prunned_cell, PrunnedCell::create(prunned_cell_info, std::move(extra))); + return Ref>(true, std::move(prunned_cell), PrivateTag{}); + } + + ExtCell(Ref> prunned_cell, PrivateTag) : prunned_cell_(std::move(prunned_cell)) { + get_thread_safe_counter().add(1); + get_thread_safe_counter_unloaded().add(prunned_cell_.load_unsafe().not_null()); + } + ~ExtCell() { + get_thread_safe_counter().add(-1); + get_thread_safe_counter_unloaded().add(-static_cast(prunned_cell_.load_unsafe().not_null())); + } + + LevelMask get_level_mask() const override { + return CellView(this)->get_level_mask(); + } + + td::Result load_cell() const override { + TRY_RESULT(data_cell, load_data_cell()); + return LoadedCell{std::move(data_cell), {}, {}}; + } + td::uint32 get_virtualization() const override { + return 0; + } + CellUsageTree::NodePtr get_tree_node() const override { + return {}; + } + bool is_loaded() const override { + return CellView(this)->is_loaded(); + } + + private: + mutable td::AtomicRef data_cell_; + mutable td::AtomicRef> prunned_cell_; + + static td::NamedThreadSafeCounter::CounterRef get_thread_safe_counter() { + static auto res = td::NamedThreadSafeCounter::get_default().get_counter("ExtCell"); + return res; + } + + static td::NamedThreadSafeCounter::CounterRef get_thread_safe_counter_unloaded() { + static auto res = td::NamedThreadSafeCounter::get_default().get_counter("ExtCell.unloaded"); + return res; + } + + struct CellView { + CellView(const ExtCell* cell) { + cell_ = cell->data_cell_.get_unsafe(); + if (cell_) { + return; + } + + prunned_cell_ = cell->prunned_cell_.load(); + if (!prunned_cell_.is_null()) { + cell_ = &*prunned_cell_; + return; + } + cell_ = cell->data_cell_.get_unsafe(); + DCHECK(cell_); + } + + const Cell* operator->() const { + return cell_; + } + + td::Ref> prunned_cell_; + const Cell* cell_; + }; + + const Hash do_get_hash(td::uint32 level) const override { + return CellView(this)->get_hash(level); + } + + td::uint16 do_get_depth(td::uint32 level) const override { + return CellView(this)->get_depth(level); + } + + td::Result> load_data_cell() const { + auto data_cell = data_cell_.get_unsafe(); + if (data_cell) { + return Ref(data_cell); + } + + auto prunned_cell = prunned_cell_.load(); + + if (prunned_cell.is_null()) { + data_cell = data_cell_.get_unsafe(); + DCHECK(data_cell); + return Ref(data_cell); + } + + TRY_RESULT(new_data_cell, Loader::load_data_cell(*this, prunned_cell->get_extra())); + TRY_STATUS(prunned_cell->check_equals_unloaded(new_data_cell)); + + if (data_cell_.store_if_empty(new_data_cell)) { + prunned_cell_.store({}); + get_thread_safe_counter_unloaded().add(-1); + } + + return data_cell_.load_unsafe(); + } +}; +} // namespace vm diff --git a/crypto/vm/cells/LevelMask.cpp b/crypto/vm/cells/LevelMask.cpp new file mode 100644 index 00000000..0a6dfb36 --- /dev/null +++ b/crypto/vm/cells/LevelMask.cpp @@ -0,0 +1,32 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "LevelMask.h" + +namespace vm { +namespace detail { +td::StringBuilder& operator<<(td::StringBuilder& sb, LevelMask level_mask) { + sb << "LevelMask{"; + for (int i = 0, level = level_mask.get_level(); i < level; i++) { + sb << "01"[(level_mask.get_mask() >> i) % 2]; + } + sb << "}"; + return sb; +} +} // namespace detail +} // namespace vm diff --git a/crypto/vm/cells/LevelMask.h b/crypto/vm/cells/LevelMask.h new file mode 100644 index 00000000..144fe3b4 --- /dev/null +++ b/crypto/vm/cells/LevelMask.h @@ -0,0 +1,84 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +#include "td/utils/bits.h" +#include "td/utils/int_types.h" +#include "td/utils/StringBuilder.h" + +namespace td { +class StringBuilder; +} + +namespace vm { +namespace detail { +class LevelMask { + public: + explicit LevelMask(td::uint32 new_mask = 0) : mask_(new_mask) { + } + td::uint32 get_mask() const { + return mask_; + } + td::uint32 get_level() const { + return 32 - td::count_leading_zeroes32(mask_); + } + td::uint32 get_hash_i() const { + return td::count_bits32(mask_); + } + td::uint32 get_hashes_count() const { + return get_hash_i() + 1; + } + LevelMask apply(td::uint32 level) const { + DCHECK(level < 32); + return LevelMask{mask_ & ((1u << level) - 1)}; + } + LevelMask apply_or(LevelMask other) const { + return LevelMask{mask_ | other.mask_}; + } + LevelMask shift_right() const { + return LevelMask{mask_ >> 1}; + } + bool is_significant(td::uint32 level) const { + DCHECK(level < 32); + bool res = level == 0 || ((mask_ >> (level - 1)) % 2 != 0); + CHECK(res == (apply(level).get_level() == level)); + return res; + } + + bool operator==(const LevelMask& other) const { + return mask_ == other.mask_; + } + bool operator!=(const LevelMask& other) const { + return !(*this == other); + } + + static LevelMask one_level(td::uint32 level) { + DCHECK(level < 32); + if (level == 0) { + return LevelMask(0); + } + return LevelMask(1 << (level - 1)); + } + + private: + td::uint32 mask_; +}; +td::StringBuilder& operator<<(td::StringBuilder& sb, LevelMask level_mask); +} // namespace detail +} // namespace vm diff --git a/crypto/vm/cells/MerkleProof.cpp b/crypto/vm/cells/MerkleProof.cpp new file mode 100644 index 00000000..1d1032d8 --- /dev/null +++ b/crypto/vm/cells/MerkleProof.cpp @@ -0,0 +1,263 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "vm/cells/MerkleProof.h" +#include "vm/cells/CellBuilder.h" +#include "vm/cells/CellSlice.h" + +#include "td/utils/HashMap.h" +#include "td/utils/HashSet.h" + +namespace vm { +namespace detail { +class MerkleProofImpl { + public: + explicit MerkleProofImpl(MerkleProof::IsPrunnedFunction is_prunned) : is_prunned_(std::move(is_prunned)) { + } + explicit MerkleProofImpl(CellUsageTree *usage_tree) : usage_tree_(usage_tree) { + } + + Ref create_from(Ref cell) { + if (!is_prunned_) { + CHECK(usage_tree_); + dfs_usage_tree(cell, usage_tree_->root_id()); + is_prunned_ = [this](const Ref &cell) { return visited_cells_.count(cell->get_hash()) == 0; }; + } + return dfs(cell, cell->get_level()); + } + + private: + using Key = std::pair; + td::HashMap> cells_; + td::HashSet visited_cells_; + CellUsageTree *usage_tree_{nullptr}; + MerkleProof::IsPrunnedFunction is_prunned_; + + void dfs_usage_tree(Ref cell, CellUsageTree::NodeId node_id) { + if (!usage_tree_->is_loaded(node_id)) { + return; + } + visited_cells_.insert(cell->get_hash()); + CellSlice cs(NoVm(), cell); + for (unsigned i = 0; i < cs.size_refs(); i++) { + dfs_usage_tree(cs.prefetch_ref(i), usage_tree_->get_child(node_id, i)); + } + } + + Ref dfs(Ref cell, int merkle_depth) { + CHECK(cell.not_null()); + Key key{cell->get_hash(), merkle_depth}; + { + auto it = cells_.find(key); + if (it != cells_.end()) { + CHECK(it->second.not_null()); + return it->second; + } + } + + if (is_prunned_(cell)) { + auto res = CellBuilder::create_pruned_branch(cell, merkle_depth + 1); + CHECK(res.not_null()); + cells_.emplace(key, res); + return res; + } + CellSlice cs(NoVm(), cell); + int children_merkle_depth = cs.child_merkle_depth(merkle_depth); + CellBuilder cb; + cb.store_bits(cs.fetch_bits(cs.size())); + for (unsigned i = 0; i < cs.size_refs(); i++) { + cb.store_ref(dfs(cs.prefetch_ref(i), children_merkle_depth)); + } + auto res = cb.finalize(cs.is_special()); + CHECK(res.not_null()); + cells_.emplace(key, res); + return res; + } +}; +} // namespace detail + +Ref MerkleProof::generate_raw(Ref cell, IsPrunnedFunction is_prunned) { + return detail::MerkleProofImpl(is_prunned).create_from(cell); +} + +Ref MerkleProof::generate_raw(Ref cell, CellUsageTree *usage_tree) { + return detail::MerkleProofImpl(usage_tree).create_from(cell); +} + +Ref MerkleProof::virtualize_raw(Ref cell, Cell::VirtualizationParameters virt) { + return cell->virtualize(virt); +} + +Ref MerkleProof::generate(Ref cell, IsPrunnedFunction is_prunned) { + int cell_level = cell->get_level(); + if (cell_level != 0) { + return {}; + } + auto raw = generate_raw(std::move(cell), is_prunned); + return CellBuilder::create_merkle_proof(std::move(raw)); +} + +Ref MerkleProof::generate(Ref cell, CellUsageTree *usage_tree) { + int cell_level = cell->get_level(); + if (cell_level != 0) { + return {}; + } + auto raw = generate_raw(std::move(cell), usage_tree); + return CellBuilder::create_merkle_proof(std::move(raw)); +} + +td::Result> unpack_proof(Ref cell) { + CHECK(cell.not_null()); + td::uint8 level = static_cast(cell->get_level()); + if (level != 0) { + return td::Status::Error("Level of MerkleProof must be zero"); + } + CellSlice cs(NoVm(), std::move(cell)); + if (cs.special_type() != Cell::SpecialType::MerkleProof) { + return td::Status::Error("Not a MekleProof cell"); + } + return cs.fetch_ref(); +} + +Ref MerkleProof::virtualize(Ref cell, int virtualization) { + auto r_raw = unpack_proof(std::move(cell)); + if (r_raw.is_error()) { + return {}; + } + return virtualize_raw(r_raw.move_as_ok(), {0 /*level*/, static_cast(virtualization)}); +} + +class MerkleProofCombine { + public: + MerkleProofCombine(Ref a, Ref b) : a_(std::move(a)), b_(std::move(b)) { + } + td::Result> run() { + TRY_RESULT(a, unpack_proof(a_)); + TRY_RESULT(b, unpack_proof(b_)); + if (a->get_hash(0) != b->get_hash(0)) { + return td::Status::Error("Can't combine MerkleProofs with different roots"); + } + dfs(a, 0); + dfs(b, 0); + return CellBuilder::create_merkle_proof(create_A(a, 0, 0)); + } + + private: + Ref a_; + Ref b_; + + struct Info { + Ref cell_; + Ref prunned_cells_[Cell::max_level]; // Cache prunned cells with different levels to reuse them + + Ref get_prunned_cell(int depth) { + if (depth < Cell::max_level) { + return prunned_cells_[depth]; + } + return {}; + } + Ref get_any_cell() const { + if (cell_.not_null()) { + return cell_; + } + for (auto &cell : prunned_cells_) { + if (cell.not_null()) { + return cell; + } + } + UNREACHABLE(); + } + }; + + using Key = std::pair; + td::HashMap cells_; + td::HashMap> create_A_res_; + td::HashSet visited_; + + void dfs(Ref cell, int merkle_depth) { + if (!visited_.emplace(cell->get_hash(), merkle_depth).second) { + return; + } + + auto &info = cells_[cell->get_hash(merkle_depth)]; + CellSlice cs(NoVm(), cell); + // check if prunned cell is bounded + if (cs.special_type() == Cell::SpecialType::PrunnedBranch && static_cast(cell->get_level()) > merkle_depth) { + info.prunned_cells_[cell->get_level() - 1] = std::move(cell); + return; + } + info.cell_ = std::move(cell); + + auto child_merkle_depth = cs.child_merkle_depth(merkle_depth); + for (size_t i = 0, size = cs.size_refs(); i < size; i++) { + dfs(cs.fetch_ref(), child_merkle_depth); + } + } + + Ref create_A(Ref cell, int merkle_depth, int a_merkle_depth) { + merkle_depth = cell->get_level_mask().apply(merkle_depth).get_level(); + auto key = Key(cell->get_hash(merkle_depth), a_merkle_depth); + auto it = create_A_res_.find(key); + if (it != create_A_res_.end()) { + return it->second; + } + + auto res = do_create_A(std::move(cell), merkle_depth, a_merkle_depth); + create_A_res_.emplace(key, res); + return res; + } + + Ref do_create_A(Ref cell, int merkle_depth, int a_merkle_depth) { + auto &info = cells_[cell->get_hash(merkle_depth)]; + + if (info.cell_.is_null()) { + Ref res = info.get_prunned_cell(a_merkle_depth); + if (res.is_null()) { + res = CellBuilder::create_pruned_branch(info.get_any_cell(), a_merkle_depth + 1, merkle_depth); + } + return res; + } + + CHECK(info.cell_.not_null()); + CellSlice cs(NoVm(), info.cell_); + + //CHECK(cs.size_refs() != 0); + if (cs.size_refs() == 0) { + return info.cell_; + } + + auto child_merkle_depth = cs.child_merkle_depth(merkle_depth); + auto child_a_merkle_depth = cs.child_merkle_depth(a_merkle_depth); + + CellBuilder cb; + cb.store_bits(cs.fetch_bits(cs.size())); + for (unsigned i = 0; i < cs.size_refs(); i++) { + cb.store_ref(create_A(cs.prefetch_ref(i), child_merkle_depth, child_a_merkle_depth)); + } + return cb.finalize(cs.is_special()); + } +}; + +Ref MerkleProof::combine(Ref a, Ref b) { + auto res = MerkleProofCombine(std::move(a), std::move(b)).run(); + if (res.is_error()) { + return {}; + } + return res.move_as_ok(); +} +} // namespace vm diff --git a/crypto/vm/cells/MerkleProof.h b/crypto/vm/cells/MerkleProof.h new file mode 100644 index 00000000..f52a6e0e --- /dev/null +++ b/crypto/vm/cells/MerkleProof.h @@ -0,0 +1,46 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once +#include "vm/cells/Cell.h" + +#include +#include + +namespace vm { +class MerkleProof { + public: + using IsPrunnedFunction = std::function &)>; + + // works with proofs wrapped in MerkleProof special cell + // cells must have zero level + static Ref generate(Ref cell, IsPrunnedFunction is_prunned); + static Ref generate(Ref cell, CellUsageTree *usage_tree); + + // cell must have zero level and must be a MerkleProof + static Ref virtualize(Ref cell, int virtualization); + + static Ref combine(Ref a, Ref b); + + // works with upwrapped proofs + // works fine with cell of non-zero level, but this is not supported (yet?) in MerkeProof special cell + static Ref generate_raw(Ref cell, IsPrunnedFunction is_prunned); + static Ref generate_raw(Ref cell, CellUsageTree *usage_tree); + static Ref virtualize_raw(Ref cell, Cell::VirtualizationParameters virt); +}; +} // namespace vm diff --git a/crypto/vm/cells/MerkleUpdate.cpp b/crypto/vm/cells/MerkleUpdate.cpp new file mode 100644 index 00000000..30b5116e --- /dev/null +++ b/crypto/vm/cells/MerkleUpdate.cpp @@ -0,0 +1,510 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "vm/cells/MerkleUpdate.h" +#include "vm/cells/MerkleProof.h" + +#include "td/utils/HashMap.h" +#include "td/utils/HashSet.h" + +namespace vm { +namespace detail { +class MerkleUpdateApply { + public: + Ref apply(Ref from, Ref update_from, Ref update_to, td::uint32 from_level, + td::uint32 to_level) { + if (from_level != from->get_level()) { + return {}; + } + dfs_both(from, update_from, from_level); + return dfs(update_to, to_level); + } + + private: + using Key = std::pair; + td::HashMap> known_cells_; + td::HashMap> ready_cells_; + + void dfs_both(Ref original, Ref update_from, int merkle_depth) { + CellSlice cs_update_from(NoVm(), update_from); + known_cells_.emplace(original->get_hash(merkle_depth), original); + if (cs_update_from.special_type() == Cell::SpecialType::PrunnedBranch) { + return; + } + int child_merkle_depth = cs_update_from.child_merkle_depth(merkle_depth); + + CellSlice cs_original(NoVm(), original); + for (unsigned i = 0; i < cs_original.size_refs(); i++) { + dfs_both(cs_original.prefetch_ref(i), cs_update_from.prefetch_ref(i), child_merkle_depth); + } + } + + Ref dfs(Ref cell, int merkle_depth) { + CellSlice cs(NoVm(), cell); + if (cs.special_type() == Cell::SpecialType::PrunnedBranch) { + if ((int)cell->get_level() == merkle_depth + 1) { + auto it = known_cells_.find(cell->get_hash(merkle_depth)); + if (it != known_cells_.end()) { + return it->second; + } + return {}; + } + return cell; + } + Key key{cell->get_hash(), merkle_depth}; + { + auto it = ready_cells_.find(key); + if (it != ready_cells_.end()) { + return it->second; + } + } + + int child_merkle_depth = cs.child_merkle_depth(merkle_depth); + + CellBuilder cb; + cb.store_bits(cs.fetch_bits(cs.size())); + for (unsigned i = 0; i < cs.size_refs(); i++) { + auto ref = dfs(cs.prefetch_ref(i), child_merkle_depth); + if (ref.is_null()) { + return {}; + } + cb.store_ref(std::move(ref)); + } + auto res = cb.finalize(cs.is_special()); + ready_cells_.emplace(key, res); + return res; + } +}; + +class MerkleUpdateValidator { + public: + td::Status validate(Ref update_from, Ref update_to, td::uint32 from_level, td::uint32 to_level) { + dfs_from(update_from, from_level); + return dfs_to(update_to, to_level); + } + + private: + td::HashSet known_cells_; + using Key = std::pair; + td::HashSet visited_from_; + td::HashSet visited_to_; + + void dfs_from(Ref cell, int merkle_depth) { + if (!visited_from_.emplace(cell->get_hash(), merkle_depth).second) { + return; + } + CellSlice cs(NoVm(), cell); + known_cells_.insert(cell->get_hash(merkle_depth)); + if (cs.special_type() == Cell::SpecialType::PrunnedBranch) { + return; + } + int child_merkle_depth = cs.child_merkle_depth(merkle_depth); + for (unsigned i = 0; i < cs.size_refs(); i++) { + dfs_from(cs.prefetch_ref(i), child_merkle_depth); + } + } + + td::Status dfs_to(Ref cell, int merkle_depth) { + if (!visited_to_.emplace(cell->get_hash(), merkle_depth).second) { + return td::Status::OK(); + } + CellSlice cs(NoVm(), cell); + if (cs.special_type() == Cell::SpecialType::PrunnedBranch) { + if ((int)cell->get_level() == merkle_depth + 1) { + if (known_cells_.count(cell->get_hash(merkle_depth)) == 0) { + return td::Status::Error(PSLICE() + << "Unknown prunned cell (validate): " << cell->get_hash(merkle_depth).to_hex()); + } + } + return td::Status::OK(); + } + int child_merkle_depth = cs.child_merkle_depth(merkle_depth); + + for (unsigned i = 0; i < cs.size_refs(); i++) { + TRY_STATUS(dfs_to(cs.prefetch_ref(i), child_merkle_depth)); + } + return td::Status::OK(); + } +}; +} // namespace detail + +td::Status MerkleUpdate::may_apply(Ref from, Ref update) { + if (update->get_level() != 0 || from->get_level() != 0) { + return td::Status::Error("Level of update of from is not zero"); + } + CellSlice cs(NoVm(), std::move(update)); + if (cs.special_type() != Cell::SpecialType::MerkleUpdate) { + return td::Status::Error("Update cell is not a MerkeUpdate"); + } + auto update_from = cs.fetch_ref(); + if (from->get_hash(0) != update_from->get_hash(0)) { + return td::Status::Error("Hash mismatch"); + } + return td::Status::OK(); +} + +Ref MerkleUpdate::apply(Ref from, Ref update) { + if (update->get_level() != 0 || from->get_level() != 0) { + return {}; + } + CellSlice cs(NoVm(), std::move(update)); + if (cs.special_type() != Cell::SpecialType::MerkleUpdate) { + return {}; + } + auto update_from = cs.fetch_ref(); + auto update_to = cs.fetch_ref(); + return apply_raw(std::move(from), std::move(update_from), std::move(update_to), 0, 0); +} + +Ref MerkleUpdate::apply_raw(Ref from, Ref update_from, Ref update_to, td::uint32 from_level, + td::uint32 to_level) { + if (from->get_hash(from_level) != update_from->get_hash(from_level)) { + LOG(DEBUG) << "invalid Merkle update: expected old value hash = " << update_from->get_hash(from_level).to_hex() + << ", applied to value with hash = " << from->get_hash(from_level).to_hex(); + return {}; + } + return detail::MerkleUpdateApply().apply(from, std::move(update_from), std::move(update_to), from_level, to_level); +} + +std::pair, Ref> MerkleUpdate::generate_raw(Ref from, Ref to, CellUsageTree *usage_tree) { + // create Merkle update cell->new_cell + auto update_to = MerkleProof::generate_raw(to, [tree = usage_tree](const Ref &cell) { + auto loaded_cell = cell->load_cell().move_as_ok(); // FIXME + if (loaded_cell.data_cell->size_refs() == 0) { + return false; + } + return !loaded_cell.tree_node.empty() && loaded_cell.tree_node.mark_path(tree); + }); + usage_tree->set_use_mark_for_is_loaded(true); + auto update_from = MerkleProof::generate_raw(from, usage_tree); + + return {std::move(update_from), std::move(update_to)}; +} + +td::Status MerkleUpdate::validate_raw(Ref update_from, Ref update_to, td::uint32 from_level, + td::uint32 to_level) { + return detail::MerkleUpdateValidator().validate(std::move(update_from), std::move(update_to), from_level, to_level); +} + +td::Status MerkleUpdate::validate(Ref update) { + if (update->get_level() != 0) { + return td::Status::Error("nonzero level"); + } + CellSlice cs(NoVm(), std::move(update)); + if (cs.special_type() != Cell::SpecialType::MerkleUpdate) { + return td::Status::Error("not a MerkleUpdate cell"); + } + auto update_from = cs.fetch_ref(); + auto update_to = cs.fetch_ref(); + return validate_raw(std::move(update_from), std::move(update_to), 0, 0); +} + +Ref MerkleUpdate::generate(Ref from, Ref to, CellUsageTree *usage_tree) { + auto from_level = from->get_level(); + auto to_level = to->get_level(); + if (from_level != 0 || to_level != 0) { + return {}; + } + auto res = generate_raw(std::move(from), std::move(to), usage_tree); + return CellBuilder::create_merkle_update(res.first, res.second); +} + +namespace detail { +class MerkleCombine { + public: + MerkleCombine(Ref AB, Ref CD) : AB_(std::move(AB)), CD_(std::move(CD)) { + } + + td::Result> run() { + TRY_RESULT(AB, unpack_update(std::move(AB_))); + TRY_RESULT(CD, unpack_update(std::move(CD_))); + std::tie(A_, B_) = AB; + std::tie(C_, D_) = CD; + if (B_->get_hash(0) != C_->get_hash(0)) { + return td::Status::Error("Impossible to combine merkle updates"); + } + + auto log = [](td::Slice name, auto cell) { + CellSlice cs(NoVm(), cell); + LOG(ERROR) << name << " " << cell->get_level(); + cs.print_rec(std::cerr); + }; + if (0) { + log("A", A_); + log("B", B_); + log("C", C_); + log("D", D_); + } + + // We have four bags of cells. A, B, C and D. + // X = Virtualize(A), A is subtree (merkle proof) of X + // Y = Virtualize(B) = Virtualize(C), B and C are subrees of Y + // Z = Virtualize(D), D is subtree of Z + // + // Prunned cells bounded by merkle proof P are essentially cells which are impossible to load during traversal of Virtualize(P) + // + // We want to create new_A and new_D + // Virtualize(new_A) = X + // Virtualize(new_D) = Z + // All prunned branches bounded by new_D must be in new_A + // i.e. if we have all cells reachable in Virtualize(new_A) we may construct Z from them (and from new_D) + // + // Main idea is following + // 1. Create maximum subtrees of X and Z with all cells in A, B, C and D + // Max(V) - such maximum subtree + // + // 2. Max(A) and Max(D) should be merkle update already. But win want to minimize it + // So we cut all branches of Max(D) which are in maxA + // When we cut branch q in Max(D) we mark some path to q in Max(A) + // Then we cut all branches of Max(A) which are not marked. + // + // How to create Max(A)? + // We just store all cells reachable from A, B, C and D in big cache. + // It we reach bounded prunned cell during traversion we may continue traversial with a cell from the cache. + // + // + // 1. load_cells(root) - caches all cell reachable in Virtualize(root); + visited_.clear(); + load_cells(A_, 0); + visited_.clear(); + load_cells(B_, 0); + visited_.clear(); + load_cells(C_, 0); + visited_.clear(); + load_cells(D_, 0); + + // 2. mark_A(A) - Traverse Max(A), but uses all cached cells from step 1. Mark all visited cells + A_usage_tree_ = std::make_shared(); + mark_A(A_, 0, A_usage_tree_->root_id()); + + // 3. create_D(D) - create new_D. Traverse Max(D), and stop at marked cells. Mark path in A to marked cells + auto new_D = create_D(D_, 0, 0); + if (new_D.is_null()) { + return td::Status::Error("Failed to combine updates. One of them is probably an invalid update"); + } + + // 4. create_A(A) - create new_A. Traverse Max(A), and stop at cells not marked at step 3. + auto new_A = create_A(A_, 0, 0); + if (0) { + log("NewD", new_D); + } + + return CellBuilder::create_merkle_update(new_A, new_D); + } + + private: + Ref AB_, CD_; + Ref A_, B_, C_, D_; + + std::shared_ptr A_usage_tree_; + + struct Info { + Ref cell_; + Ref prunned_cells_[Cell::max_level]; // Cache prunned cells with different levels to reuse them + CellUsageTree::NodeId A_node_id{0}; + + Ref get_prunned_cell(int depth) { + if (depth < Cell::max_level) { + return prunned_cells_[depth]; + } + return {}; + } + Ref get_any_cell() const { + if (cell_.not_null()) { + return cell_; + } + for (auto &cell : prunned_cells_) { + if (cell.not_null()) { + return cell; + } + } + UNREACHABLE(); + } + }; + using Key = std::pair; + td::HashMap cells_; + td::HashMap> create_A_res_; + td::HashMap> create_D_res_; + td::HashSet visited_; + + void load_cells(Ref cell, int merkle_depth) { + if (!visited_.emplace(cell->get_hash(), merkle_depth).second) { + return; + } + + auto &info = cells_[cell->get_hash(merkle_depth)]; + CellSlice cs(NoVm(), cell); + + // check if prunned cell is bounded + if (cs.special_type() == Cell::SpecialType::PrunnedBranch && static_cast(cell->get_level()) > merkle_depth) { + info.prunned_cells_[cell->get_level() - 1] = std::move(cell); + return; + } + + info.cell_ = std::move(cell); + + auto child_merkle_depth = cs.child_merkle_depth(merkle_depth); + for (size_t i = 0, size = cs.size_refs(); i < size; i++) { + load_cells(cs.fetch_ref(), child_merkle_depth); + } + } + + void mark_A(Ref cell, int merkle_depth, CellUsageTree::NodeId node_id) { + CHECK(node_id != 0); + + // cell in cache may be virtualized with different level + // so we make merkle_depth as small as possible + merkle_depth = cell->get_level_mask().apply(merkle_depth).get_level(); + + auto &info = cells_[cell->get_hash(merkle_depth)]; + if (info.A_node_id != 0) { + return; + } + info.A_node_id = node_id; + if (info.cell_.is_null()) { + return; + } + + CellSlice cs(NoVm(), info.cell_); + auto child_merkle_depth = cs.child_merkle_depth(merkle_depth); + for (int i = 0, size = cs.size_refs(); i < size; i++) { + mark_A(cs.fetch_ref(), child_merkle_depth, A_usage_tree_->create_child(node_id, i)); + } + } + + Ref create_D(Ref cell, int merkle_depth, int d_merkle_depth) { + merkle_depth = cell->get_level_mask().apply(merkle_depth).get_level(); + auto key = Key(cell->get_hash(merkle_depth), d_merkle_depth); + auto it = create_D_res_.find(key); + if (it != create_D_res_.end()) { + return it->second; + } + + auto res = do_create_D(std::move(cell), merkle_depth, d_merkle_depth); + if (res.is_null()) { + return {}; + } + create_D_res_.emplace(key, res); + return res; + } + + Ref do_create_D(Ref cell, int merkle_depth, int d_merkle_depth) { + auto &info = cells_[cell->get_hash(merkle_depth)]; + if (info.A_node_id != 0) { + A_usage_tree_->mark_path(info.A_node_id); + Ref res = info.get_prunned_cell(d_merkle_depth); + if (res.is_null()) { + res = CellBuilder::create_pruned_branch(info.get_any_cell(), d_merkle_depth + 1, merkle_depth); + } + return res; + } + + if (info.cell_.is_null()) { + return {}; + } + + CellSlice cs(NoVm(), info.cell_); + + if (cs.size_refs() == 0) { + return info.cell_; + } + + auto child_merkle_depth = cs.child_merkle_depth(merkle_depth); + auto child_d_merkle_depth = cs.child_merkle_depth(d_merkle_depth); + + CellBuilder cb; + cb.store_bits(cs.fetch_bits(cs.size())); + for (unsigned i = 0; i < cs.size_refs(); i++) { + auto ref = create_D(cs.prefetch_ref(i), child_merkle_depth, child_d_merkle_depth); + if (ref.is_null()) { + return {}; + } + cb.store_ref(std::move(ref)); + } + return cb.finalize(cs.is_special()); + } + + Ref create_A(Ref cell, int merkle_depth, int a_merkle_depth) { + merkle_depth = cell->get_level_mask().apply(merkle_depth).get_level(); + auto key = Key(cell->get_hash(merkle_depth), a_merkle_depth); + auto it = create_A_res_.find(key); + if (it != create_A_res_.end()) { + return it->second; + } + + auto res = do_create_A(std::move(cell), merkle_depth, a_merkle_depth); + create_A_res_.emplace(key, res); + return res; + } + + Ref do_create_A(Ref cell, int merkle_depth, int a_merkle_depth) { + auto &info = cells_[cell->get_hash(merkle_depth)]; + + CHECK(info.A_node_id != 0); + if (!A_usage_tree_->has_mark(info.A_node_id)) { + Ref res = info.get_prunned_cell(a_merkle_depth); + if (res.is_null()) { + res = CellBuilder::create_pruned_branch(info.get_any_cell(), a_merkle_depth + 1, merkle_depth); + } + return res; + } + + CHECK(info.cell_.not_null()); + CellSlice cs(NoVm(), info.cell_); + + CHECK(cs.size_refs() != 0); + if (cs.size_refs() == 0) { + return info.cell_; + } + + auto child_merkle_depth = cs.child_merkle_depth(merkle_depth); + auto child_a_merkle_depth = cs.child_merkle_depth(a_merkle_depth); + + CellBuilder cb; + cb.store_bits(cs.fetch_bits(cs.size())); + for (unsigned i = 0; i < cs.size_refs(); i++) { + cb.store_ref(create_A(cs.prefetch_ref(i), child_merkle_depth, child_a_merkle_depth)); + } + return cb.finalize(cs.is_special()); + } + + td::Result, Ref>> unpack_update(Ref update) const { + if (update->get_level() != 0) { + return td::Status::Error("level is not zero"); + } + CellSlice cs(NoVm(), std::move(update)); + if (cs.special_type() != Cell::SpecialType::MerkleUpdate) { + return td::Status::Error("Not a Merkle Update cell"); + } + auto update_from = cs.fetch_ref(); + auto update_to = cs.fetch_ref(); + return std::make_pair(std::move(update_from), std::move(update_to)); + } +}; +} // namespace detail + +Ref MerkleUpdate::combine(Ref ab, Ref bc) { + detail::MerkleCombine combine(ab, bc); + auto res = combine.run(); + if (res.is_error()) { + return {}; + } + return res.move_as_ok(); +} + +} // namespace vm diff --git a/crypto/vm/cells/MerkleUpdate.h b/crypto/vm/cells/MerkleUpdate.h new file mode 100644 index 00000000..2978ecc6 --- /dev/null +++ b/crypto/vm/cells/MerkleUpdate.h @@ -0,0 +1,50 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once +#include "vm/cells/Cell.h" +#include "vm/cells/CellSlice.h" +#include "vm/cells/CellBuilder.h" + +#include "td/utils/Status.h" + +#include + +namespace vm { +class MerkleUpdate { + public: + // from + update == to + static Ref generate(Ref from, Ref to, CellUsageTree *usage_tree); + // Returns empty Ref if something go wrong. If validate(from).is_ok() and may_apply(from, to).is_ok(), then it + // must not fail. + static Ref apply(Ref from, Ref update); + + // check if update is valid + static TD_WARN_UNUSED_RESULT td::Status validate(Ref update); + // check that hash in from is same as hash stored in update. Do not validate update + static TD_WARN_UNUSED_RESULT td::Status may_apply(Ref from, Ref update); + + static Ref apply_raw(Ref from, Ref update_from, Ref update_to, td::uint32 from_level, + td::uint32 to_level); + static std::pair, Ref> generate_raw(Ref from, Ref to, CellUsageTree *usage_tree); + static td::Status validate_raw(Ref update_from, Ref update_to, td::uint32 from_level, + td::uint32 to_level); + + static Ref combine(Ref ab, Ref bc); +}; +} // namespace vm diff --git a/crypto/vm/cells/PrunnedCell.h b/crypto/vm/cells/PrunnedCell.h new file mode 100644 index 00000000..caacf1f3 --- /dev/null +++ b/crypto/vm/cells/PrunnedCell.h @@ -0,0 +1,139 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once +#include "vm/cells/CellWithStorage.h" +#include "vm/cells/Cell.h" + +namespace vm { +struct PrunnedCellInfo { + Cell::LevelMask level_mask; + td::Slice hash; + td::Slice depth; +}; + +template +class PrunnedCell : public Cell { + public: + const ExtraT& get_extra() const { + return extra_; + } + + static td::Result>> create(const PrunnedCellInfo& prunned_cell_info, ExtraT&& extra) { + auto level_mask = prunned_cell_info.level_mask; + if (level_mask.get_level() > max_level) { + return td::Status::Error("Level is too big"); + } + Info info(level_mask); + auto prunned_cell = + detail::CellWithUniquePtrStorage>::create(info.get_storage_size(), info, std::move(extra)); + TRY_STATUS(prunned_cell->init(prunned_cell_info)); + return Ref>(prunned_cell.release(), typename Ref>::acquire_t{}); + } + + LevelMask get_level_mask() const override { + return LevelMask(info_.level_mask_); + } + + protected: + struct Info { + Info(LevelMask level_mask) { + level_mask_ = level_mask.get_mask() & 7; + hash_count_ = level_mask.get_hashes_count() & 7; + } + unsigned char level_mask_ : 3; + unsigned char hash_count_ : 3; + size_t get_hashes_offset() const { + return 0; + } + size_t get_depth_offset() const { + return get_hashes_offset() + hash_bytes * hash_count_; + } + size_t get_storage_size() const { + return get_depth_offset() + sizeof(td::uint16) * hash_count_; + } + const Hash* get_hashes(const char* storage) const { + return reinterpret_cast(storage + get_hashes_offset()); + } + Hash* get_hashes(char* storage) const { + return reinterpret_cast(storage + get_hashes_offset()); + } + const td::uint16* get_depth(const char* storage) const { + return reinterpret_cast(storage + get_depth_offset()); + } + td::uint16* get_depth(char* storage) const { + return reinterpret_cast(storage + get_depth_offset()); + } + }; + + Info info_; + ExtraT extra_; + virtual char* get_storage() = 0; + virtual const char* get_storage() const = 0; + void destroy_storage(char* storage) { + // noop + } + + td::Status init(const PrunnedCellInfo& prunned_cell_info) { + auto storage = get_storage(); + auto& new_hash = prunned_cell_info.hash; + auto* hash = info_.get_hashes(storage); + size_t n = prunned_cell_info.level_mask.get_hashes_count(); + CHECK(new_hash.size() == n * hash_bytes); + for (td::uint32 i = 0; i < n; i++) { + hash[i].as_slice().copy_from(new_hash.substr(i * Cell::hash_bytes, Cell::hash_bytes)); + } + + auto& new_depth = prunned_cell_info.depth; + CHECK(new_depth.size() == n * depth_bytes); + auto* depth = info_.get_depth(storage); + for (td::uint32 i = 0; i < n; i++) { + depth[i] = DataCell::load_depth(new_depth.substr(i * Cell::depth_bytes, Cell::depth_bytes).ubegin()); + if (depth[i] > max_depth) { + return td::Status::Error("Depth is too big"); + } + } + return td::Status::OK(); + } + + explicit PrunnedCell(Info info, ExtraT&& extra) : info_(info), extra_(std::move(extra)) { + } + td::uint32 get_virtualization() const override { + return 0; + } + CellUsageTree::NodePtr get_tree_node() const override { + return {}; + } + bool is_loaded() const override { + return false; + } + + private: + const Hash do_get_hash(td::uint32 level) const override { + return info_.get_hashes(get_storage())[get_level_mask().apply(level).get_hash_i()]; + } + + td::uint16 do_get_depth(td::uint32 level) const override { + return info_.get_depth(get_storage())[get_level_mask().apply(level).get_hash_i()]; + } + + td::Result load_cell() const override { + return td::Status::Error("Can't load prunned branch"); + } +}; +} // namespace vm diff --git a/crypto/vm/cells/UsageCell.h b/crypto/vm/cells/UsageCell.h new file mode 100644 index 00000000..09defae2 --- /dev/null +++ b/crypto/vm/cells/UsageCell.h @@ -0,0 +1,88 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once +#include "vm/cells/Cell.h" +#include "vm/cells/CellUsageTree.h" + +namespace vm { +class UsageCell : public Cell { + private: + struct PrivateTag {}; + + public: + UsageCell(Ref cell, CellUsageTree::NodePtr tree_node, PrivateTag) + : cell_(std::move(cell)), tree_node_(std::move(tree_node)) { + } + static Ref create(Ref cell, CellUsageTree::NodePtr tree_node) { + if (tree_node.empty()) { + return cell; + } + return Ref{true, std::move(cell), std::move(tree_node), PrivateTag{}}; + } + + // load interface + td::Result load_cell() const override { + TRY_RESULT(loaded_cell, cell_->load_cell()); + if (tree_node_.on_load()) { + CHECK(loaded_cell.tree_node.empty()); + loaded_cell.tree_node = tree_node_; + } + return std::move(loaded_cell); + } + Ref virtualize(VirtualizationParameters virt) const override { + auto virtualized_cell = cell_->virtualize(virt); + if (tree_node_.empty()) { + return virtualized_cell; + } + if (virtualized_cell.get() == cell_.get()) { + return Ref(this); + } + return create(std::move(virtualized_cell), tree_node_); + } + + td::uint32 get_virtualization() const override { + return cell_->get_virtualization(); + } + + CellUsageTree::NodePtr get_tree_node() const override { + return tree_node_; + } + + bool is_loaded() const override { + return cell_->is_loaded(); + } + + // hash and level + LevelMask get_level_mask() const override { + return cell_->get_level_mask(); + } + + protected: + const Hash do_get_hash(td::uint32 level) const override { + return cell_->get_hash(level); + } + td::uint16 do_get_depth(td::uint32 level) const override { + return cell_->get_depth(level); + } + + private: + Ref cell_; + CellUsageTree::NodePtr tree_node_; +}; +} // namespace vm diff --git a/crypto/vm/cells/VirtualCell.h b/crypto/vm/cells/VirtualCell.h new file mode 100644 index 00000000..8b6a1ac4 --- /dev/null +++ b/crypto/vm/cells/VirtualCell.h @@ -0,0 +1,87 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once +#include "vm/cells/Cell.h" + +namespace vm { +class VirtualCell : public Cell { + private: + struct PrivateTag {}; + + public: + static Ref create(VirtualizationParameters virt, Ref cell) { + if (cell->get_level() <= virt.get_level()) { + return cell; + } + return Ref{true, virt, std::move(cell), PrivateTag{}}; + } + + VirtualCell(VirtualizationParameters virt, Ref cell, PrivateTag) : virt_(virt), cell_(std::move(cell)) { + CHECK(cell_->get_virtualization() <= virt_.get_virtualization()); + } + + // load interface + td::Result load_cell() const override { + TRY_RESULT(loaded_cell, cell_->load_cell()); + loaded_cell.virt = loaded_cell.virt.apply(virt_); + return std::move(loaded_cell); + } + + Ref virtualize(VirtualizationParameters virt) const override { + auto new_virt = virt_.apply(virt); + if (new_virt == virt_) { + return Ref(this); + } + return create(new_virt, cell_); + } + + td::uint32 get_virtualization() const override { + return virt_.get_virtualization(); + } + + CellUsageTree::NodePtr get_tree_node() const override { + return cell_->get_tree_node(); + } + + bool is_loaded() const override { + return cell_->is_loaded(); + } + + // hash and level + LevelMask get_level_mask() const override { + return cell_->get_level_mask().apply(virt_.get_level()); + } + + protected: + const Hash do_get_hash(td::uint32 level) const override { + return cell_->get_hash(fix_level(level)); + } + td::uint16 do_get_depth(td::uint32 level) const override { + return cell_->get_depth(fix_level(level)); + } + + private: + VirtualizationParameters virt_; + Ref cell_; + + int fix_level(int level) const { + return get_level_mask().apply(level).get_level(); + } +}; +} // namespace vm diff --git a/crypto/vm/cells/VirtualizationParameters.h b/crypto/vm/cells/VirtualizationParameters.h new file mode 100644 index 00000000..b2a64ffb --- /dev/null +++ b/crypto/vm/cells/VirtualizationParameters.h @@ -0,0 +1,72 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +#include "td/utils/int_types.h" +#include "td/utils/logging.h" + +#include + +namespace vm { +namespace detail { +class VirtualizationParameters { + public: + static constexpr td::uint8 max_level() { + return std::numeric_limits::max(); + } + + VirtualizationParameters() = default; + + VirtualizationParameters(td::uint8 level, td::uint8 virtualization) : level_(level), virtualization_(virtualization) { + CHECK(virtualization_ != 0 || empty()); + } + + bool empty() const { + return level_ == max_level() && virtualization_ == 0; + } + + VirtualizationParameters apply(VirtualizationParameters outer) const { + if (outer.level_ >= level_) { + return *this; + } + CHECK(virtualization_ <= outer.virtualization_); + return {outer.level_, outer.virtualization_}; + } + + td::uint8 get_level() const { + return level_; + } + + td::uint8 get_virtualization() const { + return virtualization_; + } + + bool operator==(const VirtualizationParameters &other) const { + return level_ == other.level_ && virtualization_ == other.virtualization_; + } + + private: + td::uint8 level_ = max_level(); + td::uint8 virtualization_ = 0; +}; +inline td::StringBuilder &operator<<(td::StringBuilder &sb, const VirtualizationParameters &virt) { + return sb << "{level: " << virt.get_level() << ", virtualization: " << virt.get_virtualization() << "}"; +} +} // namespace detail +} // namespace vm diff --git a/crypto/vm/cellslice.h b/crypto/vm/cellslice.h new file mode 100644 index 00000000..4fe4dcc8 --- /dev/null +++ b/crypto/vm/cellslice.h @@ -0,0 +1,19 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "vm/cells/CellSlice.h" diff --git a/crypto/vm/continuation.cpp b/crypto/vm/continuation.cpp new file mode 100644 index 00000000..24157d3b --- /dev/null +++ b/crypto/vm/continuation.cpp @@ -0,0 +1,877 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "vm/dispatch.h" +#include "vm/continuation.h" +#include "vm/dict.h" +#include "vm/log.h" + +namespace vm { + +int Continuation::jump_w(VmState* st) & { + return static_cast(this)->jump(st); +} + +bool Continuation::has_c0() const { + const ControlData* cont_data = get_cdata(); + return cont_data && cont_data->save.c[0].not_null(); +} + +StackEntry ControlRegs::get(unsigned idx) const { + if (idx < creg_num) { + return get_c(idx); + } else if (idx >= dreg_idx && idx < dreg_idx + dreg_num) { + return get_d(idx); + } else if (idx == 7) { + return c7; + } else { + return {}; + } +} + +bool ControlRegs::set(unsigned idx, StackEntry value) { + if (idx < creg_num) { + auto v = std::move(value).as_cont(); + return v.not_null() && set_c(idx, std::move(v)); + } else if (idx >= dreg_idx && idx < dreg_idx + dreg_num) { + auto v = std::move(value).as_cell(); + return v.not_null() && set_d(idx, std::move(v)); + } else if (idx == 7) { + auto v = std::move(value).as_tuple(); + return v.not_null() && set_c7(std::move(v)); + } else { + return false; + } +} + +bool ControlRegs::define(unsigned idx, StackEntry value) { + if (idx < creg_num) { + auto v = std::move(value).as_cont(); + return v.not_null() && define_c(idx, std::move(v)); + } else if (idx >= dreg_idx && idx < dreg_idx + dreg_num) { + auto v = std::move(value).as_cell(); + return v.not_null() && define_d(idx, std::move(v)); + } else if (idx == 7) { + auto v = std::move(value).as_tuple(); + return v.not_null() && define_c7(std::move(v)); + } else { + return false; + } +} + +ControlRegs& ControlRegs::operator^=(const ControlRegs& save) { + for (int i = 0; i < creg_num; i++) { + c[i] ^= save.c[i]; + } + for (int i = 0; i < dreg_num; i++) { + d[i] ^= save.d[i]; + } + c7 ^= save.c7; + return *this; +} + +ControlRegs& ControlRegs::operator^=(ControlRegs&& save) { + for (int i = 0; i < creg_num; i++) { + c[i] ^= std::move(save.c[i]); + } + for (int i = 0; i < dreg_num; i++) { + d[i] ^= std::move(save.d[i]); + } + c7 ^= std::move(save.c7); + return *this; +} + +ControlRegs& ControlRegs::operator&=(const ControlRegs& save) { + for (int i = 0; i < creg_num; i++) { + c[i] &= save.c[i].is_null(); + } + for (int i = 0; i < dreg_num; i++) { + d[i] &= save.d[i].is_null(); + } + c7 &= save.c7.is_null(); + return *this; +} + +int ExcQuitCont::jump(VmState* st) const & { + int n = 0; + try { + n = st->get_stack().pop_smallint_range(0xffff); + } catch (const VmError& vme) { + n = vme.get_errno(); + } + VM_LOG(st) << "default exception handler, terminating vm with exit code " << n; + return ~n; +} + +int PushIntCont::jump(VmState* st) const & { + VM_LOG(st) << "execute implicit PUSH " << push_val << " (slow)"; + st->get_stack().push_smallint(push_val); + return st->jump(next); +} + +int PushIntCont::jump_w(VmState* st) & { + VM_LOG(st) << "execute implicit PUSH " << push_val; + st->get_stack().push_smallint(push_val); + return st->jump(std::move(next)); +} + +int ArgContExt::jump(VmState* st) const & { + st->adjust_cr(data.save); + if (data.cp != -1) { + st->force_cp(data.cp); + } + return ext->jump(st); +} + +int ArgContExt::jump_w(VmState* st) & { + st->adjust_cr(std::move(data.save)); + if (data.cp != -1) { + st->force_cp(data.cp); + } + return st->jump_to(std::move(ext)); +} + +int RepeatCont::jump(VmState* st) const & { + VM_LOG(st) << "repeat " << count << " more times (slow)\n"; + if (count <= 0) { + return st->jump(after); + } + if (body->has_c0()) { + return st->jump(body); + } + st->set_c0(Ref{true, body, after, count - 1}); + return st->jump(body); +} + +int RepeatCont::jump_w(VmState* st) & { + VM_LOG(st) << "repeat " << count << " more times\n"; + if (count <= 0) { + body.clear(); + return st->jump(std::move(after)); + } + if (body->has_c0()) { + after.clear(); + return st->jump(std::move(body)); + } + // optimization: since this is unique, reuse *this instead of creating new object + --count; + st->set_c0(Ref{this}); + return st->jump(body); +} + +int VmState::repeat(Ref body, Ref after, long long count) { + if (count <= 0) { + body.clear(); + return jump(std::move(after)); + } else { + return jump(Ref{true, std::move(body), std::move(after), count}); + } +} + +int AgainCont::jump(VmState* st) const & { + VM_LOG(st) << "again an infinite loop iteration (slow)\n"; + if (!body->has_c0()) { + st->set_c0(Ref{this}); + } + return st->jump(body); +} + +int AgainCont::jump_w(VmState* st) & { + VM_LOG(st) << "again an infinite loop iteration\n"; + if (!body->has_c0()) { + st->set_c0(Ref{this}); + return st->jump(body); + } else { + return st->jump(std::move(body)); + } +} + +int VmState::again(Ref body) { + return jump(Ref{true, std::move(body)}); +} + +int UntilCont::jump(VmState* st) const & { + VM_LOG(st) << "until loop body end (slow)\n"; + if (st->get_stack().pop_bool()) { + VM_LOG(st) << "until loop terminated\n"; + return st->jump(after); + } + if (!body->has_c0()) { + st->set_c0(Ref{this}); + } + return st->jump(body); +} + +int UntilCont::jump_w(VmState* st) & { + VM_LOG(st) << "until loop body end\n"; + if (st->get_stack().pop_bool()) { + VM_LOG(st) << "until loop terminated\n"; + body.clear(); + return st->jump(std::move(after)); + } + if (!body->has_c0()) { + st->set_c0(Ref{this}); + return st->jump(body); + } else { + after.clear(); + return st->jump(std::move(body)); + } +} + +int VmState::until(Ref body, Ref after) { + if (!body->has_c0()) { + set_c0(Ref{true, body, std::move(after)}); + } + return jump(std::move(body)); +} + +int WhileCont::jump(VmState* st) const & { + if (chkcond) { + VM_LOG(st) << "while loop condition end (slow)\n"; + if (!st->get_stack().pop_bool()) { + VM_LOG(st) << "while loop terminated\n"; + return st->jump(after); + } + if (!body->has_c0()) { + st->set_c0(Ref{true, cond, body, after, false}); + } + return st->jump(body); + } else { + VM_LOG(st) << "while loop body end (slow)\n"; + if (!cond->has_c0()) { + st->set_c0(Ref{true, cond, body, after, true}); + } + return st->jump(cond); + } +} + +int WhileCont::jump_w(VmState* st) & { + if (chkcond) { + VM_LOG(st) << "while loop condition end\n"; + if (!st->get_stack().pop_bool()) { + VM_LOG(st) << "while loop terminated\n"; + cond.clear(); + body.clear(); + return st->jump(std::move(after)); + } + if (!body->has_c0()) { + chkcond = false; // re-use current object since we hold the unique pointer to it + st->set_c0(Ref{this}); + return st->jump(body); + } else { + cond.clear(); + after.clear(); + return st->jump(std::move(body)); + } + } else { + VM_LOG(st) << "while loop body end\n"; + if (!cond->has_c0()) { + chkcond = true; // re-use current object + st->set_c0(Ref{this}); + return st->jump(cond); + } else { + body.clear(); + after.clear(); + return st->jump(std::move(cond)); + } + } +} + +int VmState::loop_while(Ref cond, Ref body, Ref after) { + if (!cond->has_c0()) { + set_c0(Ref{true, cond, std::move(body), std::move(after), true}); + } + return jump(std::move(cond)); +} + +int OrdCont::jump(VmState* st) const & { + st->adjust_cr(data.save); + st->set_code(code, data.cp); + return 0; +} + +int OrdCont::jump_w(VmState* st) & { + st->adjust_cr(std::move(data.save)); + st->set_code(std::move(code), data.cp); + return 0; +} + +void VmState::init_cregs(bool same_c3, bool push_0) { + cr.set_c0(quit0); + cr.set_c1(quit1); + cr.set_c2(Ref{true}); + if (same_c3) { + cr.set_c3(Ref{true, code, cp}); + if (push_0) { + VM_LOG(this) << "implicit PUSH 0 at start\n"; + get_stack().push_smallint(0); + } + } else { + cr.set_c3(Ref{true, 11}); + } + if (cr.d[0].is_null() || cr.d[1].is_null()) { + auto empty_cell = CellBuilder{}.finalize(); + for (int i = 0; i < ControlRegs::dreg_num; i++) { + if (cr.d[i].is_null()) { + cr.d[i] = empty_cell; + } + } + } + if (cr.c7.is_null()) { + cr.set_c7(Ref{true}); + } +} + +VmState::VmState() : cp(-1), dispatch(&dummy_dispatch_table), quit0(true, 0), quit1(true, 1) { + ensure_throw(init_cp(0)); + init_cregs(); +} + +VmState::VmState(Ref _code) + : code(std::move(_code)), cp(-1), dispatch(&dummy_dispatch_table), quit0(true, 0), quit1(true, 1) { + ensure_throw(init_cp(0)); + init_cregs(); +} + +VmState::VmState(Ref _code, Ref _stack, int flags, Ref _data, VmLog log, + std::vector> _libraries) + : code(std::move(_code)) + , stack(std::move(_stack)) + , cp(-1) + , dispatch(&dummy_dispatch_table) + , quit0(true, 0) + , quit1(true, 1) + , log(log) + , libraries(std::move(_libraries)) { + ensure_throw(init_cp(0)); + set_c4(std::move(_data)); + init_cregs(flags & 1, flags & 2); +} + +VmState::VmState(Ref _code, Ref _stack, const GasLimits& gas, int flags, Ref _data, VmLog log, + std::vector> _libraries) + : code(std::move(_code)) + , stack(std::move(_stack)) + , cp(-1) + , dispatch(&dummy_dispatch_table) + , quit0(true, 0) + , quit1(true, 1) + , log(log) + , gas(gas) + , libraries(std::move(_libraries)) { + ensure_throw(init_cp(0)); + set_c4(std::move(_data)); + init_cregs(flags & 1, flags & 2); +} + +Ref VmState::convert_code_cell(Ref code_cell) { + if (code_cell.is_null()) { + return {}; + } + Ref csr{true, NoVmOrd(), code_cell}; + if (csr->is_valid()) { + return csr; + } + return load_cell_slice_ref(CellBuilder{}.store_ref(std::move(code_cell)).finalize()); +} + +bool VmState::init_cp(int new_cp) { + const DispatchTable* dt = DispatchTable::get_table(new_cp); + if (dt) { + cp = new_cp; + dispatch = dt; + return true; + } else { + return false; + } +} + +bool VmState::set_cp(int new_cp) { + return new_cp == cp || init_cp(new_cp); +} + +void VmState::force_cp(int new_cp) { + if (!set_cp(new_cp)) { + throw VmError{Excno::inv_opcode, "unsupported codepage"}; + } +} + +// simple call to a continuation cont +int VmState::call(Ref cont) { + const ControlData* cont_data = cont->get_cdata(); + if (cont_data) { + if (cont_data->save.c[0].not_null()) { + // call reduces to a jump + return jump(std::move(cont)); + } + if (cont_data->stack.not_null() || cont_data->nargs >= 0) { + // if cont has non-empty stack or expects fixed number of arguments, call is not simple + return call(std::move(cont), -1, -1); + } + // create return continuation, to be stored into new c0 + Ref ret = Ref{true, std::move(code), cp}; + ret.unique_write().get_cdata()->save.set_c0(std::move(cr.c[0])); + cr.set_c0( + std::move(ret)); // set c0 to its final value before switching to cont; notice that cont.save.c0 is not set + return jump_to(std::move(cont)); + } + // create return continuation, to be stored into new c0 + Ref ret = Ref{true, std::move(code), cp}; + ret.unique_write().get_cdata()->save.set_c0(std::move(cr.c[0])); + // general implementation of a simple call + cr.set_c0(std::move(ret)); // set c0 to its final value before switching to cont; notice that cont.save.c0 is not set + return jump_to(std::move(cont)); +} + +// call with parameters to continuation cont +int VmState::call(Ref cont, int pass_args, int ret_args) { + const ControlData* cont_data = cont->get_cdata(); + if (cont_data) { + if (cont_data->save.c[0].not_null()) { + // call reduces to a jump + return jump(std::move(cont), pass_args); + } + int depth = stack->depth(); + if (pass_args > depth || cont_data->nargs > depth) { + throw VmError{Excno::stk_und, "stack underflow while calling a continuation: not enough arguments on stack"}; + } + if (cont_data->nargs > pass_args && pass_args >= 0) { + throw VmError{Excno::stk_und, + "stack underflow while calling a closure continuation: not enough arguments passed"}; + } + auto old_c0 = std::move(cr.c[0]); + // optimization(?): decrease refcnts of unused continuations in c[i] as early as possible + preclear_cr(cont_data->save); + // no exceptions should be thrown after this point + int copy = cont_data->nargs, skip = 0; + if (pass_args >= 0) { + if (copy >= 0) { + skip = pass_args - copy; + } else { + copy = pass_args; + } + } + // copy=-1 : pass whole stack, else pass top `copy` elements, drop next `skip` elements. + Ref new_stk; + if (cont_data->stack.not_null() && !cont_data->stack->is_empty()) { + // `cont` already has a stack, create resulting stack from it + if (copy < 0) { + copy = stack->depth(); + } + if (cont->is_unique()) { + // optimization: avoid copying stack if we hold the only copy of `cont` + new_stk = std::move(cont.unique_write().get_cdata()->stack); + } else { + new_stk = cont_data->stack; + } + new_stk.write().move_from_stack(get_stack(), copy); + if (skip > 0) { + get_stack().pop_many(skip); + } + } else if (copy >= 0) { + new_stk = get_stack().split_top(copy, skip); + } else { + new_stk = std::move(stack); + stack.clear(); + } + // create return continuation using the remainder of current stack + Ref ret = Ref{true, std::move(code), cp, std::move(stack), ret_args}; + ret.unique_write().get_cdata()->save.set_c0(std::move(old_c0)); + Ref ord_cont = static_cast>(cont); + set_stack(std::move(new_stk)); + cr.set_c0(std::move(ret)); // ??? if codepage of code in ord_cont is unknown, will end up with incorrect c0 + return jump_to(std::move(cont)); + } else { + // have no continuation data, situation is somewhat simpler + int depth = stack->depth(); + if (pass_args > depth) { + throw VmError{Excno::stk_und, "stack underflow while calling a continuation: not enough arguments on stack"}; + } + // create new stack from the top `pass_args` elements of the current stack + Ref new_stk = (pass_args >= 0 ? get_stack().split_top(pass_args) : std::move(stack)); + // create return continuation using the remainder of the current stack + Ref ret = Ref{true, std::move(code), cp, std::move(stack), ret_args}; + ret.unique_write().get_cdata()->save.set_c0(std::move(cr.c[0])); + set_stack(std::move(new_stk)); + cr.set_c0(std::move(ret)); // ??? if codepage of code in ord_cont is unknown, will end up with incorrect c0 + return jump_to(std::move(cont)); + } +} + +// simple jump to continuation cont +int VmState::jump(Ref cont) { + const ControlData* cont_data = cont->get_cdata(); + if (cont_data && (cont_data->stack.not_null() || cont_data->nargs >= 0)) { + // if cont has non-empty stack or expects fixed number of arguments, jump is not simple + return jump(std::move(cont), -1); + } else { + return jump_to(std::move(cont)); + } +} + +// general jump to continuation cont +int VmState::jump(Ref cont, int pass_args) { + const ControlData* cont_data = cont->get_cdata(); + if (cont_data) { + // first do the checks + int depth = stack->depth(); + if (pass_args > depth || cont_data->nargs > depth) { + throw VmError{Excno::stk_und, "stack underflow while jumping to a continuation: not enough arguments on stack"}; + } + if (cont_data->nargs > pass_args && pass_args >= 0) { + throw VmError{Excno::stk_und, + "stack underflow while jumping to closure continuation: not enough arguments passed"}; + } + // optimization(?): decrease refcnts of unused continuations in c[i] as early as possible + preclear_cr(cont_data->save); + // no exceptions should be thrown after this point + int copy = cont_data->nargs; + if (pass_args >= 0 && copy < 0) { + copy = pass_args; + } + // copy=-1 : pass whole stack, else pass top `copy` elements, drop the remainder. + if (cont_data->stack.not_null() && !cont_data->stack->is_empty()) { + // `cont` already has a stack, create resulting stack from it + if (copy < 0) { + copy = get_stack().depth(); + } + Ref new_stk; + if (cont->is_unique()) { + // optimization: avoid copying the stack if we hold the only copy of `cont` + new_stk = std::move(cont.unique_write().get_cdata()->stack); + } else { + new_stk = cont_data->stack; + } + new_stk.write().move_from_stack(get_stack(), copy); + set_stack(std::move(new_stk)); + } else { + if (copy >= 0) { + get_stack().drop_bottom(stack->depth() - copy); + } + } + return jump_to(std::move(cont)); + } else { + // have no continuation data, situation is somewhat simpler + if (pass_args >= 0) { + int depth = get_stack().depth(); + if (pass_args > depth) { + throw VmError{Excno::stk_und, "stack underflow while jumping to a continuation: not enough arguments on stack"}; + } + get_stack().drop_bottom(depth - pass_args); + } + return jump_to(std::move(cont)); + } +} + +int VmState::ret() { + Ref cont = quit0; + cont.swap(cr.c[0]); + return jump(std::move(cont)); +} + +int VmState::ret(int ret_args) { + Ref cont = quit0; + cont.swap(cr.c[0]); + return jump(std::move(cont), ret_args); +} + +int VmState::ret_alt() { + Ref cont = quit1; + cont.swap(cr.c[1]); + return jump(std::move(cont)); +} + +int VmState::ret_alt(int ret_args) { + Ref cont = quit1; + cont.swap(cr.c[1]); + return jump(std::move(cont), ret_args); +} + +Ref VmState::extract_cc(int save_cr, int stack_copy, int cc_args) { + Ref new_stk; + if (stack_copy < 0 || stack_copy == stack->depth()) { + new_stk = std::move(stack); + stack.clear(); + } else if (stack_copy > 0) { + stack->check_underflow(stack_copy); + new_stk = get_stack().split_top(stack_copy); + } else { + new_stk = Ref{true}; + } + Ref cc = Ref{true, std::move(code), cp, std::move(stack), cc_args}; + stack = std::move(new_stk); + if (save_cr & 7) { + ControlData* cdata = cc.unique_write().get_cdata(); + if (save_cr & 1) { + cdata->save.set_c0(std::move(cr.c[0])); + cr.set_c0(quit0); + } + if (save_cr & 2) { + cdata->save.set_c1(std::move(cr.c[1])); + cr.set_c1(quit1); + } + if (save_cr & 4) { + cdata->save.set_c2(std::move(cr.c[2])); + // cr.set_c2(Ref{true}); + } + } + return cc; +} + +int VmState::throw_exception(int excno) { + Stack& stack_ref = get_stack(); + stack_ref.clear(); + stack_ref.push_smallint(0); + stack_ref.push_smallint(excno); + code.clear(); + consume_gas(exception_gas_price); + return jump(get_c2()); +} + +int VmState::throw_exception(int excno, StackEntry&& arg) { + Stack& stack_ref = get_stack(); + stack_ref.clear(); + stack_ref.push(std::move(arg)); + stack_ref.push_smallint(excno); + code.clear(); + consume_gas(exception_gas_price); + return jump(get_c2()); +} + +void GasLimits::gas_exception() const { + throw VmNoGas{}; +} + +void GasLimits::set_limits(long long _max, long long _limit, long long _credit) { + gas_max = _max; + gas_limit = _limit; + gas_credit = _credit; + change_base(_limit + _credit); +} + +void GasLimits::change_limit(long long _limit) { + _limit = std::min(std::max(_limit, 0LL), gas_max); + gas_credit = 0; + gas_limit = _limit; + change_base(_limit); +} + +bool VmState::set_gas_limits(long long _max, long long _limit, long long _credit) { + gas.set_limits(_max, _limit, _credit); + return true; +} + +void VmState::change_gas_limit(long long new_limit) { + VM_LOG(this) << "changing gas limit to " << std::min(new_limit, gas.gas_max); + gas.change_limit(new_limit); +} + +int VmState::step() { + assert(!code.is_null()); + //VM_LOG(st) << "stack:"; stack->dump(VM_LOG(st)); + //VM_LOG(st) << "; cr0.refcnt = " << get_c0()->get_refcnt() - 1 << std::endl; + if (stack_trace) { + stack->dump(std::cerr); + } + ++steps; + if (code->size()) { + return dispatch->dispatch(this, code.write()); + } else if (code->size_refs()) { + VM_LOG(this) << "execute implicit JMPREF\n"; + Ref cont = Ref{true, load_cell_slice_ref(code->prefetch_ref()), get_cp()}; + return jump(std::move(cont)); + } else { + VM_LOG(this) << "execute implicit RET\n"; + return ret(); + } +} + +int VmState::run() { + int res; + Guard guard(this); + do { + // LOG(INFO) << "[BS] data cells: " << DataCell::get_total_data_cells(); + try { + try { + res = step(); + gas.check(); + } catch (vm::CellBuilder::CellWriteError) { + throw VmError{Excno::cell_ov}; + } catch (vm::CellBuilder::CellCreateError) { + throw VmError{Excno::cell_ov}; + } catch (vm::CellSlice::CellReadError) { + throw VmError{Excno::cell_und}; + } + } catch (const VmError& vme) { + VM_LOG(this) << "handling exception code " << vme.get_errno() << ": " << vme.get_msg(); + try { + // LOG(INFO) << "[EX] data cells: " << DataCell::get_total_data_cells(); + ++steps; + res = throw_exception(vme.get_errno()); + } catch (const VmError& vme2) { + VM_LOG(this) << "exception " << vme2.get_errno() << " while handling exception: " << vme.get_msg(); + // LOG(INFO) << "[EXX] data cells: " << DataCell::get_total_data_cells(); + return ~vme2.get_errno(); + } + } catch (VmNoGas vmoog) { + ++steps; + VM_LOG(this) << "unhandled out-of-gas exception: gas consumed=" << gas.gas_consumed() + << ", limit=" << gas.gas_limit; + get_stack().clear(); + get_stack().push_smallint(gas.gas_consumed()); + return vmoog.get_errno(); // no ~ for unhandled exceptions (to make their faking impossible) + } + } while (!res); + // LOG(INFO) << "[EN] data cells: " << DataCell::get_total_data_cells(); + return res; +} + +ControlData* force_cdata(Ref& cont) { + if (!cont->get_cdata()) { + cont = Ref{true, cont}; + return cont.unique_write().get_cdata(); + } else { + return cont.write().get_cdata(); + } +} + +ControlRegs* force_cregs(Ref& cont) { + return &force_cdata(cont)->save; +} + +int run_vm_code(Ref code, Ref& stack, int flags, Ref* data_ptr, VmLog log, long long* steps, + GasLimits* gas_limits, std::vector> libraries) { + VmState vm{ + code, std::move(stack), gas_limits ? *gas_limits : GasLimits{}, flags, data_ptr ? *data_ptr : Ref{}, + log, std::move(libraries)}; + int res = vm.run(); + stack = vm.get_stack_ref(); + if (res == -1 && data_ptr) { + *data_ptr = vm.get_c4(); + } + if (steps) { + *steps = vm.get_steps_count(); + } + if (gas_limits) { + *gas_limits = vm.get_gas_limits(); + LOG(INFO) << "steps: " << vm.get_steps_count() << " gas: used=" << gas_limits->gas_consumed() + << ", max=" << gas_limits->gas_max << ", limit=" << gas_limits->gas_limit + << ", credit=" << gas_limits->gas_credit; + } + if ((vm.get_log().log_mask & vm::VmLog::DumpStack) != 0) { + VM_LOG(&vm) << "BEGIN_STACK_DUMP"; + for (int i = stack->depth(); i > 0; i--) { + VM_LOG(&vm) << (*stack)[i - 1].to_string(); + } + VM_LOG(&vm) << "END_STACK_DUMP"; + } + + return ~res; +} + +int run_vm_code(Ref code, Stack& stack, int flags, Ref* data_ptr, VmLog log, long long* steps, + GasLimits* gas_limits, std::vector> libraries) { + Ref stk{true}; + stk.unique_write().set_contents(std::move(stack)); + stack.clear(); + int res = run_vm_code(code, stk, flags, data_ptr, log, steps, gas_limits, std::move(libraries)); + CHECK(stack.is_unique()); + if (stk.is_null()) { + stack.clear(); + } else if (&(*stk) != &stack) { + VmState* st = nullptr; + if (stk->is_unique()) { + VM_LOG(st) << "move resulting stack (" << stk->depth() << " entries)"; + stack.set_contents(std::move(stk.unique_write())); + } else { + VM_LOG(st) << "copying resulting stack (" << stk->depth() << " entries)"; + stack.set_contents(*stk); + } + } + return res; +} + +// may throw a dictionary exception; returns nullptr if library is not found in context +Ref VmState::load_library(td::ConstBitPtr hash) { + std::unique_ptr tmp_ctx; + // install temporary dummy vm state interface to prevent charging for cell load operations during library lookup + VmStateInterface::Guard(tmp_ctx.get()); + for (const auto& lib_collection : libraries) { + auto lib = lookup_library_in(hash, lib_collection); + if (lib.not_null()) { + return lib; + } + } + return {}; +} + +bool VmState::register_library_collection(Ref lib) { + if (lib.is_null()) { + return true; + } + libraries.push_back(std::move(lib)); + return true; +} + +void VmState::register_cell_load() { + consume_gas(cell_load_gas_price); +} + +void VmState::register_cell_create() { + consume_gas(cell_create_gas_price); +} + +td::BitArray<256> VmState::get_state_hash() const { + // TODO: implement properly, by serializing the stack etc, and computing the Merkle hash + td::BitArray<256> res; + res.clear(); + return res; +} + +td::BitArray<256> VmState::get_final_state_hash(int exit_code) const { + // TODO: implement properly, by serializing the stack etc, and computing the Merkle hash + td::BitArray<256> res; + res.clear(); + return res; +} + +Ref lookup_library_in(td::ConstBitPtr key, vm::Dictionary& dict) { + try { + auto val = dict.lookup(key, 256); + if (val.is_null() || !val->have_refs()) { + return {}; + } + auto root = val->prefetch_ref(); + if (root.not_null() && !root->get_hash().bits().compare(key, 256)) { + return root; + } + return {}; + } catch (vm::VmError) { + return {}; + } +} + +Ref lookup_library_in(td::ConstBitPtr key, Ref lib_root) { + if (lib_root.is_null()) { + return lib_root; + } + vm::Dictionary dict{std::move(lib_root), 256}; + return lookup_library_in(key, dict); +} + +} // namespace vm diff --git a/crypto/vm/continuation.h b/crypto/vm/continuation.h new file mode 100644 index 00000000..80f19e15 --- /dev/null +++ b/crypto/vm/continuation.h @@ -0,0 +1,585 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +#include "common/refcnt.hpp" +#include "vm/cellslice.h" +#include "vm/stack.hpp" +#include "vm/vmstate.h" +#include "vm/log.h" + +namespace vm { + +using td::Ref; + +class VmState; +class Continuation; +class DispatchTable; + +struct ControlRegs { + static constexpr int creg_num = 4, dreg_num = 2, dreg_idx = 4; + Ref c[creg_num]; // c0..c3 + Ref d[dreg_num]; // c4..c5 + Ref c7; // c7 + Ref get_c(unsigned idx) const { + return idx < creg_num ? c[idx] : Ref{}; + } + Ref get_d(unsigned idx) const { + idx -= dreg_idx; + return idx < dreg_num ? d[idx] : Ref{}; + } + Ref get_c7() const { + return c7; + } + StackEntry get(unsigned idx) const; + static bool valid_idx(unsigned idx) { + return idx < creg_num || (idx >= dreg_idx && idx < dreg_idx + dreg_num) || idx == 7; + } + void set_c0(Ref cont) { + c[0] = std::move(cont); + } + void set_c1(Ref cont) { + c[1] = std::move(cont); + } + void set_c2(Ref cont) { + c[2] = std::move(cont); + } + void set_c3(Ref cont) { + c[3] = std::move(cont); + } + void set_c4(Ref cell) { + d[0] = std::move(cell); + } + bool set_c(unsigned idx, Ref cont) { + if (idx < creg_num) { + c[idx] = std::move(cont); + return true; + } else { + return false; + } + } + bool set_d(unsigned idx, Ref cell) { + idx -= dreg_idx; + if (idx < dreg_num) { + d[idx] = std::move(cell); + return true; + } else { + return false; + } + } + bool set_c7(Ref tuple) { + c7 = std::move(tuple); + return true; + } + bool set(unsigned idx, StackEntry value); + void define_c0(Ref cont) { + if (c[0].is_null()) { + c[0] = std::move(cont); + } + } + void define_c1(Ref cont) { + if (c[1].is_null()) { + c[1] = std::move(cont); + } + } + void define_c2(Ref cont) { + if (c[2].is_null()) { + c[2] = std::move(cont); + } + } + bool define_c(unsigned idx, Ref cont) { + if (idx < creg_num && c[idx].is_null()) { + c[idx] = std::move(cont); + return true; + } else { + return false; + } + } + bool define_d(unsigned idx, Ref cell) { + idx -= dreg_idx; + if (idx < dreg_num && d[idx].is_null()) { + d[idx] = std::move(cell); + return true; + } else { + return false; + } + } + void define_c4(Ref cell) { + if (d[0].is_null()) { + d[0] = std::move(cell); + } + } + bool define_c7(Ref tuple) { + if (c7.is_null()) { + c7 = std::move(tuple); + } + return true; + } + bool define(unsigned idx, StackEntry value); + ControlRegs& operator&=(const ControlRegs& save); // clears all c[i]'s which are present in save + ControlRegs& operator^=(const ControlRegs& save); // sets c[i]=save.c[i] for all save.c[i] != 0 + ControlRegs& operator^=(ControlRegs&& save); +}; + +struct ControlData { + Ref stack; + ControlRegs save; + int nargs; + int cp; + ControlData() : nargs(-1), cp(-1) { + } + ControlData(int _cp) : nargs(-1), cp(_cp) { + } + ControlData(Ref _stack) : stack(std::move(_stack)), nargs(-1), cp(-1) { + } + ControlData(int _cp, Ref _stack, int _nargs = -1) : stack(std::move(_stack)), nargs(_nargs), cp(_cp) { + } +}; + +class Continuation : public td::CntObject { + public: + virtual int jump(VmState* st) const & = 0; + virtual int jump_w(VmState* st) &; + virtual ControlData* get_cdata() { + return 0; + } + virtual const ControlData* get_cdata() const { + return 0; + } + bool has_c0() const; + Continuation() { + } + Continuation(const Continuation&) = default; + Continuation(Continuation&&) { + } + Continuation& operator=(const Continuation&) { + return *this; + } + Continuation& operator=(Continuation&&) { + return *this; + } + ~Continuation() override = default; +}; + +class QuitCont : public Continuation { + int exit_code; + + public: + QuitCont(int _code = 0) : exit_code(_code) { + } + ~QuitCont() override = default; + int jump(VmState* st) const & override { + return ~exit_code; + } +}; + +class ExcQuitCont : public Continuation { + public: + ExcQuitCont() = default; + ~ExcQuitCont() override = default; + int jump(VmState* st) const & override; +}; + +class PushIntCont : public Continuation { + int push_val; + Ref next; + + public: + PushIntCont(int val, Ref _next) : push_val(val), next(_next) { + } + ~PushIntCont() override = default; + int jump(VmState* st) const & override; + int jump_w(VmState* st) & override; +}; + +class RepeatCont : public Continuation { + Ref body, after; + long long count; + + public: + RepeatCont(Ref _body, Ref _after, long long _count) + : body(std::move(_body)), after(std::move(_after)), count(_count) { + } + ~RepeatCont() override = default; + int jump(VmState* st) const & override; + int jump_w(VmState* st) & override; +}; + +class AgainCont : public Continuation { + Ref body; + + public: + AgainCont(Ref _body) : body(std::move(_body)) { + } + ~AgainCont() override = default; + int jump(VmState* st) const & override; + int jump_w(VmState* st) & override; +}; + +class UntilCont : public Continuation { + Ref body, after; + + public: + UntilCont(Ref _body, Ref _after) : body(std::move(_body)), after(std::move(_after)) { + } + ~UntilCont() override = default; + int jump(VmState* st) const & override; + int jump_w(VmState* st) & override; +}; + +class WhileCont : public Continuation { + Ref cond, body, after; + bool chkcond; + + public: + WhileCont(Ref _cond, Ref _body, Ref _after, bool _chk = true) + : cond(std::move(_cond)), body(std::move(_body)), after(std::move(_after)), chkcond(_chk) { + } + ~WhileCont() override = default; + int jump(VmState* st) const & override; + int jump_w(VmState* st) & override; +}; + +class ArgContExt : public Continuation { + ControlData data; + Ref ext; + + public: + ArgContExt(Ref _ext) : data(), ext(_ext) { + } + ArgContExt(Ref _ext, Ref _stack) : data(_stack), ext(_ext) { + } + ArgContExt(const ArgContExt&) = default; + ArgContExt(ArgContExt&&) = default; + ~ArgContExt() override = default; + int jump(VmState* st) const & override; + int jump_w(VmState* st) & override; + ControlData* get_cdata() override { + return &data; + } + const ControlData* get_cdata() const override { + return &data; + } + td::CntObject* make_copy() const override { + return new ArgContExt{*this}; + } +}; + +class OrdCont : public Continuation { + ControlData data; + Ref code; + friend class VmState; + + public: + OrdCont() : data(), code() { + } + //OrdCont(Ref _code) : data(), code(std::move(_code)) {} + OrdCont(Ref _code, int _cp) : data(_cp), code(std::move(_code)) { + } + //OrdCont(Ref _code, Ref _stack) : data(std::move(_stack)), code(std::move(_code)) {} + OrdCont(Ref _code, int _cp, Ref _stack, int nargs = -1) + : data(_cp, std::move(_stack), nargs), code(std::move(_code)) { + } + OrdCont(const OrdCont&) = default; + OrdCont(OrdCont&&) = default; + ~OrdCont() override = default; + + td::CntObject* make_copy() const override { + return new OrdCont{*this}; + } + int jump(VmState* st) const & override; + int jump_w(VmState* st) & override; + + ControlData* get_cdata() override { + return &data; + } + const ControlData* get_cdata() const override { + return &data; + } + Stack& get_stack() { + return data.stack.write(); + } + const Stack& get_stack_const() const { + return *(data.stack); + } + Ref get_stack_ref() const { + return data.stack; + } + Ref copy_ord() const & { + return Ref{true, *this}; + } + Ref copy_ord() && { + return Ref{true, *this}; + } +}; + +struct GasLimits { + static constexpr long long infty = (1ULL << 63) - 1; + long long gas_max, gas_limit, gas_credit, gas_remaining, gas_base; + GasLimits() : gas_max(infty), gas_limit(infty), gas_credit(0), gas_remaining(infty), gas_base(infty) { + } + GasLimits(long long _limit, long long _max = infty, long long _credit = 0) + : gas_max(_max) + , gas_limit(_limit) + , gas_credit(_credit) + , gas_remaining(_limit + _credit) + , gas_base(gas_remaining) { + } + long long gas_consumed() const { + return gas_base - gas_remaining; + } + void set_limits(long long _max, long long _limit, long long _credit = 0); + void change_base(long long _base) { + gas_remaining += _base - gas_base; + gas_base = _base; + } + void change_limit(long long _limit); + void consume(long long amount) { + // LOG(DEBUG) << "consume " << amount << " gas (" << gas_remaining << " remaining)"; + gas_remaining -= amount; + } + bool try_consume(long long amount) { + // LOG(DEBUG) << "try consume " << amount << " gas (" << gas_remaining << " remaining)"; + return (gas_remaining -= amount) >= 0; + } + void gas_exception() const; + void gas_exception(bool cond) const { + if (!cond) { + gas_exception(); + } + } + void consume_chk(long long amount) { + gas_exception(try_consume(amount)); + } + void check() const { + gas_exception(gas_remaining >= 0); + } + bool final_ok() const { + return gas_remaining >= gas_credit; + } +}; + +class VmState final : public VmStateInterface { + Ref code; + Ref stack; + ControlRegs cr; + int cp; + long long steps{0}; + const DispatchTable* dispatch; + Ref quit0, quit1; + VmLog log; + GasLimits gas; + std::vector> libraries; + int stack_trace{0}, debug_off{0}; + + public: + static constexpr unsigned cell_load_gas_price = 100, cell_create_gas_price = 500, exception_gas_price = 50, + tuple_entry_gas_price = 1; + VmState(); + VmState(Ref _code); + VmState(Ref _code, Ref _stack, int flags = 0, Ref _data = {}, VmLog log = {}, + std::vector> _libraries = {}); + VmState(Ref _code, Ref _stack, const GasLimits& _gas, int flags = 0, Ref _data = {}, + VmLog log = {}, std::vector> _libraries = {}); + template + VmState(Ref code_cell, Args&&... args) + : VmState(convert_code_cell(std::move(code_cell)), std::forward(args)...) { + } + bool set_gas_limits(long long _max, long long _limit, long long _credit = 0); + bool final_gas_ok() const { + return gas.final_ok(); + } + long long gas_consumed() const { + return gas.gas_consumed(); + } + void consume_gas(long long amount) { + gas.consume(amount); + } + void consume_tuple_gas(unsigned tuple_len) { + consume_gas(tuple_len * tuple_entry_gas_price); + } + void consume_tuple_gas(const Ref& tup) { + if (tup.not_null()) { + consume_tuple_gas((unsigned)tup->size()); + } + } + GasLimits get_gas_limits() const { + return gas; + } + void change_gas_limit(long long new_limit); + template + void check_underflow(Args... args) { + stack->check_underflow(args...); + } + bool register_library_collection(Ref lib); + Ref load_library( + td::ConstBitPtr hash) override; // may throw a dictionary exception; returns nullptr if library is not found + void register_cell_load() override; + void register_cell_create() override; + bool init_cp(int new_cp); + bool set_cp(int new_cp); + void force_cp(int new_cp); + int get_cp() const { + return cp; + } + int incr_stack_trace(int v) { + return stack_trace += v; + } + long long get_steps_count() const { + return steps; + } + td::BitArray<256> get_state_hash() const; + td::BitArray<256> get_final_state_hash(int exit_code) const; + int step(); + int run(); + Stack& get_stack() { + return stack.write(); + } + const Stack& get_stack_const() const { + return *stack; + } + Ref get_stack_ref() const { + return stack; + } + Ref get_c0() const { + return cr.c[0]; + } + Ref get_c1() const { + return cr.c[1]; + } + Ref get_c2() const { + return cr.c[2]; + } + Ref get_c3() const { + return cr.c[3]; + } + Ref get_c4() const { + return cr.d[0]; + } + Ref get_c7() const { + return cr.c7; + } + Ref get_c(unsigned idx) const { + return cr.get_c(idx); + } + Ref get_d(unsigned idx) const { + return cr.get_d(idx); + } + StackEntry get(unsigned idx) const { + return cr.get(idx); + } + const VmLog& get_log() const { + return log; + } + void define_c0(Ref cont) { + cr.define_c0(std::move(cont)); + } + void set_c0(Ref cont) { + cr.set_c0(std::move(cont)); + } + void set_c1(Ref cont) { + cr.set_c1(std::move(cont)); + } + void set_c2(Ref cont) { + cr.set_c2(std::move(cont)); + } + bool set_c(unsigned idx, Ref val) { + return cr.set_c(idx, std::move(val)); + } + bool set_d(unsigned idx, Ref val) { + return cr.set_d(idx, std::move(val)); + } + void set_c4(Ref val) { + cr.set_c4(std::move(val)); + } + bool set_c7(Ref val) { + return cr.set_c7(std::move(val)); + } + bool set(unsigned idx, StackEntry val) { + return cr.set(idx, std::move(val)); + } + void set_stack(Ref new_stk) { + stack = std::move(new_stk); + } + Ref swap_stack(Ref new_stk) { + stack.swap(new_stk); + return new_stk; + } + void ensure_throw(bool cond) const { + if (!cond) { + fatal(); + } + } + void set_code(Ref _code, int _cp) { + code = std::move(_code); + force_cp(_cp); + } + Ref get_code() const { + return code; + } + void push_code() { + get_stack().push_cellslice(get_code()); + } + void adjust_cr(const ControlRegs& save) { + cr ^= save; + } + void adjust_cr(ControlRegs&& save) { + cr ^= save; + } + void preclear_cr(const ControlRegs& save) { + cr &= save; + } + int call(Ref cont); + int call(Ref cont, int pass_args, int ret_args = -1); + int jump(Ref cont); + int jump(Ref cont, int pass_args); + int ret(); + int ret(int ret_args); + int ret_alt(); + int ret_alt(int ret_args); + int repeat(Ref body, Ref after, long long count); + int again(Ref body); + int until(Ref body, Ref after); + int loop_while(Ref cond, Ref body, Ref after); + int throw_exception(int excno, StackEntry&& arg); + int throw_exception(int excno); + Ref extract_cc(int save_cr = 1, int stack_copy = -1, int cc_args = -1); + void fatal(void) const { + throw VmFatal{}; + } + int jump_to(Ref cont) { + return cont->is_unique() ? cont.unique_write().jump_w(this) : cont->jump(this); + } + static Ref convert_code_cell(Ref code_cell); + + private: + void init_cregs(bool same_c3 = false, bool push_0 = true); +}; + +int run_vm_code(Ref _code, Ref& _stack, int flags = 0, Ref* data_ptr = 0, VmLog log = {}, + long long* steps = nullptr, GasLimits* gas_limits = nullptr, std::vector> libraries = {}); +int run_vm_code(Ref _code, Stack& _stack, int flags = 0, Ref* data_ptr = 0, VmLog log = {}, + long long* steps = nullptr, GasLimits* gas_limits = nullptr, std::vector> libraries = {}); + +ControlData* force_cdata(Ref& cont); +ControlRegs* force_cregs(Ref& cont); + +Ref lookup_library_in(td::ConstBitPtr key, Ref lib_root); + +} // namespace vm diff --git a/crypto/vm/contops.cpp b/crypto/vm/contops.cpp new file mode 100644 index 00000000..0039439e --- /dev/null +++ b/crypto/vm/contops.cpp @@ -0,0 +1,1043 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include +#include "vm/contops.h" +#include "vm/log.h" +#include "vm/opctable.h" +#include "vm/stack.hpp" +#include "vm/continuation.h" +#include "vm/cellops.h" +#include "vm/excno.hpp" + +namespace vm { + +int exec_execute(VmState* st) { + VM_LOG(st) << "execute EXECUTE\n"; + auto cont = st->get_stack().pop_cont(); + return st->call(std::move(cont)); +} + +int exec_callx_args(VmState* st, unsigned args) { + unsigned params = (args >> 4) & 15, retvals = (args & 15); + VM_LOG(st) << "execute CALLXARGS " << params << ',' << retvals; + auto cont = st->get_stack().pop_cont(); + return st->call(std::move(cont), params, retvals); +} + +int exec_callx_args_p(VmState* st, unsigned args) { + unsigned params = (args & 15); + VM_LOG(st) << "execute CALLXARGS " << params << ",-1\n"; + auto cont = st->get_stack().pop_cont(); + return st->call(std::move(cont), params, -1); +} + +int exec_jmpx(VmState* st) { + VM_LOG(st) << "execute JMPX\n"; + auto cont = st->get_stack().pop_cont(); + return st->jump(std::move(cont)); +} + +int exec_jmpx_args(VmState* st, unsigned args) { + unsigned params = args & 15; + VM_LOG(st) << "execute JMPXARGS " << params; + auto cont = st->get_stack().pop_cont(); + return st->jump(std::move(cont), params); +} + +int exec_ret(VmState* st) { + VM_LOG(st) << "execute RET\n"; + return st->ret(); +} + +int exec_ret_args(VmState* st, unsigned args) { + unsigned params = args & 15; + VM_LOG(st) << "execute RETARGS " << params; + return st->ret(params); +} + +int exec_ret_alt(VmState* st) { + VM_LOG(st) << "execute RETALT\n"; + return st->ret_alt(); +} + +int exec_ret_bool(VmState* st) { + VM_LOG(st) << "execute RETBOOL\n"; + return st->get_stack().pop_bool() ? st->ret() : st->ret_alt(); +} + +int exec_callcc(VmState* st) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute CALLCC\n"; + auto cont = stack.pop_cont(); + auto cc = st->extract_cc(3); + st->get_stack().push_cont(std::move(cc)); + return st->jump(std::move(cont)); +} + +int exec_jmpx_data(VmState* st) { + VM_LOG(st) << "execute JMPXDATA\n"; + auto cont = st->get_stack().pop_cont(); + st->push_code(); + return st->jump(std::move(cont)); +} + +int exec_callcc_args(VmState* st, unsigned args) { + int params = (args >> 4) & 15, retvals = ((args + 1) & 15) - 1; + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute CALLCCARGS " << params << ',' << retvals; + stack.check_underflow(params + 1); + auto cont = stack.pop_cont(); + auto cc = st->extract_cc(3, params, retvals); + st->get_stack().push_cont(std::move(cc)); + return st->jump(std::move(cont)); +} + +int exec_callx_varargs(VmState* st) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute CALLXVARARGS\n"; + stack.check_underflow(3); + int retvals = stack.pop_smallint_range(254, -1); + int params = stack.pop_smallint_range(254, -1); + return st->call(stack.pop_cont(), params, retvals); +} + +int exec_ret_varargs(VmState* st) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute RETVARARGS\n"; + int retvals = stack.pop_smallint_range(254, -1); + return st->ret(retvals); +} + +int exec_jmpx_varargs(VmState* st) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute JMPXVARARGS\n"; + stack.check_underflow(2); + int params = stack.pop_smallint_range(254, -1); + stack.check_underflow(params + 1); + return st->jump(stack.pop_cont(), params); +} + +int exec_callcc_varargs(VmState* st) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute CALLCCVARARGS\n"; + stack.check_underflow(3); + int retvals = stack.pop_smallint_range(254, -1); + int params = stack.pop_smallint_range(254, -1); + stack.check_underflow(params + 1); + auto cont = stack.pop_cont(); + auto cc = st->extract_cc(3, params, retvals); + st->get_stack().push_cont(std::move(cc)); + return st->jump(std::move(cont)); +} + +int exec_do_with_ref(VmState* st, CellSlice& cs, int pfx_bits, const std::function)>& func, + const char* name) { + if (!cs.have_refs(1)) { + throw VmError{Excno::inv_opcode, "no references left for a CALLREF instruction"}; + } + cs.advance(pfx_bits); + auto cell = cs.fetch_ref(); + VM_LOG(st) << "execute " << name << " (" << cell->get_hash().to_hex() << ")"; + return func(st, Ref{true, load_cell_slice_ref(std::move(cell)), st->get_cp()}); +} + +int exec_ret_data(VmState* st) { + VM_LOG(st) << "execute RETDATA\n"; + st->push_code(); + return st->ret(); +} + +void register_continuation_jump_ops(OpcodeTable& cp0) { + using namespace std::placeholders; + cp0.insert(OpcodeInstr::mksimple(0xd8, 8, "EXECUTE", exec_execute)) + .insert(OpcodeInstr::mksimple(0xd9, 8, "JMPX", exec_jmpx)) + .insert(OpcodeInstr::mkfixed(0xda, 8, 8, instr::dump_2c("CALLXARGS ", ","), exec_callx_args)) + .insert(OpcodeInstr::mkfixed(0xdb0, 12, 4, instr::dump_1c("CALLXARGS ", ",-1"), exec_callx_args_p)) + .insert(OpcodeInstr::mkfixed(0xdb1, 12, 4, instr::dump_1c("JMPXARGS "), exec_jmpx_args)) + .insert(OpcodeInstr::mkfixed(0xdb2, 12, 4, instr::dump_1c("RETARGS "), exec_ret_args)) + .insert(OpcodeInstr::mksimple(0xdb30, 16, "RET", exec_ret)) + .insert(OpcodeInstr::mksimple(0xdb31, 16, "RETALT", exec_ret_alt)) + .insert(OpcodeInstr::mksimple(0xdb32, 16, "RETBOOL", exec_ret_bool)) + .insert(OpcodeInstr::mksimple(0xdb34, 16, "CALLCC", exec_callcc)) + .insert(OpcodeInstr::mksimple(0xdb35, 16, "JMPXDATA", exec_jmpx_data)) + .insert(OpcodeInstr::mkfixed(0xdb36, 16, 8, instr::dump_2c("CALLCCARGS ", ","), exec_callcc_args)) + .insert(OpcodeInstr::mksimple(0xdb38, 16, "CALLXVARARGS", exec_callx_varargs)) + .insert(OpcodeInstr::mksimple(0xdb39, 16, "RETVARARGS", exec_ret_varargs)) + .insert(OpcodeInstr::mksimple(0xdb3a, 16, "JMPXVARARGS", exec_jmpx_varargs)) + .insert(OpcodeInstr::mksimple(0xdb3b, 16, "CALLCCVARARGS", exec_callcc_varargs)) + .insert(OpcodeInstr::mkext(0xdb3c, 16, 0, std::bind(dump_push_ref, _1, _2, _3, "CALLREF"), + std::bind(exec_do_with_ref, _1, _2, _4, + [](auto st, auto cont) { return st->call(std::move(cont)); }, "CALLREF"), + compute_len_push_ref)) + .insert(OpcodeInstr::mkext(0xdb3d, 16, 0, std::bind(dump_push_ref, _1, _2, _3, "JMPREF"), + std::bind(exec_do_with_ref, _1, _2, _4, + [](auto st, auto cont) { return st->jump(std::move(cont)); }, "JMPREF"), + compute_len_push_ref)) + .insert(OpcodeInstr::mkext(0xdb3e, 16, 0, std::bind(dump_push_ref, _1, _2, _3, "JMPREFDATA"), + std::bind(exec_do_with_ref, _1, _2, _4, + [](auto st, auto cont) { + st->push_code(); + return st->jump(std::move(cont)); + }, + "JMPREFDATA"), + compute_len_push_ref)) + .insert(OpcodeInstr::mksimple(0xdb3f, 16, "RETDATA", exec_ret_data)); +} + +int exec_if(VmState* st) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute IF\n"; + stack.check_underflow(2); + auto cont = stack.pop_cont(); + if (stack.pop_bool()) { + return st->call(std::move(cont)); + } + return 0; +} + +int exec_ifnot(VmState* st) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute IFNOT\n"; + stack.check_underflow(2); + auto cont = stack.pop_cont(); + if (!stack.pop_bool()) { + return st->call(std::move(cont)); + } + return 0; +} + +int exec_if_jmp(VmState* st) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute IFJMP\n"; + stack.check_underflow(2); + auto cont = stack.pop_cont(); + if (stack.pop_bool()) { + return st->jump(std::move(cont)); + } + return 0; +} + +int exec_ifnot_jmp(VmState* st) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute IFNOTJMP\n"; + stack.check_underflow(2); + auto cont = stack.pop_cont(); + if (!stack.pop_bool()) { + return st->jump(std::move(cont)); + } + return 0; +} + +int exec_ifret(VmState* st) { + VM_LOG(st) << "execute IFRET\n"; + if (st->get_stack().pop_bool()) { + return st->ret(); + } + return 0; +} + +int exec_ifnotret(VmState* st) { + VM_LOG(st) << "execute IFNOTRET\n"; + if (!st->get_stack().pop_bool()) { + return st->ret(); + } + return 0; +} + +int exec_if_else(VmState* st) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute IFELSE\n"; + stack.check_underflow(3); + auto cont0 = stack.pop_cont(); + auto cont1 = stack.pop_cont(); + if (stack.pop_bool()) { + std::swap(cont0, cont1); + } + cont1.clear(); + return st->call(std::move(cont0)); +} + +int exec_condsel(VmState* st) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute CONDSEL\n"; + stack.check_underflow(3); + auto y = stack.pop(); + auto x = stack.pop(); + stack.push(stack.pop_bool() ? std::move(x) : std::move(y)); + return 0; +} + +int exec_condsel_chk(VmState* st) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute CONDSELCHK\n"; + stack.check_underflow(3); + auto y = stack.pop(); + auto x = stack.pop(); + if (x.type() != y.type()) { + throw VmError{Excno::type_chk, "two arguments of CONDSELCHK have different type"}; + } + stack.push(stack.pop_bool() ? std::move(x) : std::move(y)); + return 0; +} + +int exec_ifretalt(VmState* st) { + VM_LOG(st) << "execute IFRETALT\n"; + if (st->get_stack().pop_bool()) { + return st->ret_alt(); + } + return 0; +} + +int exec_ifnotretalt(VmState* st) { + VM_LOG(st) << "execute IFNOTRETALT\n"; + if (!st->get_stack().pop_bool()) { + return st->ret_alt(); + } + return 0; +} + +int exec_if_bit_jmp(VmState* st, unsigned args) { + bool negate = args & 0x20; + unsigned bit = args & 0x1f; + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute IF" << (negate ? "NBITJMP " : "BITJMP ") << bit; + stack.check_underflow(2); + auto cont = stack.pop_cont(); + auto x = stack.pop_int_finite(); + bool val = x->get_bit(bit); + stack.push_int(std::move(x)); + if (val ^ negate) { + return st->jump(std::move(cont)); + } + return 0; +} + +std::string dump_if_bit_jmp(CellSlice& cs, unsigned args) { + std::ostringstream os{args & 0x20 ? "IFN" : " IF"}; + os << "BITJMP " << (args & 0x1f); + return os.str(); +} + +int exec_if_bit_jmpref(VmState* st, CellSlice& cs, unsigned args, int pfx_bits) { + if (!cs.have_refs()) { + throw VmError{Excno::inv_opcode, "no references left for a IFBITJMPREF instruction"}; + } + cs.advance(pfx_bits); + auto cell = cs.fetch_ref(); + bool negate = args & 0x20; + unsigned bit = args & 0x1f; + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute IF" << (negate ? "N" : "") << "BITJMPREF " << bit << " (" << cell->get_hash().to_hex() << ")"; + auto x = stack.pop_int_finite(); + bool val = x->get_bit(bit); + stack.push_int(std::move(x)); + if (val ^ negate) { + return st->jump(Ref{true, load_cell_slice_ref(std::move(cell)), st->get_cp()}); + } + return 0; +} + +std::string dump_if_bit_jmpref(CellSlice& cs, unsigned args, int pfx_bits) { + if (!cs.have_refs()) { + return ""; + } + cs.advance(pfx_bits); + cs.advance_refs(1); + std::ostringstream os{args & 0x20 ? "IFN" : " IF"}; + os << "BITJMPREF " << (args & 0x1f); + return os.str(); +} + +int exec_repeat(VmState* st) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute REPEAT\n"; + stack.check_underflow(2); + auto cont = stack.pop_cont(); + int c = stack.pop_smallint_range(0x7fffffff, 0x80000000); + if (c <= 0) { + return 0; + } + return st->repeat(std::move(cont), st->extract_cc(1), c); +} + +int exec_repeat_end(VmState* st) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute REPEATEND\n"; + stack.check_underflow(1); + int c = stack.pop_smallint_range(0x7fffffff, 0x80000000); + if (c <= 0) { + return st->ret(); + } + auto cont = st->extract_cc(0); + return st->repeat(std::move(cont), st->get_c0(), c); +} + +int exec_until(VmState* st) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute UNTIL\n"; + auto cont = stack.pop_cont(); + return st->until(std::move(cont), st->extract_cc(1)); +} + +int exec_until_end(VmState* st) { + VM_LOG(st) << "execute UNTILEND\n"; + auto cont = st->extract_cc(0); + return st->until(std::move(cont), st->get_c0()); +} + +int exec_while(VmState* st) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute WHILE\n"; + stack.check_underflow(2); + auto body = stack.pop_cont(); + auto cond = stack.pop_cont(); + return st->loop_while(std::move(cond), std::move(body), st->extract_cc(1)); +} + +int exec_while_end(VmState* st) { + VM_LOG(st) << "execute WHILEEND\n"; + auto cond = st->get_stack().pop_cont(); + auto body = st->extract_cc(0); + return st->loop_while(std::move(cond), std::move(body), st->get_c0()); +} + +int exec_again(VmState* st) { + VM_LOG(st) << "execute AGAIN\n"; + return st->again(st->get_stack().pop_cont()); +} + +int exec_again_end(VmState* st) { + VM_LOG(st) << "execute AGAINEND\n"; + return st->again(st->extract_cc(0)); +} + +void register_continuation_cond_loop_ops(OpcodeTable& cp0) { + using namespace std::placeholders; + cp0.insert(OpcodeInstr::mksimple(0xdc, 8, "IFRET", exec_ifret)) + .insert(OpcodeInstr::mksimple(0xdd, 8, "IFNOTRET", exec_ifnotret)) + .insert(OpcodeInstr::mksimple(0xde, 8, "IF", exec_if)) + .insert(OpcodeInstr::mksimple(0xdf, 8, "IFNOT", exec_ifnot)) + .insert(OpcodeInstr::mksimple(0xe0, 8, "IFJMP", exec_if_jmp)) + .insert(OpcodeInstr::mksimple(0xe1, 8, "IFNOTJMP", exec_ifnot_jmp)) + .insert(OpcodeInstr::mksimple(0xe2, 8, "IFELSE", exec_if_else)) + .insert(OpcodeInstr::mkext( + 0xe300, 16, 0, std::bind(dump_push_ref, _1, _2, _3, "IFREF"), + std::bind(exec_do_with_ref, _1, _2, _4, + [](auto st, auto cont) { return st->get_stack().pop_bool() ? st->call(std::move(cont)) : 0; }, + "IFREF"), + compute_len_push_ref)) + .insert(OpcodeInstr::mkext( + 0xe301, 16, 0, std::bind(dump_push_ref, _1, _2, _3, "IFNOTREF"), + std::bind(exec_do_with_ref, _1, _2, _4, + [](auto st, auto cont) { return st->get_stack().pop_bool() ? 0 : st->call(std::move(cont)); }, + "IFNOTREF"), + compute_len_push_ref)) + .insert(OpcodeInstr::mkext( + 0xe302, 16, 0, std::bind(dump_push_ref, _1, _2, _3, "IFJMPREF"), + std::bind(exec_do_with_ref, _1, _2, _4, + [](auto st, auto cont) { return st->get_stack().pop_bool() ? st->jump(std::move(cont)) : 0; }, + "IFJMPREF"), + compute_len_push_ref)) + .insert(OpcodeInstr::mkext( + 0xe303, 16, 0, std::bind(dump_push_ref, _1, _2, _3, "IFNOTJMPREF"), + std::bind(exec_do_with_ref, _1, _2, _4, + [](auto st, auto cont) { return st->get_stack().pop_bool() ? 0 : st->jump(std::move(cont)); }, + "IFNOTJMPREF"), + compute_len_push_ref)) + .insert(OpcodeInstr::mksimple(0xe304, 16, "CONDSEL", exec_condsel)) + .insert(OpcodeInstr::mksimple(0xe305, 16, "CONDSELCHK", exec_condsel_chk)) + .insert(OpcodeInstr::mksimple(0xe308, 16, "IFRETALT", exec_ifretalt)) + .insert(OpcodeInstr::mksimple(0xe309, 16, "IFNOTRETALT", exec_ifnotretalt)) + .insert(OpcodeInstr::mkfixed(0xe380 >> 6, 10, 6, dump_if_bit_jmp, exec_if_bit_jmp)) + .insert(OpcodeInstr::mkext(0xe3c0 >> 6, 10, 6, dump_if_bit_jmpref, exec_if_bit_jmpref, compute_len_push_ref)) + .insert(OpcodeInstr::mksimple(0xe4, 8, "REPEAT", exec_repeat)) + .insert(OpcodeInstr::mksimple(0xe5, 8, "REPEATEND", exec_repeat_end)) + .insert(OpcodeInstr::mksimple(0xe6, 8, "UNTIL", exec_until)) + .insert(OpcodeInstr::mksimple(0xe7, 8, "UNTILEND", exec_until_end)) + .insert(OpcodeInstr::mksimple(0xe8, 8, "WHILE", exec_while)) + .insert(OpcodeInstr::mksimple(0xe9, 8, "WHILEEND", exec_while_end)) + .insert(OpcodeInstr::mksimple(0xea, 8, "AGAIN", exec_again)) + .insert(OpcodeInstr::mksimple(0xeb, 8, "AGAINEND", exec_again_end)); +} + +int exec_setcontargs_common(VmState* st, int copy, int more) { + Stack& stack = st->get_stack(); + stack.check_underflow(copy + 1); + auto cont = stack.pop_cont(); + if (copy || more >= 0) { + ControlData* cdata = force_cdata(cont); + if (copy > 0) { + if (cdata->nargs >= 0 && cdata->nargs < copy) { + throw VmError{Excno::stk_ov, "too many arguments copied into a closure continuation"}; + } + if (cdata->stack.is_null()) { + cdata->stack = stack.split_top(copy); + } else { + cdata->stack.write().move_from_stack(stack, copy); + } + if (cdata->nargs >= 0) { + cdata->nargs -= copy; + } + } + if (more >= 0) { + if (cdata->nargs > more) { + cdata->nargs = 0x40000000; // will throw an exception if run + } else if (cdata->nargs < 0) { + cdata->nargs = more; + } + } + } + st->get_stack().push_cont(std::move(cont)); + return 0; +} + +int exec_setcontargs(VmState* st, unsigned args) { + int copy = (args >> 4) & 15, more = ((args + 1) & 15) - 1; + VM_LOG(st) << "execute SETCONTARGS " << copy << ',' << more; + return exec_setcontargs_common(st, copy, more); +} + +std::string dump_setcontargs(CellSlice& cs, unsigned args, const char* name) { + int copy = (args >> 4) & 15, more = ((args + 1) & 15) - 1; + std::ostringstream os{name}; + os << ' ' << copy << ',' << more; + return os.str(); +} + +int exec_setcont_varargs(VmState* st) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute SETCONTVARARGS\n"; + stack.check_underflow(2); + int more = stack.pop_smallint_range(255, -1); + int copy = stack.pop_smallint_range(255); + return exec_setcontargs_common(st, copy, more); +} + +int exec_setnum_varargs(VmState* st) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute SETNUMVARARGS\n"; + stack.check_underflow(2); + int more = stack.pop_smallint_range(255, -1); + return exec_setcontargs_common(st, 0, more); +} + +int exec_return_args_common(VmState* st, int count) { + Stack& stack = st->get_stack(); + stack.check_underflow(count); + if (stack.depth() == count) { + return 0; + } + int copy = stack.depth() - count; + Ref alt_stk = st->swap_stack(stack.split_top(count)); + auto cont = st->get_c0(); + ControlData* cdata = force_cdata(cont); + if (cdata->nargs >= 0 && cdata->nargs < copy) { + throw VmError{Excno::stk_ov, "too many arguments copied into a closure continuation"}; + } + if (cdata->stack.is_null()) { + cdata->stack = std::move(alt_stk); + } else { + cdata->stack.write().move_from_stack(alt_stk.write(), copy); + alt_stk.clear(); + } + if (cdata->nargs >= 0) { + cdata->nargs -= copy; + } + st->set_c0(std::move(cont)); + return 0; +} + +int exec_return_args(VmState* st, unsigned args) { + int count = args & 15; + VM_LOG(st) << "execute RETURNARGS " << count; + return exec_return_args_common(st, count); +} + +int exec_return_varargs(VmState* st) { + VM_LOG(st) << "execute RETURNVARARGS\n"; + return exec_return_args_common(st, st->get_stack().pop_smallint_range(255)); +} + +int exec_bless(VmState* st) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute BLESS\n"; + stack.push_cont(Ref{true, stack.pop_cellslice(), st->get_cp()}); + return 0; +} + +int exec_bless_args_common(VmState* st, int copy, int more) { + Stack& stack = st->get_stack(); + stack.check_underflow(copy + 1); + auto cs = stack.pop_cellslice(); + auto new_stk = stack.split_top(copy); + stack.push_cont(Ref{true, std::move(cs), st->get_cp(), std::move(new_stk), more}); + return 0; +} + +int exec_bless_varargs(VmState* st) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute BLESSVARARGS\n"; + stack.check_underflow(2); + int more = stack.pop_smallint_range(255, -1); + int copy = stack.pop_smallint_range(255); + return exec_bless_args_common(st, copy, more); +} + +int exec_bless_args(VmState* st, unsigned args) { + int copy = (args >> 4) & 15, more = ((args + 1) & 15) - 1; + VM_LOG(st) << "execute BLESSARGS " << copy << ',' << more; + return exec_bless_args_common(st, copy, more); +} + +int exec_push_ctr(VmState* st, unsigned args) { + unsigned idx = args & 15; + VM_LOG(st) << "execute PUSH c" << idx; + st->get_stack().push(st->get(idx)); + return 0; +} + +namespace { +inline void throw_typechk(bool ok) { + if (!ok) { + throw VmError{Excno::type_chk, "invalid value type for control register"}; + } +} + +inline void throw_rangechk(bool ok) { + if (!ok) { + throw VmError{Excno::range_chk, "control register index out of range"}; + } +} +} // namespace + +int exec_pop_ctr(VmState* st, unsigned args) { + unsigned idx = args & 15; + VM_LOG(st) << "execute POP c" << idx; + throw_typechk(st->set(idx, st->get_stack().pop_chk())); + return 0; +} + +int exec_setcont_ctr(VmState* st, unsigned args) { + unsigned idx = args & 15; + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute SETCONTCTR c" << idx; + stack.check_underflow(2); + auto cont = stack.pop_cont(); + throw_typechk(force_cregs(cont)->define(idx, stack.pop_chk())); + st->get_stack().push_cont(std::move(cont)); + return 0; +} + +int exec_setret_ctr(VmState* st, unsigned args) { + unsigned idx = args & 15; + VM_LOG(st) << "execute SETRETCTR c" << idx; + auto cont = st->get_c0(); + throw_typechk(force_cregs(cont)->define(idx, st->get_stack().pop_chk())); + st->set_c0(std::move(cont)); + return 0; +} + +int exec_setalt_ctr(VmState* st, unsigned args) { + unsigned idx = args & 15; + VM_LOG(st) << "execute SETALTCTR c" << idx; + auto cont = st->get_c1(); + throw_typechk(force_cregs(cont)->define(idx, st->get_stack().pop_chk())); + st->set_c1(std::move(cont)); + return 0; +} + +int exec_popsave_ctr(VmState* st, unsigned args) { + unsigned idx = args & 15; + VM_LOG(st) << "execute POPSAVE c" << idx; + auto val = st->get_stack().pop_chk(); + auto c0 = st->get_c0(); + throw_typechk(idx || val.is(StackEntry::t_vmcont)); + force_cregs(c0)->define(idx, st->get(idx)); + if (!idx) { + st->set_c0(std::move(c0)); + throw_typechk(st->set(idx, std::move(val))); + } else { + throw_typechk(st->set(idx, std::move(val))); + st->set_c0(std::move(c0)); + } + return 0; +} + +int exec_save_ctr(VmState* st, unsigned args) { + unsigned idx = args & 15; + VM_LOG(st) << "execute SAVECTR c" << idx; + auto c0 = st->get_c0(); + throw_typechk(force_cregs(c0)->define(idx, st->get(idx))); + st->set_c0(std::move(c0)); + return 0; +} + +int exec_savealt_ctr(VmState* st, unsigned args) { + unsigned idx = args & 15; + VM_LOG(st) << "execute SAVEALTCTR c" << idx; + auto c1 = st->get_c1(); + throw_typechk(force_cregs(c1)->define(idx, st->get(idx))); + st->set_c1(std::move(c1)); + return 0; +} + +int exec_saveboth_ctr(VmState* st, unsigned args) { + unsigned idx = args & 15; + VM_LOG(st) << "execute SAVEBOTHCTR c" << idx; + auto c0 = st->get_c0(), c1 = st->get_c1(); + auto val = st->get(idx); + force_cregs(c0)->define(idx, val); + force_cregs(c1)->define(idx, std::move(val)); + st->set_c0(std::move(c0)); + st->set_c1(std::move(c1)); + return 0; +} + +int exec_push_ctr_var(VmState* st) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute PUSHCTRX\n"; + unsigned idx = stack.pop_smallint_range(16); + auto val = st->get(idx); + throw_rangechk(!val.empty()); + stack.push(std::move(val)); + return 0; +} + +int exec_pop_ctr_var(VmState* st) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute POPCTRX\n"; + stack.check_underflow(2); + unsigned idx = stack.pop_smallint_range(16); + throw_rangechk(ControlRegs::valid_idx(idx)); + throw_typechk(st->set(idx, stack.pop_chk())); + return 0; +} + +int exec_setcont_ctr_var(VmState* st) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute SETCONTCTRX\n"; + stack.check_underflow(3); + unsigned idx = stack.pop_smallint_range(16); + throw_rangechk(ControlRegs::valid_idx(idx)); + auto cont = stack.pop_cont(); + throw_typechk(force_cregs(cont)->define(idx, stack.pop_chk())); + st->get_stack().push_cont(std::move(cont)); + return 0; +} + +int exec_compos(VmState* st, unsigned mask, const char* name) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute " << name; + stack.check_underflow(2); + auto val = stack.pop_cont(); + auto cont = stack.pop_cont(); + ControlRegs* regs = force_cregs(cont); + switch (mask) { + case 1: + regs->define_c0(std::move(val)); + break; + case 3: + regs->define_c0(val); + // fallthrough + case 2: + regs->define_c1(std::move(val)); + break; + } + st->get_stack().push_cont(std::move(cont)); + return 0; +} + +int exec_atexit(VmState* st) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute ATEXIT\n"; + auto cont = stack.pop_cont(); + force_cregs(cont)->define_c0(st->get_c0()); + st->set_c0(std::move(cont)); + return 0; +} + +int exec_atexit_alt(VmState* st) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute ATEXITALT\n"; + auto cont = stack.pop_cont(); + force_cregs(cont)->define_c1(st->get_c1()); + st->set_c1(std::move(cont)); + return 0; +} + +int exec_setexit_alt(VmState* st) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute SETEXITALT\n"; + auto cont = stack.pop_cont(); + force_cregs(cont)->define_c0(st->get_c0()); + force_cregs(cont)->define_c1(st->get_c1()); + st->set_c1(std::move(cont)); + return 0; +} + +int exec_thenret(VmState* st) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute THENRET\n"; + auto cont = stack.pop_cont(); + force_cregs(cont)->define_c0(st->get_c0()); + stack.push_cont(std::move(cont)); + return 0; +} + +int exec_thenret_alt(VmState* st) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute THENRETALT\n"; + auto cont = stack.pop_cont(); + force_cregs(cont)->define_c0(st->get_c1()); + stack.push_cont(std::move(cont)); + return 0; +} + +int exec_invert(VmState* st) { + VM_LOG(st) << "execute INVERT\n"; + auto c0 = st->get_c0(), c1 = st->get_c1(); + st->set_c0(std::move(c1)); + st->set_c1(std::move(c0)); + return 0; +} + +int exec_booleval(VmState* st) { + VM_LOG(st) << "execute BOOLEVAL\n"; + auto cont = st->get_stack().pop_cont(); + auto cc = st->extract_cc(3); + st->set_c0(Ref{true, -1, cc}); + st->set_c1(Ref{true, 0, std::move(cc)}); + return st->jump(std::move(cont)); +} + +void reg_ctr_oprange(OpcodeTable& cp, unsigned opcode, std::string name, exec_arg_instr_func_t exec_ctr) { + cp.insert(OpcodeInstr::mkfixedrange(opcode, opcode + 4, 16, 4, instr::dump_1c(name + " c"), exec_ctr)) + .insert(OpcodeInstr::mkfixedrange(opcode + 4, opcode + 6, 16, 4, instr::dump_1c(name + " c"), exec_ctr)) + .insert(OpcodeInstr::mkfixedrange(opcode + 7, opcode + 8, 16, 4, instr::dump_1c(name + " c"), exec_ctr)); +} + +void register_continuation_change_ops(OpcodeTable& cp0) { + using namespace std::placeholders; + cp0.insert(OpcodeInstr::mkfixed(0xec, 8, 8, std::bind(dump_setcontargs, _1, _2, "SETCONTARGS"), exec_setcontargs)) + .insert(OpcodeInstr::mkfixed(0xed0, 12, 4, instr::dump_1c("RETURNARGS "), exec_return_args)) + .insert(OpcodeInstr::mksimple(0xed10, 16, "RETURNVARARGS", exec_return_varargs)) + .insert(OpcodeInstr::mksimple(0xed11, 16, "SETCONTVARARGS", exec_setcont_varargs)) + .insert(OpcodeInstr::mksimple(0xed12, 16, "SETNUMVARARGS", exec_setnum_varargs)) + .insert(OpcodeInstr::mksimple(0xed1e, 16, "BLESS", exec_bless)) + .insert(OpcodeInstr::mksimple(0xed1f, 16, "BLESSVARARGS", exec_bless_varargs)); + + reg_ctr_oprange(cp0, 0xed40, "PUSH", exec_push_ctr); + reg_ctr_oprange(cp0, 0xed50, "POP", exec_pop_ctr); + reg_ctr_oprange(cp0, 0xed60, "SETCONTCTR", exec_setcont_ctr); + reg_ctr_oprange(cp0, 0xed70, "SETRETCTR", exec_setret_ctr); + reg_ctr_oprange(cp0, 0xed80, "SETALTCTR", exec_setalt_ctr); + reg_ctr_oprange(cp0, 0xed90, "POPSAVE", exec_popsave_ctr); + reg_ctr_oprange(cp0, 0xeda0, "SAVECTR", exec_save_ctr); + reg_ctr_oprange(cp0, 0xedb0, "SAVEALTCTR", exec_savealt_ctr); + reg_ctr_oprange(cp0, 0xedc0, "SAVEBOTHCTR", exec_saveboth_ctr); + + cp0.insert(OpcodeInstr::mksimple(0xede0, 16, "PUSHCTRX", exec_push_ctr_var)) + .insert(OpcodeInstr::mksimple(0xede1, 16, "POPCTRX", exec_pop_ctr_var)) + .insert(OpcodeInstr::mksimple(0xede2, 16, "SETCONTCTRX", exec_setcont_ctr_var)) + .insert(OpcodeInstr::mksimple(0xedf0, 16, "BOOLAND", std::bind(exec_compos, _1, 1, "BOOLAND"))) + .insert(OpcodeInstr::mksimple(0xedf1, 16, "BOOLOR", std::bind(exec_compos, _1, 2, "BOOLOR"))) + .insert(OpcodeInstr::mksimple(0xedf2, 16, "COMPOSBOTH", std::bind(exec_compos, _1, 3, "COMPOSBOTH"))) + .insert(OpcodeInstr::mksimple(0xedf3, 16, "ATEXIT", exec_atexit)) + .insert(OpcodeInstr::mksimple(0xedf4, 16, "ATEXITALT", exec_atexit_alt)) + .insert(OpcodeInstr::mksimple(0xedf5, 16, "SETEXITALT", exec_setexit_alt)) + .insert(OpcodeInstr::mksimple(0xedf6, 16, "THENRET", exec_thenret)) + .insert(OpcodeInstr::mksimple(0xedf7, 16, "THENRETALT", exec_thenret_alt)) + .insert(OpcodeInstr::mksimple(0xedf8, 16, "INVERT", exec_invert)) + .insert(OpcodeInstr::mksimple(0xedf9, 16, "BOOLEVAL", exec_booleval)) + .insert(OpcodeInstr::mkfixed(0xee, 8, 8, std::bind(dump_setcontargs, _1, _2, "BLESSARGS"), exec_bless_args)); +} + +int exec_calldict_short(VmState* st, unsigned args) { + args &= 0xff; + VM_LOG(st) << "execute CALLDICT " << args; + st->get_stack().push_smallint(args); + return st->call(st->get_c3()); +} + +int exec_calldict(VmState* st, unsigned args) { + args &= 0x3fff; + VM_LOG(st) << "execute CALLDICT " << args; + st->get_stack().push_smallint(args); + return st->call(st->get_c3()); +} + +int exec_jmpdict(VmState* st, unsigned args) { + args &= 0x3fff; + VM_LOG(st) << "execute JMPDICT " << args; + st->get_stack().push_smallint(args); + return st->jump(st->get_c3()); +} + +int exec_preparedict(VmState* st, unsigned args) { + args &= 0x3fff; + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute PREPAREDICT " << args; + stack.push_smallint(args); + stack.push_cont(st->get_c3()); + return 0; +} + +void register_continuation_dict_jump_ops(OpcodeTable& cp0) { + using namespace std::placeholders; + cp0.insert(OpcodeInstr::mkfixed(0xf0, 8, 8, instr::dump_1c_and(255, "CALLDICT "), exec_calldict_short)) + .insert(OpcodeInstr::mkfixed(0xf100 >> 6, 10, 14, instr::dump_1c_and(0x3fff, "CALLDICT "), exec_calldict)) + .insert(OpcodeInstr::mkfixed(0xf140 >> 6, 10, 14, instr::dump_1c_and(0x3fff, "JMPDICT "), exec_jmpdict)) + .insert(OpcodeInstr::mkfixed(0xf180 >> 6, 10, 14, instr::dump_1c_and(0x3fff, "PREPAREDICT "), exec_preparedict)); +} + +int exec_throw_fixed(VmState* st, unsigned args, unsigned mask, int mode) { + unsigned excno = args & mask; + VM_LOG(st) << "execute THROW" << (mode ? "IF" : "") << (mode == 2 ? "NOT " : " ") << excno; + if (mode && st->get_stack().pop_bool() != (bool)(mode & 1)) { + return 0; + } else { + return st->throw_exception(excno); + } +} + +int exec_throw_arg_fixed(VmState* st, unsigned args, unsigned mask, int mode) { + unsigned excno = args & mask; + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute THROWARG" << (mode ? "IF" : "") << (mode == 2 ? "NOT " : " ") << excno; + stack.check_underflow(mode ? 2 : 1); + if (mode && stack.pop_bool() != (bool)(mode & 1)) { + stack.pop(); + return 0; + } else { + return st->throw_exception(excno, stack.pop()); + } +} + +int exec_throw_any(VmState* st, unsigned args) { + bool has_param = args & 1; + bool has_cond = args & 6; + bool throw_cond = args & 2; + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute THROW" << (has_param ? "ARG" : "") << "ANY" << (has_cond ? (throw_cond ? "IF" : "IFNOT") : ""); + stack.check_underflow(1 + (int)has_cond + (int)has_param); + bool flag = has_cond ? stack.pop_bool() : throw_cond; + int excno = stack.pop_smallint_range(0xffff); + if (flag != throw_cond) { + if (has_param) { + stack.pop(); + } + return 0; + } else if (has_param) { + return st->throw_exception(excno, stack.pop()); + } else { + return st->throw_exception(excno); + } +} + +std::string dump_throw_any(CellSlice& cs, unsigned args) { + bool has_param = args & 1; + bool has_cond = args & 6; + bool throw_cond = args & 2; + std::ostringstream os{has_param ? "THROWARG" : "THROW"}; + os << "ANY"; + if (has_cond) { + os << (throw_cond ? "IF" : "IFNOT"); + } + return os.str(); +} + +int exec_try(VmState* st, int args) { + int params = (args >> 4) & 15, retvals = args & 15; + Stack& stack = st->get_stack(); + if (args < 0) { + VM_LOG(st) << "execute TRY"; + } else { + VM_LOG(st) << "execute TRYARGS " << params << "," << retvals; + } + stack.check_underflow(args >= 0 ? params + 2 : 2); + auto handler_cont = stack.pop_cont(); + auto cont = stack.pop_cont(); + auto old_c2 = st->get_c2(); + auto cc = (args >= 0 ? st->extract_cc(7, params, retvals) : st->extract_cc(7)); + ControlRegs* handler_cr = force_cregs(handler_cont); + handler_cr->define_c2(std::move(old_c2)); + handler_cr->define_c0(cc); + st->set_c0(std::move(cc)); + st->set_c2(std::move(handler_cont)); + return st->jump(std::move(cont)); +} + +void register_exception_ops(OpcodeTable& cp0) { + using namespace std::placeholders; + cp0.insert(OpcodeInstr::mkfixed(0xf200 >> 6, 10, 6, instr::dump_1c_and(0x3f, "THROW "), + std::bind(exec_throw_fixed, _1, _2, 63, 0))) + .insert(OpcodeInstr::mkfixed(0xf240 >> 6, 10, 6, instr::dump_1c_and(0x3f, "THROWIF "), + std::bind(exec_throw_fixed, _1, _2, 63, 3))) + .insert(OpcodeInstr::mkfixed(0xf280 >> 6, 10, 6, instr::dump_1c_and(0x3f, "THROWIFNOT "), + std::bind(exec_throw_fixed, _1, _2, 63, 2))) + .insert(OpcodeInstr::mkfixed(0xf2c0 >> 3, 13, 11, instr::dump_1c_and(0x7ff, "THROW "), + std::bind(exec_throw_fixed, _1, _2, 0x7ff, 0))) + .insert(OpcodeInstr::mkfixed(0xf2c8 >> 3, 13, 11, instr::dump_1c_and(0x7ff, "THROWARG "), + std::bind(exec_throw_arg_fixed, _1, _2, 0x7ff, 0))) + .insert(OpcodeInstr::mkfixed(0xf2d0 >> 3, 13, 11, instr::dump_1c_and(0x7ff, "THROWIF "), + std::bind(exec_throw_fixed, _1, _2, 0x7ff, 3))) + .insert(OpcodeInstr::mkfixed(0xf2d8 >> 3, 13, 11, instr::dump_1c_and(0x7ff, "THROWARGIF "), + std::bind(exec_throw_arg_fixed, _1, _2, 0x7ff, 3))) + .insert(OpcodeInstr::mkfixed(0xf2e0 >> 3, 13, 11, instr::dump_1c_and(0x7ff, "THROWIFNOT "), + std::bind(exec_throw_fixed, _1, _2, 0x7ff, 2))) + .insert(OpcodeInstr::mkfixed(0xf2e8 >> 3, 13, 11, instr::dump_1c_and(0x7ff, "THROWARGIFNOT "), + std::bind(exec_throw_arg_fixed, _1, _2, 0x7ff, 2))) + .insert(OpcodeInstr::mkfixedrange(0xf2f0, 0xf2f6, 16, 3, dump_throw_any, exec_throw_any)) + .insert(OpcodeInstr::mksimple(0xf2ff, 16, "TRY", std::bind(exec_try, _1, -1))) + .insert(OpcodeInstr::mkfixed(0xf3, 8, 8, instr::dump_2c("TRYARGS ", ","), exec_try)); +} + +int exec_set_cp_generic(VmState* st, int new_codepage) { + st->force_cp(new_codepage); + return 0; +} + +int exec_set_cp(VmState* st, int args) { + int cp = ((args + 0x10) & 0xff) - 0x10; + VM_LOG(st) << "execute SETCP " << cp; + return exec_set_cp_generic(st, cp); +} + +int exec_set_cp_any(VmState* st) { + VM_LOG(st) << "execute SETCPX"; + int cp = st->get_stack().pop_smallint_range(0x7fff, -0x8000); + return exec_set_cp_generic(st, cp); +} + +void register_codepage_ops(OpcodeTable& cp0) { + using namespace std::placeholders; + cp0.insert(OpcodeInstr::mkfixedrange(0xff00, 0xfff0, 16, 8, instr::dump_1c_and(0xff, "SETCP "), exec_set_cp)) + .insert(OpcodeInstr::mkfixedrange(0xfff1, 0x10000, 16, 8, instr::dump_1c_l_add(-256, "SETCP "), exec_set_cp)) + .insert(OpcodeInstr::mksimple(0xfff0, 16, "SETCPX", exec_set_cp_any)); +} + +void register_continuation_ops(OpcodeTable& cp0) { + register_continuation_jump_ops(cp0); + register_continuation_cond_loop_ops(cp0); + register_continuation_change_ops(cp0); + register_continuation_dict_jump_ops(cp0); + register_exception_ops(cp0); +} + +} // namespace vm diff --git a/crypto/vm/contops.h b/crypto/vm/contops.h new file mode 100644 index 00000000..5919b07e --- /dev/null +++ b/crypto/vm/contops.h @@ -0,0 +1,28 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +namespace vm { + +class OpcodeTable; + +void register_continuation_ops(OpcodeTable& cp0); +void register_codepage_ops(OpcodeTable& cp0); + +} // namespace vm diff --git a/crypto/vm/cp0.cpp b/crypto/vm/cp0.cpp new file mode 100644 index 00000000..3e062140 --- /dev/null +++ b/crypto/vm/cp0.cpp @@ -0,0 +1,50 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "cp0.h" +#include "opctable.h" +#include "stackops.h" +#include "tupleops.h" +#include "arithops.h" +#include "cellops.h" +#include "contops.h" +#include "dictops.h" +#include "debugops.h" +#include "tonops.h" + +namespace vm { + +const OpcodeTable* init_op_cp0() { + static const OpcodeTable* static_op_cp0 = [] { + auto op_cp0 = new OpcodeTable("TEST CODEPAGE", Codepage::test_cp); + register_stack_ops(*op_cp0); // stackops.cpp + register_tuple_ops(*op_cp0); // tupleops.cpp + register_arith_ops(*op_cp0); // arithops.cpp + register_cell_ops(*op_cp0); // cellops.cpp + register_continuation_ops(*op_cp0); // contops.cpp + register_dictionary_ops(*op_cp0); // dictops.cpp + register_ton_ops(*op_cp0); // tonops.cpp + register_debug_ops(*op_cp0); // debugops.cpp + register_codepage_ops(*op_cp0); // contops.cpp + op_cp0->finalize()->register_table(Codepage::test_cp); + return op_cp0; + }(); + return static_op_cp0; +} + +} // namespace vm diff --git a/crypto/vm/cp0.h b/crypto/vm/cp0.h new file mode 100644 index 00000000..34fc9711 --- /dev/null +++ b/crypto/vm/cp0.h @@ -0,0 +1,28 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once +#include "vm/dispatch.h" + +namespace vm { + +class OpcodeTable; + +const OpcodeTable* init_op_cp0(); + +} // namespace vm diff --git a/crypto/vm/db/BlobView.cpp b/crypto/vm/db/BlobView.cpp new file mode 100644 index 00000000..2f0ecb0f --- /dev/null +++ b/crypto/vm/db/BlobView.cpp @@ -0,0 +1,181 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "vm/db/BlobView.h" + +#include "td/utils/port/FileFd.h" +#include "td/utils/HashMap.h" + +#include "td/utils/format.h" +#include "td/utils/port/RwMutex.h" +#include "td/utils/port/MemoryMapping.h" + +#include +#include + +namespace vm { +td::Result BlobView::view(td::MutableSlice slice, td::uint64 offset) { + if (offset > size() || slice.size() > size() - offset) { + return td::Status::Error(PSLICE() << "BlobView: invalid range requested " << td::tag("slice offset", offset) + << td::tag("slice size", slice.size()) << td::tag("blob size", size())); + } + return view_impl(slice, offset); +} +namespace { +class BufferSliceBlobViewImpl : public BlobView { + public: + BufferSliceBlobViewImpl(td::BufferSlice slice) : slice_(std::move(slice)) { + } + td::Result view_impl(td::MutableSlice slice, td::uint64 offset) override { + // optimize anyway + if (offset > std::numeric_limits::max()) { + return td::Slice(); + } + return slice_.as_slice().substr(static_cast(offset), slice.size()); + } + td::uint64 size() override { + return slice_.size(); + } + + private: + td::BufferSlice slice_; +}; +} // namespace + +std::unique_ptr BufferSliceBlobView::create(td::BufferSlice slice) { + return std::make_unique(std::move(slice)); +} + +class FileBlobViewImpl : public BlobView { + public: + FileBlobViewImpl(td::FileFd fd, td::uint64 file_size) : fd_(std::move(fd)), file_size_(file_size) { + } + + td::uint64 size() override { + return file_size_; + } + td::Result view_impl(td::MutableSlice slice, td::uint64 offset) override { + CHECK(offset < size()); + CHECK(size() - offset >= slice.size()); + slice.truncate(file_size_ - offset); + auto first_page = offset / page_size; + auto last_page = (offset + slice.size() - 1) / page_size; + td::uint64 res_offset = 0; + for (auto page_i = first_page; page_i <= last_page; page_i++) { + auto page_offset = page_i * page_size; + auto from = td::max(page_offset, offset); + auto till = td::min(page_offset + page_size, offset + slice.size()); + CHECK(from < till); + TRY_RESULT(page, load_page(page_i)); + auto len = till - from; + slice.substr(res_offset, len).copy_from(page.substr(from - page_offset, len)); + res_offset += len; + } + CHECK(slice.size() == res_offset); + total_view_size_ += slice.size(); + return slice; + } + ~FileBlobViewImpl() { + //LOG(ERROR) << "LOADED " << pages_.size() << " " << total_view_size_; + } + + private: + td::FileFd fd_; + td::uint64 file_size_; + const td::uint64 page_size = 4096; + td::uint64 total_view_size_{0}; + + td::RwMutex pages_rw_mutex_; + td::HashMap pages_; + + std::mutex fd_mutex_; + + td::Result load_page(td::uint64 page_i) { + { + auto pages_guard = pages_rw_mutex_.lock_read(); + auto it = pages_.find(page_i); + if (it != pages_.end()) { + return it->second.as_slice(); + } + } + + std::lock_guard fd_guard(fd_mutex_); + { + auto pages_guard = pages_rw_mutex_.lock_read(); + auto it = pages_.find(page_i); + if (it != pages_.end()) { + return it->second.as_slice(); + } + } + auto offset = page_i * page_size; + + auto size = td::min(file_size_ - offset, page_size); + auto buffer_slice = td::BufferSlice(size); + TRY_RESULT(read_size, fd_.pread(buffer_slice.as_slice(), offset)); + if (read_size != buffer_slice.size()) { + return td::Status::Error("not enough data in file"); + } + + auto pages_guard = pages_rw_mutex_.lock_write(); + auto &res = pages_[page_i]; + res = std::move(buffer_slice); + return res.as_slice(); + } +}; +td::Result> FileBlobView::create(td::CSlice file_path, td::uint64 file_size) { + TRY_RESULT(fd, td::FileFd::open(file_path, td::FileFd::Flags::Read)); + TRY_RESULT(stat, fd.stat()); + if (file_size == 0) { + file_size = stat.size_; + } else if (file_size != (td::uint64)stat.size_) { + return td::Status::Error("Wrong file size"); + } + return std::make_unique(std::move(fd), file_size); +} + +class FileMemoryMappingBlobViewImpl : public BlobView { + public: + FileMemoryMappingBlobViewImpl(td::MemoryMapping mapping) : mapping_(std::move(mapping)) { + } + td::Result view_impl(td::MutableSlice slice, td::uint64 offset) override { + // optimize anyway + return mapping_.as_slice().substr(offset, slice.size()); + } + td::uint64 size() override { + return mapping_.as_slice().size(); + } + + private: + td::MemoryMapping mapping_; +}; + +td::Result> FileMemoryMappingBlobView::create(td::CSlice file_path, td::uint64 file_size) { + TRY_RESULT(fd, td::FileFd::open(file_path, td::FileFd::Flags::Read)); + TRY_RESULT(stat, fd.stat()); + if (file_size == 0) { + file_size = stat.size_; + } else if (file_size != (td::uint64)stat.size_) { + return td::Status::Error("Wrong file size"); + } + + TRY_RESULT(mapping, td::MemoryMapping::create_from_file(fd)); + + return std::make_unique(std::move(mapping)); +} + +} // namespace vm diff --git a/crypto/vm/db/BlobView.h b/crypto/vm/db/BlobView.h new file mode 100644 index 00000000..8ba6f215 --- /dev/null +++ b/crypto/vm/db/BlobView.h @@ -0,0 +1,45 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once +#include "td/utils/buffer.h" + +namespace vm { +class BlobView { + public: + virtual ~BlobView() = default; + td::Result view(td::MutableSlice slice, td::uint64 offset); + virtual td::uint64 size() = 0; + + private: + virtual td::Result view_impl(td::MutableSlice slice, td::uint64 offset) = 0; +}; + +class BufferSliceBlobView { + public: + static std::unique_ptr create(td::BufferSlice slice); +}; +class FileBlobView { + public: + static td::Result> create(td::CSlice file_path, td::uint64 file_size = 0); +}; +class FileMemoryMappingBlobView { + public: + static td::Result> create(td::CSlice file_path, td::uint64 file_size = 0); +}; +} // namespace vm diff --git a/crypto/vm/db/CellHashTable.h b/crypto/vm/db/CellHashTable.h new file mode 100644 index 00000000..057c9070 --- /dev/null +++ b/crypto/vm/db/CellHashTable.h @@ -0,0 +1,71 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +#include "td/utils/Slice.h" + +#include + +namespace vm { +template +class CellHashTable { + public: + template + InfoT &apply(td::Slice hash, F &&f) { + auto it = set_.find(hash); + if (it != set_.end()) { + auto &res = const_cast(*it); + f(res); + return res; + } + InfoT info; + f(info); + auto &res = const_cast(*(set_.insert(std::move(info)).first)); + return res; + } + + template + void for_each(F &&f) { + for (auto &info : set_) { + f(info); + } + } + template + void filter(F &&f) { + for (auto it = set_.begin(); it != set_.end();) { + if (f(*it)) { + it++; + } else { + it = set_.erase(it); + } + } + } + void erase(td::Slice hash) { + auto it = set_.find(hash); + CHECK(it != set_.end()); + set_.erase(it); + } + size_t size() const { + return set_.size(); + } + + private: + std::set> set_; +}; +} // namespace vm diff --git a/crypto/vm/db/CellStorage.cpp b/crypto/vm/db/CellStorage.cpp new file mode 100644 index 00000000..317fead3 --- /dev/null +++ b/crypto/vm/db/CellStorage.cpp @@ -0,0 +1,163 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "vm/db/CellStorage.h" +#include "vm/db/DynamicBagOfCellsDb.h" +#include "vm/boc.h" +#include "td/utils/base64.h" +#include "td/utils/tl_parsers.h" +#include "td/utils/tl_helpers.h" + +namespace vm { +namespace { +class RefcntCellStorer { + public: + RefcntCellStorer(td::int32 refcnt, const DataCell &cell) : refcnt_(refcnt), cell_(cell) { + } + + template + void store(StorerT &storer) const { + using td::store; + store(refcnt_, storer); + store(cell_, storer); + for (unsigned i = 0; i < cell_.size_refs(); i++) { + auto cell = cell_.get_ref(i); + auto level_mask = cell->get_level_mask(); + auto level = level_mask.get_level(); + td::uint8 x = static_cast(level_mask.get_mask()); + storer.store_slice(td::Slice(&x, 1)); + for (unsigned level_i = 0; level_i <= level; level_i++) { + if (!level_mask.is_significant(level_i)) { + continue; + } + storer.store_slice(cell->get_hash(level_i).as_slice()); + } + for (unsigned level_i = 0; level_i <= level; level_i++) { + if (!level_mask.is_significant(level_i)) { + continue; + } + td::uint8 depth_buf[Cell::depth_bytes]; + DataCell::store_depth(depth_buf, cell->get_depth(level_i)); + storer.store_slice(td::Slice(depth_buf, Cell::depth_bytes)); + } + } + } + + private: + td::int32 refcnt_; + const DataCell &cell_; +}; + +class RefcntCellParser { + public: + RefcntCellParser(bool need_data) : need_data_(need_data) { + } + td::int32 refcnt; + Ref cell; + + template + void parse(ParserT &parser, ExtCellCreator &ext_cell_creator) { + using ::td::parse; + parse(refcnt, parser); + if (!need_data_) { + return; + } + auto status = [&]() -> td::Status { + TRY_STATUS(parser.get_status()); + auto size = parser.get_left_len(); + td::Slice data = parser.template fetch_string_raw(size); + CellSerializationInfo info; + auto cell_data = data; + TRY_STATUS(info.init(cell_data, 0 /*ref_byte_size*/)); + data = data.substr(info.end_offset); + + Ref refs[Cell::max_refs]; + for (int i = 0; i < info.refs_cnt; i++) { + if (data.size() < 1) { + return td::Status::Error("Not enought data"); + } + Cell::LevelMask level_mask(data[0]); + auto n = level_mask.get_hashes_count(); + auto end_offset = 1 + n * (Cell::hash_bytes + Cell::depth_bytes); + if (data.size() < end_offset) { + return td::Status::Error("Not enought data"); + } + + TRY_RESULT(ext_cell, ext_cell_creator.ext_cell(level_mask, data.substr(1, n * Cell::hash_bytes), + data.substr(1 + n * Cell::hash_bytes, n * Cell::depth_bytes))); + refs[i] = std::move(ext_cell); + CHECK(refs[i]->get_level() == level_mask.get_level()); + data = data.substr(end_offset); + } + if (!data.empty()) { + return td::Status::Error("Too much data"); + } + TRY_RESULT(data_cell, info.create_data_cell(cell_data, td::Span>(refs, info.refs_cnt))); + cell = std::move(data_cell); + return td::Status::OK(); + }(); + if (status.is_error()) { + parser.set_error(status.message().str()); + return; + } + } + + private: + bool need_data_; +}; +} // namespace + +CellLoader::CellLoader(std::shared_ptr reader) : reader_(std::move(reader)) { + CHECK(reader_); +} + +td::Result CellLoader::load(td::Slice hash, bool need_data, ExtCellCreator &ext_cell_creator) { + //LOG(ERROR) << "Storage: load cell " << hash.size() << " " << td::base64_encode(hash); + LoadResult res; + std::string serialized; + TRY_RESULT(get_status, reader_->get(hash, serialized)); + if (get_status != KeyValue::GetStatus::Ok) { + DCHECK(get_status == KeyValue::GetStatus::NotFound); + return res; + } + + res.status = LoadResult::Ok; + + RefcntCellParser refcnt_cell(need_data); + td::TlParser parser(serialized); + refcnt_cell.parse(parser, ext_cell_creator); + TRY_STATUS(parser.get_status()); + + res.refcnt_ = refcnt_cell.refcnt; + res.cell_ = std::move(refcnt_cell.cell); + //CHECK(res.cell_->get_hash() == hash); + + return res; +} + +CellStorer::CellStorer(KeyValue &kv) : kv_(kv) { +} + +td::Status CellStorer::erase(td::Slice hash) { + return kv_.erase(hash); +} + +td::Status CellStorer::set(td::int32 refcnt, const DataCell &cell) { + return kv_.set(cell.get_hash().as_slice(), td::serialize(RefcntCellStorer(refcnt, cell))); +} +} // namespace vm diff --git a/crypto/vm/db/CellStorage.h b/crypto/vm/db/CellStorage.h new file mode 100644 index 00000000..26e02330 --- /dev/null +++ b/crypto/vm/db/CellStorage.h @@ -0,0 +1,65 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once +#include "td/db/KeyValue.h" +#include "vm/db/DynamicBagOfCellsDb.h" +#include "vm/cells.h" + +#include "td/utils/Slice.h" +#include "td/utils/Status.h" + +namespace vm { +using KeyValue = td::KeyValue; +using KeyValueReader = td::KeyValueReader; + +class CellLoader { + public: + struct LoadResult { + public: + enum { Ok, NotFound } status{NotFound}; + + Ref &cell() { + DCHECK(status == Ok); + return cell_; + } + + td::int32 refcnt() const { + return refcnt_; + } + + Ref cell_; + td::int32 refcnt_{0}; + }; + CellLoader(std::shared_ptr reader); + td::Result load(td::Slice hash, bool need_data, ExtCellCreator &ext_cell_creator); + + private: + std::shared_ptr reader_; +}; + +class CellStorer { + public: + CellStorer(KeyValue &kv); + td::Status erase(td::Slice hash); + td::Status set(td::int32 refcnt, const DataCell &cell); + + private: + KeyValue &kv_; +}; +} // namespace vm diff --git a/crypto/vm/db/DynamicBagOfCellsDb.cpp b/crypto/vm/db/DynamicBagOfCellsDb.cpp new file mode 100644 index 00000000..732c1691 --- /dev/null +++ b/crypto/vm/db/DynamicBagOfCellsDb.cpp @@ -0,0 +1,500 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "vm/db/DynamicBagOfCellsDb.h" +#include "vm/db/CellStorage.h" +#include "vm/db/CellHashTable.h" + +#include "vm/cells/ExtCell.h" + +#include "td/utils/base64.h" +#include "td/utils/format.h" +#include "td/utils/ThreadSafeCounter.h" + +#include "vm/cellslice.h" + +namespace vm { +namespace { + +class CellDbReader { + public: + virtual ~CellDbReader() = default; + virtual td::Result> load_cell(td::Slice hash) = 0; +}; + +struct DynamicBocExtCellExtra { + std::shared_ptr reader; +}; + +class DynamicBocCellLoader { + public: + static td::Result> load_data_cell(const Cell &cell, const DynamicBocExtCellExtra &extra) { + return extra.reader->load_cell(cell.get_hash().as_slice()); + } +}; + +using DynamicBocExtCell = ExtCell; + +struct CellInfo { + bool sync_with_db{false}; + bool in_db{false}; + + bool was_dfs_new_cells{false}; + bool was{false}; + + td::int32 db_refcnt{0}; + td::int32 refcnt_diff{0}; + Ref cell; + Cell::Hash key() const { + return cell->get_hash(); + } + bool operator<(const CellInfo &other) const { + return key() < other.key(); + } +}; + +bool operator<(const CellInfo &a, td::Slice b) { + return a.key().as_slice() < b; +} + +bool operator<(td::Slice a, const CellInfo &b) { + return a < b.key().as_slice(); +} + +class DynamicBagOfCellsDbImpl : public DynamicBagOfCellsDb, private ExtCellCreator { + public: + DynamicBagOfCellsDbImpl() { + get_thread_safe_counter().add(1); + } + ~DynamicBagOfCellsDbImpl() { + get_thread_safe_counter().add(-1); + reset_cell_db_reader(); + } + td::Result> ext_cell(Cell::LevelMask level_mask, td::Slice hash, td::Slice depth) override { + return get_cell_info_lazy(level_mask, hash, depth).cell; + } + td::Result> load_cell(td::Slice hash) override { + TRY_RESULT(loaded_cell, get_cell_info_force(hash).cell->load_cell()); + return std::move(loaded_cell.data_cell); + } + CellInfo &get_cell_info_force(td::Slice hash) { + return hash_table_.apply(hash, [&](CellInfo &info) { update_cell_info_force(info, hash); }); + } + CellInfo &get_cell_info_lazy(Cell::LevelMask level_mask, td::Slice hash, td::Slice depth) { + return hash_table_.apply(hash.substr(hash.size() - Cell::hash_bytes), + [&](CellInfo &info) { update_cell_info_lazy(info, level_mask, hash, depth); }); + } + CellInfo &get_cell_info(const Ref &cell) { + return hash_table_.apply(cell->get_hash().as_slice(), [&](CellInfo &info) { update_cell_info(info, cell); }); + } + + void inc(const Ref &cell) override { + if (cell.is_null()) { + return; + } + if (cell->get_virtualization() != 0) { + return; + } + //LOG(ERROR) << "INC"; + //CellSlice(cell, nullptr).print_rec(std::cout); + to_inc_.push_back(cell); + } + void dec(const Ref &cell) override { + if (cell.is_null()) { + return; + } + if (cell->get_virtualization() != 0) { + return; + } + //LOG(ERROR) << "DEC"; + //CellSlice(cell, nullptr).print_rec(std::cout); + to_dec_.push_back(cell); + } + + bool is_prepared_for_commit() { + return to_inc_.empty() && to_dec_.empty(); + } + + Stats get_stats_diff() override { + CHECK(is_prepared_for_commit()); + return stats_diff_; + } + + td::Status prepare_commit() override { + if (is_prepared_for_commit()) { + return td::Status::OK(); + } + //LOG(ERROR) << "dfs_new_cells_in_db"; + for (auto &new_cell : to_inc_) { + auto &new_cell_info = get_cell_info(new_cell); + dfs_new_cells_in_db(new_cell_info); + } + //return td::Status::OK(); + //LOG(ERROR) << "dfs_new_cells"; + for (auto &new_cell : to_inc_) { + auto &new_cell_info = get_cell_info(new_cell); + dfs_new_cells(new_cell_info); + } + + //LOG(ERROR) << "dfs_old_cells"; + for (auto &old_cell : to_dec_) { + auto &old_cell_info = get_cell_info(old_cell); + dfs_old_cells(old_cell_info); + } + + //LOG(ERROR) << "save_diff_prepare"; + save_diff_prepare(); + + to_inc_.clear(); + to_dec_.clear(); + + return td::Status::OK(); + } + + td::Status commit(CellStorer &storer) override { + prepare_commit(); + save_diff(storer); + // Some elements are erased from hash table, to keep it small. + // Hash table is no longer represents the difference between the loader and + // the current bag of cells. + reset_cell_db_reader(); + return td::Status::OK(); + } + + td::Status set_loader(std::unique_ptr loader) override { + reset_cell_db_reader(); + loader_ = std::move(loader); + //cell_db_reader_ = std::make_shared(this); + // Temporary(?) fix to make ExtCell thread safe. + // Downside(?) - loaded cells won't be cached + cell_db_reader_ = std::make_shared(std::make_unique(*loader_)); + stats_diff_ = {}; + return td::Status::OK(); + } + + private: + std::unique_ptr loader_; + std::vector> to_inc_; + std::vector> to_dec_; + CellHashTable hash_table_; + std::vector visited_; + Stats stats_diff_; + + static td::NamedThreadSafeCounter::CounterRef get_thread_safe_counter() { + static auto res = td::NamedThreadSafeCounter::get_default().get_counter("DynamicBagOfCellsDb"); + return res; + } + + class CellDbReaderImpl : public CellDbReader, + private ExtCellCreator, + public std::enable_shared_from_this { + public: + CellDbReaderImpl(std::unique_ptr cell_loader) : db_(nullptr), cell_loader_(std::move(cell_loader)) { + if (cell_loader_) { + get_thread_safe_counter().add(1); + } + } + CellDbReaderImpl(DynamicBagOfCellsDb *db) : db_(db) { + } + ~CellDbReaderImpl() { + if (cell_loader_) { + get_thread_safe_counter().add(-1); + } + } + void set_loader(std::unique_ptr cell_loader) { + if (cell_loader_) { + // avoid race + return; + } + cell_loader_ = std::move(cell_loader); + db_ = nullptr; + if (cell_loader_) { + get_thread_safe_counter().add(1); + } + } + + td::Result> ext_cell(Cell::LevelMask level_mask, td::Slice hash, td::Slice depth) override { + CHECK(!db_); + TRY_RESULT(ext_cell, DynamicBocExtCell::create(PrunnedCellInfo{level_mask, hash, depth}, + DynamicBocExtCellExtra{shared_from_this()})); + return std::move(ext_cell); + } + + td::Result> load_cell(td::Slice hash) override { + if (db_) { + return db_->load_cell(hash); + } + TRY_RESULT(load_result, cell_loader_->load(hash, true, *this)); + CHECK(load_result.status == CellLoader::LoadResult::Ok); + return std::move(load_result.cell()); + } + + private: + static td::NamedThreadSafeCounter::CounterRef get_thread_safe_counter() { + static auto res = td::NamedThreadSafeCounter::get_default().get_counter("DynamicBagOfCellsDbLoader"); + return res; + } + DynamicBagOfCellsDb *db_; + std::unique_ptr cell_loader_; + }; + + std::shared_ptr cell_db_reader_; + + void reset_cell_db_reader() { + if (!cell_db_reader_) { + return; + } + cell_db_reader_->set_loader(std::move(loader_)); + cell_db_reader_.reset(); + //EXPERIMENTAL: clear cache to drop all references to old reader. + hash_table_ = {}; + } + + bool is_in_db(CellInfo &info) { + if (info.in_db) { + return true; + } + load_cell(info); + return info.in_db; + } + bool is_loaded(CellInfo &info) { + return info.sync_with_db; + } + + void load_cell(CellInfo &info) { + if (is_loaded(info)) { + return; + } + do_load_cell(info); + } + + bool dfs_new_cells_in_db(CellInfo &info) { + if (info.sync_with_db) { + return is_in_db(info); + } + if (info.in_db) { + return true; + } + + bool not_in_db = false; + for_each( + info, [¬_in_db, this](auto &child_info) { not_in_db |= !dfs_new_cells_in_db(child_info); }, false); + + if (not_in_db) { + CHECK(!info.in_db); + info.sync_with_db = true; + } + return is_in_db(info); + } + + void dfs_new_cells(CellInfo &info) { + info.refcnt_diff++; + if (!info.was) { + info.was = true; + visited_.push_back(&info); + } + //LOG(ERROR) << "dfs new " << td::format::escaped(info.cell->hash()); + + if (info.was_dfs_new_cells) { + return; + } + info.was_dfs_new_cells = true; + + if (is_in_db(info)) { + return; + } + + CHECK(is_loaded(info)); + for_each(info, [this](auto &child_info) { dfs_new_cells(child_info); }); + } + + void dfs_old_cells(CellInfo &info) { + info.refcnt_diff--; + if (!info.was) { + info.was = true; + visited_.push_back(&info); + } + //LOG(ERROR) << "dfs old " << td::format::escaped(info.cell->hash()); + + load_cell(info); + + auto new_refcnt = info.refcnt_diff + info.db_refcnt; + CHECK(new_refcnt >= 0); + if (new_refcnt != 0) { + return; + } + + for_each(info, [this](auto &child_info) { dfs_old_cells(child_info); }); + } + + void save_diff_prepare() { + stats_diff_ = {}; + for (auto info_ptr : visited_) { + save_cell_prepare(*info_ptr); + } + } + + void save_diff(CellStorer &storer) { + //LOG(ERROR) << hash_table_.size(); + for (auto info_ptr : visited_) { + save_cell(*info_ptr, storer); + } + visited_.clear(); + } + + void save_cell_prepare(CellInfo &info) { + if (info.refcnt_diff == 0) { + //CellSlice(info.cell, nullptr).print_rec(std::cout); + return; + } + load_cell(info); + + auto loaded_cell = info.cell->load_cell().move_as_ok(); + if (info.db_refcnt + info.refcnt_diff == 0) { + CHECK(info.in_db); + // erase + stats_diff_.cells_total_count--; + stats_diff_.cells_total_size -= loaded_cell.data_cell->get_serialized_size(true); + } else { + //save + if (info.in_db == false) { + stats_diff_.cells_total_count++; + stats_diff_.cells_total_size += loaded_cell.data_cell->get_serialized_size(true); + } + } + } + + void save_cell(CellInfo &info, CellStorer &storer) { + auto guard = td::ScopeExit{} + [&] { + info.was_dfs_new_cells = false; + info.was = false; + }; + if (info.refcnt_diff == 0) { + //CellSlice(info.cell, nullptr).print_rec(std::cout); + return; + } + CHECK(info.sync_with_db); + + info.db_refcnt += info.refcnt_diff; + info.refcnt_diff = 0; + + if (info.db_refcnt == 0) { + CHECK(info.in_db); + //LOG(ERROR) << "ERASE"; + //CellSlice(NoVm(), info.cell).print_rec(std::cout); + storer.erase(info.cell->get_hash().as_slice()); + info.in_db = false; + hash_table_.erase(info.cell->get_hash().as_slice()); + guard.dismiss(); + } else { + //LOG(ERROR) << "SAVE " << info.db_refcnt; + //CellSlice(NoVm(), info.cell).print_rec(std::cout); + auto loaded_cell = info.cell->load_cell().move_as_ok(); + storer.set(info.db_refcnt, *loaded_cell.data_cell); + info.in_db = true; + } + } + + template + void for_each(CellInfo &info, F &&f, bool force = true) { + auto cell = info.cell; + + if (!cell->is_loaded()) { + if (!force) { + return; + } + load_cell(info); + cell = info.cell; + } + if (!cell->is_loaded()) { + cell->load_cell().ensure(); + } + CHECK(cell->is_loaded()); + vm::CellSlice cs(vm::NoVm{}, cell); // FIXME + for (unsigned i = 0; i < cs.size_refs(); i++) { + //LOG(ERROR) << "---> " << td::format::escaped(cell->ref(i)->hash()); + f(get_cell_info(cs.prefetch_ref(i))); + } + } + + void do_load_cell(CellInfo &info) { + update_cell_info_force(info, info.cell->get_hash().as_slice()); + } + + void update_cell_info(CellInfo &info, const Ref &cell) { + CHECK(!cell.is_null()); + if (info.sync_with_db) { + return; + } + info.cell = cell; + } + + void update_cell_info_lazy(CellInfo &info, Cell::LevelMask level_mask, td::Slice hash, td::Slice depth) { + if (info.sync_with_db) { + CHECK(info.cell.not_null()); + CHECK(info.cell->get_level_mask() == level_mask); + return; + } + if (info.cell.is_null()) { + auto ext_cell_r = create_empty_ext_cell(level_mask, hash, depth); + if (ext_cell_r.is_error()) { + //FIXME + LOG(ERROR) << "Failed to create ext_cell" << ext_cell_r.error(); + return; + } + info.cell = ext_cell_r.move_as_ok(); + info.in_db = true; // TODO + } + } + void update_cell_info_force(CellInfo &info, td::Slice hash) { + if (info.sync_with_db) { + return; + } + + do { + CHECK(loader_); + auto r_res = loader_->load(hash, true, *this); + if (r_res.is_error()) { + //FIXME + LOG(ERROR) << "Failed to load cell from db" << r_res.error(); + break; + } + auto res = r_res.move_as_ok(); + if (res.status != CellLoader::LoadResult::Ok) { + break; + } + info.cell = std::move(res.cell()); + CHECK(info.cell->get_hash().as_slice() == hash); + info.in_db = true; + info.db_refcnt = res.refcnt(); + } while (false); + info.sync_with_db = true; + } + + td::Result> create_empty_ext_cell(Cell::LevelMask level_mask, td::Slice hash, td::Slice depth) { + TRY_RESULT(res, DynamicBocExtCell::create(PrunnedCellInfo{level_mask, hash, depth}, + DynamicBocExtCellExtra{cell_db_reader_})); + return std::move(res); + } +}; +} // namespace + +std::unique_ptr DynamicBagOfCellsDb::create() { + return std::make_unique(); +} +} // namespace vm diff --git a/crypto/vm/db/DynamicBagOfCellsDb.h b/crypto/vm/db/DynamicBagOfCellsDb.h new file mode 100644 index 00000000..54055a25 --- /dev/null +++ b/crypto/vm/db/DynamicBagOfCellsDb.h @@ -0,0 +1,62 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once +#include "vm/cells.h" + +#include "td/utils/Slice.h" +#include "td/utils/Status.h" + +namespace vm { +class CellLoader; +class CellStorer; +} // namespace vm + +namespace vm { +class ExtCellCreator { + public: + virtual ~ExtCellCreator() = default; + virtual td::Result> ext_cell(Cell::LevelMask level_mask, td::Slice hash, td::Slice depth) = 0; +}; + +class DynamicBagOfCellsDb { + public: + virtual ~DynamicBagOfCellsDb() = default; + virtual td::Result> load_cell(td::Slice hash) = 0; + struct Stats { + td::int64 cells_total_count{0}; + td::int64 cells_total_size{0}; + void apply_diff(Stats diff) { + cells_total_count += diff.cells_total_count; + cells_total_size += diff.cells_total_size; + } + }; + virtual void inc(const Ref &old_root) = 0; + virtual void dec(const Ref &old_root) = 0; + + virtual td::Status prepare_commit() = 0; + virtual Stats get_stats_diff() = 0; + virtual td::Status commit(CellStorer &) = 0; + + // restart with new loader will also reset stats_diff + virtual td::Status set_loader(std::unique_ptr loader) = 0; + + static std::unique_ptr create(); +}; + +} // namespace vm diff --git a/crypto/vm/db/StaticBagOfCellsDb.cpp b/crypto/vm/db/StaticBagOfCellsDb.cpp new file mode 100644 index 00000000..b69b917d --- /dev/null +++ b/crypto/vm/db/StaticBagOfCellsDb.cpp @@ -0,0 +1,545 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "vm/db/StaticBagOfCellsDb.h" + +#include "vm/cells/CellWithStorage.h" +#include "vm/boc.h" + +#include "vm/cells/ExtCell.h" + +#include "td/utils/crypto.h" +#include "td/utils/format.h" +#include "td/utils/misc.h" +#include "td/utils/port/RwMutex.h" +#include "td/utils/ConcurrentHashTable.h" + +#include + +namespace vm { +// +// Common interface +// +template +class RootCell : public Cell { + struct PrivateTag {}; + + public: + td::Result load_cell() const override { + return cell_->load_cell(); + } + Ref virtualize(VirtualizationParameters virt) const override { + return cell_->virtualize(virt); + } + td::uint32 get_virtualization() const override { + return cell_->get_virtualization(); + } + CellUsageTree::NodePtr get_tree_node() const override { + return cell_->get_tree_node(); + } + bool is_loaded() const override { + return cell_->is_loaded(); + } + + // hash and level + LevelMask get_level_mask() const override { + return cell_->get_level_mask(); + } + + template + static Ref create(Ref cell, T&& extra) { + return Ref(true, std::move(cell), std::forward(extra), PrivateTag{}); + } + template + RootCell(Ref cell, T&& extra, PrivateTag) : cell_(std::move(cell)), extra_(std::forward(extra)) { + } + + private: + Ref cell_; + ExtraT extra_; + td::uint16 do_get_depth(td::uint32 level) const override { + return cell_->get_depth(level); + } + const Hash do_get_hash(td::uint32 level) const override { + return cell_->get_hash(level); + } +}; + +class DataCellCacheNoop { + public: + Ref store(int idx, Ref cell) { + return cell; + } + Ref load(int idx) { + return {}; + } + void clear() { + } +}; +class DataCellCacheMutex { + public: + Ref store(int idx, Ref cell) { + auto lock = cells_rw_mutex_.lock_write(); + return cells_.emplace(idx, std::move(cell)).first->second; + } + Ref load(int idx) { + auto lock = cells_rw_mutex_.lock_read(); + auto it = cells_.find(idx); + if (it != cells_.end()) { + return it->second; + } + return {}; + } + void clear() { + auto guard = cells_rw_mutex_.lock_write(); + cells_.clear(); + } + + private: + td::RwMutex cells_rw_mutex_; + td::HashMap> cells_; +}; + +class DataCellCacheTdlib { + public: + Ref store(int idx, Ref cell) { + return Ref(cells_.insert(as_key(idx), cell.get())); + } + Ref load(int idx) { + return Ref(cells_.find(as_key(idx), nullptr)); + } + + void clear() { + cells_.for_each([](auto key, auto value) { Ref{value}; }); + } + + private: + td::ConcurrentHashMap cells_; + td::uint32 as_key(int idx) { + td::uint32 key = static_cast(idx + 1); + key *= 1000000007; + return key; + } +}; + +struct StaticBocExtCellExtra { + int idx; + std::weak_ptr deserializer; +}; + +class StaticBocLoader { + public: + static td::Result> load_data_cell(const Cell& cell, const StaticBocExtCellExtra& extra) { + auto deserializer = extra.deserializer.lock(); + if (!deserializer) { + return td::Status::Error("StaticBocDb is already destroyed, cannot fetch cell"); + } + return deserializer->load_by_idx(extra.idx); + } +}; +using StaticBocExtCell = ExtCell; +using StaticBocRootCell = RootCell>; + +td::Result> StaticBagOfCellsDb::create_ext_cell(Cell::LevelMask level_mask, td::Slice hash, td::Slice depth, + int idx) { + TRY_RESULT(res, StaticBocExtCell::create(PrunnedCellInfo{level_mask, hash, depth}, + StaticBocExtCellExtra{idx, shared_from_this()})); + return std::move(res); +} + +// +// Baseline implementation +// +class StaticBagOfCellsDbBaselineImpl : public StaticBagOfCellsDb { + public: + StaticBagOfCellsDbBaselineImpl(std::vector> roots) : roots_(std::move(roots)) { + } + td::Result get_root_count() override { + return roots_.size(); + }; + td::Result> get_root_cell(size_t idx) override { + if (idx >= roots_.size()) { + return td::Status::Error(PSLICE() << "invalid root_cell index: " << idx); + } + return roots_[idx]; + }; + + private: + std::vector> roots_; + + td::Result> load_by_idx(int idx) override { + UNREACHABLE(); + } +}; + +td::Result> StaticBagOfCellsDbBaseline::create(std::unique_ptr data) { + std::string buf(data->size(), '\0'); + TRY_RESULT(slice, data->view(buf, 0)); + return create(slice); +} + +td::Result> StaticBagOfCellsDbBaseline::create(td::Slice data) { + BagOfCells boc; + TRY_RESULT(x, boc.deserialize(data)); + if (x <= 0) { + return td::Status::Error("failed to deserialize"); + } + std::vector> roots(boc.get_root_count()); + for (int i = 0; i < boc.get_root_count(); i++) { + roots[i] = boc.get_root_cell(i); + } + return std::make_shared(std::move(roots)); +} + +// +// Main implementation +// +class StaticBagOfCellsDbLazyImpl : public StaticBagOfCellsDb { + public: + explicit StaticBagOfCellsDbLazyImpl(std::unique_ptr data, StaticBagOfCellsDbLazy::Options options) + : data_(std::move(data)), options_(std::move(options)) { + get_thread_safe_counter().add(1); + } + td::Result get_root_count() override { + TRY_STATUS(check_status()); + TRY_STATUS(check_result(load_header())); + return info_.root_count; + }; + td::Result> get_root_cell(size_t idx) override { + TRY_STATUS(check_status()); + TRY_RESULT(root_count, get_root_count()); + if (idx >= root_count) { + return td::Status::Error(PSLICE() << "invalid root_cell index: " << idx); + } + TRY_RESULT(cell_idx, load_root_idx(td::narrow_cast(idx))); + // Load DataCell in order to ensure lower hashes correctness + // They will be valid for all non-root cell automaically + TRY_RESULT(data_cell, check_result(load_data_cell(td::narrow_cast(cell_idx)))); + return create_root_cell(std::move(data_cell)); + }; + + ~StaticBagOfCellsDbLazyImpl() { + //LOG(ERROR) << deserialize_cell_cnt_ << " " << deserialize_cell_hash_cnt_; + get_thread_safe_counter().add(-1); + } + + private: + std::atomic should_cache_cells_{true}; + std::unique_ptr data_; + StaticBagOfCellsDbLazy::Options options_; + bool has_info_{false}; + BagOfCells::Info info_; + + std::mutex index_i_mutex_; + td::RwMutex index_data_rw_mutex_; + std::string index_data_; + std::atomic index_i_{0}; + size_t index_offset_{0}; + DataCellCacheMutex cells_; + //DataCellCacheNoop cells_; + //DataCellCacheTdlib cells_; + int next_idx_{0}; + Ref empty_cell_; + + //stats + td::ThreadSafeCounter deserialize_cell_cnt_; + td::ThreadSafeCounter deserialize_cell_hash_cnt_; + + std::atomic has_error_{false}; + std::mutex status_mutex_; + td::Status status_; + + static td::NamedThreadSafeCounter::CounterRef get_thread_safe_counter() { + static auto res = td::NamedThreadSafeCounter::get_default().get_counter("StaticBagOfCellsDbLazy"); + return res; + } + + td::Status check_status() TD_WARN_UNUSED_RESULT { + if (has_error_.load(std::memory_order_relaxed)) { + std::lock_guard guard(status_mutex_); + return status_.clone(); + } + return td::Status::OK(); + } + template + T check_result(T&& to_check) { + CHECK(status_.is_ok()); + if (to_check.is_error()) { + std::lock_guard guard(status_mutex_); + has_error_.store(true); + status_ = to_check.error().clone(); + } + return std::forward(to_check); + } + + td::Result> load_by_idx(int idx) override { + TRY_STATUS(check_status()); + return check_result(load_data_cell(idx)); + } + + struct Ptr { + td::MutableSlice as_slice() { + return data; + } + td::string data; + }; + // May be optimized + auto alloc(size_t size) { + //return td::StackAllocator::alloc(size); + return Ptr{std::string(size, '\0')}; + } + + td::Result load_idx_offset(int idx) { + if (idx < 0) { + return 0; + } + td::Slice offset_view; + CHECK(info_.offset_byte_size <= 8); + char arr[8]; + td::RwMutex::ReadLock guard; + if (info_.has_index) { + TRY_RESULT(new_offset_view, data_->view(td::MutableSlice(arr, info_.offset_byte_size), + info_.index_offset + idx * info_.offset_byte_size)); + offset_view = new_offset_view; + } else { + guard = index_data_rw_mutex_.lock_read().move_as_ok(); + offset_view = td::Slice(index_data_).substr(idx * info_.offset_byte_size, info_.offset_byte_size); + } + + CHECK(offset_view.size() == (size_t)info_.offset_byte_size); + return td::narrow_cast(info_.read_offset(offset_view.ubegin())); + } + + td::Result load_root_idx(int root_i) { + CHECK(root_i >= 0 && root_i < info_.root_count); + if (!info_.has_roots) { + return 0; + } + char arr[8]; + TRY_RESULT(idx_view, data_->view(td::MutableSlice(arr, info_.ref_byte_size), + info_.roots_offset + root_i * info_.ref_byte_size)); + CHECK(idx_view.size() == (size_t)info_.ref_byte_size); + return info_.read_ref(idx_view.ubegin()); + } + + struct CellLocation { + std::size_t begin; + std::size_t end; + bool should_cache; + }; + td::Result get_cell_location(int idx) { + CHECK(idx >= 0); + CHECK(idx < info_.cell_count); + TRY_STATUS(preload_index(idx)); + TRY_RESULT(from, load_idx_offset(idx - 1)); + TRY_RESULT(till, load_idx_offset(idx)); + CellLocation res; + res.begin = from; + res.end = till; + res.should_cache = true; + if (info_.has_cache_bits) { + res.begin /= 2; + res.should_cache = res.end % 2 == 1; + res.end /= 2; + } + CHECK(std::numeric_limits::max() - res.begin >= info_.data_offset); + CHECK(std::numeric_limits::max() - res.end >= info_.data_offset); + res.begin += static_cast(info_.data_offset); + res.end += static_cast(info_.data_offset); + return res; + } + + td::Status load_header() { + if (has_info_) { + return td::Status::OK(); + } + std::string header(1000, '\0'); + TRY_RESULT(header_view, data_->view(td::MutableSlice(header).truncate(data_->size()), 0)) + auto parse_res = info_.parse_serialized_header(header_view); + if (parse_res <= 0) { + return td::Status::Error("bag-of-cell error: failed to read header"); + } + if (info_.total_size < data_->size()) { + return td::Status::Error("bag-of-cell error: not enough data"); + } + if (options_.check_crc32c && info_.has_crc32c) { + std::string buf(td::narrow_cast(info_.total_size), '\0'); + TRY_RESULT(data, data_->view(td::MutableSlice(buf), 0)); + unsigned crc_computed = td::crc32c(td::Slice{data.ubegin(), data.uend() - 4}); + unsigned crc_stored = td::as(data.uend() - 4); + if (crc_computed != crc_stored) { + return td::Status::Error(PSLICE() + << "bag-of-cells CRC32C mismatch: expected " << td::format::as_hex(crc_computed) + << ", found " << td::format::as_hex(crc_stored)); + } + } + has_info_ = true; + return td::Status::OK(); + } + + td::Status preload_index(int idx) { + if (info_.has_index) { + return td::Status::OK(); + } + + CHECK(idx < info_.cell_count); + if (index_i_.load(std::memory_order_relaxed) > idx) { + return td::Status::OK(); + } + + std::lock_guard index_i_guard(index_i_mutex_); + std::array buf; + auto buf_slice = td::MutableSlice(buf.data(), buf.size()); + for (; index_i_ <= idx; index_i_++) { + auto offset = td::narrow_cast(info_.data_offset + index_offset_); + CHECK(data_->size() >= offset); + TRY_RESULT(cell, data_->view(buf_slice.copy().truncate(data_->size() - offset), offset)); + CellSerializationInfo cell_info; + TRY_STATUS(cell_info.init(cell, info_.ref_byte_size)); + index_offset_ += cell_info.end_offset; + LOG_CHECK((unsigned)info_.offset_byte_size <= 8) << info_.offset_byte_size; + td::uint8 tmp[8]; + info_.write_offset(tmp, index_offset_); + auto guard = index_data_rw_mutex_.lock_write(); + index_data_.append(reinterpret_cast(tmp), info_.offset_byte_size); + } + return td::Status::OK(); + } + + Ref get_any_cell(int idx) { + return get_data_cell(idx); + } + + Ref get_data_cell(int idx) { + return cells_.load(idx); + } + + Ref set_data_cell(int idx, Ref cell) { + if (/*idx >= info_.root_count || */ !should_cache_cells_.load(std::memory_order_relaxed)) { + return cell; + } + CHECK(cell.not_null()); + return cells_.store(idx, std::move(cell)); + } + + Ref set_any_cell(int idx, Ref cell) { + auto data_cell = Ref(cell); + if (data_cell.is_null()) { + return cell; + } + return set_data_cell(idx, std::move(data_cell)); + } + + td::Result> load_any_cell(int idx) { + { + auto cell = get_any_cell(idx); + if (cell.not_null()) { + return std::move(cell); + } + } + + TRY_RESULT(cell_location, get_cell_location(idx)); + auto buf = alloc(cell_location.end - cell_location.begin); + TRY_RESULT(cell_slice, data_->view(buf.as_slice(), cell_location.begin)); + TRY_RESULT(res, deserialize_any_cell(idx, cell_slice, cell_location.should_cache)); + return std::move(res); + } + + td::Result> load_data_cell(int idx) { + { + auto cell = get_data_cell(idx); + if (cell.not_null()) { + return std::move(cell); + } + } + + TRY_RESULT(cell_location, get_cell_location(idx)); + auto buf = alloc(cell_location.end - cell_location.begin); + TRY_RESULT(cell_slice, data_->view(buf.as_slice(), cell_location.begin)); + TRY_RESULT(res, deserialize_data_cell(idx, cell_slice, cell_location.should_cache)); + return std::move(res); + } + + td::Result> deserialize_data_cell(int idx, td::Slice cell_slice, bool should_cache) { + CellSerializationInfo cell_info; + TRY_STATUS(cell_info.init(cell_slice, info_.ref_byte_size)); + if (cell_slice.size() != cell_info.end_offset) { + return td::Status::Error(PSLICE() << "unused space in cell #" << idx << " serialization"); + } + return deserialize_data_cell(idx, cell_slice, cell_info, should_cache); + } + + td::Result> deserialize_data_cell(int idx, td::Slice cell_slice, const CellSerializationInfo& cell_info, + bool should_cache) { + deserialize_cell_cnt_.add(1); + Ref refs[4]; + CHECK(cell_info.refs_cnt <= 4); + auto* ref_ptr = cell_slice.ubegin() + cell_info.refs_offset; + for (int k = 0; k < cell_info.refs_cnt; k++, ref_ptr += info_.ref_byte_size) { + int ref_idx = td::narrow_cast(info_.read_ref(ref_ptr)); + if (ref_idx >= info_.cell_count) { + return td::Status::Error(PSLICE() << "invalid bag-of-cells cell #" << idx << " refers to cell #" << ref_idx + << " which is too big " << td::tag("cell_count", info_.cell_count)); + } + if (idx >= ref_idx) { + return td::Status::Error(PSLICE() << "invalid bag-of-cells cell #" << idx << " refers to cell #" << ref_idx + << " which is a backward reference"); + } + TRY_RESULT(ref, load_any_cell(ref_idx)); + refs[k] = std::move(ref); + } + + TRY_RESULT(data_cell, cell_info.create_data_cell(cell_slice, td::Span>(refs, cell_info.refs_cnt))); + if (!should_cache) { + return std::move(data_cell); + } + return set_data_cell(idx, std::move(data_cell)); + } + + td::Result> deserialize_any_cell(int idx, td::Slice cell_slice, bool should_cache) { + CellSerializationInfo cell_info; + TRY_STATUS(cell_info.init(cell_slice, info_.ref_byte_size)); + if (cell_info.with_hashes) { + deserialize_cell_hash_cnt_.add(1); + int n = cell_info.level_mask.get_hashes_count(); + return create_ext_cell(cell_info.level_mask, cell_slice.substr(cell_info.hashes_offset, n * Cell::hash_bytes), + cell_slice.substr(cell_info.depth_offset, n * Cell::depth_bytes), idx); + } + TRY_RESULT(data_cell, deserialize_data_cell(idx, cell_slice, cell_info, should_cache)); + return std::move(data_cell); + } + td::Result> create_root_cell(Ref data_cell) { + return StaticBocRootCell::create(std::move(data_cell), shared_from_this()); + } +}; + +td::Result> StaticBagOfCellsDbLazy::create(std::unique_ptr data, + Options options) { + return std::make_shared(std::move(data), std::move(options)); +} + +td::Result> StaticBagOfCellsDbLazy::create(td::BufferSlice data, Options options) { + return std::make_shared(vm::BufferSliceBlobView::create(std::move(data)), + std::move(options)); +} + +td::Result> StaticBagOfCellsDbLazy::create(std::string data, Options options) { + return create(BufferSliceBlobView::create(td::BufferSlice(data)), std::move(options)); +} + +} // namespace vm diff --git a/crypto/vm/db/StaticBagOfCellsDb.h b/crypto/vm/db/StaticBagOfCellsDb.h new file mode 100644 index 00000000..4a129fb7 --- /dev/null +++ b/crypto/vm/db/StaticBagOfCellsDb.h @@ -0,0 +1,60 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +#include "vm/cells.h" +#include "vm/db/BlobView.h" + +#include "td/utils/Status.h" + +namespace vm { +class StaticBagOfCellsDb : public std::enable_shared_from_this { + public: + virtual ~StaticBagOfCellsDb() = default; + // TODO: handle errors + virtual td::Result get_root_count() = 0; + virtual td::Result> get_root_cell(size_t idx) = 0; + + protected: + virtual td::Result> load_by_idx(int idx) = 0; + friend class StaticBocLoader; + friend class StaticBocRootLoader; + td::Result> create_ext_cell(Cell::LevelMask level_mask, td::Slice hash, td::Slice depth, int idx); + td::Result> create_root_ext_cell(Cell::LevelMask level_mask, td::Slice hash, td::Slice depth, int idx); +}; + +class StaticBagOfCellsDbBaseline { + public: + static td::Result> create(std::unique_ptr data); + static td::Result> create(td::Slice data); +}; + +class StaticBagOfCellsDbLazy { + public: + struct Options { + Options() { + } + bool check_crc32c{false}; + }; + static td::Result> create(std::unique_ptr data, Options options = {}); + static td::Result> create(td::BufferSlice data, Options options = {}); + static td::Result> create(std::string data, Options options = {}); +}; + +} // namespace vm diff --git a/crypto/vm/db/TonDb.cpp b/crypto/vm/db/TonDb.cpp new file mode 100644 index 00000000..f478c992 --- /dev/null +++ b/crypto/vm/db/TonDb.cpp @@ -0,0 +1,325 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "vm/db/TonDb.h" + +#include "td/utils/tl_helpers.h" +#include "td/utils/Random.h" + +#if TDDB_USE_ROCKSDB +#include "td/db/RocksDb.h" +#endif + +namespace vm { + +template +void SmartContractMeta::store(StorerT &storer) const { + using td::store; + store(stats.cells_total_count, storer); + store(stats.cells_total_size, storer); + store(type, storer); +} +template +void SmartContractMeta::parse(ParserT &parser) { + using td::parse; + parse(stats.cells_total_count, parser); + parse(stats.cells_total_size, parser); + parse(type, parser); +} + +// +// SmartContractDbImpl +// +Ref SmartContractDbImpl::get_root() { + if (sync_root_with_db_ || !new_root_.is_null()) { + return new_root_; + } + + sync_root_with_db(); + return new_root_; +} + +void SmartContractDbImpl::set_root(Ref new_root) { + CHECK(new_root.not_null()); + sync_root_with_db(); + if (is_dynamic()) { + cell_db_->dec(new_root_); + } + new_root_ = std::move(new_root); + if (is_dynamic()) { + cell_db_->inc(new_root_); + } +} + +SmartContractDbImpl::SmartContractDbImpl(td::Slice hash, std::shared_ptr kv) + : hash_(hash.str()), kv_(std::move(kv)) { + cell_db_ = DynamicBagOfCellsDb::create(); +} + +SmartContractMeta SmartContractDbImpl::get_meta() { + sync_root_with_db(); + return meta_; +} +td::Status SmartContractDbImpl::validate_meta() { + if (!is_dynamic()) { + return td::Status::OK(); + } + sync_root_with_db(); + TRY_RESULT(in_db, kv_->count({})); + if (static_cast(in_db) != meta_.stats.cells_total_count + 2) { + return td::Status::Error(PSLICE() << "Invalid meta " << td::tag("expected_count", in_db) + << td::tag("meta_count", meta_.stats.cells_total_count + 2)); + } + return td::Status::OK(); +} + +bool SmartContractDbImpl::is_dynamic() const { + return meta_.type == SmartContractMeta::Dynamic; +} + +bool SmartContractDbImpl::is_root_changed() const { + return !new_root_.is_null() && (db_root_.is_null() || db_root_->get_hash() != new_root_->get_hash()); +} + +void SmartContractDbImpl::sync_root_with_db() { + if (sync_root_with_db_) { + return; + } + std::string root_hash; + kv_->get("root", root_hash); + std::string meta_serialized; + kv_->get("meta", meta_serialized); + // TODO: proper serialization + td::unserialize(meta_, meta_serialized).ignore(); + sync_root_with_db_ = true; + + if (root_hash.empty()) { + meta_.type = SmartContractMeta::Static; + //meta_.type = SmartContractMeta::Dynamic; + } else { + if (is_dynamic()) { + //FIXME: error handling + db_root_ = cell_db_->load_cell(root_hash).move_as_ok(); + } else { + std::string boc_serialized; + kv_->get("boc", boc_serialized); + BagOfCells boc; + //TODO: check error + boc.deserialize(boc_serialized); + db_root_ = boc.get_root_cell(); + } + CHECK(db_root_->get_hash().as_slice() == root_hash); + new_root_ = db_root_; + } +} + +enum { boc_size = 2000 }; +void SmartContractDbImpl::prepare_commit_dynamic(bool force) { + if (!is_dynamic()) { + CHECK(force); + meta_.stats = {}; + cell_db_->inc(new_root_); + } + cell_db_->prepare_commit(); + meta_.stats.apply_diff(cell_db_->get_stats_diff()); + + if (!force && meta_.stats.cells_total_size < boc_size) { + //LOG(ERROR) << "DYNAMIC -> BOC"; + return prepare_commit_static(true); + } + is_dynamic_commit_ = true; +}; + +void SmartContractDbImpl::prepare_commit_static(bool force) { + BagOfCells boc; + boc.add_root(new_root_); + boc.import_cells().ensure(); // FIXME + if (!force && boc.estimate_serialized_size(15) > boc_size) { + //LOG(ERROR) << "BOC -> DYNAMIC "; + return prepare_commit_dynamic(true); + } + if (is_dynamic()) { + cell_db_->dec(new_root_); + cell_db_->prepare_commit(); + // stats is invalid now + } + is_dynamic_commit_ = false; + boc_to_commit_ = boc.serialize_to_string(15); + meta_.stats = {}; +} + +void SmartContractDbImpl::prepare_transaction() { + sync_root_with_db(); + if (!is_root_changed()) { + return; + } + + if (is_dynamic()) { + prepare_commit_dynamic(false); + } else { + prepare_commit_static(false); + } +} + +void SmartContractDbImpl::commit_transaction(KeyValue &kv) { + if (!is_root_changed()) { + return; + } + + if (is_dynamic_commit_) { + //LOG(ERROR) << "STORE DYNAMIC"; + if (!is_dynamic() && db_root_.not_null()) { + kv.erase("boc"); + } + CellStorer storer(kv); + cell_db_->commit(storer); + meta_.type = SmartContractMeta::Dynamic; + } else { + //LOG(ERROR) << "STORE BOC"; + if (is_dynamic() && db_root_.not_null()) { + //LOG(ERROR) << "Clear Dynamic db"; + CellStorer storer(kv); + cell_db_->commit(storer); + cell_db_ = DynamicBagOfCellsDb::create(); + } + meta_.type = SmartContractMeta::Static; + kv.set("boc", boc_to_commit_); + boc_to_commit_ = {}; + } + + kv.set("root", new_root_->get_hash().as_slice()); + kv.set("meta", td::serialize(meta_)); + db_root_ = new_root_; +} + +void SmartContractDbImpl::set_reader(std::shared_ptr reader) { + kv_ = std::move(reader); + cell_db_->set_loader(std::make_unique(kv_)); +} + +// +// TonDbTransactionImpl +// +SmartContractDb TonDbTransactionImpl::begin_smartcontract(td::Slice hash) { + SmartContractDb res; + contracts_.apply(hash, [&](auto &info) { + if (!info.is_inited) { + info.is_inited = true; + info.hash = hash.str(); + info.smart_contract_db = std::make_unique(hash, nullptr); + } + LOG_CHECK(info.generation_ != generation_) << "Cannot begin one smartcontract twice during the same transaction"; + CHECK(info.smart_contract_db); + info.smart_contract_db->set_reader(std::make_shared(reader_, hash)); + res = std::move(info.smart_contract_db); + }); + return res; +} + +void TonDbTransactionImpl::commit_smartcontract(SmartContractDb txn) { + commit_smartcontract(SmartContractDiff(std::move(txn))); +} +void TonDbTransactionImpl::commit_smartcontract(SmartContractDiff txn) { + { + td::PrefixedKeyValue kv(kv_, txn.hash()); + txn.commit_transaction(kv); + } + end_smartcontract(txn.extract_smartcontract()); +} + +void TonDbTransactionImpl::abort_smartcontract(SmartContractDb txn) { + end_smartcontract(std::move(txn)); +} +void TonDbTransactionImpl::abort_smartcontract(SmartContractDiff txn) { + end_smartcontract(txn.extract_smartcontract()); +} + +TonDbTransactionImpl::TonDbTransactionImpl(std::shared_ptr kv) : kv_(std::move(kv)) { + CHECK(kv_ != nullptr); + reader_.reset(kv_->snapshot().release()); +} + +void TonDbTransactionImpl::begin() { + kv_->begin_transaction(); + generation_++; +} +void TonDbTransactionImpl::commit() { + kv_->commit_transaction(); + reader_.reset(kv_->snapshot().release()); +} +void TonDbTransactionImpl::abort() { + kv_->abort_transaction(); +} +void TonDbTransactionImpl::clear_cache() { + contracts_ = {}; +} + +void TonDbTransactionImpl::end_smartcontract(SmartContractDb smart_contract) { + contracts_.apply(smart_contract->hash(), [&](auto &info) { + CHECK(info.hash == smart_contract->hash()); + CHECK(!info.smart_contract_db); + info.smart_contract_db = std::move(smart_contract); + }); +} + +// +// TonDbImpl +// +TonDbImpl::TonDbImpl(std::unique_ptr kv) + : kv_(std::move(kv)), transaction_(std::make_unique(kv_)) { +} +TonDbImpl::~TonDbImpl() { + CHECK(transaction_); + kv_->flush(); +} +TonDbTransaction TonDbImpl::begin_transaction() { + CHECK(transaction_); + transaction_->begin(); + return std::move(transaction_); +} +void TonDbImpl::commit_transaction(TonDbTransaction transaction) { + CHECK(!transaction_); + CHECK(&transaction->kv() == kv_.get()); + transaction_ = std::move(transaction); + transaction_->commit(); +} +void TonDbImpl::abort_transaction(TonDbTransaction transaction) { + CHECK(!transaction_); + CHECK(&transaction->kv() == kv_.get()); + transaction_ = std::move(transaction); + transaction_->abort(); +} +void TonDbImpl::clear_cache() { + CHECK(transaction_); + transaction_->clear_cache(); +} + +std::string TonDbImpl::stats() const { + return kv_->stats(); +} + +td::Result TonDbImpl::open(td::Slice path) { +#if TDDB_USE_ROCKSDB + TRY_RESULT(rocksdb, td::RocksDb::open(path.str())); + return std::make_unique(std::make_unique(std::move(rocksdb))); +#else + return td::Status::Error("TonDb is not supported in this build"); +#endif +} + +} // namespace vm diff --git a/crypto/vm/db/TonDb.h b/crypto/vm/db/TonDb.h new file mode 100644 index 00000000..2f4ba090 --- /dev/null +++ b/crypto/vm/db/TonDb.h @@ -0,0 +1,179 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once +#include "vm/cellslice.h" +#include "vm/cells.h" +#include "vm/boc.h" +#include "td/db/KeyValue.h" +#include "vm/db/CellStorage.h" +#include "vm/db/CellHashTable.h" + +#include "td/utils/Slice.h" +#include "td/utils/Status.h" + +namespace vm { +class SmartContractDbImpl; +using SmartContractDb = std::unique_ptr; +using KeyValue = td::KeyValue; +using KeyValueReader = td::KeyValueReader; + +struct SmartContractMeta { + DynamicBagOfCellsDb::Stats stats; + enum BagOfCellsType { Dynamic, Static } type{Static}; + + template + void store(StorerT &storer) const; + template + void parse(ParserT &parser); +}; + +class SmartContractDbImpl { + public: + Ref get_root(); + SmartContractMeta get_meta(); + td::Status validate_meta(); + + void set_root(Ref new_root); + + SmartContractDbImpl(td::Slice hash, std::shared_ptr kv); + + private: + std::string hash_; + std::shared_ptr kv_; + + bool sync_root_with_db_{false}; + Ref db_root_; + Ref new_root_; + SmartContractMeta meta_; + bool is_dynamic_commit_; + std::string boc_to_commit_; + + std::unique_ptr cell_db_; + std::unique_ptr bag_of_cells_; + + friend class SmartContractDiff; + friend class TonDbTransactionImpl; + + void sync_root_with_db(); + + td::Slice hash() const { + return hash_; + } + + void prepare_transaction(); + void commit_transaction(KeyValue &kv); + + void set_reader(std::shared_ptr reader); + + bool is_dynamic() const; + void prepare_commit_dynamic(bool force); + void prepare_commit_static(bool force); + bool is_root_changed() const; +}; + +class SmartContractDiff { + public: + explicit SmartContractDiff(SmartContractDb db) : db_(std::move(db)) { + db_->prepare_transaction(); + } + + SmartContractDb extract_smartcontract() { + return std::move(db_); + } + + td::Slice hash() const { + return db_->hash(); + } + + void commit_transaction(KeyValue &kv) { + db_->commit_transaction(kv); + } + + private: + SmartContractDb db_; +}; + +class TonDbTransactionImpl; +using TonDbTransaction = std::unique_ptr; +class TonDbTransactionImpl { + public: + SmartContractDb begin_smartcontract(td::Slice hash = {}); + + void commit_smartcontract(SmartContractDb txn); + void commit_smartcontract(SmartContractDiff txn); + + void abort_smartcontract(SmartContractDb txn); + void abort_smartcontract(SmartContractDiff txn); + + TonDbTransactionImpl(std::shared_ptr kv); + + private: + std::shared_ptr kv_; + std::shared_ptr reader_; + td::uint64 generation_{0}; + + struct SmartContractInfo { + bool is_inited{false}; + td::uint64 generation_{0}; + std::string hash; + SmartContractDb smart_contract_db; + bool operator<(const SmartContractInfo &other) const { + return hash < other.hash; + } + friend bool operator<(const SmartContractInfo &info, td::Slice hash) { + return info.hash < hash; + } + friend bool operator<(td::Slice hash, const SmartContractInfo &info) { + return hash < info.hash; + } + }; + + CellHashTable contracts_; + + KeyValue &kv() { + return *kv_; + } + friend class TonDbImpl; + + void begin(); + void commit(); + void abort(); + void clear_cache(); + + void end_smartcontract(SmartContractDb smart_contract); +}; + +class TonDbImpl; +using TonDb = std::unique_ptr; +class TonDbImpl { + public: + TonDbImpl(std::unique_ptr kv); + ~TonDbImpl(); + TonDbTransaction begin_transaction(); + void commit_transaction(TonDbTransaction transaction); + void abort_transaction(TonDbTransaction transaction); + void clear_cache(); + static td::Result open(td::Slice path); + std::string stats() const; + + private: + std::shared_ptr kv_; + TonDbTransaction transaction_; +}; +} // namespace vm diff --git a/crypto/vm/debugops.cpp b/crypto/vm/debugops.cpp new file mode 100644 index 00000000..911d6313 --- /dev/null +++ b/crypto/vm/debugops.cpp @@ -0,0 +1,110 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include +#include "vm/debugops.h" +#include "vm/log.h" +#include "vm/opctable.h" +#include "vm/stack.hpp" +#include "vm/continuation.h" +#include "vm/excno.hpp" + +namespace vm { + +bool vm_debug_enabled = true; + +int exec_dummy_debug(VmState* st, int args) { + VM_LOG(st) << "execute DEBUG " << (args & 0xff); + return 0; +} + +// similar to PUSHSLICE instruction in cellops.cpp +int exec_dummy_debug_str(VmState* st, CellSlice& cs, unsigned args, int pfx_bits) { + int data_bits = ((args & 15) + 1) * 8; + if (!cs.have(pfx_bits + data_bits)) { + throw VmError{Excno::inv_opcode, "not enough data bits for a DEBUGSTR instruction"}; + } + cs.advance(pfx_bits); + auto slice = cs.fetch_subslice(data_bits); + VM_LOG(st) << "execute DEBUGSTR " << slice->as_bitslice().to_hex(); + return 0; +} + +std::string dump_dummy_debug_str(CellSlice& cs, unsigned args, int pfx_bits) { + int data_bits = ((args & 15) + 1) * 8; + if (!cs.have(pfx_bits + data_bits)) { + return ""; + } + cs.advance(pfx_bits); + auto slice = cs.fetch_subslice(data_bits); + slice.unique_write().remove_trailing(); + std::ostringstream os; + os << "DEBUGSTR "; + slice->dump_hex(os, 1, false); + return os.str(); +} + +int compute_len_debug_str(const CellSlice& cs, unsigned args, int pfx_bits) { + unsigned bits = pfx_bits + ((args & 15) + 1) * 8; + return cs.have(bits) ? bits : 0; +} + +int exec_dump_stack(VmState* st) { + VM_LOG(st) << "execute DUMPSTK"; + Stack& stack = st->get_stack(); + int d = stack.depth(); + std::cerr << "#DEBUG#: stack(" << d << " values) : "; + if (d > 255) { + std::cerr << "... "; + d = 255; + } + for (int i = d; i > 0; i--) { + std::cerr << stack[i - 1].to_string() << " "; + } + std::cerr << std::endl; + return 0; +} + +int exec_dump_value(VmState* st, unsigned arg) { + arg &= 15; + VM_LOG(st) << "execute DUMP s" << arg; + Stack& stack = st->get_stack(); + if ((int)arg < stack.depth()) { + std::cerr << "#DEBUG#: s" << arg << " = " << stack[arg].to_string() << std::endl; + } else { + std::cerr << "#DEBUG#: s" << arg << " is absent" << std::endl; + } + return 0; +} + +void register_debug_ops(OpcodeTable& cp0) { + using namespace std::placeholders; + if (!vm_debug_enabled) { + cp0.insert(OpcodeInstr::mkfixedrange(0xfe00, 0xfef0, 16, 8, instr::dump_1c_and(0xff, "DEBUG "), exec_dummy_debug)) + .insert(OpcodeInstr::mkext(0xfef, 12, 4, dump_dummy_debug_str, exec_dummy_debug_str, compute_len_debug_str)); + } else { + // NB: all non-redefined opcodes in fe00..feff should be redirected to dummy debug definitions + cp0.insert(OpcodeInstr::mksimple(0xfe00, 16, "DUMPSTK", exec_dump_stack)) + .insert(OpcodeInstr::mkfixedrange(0xfe01, 0xfe20, 16, 8, instr::dump_1c_and(0xff, "DEBUG "), exec_dummy_debug)) + .insert(OpcodeInstr::mkfixed(0xfe2, 12, 4, instr::dump_1sr("DUMP"), exec_dump_value)) + .insert(OpcodeInstr::mkfixedrange(0xfe30, 0xfef0, 16, 8, instr::dump_1c_and(0xff, "DEBUG "), exec_dummy_debug)) + .insert(OpcodeInstr::mkext(0xfef, 12, 4, dump_dummy_debug_str, exec_dummy_debug_str, compute_len_debug_str)); + } +} + +} // namespace vm diff --git a/crypto/vm/debugops.h b/crypto/vm/debugops.h new file mode 100644 index 00000000..7c7b27d0 --- /dev/null +++ b/crypto/vm/debugops.h @@ -0,0 +1,29 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +namespace vm { + +class OpcodeTable; + +extern bool vm_debug_enabled; + +void register_debug_ops(OpcodeTable& cp0); + +} // namespace vm diff --git a/crypto/vm/dict.cpp b/crypto/vm/dict.cpp new file mode 100644 index 00000000..7037e7e4 --- /dev/null +++ b/crypto/vm/dict.cpp @@ -0,0 +1,2647 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "vm/dict.h" +#include "vm/cells.h" +#include "vm/cellslice.h" +#include "vm/stack.hpp" +#include "common/bitstring.h" + +#include "td/utils/bits.h" + +namespace vm { + +/* + * + * DictionaryBase : basic (common) dictionary manipulation + * + */ + +DictionaryBase::DictionaryBase(Ref _root, int _n, bool validate) + : root(std::move(_root)), root_cell(), key_bits(_n), flags(f_root_cached) { + if (validate) { + force_validate(); + } +} + +DictionaryBase::DictionaryBase(const CellSlice& root_cs, int _n, bool validate) + : root(), root_cell(), key_bits(_n), flags(0) { + int f = (int)root_cs.prefetch_ulong(1); + if (f < 0) { + flags |= f_invalid; + } else if (f > 0) { + if (root_cs.size_refs()) { + root_cell = root_cs.prefetch_ref(); + } else { + flags |= f_invalid; + } + } + if (validate) { + force_validate(); + } +} + +DictionaryBase::DictionaryBase(DictAdvance, CellSlice& root_cs, int _n, bool validate) + : root(), root_cell(), key_bits(_n), flags(0) { + int f = (int)root_cs.prefetch_ulong(1); + if (!f) { + root_cs.advance(1); + } else if (f > 0 && root_cs.size_refs()) { + root_cs.advance(1); + root_cell = root_cs.fetch_ref(); + } else { + flags |= f_invalid; + } + if (validate) { + force_validate(); + } +} + +DictionaryBase::DictionaryBase(Ref cell, int _n, bool validate) + : root(), root_cell(std::move(cell)), key_bits(_n), flags(0) { + if (validate) { + force_validate(); + } +} + +DictionaryBase::DictionaryBase(int _n, bool validate) : root(), root_cell(), key_bits(_n), flags(0) { + if (validate) { + force_validate(); + } +} + +DictionaryBase::DictionaryBase(DictNonEmpty, Ref _root, int _n, bool validate) + : root(), root_cell(), key_bits(_n), flags(0) { + if (_root.is_null() || !init_root_for_nonempty(*_root)) { // empty ? + invalidate(); // invalidate + } + if (validate) { + force_validate(); + } +} + +DictionaryBase::DictionaryBase(DictNonEmpty, const CellSlice& _root, int _n, bool validate) + : root(), root_cell(), key_bits(_n), flags(0) { + if (!init_root_for_nonempty(_root)) { + invalidate(); + } + if (validate) { + force_validate(); + } +} + +bool DictionaryBase::init_root_for_nonempty(const CellSlice& cs) { + vm::CellBuilder cb; + return cb.append_cellslice_bool(cs) && cb.finalize_to(root_cell); +} + +Ref DictionaryBase::construct_root_from(const CellSlice& root_node_cs) { + vm::CellBuilder cb; + if (cb.append_cellslice_bool(root_node_cs)) { + return cb.finalize(); + } else { + return {}; + } +} + +void DictionaryBase::force_validate() { + if (!is_valid() && !validate()) { + throw VmError{Excno::dict_err, "invalid dictionary"}; + } +} + +bool DictionaryBase::validate() { + if (is_valid()) { + return true; + } + if (flags & f_invalid) { + return false; + } + if (key_bits < 0 || key_bits > max_key_bits) { + return invalidate(); + } + if (flags & f_root_cached) { + if (root.is_null() || root->size() != 1) { + return invalidate(); + } + bool non_empty = root->prefetch_ulong(1); + if (root->size_refs() != (non_empty ? 1u : 0u)) { + return invalidate(); + } + if (root_cell.not_null()) { + return invalidate(); + } + if (non_empty) { + root_cell = root->prefetch_ref(); + } + } else if (root.not_null()) { + return invalidate(); + } + flags |= f_valid; + return true; +} + +Ref DictionaryBase::get_root() const { + if (!(flags & f_root_cached) && !compute_root()) { + return {}; + } + return root; +} + +Ref DictionaryBase::extract_root() && { + if (!(flags & f_root_cached) && !compute_root()) { + return {}; + } + flags = f_invalid; + return std::move(root); +} + +bool DictionaryBase::append_dict_to_bool(CellBuilder& cb) && { + if (!is_valid()) { + return false; + } + flags = f_invalid; + return cb.store_maybe_ref(std::move(root_cell)); +} + +bool DictionaryBase::append_dict_to_bool(CellBuilder& cb) const& { + return is_valid() && cb.store_maybe_ref(root_cell); +} + +bool DictionaryBase::compute_root() const { + if (!is_valid()) { + return false; + } + if (root_cell.is_null()) { + root = get_empty_dictionary(); + flags |= f_root_cached; + return true; + } + CellBuilder cb; + cb.store_long(1, 1); + cb.store_ref(root_cell); + root = Ref{true, cb.finalize()}; + flags |= f_root_cached; + return true; +} + +Ref DictionaryBase::get_empty_dictionary() { + static Ref empty_dict{new_empty_dictionary()}; + return empty_dict; +} + +Ref DictionaryBase::new_empty_dictionary() { + CellBuilder cb; // Builder + cb.store_long(0, 1); + return Ref{true, cb.finalize()}; +} + +Ref DictionaryFixed::finish_create_leaf(CellBuilder& cb, const CellSlice& value) const { + if (!cb.append_cellslice_bool(value)) { + throw VmError{Excno::dict_err, "cannot store new value into a dictionary leaf cell"}; + } + return cb.finalize(); +} + +Ref DictionaryFixed::finish_create_fork(CellBuilder& cb, Ref c1, Ref c2, int n) const { + assert(n > 0); + if (!(cb.store_ref_bool(std::move(c1)) && cb.store_ref_bool(std::move(c2)))) { + throw VmError{Excno::dict_err, "cannot store branch references into a dictionary fork cell"}; + } + return cb.finalize(); +} + +bool DictionaryFixed::check_fork_raw(Ref cs_ref, int n) const { + if (cs_ref.is_null()) { + return false; + } + Ref c1, c2; + CellSlice& cs = cs_ref.write(); + return cs.fetch_ref_to(c1) && cs.fetch_ref_to(c2) && check_fork(cs, std::move(c1), std::move(c2), n); +} + +/* + * + * Label parser (HmLabel n ~l) for all dictionary types + * + */ + +namespace dict { + +LabelParser::LabelParser(Ref cs, int max_label_len, int auto_validate) : remainder(), l_offs(0), l_same(0) { + if (!parse_label(cs.write(), max_label_len)) { + l_offs = 0; + } else { + s_bits = (l_same ? 0 : l_bits); + remainder = std::move(cs); + } + if (auto_validate) { + if (auto_validate > 2) { + validate_ext(max_label_len); + } else if (auto_validate == 2) { + validate_simple(max_label_len); + } else { + validate(); + } + } +} + +LabelParser::LabelParser(Ref cell, int max_label_len, int auto_validate) : remainder(), l_offs(0), l_same(0) { + Ref cs = load_cell_slice_ref(std::move(cell)); + if (!parse_label(cs.unique_write(), max_label_len)) { + l_offs = 0; + } else { + s_bits = (l_same ? 0 : l_bits); + remainder = std::move(cs); + } + if (auto_validate) { + if (auto_validate > 2) { + validate_ext(max_label_len); + } else if (auto_validate == 2) { + validate_simple(max_label_len); + } else { + validate(); + } + } +} + +bool LabelParser::parse_label(CellSlice& cs, int max_label_len) { + int ltype = (int)cs.prefetch_ulong(2); + // std::cerr << "parse_label of type " << ltype << " and maximal length " << max_label_len << " in "; + // cs.dump_hex(std::cerr, 0, true); + switch (ltype) { + case 0: { + l_bits = 0; + l_offs = 2; + cs.advance(2); + return true; + } + case 1: { + cs.advance(1); + l_bits = cs.count_leading(1); + // std::cerr << "unary-encoded l_bits = " << l_bits << ", have " << cs.size() << std::endl; + if (l_bits > max_label_len || !cs.have(2 * l_bits + 1)) { + return false; + } + l_offs = l_bits + 2; + cs.advance(l_bits + 1); + return true; + } + case 2: { + int len_bits = 32 - td::count_leading_zeroes32(max_label_len); + cs.advance(2); + l_bits = (int)cs.fetch_ulong(len_bits); + if (l_bits < 0 || l_bits > max_label_len) { + return false; + } + l_offs = len_bits + 2; + return cs.have(l_bits); + } + case 3: { + int len_bits = 32 - td::count_leading_zeroes32(max_label_len); + // std::cerr << "len_bits = " << len_bits << ", have " << cs.size() << std::endl; + if (!cs.have(3 + len_bits)) { + return false; + } + l_same = (int)cs.fetch_ulong(3); + l_bits = (int)cs.fetch_ulong(len_bits); + // std::cerr << "l_bits = " << l_bits << ", l_same = " << l_same << std::endl; + if (l_bits < 0 || l_bits > max_label_len) { + return false; + } + l_offs = -1; + return true; + } + default: + return false; + } +} + +void LabelParser::validate() const { + if (!is_valid()) { + throw VmError{Excno::cell_und, "error while parsing a dictionary node label"}; + } +} + +void LabelParser::validate_ext(int n) const { + validate(); + if (l_bits > n) { + throw VmError{Excno::dict_err, "invalid dictionary node"}; + } else if (l_bits < n && (remainder->size() != s_bits || remainder->size_refs() != 2)) { + throw VmError{Excno::dict_err, "invalid dictionary fork node"}; + } +} + +void LabelParser::validate_simple(int n) const { + validate(); + if (l_bits > n) { + throw VmError{Excno::dict_err, "invalid dictionary node"}; + } else if (l_bits < n && (remainder->size() < s_bits || remainder->size_refs() < 2)) { + throw VmError{Excno::dict_err, "invalid dictionary fork node"}; + } +} + +bool LabelParser::is_prefix_of(td::ConstBitPtr key, int len) const { + if (l_bits > len) { + return false; + } else if (!l_same) { + //std::cerr << "key is " << key.to_hex(len) << "; len = " << len << "; label_bits = " << l_bits << "; remainder = "; + //remainder->dump_hex(std::cerr, 0, true); + return remainder->has_prefix(key, l_bits); + } else { + return td::bitstring::bits_memscan(key, l_bits, l_same & 1) == (unsigned)l_bits; + } +} + +bool LabelParser::has_prefix(td::ConstBitPtr key, int len) const { + return len >= 0 && len <= l_bits && common_prefix_len(key, len) == len; +} + +int LabelParser::common_prefix_len(td::ConstBitPtr key, int len) const { + if (!l_same) { + //std::cerr << "key is " << key.to_hex(len) << "; len = " << len << "; label_bits = " << l_bits << "; remainder = "; + //remainder->dump_hex(std::cerr, 0, true); + return remainder->common_prefix_len(key, std::min(l_bits, len)); + } else { + return (int)td::bitstring::bits_memscan(key, std::min(l_bits, len), l_same & 1); + } +} + +int LabelParser::extract_label_to(td::BitPtr to) { + if (!l_same) { + to.copy_from(remainder->data_bits(), l_bits); + remainder.write().advance(l_bits); + } else { + to.fill(l_same & 1, l_bits); + } + return l_bits; +} + +int LabelParser::copy_label_prefix_to(td::BitPtr to, int max_len) const { + if (max_len <= 0) { + return max_len; + } + int sz = std::min(max_len, l_bits); + if (!l_same) { + to.copy_from(remainder->data_bits(), sz); + } else { + to.fill(l_same & 1, sz); + } + return sz; +} + +} // namespace dict + +/* + * + * Usual Dictionary + * + */ + +using dict::LabelParser; + +BitSlice DictionaryFixed::integer_key(td::RefInt256 x, unsigned n, bool sgnd, unsigned char buffer[128], bool quiet) { + if (x.not_null() && x->fits_bits(n, sgnd)) { + if (buffer) { + if (x->export_bits(buffer, 0, n, sgnd)) { + return BitSlice{{}, buffer, 0, n}; + } + } else { + Ref bs{true, n}; + if (x->export_bits(bs.unique_write().reserve_bitslice(n), sgnd)) { + return static_cast(*bs); + } + } + } + if (!quiet) { + throw VmError{Excno::range_chk, "dictionary index out of bounds"}; + } + return {}; +} + +bool DictionaryFixed::integer_key_simple(td::RefInt256 x, unsigned n, bool sgnd, td::BitPtr buffer, bool quiet) { + if (x.not_null() && x->fits_bits(n, sgnd) && x->export_bits(buffer, n, sgnd)) { + return true; + } + if (!quiet) { + throw VmError{Excno::range_chk, "dictionary index out of bounds"}; + } + return false; +} + +Ref Dictionary::extract_value_ref(Ref cs) { + if (cs.is_null()) { + return {}; + } else if (!cs->size() && cs->size_refs() == 1) { + return cs->prefetch_ref(); + } else { + throw VmError{Excno::dict_err, "dictionary value does not consist of exactly one reference"}; + } +} + +Ref DictionaryFixed::lookup(td::ConstBitPtr key, int key_len) { + force_validate(); + if (key_len != get_key_bits() || is_empty()) { + return {}; + } + //std::cerr << "dictionary lookup for key = " << key.to_hex(key_len) << std::endl; + Ref cell = get_root_cell(); + int n = key_len; + while (true) { + LabelParser label{std::move(cell), n, label_mode()}; + if (!label.is_prefix_of(key, n)) { + //std::cerr << "(not a prefix)\n"; + return {}; + } + n -= label.l_bits; + if (n <= 0) { + assert(!n); + label.skip_label(); + return std::move(label.remainder); + } + key += label.l_bits; + bool sw = *key++; + //std::cerr << "key bit at position " << key_bits - n << " equals " << sw << std::endl; + --n; + cell = label.remainder->prefetch_ref(sw); + } +} + +Ref Dictionary::lookup_ref(td::ConstBitPtr key, int key_len) { + return extract_value_ref(lookup(key, key_len)); +} + +bool DictionaryFixed::has_common_prefix(td::ConstBitPtr prefix, int prefix_len) { + force_validate(); + if (is_empty() || prefix_len <= 0) { + return true; + } + if (prefix_len > get_key_bits()) { + return false; + } + LabelParser label{get_root_cell(), get_key_bits(), label_mode()}; + return label.has_prefix(prefix, prefix_len); +} + +int DictionaryFixed::get_common_prefix(td::BitPtr buffer, unsigned buffer_len) { + force_validate(); + if (is_empty()) { + return 0; + } + LabelParser label{get_root_cell(), get_key_bits(), label_mode()}; + return label.copy_label_prefix_to(buffer, (int)buffer_len); +} + +bool DictionaryFixed::key_exists(td::ConstBitPtr key, int key_len) { + return lookup(key, key_len).not_null(); +} + +bool DictionaryFixed::int_key_exists(long long key) { + force_validate(); + int l = get_key_bits(); + if (is_empty() || l > 64) { + return false; + } + if (l < 64) { + long long m = (1LL << (l - 1)); + if (key < -m || key >= m) { + return false; + } + } + td::BitArray<64> a; + a.bits().store_int(key, l); + return key_exists(a.cbits(), l); +} + +bool DictionaryFixed::uint_key_exists(unsigned long long key) { + force_validate(); + int l = get_key_bits(); + if (is_empty() || l > 64) { + return false; + } + if (l < 64 && key >= (1ULL << l)) { + return false; + } + td::BitArray<64> a; + a.bits().store_uint(key, l); + return key_exists(a.cbits(), l); +} + +namespace { + +void append_dict_label_same(CellBuilder& cb, bool same, int len, int max_len) { + int k = 32 - td::count_leading_zeroes32(max_len); + assert(len >= 0 && len <= max_len && max_len <= 1023); + // options: mode '0', requires 2n+2 bits (always for n=0) + // mode '10', requires 2+k+n bits (only for n<=1) + // mode '11', requires 3+k bits (for n>=2, k<2n-1) + if (len > 1 && k < 2 * len - 1) { + // mode '11' + cb.store_long(6 + same, 3).store_long(len, k); + } else if (k < len) { + // mode '10' + cb.store_long(2, 2).store_long(len, k).store_long(-static_cast(same), len); + } else { + // mode '0' + cb.store_long(0, 1).store_long(-2, len + 1).store_long(-static_cast(same), len); + } +} + +void append_dict_label(CellBuilder& cb, td::ConstBitPtr label, int len, int max_len) { + assert(len <= max_len && max_len <= 1023); + if (len > 0 && (int)td::bitstring::bits_memscan(label, len, *label) == len) { + return append_dict_label_same(cb, *label, len, max_len); + } + int k = 32 - td::count_leading_zeroes32(max_len); + // two options: mode '0', requires 2n+2 bits + // mode '10', requires 2+k+n bits + if (k < len) { + cb.store_long(2, 2).store_long(len, k); + } else { + cb.store_long(0, 1).store_long(-2, len + 1); + } + if ((int)cb.remaining_bits() < len) { + throw VmError{Excno::cell_ov, "cannot store a label into a dictionary cell"}; + } + cb.store_bits(label, len); +} + +std::pair, bool> dict_set(Ref dict, td::ConstBitPtr key, int n, + const Dictionary::store_value_func_t& store_val, Dictionary::SetMode mode) { + //std::cerr << "dictionary modification for " << n << "-bit key = " << key.to_hex(n) << std::endl; + if (dict.is_null()) { + // the dictionary is very empty + if (mode == Dictionary::SetMode::Replace) { + return std::make_pair, bool>({}, false); + } + // create an one-element dictionary + CellBuilder cb; + append_dict_label(cb, key, n, n); + if (!store_val(cb)) { + throw VmError{Excno::cell_ov, "cannot store new value into a dictionary cell"}; + } + return std::make_pair(cb.finalize(), true); + } + LabelParser label{std::move(dict), n}; + label.validate(); + int pfx_len = label.common_prefix_len(key, n); + assert(pfx_len >= 0 && pfx_len <= label.l_bits && label.l_bits <= n); + if (pfx_len < label.l_bits) { + // have to insert a new node (fork) inside the current edge + if (mode == Dictionary::SetMode::Replace) { + // key not found, return unchanged dictionary + return std::make_pair(Ref{}, false); + } + // first, create the edge + new leaf cell + int m = n - pfx_len - 1; + CellBuilder cb; + append_dict_label(cb, key + (pfx_len + 1), m, m); + if (!store_val(cb)) { + throw VmError{Excno::cell_ov, "cannot store new value into a dictionary cell"}; + } + Ref c1 = cb.finalize(); // new leaf cell corresponding to `key` + //cb.reset(); + // create the lower portion of the old edge + int t = label.l_bits - pfx_len - 1; + auto cs = std::move(label.remainder); + if (label.l_same) { + append_dict_label_same(cb, label.l_same & 1, t, m); + } else { + cs.write().advance(pfx_len + 1); + append_dict_label(cb, cs->data_bits(), t, m); + cs.unique_write().advance(t); + } + // now cs is the old payload of the edge, either a value or two subdictionary references + if (!cell_builder_add_slice_bool(cb, *cs)) { + throw VmError{Excno::cell_ov, "cannot change label of an old dictionary cell (?)"}; + } + Ref c2 = cb.finalize(); // the other child of the new fork + // cb.reset(); + append_dict_label(cb, key, pfx_len, n); + bool sw_bit = key[pfx_len]; + if (sw_bit) { + c1.swap(c2); + } + cb.store_ref(std::move(c1)).store_ref(std::move(c2)); + return std::make_pair(cb.finalize(), true); + } + if (label.l_bits == n) { + // the edge leads to a leaf node + // this leaf node already contains a value for the key wanted + if (mode == Dictionary::SetMode::Add) { + // return unchanged dictionary + return std::make_pair(Ref{}, false); + } + // replace the value of the only element of the dictionary + CellBuilder cb; + append_dict_label(cb, key, n, n); + if (!store_val(cb)) { + throw VmError{Excno::cell_ov, "cannot store new value into a dictionary cell"}; + } + return std::make_pair(cb.finalize(), true); + } + // main case: the edge leads to a fork, have to insert new value either in the right or in the left subtree + auto c1 = label.remainder->prefetch_ref(0); + auto c2 = label.remainder->prefetch_ref(1); + label.remainder.clear(); + if (key[label.l_bits]) { + // insert key into the right child (c2) + auto res = dict_set(std::move(c2), key + (label.l_bits + 1), n - label.l_bits - 1, store_val, mode); + if (!res.second) { + // return unchanged dictionary + return std::make_pair(Ref{}, false); + } + c2 = std::move(res.first); + } else { + // insert key into the left child (c1) + auto res = dict_set(std::move(c1), key + (label.l_bits + 1), n - label.l_bits - 1, store_val, mode); + if (!res.second) { + // return unchanged dictionary + return std::make_pair(Ref{}, false); + } + c1 = std::move(res.first); + } + // create a new label with the same content + CellBuilder cb; + append_dict_label(cb, key, label.l_bits, n); + cb.store_ref(std::move(c1)).store_ref(std::move(c2)); + return std::make_pair(cb.finalize(), true); +} + +std::tuple, Ref, bool> dict_lookup_set(Ref dict, td::ConstBitPtr key, int n, + const Dictionary::store_value_func_t& store_val, + Dictionary::SetMode mode) { + //std::cerr << "dictionary lookup/modification for " << n << "-bit key = " << key.to_hex(n) << std::endl; + if (dict.is_null()) { + // the dictionary is very empty + if (mode == Dictionary::SetMode::Replace) { + return std::make_tuple, Ref, bool>({}, {}, false); + } + // create an one-element dictionary + CellBuilder cb; + append_dict_label(cb, key, n, n); + if (!store_val(cb)) { + throw VmError{Excno::cell_ov, "cannot store new value into a dictionary cell"}; + } + return std::make_tuple, Ref, bool>({}, cb.finalize(), true); + } + LabelParser label{std::move(dict), n}; + int pfx_len = label.common_prefix_len(key, n); + assert(pfx_len >= 0 && pfx_len <= label.l_bits && label.l_bits <= n); + if (pfx_len < label.l_bits) { + // have to insert a new node (fork) inside the current edge + if (mode == Dictionary::SetMode::Replace) { + // key not found, return unchanged dictionary + return std::make_tuple, Ref, bool>({}, {}, false); + } + // first, create the edge + new leaf cell + int m = n - pfx_len - 1; + CellBuilder cb; + append_dict_label(cb, key + (pfx_len + 1), m, m); + if (!store_val(cb)) { + throw VmError{Excno::cell_ov, "cannot store new value into a dictionary cell"}; + } + Ref c1 = cb.finalize(); // new leaf cell corresponding to `key` + //cb.reset(); + // create the lower portion of the old edge + int t = label.l_bits - pfx_len - 1; + auto cs = std::move(label.remainder); + if (label.l_same) { + append_dict_label_same(cb, label.l_same & 1, t, m); + } else { + cs.write().advance(pfx_len + 1); + append_dict_label(cb, cs->data_bits(), t, m); + cs.unique_write().fetch_bits(t); + } + // now cs is the old payload of the edge, either a value or two subdictionary references + if (!cell_builder_add_slice_bool(cb, *cs)) { + throw VmError{Excno::cell_ov, "cannot change label of an old dictionary cell (?)"}; + } + Ref c2 = cb.finalize(); // the other child of the new fork + //cb.reset(); + append_dict_label(cb, key, pfx_len, n); + bool sw_bit = key[pfx_len]; + if (sw_bit) { + c1.swap(c2); + } + cb.store_ref(std::move(c1)).store_ref(std::move(c2)); + return std::make_tuple, Ref, bool>({}, cb.finalize(), true); + } + if (label.l_bits == n) { + // the edge leads to a leaf node + // this leaf node already contains a value for the key wanted + auto old_val = std::move(label.remainder); + old_val.write().advance(label.s_bits); + if (mode == Dictionary::SetMode::Add) { + // return unchanged dictionary + return std::make_tuple, Ref, bool>(std::move(old_val), {}, false); + } + // replace the value of the only element of the dictionary + CellBuilder cb; + append_dict_label(cb, key, n, n); + if (!store_val(cb)) { + throw VmError{Excno::cell_ov, "cannot store new value into a dictionary cell"}; + } + return std::make_tuple(std::move(old_val), cb.finalize(), true); + } + // main case: the edge leads to a fork, have to insert new value either in the right or in the left subtree + auto c1 = label.remainder->prefetch_ref(0); + auto c2 = label.remainder->prefetch_ref(1); + Ref old_val; + label.remainder.clear(); + if (key[label.l_bits]) { + // insert key into the right child (c2) + auto res = dict_lookup_set(std::move(c2), key + (label.l_bits + 1), n - label.l_bits - 1, store_val, mode); + old_val = std::get>(res); + if (!std::get(res)) { + // return unchanged dictionary + return std::make_tuple, Ref, bool>(std::move(old_val), {}, false); + } + c2 = std::get>(std::move(res)); + } else { + // insert key into the left child (c1) + auto res = dict_lookup_set(std::move(c1), key + (label.l_bits + 1), n - label.l_bits - 1, store_val, mode); + old_val = std::get>(res); + if (!std::get(res)) { + // return unchanged dictionary + return std::make_tuple(std::move(old_val), Ref{}, false); + } + c1 = std::get>(std::move(res)); + } + // create a new label with the same content + CellBuilder cb; + append_dict_label(cb, key, label.l_bits, n); + cb.store_ref(std::move(c1)).store_ref(std::move(c2)); + return std::make_tuple, Ref, bool>(std::move(old_val), cb.finalize(), true); +} + +std::pair, bool> pfx_dict_set(Ref dict, td::ConstBitPtr key, int m, int n, + const PrefixDictionary::store_value_func_t& store_val, + Dictionary::SetMode mode) { + std::cerr << "up to " << n << "-bit prefix code dictionary modification for " << m << "-bit key = " << key.to_hex(m) + << std::endl; + if (m > n) { + return std::make_pair(Ref{}, false); + } + if (dict.is_null()) { + // the dictionary is very empty + if (mode == Dictionary::SetMode::Replace) { + return std::make_pair(Ref{}, false); + } + // create an one-element dictionary + CellBuilder cb; + append_dict_label(cb, key, m, n); + cb.store_long(0, 1); + if (!store_val(cb)) { + throw VmError{Excno::cell_ov, "cannot store new value into a dictionary cell"}; + } + return std::make_pair(cb.finalize(), true); + } + LabelParser label{std::move(dict), n, 1}; + int l = label.common_prefix_len(key, m); + assert(l >= 0 && l <= label.l_bits && label.l_bits <= n && l <= m && m <= n); + if (l < label.l_bits) { + // have to insert a new node (fork) inside the current edge + if (l == m || mode == Dictionary::SetMode::Replace) { + // key not found, return unchanged dictionary + return std::make_pair(Ref{}, false); + } + // first, create the edge + new leaf cell + int q = l + 1; + CellBuilder cb; + append_dict_label(cb, key + q, m - q, n - q); + cb.store_long(0, 1); + if (!store_val(cb)) { + throw VmError{Excno::cell_ov, "cannot store new value into a prefix dictionary cell"}; + } + Ref c1 = cb.finalize(); // new leaf cell corresponding to `key` + // cb.reset(); // contained in finalize() + // create the lower portion of the old edge + int t = label.l_bits - q; + auto cs = std::move(label.remainder); + if (label.l_same) { + append_dict_label_same(cb, label.l_same & 1, t, n - q); + } else { + cs.write().advance(l + 1); + append_dict_label(cb, cs->data_bits(), t, n - q); + cs.unique_write().advance(t); + } + // now cs is the old payload of the edge, either a value or two subdictionary references + if (!cell_builder_add_slice_bool(cb, *cs)) { + throw VmError{Excno::cell_ov, "cannot change label of an old dictionary cell (?)"}; + } + Ref c2 = cb.finalize(); // the other child of the new fork + //cb.reset(); + append_dict_label(cb, key, l, n); + bool sw_bit = key[l]; + if (sw_bit) { + c1.swap(c2); + } + cb.store_long(1, 1).store_ref(c1).store_ref(c2); + return std::make_pair, bool>(cb.finalize(), true); + } + assert(label.l_bits == l); + label.skip_label(); + if (!label.remainder->have(1)) { + throw VmError{Excno::dict_err, "no node constructor in a prefix code dictionary"}; + } + if (!label.remainder.unique_write().fetch_ulong(1)) { + // the edge leads to a leaf node + if (l != m || mode == Dictionary::SetMode::Add) { + // return unchanged dictionary + return std::make_pair, bool>({}, false); + } + // this leaf node already contains a value for the key wanted + // replace the value of the only element of the dictionary + CellBuilder cb; + append_dict_label(cb, key, m, n); + cb.store_long(0, 1); + if (!store_val(cb)) { + throw VmError{Excno::cell_ov, "cannot store new value into a dictionary cell"}; + } + return std::make_pair, bool>(cb.finalize(), true); + } + // main case: the edge leads to a fork, have to insert new value either in the right or in the left subtree + if (label.remainder->size() || label.remainder->size_refs() != 2) { + throw VmError{Excno::dict_err, "invalid fork node in a prefix code dictionary"}; + } + if (m == l) { + // cannot insert a value into a fork + return std::make_pair(Ref{}, false); + } + auto c1 = label.remainder->prefetch_ref(0); + auto c2 = label.remainder->prefetch_ref(1); + label.remainder.clear(); + if (key[l++]) { + // insert key into the right child (c2) + auto res = pfx_dict_set(std::move(c2), key + l, m - l, n - l, store_val, mode); + if (!res.second) { + // return unchanged dictionary + return std::make_pair(Ref{}, false); + } + c2 = std::move(res.first); + } else { + // insert key into the left child (c1) + auto res = pfx_dict_set(std::move(c1), key + l, m - l, n - l, store_val, mode); + if (!res.second) { + // return unchanged dictionary + return std::make_pair(Ref{}, false); + } + c1 = std::move(res.first); + } + // create a new label with the same content + CellBuilder cb; + append_dict_label(cb, key, l - 1, n); + cb.store_long(1, 1).store_ref(std::move(c1)).store_ref(std::move(c2)); + return std::make_pair(cb.finalize(), true); +} + +std::pair, Ref> pfx_dict_lookup_delete(Ref dict, td::ConstBitPtr key, int m, int n) { + //std::cerr << "up to " << n << "-bit prefix dictionary delete for " << m << "-bit key = " << key.to_hex(m) << std::endl; + if (dict.is_null()) { + // the dictionary is very empty + return std::make_pair(Ref{}, Ref{}); + } + LabelParser label{std::move(dict), n, 1}; + int l = label.common_prefix_len(key, m); + assert(l >= 0 && l <= label.l_bits && l <= m && m <= n && label.l_bits <= n); + if (l < label.l_bits) { + // key not found + return std::make_pair(Ref{}, Ref{}); + } + assert(label.l_bits == l); + label.skip_label(); + if (!label.remainder->have(1)) { + throw VmError{Excno::dict_err, "no node constructor in a prefix code dictionary"}; + } + if (!label.remainder.unique_write().fetch_ulong(1)) { + // the edge leads to a leaf node + if (l < m) { + // key not found + return std::make_pair(Ref{}, Ref{}); + } + // this leaf node contains the value for the key wanted + return std::make_pair(std::move(label.remainder), Ref{}); + } + // main case: the edge leads to a fork, have to delete the key either from the right or from the left subtree + if (label.remainder->size() || label.remainder->size_refs() != 2) { + throw VmError{Excno::dict_err, "invalid fork node in a prefix code dictionary"}; + } + if (l == m) { + // the fork itself cannot correspond to a key, key not found + return std::make_pair(Ref{}, Ref{}); + } + auto c1 = label.remainder->prefetch_ref(0); + auto c2 = label.remainder->prefetch_ref(1); + Ref old_val; + label.remainder.clear(); + bool sw_bit = key[l++]; + if (sw_bit) { + // delete key from the right child (c2) + auto res = pfx_dict_lookup_delete(std::move(c2), key + l, m - l, n - l); + if (res.first.is_null()) { + // return unchanged dictionary + return std::make_pair(Ref{}, Ref{}); + } + old_val = std::move(res.first); + c2 = std::move(res.second); + } else { + // delete key from the left child (c1) + auto res = pfx_dict_lookup_delete(std::move(c1), key + l, m - l, n - l); + if (res.first.is_null()) { + // return unchanged dictionary + return std::make_pair(Ref{}, Ref{}); + } + old_val = std::move(res.first); + c1 = std::move(res.second); + } + if (c1.not_null() && c2.not_null()) { + // create a new label with the same content leading to a fork with modified children + CellBuilder cb; + append_dict_label(cb, key, label.l_bits, n); + cb.store_long(1, 1).store_ref(std::move(c1)).store_ref(std::move(c2)); + return std::make_pair(std::move(old_val), cb.finalize()); + } + // have to merge current edge with the edge leading to c1 or c2 + if (!sw_bit) { + c1.swap(c2); + } + assert(c1.not_null() && c2.is_null()); + unsigned char buffer[Dictionary::max_key_bytes]; + td::BitPtr bw{buffer}; + bw.concat(key, label.l_bits); + bw.concat_same(!sw_bit, 1); + LabelParser label2{std::move(c1), n - l, 1}; + bw += label2.extract_label_to(bw); + assert(bw.offs >= 0 && bw.offs <= Dictionary::max_key_bits); + CellBuilder cb; + append_dict_label(cb, td::ConstBitPtr{buffer}, bw.offs, n); + if (!cell_builder_add_slice_bool(cb, *label2.remainder)) { + throw VmError{Excno::cell_ov, "cannot change label of an old prefix code dictionary cell while merging edges"}; + } + label2.remainder.clear(); + return std::make_pair(std::move(old_val), cb.finalize()); +} + +Ref dict_map(Ref dict, td::BitPtr key_buffer, int n, int total_key_len, + const Dictionary::map_func_t& map_func) { + if (dict.is_null()) { + // dictionary is empty + return dict; + } + LabelParser label{std::move(dict), n}; + int l = label.l_bits; + label.extract_label_to(key_buffer); + if (l == n) { + // leaf node, value left in label.remainder + CellBuilder cb; + append_dict_label(cb, key_buffer, l, n); + if (!map_func(cb, std::move(label.remainder), key_buffer + n - total_key_len, total_key_len)) { + return {}; // leaf to be omitted from the result altogether + } + return cb.finalize(); + } + assert(l >= 0 && l < n); + // a fork with two children, c1 and c2 + auto c1 = label.remainder->prefetch_ref(0); + auto c2 = label.remainder->prefetch_ref(1); + key_buffer += l + 1; + key_buffer[-1] = 0; + // recursive map applied to both children + c1 = dict_map(std::move(c1), key_buffer, n - l - 1, total_key_len, map_func); + key_buffer[-1] = 1; + c2 = dict_map(std::move(c2), key_buffer, n - l - 1, total_key_len, map_func); + if (c1.is_null() && c2.is_null()) { + return {}; // both children have become empty + } + if (c1.is_null() || c2.is_null()) { + if (c1.is_null()) { + c1 = std::move(c2); + // notice that the label of c2 is still in key_buffer + } else { + // recover label of c1 + key_buffer[-1] = 0; + } + // one of children is empty, have to combine current edge with the root edge of c1 + LabelParser label1{std::move(c1), n - l - 1}; + label1.extract_label_to(key_buffer); + CellBuilder cb; + key_buffer -= l + 1; + // store combined label for the new edge + append_dict_label(cb, key_buffer, l + 1 + label1.l_bits, n); + // store payload + if (!cell_builder_add_slice_bool(cb, *label1.remainder)) { + throw VmError{Excno::cell_ov, "cannot change label of an old dictionary cell while merging edges"}; + } + return cb.finalize(); + } + // main case: both children c1 and c2 remain non-empty + key_buffer -= l + 1; + CellBuilder cb; + append_dict_label(cb, key_buffer, l, n); + return cb.store_ref(std::move(c1)).store_ref(std::move(c2)).finalize(); +} + +} // namespace + +bool Dictionary::set_gen(td::ConstBitPtr key, int key_len, const std::function& store_val, + SetMode mode) { + force_validate(); + if (key_len != get_key_bits()) { + return false; + } + auto res = dict_set(get_root_cell(), key, key_len, store_val, mode); + if (res.second) { + set_root_cell(std::move(res.first)); + } + return res.second; +} + +bool Dictionary::set(td::ConstBitPtr key, int key_len, Ref value, SetMode mode) { + return set_gen(key, key_len, [value](CellBuilder& cb) { return cell_builder_add_slice_bool(cb, *value); }, mode); +} + +bool Dictionary::set_ref(td::ConstBitPtr key, int key_len, Ref val_ref, SetMode mode) { + return set_gen(key, key_len, [val_ref](CellBuilder& cb) { return cb.store_ref_bool(val_ref); }, mode); +} + +bool Dictionary::set_builder(td::ConstBitPtr key, int key_len, Ref val_b, SetMode mode) { + return set_gen(key, key_len, [val_b](CellBuilder& cb) { return cb.append_builder_bool(val_b); }, mode); +} + +bool Dictionary::set_builder(td::ConstBitPtr key, int key_len, const CellBuilder& val_b, SetMode mode) { + return set_gen(key, key_len, [&val_b](CellBuilder& cb) { return cb.append_builder_bool(val_b); }, mode); +} + +Ref Dictionary::lookup_set_gen(td::ConstBitPtr key, int key_len, const store_value_func_t& store_val, + SetMode mode) { + force_validate(); + if (key_len != get_key_bits()) { + return {}; + } + auto res = dict_lookup_set(get_root_cell(), key, key_len, store_val, mode); + if (std::get(res)) { + set_root_cell(std::get>(res)); + } + return std::get>(std::move(res)); +} + +Ref Dictionary::lookup_set(td::ConstBitPtr key, int key_len, Ref value, SetMode mode) { + return lookup_set_gen(key, key_len, [value](CellBuilder& cb) { return cell_builder_add_slice_bool(cb, *value); }, + mode); +} + +Ref Dictionary::lookup_set_ref(td::ConstBitPtr key, int key_len, Ref val_ref, SetMode mode) { + return extract_value_ref( + lookup_set_gen(key, key_len, [val_ref](CellBuilder& cb) { return cb.store_ref_bool(val_ref); }, mode)); +} + +Ref Dictionary::lookup_set_builder(td::ConstBitPtr key, int key_len, Ref val_b, SetMode mode) { + return lookup_set_gen(key, key_len, [val_b](CellBuilder& cb) { return cb.append_builder_bool(val_b); }, mode); +} + +std::pair, Ref> DictionaryFixed::dict_lookup_delete(Ref dict, td::ConstBitPtr key, + int n) const { + // std::cerr << "dictionary delete for " << n << "-bit key = " << key.to_hex(n) << std::endl; + if (dict.is_null()) { + // the dictionary is very empty + return std::make_pair(Ref{}, Ref{}); + } + LabelParser label{std::move(dict), n, label_mode()}; + int pfx_len = label.common_prefix_len(key, n); + assert(pfx_len >= 0 && pfx_len <= label.l_bits && label.l_bits <= n); + if (pfx_len < label.l_bits) { + // key not found + return std::make_pair(Ref{}, Ref{}); + } + if (label.l_bits == n) { + // the edge leads to a leaf node + // this leaf node contains the value for the key wanted + label.skip_label(); + return std::make_pair(std::move(label.remainder), Ref{}); + } + // main case: the edge leads to a fork, have to delete the key either from the right or from the left subtree + auto c1 = label.remainder->prefetch_ref(0); + auto c2 = label.remainder->prefetch_ref(1); + Ref old_val; + label.remainder.clear(); + bool sw_bit = key[label.l_bits]; + if (sw_bit) { + // delete key from the right child (c2) + auto res = dict_lookup_delete(std::move(c2), key + (label.l_bits + 1), n - label.l_bits - 1); + if (res.first.is_null()) { + // return unchanged dictionary + return std::make_pair(Ref{}, Ref{}); + } + old_val = std::move(res.first); + c2 = std::move(res.second); + } else { + // delete key from the left child (c1) + auto res = dict_lookup_delete(std::move(c1), key + (label.l_bits + 1), n - label.l_bits - 1); + if (res.first.is_null()) { + // return unchanged dictionary + return std::make_pair(Ref{}, Ref{}); + } + old_val = std::move(res.first); + c1 = std::move(res.second); + } + if (c1.not_null() && c2.not_null()) { + // create a new label with the same content leading to a fork with modified children + CellBuilder cb; + append_dict_label(cb, key, label.l_bits, n); + return std::make_pair(std::move(old_val), finish_create_fork(cb, std::move(c1), std::move(c2), n - label.l_bits)); + } + // have to merge current edge with the edge leading to c1 or c2 + if (!sw_bit) { + c1.swap(c2); + } + assert(c1.not_null() && c2.is_null()); + unsigned char buffer[Dictionary::max_key_bytes]; + td::BitPtr bw{buffer}; + bw.concat(key, label.l_bits); + bw.concat_same(!sw_bit, 1); + LabelParser label2{std::move(c1), n - label.l_bits - 1, label_mode()}; + bw += label2.extract_label_to(bw); + assert(bw.offs >= 0 && bw.offs <= Dictionary::max_key_bits); + CellBuilder cb; + append_dict_label(cb, td::ConstBitPtr{buffer}, bw.offs, n); + if (!cell_builder_add_slice_bool(cb, *label2.remainder)) { + throw VmError{Excno::cell_ov, "cannot change label of an old dictionary cell while merging edges"}; + } + label2.remainder.clear(); + return std::make_pair(std::move(old_val), cb.finalize()); +} + +Ref DictionaryFixed::lookup_delete(td::ConstBitPtr key, int key_len) { + force_validate(); + if (key_len != get_key_bits()) { + return {}; + } + auto res = dict_lookup_delete(get_root_cell(), key, key_len); + if (res.first.not_null()) { + set_root_cell(std::move(res.second)); + } + return std::move(res.first); +} + +Ref Dictionary::lookup_delete_ref(td::ConstBitPtr key, int key_len) { + return extract_value_ref(lookup_delete(key, key_len)); +} + +Ref DictionaryFixed::dict_lookup_minmax(Ref dict, td::BitPtr key_buffer, int n, int mode) const { + if (dict.is_null()) { + return {}; + } + while (1) { + LabelParser label{std::move(dict), n, label_mode()}; + int l = label.extract_label_to(key_buffer); + assert(l >= 0 && l <= n); + key_buffer += l; + n -= l; + if (!n) { + return std::move(label.remainder); + } + if (l) { + mode >>= 1; + } + bool bit = mode & 1; + dict = label.remainder->prefetch_ref(bit); + *key_buffer++ = bit; + --n; + mode >>= 1; + } +} + +Ref DictionaryFixed::dict_lookup_nearest(Ref dict, td::BitPtr key_buffer, int n, bool allow_eq, + int mode) const { + if (dict.is_null()) { + return {}; + } + LabelParser label{dict, n, label_mode()}; + int pfx_len = label.common_prefix_len(key_buffer, n); + assert(pfx_len >= 0 && pfx_len <= label.l_bits && label.l_bits <= n); + if (pfx_len < label.l_bits) { + if (key_buffer[pfx_len] == ((mode >> static_cast(pfx_len != 0)) & 1)) { + return {}; + } else { + return dict_lookup_minmax(std::move(dict), key_buffer, n, ~mode); + } + } + dict.clear(); + if (label.l_bits) { + mode >>= 1; + } + key_buffer += label.l_bits; + n -= label.l_bits; + if (!n) { + if (!allow_eq) { + return {}; + } + label.skip_label(); + return std::move(label.remainder); + } + bool bit = *key_buffer++; + auto res = dict_lookup_nearest(label.remainder->prefetch_ref(bit), key_buffer, n - 1, allow_eq, mode >> 1); + if (res.not_null() || bit == (mode & 1)) { + return res; + } + key_buffer[-1] = mode & 1; + dict = label.remainder->prefetch_ref(mode & 1); + label.remainder.clear(); + return dict_lookup_minmax(std::move(dict), key_buffer, n - 1, ~mode >> 1); +} + +Ref DictionaryFixed::lookup_nearest_key(td::BitPtr key_buffer, int key_len, bool fetch_next, bool allow_eq, + bool invert_first) { + force_validate(); + if (key_len != get_key_bits()) { + return {}; + } + return dict_lookup_nearest(get_root_cell(), key_buffer, key_len, allow_eq, + (-static_cast(fetch_next)) ^ static_cast(invert_first)); +} + +Ref DictionaryFixed::get_minmax_key(td::BitPtr key_buffer, int key_len, bool fetch_max, bool invert_first) { + force_validate(); + if (key_len != get_key_bits()) { + return {}; + } + return dict_lookup_minmax(get_root_cell(), key_buffer, key_len, + (-static_cast(fetch_max)) ^ static_cast(invert_first)); +} + +Ref Dictionary::get_minmax_key_ref(td::BitPtr key_buffer, int key_len, bool fetch_max, bool invert_first) { + return extract_value_ref(get_minmax_key(key_buffer, key_len, fetch_max, invert_first)); +} + +Ref DictionaryFixed::extract_minmax_key(td::BitPtr key_buffer, int key_len, bool fetch_max, + bool invert_first) { + force_validate(); + if (key_len != get_key_bits()) { + return {}; + } + auto val = dict_lookup_minmax(get_root_cell(), key_buffer, key_len, -(fetch_max ? 1 : 0) ^ (invert_first ? 1 : 0)); + if (val.is_null()) { + return {}; + } + auto res = dict_lookup_delete(get_root_cell(), key_buffer, key_len); + assert(res.first.not_null()); + set_root_cell(std::move(res.second)); + return val; +} + +Ref Dictionary::extract_minmax_key_ref(td::BitPtr key_buffer, int key_len, bool fetch_max, bool invert_first) { + return extract_value_ref(extract_minmax_key(key_buffer, key_len, fetch_max, invert_first)); +} + +std::pair, bool> DictionaryFixed::extract_prefix_subdict_internal(Ref dict, td::ConstBitPtr prefix, + int prefix_len, bool remove_prefix) const { + if (is_empty() || prefix_len <= 0) { + return {{}, false}; // unchanged + } + if (prefix_len > get_key_bits()) { + return {{}, true}; // empty dict + } + int n = get_key_bits(), m = 0; + while (true) { + LabelParser label{std::move(dict), n - m, label_mode()}; + int l = std::min(prefix_len - m, label.l_bits); + if (label.common_prefix_len(prefix + m, l) < l) { + return {{}, true}; // empty dict + } + if (m + label.l_bits < prefix_len) { + m += label.l_bits; + dict = label.remainder->prefetch_ref(prefix[m++]); + continue; + } + // end, have consumed all of prefix + vm::CellBuilder cb; + if (!remove_prefix) { + if (!m) { + // dictionary unchanged: all keys already begin with prefix + return {{}, false}; + } + // concatenate prefix with a suffix of the label + assert(m <= prefix_len); + unsigned char buffer[max_key_bytes]; + auto p = td::BitPtr{buffer}; + p.copy_from(prefix, m); + label.extract_label_to(p + m); + append_dict_label(cb, p, m + label.l_bits, key_bits); + } else if (!label.l_same) { + m += label.l_bits - prefix_len; // leave that many last bits of the label + append_dict_label(cb, label.bits_end() - m, m, key_bits - prefix_len); + label.skip_label(); + } else { + m += label.l_bits - prefix_len; // leave that many last bits of the label + append_dict_label_same(cb, label.l_same & 1, m, key_bits - prefix_len); + } + if (!cb.append_cellslice_bool(*label.remainder)) { + throw VmError{Excno::cell_ov, "cannot create new dictionary root while constructing prefix subdictionary"}; + } + return {Ref{cb.finalize()}, true}; + } +} + +bool DictionaryFixed::cut_prefix_subdict(td::ConstBitPtr prefix, int prefix_len, bool remove_prefix) { + force_validate(); + if (prefix_len < 0) { + return false; + } + if (prefix_len > key_bits && remove_prefix) { + return false; + } + auto res = extract_prefix_subdict_internal(get_root_cell(), prefix, prefix_len, remove_prefix); + if (remove_prefix) { + key_bits -= prefix_len; + } + if (res.second) { + set_root_cell(std::move(res.first)); + } + return true; +} + +Ref DictionaryFixed::extract_prefix_subdict_root(td::ConstBitPtr prefix, int prefix_len, bool remove_prefix) { + force_validate(); + auto res = extract_prefix_subdict_internal(get_root_cell(), prefix, prefix_len, remove_prefix); + return res.second ? res.first : root_cell; +} + +std::pair, int> DictionaryFixed::dict_filter(Ref dict, td::BitPtr key, int n, + const DictionaryFixed::filter_func_t& check_leaf) const { + // std::cerr << "dictionary filter for " << n << "-bit key = " << (key + n - key_bits).to_hex(key_bits - n) + // << std::endl; + if (dict.is_null()) { + // empty dictionary, return unchanged + return {{}, 0}; + } + LabelParser label{std::move(dict), n, label_mode()}; + assert(label.l_bits >= 0 && label.l_bits <= n); + label.extract_label_to(key); + key += label.l_bits; + if (label.l_bits == n) { + // leaf + int res = check_leaf(label.remainder.write(), key - key_bits, key_bits); + return {{}, res < 0 ? res : !res}; + } + // fork, process left and right subtrees + ++key; + key[-1] = false; + int delta = label.l_bits + 1; + n -= delta; + auto left_res = dict_filter(label.remainder->prefetch_ref(0), key, n, check_leaf); + if (left_res.second < 0) { + return left_res; + } + key[-1] = true; + auto right_res = dict_filter(label.remainder->prefetch_ref(1), key, n, check_leaf); + if ((left_res.second | right_res.second) <= 0) { + // error in right, or both left and right unchanged + return right_res; + } + auto left = left_res.second ? std::move(left_res.first) : label.remainder->prefetch_ref(0); + auto right = right_res.second ? std::move(right_res.first) : label.remainder->prefetch_ref(1); + auto changes = left_res.second + right_res.second; + label.clear(); + if (left.is_null()) { + if (right.is_null()) { + // both branches are empty => the result is an empty tree + return {{}, changes}; + } + std::swap(left, right); + } else if (right.is_null()) { + key[-1] = false; + } else { + // both new branches are non-empty => create new fork + CellBuilder cb; + append_dict_label(cb, key - delta, label.l_bits, n + delta); + return {finish_create_fork(cb, std::move(left), std::move(right), n + 1), changes}; + } + // only one child (in `left`) remains, collapse an edge + // NB: similar to code in lookup_delete() + assert(left.not_null() && right.is_null()); + LabelParser label2{std::move(left), n, label_mode()}; + label2.extract_label_to(key); + CellBuilder cb; + append_dict_label(cb, key - delta, delta + label2.l_bits, n + delta); + if (!cell_builder_add_slice_bool(cb, *label2.remainder)) { + throw VmError{Excno::cell_ov, "cannot change label of an old dictionary cell while merging edges"}; + } + label2.remainder.clear(); + return {cb.finalize(), changes}; +} + +int DictionaryFixed::filter(DictionaryFixed::filter_func_t check_leaf) { + force_validate(); + unsigned char buffer[DictionaryFixed::max_key_bytes]; + auto res = dict_filter(get_root_cell(), td::BitPtr{buffer}, key_bits, check_leaf); + if (res.second > 0) { + // std::cerr << "after filter (" << res.second << " changes): new augmented dictionary root is:\n"; + // vm::load_cell_slice(res.first).print_rec(std::cerr); + set_root_cell(std::move(res.first)); + } + return res.second; +} + +void Dictionary::map(const map_func_t& map_func) { + force_validate(); + int key_len = get_key_bits(); + unsigned char key_buffer[max_key_bytes]; + auto res = dict_map(get_root_cell(), td::BitPtr{key_buffer}, key_len, key_len, map_func); + set_root_cell(std::move(res)); +} + +void Dictionary::map(const simple_map_func_t& simple_map_func) { + using namespace std::placeholders; + map_func_t map_func = std::bind(simple_map_func, _1, _2); + map(map_func); +} + +// mode: +1 = forbid empty dict1 with non-empty dict2 +// +2 = forbid empty dict2 with non-empty dict1 +Ref DictionaryFixed::dict_combine_with(Ref dict1, Ref dict2, td::BitPtr key_buffer, int n, + int total_key_len, const DictionaryFixed::combine_func_t& combine_func, + int mode, int skip1, int skip2) const { + if (dict1.is_null()) { + assert(!skip2); + if ((mode & 1) && dict2.is_null()) { + throw CombineError{}; + } + return dict2; + } else if (dict2.is_null()) { + assert(!skip1); + if ((mode & 2)) { + throw CombineError{}; + } + return dict1; + } + // both dictionaries non-empty + // skip1: remove that much first bits from all keys in dictionary dict1 (its keys are actually n + skip1 bits long) + // skip2: similar for dict2 + // resulting dictionary will have n-bit keys + LabelParser label1{dict1, n + skip1, label_mode()}, label2{dict2, n + skip2, label_mode()}; + int l1 = label1.l_bits - skip1, l2 = label2.l_bits - skip2; + assert(l1 >= 0 && l2 >= 0); + assert(!skip1 || label1.common_prefix_len(key_buffer - skip1, skip1) == skip1); + assert(!skip2 || label2.common_prefix_len(key_buffer - skip2, skip2) == skip2); + label1.extract_label_to(key_buffer - skip1); + int c = label2.common_prefix_len(key_buffer - skip2, skip2 + l1) - skip2; + assert(c >= 0 && c <= l1 && c <= l2); + if (c < l1 && c < l2) { + // the two dictionaries have disjoint keys + dict1.clear(); + dict2.clear(); + if ((mode & 3)) { + throw CombineError{}; + } + CellBuilder cb; + append_dict_label(cb, key_buffer + c + 1, l1 - c - 1, n - c - 1); + if (!cell_builder_add_slice_bool(cb, *label1.remainder)) { + throw VmError{Excno::cell_ov, "cannot prune label of an old dictionary cell while merging dictionaries"}; + } + label1.remainder.clear(); + dict1 = cb.finalize(); + // cb.reset(); // included into finalize(); + // now dict1 has been "pruned" -- first skip1+c+1 bits removed from its root egde label + label2.extract_label_to(key_buffer - skip2); + append_dict_label(cb, key_buffer + c + 1, l2 - c - 1, n - c - 1); + if (!cell_builder_add_slice_bool(cb, *label2.remainder)) { + throw VmError{Excno::cell_ov, "cannot change label of an old dictionary cell while merging edges"}; + } + label2.remainder.clear(); + dict2 = cb.finalize(); + // now dict2 has also been pruned + if (!key_buffer[c]) { + std::swap(dict1, dict2); + } + // put dict1 into the left tree (with smaller labels), dict2 into the right tree + append_dict_label(cb, key_buffer, c, n); + return finish_create_fork(cb, std::move(dict1), std::move(dict2), n - c); + } + if (c == l1 && c == l2) { + // funny enough, the non-skipped parts of labels of l1 and l2 match + dict1.clear(); + dict2.clear(); + label2.skip_label(); + CellBuilder cb; + append_dict_label(cb, key_buffer, c, n); + if (c == n) { + // our two dictionaries are in fact leafs with matching edge labels (keys) + if (!combine_func(cb, std::move(label1.remainder), std::move(label2.remainder), key_buffer + n - total_key_len, + total_key_len)) { + // alas, the two values did not combine, this key will be absent from resulting dictionary + return {}; + } + return cb.finalize(); + } + assert(c < n); + key_buffer += c + 1; + key_buffer[-1] = 0; + // combine left subtrees + auto c1 = dict_combine_with(label1.remainder->prefetch_ref(0), label2.remainder->prefetch_ref(0), key_buffer, + n - c - 1, total_key_len, combine_func); + key_buffer[-1] = 1; + // combine right subtrees + auto c2 = dict_combine_with(label1.remainder->prefetch_ref(1), label2.remainder->prefetch_ref(1), key_buffer, + n - c - 1, total_key_len, combine_func); + label1.remainder.clear(); + label2.remainder.clear(); + // c1 and c2 are merged left and right children of dict1 and dict2 + if (!c1.is_null() && !c2.is_null()) { + // both children non-empty, simply put them into the new node + return finish_create_fork(cb, std::move(c1), std::move(c2), n - c); + } + if (c1.is_null() && c2.is_null()) { + return {}; // both children empty, resulting dictionary also empty + } + // exactly one of c1 and c2 is non-empty, have to merge labels + bool sw = c1.is_null(); + key_buffer[-1] = sw; + if (sw) { + c1 = std::move(c2); + } + LabelParser label3{std::move(c1), n - c - 1, label_mode()}; + label3.extract_label_to(key_buffer); + key_buffer -= c + 1; + // store combined label for the new edge + cb.reset(); + append_dict_label(cb, key_buffer, c + 1 + label3.l_bits, n); + // store payload + if (!cell_builder_add_slice_bool(cb, *label3.remainder)) { + throw VmError{Excno::cell_ov, "cannot change label of an old dictionary cell while merging edges"}; + } + return cb.finalize(); + } + if (c == l1) { + assert(c < l2); + dict1.clear(); + if ((mode & 2)) { + throw CombineError{}; + } + // children of root node of dict1 + auto c1 = label1.remainder->prefetch_ref(0); + auto c2 = label1.remainder->prefetch_ref(1); + label1.remainder.clear(); + // have to merge dict2 with one of the children of dict1 + label2.extract_label_to(key_buffer - skip2); // dict2 has longer label, extract it + bool sw = key_buffer[c]; + if (!sw) { + // merge c1 with dict2 + c1 = dict_combine_with(std::move(c1), std::move(dict2), key_buffer + c + 1, n - c - 1, total_key_len, + combine_func, mode, 0, skip2 + c + 1); + } else { + // merge c2 with dict2 + c2 = dict_combine_with(std::move(c2), std::move(dict2), key_buffer + c + 1, n - c - 1, total_key_len, + combine_func, mode, 0, skip2 + c + 1); + } + if (!c1.is_null() && !c2.is_null()) { + CellBuilder cb; + append_dict_label(cb, key_buffer, c, n); + return finish_create_fork(cb, std::move(c1), std::move(c2), n - c); + } + // one of children is empty, have to merge root edges + key_buffer[c] = !sw; + if (!sw) { + std::swap(c1, c2); + } + assert(!c1.is_null() && c2.is_null()); + LabelParser label3{std::move(c1), n - c - 1, label_mode()}; + label3.extract_label_to(key_buffer + c + 1); + CellBuilder cb; + append_dict_label(cb, key_buffer, c + 1 + label3.l_bits, n); + // store payload + if (!cell_builder_add_slice_bool(cb, *label3.remainder)) { + throw VmError{Excno::cell_ov, "cannot change label of an old dictionary cell while merging edges"}; + } + return cb.finalize(); + } else { + assert(c == l2 && c < l1); + dict2.clear(); + if ((mode & 1)) { + throw CombineError{}; + } + // children of root node of dict2 + label2.skip_label(); // dict2 had shorter label anyway, label1 is already unpacked + auto c1 = label2.remainder->prefetch_ref(0); + auto c2 = label2.remainder->prefetch_ref(1); + label2.remainder.clear(); + // have to merge dict1 with one of the children of dict2 + bool sw = key_buffer[c]; + if (!sw) { + // merge dict1 with c1 + c1 = dict_combine_with(std::move(dict1), std::move(c1), key_buffer + c + 1, n - c - 1, total_key_len, + combine_func, mode, skip1 + c + 1, 0); + } else { + // merge dict1 with c2 + c2 = dict_combine_with(std::move(dict1), std::move(c2), key_buffer + c + 1, n - c - 1, total_key_len, + combine_func, mode, skip1 + c + 1, 0); + } + if (!c1.is_null() && !c2.is_null()) { + CellBuilder cb; + append_dict_label(cb, key_buffer, c, n); + return finish_create_fork(cb, std::move(c1), std::move(c2), n - c); + } + // one of children is empty, have to merge root edges + key_buffer[c] = !sw; + if (!sw) { + std::swap(c1, c2); + } + assert(!c1.is_null() && c2.is_null()); + LabelParser label3{std::move(c1), n - c - 1, label_mode()}; + label3.extract_label_to(key_buffer + c + 1); + CellBuilder cb; + append_dict_label(cb, key_buffer, c + 1 + label3.l_bits, n); + // store payload + if (!cell_builder_add_slice_bool(cb, *label3.remainder)) { + throw VmError{Excno::cell_ov, "cannot change label of an old dictionary cell while merging edges"}; + } + return cb.finalize(); + } +} + +bool DictionaryFixed::combine_with(DictionaryFixed& dict2, const combine_func_t& combine_func, int mode) { + force_validate(); + dict2.force_validate(); + int key_len = get_key_bits(); + if (key_len != dict2.get_key_bits()) { + throw VmError{Excno::dict_err, "cannot combine dictionaries with different key lengths"}; + } + unsigned char key_buffer[max_key_bytes]; + try { + auto res = dict_combine_with(get_root_cell(), dict2.get_root_cell(), td::BitPtr{key_buffer}, key_len, key_len, + combine_func, mode); + set_root_cell(std::move(res)); + return true; + } catch (CombineError) { + return false; + } +} + +bool DictionaryFixed::combine_with(DictionaryFixed& dict2, const simple_combine_func_t& simple_combine_func, int mode) { + using namespace std::placeholders; + combine_func_t combine_func = std::bind(simple_combine_func, _1, _2, _3); + return combine_with(dict2, combine_func, mode); +} + +bool DictionaryFixed::combine_with(DictionaryFixed& dict2) { + return combine_with(dict2, + [](CellBuilder&, Ref, Ref, td::ConstBitPtr key, int key_len) -> bool { + LOG(WARNING) << "dictionary merge conflict for key " << key.to_hex(key_len); + throw CombineError{}; + }); +} + +bool DictionaryFixed::dict_check_for_each(Ref dict, td::BitPtr key_buffer, int n, int total_key_len, + const DictionaryFixed::foreach_func_t& foreach_func, + bool invert_first) const { + if (dict.is_null()) { + return true; + } + LabelParser label{std::move(dict), n, label_mode()}; + int l = label.l_bits; + label.extract_label_to(key_buffer); + if (l == n) { + // leaf node, value left in label.remainder + return foreach_func(std::move(label.remainder), key_buffer + n - total_key_len, total_key_len); + } + assert(l >= 0 && l < n); + // a fork with two children, c1 and c2 + auto c1 = label.remainder->prefetch_ref(0); + auto c2 = label.remainder->prefetch_ref(1); + label.remainder.clear(); + key_buffer += l + 1; + if (l) { + invert_first = false; + } else if (invert_first) { + std::swap(c1, c2); + } + key_buffer[-1] = invert_first; + // recursive check_foreach applied to both children + if (!dict_check_for_each(std::move(c1), key_buffer, n - l - 1, total_key_len, foreach_func)) { + return false; + } + key_buffer[-1] = !invert_first; + return dict_check_for_each(std::move(c2), key_buffer, n - l - 1, total_key_len, foreach_func); +} + +bool DictionaryFixed::check_for_each(const foreach_func_t& foreach_func, bool invert_first) { + force_validate(); + if (is_empty()) { + return true; + } + int key_len = get_key_bits(); + unsigned char key_buffer[max_key_bytes]; + return dict_check_for_each(get_root_cell(), td::BitPtr{key_buffer}, key_len, key_len, foreach_func, invert_first); +} + +static inline bool set_bit(td::BitPtr ptr, bool value = true) { + *ptr = value; + return true; +} + +// mode: +1 = check augmentation of dict1, +2 = ... of dict2 +bool DictionaryFixed::dict_scan_diff(Ref dict1, Ref dict2, td::BitPtr key_buffer, int n, int total_key_len, + const scan_diff_func_t& diff_func, int mode, int skip1, int skip2) const { + // skip1: remove that much first bits from all keys in dictionary dict1 (its keys are actually n + skip1 bits long) + // skip2: similar for dict2 + // pretending to compare subdictionaries with n-bit keys + if (dict1.is_null()) { + if (dict2.is_null()) { + return true; // both dictionaries are empty + } + assert(!skip2); + // dict1 empty, dict2 non-empty -> parse label of dict2 + LabelParser label{dict2, n, label_mode()}; + label.extract_label_to(key_buffer); + if (label.l_bits >= n) { + assert(label.l_bits == n); + // leaf in dict2, empty dict1 + auto key = key_buffer + label.l_bits - total_key_len; + if ((mode & 2) && !check_leaf(label.remainder, key, total_key_len)) { + throw VmError{Excno::dict_err, "invalid leaf in the second dictionary being compared"}; + } + return diff_func(key, total_key_len, {}, std::move(label.remainder)); + } + n -= label.l_bits + 1; + key_buffer += label.l_bits + 1; + if ((mode & 2) && !check_fork_raw(label.remainder, n + 1)) { + throw VmError{Excno::dict_err, "invalid fork in the second dictionary being compared"}; + } + // compare {} with each of children of dict2 + for (unsigned sw = 0; sw < 2; sw++) { + key_buffer[-1] = (bool)sw; + if (!dict_scan_diff({}, label.remainder->prefetch_ref(sw), key_buffer, n, total_key_len, diff_func, mode)) { + return false; + } + } + return true; + } else if (dict2.is_null()) { + assert(!skip1); + // dict2 empty, dict1 non-empty -> parse label of dict1 + LabelParser label{dict1, n, label_mode()}; + label.extract_label_to(key_buffer); + if (label.l_bits >= n) { + assert(label.l_bits == n); + // leaf in dict1, empty dict2 + auto key = key_buffer + label.l_bits - total_key_len; + if ((mode & 1) && !check_leaf(label.remainder, key, total_key_len)) { + throw VmError{Excno::dict_err, "invalid leaf in the first dictionary being compared"}; + } + return diff_func(key, total_key_len, std::move(label.remainder), {}); + } + n -= label.l_bits + 1; + key_buffer += label.l_bits + 1; + if ((mode & 1) && !check_fork_raw(label.remainder, n + 1)) { + throw VmError{Excno::dict_err, "invalid fork in the first dictionary being compared"}; + } + // compare each of children of dict1 with {} + for (unsigned sw = 0; sw < 2; sw++) { + key_buffer[-1] = (bool)sw; + if (!dict_scan_diff(label.remainder->prefetch_ref(sw), {}, key_buffer, n, total_key_len, diff_func, mode)) { + return false; + } + } + return true; + } + // both dictionaries non-empty + if (skip1 == skip2 && (dict1 == dict2 || dict1->get_hash() == dict2->get_hash())) { + // dictionaries match, subtree comparison not necessary + return true; + } + LabelParser label1{dict1, n + skip1, label_mode()}, label2{dict2, n + skip2, label_mode()}; + int l1 = label1.l_bits - skip1, l2 = label2.l_bits - skip2; + assert(l1 >= 0 && l2 >= 0); + assert(!skip1 || label1.common_prefix_len(key_buffer - skip1, skip1) == skip1); + assert(!skip2 || label2.common_prefix_len(key_buffer - skip2, skip2) == skip2); + label1.extract_label_to(key_buffer - skip1); + int c = label2.common_prefix_len(key_buffer - skip2, skip2 + l1) - skip2; + assert(c >= 0 && c <= l1 && c <= l2); + if (c < l1 && c < l2) { + // the two dictionaries have disjoint keys + if (!key_buffer[c]) { + // all keys of dict1 are before dict2 + return dict_scan_diff(std::move(dict1), {}, key_buffer - skip1, n + skip1, total_key_len, diff_func, mode) && + dict_scan_diff({}, std::move(dict2), key_buffer - skip2, n + skip2, total_key_len, diff_func, mode); + } else { + // all keys of dict2 are before dict1 + return dict_scan_diff({}, std::move(dict2), key_buffer - skip2, n + skip2, total_key_len, diff_func, mode) && + dict_scan_diff(std::move(dict1), {}, key_buffer - skip1, n + skip1, total_key_len, diff_func, mode); + } + } + if (c == l1 && c == l2) { + // funny enough, the non-skipped parts of labels of l1 and l2 match + dict1.clear(); + dict2.clear(); + label2.skip_label(); + if (c == n) { + // our two dictionaries are in fact leafs with matching edge labels (keys) + auto key = key_buffer + n - total_key_len; + if ((mode & 1) && !check_leaf(label1.remainder, key, total_key_len)) { + throw VmError{Excno::dict_err, "invalid leaf in the first dictionary being compared"}; + } + if ((mode & 2) && !check_leaf(label2.remainder, key, total_key_len)) { + throw VmError{Excno::dict_err, "invalid leaf in the second dictionary being compared"}; + } + return label1.remainder->contents_equal(*label2.remainder) || + diff_func(key, total_key_len, std::move(label1.remainder), std::move(label2.remainder)); + } + assert(c < n); + key_buffer += c + 1; + n -= c + 1; + if ((mode & 1) && !check_fork_raw(label1.remainder, n + 1)) { + throw VmError{Excno::dict_err, "invalid fork in the first dictionary being compared"}; + } + if ((mode & 2) && !check_fork_raw(label2.remainder, n + 1)) { + throw VmError{Excno::dict_err, "invalid fork in the second dictionary being compared"}; + } + for (unsigned sw = 0; sw <= 1; sw++) { + key_buffer[-1] = (bool)sw; + // compare left and then right subtrees + if (!dict_scan_diff(label1.remainder->prefetch_ref(sw), label2.remainder->prefetch_ref(sw), key_buffer, n, + total_key_len, diff_func, mode)) { + return false; + } + } + return true; + } + if (c == l1) { + assert(c < l2); + dict1.clear(); + if ((mode & 1) && !check_fork_raw(label1.remainder, n - c)) { + throw VmError{Excno::dict_err, "invalid fork in the first dictionary being compared"}; + } + // children of root node of dict1 + auto c1 = label1.remainder->prefetch_ref(0); + auto c2 = label1.remainder->prefetch_ref(1); + label1.remainder.clear(); + // have to compare dict2 with one of the children of dict1 + label2.extract_label_to(key_buffer - skip2); // dict2 has longer label, extract it + key_buffer += c + 1; + n -= c + 1; + bool sw = key_buffer[-1]; + key_buffer[-1] = false; + if (!sw) { + // compare c1 with dict2, then c2 with {} + return dict_scan_diff(std::move(c1), std::move(dict2), key_buffer, n, total_key_len, diff_func, mode, 0, + skip2 + c + 1) && + set_bit(key_buffer - 1) && + dict_scan_diff(std::move(c2), {}, key_buffer, n, total_key_len, diff_func, mode); + } else { + // compare c1 with {}, then c2 with dict2 + return dict_scan_diff(std::move(c1), {}, key_buffer, n, total_key_len, diff_func, mode) && + set_bit(key_buffer - 1) && + dict_scan_diff(std::move(c2), std::move(dict2), key_buffer, n, total_key_len, diff_func, mode, 0, + skip2 + c + 1); + } + } else { + assert(c == l2 && c < l1); + dict2.clear(); + label2.skip_label(); // dict2 had shorter label anyway, label1 is already unpacked + if ((mode & 2) && !check_fork_raw(label2.remainder, n - c)) { + throw VmError{Excno::dict_err, "invalid fork in the second dictionary being compared"}; + } + // children of root node of dict2 + auto c1 = label2.remainder->prefetch_ref(0); + auto c2 = label2.remainder->prefetch_ref(1); + label2.remainder.clear(); + // have to compare dict1 with one of the children of dict2 + key_buffer += c + 1; + n -= c + 1; + bool sw = key_buffer[-1]; + key_buffer[-1] = false; + if (!sw) { + // compare dict1 with c1, then {} with c2 + return dict_scan_diff(std::move(dict1), std::move(c1), key_buffer, n, total_key_len, diff_func, mode, + skip1 + c + 1, 0) && + set_bit(key_buffer - 1) && + dict_scan_diff({}, std::move(c2), key_buffer, n, total_key_len, diff_func, mode); + } else { + // compare {} with c1, then dict1 with c2 + return dict_scan_diff({}, std::move(c1), key_buffer, n, total_key_len, diff_func, mode) && + set_bit(key_buffer - 1) && + dict_scan_diff(std::move(dict1), std::move(c2), key_buffer, n, total_key_len, diff_func, mode, + skip1 + c + 1, 0); + } + } +} + +bool DictionaryFixed::scan_diff(DictionaryFixed& dict2, const scan_diff_func_t& diff_func, int check_augm) { + force_validate(); + dict2.force_validate(); + int key_len = get_key_bits(); + if (key_len != dict2.get_key_bits()) { + throw VmError{Excno::dict_err, "cannot compare dictionaries with different key lengths"}; + } + unsigned char key_buffer[max_key_bytes]; + try { + return dict_scan_diff(get_root_cell(), dict2.get_root_cell(), td::BitPtr{key_buffer}, key_len, key_len, diff_func, + check_augm); + } catch (CombineError) { + return false; + } +} + +bool DictionaryFixed::dict_validate_check(Ref dict, td::BitPtr key_buffer, int n, int total_key_len, + const DictionaryFixed::foreach_func_t& foreach_func, + bool invert_first) const { + //LOG(DEBUG) << "dict_validate_check for " << total_key_len - n << "-bit key prefix " << (key_buffer - n + total_key_len).to_hex(total_key_len - n); + if (dict.is_null()) { + return true; + } + LabelParser label{std::move(dict), n, label_mode()}; + int l = label.l_bits; + label.extract_label_to(key_buffer); + if (l == n) { + // leaf node, value left in label.remainder + vm::CellSlice cs{*label.remainder}; + auto key = key_buffer + n - total_key_len; + if (!(check_leaf(cs, key, total_key_len) && foreach_func(std::move(label.remainder), key, total_key_len))) { + LOG(DEBUG) << "invalid dictionary leaf node with " << total_key_len << "-bit key " << key.to_hex(total_key_len); + return false; + } + return true; + } + assert(l >= 0 && l < n); + // a fork with two children, c1 and c2 + auto c1 = label.remainder.write().fetch_ref(); + auto c2 = label.remainder.unique_write().fetch_ref(); + key_buffer += l + 1; + n -= l + 1; + if (!check_fork(label.remainder.write(), c1, c2, n + 1)) { + LOG(DEBUG) << "invalid dictionary fork augmentation for fork node with " << total_key_len - n - 1 + << "-bit key prefix " << (key_buffer + n - total_key_len).to_hex(total_key_len - n - 1); + return false; + } + label.remainder.clear(); + if (l) { + invert_first = false; + } else if (invert_first) { + std::swap(c1, c2); + } + key_buffer[-1] = invert_first; + // recursive check_foreach applied to both children + if (!dict_validate_check(std::move(c1), key_buffer, n, total_key_len, foreach_func)) { + return false; + } + key_buffer[-1] = !invert_first; + return dict_validate_check(std::move(c2), key_buffer, n, total_key_len, foreach_func); +} + +bool DictionaryFixed::validate_check(const DictionaryFixed::foreach_func_t& foreach_func, bool invert_first) { + if (!validate()) { + return false; + } + if (is_empty()) { + return true; + } + int key_len = get_key_bits(); + unsigned char key_buffer[max_key_bytes]; + return dict_validate_check(get_root_cell(), td::BitPtr{key_buffer}, key_len, key_len, foreach_func, invert_first); +} + +bool DictionaryFixed::validate_all() { + return validate_check([](Ref value, td::ConstBitPtr key, int n) { return true; }) || invalidate(); +} + +/* + * + * PREFIX DICTIONARIES + * + */ + +std::pair, int> PrefixDictionary::lookup_prefix(td::ConstBitPtr key, int key_len) { + force_validate(); + int n = get_key_bits(); + if (is_empty()) { + return std::make_pair(Ref{}, 0); + } + //std::cerr << "dictionary lookup for key = " << key.to_hex(key_len) << std::endl; + Ref cell = get_root_cell(); + int m = key_len; + while (true) { + LabelParser label{std::move(cell), n, 1}; + int l = label.common_prefix_len(key, m); + if (l < label.l_bits) { + //std::cerr << "(not a prefix)\n"; + return std::make_pair(Ref{}, key_len - m + l); + } + n -= label.l_bits; + m -= label.l_bits; + assert(m >= 0); + label.skip_label(); + Ref cs = std::move(label.remainder); + if (!cs->have(1)) { + throw VmError{Excno::dict_err, "no node constructor in a prefix code dictionary"}; + } + if (!cs.unique_write().fetch_ulong(1)) { + return std::make_pair(std::move(cs), key_len - m); + } + if (!n) { + throw VmError{Excno::dict_err, "a fork node in a prefix code dictionary with zero remaining key length"}; + } + if (cs->size() != 0 || cs->size_refs() != 2) { + throw VmError{Excno::dict_err, "invalid fork node in a prefix code dictionary"}; + } + if (!m) { + return std::make_pair(Ref{}, key_len); + } + key += label.l_bits; + bool sw = *key++; + //std::cerr << "key bit at position " << key_bits - n << " equals " << sw << std::endl; + --n; + --m; + cell = cs->prefetch_ref(sw); + } +} + +Ref PrefixDictionary::lookup(td::ConstBitPtr key, int key_len) { + force_validate(); + if (key_len > get_key_bits()) { + return {}; + } + auto res = lookup_prefix(key, key_len); + return res.second == key_len ? std::move(res.first) : Ref{}; +} + +bool PrefixDictionary::set_gen(td::ConstBitPtr key, int key_len, const std::function& store_val, + SetMode mode) { + force_validate(); + if (key_len > get_key_bits() || key_len < 0) { + return false; + } + auto res = pfx_dict_set(get_root_cell(), key, key_len, get_key_bits(), store_val, mode); + if (res.second) { + set_root_cell(std::move(res.first)); + } + return res.second; +} + +bool PrefixDictionary::set(td::ConstBitPtr key, int key_len, Ref value, SetMode mode) { + return set_gen(key, key_len, [value](CellBuilder& cb) { return cell_builder_add_slice_bool(cb, *value); }, mode); +} + +bool PrefixDictionary::set_builder(td::ConstBitPtr key, int key_len, Ref val_b, SetMode mode) { + return set_gen(key, key_len, [val_b](CellBuilder& cb) { return cb.append_builder_bool(val_b); }, mode); +} + +Ref PrefixDictionary::lookup_delete(td::ConstBitPtr key, int key_len) { + force_validate(); + if (key_len > get_key_bits() || key_len < 0) { + return {}; + } + auto res = pfx_dict_lookup_delete(get_root_cell(), key, key_len, get_key_bits()); + if (res.first.not_null()) { + set_root_cell(std::move(res.second)); + } + return std::move(res.first); +} + +/* + * + * AUGMENTED DICTIONARIES + * + */ + +namespace dict { + +bool AugmentationData::check_empty(vm::CellSlice& cs) const { + vm::CellBuilder cb; + return eval_empty(cb) && cb.contents_equal(cs); +} + +bool AugmentationData::check_leaf(vm::CellSlice& cs, vm::CellSlice& val_cs) const { + vm::CellBuilder cb; + return eval_leaf(cb, val_cs) && cb.contents_equal(cs); +} + +bool AugmentationData::check_fork(vm::CellSlice& cs, vm::CellSlice& left_cs, vm::CellSlice& right_cs) const { + vm::CellBuilder cb; + return eval_fork(cb, left_cs, right_cs) && cb.contents_equal(cs); +} + +Ref AugmentationData::extract_extra(vm::CellSlice& cs) const { + Ref res{true, cs}; + return skip_extra(cs) && res.write().cut_tail(cs) ? std::move(res) : Ref{}; +} + +Ref AugmentationData::extract_extra(Ref cs_ref) const { + CellSlice cs{*cs_ref}; + return skip_extra(cs) && cs_ref.write().cut_tail(cs) ? std::move(cs_ref) : Ref{}; +} + +bool AugmentationData::extract_extra_to(vm::CellSlice& cs, vm::CellSlice& extra) const { + extra = cs; + return cs.is_valid() && skip_extra(cs) && extra.cut_tail(cs); +} + +} // namespace dict + +using dict::AugmentationData; +using dict::LabelParser; + +AugmentedDictionary::AugmentedDictionary(int _n, const AugmentationData& _aug, bool validate) + : DictionaryFixed(_n, false), aug(_aug) { + if (validate) { + force_validate(); + } +} + +AugmentedDictionary::AugmentedDictionary(Ref _root, int _n, const AugmentationData& _aug, bool validate) + : DictionaryFixed(std::move(_root), _n, false), aug(_aug) { + if (validate) { + force_validate(); + } +} + +AugmentedDictionary::AugmentedDictionary(Ref cell, int _n, const AugmentationData& _aug, bool validate) + : DictionaryFixed(std::move(cell), _n, false), aug(_aug) { + if (validate) { + force_validate(); + } +} + +AugmentedDictionary::AugmentedDictionary(DictNonEmpty, Ref _root, int _n, const AugmentationData& _aug, + bool validate) + : DictionaryFixed(DictNonEmpty{}, std::move(_root), _n, false), aug(_aug) { + if (validate) { + force_validate(); + } +} + +bool AugmentedDictionary::validate() { + if (is_valid()) { + return true; + } + if (flags & f_invalid) { + return false; + } + if (key_bits < 0 || key_bits > max_key_bits) { + return invalidate(); + } + if (flags & f_root_cached) { + if (root.is_null() || !root->size()) { + return invalidate(); + } + bool non_empty = root->prefetch_ulong(1); + if (non_empty && !root->size_refs()) { + return invalidate(); + } + if (root_cell.not_null()) { + return invalidate(); + } + vm::CellSlice cs{*root}; + if (!cs.advance(1)) { + return invalidate(); + } + if (non_empty) { + root_cell = cs.fetch_ref(); + auto root_extra = get_root_extra(); + if (!(root_extra.not_null() && root_extra->contents_equal(cs))) { + return invalidate(); + } + } else { + if (!aug.check_empty(cs)) { + return invalidate(); + } + } + } else if (root.not_null()) { + return invalidate(); + } + flags |= f_valid; + return true; +} + +Ref AugmentedDictionary::get_root() const { + if (!(flags & f_root_cached) && !compute_root()) { + return {}; + } + return root; +} + +Ref AugmentedDictionary::extract_root() && { + if (!(flags & f_root_cached) && !compute_root()) { + return {}; + } + flags = f_invalid; + return std::move(root); +} + +bool AugmentedDictionary::append_dict_to_bool(CellBuilder& cb) const& { + if (!is_valid()) { + return false; + } + if (root_cell.is_null()) { + return cb.store_zeroes_bool(1) && aug.eval_empty(cb); + } else { + return cb.store_ones_bool(1) && cb.store_ref_bool(root_cell) && cb.append_cellslice_bool(get_root_extra()); + } +} + +bool AugmentedDictionary::append_dict_to_bool(CellBuilder& cb) && { + if (!is_valid()) { + return false; + } + flags = f_invalid; + if (root_cell.is_null()) { + return cb.store_zeroes_bool(1) && aug.eval_empty(cb); + } else { + return cb.store_ones_bool(1) && cb.store_ref_bool(root_cell) && cb.append_cellslice_bool(get_root_extra()); + } +} + +bool AugmentedDictionary::compute_root() const { + if (!is_valid()) { + return false; + } + if (root_cell.is_null()) { + root = get_empty_dictionary(); + flags |= f_root_cached; + return true; + } + CellBuilder cb; + if (cb.store_long_bool(1, 1) && cb.store_ref_bool(root_cell) && cb.append_cellslice_bool(get_root_extra())) { + root = Ref{true, cb.finalize()}; + flags |= f_root_cached; + return true; + } else { + return false; + } +} + +Ref AugmentedDictionary::get_empty_dictionary() const { + CellBuilder cb; + cb.store_long(0, 1); + return aug.eval_empty(cb) ? Ref{true, cb.finalize()} : Ref{}; +} + +Ref AugmentedDictionary::get_node_extra(Ref cell_ref, int n) const { + if (cell_ref.is_null()) { + CellBuilder cb; + if (!aug.eval_empty(cb)) { + return {}; + } + return Ref{true, cb.finalize()}; + } + LabelParser label{std::move(cell_ref), n, 2}; + label.skip_label(); + if (label.l_bits == n) { + return aug.extract_extra(std::move(label.remainder)); + } else if (label.remainder.write().advance_refs(2)) { + vm::CellSlice cs{*label.remainder}; + if (aug.skip_extra(cs) && cs.empty_ext()) { + return std::move(label.remainder); + } + } + return {}; +} + +Ref AugmentedDictionary::get_root_extra() const { + return get_node_extra(root_cell, key_bits); +} + +Ref AugmentedDictionary::extract_value(Ref value_extra) const { + if (value_extra.not_null() && aug.skip_extra(value_extra.write())) { + return value_extra; + } else { + return {}; + } +} + +Ref AugmentedDictionary::extract_value_ref(Ref value_extra) const { + if (value_extra.not_null() && aug.skip_extra(value_extra.write()) && value_extra->size_ext() == 0x10000) { + return value_extra->prefetch_ref(); + } else { + return {}; + } +} + +std::pair, Ref> AugmentedDictionary::decompose_value_extra(Ref value_extra) const { + if (value_extra.is_null()) { + return {}; + } + auto extra = aug.extract_extra(value_extra.write()); + if (extra.is_null()) { + return {}; + } else { + return {std::move(value_extra), std::move(extra)}; + } +} + +std::pair, Ref> AugmentedDictionary::decompose_value_ref_extra(Ref value_extra) const { + if (value_extra.is_null()) { + return {}; + } + auto extra = aug.extract_extra(value_extra.write()); + if (extra.is_null() || value_extra->size_ext() != 0x10000) { + return {}; + } else { + return {value_extra->prefetch_ref(), std::move(extra)}; + } +} + +Ref AugmentedDictionary::lookup_with_extra(td::ConstBitPtr key, int key_len) { + return DictionaryFixed::lookup(key, key_len); +} + +Ref AugmentedDictionary::lookup(td::ConstBitPtr key, int key_len) { + return extract_value(lookup_with_extra(key, key_len)); +} + +Ref AugmentedDictionary::lookup_ref(td::ConstBitPtr key, int key_len) { + return extract_value_ref(lookup_with_extra(key, key_len)); +} + +std::pair, Ref> AugmentedDictionary::lookup_extra(td::ConstBitPtr key, int key_len) { + return decompose_value_extra(lookup_with_extra(key, key_len)); +} + +std::pair, Ref> AugmentedDictionary::lookup_ref_extra(td::ConstBitPtr key, int key_len) { + return decompose_value_ref_extra(lookup_with_extra(key, key_len)); +} + +Ref AugmentedDictionary::lookup_delete_with_extra(td::ConstBitPtr key, int key_len) { + return DictionaryFixed::lookup_delete(key, key_len); +} + +Ref AugmentedDictionary::lookup_delete(td::ConstBitPtr key, int key_len) { + return extract_value(lookup_delete_with_extra(key, key_len)); +} + +Ref AugmentedDictionary::lookup_delete_ref(td::ConstBitPtr key, int key_len) { + return extract_value_ref(lookup_delete_with_extra(key, key_len)); +} + +std::pair, Ref> AugmentedDictionary::lookup_delete_extra(td::ConstBitPtr key, int key_len) { + return decompose_value_extra(lookup_delete_with_extra(key, key_len)); +} + +bool AugmentedDictionary::check_leaf(CellSlice& cs, td::ConstBitPtr key, int key_len) const { + vm::CellSlice extra; + return aug.extract_extra_to(cs, extra) && aug.check_leaf_key_extra(cs, extra, key, key_len); +} + +bool AugmentedDictionary::check_fork(CellSlice& cs, Ref c1, Ref c2, int n) const { + if (n <= 0) { + return false; + } + auto extra1 = get_node_extra(std::move(c1), n - 1); + auto extra2 = get_node_extra(std::move(c2), n - 1); + return extra1.not_null() && extra2.not_null() && aug.check_fork(cs, extra1.write(), extra2.write()); +} + +Ref AugmentedDictionary::finish_create_leaf(CellBuilder& cb, const CellSlice& value) const { + CellSlice value_copy{value}; + if (!aug.eval_leaf(cb, value_copy)) { + throw VmError{Excno::dict_err, "cannot compute and store extra value into an augmented dictionary cell"}; + } + if (!cb.append_cellslice_bool(value)) { + throw VmError{Excno::dict_err, "cannot store new value into an augmented dictionary cell"}; + } + return cb.finalize(); +} + +Ref AugmentedDictionary::finish_create_fork(CellBuilder& cb, Ref c1, Ref c2, int n) const { + assert(n > 0); + if (!(cb.store_ref_bool(c1) && cb.store_ref_bool(c2))) { + throw VmError{Excno::dict_err, "cannot store branch references into an augmented dictionary cell"}; + } + auto extra1 = get_node_extra(std::move(c1), n - 1); + auto extra2 = get_node_extra(std::move(c2), n - 1); + if (extra1.is_null()) { + throw VmError{Excno::dict_err, "cannot extract extra value from left branch of an augmented dictionary fork node"}; + } + if (extra2.is_null()) { + throw VmError{Excno::dict_err, "cannot extract extra value from left branch of an augmented dictionary fork node"}; + } + if (!aug.eval_fork(cb, extra1.write(), extra2.write())) { + throw VmError{Excno::dict_err, "cannot compute extra value for an augmented dictionary fork node"}; + } + return cb.finalize(); +} + +std::pair, bool> AugmentedDictionary::dict_set(Ref dict, td::ConstBitPtr key, int n, + const CellSlice& value, Dictionary::SetMode mode) const { + //std::cerr << "augmented dictionary modification for " << n << "-bit key = " << key.to_hex(n) << std::endl; + if (dict.is_null()) { + // the dictionary is very empty + if (mode == Dictionary::SetMode::Replace) { + return std::make_pair, bool>({}, false); + } + // create an one-element dictionary + CellBuilder cb; + append_dict_label(cb, key, n, n); + return std::make_pair(finish_create_leaf(cb, value), true); + } + LabelParser label{std::move(dict), n, 2}; + label.validate(); + int pfx_len = label.common_prefix_len(key, n); + assert(pfx_len >= 0 && pfx_len <= label.l_bits && label.l_bits <= n); + if (pfx_len < label.l_bits) { + // have to insert a new node (fork) inside the current edge + if (mode == Dictionary::SetMode::Replace) { + // key not found, return unchanged dictionary + return std::make_pair(Ref{}, false); + } + // first, create the edge + new leaf cell + int m = n - pfx_len - 1; + CellBuilder cb; + append_dict_label(cb, key + (pfx_len + 1), m, m); + Ref c1 = finish_create_leaf(cb, value); // new leaf cell corresponding to `key` + //cb.reset(); + // create the lower portion of the old edge + int t = label.l_bits - pfx_len - 1; + auto cs = std::move(label.remainder); + if (label.l_same) { + append_dict_label_same(cb, label.l_same & 1, t, m); + } else { + cs.write().advance(pfx_len + 1); + append_dict_label(cb, cs->data_bits(), t, m); + cs.unique_write().advance(t); + } + // now cs is the old payload of the edge, either a value or two subdictionary references + if (!cell_builder_add_slice_bool(cb, *cs)) { + throw VmError{Excno::cell_ov, "cannot change label of an old augmented dictionary cell (?)"}; + } + Ref c2 = cb.finalize(); // the other child of the new fork + // cb.reset(); + append_dict_label(cb, key, pfx_len, n); + bool sw_bit = key[pfx_len]; + if (sw_bit) { + c1.swap(c2); + } + return std::make_pair(finish_create_fork(cb, std::move(c1), std::move(c2), n - pfx_len), true); + } + if (label.l_bits == n) { + // the edge leads to a leaf node + // this leaf node already contains a value for the key wanted + if (mode == Dictionary::SetMode::Add) { + // return unchanged dictionary + return std::make_pair(Ref{}, false); + } + // replace the value of the only element of the dictionary + CellBuilder cb; + append_dict_label(cb, key, n, n); + return std::make_pair(finish_create_leaf(cb, value), true); + } + // main case: the edge leads to a fork, have to insert new value either in the right or in the left subtree + auto c1 = label.remainder->prefetch_ref(0); + auto c2 = label.remainder->prefetch_ref(1); + label.remainder.clear(); + if (key[label.l_bits]) { + // insert key into the right child (c2) + auto res = dict_set(std::move(c2), key + (label.l_bits + 1), n - label.l_bits - 1, value, mode); + if (!res.second) { + // return unchanged dictionary + return std::make_pair(Ref{}, false); + } + c2 = std::move(res.first); + } else { + // insert key into the left child (c1) + auto res = dict_set(std::move(c1), key + (label.l_bits + 1), n - label.l_bits - 1, value, mode); + if (!res.second) { + // return unchanged dictionary + return std::make_pair(Ref{}, false); + } + c1 = std::move(res.first); + } + // create a new label with the same content + CellBuilder cb; + append_dict_label(cb, key, label.l_bits, n); + return std::make_pair(finish_create_fork(cb, std::move(c1), std::move(c2), n - label.l_bits), true); +} + +bool AugmentedDictionary::set(td::ConstBitPtr key, int key_len, Ref value, SetMode mode) { + return value.not_null() && set(key, key_len, *value, mode); +} + +bool AugmentedDictionary::set(td::ConstBitPtr key, int key_len, const CellSlice& value, SetMode mode) { + force_validate(); + if (key_len != get_key_bits()) { + return false; + } + auto res = dict_set(get_root_cell(), key, key_len, value, mode); + if (res.second) { + //vm::CellSlice cs{vm::NoVm{}, res.first}; + //std::cerr << "new augmented dictionary root is:\n"; + //cs.print_rec(std::cerr); + set_root_cell(std::move(res.first)); + } + return res.second; +} + +bool AugmentedDictionary::set_ref(td::ConstBitPtr key, int key_len, Ref value_ref, SetMode mode) { + if (value_ref.not_null()) { + CellBuilder cb; + cb.store_ref(std::move(value_ref)); + return set(key, key_len, load_cell_slice(cb.finalize())); + } else { + return false; + } +} + +bool AugmentedDictionary::set_builder(td::ConstBitPtr key, int key_len, const CellBuilder& value, SetMode mode) { + return set(key, key_len, load_cell_slice(value.finalize_copy())); +} + +bool AugmentedDictionary::check_for_each_extra(const foreach_extra_func_t& foreach_extra_func, bool invert_first) { + force_validate(); + const auto& augm = aug; + foreach_func_t foreach_func = [&foreach_extra_func, &augm](Ref value_extra, td::ConstBitPtr key, + int key_len) { + auto extra = augm.extract_extra(value_extra.write()); + return extra.not_null() && foreach_extra_func(std::move(value_extra), std::move(extra), key, key_len); + }; + return DictionaryFixed::check_for_each(foreach_func, invert_first); +} + +std::pair, Ref> AugmentedDictionary::dict_traverse_extra( + Ref dict, td::BitPtr key_buffer, int n, const traverse_func_t& traverse_node) const { + int m = get_key_bits(); + while (true) { + CHECK(dict.not_null()); + LabelParser label{std::move(dict), n, 2}; + label.extract_label_to(key_buffer); + key_buffer += label.l_bits; + n -= label.l_bits; + if (n <= 0) { + // reached a leaf, check it + assert(!n); + auto pair = decompose_value_extra(std::move(label.remainder)); + if (pair.first.is_null()) { + throw VmError{Excno::dict_err, "invalid leaf value/extra in an augmented dictionary"}; + } + int r = traverse_node(key_buffer - m, m, pair.second /* extra */, pair.first /* value */); + if (r < 0) { + throw CombineErrorValue{r}; + } else if (r > 0) { + return pair; + } else { + return {}; + } + } + // visit (traverse) fork + auto c1 = label.remainder.write().fetch_ref(), c2 = label.remainder.write().fetch_ref(); + int r = traverse_node(key_buffer + n - m, m - n, std::move(label.remainder) /* extra */, {}); + if (r < 0 || (r & 3) == 3) { + throw CombineErrorValue{r}; + } else if (!(r & 3)) { + return {}; + } + // r = 1 : visit only left, 2 = visit only right, 5 = visit right, then left, 6 = visit left, then right + ++key_buffer; + --n; + bool sw = r & 1; + if (sw) { + std::swap(c1, c2); + } + if (r & 4) { + // have to visit both children in some order; do a recursive call to visit the first child + key_buffer[-1] = sw; + auto tmp = dict_traverse_extra(std::move(c1), key_buffer, n, traverse_node); + if (tmp.first.not_null()) { + return tmp; + } + } + // visit the remaining child + key_buffer[-1] = !sw; + dict = std::move(c2); + } +} + +std::pair, Ref> AugmentedDictionary::traverse_extra(td::BitPtr key_buffer, int key_len, + const traverse_func_t& traverse_node) { + force_validate(); + if (key_len != get_key_bits() || is_empty()) { + return {}; + } + return dict_traverse_extra(get_root_cell(), key_buffer, key_len, traverse_node); +} + +bool AugmentedDictionary::validate_check_extra(const AugmentedDictionary::foreach_extra_func_t& foreach_extra_func, + bool invert_first) { + const AugmentationData& augm = aug; + int key_len = get_key_bits(); + return validate_check( + [&foreach_extra_func, &augm, key_len](Ref value_extra, td::ConstBitPtr key, int value) { + auto extra = augm.extract_extra(value_extra.write()); + return extra.not_null() && foreach_extra_func(std::move(value_extra), std::move(extra), key, key_len); + }, + invert_first); +} + +} // namespace vm diff --git a/crypto/vm/dict.h b/crypto/vm/dict.h new file mode 100644 index 00000000..11234277 --- /dev/null +++ b/crypto/vm/dict.h @@ -0,0 +1,474 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once +#include "common/bitstring.h" +#include "vm/cells.h" +#include "vm/cellslice.h" +#include "vm/stack.hpp" +#include + +namespace vm { +using td::BitSlice; +using td::Ref; + +namespace dict { + +struct LabelParser { + enum { chk_none = 0, chk_min = 1, chk_size = 2, chk_all = 3 }; + Ref remainder; + int l_offs; + int l_same; + int l_bits; + unsigned s_bits; + LabelParser(Ref cs, int max_label_len, int auto_validate = chk_all); + LabelParser(Ref cell, int max_label_len, int auto_validate = chk_all); + int is_valid() const { + return l_offs; + } + void validate() const; + void validate_simple(int n) const; + void validate_ext(int n) const; + bool is_prefix_of(td::ConstBitPtr key, int len) const; + bool has_prefix(td::ConstBitPtr key, int len) const; + int common_prefix_len(td::ConstBitPtr key, int len) const; + int extract_label_to(td::BitPtr to); + int copy_label_prefix_to(td::BitPtr to, int max_len) const; + td::ConstBitPtr bits() const { + return remainder->data_bits(); + } + td::ConstBitPtr bits_end() const { + return bits() + l_bits; + } + void skip_label() { + remainder.write().advance(s_bits); + } + void clear() { + remainder.clear(); + } + + private: + bool parse_label(CellSlice& cs, int max_label_len); +}; + +struct AugmentationData { + virtual ~AugmentationData() = default; + virtual bool skip_extra(vm::CellSlice& cs) const = 0; + virtual bool eval_leaf(vm::CellBuilder& cb, vm::CellSlice& val_cs) const = 0; + virtual bool eval_fork(vm::CellBuilder& cb, vm::CellSlice& left_cs, vm::CellSlice& right_cs) const = 0; + virtual bool eval_empty(vm::CellBuilder& cb) const = 0; + virtual bool check_leaf(vm::CellSlice& cs, vm::CellSlice& val_cs) const; + virtual bool check_fork(vm::CellSlice& cs, vm::CellSlice& left_cs, vm::CellSlice& right_cs) const; + virtual bool check_empty(vm::CellSlice& cs) const; + virtual bool check_leaf_key_extra(vm::CellSlice& val_cs, vm::CellSlice& extra_cs, td::ConstBitPtr key, + int key_len) const { + return check_leaf(extra_cs, val_cs); + } + Ref extract_extra(vm::CellSlice& cs) const; + Ref extract_extra(Ref cs_ref) const; + bool extract_extra_to(vm::CellSlice& cs, Ref& extra_csr) const { + return (extra_csr = extract_extra(cs)).not_null(); + } + bool extract_extra_to(Ref cs_ref, Ref& extra_csr) const { + return (extra_csr = extract_extra(std::move(cs_ref))).not_null(); + } + bool extract_extra_to(vm::CellSlice& cs, vm::CellSlice& extra) const; +}; + +static inline bool store_cell_dict(vm::CellBuilder& cb, Ref dict_root) { + return dict_root.not_null() ? cb.store_long_bool(1, 1) && cb.store_ref_bool(std::move(dict_root)) + : cb.store_long_bool(0, 1); +} + +} // namespace dict + +struct CombineError {}; // thrown by Dictionary::combine_with +struct CombineErrorValue { + int arg_; +}; + +struct DictNonEmpty {}; +struct DictAdvance {}; + +class DictionaryBase { + protected: + mutable Ref root; + Ref root_cell; + int key_bits; + mutable int flags; + enum { f_valid = 1, f_root_cached = 2, f_invalid = 0x80 }; + + public: + enum class SetMode : int { Set = 3, Replace = 1, Add = 2 }; + enum { max_key_bits = 1023, max_key_bytes = (max_key_bits + 7) / 8 }; + + typedef std::function store_value_func_t; + + DictionaryBase(int _n, bool validate = true); + DictionaryBase(Ref _root, int _n, bool validate = true); + DictionaryBase(const CellSlice& root_cs, int _n, bool validate = true); + DictionaryBase(DictAdvance, CellSlice& root_cs, int _n, bool validate = true); + DictionaryBase(Ref cell, int _n, bool validate = true); + DictionaryBase(DictNonEmpty, Ref _root, int _n, bool validate = true); + DictionaryBase(DictNonEmpty, const CellSlice& root_cs, int _n, bool validate = true); + virtual ~DictionaryBase() = default; + + static Ref construct_root_from(const CellSlice& root_node_cs); + Ref get_root() const; + Ref extract_root() &&; + Ref get_root_cell() const { + return root_cell; + } + Ref extract_root_cell() && { + return std::move(root_cell); + } + bool append_dict_to_bool(CellBuilder& cb) &&; + bool append_dict_to_bool(CellBuilder& cb) const &; + int get_key_bits() const { + return key_bits; + } + bool is_valid() const { + return flags & f_valid; + } + void reset() { + root.clear(); + root_cell.clear(); + flags = f_valid; + } + virtual bool validate(); + void force_validate(); + bool is_empty() const { + return root_cell.is_null(); + } + static Ref get_empty_dictionary(); + + protected: + bool init_root_for_nonempty(const CellSlice& cs); + bool invalidate() { + flags |= f_invalid; + return false; + } + bool compute_root() const; + static Ref new_empty_dictionary(); + void set_root_cell(Ref cell) { + root_cell = std::move(cell); + flags &= ~f_root_cached; + } +}; + +class DictionaryFixed : public DictionaryBase { + public: + typedef std::function filter_func_t; + typedef std::function, Ref)> simple_combine_func_t; + typedef std::function, Ref, td::ConstBitPtr, int)> combine_func_t; + typedef std::function, td::ConstBitPtr, int)> foreach_func_t; + typedef std::function, Ref)> scan_diff_func_t; + + DictionaryFixed(int _n, bool validate = true) : DictionaryBase(_n, validate) { + } + DictionaryFixed(Ref _root, int _n, bool validate = true) : DictionaryBase(std::move(_root), _n, validate) { + } + DictionaryFixed(const CellSlice& root_cs, int _n, bool validate = true) : DictionaryBase(root_cs, _n, validate) { + } + DictionaryFixed(DictAdvance, CellSlice& root_cs, int _n, bool validate = true) + : DictionaryBase(DictAdvance(), root_cs, _n, validate) { + } + DictionaryFixed(Ref cell, int _n, bool validate = true) : DictionaryBase(std::move(cell), _n, validate) { + } + DictionaryFixed(DictNonEmpty, Ref _root, int _n, bool validate = true) + : DictionaryBase(DictNonEmpty(), std::move(_root), _n, validate) { + } + DictionaryFixed(DictNonEmpty, const CellSlice& root_cs, int _n, bool validate = true) + : DictionaryBase(DictNonEmpty(), root_cs, _n, validate) { + } + static BitSlice integer_key(td::RefInt256 x, unsigned n, bool sgnd = true, unsigned char buffer[128] = 0, + bool quiet = false); + static bool integer_key_simple(td::RefInt256 x, unsigned n, bool sgnd, td::BitPtr buffer, bool quiet = false); + bool key_exists(td::ConstBitPtr key, int key_len); + bool int_key_exists(long long key); + bool uint_key_exists(unsigned long long key); + Ref lookup(td::ConstBitPtr key, int key_len); + Ref lookup_delete(td::ConstBitPtr key, int key_len); + Ref get_minmax_key(td::BitPtr key_buffer, int key_len, bool fetch_max = false, bool invert_first = false); + Ref extract_minmax_key(td::BitPtr key_buffer, int key_len, bool fetch_max = false, + bool invert_first = false); + Ref lookup_nearest_key(td::BitPtr key_buffer, int key_len, bool fetch_next = false, bool allow_eq = false, + bool invert_first = false); + bool has_common_prefix(td::ConstBitPtr prefix, int prefix_len); + int get_common_prefix(td::BitPtr buffer, unsigned buffer_len); + bool cut_prefix_subdict(td::ConstBitPtr prefix, int prefix_len, bool remove_prefix = false); + Ref extract_prefix_subdict_root(td::ConstBitPtr prefix, int prefix_len, bool remove_prefix = false); + bool check_for_each(const foreach_func_t& foreach_func, bool invert_first = false); + int filter(filter_func_t check); + bool combine_with(DictionaryFixed& dict2, const combine_func_t& combine_func, int mode = 0); + bool combine_with(DictionaryFixed& dict2, const simple_combine_func_t& simple_combine_func, int mode = 0); + bool combine_with(DictionaryFixed& dict2); + bool scan_diff(DictionaryFixed& dict2, const scan_diff_func_t& diff_func, int check_augm = 0); + bool validate_check(const foreach_func_t& foreach_func, bool invert_first = false); + bool validate_all(); + template + bool key_exists(const T& key) { + return key_exists(key.bits(), key.size()); + } + template + Ref lookup(const T& key) { + return lookup(key.bits(), key.size()); + } + template + Ref lookup_delete(const T& key) { + return lookup_delete(key.bits(), key.size()); + } + template + Ref get_minmax_key(T& key_buffer, bool fetch_max = false, bool invert_first = false) { + return get_minmax_key(key_buffer.bits(), key_buffer.size(), fetch_max, invert_first); + } + + protected: + virtual int label_mode() const { + return dict::LabelParser::chk_all; + } + virtual Ref finish_create_leaf(CellBuilder& cb, const CellSlice& value) const; + virtual Ref finish_create_fork(CellBuilder& cb, Ref c1, Ref c2, int n) const; + virtual bool check_fork(CellSlice& cs, Ref c1, Ref c2, int n) const { + return true; + } + virtual bool check_leaf(CellSlice& cs, td::ConstBitPtr key, int key_len) const { + return true; + } + bool check_leaf(Ref cs_ref, td::ConstBitPtr key, int key_len) const { + return check_leaf(cs_ref.write(), key, key_len); + } + bool check_fork_raw(Ref cs_ref, int n) const; + + private: + std::pair, Ref> dict_lookup_delete(Ref dict, td::ConstBitPtr key, int n) const; + Ref dict_lookup_minmax(Ref dict, td::BitPtr key_buffer, int n, int mode) const; + Ref dict_lookup_nearest(Ref dict, td::BitPtr key_buffer, int n, bool allow_eq, int mode) const; + std::pair, bool> extract_prefix_subdict_internal(Ref dict, td::ConstBitPtr prefix, int prefix_len, + bool remove_prefix = false) const; + bool dict_check_for_each(Ref dict, td::BitPtr key_buffer, int n, int total_key_len, + const foreach_func_t& foreach_func, bool invert_first = false) const; + std::pair, int> dict_filter(Ref dict, td::BitPtr key, int n, const filter_func_t& check_leaf) const; + Ref dict_combine_with(Ref dict1, Ref dict2, td::BitPtr key_buffer, int n, int total_key_len, + const combine_func_t& combine_func, int mode = 0, int skip1 = 0, int skip2 = 0) const; + bool dict_scan_diff(Ref dict1, Ref dict2, td::BitPtr key_buffer, int n, int total_key_len, + const scan_diff_func_t& diff_func, int mode = 0, int skip1 = 0, int skip2 = 0) const; + bool dict_validate_check(Ref dict, td::BitPtr key_buffer, int n, int total_key_len, + const foreach_func_t& foreach_func, bool invert_first = false) const; +}; + +class Dictionary final : public DictionaryFixed { + public: + typedef std::function)> simple_map_func_t; + typedef std::function, td::ConstBitPtr, int)> map_func_t; + Dictionary(int _n, bool validate = true) : DictionaryFixed(_n, validate) { + } + Dictionary(Ref _root, int _n, bool validate = true) : DictionaryFixed(std::move(_root), _n, validate) { + } + Dictionary(const CellSlice& root_cs, int _n, bool validate = true) : DictionaryFixed(root_cs, _n, validate) { + } + Dictionary(DictAdvance, CellSlice& root_cs, int _n, bool validate = true) + : DictionaryFixed(DictAdvance(), root_cs, _n, validate) { + } + Dictionary(Ref cell, int _n, bool validate = true) : DictionaryFixed(std::move(cell), _n, validate) { + } + Dictionary(DictNonEmpty, Ref _root, int _n, bool validate = true) + : DictionaryFixed(DictNonEmpty(), std::move(_root), _n, validate) { + } + Dictionary(DictNonEmpty, const CellSlice& root_cs, int _n, bool validate = true) + : DictionaryFixed(DictNonEmpty(), root_cs, _n, validate) { + } + Ref lookup_ref(td::ConstBitPtr key, int key_len); + Ref lookup_delete_ref(td::ConstBitPtr key, int key_len); + bool set(td::ConstBitPtr key, int key_len, Ref value, SetMode mode = SetMode::Set); + bool set_ref(td::ConstBitPtr key, int key_len, Ref val_ref, SetMode mode = SetMode::Set); + bool set_builder(td::ConstBitPtr key, int key_len, Ref val_b, SetMode mode = SetMode::Set); + bool set_builder(td::ConstBitPtr key, int key_len, const CellBuilder& val_b, SetMode mode = SetMode::Set); + bool set_gen(td::ConstBitPtr key, int key_len, const store_value_func_t& store_val, SetMode mode = SetMode::Set); + Ref lookup_set(td::ConstBitPtr key, int key_len, Ref value, SetMode mode = SetMode::Set); + Ref lookup_set_ref(td::ConstBitPtr key, int key_len, Ref val_ref, SetMode mode = SetMode::Set); + Ref lookup_set_builder(td::ConstBitPtr key, int key_len, Ref val_b, + SetMode mode = SetMode::Set); + Ref lookup_set_gen(td::ConstBitPtr key, int key_len, const store_value_func_t& store_val, + SetMode mode = SetMode::Set); + Ref get_minmax_key_ref(td::BitPtr key_buffer, int key_len, bool fetch_max = false, bool invert_first = false); + Ref extract_minmax_key_ref(td::BitPtr key_buffer, int key_len, bool fetch_max = false, + bool invert_first = false); + void map(const map_func_t& map_func); + void map(const simple_map_func_t& simple_map_func); + template + Ref lookup_ref(const T& key) { + return lookup_ref(key.bits(), key.size()); + } + template + Ref lookup_delete_ref(const T& key) { + return lookup_delete_ref(key.bits(), key.size()); + } + template + bool set(const T& key, Ref value, SetMode mode = SetMode::Set) { + return set(key.bits(), key.size(), std::move(value), mode); + } + template + bool set_ref(const T& key, Ref val_ref, SetMode mode = SetMode::Set) { + return set_ref(key.bits(), key.size(), std::move(val_ref), mode); + } + template + bool set_builder(const T& key, const CellBuilder& val_b, SetMode mode = SetMode::Set) { + return set_builder(key.bits(), key.size(), val_b, mode); + } + template + bool set_builder(const T& key, Ref val_ref, SetMode mode = SetMode::Set) { + return set_builder(key.bits(), key.size(), std::move(val_ref), mode); + } + template + Ref lookup_set(const T& key, Ref value, SetMode mode = SetMode::Set) { + return lookup_set(key.bits(), key.size(), std::move(value), mode); + } + template + Ref lookup_set_ref(const T& key, Ref val_ref, SetMode mode = SetMode::Set) { + return lookup_set_ref(key.bits(), key.size(), std::move(val_ref), mode); + } + template + Ref lookup_set_builder(const T& key, const CellBuilder& val_b, SetMode mode = SetMode::Set) { + return lookup_set_builder(key.bits(), key.size(), val_b, mode); + } + template + Ref lookup_set_builder(const T& key, Ref val_ref, SetMode mode = SetMode::Set) { + return lookup_set_builder(key.bits(), key.size(), std::move(val_ref), mode); + } + + private: + bool check_fork(CellSlice& cs, Ref c1, Ref c2, int n) const override { + return cs.empty_ext(); + } + static Ref extract_value_ref(Ref cs); + std::pair, int> dict_filter(Ref dict, td::BitPtr key, int n, const filter_func_t& check_leaf) const; +}; + +class PrefixDictionary final : public DictionaryBase { + public: + PrefixDictionary(int _n, bool validate = true) : DictionaryBase(_n, validate) { + } + PrefixDictionary(Ref _root, int _n, bool validate = true) + : DictionaryBase(std::move(_root), _n, validate) { + } + PrefixDictionary(Ref cell, int _n, bool validate = true) : DictionaryBase(std::move(cell), _n, validate) { + } + Ref lookup(td::ConstBitPtr key, int key_len); + std::pair, int> lookup_prefix(td::ConstBitPtr key, int key_len); + Ref lookup_delete(td::ConstBitPtr key, int key_len); + bool set(td::ConstBitPtr key, int key_len, Ref value, SetMode mode = SetMode::Set); + bool set_builder(td::ConstBitPtr key, int key_len, Ref val_b, SetMode mode = SetMode::Set); + bool set_gen(td::ConstBitPtr key, int key_len, const store_value_func_t& store_val, SetMode mode = SetMode::Set); +}; + +using dict::AugmentationData; + +class AugmentedDictionary final : public DictionaryFixed { + const AugmentationData& aug; + + public: + typedef std::function, Ref, td::ConstBitPtr, int)> foreach_extra_func_t; + // return value of traverse_func: < 0 = error, 0 = skip, 1 = visit only left, 2 = visit only right, 5 = visit right, then left, 6 = visit left, then right + // for leaf nodes, all >0 values mean accept and return node as the final result, 0 = skip (continue scanning) + typedef std::function extra, Ref value)> + traverse_func_t; + AugmentedDictionary(int _n, const AugmentationData& _aug, bool validate = true); + AugmentedDictionary(Ref _root, int _n, const AugmentationData& _aug, bool validate = true); + AugmentedDictionary(Ref cell, int _n, const AugmentationData& _aug, bool validate = true); + AugmentedDictionary(DictNonEmpty, Ref _root, int _n, const AugmentationData& _aug, bool validate = true); + Ref get_empty_dictionary() const; + Ref get_root() const; + Ref extract_root() &&; + bool append_dict_to_bool(CellBuilder& cb) &&; + bool append_dict_to_bool(CellBuilder& cb) const &; + Ref get_root_extra() const; + Ref lookup(td::ConstBitPtr key, int key_len); + Ref lookup_ref(td::ConstBitPtr key, int key_len); + Ref lookup_with_extra(td::ConstBitPtr key, int key_len); + std::pair, Ref> lookup_extra(td::ConstBitPtr key, int key_len); + std::pair, Ref> lookup_ref_extra(td::ConstBitPtr key, int key_len); + Ref lookup_delete(td::ConstBitPtr key, int key_len); + Ref lookup_delete_ref(td::ConstBitPtr key, int key_len); + Ref lookup_delete_with_extra(td::ConstBitPtr key, int key_len); + std::pair, Ref> lookup_delete_extra(td::ConstBitPtr key, int key_len); + std::pair, Ref> lookup_delete_ref_extra(td::ConstBitPtr key, int key_len); + bool set(td::ConstBitPtr key, int key_len, const CellSlice& value, SetMode mode = SetMode::Set); + bool set(td::ConstBitPtr key, int key_len, Ref value, SetMode mode = SetMode::Set); + bool set_ref(td::ConstBitPtr key, int key_len, Ref val_ref, SetMode mode = SetMode::Set); + bool set_builder(td::ConstBitPtr key, int key_len, const CellBuilder& value, SetMode mode = SetMode::Set); + bool check_for_each_extra(const foreach_extra_func_t& foreach_extra_func, bool invert_first = false); + std::pair, Ref> traverse_extra(td::BitPtr key_buffer, int key_len, + const traverse_func_t& traverse_node); + bool validate_check_extra(const foreach_extra_func_t& foreach_extra_func, bool invert_first = false); + bool validate() override; + template + Ref lookup(const T& key) { + return lookup(key.bits(), key.size()); + } + template + Ref lookup_ref(const T& key) { + return lookup_ref(key.bits(), key.size()); + } + template + bool set(const T& key, Ref val_ref, SetMode mode = SetMode::Set) { + return set(key.bits(), key.size(), std::move(val_ref), mode); + } + template + bool set(const T& key, const CellSlice& value, SetMode mode = SetMode::Set) { + return set(key.bits(), key.size(), value, mode); + } + template + bool set_ref(const T& key, Ref val_ref, SetMode mode = SetMode::Set) { + return set_ref(key.bits(), key.size(), std::move(val_ref), mode); + } + template + bool set_builder(const T& key, const CellBuilder& val_b, SetMode mode = SetMode::Set) { + return set_builder(key.bits(), key.size(), val_b, mode); + } + template + Ref lookup_delete(const T& key) { + return lookup_delete(key.bits(), key.size()); + } + template + Ref lookup_delete_ref(const T& key) { + return lookup_delete_ref(key.bits(), key.size()); + } + + Ref extract_value(Ref value_extra) const; + Ref extract_value_ref(Ref value_extra) const; + std::pair, Ref> decompose_value_extra(Ref value_extra) const; + std::pair, Ref> decompose_value_ref_extra(Ref value_extra) const; + + private: + bool compute_root() const; + Ref get_node_extra(Ref cell_ref, int n) const; + bool check_leaf(CellSlice& cs, td::ConstBitPtr key, int key_len) const override; + bool check_fork(CellSlice& cs, Ref c1, Ref c2, int n) const override; + Ref finish_create_leaf(CellBuilder& cb, const CellSlice& value) const override; + Ref finish_create_fork(CellBuilder& cb, Ref c1, Ref c2, int n) const override; + std::pair, bool> dict_set(Ref dict, td::ConstBitPtr key, int n, const CellSlice& value, + SetMode mode = SetMode::Set) const; + int label_mode() const override { + return dict::LabelParser::chk_size; + } + std::pair, Ref> dict_traverse_extra(Ref dict, td::BitPtr key_buffer, int n, + const traverse_func_t& traverse_node) const; +}; + +} // namespace vm diff --git a/crypto/vm/dictops.cpp b/crypto/vm/dictops.cpp new file mode 100644 index 00000000..9f4faed9 --- /dev/null +++ b/crypto/vm/dictops.cpp @@ -0,0 +1,811 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include +#include "vm/log.h" +#include "vm/opctable.h" +#include "vm/stack.hpp" +#include "vm/continuation.h" +#include "vm/excno.hpp" +#include "common/bigint.hpp" +#include "common/refint.h" +#include "vm/dictops.h" +#include "vm/dict.h" + +namespace vm { + +template +void push_dict(Stack& stack, T&& dict) { + stack.push_maybe_cell(std::move(dict).extract_root_cell()); +} + +template +void push_dict(Stack& stack, const T& dict) { + stack.push_maybe_cell(dict.get_root_cell()); +} + +int exec_dict_empty(VmState* st) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute DICTEMPTY"; + auto dict = stack.pop_cellslice(); + if (!dict->have(1)) { + throw VmError{Excno::cell_und}; + } + stack.push_smallint(~dict->prefetch_long(1)); + return 0; +} + +int exec_store_dict(VmState* st) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute STDICT"; + stack.check_underflow(2); + auto cb = stack.pop_builder(); + auto d = stack.pop_maybe_cell(); + if (!cb.write().store_maybe_ref(std::move(d))) { + throw VmError{Excno::cell_ov}; + } + stack.push_builder(std::move(cb)); + return 0; +} + +int dict_nonempty(const CellSlice& dict) { + if (!dict.have(1)) { + return -1; + } + int res = (int)dict.prefetch_ulong(1); + return dict.have_refs(res) ? res : -1; +} + +int dict_nonempty_chk(const CellSlice& dict) { + int res = dict_nonempty(dict); + if (res < 0) { + throw VmError{Excno::cell_und}; + } + return res; +} + +int exec_skip_dict(VmState* st) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute SKIPDICT\n"; + auto dict = stack.pop_cellslice(); + int res = dict_nonempty_chk(*dict); + dict.write().advance_ext(1, res); + stack.push_cellslice(std::move(dict)); + return 0; +} + +int exec_load_optref(VmState* st) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute LDOPTREF\n"; + auto dict = stack.pop_cellslice(); + int res = dict_nonempty_chk(*dict); + dict.write().advance(1); + if (res) { + auto cell = dict.write().fetch_ref(); + stack.push_cellslice(std::move(dict)); + stack.push_cell(std::move(cell)); + } else { + stack.push_cellslice(std::move(dict)); + } + stack.push_smallint(-res); + return 0; +} + +int exec_preload_optref(VmState* st) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute PLDOPTREF\n"; + auto dict = stack.pop_cellslice(); + int res = dict_nonempty_chk(*dict); + if (res) { + stack.push_cell(dict->prefetch_ref()); + } + stack.push_smallint(-res); + return 0; +} + +int exec_load_dict_slice(VmState* st, unsigned args) { + bool preload = args & 1, quiet = args & 2; + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute " << (preload ? "P" : "") << "LDDICTS" << (quiet ? "Q\n" : "\n"); + auto cs = stack.pop_cellslice(); + int res = dict_nonempty(*cs); + if (res < 0) { + if (!quiet) { + throw VmError{Excno::cell_und}; + } + if (!preload) { + stack.push_cellslice(std::move(cs)); + } + } else { + if (preload) { + stack.push_cellslice(cs->prefetch_subslice(1, res)); + } else { + stack.push_cellslice(cs.write().fetch_subslice(1, res)); + stack.push_cellslice(std::move(cs)); + } + } + if (quiet) { + stack.push_bool(res >= 0); + } + return 0; +} + +int exec_load_dict(VmState* st, unsigned args) { + bool preload = args & 1, quiet = args & 2; + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute " << (preload ? "P" : "") << "LDDICT" << (quiet ? "Q\n" : "\n"); + auto cs = stack.pop_cellslice(); + int res = dict_nonempty(*cs); + if (res < 0) { + if (!quiet) { + throw VmError{Excno::cell_und}; + } + if (!preload) { + stack.push_cellslice(std::move(cs)); + } + } else { + stack.push_maybe_cell(res ? cs->prefetch_ref() : Ref{}); + if (!preload) { + cs.write().advance_ext(1, res); + stack.push_cellslice(std::move(cs)); + } + } + if (quiet) { + stack.push_bool(res >= 0); + } + return 0; +} + +std::string dump_dictop(unsigned args, const char* name) { + std::ostringstream os{"DICT"}; + if (args & 4) { + os << (args & 2 ? 'U' : 'I'); + } + os << name; + if (args & 1) { + os << "REF"; + } + return os.str(); +} + +std::string dump_dictop2(unsigned args, const char* name) { + std::ostringstream os{"DICT"}; + if (args & 2) { + os << (args & 1 ? 'U' : 'I'); + } + os << name; + return os.str(); +} + +std::string dump_subdictop2(unsigned args, const char* name) { + std::ostringstream os{"SUBDICT"}; + if (args & 2) { + os << (args & 1 ? 'U' : 'I'); + } + os << name; + return os.str(); +} + +int exec_dict_get(VmState* st, unsigned args) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute DICT" << (args & 4 ? (args & 2 ? "U" : "I") : "") << "GET" << (args & 1 ? "REF" : ""); + stack.check_underflow(3); + int n = stack.pop_smallint_range(Dictionary::max_key_bits); + Dictionary dict{stack.pop_maybe_cell(), n}; + BitSlice key; + unsigned char buffer[Dictionary::max_key_bytes]; + if (args & 4) { + key = dict.integer_key(stack.pop_int(), n, !(args & 2), buffer, true); + if (!key.is_valid()) { + stack.push_smallint(0); + return 0; + } + } else { + key = stack.pop_cellslice()->prefetch_bits(n); + } + if (!key.is_valid()) { + throw VmError{Excno::cell_und, "not enough bits for a dictionary key"}; + } + if (args & 1) { + auto value = dict.lookup_ref(key); + if (value.not_null()) { + stack.push_cell(std::move(value)); + stack.push_smallint(-1); + } else { + stack.push_smallint(0); + } + } else { + auto value = dict.lookup(key); + if (value.not_null()) { + stack.push_cellslice(std::move(value)); + stack.push_smallint(-1); + } else { + stack.push_smallint(0); + } + } + return 0; +} + +int exec_dict_get_optref(VmState* st, unsigned args) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute DICT" << (args & 2 ? (args & 1 ? "U" : "I") : "") << "GETOPTREF"; + stack.check_underflow(3); + int n = stack.pop_smallint_range(Dictionary::max_key_bits); + Dictionary dict{stack.pop_maybe_cell(), n}; + BitSlice key; + unsigned char buffer[Dictionary::max_key_bytes]; + if (args & 2) { + key = dict.integer_key(stack.pop_int(), n, !(args & 1), buffer, true); + if (!key.is_valid()) { + stack.push_null(); + return 0; + } + } else { + key = stack.pop_cellslice()->prefetch_bits(n); + } + if (!key.is_valid()) { + throw VmError{Excno::cell_und, "not enough bits for a dictionary key"}; + } + stack.push_maybe_cell(dict.lookup_ref(key)); + return 0; +} + +int exec_dict_set(VmState* st, unsigned args, Dictionary::SetMode mode, const char* name, bool bld = false) { + args <<= (bld ? 1 : 0); + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute DICT" << (args & 4 ? (args & 2 ? "U" : "I") : "") << name + << (args & 1 ? "REF" : (bld ? "B" : "")); + stack.check_underflow(4); + int n = stack.pop_smallint_range(Dictionary::max_key_bits); + Dictionary dict{stack.pop_maybe_cell(), n}; + BitSlice key; + unsigned char buffer[Dictionary::max_key_bytes]; + if (args & 4) { + key = dict.integer_key(stack.pop_int(), n, !(args & 2), buffer); + } else { + key = stack.pop_cellslice()->prefetch_bits(n); + } + bool res; + if (bld) { + auto new_value = stack.pop_builder(); + if (!key.is_valid()) { + throw VmError{Excno::cell_und, "not enough bits for a dictionary key"}; + } + res = dict.set_builder(key, std::move(new_value), mode); + } else if (!(args & 1)) { + auto new_value = stack.pop_cellslice(); + if (!key.is_valid()) { + throw VmError{Excno::cell_und, "not enough bits for a dictionary key"}; + } + res = dict.set(key, std::move(new_value), mode); + } else { + auto new_value_ref = stack.pop_cell(); + if (!key.is_valid()) { + throw VmError{Excno::cell_und, "not enough bits for a dictionary key"}; + } + res = dict.set_ref(key, std::move(new_value_ref), mode); + } + push_dict(stack, std::move(dict)); + if (mode == Dictionary::SetMode::Set) { + st->ensure_throw(res); + } else { + stack.push_bool(res); + } + return 0; +} + +int exec_dict_setget(VmState* st, unsigned args, Dictionary::SetMode mode, const char* name, bool bld = false) { + args <<= (bld ? 1 : 0); + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute DICT" << (args & 4 ? (args & 2 ? "U" : "I") : "") << name + << (args & 1 ? "REF\n" : (bld ? "B\n" : "\n")); + stack.check_underflow(4); + int n = stack.pop_smallint_range(Dictionary::max_key_bits); + Dictionary dict{stack.pop_maybe_cell(), n}; + BitSlice key; + unsigned char buffer[Dictionary::max_key_bytes]; + if (args & 4) { + key = dict.integer_key(stack.pop_int(), n, !(args & 2), buffer); + } else { + key = stack.pop_cellslice()->prefetch_bits(n); + } + bool ok_f = (mode != Dictionary::SetMode::Add); + if (bld) { + auto new_value = stack.pop_builder(); + if (!key.is_valid()) { + throw VmError{Excno::cell_und, "not enough bits for a dictionary key"}; + } + auto res = dict.lookup_set_builder(key, std::move(new_value), mode); + push_dict(stack, std::move(dict)); + if (res.not_null()) { + stack.push_cellslice(std::move(res)); + stack.push_bool(ok_f); + } else { + stack.push_bool(!ok_f); + } + } else if (!(args & 1)) { + auto new_value = stack.pop_cellslice(); + if (!key.is_valid()) { + throw VmError{Excno::cell_und, "not enough bits for a dictionary key"}; + } + auto res = dict.lookup_set(key, std::move(new_value), mode); + push_dict(stack, std::move(dict)); + if (res.not_null()) { + stack.push_cellslice(std::move(res)); + stack.push_bool(ok_f); + } else { + stack.push_bool(!ok_f); + } + } else { + auto new_value_ref = stack.pop_cell(); + if (!key.is_valid()) { + throw VmError{Excno::cell_und, "not enough bits for a dictionary key"}; + } + auto res = dict.lookup_set_ref(key, std::move(new_value_ref), mode); + push_dict(stack, std::move(dict)); + if (res.not_null()) { + stack.push_cell(std::move(res)); + stack.push_bool(ok_f); + } else { + stack.push_bool(!ok_f); + } + } + return 0; +} + +int exec_dict_delete(VmState* st, unsigned args) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute DICT" << (args & 2 ? (args & 1 ? "U" : "I") : "") << "DEL\n"; + stack.check_underflow(3); + int n = stack.pop_smallint_range(Dictionary::max_key_bits); + Dictionary dict{stack.pop_maybe_cell(), n}; + BitSlice key; + unsigned char buffer[Dictionary::max_key_bytes]; + if (args & 2) { + key = dict.integer_key(stack.pop_int(), n, !(args & 1), buffer); + if (!key.is_valid()) { + push_dict(stack, std::move(dict)); + stack.push_smallint(0); + return 0; + } + } else { + key = stack.pop_cellslice()->prefetch_bits(n); + } + if (!key.is_valid()) { + throw VmError{Excno::cell_und, "not enough bits for a dictionary key"}; + } + bool res = dict.lookup_delete(key).not_null(); + push_dict(stack, std::move(dict)); + stack.push_bool(res); + return 0; +} + +int exec_dict_deleteget(VmState* st, unsigned args) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute DICT" << (args & 4 ? (args & 2 ? "U" : "I") : "") << "DELGET" << (args & 1 ? "REF\n" : "\n"); + stack.check_underflow(3); + int n = stack.pop_smallint_range(Dictionary::max_key_bits); + Dictionary dict{stack.pop_maybe_cell(), n}; + BitSlice key; + unsigned char buffer[Dictionary::max_key_bytes]; + if (args & 4) { + key = dict.integer_key(stack.pop_int(), n, !(args & 2), buffer); + if (!key.is_valid()) { + push_dict(stack, std::move(dict)); + stack.push_smallint(0); + return 0; + } + } else { + key = stack.pop_cellslice()->prefetch_bits(n); + } + if (!key.is_valid()) { + throw VmError{Excno::cell_und, "not enough bits for a dictionary key"}; + } + if (!(args & 1)) { + auto res = dict.lookup_delete(key); + push_dict(stack, std::move(dict)); + bool ok = res.not_null(); + if (ok) { + stack.push_cellslice(std::move(res)); + } + stack.push_bool(ok); + } else { + auto res = dict.lookup_delete_ref(key); + push_dict(stack, std::move(dict)); + bool ok = res.not_null(); + if (ok) { + stack.push_cell(std::move(res)); + } + stack.push_bool(ok); + } + return 0; +} + +int exec_dict_setget_optref(VmState* st, unsigned args) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute DICT" << (args & 2 ? (args & 1 ? "U" : "I") : "") << "SETGETOPTREF"; + stack.check_underflow(4); + int n = stack.pop_smallint_range(Dictionary::max_key_bits); + Dictionary dict{stack.pop_maybe_cell(), n}; + BitSlice key; + unsigned char buffer[Dictionary::max_key_bytes]; + if (args & 2) { + key = dict.integer_key(stack.pop_int(), n, !(args & 1), buffer); + } else { + key = stack.pop_cellslice()->prefetch_bits(n); + } + auto new_value = stack.pop_maybe_cell(); + if (!key.is_valid()) { + throw VmError{Excno::cell_und, "not enough bits for a dictionary key"}; + } + Ref value; + if (new_value.not_null()) { + value = dict.lookup_set_ref(key, std::move(new_value)); + } else { + value = dict.lookup_delete_ref(key); + } + push_dict(stack, std::move(dict)); + stack.push_maybe_cell(std::move(value)); + return 0; +} + +int exec_dict_getmin(VmState* st, unsigned args) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute DICT" << (args & 4 ? (args & 2 ? "U" : "I") : "") << (args & 16 ? "REM" : "") + << (args & 8 ? "MAX" : "MIN") << (args & 1 ? "REF\n" : "\n"); + stack.check_underflow(2); + int n = stack.pop_smallint_range(args & 4 ? (args & 2 ? 256 : 257) : Dictionary::max_key_bits); + Dictionary dict{stack.pop_maybe_cell(), n}; + unsigned char buffer[Dictionary::max_key_bytes]; + bool flip_first = !(args & 2); + if (!(args & 1)) { + auto res = (args & 16) ? dict.extract_minmax_key(buffer, n, args & 8, flip_first) + : dict.get_minmax_key(buffer, n, args & 8, flip_first); + if (args & 16) { + push_dict(stack, std::move(dict)); + } + if (res.is_null()) { + stack.push_bool(false); + return 0; + } + stack.push_cellslice(std::move(res)); + } else { + auto res = (args & 16) ? dict.extract_minmax_key_ref(buffer, n, args & 8, flip_first) + : dict.get_minmax_key_ref(buffer, n, args & 8, flip_first); + if (args & 16) { + push_dict(stack, std::move(dict)); + } + if (res.is_null()) { + stack.push_bool(false); + return 0; + } + stack.push_cell(std::move(res)); + } + if (args & 4) { + td::RefInt256 x{true}; + x.unique_write().import_bits(td::ConstBitPtr{buffer}, n, !(args & 2)); + stack.push_int(std::move(x)); + } else { + stack.push_cellslice(Ref{true, CellBuilder().store_bits(td::ConstBitPtr{buffer}, n).finalize()}); + } + stack.push_bool(true); + return 0; +} + +std::string dump_dictop_getnear(CellSlice& cs, unsigned args) { + std::ostringstream os{"DICT"}; + if (args & 8) { + os << (args & 4 ? 'U' : 'I'); + } + os << "GET" << (args & 2 ? "PREV" : "NEXT") << (args & 1 ? "EQ" : ""); + return os.str(); +} + +int exec_dict_getnear(VmState* st, unsigned args) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute DICT" << (args & 8 ? (args & 4 ? "U" : "I") : "") << "GET" << (args & 2 ? "PREV" : "NEXT") + << (args & 1 ? "EQ\n" : "\n"); + stack.check_underflow(3); + int n = stack.pop_smallint_range(args & 8 ? (args & 4 ? 256 : 257) : Dictionary::max_key_bits); + Dictionary dict{stack.pop_maybe_cell(), n}; + unsigned char buffer[Dictionary::max_key_bytes]; + bool sgnd = !(args & 4), go_up = !(args & 2), allow_eq = args & 1; + if (!(args & 8)) { + auto key_hint = stack.pop_cellslice()->prefetch_bits(n); + if (!key_hint.is_valid()) { + throw VmError{Excno::cell_und, "not enough bits for a dictionary key hint"}; + } + td::BitPtr{buffer}.copy_from(key_hint.bits(), n); + key_hint.forget(); + auto res = dict.lookup_nearest_key(td::BitPtr{buffer}, n, go_up, allow_eq, false); + if (res.is_null()) { + stack.push_bool(false); + return 0; + } + stack.push_cellslice(std::move(res)); + stack.push_cellslice(Ref{true, CellBuilder().store_bits(td::ConstBitPtr{buffer}, n).finalize()}); + } else { + auto key = stack.pop_int_finite(); + Ref res; + if (key->export_bits(td::BitPtr{buffer}, n, sgnd)) { + res = dict.lookup_nearest_key(buffer, n, go_up, allow_eq, sgnd); + } else if ((td::sgn(key) >= 0) ^ go_up) { + res = dict.get_minmax_key(buffer, n, !go_up, sgnd); + } + if (res.is_null()) { + stack.push_bool(false); + return 0; + } + stack.push_cellslice(std::move(res)); + key.write().import_bits(td::ConstBitPtr{buffer}, n, sgnd); + stack.push_int(std::move(key)); + } + stack.push_bool(true); + return 0; +} + +int exec_pfx_dict_set(VmState* st, Dictionary::SetMode mode, const char* name) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute PFXDICT" << name; + stack.check_underflow(3); + int n = stack.pop_smallint_range(PrefixDictionary::max_key_bits); + PrefixDictionary dict{stack.pop_maybe_cell(), n}; + auto key_slice = stack.pop_cellslice(); + auto new_value = stack.pop_cellslice(); + bool res = dict.set(key_slice->data_bits(), key_slice->size(), std::move(new_value), mode); + push_dict(stack, std::move(dict)); + stack.push_bool(res); + return 0; +} + +int exec_pfx_dict_delete(VmState* st) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute PFXDICTDEL\n"; + stack.check_underflow(2); + int n = stack.pop_smallint_range(PrefixDictionary::max_key_bits); + PrefixDictionary dict{stack.pop_maybe_cell(), n}; + auto key_slice = stack.pop_cellslice(); + bool res = dict.lookup_delete(key_slice->data_bits(), key_slice->size()).not_null(); + push_dict(stack, std::move(dict)); + stack.push_bool(res); + return 0; +} + +int exec_dict_get_exec(VmState* st, unsigned args) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute DICT" << (args & 1 ? 'U' : 'I') << "GET" << (args & 2 ? "EXEC\n" : "JMP\n"); + stack.check_underflow(3); + int n = stack.pop_smallint_range(Dictionary::max_key_bits); + Dictionary dict{stack.pop_maybe_cell(), n}; + unsigned char buffer[Dictionary::max_key_bytes]; + dict.integer_key_simple(stack.pop_int(), n, !(args & 1), td::BitPtr{buffer}); + auto value = dict.lookup(td::BitPtr{buffer}, n); + if (value.not_null()) { + Ref cont{true, std::move(value), st->get_cp()}; + return (args & 2) ? st->call(std::move(cont)) : st->jump(std::move(cont)); + } else { + return 0; + } +} + +std::string dump_dict_get_exec(CellSlice& cs, unsigned args) { + return std::string{"DICT"} + (args & 1 ? 'U' : 'I') + "GET" + (args & 2 ? "EXEC" : "JMP"); +} + +int exec_push_const_dict(VmState* st, CellSlice& cs, unsigned args, int pfx_bits) { + if (!cs.have(pfx_bits)) { + throw VmError{Excno::inv_opcode, "not enough data bits for a DICTPUSHCONST instruction"}; + } + if (!cs.have_refs(1)) { + throw VmError{Excno::inv_opcode, "not enough references for a DICTPUSHCONST instruction"}; + } + Stack& stack = st->get_stack(); + cs.advance(pfx_bits - 11); + auto slice = cs.fetch_subslice(1, 1); + int n = (int)cs.fetch_ulong(10); + VM_LOG(st) << "execute DICTPUSHCONST " << n << " (" << slice << ")"; + stack.push_cell(slice->prefetch_ref()); + stack.push_smallint(n); + return 0; +} + +std::string dump_push_const_dict(CellSlice& cs, int pfx_bits, const char* name) { + if (!cs.have(pfx_bits, 1)) { + return ""; + } + cs.advance(pfx_bits - 11); + auto slice = cs.fetch_subslice(1, 1); + int n = (int)cs.fetch_ulong(10); + std::ostringstream os{name}; + os << ' ' << n << " ("; + slice->dump_hex(os, false); + os << ')'; + return os.str(); +} + +int compute_len_push_const_dict(const CellSlice& cs, unsigned args, int pfx_bits) { + if (!cs.have(pfx_bits, 1)) { + return 0; + } + return 0x10000 + pfx_bits; +} + +int exec_pfx_dict_get(VmState* st, int op, const char* name_suff) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute PFXDICTGET" << name_suff; + stack.check_underflow(3); + int n = stack.pop_smallint_range(PrefixDictionary::max_key_bits); + PrefixDictionary dict{stack.pop_maybe_cell(), n}; + auto cs = stack.pop_cellslice(); + auto res = dict.lookup_prefix(cs->data_bits(), cs->size()); + if (res.first.is_null()) { + if (op & 1) { + throw VmError{Excno::cell_und, "cannot parse a prefix belonging to a given prefix code dictionary"}; + } + stack.push_cellslice(std::move(cs)); + if (!op) { + stack.push_bool(false); + } + return 0; + } + stack.push_cellslice(cs.write().fetch_subslice(res.second)); + if (!(op & 2)) { + stack.push_cellslice(std::move(res.first)); + } + stack.push_cellslice(std::move(cs)); + if (!op) { + stack.push_bool(true); + return 0; + } + if (op == 1) { + return 0; + } + Ref cont{true, std::move(res.first), st->get_cp()}; + return op & 1 ? st->call(std::move(cont)) : st->jump(std::move(cont)); +} + +int exec_const_pfx_dict_switch(VmState* st, CellSlice& cs, unsigned args, int pfx_bits) { + if (!cs.have(pfx_bits)) { + throw VmError{Excno::inv_opcode, "not enough data bits for a PFXDICTSWITCH instruction"}; + } + if (!cs.have_refs(1)) { + throw VmError{Excno::inv_opcode, "not enough references for a PFXDICTSWITCH instruction"}; + } + Stack& stack = st->get_stack(); + cs.advance(pfx_bits - 11); + + auto dict_slice = cs.fetch_subslice(1, 1); + int n = (int)cs.fetch_ulong(10); + + VM_LOG(st) << "execute PFXDICTSWITCH " << n << " (" << dict_slice << ")"; + + PrefixDictionary dict{std::move(dict_slice), n}; + auto cs1 = stack.pop_cellslice(); + auto res = dict.lookup_prefix(cs1->data_bits(), cs1->size()); + + if (res.first.is_null()) { + stack.push_cellslice(std::move(cs1)); + return 0; + } else { + stack.push_cellslice(cs1.write().fetch_subslice(res.second)); + stack.push_cellslice(std::move(cs1)); + Ref cont{true, std::move(res.first), st->get_cp()}; + return st->jump(std::move(cont)); + } +} + +int exec_subdict_get(VmState* st, unsigned args) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute SUBDICT" << (args & 2 ? (args & 1 ? "U" : "I") : "") << (args & 4 ? "RP" : "") << "GET"; + stack.check_underflow(4); + int n = stack.pop_smallint_range(Dictionary::max_key_bits); + Dictionary dict{stack.pop_maybe_cell(), n}; + int mk = (args & 2 ? (args & 1 ? 256 : 257) : Dictionary::max_key_bits); + int k = stack.pop_smallint_range(std::min(mk, n)); + BitSlice key; + unsigned char buffer[Dictionary::max_key_bytes]; + if (args & 2) { + key = dict.integer_key(stack.pop_int(), k, !(args & 1), buffer, true); + } else { + key = stack.pop_cellslice()->prefetch_bits(k); + } + if (!key.is_valid()) { + throw VmError{Excno::cell_und, "not enough bits for a dictionary key prefix"}; + } + if (!dict.cut_prefix_subdict(key.bits(), k, args & 4)) { + throw VmError{Excno::dict_err, "cannot construct subdictionary by key prefix"}; + } + push_dict(stack, std::move(dict)); + return 0; +} + +void register_dictionary_ops(OpcodeTable& cp0) { + using namespace std::placeholders; + cp0.insert(OpcodeInstr::mksimple(0xf400, 16, "STDICT", exec_store_dict)) + .insert(OpcodeInstr::mksimple(0xf401, 16, "SKIPDICT", exec_skip_dict)) + .insert(OpcodeInstr::mksimple(0xf402, 16, "LDDICTS", std::bind(exec_load_dict_slice, _1, 0))) + .insert(OpcodeInstr::mksimple(0xf403, 16, "PLDDICTS", std::bind(exec_load_dict_slice, _1, 1))) + .insert(OpcodeInstr::mksimple(0xf404, 16, "LDDICT", std::bind(exec_load_dict, _1, 0))) + .insert(OpcodeInstr::mksimple(0xf405, 16, "PLDDICT", std::bind(exec_load_dict, _1, 1))) + .insert(OpcodeInstr::mksimple(0xf406, 16, "LDDICTQ", std::bind(exec_load_dict, _1, 2))) + .insert(OpcodeInstr::mksimple(0xf407, 16, "PLDDICTQ", std::bind(exec_load_dict, _1, 3))) + .insert(OpcodeInstr::mkfixedrange(0xf40a, 0xf410, 16, 3, std::bind(dump_dictop, _2, "GET"), exec_dict_get)) + .insert(OpcodeInstr::mkfixedrange(0xf412, 0xf418, 16, 3, std::bind(dump_dictop, _2, "SET"), + std::bind(exec_dict_set, _1, _2, Dictionary::SetMode::Set, "SET", false))) + .insert(OpcodeInstr::mkfixedrange(0xf41a, 0xf420, 16, 3, std::bind(dump_dictop, _2, "SETGET"), + std::bind(exec_dict_setget, _1, _2, Dictionary::SetMode::Set, "SETGET", false))) + .insert( + OpcodeInstr::mkfixedrange(0xf422, 0xf428, 16, 3, std::bind(dump_dictop, _2, "REPLACE"), + std::bind(exec_dict_set, _1, _2, Dictionary::SetMode::Replace, "REPLACE", false))) + .insert(OpcodeInstr::mkfixedrange( + 0xf42a, 0xf430, 16, 3, std::bind(dump_dictop, _2, "REPLACEGET"), + std::bind(exec_dict_setget, _1, _2, Dictionary::SetMode::Replace, "REPLACEGET", false))) + .insert(OpcodeInstr::mkfixedrange(0xf432, 0xf438, 16, 3, std::bind(dump_dictop, _2, "ADD"), + std::bind(exec_dict_set, _1, _2, Dictionary::SetMode::Add, "ADD", false))) + .insert(OpcodeInstr::mkfixedrange(0xf43a, 0xf440, 16, 3, std::bind(dump_dictop, _2, "ADDGET"), + std::bind(exec_dict_setget, _1, _2, Dictionary::SetMode::Add, "ADDGET", false))) + .insert(OpcodeInstr::mkfixedrange(0xf441, 0xf444, 16, 2, std::bind(dump_dictop2, _2, "SETB"), + std::bind(exec_dict_set, _1, _2, Dictionary::SetMode::Set, "SET", true))) + .insert(OpcodeInstr::mkfixedrange(0xf445, 0xf448, 16, 2, std::bind(dump_dictop2, _2, "SETGETB"), + std::bind(exec_dict_setget, _1, _2, Dictionary::SetMode::Set, "SETGET", true))) + .insert( + OpcodeInstr::mkfixedrange(0xf449, 0xf44c, 16, 2, std::bind(dump_dictop2, _2, "REPLACEB"), + std::bind(exec_dict_set, _1, _2, Dictionary::SetMode::Replace, "REPLACE", true))) + .insert(OpcodeInstr::mkfixedrange( + 0xf44d, 0xf450, 16, 2, std::bind(dump_dictop2, _2, "REPLACEGETB"), + std::bind(exec_dict_setget, _1, _2, Dictionary::SetMode::Replace, "REPLACEGET", true))) + .insert(OpcodeInstr::mkfixedrange(0xf451, 0xf454, 16, 2, std::bind(dump_dictop2, _2, "ADDB"), + std::bind(exec_dict_set, _1, _2, Dictionary::SetMode::Add, "ADD", true))) + .insert(OpcodeInstr::mkfixedrange(0xf455, 0xf458, 16, 2, std::bind(dump_dictop2, _2, "ADDGETB"), + std::bind(exec_dict_setget, _1, _2, Dictionary::SetMode::Add, "ADDGET", true))) + .insert(OpcodeInstr::mkfixedrange(0xf459, 0xf45c, 16, 2, std::bind(dump_dictop2, _2, "DEL"), exec_dict_delete)) + .insert( + OpcodeInstr::mkfixedrange(0xf462, 0xf468, 16, 3, std::bind(dump_dictop, _2, "DELGET"), exec_dict_deleteget)) + .insert(OpcodeInstr::mkfixedrange(0xf469, 0xf46c, 16, 2, std::bind(dump_dictop2, _2, "GETOPTREF"), + exec_dict_get_optref)) + .insert(OpcodeInstr::mkfixedrange(0xf46d, 0xf470, 16, 2, std::bind(dump_dictop2, _2, "SETGETOPTREF"), + exec_dict_setget_optref)) + .insert(OpcodeInstr::mksimple(0xf470, 16, "PFXDICTSET", + std::bind(exec_pfx_dict_set, _1, PrefixDictionary::SetMode::Set, "SET"))) + .insert(OpcodeInstr::mksimple(0xf471, 16, "PFXDICTREPLACE", + std::bind(exec_pfx_dict_set, _1, PrefixDictionary::SetMode::Replace, "REPLACE"))) + .insert(OpcodeInstr::mksimple(0xf472, 16, "PFXDICTADD", + std::bind(exec_pfx_dict_set, _1, PrefixDictionary::SetMode::Add, "ADD"))) + .insert(OpcodeInstr::mksimple(0xf473, 16, "PFXDICTDEL", exec_pfx_dict_delete)) + .insert(OpcodeInstr::mkfixedrange(0xf474, 0xf480, 16, 4, dump_dictop_getnear, exec_dict_getnear)) + .insert(OpcodeInstr::mkfixedrange(0xf482, 0xf488, 16, 5, std::bind(dump_dictop, _2, "MIN"), exec_dict_getmin)) + .insert(OpcodeInstr::mkfixedrange(0xf48a, 0xf490, 16, 5, std::bind(dump_dictop, _2, "MAX"), exec_dict_getmin)) + .insert(OpcodeInstr::mkfixedrange(0xf492, 0xf498, 16, 5, std::bind(dump_dictop, _2, "REMMIN"), exec_dict_getmin)) + .insert(OpcodeInstr::mkfixedrange(0xf49a, 0xf4a0, 16, 5, std::bind(dump_dictop, _2, "REMMAX"), exec_dict_getmin)) + .insert(OpcodeInstr::mkfixed(0xf4a0 >> 2, 14, 2, dump_dict_get_exec, exec_dict_get_exec)) + .insert(OpcodeInstr::mkextrange(0xf4a400, 0xf4a800, 24, 11, + std::bind(dump_push_const_dict, _1, _3, "DICTPUSHCONST"), exec_push_const_dict, + compute_len_push_const_dict)) + .insert(OpcodeInstr::mksimple(0xf4a8, 16, "PFXDICTGETQ", std::bind(exec_pfx_dict_get, _1, 0, "Q"))) + .insert(OpcodeInstr::mksimple(0xf4a9, 16, "PFXDICTGET", std::bind(exec_pfx_dict_get, _1, 1, ""))) + .insert(OpcodeInstr::mksimple(0xf4aa, 16, "PFXDICTGETJMP", std::bind(exec_pfx_dict_get, _1, 2, "JMP"))) + .insert(OpcodeInstr::mksimple(0xf4ab, 16, "PFXDICTGETEXEC", std::bind(exec_pfx_dict_get, _1, 3, "EXEC"))) + .insert(OpcodeInstr::mkextrange(0xf4ac00, 0xf4b000, 24, 11, + std::bind(dump_push_const_dict, _1, _3, "PFXDICTSWITCH"), + exec_const_pfx_dict_switch, compute_len_push_const_dict)) + .insert(OpcodeInstr::mkfixedrange(0xf4b1, 0xf4b4, 16, 3, std::bind(dump_subdictop2, _2, "GET"), exec_subdict_get)) + .insert( + OpcodeInstr::mkfixedrange(0xf4b5, 0xf4b8, 16, 3, std::bind(dump_subdictop2, _2, "RPGET"), exec_subdict_get)); +} + +} // namespace vm diff --git a/crypto/vm/dictops.h b/crypto/vm/dictops.h new file mode 100644 index 00000000..d555cf7a --- /dev/null +++ b/crypto/vm/dictops.h @@ -0,0 +1,27 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +namespace vm { + +class OpcodeTable; + +void register_dictionary_ops(OpcodeTable& cp0); + +} // namespace vm diff --git a/crypto/vm/dispatch.cpp b/crypto/vm/dispatch.cpp new file mode 100644 index 00000000..741982dc --- /dev/null +++ b/crypto/vm/dispatch.cpp @@ -0,0 +1,71 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "vm/dispatch.h" +#include "vm/excno.hpp" +#include +#include +#include + +namespace vm { + +namespace { +std::mutex dispatch_tables_mutex; +std::map dispatch_tables; +} // namespace + +DummyDispatchTable dummy_dispatch_table; + +bool DispatchTable::register_table(Codepage _cp, const DispatchTable& dt) { + assert(dt.is_final()); + int cp = (int)_cp; + if (cp < -0x8000 || cp >= 0x8000 || cp == -1) { + return false; + } else { + std::lock_guard guard(dispatch_tables_mutex); + return dispatch_tables.emplace(cp, &dt).second; + } +} + +bool DispatchTable::register_table(Codepage cp) const { + return register_table(cp, *this); +} + +const DispatchTable* DispatchTable::get_table(Codepage cp) { + return get_table((int)cp); +} + +const DispatchTable* DispatchTable::get_table(int cp) { + std::lock_guard guard(dispatch_tables_mutex); + auto entry = dispatch_tables.find(cp); + return entry != dispatch_tables.end() ? entry->second : 0; +} + +int DummyDispatchTable::dispatch(VmState* st, CellSlice& cs) const { + throw VmError{Excno::inv_opcode, "empty opcode table"}; +} + +std::string DummyDispatchTable::dump_instr(CellSlice& cs) const { + return ""; +} + +int DummyDispatchTable::instr_len(const CellSlice& cs) const { + return 0; +} + +} // namespace vm diff --git a/crypto/vm/dispatch.h b/crypto/vm/dispatch.h new file mode 100644 index 00000000..509ecfca --- /dev/null +++ b/crypto/vm/dispatch.h @@ -0,0 +1,62 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once +#include + +namespace vm { + +class VmState; +class CellSlice; + +enum class Codepage { test_cp = 0 }; + +class DispatchTable { + public: + DispatchTable() = default; + virtual ~DispatchTable() = default; + virtual int dispatch(VmState* st, CellSlice& cs) const = 0; + virtual std::string dump_instr(CellSlice& cs) const = 0; + virtual int instr_len(const CellSlice& cs) const = 0; + virtual DispatchTable* finalize() = 0; + virtual bool is_final() const = 0; + static const DispatchTable* get_table(Codepage cp); + static const DispatchTable* get_table(int cp); + static bool register_table(Codepage cp, const DispatchTable& dt); + bool register_table(Codepage cp) const; +}; + +class DummyDispatchTable : public DispatchTable { + public: + DummyDispatchTable() : DispatchTable() { + } + ~DummyDispatchTable() override = default; + int dispatch(VmState* st, CellSlice& cs) const override; + std::string dump_instr(CellSlice& cs) const override; + int instr_len(const CellSlice& cs) const override; + DispatchTable* finalize() override { + return this; + } + bool is_final() const override { + return true; + } +}; + +extern DummyDispatchTable dummy_dispatch_table; + +} // namespace vm diff --git a/crypto/vm/excno.hpp b/crypto/vm/excno.hpp new file mode 100644 index 00000000..f67d8a4e --- /dev/null +++ b/crypto/vm/excno.hpp @@ -0,0 +1,98 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +namespace vm { + +enum class Excno : int { + none = 0, + alt = 1, + stk_und = 2, + stk_ov = 3, + int_ov = 4, + range_chk = 5, + inv_opcode = 6, + type_chk = 7, + cell_ov = 8, + cell_und = 9, + dict_err = 10, + unknown = 11, + fatal = 12, + out_of_gas = 13, + virt_err = 14, + total +}; + +const char* get_exception_msg(Excno exc_no); + +class VmError { + Excno exc_no; + const char* msg; + long long arg; + + public: + VmError(Excno _excno, const char* _msg) : exc_no(_excno), msg(_msg), arg(0) { + } + VmError(Excno _excno) : exc_no(_excno), msg(0), arg(0) { + } + VmError(Excno _excno, const char* _msg, long long _arg) : exc_no(_excno), msg(_msg), arg(_arg) { + } + int get_errno() const { + return static_cast(exc_no); + } + const char* get_msg() const { + return msg ? msg : get_exception_msg(exc_no); + } + long long get_arg() const { + return arg; + } +}; + +struct VmNoGas { + VmNoGas() = default; + int get_errno() const { + return static_cast(Excno::out_of_gas); + } + const char* get_msg() const { + return "out of gas"; + } + operator VmError() const { + return VmError{Excno::out_of_gas, "out of gas"}; + } +}; + +struct VmVirtError { + int virtualization{0}; + VmVirtError() = default; + VmVirtError(int virtualization) : virtualization(virtualization) { + } + int get_errno() const { + return static_cast(Excno::virt_err); + } + const char* get_msg() const { + return "prunned branch"; + } + operator VmError() const { + return VmError{Excno::virt_err, "prunned branch", virtualization}; + } +}; + +struct VmFatal {}; + +} // namespace vm diff --git a/crypto/vm/fmt.hpp b/crypto/vm/fmt.hpp new file mode 100644 index 00000000..13096fbe --- /dev/null +++ b/crypto/vm/fmt.hpp @@ -0,0 +1,503 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once +#include "common/refint.h" +#include "vm/cells.h" +#include "vm/cellslice.h" +#include "vm/cellparse.hpp" +#include + +namespace vm { + +namespace fmt { + +// main idea: use cs >> i(32,x) >> ... or cs >> i32(x) to deserialize integers, instead of cs.write().fetch_long(32, true) +// and cb << i(32,x+y) or cb << i32(x+y) will serialize 32-bit integers + +// i, u, i16, u16, i32, u32, ub=u1, ib=i1 for integers + +template +class ConstInt { + int bits; + T value; + + public: + ConstInt(int _bits, T _val) : bits(_bits), value(_val) { + } + bool serialize(CellBuilder& cb) const { + if (C) { + return (S ? cb.store_long_rchk_bool(value, bits) : cb.store_ulong_rchk_bool(value, bits)); + } else { + return cb.store_long_bool(value, bits); + } + } + bool deserialize(CellSlice& cs) const { + if (S) { + long long x; + return cs.fetch_long_bool(bits, x) && x == value; + } else { + unsigned long long x; + return cs.fetch_ulong_bool(bits, x) && x == value; + } + } +}; + +template +class Int { + int bits; + T& value; + + public: + Int(int _bits, T& _val) : bits(_bits), value(_val) { + } + bool deserialize(CellSlice& cs) const { + if (S) { + long long x; + if (cs.fetch_long_bool(bits, x)) { + value = static_cast(x); + return true; + } + } else { + unsigned long long x; + if (cs.fetch_ulong_bool(bits, x)) { + value = static_cast(x); + return true; + } + } + return false; + } + bool serialize(CellBuilder& cb) const { + if (C) { + return S ? cb.store_long_rchk_bool(value, bits) : cb.store_ulong_rchk_bool(value, bits); + } else { + return cb.store_long_bool(value, bits); + } + } +}; + +template +class PrefetchInt { + int bits; + T& value; + + public: + PrefetchInt(int _bits, T& _val) : bits(_bits), value(_val) { + } + bool deserialize(CellSlice& cs) const { + if (S) { + long long x; + if (cs.prefetch_long_bool(bits, x)) { + value = static_cast(x); + return true; + } + } else { + unsigned long long x; + if (cs.prefetch_ulong_bool(bits, x)) { + value = static_cast(x); + return true; + } + } + return false; + } +}; + +template +class ConstRefInt { + int bits; + const td::RefInt256& value; + + public: + ConstRefInt(int _bits, const td::RefInt256& _val) : bits(_bits), value(_val) { + } + bool serialize(CellBuilder& cb) const { + return value.not_null() && cb.store_int256_bool(*value, bits, S); + } +}; + +template +class ConstRefIntVal { + int bits; + td::RefInt256 value; + + public: + ConstRefIntVal(int _bits, const td::RefInt256& _val) : bits(_bits), value(_val) { + } + ConstRefIntVal(int _bits, td::RefInt256&& _val) : bits(_bits), value(std::move(_val)) { + } + bool serialize(CellBuilder& cb) const { + return value.not_null() && cb.store_int256_bool(*value, bits, S); + } +}; + +template +class ConstBigInt { + int bits; + const td::BigInt256& value; + + public: + ConstBigInt(int _bits, const td::BigInt256& _val) : bits(_bits), value(_val) { + } + bool serialize(CellBuilder& cb) const { + return cb.store_int256_bool(value, bits, S); + } +}; + +template +class RefInt { + int bits; + td::RefInt256& value; + + public: + RefInt(int _bits, td::RefInt256& _val) : bits(_bits), value(_val) { + } + bool deserialize(CellSlice& cs) const { + value = cs.fetch_int256(bits, S); + return value.not_null(); + } + bool serialize(CellBuilder& cb) const { + return value.not_null() && cb.store_int256_bool(*value, bits, S); + } +}; + +inline ConstRefInt i(int l, const td::RefInt256& val) { + return {l, val}; +} + +inline ConstRefIntVal i(int l, td::RefInt256&& val) { + return {l, std::move(val)}; +} + +inline ConstBigInt i(int l, const td::BigInt256& val) { + return {l, val}; +} + +inline RefInt i(int l, td::RefInt256& val) { + return {l, val}; +} + +inline ConstRefInt u(int l, const td::RefInt256& val) { + return {l, val}; +} + +inline ConstRefIntVal u(int l, td::RefInt256&& val) { + return {l, std::move(val)}; +} + +inline ConstBigInt u(int l, const td::BigInt256& val) { + return {l, val}; +} + +inline RefInt u(int l, td::RefInt256& val) { + return {l, val}; +} + +template +const ConstInt i(int l, const T& val) { + return {l, val}; +} + +template +Int i(int l, T& val) { + return {l, val}; +} + +template +PrefetchInt pi(int l, T& val) { + return {l, val}; +} + +template +const ConstInt u(int l, const T& val) { + return {l, val}; +} + +template +Int u(int l, T& val) { + return {l, val}; +} + +template +PrefetchInt pu(int l, T& val) { + return {l, val}; +} + +template +const ConstInt iw(int l, const T& val) { + return {l, val}; +} + +template +Int iw(int l, T& val) { + return {l, val}; +} + +inline ConstInt ib(bool flag) { + return {1, flag}; +} + +template +Int ib(T& val) { + return {1, val}; +} + +template +PrefetchInt pib(T& val) { + return {1, val}; +} + +inline ConstInt ub(bool flag) { + return {1, flag}; +} + +template +Int ub(T& val) { + return {1, val}; +} + +template +PrefetchInt pub(T& val) { + return {1, val}; +} + +inline ConstInt i8(signed char val) { + return {8, val}; +} + +template +Int i8(T& val) { + return {8, val}; +} + +inline ConstInt u8(unsigned char val) { + return {8, val}; +} + +template +Int u8(T& val) { + return {8, val}; +} + +inline ConstInt i16(short val) { + return {16, val}; +} + +template +Int i16(T& val) { + static_assert(sizeof(T) >= 2, "i16 needs at least 16-bit integer variable as a result"); + return {16, val}; +} + +inline ConstInt u16(unsigned short val) { + return {16, val}; +} + +template +Int u16(T& val) { + static_assert(sizeof(T) >= 2, "u16 needs at least 16-bit integer variable as a result"); + return {16, val}; +} + +template +const ConstInt i32(const T& val) { + return {32, val}; +} + +template +Int i32(T& val) { + static_assert(sizeof(T) >= 4, "i32 needs at least 32-bit integer variable as a result"); + return {32, val}; +} + +template +PrefetchInt pi32(T& val) { + static_assert(sizeof(T) >= 4, "pi32 needs at least 32-bit integer variable as a result"); + return {32, val}; +} + +template +const ConstInt u32(const T& val) { + return {32, val}; +} + +template +Int u32(T& val) { + static_assert(sizeof(T) >= 4, "u32 needs at least 32-bit integer variable as a result"); + return {32, val}; +} + +template +PrefetchInt pu32(T& val) { + static_assert(sizeof(T) >= 4, "pu32 needs at least 32-bit integer variable as a result"); + return {32, val}; +} + +template +const ConstInt i64(const T& val) { + return {64, val}; +} + +template +Int i64(T& val) { + static_assert(sizeof(T) >= 8, "i64 needs 64-bit integer variable as a result"); + return {64, val}; +} + +template +const ConstInt u64(const T& val) { + return {64, val}; +} + +template +Int u64(T& val) { + static_assert(sizeof(T) >= 8, "u64 needs 64-bit integer variable as a result"); + return {64, val}; +} + +/* + * + * non-integer types + * + */ + +// cr(Ref& cell_ref) for (de)serializing cell references + +class ConstCellRef { + const td::Ref& value; + + public: + ConstCellRef(const td::Ref& _val) : value(_val) { + } + bool serialize(CellBuilder& cb) const { + return cb.store_ref_bool(value); + } +}; + +class ConstCellRefVal { + td::Ref value; + + public: + ConstCellRefVal(const td::Ref& _val) : value(_val) { + } + ConstCellRefVal(td::Ref&& _val) : value(std::move(_val)) { + } + bool serialize(CellBuilder& cb) const { + return cb.store_ref_bool(std::move(value)); + } +}; + +class CellRefFmt { + td::Ref& value; + + public: + CellRefFmt(td::Ref& _val) : value(_val) { + } + bool deserialize(CellSlice& cs) const { + value = cs.fetch_ref(); + return value.not_null(); + } +}; + +inline ConstCellRef cr(const td::Ref& val) { + return {val}; +} + +inline ConstCellRefVal cr(td::Ref&& val) { + return {std::move(val)}; +} + +inline CellRefFmt cr(td::Ref& val) { + return {val}; +} + +// skip(n) will skip n bits + +class SkipFmt { + int bits; + + public: + explicit SkipFmt(int _bits) : bits(_bits) { + } + bool deserialize(CellSlice& cs) const { + return cs.advance(bits); + } +}; + +inline SkipFmt skip(int bits) { + return SkipFmt{bits}; +} + +// end will throw an exception if any bits or references remain, or if a previous operation failed +// ends similar, but checks only bits + +class ChkEnd { + public: + explicit ChkEnd() = default; + bool deserialize(CellSlice& cs) const { + return (cs.empty() && !cs.size_refs()); + } +}; + +class ChkEndS { + public: + explicit ChkEndS() = default; + bool deserialize(CellSlice& cs) const { + return cs.empty(); + } +}; + +template +class Chk { + Cond cond; + + public: + template + explicit constexpr Chk(Args... args) : cond(args...){}; + bool deserialize_ext(CellSlice& cs, bool state) const { + if (!state || !cond.deserialize(cs)) { + cs.error(); + } + return true; + } +}; + +class ChkOk { + public: + explicit ChkOk() = default; + bool deserialize_ext(CellSlice& cs, bool state) const { + if (!state) { + cs.error(); + } + return true; + } +}; + +constexpr ChkEnd end = ChkEnd{}; +constexpr ChkEndS ends = ChkEndS{}; +constexpr Chk okend = Chk{}; +constexpr Chk okends = Chk{}; +constexpr Chk oke = Chk{}; +constexpr ChkOk ok = ChkOk{}; + +inline ::vm::CellParser parser(CellSlice& cs) { + return ::vm::CellParser{cs}; +} + +} // namespace fmt + +} // namespace vm diff --git a/crypto/vm/log.h b/crypto/vm/log.h new file mode 100644 index 00000000..e0631411 --- /dev/null +++ b/crypto/vm/log.h @@ -0,0 +1,53 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +#include "td/utils/logging.h" + +#define VM_LOG_IMPL(st, mask) \ + LOG_IMPL_FULL(get_log_interface(st), get_log_options(st), DEBUG, VERBOSITY_NAME(DEBUG), \ + (get_log_mask(st) & mask) != 0, "") + +#define VM_LOG(st) VM_LOG_IMPL(st, 1) +#define VM_LOG_MASK(st, mask) VM_LOG_IMPL(st, mask) + +namespace vm { +struct VmLog { + td::LogInterface *log_interface{td::log_interface}; + td::LogOptions log_options{td::log_options}; + enum { DumpStack = 2 }; + int log_mask{1}; +}; + +template +td::LogInterface &get_log_interface(State *st) { + return st ? *st->get_log().log_interface : *::td::log_interface; +} + +template +auto get_log_options(State *st) { + return st ? st->get_log().log_options : ::td::log_options; +} + +template +auto get_log_mask(State *st) { + return st ? st->get_log().log_mask : 1; +} + +} // namespace vm diff --git a/crypto/vm/opctable.cpp b/crypto/vm/opctable.cpp new file mode 100644 index 00000000..fbdc2578 --- /dev/null +++ b/crypto/vm/opctable.cpp @@ -0,0 +1,450 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include +#include +#include "vm/opctable.h" +#include "vm/cellslice.h" +#include "vm/excno.hpp" +#include "vm/continuation.h" +#include +#include +#include +#include + +#include "td/utils/format.h" + +namespace vm { + +DispatchTable* OpcodeTable::finalize() { + if (final) { + return this; + } + instruction_list.clear(); + instruction_list.reserve(instructions.size() * 2 + 1); + + unsigned upto = 0; + for (const auto& x : instructions) { + auto range = x.second->get_opcode_range(); + assert(range.first == x.first); + assert(range.first < range.second); + assert(range.first >= upto); + assert(range.second <= top_opcode); + if (range.first > upto) { + instruction_list.emplace_back(upto, new OpcodeInstrDummy{upto, range.first}); + } + instruction_list.emplace_back(x); + upto = range.second; + } + + if (upto < top_opcode) { + instruction_list.emplace_back(upto, new OpcodeInstrDummy{upto, top_opcode}); + } + + instruction_list.shrink_to_fit(); + final = true; + return this; +} + +OpcodeTable& OpcodeTable::insert(const OpcodeInstr* instr) { + LOG_IF(FATAL, !insert_bool(instr)) << td::format::lambda([&](auto& sb) { + sb << "cannot insert instruction into table " << name << ": "; + if (!instr) { + sb << "instruction is null"; + } else if (final) { + sb << "instruction table already finalized"; + } else { + auto range = instr->get_opcode_range(); + sb << "opcode range " << td::format::as_hex(range.first) << ".." << td::format::as_hex(range.second - 1) + << " already occupied or invalid"; + } + }); + return *this; +} + +bool OpcodeTable::insert_bool(const OpcodeInstr* instr) { + if (!instr || final) { + return false; + } + auto range = instr->get_opcode_range(); + assert(range.first < range.second); + assert(range.second <= top_opcode); + auto it = instructions.lower_bound(range.first); + if (it != instructions.end() && it->first < range.second) { + return false; + } + if (it != instructions.begin()) { + auto prev_range = std::prev(it)->second->get_opcode_range(); + assert(prev_range.first < prev_range.second); + assert(prev_range.first == std::prev(it)->first); + if (prev_range.second > range.first) { + return false; + } + } + instructions.emplace_hint(it, range.first, instr); + return true; +} + +const OpcodeInstr* OpcodeTable::lookup_instr(unsigned opcode, unsigned bits) const { + std::size_t i = 0, j = instruction_list.size(); + assert(j); + while (j - i > 1) { + auto k = ((j + i) >> 1); + if (instruction_list[k].first <= opcode) { + i = k; + } else { + j = k; + } + } + return instruction_list[i].second; +} + +const OpcodeInstr* OpcodeTable::lookup_instr(const CellSlice& cs, unsigned& opcode, unsigned& bits) const { + bits = max_opcode_bits; + unsigned long long prefetch = cs.prefetch_ulong_top(bits); + opcode = (unsigned)(prefetch >> (64 - max_opcode_bits)); + opcode &= (static_cast(static_cast(-1) << max_opcode_bits) >> bits); + return lookup_instr(opcode, bits); +} + +int OpcodeTable::dispatch(VmState* st, CellSlice& cs) const { + assert(final); + unsigned bits, opcode; + auto instr = lookup_instr(cs, opcode, bits); + //std::cerr << "lookup_instr: cs.size()=" << cs.size() << "; bits=" << bits << "; opcode=" << std::setw(6) << std::setfill('0') << std::hex << opcode << std::dec << std::endl; + return instr->dispatch(st, cs, opcode, bits); +} + +std::string OpcodeTable::dump_instr(CellSlice& cs) const { + assert(final); + unsigned bits, opcode; + auto instr = lookup_instr(cs, opcode, bits); + return instr->dump(cs, opcode, bits); +} + +int OpcodeTable::instr_len(const CellSlice& cs) const { + assert(final); + unsigned bits, opcode; + auto instr = lookup_instr(cs, opcode, bits); + return instr->instr_len(cs, opcode, bits); +} + +OpcodeInstr::OpcodeInstr(unsigned _opcode, unsigned _bits, bool) + : min_opcode(_opcode << (max_opcode_bits - _bits)), max_opcode((_opcode + 1) << (max_opcode_bits - _bits)) { + assert(_opcode < (1U << _bits) && _bits <= max_opcode_bits); +} + +int OpcodeInstrDummy::dispatch(VmState* st, CellSlice& cs, unsigned opcode, unsigned bits) const { + st->consume_gas(gas_per_instr); + throw VmError{Excno::inv_opcode, "invalid opcode", opcode}; +} + +std::string OpcodeInstr::dump(CellSlice& cs, unsigned opcode, unsigned bits) const { + return ""; +} + +int OpcodeInstr::instr_len(const CellSlice& cs, unsigned opcode, unsigned bits) const { + return 0; +} + +OpcodeInstrSimple::OpcodeInstrSimple(unsigned opcode, unsigned _opc_bits, std::string _name, exec_instr_func_t exec) + : OpcodeInstr(opcode, _opc_bits, false) + , opc_bits(static_cast(_opc_bits)) + , name(_name) + , exec_instr(exec) { +} + +int OpcodeInstrSimple::dispatch(VmState* st, CellSlice& cs, unsigned opcode, unsigned bits) const { + st->consume_gas(gas_per_instr + opc_bits * gas_per_bit); + if (bits < opc_bits) { + throw VmError{Excno::inv_opcode, "invalid or too short opcode", opcode + (bits << max_opcode_bits)}; + } + cs.advance(opc_bits); + return exec_instr(st, cs, opcode >> (max_opcode_bits - opc_bits), opc_bits); +} + +std::string OpcodeInstrSimple::dump(CellSlice& cs, unsigned opcode, unsigned bits) const { + if (bits < opc_bits) { + return ""; + } + cs.advance(opc_bits); + return name; +} + +int OpcodeInstrSimple::instr_len(const CellSlice& cs, unsigned opcode, unsigned bits) const { + if (bits < opc_bits) { + return 0; + } else { + return opc_bits; + } +} + +OpcodeInstrSimplest::OpcodeInstrSimplest(unsigned opcode, unsigned _opc_bits, std::string _name, + exec_simple_instr_func_t exec) + : OpcodeInstr(opcode, _opc_bits, false) + , opc_bits(static_cast(_opc_bits)) + , name(_name) + , exec_instr(exec) { +} + +int OpcodeInstrSimplest::dispatch(VmState* st, CellSlice& cs, unsigned opcode, unsigned bits) const { + st->consume_gas(gas_per_instr + opc_bits * gas_per_bit); + if (bits < opc_bits) { + throw VmError{Excno::inv_opcode, "invalid or too short opcode", opcode + (bits << max_opcode_bits)}; + } + cs.advance(opc_bits); + return exec_instr(st); +} + +std::string OpcodeInstrSimplest::dump(CellSlice& cs, unsigned opcode, unsigned bits) const { + if (bits < opc_bits) { + return ""; + } + cs.advance(opc_bits); + return name; +} + +int OpcodeInstrSimplest::instr_len(const CellSlice& cs, unsigned opcode, unsigned bits) const { + if (bits < opc_bits) { + return 0; + } else { + return opc_bits; + } +} + +OpcodeInstrFixed::OpcodeInstrFixed(unsigned opcode, unsigned _opc_bits, unsigned _arg_bits, dump_arg_instr_func_t dump, + exec_arg_instr_func_t exec) + : OpcodeInstr(opcode, _opc_bits, false) + , opc_bits(static_cast(_opc_bits)) + , tot_bits(static_cast(_opc_bits + _arg_bits)) + , dump_instr(dump) + , exec_instr(exec) { + assert(_arg_bits <= max_opcode_bits && _opc_bits <= max_opcode_bits && _arg_bits + _opc_bits <= max_opcode_bits); +} + +OpcodeInstrFixed::OpcodeInstrFixed(unsigned opcode_min, unsigned opcode_max, unsigned _tot_bits, unsigned _arg_bits, + dump_arg_instr_func_t dump, exec_arg_instr_func_t exec) + : OpcodeInstr(opcode_min << (max_opcode_bits - _tot_bits), opcode_max << (max_opcode_bits - _tot_bits)) + , opc_bits(static_cast(_tot_bits - _arg_bits)) + , tot_bits(static_cast(_tot_bits)) + , dump_instr(dump) + , exec_instr(exec) { + assert(_arg_bits <= _tot_bits && _tot_bits <= max_opcode_bits); + assert(opcode_min < opcode_max && opcode_max <= (1U << _tot_bits)); +} + +int OpcodeInstrFixed::dispatch(VmState* st, CellSlice& cs, unsigned opcode, unsigned bits) const { + st->consume_gas(gas_per_instr + tot_bits * gas_per_bit); + if (bits < tot_bits) { + throw VmError{Excno::inv_opcode, "invalid or too short opcode", opcode + (bits << max_opcode_bits)}; + } + cs.advance(tot_bits); + return exec_instr(st, opcode >> (max_opcode_bits - tot_bits)); +} + +std::string OpcodeInstrFixed::dump(CellSlice& cs, unsigned opcode, unsigned bits) const { + if (bits < tot_bits) { + return ""; + } + cs.advance(tot_bits); + return dump_instr(cs, opcode >> (max_opcode_bits - tot_bits)); +} + +int OpcodeInstrFixed::instr_len(const CellSlice& cs, unsigned opcode, unsigned bits) const { + if (bits < tot_bits) { + return 0; + } else { + return tot_bits; + } +} + +OpcodeInstrExt::OpcodeInstrExt(unsigned opcode, unsigned _opc_bits, unsigned _arg_bits, dump_instr_func_t dump, + exec_instr_func_t exec, compute_instr_len_func_t comp_len) + : OpcodeInstr(opcode, _opc_bits, false) + , opc_bits(static_cast(_opc_bits)) + , tot_bits(static_cast(_opc_bits + _arg_bits)) + , dump_instr(dump) + , exec_instr(exec) + , compute_instr_len(comp_len) { + assert(_arg_bits <= max_opcode_bits && _opc_bits <= max_opcode_bits && _arg_bits + _opc_bits <= max_opcode_bits); +} + +OpcodeInstrExt::OpcodeInstrExt(unsigned opcode_min, unsigned opcode_max, unsigned _tot_bits, unsigned _arg_bits, + dump_instr_func_t dump, exec_instr_func_t exec, compute_instr_len_func_t comp_len) + : OpcodeInstr(opcode_min << (max_opcode_bits - _tot_bits), opcode_max << (max_opcode_bits - _tot_bits)) + , opc_bits(static_cast(_tot_bits - _arg_bits)) + , tot_bits(static_cast(_tot_bits)) + , dump_instr(dump) + , exec_instr(exec) + , compute_instr_len(comp_len) { + assert(_arg_bits <= _tot_bits && _tot_bits <= max_opcode_bits); + assert(opcode_min < opcode_max && opcode_max <= (1U << _tot_bits)); +} + +int OpcodeInstrExt::dispatch(VmState* st, CellSlice& cs, unsigned opcode, unsigned bits) const { + st->consume_gas(gas_per_instr + tot_bits * gas_per_bit); + if (bits < tot_bits) { + throw VmError{Excno::inv_opcode, "invalid or too short opcode", opcode + (bits << max_opcode_bits)}; + } + return exec_instr(st, cs, opcode >> (max_opcode_bits - tot_bits), tot_bits); +} + +std::string OpcodeInstrExt::dump(CellSlice& cs, unsigned opcode, unsigned bits) const { + if (bits < tot_bits) { + return ""; + } + return dump_instr(cs, opcode >> (max_opcode_bits - tot_bits), (int)tot_bits); +} + +int OpcodeInstrExt::instr_len(const CellSlice& cs, unsigned opcode, unsigned bits) const { + if (bits < tot_bits) { + return 0; + } else { + return compute_instr_len(cs, opcode >> (max_opcode_bits - tot_bits), (int)tot_bits); + } +} + +/* +OpcodeInstr* OpcodeInstr::mksimple(unsigned opcode, unsigned opc_bits, std::string _name, exec_instr_func_t exec) { + return new OpcodeInstrSimple(opcode, opc_bits, _name, exec); +} +*/ + +OpcodeInstr* OpcodeInstr::mksimple(unsigned opcode, unsigned opc_bits, std::string _name, + exec_simple_instr_func_t exec) { + return new OpcodeInstrSimplest(opcode, opc_bits, _name, exec); +} + +OpcodeInstr* OpcodeInstr::mkfixed(unsigned opcode, unsigned opc_bits, unsigned arg_bits, dump_arg_instr_func_t dump, + exec_arg_instr_func_t exec) { + return new OpcodeInstrFixed(opcode, opc_bits, arg_bits, dump, exec); +} + +OpcodeInstr* OpcodeInstr::mkfixedrange(unsigned opcode_min, unsigned opcode_max, unsigned tot_bits, unsigned arg_bits, + dump_arg_instr_func_t dump, exec_arg_instr_func_t exec) { + return new OpcodeInstrFixed(opcode_min, opcode_max, tot_bits, arg_bits, dump, exec); +} + +OpcodeInstr* OpcodeInstr::mkext(unsigned opcode, unsigned opc_bits, unsigned arg_bits, dump_instr_func_t dump, + exec_instr_func_t exec, compute_instr_len_func_t comp_len) { + return new OpcodeInstrExt(opcode, opc_bits, arg_bits, dump, exec, comp_len); +} + +OpcodeInstr* OpcodeInstr::mkextrange(unsigned opcode_min, unsigned opcode_max, unsigned tot_bits, unsigned arg_bits, + dump_instr_func_t dump, exec_instr_func_t exec, + compute_instr_len_func_t comp_len) { + return new OpcodeInstrExt(opcode_min, opcode_max, tot_bits, arg_bits, dump, exec, comp_len); +} + +namespace instr { + +using namespace std::placeholders; + +dump_arg_instr_func_t dump_1sr(std::string prefix, std::string suffix) { + return [prefix, suffix](CellSlice&, unsigned args) -> std::string { + std::ostringstream os{prefix}; + os << 's' << (args & 15) << suffix; + return os.str(); + }; +} + +dump_arg_instr_func_t dump_1sr_l(std::string prefix, std::string suffix) { + return [prefix, suffix](CellSlice&, unsigned args) -> std::string { + std::ostringstream os{prefix}; + os << 's' << (args & 255) << suffix; + return os.str(); + }; +} + +dump_arg_instr_func_t dump_2sr(std::string prefix, std::string suffix) { + return [prefix, suffix](CellSlice&, unsigned args) -> std::string { + std::ostringstream os{prefix}; + os << 's' << ((args >> 4) & 15) << ",s" << (args & 15) << suffix; + return os.str(); + }; +} + +dump_arg_instr_func_t dump_2sr_adj(unsigned adj, std::string prefix, std::string suffix) { + return [adj, prefix, suffix](CellSlice&, unsigned args) -> std::string { + std::ostringstream os{prefix}; + os << 's' << (int)((args >> 4) & 15) - (int)((adj >> 4) & 15) << ",s" << (int)(args & 15) - (int)(adj & 15) + << suffix; + return os.str(); + }; +} + +dump_arg_instr_func_t dump_3sr(std::string prefix, std::string suffix) { + return [prefix, suffix](CellSlice&, unsigned args) -> std::string { + std::ostringstream os{prefix}; + os << 's' << ((args >> 8) & 15) << ",s" << ((args >> 4) & 15) << ",s" << (args & 15) << suffix; + return os.str(); + }; +} + +dump_arg_instr_func_t dump_3sr_adj(unsigned adj, std::string prefix, std::string suffix) { + return [adj, prefix, suffix](CellSlice&, unsigned args) -> std::string { + std::ostringstream os{prefix}; + os << 's' << (int)((args >> 8) & 15) - (int)((adj >> 8) & 15) << ",s" + << (int)((args >> 4) & 15) - (int)((adj >> 4) & 15) << ",s" << (int)(args & 15) - (int)(adj & 15) << suffix; + return os.str(); + }; +} + +dump_arg_instr_func_t dump_1c(std::string prefix, std::string suffix) { + return [prefix, suffix](CellSlice&, unsigned args) -> std::string { + std::ostringstream os{prefix}; + os << (args & 15) << suffix; + return os.str(); + }; +} + +dump_arg_instr_func_t dump_1c_l_add(int adj, std::string prefix, std::string suffix) { + return [adj, prefix, suffix](CellSlice&, unsigned args) -> std::string { + std::ostringstream os{prefix}; + os << (int)(args & 255) + adj << suffix; + return os.str(); + }; +} + +dump_arg_instr_func_t dump_1c_and(unsigned mask, std::string prefix, std::string suffix) { + return [mask, prefix, suffix](CellSlice&, unsigned args) -> std::string { + std::ostringstream os{prefix}; + os << (args & mask) << suffix; + return os.str(); + }; +} + +dump_arg_instr_func_t dump_2c(std::string prefix, std::string interfix, std::string suffix) { + return [prefix, interfix, suffix](CellSlice&, unsigned args) -> std::string { + std::ostringstream os{prefix}; + os << ((args >> 4) & 15) << interfix << (args & 15) << suffix; + return os.str(); + }; +} + +dump_arg_instr_func_t dump_2c_add(unsigned add, std::string prefix, std::string interfix, std::string suffix) { + return [add, prefix, interfix, suffix](CellSlice&, unsigned args) -> std::string { + std::ostringstream os{prefix}; + os << ((args >> 4) & 15) + ((add >> 4) & 15) << interfix << (args & 15) + (add & 15) << suffix; + return os.str(); + }; +} + +} // namespace instr + +} // namespace vm diff --git a/crypto/vm/opctable.h b/crypto/vm/opctable.h new file mode 100644 index 00000000..d441fd78 --- /dev/null +++ b/crypto/vm/opctable.h @@ -0,0 +1,191 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once +#include "vm/dispatch.h" +#include +#include +#include +#include + +namespace vm { + +typedef std::function compute_instr_len_func_t; +//typedef std::function compute_arg_instr_len_func_t; +typedef std::function dump_instr_func_t; +typedef std::function dump_arg_instr_func_t; +typedef std::function exec_instr_func_t; +typedef std::function exec_arg_instr_func_t; +typedef std::function exec_simple_instr_func_t; + +enum { max_opcode_bits = 24 }; +const unsigned top_opcode = (1U << max_opcode_bits); + +class OpcodeInstr { + unsigned min_opcode, max_opcode; + + public: + static constexpr unsigned gas_per_instr = 10, gas_per_bit = 1; + virtual ~OpcodeInstr() = default; + virtual int dispatch(VmState* st, CellSlice& cs, unsigned opcode, unsigned bits) const = 0; + virtual std::string dump(CellSlice& cs, unsigned opcode, unsigned bits) const; + virtual int instr_len(const CellSlice& cs, unsigned opcode, unsigned bits) const; + OpcodeInstr(unsigned _min, unsigned _max) : min_opcode(_min), max_opcode(_max) { + } + OpcodeInstr(unsigned _opcode, unsigned _bits, bool); + unsigned get_opcode_min() const { + return min_opcode; + } + unsigned get_opcode_max() const { + return max_opcode; + } + std::pair get_opcode_range() const { + return {min_opcode, max_opcode}; + } + //static OpcodeInstr* mksimple(unsigned opcode, unsigned opc_bits, std::string _name, exec_instr_func_t exec); + static OpcodeInstr* mksimple(unsigned opcode, unsigned opc_bits, std::string _name, exec_simple_instr_func_t exec); + static OpcodeInstr* mkfixed(unsigned opcode, unsigned opc_bits, unsigned arg_bits, dump_arg_instr_func_t dump, + exec_arg_instr_func_t exec); + static OpcodeInstr* mkfixedrange(unsigned opcode_min, unsigned opcode_max, unsigned tot_bits, unsigned arg_bits, + dump_arg_instr_func_t dump, exec_arg_instr_func_t exec); + static OpcodeInstr* mkext(unsigned opcode, unsigned opc_bits, unsigned arg_bits, dump_instr_func_t dump, + exec_instr_func_t exec, compute_instr_len_func_t comp_len); + static OpcodeInstr* mkextrange(unsigned opcode_min, unsigned opcode_max, unsigned tot_bits, unsigned arg_bits, + dump_instr_func_t dump, exec_instr_func_t exec, compute_instr_len_func_t comp_len); +}; + +namespace instr { + +dump_arg_instr_func_t dump_1sr(std::string prefix, std::string suffix = ""); +dump_arg_instr_func_t dump_1sr_l(std::string prefix, std::string suffix = ""); +dump_arg_instr_func_t dump_2sr(std::string prefix, std::string suffix = ""); +dump_arg_instr_func_t dump_2sr_adj(unsigned adj, std::string prefix, std::string suffix = ""); +dump_arg_instr_func_t dump_3sr(std::string prefix, std::string suffix = ""); +dump_arg_instr_func_t dump_3sr_adj(unsigned adj, std::string prefix, std::string suffix = ""); +dump_arg_instr_func_t dump_1c(std::string prefix, std::string suffix = ""); +dump_arg_instr_func_t dump_1c_l_add(int adj, std::string prefix, std::string suffix = ""); +dump_arg_instr_func_t dump_1c_and(unsigned mask, std::string prefix, std::string suffix = ""); +dump_arg_instr_func_t dump_2c(std::string prefix, std::string interfix, std::string suffix = ""); +dump_arg_instr_func_t dump_2c_add(unsigned add, std::string prefix, std::string interfix, std::string suffix = ""); + +} // namespace instr + +class OpcodeTable : public DispatchTable { + std::map instructions; + std::vector> instruction_list; + std::string name; + Codepage codepage; + bool final; + + public: + OpcodeTable(std::string _name, Codepage cp) : name(_name), codepage(cp), final(false) { + } + OpcodeTable(const OpcodeTable&) = delete; + OpcodeTable(OpcodeTable&&) = delete; + OpcodeTable& operator=(const OpcodeTable&) = delete; + OpcodeTable& operator=(OpcodeTable&&) = delete; + ~OpcodeTable() override = default; + DispatchTable* finalize() override; + bool is_final() const override { + return final; + } + int dispatch(VmState* st, CellSlice& cs) const override; + std::string dump_instr(CellSlice& cs) const override; + int instr_len(const CellSlice& cs) const override; + bool insert_bool(const OpcodeInstr*); + OpcodeTable& insert(const OpcodeInstr*); + + private: + const OpcodeInstr* lookup_instr(unsigned opcode, unsigned bits) const; + const OpcodeInstr* lookup_instr(const CellSlice& cs, unsigned& opcode, unsigned& bits) const; +}; + +class OpcodeInstrDummy : public OpcodeInstr { + public: + OpcodeInstrDummy() = delete; + OpcodeInstrDummy(unsigned _minopc, unsigned _maxopc) : OpcodeInstr(_minopc, _maxopc) { + } + ~OpcodeInstrDummy() override = default; + int dispatch(VmState* st, CellSlice& cs, unsigned opcode, unsigned bits) const override; +}; + +class OpcodeInstrSimple : public OpcodeInstr { + unsigned char opc_bits; + std::string name; + exec_instr_func_t exec_instr; + + public: + OpcodeInstrSimple() = delete; + OpcodeInstrSimple(unsigned opcode, unsigned _opc_bits, std::string _name, exec_instr_func_t exec); + ~OpcodeInstrSimple() override = default; + int dispatch(VmState* st, CellSlice& cs, unsigned opcode, unsigned bits) const override; + std::string dump(CellSlice& cs, unsigned opcode, unsigned bits) const override; + int instr_len(const CellSlice& cs, unsigned opcode, unsigned bits) const override; +}; + +class OpcodeInstrSimplest : public OpcodeInstr { + unsigned char opc_bits; + std::string name; + exec_simple_instr_func_t exec_instr; + + public: + OpcodeInstrSimplest() = delete; + OpcodeInstrSimplest(unsigned opcode, unsigned _opc_bits, std::string _name, exec_simple_instr_func_t exec); + ~OpcodeInstrSimplest() override = default; + int dispatch(VmState* st, CellSlice& cs, unsigned opcode, unsigned bits) const override; + std::string dump(CellSlice& cs, unsigned opcode, unsigned bits) const override; + int instr_len(const CellSlice& cs, unsigned opcode, unsigned bits) const override; +}; + +class OpcodeInstrFixed : public OpcodeInstr { + unsigned char opc_bits, tot_bits; + std::string name; + dump_arg_instr_func_t dump_instr; + exec_arg_instr_func_t exec_instr; + + public: + OpcodeInstrFixed() = delete; + OpcodeInstrFixed(unsigned opcode, unsigned _opc_bits, unsigned _arg_bits, dump_arg_instr_func_t dump, + exec_arg_instr_func_t exec); + OpcodeInstrFixed(unsigned opcode_min, unsigned opcode_max, unsigned _tot_bits, unsigned _arg_bits, + dump_arg_instr_func_t dump, exec_arg_instr_func_t exec); + ~OpcodeInstrFixed() override = default; + int dispatch(VmState* st, CellSlice& cs, unsigned opcode, unsigned bits) const override; + std::string dump(CellSlice& cs, unsigned opcode, unsigned bits) const override; + int instr_len(const CellSlice& cs, unsigned opcode, unsigned bits) const override; +}; + +class OpcodeInstrExt : public OpcodeInstr { + unsigned char opc_bits, tot_bits; + dump_instr_func_t dump_instr; + exec_instr_func_t exec_instr; + compute_instr_len_func_t compute_instr_len; + + public: + OpcodeInstrExt() = delete; + OpcodeInstrExt(unsigned opcode, unsigned _opc_bits, unsigned _arg_bits, dump_instr_func_t dump, + exec_instr_func_t exec, compute_instr_len_func_t comp_len); + OpcodeInstrExt(unsigned opcode_min, unsigned opcode_max, unsigned _tot_bits, unsigned _arg_bits, + dump_instr_func_t dump, exec_instr_func_t exec, compute_instr_len_func_t comp_len); + ~OpcodeInstrExt() override = default; + int dispatch(VmState* st, CellSlice& cs, unsigned opcode, unsigned bits) const override; + std::string dump(CellSlice& cs, unsigned opcode, unsigned bits) const override; + int instr_len(const CellSlice& cs, unsigned opcode, unsigned bits) const override; +}; + +} // namespace vm diff --git a/crypto/vm/stack.cpp b/crypto/vm/stack.cpp new file mode 100644 index 00000000..0b63aec9 --- /dev/null +++ b/crypto/vm/stack.cpp @@ -0,0 +1,632 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "vm/stack.hpp" +#include "vm/continuation.h" +#include "vm/box.hpp" +#include "vm/atom.h" + +namespace td { +template class td::Cnt; +template class td::Ref>; +template class td::Cnt>; +template class td::Ref>>; +} // namespace td + +namespace vm { + +// from_object_t from_object{}; + +const char* exception_messages[(int)(Excno::total)] = { + "normal termination", "alternative termination", "stack underflow", "stack overflow", "integer overflow", + "integer out of range", "invalid opcode", "type check error", "cell overflow", "cell underflow", + "dictionary error", "unknown error", "fatal error"}; + +const char* get_exception_msg(Excno exc_no) { + if (exc_no >= Excno::none && exc_no < Excno::total) { + return exception_messages[static_cast(exc_no)]; + } else { + return "unknown vm exception"; + } +} + +static const char HEX_digits[] = "0123456789ABCDEF"; + +std::string str_to_hex(std::string data, std::string prefix) { + prefix.reserve(prefix.size() + data.size() * 2); + for (char c : data) { + prefix += HEX_digits[(c >> 4) & 15]; + prefix += HEX_digits[c & 15]; + } + return prefix; +} + +std::string StackEntry::to_string() const { + std::ostringstream os; + dump(os); + return std::move(os).str(); +} + +void StackEntry::dump(std::ostream& os) const { + switch (tp) { + case t_null: + os << "(null)"; + break; + case t_int: + os << dec_string(as_int()); + break; + case t_cell: + os << "C{" << static_cast>(ref)->get_hash().to_hex() << "}"; + break; + case t_builder: + os << "BC{" << static_cast>(ref)->to_hex() << "}"; + break; + case t_slice: { + os << "CS{"; + static_cast>(ref)->dump(os, 1, false); + os << '}'; + break; + } + case t_string: + os << "\"" << as_string() << "\""; + break; + case t_bytes: + os << "BYTES:" << str_to_hex(as_bytes()); + break; + case t_box: { + os << "Box{" << (const void*)&*ref << "}"; + break; + } + case t_atom: + os << as_atom(); + break; + case t_tuple: { + const auto& tuple = *static_cast>(ref); + auto n = tuple.size(); + if (!n) { + os << "[]"; + } else if (n == 1) { + os << "[ "; + tuple[0].dump(os); + os << " ]"; + } else { + os << "[ "; + for (const auto& entry : tuple) { + entry.dump(os); + os << ' '; + } + os << ']'; + } + break; + } + case t_object: { + os << "Object{" << (const void*)&*ref << "}"; + break; + } + default: + os << "???"; + } +} + +void StackEntry::print_list(std::ostream& os) const { + switch (tp) { + case t_null: + os << "()"; + break; + case t_tuple: { + const auto& tuple = *static_cast>(ref); + auto n = tuple.size(); + if (!n) { + os << "[]"; + } else if (n == 1) { + os << "["; + tuple[0].print_list(os); + os << "]"; + } else if (n != 2) { + os << "["; + unsigned c = 0; + for (const auto& entry : tuple) { + if (c++) { + os << " "; + } + entry.print_list(os); + } + os << ']'; + } else { + os << '('; + tuple[0].print_list(os); + tuple[1].print_list_tail(os); + } + break; + } + default: + dump(os); + } +} + +void StackEntry::print_list_tail(std::ostream& os) const { + switch (tp) { + case t_null: + os << ')'; + break; + case t_tuple: { + const auto& tuple = *static_cast>(ref); + if (tuple.size() == 2) { + os << ' '; + tuple[0].print_list(os); + tuple[1].print_list_tail(os); + break; + } + } + // fall through + default: + os << " . "; + print_list(os); + os << ')'; + } +} + +StackEntry::StackEntry(Ref stack_ref) : ref(std::move(stack_ref)), tp(t_stack) { +} + +StackEntry::StackEntry(Ref cont_ref) : ref(std::move(cont_ref)), tp(t_vmcont) { +} + +Ref StackEntry::as_cont() const & { + return as(); +} + +Ref StackEntry::as_cont() && { + return move_as(); +} + +StackEntry::StackEntry(Ref box_ref) : ref(std::move(box_ref)), tp(t_box) { +} + +Ref StackEntry::as_box() const & { + return as(); +} + +Ref StackEntry::as_box() && { + return move_as(); +} + +StackEntry::StackEntry(Ref tuple_ref) : ref(std::move(tuple_ref)), tp(t_tuple) { +} + +StackEntry::StackEntry(const std::vector& tuple_components) + : ref(Ref{true, tuple_components}), tp(t_tuple) { +} + +StackEntry::StackEntry(std::vector&& tuple_components) + : ref(Ref{true, std::move(tuple_components)}), tp(t_tuple) { +} + +Ref StackEntry::as_tuple() const & { + return as(); +} + +Ref StackEntry::as_tuple() && { + return move_as(); +} + +Ref StackEntry::as_tuple_range(unsigned max_len, unsigned min_len) const & { + auto t = as(); + if (t.not_null() && t->size() <= max_len && t->size() >= min_len) { + return t; + } else { + return {}; + } +} + +Ref StackEntry::as_tuple_range(unsigned max_len, unsigned min_len) && { + auto t = move_as(); + if (t.not_null() && t->size() <= max_len && t->size() >= min_len) { + return t; + } else { + return {}; + } +} + +StackEntry::StackEntry(Ref atom_ref) : ref(std::move(atom_ref)), tp(t_atom) { +} + +Ref StackEntry::as_atom() const & { + return as(); +} + +Ref StackEntry::as_atom() && { + return move_as(); +} + +const StackEntry& tuple_index(const Tuple& tup, unsigned idx) { + if (idx >= tup->size()) { + throw VmError{Excno::range_chk, "tuple index out of range"}; + } + return (*tup)[idx]; +} + +StackEntry tuple_extend_index(const Ref& tup, unsigned idx) { + if (tup.is_null() || idx >= tup->size()) { + return {}; + } else { + return tup->at(idx); + } +} + +unsigned tuple_extend_set_index(Ref& tup, unsigned idx, StackEntry&& value, bool force) { + if (tup.is_null()) { + if (value.empty() && !force) { + return 0; + } + tup = Ref{true, idx + 1}; + tup.unique_write().at(idx) = std::move(value); + return idx + 1; + } + if (tup->size() <= idx) { + if (value.empty() && !force) { + return 0; + } + auto& tuple = tup.write(); + tuple.resize(idx + 1); + tuple.at(idx) = std::move(value); + return idx + 1; + } else { + tup.write().at(idx) = std::move(value); + return (unsigned)tup->size(); + } +} + +Stack::Stack(const Stack& old_stack, unsigned copy_elem, unsigned skip_top) { + push_from_stack(old_stack, copy_elem, skip_top); +} + +Stack::Stack(Stack&& old_stack, unsigned copy_elem, unsigned skip_top) { + push_from_stack(old_stack, copy_elem, skip_top); +} + +void Stack::push_from_stack(const Stack& old_stack, unsigned copy_elem, unsigned skip_top) { + unsigned n = old_stack.depth(); + if (skip_top > n || copy_elem > n - skip_top) { + throw VmError{Excno::stk_und, "cannot construct stack from another one: not enough elements"}; + } + stack.reserve(stack.size() + copy_elem); + auto it = old_stack.stack.cend() - skip_top; + std::copy(it - copy_elem, it, std::back_inserter(stack)); +} + +void Stack::push_from_stack(Stack&& old_stack, unsigned copy_elem, unsigned skip_top) { + unsigned n = old_stack.depth(); + if (skip_top > n || copy_elem > n - skip_top) { + throw VmError{Excno::stk_und, "cannot construct stack from another one: not enough elements"}; + } + stack.reserve(stack.size() + copy_elem); + auto it = old_stack.stack.cend() - skip_top; + std::move(it - copy_elem, it, std::back_inserter(stack)); +} + +void Stack::move_from_stack(Stack& old_stack, unsigned copy_elem) { + unsigned n = old_stack.depth(); + if (copy_elem > n) { + throw VmError{Excno::stk_und, "cannot construct stack from another one: not enough elements"}; + } + LOG(DEBUG) << "moving " << copy_elem << " top elements to another stack\n"; + stack.reserve(stack.size() + copy_elem); + auto it = old_stack.stack.cend(); + std::move(it - copy_elem, it, std::back_inserter(stack)); + old_stack.pop_many(copy_elem); +} + +void Stack::pop_null() { + check_underflow(1); + if (!pop().empty()) { + throw VmError{Excno::type_chk, "not an null"}; + } +} + +td::RefInt256 Stack::pop_int() { + check_underflow(1); + td::RefInt256 res = pop().as_int(); + if (res.is_null()) { + throw VmError{Excno::type_chk, "not an integer"}; + } + return res; +} + +td::RefInt256 Stack::pop_int_finite() { + auto res = pop_int(); + if (!res->is_valid()) { + throw VmError{Excno::int_ov}; + } + return res; +} + +bool Stack::pop_bool() { + return sgn(pop_int_finite()) != 0; +} + +long long Stack::pop_long() { + return pop_int()->to_long(); +} + +long long Stack::pop_long_range(long long max, long long min) { + auto res = pop_long(); + if (res > max || res < min) { + throw VmError{Excno::range_chk}; + } + return res; +} + +int Stack::pop_smallint_range(int max, int min) { + return (int)pop_long_range(max, min); +} + +Ref Stack::pop_cell() { + check_underflow(1); + auto res = pop().as_cell(); + if (res.is_null()) { + throw VmError{Excno::type_chk, "not a cell"}; + } + return res; +} + +Ref Stack::pop_maybe_cell() { + check_underflow(1); + auto tmp = pop(); + if (tmp.empty()) { + return {}; + } + auto res = std::move(tmp).as_cell(); + if (res.is_null()) { + throw VmError{Excno::type_chk, "not a cell"}; + } + return res; +} + +Ref Stack::pop_builder() { + check_underflow(1); + auto res = pop().as_builder(); + if (res.is_null()) { + throw VmError{Excno::type_chk, "not a cell builder"}; + } + return res; +} + +Ref Stack::pop_cellslice() { + check_underflow(1); + auto res = pop().as_slice(); + if (res.is_null()) { + throw VmError{Excno::type_chk, "not a cell slice"}; + } + return res; +} + +std::string Stack::pop_string() { + check_underflow(1); + auto res = pop().as_string_ref(); + if (res.is_null()) { + throw VmError{Excno::type_chk, "not a string"}; + } + return *res; +} + +std::string Stack::pop_bytes() { + check_underflow(1); + auto res = pop().as_bytes_ref(); + if (res.is_null()) { + throw VmError{Excno::type_chk, "not a bytes chunk"}; + } + return *res; +} + +Ref Stack::pop_cont() { + check_underflow(1); + auto res = pop().as_cont(); + if (res.is_null()) { + throw VmError{Excno::type_chk, "not a continuation"}; + } + return res; +} + +Ref Stack::pop_box() { + check_underflow(1); + auto res = pop().as_box(); + if (res.is_null()) { + throw VmError{Excno::type_chk, "not a box"}; + } + return res; +} + +Ref Stack::pop_tuple() { + check_underflow(1); + auto res = pop().as_tuple(); + if (res.is_null()) { + throw VmError{Excno::type_chk, "not a tuple"}; + } + return res; +} + +Ref Stack::pop_tuple_range(unsigned max_len, unsigned min_len) { + check_underflow(1); + auto res = pop().as_tuple(); + if (res.is_null() || res->size() > max_len || res->size() < min_len) { + throw VmError{Excno::type_chk, "not a tuple of valid size"}; + } + return res; +} + +Ref Stack::pop_maybe_tuple() { + check_underflow(1); + auto val = pop(); + if (val.empty()) { + return {}; + } + auto res = std::move(val).as_tuple(); + if (res.is_null()) { + throw VmError{Excno::type_chk, "not a tuple"}; + } + return res; +} + +Ref Stack::pop_maybe_tuple_range(unsigned max_len) { + check_underflow(1); + auto val = pop(); + if (val.empty()) { + return {}; + } + auto res = std::move(val).as_tuple(); + if (res.is_null() || res->size() > max_len) { + throw VmError{Excno::type_chk, "not a tuple of valid size"}; + } + return res; +} + +Ref Stack::pop_atom() { + check_underflow(1); + auto res = pop().as_atom(); + if (res.is_null()) { + throw VmError{Excno::type_chk, "not an atom"}; + } + return res; +} + +void Stack::push_null() { + push({}); +} + +void Stack::push_int(td::RefInt256 val) { + if (!val->signed_fits_bits(257)) { + throw VmError{Excno::int_ov}; + } + push(std::move(val)); +} + +void Stack::push_int_quiet(td::RefInt256 val, bool quiet) { + if (!val->signed_fits_bits(257)) { + if (!quiet) { + throw VmError{Excno::int_ov}; + } else if (val->is_valid()) { + push(td::RefInt256{true}); + return; + } + } + push(std::move(val)); +} + +void Stack::push_string(std::string str) { + push(std::move(str)); +} + +void Stack::push_string(td::Slice slice) { + push(slice.str()); +} + +void Stack::push_bytes(std::string str) { + push(std::move(str), true); +} + +void Stack::push_bytes(td::Slice slice) { + push(slice.str(), true); +} + +void Stack::push_cell(Ref cell) { + push(std::move(cell)); +} + +void Stack::push_maybe_cell(Ref cell) { + push_maybe(std::move(cell)); +} + +void Stack::push_builder(Ref cb) { + push(std::move(cb)); +} + +void Stack::push_smallint(long long val) { + push(td::RefInt256{true, val}); +} + +void Stack::push_bool(bool val) { + push_smallint(val ? -1 : 0); +} + +void Stack::push_cont(Ref cont) { + push(std::move(cont)); +} + +void Stack::push_box(Ref box) { + push(std::move(box)); +} + +void Stack::push_tuple(Ref tuple) { + push(std::move(tuple)); +} + +void Stack::push_maybe_tuple(Ref tuple) { + if (tuple.not_null()) { + push(std::move(tuple)); + } else { + push_null(); + } +} + +void Stack::push_tuple(const std::vector& components) { + push(components); +} + +void Stack::push_tuple(std::vector&& components) { + push(std::move(components)); +} + +void Stack::push_atom(Ref atom) { + push(std::move(atom)); +} + +Ref Stack::split_top(unsigned top_cnt, unsigned drop_cnt) { + unsigned n = depth(); + if (top_cnt > n || drop_cnt > n - top_cnt) { + return Ref{}; + } + Ref new_stk = Ref{true}; + if (top_cnt) { + new_stk.unique_write().move_from_stack(*this, top_cnt); + } + if (drop_cnt) { + pop_many(drop_cnt); + } + return new_stk; +} + +void Stack::dump(std::ostream& os, bool cr) const { + os << " [ "; + for (const auto& x : stack) { + os << x.to_string() << ' '; + } + os << "] "; + if (cr) { + os << std::endl; + } +} +void Stack::push_cellslice(Ref cs) { + push(std::move(cs)); +} + +void Stack::push_maybe_cellslice(Ref cs) { + push_maybe(std::move(cs)); +} + +} // namespace vm diff --git a/crypto/vm/stack.hpp b/crypto/vm/stack.hpp new file mode 100644 index 00000000..c87bd4d9 --- /dev/null +++ b/crypto/vm/stack.hpp @@ -0,0 +1,496 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +#include +#include +#include +#include +#include +#include +#include +#include "common/refcnt.hpp" +#include "common/bigint.hpp" +#include "common/refint.h" +#include "common/bitstring.h" +#include "vm/cells.h" +#include "vm/cellslice.h" +#include "vm/excno.hpp" + +namespace td { +extern template class td::Cnt; +extern template class td::Ref>; +} // namespace td + +namespace vm { + +using td::Cnt; +using td::Ref; +using td::RefAny; + +const char* get_exception_msg(Excno exc_no); +std::string str_to_hex(std::string data, std::string prefix = ""); + +class StackEntry; +class Stack; +class Continuation; +class Box; +class Atom; + +using Tuple = td::Cnt>; + +struct from_object_t {}; +constexpr from_object_t from_object{}; + +class StackEntry { + public: + enum Type { + t_null, + t_int, + t_cell, + t_builder, + t_slice, + t_vmcont, + t_tuple, + t_stack, + t_string, + t_bytes, + t_bitstring, + t_box, + t_atom, + t_object + }; + + private: + RefAny ref; + Type tp; + + public: + StackEntry() : ref(), tp(t_null) { + } + ~StackEntry() { + } + StackEntry(Ref cell_ref) : ref(std::move(cell_ref)), tp(t_cell) { + } + StackEntry(Ref cb_ref) : ref(std::move(cb_ref)), tp(t_builder) { + } + StackEntry(Ref cs_ref) : ref(std::move(cs_ref)), tp(t_slice) { + } + StackEntry(td::RefInt256 int_ref) : ref(std::move(int_ref)), tp(t_int) { + } + StackEntry(std::string str, bool bytes = false) : ref(), tp(bytes ? t_bytes : t_string) { + ref = Ref>{true, std::move(str)}; + } + StackEntry(Ref stack_ref); + StackEntry(Ref cont_ref); + StackEntry(Ref box_ref); + StackEntry(Ref tuple_ref); + StackEntry(const std::vector& tuple_components); + StackEntry(std::vector&& tuple_components); + StackEntry(Ref atom_ref); + StackEntry(const StackEntry& se) : ref(se.ref), tp(se.tp) { + } + StackEntry(StackEntry&& se) noexcept : ref(std::move(se.ref)), tp(se.tp) { + se.tp = t_null; + } + template + StackEntry(from_object_t, Ref obj_ref) : ref(std::move(obj_ref)), tp(t_object) { + } + StackEntry& operator=(const StackEntry& se) { + ref = se.ref; + tp = se.tp; + return *this; + } + StackEntry& operator=(StackEntry&& se) { + ref = std::move(se.ref); + tp = se.tp; + se.tp = t_null; + return *this; + } + StackEntry& clear() { + ref.clear(); + tp = t_null; + return *this; + } + bool empty() const { + return tp == t_null; + } + bool is_tuple() const { + return tp == t_tuple; + } + bool is_atom() const { + return tp == t_atom; + } + bool is(int wanted) const { + return tp == wanted; + } + void swap(StackEntry& se) { + ref.swap(se.ref); + std::swap(tp, se.tp); + } + bool operator==(const StackEntry& other) const { + return tp == other.tp && ref == other.ref; + } + bool operator!=(const StackEntry& other) const { + return !(tp == other.tp && ref == other.ref); + } + Type type() const { + return tp; + } + + private: + template + Ref dynamic_as() const & { + return tp == tag ? static_cast>(ref) : td::Ref{}; + } + template + Ref dynamic_as() && { + return tp == tag ? static_cast>(std::move(ref)) : td::Ref{}; + } + template + Ref dynamic_move_as() & { + return tp == tag ? static_cast>(std::move(ref)) : td::Ref{}; + } + template + Ref as() const & { + return tp == tag ? Ref{td::static_cast_ref(), ref} : td::Ref{}; + } + template + Ref as() && { + return tp == tag ? Ref{td::static_cast_ref(), std::move(ref)} : td::Ref{}; + } + template + Ref move_as() & { + return tp == tag ? Ref{td::static_cast_ref(), std::move(ref)} : td::Ref{}; + } + + public: + template + static StackEntry maybe(Ref ref) { + if (ref.is_null()) { + return {}; + } else { + return ref; + } + } + td::RefInt256 as_int() const & { + return as(); + } + td::RefInt256 as_int() && { + return move_as(); + } + Ref as_cell() const & { + return as(); + } + Ref as_cell() && { + return move_as(); + } + Ref as_builder() const & { + return as(); + } + Ref as_builder() && { + return move_as(); + } + Ref as_slice() const & { + return as(); + } + Ref as_slice() && { + return move_as(); + } + Ref as_cont() const &; + Ref as_cont() &&; + Ref> as_string_ref() const { + return as, t_string>(); + } + Ref> as_bytes_ref() const { + return as, t_bytes>(); + } + std::string as_string() const { + //assert(!as_string_ref().is_null()); + return tp == t_string ? *as_string_ref() : ""; + } + std::string as_bytes() const { + return tp == t_bytes ? *as_bytes_ref() : ""; + } + Ref as_box() const &; + Ref as_box() &&; + Ref as_tuple() const &; + Ref as_tuple() &&; + Ref as_tuple_range(unsigned max_len = 255, unsigned min_len = 0) const &; + Ref as_tuple_range(unsigned max_len = 255, unsigned min_len = 0) &&; + Ref as_atom() const &; + Ref as_atom() &&; + template + Ref as_object() const & { + return dynamic_as(); + } + template + Ref as_object() && { + return dynamic_move_as(); + } + void dump(std::ostream& os) const; + void print_list(std::ostream& os) const; + void print_list_tail(std::ostream& os) const; + std::string to_string() const; +}; + +inline void swap(StackEntry& se1, StackEntry& se2) { + se1.swap(se2); +} + +template +Ref make_tuple_ref(Args&&... args) { + return td::make_cnt_ref>(std::vector{std::forward(args)...}); +} + +const StackEntry& tuple_index(const Tuple& tup, unsigned idx); +StackEntry tuple_extend_index(const Ref& tup, unsigned idx); +unsigned tuple_extend_set_index(Ref& tup, unsigned idx, StackEntry&& value, bool force = false); + +class Stack : public td::CntObject { + std::vector stack; + + public: + Stack() { + } + ~Stack() override = default; + Stack(const std::vector& _stack) : stack(_stack) { + } + Stack(std::vector&& _stack) : stack(std::move(_stack)) { + } + Stack(const Stack& old_stack, unsigned copy_elem, unsigned skip_top); + Stack(Stack&& old_stack, unsigned copy_elem, unsigned skip_top); + td::CntObject* make_copy() const override { + std::cerr << "copy stack at " << (const void*)this << " (" << depth() << " entries)\n"; + return new Stack{stack}; + } + void push_from_stack(const Stack& old_stack, unsigned copy_elem, unsigned skip_top = 0); + void push_from_stack(Stack&& old_stack, unsigned copy_elem, unsigned skip_top = 0); + void move_from_stack(Stack& old_stack, unsigned copy_elem); + Ref split_top(unsigned top_cnt, unsigned drop_cnt = 0); + + StackEntry& push() { + stack.emplace_back(); + return stack.back(); + } + template + StackEntry& push(Args&&... args) { + stack.emplace_back(args...); + return stack.back(); + } + StackEntry& push(const StackEntry& se) { + stack.push_back(se); + return stack.back(); + } + StackEntry& push(StackEntry&& se) { + stack.emplace_back(std::move(se)); + return stack.back(); + } + void pop(StackEntry& se) { + stack.back().swap(se); + stack.pop_back(); + } + StackEntry pop() { + StackEntry res = std::move(stack.back()); + stack.pop_back(); + return res; + } + StackEntry pop_chk() { + check_underflow(1); + return pop(); + } + void pop_many(int count) { + stack.resize(stack.size() - count); + } + void drop_bottom(int count) { + std::move(stack.cbegin() + count, stack.cend(), stack.begin()); + pop_many(count); + } + StackEntry& operator[](int idx) { // NB: we sometimes use idx=-1 + return stack[stack.size() - idx - 1]; + } + const StackEntry& operator[](int idx) const { + return stack[stack.size() - idx - 1]; + } + StackEntry& at(int idx) { + return stack.at(stack.size() - idx - 1); + } + const StackEntry& at(int idx) const { + return stack.at(stack.size() - idx - 1); + } + StackEntry fetch(int idx) const { + return stack[stack.size() - idx - 1]; + } + StackEntry& tos() { + return stack.back(); + } + const StackEntry& tos() const { + return stack.back(); + } + bool is_empty() const { + return stack.empty(); + } + int depth() const { + return (int)stack.size(); + } + std::vector::iterator top() { + return stack.end(); + } + std::vector::const_iterator top() const { + return stack.cend(); + } + std::vector::iterator from_top(int offs) { + return stack.end() - offs; + } + std::vector::const_iterator from_top(int offs) const { + return stack.cend() - offs; + } + bool at_least(int req) const { + return depth() >= req; + } + template + bool at_least(int req, Args... args) const { + return at_least(req) && at_least(args...); + } + bool more_than(int req) const { + return depth() > req; + } + template + bool more_than(int req, Args... args) const { + return more_than(req) && more_than(args...); + } + void clear() { + stack.clear(); + } + Stack& set_contents(const Stack& other_stack) { + stack = other_stack.stack; + return *this; + } + Stack& set_contents(Stack&& other_stack) { + stack = std::move(other_stack.stack); + return *this; + } + Stack& set_contents(Ref ref) { + if (ref.is_null()) { + clear(); + } else if (ref->is_unique()) { + set_contents(std::move(ref.unique_write())); + } else { + set_contents(*ref); + } + return *this; + } + template + const Stack& check_underflow(Args... args) const { + if (!at_least(args...)) { + throw VmError{Excno::stk_und}; + } + return *this; + } + template + Stack& check_underflow(Args... args) { + if (!at_least(args...)) { + throw VmError{Excno::stk_und}; + } + return *this; + } + template + const Stack& check_underflow_p(Args... args) const { + if (!more_than(args...)) { + throw VmError{Excno::stk_und}; + } + return *this; + } + template + Stack& check_underflow_p(Args... args) { + if (!more_than(args...)) { + throw VmError{Excno::stk_und}; + } + return *this; + } + Stack& reserve(int cnt) { + stack.reserve(cnt); + return *this; + } + void pop_null(); + td::RefInt256 pop_int(); + td::RefInt256 pop_int_finite(); + bool pop_bool(); + long long pop_long(); + long long pop_long_range(long long max, long long min = 0); + int pop_smallint_range(int max, int min = 0); + Ref pop_cell(); + Ref pop_maybe_cell(); + Ref pop_builder(); + Ref pop_cellslice(); + Ref pop_cont(); + Ref pop_box(); + Ref pop_tuple(); + Ref pop_tuple_range(unsigned max_len = 255, unsigned min_len = 0); + Ref pop_maybe_tuple(); + Ref pop_maybe_tuple_range(unsigned max_len = 255); + Ref pop_atom(); + std::string pop_string(); + std::string pop_bytes(); + void push_null(); + void push_int(td::RefInt256 val); + void push_int_quiet(td::RefInt256 val, bool quiet = true); + void push_smallint(long long val); + void push_bool(bool val); + void push_string(std::string str); + void push_string(td::Slice slice); + void push_bytes(std::string str); + void push_bytes(td::Slice slice); + void push_cell(Ref cell); + void push_maybe_cell(Ref cell); + void push_maybe_cellslice(Ref cs); + void push_builder(Ref cb); + void push_cellslice(Ref cs); + void push_cont(Ref cont); + void push_box(Ref box); + void push_tuple(Ref tuple); + void push_tuple(const std::vector& components); + void push_tuple(std::vector&& components); + void push_maybe_tuple(Ref tuple); + void push_atom(Ref atom); + template + void push_object(Ref obj) { + push({vm::from_object, std::move(obj)}); + } + template + void push_make_object(Args&&... args) { + push_object(td::make_ref(std::forward(args)...)); + } + template + void push_maybe(Ref val) { + if (val.is_null()) { + push({}); + } else { + push(std::move(val)); + } + } + void dump(std::ostream& os, bool cr = true) const; +}; + +} // namespace vm + +namespace td { +extern template class td::Cnt>; +extern template class td::Ref>>; +} // namespace td diff --git a/crypto/vm/stackops.cpp b/crypto/vm/stackops.cpp new file mode 100644 index 00000000..7fdbcba0 --- /dev/null +++ b/crypto/vm/stackops.cpp @@ -0,0 +1,576 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "vm/log.h" +#include "vm/stackops.h" +#include "vm/opctable.h" +#include "vm/stack.hpp" +#include "vm/continuation.h" +#include "vm/excno.hpp" + +namespace vm { + +int exec_nop(VmState* st) { + VM_LOG(st) << "execute NOP\n"; + return 0; +} + +// basic stack manipulation primitives + +int exec_swap(VmState* st) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute SWAP\n"; + stack.check_underflow(2); + swap(stack[0], stack[1]); + return 0; +} + +int exec_xchg0(VmState* st, unsigned args) { + int x = args & 15; + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute XCHG s" << x; + stack.check_underflow_p(x); + swap(stack[0], stack[x]); + return 0; +} + +int exec_xchg0_l(VmState* st, unsigned args) { + int x = args & 255; + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute XCHG s" << x; + stack.check_underflow_p(x); + swap(stack[0], stack[x]); + return 0; +} + +int exec_xchg(VmState* st, unsigned args) { + int x = (args >> 4) & 15, y = args & 15; + if (!x || x >= y) { + throw VmError{Excno::inv_opcode, "invalid XCHG arguments"}; + } + VM_LOG(st) << "execute XCHG s" << x << ",s" << y; + Stack& stack = st->get_stack(); + stack.check_underflow_p(y); + swap(stack[x], stack[y]); + return 0; +} + +std::string dump_xchg(CellSlice&, unsigned args) { + int x = (args >> 4) & 15, y = args & 15; + if (!x || x >= y) { + return ""; + } + std::ostringstream os{"XCHG s"}; + os << x << ",s" << y; + return os.str(); +} + +int exec_xchg1(VmState* st, unsigned args) { + int x = args & 15; + assert(x >= 2); + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute XCHG s1,s" << x; + stack.check_underflow_p(x); + swap(stack[1], stack[x]); + return 0; +} + +int exec_dup(VmState* st) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute DUP\n"; + stack.check_underflow(1); + stack.push(stack.fetch(0)); + return 0; +} + +int exec_over(VmState* st) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute OVER\n"; + stack.check_underflow(2); + stack.push(stack.fetch(1)); + return 0; +} + +int exec_push(VmState* st, unsigned args) { + int x = args & 15; + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute PUSH s" << x; + stack.check_underflow_p(x); + stack.push(stack.fetch(x)); + return 0; +} + +int exec_push_l(VmState* st, unsigned args) { + int x = args & 255; + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute PUSH s" << x; + stack.check_underflow_p(x); + stack.push(stack.fetch(x)); + return 0; +} + +int exec_drop(VmState* st) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute DROP\n"; + stack.check_underflow(1); + stack.pop(); + return 0; +} + +int exec_nip(VmState* st) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute NIP\n"; + stack.check_underflow(2); + stack.pop(stack[1]); + return 0; +} + +int exec_pop(VmState* st, unsigned args) { + int x = args & 15; + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute POP s" << x; + stack.check_underflow_p(x); + stack.pop(stack[x]); + return 0; +} + +int exec_pop_l(VmState* st, unsigned args) { + int x = args & 255; + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute POP s" << x; + stack.check_underflow_p(x); + stack.pop(stack[x]); + return 0; +} + +// compound stack manipulation primitives + +int exec_xchg2(VmState* st, unsigned args) { + int x = (args >> 4) & 15, y = args & 15; + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute XCHG2 s" << x << ",s" << y; + stack.check_underflow_p(x, y, 1); + swap(stack[1], stack[x]); + swap(stack[0], stack[y]); + return 0; +} + +int exec_xcpu(VmState* st, unsigned args) { + int x = (args >> 4) & 15, y = args & 15; + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute XCPU s" << x << ",s" << y; + stack.check_underflow_p(x, y); + swap(stack[0], stack[x]); + stack.push(stack.fetch(y)); + return 0; +} + +int exec_puxc(VmState* st, unsigned args) { + int x = (args >> 4) & 15, y = args & 15; + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute PUXC s" << x << ",s" << y - 1; + stack.check_underflow_p(x).check_underflow(y); + stack.push(stack.fetch(x)); + swap(stack[0], stack[1]); + swap(stack[0], stack[y]); + return 0; +} + +int exec_push2(VmState* st, unsigned args) { + int x = (args >> 4) & 15, y = args & 15; + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute PUSH2 s" << x << ",s" << y; + stack.check_underflow_p(x, y); + stack.push(stack.fetch(x)); + stack.push(stack.fetch(y + 1)); + return 0; +} + +int exec_xchg3(VmState* st, unsigned args) { + int x = (args >> 8) & 15, y = (args >> 4) & 15, z = args & 15; + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute XCHG3 s" << x << ",s" << y << ",s" << z; + stack.check_underflow_p(x, y, z, 2); + swap(stack[2], stack[x]); + swap(stack[1], stack[y]); + swap(stack[0], stack[z]); + return 0; +} + +int exec_xc2pu(VmState* st, unsigned args) { + int x = (args >> 8) & 15, y = (args >> 4) & 15, z = args & 15; + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute XC2PU s" << x << ",s" << y << ",s" << z; + stack.check_underflow_p(x, y, z, 1); + swap(stack[1], stack[x]); + swap(stack[0], stack[y]); + stack.push(stack.fetch(z)); + return 0; +} + +int exec_xcpuxc(VmState* st, unsigned args) { + int x = (args >> 8) & 15, y = (args >> 4) & 15, z = args & 15; + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute XCPUXC s" << x << ",s" << y << ",s" << z - 1; + stack.check_underflow_p(x, y, 1).check_underflow(z); + swap(stack[1], stack[x]); + stack.push(stack.fetch(y)); + swap(stack[0], stack[1]); + swap(stack[0], stack[z]); + return 0; +} + +int exec_xcpu2(VmState* st, unsigned args) { + int x = (args >> 8) & 15, y = (args >> 4) & 15, z = args & 15; + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute XCPU2 s" << x << ",s" << y << ",s" << z; + stack.check_underflow_p(x, y, z); + swap(stack[0], stack[x]); + stack.push(stack.fetch(y)); + stack.push(stack.fetch(z + 1)); + return 0; +} + +int exec_puxc2(VmState* st, unsigned args) { + int x = (args >> 8) & 15, y = (args >> 4) & 15, z = args & 15; + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute PUXC2 s" << x << ",s" << y - 1 << ",s" << z - 1; + stack.check_underflow_p(x, 1).check_underflow(y, z); + stack.push(stack.fetch(x)); + swap(stack[2], stack[0]); + swap(stack[1], stack[y]); + swap(stack[0], stack[z]); + return 0; +} + +int exec_puxcpu(VmState* st, unsigned args) { + int x = (args >> 8) & 15, y = (args >> 4) & 15, z = args & 15; + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute PUXCPU s" << x << ",s" << y - 1 << ",s" << z - 1; + stack.check_underflow_p(x).check_underflow(y, z); + stack.push(stack.fetch(x)); + swap(stack[0], stack[1]); + swap(stack[0], stack[y]); + stack.push(stack.fetch(z)); + return 0; +} + +int exec_pu2xc(VmState* st, unsigned args) { + int x = (args >> 8) & 15, y = (args >> 4) & 15, z = args & 15; + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute PU2XC s" << x << ",s" << y - 1 << ",s" << z - 2; + stack.check_underflow_p(x).check_underflow(y, z - 1); + stack.push(stack.fetch(x)); + swap(stack[1], stack[0]); + stack.push(stack.fetch(y)); + swap(stack[1], stack[0]); + swap(stack[0], stack[z]); + return 0; +} + +int exec_push3(VmState* st, unsigned args) { + int x = (args >> 8) & 15, y = (args >> 4) & 15, z = args & 15; + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute PUSH3 s" << x << ",s" << y << ",s" << z; + stack.check_underflow_p(x, y, z); + stack.push(stack.fetch(x)); + stack.push(stack.fetch(y + 1)); + stack.push(stack.fetch(z + 2)); + return 0; +} + +// exotic stack manipulation primitives + +int exec_blkswap(VmState* st, unsigned args) { + int x = ((args >> 4) & 15) + 1, y = (args & 15) + 1; + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute BLKSWAP " << x << ',' << y; + stack.check_underflow(x + y); + std::reverse(stack.from_top(x + y), stack.from_top(y)); + std::reverse(stack.from_top(y), stack.top()); + std::reverse(stack.from_top(x + y), stack.top()); + return 0; +} + +int exec_rot(VmState* st) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute ROT\n"; + stack.check_underflow(3); + swap(stack[1], stack[2]); + swap(stack[0], stack[1]); + return 0; +} + +int exec_rotrev(VmState* st) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute ROTREV\n"; + stack.check_underflow(3); + swap(stack[0], stack[1]); + swap(stack[1], stack[2]); + return 0; +} + +int exec_2swap(VmState* st) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute 2SWAP\n"; + stack.check_underflow(4); + swap(stack[1], stack[3]); + swap(stack[0], stack[2]); + return 0; +} + +int exec_2drop(VmState* st) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute 2DROP\n"; + stack.check_underflow(2); + stack.pop(); + stack.pop(); + return 0; +} + +int exec_2dup(VmState* st) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute 2DUP\n"; + stack.check_underflow(2); + stack.push(stack.fetch(1)); + stack.push(stack.fetch(1)); + return 0; +} + +int exec_2over(VmState* st) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute 2OVER\n"; + stack.check_underflow(4); + stack.push(stack.fetch(3)); + stack.push(stack.fetch(3)); + return 0; +} + +int exec_reverse(VmState* st, unsigned args) { + int x = ((args >> 4) & 15) + 2, y = (args & 15); + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute REVERSE " << x << ',' << y; + stack.check_underflow(x + y); + std::reverse(stack.from_top(x + y), stack.from_top(y)); + return 0; +} + +int exec_blkdrop(VmState* st, unsigned args) { + int x = (args & 15); + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute BLKDROP " << x; + stack.check_underflow(x); + stack.pop_many(x); + return 0; +} + +int exec_blkpush(VmState* st, unsigned args) { + int x = ((args >> 4) & 15), y = (args & 15); + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute BLKPUSH " << x << ',' << y; + stack.check_underflow_p(y); + while (--x >= 0) { + stack.push(stack.fetch(y)); + } + return 0; +} + +int exec_pick(VmState* st) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute PICK\n"; + stack.check_underflow(1); + int x = stack.pop_smallint_range(255); + stack.check_underflow_p(x); + stack.push(stack.fetch(x)); + return 0; +} + +int exec_roll(VmState* st) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute ROLL\n"; + stack.check_underflow(1); + int x = stack.pop_smallint_range(255); + stack.check_underflow_p(x); + while (--x >= 0) { + swap(stack[x], stack[x + 1]); + } + return 0; +} + +int exec_rollrev(VmState* st) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute ROLLREV\n"; + stack.check_underflow(1); + int x = stack.pop_smallint_range(255); + stack.check_underflow_p(x); + for (int i = 0; i < x; i++) { + swap(stack[i], stack[i + 1]); + } + return 0; +} + +int exec_blkswap_x(VmState* st) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute BLKSWX\n"; + stack.check_underflow(2); + int y = stack.pop_smallint_range(255); + int x = stack.pop_smallint_range(255); + stack.check_underflow(x + y); + if (x > 0 && y > 0) { + std::reverse(stack.from_top(x + y), stack.from_top(y)); + std::reverse(stack.from_top(y), stack.top()); + std::reverse(stack.from_top(x + y), stack.top()); + } + return 0; +} + +int exec_reverse_x(VmState* st) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute REVX\n"; + stack.check_underflow(2); + int y = stack.pop_smallint_range(255); + int x = stack.pop_smallint_range(255); + stack.check_underflow(x + y); + std::reverse(stack.from_top(x + y), stack.from_top(y)); + return 0; +} + +int exec_drop_x(VmState* st) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute DROPX\n"; + stack.check_underflow(1); + int x = stack.pop_smallint_range(255); + stack.check_underflow(x); + stack.pop_many(x); + return 0; +} + +int exec_tuck(VmState* st) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute TUCK\n"; + stack.check_underflow(2); + swap(stack[0], stack[1]); + stack.push(stack.fetch(1)); + return 0; +} + +int exec_xchg_x(VmState* st) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute XCHGX\n"; + stack.check_underflow(1); + int x = stack.pop_smallint_range(255); + stack.check_underflow_p(x); + swap(stack[0], stack[x]); + return 0; +} + +int exec_depth(VmState* st) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute DEPTH\n"; + stack.push_smallint(stack.depth()); + return 0; +} + +int exec_chkdepth(VmState* st) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute CHKDEPTH\n"; + stack.check_underflow(1); + int x = stack.pop_smallint_range(255); + stack.check_underflow(x); + return 0; +} + +int exec_onlytop_x(VmState* st) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute ONLYTOPX\n"; + stack.check_underflow(1); + int x = stack.pop_smallint_range(255); + stack.check_underflow(x); + int n = stack.depth(), d = n - x; + if (d > 0) { + for (int i = n - 1; i >= d; i--) { + stack[i] = std::move(stack[i - d]); + } + } + stack.pop_many(d); + return 0; +} + +int exec_only_x(VmState* st) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute ONLYX\n"; + stack.check_underflow(1); + int x = stack.pop_smallint_range(255); + stack.check_underflow(x); + stack.pop_many(stack.depth() - x); + return 0; +} + +void register_stack_ops(OpcodeTable& cp0) { + cp0.insert(OpcodeInstr::mksimple(0x00, 8, "NOP", exec_nop)) + .insert(OpcodeInstr::mksimple(0x01, 8, "SWAP", exec_swap)) + .insert(OpcodeInstr::mkfixedrange(0x02, 0x10, 8, 4, instr::dump_1sr("XCHG "), exec_xchg0)) + .insert(OpcodeInstr::mkfixed(0x10, 8, 8, dump_xchg, exec_xchg)) + .insert(OpcodeInstr::mkfixed(0x11, 8, 8, instr::dump_1sr_l("XCHG "), exec_xchg0_l)) + .insert(OpcodeInstr::mkfixedrange(0x12, 0x20, 8, 4, instr::dump_1sr("XCHG s1,"), exec_xchg1)) + .insert(OpcodeInstr::mksimple(0x20, 8, "DUP", exec_dup)) + .insert(OpcodeInstr::mksimple(0x21, 8, "OVER", exec_over)) + .insert(OpcodeInstr::mkfixedrange(0x22, 0x30, 8, 4, instr::dump_1sr("PUSH "), exec_push)) + .insert(OpcodeInstr::mksimple(0x30, 8, "DROP", exec_drop)) + .insert(OpcodeInstr::mksimple(0x31, 8, "NIP", exec_nip)) + .insert(OpcodeInstr::mkfixedrange(0x32, 0x40, 8, 4, instr::dump_1sr("POP "), exec_pop)) + .insert(OpcodeInstr::mkfixed(0x4, 4, 12, instr::dump_3sr("XCHG3 "), exec_xchg3)) + .insert(OpcodeInstr::mkfixed(0x50, 8, 8, instr::dump_2sr("XCHG2 "), exec_xchg2)) + .insert(OpcodeInstr::mkfixed(0x51, 8, 8, instr::dump_2sr("XCPU "), exec_xcpu)) + .insert(OpcodeInstr::mkfixed(0x52, 8, 8, instr::dump_2sr_adj(1, "PUXC "), exec_puxc)) + .insert(OpcodeInstr::mkfixed(0x53, 8, 8, instr::dump_2sr("PUSH2 "), exec_push2)) + .insert(OpcodeInstr::mkfixed(0x540, 12, 12, instr::dump_3sr("XCHG3 "), exec_xchg3)) + .insert(OpcodeInstr::mkfixed(0x541, 12, 12, instr::dump_3sr("XC2PU "), exec_xc2pu)) + .insert(OpcodeInstr::mkfixed(0x542, 12, 12, instr::dump_3sr_adj(1, "XCPUXC "), exec_xcpuxc)) + .insert(OpcodeInstr::mkfixed(0x543, 12, 12, instr::dump_3sr("XCPU2 "), exec_xcpu2)) + .insert(OpcodeInstr::mkfixed(0x544, 12, 12, instr::dump_3sr_adj(0x11, "PUXC2 "), exec_puxc2)) + .insert(OpcodeInstr::mkfixed(0x545, 12, 12, instr::dump_3sr_adj(0x11, "PUXCPU "), exec_puxcpu)) + .insert(OpcodeInstr::mkfixed(0x546, 12, 12, instr::dump_3sr_adj(0x12, "PU2XC "), exec_pu2xc)) + .insert(OpcodeInstr::mkfixed(0x547, 12, 12, instr::dump_3sr("PUSH3 "), exec_push3)) + .insert(OpcodeInstr::mkfixed(0x55, 8, 8, instr::dump_2c_add(0x11, "BLKSWAP ", ","), exec_blkswap)) + .insert(OpcodeInstr::mkfixed(0x56, 8, 8, instr::dump_1sr_l("PUSH "), exec_push_l)) + .insert(OpcodeInstr::mkfixed(0x57, 8, 8, instr::dump_1sr_l("POP "), exec_pop_l)) + .insert(OpcodeInstr::mksimple(0x58, 8, "ROT", exec_rot)) + .insert(OpcodeInstr::mksimple(0x59, 8, "ROTREV", exec_rotrev)) + .insert(OpcodeInstr::mksimple(0x5a, 8, "2SWAP", exec_2swap)) + .insert(OpcodeInstr::mksimple(0x5b, 8, "2DROP", exec_2drop)) + .insert(OpcodeInstr::mksimple(0x5c, 8, "2DUP", exec_2dup)) + .insert(OpcodeInstr::mksimple(0x5d, 8, "2OVER", exec_2over)) + .insert(OpcodeInstr::mkfixed(0x5e, 8, 8, instr::dump_2c_add(0x20, "REVERSE ", ","), exec_reverse)) + .insert(OpcodeInstr::mkfixed(0x5f0, 12, 4, instr::dump_1c("BLKDROP "), exec_blkdrop)) + .insert(OpcodeInstr::mkfixedrange(0x5f10, 0x6000, 16, 8, instr::dump_2c("BLKPUSH ", ","), exec_blkpush)) + .insert(OpcodeInstr::mksimple(0x60, 8, "PICK", exec_pick)) + .insert(OpcodeInstr::mksimple(0x61, 8, "ROLL", exec_roll)) + .insert(OpcodeInstr::mksimple(0x62, 8, "ROLLREV", exec_rollrev)) + .insert(OpcodeInstr::mksimple(0x63, 8, "BLKSWX", exec_blkswap_x)) + .insert(OpcodeInstr::mksimple(0x64, 8, "REVX", exec_reverse_x)) + .insert(OpcodeInstr::mksimple(0x65, 8, "DROPX", exec_drop_x)) + .insert(OpcodeInstr::mksimple(0x66, 8, "TUCK", exec_tuck)) + .insert(OpcodeInstr::mksimple(0x67, 8, "XCHGX", exec_xchg_x)) + .insert(OpcodeInstr::mksimple(0x68, 8, "DEPTH", exec_depth)) + .insert(OpcodeInstr::mksimple(0x69, 8, "CHKDEPTH", exec_chkdepth)) + .insert(OpcodeInstr::mksimple(0x6a, 8, "ONLYTOPX", exec_onlytop_x)) + .insert(OpcodeInstr::mksimple(0x6b, 8, "ONLYX", exec_only_x)); +} + +} // namespace vm diff --git a/crypto/vm/stackops.h b/crypto/vm/stackops.h new file mode 100644 index 00000000..7ae940d3 --- /dev/null +++ b/crypto/vm/stackops.h @@ -0,0 +1,27 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +namespace vm { + +class OpcodeTable; + +void register_stack_ops(OpcodeTable& cp0); + +} // namespace vm diff --git a/crypto/vm/tonops.cpp b/crypto/vm/tonops.cpp new file mode 100644 index 00000000..30d3d14b --- /dev/null +++ b/crypto/vm/tonops.cpp @@ -0,0 +1,675 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include +#include "vm/tonops.h" +#include "vm/log.h" +#include "vm/opctable.h" +#include "vm/stack.hpp" +#include "vm/continuation.h" +#include "vm/excno.hpp" +#include "vm/dict.h" +#include "Ed25519.h" + +namespace vm { + +namespace { + +bool debug(const char* str) TD_UNUSED; +bool debug(const char* str) { + std::cerr << str; + return true; +} + +bool debug(int x) TD_UNUSED; +bool debug(int x) { + if (x < 100) { + std::cerr << '[' << (char)(64 + x) << ']'; + } else { + std::cerr << '[' << (char)(64 + x / 100) << x % 100 << ']'; + } + return true; +} +} // namespace + +#define DBG_START int dbg = 0; +#define DBG debug(++dbg)&& +#define DEB_START DBG_START +#define DEB DBG + +int exec_set_gas_generic(VmState* st, long long new_gas_limit) { + if (new_gas_limit < st->gas_consumed()) { + throw VmNoGas{}; + } + st->change_gas_limit(new_gas_limit); + return 0; +} + +int exec_accept(VmState* st) { + VM_LOG(st) << "execute ACCEPT"; + return exec_set_gas_generic(st, GasLimits::infty); +} + +int exec_set_gas_limit(VmState* st) { + VM_LOG(st) << "execute SETGASLIMIT"; + td::RefInt256 x = st->get_stack().pop_int_finite(); + long long gas = 0; + if (x->sgn() > 0) { + gas = x->unsigned_fits_bits(63) ? x->to_long() : GasLimits::infty; + } + return exec_set_gas_generic(st, gas); +} + +void register_basic_gas_ops(OpcodeTable& cp0) { + using namespace std::placeholders; + cp0.insert(OpcodeInstr::mksimple(0xf800, 16, "ACCEPT", exec_accept)) + .insert(OpcodeInstr::mksimple(0xf801, 16, "SETGASLIMIT", exec_set_gas_limit)); +} + +void register_ton_gas_ops(OpcodeTable& cp0) { + using namespace std::placeholders; +} + +int exec_get_param(VmState* st, unsigned idx, const char* name) { + if (name) { + VM_LOG(st) << "execute " << name; + } + Stack& stack = st->get_stack(); + auto tuple = st->get_c7(); + auto t1 = tuple_index(*tuple, 0).as_tuple_range(255); + if (t1.is_null()) { + throw VmError{Excno::type_chk, "intermediate value is not a tuple"}; + } + stack.push(tuple_index(*t1, idx)); + return 0; +} + +int exec_get_var_param(VmState* st, unsigned idx) { + idx &= 15; + VM_LOG(st) << "execute GETPARAM " << idx; + return exec_get_param(st, idx, nullptr); +} + +int exec_get_config_dict(VmState* st) { + exec_get_param(st, 9, "CONFIGDICT"); + st->get_stack().push_smallint(32); + return 0; +} + +int exec_get_config_param(VmState* st, bool opt) { + VM_LOG(st) << "execute CONFIG" << (opt ? "OPTPARAM" : "PARAM"); + Stack& stack = st->get_stack(); + auto idx = stack.pop_int(); + exec_get_param(st, 9, nullptr); + Dictionary dict{stack.pop_maybe_cell(), 32}; + td::BitArray<32> key; + Ref value; + if (idx->export_bits(key.bits(), key.size(), true)) { + value = dict.lookup_ref(key); + } + if (opt) { + stack.push_maybe_cell(std::move(value)); + } else if (value.not_null()) { + stack.push_cell(std::move(value)); + stack.push_bool(true); + } else { + stack.push_bool(false); + } + return 0; +} + +int exec_get_global_common(VmState* st, unsigned n) { + st->get_stack().push(tuple_extend_index(st->get_c7(), n)); + return 0; +} + +int exec_get_global(VmState* st, unsigned args) { + args &= 31; + VM_LOG(st) << "execute GETGLOB " << args; + return exec_get_global_common(st, args); +} + +int exec_get_global_var(VmState* st) { + VM_LOG(st) << "execute GETGLOBVAR"; + st->check_underflow(1); + unsigned args = st->get_stack().pop_smallint_range(254); + return exec_get_global_common(st, args); +} + +int exec_set_global_common(VmState* st, unsigned idx) { + Stack& stack = st->get_stack(); + auto x = stack.pop(); + auto tuple = st->get_c7(); + if (idx >= 255) { + throw VmError{Excno::range_chk, "tuple index out of range"}; + } + auto tpay = tuple_extend_set_index(tuple, idx, std::move(x)); + if (tpay > 0) { + st->consume_tuple_gas(tpay); + } + st->set_c7(std::move(tuple)); + return 0; +} + +int exec_set_global(VmState* st, unsigned args) { + args &= 31; + VM_LOG(st) << "execute SETGLOB " << args; + st->check_underflow(1); + return exec_set_global_common(st, args); +} + +int exec_set_global_var(VmState* st) { + VM_LOG(st) << "execute SETGLOBVAR"; + st->check_underflow(2); + unsigned args = st->get_stack().pop_smallint_range(254); + return exec_set_global_common(st, args); +} + +void register_ton_config_ops(OpcodeTable& cp0) { + using namespace std::placeholders; + cp0.insert(OpcodeInstr::mkfixedrange(0xf820, 0xf823, 16, 4, instr::dump_1c("GETPARAM "), exec_get_var_param)) + .insert(OpcodeInstr::mksimple(0xf823, 16, "NOW", std::bind(exec_get_param, _1, 3, "NOW"))) + .insert(OpcodeInstr::mksimple(0xf824, 16, "BLOCKLT", std::bind(exec_get_param, _1, 4, "BLOCKLT"))) + .insert(OpcodeInstr::mksimple(0xf825, 16, "LTIME", std::bind(exec_get_param, _1, 5, "LTIME"))) + .insert(OpcodeInstr::mkfixedrange(0xf826, 0xf828, 16, 4, instr::dump_1c("GETPARAM "), exec_get_var_param)) + .insert(OpcodeInstr::mksimple(0xf828, 16, "MYADDR", std::bind(exec_get_param, _1, 8, "MYADDR"))) + .insert(OpcodeInstr::mksimple(0xf829, 16, "CONFIGROOT", std::bind(exec_get_param, _1, 9, "CONFIGROOT"))) + .insert(OpcodeInstr::mkfixedrange(0xf82a, 0xf830, 16, 4, instr::dump_1c("GETPARAM "), exec_get_var_param)) + .insert(OpcodeInstr::mksimple(0xf830, 16, "CONFIGDICT", exec_get_config_dict)) + .insert(OpcodeInstr::mksimple(0xf832, 16, "CONFIGPARAM", std::bind(exec_get_config_param, _1, false))) + .insert(OpcodeInstr::mksimple(0xf833, 16, "CONFIGOPTPARAM", std::bind(exec_get_config_param, _1, true))) + .insert(OpcodeInstr::mksimple(0xf840, 16, "GETGLOBVAR", exec_get_global_var)) + .insert(OpcodeInstr::mkfixedrange(0xf841, 0xf860, 16, 5, instr::dump_1c_and(31, "GETGLOB "), exec_get_global)) + .insert(OpcodeInstr::mksimple(0xf860, 16, "SETGLOBVAR", exec_set_global_var)) + .insert(OpcodeInstr::mkfixedrange(0xf861, 0xf880, 16, 5, instr::dump_1c_and(31, "SETGLOB "), exec_set_global)); +} + +int exec_compute_hash(VmState* st, int mode) { + VM_LOG(st) << "execute HASH" << (mode & 1 ? 'S' : 'C') << 'U'; + Stack& stack = st->get_stack(); + std::array hash; + if (!(mode & 1)) { + auto cell = stack.pop_cell(); + hash = cell->get_hash().as_array(); + } else { + auto cs = stack.pop_cellslice(); + vm::CellBuilder cb; + CHECK(cb.append_cellslice_bool(std::move(cs))); + // TODO: use cb.get_hash() instead + hash = cb.finalize()->get_hash().as_array(); + } + td::RefInt256 res{true}; + CHECK(res.write().import_bytes(hash.data(), hash.size(), false)); + stack.push_int(std::move(res)); + return 0; +} + +int exec_compute_sha256(VmState* st) { + VM_LOG(st) << "execute SHA256U"; + Stack& stack = st->get_stack(); + auto cs = stack.pop_cellslice(); + if (cs->size() & 7) { + throw VmError{Excno::cell_und, "Slice does not consist of an integer number of bytes"}; + } + auto len = (cs->size() >> 3); + unsigned char data[128], hash[32]; + CHECK(len <= sizeof(data)); + CHECK(cs->prefetch_bytes(data, len)); + digest::hash_str(hash, data, len); + td::RefInt256 res{true}; + CHECK(res.write().import_bytes(hash, 32, false)); + stack.push_int(std::move(res)); + return 0; +} + +int exec_ed25519_check_signature(VmState* st, bool from_slice) { + VM_LOG(st) << "execute CHKSIGN" << (from_slice ? 'S' : 'U'); + Stack& stack = st->get_stack(); + stack.check_underflow(3); + auto key_int = stack.pop_int(); + auto signature_cs = stack.pop_cellslice(); + unsigned char data[128], key[32], signature[64]; + unsigned data_len; + if (from_slice) { + auto cs = stack.pop_cellslice(); + if (cs->size() & 7) { + throw VmError{Excno::cell_und, "Slice does not consist of an integer number of bytes"}; + } + data_len = (cs->size() >> 3); + CHECK(data_len <= sizeof(data)); + CHECK(cs->prefetch_bytes(data, data_len)); + } else { + auto hash_int = stack.pop_int(); + data_len = 32; + if (!hash_int->export_bytes(data, data_len, false)) { + throw VmError{Excno::range_chk, "data hash must fit in an unsigned 256-bit integer"}; + } + } + if (!signature_cs->prefetch_bytes(signature, 64)) { + throw VmError{Excno::cell_und, "Ed25519 signature must contain at least 512 data bits"}; + } + if (!key_int->export_bytes(key, 32, false)) { + throw VmError{Excno::range_chk, "Ed25519 public key must fit in an unsigned 256-bit integer"}; + } + td::Ed25519::PublicKey pub_key{td::SecureString(td::Slice{key, 32})}; + auto res = pub_key.verify_signature(td::Slice{data, data_len}, td::Slice{signature, 64}); + stack.push_bool(res.is_ok()); + return 0; +} + +void register_ton_crypto_ops(OpcodeTable& cp0) { + using namespace std::placeholders; + cp0.insert(OpcodeInstr::mksimple(0xf900, 16, "HASHCU", std::bind(exec_compute_hash, _1, 0))) + .insert(OpcodeInstr::mksimple(0xf901, 16, "HASHSU", std::bind(exec_compute_hash, _1, 1))) + .insert(OpcodeInstr::mksimple(0xf902, 16, "SHA256U", exec_compute_sha256)) + .insert(OpcodeInstr::mksimple(0xf910, 16, "CHKSIGNU", std::bind(exec_ed25519_check_signature, _1, false))) + .insert(OpcodeInstr::mksimple(0xf911, 16, "CHKSIGNS", std::bind(exec_ed25519_check_signature, _1, true))); +} + +int exec_load_var_integer(VmState* st, int len_bits, bool sgnd, bool quiet) { + if (len_bits == 4 && !sgnd) { + VM_LOG(st) << "execute LDGRAMS" << (quiet ? "Q" : ""); + } else { + VM_LOG(st) << "execute LDVAR" << (sgnd ? "" : "U") << "INT" << (1 << len_bits) << (quiet ? "Q" : ""); + } + Stack& stack = st->get_stack(); + auto csr = stack.pop_cellslice(); + td::RefInt256 x; + int len; + if (!(csr.write().fetch_uint_to(len_bits, len) && csr.unique_write().fetch_int256_to(len * 8, x, sgnd))) { + if (quiet) { + stack.push_bool(false); + } else { + throw VmError{Excno::cell_und, "cannot deserialize a variable-length integer"}; + } + } else { + stack.push_int(std::move(x)); + stack.push_cellslice(std::move(csr)); + if (quiet) { + stack.push_bool(true); + } + } + return 0; +} + +int exec_store_var_integer(VmState* st, int len_bits, bool sgnd, bool quiet) { + if (len_bits == 4 && !sgnd) { + VM_LOG(st) << "execute STGRAMS" << (quiet ? "Q" : ""); + } else { + VM_LOG(st) << "execute STVAR" << (sgnd ? "" : "U") << "INT" << (1 << len_bits) << (quiet ? "Q" : ""); + } + Stack& stack = st->get_stack(); + stack.check_underflow(2); + auto x = stack.pop_int(); + auto cbr = stack.pop_builder(); + unsigned len = ((x->bit_size(sgnd) + 7) >> 3); + if (len >= (1u << len_bits)) { + throw VmError{Excno::range_chk}; + } + if (!(cbr.write().store_long_bool(len, len_bits) && cbr.unique_write().store_int256_bool(*x, len * 8, sgnd))) { + if (quiet) { + stack.push_bool(false); + } else { + throw VmError{Excno::cell_ov, "cannot serialize a variable-length integer"}; + } + } else { + stack.push_builder(std::move(cbr)); + if (quiet) { + stack.push_bool(true); + } + } + return 0; +} + +bool skip_maybe_anycast(CellSlice& cs) { + if (cs.prefetch_ulong(1) != 1) { + return cs.advance(1); + } + unsigned depth; + return cs.advance(1) // just$1 + && cs.fetch_uint_leq(30, depth) // anycast_info$_ depth:(#<= 30) + && depth >= 1 // { depth >= 1 } + && cs.advance(depth); // rewrite_pfx:(bits depth) = Anycast; +} + +bool skip_message_addr(CellSlice& cs) { + switch ((unsigned)cs.fetch_ulong(2)) { + case 0: // addr_none$00 = MsgAddressExt; + return true; + case 1: { // addr_extern$01 + unsigned len; + return cs.fetch_uint_to(9, len) // len:(## 9) + && cs.advance(len); // external_address:(bits len) = MsgAddressExt; + } + case 2: { // addr_std$10 + return skip_maybe_anycast(cs) // anycast:(Maybe Anycast) + && cs.advance(8 + 256); // workchain_id:int8 address:bits256 = MsgAddressInt; + } + case 3: { // addr_var$11 + unsigned len; + return skip_maybe_anycast(cs) // anycast:(Maybe Anycast) + && cs.fetch_uint_to(9, len) // addr_len:(## 9) + && cs.advance(32 + len); // workchain_id:int32 address:(bits addr_len) = MsgAddressInt; + } + default: + return false; + } +} + +int exec_load_message_addr(VmState* st, bool quiet) { + VM_LOG(st) << "execute LDMSGADDR" << (quiet ? "Q" : ""); + Stack& stack = st->get_stack(); + auto csr = stack.pop_cellslice(), csr_copy = csr; + auto& cs = csr.write(); + if (!(skip_message_addr(cs) && csr_copy.write().cut_tail(cs))) { + csr.clear(); + if (quiet) { + stack.push_cellslice(std::move(csr_copy)); + stack.push_bool(false); + } else { + throw VmError{Excno::cell_und, "cannot load a MsgAddress"}; + } + } else { + stack.push_cellslice(std::move(csr_copy)); + stack.push_cellslice(std::move(csr)); + if (quiet) { + stack.push_bool(true); + } + } + return 0; +} + +bool parse_maybe_anycast(CellSlice& cs, StackEntry& res) { + res = StackEntry{}; + if (cs.prefetch_ulong(1) != 1) { + return cs.advance(1); + } + unsigned depth; + Ref pfx; + if (cs.advance(1) // just$1 + && cs.fetch_uint_leq(30, depth) // anycast_info$_ depth:(#<= 30) + && depth >= 1 // { depth >= 1 } + && cs.fetch_subslice_to(depth, pfx)) { // rewrite_pfx:(bits depth) = Anycast; + res = std::move(pfx); + return true; + } + return false; +} + +bool parse_message_addr(CellSlice& cs, std::vector& res) { + res.clear(); + switch ((unsigned)cs.fetch_ulong(2)) { + case 0: // addr_none$00 = MsgAddressExt; + res.emplace_back(td::RefInt256{true, 0}); // -> (0) + return true; + case 1: { // addr_extern$01 + unsigned len; + Ref addr; + if (cs.fetch_uint_to(9, len) // len:(## 9) + && cs.fetch_subslice_to(len, addr)) { // external_address:(bits len) = MsgAddressExt; + res.emplace_back(td::RefInt256{true, 1}); + res.emplace_back(std::move(addr)); + return true; + } + break; + } + case 2: { // addr_std$10 + StackEntry v; + int workchain; + Ref addr; + if (parse_maybe_anycast(cs, v) // anycast:(Maybe Anycast) + && cs.fetch_int_to(8, workchain) // workchain_id:int8 + && cs.fetch_subslice_to(256, addr)) { // address:bits256 = MsgAddressInt; + res.emplace_back(td::RefInt256{true, 2}); + res.emplace_back(std::move(v)); + res.emplace_back(td::RefInt256{true, workchain}); + res.emplace_back(std::move(addr)); + return true; + } + break; + } + case 3: { // addr_var$11 + StackEntry v; + int len, workchain; + Ref addr; + if (parse_maybe_anycast(cs, v) // anycast:(Maybe Anycast) + && cs.fetch_uint_to(9, len) // addr_len:(## 9) + && cs.fetch_int_to(32, workchain) // workchain_id:int32 + && cs.fetch_subslice_to(len, addr)) { // address:(bits addr_len) = MsgAddressInt; + res.emplace_back(td::RefInt256{true, 3}); + res.emplace_back(std::move(v)); + res.emplace_back(td::RefInt256{true, workchain}); + res.emplace_back(std::move(addr)); + return true; + } + break; + } + } + return false; +} + +int exec_parse_message_addr(VmState* st, bool quiet) { + VM_LOG(st) << "execute PARSEMSGADDR" << (quiet ? "Q" : ""); + Stack& stack = st->get_stack(); + auto csr = stack.pop_cellslice(); + auto& cs = csr.write(); + std::vector res; + if (!(parse_message_addr(cs, res) && cs.empty_ext())) { + if (quiet) { + stack.push_bool(false); + } else { + throw VmError{Excno::cell_und, "cannot parse a MsgAddress"}; + } + } else { + stack.push_tuple(std::move(res)); + if (quiet) { + stack.push_bool(true); + } + } + return 0; +} + +// replaces first bits of `addr` with those of `prefix` +Ref do_rewrite_addr(Ref addr, Ref prefix) { + if (prefix.is_null() || !prefix->size()) { + return std::move(addr); + } + if (prefix->size() > addr->size()) { + return {}; + } + if (prefix->size() == addr->size()) { + return std::move(prefix); + } + vm::CellBuilder cb; + if (!(addr.write().advance(prefix->size()) && cb.append_cellslice_bool(std::move(prefix)) && + cb.append_cellslice_bool(std::move(addr)))) { + return {}; + } + return vm::load_cell_slice_ref(cb.finalize()); +} + +int exec_rewrite_message_addr(VmState* st, bool allow_var_addr, bool quiet) { + VM_LOG(st) << "execute REWRITE" << (allow_var_addr ? "VAR" : "STD") << "ADDR" << (quiet ? "Q" : ""); + Stack& stack = st->get_stack(); + auto csr = stack.pop_cellslice(); + auto& cs = csr.write(); + std::vector tuple; + if (!(parse_message_addr(cs, tuple) && cs.empty_ext())) { + if (quiet) { + stack.push_bool(false); + return 0; + } + throw VmError{Excno::cell_und, "cannot parse a MsgAddress"}; + } + int t = (int)std::move(tuple[0]).as_int()->to_long(); + if (t != 2 && t != 3) { + if (quiet) { + stack.push_bool(false); + return 0; + } + throw VmError{Excno::cell_und, "cannot parse a MsgAddressInt"}; + } + auto addr = std::move(tuple[3]).as_slice(); + auto prefix = std::move(tuple[1]).as_slice(); + if (!allow_var_addr) { + if (addr->size() != 256) { + if (quiet) { + stack.push_bool(false); + return 0; + } + throw VmError{Excno::cell_und, "MsgAddressInt is not a standard 256-bit address"}; + } + td::Bits256 rw_addr; + td::RefInt256 int_addr{true}; + CHECK(addr->prefetch_bits_to(rw_addr) && + (prefix.is_null() || prefix->prefetch_bits_to(rw_addr.bits(), prefix->size())) && + int_addr.unique_write().import_bits(rw_addr, false)); + stack.push(std::move(tuple[2])); + stack.push(std::move(int_addr)); + } else { + addr = do_rewrite_addr(std::move(addr), std::move(prefix)); + if (addr.is_null()) { + if (quiet) { + stack.push_bool(false); + return 0; + } + throw VmError{Excno::cell_und, "cannot rewrite address in a MsgAddressInt"}; + } + stack.push(std::move(tuple[2])); + stack.push(std::move(addr)); + } + if (quiet) { + stack.push_bool(true); + } + return 0; +} + +void register_ton_currency_address_ops(OpcodeTable& cp0) { + using namespace std::placeholders; + cp0.insert(OpcodeInstr::mksimple(0xfa00, 16, "LDGRAMS", std::bind(exec_load_var_integer, _1, 4, false, false))) + .insert(OpcodeInstr::mksimple(0xfa01, 16, "LDVARINT16", std::bind(exec_load_var_integer, _1, 4, true, false))) + .insert(OpcodeInstr::mksimple(0xfa02, 16, "STGRAMS", std::bind(exec_store_var_integer, _1, 4, false, false))) + .insert(OpcodeInstr::mksimple(0xfa03, 16, "STVARINT16", std::bind(exec_store_var_integer, _1, 4, true, false))) + .insert(OpcodeInstr::mksimple(0xfa04, 16, "LDVARUINT32", std::bind(exec_load_var_integer, _1, 5, false, false))) + .insert(OpcodeInstr::mksimple(0xfa05, 16, "LDVARINT32", std::bind(exec_load_var_integer, _1, 5, true, false))) + .insert(OpcodeInstr::mksimple(0xfa06, 16, "STVARUINT32", std::bind(exec_store_var_integer, _1, 5, false, false))) + .insert(OpcodeInstr::mksimple(0xfa07, 16, "STVARINT32", std::bind(exec_store_var_integer, _1, 5, true, false))) + .insert(OpcodeInstr::mksimple(0xfa40, 16, "LDMSGADDR", std::bind(exec_load_message_addr, _1, false))) + .insert(OpcodeInstr::mksimple(0xfa41, 16, "LDMSGADDRQ", std::bind(exec_load_message_addr, _1, true))) + .insert(OpcodeInstr::mksimple(0xfa42, 16, "PARSEMSGADDR", std::bind(exec_parse_message_addr, _1, false))) + .insert(OpcodeInstr::mksimple(0xfa43, 16, "PARSEMSGADDRQ", std::bind(exec_parse_message_addr, _1, true))) + .insert( + OpcodeInstr::mksimple(0xfa44, 16, "REWRITESTDADDR", std::bind(exec_rewrite_message_addr, _1, false, false))) + .insert( + OpcodeInstr::mksimple(0xfa45, 16, "REWRITESTDADDRQ", std::bind(exec_rewrite_message_addr, _1, false, true))) + .insert( + OpcodeInstr::mksimple(0xfa46, 16, "REWRITEVARADDR", std::bind(exec_rewrite_message_addr, _1, true, false))) + .insert( + OpcodeInstr::mksimple(0xfa47, 16, "REWRITEVARADDRQ", std::bind(exec_rewrite_message_addr, _1, true, true))); +} + +static constexpr int output_actions_idx = 5; + +int install_output_action(VmState* st, Ref new_action_head) { + // TODO: increase actions:uint16 and msgs_sent:uint16 in SmartContractInfo at first reference of c5 + VM_LOG(st) << "installing an output action"; + st->set_d(output_actions_idx, std::move(new_action_head)); + return 0; +} + +static inline Ref get_actions(VmState* st) { + return st->get_d(output_actions_idx); +} + +int exec_send_raw_message(VmState* st) { + VM_LOG(st) << "execute SENDRAWMSG"; + Stack& stack = st->get_stack(); + stack.check_underflow(2); + int f = stack.pop_smallint_range(255); + Ref msg_cell = stack.pop_cell(); + CellBuilder cb; + if (!(cb.store_ref_bool(get_actions(st)) // out_list$_ {n:#} prev:^(OutList n) + && cb.store_long_bool(0x0ec3c86d, 32) // action_send_msg#0ec3c86d + && cb.store_long_bool(f, 8) // mode:(## 8) + && cb.store_ref_bool(std::move(msg_cell)))) { + throw VmError{Excno::cell_ov, "cannot serialize raw output message into an output action cell"}; + } + return install_output_action(st, cb.finalize()); +} + +bool store_grams(CellBuilder& cb, td::RefInt256 value) { + int k = value->bit_size(false); + return k <= 15 * 8 && cb.store_long_bool((k + 7) >> 3, 4) && cb.store_int256_bool(*value, (k + 7) & -8, false); +} + +int exec_reserve_raw(VmState* st, int mode) { + VM_LOG(st) << "execute RESERVERAW" << (mode & 1 ? "X" : ""); + Stack& stack = st->get_stack(); + stack.check_underflow(2); + int f = stack.pop_smallint_range(3); + td::RefInt256 x; + Ref csr; + if (mode & 1) { + csr = stack.pop_cellslice(); + } else { + x = stack.pop_int_finite(); + if (td::sgn(x) < 0) { + throw VmError{Excno::range_chk, "amount of nanograms must be non-negative"}; + } + } + CellBuilder cb; + if (!(cb.store_ref_bool(get_actions(st)) // out_list$_ {n:#} prev:^(OutList n) + && cb.store_long_bool(0x36e6b809, 32) // action_reserve_currency#36e6b809 + && cb.store_long_bool(f, 8) // mode:(## 8) + && (mode & 1 ? cb.append_cellslice_bool(std::move(csr)) + : (store_grams(cb, std::move(x)) && cb.store_bool_bool(false))))) { + throw VmError{Excno::cell_ov, "cannot serialize raw reserved currency amount into an output action cell"}; + } + return install_output_action(st, cb.finalize()); +} + +int exec_set_code(VmState* st) { + VM_LOG(st) << "execute SETCODE"; + auto code = st->get_stack().pop_cell(); + CellBuilder cb; + if (!(cb.store_ref_bool(get_actions(st)) // out_list$_ {n:#} prev:^(OutList n) + && cb.store_long_bool(0xad4de08e, 32) // action_set_code#ad4de08e + && cb.store_ref_bool(std::move(code)))) { // new_code:^Cell = OutAction; + throw VmError{Excno::cell_ov, "cannot serialize new smart contract code into an output action cell"}; + } + return install_output_action(st, cb.finalize()); +} + +void register_ton_message_ops(OpcodeTable& cp0) { + using namespace std::placeholders; + cp0.insert(OpcodeInstr::mksimple(0xfb00, 16, "SENDRAWMSG", exec_send_raw_message)) + .insert(OpcodeInstr::mksimple(0xfb02, 16, "RESERVERAW", std::bind(exec_reserve_raw, _1, 0))) + .insert(OpcodeInstr::mksimple(0xfb03, 16, "RESERVERAWX", std::bind(exec_reserve_raw, _1, 1))) + .insert(OpcodeInstr::mksimple(0xfb04, 16, "SETCODE", exec_set_code)); +} + +void register_ton_ops(OpcodeTable& cp0) { + register_basic_gas_ops(cp0); + register_ton_gas_ops(cp0); + register_ton_config_ops(cp0); + register_ton_crypto_ops(cp0); + register_ton_currency_address_ops(cp0); + register_ton_message_ops(cp0); +} + +} // namespace vm diff --git a/crypto/vm/tonops.h b/crypto/vm/tonops.h new file mode 100644 index 00000000..c2809e91 --- /dev/null +++ b/crypto/vm/tonops.h @@ -0,0 +1,27 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +namespace vm { + +class OpcodeTable; + +void register_ton_ops(OpcodeTable& cp0); + +} // namespace vm diff --git a/crypto/vm/tupleops.cpp b/crypto/vm/tupleops.cpp new file mode 100644 index 00000000..de4de834 --- /dev/null +++ b/crypto/vm/tupleops.cpp @@ -0,0 +1,381 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "vm/log.h" +#include "vm/stackops.h" +#include "vm/opctable.h" +#include "vm/stack.hpp" +#include "vm/continuation.h" +#include "vm/excno.hpp" + +namespace vm { + +int exec_push_null(VmState* st) { + VM_LOG(st) << "execute PUSHNULL"; + st->get_stack().push({}); + return 0; +} + +int exec_is_null(VmState* st) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute ISNULL"; + stack.push_bool(stack.pop_chk().empty()); + return 0; +} + +int exec_null_swap_if(VmState* st, bool cond, int depth) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute NULL" << (depth ? "ROTR" : "SWAP") << (cond ? "IF" : "IFNOT"); + stack.check_underflow(depth + 1); + auto x = stack.pop_int_finite(); + if (!x->sgn() != cond) { + stack.push({}); + for (int i = 0; i < depth; i++) { + swap(stack[i], stack[i + 1]); + } + } + stack.push_int(std::move(x)); + return 0; +} + +int exec_mktuple_common(VmState* st, unsigned n) { + Stack& stack = st->get_stack(); + stack.check_underflow(n); + Ref ref{true}; + auto& tuple = ref.unique_write(); + tuple.reserve(n); + for (int i = n - 1; i >= 0; i--) { + tuple.push_back(std::move(stack[i])); + } + stack.pop_many(n); + st->consume_tuple_gas(n); + stack.push_tuple(std::move(ref)); + return 0; +} + +int exec_mktuple(VmState* st, unsigned args) { + args &= 15; + VM_LOG(st) << "execute TUPLE " << args; + return exec_mktuple_common(st, args); +} + +int exec_mktuple_var(VmState* st) { + VM_LOG(st) << "execute TUPLEVAR"; + unsigned args = st->get_stack().pop_smallint_range(255); + return exec_mktuple_common(st, args); +} + +int exec_tuple_index_common(Stack& stack, unsigned n) { + auto tuple = stack.pop_tuple_range(255); + stack.push(tuple_index(*tuple, n)); + return 0; +} + +int exec_tuple_index(VmState* st, unsigned args) { + args &= 15; + VM_LOG(st) << "execute INDEX " << args; + return exec_tuple_index_common(st->get_stack(), args); +} + +int exec_tuple_index_var(VmState* st) { + VM_LOG(st) << "execute INDEXVAR"; + st->check_underflow(2); + unsigned args = st->get_stack().pop_smallint_range(254); + return exec_tuple_index_common(st->get_stack(), args); +} + +int exec_tuple_quiet_index_common(Stack& stack, unsigned n) { + stack.push(tuple_extend_index(stack.pop_maybe_tuple_range(255), n)); + return 0; +} + +int exec_tuple_quiet_index(VmState* st, unsigned args) { + args &= 15; + VM_LOG(st) << "execute INDEXQ " << args; + return exec_tuple_quiet_index_common(st->get_stack(), args); +} + +int exec_tuple_quiet_index_var(VmState* st) { + VM_LOG(st) << "execute INDEXVARQ"; + st->check_underflow(2); + unsigned args = st->get_stack().pop_smallint_range(254); + return exec_tuple_quiet_index_common(st->get_stack(), args); +} + +int do_explode_tuple(VmState* st, Ref tuple, unsigned n) { + auto& stack = st->get_stack(); + if (tuple.is_unique()) { + auto& tw = tuple.unique_write(); + for (unsigned i = 0; i < n; i++) { + stack.push(std::move(tw[i])); + } + } else { + const auto& t = *tuple; + for (unsigned i = 0; i < n; i++) { + stack.push(t[i]); + } + } + st->consume_tuple_gas(n); + return 0; +} + +int exec_untuple_common(VmState* st, unsigned n) { + return do_explode_tuple(st, st->get_stack().pop_tuple_range(n, n), n); +} + +int exec_untuple(VmState* st, unsigned args) { + args &= 15; + VM_LOG(st) << "execute UNTUPLE " << args; + return exec_untuple_common(st, args); +} + +int exec_untuple_var(VmState* st) { + VM_LOG(st) << "execute UNTUPLEVAR"; + st->check_underflow(2); + unsigned args = st->get_stack().pop_smallint_range(255); + return exec_untuple_common(st, args); +} + +int exec_untuple_first_common(VmState* st, unsigned n) { + return do_explode_tuple(st, st->get_stack().pop_tuple_range(255, n), n); +} + +int exec_untuple_first(VmState* st, unsigned args) { + args &= 15; + VM_LOG(st) << "execute UNPACKFIRST " << args; + return exec_untuple_first_common(st, args); +} + +int exec_untuple_first_var(VmState* st) { + VM_LOG(st) << "execute UNPACKFIRSTVAR"; + st->check_underflow(2); + unsigned args = st->get_stack().pop_smallint_range(255); + return exec_untuple_first_common(st, args); +} + +int exec_explode_tuple_common(VmState* st, unsigned n) { + auto t = st->get_stack().pop_tuple_range(n); + unsigned l = (unsigned)(t->size()); + do_explode_tuple(st, std::move(t), l); + st->get_stack().push_smallint(l); + return 0; +} + +int exec_explode_tuple(VmState* st, unsigned args) { + args &= 15; + VM_LOG(st) << "execute EXPLODE " << args; + return exec_explode_tuple_common(st, args); +} + +int exec_explode_tuple_var(VmState* st) { + VM_LOG(st) << "execute EXPLODEVAR"; + st->check_underflow(2); + unsigned args = st->get_stack().pop_smallint_range(255); + return exec_explode_tuple_common(st, args); +} + +int exec_tuple_set_index_common(VmState* st, unsigned idx) { + Stack& stack = st->get_stack(); + auto x = stack.pop(); + auto tuple = stack.pop_tuple_range(255); + if (idx >= tuple->size()) { + throw VmError{Excno::range_chk, "tuple index out of range"}; + } + tuple.write()[idx] = std::move(x); + st->consume_tuple_gas(tuple); + stack.push(std::move(tuple)); + return 0; +} + +int exec_tuple_set_index(VmState* st, unsigned args) { + args &= 15; + VM_LOG(st) << "execute SETINDEX " << args; + st->check_underflow(2); + return exec_tuple_set_index_common(st, args); +} + +int exec_tuple_set_index_var(VmState* st) { + VM_LOG(st) << "execute SETINDEXVAR"; + st->check_underflow(3); + unsigned args = st->get_stack().pop_smallint_range(254); + return exec_tuple_set_index_common(st, args); +} + +int exec_tuple_quiet_set_index_common(VmState* st, unsigned idx) { + Stack& stack = st->get_stack(); + auto x = stack.pop(); + auto tuple = stack.pop_maybe_tuple_range(255); + if (idx >= 255) { + throw VmError{Excno::range_chk, "tuple index out of range"}; + } + auto tpay = tuple_extend_set_index(tuple, idx, std::move(x)); + if (tpay > 0) { + st->consume_tuple_gas(tpay); + } + stack.push_maybe_tuple(std::move(tuple)); + return 0; +} + +int exec_tuple_quiet_set_index(VmState* st, unsigned args) { + args &= 15; + VM_LOG(st) << "execute SETINDEXQ " << args; + st->check_underflow(2); + return exec_tuple_quiet_set_index_common(st, args); +} + +int exec_tuple_quiet_set_index_var(VmState* st) { + VM_LOG(st) << "execute SETINDEXVARQ"; + st->check_underflow(3); + unsigned args = st->get_stack().pop_smallint_range(254); + return exec_tuple_quiet_set_index_common(st, args); +} + +int exec_tuple_length(VmState* st) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute TLEN"; + auto t = stack.pop_tuple_range(255); + stack.push_smallint((long long)(t->size())); + return 0; +} + +int exec_tuple_length_quiet(VmState* st) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute QTLEN"; + auto t = stack.pop_chk(); + stack.push_smallint(t.is_tuple() ? (long long)(t.as_tuple()->size()) : -1LL); + return 0; +} + +int exec_is_tuple(VmState* st) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute ISTUPLE"; + stack.push_bool(stack.pop_chk().is_tuple()); + return 0; +} + +int exec_tuple_last(VmState* st) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute LAST"; + auto t = stack.pop_tuple_range(255, 1); + stack.push(t->back()); + return 0; +} + +int exec_tuple_push(VmState* st) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute TPUSH"; + stack.check_underflow(2); + auto x = stack.pop(); + auto t = stack.pop_tuple_range(254); + t.write().push_back(std::move(x)); + st->consume_tuple_gas(t); + stack.push(std::move(t)); + return 0; +} + +int exec_tuple_pop(VmState* st) { + Stack& stack = st->get_stack(); + VM_LOG(st) << "execute TPOP"; + auto t = stack.pop_tuple_range(255, 1); + auto x = std::move(t.write().back()); + t.write().pop_back(); + st->consume_tuple_gas(t); + stack.push(std::move(t)); + stack.push(std::move(x)); + return 0; +} + +int exec_tuple_index2(VmState* st, unsigned args) { + unsigned i = (args >> 2) & 3, j = args & 3; + VM_LOG(st) << "execute INDEX2 " << i << "," << j; + Stack& stack = st->get_stack(); + auto tuple = stack.pop_tuple_range(255); + auto t1 = tuple_index(*tuple, i).as_tuple_range(255); + if (t1.is_null()) { + throw VmError{Excno::type_chk, "intermediate value is not a tuple"}; + } + stack.push(tuple_index(*t1, j)); + return 0; +} + +std::string dump_tuple_index2(CellSlice& cs, unsigned args) { + unsigned i = (args >> 2) & 3, j = args & 3; + std::ostringstream os; + os << "INDEX2 " << i << ',' << j; + return os.str(); +} + +int exec_tuple_index3(VmState* st, unsigned args) { + unsigned i = (args >> 4) & 3, j = (args >> 2) & 3, k = args & 3; + VM_LOG(st) << "execute INDEX3 " << i << "," << j << "," << k; + Stack& stack = st->get_stack(); + auto tuple = stack.pop_tuple_range(255); + auto t1 = tuple_index(*tuple, i).as_tuple_range(255); + if (t1.is_null()) { + throw VmError{Excno::type_chk, "intermediate value is not a tuple"}; + } + auto t2 = tuple_index(*t1, j).as_tuple_range(255); + if (t2.is_null()) { + throw VmError{Excno::type_chk, "intermediate value is not a tuple"}; + } + stack.push(tuple_index(*t2, k)); + return 0; +} + +std::string dump_tuple_index3(CellSlice& cs, unsigned args) { + unsigned i = (args >> 4) & 3, j = (args >> 2) & 3, k = args & 3; + std::ostringstream os; + os << "INDEX3 " << i << ',' << j << ',' << k; + return os.str(); +} + +void register_tuple_ops(OpcodeTable& cp0) { + using namespace std::placeholders; + cp0.insert(OpcodeInstr::mksimple(0x6d, 8, "PUSHNULL", exec_push_null)) + .insert(OpcodeInstr::mksimple(0x6e, 8, "ISNULL", exec_is_null)) + .insert(OpcodeInstr::mkfixed(0x6f0, 12, 4, instr::dump_1c("TUPLE "), exec_mktuple)) + .insert(OpcodeInstr::mkfixed(0x6f1, 12, 4, instr::dump_1c("INDEX "), exec_tuple_index)) + .insert(OpcodeInstr::mkfixed(0x6f2, 12, 4, instr::dump_1c("UNTUPLE "), exec_untuple)) + .insert(OpcodeInstr::mkfixed(0x6f3, 12, 4, instr::dump_1c("UNPACKFIRST "), exec_untuple_first)) + .insert(OpcodeInstr::mkfixed(0x6f4, 12, 4, instr::dump_1c("EXPLODE "), exec_explode_tuple)) + .insert(OpcodeInstr::mkfixed(0x6f5, 12, 4, instr::dump_1c("SETINDEX "), exec_tuple_set_index)) + .insert(OpcodeInstr::mkfixed(0x6f6, 12, 4, instr::dump_1c("INDEXQ "), exec_tuple_quiet_index)) + .insert(OpcodeInstr::mkfixed(0x6f7, 12, 4, instr::dump_1c("SETINDEXQ "), exec_tuple_quiet_set_index)) + .insert(OpcodeInstr::mksimple(0x6f80, 16, "TUPLEVAR", exec_mktuple_var)) + .insert(OpcodeInstr::mksimple(0x6f81, 16, "INDEXVAR", exec_tuple_index_var)) + .insert(OpcodeInstr::mksimple(0x6f82, 16, "UNTUPLEVAR", exec_untuple_var)) + .insert(OpcodeInstr::mksimple(0x6f83, 16, "UNPACKFIRSTVAR", exec_untuple_first_var)) + .insert(OpcodeInstr::mksimple(0x6f84, 16, "EXPLODEVAR", exec_explode_tuple_var)) + .insert(OpcodeInstr::mksimple(0x6f85, 16, "SETINDEXVAR", exec_tuple_set_index_var)) + .insert(OpcodeInstr::mksimple(0x6f86, 16, "INDEXVARQ", exec_tuple_quiet_index_var)) + .insert(OpcodeInstr::mksimple(0x6f87, 16, "SETINDEXVARQ", exec_tuple_quiet_set_index_var)) + .insert(OpcodeInstr::mksimple(0x6f88, 16, "TLEN", exec_tuple_length)) + .insert(OpcodeInstr::mksimple(0x6f89, 16, "QTLEN", exec_tuple_length_quiet)) + .insert(OpcodeInstr::mksimple(0x6f8a, 16, "ISTUPLE", exec_is_tuple)) + .insert(OpcodeInstr::mksimple(0x6f8b, 16, "LAST", exec_tuple_last)) + .insert(OpcodeInstr::mksimple(0x6f8c, 16, "TPUSH", exec_tuple_push)) + .insert(OpcodeInstr::mksimple(0x6f8d, 16, "TPOP", exec_tuple_pop)) + .insert(OpcodeInstr::mksimple(0x6fa0, 16, "NULLSWAPIF", std::bind(exec_null_swap_if, _1, true, 0))) + .insert(OpcodeInstr::mksimple(0x6fa1, 16, "NULLSWAPIFNOT", std::bind(exec_null_swap_if, _1, false, 0))) + .insert(OpcodeInstr::mksimple(0x6fa2, 16, "NULLROTRIF", std::bind(exec_null_swap_if, _1, true, 1))) + .insert(OpcodeInstr::mksimple(0x6fa3, 16, "NULLROTRIFNOT", std::bind(exec_null_swap_if, _1, false, 1))) + .insert(OpcodeInstr::mkfixed(0x6fb, 12, 4, dump_tuple_index2, exec_tuple_index2)) + .insert(OpcodeInstr::mkfixed(0x6fc >> 2, 10, 6, dump_tuple_index3, exec_tuple_index3)); +} + +} // namespace vm diff --git a/crypto/vm/tupleops.h b/crypto/vm/tupleops.h new file mode 100644 index 00000000..cef8a0f4 --- /dev/null +++ b/crypto/vm/tupleops.h @@ -0,0 +1,27 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +namespace vm { + +class OpcodeTable; + +void register_tuple_ops(OpcodeTable& cp0); + +} // namespace vm diff --git a/crypto/vm/vmstate.h b/crypto/vm/vmstate.h new file mode 100644 index 00000000..312b6626 --- /dev/null +++ b/crypto/vm/vmstate.h @@ -0,0 +1,39 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once +#include "common/refcnt.hpp" +#include "vm/cells.h" + +#include "td/utils/Context.h" + +namespace vm { +using td::Ref; + +class VmStateInterface : public td::Context { + public: + virtual ~VmStateInterface() = default; + virtual Ref load_library( + td::ConstBitPtr hash) { // may throw a dictionary exception; returns nullptr if library is not found + return {}; + } + virtual void register_cell_load(){}; + virtual void register_cell_create(){}; +}; + +} // namespace vm diff --git a/dht-server/CMakeLists.txt b/dht-server/CMakeLists.txt new file mode 100644 index 00000000..af67a432 --- /dev/null +++ b/dht-server/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 3.0.2 FATAL_ERROR) + +if (NOT OPENSSL_FOUND) + find_package(OpenSSL REQUIRED) +endif() + + +set (DHT_SERVER_SOURCE + dht-server.cpp + dht-server.hpp +) + +add_executable (dht-server ${DHT_SERVER_SOURCE}) +target_link_libraries(dht-server tdutils tdactor adnl tl_api dht memprof ${JEMALLOC_LIBRARIES}) diff --git a/dht-server/dht-server.cpp b/dht-server/dht-server.cpp new file mode 100644 index 00000000..9d97df61 --- /dev/null +++ b/dht-server/dht-server.cpp @@ -0,0 +1,1238 @@ +/* + This file is part of TON Blockchain source code. + + TON Blockchain is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + TON Blockchain is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with TON Blockchain. If not, see . + + In addition, as a special exception, the copyright holders give permission + to link the code of portions of this program with the OpenSSL library. + You must obey the GNU General Public License in all respects for all + of the code used other than OpenSSL. If you modify file(s) with this + exception, you may extend this exception to your version of the file(s), + but you are not obligated to do so. If you do not wish to do so, delete this + exception statement from your version. If you delete this exception statement + from all source files in the program, then also delete it here. + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "dht-server.hpp" + +#include "td/utils/filesystem.h" +#include "td/actor/MultiPromise.h" +#include "td/utils/overloaded.h" +#include "td/utils/OptionsParser.h" +#include "td/utils/port/path.h" +#include "td/utils/port/user.h" +#include "td/utils/port/signals.h" +#include "td/utils/ThreadSafeCounter.h" +#include "td/utils/TsFileLog.h" +#include "td/utils/Random.h" + +#include "memprof/memprof.h" + +#if TD_DARWIN || TD_LINUX +#include +#endif +#include +#include +#include +#include +#include + +Config::Config() { + out_port = 3278; +} + +Config::Config(ton::ton_api::engine_validator_config &config) { + out_port = static_cast(config.out_port_); + if (!out_port) { + out_port = 3278; + } + for (auto &addr : config.addrs_) { + td::IPAddress in_ip; + td::IPAddress out_ip; + std::shared_ptr proxy = nullptr; + std::vector categories; + std::vector priority_categories; + ton::ton_api::downcast_call( + *addr.get(), + td::overloaded( + [&](const ton::ton_api::engine_addr &obj) { + in_ip.init_ipv4_port(td::IPAddress::ipv4_to_str(obj.ip_), static_cast(obj.port_)).ensure(); + out_ip = in_ip; + categories = obj.categories_; + priority_categories = obj.priority_categories_; + }, + [&](const ton::ton_api::engine_addrProxy &obj) { + in_ip.init_ipv4_port(td::IPAddress::ipv4_to_str(obj.in_ip_), static_cast(obj.in_port_)) + .ensure(); + out_ip.init_ipv4_port(td::IPAddress::ipv4_to_str(obj.out_ip_), static_cast(obj.out_port_)) + .ensure(); + if (obj.proxy_type_) { + auto R = ton::adnl::AdnlProxy::create(*obj.proxy_type_.get()); + R.ensure(); + proxy = R.move_as_ok(); + categories = obj.categories_; + priority_categories = obj.priority_categories_; + } + })); + + config_add_network_addr(in_ip, out_ip, std::move(proxy), categories, priority_categories).ensure(); + } + for (auto &adnl : config.adnl_) { + config_add_adnl_addr(ton::PublicKeyHash{adnl->id_}, adnl->category_).ensure(); + } + for (auto &dht : config.dht_) { + config_add_dht_node(ton::PublicKeyHash{dht->id_}).ensure(); + } + + for (auto &serv : config.control_) { + auto key = ton::PublicKeyHash{serv->id_}; + config_add_control_interface(key, serv->port_).ensure(); + + for (auto &proc : serv->allowed_) { + config_add_control_process(key, serv->port_, ton::PublicKeyHash{proc->id_}, proc->permissions_).ensure(); + } + } + + if (config.gc_) { + for (auto &gc : config.gc_->ids_) { + config_add_gc(ton::PublicKeyHash{gc}).ensure(); + } + } +} + +ton::tl_object_ptr Config::tl() const { + std::vector> addrs_vec; + for (auto &x : addrs) { + if (x.second.proxy) { + addrs_vec.push_back(ton::create_tl_object( + static_cast(x.second.in_addr.get_ipv4()), x.second.in_addr.get_port(), + static_cast(x.first.addr.get_ipv4()), x.first.addr.get_port(), x.second.proxy->tl(), + std::vector(x.second.cats.begin(), x.second.cats.end()), + std::vector(x.second.priority_cats.begin(), x.second.priority_cats.end()))); + } else { + addrs_vec.push_back(ton::create_tl_object( + static_cast(x.first.addr.get_ipv4()), x.first.addr.get_port(), + std::vector(x.second.cats.begin(), x.second.cats.end()), + std::vector(x.second.priority_cats.begin(), x.second.priority_cats.end()))); + } + } + std::vector> adnl_vec; + for (auto &x : adnl_ids) { + adnl_vec.push_back(ton::create_tl_object(x.first.tl(), x.second)); + } + std::vector> dht_vec; + for (auto &x : dht_ids) { + dht_vec.push_back(ton::create_tl_object(x.tl())); + } + + std::vector> val_vec; + + std::vector> liteserver_vec; + + std::vector> control_vec; + for (auto &x : controls) { + std::vector> control_proc_vec; + for (auto &y : x.second.clients) { + control_proc_vec.push_back(ton::create_tl_object(y.first.tl(), y.second)); + } + control_vec.push_back(ton::create_tl_object(x.second.key.tl(), x.first, + std::move(control_proc_vec))); + } + + auto gc_vec = ton::create_tl_object(std::vector{}); + for (auto &id : gc) { + gc_vec->ids_.push_back(id.tl()); + } + return ton::create_tl_object( + out_port, std::move(addrs_vec), std::move(adnl_vec), std::move(dht_vec), std::move(val_vec), + ton::PublicKeyHash::zero().tl(), std::move(liteserver_vec), std::move(control_vec), std::move(gc_vec)); +} + +td::Result Config::config_add_network_addr(td::IPAddress in_ip, td::IPAddress out_ip, + std::shared_ptr proxy, + std::vector cats, std::vector prio_cats) { + Addr addr{out_ip}; + + auto it = addrs.find(addr); + if (it != addrs.end()) { + bool mod = false; + if (!(it->second.in_addr == in_ip)) { + it->second.in_addr = in_ip; + mod = true; + } + if (it->second.proxy != proxy) { + it->second.proxy = std::move(proxy); + mod = true; + } + for (auto &c : cats) { + if (it->second.cats.insert(c).second) { + mod = true; + } + } + for (auto &c : prio_cats) { + if (it->second.priority_cats.insert(c).second) { + mod = true; + } + } + return mod; + } else { + it = addrs.emplace(std::move(addr), AddrCats{}).first; + it->second.in_addr = in_ip; + it->second.proxy = std::move(proxy); + for (auto &c : cats) { + it->second.cats.insert(c); + } + for (auto &c : prio_cats) { + it->second.priority_cats.insert(c); + } + return true; + } +} + +td::Result Config::config_add_adnl_addr(ton::PublicKeyHash addr, AdnlCategory cat) { + auto it = adnl_ids.find(addr); + if (it != adnl_ids.end()) { + if (it->second != cat) { + it->second = cat; + return true; + } else { + return false; + } + } else { + incref(addr); + adnl_ids.emplace(addr, cat); + return true; + } +} + +td::Result Config::config_add_dht_node(ton::PublicKeyHash id) { + if (dht_ids.count(id) > 0) { + return false; + } + if (adnl_ids.count(id) == 0) { + return td::Status::Error(ton::ErrorCode::notready, "to-be-added dht node not in adnl nodes list"); + } + incref(id); + dht_ids.insert(id); + return true; +} + +td::Result Config::config_add_control_interface(ton::PublicKeyHash key, td::int32 port) { + auto it = controls.find(port); + if (it != controls.end()) { + if (it->second.key == key) { + return false; + } else { + return td::Status::Error(ton::ErrorCode::error, "duplicate port"); + } + } else { + incref(key); + controls.emplace(port, Control{key, {}}); + return true; + } +} + +td::Result Config::config_add_control_process(ton::PublicKeyHash key, td::int32 port, ton::PublicKeyHash id, + td::uint32 permissions) { + if (controls.count(port) == 0) { + return td::Status::Error(ton::ErrorCode::error, "unknown control interface"); + } + auto &v = controls[port]; + if (v.key != key) { + return td::Status::Error(ton::ErrorCode::error, "unknown control interface"); + } + + auto it = v.clients.find(id); + if (it != v.clients.end()) { + if (!permissions) { + v.clients.erase(id); + return true; + } + if (it->second != permissions) { + it->second = permissions; + return true; + } else { + return false; + } + } else { + if (!permissions) { + return false; + } + v.clients.emplace(id, permissions); + return true; + } +} + +td::Result Config::config_add_gc(ton::PublicKeyHash key) { + return gc.insert(key).second; +} + +void Config::decref(ton::PublicKeyHash key) { + auto v = keys_refcnt[key]--; + CHECK(v > 0); + if (v == 1) { + config_add_gc(key).ensure(); + } +} + +td::Result Config::config_del_network_addr(td::IPAddress a, std::vector cats, + std::vector prio_cats) { + Addr addr{a}; + + auto it = addrs.find(addr); + if (it != addrs.end()) { + bool mod = false; + for (auto &c : cats) { + if (it->second.cats.erase(c)) { + mod = true; + } + } + for (auto &c : prio_cats) { + if (it->second.priority_cats.erase(c)) { + mod = true; + } + } + return mod; + } else { + return false; + } +} + +td::Result Config::config_del_adnl_addr(ton::PublicKeyHash addr) { + if (adnl_ids.count(addr) == 0) { + return false; + } + + if (dht_ids.count(addr)) { + return td::Status::Error(ton::ErrorCode::error, "adnl addr still in use"); + } + + decref(addr); + adnl_ids.erase(addr); + return true; +} + +td::Result Config::config_del_dht_node(ton::PublicKeyHash id) { + if (dht_ids.count(id) == 0) { + return false; + } + decref(id); + dht_ids.erase(id); + return true; +} + +td::Result Config::config_del_control_interface(td::int32 port) { + auto it = controls.find(port); + if (it != controls.end()) { + decref(it->second.key); + controls.erase(it); + return true; + } else { + return false; + } +} + +td::Result Config::config_del_control_process(td::int32 port, ton::PublicKeyHash id) { + auto it = controls.find(port); + if (it != controls.end()) { + return it->second.clients.erase(id); + } else { + return false; + } +} + +td::Result Config::config_del_gc(ton::PublicKeyHash key) { + return gc.erase(key); +} + +void DhtServer::set_local_config(std::string str) { + local_config_ = str; +} +void DhtServer::set_global_config(std::string str) { + global_config_ = str; +} +void DhtServer::set_db_root(std::string db_root) { + db_root_ = db_root; +} +void DhtServer::start_up() { + alarm_timestamp() = td::Timestamp::in(1.0 + td::Random::fast(0, 100) * 0.01); +} + +void DhtServer::alarm() { + alarm_timestamp() = td::Timestamp::in(1.0 + td::Random::fast(0, 100) * 0.01); + + if (started_) { + for (auto &x : config_.gc) { + if (running_gc_.count(x) == 0) { + running_gc_.insert(x); + + auto P = td::PromiseCreator::lambda([SelfId = actor_id(this), x](td::Result R) { + R.ensure(); + td::actor::send_closure(SelfId, &DhtServer::deleted_key, x); + }); + td::actor::send_closure(keyring_, &ton::keyring::Keyring::del_key, x, std::move(P)); + } + } + } +} + +void DhtServer::deleted_key(ton::PublicKeyHash x) { + CHECK(running_gc_.count(x) == 1); + running_gc_.erase(x); + auto R = config_.config_del_gc(x); + R.ensure(); + if (R.move_as_ok()) { + write_config([](td::Unit) {}); + } +} + +td::Status DhtServer::load_global_config() { + TRY_RESULT_PREFIX(conf_data, td::read_file(global_config_), "failed to read: "); + TRY_RESULT_PREFIX(conf_json, td::json_decode(conf_data.as_slice()), "failed to parse json: "); + + ton::ton_api::config_global conf; + TRY_STATUS_PREFIX(ton::ton_api::from_json(conf, conf_json.get_object()), "json does not fit TL scheme: "); + + // TODO + // add adnl static nodes + //if (conf.adnl_) { + // td::actor::send_closure(adnl_, &ton::adnl::Adnl::add_static_nodes_from_config, + // std::move(conf.adnl_->static_nodes_)); + //} + if (!conf.dht_) { + return td::Status::Error(ton::ErrorCode::error, "does not contain [dht] section"); + } + + TRY_RESULT_PREFIX(dht, ton::dht::Dht::create_global_config(std::move(conf.dht_)), "bad [dht] section: "); + dht_config_ = std::move(dht); + + if (!conf.validator_) { + return td::Status::Error(ton::ErrorCode::error, "does not contain [validator] section"); + } + + if (!conf.validator_->zero_state_) { + return td::Status::Error(ton::ErrorCode::error, "[validator] section does not contain [zero_state]"); + } + + return td::Status::OK(); +} + +void DhtServer::load_empty_local_config(td::Promise promise) { + auto ret_promise = td::PromiseCreator::lambda( + [SelfId = actor_id(this), promise = std::move(promise)](td::Result R) mutable { + if (R.is_error()) { + promise.set_error(R.move_as_error()); + } else { + td::actor::send_closure(SelfId, &DhtServer::write_config, std::move(promise)); + } + }); + + td::MultiPromise mp; + auto ig = mp.init_guard(); + ig.add_promise(std::move(ret_promise)); + + for (auto &addr : addrs_) { + config_.config_add_network_addr(addr, addr, nullptr, std::vector{0, 1, 2, 3}, std::vector{}) + .ensure(); + } + + { + auto pk = ton::PrivateKey{ton::privkeys::Ed25519::random()}; + keys_.emplace(pk.compute_short_id(), pk.compute_public_key()); + auto id = pk.compute_short_id(); + td::actor::send_closure(keyring_, &ton::keyring::Keyring::add_key, std::move(pk), false, ig.get_promise()); + config_.config_add_adnl_addr(id, 0).ensure(); + config_.config_add_dht_node(id).ensure(); + } +} + +void DhtServer::load_local_config(td::Promise promise) { + if (local_config_.size() == 0) { + load_empty_local_config(std::move(promise)); + return; + } + auto conf_data_R = td::read_file(local_config_); + if (conf_data_R.is_error()) { + promise.set_error(conf_data_R.move_as_error_prefix("failed to read: ")); + return; + } + auto conf_data = conf_data_R.move_as_ok(); + auto conf_json_R = td::json_decode(conf_data.as_slice()); + if (conf_json_R.is_error()) { + promise.set_error(conf_data_R.move_as_error_prefix("failed to parse json: ")); + return; + } + auto conf_json = conf_json_R.move_as_ok(); + + ton::ton_api::config_local conf; + auto S = ton::ton_api::from_json(conf, conf_json.get_object()); + if (S.is_error()) { + promise.set_error(S.move_as_error_prefix("json does not fit TL scheme")); + return; + } + + auto ret_promise = td::PromiseCreator::lambda( + [SelfId = actor_id(this), promise = std::move(promise)](td::Result R) mutable { + if (R.is_error()) { + promise.set_error(R.move_as_error()); + } else { + td::actor::send_closure(SelfId, &DhtServer::write_config, std::move(promise)); + } + }); + + td::MultiPromise mp; + auto ig = mp.init_guard(); + ig.add_promise(std::move(ret_promise)); + + for (auto &addr : addrs_) { + config_.config_add_network_addr(addr, addr, nullptr, std::vector{0, 1, 2, 3}, std::vector{}) + .ensure(); + } + + for (auto &local_id : conf.local_ids_) { + ton::PrivateKey pk{local_id->id_}; + keys_.emplace(pk.compute_short_id(), pk.compute_public_key()); + td::actor::send_closure(keyring_, &ton::keyring::Keyring::add_key, std::move(pk), false, ig.get_promise()); + } + + if (conf.dht_.size() > 0) { + for (auto &d : conf.dht_) { + ton::ton_api::downcast_call(*d.get(), td::overloaded( + [&](ton::ton_api::dht_config_local &obj) { + auto node_id = ton::adnl::AdnlNodeIdShort{obj.id_->id_}; + auto it = keys_.find(node_id.pubkey_hash()); + if (it == keys_.end()) { + ig.get_promise().set_error(td::Status::Error( + ton::ErrorCode::error, "cannot find private key for dht")); + return; + } + + config_.config_add_adnl_addr(node_id.pubkey_hash(), 0).ensure(); + config_.config_add_dht_node(node_id.pubkey_hash()).ensure(); + }, + [&](ton::ton_api::dht_config_random_local &obj) { + for (td::int32 i = 0; i < obj.cnt_; i++) { + auto pk = ton::PrivateKey{ton::privkeys::Ed25519::random()}; + keys_.emplace(pk.compute_short_id(), pk.compute_public_key()); + auto id = pk.compute_short_id(); + td::actor::send_closure(keyring_, &ton::keyring::Keyring::add_key, + std::move(pk), false, ig.get_promise()); + config_.config_add_adnl_addr(id, 0).ensure(); + config_.config_add_dht_node(id).ensure(); + } + })); + } + } else { + auto pk = ton::PrivateKey{ton::privkeys::Ed25519::random()}; + keys_.emplace(pk.compute_short_id(), pk.compute_public_key()); + auto id = pk.compute_short_id(); + td::actor::send_closure(keyring_, &ton::keyring::Keyring::add_key, std::move(pk), false, ig.get_promise()); + config_.config_add_adnl_addr(id, 0).ensure(); + config_.config_add_dht_node(id).ensure(); + } + + for (auto &ci : conf.control_) { + ton::PrivateKey pk{ci->priv_}; + keys_.emplace(pk.compute_short_id(), pk.compute_public_key()); + auto short_id = pk.compute_short_id(); + td::actor::send_closure(keyring_, &ton::keyring::Keyring::add_key, std::move(pk), false, ig.get_promise()); + + config_.config_add_control_interface(short_id, ci->port_).ensure(); + config_.config_add_control_process(short_id, ci->port_, ton::PublicKeyHash{ci->pub_}, 0x7fffffff).ensure(); + } +} + +void DhtServer::load_config(td::Promise promise) { + if (!config_file_.size()) { + config_file_ = db_root_ + "/config.json"; + } + auto conf_data_R = td::read_file(config_file_); + if (conf_data_R.is_error()) { + auto P = td::PromiseCreator::lambda( + [name = local_config_, new_name = config_file_, promise = std::move(promise)](td::Result R) { + if (R.is_error()) { + LOG(ERROR) << "failed to parse local config '" << name << "': " << R.move_as_error(); + std::_Exit(2); + } else { + LOG(ERROR) << "created config file '" << new_name << "'"; + LOG(ERROR) << "check it manually before continue"; + std::_Exit(0); + } + }); + + load_local_config(std::move(P)); + return; + } + + auto conf_data = conf_data_R.move_as_ok(); + auto conf_json_R = td::json_decode(conf_data.as_slice()); + if (conf_json_R.is_error()) { + promise.set_error(conf_data_R.move_as_error_prefix("failed to parse json: ")); + return; + } + auto conf_json = conf_json_R.move_as_ok(); + + ton::ton_api::engine_validator_config conf; + auto S = ton::ton_api::from_json(conf, conf_json.get_object()); + if (S.is_error()) { + promise.set_error(S.move_as_error_prefix("json does not fit TL scheme")); + return; + } + + config_ = Config{conf}; + + td::MultiPromise mp; + auto ig = mp.init_guard(); + ig.add_promise(std::move(promise)); + + for (auto &key : config_.keys_refcnt) { + td::actor::send_closure(keyring_, &ton::keyring::Keyring::add_key_short, key.first, get_key_promise(ig)); + } + + write_config(ig.get_promise()); +} + +void DhtServer::write_config(td::Promise promise) { + auto s = td::json_encode(td::ToJson(*config_.tl().get()), true); + + auto S = td::write_file(config_file_, s); + if (S.is_ok()) { + promise.set_value(td::Unit()); + } else { + promise.set_error(std::move(S)); + } +} + +td::Promise DhtServer::get_key_promise(td::MultiPromise::InitGuard &ig) { + auto P = td::PromiseCreator::lambda( + [SelfId = actor_id(this), promise = ig.get_promise()](td::Result R) mutable { + if (R.is_error()) { + promise.set_error(R.move_as_error()); + } else { + td::actor::send_closure(SelfId, &DhtServer::got_key, R.move_as_ok()); + promise.set_value(td::Unit()); + } + }); + return std::move(P); +} + +void DhtServer::got_key(ton::PublicKey key) { + keys_[key.compute_short_id()] = key; +} + +void DhtServer::start() { + read_config_ = true; + start_adnl(); +} + +void DhtServer::start_adnl() { + adnl_network_manager_ = ton::adnl::AdnlNetworkManager::create(config_.out_port); + adnl_ = ton::adnl::Adnl::create(db_root_, keyring_.get()); + td::actor::send_closure(adnl_, &ton::adnl::Adnl::register_network_manager, adnl_network_manager_.get()); + + for (auto &addr : config_.addrs) { + add_addr(addr.first, addr.second); + } + for (auto &adnl : config_.adnl_ids) { + add_adnl(adnl.first, adnl.second); + } + started_adnl(); +} + +void DhtServer::add_addr(const Config::Addr &addr, const Config::AddrCats &cats) { + if (!cats.proxy) { + td::actor::send_closure(adnl_network_manager_, &ton::adnl::AdnlNetworkManager::add_self_addr, addr.addr, + cats.cats.size() ? 0 : 1); + } else { + td::actor::send_closure(adnl_network_manager_, &ton::adnl::AdnlNetworkManager::add_proxy_addr, addr.addr, + cats.proxy, cats.cats.size() ? 0 : 1); + } + + td::uint32 ts = static_cast(td::Clocks::system()); + + for (auto cat : cats.cats) { + CHECK(cat >= 0); + ton::adnl::AdnlAddress x = ton::adnl::AdnlAddressImpl::create( + ton::create_tl_object(cats.in_addr.get_ipv4(), cats.in_addr.get_port())); + addr_lists_[cat].add_addr(std::move(x)); + addr_lists_[cat].set_version(ts); + addr_lists_[cat].set_reinit_date(ton::adnl::Adnl::adnl_start_time()); + } + for (auto cat : cats.priority_cats) { + CHECK(cat >= 0); + ton::adnl::AdnlAddress x = ton::adnl::AdnlAddressImpl::create( + ton::create_tl_object(cats.in_addr.get_ipv4(), cats.in_addr.get_port())); + prio_addr_lists_[cat].add_addr(std::move(x)); + prio_addr_lists_[cat].set_version(ts); + prio_addr_lists_[cat].set_reinit_date(ton::adnl::Adnl::adnl_start_time()); + } +} + +void DhtServer::add_adnl(ton::PublicKeyHash id, AdnlCategory cat) { + CHECK(addr_lists_[cat].size() > 0); + CHECK(keys_.count(id) > 0); + td::actor::send_closure(adnl_, &ton::adnl::Adnl::add_id, ton::adnl::AdnlNodeIdFull{keys_[id]}, addr_lists_[cat]); +} + +void DhtServer::started_adnl() { + start_dht(); +} + +void DhtServer::start_dht() { + for (auto &dht : config_.dht_ids) { + auto D = ton::dht::Dht::create(ton::adnl::AdnlNodeIdShort{dht}, db_root_, dht_config_, keyring_.get(), adnl_.get()); + D.ensure(); + + dht_nodes_[dht] = D.move_as_ok(); + if (default_dht_node_.is_zero()) { + default_dht_node_ = dht; + } + } + + CHECK(!default_dht_node_.is_zero()); + td::actor::send_closure(adnl_, &ton::adnl::Adnl::register_dht_node, dht_nodes_[default_dht_node_].get()); + + started_dht(); +} + +void DhtServer::started_dht() { + start_control_interface(); +} + +void DhtServer::start_control_interface() { + class Callback : public ton::adnl::Adnl::Callback { + public: + void receive_message(ton::adnl::AdnlNodeIdShort src, ton::adnl::AdnlNodeIdShort dst, + td::BufferSlice data) override { + } + void receive_query(ton::adnl::AdnlNodeIdShort src, ton::adnl::AdnlNodeIdShort dst, td::BufferSlice data, + td::Promise promise) override { + td::actor::send_closure(id_, &DhtServer::process_control_query, src, dst, std::move(data), std::move(promise)); + } + + Callback(td::actor::ActorId id) : id_(id) { + } + + private: + td::actor::ActorId id_; + }; + + std::vector c_ids; + std::vector ports; + + for (auto &s : config_.controls) { + td::actor::send_closure(adnl_, &ton::adnl::Adnl::add_id, ton::adnl::AdnlNodeIdFull{keys_[s.second.key]}, + ton::adnl::AdnlAddressList{}); + td::actor::send_closure(adnl_, &ton::adnl::Adnl::subscribe, ton::adnl::AdnlNodeIdShort{s.second.key}, + std::string(""), std::make_unique(actor_id(this))); + + c_ids.push_back(ton::adnl::AdnlNodeIdShort{s.second.key}); + ports.push_back(static_cast(s.first)); + + for (auto &p : s.second.clients) { + control_permissions_[p.first] |= p.second; + } + } + + auto P = td::PromiseCreator::lambda( + [SelfId = actor_id(this)](td::Result> R) { + R.ensure(); + td::actor::send_closure(SelfId, &DhtServer::started_control_interface, R.move_as_ok()); + }); + td::actor::send_closure(adnl_, &ton::adnl::Adnl::create_ext_server, std::move(c_ids), std::move(ports), std::move(P)); +} + +void DhtServer::started_control_interface(td::actor::ActorOwn control_ext_server) { + control_ext_server_ = std::move(control_ext_server); + started(); +} + +void DhtServer::started() { + started_ = true; +} + +void DhtServer::add_adnl_node(ton::PublicKey key, AdnlCategory cat, td::Promise promise) { + if (cat < 0 || static_cast(cat) > max_cat()) { + promise.set_error(td::Status::Error(ton::ErrorCode::protoviolation, "bad category value")); + return; + } + + auto R = config_.config_add_adnl_addr(key.compute_short_id(), cat); + if (R.is_error()) { + promise.set_error(R.move_as_error()); + return; + } + + if (!R.move_as_ok()) { + promise.set_value(td::Unit()); + return; + } + + if (!adnl_.empty()) { + td::actor::send_closure(adnl_, &ton::adnl::Adnl::add_id, ton::adnl::AdnlNodeIdFull{key}, addr_lists_[cat]); + } + + write_config(std::move(promise)); +} + +void DhtServer::add_dht_node(ton::PublicKeyHash key_hash, td::Promise promise) { + auto R = config_.config_add_dht_node(key_hash); + if (R.is_error()) { + promise.set_error(R.move_as_error()); + return; + } + + if (!R.move_as_ok()) { + promise.set_value(td::Unit()); + return; + } + + if (dht_nodes_.size() > 0) { + auto D = + ton::dht::Dht::create(ton::adnl::AdnlNodeIdShort{key_hash}, db_root_, dht_config_, keyring_.get(), adnl_.get()); + D.ensure(); + + dht_nodes_[key_hash] = D.move_as_ok(); + + if (default_dht_node_.is_zero()) { + default_dht_node_ = key_hash; + } + } + + write_config(std::move(promise)); +} + +void DhtServer::add_control_interface(ton::PublicKeyHash id, td::int32 port, td::Promise promise) { + auto R = config_.config_add_control_interface(id, port); + if (R.is_error()) { + promise.set_error(R.move_as_error()); + return; + } + + if (!R.move_as_ok()) { + promise.set_value(td::Unit()); + return; + } + + td::actor::send_closure(control_ext_server_, &ton::adnl::AdnlExtServer::add_local_id, ton::adnl::AdnlNodeIdShort{id}); + td::actor::send_closure(control_ext_server_, &ton::adnl::AdnlExtServer::add_tcp_port, static_cast(port)); + + write_config(std::move(promise)); +} + +void DhtServer::add_control_process(ton::PublicKeyHash id, td::int32 port, ton::PublicKeyHash pub, + td::int32 permissions, td::Promise promise) { + auto R = config_.config_add_control_process(id, port, pub, permissions); + if (R.is_error()) { + promise.set_error(R.move_as_error()); + return; + } + + if (!R.move_as_ok()) { + promise.set_value(td::Unit()); + return; + } + + control_permissions_[pub] |= permissions; + + write_config(std::move(promise)); +} + +td::BufferSlice DhtServer::create_control_query_error(td::Status error) { + return ton::serialize_tl_object( + ton::create_tl_object(error.code(), error.message().str()), + true); +} + +void DhtServer::run_control_query(ton::ton_api::engine_validator_getTime &query, td::BufferSlice data, + ton::PublicKeyHash src, td::uint32 perm, td::Promise promise) { + if (!(perm & DhtServerPermissions::vep_default)) { + promise.set_value(create_control_query_error(td::Status::Error(ton::ErrorCode::error, "not authorized"))); + return; + } + auto obj = ton::create_tl_object(static_cast(td::Clocks::system())); + promise.set_value(ton::serialize_tl_object(obj, true)); +} + +void DhtServer::run_control_query(ton::ton_api::engine_validator_importPrivateKey &query, td::BufferSlice data, + ton::PublicKeyHash src, td::uint32 perm, td::Promise promise) { + if (!(perm & DhtServerPermissions::vep_default)) { + promise.set_value(create_control_query_error(td::Status::Error(ton::ErrorCode::error, "not authorized"))); + return; + } + if (keyring_.empty()) { + promise.set_value(create_control_query_error(td::Status::Error(ton::ErrorCode::notready, "not started keyring"))); + return; + } + + auto pk = ton::PrivateKey{query.key_}; + auto P = td::PromiseCreator::lambda( + [promise = std::move(promise), hash = pk.compute_short_id()](td::Result R) mutable { + if (R.is_error()) { + promise.set_value(create_control_query_error(R.move_as_error())); + } else { + promise.set_value( + ton::serialize_tl_object(ton::create_tl_object(hash.tl()), true)); + } + }); + + td::actor::send_closure(keyring_, &ton::keyring::Keyring::add_key, std::move(pk), false, std::move(P)); +} + +void DhtServer::run_control_query(ton::ton_api::engine_validator_exportPrivateKey &query, td::BufferSlice data, + ton::PublicKeyHash src, td::uint32 perm, td::Promise promise) { + if (!(perm & DhtServerPermissions::vep_unsafe)) { + promise.set_value(create_control_query_error(td::Status::Error(ton::ErrorCode::error, "not authorized"))); + return; + } + if (keyring_.empty()) { + promise.set_value(create_control_query_error(td::Status::Error(ton::ErrorCode::notready, "not started keyring"))); + return; + } + + promise.set_value(create_control_query_error(td::Status::Error(ton::ErrorCode::notready, "not implemented"))); +} + +void DhtServer::run_control_query(ton::ton_api::engine_validator_exportPublicKey &query, td::BufferSlice data, + ton::PublicKeyHash src, td::uint32 perm, td::Promise promise) { + if (!(perm & DhtServerPermissions::vep_default)) { + promise.set_value(create_control_query_error(td::Status::Error(ton::ErrorCode::error, "not authorized"))); + return; + } + if (keyring_.empty()) { + promise.set_value(create_control_query_error(td::Status::Error(ton::ErrorCode::notready, "not started keyring"))); + return; + } + + auto P = td::PromiseCreator::lambda([promise = std::move(promise)](td::Result R) mutable { + if (R.is_error()) { + promise.set_value(create_control_query_error(R.move_as_error())); + } else { + auto pub = R.move_as_ok(); + promise.set_value(ton::serialize_tl_object(pub.tl(), true)); + } + }); + + td::actor::send_closure(keyring_, &ton::keyring::Keyring::get_public_key, ton::PublicKeyHash{query.key_hash_}, + std::move(P)); +} + +void DhtServer::run_control_query(ton::ton_api::engine_validator_generateKeyPair &query, td::BufferSlice data, + ton::PublicKeyHash src, td::uint32 perm, td::Promise promise) { + if (!(perm & DhtServerPermissions::vep_default)) { + promise.set_value(create_control_query_error(td::Status::Error(ton::ErrorCode::error, "not authorized"))); + return; + } + if (keyring_.empty()) { + promise.set_value(create_control_query_error(td::Status::Error(ton::ErrorCode::notready, "not started keyring"))); + return; + } + + auto pk = ton::PrivateKey{ton::privkeys::Ed25519::random()}; + + auto P = td::PromiseCreator::lambda( + [promise = std::move(promise), hash = pk.compute_short_id()](td::Result R) mutable { + if (R.is_error()) { + promise.set_value(create_control_query_error(R.move_as_error())); + } else { + promise.set_value( + ton::serialize_tl_object(ton::create_tl_object(hash.tl()), true)); + } + }); + + td::actor::send_closure(keyring_, &ton::keyring::Keyring::add_key, std::move(pk), false, std::move(P)); +} + +void DhtServer::run_control_query(ton::ton_api::engine_validator_addAdnlId &query, td::BufferSlice data, + ton::PublicKeyHash src, td::uint32 perm, td::Promise promise) { + if (!(perm & DhtServerPermissions::vep_modify)) { + promise.set_value(create_control_query_error(td::Status::Error(ton::ErrorCode::error, "not authorized"))); + return; + } + if (!started_) { + promise.set_value(create_control_query_error(td::Status::Error(ton::ErrorCode::notready, "not started"))); + return; + } + + auto P = td::PromiseCreator::lambda([SelfId = actor_id(this), cat = query.category_, + promise = std::move(promise)](td::Result R) mutable { + if (R.is_error()) { + promise.set_value(create_control_query_error(R.move_as_error_prefix("failed to get public key: "))); + return; + } + auto pub = R.move_as_ok(); + auto P = td::PromiseCreator::lambda([promise = std::move(promise)](td::Result R) mutable { + if (R.is_error()) { + promise.set_value(create_control_query_error(R.move_as_error_prefix("failed to add adnl node: "))); + } else { + promise.set_value( + ton::serialize_tl_object(ton::create_tl_object(), true)); + } + }); + td::actor::send_closure(SelfId, &DhtServer::add_adnl_node, pub, cat, std::move(P)); + }); + + td::actor::send_closure(keyring_, &ton::keyring::Keyring::get_public_key, ton::PublicKeyHash{query.key_hash_}, + std::move(P)); +} + +void DhtServer::run_control_query(ton::ton_api::engine_validator_addDhtId &query, td::BufferSlice data, + ton::PublicKeyHash src, td::uint32 perm, td::Promise promise) { + if (!(perm & DhtServerPermissions::vep_modify)) { + promise.set_value(create_control_query_error(td::Status::Error(ton::ErrorCode::error, "not authorized"))); + return; + } + if (!started_) { + promise.set_value(create_control_query_error(td::Status::Error(ton::ErrorCode::notready, "not started"))); + return; + } + + auto P = td::PromiseCreator::lambda([SelfId = actor_id(this), key_hash = ton::PublicKeyHash{query.key_hash_}, + promise = std::move(promise)](td::Result R) mutable { + if (R.is_error()) { + promise.set_value(create_control_query_error(R.move_as_error_prefix("failed to get public key: "))); + return; + } + auto P = td::PromiseCreator::lambda([promise = std::move(promise)](td::Result R) mutable { + if (R.is_error()) { + promise.set_value(create_control_query_error(R.move_as_error_prefix("failed to add adnl node: "))); + } else { + promise.set_value( + ton::serialize_tl_object(ton::create_tl_object(), true)); + } + }); + td::actor::send_closure(SelfId, &DhtServer::add_dht_node, key_hash, std::move(P)); + }); + + td::actor::send_closure(keyring_, &ton::keyring::Keyring::check_key, ton::PublicKeyHash{query.key_hash_}, + std::move(P)); +} + +void DhtServer::run_control_query(ton::ton_api::engine_validator_getConfig &query, td::BufferSlice data, + ton::PublicKeyHash src, td::uint32 perm, td::Promise promise) { + if (!(perm & DhtServerPermissions::vep_default)) { + promise.set_value(create_control_query_error(td::Status::Error(ton::ErrorCode::error, "not authorized"))); + return; + } + + auto s = td::json_encode(td::ToJson(*config_.tl().get()), true); + promise.set_value(td::BufferSlice{s}); +} + +void DhtServer::run_control_query(ton::ton_api::engine_validator_sign &query, td::BufferSlice data, + ton::PublicKeyHash src, td::uint32 perm, td::Promise promise) { + if (!(perm & DhtServerPermissions::vep_unsafe)) { + promise.set_value(create_control_query_error(td::Status::Error(ton::ErrorCode::error, "not authorized"))); + return; + } + if (!started_) { + promise.set_value(create_control_query_error(td::Status::Error(ton::ErrorCode::notready, "not started"))); + return; + } + + auto P = td::PromiseCreator::lambda([promise = std::move(promise)](td::Result R) mutable { + if (R.is_error()) { + promise.set_value(create_control_query_error(R.move_as_error())); + } else { + promise.set_value(ton::serialize_tl_object( + ton::create_tl_object(R.move_as_ok()), true)); + } + }); + td::actor::send_closure(keyring_, &ton::keyring::Keyring::sign_message, ton::PublicKeyHash{query.key_hash_}, + std::move(query.data_), std::move(P)); +} + +void DhtServer::process_control_query(ton::adnl::AdnlNodeIdShort src, ton::adnl::AdnlNodeIdShort dst, + td::BufferSlice data, td::Promise promise) { + auto G = ton::fetch_tl_object(std::move(data), true); + if (G.is_error()) { + promise.set_error(G.move_as_error_prefix("failed to parse validator query: ")); + return; + } + data = std::move(G.move_as_ok()->data_); + auto F = ton::fetch_tl_object(data.clone(), true); + if (F.is_error()) { + promise.set_error(F.move_as_error_prefix("failed to parse validator query: ")); + return; + } + auto f = F.move_as_ok(); + + auto it = control_permissions_.find(src.pubkey_hash()); + if (it == control_permissions_.end()) { + promise.set_error(td::Status::Error(ton::ErrorCode::protoviolation, "forbidden")); + return; + } + + ton::ton_api::downcast_call(*f.get(), [&](auto &obj) { + run_control_query(obj, std::move(data), src.pubkey_hash(), it->second, std::move(promise)); + }); +} + +void DhtServer::run() { + td::mkdir(db_root_).ensure(); + + auto Sr = load_global_config(); + if (Sr.is_error()) { + LOG(ERROR) << "failed to load global config'" << global_config_ << "': " << Sr; + std::_Exit(2); + } + + keyring_ = ton::keyring::Keyring::create(db_root_ + "/keyring"); + // TODO wait for password + started_keyring_ = true; + + auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result R) { + if (R.is_error()) { + LOG(ERROR) << "failed to parse config: " << R.move_as_error(); + std::_Exit(2); + } else { + td::actor::send_closure(SelfId, &DhtServer::start); + } + }); + load_config(std::move(P)); +} + +std::atomic need_stats_flag{false}; +void need_stats(int sig) { + need_stats_flag.store(true); +} +void dump_memory_stats() { + if (!is_memprof_on()) { + return; + } + LOG(WARNING) << "memory_dump"; + std::vector v; + dump_alloc([&](const AllocInfo &info) { v.push_back(info); }); + std::sort(v.begin(), v.end(), [](const AllocInfo &a, const AllocInfo &b) { return a.size > b.size; }); + size_t total_size = 0; + size_t other_size = 0; + int cnt = 0; + for (auto &info : v) { + if (cnt++ < 50) { + LOG(WARNING) << td::format::as_size(info.size) << td::format::as_array(info.backtrace); + } else { + other_size += info.size; + } + total_size += info.size; + } + LOG(WARNING) << td::tag("other", td::format::as_size(other_size)); + LOG(WARNING) << td::tag("total", td::format::as_size(total_size)); + LOG(WARNING) << td::tag("total traces", get_ht_size()); + LOG(WARNING) << td::tag("fast_backtrace_success_rate", get_fast_backtrace_success_rate()); +} +void dump_stats() { + dump_memory_stats(); + LOG(WARNING) << td::NamedThreadSafeCounter::get_default(); +} + +int main(int argc, char *argv[]) { + SET_VERBOSITY_LEVEL(verbosity_INFO); + + td::set_default_failure_signal_handler().ensure(); + + td::actor::ActorOwn x; + td::unique_ptr logger_; + SCOPE_EXIT { + td::log_interface = td::default_log_interface; + }; + + std::vector> acts; + + td::OptionsParser p; + p.set_description("dht server for TON DHT network"); + p.add_option('v', "verbosity", "set verbosity level", [&](td::Slice arg) { + int v = VERBOSITY_NAME(FATAL) + (td::to_integer(arg)); + SET_VERBOSITY_LEVEL(v); + return td::Status::OK(); + }); + p.add_option('h', "help", "prints_help", [&]() { + char b[10240]; + td::StringBuilder sb(td::MutableSlice{b, 10000}); + sb << p; + std::cout << sb.as_cslice().c_str(); + std::exit(2); + return td::Status::OK(); + }); + p.add_option('C', "global-config", "file to read global config", [&](td::Slice fname) { + acts.push_back([&x, fname = fname.str()]() { td::actor::send_closure(x, &DhtServer::set_global_config, fname); }); + return td::Status::OK(); + }); + p.add_option('c', "local-config", "file to read local config", [&](td::Slice fname) { + acts.push_back([&x, fname = fname.str()]() { td::actor::send_closure(x, &DhtServer::set_local_config, fname); }); + return td::Status::OK(); + }); + p.add_option('I', "ip", "ip:port of instance", [&](td::Slice arg) { + td::IPAddress addr; + TRY_STATUS(addr.init_host_port(arg.str())); + acts.push_back([&x, addr]() { td::actor::send_closure(x, &DhtServer::add_ip, addr); }); + return td::Status::OK(); + }); + p.add_option('D', "db", "root for dbs", [&](td::Slice fname) { + acts.push_back([&x, fname = fname.str()]() { td::actor::send_closure(x, &DhtServer::set_db_root, fname); }); + return td::Status::OK(); + }); + p.add_option('d', "daemonize", "set SIGHUP", [&]() { + td::set_signal_handler(td::SignalType::HangUp, [](int sig) { +#if TD_DARWIN || TD_LINUX + close(0); + setsid(); +#endif + }).ensure(); + return td::Status::OK(); + }); +#if TD_DARWIN || TD_LINUX + p.add_option('l', "logname", "log to file", [&](td::Slice fname) { + logger_ = td::TsFileLog::create(fname.str()).move_as_ok(); + td::log_interface = logger_.get(); + return td::Status::OK(); + }); +#endif + td::uint32 threads = 7; + p.add_option('t', "threads", PSTRING() << "number of threads (default=" << threads << ")", [&](td::Slice fname) { + td::int32 v; + try { + v = std::stoi(fname.str()); + } catch (...) { + return td::Status::Error(ton::ErrorCode::error, "bad value for --threads: not a number"); + } + if (v < 1 || v > 256) { + return td::Status::Error(ton::ErrorCode::error, "bad value for --threads: should be in range [1..256]"); + } + threads = v; + return td::Status::OK(); + }); + p.add_option('u', "user", "change user", [&](td::Slice user) { return td::change_user(user); }); + p.run(argc, argv).ensure(); + + td::set_runtime_signal_handler(1, need_stats).ensure(); + + td::actor::Scheduler scheduler({threads}); + + scheduler.run_in_context([&] { + x = td::actor::create_actor("dht-server"); + for (auto &act : acts) { + act(); + } + acts.clear(); + td::actor::send_closure(x, &DhtServer::run); + }); + while (scheduler.run(1)) { + if (need_stats_flag.exchange(false)) { + dump_stats(); + } + } + + return 0; +} diff --git a/dht-server/dht-server.hpp b/dht-server/dht-server.hpp new file mode 100644 index 00000000..3d1c9ff6 --- /dev/null +++ b/dht-server/dht-server.hpp @@ -0,0 +1,217 @@ +/* + This file is part of TON Blockchain source code. + + TON Blockchain is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + TON Blockchain is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with TON Blockchain. If not, see . + + In addition, as a special exception, the copyright holders give permission + to link the code of portions of this program with the OpenSSL library. + You must obey the GNU General Public License in all respects for all + of the code used other than OpenSSL. If you modify file(s) with this + exception, you may extend this exception to your version of the file(s), + but you are not obligated to do so. If you do not wish to do so, delete this + exception statement from your version. If you delete this exception statement + from all source files in the program, then also delete it here. + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "adnl/adnl.h" +#include "dht/dht.h" + +#include "td/actor/MultiPromise.h" + +#include "ton/ton-types.h" + +#include "auto/tl/ton_api_json.h" +#include "auto/tl/ton_api.hpp" + +#include +#include + +enum DhtServerPermissions : td::uint32 { vep_default = 1, vep_modify = 2, vep_unsafe = 4 }; + +using AdnlCategory = td::int32; + +struct Config { + struct Addr { + td::IPAddress addr; + + bool operator<(const Addr &with) const { + return addr < with.addr; + } + }; + struct AddrCats { + td::IPAddress in_addr; + std::shared_ptr proxy; + std::set cats; + std::set priority_cats; + }; + struct Control { + ton::PublicKeyHash key; + std::map clients; + }; + + std::map keys_refcnt; + td::uint16 out_port; + std::map addrs; + std::map adnl_ids; + std::set dht_ids; + std::map controls; + std::set gc; + + void decref(ton::PublicKeyHash key); + void incref(ton::PublicKeyHash key) { + keys_refcnt[key]++; + } + + td::Result config_add_network_addr(td::IPAddress in_addr, td::IPAddress out_addr, + std::shared_ptr proxy, std::vector cats, + std::vector prio_cats); + td::Result config_add_adnl_addr(ton::PublicKeyHash addr, AdnlCategory cat); + td::Result config_add_dht_node(ton::PublicKeyHash id); + td::Result config_add_control_interface(ton::PublicKeyHash key, td::int32 port); + td::Result config_add_control_process(ton::PublicKeyHash key, td::int32 port, ton::PublicKeyHash id, + td::uint32 permissions); + td::Result config_add_gc(ton::PublicKeyHash key); + td::Result config_del_network_addr(td::IPAddress addr, std::vector cats, + std::vector prio_cats); + td::Result config_del_adnl_addr(ton::PublicKeyHash addr); + td::Result config_del_dht_node(ton::PublicKeyHash id); + td::Result config_del_control_interface(td::int32 port); + td::Result config_del_control_process(td::int32 port, ton::PublicKeyHash id); + td::Result config_del_gc(ton::PublicKeyHash key); + + ton::tl_object_ptr tl() const; + + Config(); + Config(ton::ton_api::engine_validator_config &config); +}; + +class DhtServer : public td::actor::Actor { + private: + td::actor::ActorOwn keyring_; + td::actor::ActorOwn adnl_network_manager_; + td::actor::ActorOwn adnl_; + std::map> dht_nodes_; + ton::PublicKeyHash default_dht_node_ = ton::PublicKeyHash::zero(); + td::actor::ActorOwn control_ext_server_; + + std::string local_config_ = ""; + std::string global_config_ = "ton-global.config"; + std::string config_file_; + + std::string db_root_ = "/var/ton-work/db/"; + + std::vector addrs_; + std::vector proxy_addrs_; + + std::shared_ptr dht_config_; + Config config_; + + std::set running_gc_; + + std::map keys_; + + td::Promise get_key_promise(td::MultiPromise::InitGuard &ig); + void got_key(ton::PublicKey key); + void deleted_key(ton::PublicKeyHash key); + + void write_config(td::Promise promise); + + std::map addr_lists_; + std::map prio_addr_lists_; + + std::map control_permissions_; + + bool read_config_ = false; + bool started_keyring_ = false; + bool started_ = false; + + public: + static constexpr td::uint32 max_cat() { + return 256; + } + + void set_local_config(std::string str); + void set_global_config(std::string str); + void set_db_root(std::string db_root); + void add_ip(td::IPAddress addr) { + addrs_.push_back(addr); + } + void start_up() override; + DhtServer() { + } + + // load config + td::Status load_global_config(); + void load_empty_local_config(td::Promise promise); + void load_local_config(td::Promise promise); + void load_config(td::Promise promise); + + void start(); + + void start_adnl(); + void add_addr(const Config::Addr &addr, const Config::AddrCats &cats); + void add_adnl(ton::PublicKeyHash id, AdnlCategory cat); + void started_adnl(); + + void start_dht(); + void add_dht(td::actor::ActorOwn dht, td::Promise promise); + void started_dht(); + + void start_control_interface(); + void started_control_interface(td::actor::ActorOwn control_ext_server); + + void started(); + + void alarm() override; + void run(); + + void add_adnl_node(ton::PublicKey pub, td::int32 cat, td::Promise promise); + void add_dht_node(ton::PublicKeyHash pub, td::Promise promise); + void add_control_interface(ton::PublicKeyHash id, td::int32 port, td::Promise promise); + void add_control_process(ton::PublicKeyHash id, td::int32 port, ton::PublicKeyHash pub, td::int32 permissions, + td::Promise promise); + void del_adnl_node(ton::PublicKeyHash pub, td::Promise promise); + void del_dht_node(ton::PublicKeyHash pub, td::Promise promise); + + static td::BufferSlice create_control_query_error(td::Status error); + + void run_control_query(ton::ton_api::engine_validator_getTime &query, td::BufferSlice data, ton::PublicKeyHash src, + td::uint32 perm, td::Promise promise); + void run_control_query(ton::ton_api::engine_validator_importPrivateKey &query, td::BufferSlice data, + ton::PublicKeyHash src, td::uint32 perm, td::Promise promise); + void run_control_query(ton::ton_api::engine_validator_exportPrivateKey &query, td::BufferSlice data, + ton::PublicKeyHash src, td::uint32 perm, td::Promise promise); + void run_control_query(ton::ton_api::engine_validator_exportPublicKey &query, td::BufferSlice data, + ton::PublicKeyHash src, td::uint32 perm, td::Promise promise); + void run_control_query(ton::ton_api::engine_validator_generateKeyPair &query, td::BufferSlice data, + ton::PublicKeyHash src, td::uint32 perm, td::Promise promise); + void run_control_query(ton::ton_api::engine_validator_addAdnlId &query, td::BufferSlice data, ton::PublicKeyHash src, + td::uint32 perm, td::Promise promise); + void run_control_query(ton::ton_api::engine_validator_addDhtId &query, td::BufferSlice data, ton::PublicKeyHash src, + td::uint32 perm, td::Promise promise); + void run_control_query(ton::ton_api::engine_validator_getConfig &query, td::BufferSlice data, ton::PublicKeyHash src, + td::uint32 perm, td::Promise promise); + void run_control_query(ton::ton_api::engine_validator_sign &query, td::BufferSlice data, ton::PublicKeyHash src, + td::uint32 perm, td::Promise promise); + template + void run_control_query(T &query, td::BufferSlice data, ton::PublicKeyHash src, td::uint32 perm, + td::Promise promise) { + promise.set_value( + create_control_query_error(td::Status::Error(ton::ErrorCode::protoviolation, "query not supported"))); + } + void process_control_query(ton::adnl::AdnlNodeIdShort src, ton::adnl::AdnlNodeIdShort dst, td::BufferSlice data, + td::Promise promise); +}; + diff --git a/dht/CMakeLists.txt b/dht/CMakeLists.txt new file mode 100644 index 00000000..36cfdd82 --- /dev/null +++ b/dht/CMakeLists.txt @@ -0,0 +1,32 @@ +cmake_minimum_required(VERSION 3.0.2 FATAL_ERROR) + +if (NOT OPENSSL_FOUND) + find_package(OpenSSL REQUIRED) +endif() + +set(DHT_SOURCE + dht.cpp + dht-remote-node.cpp + dht-bucket.cpp + dht-node.cpp + dht-query.cpp + dht-types.cpp + + dht-bucket.hpp + dht-in.hpp + dht-query.hpp + dht-remote-node.hpp + dht-types.h + dht.h + dht.hpp +) + +add_library(dht STATIC ${DHT_SOURCE}) + +target_include_directories(dht PUBLIC + $ + $/.. + ${OPENSSL_INCLUDE_DIR} +) +target_link_libraries(dht PRIVATE tdutils tdactor adnl tl_api) + diff --git a/dht/dht-bucket.cpp b/dht/dht-bucket.cpp new file mode 100644 index 00000000..4e4d9918 --- /dev/null +++ b/dht/dht-bucket.cpp @@ -0,0 +1,208 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "td/utils/tl_storers.h" +#include "td/utils/crypto.h" +#include "td/utils/tl_parsers.h" +#include "td/utils/Random.h" + +#include "td/utils/format.h" + +#include "keys/encryptor.h" + +#include "auto/tl/ton_api.hpp" + +#include "dht-remote-node.hpp" +#include "dht-bucket.hpp" +#include "dht.hpp" + +//#include + +namespace ton { + +namespace dht { + +void DhtBucket::get_nearest_nodes(DhtKeyId id, td::uint32 bit, DhtNodesList &vec, td::uint32 k) { + if (active_nodes_.size() == 0) { + return; + } + std::map list; + + for (size_t i = 0; i < active_nodes_.size(); i++) { + auto &node = active_nodes_[i]; + if (node) { + list.emplace(id ^ node->get_key(), i); + } + } + + for (auto it = list.begin(); it != list.end() && vec.size() < k; it++) { + vec.push_back(active_nodes_[it->second]->get_node()); + } +} + +td::uint32 DhtBucket::active_cnt() { + td::uint32 cnt = 0; + for (auto &node : active_nodes_) { + if (node) { + cnt++; + } + } + return cnt; +} + +td::Status DhtBucket::add_full_node(DhtKeyId id, DhtNode newnode, td::actor::ActorId adnl, + adnl::AdnlNodeIdShort self_id) { + for (auto &node : active_nodes_) { + if (node && node->get_key() == id) { + return node->update_value(std::move(newnode), adnl, self_id); + } + } + for (auto &node : backup_nodes_) { + if (node && node->get_key() == id) { + return node->update_value(std::move(newnode), adnl, self_id); + } + } + + TRY_RESULT_PREFIX(N, DhtRemoteNode::create(std::move(newnode), max_missed_pings_), "failed to add new node: "); + + for (auto &node : backup_nodes_) { + if (node == nullptr) { + node = std::move(N); + return td::Status::OK(); + } + } + + for (auto &node : backup_nodes_) { + CHECK(node); + if (node->ready_from() == 0 && node->failed_from() + 60 < td::Time::now_cached()) { + node = std::move(N); + return td::Status::OK(); + } + } + + return td::Status::OK(); +} + +void DhtBucket::receive_ping(DhtKeyId id, DhtNode result, td::actor::ActorId adnl, + adnl::AdnlNodeIdShort self_id) { + for (auto &node : active_nodes_) { + if (node && node->get_key() == id) { + node->receive_ping(std::move(result), adnl, self_id); + return; + } + } + for (size_t i = 0; i < backup_nodes_.size(); i++) { + auto &node = backup_nodes_[i]; + if (node && node->get_key() == id) { + node->receive_ping(std::move(result), adnl, self_id); + if (node->is_ready()) { + promote_node(i); + } + return; + } + } +} + +void DhtBucket::demote_node(size_t idx) { + for (auto &node : backup_nodes_) { + if (node == nullptr) { + node = std::move(active_nodes_[idx]); + return; + } + } + for (auto &node : backup_nodes_) { + if (node->ready_from() == 0 && node->failed_from() + 60 < td::Time::now_cached()) { + node = std::move(active_nodes_[idx]); + return; + } + } + active_nodes_[idx] = nullptr; +} + +void DhtBucket::promote_node(size_t idx) { + CHECK(backup_nodes_[idx]); + for (auto &node : active_nodes_) { + if (node == nullptr) { + node = std::move(backup_nodes_[idx]); + return; + } + CHECK(node->is_ready()); + } +} + +void DhtBucket::check(td::actor::ActorId adnl, td::actor::ActorId dht, + adnl::AdnlNodeIdShort src) { + size_t have_space = 0; + for (size_t i = 0; i < active_nodes_.size(); i++) { + auto &node = active_nodes_[i]; + if (node && td::Time::now_cached() - node->last_ping_at() > ping_timeout_) { + node->send_ping(adnl, dht, src); + if (node->ready_from() == 0) { + demote_node(i); + } + } + if (node == nullptr) { + have_space++; + } + } + for (size_t i = 0; i < backup_nodes_.size(); i++) { + auto &node = backup_nodes_[i]; + if (node && td::Time::now_cached() - node->last_ping_at() > ping_timeout_) { + node->send_ping(adnl, dht, src); + } + if (node && have_space > 0 && node->is_ready()) { + promote_node(i); + have_space--; + } + } +} + +void DhtBucket::dump(td::StringBuilder &sb) const { + sb << " bucket:\n"; + sb << " active:\n"; + for (auto &node : active_nodes_) { + if (node) { + sb << " " << node->get_key() << "\n"; + } + } + sb << " backup:\n"; + for (auto &node : backup_nodes_) { + if (node) { + sb << " " << node->get_key() << "\n"; + } + } +} + +DhtNodesList DhtBucket::export_nodes() const { + DhtNodesList list; + for (auto &node : active_nodes_) { + if (node) { + list.push_back(node->get_node()); + } + } + for (auto &node : backup_nodes_) { + if (node) { + list.push_back(node->get_node()); + } + } + return list; +} + +} // namespace dht + +} // namespace ton diff --git a/dht/dht-bucket.hpp b/dht/dht-bucket.hpp new file mode 100644 index 00000000..86407b3c --- /dev/null +++ b/dht/dht-bucket.hpp @@ -0,0 +1,64 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +#include "dht-remote-node.hpp" +#include "dht.hpp" + +#include + +namespace ton { + +namespace dht { + +class DhtMember; + +class DhtBucket { + private: + double ping_timeout_ = 60; + td::uint32 max_missed_pings_ = 3; + + std::vector> active_nodes_; + std::vector> backup_nodes_; + + //std::map> pending_nodes_; + td::uint32 k_; + bool check_one(td::actor::ActorId adnl, td::actor::ActorId node, adnl::AdnlNodeIdShort src, + const DhtMember::PrintId &print_id); + void demote_node(size_t idx); + void promote_node(size_t idx); + + public: + DhtBucket(td::uint32 k) : k_(k) { + active_nodes_.resize(k); + backup_nodes_.resize(k); + } + td::uint32 active_cnt(); + td::Status add_full_node(DhtKeyId id, DhtNode node, td::actor::ActorId adnl, + adnl::AdnlNodeIdShort self_id); + void check(td::actor::ActorId adnl, td::actor::ActorId node, adnl::AdnlNodeIdShort src); + void receive_ping(DhtKeyId id, DhtNode result, td::actor::ActorId adnl, adnl::AdnlNodeIdShort self_id); + void get_nearest_nodes(DhtKeyId id, td::uint32 bit, DhtNodesList &vec, td::uint32 k); + void dump(td::StringBuilder &sb) const; + DhtNodesList export_nodes() const; +}; + +} // namespace dht + +} // namespace ton diff --git a/dht/dht-in.hpp b/dht/dht-in.hpp new file mode 100644 index 00000000..43962208 --- /dev/null +++ b/dht/dht-in.hpp @@ -0,0 +1,168 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +#include "dht.hpp" +#include "td/db/KeyValueAsync.h" + +#include + +namespace ton { + +namespace dht { + +class DhtRemoteNode; +class DhtBucket; + +class DhtMemberImpl : public DhtMember { + private: + class DhtKeyValueLru : public td::ListNode { + public: + DhtKeyValueLru(DhtValue value) : kv_(std::move(value)) { + } + DhtValue kv_; + static inline DhtKeyValueLru *from_list_node(ListNode *node) { + return static_cast(node); + } + }; + //std::unique_ptr decryptor_; + adnl::AdnlNodeIdShort id_; + DhtKeyId key_; + td::uint32 k_; + td::uint32 a_; + td::uint32 max_cache_time_ = 60; + td::uint32 max_cache_size_ = 100; + + std::vector buckets_; + + std::string db_root_; + + // to be republished once in a while + std::map our_values_; + + std::map cached_values_; + td::ListNode cached_values_lru_; + + std::map values_; + + td::Timestamp fill_att_ = td::Timestamp::in(0); + td::Timestamp republish_att_ = td::Timestamp::in(0); + + DhtKeyId last_republish_key_ = DhtKeyId::zero(); + DhtKeyId last_check_key_ = DhtKeyId::zero(); + + class Callback : public adnl::Adnl::Callback { + public: + void receive_message(adnl::AdnlNodeIdShort src, adnl::AdnlNodeIdShort dst, td::BufferSlice data) override { + CHECK(dst == id_); + td::actor::send_closure(self_, &DhtMemberImpl::receive_message, src, std::move(data)); + } + void receive_query(adnl::AdnlNodeIdShort src, adnl::AdnlNodeIdShort dst, td::BufferSlice data, + td::Promise promise) override { + CHECK(dst == id_); + td::actor::send_closure(self_, &DhtMemberImpl::receive_query, src, std::move(data), std::move(promise)); + } + + Callback(td::actor::ActorId self, adnl::AdnlNodeIdShort id) : self_(self), id_(id) { + } + + private: + td::actor::ActorId self_; + adnl::AdnlNodeIdShort id_; + }; + + void receive_query(adnl::AdnlNodeIdShort src, td::BufferSlice data, td::Promise promise); + + void receive_message(adnl::AdnlNodeIdShort src, td::BufferSlice data); + + td::actor::ActorId keyring_; + td::actor::ActorId adnl_; + + td::uint64 ping_queries_{0}; + td::uint64 find_node_queries_{0}; + td::uint64 find_value_queries_{0}; + td::uint64 store_queries_{0}; + td::uint64 get_addr_list_queries_{0}; + + using DbType = td::KeyValueAsync; + DbType db_; + td::Timestamp next_save_to_db_at_ = td::Timestamp::in(10.0); + + void save_to_db(); + + DhtNodesList get_nearest_nodes(DhtKeyId id, td::uint32 k); + void check(); + + template + void process_query(adnl::AdnlNodeIdShort src, T &query, td::Promise promise) { + promise.set_error(td::Status::Error(ErrorCode::protoviolation, "bad DHT query")); + } + + void process_query(adnl::AdnlNodeIdShort src, ton_api::dht_ping &query, td::Promise promise); + void process_query(adnl::AdnlNodeIdShort src, ton_api::dht_findNode &query, td::Promise promise); + void process_query(adnl::AdnlNodeIdShort src, ton_api::dht_findValue &query, td::Promise promise); + void process_query(adnl::AdnlNodeIdShort src, ton_api::dht_store &query, td::Promise promise); + void process_query(adnl::AdnlNodeIdShort src, ton_api::dht_getSignedAddressList &query, + td::Promise promise); + + public: + DhtMemberImpl(adnl::AdnlNodeIdShort id, std::string db_root, td::actor::ActorId keyring, + td::actor::ActorId adnl, td::uint32 k, td::uint32 a = 3) + : id_(id), key_{id_}, k_(k), a_(a), db_root_(db_root), keyring_(keyring), adnl_(adnl) { + for (size_t i = 0; i < 256; i++) { + buckets_.emplace_back(k_); + } + } + + void add_full_node(DhtKeyId id, DhtNode node) override; + + adnl::AdnlNodeIdShort get_id() const override { + return id_; + } + + void receive_ping(DhtKeyId id, DhtNode result) override; + + void set_value(DhtValue key_value, td::Promise result) override; + td::uint32 distance(DhtKeyId key_id, td::uint32 max_value); + + td::Status store_in(DhtValue value) override; + void send_store(DhtValue value, td::Promise promise); + + void get_value_in(DhtKeyId key, td::Promise result) override; + void get_value(DhtKey key, td::Promise result) override { + get_value_in(key.compute_key_id(), std::move(result)); + } + + void alarm() override { + alarm_timestamp() = td::Timestamp::in(1.0); + check(); + } + void start_up() override; + void tear_down() override; + void dump(td::StringBuilder &sb) const override; + PrintId print_id() const override { + return PrintId{id_}; + } + + void get_self_node(td::Promise promise) override; +}; + +} // namespace dht + +} // namespace ton diff --git a/dht/dht-node.cpp b/dht/dht-node.cpp new file mode 100644 index 00000000..f181ca86 --- /dev/null +++ b/dht/dht-node.cpp @@ -0,0 +1,64 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "dht-node.hpp" +#include "keys/encryptor.h" + +namespace ton { + +namespace dht { + +td::Status DhtNode::update(tl_object_ptr obj) { + if (version_ && obj->version_ <= version_) { + return td::Status::Error(ErrorCode::notready, "too old version"); + } + auto signature = std::move(obj->signature_); + auto B = serialize_tl_object(obj, true); + + TRY_RESULT(pub, adnl::AdnlNodeIdFull::create(obj->id_)); + TRY_RESULT(addr_list, adnl::AdnlAddressList::create(std::move(obj->addr_list_))); + + if (!addr_list.public_only()) { + return td::Status::Error(ErrorCode::notready, "dht node must have only public addresses"); + } + if (!addr_list.size()) { + return td::Status::Error(ErrorCode::notready, "dht node must have >0 addresses"); + } + + TRY_RESULT(E, pub.pubkey().create_encryptor()); + TRY_STATUS(E->check_signature(B.as_slice(), signature.as_slice())); + + id_ = pub; + addr_list_ = addr_list; + version_ = obj->version_; + signature_ = td::SharedSlice(signature.as_slice()); + + return td::Status::OK(); +} + +tl_object_ptr DhtNodesList::tl() const { + std::vector> L; + for (auto &n : list_) { + L.emplace_back(n.tl()); + } + return create_tl_object(std::move(L)); +} + +} // namespace dht + +} // namespace ton diff --git a/dht/dht-node.hpp b/dht/dht-node.hpp new file mode 100644 index 00000000..c34b0d01 --- /dev/null +++ b/dht/dht-node.hpp @@ -0,0 +1,114 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +#include "adnl/adnl-node-id.hpp" +#include "adnl/adnl-address-list.hpp" + +#include "dht-types.h" + +namespace ton { + +namespace dht { + +class DhtNode { + private: + adnl::AdnlNodeIdFull id_; + adnl::AdnlAddressList addr_list_; + td::int32 version_{0}; + td::SharedSlice signature_; + + public: + DhtNode() { + } + DhtNode(adnl::AdnlNodeIdFull id, adnl::AdnlAddressList addr_list, td::int32 version, td::BufferSlice signature) + : id_(id), addr_list_(addr_list), version_(version), signature_(signature.as_slice()) { + } + DhtNode(adnl::AdnlNodeIdFull id, adnl::AdnlAddressList addr_list, td::int32 version, td::SharedSlice signature) + : id_(id), addr_list_(addr_list), version_(version), signature_(std::move(signature)) { + } + static td::Result create(tl_object_ptr obj) { + if (obj->version_ == 0) { + return td::Status::Error(ErrorCode::protoviolation, "zero version"); + } + DhtNode n; + TRY_STATUS(n.update(std::move(obj))); + return std::move(n); + } + td::Status update(tl_object_ptr obj); + DhtKeyId get_key() const { + CHECK(!id_.empty()); + return DhtKeyId{id_.compute_short_id()}; + } + + adnl::AdnlNodeIdFull adnl_id() const { + return id_; + } + adnl::AdnlAddressList addr_list() const { + return addr_list_; + } + td::int32 version() const { + return version_; + } + + tl_object_ptr tl() const { + return create_tl_object(id_.tl(), addr_list_.tl(), version_, signature_.clone_as_buffer_slice()); + } + DhtNode clone() const { + return DhtNode{id_, addr_list_, version_, signature_.clone()}; + } +}; + +class DhtNodesList { + public: + DhtNodesList() { + } + DhtNodesList(tl_object_ptr R) { + for (auto &n : R->nodes_) { + auto N = DhtNode::create(std::move(n)); + if (N.is_ok()) { + list_.emplace_back(N.move_as_ok()); + } else { + LOG(WARNING) << "bad dht node: " << N.move_as_error(); + } + } + } + + void push_back(DhtNode node) { + list_.emplace_back(std::move(node)); + } + + tl_object_ptr tl() const; + std::vector &list() { + return list_; + } + const std::vector &list() const { + return list_; + } + td::uint32 size() const { + return static_cast(list_.size()); + } + + private: + std::vector list_; +}; + +} // namespace dht + +} // namespace ton diff --git a/dht/dht-query.cpp b/dht/dht-query.cpp new file mode 100644 index 00000000..440e6cb1 --- /dev/null +++ b/dht/dht-query.cpp @@ -0,0 +1,273 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "dht.hpp" + +#include "td/utils/tl_storers.h" +#include "td/utils/crypto.h" +#include "td/utils/tl_parsers.h" +#include "td/utils/Random.h" +#include "td/utils/overloaded.h" + +#include "td/utils/format.h" + +#include "keys/encryptor.h" + +#include "auto/tl/ton_api.hpp" + +#include "dht-query.hpp" + +namespace ton { + +namespace dht { + +void DhtQuery::send_queries() { + VLOG(DHT_EXTRA_DEBUG) << this << ": sending new queries. active=" << active_queries_ << " max_active=" << a_; + while (pending_ids_.size() > 0 && active_queries_ < a_) { + active_queries_++; + auto id_xor = *pending_ids_.begin(); + auto id = id_xor ^ key_; + VLOG(DHT_EXTRA_DEBUG) << this << ": sending " << get_name() << " query to " << id; + pending_ids_.erase(id_xor); + + auto it = list_.find(id_xor); + CHECK(it != list_.end()); + td::actor::send_closure(adnl_, &adnl::Adnl::add_peer, get_src(), it->second.adnl_id(), it->second.addr_list()); + send_one_query(id.to_adnl()); + } + if (active_queries_ == 0) { + CHECK(pending_ids_.size() == 0); + DhtNodesList list; + for (auto &node : list_) { + list.push_back(std::move(node.second)); + } + CHECK(list.size() <= k_); + VLOG(DHT_EXTRA_DEBUG) << this << ": finalizing " << get_name() << " query. List size=" << list.size(); + finish(std::move(list)); + stop(); + } +} + +void DhtQuery::add_nodes(DhtNodesList list) { + VLOG(DHT_EXTRA_DEBUG) << this << ": " << get_name() << " query: received " << list.size() << " new dht nodes"; + for (auto &node : list.list()) { + auto id = node.get_key(); + auto id_xor = key_ ^ id; + if (list_.find(id_xor) != list_.end()) { + continue; + } + td::actor::send_closure(node_, &DhtMember::add_full_node, id, node.clone()); + + DhtKeyId last_id_xor; + if (list_.size() > 0) { + last_id_xor = list_.rbegin()->first; + } + + if (list_.size() < k_ || id_xor < last_id_xor) { + list_[id_xor] = std::move(node); + pending_ids_.insert(id_xor); + if (list_.size() > k_) { + CHECK(id_xor != last_id_xor); + VLOG(DHT_EXTRA_DEBUG) << this << ": " << get_name() << " query: replacing " << (last_id_xor ^ key_) + << " key with " << id; + pending_ids_.erase(last_id_xor); + list_.erase(last_id_xor); + } else { + VLOG(DHT_EXTRA_DEBUG) << this << ": " << get_name() << " query: adding " << id << " key"; + } + } + } +} + +void DhtQueryFindNodes::send_one_query(adnl::AdnlNodeIdShort id) { + auto P = create_serialize_tl_object(get_key().tl(), get_k()); + auto B = create_serialize_tl_object_suffix(P.as_slice(), self_.tl()); + + auto Pr = td::PromiseCreator::lambda([SelfId = actor_id(this), dst = id](td::Result R) { + td::actor::send_closure(SelfId, &DhtQueryFindNodes::on_result, std::move(R), dst); + }); + + td::actor::send_closure(adnl_, &adnl::Adnl::send_query, get_src(), id, "dht findNode", std::move(Pr), + td::Timestamp::in(2.0 + td::Random::fast(0, 20) * 0.1), std::move(B)); +} + +void DhtQueryFindNodes::on_result(td::Result R, adnl::AdnlNodeIdShort dst) { + if (R.is_error()) { + VLOG(DHT_INFO) << this << ": failed find nodes query " << get_src() << "->" << dst << ": " << R.move_as_error(); + finish_query(); + return; + } + + auto Res = fetch_tl_object(R.move_as_ok(), true); + if (Res.is_error()) { + VLOG(DHT_WARNING) << this << ": incorrect result on dht.findNodes query from " << dst << ": " + << Res.move_as_error(); + } else { + add_nodes(DhtNodesList{Res.move_as_ok()}); + } + finish_query(); +} + +void DhtQueryFindNodes::finish(DhtNodesList list) { + promise_.set_result(std::move(list)); +} + +void DhtQueryFindValue::send_one_query(adnl::AdnlNodeIdShort id) { + auto P = create_serialize_tl_object(get_key().tl(), get_k()); + auto B = create_serialize_tl_object_suffix(P.as_slice(), self_.tl()); + + auto Pr = td::PromiseCreator::lambda([SelfId = actor_id(this), dst = id](td::Result R) { + td::actor::send_closure(SelfId, &DhtQueryFindValue::on_result, std::move(R), dst); + }); + + td::actor::send_closure(adnl_, &adnl::Adnl::send_query, get_src(), id, "dht findValue", std::move(Pr), + td::Timestamp::in(2.0 + td::Random::fast(0, 20) * 0.1), std::move(B)); +} + +void DhtQueryFindValue::on_result(td::Result R, adnl::AdnlNodeIdShort dst) { + if (R.is_error()) { + VLOG(DHT_INFO) << this << ": failed find value query " << get_src() << "->" << dst << ": " << R.move_as_error(); + finish_query(); + return; + } + auto Res = fetch_tl_object(R.move_as_ok(), true); + if (Res.is_error()) { + VLOG(DHT_WARNING) << this << ": dropping incorrect answer on dht.findValue query from " << dst << ": " + << Res.move_as_error(); + finish_query(); + return; + } + + bool need_stop = false; + + auto A = Res.move_as_ok(); + ton_api::downcast_call( + *A.get(), td::overloaded( + [&](ton_api::dht_valueFound &v) { + auto valueR = DhtValue::create(std::move(v.value_), true); + if (valueR.is_error()) { + VLOG(DHT_WARNING) << this << ": received incorrect dht answer on find value query from " << dst + << ": " << valueR.move_as_error(); + return; + } + auto value = valueR.move_as_ok(); + if (value.key_id() != key_) { + VLOG(DHT_WARNING) << this << ": received value for bad key on find value query from " << dst; + return; + } + promise_.set_value(std::move(value)); + need_stop = true; + }, + [&](ton_api::dht_valueNotFound &v) { add_nodes(DhtNodesList{std::move(v.nodes_)}); })); + if (need_stop) { + stop(); + } else { + finish_query(); + } +} + +void DhtQueryFindValue::finish(DhtNodesList list) { + promise_.set_error(td::Status::Error(ErrorCode::notready, "dht key not found")); +} + +DhtQueryStore::DhtQueryStore(DhtValue key_value, DhtMember::PrintId print_id, adnl::AdnlNodeIdShort src, + DhtNodesList list, td::uint32 k, td::uint32 a, DhtNode self, + td::actor::ActorId node, td::actor::ActorId adnl, + td::Promise promise) + : print_id_(print_id) + , k_(k) + , a_(a) + , promise_(std::move(promise)) + , value_(std::move(key_value)) + , list_(std::move(list)) + , self_(std::move(self)) { + node_ = node; + adnl_ = adnl; + src_ = src; +} + +void DhtQueryStore::start_up() { + auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result res) { + td::actor::send_closure(SelfId, &DhtQueryStore::send_stores, std::move(res)); + }); + + auto key = value_.key_id(); + auto A = td::actor::create_actor("FindNodesQuery", key, print_id_, src_, std::move(list_), k_, a_, + self_.clone(), node_, adnl_, std::move(P)); + A.release(); +} + +void DhtQueryStore::send_stores(td::Result R) { + if (R.is_error()) { + auto S = R.move_as_error(); + VLOG(DHT_NOTICE) << this << ": failed to get nearest nodes to " << value_.key_id() << ": " << S; + promise_.set_error(std::move(S)); + stop(); + return; + } + auto list = R.move_as_ok(); + if (list.size() < k_) { + td::actor::send_closure(node_, &DhtMember::store_in, value_.clone()); + } else { + auto last_key = list.list().rbegin()->get_key(); + auto value_key = value_.key_id(); + if ((value_key ^ src_) < (value_key ^ last_key)) { + td::actor::send_closure(node_, &DhtMember::store_in, value_.clone()); + } + } + + remaining_ = static_cast(list.size()); + + for (auto &node : list.list()) { + auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result R) { + td::actor::send_closure(SelfId, &DhtQueryStore::store_ready, std::move(R)); + }); + auto M = create_serialize_tl_object(value_.tl()); + td::actor::send_closure(adnl_, &adnl::Adnl::send_query, src_, node.adnl_id().compute_short_id(), "dht store", + std::move(P), td::Timestamp::in(2.0 + td::Random::fast(0, 20) * 0.1), std::move(M)); + } +} + +void DhtQueryStore::store_ready(td::Result R) { + if (R.is_error()) { + fail_++; + VLOG(DHT_INFO) << this << ": failed store query: " << R.move_as_error(); + } else { + auto R2 = fetch_tl_object(R.move_as_ok(), true); + if (R2.is_error()) { + fail_++; + VLOG(DHT_WARNING) << this << ": can not parse answer (expected dht.stored): " << R2.move_as_error(); + } else { + success_++; + } + } + CHECK(remaining_ > 0); + remaining_--; + if (remaining_ == 0) { + if (success_ > 0) { + promise_.set_value(td::Unit()); + } else { + promise_.set_result(td::Status::Error("failed to make actual store query")); + } + stop(); + } +} + +} // namespace dht + +} // namespace ton diff --git a/dht/dht-query.hpp b/dht/dht-query.hpp new file mode 100644 index 00000000..a5624373 --- /dev/null +++ b/dht/dht-query.hpp @@ -0,0 +1,177 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +#include +#include + +#include "td/utils/int_types.h" +#include "td/actor/actor.h" + +#include "adnl/adnl.h" +#include "td/actor/PromiseFuture.h" + +#include "auto/tl/ton_api.hpp" + +#include "dht.hpp" + +namespace ton { + +namespace dht { + +class DhtMember; + +class DhtQuery : public td::actor::Actor { + protected: + DhtKeyId key_; + DhtNode self_; + + public: + DhtQuery(DhtKeyId key, DhtMember::PrintId print_id, adnl::AdnlNodeIdShort src, DhtNodesList list, td::uint32 k, + td::uint32 a, DhtNode self, td::actor::ActorId node, td::actor::ActorId adnl) + : key_(key), self_(std::move(self)), print_id_(print_id), src_(src), k_(k), a_(a), node_(node), adnl_(adnl) { + add_nodes(std::move(list)); + } + DhtMember::PrintId print_id() const { + return print_id_; + } + void send_queries(); + void add_nodes(DhtNodesList list); + void finish_query() { + active_queries_--; + CHECK(active_queries_ <= k_); + send_queries(); + } + DhtKeyId get_key() const { + return key_; + } + adnl::AdnlNodeIdShort get_src() const { + return src_; + } + td::uint32 get_k() const { + return k_; + } + void start_up() override { + send_queries(); + } + virtual void send_one_query(adnl::AdnlNodeIdShort id) = 0; + virtual void finish(DhtNodesList list) = 0; + virtual std::string get_name() const = 0; + + private: + DhtMember::PrintId print_id_; + adnl::AdnlNodeIdShort src_; + std::map list_; + std::set pending_ids_; + td::uint32 k_; + td::uint32 a_; + td::actor::ActorId node_; + td::uint32 active_queries_ = 0; + + protected: + td::actor::ActorId adnl_; +}; + +class DhtQueryFindNodes : public DhtQuery { + private: + td::Promise promise_; + + public: + DhtQueryFindNodes(DhtKeyId key, DhtMember::PrintId print_id, adnl::AdnlNodeIdShort src, DhtNodesList list, + td::uint32 k, td::uint32 a, DhtNode self, td::actor::ActorId node, + td::actor::ActorId adnl, td::Promise promise) + : DhtQuery(key, print_id, src, std::move(list), k, a, std::move(self), node, adnl), promise_(std::move(promise)) { + } + void send_one_query(adnl::AdnlNodeIdShort id) override; + void on_result(td::Result R, adnl::AdnlNodeIdShort dst); + void finish(DhtNodesList list) override; + std::string get_name() const override { + return "find nodes"; + } +}; + +class DhtQueryFindValue : public DhtQuery { + private: + td::Promise promise_; + + public: + DhtQueryFindValue(DhtKeyId key, DhtMember::PrintId print_id, adnl::AdnlNodeIdShort src, DhtNodesList list, + td::uint32 k, td::uint32 a, DhtNode self, td::actor::ActorId node, + td::actor::ActorId adnl, td::Promise promise) + : DhtQuery(key, print_id, src, std::move(list), k, a, std::move(self), node, adnl), promise_(std::move(promise)) { + } + void send_one_query(adnl::AdnlNodeIdShort id) override; + void on_result(td::Result R, adnl::AdnlNodeIdShort dst); + void finish(DhtNodesList list) override; + std::string get_name() const override { + return "find value"; + } +}; + +class DhtQueryStore : public td::actor::Actor { + private: + DhtMember::PrintId print_id_; + td::uint32 k_; + td::uint32 a_; + td::Promise promise_; + td::actor::ActorId node_; + td::actor::ActorId adnl_; + adnl::AdnlNodeIdShort src_; + DhtValue value_; + td::uint32 success_ = 0; + td::uint32 fail_ = 0; + td::uint32 remaining_; + DhtNodesList list_; + DhtNode self_; + + public: + DhtQueryStore(DhtValue key_value, DhtMember::PrintId print_id, adnl::AdnlNodeIdShort src, DhtNodesList list, + td::uint32 k, td::uint32 a, DhtNode self, td::actor::ActorId node, + td::actor::ActorId adnl, td::Promise promise); + void send_stores(td::Result res); + void store_ready(td::Result res); + void start_up() override; + DhtMember::PrintId print_id() const { + return print_id_; + } +}; + +inline td::StringBuilder &operator<<(td::StringBuilder &sb, const DhtQuery &dht) { + sb << dht.print_id(); + return sb; +} + +inline td::StringBuilder &operator<<(td::StringBuilder &sb, const DhtQuery *dht) { + sb << dht->print_id(); + return sb; +} + +inline td::StringBuilder &operator<<(td::StringBuilder &sb, const DhtQueryStore &dht) { + sb << dht.print_id(); + return sb; +} + +inline td::StringBuilder &operator<<(td::StringBuilder &sb, const DhtQueryStore *dht) { + sb << dht->print_id(); + return sb; +} + +} // namespace dht + +} // namespace ton diff --git a/dht/dht-remote-node.cpp b/dht/dht-remote-node.cpp new file mode 100644 index 00000000..a6328bd0 --- /dev/null +++ b/dht/dht-remote-node.cpp @@ -0,0 +1,134 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "dht.hpp" + +#include "td/utils/tl_storers.h" +#include "td/utils/crypto.h" +#include "td/utils/tl_parsers.h" +#include "td/utils/Random.h" + +#include "td/utils/format.h" + +#include "keys/encryptor.h" + +#include "auto/tl/ton_api.hpp" + +#include "dht-remote-node.hpp" + +namespace ton { + +namespace dht { + +td::Status DhtRemoteNode::receive_ping(DhtNode node, td::actor::ActorId adnl, + adnl::AdnlNodeIdShort self_id) { + TRY_STATUS(update_value(std::move(node), adnl, self_id)); + missed_pings_ = 0; + if (ready_from_ == 0) { + ready_from_ = td::Time::now_cached(); + } + return td::Status::OK(); +} + +td::Status DhtRemoteNode::update_value(DhtNode node, td::actor::ActorId adnl, + adnl::AdnlNodeIdShort self_id) { + CHECK(node.adnl_id() == node_.adnl_id()); + if (node.version() <= node_.version()) { + return td::Status::OK(); + } + + TRY_RESULT(enc, node.adnl_id().pubkey().create_encryptor()); + auto tl = node.tl(); + auto sig = std::move(tl->signature_); + TRY_STATUS_PREFIX(enc->check_signature(serialize_tl_object(tl, true).as_slice(), sig.as_slice()), + "bad node signature: "); + + node_ = std::move(node); + td::actor::send_closure(adnl, &adnl::Adnl::add_peer, self_id, node_.adnl_id(), node_.addr_list()); + return td::Status::OK(); +} + +void DhtRemoteNode::send_ping(td::actor::ActorId adnl, td::actor::ActorId node, + adnl::AdnlNodeIdShort src) { + missed_pings_++; + if (missed_pings_ > max_missed_pings_ && ready_from_ > 0) { + ready_from_ = 0; + failed_from_ = td::Time::now_cached(); + } + + last_ping_at_ = td::Time::now_cached(); + + td::actor::send_closure(adnl, &adnl::Adnl::add_peer, src, node_.adnl_id(), node_.addr_list()); + + auto P = td::PromiseCreator::lambda( + [key = id_, id = node_.adnl_id().compute_short_id(), node, src, adnl](td::Result R) mutable { + if (R.is_error()) { + LOG(ERROR) << "[dht]: failed to get self node"; + return; + } + auto P = td::PromiseCreator::lambda([key, node, adnl](td::Result R) { + if (R.is_error()) { + VLOG(DHT_INFO) << "[dht]: received error for query to " << key << ": " << R.move_as_error(); + return; + } + auto F = fetch_tl_object(R.move_as_ok(), true); + + if (F.is_ok()) { + auto N = DhtNode::create(F.move_as_ok()); + if (N.is_ok()) { + td::actor::send_closure(node, &DhtMember::receive_ping, key, N.move_as_ok()); + } else { + VLOG(DHT_WARNING) << "[dht]: bad answer from " << key + << ": dropping bad getSignedAddressList() query answer: " << N.move_as_error(); + } + } else { + VLOG(DHT_WARNING) << "[dht]: bad answer from " << key + << ": dropping invalid getSignedAddressList() query answer: " << F.move_as_error(); + } + }); + auto Q = create_serialize_tl_object(); + auto B = create_serialize_tl_object_suffix(Q.as_slice(), R.move_as_ok().tl()); + td::actor::send_closure(adnl, &adnl::Adnl::send_query, src, id, "dht ping", std::move(P), + td::Timestamp::in(10.0 + td::Random::fast(0, 100) * 0.1), std::move(B)); + }); + + td::actor::send_closure(node, &DhtMember::get_self_node, std::move(P)); +} + +adnl::AdnlAddressList DhtRemoteNode::get_addr_list() const { + return node_.addr_list(); +} + +adnl::AdnlNodeIdFull DhtRemoteNode::get_full_id() const { + return node_.adnl_id(); +} + +td::Result> DhtRemoteNode::create(DhtNode node, td::uint32 max_missed_pings) { + TRY_RESULT(enc, node.adnl_id().pubkey().create_encryptor()); + auto tl = node.tl(); + auto sig = std::move(tl->signature_); + + TRY_STATUS_PREFIX(enc->check_signature(serialize_tl_object(tl, true).as_slice(), sig.as_slice()), + "bad node signature: "); + + return std::make_unique(std::move(node), max_missed_pings); +} + +} // namespace dht + +} // namespace ton diff --git a/dht/dht-remote-node.hpp b/dht/dht-remote-node.hpp new file mode 100644 index 00000000..6af46b8c --- /dev/null +++ b/dht/dht-remote-node.hpp @@ -0,0 +1,86 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +#include "td/utils/int_types.h" +#include "td/actor/actor.h" + +#include "adnl/adnl.h" +#include "adnl/utils.hpp" + +#include "dht.hpp" + +#include "auto/tl/ton_api.hpp" + +namespace ton { + +namespace dht { + +class DhtMember; + +class DhtRemoteNode { + private: + DhtKeyId id_; + DhtNode node_; + + td::uint32 max_missed_pings_; + td::uint32 missed_pings_ = 0; + double last_ping_at_ = 0; + double ready_from_ = 0; + double failed_from_ = 0; + td::int32 version_; + + public: + DhtRemoteNode(DhtNode node, td::uint32 max_missed_pings) + : node_(std::move(node)), max_missed_pings_(max_missed_pings) { + failed_from_ = td::Time::now_cached(); + id_ = node.get_key(); + } + static td::Result> create(DhtNode node, td::uint32 max_missed_pings); + DhtNode get_node() const { + return node_.clone(); + } + double failed_from() const { + return failed_from_; + } + adnl::AdnlAddressList get_addr_list() const; + adnl::AdnlNodeIdFull get_full_id() const; + DhtKeyId get_key() const { + return id_; + } + td::uint32 missed_pings() const { + return missed_pings_; + } + bool is_ready() const { + return ready_from_ > 0; + } + double ready_from() const { + return ready_from_; + } + double last_ping_at() const { + return last_ping_at_; + } + void send_ping(td::actor::ActorId adnl, td::actor::ActorId node, adnl::AdnlNodeIdShort src); + td::Status receive_ping(DhtNode node, td::actor::ActorId adnl, adnl::AdnlNodeIdShort self_id); + td::Status update_value(DhtNode node, td::actor::ActorId adnl, adnl::AdnlNodeIdShort self_id); +}; + +} // namespace dht + +} // namespace ton diff --git a/dht/dht-types.cpp b/dht/dht-types.cpp new file mode 100644 index 00000000..052f9305 --- /dev/null +++ b/dht/dht-types.cpp @@ -0,0 +1,383 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "dht-types.h" +#include "td/utils/Random.h" +#include "td/utils/overloaded.h" +#include "keys/encryptor.h" + +#include "auto/tl/ton_api.hpp" + +#include + +namespace ton { + +namespace dht { + +td::Result DhtKey::create(tl_object_ptr key) { + if (key->name_.length() > max_name_length()) { + return td::Status::Error(ErrorCode::error, PSTRING() << "too big name length. length=" << key->name_.length()); + } + if (!key->name_.length()) { + return td::Status::Error(ErrorCode::error, PSTRING() << "empty dht key name"); + } + if (key->idx_ < 0 || static_cast(key->idx_) > max_index()) { + return td::Status::Error(ErrorCode::error, PSTRING() << "bad dht key index " << key->idx_); + } + + return DhtKey{PublicKeyHash{key->id_}, key->name_.as_slice().str(), static_cast(key->idx_)}; +} + +tl_object_ptr DhtKey::tl() const { + return create_tl_object(id_.tl(), td::BufferSlice{namestr_}, idx_); +} + +td::Status DhtKey::check() const { + if (namestr_.length() > max_name_length()) { + return td::Status::Error(ErrorCode::error, PSTRING() << "too big name length. length=" << namestr_.length()); + } + if (namestr_.length() == 0) { + return td::Status::Error(ErrorCode::error, PSTRING() << "empty dht key name"); + } + if (static_cast(idx_) > max_index()) { + return td::Status::Error(ErrorCode::error, PSTRING() << "bad dht key index " << idx_); + } + return td::Status::OK(); +} + +DhtKeyId DhtKey::compute_key_id() const { + return DhtKeyId{get_tl_object_sha_bits256(tl())}; +} + +DhtKey DhtKey::clone() const { + return DhtKey{id_, namestr_, idx_}; +} + +void DhtKeyDescription::update_signature(td::BufferSlice signature) { + signature_ = td::SharedSlice{signature.as_slice()}; +} + +void DhtKeyDescription::update_signature(td::SharedSlice signature) { + signature_ = std::move(signature); +} + +td::Status DhtKeyDescription::check() const { + TRY_STATUS(key_.check()); + if (public_key_.compute_short_id() != key_.public_key_hash()) { + return td::Status::Error(ErrorCode::protoviolation, "key hash mismatch"); + } + auto obj = tl(); + obj->signature_ = td::BufferSlice{}; + + auto B = serialize_tl_object(obj, true); + TRY_RESULT(E, public_key_.create_encryptor()); + TRY_STATUS(E->check_signature(B.as_slice(), signature_.as_slice())); + return td::Status::OK(); +} + +tl_object_ptr DhtKeyDescription::tl() const { + return create_tl_object(key_.tl(), public_key_.tl(), update_rule_->tl(), + signature_.clone_as_buffer_slice()); +} + +td::BufferSlice DhtKeyDescription::to_sign() const { + return create_serialize_tl_object(key_.tl(), public_key_.tl(), update_rule_->tl(), + td::BufferSlice()); +} + +DhtKeyDescription DhtKeyDescription::clone() const { + return DhtKeyDescription{key_.clone(), public_key_, update_rule_, signature_.clone_as_buffer_slice()}; +} + +td::Result DhtKeyDescription::create(DhtKey key, PublicKey public_key, + std::shared_ptr update_rule, + td::BufferSlice signature) { + DhtKeyDescription desc{std::move(key), std::move(public_key), std::move(update_rule), std::move(signature)}; + TRY_STATUS(desc.check()); + return std::move(desc); +} + +td::Result DhtKeyDescription::create(DhtKey key, PublicKey public_key, + std::shared_ptr update_rule, + td::SharedSlice signature) { + DhtKeyDescription desc{std::move(key), std::move(public_key), std::move(update_rule), std::move(signature)}; + TRY_STATUS(desc.check()); + return std::move(desc); +} + +td::Result DhtKeyDescription::create(tl_object_ptr desc, + bool check_signature) { + auto signature = std::move(desc->signature_); + td::BufferSlice to_sign; + if (check_signature) { + to_sign = serialize_tl_object(desc, true); + } + auto public_key = PublicKey{desc->id_}; + + TRY_RESULT(key, DhtKey::create(std::move(desc->key_))); + if (key.public_key_hash() != public_key.compute_short_id()) { + return td::Status::Error(ErrorCode::error, "inconsistent dht key description"); + } + TRY_RESULT(update_rule, DhtUpdateRule::create(std::move(desc->update_rule_))); + + if (check_signature) { + TRY_RESULT(E, public_key.create_encryptor()); + TRY_STATUS(E->check_signature(to_sign.as_slice(), signature.as_slice())); + } + + return DhtKeyDescription{std::move(key), std::move(public_key), std::move(update_rule), std::move(signature)}; +} + +td::Result DhtValue::create(tl_object_ptr obj, bool check_signature) { + TRY_RESULT(desc, DhtKeyDescription::create(std::move(obj->key_), check_signature)); + + return create(std::move(desc), std::move(obj->value_), obj->ttl_, std::move(obj->signature_)); +} + +td::Result DhtValue::create(DhtKeyDescription key, td::BufferSlice value, td::uint32 ttl, + td::BufferSlice signature) { + TRY_STATUS(key.check()); + DhtValue v{std::move(key), std::move(value), ttl, std::move(signature)}; + TRY_STATUS(v.key().update_rule()->check_value(v)); + return std::move(v); +} + +td::Result DhtValue::create(DhtKeyDescription key, td::SharedSlice value, td::uint32 ttl, + td::SharedSlice signature) { + TRY_STATUS(key.check()); + DhtValue v{std::move(key), std::move(value), ttl, std::move(signature)}; + TRY_STATUS(v.key().update_rule()->check_value(v)); + return std::move(v); +} + +DhtValue DhtValue::clone() const { + return DhtValue{key_.clone(), value_.clone(), ttl_, signature_.clone()}; +} + +tl_object_ptr DhtValue::tl() const { + return create_tl_object(key_.tl(), value_.clone_as_buffer_slice(), ttl_, + signature_.clone_as_buffer_slice()); +} + +td::BufferSlice DhtValue::to_sign() const { + return create_serialize_tl_object(key_.tl(), value_.clone_as_buffer_slice(), ttl_, + td::BufferSlice()); +} + +td::Status DhtValue::update(DhtValue &&value) { + TRY_STATUS(value.check()); + return key_.update_rule()->update_value(*this, std::move(value)); +} + +void DhtValue::set(td::BufferSlice value, td::uint32 ttl, td::BufferSlice signature) { + value_ = td::SharedSlice{value.as_slice()}; + ttl_ = ttl; + signature_ = td::SharedSlice{signature.as_slice()}; +} + +void DhtValue::set(td::SharedSlice value, td::uint32 ttl, td::SharedSlice signature) { + value_ = std::move(value); + ttl_ = ttl; + signature_ = std::move(signature); +} + +void DhtValue::update_signature(td::BufferSlice signature) { + signature_ = td::SharedSlice{signature.as_slice()}; +} + +void DhtValue::update_signature(td::SharedSlice signature) { + signature_ = std::move(signature); +} + +td::Status DhtValue::check() const { + TRY_STATUS(key_.check()); + return key_.update_rule()->check_value(*this); +} + +DhtKeyId DhtValue::key_id() const { + return key_.key().compute_key_id(); +} + +td::Status DhtUpdateRuleSignature::check_value(const DhtValue &value) { + if (value.value().size() > DhtValue::max_value_size()) { + return td::Status::Error(ErrorCode::protoviolation, "too big value"); + } + TRY_RESULT(E, value.key().public_key().create_encryptor()); + auto tl = value.tl(); + auto sig = std::move(tl->signature_); + auto B = serialize_tl_object(tl, true); + return E->check_signature(B.as_slice(), sig.as_slice()); +} + +td::Status DhtUpdateRuleSignature::update_value(DhtValue &value, DhtValue &&new_value) { + TRY_STATUS(new_value.check()); + CHECK(value.key_id() == new_value.key_id()); + if (new_value.ttl() > value.ttl()) { + value.set(new_value.value().clone(), new_value.ttl(), new_value.signature().clone()); + value.check().ensure(); + } + return td::Status::OK(); +} + +tl_object_ptr DhtUpdateRuleSignature::tl() const { + return create_tl_object(); +} + +td::Result> DhtUpdateRuleSignature::create() { + return std::make_shared(); +} + +td::Status DhtUpdateRuleAnybody::check_value(const DhtValue &value) { + if (value.value().size() > DhtValue::max_value_size()) { + return td::Status::Error(ErrorCode::protoviolation, "too big value"); + } + if (value.signature().size() > 0) { + return td::Status::Error(ErrorCode::protoviolation, "cannot have signature in DhtUpdateRuleAnybody"); + } + return td::Status::OK(); +} + +td::Status DhtUpdateRuleAnybody::update_value(DhtValue &value, DhtValue &&new_value) { + CHECK(value.key_id() == new_value.key_id()); + value.set(new_value.value().clone(), new_value.ttl(), new_value.signature().clone()); + return td::Status::OK(); +} + +tl_object_ptr DhtUpdateRuleAnybody::tl() const { + return create_tl_object(); +} + +td::Result> DhtUpdateRuleAnybody::create() { + return std::make_shared(); +} + +td::Status DhtUpdateRuleOverlayNodes::check_value(const DhtValue &value) { + if (value.value().size() > DhtValue::max_value_size()) { + return td::Status::Error(ErrorCode::protoviolation, "too big value"); + } + if (value.signature().size() > 0) { + return td::Status::Error(ErrorCode::protoviolation, "cannot have signature in DhtUpdateRuleOverlayNodes"); + } + auto F = fetch_tl_object(value.value().clone_as_buffer_slice(), true); + if (F.is_error()) { + return td::Status::Error(ErrorCode::protoviolation, "bad overlay nodes value"); + } + auto L = F.move_as_ok(); + for (auto &node : L->nodes_) { + TRY_RESULT(pub, adnl::AdnlNodeIdFull::create(node->id_)); + auto sig = std::move(node->signature_); + auto obj = + create_tl_object(pub.compute_short_id().tl(), node->overlay_, node->version_); + if (node->overlay_ != value.key().key().public_key_hash().bits256_value()) { + return td::Status::Error(ErrorCode::protoviolation, "bad overlay id"); + } + auto B = serialize_tl_object(obj, true); + TRY_RESULT(E, pub.pubkey().create_encryptor()); + TRY_STATUS(E->check_signature(B.as_slice(), sig.as_slice())); + } + return td::Status::OK(); +} + +td::Status DhtUpdateRuleOverlayNodes::update_value(DhtValue &value, DhtValue &&new_value) { + TRY_RESULT_PREFIX(N, fetch_tl_object(value.value().clone_as_buffer_slice(), true), + "bad dht value in updateRule.overlayNodes: "); + TRY_RESULT_PREFIX(L, fetch_tl_object(new_value.value().clone_as_buffer_slice(), true), + "bad dht value in updateRule.overlayNodes: "); + + std::vector> res; + + std::map S; + for (auto &n : N->nodes_) { + TRY_RESULT(pub, adnl::AdnlNodeIdFull::create(n->id_)); + auto id = pub.compute_short_id(); + auto it = S.find(id); + if (it != S.end()) { + auto &m = res[it->second]; + if (m->version_ < n->version_) { + m = std::move(n); + } + } else { + S.emplace(id, res.size()); + res.emplace_back(std::move(n)); + } + } + for (auto &n : L->nodes_) { + TRY_RESULT(pub, adnl::AdnlNodeIdFull::create(n->id_)); + auto id = pub.compute_short_id(); + auto it = S.find(id); + if (it != S.end()) { + auto &m = res[it->second]; + if (m->version_ < n->version_) { + m = std::move(n); + } + } else { + S.emplace(id, res.size()); + res.emplace_back(std::move(n)); + } + } + + size_t size = 8; // magic + size + std::vector> v; + for (td::uint32 i = 0; i < res.size(); i++) { + v.emplace_back(i, serialize_tl_object(res[i], false).size()); + size += v[i].second; + } + + while (size > DhtValue::max_value_size()) { + CHECK(v.size() > 0); + auto idx = td::Random::fast(0, static_cast(v.size() - 1)); + size -= v[idx].second; + v[idx] = v[v.size() - 1]; + v.resize(v.size() - 1); + } + + std::vector> vec; + for (auto &p : v) { + vec.push_back(std::move(res[p.first])); + } + auto nodes = create_serialize_tl_object(std::move(vec)); + CHECK(nodes.size() == size); + CHECK(nodes.size() <= DhtValue::max_value_size()); + + value.set(std::move(nodes), std::max(value.ttl(), new_value.ttl()), td::BufferSlice{}); + value.check().ensure(); + + return td::Status::OK(); +} + +tl_object_ptr DhtUpdateRuleOverlayNodes::tl() const { + return create_tl_object(); +} + +td::Result> DhtUpdateRuleOverlayNodes::create() { + return std::make_shared(); +} + +td::Result> DhtUpdateRule::create(tl_object_ptr obj) { + td::Result> R; + ton_api::downcast_call( + *obj.get(), + td::overloaded([&](ton_api::dht_updateRule_signature &obj) { R = DhtUpdateRuleSignature::create(); }, + [&](ton_api::dht_updateRule_anybody &obj) { R = DhtUpdateRuleAnybody::create(); }, + [&](ton_api::dht_updateRule_overlayNodes &obj) { R = DhtUpdateRuleOverlayNodes::create(); })); + return R; +} + +} // namespace dht + +} // namespace ton diff --git a/dht/dht-types.h b/dht/dht-types.h new file mode 100644 index 00000000..bd09785c --- /dev/null +++ b/dht/dht-types.h @@ -0,0 +1,268 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +#include "td/utils/int_types.h" +#include "td/utils/SharedSlice.h" +#include "adnl/adnl-node-id.hpp" +#include "tl-utils/tl-utils.hpp" +#include "common/io.hpp" + +namespace ton { + +namespace dht { + +using DhtKeyName = std::string; + +class DhtKeyId { + public: + explicit DhtKeyId(td::Bits256 value) : value_(value) { + } + explicit DhtKeyId(adnl::AdnlNodeIdShort value) : value_(value.bits256_value()) { + } + DhtKeyId() { + } + td::Bits256 tl() const { + return value_; + } + + bool operator<(const DhtKeyId &with) const { + return value_ < with.value_; + } + bool operator==(const DhtKeyId &with) const { + return value_ == with.value_; + } + bool operator!=(const DhtKeyId &with) const { + return value_ != with.value_; + } + DhtKeyId operator^(const DhtKeyId &with) const { + return DhtKeyId{value_ ^ with.value_}; + } + DhtKeyId operator^(const adnl::AdnlNodeIdShort &with) const { + return DhtKeyId{value_ ^ with.bits256_value()}; + } + + bool get_bit(td::uint32 bit) const { + return value_[bit]; + } + + td::uint32 count_leading_zeroes() const { + return value_.count_leading_zeroes(); + } + adnl::AdnlNodeIdShort to_adnl() const { + return adnl::AdnlNodeIdShort{value_}; + } + + static DhtKeyId zero() { + return DhtKeyId{td::Bits256::zero()}; + } + + private: + td::Bits256 value_; +}; + +using DhtXoredKeyId = DhtKeyId; + +class DhtKey { + public: + static constexpr td::uint32 max_name_length() { + return 127; + } + static constexpr td::uint32 max_index() { + return 15; + } + DhtKey(PublicKeyHash id, DhtKeyName namestr, td::uint32 idx) + : id_(std::move(id)), namestr_(std::move(namestr)), idx_(idx) { + CHECK(namestr.size() <= max_name_length()); + } + static td::Result create(tl_object_ptr key); + td::Status check() const; + const auto &public_key_hash() const { + return id_; + } + const auto &name() const { + return namestr_; + } + td::uint32 idx() const { + return idx_; + } + tl_object_ptr tl() const; + DhtKeyId compute_key_id() const; + DhtKey clone() const; + + private: + PublicKeyHash id_; + DhtKeyName namestr_; + td::uint32 idx_; +}; + +class DhtValue; + +class DhtUpdateRule { + public: + virtual ~DhtUpdateRule() = default; + virtual td::Status check_value(const DhtValue &value) = 0; + virtual td::Status update_value(DhtValue &value, DhtValue &&new_value) = 0; + virtual bool need_republish() const = 0; + virtual tl_object_ptr tl() const = 0; + static td::Result> create(tl_object_ptr obj); +}; + +class DhtKeyDescription { + public: + DhtKeyDescription(DhtKey key, PublicKey public_key, std::shared_ptr update_rule, + td::BufferSlice signature) + : key_(std::move(key)) + , public_key_(std::move(public_key)) + , update_rule_(std::move(update_rule)) + , signature_(signature.as_slice()) { + } + DhtKeyDescription(DhtKey key, PublicKey public_key, std::shared_ptr update_rule, + td::SharedSlice signature) + : key_(std::move(key)) + , public_key_(std::move(public_key)) + , update_rule_(std::move(update_rule)) + , signature_(std::move(signature)) { + } + const auto &key() const { + return key_; + } + const auto &public_key() const { + return public_key_; + } + const auto &update_rule() const { + return update_rule_; + } + void update_signature(td::BufferSlice signature); + void update_signature(td::SharedSlice signature); + td::BufferSlice to_sign() const; + td::Status check() const; + tl_object_ptr tl() const; + DhtKeyDescription clone() const; + static td::Result create(tl_object_ptr desc, bool check_signature); + static td::Result create(DhtKey key, PublicKey public_key, + std::shared_ptr update_rule, td::BufferSlice signature); + static td::Result create(DhtKey key, PublicKey public_key, + std::shared_ptr update_rule, td::SharedSlice signature); + + private: + DhtKey key_; + PublicKey public_key_; + std::shared_ptr update_rule_; + td::SharedSlice signature_; +}; + +class DhtValue { + public: + static constexpr td::uint32 max_value_size() { + return 768; + } + + DhtValue(DhtKeyDescription key, td::BufferSlice value, td::uint32 ttl, td::BufferSlice signature) + : key_(std::move(key)), value_(value.as_slice()), ttl_(ttl), signature_(signature.as_slice()) { + } + DhtValue(DhtKeyDescription key, td::SharedSlice value, td::uint32 ttl, td::SharedSlice signature) + : key_(std::move(key)), value_(std::move(value)), ttl_(ttl), signature_(std::move(signature)) { + } + + static td::Result create(tl_object_ptr obj, bool check_signature); + static td::Result create(DhtKeyDescription key, td::BufferSlice value, td::uint32 ttl, + td::BufferSlice signature); + static td::Result create(DhtKeyDescription key, td::SharedSlice value, td::uint32 ttl, + td::SharedSlice signature); + const auto &key() const { + return key_; + } + const auto &value() const { + return value_; + } + const auto &signature() const { + return signature_; + } + td::uint32 ttl() const { + return ttl_; + } + bool expired() const { + return ttl_ < td::Clocks::system(); + } + DhtValue clone() const; + + tl_object_ptr tl() const; + td::BufferSlice to_sign() const; + td::Status update(DhtValue &&value); + void set(td::BufferSlice value, td::uint32 ttl, td::BufferSlice signature); + void set(td::SharedSlice value, td::uint32 ttl, td::SharedSlice signature); + void update_signature(td::BufferSlice signature); + void update_signature(td::SharedSlice signature); + td::Status check() const; + + DhtKeyId key_id() const; + + private: + DhtKeyDescription key_; + td::SharedSlice value_; + td::uint32 ttl_; + td::SharedSlice signature_; +}; + +class DhtUpdateRuleSignature : public DhtUpdateRule { + public: + td::Status check_value(const DhtValue &value) override; + td::Status update_value(DhtValue &value, DhtValue &&new_value) override; + bool need_republish() const override { + return true; + } + tl_object_ptr tl() const override; + static td::Result> create(); +}; + +class DhtUpdateRuleAnybody : public DhtUpdateRule { + public: + td::Status check_value(const DhtValue &value) override; + td::Status update_value(DhtValue &value, DhtValue &&new_value) override; + bool need_republish() const override { + return false; + } + tl_object_ptr tl() const override; + static td::Result> create(); +}; + +class DhtUpdateRuleOverlayNodes : public DhtUpdateRule { + public: + td::Status check_value(const DhtValue &value) override; + td::Status update_value(DhtValue &value, DhtValue &&new_value) override; + bool need_republish() const override { + return false; + } + tl_object_ptr tl() const override; + static td::Result> create(); +}; + +} // namespace dht + +} // namespace ton + +namespace td { + +inline td::StringBuilder &operator<<(td::StringBuilder &sb, const ton::dht::DhtKeyId &dht) { + sb << dht.tl(); + return sb; +} + +} // namespace td diff --git a/dht/dht.cpp b/dht/dht.cpp new file mode 100644 index 00000000..8231f89f --- /dev/null +++ b/dht/dht.cpp @@ -0,0 +1,552 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "dht.hpp" + +#include "td/utils/tl_storers.h" +#include "td/utils/crypto.h" +#include "td/utils/tl_parsers.h" +#include "td/utils/Random.h" +#include "td/utils/base64.h" + +#include "td/utils/format.h" + +#include "td/db/RocksDb.h" + +#include "keys/encryptor.h" +#include "adnl/utils.hpp" + +#include "auto/tl/ton_api.hpp" + +#include "dht.h" +#include "dht-bucket.hpp" +#include "dht-query.hpp" +#include "dht-in.hpp" + +namespace ton { + +namespace dht { + +td::actor::ActorOwn DhtMember::create(adnl::AdnlNodeIdShort id, std::string db_root, + td::actor::ActorId keyring, + td::actor::ActorId adnl, td::uint32 k, td::uint32 a) { + return td::actor::ActorOwn( + td::actor::create_actor("dht", id, db_root, keyring, adnl, k, a)); +} + +td::Result> Dht::create(adnl::AdnlNodeIdShort id, std::string db_root, + std::shared_ptr conf, + td::actor::ActorId keyring, + td::actor::ActorId adnl) { + CHECK(conf->get_k() > 0); + CHECK(conf->get_a() > 0); + + auto D = DhtMember::create(id, db_root, keyring, adnl, conf->get_k(), conf->get_a()); + auto &nodes = conf->nodes(); + + for (auto &node : nodes.list()) { + auto key = node.get_key(); + td::actor::send_closure(D, &DhtMember::add_full_node, key, node.clone()); + } + return std::move(D); +} + +void DhtMemberImpl::start_up() { + std::shared_ptr kv = std::make_shared( + td::RocksDb::open(PSTRING() << db_root_ << "/dht-" << td::base64url_encode(id_.as_slice())).move_as_ok()); + std::vector methods = {ton_api::dht_getSignedAddressList::ID, + ton_api::dht_findNode::ID, + ton_api::dht_findValue::ID, + ton_api::dht_store::ID, + ton_api::dht_ping::ID, + ton_api::dht_query::ID, + ton_api::dht_message::ID}; + + for (auto it : methods) { + td::actor::send_closure(adnl_, &adnl::Adnl::subscribe, id_, adnl::Adnl::int_to_bytestring(it), + std::make_unique(actor_id(this), id_)); + } + alarm_timestamp() = td::Timestamp::in(1.0); + for (td::uint32 bit = 0; bit < 256; bit++) { + auto key = create_hash_tl_object(bit); + std::string value; + auto R = kv->get(key.as_slice(), value); + R.ensure(); + if (R.move_as_ok() == td::KeyValue::GetStatus::Ok) { + auto V = fetch_tl_object(td::BufferSlice{value}, true); + V.ensure(); + auto nodes = std::move(V.move_as_ok()->nodes_); + auto s = nodes->nodes_.size(); + DhtNodesList list{std::move(nodes)}; + CHECK(list.size() == s); + auto &B = buckets_[bit]; + for (auto &node : list.list()) { + auto key = node.get_key(); + B.add_full_node(key, std::move(node), adnl_, id_); + } + } + } + db_ = DbType{std::move(kv)}; +} + +void DhtMemberImpl::tear_down() { + std::vector methods = {ton_api::dht_getSignedAddressList::ID, + ton_api::dht_findNode::ID, + ton_api::dht_findValue::ID, + ton_api::dht_store::ID, + ton_api::dht_ping::ID, + ton_api::dht_query::ID, + ton_api::dht_message::ID}; + + for (auto it : methods) { + td::actor::send_closure(adnl_, &adnl::Adnl::unsubscribe, id_, adnl::Adnl::int_to_bytestring(it)); + } +} + +void DhtMemberImpl::save_to_db() { + next_save_to_db_at_ = td::Timestamp::in(10.0); + alarm_timestamp().relax(next_save_to_db_at_); + + td::uint32 bit = td::Random::fast(0, 255); + auto &B = buckets_[bit]; + auto list = B.export_nodes(); + if (list.size() > 0) { + auto key = create_hash_tl_object(bit); + auto value = create_serialize_tl_object(list.tl()); + + db_.set(key, std::move(value)); + } +} + +DhtNodesList DhtMemberImpl::get_nearest_nodes(DhtKeyId id, td::uint32 k) { + DhtNodesList vec; + + auto id_xor = id ^ key_; + + for (td::uint32 bit = 0; bit < 256; bit++) { + if (id_xor.get_bit(bit)) { + buckets_[bit].get_nearest_nodes(id, bit, vec, k); + if (vec.size() >= k) { + break; + } + } + } + + for (auto &el : vec.list()) { + CHECK((el.get_key() ^ id) < id_xor); + } + if (vec.size() < k) { + for (td::uint32 bit = 255; bit != 256; bit = bit ? (bit - 1) : 256) { + if (!id_xor.get_bit(bit)) { + buckets_[bit].get_nearest_nodes(id, bit, vec, k); + if (vec.size() >= k) { + break; + } + } + } + } + CHECK(vec.size() <= k); + return vec; +} + +td::uint32 DhtMemberImpl::distance(DhtKeyId key_id, td::uint32 max_value) { + if (!max_value) { + max_value = 2 * k_; + } + td::uint32 res = 0; + auto id_xor = key_id ^ key_; + + for (td::uint32 bit = 0; bit < 256; bit++) { + if (id_xor.get_bit(bit)) { + res += buckets_[bit].active_cnt(); + if (res >= max_value) { + return max_value; + } + } + } + return res; +} + +void DhtMemberImpl::process_query(adnl::AdnlNodeIdShort src, ton_api::dht_ping &query, + td::Promise promise) { + ping_queries_++; + promise.set_value(create_serialize_tl_object(query.random_id_)); +} + +void DhtMemberImpl::process_query(adnl::AdnlNodeIdShort src, ton_api::dht_findNode &query, + td::Promise promise) { + find_node_queries_++; + auto k = static_cast(query.k_); + if (k > max_k()) { + k = max_k(); + } + auto R = get_nearest_nodes(DhtKeyId{query.key_}, k); + promise.set_value(serialize_tl_object(R.tl(), true)); +} + +void DhtMemberImpl::process_query(adnl::AdnlNodeIdShort src, ton_api::dht_findValue &query, + td::Promise promise) { + find_value_queries_++; + auto it = values_.find(DhtKeyId{query.key_}); + if (it != values_.end() && it->second.expired()) { + values_.erase(it); + it = values_.end(); + } + if (it != values_.end()) { + promise.set_value(create_serialize_tl_object(it->second.tl())); + return; + } + + auto k = static_cast(query.k_); + if (k > max_k()) { + k = max_k(); + } + auto R = get_nearest_nodes(DhtKeyId{query.key_}, k); + + promise.set_value(create_serialize_tl_object(R.tl())); +} + +td::Status DhtMemberImpl::store_in(DhtValue value) { + if (value.expired()) { + VLOG(DHT_INFO) << this << ": dropping expired value: " << value.key_id() << " expire_at = " << value.ttl(); + return td::Status::OK(); + } + TRY_STATUS(value.check()); + + auto key_id = value.key_id(); + + auto dist = distance(key_id, k_ + 10); + if (dist < k_ + 10) { + auto it = values_.find(key_id); + if (it != values_.end()) { + it->second.update(std::move(value)); + } else { + values_.emplace(key_id, std::move(value)); + } + } else { + VLOG(DHT_INFO) << this << ": dropping too remote value: " << value.key_id() << " distance = " << dist; + } + return td::Status::OK(); +} + +void DhtMemberImpl::process_query(adnl::AdnlNodeIdShort src, ton_api::dht_store &query, + td::Promise promise) { + store_queries_++; + auto V = DhtValue::create(std::move(query.value_), true); + if (V.is_error()) { + promise.set_error(td::Status::Error(ErrorCode::protoviolation, + PSTRING() << "dropping invalid dht_store() value: " << V.error().to_string())); + VLOG(DHT_INFO) << this << ": dropping invalid value: " << V.move_as_error(); + return; + } + auto b = store_in(V.move_as_ok()); + + if (b.is_ok()) { + promise.set_value(create_serialize_tl_object()); + } else { + VLOG(DHT_INFO) << this << ": dropping store() query from " << src << ": " << b.move_as_error(); + promise.set_error(td::Status::Error(ErrorCode::protoviolation, "dropping dht_store() query")); + } +} + +void DhtMemberImpl::process_query(adnl::AdnlNodeIdShort src, ton_api::dht_getSignedAddressList &query, + td::Promise promise) { + get_addr_list_queries_++; + + auto P = td::PromiseCreator::lambda([promise = std::move(promise)](td::Result R) mutable { + R.ensure(); + promise.set_value(serialize_tl_object(R.move_as_ok().tl(), true)); + }); + get_self_node(std::move(P)); +} + +void DhtMemberImpl::receive_query(adnl::AdnlNodeIdShort src, td::BufferSlice data, + td::Promise promise) { + { + auto R = fetch_tl_prefix(data, true); + if (R.is_ok()) { + auto N = DhtNode::create(std::move(R.move_as_ok()->node_)); + if (N.is_ok()) { + auto node = N.move_as_ok(); + auto key = node.get_key(); + add_full_node(key, std::move(node)); + } else { + VLOG(DHT_WARNING) << this << ": dropping bad node " << N.move_as_error(); + } + } + } + auto R = fetch_tl_object(std::move(data), true); + + if (R.is_error()) { + VLOG(DHT_WARNING) << this << ": dropping unknown query to DHT node: " << R.move_as_error(); + promise.set_error(td::Status::Error(ErrorCode::protoviolation, "failed to parse dht query")); + return; + } + + auto Q = R.move_as_ok(); + if (td::Random::fast(0, 127) == 0) { + VLOG(DHT_DEBUG) << this << ": ping=" << ping_queries_ << " fnode=" << find_node_queries_ + << " fvalue=" << find_value_queries_ << " store=" << store_queries_ + << " addrlist=" << get_addr_list_queries_; + VLOG(DHT_DEBUG) << this << ": query to DHT from " << src << ": " << ton_api::to_string(Q); + } + + VLOG(DHT_EXTRA_DEBUG) << this << ": query to DHT from " << src << ": " << ton_api::to_string(Q); + + ton_api::downcast_call(*Q.get(), [&](auto &object) { this->process_query(src, object, std::move(promise)); }); +} + +void DhtMemberImpl::add_full_node(DhtKeyId key, DhtNode node) { + VLOG(DHT_EXTRA_DEBUG) << this << ": adding full node " << key; + + auto eid = key ^ key_; + auto bit = eid.count_leading_zeroes(); +#ifndef NDEBUG + for (td::uint32 i = 0; i < bit; i++) { + CHECK(key.get_bit(i) == key_.get_bit(i)); + } +#endif + if (bit < 256) { + CHECK(key.get_bit(bit) != key_.get_bit(bit)); + buckets_[bit].add_full_node(key, std::move(node), adnl_, id_); + } else { + CHECK(key == key_); + } +} + +void DhtMemberImpl::receive_ping(DhtKeyId key, DhtNode result) { + VLOG(DHT_EXTRA_DEBUG) << this << ": received ping from " << key; + + auto eid = key ^ key_; + auto bit = eid.count_leading_zeroes(); + if (bit < 256) { + buckets_[bit].receive_ping(key, std::move(result), adnl_, id_); + } else { + CHECK(key == key_); + } +} + +void DhtMemberImpl::receive_message(adnl::AdnlNodeIdShort src, td::BufferSlice data) { +} + +void DhtMemberImpl::set_value(DhtValue value, td::Promise promise) { + auto S = value.check(); + if (S.is_error()) { + promise.set_error(std::move(S)); + return; + } + auto h = value.key_id(); + our_values_.emplace(h, value.clone()); + + send_store(std::move(value), std::move(promise)); +} + +void DhtMemberImpl::get_value_in(DhtKeyId key, td::Promise result) { + auto P = td::PromiseCreator::lambda([key, promise = std::move(result), SelfId = actor_id(this), print_id = print_id(), + adnl = adnl_, list = get_nearest_nodes(key, k_), k = k_, a = a_, + id = id_](td::Result R) mutable { + R.ensure(); + td::actor::create_actor("FindValueQuery", key, print_id, id, std::move(list), k, a, + R.move_as_ok(), SelfId, adnl, std::move(promise)) + .release(); + }); + + get_self_node(std::move(P)); +} + +void DhtMemberImpl::check() { + VLOG(DHT_INFO) << this << ": ping=" << ping_queries_ << " fnode=" << find_node_queries_ + << " fvalue=" << find_value_queries_ << " store=" << store_queries_ + << " addrlist=" << get_addr_list_queries_; + for (auto &bucket : buckets_) { + bucket.check(adnl_, actor_id(this), id_); + } + if (next_save_to_db_at_.is_in_past()) { + save_to_db(); + } + + if (values_.size() > 0) { + auto it = values_.lower_bound(last_check_key_); + if (it != values_.end() && it->first == last_check_key_) { + it++; + } + if (it == values_.end()) { + it = values_.begin(); + } + + td::uint32 cnt = 0; + auto s = last_check_key_; + while (values_.size() > 0 && cnt < 1 && it->first != s) { + last_check_key_ = it->first; + cnt++; + if (it->second.expired()) { + it = values_.erase(it); + + // do not republish soon-to-be-expired values + } else if (it->second.ttl() > td::Clocks::system() + 60) { + auto dist = distance(it->first, k_ + 10); + + if (dist == 0) { + if (it->second.key().update_rule()->need_republish()) { + auto P = td::PromiseCreator::lambda([print_id = print_id()](td::Result R) { + if (R.is_error()) { + VLOG(DHT_INFO) << print_id << ": failed to store: " << R.move_as_error(); + } + }); + send_store(it->second.clone(), std::move(P)); + } + it++; + } else if (dist >= k_ + 10) { + it = values_.erase(it); + } else { + it++; + } + } else { + it++; + } + if (values_.size() == 0) { + break; + } + if (it == values_.end()) { + it = values_.begin(); + } + } + } + + if (republish_att_.is_in_past()) { + auto it = our_values_.lower_bound(last_republish_key_); + if (it != our_values_.end() && it->first == last_republish_key_) { + it++; + } + if (it == our_values_.end()) { + it = our_values_.begin(); + } + if (it != our_values_.end()) { + if (it->second.ttl() > td::Clocks::system() + 60) { + auto P = td::PromiseCreator::lambda([print_id = print_id()](td::Result R) { + if (R.is_error()) { + VLOG(DHT_INFO) << print_id << ": failed to store: " << R.move_as_error(); + } + }); + send_store(it->second.clone(), std::move(P)); + } + last_republish_key_ = it->first; + } + republish_att_ = td::Timestamp::in(10.0 + td::Random::fast(0, 1000) * 0.001); + } + + if (fill_att_.is_in_past()) { + auto promise = td::PromiseCreator::lambda([](td::Result R) { + if (R.is_error()) { + VLOG(DHT_WARNING) << "failed find self query: " << R.move_as_error(); + } + }); + + td::Bits256 x; + + td::uint32 t = td::Random::fast(0, 6); + td::uint32 b = 64 - td::Random::fast(0, 1 << t); + td::Random::secure_bytes(x.as_slice()); + for (td::uint32 i = 0; i < b; i++) { + x.bits()[i] = key_.get_bit(i); + } + + DhtKeyId key{x}; + auto P = td::PromiseCreator::lambda([key, promise = std::move(promise), SelfId = actor_id(this), + print_id = print_id(), adnl = adnl_, list = get_nearest_nodes(key, k_), k = k_, + a = a_, id = id_](td::Result R) mutable { + R.ensure(); + td::actor::create_actor("FindNodesQuery", key, print_id, id, std::move(list), k, a, + R.move_as_ok(), SelfId, adnl, std::move(promise)) + .release(); + }); + + get_self_node(std::move(P)); + + fill_att_ = td::Timestamp::in(10.0 + td::Random::fast(0, 100) * 0.1); + } +} + +void DhtMemberImpl::dump(td::StringBuilder &sb) const { + for (auto &B : buckets_) { + B.dump(sb); + } +} + +void DhtMemberImpl::send_store(DhtValue value, td::Promise promise) { + value.check().ensure(); + auto key_id = value.key_id(); + + auto P = td::PromiseCreator::lambda([value = std::move(value), print_id = print_id(), id = id_, + list = get_nearest_nodes(key_id, k_), k = k_, a = a_, SelfId = actor_id(this), + adnl = adnl_, promise = std::move(promise)](td::Result R) mutable { + R.ensure(); + td::actor::create_actor("StoreQuery", std::move(value), print_id, id, std::move(list), k, a, + R.move_as_ok(), SelfId, adnl, std::move(promise)) + .release(); + }); + + get_self_node(std::move(P)); +} + +void DhtMemberImpl::get_self_node(td::Promise promise) { + auto P = td::PromiseCreator::lambda([promise = std::move(promise), print_id = print_id(), id = id_, + keyring = keyring_](td::Result R) mutable { + R.ensure(); + auto node = R.move_as_ok(); + auto version = static_cast(td::Clocks::system()); + auto B = create_serialize_tl_object(node.pub_id().tl(), node.addr_list().tl(), version, + td::BufferSlice()); + CHECK(node.addr_list().size() > 0); + auto P = td::PromiseCreator::lambda( + [promise = std::move(promise), node = std::move(node), version](td::Result R) mutable { + R.ensure(); + DhtNode n{node.pub_id(), node.addr_list(), version, R.move_as_ok()}; + promise.set_result(std::move(n)); + }); + td::actor::send_closure(keyring, &keyring::Keyring::sign_message, id.pubkey_hash(), std::move(B), std::move(P)); + }); + td::actor::send_closure(adnl_, &adnl::Adnl::get_self_node, id_, std::move(P)); +} + +td::Result> Dht::create_global_config(tl_object_ptr conf) { + td::uint32 k; + if (conf->k_ == 0) { + k = DhtMember::default_k(); + } else if (conf->k_ > 0 && static_cast(conf->k_) <= DhtMember::max_k()) { + k = conf->k_; + } else { + return td::Status::Error(ErrorCode::protoviolation, PSTRING() << "bad value k=" << conf->k_); + } + td::uint32 a; + if (conf->a_ == 0) { + a = DhtMember::default_a(); + } else if (conf->a_ > 0 && static_cast(conf->a_) <= DhtMember::max_a()) { + a = conf->a_; + } else { + return td::Status::Error(ErrorCode::protoviolation, PSTRING() << "bad value a=" << conf->a_); + } + + DhtNodesList l{std::move(conf->static_nodes_)}; + + return std::make_shared(k, a, std::move(l)); +} + +} // namespace dht + +} // namespace ton diff --git a/dht/dht.h b/dht/dht.h new file mode 100644 index 00000000..fea18e5d --- /dev/null +++ b/dht/dht.h @@ -0,0 +1,60 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +#include "td/utils/int_types.h" + +#include "td/actor/PromiseFuture.h" + +#include "td/actor/actor.h" + +#include "adnl/adnl.h" + +#include "auto/tl/ton_api.hpp" + +#include "dht-types.h" + +namespace ton { + +namespace dht { + +class DhtGlobalConfig; + +class Dht : public td::actor::Actor { + public: + static td::Result> create(adnl::AdnlNodeIdShort id, std::string db_root, + std::shared_ptr conf, + td::actor::ActorId keyring, + td::actor::ActorId adnl); + static td::Result> create_global_config( + tl_object_ptr conf); + + virtual adnl::AdnlNodeIdShort get_id() const = 0; + + virtual void set_value(DhtValue key_value, td::Promise result) = 0; + virtual void get_value(DhtKey key, td::Promise result) = 0; + + virtual void dump(td::StringBuilder &sb) const = 0; + + virtual ~Dht() = default; +}; + +} // namespace dht + +} // namespace ton diff --git a/dht/dht.hpp b/dht/dht.hpp new file mode 100644 index 00000000..30c580ae --- /dev/null +++ b/dht/dht.hpp @@ -0,0 +1,122 @@ +/* + This file is part of TON Blockchain Library. + + TON Blockchain Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + TON Blockchain Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with TON Blockchain Library. If not, see . + + Copyright 2017-2019 Telegram Systems LLP +*/ +#pragma once + +#include + +#include "td/utils/int_types.h" +#include "td/actor/actor.h" +#include "td/utils/List.h" + +#include "adnl/adnl.h" + +#include "adnl/utils.hpp" +#include "keys/encryptor.h" + +#include "dht.h" +#include "dht-node.hpp" + +#include "auto/tl/ton_api.hpp" + +namespace ton { + +namespace dht { + +constexpr int VERBOSITY_NAME(DHT_WARNING) = verbosity_INFO; +constexpr int VERBOSITY_NAME(DHT_NOTICE) = verbosity_DEBUG; +constexpr int VERBOSITY_NAME(DHT_INFO) = verbosity_DEBUG; +constexpr int VERBOSITY_NAME(DHT_DEBUG) = verbosity_DEBUG + 1; +constexpr int VERBOSITY_NAME(DHT_EXTRA_DEBUG) = verbosity_DEBUG + 10; + +class DhtGlobalConfig { + public: + auto get_k() const { + return k_; + } + auto get_a() const { + return a_; + } + const auto &nodes() const { + return static_nodes_; + } + DhtGlobalConfig(td::uint32 k, td::uint32 a, DhtNodesList nodes) : k_(k), a_(a), static_nodes_(std::move(nodes)) { + } + + private: + td::uint32 k_; + td::uint32 a_; + DhtNodesList static_nodes_; +}; + +class DhtMember : public Dht { + public: + static constexpr td::uint32 default_k() { + return 10; + } + static constexpr td::uint32 default_a() { + return 3; + } + static constexpr td::uint32 max_k() { + return 100; + } + static constexpr td::uint32 max_a() { + return 10; + } + + struct PrintId { + adnl::AdnlNodeIdShort id; + }; + + static td::actor::ActorOwn create(adnl::AdnlNodeIdShort id, std::string db_root, + td::actor::ActorId keyring, td::actor::ActorId adnl, + td::uint32 k = 10, td::uint32 a = 3); + + //virtual void update_addr_list(tl_object_ptr addr_list) = 0; + //virtual void add_node(adnl::AdnlNodeIdShort id) = 0; + virtual void add_full_node(DhtKeyId id, DhtNode node) = 0; + + virtual void receive_ping(DhtKeyId id, DhtNode result) = 0; + + virtual void get_value_in(DhtKeyId key, td::Promise result) = 0; + + virtual td::Status store_in(DhtValue value) = 0; + + virtual void get_self_node(td::Promise promise) = 0; + + virtual PrintId print_id() const = 0; +}; + +inline td::StringBuilder &operator<<(td::StringBuilder &sb, const DhtMember::PrintId &id) { + sb << "[dhtnode " << id.id << "]"; + return sb; +} + +inline td::StringBuilder &operator<<(td::StringBuilder &sb, const DhtMember &dht) { + sb << dht.print_id(); + return sb; +} + +inline td::StringBuilder &operator<<(td::StringBuilder &sb, const DhtMember *dht) { + sb << dht->print_id(); + return sb; +} + +} // namespace dht + +} // namespace ton diff --git a/dht/test/dht-test-ping.cpp b/dht/test/dht-test-ping.cpp new file mode 100644 index 00000000..d51b34bb --- /dev/null +++ b/dht/test/dht-test-ping.cpp @@ -0,0 +1,303 @@ +/* + This file is part of TON Blockchain source code. + + TON Blockchain is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + TON Blockchain is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with TON Blockchain. If not, see . + + In addition, as a special exception, the copyright holders give permission + to link the code of portions of this program with the OpenSSL library. + You must obey the GNU General Public License in all respects for all + of the code used other than OpenSSL. If you modify file(s) with this + exception, you may extend this exception to your version of the file(s), + but you are not obligated to do so. If you do not wish to do so, delete this + exception statement from your version. If you delete this exception statement + from all source files in the program, then also delete it here. + + Copyright 2017-2019 Telegram Systems LLP +*/ +#include "adnl/adnl-network-manager.h" +#include "adnl/adnl-peer-table.h" +#include "adnl/utils.hpp" +#include "keys/encryptor.h" +#include "td/utils/Time.h" +#include "td/utils/format.h" +#include "td/utils/OptionsParser.h" +#include "td/utils/filesystem.h" +#include "dht/dht.h" +#include "auto/tl/ton_api_json.h" + +#include +#include + +template +std::ostream &operator<<(std::ostream &stream, const td::UInt &x) { + for (size_t i = 0; i < size / 8; i++) { + stream << td::format::hex_digit((x.raw[i] >> 4) & 15) << td::format::hex_digit(x.raw[i] & 15); + } + + return stream; +} + +class adnl::AdnlNode : public td::actor::Actor { + private: + std::vector ping_ids_; + + td::actor::ActorOwn network_manager_; + td::actor::ActorOwn peer_table_; + td::actor::ActorOwn dht_node_; + + td::UInt256 local_id_; + bool local_id_set_ = false; + + std::string host_ = "127.0.0.1"; + td::uint32 ip_ = 0x7f000001; + td::uint16 port_ = 2380; + + std::string local_config_ = "ton-local.config"; + std::string global_config_ = "ton-global.config"; + + void receive_message(td::UInt256 src, td::UInt256 dst, td::BufferSlice data) { + std::cout << "MESSAGE FROM " << src << " to " << dst << " of size " << std::to_string(data.size()) << "\n"; + } + + void receive_query(td::UInt256 src, td::UInt256 dst, td::uint64 query_id, td::BufferSlice data) { + std::cout << "QUERY " << std::to_string(query_id) << " FROM " << src << " to " << dst << " of size " + << std::to_string(data.size()) << "\n"; + td::actor::send_closure(peer_table_, &ton::adnl::AdnlPeerTable::answer_query, dst, src, query_id, + ton::create_tl_object()); + } + + std::unique_ptr make_callback() { + class Callback : public ton::adnl::AdnlPeerTable::Callback { + public: + void receive_message(td::UInt256 src, td::UInt256 dst, td::BufferSlice data) override { + td::actor::send_closure(id_, &adnl::AdnlNode::receive_message, src, dst, std::move(data)); + } + void receive_query(td::UInt256 src, td::UInt256 dst, td::uint64 query_id, td::BufferSlice data) override { + td::actor::send_closure(id_, &adnl::AdnlNode::receive_query, src, dst, query_id, std::move(data)); + } + Callback(td::actor::ActorId id) : id_(std::move(id)) { + } + + private: + td::actor::ActorId id_; + }; + + return std::make_unique(td::actor::actor_id(this)); + } + + public: + void set_local_config(std::string str) { + local_config_ = str; + } + void set_global_config(std::string str) { + global_config_ = str; + } + void start_up() override { + alarm_timestamp() = td::Timestamp::in(1); + } + adnl::AdnlNode() { + network_manager_ = ton::adnl::AdnlNetworkManager::create(); + peer_table_ = ton::adnl::AdnlPeerTable::create(); + td::actor::send_closure(network_manager_, &ton::adnl::AdnlNetworkManager::register_peer_table, peer_table_.get()); + td::actor::send_closure(peer_table_, &ton::adnl::AdnlPeerTable::register_network_manager, network_manager_.get()); + } + void listen_udp(td::uint16 port) { + td::actor::send_closure(network_manager_, &ton::adnl::AdnlNetworkManager::add_listening_udp_port, "0.0.0.0", port); + port_ = port; + } + void run() { + auto L = td::read_file(local_config_); + if (L.is_error()) { + LOG(FATAL) << "can not read local config: " << L.move_as_error(); + } + auto L2 = td::json_decode(L.move_as_ok().as_slice()); + if (L2.is_error()) { + LOG(FATAL) << "can not parse local config: " << L2.move_as_error(); + } + auto lc_j = L2.move_as_ok(); + if (lc_j.type() != td::JsonValue::Type::Object) { + LOG(FATAL) << "can not parse local config: expected json object"; + } + + ton::ton_api::config_local lc; + auto rl = ton::ton_api::from_json(lc, lc_j.get_object()); + if (rl.is_error()) { + LOG(FATAL) << "can not interpret local config: " << rl.move_as_error(); + } + + auto G = td::read_file(global_config_); + if (G.is_error()) { + LOG(FATAL) << "can not read global config: " << G.move_as_error(); + } + auto G2 = td::json_decode(G.move_as_ok().as_slice()); + if (G2.is_error()) { + LOG(FATAL) << "can not parse global config: " << G2.move_as_error(); + } + auto gc_j = G2.move_as_ok(); + if (gc_j.type() != td::JsonValue::Type::Object) { + LOG(FATAL) << "can not parse global config: expected json object"; + } + + ton::ton_api::config_global gc; + auto rg = ton::ton_api::from_json(gc, gc_j.get_object()); + if (rg.is_error()) { + LOG(FATAL) << "can not interpret local config: " << rg.move_as_error(); + } + + if (gc.adnl_) { + auto it = gc.adnl_->static_nodes_.begin(); + while (it != gc.adnl_->static_nodes_.end()) { + auto R = ton::adnl_validate_full_id(std::move((*it)->id_)); + if (R.is_error()) { + LOG(FATAL) << "can not apply global config: " << R.move_as_error(); + } + auto R2 = ton::adnl_validate_addr_list(std::move((*it)->addr_list_)); + if (R2.is_error()) { + LOG(FATAL) << "can not apply global config: " << R2.move_as_error(); + } + td::actor::send_closure(peer_table_, &ton::adnl::AdnlPeerTable::add_peer, R.move_as_ok(), R2.move_as_ok()); + it++; + } + } + + if (!gc.dht_) { + LOG(FATAL) << "global config does not contain dht section"; + } + if (lc.dht_.size() != 1) { + LOG(FATAL) << "local config must contain exactly one dht section"; + } + + auto R = ton::DhtNode::create_from_json(std::move(gc.dht_), std::move(lc.dht_[0]), peer_table_.get()); + if (R.is_error()) { + LOG(FATAL) << "fail creating dht node: " << R.move_as_error(); + } + + dht_node_ = R.move_as_ok(); + } + /* + void set_host(td::IPAddress ip, std::string host) { + ip_ = ip.get_ipv4(); + host_ = host; + } + void send_pings_to(td::UInt256 id) { + std::cout << "send pings to " << id << "\n"; + ping_ids_.push_back(id); + } + void add_local_id(ton::tl_object_ptr pk_) { + auto pub_ = ton::get_public_key(pk_); + local_id_ = ton::adnl_short_id(pub_); + std::cout << "local_id = '" << local_id_ << "'\n"; + auto x = ton::create_tl_object(ip_, port_); + auto v = std::vector>(); + v.push_back(ton::move_tl_object_as(x)); + auto y = + ton::create_tl_object(std::move(v), static_cast(td::Time::now())); + + LOG(INFO) << "local_addr_list: " << ton::ton_api::to_string(y); + td::actor::send_closure(peer_table_, &ton::adnl::AdnlPeerTable::add_id, ton::clone_tl_object(pk_), + ton::clone_tl_object(y)); + td::actor::send_closure(peer_table_, &ton::adnl::AdnlPeerTable::subscribe_custom, local_id_, "TEST", make_callback()); + local_id_set_ = true; + + dht_node_ = ton::DhtNode::create(std::move(pk_), peer_table_.get()); + td::actor::send_closure(dht_node_, &ton::DhtNode::update_addr_list, std::move(y)); + } + + void add_static_dht_node(ton::tl_object_ptr id, + ton::tl_object_ptr addr_list, + td::BufferSlice signature) { + auto Id = ton::adnl_short_id(id); + td::actor::send_closure( + dht_node_, &ton::DhtNode::add_full_node, Id, + ton::create_tl_object(std::move(id), std::move(addr_list), signature.as_slice().str())); + } + + void add_foreign(ton::tl_object_ptr id, + ton::tl_object_ptr addr_list) { + std::cout << ton::adnl_short_id(id) << "\n"; + td::actor::send_closure(peer_table_, &ton::adnl::AdnlPeerTable::add_peer, std::move(id), std::move(addr_list)); + } + + void alarm() override { + std::cout << "alarm\n"; + if (local_id_set_) { + for (auto it = ping_ids_.begin(); it != ping_ids_.end(); it++) { + auto P = td::PromiseCreator::lambda([](td::Result> result) { + if (result.is_error()) { + std::cout << "received error " << result.move_as_error().to_string() << "\n"; + } else { + auto message = result.move_as_ok(); + std::cout << "received answer to query\n"; + } + }); + td::actor::send_closure(peer_table_, &ton::adnl::AdnlPeerTable::send_query, local_id_, *it, std::move(P), + td::Timestamp::in(5), + ton::move_tl_object_as( + ton::create_tl_object("TEST"))); + } + } + } + */ +}; + +td::Result get_uint256(std::string str) { + if (str.size() != 64) { + return td::Status::Error("uint256 must have 64 bytes"); + } + td::UInt256 res; + for (size_t i = 0; i < 32; i++) { + res.raw[i] = static_cast(td::hex_to_int(str[2 * i]) * 16 + td::hex_to_int(str[2 * i + 1])); + } + return res; +} + +int main(int argc, char *argv[]) { + td::actor::ActorOwn x; + + td::OptionsParser p; + p.set_description("test basic adnl functionality"); + p.add_option('h', "help", "prints_help", [&]() { + char b[10240]; + td::StringBuilder sb({b, 10000}); + sb << p; + std::cout << sb.as_cslice().c_str(); + std::exit(2); + return td::Status::OK(); + }); + p.add_option('p', "port", "sets udp port", [&](td::Slice port) { + td::actor::send_closure(x, &adnl::AdnlNode::listen_udp, static_cast(std::stoi(port.str()))); + return td::Status::OK(); + }); + p.add_option('C', "global-config", "file to read global config", [&](td::Slice fname) { + td::actor::send_closure(x, &adnl::AdnlNode::set_global_config, fname.str()); + return td::Status::OK(); + }); + p.add_option('c', "local-config", "file to read local config", [&](td::Slice fname) { + td::actor::send_closure(x, &adnl::AdnlNode::set_local_config, fname.str()); + return td::Status::OK(); + }); + + td::actor::Scheduler scheduler({2}); + + scheduler.run_in_context([&] { + x = td::actor::create_actor(td::actor::ActorInfoCreator::Options().with_name("A").with_poll()); + }); + + scheduler.run_in_context([&] { p.run(argc, argv).ensure(); }); + scheduler.run_in_context([&] { td::actor::send_closure(x, &adnl::AdnlNode::run); }); + + scheduler.run(); + + return 0; +} diff --git a/doc/FullNode-HOWTO b/doc/FullNode-HOWTO new file mode 100644 index 00000000..fa927058 --- /dev/null +++ b/doc/FullNode-HOWTO @@ -0,0 +1,172 @@ +The aim of this document is to provide step-by-step instructions for setting up a full node for the TON Blockchain. We assume some familiarity with the TON Blockchain Lite Client, at least to the extent explained in the Lite Client HOWTO. + +Notice that you need a machine with a public IP address and a high-bandwidth network connection to run a TON Blockchain Full Node. Typically you'll need a sufficiently powerful server in a datacenter. It is a bad idea to run a Full Node on your home computer; instead, you could run a Full Node on a remote server, and use the TON Blockchain Lite Client to connect to it from home. + +0. Downloading and compiling +~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +The complete TON Blockchain Library and Validator software is downloaded and compiled similarly to the Lite Client. This process is outlined in the corresponding README file. The most important difference is that you have to download the complete source archive available at https://test.ton.org/ton-blockchain-full.tar.xz instead of the smaller Lite Client source archive. You should also build all goals defined in CMakeLists.txt (e.g. by running `cmake ` and `make` in your build directory), not only those specifically related to the Lite Client (which is also included in the larger distribution). + +1. Full Node binaries +~~~~~~~~~~~~~~~~~~~~~ + +After the sources have been compiled successfully, you should obtain executable files `validator-engine/validator-engine` and `validator-engine-console/validator-engine-console` in your build directory. These are the most important files you need to run a TON Blockchain Full Node (or even a Validator), and to control it. You might wish to install them into your /usr/bin or similar directory. You are also likely to need the `generate-random-id` utility during setup. + +2. Working directory of the Full Node +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +The Full Node (also known as "validator-engine") stores its data in subdirectories of its working directory, e.g., `/var/ton-work/db`. It requires write access to this directory. If you want to use another directory as the working directory of the Full Node, you can use the command line option --db : + +$ validator-engine --db ${DB_ROOT} + +where ${DB_ROOT} is /var/ton-work/db or any other directory where validator-engine has write permissions. + +3. Working directory layout +~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +An approximate layout of the working directory of the TON Blockchain Full Node software is as follows: + +* ${DB_ROOT}/config.json -- Automatically generated configuration file. It is automatically regenerated by validator-engine on some occasions. When validator-engine is not running, you can edit this file in a text editor because it is a JSON file. +* ${DB_ROOT}/static -- A directory with files that cannot be downloaded from the network, such as the "zerostate" (corresponding to the Genesis block of other blockchain architectures) of the masterchain and active workchains. Normally you don't have to initialize this directory unless you want to run your own instance of the TON Blockchain (for example, for testing or development purposes). Full nodes of existing instances of the TON Blockchain (such as the "testnet" and the "mainnet") will be able to download all required files from already running full nodes. +* ${DB_ROOT}/keyring -- Stores public and private keys known to validator-engine. For example, if your full node runs as a validator for some of the TON Blockchain shardchains, the validator block signing key is kept here. You may wish to set more restrictive permissions for this directory, such as 0700 (in *nix systems), so that only the user under which validator-engine is running would have access to this directory. +* ${DB_ROOT}/error -- A directory where validator-engine copies files related to severe error conditions (e.g., invalid block candidates) for further study. It is normally empty, and validator-engine never deletes files from this directory. +* ${DB_ROOT}/archive -- A directory where old and rarely used blocks are kept until their storage period expires. You can mount a larger but slower disk partition at this directory, or make this directory a symlink to a directory in such a partition. We recommend locating the remainder of ${DB_ROOT} in a fast storage device such as an SSD. +* ${DB_ROOT}/etc -- (Non-automatic) configuration files may be kept here, or in any other directory read-accessible to validator-engine. +* Other subdirectories of ${DB_ROOT} are used to keep ADNL cache data, recent blocks and states, and so on. They are not relevant to the purposes of this document. + +4. Global configuration of the TON Blockchain +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +In order to set up your Full Node, you'll need a special JSON file called the "global configuration (file)". It is called this because it is the same for all full nodes, and even nodes participating in different instances of the TON Blockchain (e.g., "testnet" vs. "mainnet") share an almost identical global configuration. + +The "testnet" global configuration can be downloaded at https://test.ton.org/ton-global.config.json as follows: + +$ wget https://test.ton.org/ton-global.config.json + +You may wish to put this file into /var/ton-work/etc/ton-global.config.json . + +We'll discuss the structure of this file later in more detail. For now, let us remark that the bulk of this file consists of a list of known TON DHT nodes required for the bootstrapping of the TON Network. A smaller section near the end of this file describes the particular instance of the TON Blockchain that we wish to connect to. + +All instances of the TON Blockchain use the same "global" TON Network (i.e., the TON Network is not fragmented into several instances for each blockchain instance). While the global network configuration is therefore independent of the particular TON Blockchain instance chosen, the Full Nodes belonging to different instances will later connect to different overlay subnetworks inside the TON Network. + +It is important to distinguish this "global configuration file", used for setting up a TON Blockchain Full Node, and the "local" or "automatic configuration file", automatically updated by validator-engine and usually stored in ${DB_ROOT}/config.json. The global configuration is used only once to generate the initial automatic configuration file, which is thereafter updated by validator-engine itself (e.g., by adding new DHT nodes or storing hashes of newer masterchain blocks). + +5. Initializing the local configuration +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Once the global configuration file is downloaded, it can be used to create the initial local configuration in ${DB_ROOT}/config.json. To do this, execute validator-engine once: + +$ validator-engine -C /var/ton-work/etc/ton-global.config.json --db /var/ton-work/db/ --ip : -l /var/ton-work/log + +Here `/var/ton-work/log` is the log directory of `validator-engine`, where it will create its log files. The argument to the `-C` command-line option is the global configuration file downloaded from test.ton.org as explained above, and `/var/ton-work/db/` is the working directory ${DB_ROOT}. Finally, : are the global IP address of this full node (you need to indicate a public IPv4 address here) and the UDP port used to run TON Network protocols such as ADNL and RLDP. Make sure that your firewall is configured to pass UDP packets with source or destination : at least for the `validator-engine` binary. + +When validator-engine is invoked as above, and ${DB_ROOT}/config.json does not exist, it creates a new local configuration file ${DB_ROOT}/config.json using the information from the global configuration file and from the command-line options such as --ip, and then exits. If ${DB_ROOT}/config.json already exists, it is not rewritten; instead the global configuration is ignored, and validator-engine starts up as a daemon using the local configuration. + +If you need to change the local configuration afterwards, you'll need to either delete this file and regenerate it from the global configuration (potentially forgetting other important information accumulated in the local configuration), or edit the local configuration in a text editor (when validator-engine is not running). + +6. Setting up remote control CLI +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +You will almost surely want to enable validator-engine-console in the local configuration, to be able to control your Full Node (i.e., validator-engine daemon) when it is running. For this, you'll need to generate two keypairs, one for the server (validator-engine) and one for the client (validator-engine-console). In the examples below we assume that validator-engine-console runs on the same machine and connects to validator-engine through the loopback network interface. (This is not necessarily so; you can use validator-engine-console for remote control as well.) + +As a first step, use the `generate-random-id` executable to create two keypairs, one for the server (on the machine running `validator-engine`) and one for the client (on the machine running `validator-engine-console`): + +$ ./generate-random-id -m keys -n server +6E9FD109F76E08B5710445C72D2C5FEDE04A96357DAA4EC0DDAEA025ED3AC3F7 bp/RCfduCLVxBEXHLSxf7eBKljV9qk7A3a6gJe06w/c= + +This utility generates a new keypair and saves the private key into file `server` and the public key into `server.pub`. The hexadecimal (6E9F...F7) and the base64 ("bp/RC...6wc/=") representations of the public key are displayed in the standard output, and are used henceforth to identify this key. + +We have to install the private key `server` into the keyring of the Full Node (validator-engine): + +$ mv server /var/ton-work/db/keyring/6E9FD109F76E08B5710445C72D2C5FEDE04A96357DAA4EC0DDAEA025ED3AC3F7 + +Notice that the file name to store this private key inside the keyring equals the hexadecimal identifier (which essentially is a hash of the public key) of this key. + +Next, we generate the client keypair: + +$ ./generate-random-id -m keys -n client +8BBA4F8FCD7CC4EF573B9FF48DC63B212A8E9292B81FC0359B5DBB8719C44656 i7pPj818xO9XO5/0jcY7ISqOkpK4H8A1m127hxnERlY= + +We obtain a client keypair, saved into files `client` and `client.pub`. This second operation should be run in the working directory of the client (validator-engine-console), possibly on another machine. + +Now we have to list the client's public key in the server's local configuration file ${DB_ROOT}/config.json. To do this, open the local configuration file in a text editor (after terminating validator-engine if it was running) and find the empty "control" section: + +"control": [ +] + +Replace it with the following: + +"control" : [ + { "id" : "bp/RCfduCLVxBEXHLSxf7eBKljV9qk7A3a6gJe06w/c=", + "port" : , + "allowed" : [ + { "id" : "i7pPj818xO9XO5/0jcY7ISqOkpK4H8A1m127hxnERlY=", + "permissions" : 15 + } + ] + } +], + +`control.0.id` is set to the base64 identifier of the server's public key, and `control.0.allowed.0.id` is the base64 identifier of the client's public key. is the UDP port the server will listen to for console commands. + +7. Running the Full Node +~~~~~~~~~~~~~~~~~~~~~~~~ + +To run the full node, simply run the validator-engine binary in a console: + +$ validator-engine --db ${DB_ROOT} -l /var/ton-work/log + +It will read the local configuration from ${DB_ROOT}/config.json, and continue running silently. You should write suitable scripts for invoking validator-engine as a daemon (so that it does not terminate when the console is closed), but we'll skip these considerations for simplicity. (Using "nohup" is a cheap way of achieving this on some Linux systems.) + +If the configuration is invalid, validator-engine will terminate immediately and, in most cases, output nothing. You'll have to study the log files under /var/ton-work/log to find out what went wrong. Otherwise, validator-engine will keep working silently. Again, you can understand what's going on by inspecting the log files, and by looking into subdirectories of the ${DB_ROOT} directory. + +If everything works as expected, validator-engine will locate other full nodes participating in the same instance of the TON Blockchain, and download recent blocks of the masterchain and all shardchains. (You can actually control the number of recent blocks to be downloaded, or even download them all starting from the zerostate---but this topic is outside the scope of this document; try running validator-engine with command-line option `-h` to find out the list of all available options with brief explanations). + +8. Using the Console CLI +~~~~~~~~~~~~~~~~~~~~~~~~ + +If the validator-engine-console has been set up as explained in Section 6., you can use it to connect to the running validator-engine (i.e., your Full Node) and run simple status and key management queries: + +$ ./validator-engine-console -k client -p server.pub -a : + +connecting to [:] +local key: 8BBA4F8FCD7CC4EF573B9FF48DC63B212A8E9292B81FC0359B5DBB8719C44656 +remote key: 6E9FD109F76E08B5710445C72D2C5FEDE04A96357DAA4EC0DDAEA025ED3AC3F7 +conn ready +> gettime +received validator time: time=1566568904 + +The `gettime` command obtains the current Unix time at the validator. If everything has been configured properly, you'll see an output similar to the one above. Notice that you need both the client's private key ("client") and the server's public key ("server.pub") for the console to work. You might wish to move them (especially the client's private key) into a separate directory with suitable permissions. + +Other console commands are available in validator-engine-console. For instance, `help` displays a list of all console commands with short descriptions. In particular, they are used to set up the Full Node as a Validator for the TON Blockchain, as explained in the separate document Validator-HOWTO. + +9. Setting up the Full Node as a Lite Server +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +You can set up your Full Node to function as a Lite Server, so that you can use the Lite Client to connect to it from the same or a remote host. For instance, sending the command `last` in a Lite Client connected to your Full Node will display the identifier of the most recent masterchain block known to your Full Node, so that you can inspect the progress of block downloading. You can also inspect the state of all smart contracts, send external messages (e.g., wallet queries), and so on as explained in the Lite Client HOWTO. + +In order to set up your Full Node as a Lite Server, you have to generate another keypair and install the private key into the server's keyring, similarly to what we did to enable the remote console: + +$ utils/generate-random-id -m keys -n liteserver +BDFEA84525ADB3B16D0192488837C04883C10FF1F4031BB6DEECDD17544F5347 vf6oRSWts7FtAZJIiDfASIPBD/H0Axu23uzdF1RPU0c= +mv liteserver ${DB_ROOT}/keyring/BDFEA84525ADB3B16D0192488837C04883C10FF1F4031BB6DEECDD17544F5347 + +After that, stop validator-engine if it is running and open the local configuration file ${TON_DB}/config.json in a text editor. Find the empty section + +"liteservers" : [ +] + +and replace it with a record containing the TCP port for listening to inbound Lite Client connections and the lite server's public key: + +"liteservers" : [ +{ +"id" : "vf6oRSWts7FtAZJIiDfASIPBD/H0Axu23uzdF1RPU0c=", +"port" : +} +], + +Now start `validator-engine` again. If it does not terminate immediately, it is likely that you have re-configured it properly. Now you can use the lite-client binary (usually located as "lite-client/lite-client" with respect to the build directory) to connect to the Lite Server running as a part of your Full Node: + +$ lite-client -a : -p liteserver.pub + +Again, `help` lists all commands available in the Lite Client. The Lite Client HOWTO contains some examples of what can be done with the Lite Client. diff --git a/doc/LiteClient-HOWTO b/doc/LiteClient-HOWTO new file mode 100644 index 00000000..bff3b124 --- /dev/null +++ b/doc/LiteClient-HOWTO @@ -0,0 +1,485 @@ +The aim of this document is to provide step-by-step instructions for compiling and creating a simple smart contract (a simple wallet) in the TON Blockchain Test Network using the TON Blockchain Lite Client and associated software. + +Download and installation instructions may be found in README. We assume here that the Lite Client is already properly downloaded, compiled and installed. + +1. Smart-contract addresses +~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Smart-contract addresses in the TON Network consist of two parts: (a) the workchain ID (a signed 32-bit integer) and (b) the address inside the workchain (64-512 bits depending on the workchain). Currently, only the masterchain (workchain_id=-1) and occasionally the basic workchain (workchain_id=0) are running in the TON Blockchain Test Network. Both of them have 256-bit addresses, so we henceforth assume that workchain_id is either 0 or -1 and that the address inside the workchain is exactly 256-bit. + +Under the conditions stated above, the smart-contract address can be represented in the following forms: + +A) "Raw": :<64 hexadecimal digits with address> +B) "User-friendly", which is obtained by first generating: +- one tag byte (0x11 for "bounceable" addresses, 0x51 for "non-bounceable"; add +0x80 if the address should not be accepted by software running in the production network) +- one byte containing a signed 8-bit integer with the workchain_id (0x00 for the basic workchain, 0xff for the masterchain) +- 32 bytes containing 256 bits of the smart-contract address inside the workchain (big-endian) +- 2 bytes containing CRC16-CCITT of the previous 34 bytes + +In case B), the 36 bytes thus obtained are then encoded using base64 (i.e., with digits, upper- and lowercase Latin letters, '/' and '+') or base64url (with '_' and '-' instead of '/' and '+'), yielding 48 printable non-space characters. + +Example: + +The "test giver" (a special smart contract residing in the masterchain of the Test Network that gives up to 20 test Grams to anybody who asks) has the address + +-1:8156775b79325e5d62e742d9b96c30b6515a5cd2f1f64c5da4b193c03f070e0d + +in the "raw" form (notice that uppercase Latin letters 'A'..'F' may be used instead of 'a'..'f') + +and + +Ef+BVndbeTJeXWLnQtm5bDC2UVpc0vH2TF2ksZPAPwcODSkb (base64) or +Ef-BVndbeTJeXWLnQtm5bDC2UVpc0vH2TF2ksZPAPwcODSkb (base64url) + + +in the "user-friendly" form (to be displayed by user-friendly clients). Notice that both forms (base64 and base64url) are valid and must be accepted. + +2. Inspecting the state of a smart contract +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Inspecting the state of smart contracts with the aid of the TON Lite Client is easy. For the sample smart contract described above, you would run the Lite Client and enter the following commands: + +> last +... +> getaccount -1:8156775b79325e5d62e742d9b96c30b6515a5cd2f1f64c5da4b193c03f070e0d +or +> getaccount Ef-BVndbeTJeXWLnQtm5bDC2UVpc0vH2TF2ksZPAPwcODSkb + +You will see something like this: + +------------------------------------ +got account state for -1:8156775B79325E5D62E742D9B96C30B6515A5CD2F1F64C5DA4B193C03F070E0D with respect to blocks (-1,8000000000000000,1645):4D5E1B928490BE34A4F03C9BC996661B8AD4E988F1460DB69BE3634B1E843DAF:9D85BB116D1DA4BFD999E9092BBD43B3A9F96EC9C9ED7AFDC81B27B08D3861A0 and (-1,8000000000000000,1645):4D5E1B928490BE34A4F03C9BC996661B8AD4E988F1460DB69BE3634B1E843DAF:9D85BB116D1DA4BFD999E9092BBD43B3A9F96EC9C9ED7AFDC81B27B08D3861A0 +account state is (account + addr:(addr_std + anycast:nothing workchain_id:-1 address:x8156775B79325E5D62E742D9B96C30B6515A5CD2F1F64C5DA4B193C03F070E0D) + storage_stat:(storage_info + used:(storage_used + cells:(var_uint len:1 value:3) + bits:(var_uint len:2 value:539) + public_cells:(var_uint len:0 value:0)) last_paid:0 + due_payment:nothing) + storage:(account_storage last_trans_lt:0 + balance:(currencies + grams:(nanograms + amount:(var_uint len:7 value:1000000000000000)) + other:(extra_currencies + dict:hme_empty)) + state:(account_active + ( + split_depth:nothing + special:nothing + code:(just + value:(raw@^Cell + x{} + x{FF0020DDA4F260D31F01ED44D0D31FD166BAF2A1F80001D307D4D1821804A817C80073FB0201FB00A4C8CB1FC9ED54} + )) + data:(just + value:(raw@^Cell + x{} + x{00000000} + )) + library:hme_empty)))) +x{CFF8156775B79325E5D62E742D9B96C30B6515A5CD2F1F64C5DA4B193C03F070E0D2068086C0000000000000000000000001C0E35FA931A000134_} + x{FF0020DDA4F260D31F01ED44D0D31FD166BAF2A1F80001D307D4D1821804A817C80073FB0201FB00A4C8CB1FC9ED54} + x{00000000} +------------------------------------ + +The first information line "got account state ... for ..." shows the account address and the masterchain block identifier with respect to which the account state has been dumped. Notice that even if the account state changes in a subsequent block, the `getaccount xxx` command will return the same result until the reference block is updated to a newer value by a `last` command. In this way one can study the state of all accounts and obtain consistent results. + +The "account state is (account ... " line begins the pretty-printed deserialized view of the account state. It is a deserialization of TL-B data type Account, used to represent account states in the TON Blockchain as explained in the TON Blockchain documentation. (You can find the TL-B scheme used for deserialization in the source file crypto/block/block.tlb; notice that if the scheme is out of date, the deserialization may break down.) + +Finally, the last several lines beginning with x{CFF538... (the "raw dump") contain the same information displayed as a tree of cells. In this case, we have one root cell containing the data bits CFF...134_ (the underscore means that the last binary one and all subsequent binary zeroes are to be removed, so hexadecimal "4_" corresponds to binary "0"), and two cells that are its children (displayed with one-space indentation). + +We can see that x{FF0020DDA4F260...} is the code of this smart contract. If we consult the Appendix A of the TON Virtual Machine documentation, we can even disassemble this code: FF00 is SETCP 0, 20 is DUP, DD is IFNOTRET, A4 is INC, F260 is THROWIF 32, and so on. (Incidentally, you can find the source code of this smartcontract in the source file crypto/block/mc0.fif .) + +We can also see that x{00000000} (the actual value you see may be different) is the persistent data of this smart contract. It is actually an unsigned 32-bit integer, used by the smart contract as the counter of operations performed so far. Notice that this value is big-endian (i.e., 3 is encoded as x{00000003}, not as x{03000000}), as are all integers inside the TON Blockchain. + +The current balance of the smart contract is easily seen in the pretty-printed portion of the output. In this case, we see ... balance:(currencies:(grams:(nanograms:(... value:1000000000000000...)))), which is the balance of the account in (test) nanograms (a million test Grams in this example; the actual number you see may be smaller). If you study the TL-B scheme provided in crypto/block/scheme.tlb, you will be able to find this number (10^15) in binary big-endian form in the raw dump portion as well (it is located near the end of the data bits of the root cell). + +3. Compiling a new smart contract +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Before uploading a new smart contract into the TON Blockchain, you need to determine its code and data and save them in serialized form into a file (called a "bag-of-cells" or BOC file, usually with a .boc suffix). Let us consider the case of a simple wallet smart contract, which stores a 32-bit operations counter and a 256-bit Ed25519 public key of its owner in its persistent data. + +Obviously, you'll need some tools for developing smart contracts - namely, a TON smart contract compiler. Basically, a TON smart contract compiler is a program that reads the source of a smart contract in a specialized high-level programming language and creates a .boc file from this source. + +One such tool is the Fift interpreter, which is included in this distribution and can help create simple smart contracts. You may wish to develop more sophisticated tools. However, Fift is sufficient for demonstration purposes. + +Create the file `new-wallet.fif` containing the source of our new smart contract: + +------------------------------------ +#!/usr/bin/fift -s +"Asm.fif" include + +0 constant wc // create a wallet in workchain 0 (basechain) + +// Create new simple wallet +<{ SETCP0 DUP IFNOTRET INC 32 THROWIF // return if recv_internal, fail unless recv_external + 512 INT LDSLICEX DUP 32 PLDU // sign cs cnt + c4 PUSHCTR CTOS 32 LDU 256 LDU ENDS // sign cs cnt cnt' pubk + s1 s2 XCPU // sign cs cnt pubk cnt' cnt + EQUAL 33 THROWIFNOT // ( seqno mismatch? ) + s2 PUSH HASHSU // sign cs cnt pubk hash + s0 s4 s4 XC2PU // pubk cs cnt hash sign pubk + CHKSIGNU // pubk cs cnt ? + 34 THROWIFNOT // signature mismatch + ACCEPT + SWAP 32 LDU NIP + DUP SREFS IF:<{ + 8 LDU LDREF // pubk cnt mode msg cs + s0 s2 XCHG SENDRAWMSG // pubk cnt cs ; ( message sent ) + }> + ENDS + INC NEWC 32 STU 256 STU ENDC c4 POPCTR +}>c +// code +file + B, +b> // data +// no libraries + // create StateInit +dup ."StateInit: " $ type cr +256 u>B "new-wallet.addr" B>file + +dup ."signing message: " +dup ."External message for initialization is " B dup Bx. cr +"new-wallet-query.boc" tuck B>file +."(Saved to file " type .")" cr +-------------------------------------------- + +Incidentally, you can find a more sophisticated version of this sample file in crypto/smartcont/new-wallet.fif. It accepts command-line arguments, so you don't need to edit the source file each time you want to create a new wallet. + +Now, provided that you have compiled Fift binary (usually located as "crypto/fift" with respect to the build directory), you can run + +crypto/fift -I/crypto/fift/lib new-wallet.fif + +assuming that you have copied new-wallet.fif into the current directory. Alternatively, you might skip the source editing phase and simply run + +crypto/fift -I/crypto/fift/lib -s /smartcont/new-wallet.fif 0 my_wallet_name + +where 0 is the workchain to contain the new wallet (0 = basechain, -1 = masterchain), `my_wallet_name` is any identifier you wish to be associated with this wallet. The address of the new wallet will be saved into file `my_wallet_name.addr`, its newly-generated private key will be saved to `my_wallet_name.pk` (unless this file already exists; then the key will be loaded from this file instead), and the external message will be saved into my_new_wallet-query.boc. If you do not indicate the name of your wallet (`my_wallet_name` in the example above), the default name `new-wallet` is used. + +You may wish to set the FIFTPATH environment variable to /crypto/fift/lib, the directory containing Fift.fif and Asm.fif library files; then you can omit the -I argument to the Fift interpreter. + +If everything worked, you'll see something like the following + +-------------------------------------------- +StateInit: x{34_} + x{FF0020DDA4F260810200D71820D70B1FED44D0D7091FD709FFD15112BAF2A122F901541044F910F2A2F80001D7091F3120D74A97D70907D402FB00DED1A4C8CB1FCBFFC9ED54} + x{00000000F61CF0BC8E891AD7636E0CD35229D579323AA2DA827EB85D8071407464DC2FA3} + +new wallet address = -1 : 60c04141c6a7b96d68615e7a91d265ad0f3a9a922e9ae9c901d4fa83f5d3c0d0 +0f9gwEFBxqe5bWhhXnqR0mWtDzqaki6a6ckB1PqD9dPA0EKD +signing message: x{00000000} + +External message for initialization is x{89FEC18082838D4F72DAD0C2BCF523A4CB5A1E7535245D35D39203A9F507EBA781A0119401748E6F89C1BA026A363C9F58765508DFF6854475357210D0D69F07C3A5453CEEDF1A0383FC405B57FF10CE060C2377BDD954A336DE5161F0AC1C61084180E00000001_} + x{FF0020DDA4F260810200D71820D70B1FED44D0D7091FD709FFD15112BAF2A122F901541044F910F2A2F80001D7091F3120D74A97D70907D402FB00DED1A4C8CB1FCBFFC9ED54} + x{00000000F61CF0BC8E891AD7636E0CD35229D579323AA2DA827EB85D8071407464DC2FA3} + +B5EE9C724104030100000000DA0002CF89FEC18082838D4F72DAD0C2BCF523A4CB5A1E7535245D35D39203A9F507EBA781A0119401748E6F89C1BA026A363C9F58765508DFF6854475357210D0D69F07C3A5453CEEDF1A0383FC405B57FF10CE060C2377BDD954A336DE5161F0AC1C61084180E0000000100102008CFF0020DDA4F260810200D71820D70B1FED44D0D7091FD709FFD15112BAF2A122F901541044F910F2A2F80001D7091F3120D74A97D70907D402FB00DED1A4C8CB1FCBFFC9ED54004800000000F61CF0BC8E891AD7636E0CD35229D579323AA2DA827EB85D8071407464DC2FA32DB9BE63 +(Saved to file new-wallet-query.boc) +-------------------------------------------- + +In a nutshell, the Fift assembler (loaded by the "Asm.fif" include line) is used to compile the source code of the smart contract (contained in <{ SETCP0 ... c4 POPCTR }> lines) into its internal representation. The initial data of the smart contract is also created (by lines), containing a 32-bit sequence number (equal to zero) and a 256-bit public key from a newly-generated Ed25519 keypair. The corresponding private key is saved into the file `new-wallet.pk` (be careful not to run this code twice in the same directory, otherwise this private key file will be overwritten). + +The code and data for the new smart contract are combined into a StateInit structure (in the next lines), the address of the new smart contract (equal to the hash of this StateInit structure) is computed and output, and then an external message with a destination address equal to that of the new smart contract is created. This external message contains both the correct StateInit for the new smart contract and a non-trivial payload (signed by the correct private key). + +Finally, the external message is serialized into a bag of cells (represented by B5EE...BE63) and saved into the file `new-wallet-query.boc`. Essentially, this file is your compiled smart contract with all additional information necessary to upload it into the TON Blockchain. + +4. Transferring some funds to the new smart contract +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +You might try to upload the new smart contract immediately by running the Lite Client and typing + +> sendfile new-wallet-query.boc + +or + +> sendfile my_new_wallet-query.boc + +if you chose to name your wallet `my_new_wallet`. + +Unfortunately, this won't work, because smart contracts must have a positive balance to be able to pay for storing and processing their data in the blockchain. So you have to transfer some funds to your new smart contract address first, displayed during its generation as -1:60c0...c0d0 (in raw form) and 0f9..EKD (in user-friendly form). + +In a real scenario, you would either transfer some Grams from your already existing wallet, ask a friend to do so, or buy some Grams at a cryptocurrency exchange, indicating 0f9...EKD as the account to transfer the new Grams to. + +In the Test Network, you have another option: you can ask the "test giver" to give you some test Grams (up to 20). Let us explain how to do it. + +5. Using the test giver smart contract +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +You need to know the address of the test giver smart contract. We'll assume that it is -1:8156775b79325e5d62e742d9b96c30b6515a5cd2f1f64c5da4b193c03f070e0d, or, equivalently, Ef-BVndbeTJeXWLnQtm5bDC2UVpc0vH2TF2ksZPAPwcODSkb, as indicated in one of the previous examples. You inspect the state of this smart contract in the Lite Client by typing + +> last +> getaccount Ef-BVndbeTJeXWLnQtm5bDC2UVpc0vH2TF2ksZPAPwcODSkb + +as explained above in Section 2. The only number you need from the output is the 32-bit sequence number stored in the smart contract data (it is zero in the example above, but generally it will be non-zero). + +Next, you create an external message to the test giver asking it to send another message to your (uninitialized) smart contract carrying a specified amount of test Grams. There is a special Fift source file for generating this external message, a more sophisticated version of which is located at crypto/smartcont/testgiver.fif: + +-------------------------------------------- +// "testgiver.addr" file>B 256 B>u@ +0x8156775b79325e5d62e742d9b96c30b6515a5cd2f1f64c5da4b193c03f070e0d +dup constant wallet_addr ."Test giver address = " x. cr + +0x60c04141c6a7b96d68615e7a91d265ad0f3a9a922e9ae9c901d4fa83f5d3c0d0 +constant dest_addr + +-1 constant wc +0 constant seqno + +1000000000 constant Gram +{ Gram swap */ } : Gram*/ + +6.666 Gram*/ constant amount + +// b x --> b' ( serializes a Gram amount ) +{ -1 { 1+ 2dup 8 * ufits } until + rot over 4 u, -rot 8 * u, } : Gram, + +// create a message (NB: 01b00.., b = bounce) + + +dup ."enveloping message: " +dup ."resulting external message: " B dup Bx. cr +"wallet-query.boc" B>file +--------------------------------------------- + +In general, you will need to edit the line containing the destination address (0x60c0...c0d0 in our example) and the sequence number of the test giver (the "0" in the "0 constant seqno" line). Alternatively, you can pass the required parameters as command-line arguments to + + fift -I -s [] + +For instance, + + fift -I/crypto/fift/lib:/crypto/smartcont -s testgiver.fif 0f9gwEFBxqe5bWhhXnqR0mWtDzqaki6a6ckB1PqD9dPA0EKD 6.666 wallet-query + +(Again, setting FIFTPATH to /crypto/fift/lib:/crypto/smartcont and installing fift binary as /usr/bin/fift is a good idea.) + +The newly-created message to the new smart contract must have its bounce bit clear, otherwise the transfer will be "bounced" to its sender. + +This Fift code creates an internal message from the test giver smart contract to the address of our new smart contract carrying 6.666 test Grams (you can enter any other amount here up to approximately 20 Grams). Then this message is enveloped into an external message addressed to the test giver; this external message must also contain the correct sequence number of the test giver. When the test giver receives such an external message, it checks whether the sequence number matches the one stored in its persistent data, and if it does, sends the embedded internal message with the required amount of test Grams to its destination (our smart contract in this case). + +The external message is serialized and saved into the file `wallet-query.boc`. Some output is generated in the process: + +--------------------------------------------- +Test giver address = 8156775b79325e5d62e742d9b96c30b6515a5cd2f1f64c5da4b193c03f070e0d +enveloping message: x{0000000001} + x{427FB06020A0E353DCB6B430AF3D48E932D6879D4D49174D74E480EA7D41FAE9E068280C6A98B4000000000000000000000000000047494654} + +resulting external message: x{89FEA71F4F9849FF1D54203B094BE356FD065FC3B0966139BFDE9DD286E755901EFA00000000000C_} + x{427FB06020A0E353DCB6B430AF3D48E932D6879D4D49174D74E480EA7D41FAE9E068280C6A98B4000000000000000000000000000047494654} + +B5EE9C7241040201000000006600014F89FEA71F4F9849FF1D54203B094BE356FD065FC3B0966139BFDE9DD286E755901EFA00000000000C010072427FB06020A0E353DCB6B430AF3D48E932D6879D4D49174D74E480EA7D41FAE9E068280C6A98B40000000000000000000000000000474946545D6254A9 +--------------------------------------------- + +6. Uploading the external message to the test giver smart contract +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Now we can invoke the Lite Client, check the state of the test giver (if the sequence number has changed, our external message will fail), and then type + +> sendfile wallet-query.boc + +We will see some output: +... external message status is 1 + +which means that the external message has been delivered to the collator pool. Afterwards one of the collators might choose to include this external message in a block, creating a transaction for the test giver smart contract to process this external message. We can check whether the state of the test giver has changed: + +> last +> getaccount Ef-BVndbeTJeXWLnQtm5bDC2UVpc0vH2TF2ksZPAPwcODSkb + +(If you forget to type `last`, you are likely to see the unchanged state of the test giver smart contract.) The resulting output would be: + +--------------------------------------------- +got account state for -1:8156775B79325E5D62E742D9B96C30B6515A5CD2F1F64C5DA4B193C03F070E0D with respect to blocks (-1,8000000000000000,10441):2DCFB7F734913261B85B8866DFF7CBEF07205EAA0769F4EE15E242AB520A2CC5:4F96F417BCD74D5DAE0CC3CCC285E513B85652002D9AD8CC884781D8465E3591 and (-1,8000000000000000,10441):2DCFB7F734913261B85B8866DFF7CBEF07205EAA0769F4EE15E242AB520A2CC5:4F96F417BCD74D5DAE0CC3CCC285E513B85652002D9AD8CC884781D8465E3591 +account state is (account + addr:(addr_std + anycast:nothing workchain_id:-1 address:x8156775B79325E5D62E742D9B96C30B6515A5CD2F1F64C5DA4B193C03F070E0D) + storage_stat:(storage_info + used:(storage_used + cells:(var_uint len:1 value:3) + bits:(var_uint len:2 value:539) + public_cells:(var_uint len:0 value:0)) last_paid:0 + due_payment:nothing) + storage:(account_storage last_trans_lt:10697000003 + balance:(currencies + grams:(nanograms + amount:(var_uint len:7 value:999993280210000)) + other:(extra_currencies + dict:hme_empty)) + state:(account_active + ( + split_depth:nothing + special:nothing + code:(just + value:(raw@^Cell + x{} + x{FF0020DDA4F260D31F01ED44D0D31FD166BAF2A1F80001D307D4D1821804A817C80073FB0201FB00A4C8CB1FC9ED54} + )) + data:(just + value:(raw@^Cell + x{} + x{00000001} + )) + library:hme_empty)))) +x{CFF8156775B79325E5D62E742D9B96C30B6515A5CD2F1F64C5DA4B193C03F070E0D2068086C00000000000000009F65D110DC0E35F450FA914134_} + x{FF0020DDA4F260D31F01ED44D0D31FD166BAF2A1F80001D307D4D1821804A817C80073FB0201FB00A4C8CB1FC9ED54} + x{00000001} +--------------------------------------------- + +You may notice that the sequence number stored in the persistent data has changed (in our example, to one), and the last_trans_lt field (the logical time of the last transaction of this account) has been increased. + +Now we can inspect the state of our new smart contract: + +> getaccount 0f9gwEFBxqe5bWhhXnqR0mWtDzqaki6a6ckB1PqD9dPA0EKD +or +> getaccount -1:60c04141c6a7b96d68615e7a91d265ad0f3a9a922e9ae9c901d4fa83f5d3c0d0 + +Now we see + +--------------------------------------------- +got account state for -1:60C04141C6A7B96D68615E7A91D265AD0F3A9A922E9AE9C901D4FA83F5D3C0D0 with respect to blocks (-1,8000000000000000,16481):890F4D549428B2929F5D5E0C5719FBCDA60B308BA4B907797C9E846E644ADF26:22387176928F7BCEF654411CA820D858D57A10BBF1A0E153E1F77DE2EFB2A3FB and (-1,8000000000000000,16481):890F4D549428B2929F5D5E0C5719FBCDA60B308BA4B907797C9E846E644ADF26:22387176928F7BCEF654411CA820D858D57A10BBF1A0E153E1F77DE2EFB2A3FB +account state is (account + addr:(addr_std + anycast:nothing workchain_id:-1 address:x60C04141C6A7B96D68615E7A91D265AD0F3A9A922E9AE9C901D4FA83F5D3C0D0) + storage_stat:(storage_info + used:(storage_used + cells:(var_uint len:1 value:1) + bits:(var_uint len:1 value:111) + public_cells:(var_uint len:0 value:0)) last_paid:1553210152 + due_payment:nothing) + storage:(account_storage last_trans_lt:16413000004 + balance:(currencies + grams:(nanograms + amount:(var_uint len:5 value:6666000000)) + other:(extra_currencies + dict:hme_empty)) + state:account_uninit)) +x{CFF60C04141C6A7B96D68615E7A91D265AD0F3A9A922E9AE9C901D4FA83F5D3C0D02025BC2E4A0D9400000000F492A0511406354C5A004_} +--------------------------------------------- + +Our new smart contract has some positive balance (of 6.666 test Grams), but has no code or data (reflected by `state:account_uninit`). + +7. Uploading the code and data of the new smart contract +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Now you can finally upload the external message with the StateInit of the new smart contract, containing its code and data: + +--------------------------------------------- +> sendfile new-wallet-query.boc +... external message status is 1 +> last +... +> getaccount -1:60c04141c6a7b96d68615e7a91d265ad0f3a9a922e9ae9c901d4fa83f5d3c0d0 +... +got account state for -1:60C04141C6A7B96D68615E7A91D265AD0F3A9A922E9AE9C901D4FA83F5D3C0D0 with respect to blocks (-1,8000000000000000,16709):D223B25D8D68401B4AA19893C00221CF9AB6B4E5BFECC75FD6048C27E001E0E2:4C184191CE996CF6F91F59CAD9B99B2FD5F3AA6F55B0B6135069AB432264358E and (-1,8000000000000000,16709):D223B25D8D68401B4AA19893C00221CF9AB6B4E5BFECC75FD6048C27E001E0E2:4C184191CE996CF6F91F59CAD9B99B2FD5F3AA6F55B0B6135069AB432264358E +account state is (account + addr:(addr_std + anycast:nothing workchain_id:-1 address:x60C04141C6A7B96D68615E7A91D265AD0F3A9A922E9AE9C901D4FA83F5D3C0D0) + storage_stat:(storage_info + used:(storage_used + cells:(var_uint len:1 value:3) + bits:(var_uint len:2 value:963) + public_cells:(var_uint len:0 value:0)) last_paid:1553210725 + due_payment:nothing) + storage:(account_storage last_trans_lt:16625000002 + balance:(currencies + grams:(nanograms + amount:(var_uint len:5 value:5983177000)) + other:(extra_currencies + dict:hme_empty)) + state:(account_active + ( + split_depth:nothing + special:nothing + code:(just + value:(raw@^Cell + x{} + x{FF0020DDA4F260810200D71820D70B1FED44D0D7091FD709FFD15112BAF2A122F901541044F910F2A2F80001D7091F3120D74A97D70907D402FB00DED1A4C8CB1FCBFFC9ED54} + )) + data:(just + value:(raw@^Cell + x{} + x{00000001F61CF0BC8E891AD7636E0CD35229D579323AA2DA827EB85D8071407464DC2FA3} + )) + library:hme_empty)))) +x{CFF60C04141C6A7B96D68615E7A91D265AD0F3A9A922E9AE9C901D4FA83F5D3C0D020680F0C2E4A0EB280000000F7BB57909405928024A134_} + x{FF0020DDA4F260810200D71820D70B1FED44D0D7091FD709FFD15112BAF2A122F901541044F910F2A2F80001D7091F3120D74A97D70907D402FB00DED1A4C8CB1FCBFFC9ED54} + x{00000001F61CF0BC8E891AD7636E0CD35229D579323AA2DA827EB85D8071407464DC2FA3} +--------------------------------------------- + +You will see that the smart contract has been initialized using code and data from the StateInit of the external message, and its balance has been slightly decreased because of the processing fees. Now it is up and running, and you can activate it by generating new external messages and uploading them to the TON Blockchain using the "sendfile" command of the Lite Client. + +8. Using the simple wallet smart contract +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Actually, the simple wallet smart contract used in this example can be used to transfer test Grams to any other accounts. It is in this respect similar to the test giver smart contract discussed above, with the difference that it processes only external messages signed by the correct private key (of its owner). In our case, it is the private key saved into the file "new-wallet.pk" during the compilation of the smart contract (see Section 3). + +An example of how you might use this smart contract is provided in sample file crypto/smartcont/wallet.fif : + +-------------------------------------------------------- +#!/usr/bin/fift -s +def? $1 { "new-wallet" =: $1 } ifnot +$1 $len { "new-wallet" =: $1 } ifnot +$1 +".addr" file>B 256 B>u@ dup constant wallet_addr +."Wallet address = " x. cr +$1 +".pk" file>B dup Blen 32 <> abort"Private key must be exactly 32 bytes long" +constant wallet_pk + +// 0x1111111122222222333333334444444455555555666666667777777788888888 constant dest_addr +0x13CB612A00A7C092C7DFD2EA45D603A9B54591BA4C88F71E707E009B879F0FB2 constant dest_addr +-1 constant wc +0 constant seqno + +1000000000 constant Gram +{ Gram swap */ } : Gram*/ + +.666 Gram*/ constant amount + +// b x --> b' ( serializes a Gram amount ) +{ -1 { 1+ 2dup 8 * ufits } until + rot over 4 u, -rot 8 * u, } : Gram, + +// create a message + + +dup ."signing message: " +dup ."resulting external message: " B dup Bx. cr +$1 +"-query.boc" B>file +------------------------------------- + +You can hard-code the address of your smart contract here by changing the fourth line to, say, + +0x60c04141c6a7b96d68615e7a91d265ad0f3a9a922e9ae9c901d4fa83f5d3c0d0 dup constant wallet_addr + +You will also need to change the destination address, the name of the file with the private key, the sequence number (it will be 1 immediately after the smart contract is initialized; the actual value can always be retrieved by inspecting the current account state) and the Gram amount to be transferred. The payload of the internal message contains 32 bits with the string "TEST"; it can be changed to something more useful if necessary. + +Alternatively, you can use the more sophisticated version of this script provided in crypto/smartcont/wallet.fif in the source tree, and pass all the required parameters as command-line arguments: + + fift -I/crypto/fift/lib:/crypto/smartcont -s wallet.fif + +For example, + + fift -I/crypto/fift/lib:/crypto/smartcont -s wallet.fif my_new_wallet kf8Ty2EqAKfAksff0upF1gOptUWRukyI9x5wfgCbh58Pss9j 1 .666 + +Here `my_new_wallet` is the identifier of your wallet used before with new-wallet.fif; the address and the private key of your test wallet will be loaded from files `my_new_wallet.addr` and `my_new_wallet.pk` in the current directory. + +When you run this code (by invoking the Fift interpreter), you create an external message with a destination equal to the address of your wallet smart contract, containing a correct Ed25519 signature, a sequence number, and an enveloped internal message from your wallet smart contract to the smart contract indicated in dest_addr, with an arbitrary value attached and an arbitrary payload. When your smart contract receives and processes this external message, it first checks the signature and the sequence number. If they are correct, it accepts the external message, sends the embedded internal message from itself to the intended destination, and increases the sequence number in its persistent data (this is a simple measure to prevent replay attacks, in case this sample wallet smart contract code ends up used in a real wallet application). + +Of course, a true TON Blockchain wallet application would hide all the intermediate steps explained above. It would first communicate the address of the new smart contract to the user, asking them to transfer some funds to the indicated address (displayed in its non-bounceable user-friendly form) from another wallet or a cryptocurrency exchange, and then would provide a simple interface to display the current balance and to transfer funds to whatever other addresses the user wants. (The aim of this document is to explain how to create new non-trivial smart contracts and experiment with the TON Blockchain Test Network, rather than to explain how one could use the Lite Client instead of a more user-friendly wallet application.) + +One final remark: The above examples used smart contracts in the basic workchain (workchain 0). They would work in exactly the same way in the masterchain (workchain -1), if one changes the "0 constant wc" to "-1 constant wc" in relevant places. The only difference is that the processing and storage fees in the basic workchain are 10-100 times lower than in the masterchain. Some smart contracts (such as the validator election smart contract) accept transfers only from masterchain smart contracts, so you'll need a wallet in the masterchain if you wish to make stakes on behalf of your own validator and participate in the elections. diff --git a/doc/Validator-HOWTO b/doc/Validator-HOWTO new file mode 100644 index 00000000..f67a4bfd --- /dev/null +++ b/doc/Validator-HOWTO @@ -0,0 +1,234 @@ +The aim of this document is to provide step-by-step instructions for setting up a full node for the TON Blockchain as a validator. We assume that a TON Blockchain Full Node is already up and running as explained in FullNode-HOWTO. We also assume some familiarity with the TON Blockchain Lite Client. + +Notice that a validator must be run on a dedicated high-performance server with high network bandwidth installed in a reliable datacenter, and that you'll need a large amount of Grams (test Grams, if you want to run a validator in the "testnet") as stakes for your validator. If your validator works incorrectly or is not available for prolonged periods of time, you may lose part or all of your stake, so it makes sense to use high-performance reliable servers. + +0. Downloading and compiling +~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +The basic instructions are the same as for a TON Blockchain Full Node, as explained in FullNode-HOWTO. In fact, any Full Node will automatically work as a validator if it discovers that the public key corresponding to its private key appears as a member of the current validator set for the currently selected TON Blockchain instance. In particular, the Full Node and the Validator use the same binary file validator-engine, and are controlled by means of the same validator-engine-console. + +1. Controlling smart contract of a validator +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +In order to run a Validator, you'll need a Full Node that is already up and running (and completely synchronized with the current blockchain state), and a wallet in the masterchain holding large amounts of Grams (or test Grams, if you want to run a validator in the "testnet" TON Blockchain instance). Typically you'll need at least 100,000 Grams. + +Each validator is identified by its (Ed25519) public key. During the validator elections, the validator (or rather its public key) is also associated with a smart contract residing in the masterchain. For simplicity, we say that the validator is "controlled" by this smart contract (e.g., a wallet smart contract). Stakes are accepted on behalf of this validator only if they arrive from its associated smart contract, and only that associated smart contract is entitled to collect the validator stake after it is unfrozen, along with the validator's share of bonuses (block mining fees, transaction and message forwarding fees collected from the users of the TON Blockchain by the validator pool). Typically the bonuses are distributed proportionally to the (effective) stakes of the validators. On the other hand, validators with higher stakes get a larger amount of work to perform (have to create and validate blocks for more shardchains), so it is important not to get too greedy. + +Notice that each validator (identified by its public key) can be associated with at most one controlling smartcontract (residing in the masterchain), but the same controlling smart contract may be associated with several validators. In this way you can run several validators (on different physical servers) and make stakes for them from the same smart contract. If one of these validators breaks down and you lose its stake, at least the other validators will continue working and will keep their stakes and receive bonuses. + +2. Creating the controlling smart contract +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +If you don't have a controlling smart contract, you can simply create a wallet in the masterchain. A simple wallet can be created with the aid of the script new-wallet.fif, located in the subdirectory crypto/smartcont of the source tree. In what follows, we assume that you have configured the environment variable FIFTPATH to include /crypto/fift/lib:/crypto/smartcont, and that your PATH includes a directory with the Fift binary (located as /crypto/fift). Then you can simply run + +$ fift -s new-wallet.fif -1 my_wallet_id + +where "my_wallet_id" is any identifier you want to assign to your new wallet, and -1 is the workchain identifier for the masterchain. If you have not set up FIFTPATH and PATH, then you'll have to run a longer version of this command + +$ crypto/fift -I /crypto/fift/lib:/crypto/smartcont -s new-wallet.fif -1 my_wallet_id + +in your build directory. + +Once you run this script, the address of the new smart contract is displayed: +... +new wallet address = -1:af17db43f40b6aa24e7203a9f8c8652310c88c125062d1129fe883eaa1bd6763 +(Saving address to file my_wallet_id.addr) +Non-bounceable address (for init): 0f-vF9tD9Atqok5yA6n4yGUjEMiMElBi0RKf6IPqob1nYzqK +Bounceable address (for later access): kf-vF9tD9Atqok5yA6n4yGUjEMiMElBi0RKf6IPqob1nY2dP +... +(Saved wallet creating query to file my_wallet_id-query.boc) + +Now my_wallet_id.pk is a new file containing the private key for controlling this wallet (you must keep it secret), and my_wallet_id.addr is a (not so secret) file containing the address of this wallet. Once this is done, you have to transfer some (test) Grams to the non-bounceable address of your wallet, and run "sendfile my_wallet_id-query.boc" in the Lite Client to finish creating the new wallet. This process is explained in more detail in the LiteClient-HOWTO. + +If you are running a validator in the "mainnet", it is a good idea to use more sophisticated wallet smart contracts, e.g., a multi-signature wallet. For the "testnet", the simple wallet should be enough. + +3. Elector smart contract +~~~~~~~~~~~~~~~~~~~~~~~~~ + +The elector smart contract is a special smart contract residing in the masterchain. Its full address is -1:xxx..xxx, where -1 is the workchain identifier (-1 corresponds to the masterchain), and xxx..xxx is the hexadecimal representation of its 256-bit address inside the masterchain. In order to find out this address, you have to read the configuration parameter #1 from a recent state of the blockchain. This is easily done by means of the command "getconfig 1" in the Lite Client: + +> getconfig 1 +ConfigParam(1) = ( elector_addr:xA4C2C7C05B093D470DE2316DBA089FA0DD775FD9B1EBFC9DC9D04B498D3A2DDA) +x{A4C2C7C05B093D470DE2316DBA089FA0DD775FD9B1EBFC9DC9D04B498D3A2DDA} + +In this case, the complete elector address is -1:A4C2C7C05B093D470DE2316DBA089FA0DD775FD9B1EBFC9DC9D04B498D3A2DDA + +We assume familiarity with the Lite Client and that you know how to run it and how to obtain a global configuration file for it. Notice that the above command can be run in batch mode by using '-c' command line option of the Lite Client: + +$ lite-client -C -c 'getconfig 1' +... +ConfigParam(1) = ( elector_addr:xA4C2C7C05B093D470DE2316DBA089FA0DD775FD9B1EBFC9DC9D04B498D3A2DDA) +x{A4C2C7C05B093D470DE2316DBA089FA0DD775FD9B1EBFC9DC9D04B498D3A2DDA} + +$ + +The elector smart contract is used for several things. Most importantly, you can participate in validator elections or collect unfrozen stakes and bonuses by sending messages from the controlling smart contract of your validator to the elector smart contract. You can also learn about current validator elections and their participants by invoking the so-called "get methods" of the elector smart contract. + +Namely, running +> runmethod -1:A4C2C7C05B093D470DE2316DBA089FA0DD775FD9B1EBFC9DC9D04B498D3A2DDA active_election_id +... +arguments: [ 86535 ] +result: [ 1567633899 ] + +(or lite-client -C -c "runmethod -1: active_election_id" in batch mode) will return the identifier of the currently active elections (a non-zero integer, typically the unixtime of the start of the service term of the validator group being elected), or 0, if no elections are currently active. In this example, the identifier of the active elections is 1567633899. + +You can also recover the list of all active participants (pairs of 256-bit validator public keys and their corresponding stakes expressed in nanograms) by running the method "participant_list" instead of "active_election_id". + +4. Creating a validator public key and ADNL address +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +In order to participate in validator elections, you need to know the elections identifier (obtained by running get-method "active_elections_id" of the elector smart contract), and also the public key of your validator. The public key is created by running validator-engine-console (as explained in FullNode-HOWTO) and running the following commands: + +$ validator-engine-console ... +... +conn ready +> newkey +created new key BCA335626726CF2E522D287B27E4FAFFF82D1D98615957DB8E224CB397B2EB67 +> exportpub BCA335626726CF2E522D287B27E4FAFFF82D1D98615957DB8E224CB397B2EB67 +got public key: xrQTSIQEsqZkWnoADMiBnyBFRUUweXTvzRQFqb5nHd5xmeE6 +> addpermkey BCA335626726CF2E522D287B27E4FAFFF82D1D98615957DB8E224CB397B2EB67 1567633899 1567733900 +success + +Now the full node (validator-engine) has generated a new keypair, exported the Base64 representation of the public key (xrQT...E6), and registered it as a persistent key for signing blocks starting from unixtime 1567633899 (equal to the election identifier) until 1567733900 (equal to the previous number plus the term duration of the validator set to be elected, available in configuration parameter #15, which can be learned by typing "getconfig 15" in the Lite Client, plus a safety margin in case elections actually happen later than intended). + +You also need to define a temporary key to be used by the validator to participate in the network consensus protocol. The simplest way (sufficient for testing purposes) is to set this key equal to the persistent (block signing) key: + +> addtempkey BCA335626726CF2E522D287B27E4FAFFF82D1D98615957DB8E224CB397B2EB67 BCA335626726CF2E522D287B27E4FAFFF82D1D98615957DB8E224CB397B2EB67 1567733900 +success + +It is also a good idea to create a dedicated ADNL address to be used exclusively for validator purposes: + +> newkey +created new key C5C2B94529405FB07D1DDFB4C42BFB07727E7BA07006B2DB569FBF23060B9E5C +> addadnl C5C2B94529405FB07D1DDFB4C42BFB07727E7BA07006B2DB569FBF23060B9E5C 0 +success +> addvalidatoraddr BCA335626726CF2E522D287B27E4FAFFF82D1D98615957DB8E224CB397B2EB67 C5C2B94529405FB07D1DDFB4C42BFB07727E7BA07006B2DB569FBF23060B9E5C 1567733900 +success + +Now C5C2B94529405FB07D1DDFB4C42BFB07727E7BA07006B2DB569FBF23060B9E5C is a new ADNL address, which will be used by the Full Node for running as a validator with the public key BCA...B67, with expiration time set to 1567733900. + +5. Creating an election participation request +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +The special script validator-elect-req.fif (located in /crypto/smartcont) is used to create a message that has to be signed by the validator in order to participate in the elections. It is run as follows: + +$ fift -s validator-elect-req.fif [] + +For example, + +$ fift -s validator-elect-req.fif kf-vF9tD9Atqok5yA6n4yGUjEMiMElBi0RKf6IPqob1nY2dP 1567633899 2.7 C5C2B94529405FB07D1DDFB4C42BFB07727E7BA07006B2DB569FBF23060B9E5C + +or, if you have created the controlling wallet by means of new-wallet.fif, you can use @my_wallet_id.addr instead of copying the wallet address kf-vF...dP: + +--------------------------------------- +$ fift -s validator-elect-req.fif @my_wallet_id.addr 1567633899 2.7 C5C2B94529405FB07D1DDFB4C42BFB07727E7BA07006B2DB569FBF23060B9E5C + +Creating a request to participate in validator elections at time 1567633899 from smart contract Uf+vF9tD9Atqok5yA6n4yGUjEMiMElBi0RKf6IPqob1nY4EA = -1:af17db43f40b6aa24e7203a9f8c8652310c88c125062d1129fe883eaa1bd6763 with maximal stake factor with respect to the minimal stake 176947/65536 and validator ADNL address c5c2b94529405fb07d1ddfb4c42bfb07727e7ba07006b2db569fbf23060b9e5c +654C50745D7031EB0002B333AF17DB43F40B6AA24E7203A9F8C8652310C88C125062D1129FE883EAA1BD6763C5C2B94529405FB07D1DDFB4C42BFB07727E7BA07006B2DB569FBF23060B9E5C +ZUxQdF1wMesAArMzrxfbQ_QLaqJOcgOp-MhlIxDIjBJQYtESn-iD6qG9Z2PFwrlFKUBfsH0d37TEK_sHcn57oHAGsttWn78jBgueXA== +--------------------------------------- + +Here = 2.7 is the maximal allowed ratio of your stake with respect to the minimal validator stake in the elected validator group. In this way you can be sure that your stake will be not more than 2.7 times the smallest stake, so the workload of your validator is at most 2.7 times the minimal one. If your stake is too large with respect to the stakes of other validators, then it will be clipped to this value (2.7 times the minimal stake), and the remainder will be returned to you (i.e., to the controlling smart contract of your validator) immediately after elections. + +Now you obtain a binary string in hexadecimal (654C...9E5C) and base64 form to be signed by the validator. This can be done in validator-engine-console: + +> sign BCA335626726CF2E522D287B27E4FAFFF82D1D98615957DB8E224CB397B2EB67 654C50745D7031EB0002B333AF17DB43F40B6AA24E7203A9F8C8652310C88C125062D1129FE883EAA1BD6763C5C2B94529405FB07D1DDFB4C42BFB07727E7BA07006B2DB569FBF23060B9E5C +got signature ovf9cmr2J/speJEtMU+tZm6zH/GBEyZCPpaukqL3mmNH9Wipyoys63VFh0yR386bARHKMPpfKAYBYslOjdSjCQ + +Here BCA...B67 is the identifier of the signing key of our validator, and 654...E5C is the message generated by validator-elect-req.fif. The signature is ovf9...jCQ (this is the Base64 representation of 64-byte Ed25519 signature). + +Now you have to run another script validator-elect-signed.fif, which also requires the public key and the signature of the validator: + +------------------------------------ +$ fift -s validator-elect-signed.fif @my_wallet_id.addr 1567633899 2.7 C5C2B94529405FB07D1DDFB4C42BFB07727E7BA07006B2DB569FBF23060B9E5C xrQTSIQEsqZkWnoADMiBnyBFRUUweXTvzRQFqb5nHd5xmeE6 ovf9cmr2J/speJEtMU+tZm6zH/GBEyZCPpaukqL3mmNH9Wipyoys63VFh0yR386bARHKMPpfKAYBYslOjdSjCQ== +Creating a request to participate in validator elections at time 1567633899 from smart contract Uf+vF9tD9Atqok5yA6n4yGUjEMiMElBi0RKf6IPqob1nY4EA = -1:af17db43f40b6aa24e7203a9f8c8652310c88c125062d1129fe883eaa1bd6763 with maximal stake factor with respect to the minimal stake 176947/65536 and validator ADNL address c5c2b94529405fb07d1ddfb4c42bfb07727e7ba07006b2db569fbf23060b9e5c +String to sign is: 654C50745D7031EB0002B333AF17DB43F40B6AA24E7203A9F8C8652310C88C125062D1129FE883EAA1BD6763C5C2B94529405FB07D1DDFB4C42BFB07727E7BA07006B2DB569FBF23060B9E5C +Provided a valid Ed25519 signature A2F7FD726AF627FB2978912D314FAD666EB31FF1811326423E96AE92A2F79A6347F568A9CA8CACEB7545874C91DFCE9B0111CA30FA5F28060162C94E8DD4A309 with validator public key 8404B2A6645A7A000CC8819F20454545307974EFCD1405A9BE671DDE7199E13A +query_id set to 1567632790 + +Message body is x{4E73744B000000005D702D968404B2A6645A7A000CC8819F20454545307974EFCD1405A9BE671DDE7199E13A5D7031EB0002B333C5C2B94529405FB07D1DDFB4C42BFB07727E7BA07006B2DB569FBF23060B9E5C} + x{A2F7FD726AF627FB2978912D314FAD666EB31FF1811326423E96AE92A2F79A6347F568A9CA8CACEB7545874C91DFCE9B0111CA30FA5F28060162C94E8DD4A309} + +Saved to file validator-query.boc +----------------------- + +Alternatively, if you are running validator-engine-console on the same machine as your wallet, you can skip the above steps and instead use "createelectionbid" to create a file with the external message to be sent by the lite client. For this command to work, you have to run validator-engine with `-f ` command line option, where is a directory containing copies of all required Fift source files (such as Fift.fif, TonUtil.fif, validator-elect-req.fif, and validator-elect-signed.fif), even though these files normally reside in different source directories (/crypto/fift/lib and /crypto/smartcont). + +Now you have a message body containing your elections participation request. You have to send it from the controlling smart contract, carrying the stake as its value (plus one extra Gram for sending confirmation). If you use the simple wallet smart contract, this can be done by using -B command line argument to wallet.fif: + +-------------------------------------------- +$ fift -s wallet.fif my_wallet_id -1:A4C2C7C05B093D470DE2316DBA089FA0DD775FD9B1EBFC9DC9D04B498D3A2DDA 1 100001. -B validator-query.boc +Source wallet address = -1:af17db43f40b6aa24e7203a9f8c8652310c88c125062d1129fe883eaa1bd6763 +kf-vF9tD9Atqok5yA6n4yGUjEMiMElBi0RKf6IPqob1nY2dP +Loading private key from file my_wallet_id.pk +Transferring GR$100001. to account kf-kwsfAWwk9Rw3iMW26CJ-g3Xdf2bHr_J3J0EtJjTot2lHQ = -1:a4c2c7c05b093d470de2316dba089fa0dd775fd9b1ebfc9dc9d04b498d3a2dda seqno=0x1 bounce=-1 +Body of transfer message is x{4E73744B000000005D702D968404B2A6645A7A000CC8819F20454545307974EFCD1405A9BE671DDE7199E13A5D7031EB0002B333C5C2B94529405FB07D1DDFB4C42BFB07727E7BA07006B2DB569FBF23060B9E5C} + x{A2F7FD726AF627FB2978912D314FAD666EB31FF1811326423E96AE92A2F79A6347F568A9CA8CACEB7545874C91DFCE9B0111CA30FA5F28060162C94E8DD4A309} + +signing message: x{0000000101} + x{627FD26163E02D849EA386F118B6DD044FD06EBBAFECD8F5FE4EE4E825A4C69D16ED32D79A60A8500000000000000000000000000001} + x{4E73744B000000005D702D968404B2A6645A7A000CC8819F20454545307974EFCD1405A9BE671DDE7199E13A5D7031EB0002B333C5C2B94529405FB07D1DDFB4C42BFB07727E7BA07006B2DB569FBF23060B9E5C} + x{A2F7FD726AF627FB2978912D314FAD666EB31FF1811326423E96AE92A2F79A6347F568A9CA8CACEB7545874C91DFCE9B0111CA30FA5F28060162C94E8DD4A309} + +resulting external message: x{89FF5E2FB687E816D5449CE40753F190CA4621911824A0C5A2253FD107D5437ACEC6049CF8B8EA035B0446E232DB8C1DFEA97738076162B2E053513310D2A3A66A2A6C16294189F8D60A9E33D1E74518721B126A47DA3A813812959BD0BD607923B010000000080C_} + x{627FD26163E02D849EA386F118B6DD044FD06EBBAFECD8F5FE4EE4E825A4C69D16ED32D79A60A8500000000000000000000000000001} + x{4E73744B000000005D702D968404B2A6645A7A000CC8819F20454545307974EFCD1405A9BE671DDE7199E13A5D7031EB0002B333C5C2B94529405FB07D1DDFB4C42BFB07727E7BA07006B2DB569FBF23060B9E5C} + x{A2F7FD726AF627FB2978912D314FAD666EB31FF1811326423E96AE92A2F79A6347F568A9CA8CACEB7545874C91DFCE9B0111CA30FA5F28060162C94E8DD4A309} + +B5EE9C7241040401000000013D0001CF89FF5E2FB687E816D5449CE40753F190CA4621911824A0C5A2253FD107D5437ACEC6049CF8B8EA035B0446E232DB8C1DFEA97738076162B2E053513310D2A3A66A2A6C16294189F8D60A9E33D1E74518721B126A47DA3A813812959BD0BD607923B010000000080C01016C627FD26163E02D849EA386F118B6DD044FD06EBBAFECD8F5FE4EE4E825A4C69D16ED32D79A60A85000000000000000000000000000010201A84E73744B000000005D702D968404B2A6645A7A000CC8819F20454545307974EFCD1405A9BE671DDE7199E13A5D7031EB0002B333C5C2B94529405FB07D1DDFB4C42BFB07727E7BA07006B2DB569FBF23060B9E5C030080A2F7FD726AF627FB2978912D314FAD666EB31FF1811326423E96AE92A2F79A6347F568A9CA8CACEB7545874C91DFCE9B0111CA30FA5F28060162C94E8DD4A309062A7721 +(Saved to file wallet-query.boc) +---------------------------------- + +Now you just have to send wallet-query.boc from the Lite Client (not the Validator Console): + +> sendfile wallet-query.boc + +or you can use LiteClient in the batch mode +$ lite-client -C -c "sendfile wallet-query.boc" + +This is an external message signed by your private key (which controls your wallet), that instructs your wallet smart contract to send an internal message to the elector smart contract with the prescribed payload (containing the validator bid and signed by its key) and transfer the specified amount of grams. When the elector smart contract receives this internal message, it registers your bid (with the stake equal to the specified amount of grams minus one), and sends you (i.e., the wallet smart contract) a confirmation (carrying 1 gram back) or a rejection message with an error code (carrying back almost all of the original stake amount minus processing fees). + +You can check whether your stake has been accepted by running get-method "participant_list" of the elector smart contract. + +6. Recovering stakes and bonuses +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +If your stake is only partially accepted (because of max-factor) during the elections, or after your stake is unfrozen (this happens some time after the expiration of the term of the validator group to which your validator has been elected), you may want to collect back all or part of your stake, along with whatever share of bonuses is due to your validator. The elector smart contract does not send the stake and bonuses to you (i.e., the controlling smart contract) in a message. Instead, it credits the amount to be returned to you inside a special table, which can be inspected with the aid of "compute_returned_stake" get-method (which expects the address of the controlling smart contract as an argument): + +$ lite-client -C ton-lite-client-test1.config.json -rc 'runmethod -1:A4C2C7C05B093D470DE2316DBA089FA0DD775FD9B1EBFC9DC9D04B498D3A2DDA compute_returned_stake 0xaf17db43f40b6aa24e7203a9f8c8652310c88c125062d1129fe883eaa1bd6763' +arguments: [ 79196899299028790296381692623119733846152089453039582491866112477478757689187 130944 ] +result: [ 0 ] + +If the result is zero, nothing is due to you. Otherwise, you'll see part or all of your stake, maybe with some bonuses. In that case, you can create a stake recovery request by using recover-stake.fif: + +----------------------------- +$ fift -s recover-stake.fif +query_id for stake recovery message is set to 1567634299 + +Message body is x{47657424000000005D70337B} + +Saved to file recover-query.boc +----------------------------- + +Again, you have to send recover-query.boc as the payload of a message from the controlling smart contract (i.e., your wallet) to the elector smart contract: + +$ fift -s wallet.fif my_wallet_id -B recover-query.boc + +For example, + +$ fift -s wallet.fif my_wallet_id -1:A4C2C7C05B093D470DE2316DBA089FA0DD775FD9B1EBFC9DC9D04B498D3A2DDA 2 1. -B recover-query.boc +... +(Saved to file wallet-query.boc) + +Notice that this message carries a small value (one Gram) just to pay the message forwarding and processing fees. If you indicate a value equal to zero, the message will not be processed by the election smart contract (a message with exactly zero value is almost useless in the TON Blockchain context). + +Once wallet-query.boc is ready, you can send it from the Lite Client: + +$ liteclient -C -c 'sendfile wallet-query.boc' + +If you have done everything correctly (in particular indicated the correct seqno of your wallet instead of "2" in the example above), you'll obtain a message from the elector smart contract containing the change from the small value you sent with your request (1. Gram in this example) plus the recovered portion of your stake and bonuses. + +7. Participating in the next elections +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Notice that even before the term of the validator group containing your elected validator finishes, new elections for the next validator group will be announced. You'll probably want to participate in them as well. For this, you can use the same validator, but you must generate a new validator key and new ADNL address. You'll also have to make a new stake before your previous stake is returned (because your previous stake will be unfrozen and returned only some time after the next validator group becomes active), so it does not make sense to stake more than half of your Grams. diff --git a/doc/fiftbase.tex b/doc/fiftbase.tex new file mode 100644 index 00000000..35b21d9f --- /dev/null +++ b/doc/fiftbase.tex @@ -0,0 +1,2173 @@ +\documentclass[12pt,oneside]{article} +\usepackage[T1]{fontenc} +%\usepackage{euler} +\usepackage{amssymb, amsmath, amsfonts, stmaryrd} +\usepackage[mathscr]{euscript} +\usepackage{mathrsfs} +\usepackage{theorem} +\usepackage[english]{babel} +\usepackage{bm} +\usepackage[all]{xy} +\usepackage{array} +\usepackage{multirow} +%\usepackage{chngcntr} +%\CompileMatrices +\usepackage[bookmarks=false,pdfauthor={Nikolai Durov},pdftitle={Fift: A Brief Introduction}]{hyperref} +\usepackage{fancyhdr} +\usepackage{caption} +% +\setlength{\headheight}{15.2pt} +\pagestyle{fancy} +\renewcommand{\headrulewidth}{0.5pt} +% +\def\makepoint#1{\medbreak\noindent{\bf #1.\ }} +\def\zeropoint{\setcounter{subsection}{-1}} +\def\zerosubpoint{\setcounter{subsubsection}{-1}} +\def\nxpoint{\refstepcounter{subsection}% + \smallbreak\makepoint{\thesubsection}} +\def\nxsubpoint{\refstepcounter{subsubsection}% + \smallbreak\makepoint{\thesubsubsection}} +\def\nxsubsubpoint{\refstepcounter{paragraph}% + \makepoint{\paragraph}} +%\setcounter{secnumdepth}{4} +%\counterwithin{paragraph}{subsubsection} +\def\refpoint#1{{\rm\textbf{\ref{#1}}}} +\let\ptref=\refpoint +\def\embt(#1.){\textbf{#1.}} +\def\embtx(#1){\textbf{#1}} +\def\emb#1{\textbf{#1.}} +\long\def\nodo#1{} +% +%\def\markbothsame#1{\markboth{#1}{#1}} +\fancyhf{} +\fancyfoot[C]{\thepage} +\def\markbothsame#1{\fancyhead[C]{#1}} +\def\mysection#1{\section{#1}\fancyhead[C]{\textsc{Chapter \textbf{\thesection.} #1}}} +\def\mysubsection#1{\subsection{#1}\fancyhead[C]{\small{\textsc{\textrm{\thesubsection.} #1}}}} +\def\myappendix#1{\section{#1}\fancyhead[C]{\textsc{Appendix \textbf{\thesection.} #1}}} +% +\let\tp=\textit +\let\vr=\textit +\def\workchainid{\vr{workchain\_id\/}} +\def\shardpfx{\vr{shard\_prefix}} +\def\accountid{\vr{account\_id\/}} +\def\currencyid{\vr{currency\_id\/}} +\def\uint{\tp{uint}} +\def\opsc#1{\operatorname{\textsc{#1}}} +\def\CellRepr{\opsc{CellRepr}} +\def\blkseqno{\opsc{blk-seqno}} +\def\blkprev{\opsc{blk-prev}} +\def\blkhash{\opsc{blk-hash}} +\def\Hash{\opsc{Hash}} +\def\Sha{\opsc{sha256}} +\def\SHA#1{\opsc{sha#1}} +\def\Int{\opsc{int}} +\def\height{\opsc{height}} +\def\len{\opsc{len}} +\def\leaf{\opsc{Leaf}} +\def\node{\opsc{Node}} +\def\Seqno{\opsc{SeqNo}} +\def\LT{\opsc{Lt}} +\def\NextHop{\opsc{NextHop}} +\def\root{\opsc{Root}} +\def\emptyroot{\opsc{EmptyRoot}} +\def\code{\opsc{code}} +\def\Ping{\opsc{Ping}} +\def\Store{\opsc{Store}} +\def\FindNode{\opsc{Find\_Node}} +\def\FindValue{\opsc{Find\_Value}} +\def\Bytes{\tp{Bytes}} +\def\Transaction{\tp{Transaction}} +\def\Account{\tp{Account}} +\def\State{\tp{State}} +\def\Maybe{\opsc{Maybe}} +\def\List{\opsc{List}} +\def\Block{\tp{Block}} +\def\Blockchain{\tp{Blockchain}} +\def\isValidBc{\tp{isValidBc}} +\def\evtrans{\vr{ev\_trans}} +\def\evblock{\vr{ev\_block}} +\def\Hashmap{\tp{Hashmap}} +\def\HashmapE{\tp{HashmapE}} +\def\Type{\tp{Type}} +\def\nat{\tp{nat\/}} +\def\hget{\vr{hget\/}} +\def\bbB{{\mathbb{B}}} +\def\bbP{{\mathbb{P}}} +\def\bbF{{\mathbb{F}}} +\def\bbZ{{\mathbb{Z}}} +\def\st#1{{\mathbf{#1}}} +\def\sgn{\operatorname{sgn}} +\def\charact{\operatorname{char}} +\def\caret{\^{}} +\def\cF{\mathscr{F}} +% +\hfuzz=0.8pt + +\title{Fift: A Brief Introduction} +\author{Nikolai Durov} +\begin{document} + +%\pagestyle{myheadings} +\maketitle + +\begin{abstract} + The aim of this text is to provide a brief description of Fift, a new programming language specifically designed for creating and managing TON Blockchain smart contracts, and its features used for interaction with the TON Virtual Machine~\cite{TVM} and the TON Blockchain~\cite{TBC}. +\end{abstract} + +\section*{Introduction} +\markbothsame{Introduction} + +This document provides a brief description of Fift, a stack-based general-purpose programming language optimized for creating, debugging, and managing TON Blockchain smart contracts. + +Fift has been specifically designed to interact with the TON Virtual Machine (TON VM or TVM)~\cite{TVM} and the TON Blockchain~\cite{TBC}. In particular, it offers native support for 257-bit integer arithmetic and TVM cell manipulation shared with TVM, as well as an interface to the Ed25519-based cryptography employed by the TON Blockchain. A macro assembler for TVM code, useful for writing new smart contracts, is also included in the Fift distribution. + +Being a stack-based language, Fift is not unlike Forth. Because of the brevity of this text, some knowledge of Forth might be helpful for understanding Fift.\footnote{Good introductions to Forth exist; we can recommend~\cite{Brodie}.} However, there are significant differences between the two languages. For instance, Fift enforces runtime type-checking, and keeps values of different types (not only integers) in its stack. + +A list of words (built-in functions, or primitives) defined in Fift, along with their brief descriptions, is presented in Appendix~\ref{app:words}. + +Please note that the current version of this document describes a preliminary test version of Fift; some minor details are likely to change in the future. + +\clearpage +\tableofcontents + +\clearpage +\mysection{Overview}\label{sect:overview} + +Fift is a simple stack-based programming language designed for testing and debugging the TON Virtual Machine \cite{TVM} and the TON Blockchain \cite{TBC}, but potentially useful for other purposes as well. When Fift is invoked (usually by executing a binary file called {\tt fift}), it either reads, parses, and interprets one or several source files indicated in the command line, or enters the interactive mode and interprets Fift commands read and parsed from the standard input. There is also a ``script mode'', activated by command line switch {\tt -s}, in which all command line arguments except the first one are passed to the Fift program by means of the variables {\tt \$$n$} and {\tt \$\#}. In this way, Fift can be used both for interactive experimentation and debugging as well as for writing simple scripts. + +All data manipulated by Fift is kept in a (LIFO) stack. Each stack entry is supplemented by a {\em type tag}, which unambiguously determines the type of the value kept in the corresponding stack entry. The types of values supported by Fift include {\em Integer\/} (representing signed 257-bit integers), {\em Cell\/} (representing a TVM cell, which consists of up to 1023 data bits and up to four references to other cells as explained in \cite{TVM}), {\em Slice\/} (a partial view of a {\em Cell\/} used for parsing cells), and {\em Builder\/} (used for building new cells). These data types (and their implementations) are shared with TVM~\cite{TVM}, and can be safely passed from the Fift stack to the TVM stack and back when necessary (e.g., when TVM is invoked from Fift by using a Fift primitive such as {\tt runvmcode}). + +In addition to the data types shared with TVM, Fift introduces some unique data types, such as {\em Bytes\/} (arbitrary byte sequences), {\em String\/} (UTF-8 strings), {\em WordList\/}, and {\em WordDef\/} (used by Fift to create new ``words'' and manipulate their definitions). In fact, Fift can be extended to manipulate arbitrary ``objects'' (represented by the generic type {\em Object}), provided they are derived from C++ class {\tt td::CntObject} in the current implementation. + +Fift source files and libraries are usually kept in text files with the suffix {\tt .fif}. A search path for libraries and included files is passed to the Fift executable either in a {\tt -I} command line argument or in the {\tt FIFTPATH} environment variable. If neither is set, the default library search path {\tt /usr/lib/fift} is used. + +On startup, the standard Fift library is read from the file {\tt Fift.fif} before interpreting any other sources. It must be present in the library search path, otherwise Fift execution will fail. + +A fundamental Fift data structure is its global {\em dictionary}, containing {\em words}---or, more precisely, {\em word definitions\/}---that correspond both to built-in primitives and functions and to user-defined functions.\footnote{Fift words are typically shorter than functions or subroutines of other programming languages. A nice discussion and some guidelines (for Forth words) may be found in~\cite{Brodie2}.} A word can be executed in Fift simply by typing its name (a UTF-8 string without space characters) in interactive mode. When Fift starts up, some words ({\em primitives\/}) are already defined (by some C++ code in the current implementation); other words are defined in the standard library {\tt Fift.fif}. After that, the user may extend the dictionary by defining new words or redefining old ones. + +The dictionary is supposed to be split into several {\em vocabularies}, or {\em name\-spaces}; however, namespaces are not implemented yet, so all words are currently defined in the same global namespace. + +The Fift parser for input source files and for the standard input (in the interactive mode) is rather simple: the input is read line-by-line, then blank characters are skipped, and the longest prefix of the remaining line that is (the name of) a dictionary word is detected and removed from the input line.\footnote{Notice that in contrast to Forth, Fift word names are case-sensitive: {\tt dup} and {\tt DUP} are distinct words.} After that, the word thus found is executed, and the process repeats until the end of the line. When the input line is exhausted, a subsequent line is read from the current input file or the standard input. + +In order to be detected, most words require a blank character or an end-of-line immediately after them; this is reflected by appending a space to their names in the dictionary. Other words, called {\em prefix words}, do not require a blank character immediately after them. + +If no word is found, the string consisting of the first remaining characters of the input line until the next blank or end-of-line character is interpreted as an {\em Integer\/} and pushed into the stack. For instance, if we invoke Fift, type {\tt 2 3 + .} (and press Enter), Fift first pushes an {\em Integer\/} constant equal to $2$ into its stack, followed by another integer constant equal to $3$. After that, the built-in primitive ``{\tt +}'' is parsed and found in the dictionary; when invoked, it takes the two topmost elements from the stack and replaces them with their sum ($5$ in our example). Finally, ``{\tt .}'' is a primitive that prints the decimal representation of the top-of-stack {\em Integer}, followed by a space. As a result, we observe ``{\tt 5 ok}'' printed by the Fift interpreter into the standard output. The string ``{\tt ok}'' is printed by the interpreter whenever it finishes interpreting a line read from the standard input in the interactive mode. + +A list of built-in words may be found in Appendix~\ref{app:words}. + +\mysection{Fift basics} +This chapter provides an introduction into the basic features of the Fift programming language. The discussion is informal and incomplete at first, but gradually becomes more formal and more precise. In some cases, later chapters and Appendix~\ref{app:words} provide more details about the words first mentioned in this chapter; similarly, some tricks that will be dutifully explained in later chapters are already used here where appropriate. + +\mysubsection{List of Fift stack value types}\label{p:stack.types} +Currently, the values of the following data types can be kept in a Fift stack: +\begin{itemize} +\item {\em Integer\/} --- A signed 257-bit integer. Usually denoted by $x$, $y$, or $z$ in the stack notation (when the stack effect of a Fift word is described). +\item {\em Cell\/} --- A TVM cell, consisting of up to 1023 data bits and up to 4 references to other cells (cf.~\cite{TVM}). Usually denoted by~$c$ or its variants, such as $c'$ or $c_2$. +\item {\em Slice\/} --- A partial view of a TVM cell, used for parsing data from {\em Cell\/}s. Usually denoted by~$s$. +\item {\em Builder\/} --- A partially built {\em Cell}, containing up to 1023 data bits and up to four references; can be used to create new {\em Cell\/}s. Usually denoted by~$b$. +\item {\em Null\/} --- A type with exactly one ``null'' value. Used to initialize new {\em Box\/}es. Usually denoted by~$\bot$. +\item {\em Tuple\/} --- An ordered collection of values of any of these types (not necessarily the same); can be used to represent values of arbitrary algebraic data types and Lisp-style lists. +\item {\em String\/} --- A (usually printable) UTF-8 string. Usually denoted by~$S$. +\item {\em Bytes\/} --- An arbitrary sequence of 8-bit bytes, typically used to represent binary data. Usually denoted by~$B$. +\item {\em WordList\/} --- A (partially created) list of word references, used for creating new Fift word definitions. Usually denoted by~$l$. +\item {\em WordDef\/} --- An execution token, usually representing the definition of an existing Fift word. Usually denoted by~$e$. +\item {\em Box\/} --- A location in memory that can be used to store one stack value. Usually denoted by~$p$. +\item {\em Atom\/} --- A simple entity uniquely identified by its name, a string. Can be used to represent identifiers, labels, operation names, tags, and stack markers. Usually denoted by~$a$. +\item {\em Object\/} --- An arbitrary C++ object of any class derived from base class {\tt td::CntObject}; may be used by Fift extensions to manipulate other data types and interface with other C++ libraries. +\end{itemize} + +The first six types listed above are shared with TVM; the remainder are Fift-specific. Notice that not all TVM stack types are present in Fift. For instance, the TVM {\em Continuation\/} type is not explicitly recognized by Fift; if a value of this type ends up in a Fift stack, it is manipulated as a generic {\em Object}. + +\mysubsection{Comments}\label{p:comments} +Fift recognizes two kinds of comments: ``{\tt // }'' (which must be followed by a space) opens a single-line comment until the end of the line, and {\tt /*} defines a multi-line comment until {\tt */}. Both words {\tt //} and {\tt /*} are defined in the standard Fift library ({\tt Fift.fif}). + +\mysubsection{Terminating Fift}\label{p:exit.fift} +The word {\tt bye} terminates the Fift interpreter with a zero exit code. If a non-zero exit code is required (for instance, in Fift scripts), one can use word {\tt halt}, which terminates Fift with the given exit code (passed as an {\em Integer\/} at the top of the stack). In contrast, {\tt quit} does not quit to the operating system, but rather exits to the top level of the Fift interpreter. + +\mysubsection{Simple integer arithmetic}\label{p:arith.op} +When Fift encounters a word that is absent from the dictionary, but which can be interpreted as an integer constant (or ``literal''), its value is pushed into the stack (as explained in~\ptref{p:int.lit} in more detail). Apart from that, several integer arithmetic primitives are defined: +\begin{itemize} +\item {\tt +} ($x$ $y$ -- $x+y$), replaces two {\em Integer\/}s $x$ and $y$ passed at the top of the stack with their sum $x+y$. All deeper stack elements remain intact. If either $x$ or $y$ is not an {\em Integer}, or if the sum does not fit into a signed 257-bit {\em Integer}, an exception is thrown. +\item {\tt -} ($x$ $y$ -- $x-y$), computes the difference $x-y$ of two {\em Integer\/}s $x$ and $y$. Notice that the first argument $x$ is the second entry from the top of the stack, while the second argument $y$ is taken from the top of the stack. +\item {\tt negate} ($x$ -- $-x$), changes the sign of an {\em Integer}. +\item {\tt *} ($x$ $y$ -- $xy$), computes the product $xy$ of two {\em Integer\/}s $x$ and $y$. +\item {\tt /} ($x$ $y$ -- $q:=\lfloor x/y\rfloor$), computes the floor-rounded quotient $\lfloor x/y\rfloor$ of two {\em Integer\/}s. +\item {\tt mod} ($x$ $y$ -- $r:=x\bmod y$), computes the remainder $x\bmod y=x-y\cdot\lfloor x/y\rfloor$ of division of $x$ by $y$. +\item {\tt /mod} ($x$ $y$ -- $q$ $r$), computes both the quotient and the remainder. +\item {\tt /c}, {\tt /r} ($x$ $y$ -- $q$), division words similar to {\tt /}, but using ceiling rounding ($q:=\lceil x/y\rceil$) and nearest-integer rounding ($q:=\lfloor 1/2+x/y\rfloor$), respectively. +\item {\tt /cmod}, {\tt /rmod} ($x$ $y$ -- $q$ $r:=x-qy$), division words similar to {\tt /mod}, but using ceiling or nearest-integer rounding. +\item {\tt <{}<} ($x$ $y$ -- $x\cdot 2^y$), computes an arithmetic left shift of binary number $x$ by $y\geq0$ positions, yielding $x\cdot 2^y$. +\item {\tt >{}>} ($x$ $y$ -- $q:=\lfloor x\cdot 2^{-y}\rfloor$), computes an arithmetic right shift by $y\geq0$ positions. +\item {\tt >{}>c}, {\tt >{}>r} ($x$ $y$ -- $q$), similar to {\tt >{}>}, but using ceiling or nearest-integer rounding. +\item {\tt and}, {\tt or}, {\tt xor} ($x$ $y$ -- $x\oplus y$), compute the bitwise AND, OR, or XOR of two {\em Integer\/}s. +\item {\tt not} ($x$ -- $-1-x$), bitwise complement of an {\em Integer}. +\item {\tt */} ($x$ $y$ $z$ -- $\lfloor xy/z\rfloor$), ``multiply-then-divide'': multiplies two integers $x$ and $y$ producing a 513-bit intermediate result, then divides the product by $z$. +\item {\tt */mod} ($x$ $y$ $z$ -- $q$ $r$), similar to {\tt */}, but computes both the quotient and the remainder. +\item {\tt */c}, {\tt */r} ($x$ $y$ $z$ -- $q$), {\tt */cmod}, {\tt */rmod} ($x$ $y$ $z$ -- $q$ $r$), similar to {\tt */} or {\tt */mod}, but using ceiling or nearest-integer rounding. +\item {\tt *>{}>}, {\tt *>{}>c}, {\tt *>{}>r} ($x$ $y$ $z$ -- $q$), similar to {\tt */} and its variants, but with division replaced with a right shift. Compute $q=xy/2^z$ rounded in the indicated fashion (floor, ceiling, or nearest integer). +\item {\tt <{} + nip -trailing0 } : (.GR) +{ (.GR) ."GR$" type space } : .GR +-17239000000 .GR +\end{verbatim} +produces {\tt GR\$-17.239 ok}. The above definitions make use of tricks explained in later portions of this document (especially Chapter~\ptref{s:dict.compiler}). + +We can also manipulate fractions by themselves by defining suitable ``rational arithmetic words'': +\begin{verbatim} +// a b c d -- (a*d-b*c) b*d +{ -rot over * 2swap tuck * rot - -rot * } : R- +// a b c d -- a*c b*d +{ rot * -rot * swap } : R* +// a b -- +{ swap ._ ."/" . } : R. +1.7 2/3 R- R. +\end{verbatim} +will output ``{\tt 31/30 ok}'', indicating that $1.7-2/3=31/30$. Here ``{\tt .\_}'' is a variant of ``{\tt .}'' that does not print a space after the decimal representation of an {\em Integer}. + +\mysubsection{String literals}\label{p:string.lit} +String literals are introduced by means of the prefix word {\tt "}, which scans the remainder of the line until the next {\tt "} character, and pushes the string thus obtained into the stack as a value of type {\em String}. For instance, {\tt "Hello, world!"} pushes the corresponding {\em String\/} into the stack: +\begin{verbatim} +"Hello, world!" .s +\end{verbatim} + +\mysubsection{Simple string manipulation}\label{p:string.ops} +The following words can be used to manipulate strings: +\begin{itemize} +\item {\tt"$\langle\textit{string}\rangle$"} ( -- $S$), pushes a {\em String\/} literal into the stack. +\item {\tt."$\langle\textit{string}\rangle$"} ( -- ), prints a constant string into the standard output. +\item {\tt type} ($S$ -- ), prints a {\em String\/} $S$ taken from the top of the stack into the standard output. +\item {\tt cr} ( -- ), outputs a carriage return (or a newline character) into the standard output. +\item {\tt emit} ($x$ -- ), prints a UTF-8 encoded character with Unicode codepoint given by {\em Integer\/} $x$ into the standard output. +\item {\tt char $\langle\textit{string}\rangle$} ( -- $x$), pushes an {\em Integer\/} with the Unicode codepoint of the first character of {\tt $\langle\textit{string}\rangle$}. +\item {\tt bl} ( -- $x$), pushes the Unicode codepoint of a space, i.e., 32. +\item {\tt space} ( -- ), prints one space, equivalent to {\tt bl emit}. +\item {\tt \$+} ($S$ $S'$ -- $S.S'$), concatenates two strings. +\item {\tt \$len} ($S$ -- $x$), computes the byte length (not the UTF-8 character length!) of a string. +\item {\tt +"$\langle\textit{string}\rangle$"} ($S$ -- $S'$), concatenates {\em String\/}~$S$ with a string literal. Equivalent to {\tt "$\langle\textit{string}\rangle$" \$+}. +\item {\tt word} ($x$ -- $S$), parses a word delimited by the character with the Unicode codepoint $x$ from the remainder of the current input line and pushes the result as a {\em String}. For instance, {\tt bl word abracadabra type} will print the string ``{\tt abracadabra}''. If $x=0$, skips leading spaces, and then scans until the end of the current input line. If $x=32$, skips leading spaces before parsing the next word. +\item {\tt (.)} ($x$ -- $S$), returns the {\em String\/} with the decimal representation of {\em Integer\/}~$x$. +\item {\tt (number)} ($S$ -- $0$ or $x$ $1$ or $x$ $y$ $2$), attempts to parse the {\em String\/} $S$ as an integer or fractional literal as explained in~\ptref{p:int.lit}. +\end{itemize} +For instance, {\tt ."*"}, {\tt "*" type}, {\tt 42 emit}, and {\tt char * emit} are four different ways to output a single asterisk. + +\mysubsection{Boolean expressions, or flags}\label{p:bool} +Fift does not have a separate value type for representing boolean values. Instead, any non-zero {\em Integer\/} can be used to represent truth (with $-1$ being the standard representation), while a zero {\em Integer\/} represents falsehood. Comparison primitives normally return $-1$ to indicate success and $0$ otherwise. + +Constants {\tt true} and {\tt false} can be used to push these special integers into the stack: +\begin{itemize} +\item {\tt true} ( -- $-1$), pushes $-1$ into the stack. +\item {\tt false} ( -- $0$), pushes $0$ into the stack. +\end{itemize} + +If boolean values are standard (either $0$ or $-1$), they can be manipulated by means of bitwise logical operations {\tt and}, {\tt or}, {\tt xor}, {\tt not} (listed in~\ptref{p:arith.op}). Otherwise, they must first be reduced to the standard form using {\tt 0<>}: +\begin{itemize} +\item {\tt 0<>} ($x$ -- $x\neq0$), pushes $-1$ if {\em Integer\/} $x$ is non-zero, $0$ otherwise. +\end{itemize} + +\mysubsection{Integer comparison operations}\label{p:int.comp} +Several integer comparison operations can be used to obtain boolean values: +\begin{itemize} +\item {\tt <} ($x$ $y$ -- $?$), checks whether $x}, {\tt =}, {\tt <>}, {\tt <=}, {\tt >=} ($x$ $y$ -- $?$), compare $x$ and $y$ and push $-1$ or $0$ depending on the result of the comparison. +\item {\tt 0<} ($x$ -- $?$), checks whether $x<0$ (i.e., pushes $-1$ if $x$ is negative, $0$ otherwise). Equivalent to {\tt 0 <}. +\item {\tt 0>}, {\tt 0=}, {\tt 0<>}, {\tt 0<=}, {\tt 0>=} ($x$ -- $?$), compare $x$ against zero. +\item {\tt cmp} ($x$ $y$ -- $z$), pushes $1$ if $x>y$, $-1$ if $x0$, $-1$ if $x<0$, and $0$ if $x=0$. Equivalent to {\tt 0 cmp}. +\end{itemize} +Example: +\begin{verbatim} +2 3 < . +\end{verbatim} +prints ``{\tt -1 ok}'', because $2$ is less than $3$. + +A more convoluted example: +\begin{verbatim} +{ "true " "false " rot 0= 1+ pick type 2drop } : ?. +2 3 < ?. 2 3 = ?. 2 3 > ?. +\end{verbatim} +prints ``{\tt true false false ok}''. + +\mysubsection{String comparison operations}\label{p:string.cmp.ops} +Strings can be lexicographically compared by means of the following words: +\begin{itemize} +\item {\tt \$=} ($S$ $S'$ -- $?$), returns $-1$ if strings $S$ and $S'$ are equal, $0$ otherwise. +\item {\tt \$cmp} ($S$ $S'$ -- $x$), returns $0$ if strings $S$ and $S'$ are equal, $-1$ if $S$ is lexicographically less than $S'$, and $1$ if $S$ is lexicographically greater than $S'$. +\end{itemize} + +\mysubsection{Named and unnamed variables}\label{p:variables} +In addition to constants introduced in~\ptref{p:constants}, Fift supports {\em variables}, which are a more efficient way to represent changeable values. For instance, the last two code fragments of~\ptref{p:constants} could have been written with the aid of variables instead of constants as follows: +\begin{verbatim} +variable x variable y +{ dup x ! dup * y ! } : setxy +3 setxy x @ . y @ . x @ y @ + . +7 setxy x @ . y @ . x @ y @ + . +{ ."( " x @ . .", " y @ . .") " } : showxy +3 setxy showxy +\end{verbatim} +producing the same output as before: +\begin{verbatim} +3 9 12 ok +7 49 56 ok +( 3 , 9 ) ok +\end{verbatim} + +The phrase {\tt variable x} creates a new {\em Box}, i.e., a memory location that can be used to store exactly one value of any Fift-supported type, and defines {\tt x} as a constant equal to this {\em Box\/}: +\begin{itemize} +\item {\tt variable} ( -- ), scans a blank-delimited word name $S$ from the remainder of the input, allocates an empty {\em Box}, and defines a new ordinary word $S$ as a constant, which will push the new {\em Box\/} when invoked. Equivalent to {\tt hole constant}. +\item {\tt hole} ( -- $p$), creates a new {\em Box\/}~$p$ that does not hold any value. Equivalent to {\tt null box}. +\item {\tt box} ($x$ -- $p$), creates a new {\em Box\/} containing specified value~$x$. Equivalent to {\tt hole tuck !}. +\end{itemize} +The value currently stored in a {\em Box\/} may be fetched by means of word {\tt @} (pronounced ``fetch''), and modified by means of word {\tt !} (pronounced ``store''): +\begin{itemize} +\item {\tt @} ($p$ -- $x$), fetches the value currently stored in {\em Box\/}~$p$. +\item {\tt !} ($x$ $p$ -- ), stores new value~$x$ into {\em Box\/}~$p$. +\end{itemize} +Several auxiliary words exist that can modify the current value in a more sophisticated fashion: +\begin{itemize} +\item {\tt +!} ($x$ $p$ -- ), increases the integer value stored in {\em Box\/}~$p$ by {\em Integer\/}~$x$. Equivalent to {\tt tuck @ + swap !}. +\item {\tt 1+!} ($p$ -- ), increases the integer value stored in {\em Box\/}~$p$ by one. Equivalent to {\tt 1 swap +!}. +\item {\tt 0!} ($p$ -- ), stores {\em Integer\/} $0$ into {\em Box\/}~$p$. Equivalent to {\tt 0 swap !}. +\end{itemize} +In this way we can implement a simple counter: +\begin{verbatim} +variable counter +{ counter 0! } : reset-counter +{ counter @ 1+ dup counter ! } : next-counter +reset-counter next-counter . next-counter . next-counter . +reset-counter next-counter . +\end{verbatim} +produces +\begin{verbatim} +1 2 3 ok +1 ok +\end{verbatim} +After these definitions are in place, we can even forget the definition of {\tt counter} by means of the phrase {\tt forget counter}. Then the only way to access the value of this variable is by means of {\tt reset-counter} and {\tt next-counter}. + +Variables are usually created by {\tt variable} with no value, or rather with a {\em Null\/} value. If one wishes to create initialized variables, one can use the phrase {\tt box constant}: +\begin{verbatim} +17 box constant x +x 1+! x @ . +\end{verbatim} +prints ``{\tt 18 ok}''. One can even define a special defining word for initialized variables, if they are needed often: +\begin{verbatim} +{ box constant } : init-variable +17 init-variable x +"test" init-variable y +x 1+! x @ . y @ type +\end{verbatim} +prints ``{\tt 18 test ok}''. + +The variables have so far only one disadvantage compared to the constants: one has to access their current values by means of an auxiliary word {\tt @}. Of course, one can mitigate this by defining a ``getter'' and a ``setter'' word for a variable, and use these words to write better-looking code: +\begin{verbatim} +variable x-box +{ x-box @ } : x +{ x-box ! } : x! +{ x x * 5 x * + 6 + } : f(x) +{ ."( " x . .", " f(x) . .") " } : .xy +3 x! .xy 5 x! .xy +\end{verbatim} +prints ``{\tt ( 3 , 30 ) ( 5 , 56 ) ok}'', which are the points $(x,f(x))$ on the graph of $f(x)=x^2+5x+6$ with $x=3$ and $x=5$. + +Again, if we want to define ``getters'' for all our variables, we can first define a defining word as explained in~\ptref{p:custom.defw}, and use this word to define both a getter and a setter at the same time: +\begin{verbatim} +{ hole dup 1 ' @ does create 1 ' ! does create } : variable-set +variable-set x x! +variable-set y y! +{ ."x=" x . ."y=" y . ."x*y=" x y * . cr } : show +{ y 1+ y! } : up +{ x 1+ x! } : right +{ x y x! y! } : reflect +2 x! 5 y! show up show right show up show reflect show +\end{verbatim} +produces +\begin{verbatim} +x=2 y=5 x*y=10 +x=2 y=6 x*y=12 +x=3 y=6 x*y=18 +x=3 y=7 x*y=21 +x=7 y=3 x*y=21 +\end{verbatim} + +\mysubsection{Tuples and arrays}\label{p:tuples} +Fift also supports {\em Tuple\/}s, i.e., immutable ordered collections of arbitrary values of stack value types (cf.~\ptref{p:stack.types}). When a {\em Tuple\/}~$t$ consists of values $x_1$, \dots, $x_n$ (in that order), we write $t=(x_1,\ldots,x_n)$. The number~$n$ is called the {\em length\/} of {\em Tuple\/}~$t$; it is also denoted by~$|t|$. Tuples of length two are also called {\em pairs}, tuples of length three are {\em triples}. +\begin{itemize} +\item {\tt tuple} ($x_1$ \dots $x_n$ $n$ -- $t$), creates new {\em Tuple\/} $t:=(x_1,\ldots,x_n)$ from $n\geq0$ topmost stack values. +\item {\tt pair} ($x$ $y$ -- $t$), creates new pair $t=(x,y)$. Equivalent to {\tt 2 tuple}. +\item {\tt triple} ($x$ $y$ $z$ -- $t$), creates new triple $t=(x,y,z)$. Equivalent to {\tt 3 tuple}. +\item {\tt |} ( -- $t$), creates an empty {\em Tuple\/} $t=()$. Equivalent to {\tt 0 tuple}. +\item {\tt ,} ($t$ $x$ -- $t'$), appends $x$ to the end of {\em Tuple\/}~$t$, and returns the resulting {\em Tuple\/}~$t'$. +\item {\tt .dump} ($x$ -- ), dumps the topmost stack entry in the same way as {\tt .s} dumps all stack elements. +\end{itemize} +For instance, both +\begin{verbatim} +| 2 , 3 , 9 , .dump +\end{verbatim} +and +\begin{verbatim} +2 3 9 triple .dump +\end{verbatim} +construct and print triple $(2,3,9)$: +\begin{verbatim} +[ 2 3 9 ] ok +\end{verbatim} +Notice that the components of a {\em Tuple\/} are not necessarily of the same type, and that a component of a {\em Tuple\/} can also be a {\em Tuple\/}: +\begin{verbatim} +1 2 3 triple 4 5 6 triple 7 8 9 triple triple constant Matrix +Matrix .dump cr +| 1 "one" pair , 2 "two" pair , 3 "three" pair , .dump +\end{verbatim} +produces +\begin{verbatim} +[ [ 1 2 3 ] [ 4 5 6 ] [ 7 8 9 ] ] +[ [ 1 "one" ] [ 2 "two" ] [ 3 "three" ] ] ok +\end{verbatim} + +Once a {\em Tuple\/} has been constructed, we can extract any of its components, or completely unpack the {\em Tuple\/} into the stack: +\begin{itemize} +\item {\tt untuple} ($t$ $n$ -- $x_1$ \dots $x_n$), returns all components of a {\em Tuple\/}~$t=(x_1,\ldots,x_n)$, but only if its length is equal to~$n$. Otherwise throws an exception. +\item {\tt unpair} ($t$ -- $x$ $y$), unpacks a pair $t=(x,y)$. Equivalent to {\tt 2 untuple}. +\item {\tt untriple} ($t$ -- $x$ $y$ $z$), unpacks a triple $t=(x,y,z)$. Equivalent to {\tt 3 untuple}. +\item {\tt explode} ($t$ -- $x_1$ \dots $x_n$ $n$), unpacks a {\em Tuple\/}~$t=(x_1,\ldots,x_n)$ of unknown length~$n$, and returns that length. +\item {\tt count} ($t$ -- $n$), returns the length $n=|t|$ of {\em Tuple\/}~$t$. +\item {\tt tuple?} ($t$ -- $?$), checks whether $t$ is a {\em Tuple}, and returns $-1$ or $0$ accordingly. +\item {\tt []} ($t$ $i$ -- $x$), returns the $(i+1)$-st component $t_{i+1}$ of {\em Tuple\/}~$t$, where $0\leq i<|t|$. +\item {\tt first} ($t$ -- $x$), returns the first component of a {\em Tuple}. Equivalent to {\tt 0 []}. +\item {\tt second} ($t$ -- $x$), returns the second component of a {\em Tuple}. Equivalent to {\tt 1 []}. +\item {\tt third} ($t$ -- $x$), returns the third component of a {\em Tuple}. Equivalent to {\tt 2 []}. +\end{itemize} +For instance, we can access individual elements and rows of a matrix: +\begin{verbatim} +1 2 3 triple 4 5 6 triple 7 8 9 triple triple constant Matrix +Matrix .dump cr +Matrix 1 [] 2 [] . cr +Matrix third .dump cr +\end{verbatim} +produces +\begin{verbatim} +[ [ 1 2 3 ] [ 4 5 6 ] [ 7 8 9 ] ] +6 +[ 7 8 9 ] +\end{verbatim} + +Notice that {\em Tuple\/}s are somewhat similar to arrays of other programming languages, but are immutable: we cannot change one individual component of a {\em Tuple}. If we still want to create something like an array, we need a {\em Tuple\/} of {\em Box\/}es (cf.~\ptref{p:variables}): +\begin{itemize} +\item {\tt allot} ($n$ -- $t$), creates a {\em Tuple\/} that consists of $n$ new empty {\em Box\/}es. Equivalent to {\tt | \{ hole , \} rot times}. +\end{itemize} +For instance, +\begin{verbatim} +10 allot constant A +| 3 box , 1 box , 4 box , 1 box , 5 box , 9 box , constant B +{ over @ over @ swap rot ! swap ! } : swap-values-of +{ B swap [] } : B[] +{ B[] swap B[] swap-values-of } : swap-B +{ B[] @ . } : .B[] +0 1 swap-B 1 3 swap-B 0 2 swap-B +0 .B[] 1 .B[] 2 .B[] 3 .B[] +\end{verbatim} +creates an uninitialized array~$A$ of length~10, an initialized array~$B$ of length~6, and then interchanges some elements of $B$ and prints the first four elements of the resulting~$B$: +\begin{verbatim} +4 1 1 3 ok +\end{verbatim} + +\mysubsection{Lists}\label{p:lists} +Lisp-style lists can also be represented in Fift. First of all, two special words are introduced to manipulate values of type {\em Null}, used to represent the empty list (not to be confused with the empty {\em Tuple}): +\begin{itemize} +\item {\tt null} ( -- $\bot$), pushes the only value~$\bot$ of type {\em Null}, which is also used to represent an empty list. +\item {\tt null?} ($x$ -- $?$), checks whether~$x$ is a {\em Null}. Can also be used to check whether a list is empty. +\end{itemize} +After that, {\tt cons} and {\tt uncons} are defined as aliases for {\tt pair} and {\tt unpair}: +\begin{itemize} +\item {\tt cons} ($h$ $t$ -- $l$), constructs a list from its head (first element) $h$ and its tail (the list consisting of all remaining elements)~$t$. Equivalent to {\tt pair}. +\item {\tt uncons} ($l$ -- $h$ $t$), decomposes a non-empty list into its head and its tail. Equivalent to {\tt unpair}. +\item {\tt car} ($l$ -- $h$), returns the head of a list. Equivalent to {\tt first}. +\item {\tt cdr} ($l$ -- $t$), returns the tail of a list. Equivalent to {\tt second}. +\item {\tt cadr} ($l$ -- $h'$), returns the second element of a list. Equivalent to {\tt cdr car}. +\item {\tt list} ($x_1$ \dots $x_n$ $n$ -- $l$), constructs a list $l$ of length~$n$ with elements $x_1$, \ldots, $x_n$, in that order. Equivalent to {\tt null ' cons rot times}. +\item {\tt .l} ($l$ -- ), prints a Lisp-style list~$l$. +\end{itemize} +For instance, +\begin{verbatim} +2 3 9 3 tuple .dump cr +2 3 9 3 list dup .dump space dup .l cr +"test" swap cons .l cr +\end{verbatim} +produces +\begin{verbatim} +[ 2 3 9 ] +[ 2 [ 3 [ 9 (null) ] ] ] (2 3 9) +("test" 2 3 9) +\end{verbatim} +Notice that the three-element list $(2\ 3\ 9)$ is distinct from the triple $(2,3,9)$. + +\mysubsection{Atoms}\label{p:atoms} +An {\em Atom\/} is a simple entity uniquely identified by its name. {\em Atom\/}s can be used to represent identifiers, labels, operation names, tags, and stack markers. Fift offers the following words to manipulate {\em Atom\/}s: +\begin{itemize} +\item {\tt (atom)} ($S$ $x$ -- $a$ $-1$ or $0$), returns the only {\em Atom\/} $a$ with the name given by {\em String\/}~$S$. If there is no such {\em Atom\/} yet, either creates it (if {\em Integer\/}~$x$ is non-zero) or returns a single zero to indicate failure (if $x$ is zero). +\item {\tt atom} ($S$ -- $a$), returns the only {\em Atom\/}~$a$ with the name~$S$, creating such an atom if necessary. Equivalent to {\tt true (atom) drop}. +\item {\tt `$\langle\textit{word\/}\rangle$} ( -- $a$), introduces an {\em Atom\/} literal, equal to the only {\em Atom\/} with the name equal to $\langle\textit{word\/}\rangle$. Equivalent to {\tt "$\langle\textit{word\/}\rangle$" atom}. +\item {\tt anon} ( -- $a$), creates a new unique anonymous {\em Atom}. +\item {\tt atom?} ($u$ -- $?$), checks whether $u$ is an {\em Atom}. +\item {\tt eq?} ($u$ $v$ -- $?$), checks whether $u$ and $v$ are equal {\em Integer\/}s, {\em Atom\/}s, or {\em Null\/}s. If they are not equal, or if they are of different types, or not of one of the types listed, returns zero. +\end{itemize} + +For instance, +\begin{verbatim} +`+ 2 `* 3 4 3 list 3 list .l +\end{verbatim} +creates and prints the list +\begin{verbatim} +(+ 2 (* 3 4)) +\end{verbatim} +which is the Lisp-style representation of arithmetical expression $2+3\cdot 4$. An interpreter for such expressions might use {\tt eq?} to check the operation sign (cf.~\ptref{p:recursion} for an explanation of recursive functions in Fift): +\begin{verbatim} +variable 'eval +{ 'eval @ execute } : eval +{ dup tuple? { + uncons uncons uncons + null? not abort"three-element list expected" + swap eval swap eval rot + dup `+ eq? { drop + } { + dup `- eq? { drop - } { + `* eq? not abort"unknown operation" * + } cond + } cond + } if +} 'eval ! +`+ 2 `* 3 4 3 list 3 list dup .l cr eval . cr +\end{verbatim} +prints +\begin{verbatim} +(+ 2 (* 3 4)) +14 +\end{verbatim} +If we load {\tt Lisp.fif} to enable Lisp-style list syntax, we can enter +\begin{verbatim} +"Lisp.fif" include +( `+ 2 ( `* 3 4 ) ) dup .l cr eval . cr +\end{verbatim} +with the same result as before. The word {\tt (}, defined in {\tt Lisp.fif}, uses an anonymous {\em Atom\/} created by {\tt anon} to mark the current stack position, and then {\tt )} builds a list from several top stack entries, scanning the stack until the anonymous {\em Atom\/} marker is found: +\begin{verbatim} +variable ') +{ ") without (" abort } ') ! +{ ') @ execute } : ) +{ null { -rot 2dup eq? not } { swap rot cons } while 2drop +} : list-until-marker +{ anon dup ') @ 2 { ') ! list-until-marker } does ') ! } : ( +\end{verbatim} + +\mysubsection{Command line arguments in script mode}\label{p:cmdline.ops} +The Fift interpreter can be invoked in {\em script mode\/} by passing {\tt -s} as a command line option. In this mode, all further command line arguments are not scanned for Fift startup command line options. Rather, the next argument after {\tt -s} is used as the filename of the Fift source file, and all further command line arguments are passed to the Fift program by means of special words {\tt \$$n$} and {\tt \$\#}: +\begin{itemize} +\item {\tt \$\#} ( -- $x$), pushes the total number of command-line arguments passed to the Fift program. +\item {\tt \$$n$} ( -- $S$), pushes the $n$-th command-line argument as a {\em String}~$S$. For instance, {\tt \$0} pushes the name of the script being executed, {\tt \$1} the first command line argument, and so on. +\item {\tt \$()} ($x$ -- $S$), pushes the $x$-th command-line argument similarly to {\tt \$$n$}, but with {\em Integer\/}~$x$ taken from the stack. +\end{itemize} +Additionally, if the very first line of a Fift source file begins with the two characters ``{\tt \#!}'', this line is ignored. In this way simple Fift scripts can be written in a *ix system. For instance, if +\begin{verbatim} +#!/usr/bin/fift -s +{ ."usage: " $0 type ." " cr + ."Computes the product of two integers." cr 1 halt } : usage +{ ' usage if } : ?usage +$# 2 <> ?usage +$1 (number) 1- ?usage +$2 (number) 1- ?usage +* . cr +\end{verbatim} +is saved into a file {\tt cmdline.fif} in the current directory, and its execution bit is set (e.g., by {\tt chmod 755 cmdline.fif}), then it can be invoked from the shell or any other program, provided the Fift interpreter is installed as {\tt /usr/bin/fift}, and its standard library {\tt Fift.fif} is installed as {\tt /usr/lib/fift/Fift.fif}: +\begin{verbatim} +$ ./cmdline.fif 12 -5 +\end{verbatim} +prints +\begin{verbatim} +-60 +\end{verbatim} +when invoked from a *ix shell such as the Bourne--again shell (Bash). + +\mysection{Blocks, loops, and conditionals} +Similarly to the arithmetic operations, the execution flow in Fift is controlled by stack-based primitives. This leads to an inversion typical of reverse Polish notation and stack-based arithmetic: one first pushes a block representing a conditional branch or the body of a loop into the stack, and then invokes a conditional or iterated execution primitive. In this respect, Fift is more similar to PostScript than to Forth. + +\mysubsection{Defining and executing blocks}\label{p:blocks} +A block is normally defined using the special words ``{\tt \{}'' and ``{\tt \}}''. Roughly speaking, all words listed between {\tt \{} and {\tt \}} constitute the body of a new block, which is pushed into the stack as a value of type {\em WordDef}. A block may be stored as a definition of a new Fift word by means of the defining word ``{\tt :}'' as explained in \ptref{p:colon.def}, or executed by means of the word {\tt execute}: +\begin{verbatim} +17 { 2 * } execute . +\end{verbatim} +prints ``{\tt 34 ok}'', being essentially equivalent to ``{\tt 17 2 * .}''. A slightly more convoluted example: +\begin{verbatim} +{ 2 * } 17 over execute swap execute . +\end{verbatim} +applies ``anonymous function'' $x\mapsto 2x$ twice to $17$, and prints the result $2\cdot(2\cdot 17)=68$. In this way a block is an execution token, which can be duplicated, stored into a constant, used to define a new word, or executed. + +The word {\tt '} recovers the current definition of a word. Namely, the construct {\tt ' $\langle\textit{word-name}\rangle$} pushes the execution token equivalent to the current definition of the word $\langle\textit{word-name}\rangle$. For instance, +\begin{verbatim} +' dup execute +\end{verbatim} +is equivalent to {\tt dup}, and +\begin{verbatim} +' dup : duplicate +\end{verbatim} +defines {\tt duplicate} as a synonym for (the current definition of) {\tt dup}. + +Alternatively, we can duplicate a block to define two new words with the same definition: +\begin{verbatim} +{ dup * } +dup : square : **2 +\end{verbatim} +defines both {\tt square} and {\tt **2} to be equivalent to {\tt dup *}. + +\mysubsection{Conditional execution of blocks}\label{p:cond.ops} +Conditional execution of blocks is achieved using the words {\tt if}, {\tt ifnot}, and {\tt cond}: +\begin{itemize} +\item {\tt if} ($x$ $e$ -- ), executes $e$ (which must be an execution token, i.e., a {\em WordDef\/}),\footnote{A {\em WordDef\/} is more general than a {\em WordList}. For instance, the definition of the primitive {\tt +} is a {\em WordDef}, but not a {\em WordList}, because {\tt +} is not defined in terms of other Fift words.} but only if {\em Integer\/} $x$ is non-zero. +\item {\tt ifnot} ($x$ $e$ -- ), executes execution token $e$, but only if {\em Integer\/} $x$ is zero. +\item {\tt cond} ($x$ $e$ $e'$ -- ), if {\em Integer\/} $x$ is non-zero, executes $e$, otherwise executes $e'$. +\end{itemize} + +For instance, the last example in \ptref{p:int.comp} can be more conveniently rewritten as +\begin{verbatim} +{ { ."true " } { ."false " } cond } : ?. +2 3 < ?. 2 3 = ?. 2 3 > ?. +\end{verbatim} +still resulting in ``{\tt true false false ok}''. + +Notice that blocks can be arbitrarily nested, as already shown in the previous example. One can write, for example, +\begin{verbatim} +{ ?dup + { 0< + { ."negative " } + { ."positive " } + cond + } + { ."zero " } + cond +} : chksign +-17 chksign +\end{verbatim} +to obtain ``{\tt negative ok}'', because $-17$ is negative. + +\mysubsection{Simple loops}\label{p:simple.loops} +The simplest loops are implemented by {\tt times}: +\begin{itemize} +\item {\tt times} ($e$ $n$ -- ), executes $e$ exactly $n$ times, if $n\geq0$. If $n$ is negative, throws an exception. +\end{itemize} +For instance, +\begin{verbatim} +1 { 10 * } 70 times . +\end{verbatim} +computes and prints $10^{70}$. + +We can use this kind of loop to implement a simple factorial function: +\begin{verbatim} +{ 0 1 rot { swap 1+ tuck * } swap times nip } : fact +5 fact . +\end{verbatim} +prints ``{\tt 120 ok}'', because $5!=1\cdot 2\cdot 3\cdot 4\cdot 5=120$. + +This loop can be modified to compute Fibonacci numbers instead: +\begin{verbatim} +{ 0 1 rot { tuck + } swap times nip } : fibo +6 fibo . +\end{verbatim} +computes the sixth Fibonacci number $F_6=13$. + +\mysubsection{Loops with an exit condition}\label{p:loops} +More sophisticated loops can be created with the aid of {\tt until} and {\tt while}: +\begin{itemize} +\item {\tt until} ($e$ -- ), executes $e$, then removes the top-of-stack integer and checks whether it is zero. If it is, then begins a new iteration of the loop by executing $e$. Otherwise exits the loop. +\item {\tt while} ($e$ $e'$ -- ), executes $e$, then removes and checks the top-of-stack integer. If it is zero, exits the loop. Otherwise executes $e'$, then begins a new loop iteration by executing $e$ and checking the exit condition afterwards. +\end{itemize} +For instance, we can compute the first two Fibonacci numbers greater than 1000: +\begin{verbatim} +{ 1 0 rot { -rot over + swap rot 2dup >= } until drop +} : fib-gtr +1000 fib-gtr . . +\end{verbatim} +prints ``{\tt 1597 2584 ok}''. + +We can use this word to compute the first 70 decimal digits of the golden ratio $\phi=(1+\sqrt{5})/2\approx 1.61803$: +\begin{verbatim} +1 { 10 * } 70 times dup fib-gtr */ . +\end{verbatim} +prints ``{\tt $161803\ldots2604$ ok}''. + +\mysubsection{Recursion}\label{p:recursion} +Notice that, whenever a word is mentioned inside a {\tt \{ \dots \}} block, the current (compile-time) definition is included in the {\em WordList\/} being created. In this way we can refer to the previous definition of a word while defining a new version of it: +\begin{verbatim} +{ + . } : print-sum +{ ."number " . } : . +{ 1+ . } : print-next +2 . 3 . 2 3 print-sum 7 print-next +\end{verbatim} +produces ``{\tt number 2 number 3 5 number 8 ok}''. Notice that {\tt print-sum} continues to use the original definition of ``{\tt .}'', but {\tt print-next} already uses the modified ``{\tt .}''. + +This feature may be convenient on some occasions, but it prevents us from introducing recursive definitions in the most straightforward fashion. For instance, the classical recursive definition of the factorial +\begin{verbatim} +{ ?dup { dup 1- fact * } { 1 } cond } : fact +\end{verbatim} +will fail to compile, because {\tt fact} happens to be an undefined word when the definition is compiled. + +A simple way around this obstacle is to use the word {\tt @'} (cf.~\ptref{p:dict.lookup}) that looks up the current definition of the next word during the execution time and then executes it, similarly to what we already did in~\ptref{p:constants}: +\begin{verbatim} +{ ?dup { dup 1- @' fact * } { 1 } cond } : fact +5 fact . +\end{verbatim} +produces ``{\tt 120 ok}'', as expected. + +However, this solution is rather inefficient, because it uses a dictionary lookup each time {\tt fact} is recursively executed. We can avoid this dictionary lookup by using variables (cf.~\ptref{p:variables} and \ptref{p:constants}): +\begin{verbatim} +variable 'fact +{ 'fact @ execute } : fact +{ ?dup { dup 1- fact * } { 1 } cond } 'fact ! +5 fact . +\end{verbatim} +This somewhat longer definition of the factorial avoids dictionary lookups at execution time by introducing a special variable {\tt 'fact} to hold the final definition of the factorial.\footnote{Variables that hold a {\em WordDef\/} to be {\tt execute}d later are called {\em vector variables}. The process of replacing {\tt fact} with {\tt 'fact @ execute}, where {\tt 'fact} is a vector variable, is called {\em vectorization}.} Then {\tt fact} is defined to execute whatever {\em WordDef} is currently stored in {\tt 'fact}, and once the body of the recursive definition of the factorial is constructed, it is stored into this variable by means of the phrase {\tt 'fact !}, which replaces the more customary phrase {\tt :\ fact}. + +We could rewrite the above definition by using special ``getter'' and ``setter'' words for vector variable {\tt 'fact} as we did for variables in \ptref{p:variables}: +\begin{verbatim} +variable 'fact +{ 'fact @ execute } : fact +{ 'fact ! } : :fact +forget 'fact +{ ?dup { dup 1- fact * } { 1 } cond } :fact +5 fact . +\end{verbatim} +If we need to introduce a lot of recursive and mutually-recursive definitions, we might first introduce a custom defining word (cf.~\ptref{p:custom.defw}) for simultaneously defining both the ``getter'' and the ``setter'' words for anonymous vector variables, similarly to what we did in~\ptref{p:variables}: +\begin{verbatim} +{ hole dup 1 { @ execute } does create 1 ' ! does create +} : vector-set +vector-set fact :fact +{ ?dup { dup 1- fact * } { 1 } cond } :fact +5 fact . +\end{verbatim} +The first three lines of this fragment define {\tt fact} and {\tt :fact} essentially in the same way they had been defined in the first four lines of the previous fragment. + +If we wish to make {\tt fact} unchangeable in the future, we can add a {\tt forget :fact} line once the definition of the factorial is complete: +\begin{verbatim} +{ hole dup 1 { @ execute } does create 1 ' ! does create +} : vector-set +vector-set fact :fact +{ ?dup { dup 1- fact * } { 1 } cond } :fact +forget :fact +5 fact . +\end{verbatim} +Alternatively, we can modify the definition of {\tt vector-set} in such a way that {\tt :fact} would forget itself once it is invoked: +\begin{verbatim} +{ hole dup 1 { @ execute } does create + bl word tuck 2 { (forget) ! } does swap 0 (create) +} : vector-set-once +vector-set-once fact :fact +{ ?dup { dup 1- fact * } { 1 } cond } :fact +5 fact . +\end{verbatim} + +However, some vector variables must be modified more than once, for instance, to modify the behavior of the comparison word {\tt less} in a merge sort algorithm: +\begin{verbatim} +{ hole dup 1 { @ execute } does create 1 ' ! does create +} : vector-set +vector-set sort :sort +vector-set merge :merge +vector-set less :less +{ null null rot + { dup null? not } + { uncons swap rot cons -rot } while drop +} : split +{ dup null? { drop } { + over null? { nip } { + over car over car less ' swap if + uncons rot merge cons + } cond + } cond +} :merge +{ dup null? { + dup cdr null? { + split sort swap sort merge + } ifnot + } ifnot +} :sort +forget :merge +forget :sort +// set `less` to compare numbers, sort a list of numbers +' < :less +3 1 4 1 5 9 2 6 5 9 list +dup .l cr sort .l cr +// set `less` to compare strings, sort a list of strings +{ $cmp 0< } :less +"once" "upon" "a" "time" "there" "lived" "a" "kitten" 8 list +dup .l cr sort .l cr +\end{verbatim} +producing the following output: +\begin{verbatim} +(3 1 4 1 5 9 2 6 5) +(1 1 2 3 4 5 5 6 9) +("once" "upon" "a" "time" "there" "lived" "a" "kitten") +("a" "a" "kitten" "lived" "once" "there" "time" "upon") +\end{verbatim} + +\mysubsection{Throwing exceptions}\label{p:exception.ops} +Two built-in words are used to throw exceptions: +\begin{itemize} +\item {\tt abort} ($S$ -- ), throws an exception with an error message taken from {\em String\/} $S$. +\item {\tt abort"$\langle\textit{message}\rangle$"} ($x$ -- ), throws an exception with the error message $\langle\textit{message}\rangle$ if $x$ is a non-zero integer. +\end{itemize} +The exception thrown by these words is represented by the C++ exception {\tt fift::IntError} with its value equal to the specified string. It is normally handled within the Fift interpreter itself by aborting all execution up to the top level and printing a message with the name of the source file being interpreted, the line number, the currently interpreted word, and the specified error message. For instance: +\begin{verbatim} +{ dup 0= abort"Division by zero" / } : safe/ +5 0 safe/ . +\end{verbatim} +prints ``{\tt safe/: Division by zero}'', without the usual ``{\tt ok}''. The stack is cleared in the process. + +Incidentally, when the Fift interpreter encounters an unknown word that cannot be parsed as an integer literal, an exception with the message ``{\tt -?}'' is thrown, with the effect indicated above, including the stack being cleared. + +\mysection{Dictionary, interpreter, and compiler}\label{s:dict.compiler} +In this chapter we present several specific Fift words for dictionary manipulation and compiler control. The ``compiler'' is the part of the Fift interpreter that builds lists of word references (represented by {\em WordList\/} stack values) from word names; it is activated by the primitive ``{\tt \{}'' employed for defining blocks as explained in~\ptref{p:colon.def} and~\ptref{p:blocks}. + +Most of the information included in this chapter is rather sophisticated and may be skipped during a first reading. However, the techniques described here are heavily employed by the Fift assembler, used to compile TVM code. Therefore, this section is indispensable if one wishes to understand the current implementation of the Fift assembler. + +\mysubsection{The state of the Fift interpreter}\label{sp:fift.state} +The state of the Fift interpreter is controlled by an internal integer variable called {\tt state}, currently unavailable from Fift itself. When {\tt state} is zero, all words parsed from the input (i.e., the Fift source file or the standard input in the interactive mode) are looked up in the dictionary and immediately executed afterwards. When {\tt state} is positive, the words found in the dictionary are not executed. Instead, they (or rather the references to their current definitions) are {\em compiled}, i.e., added to the end of the {\em WordList\/} being constructed. + +Typically, {\tt state} equals the number of the currently open blocks. For instance, after interpreting ``{\tt \{ 0= \{ ."zero"}'' the {\tt state} variable will be equal to two, because there are two nested blocks. The {\em WordList\/} being constructed is kept at the top of the stack. + +The primitive ``{\tt \{}'' simply pushes a new empty {\em WordList\/} into the stack, and increases {\tt state} by one. The primitive ``{\tt \}}'' throws an exception if {\tt state} is already zero; otherwise it decreases {\tt state} by one, and leaves the resulting {\em WordList\/} in the stack, representing the block just constructed.\footnote{The word {\tt \}} also transforms this {\em WordList\/} into a {\em WordDef}, which has a different type tag and therefore is a different Fift value, even if the same underlying C++ object is used by the C++ implementation.} After that, if the resulting value of {\tt state} is non-zero, the new block is compiled as a literal (unnamed constant) into the encompassing block. + +\mysubsection{Active and ordinary words}\label{p:active.words} +All dictionary words have a special flag indicating whether they are {\em active\/} words or {\em ordinary\/} words. By default, all words are ordinary. In particular, all words defined with the aid of ``{\tt :}'' and {\tt constant} are ordinary. + +When the Fift interpreter finds a word definition in the dictionary, it checks whether it is an ordinary word. If it is, then the current word definition is either executed (if {\tt state} is zero) or ``compiled'' (if {\tt state} is greater than zero) as explained in~\ptref{sp:fift.state}. + +On the other hand, if the word is active, then it is always executed, even if {\tt state} is positive. An active word is expected to leave some values $x_1$ \dots $x_n$ $n$ $e$ in the stack, where $n\geq 0$ is an integer, $x_1\dots x_n$ are $n$ values of arbitrary types, and $e$ is an execution token (a value of type {\em WordDef\/}). After that, the interpreter performs different actions depending on {\tt state}: if {\tt state} is zero, then $n$ is discarded and $e$ is executed, as if a {\tt nip execute} phrase were found. If {\tt state} is non-zero, then this collection is ``compiled'' in the current {\em WordList} (located immediately below $x_1$ in the stack) in the same way as if the {\tt (compile)} primitive were invoked. This compilation amounts to adding some code to the end of the current {\em WordList\/} that would push $x_1$, \dots, $x_n$ into the stack when invoked, and then adding a reference to $e$ (representing a delayed execution of $e$). If $e$ is equal to the special value {\tt 'nop}, representing an execution token that does nothing when executed, then this last step is omitted. + +\mysubsection{Compiling literals} +When the Fift interpreter encounters a word that is absent from the dictionary, it invokes the primitive {\tt (number)} to attempt to parse it as an integer or fractional literal. If this attempt succeeds, then the special value {\tt 'nop} is pushed, and the interpretation proceeds in the same way as if an active word were encountered. In other words, if {\tt state} is zero, then the literal is simply left in the stack; otherwise, {\tt (compile)} is invoked to modify the current {\em WordList\/} so that it would push the literal when executed. + +\mysubsection{Defining new active words}\label{p:def.active} +New active words are defined similarly to new ordinary words, but using ``{\tt ::}'' instead of ``{\tt :}''. For instance, +\begin{verbatim} +{ bl word 1 ' type } :: say +\end{verbatim} +defines the active word {\tt say}, which scans the next blank-separated word after itself and compiles it as a literal along with a reference to the current definition of {\tt type} into the current {\em WordList} (if {\tt state} is non-zero, i.e., if the Fift interpreter is compiling a block). When invoked, this addition to the block will push the stored string into the stack and execute {\tt type}, thus printing the next word after {\tt say}. On the other hand, if {\tt state} is zero, then these two actions are performed by the Fift interpreter immediately. In this way, +\begin{verbatim} +1 2 say hello + . +\end{verbatim} +will print ``{\tt hello3 ok}'', while +\begin{verbatim} +{ 2 say hello + . } : test +1 test 4 test +\end{verbatim} +will print ``{\tt hello3 hello6 ok}''. + +Of course, a block may be used to represent the required action instead of {\tt ' type}. For instance, if we want a version of {\tt say} that prints a space after the stored word, we can write +\begin{verbatim} +{ bl word 1 { type space } } :: say +{ 2 say hello + . } : test +1 test 4 test +\end{verbatim} +to obtain ``{\tt hello 3 hello 6 ok}''. + +Incidentally, the words {\tt "} (introducing a string literal) and {\tt ."} (printing a string literal) can be defined as follows: +\begin{verbatim} +{ char " word 1 'nop } ::_ " +{ char " word 1 ' type } ::_ ." +\end{verbatim} +The new defining word ``{\tt ::\_}'' defines an active {\em prefix\/} word, i.e., an active word that does not require a space afterwards. + +\mysubsection{Defining words and dictionary manipulation}\label{p:dict.create} +{\em Defining words\/} are words that define new words in the Fift dictionary. For instance, ``{\tt :}'', ``{\tt ::\_}'', and {\tt constant} are defining words. All of these defining words might have been defined using the primitive {\tt (create)}; in fact, the user can introduce custom defining words if so desired. Let us list some defining words and dictionary manipulation words: +\begin{itemize} +\item {\tt create $\langle\textit{word-name}\rangle$} ($e$ -- ), defines a new ordinary word with the name equal to the next word scanned from the input, using {\em WordDef\/} $e$ as its definition. If the word already exists, it is tacitly redefined. +\item {\tt (create)} ($e$ $S$ $x$ -- ), creates a new word with the name equal to {\em String\/} $S$ and definition equal to {\em WordDef\/}~$e$, using flags passed in {\em Integer\/} $0\leq x\leq 3$. If bit $+1$ is set in $x$, creates an active word; if bit $+2$ is set in $x$, creates a prefix word. +\item {\tt : $\langle\textit{word-name}\rangle$} ($e$ -- ), defines a new ordinary word $\langle\textit{word-name}\rangle$ in the dictionary using {\em WordDef} $e$ as its definition. If the specified word is already present in the dictionary, it is tacitly redefined. +\item {\tt forget $\langle\textit{word-name}\rangle$} ( -- ), forgets (removes from the dictionary) the definition of the specified word. +\item {\tt (forget)} ($S$ -- ), forgets the word with the name specified in {\em String}~$S$. If the word is not found, throws an exception. +\item {\tt :\_ $\langle\textit{word-name}\rangle$} ($e$ -- ), defines a new ordinary {\em prefix\/} word $\langle\textit{word-name}\rangle$, meaning that a blank or an end-of-line character is not required by the Fift input parser after the word name. In all other respects it is similar to ``{\tt:}''. +\item {\tt :: $\langle\textit{word-name}\rangle$} ($e$ -- ), defines a new {\em active\/} word $\langle\textit{word-name}\rangle$ in the dictionary using {\em WordDef} $e$ as its definition. If the specified word is already present in the dictionary, it is tacitly redefined. +\item {\tt ::\_ $\langle\textit{word-name}\rangle$} ($e$ -- ), defines a new active {\em prefix\/} word $\langle\textit{word-name}\rangle$, meaning that a blank or an end-of-line character is not required by the Fift input parser after the word name. In all other respects it is similar to ``{\tt::}''. +\item {\tt constant $\langle\textit{word-name}\rangle$} ($x$ -- ), defines a new ordinary word $\langle\textit{word-name}\rangle$ that would push the given value $x$ when invoked. +\item {\tt 2constant $\langle\textit{word-name}\rangle$} ($x$ $y$ -- ), defines a new ordinary word named $\langle\textit{word-name}\rangle$ that would push the given values $x$ and $y$ (in that order) when invoked. +\item {\tt =: $\langle\textit{word-name}\rangle$} ($x$ -- ), defines a new ordinary word $\langle\textit{word-name}\rangle$ that would push the given value $x$ when invoked, similarly to {\tt constant}, but works inside blocks and colon definitions. +\item {\tt 2=: $\langle\textit{word-name}\rangle$} ($x$ $y$ -- ), defines a new ordinary word $\langle\textit{word-name}\rangle$ that would push the given values $x$ and $y$ (in that order) when invoked, similarly to {\tt 2constant}, but works inside blocks and colon definitions. +\end{itemize} +Notice that most of the above words might have been defined in terms of {\tt (create)}: +\begin{verbatim} +{ bl word 1 2 ' (create) } "::" 1 (create) +{ bl word 0 2 ' (create) } :: : +{ bl word 2 2 ' (create) } :: :_ +{ bl word 3 2 ' (create) } :: ::_ +{ bl word 0 (create) } : create +{ bl word (forget) } : forget +\end{verbatim} + +\mysubsection{Dictionary lookup}\label{p:dict.lookup} +The following words can be used to look up words in the dictionary: +\begin{itemize} +\item {\tt ' $\langle\textit{word-name}\rangle$} ( -- $e$), pushes the definition of the word $\langle\textit{word-name}\rangle$, recovered at the compile time. If the indicated word is not found, throws an exception. Notice that {\tt ' $\langle\textit{word-name}\rangle$ execute} is always equivalent to {\tt $\langle\textit{word-name}\rangle$} for ordinary words, but not for active words. +\item {\tt nop} ( -- ), does nothing. +\item {\tt 'nop} ( -- $e$), pushes the default definition of {\tt nop}---an execution token that does nothing when executed. +\item {\tt find} ($S$ -- $e$ $-1$ or $e$ $1$ or $0$), looks up {\em String\/} $S$ in the dictionary and returns its definition as a {\em WordDef\/}~$e$ if found, followed by $-1$ for ordinary words or $1$ for active words. Otherwise pushes $0$. +\item {\tt (') $\langle\textit{word-name}\rangle$} ( -- $e$), similar to {\tt '}, but returns the definition of the specified word at execution time, performing a dictionary lookup each time it is invoked. May be used to recover current values of constants inside word definitions and other blocks by using the phrase {\tt (') $\langle\textit{word-name}\rangle$ execute}. +\item {\tt @' $\langle\textit{word-name}\rangle$} ( -- $e$), similar to {\tt (')}, but recovers the definition of the specified word at execution time, performing a dictionary lookup each time it is invoked, and then executes this definition. May be used to recover current values of constants inside word definitions and other blocks by using the phrase {\tt @' $\langle\textit{word-name}\rangle$}, equivalent to {\tt (') $\langle\textit{word-name}\rangle$ execute}, cf.~\ptref{p:constants}. +\item {\tt [compile] $\langle\textit{word-name}\rangle$} ( -- ), compiles $\langle\textit{word-name}\rangle$ as if it were an ordinary word, even if it is active. Essentially equivalent to {\tt ' $\langle\textit{word-name}\rangle$ execute}. +\item {\tt words} ( -- ), prints the names of all words currently defined in the dictionary. +\end{itemize} + +\mysubsection{Creating and manipulating word lists}\label{p:wordlist.ops} +In the Fift stack, lists of references to word definitions and literals, to be used as blocks or word definitions, are represented by the values of the type {\em WordList}. Some words for manipulating {\em WordList\/}s include: +\begin{itemize} +\item {\tt \{} ( -- $l$), an active word that increases internal variable {\tt state} by one and pushes a new empty {\em WordList\/} into the stack. +\item {\tt \}} ($l$ -- $e$), an active word that transforms a {\em WordList} $l$ into a {\em WordDef\/} (an execution token) $e$, thus making all further modifications of~$l$ impossible, and decreases internal variable {\tt state} by one and pushes the integer $1$, followed by a {\tt 'nop}. The net effect is to transform the constructed {\em WordList\/} into an execution token and push this execution token into the stack, either immediately or during the execution of an outer block. +\item {\tt (\{)} ( -- $l$), pushes an empty {\em WordList\/} into the stack. +\item {\tt (\})} ($l$ -- $e$), transforms a {\em WordList\/} into an execution token, making all further modifications impossible. +\item {\tt (compile)} ($l$ $x_1$ \dots $x_n$ $n$ $e$ -- $l'$), extends {\em WordList\/}~$l$ so that it would push $0\leq n\leq 255$ values $x_1$, \ldots, $x_n$ into the stack and execute the execution token $e$ when invoked, where $0\leq n\leq 255$ is an {\em Integer}. If $e$ is equal to the special value {\tt 'nop}, the last step is omitted. +\item {\tt does} ($x_1$ \dots $x_n$ $n$ $e$ -- $e'$), creates a new execution token $e'$ that would push $n$ values $x_1$, \dots, $x_n$ into the stack and then execute $e$. It is roughly equivalent to a combination of {\tt (\{)}, {\tt (compile)}, and {\tt (\})}. +\end{itemize} + +\mysubsection{Custom defining words}\label{p:custom.defw} +The word {\tt does} is actually defined in terms of simpler words: +\begin{verbatim} +{ swap ({) over 2+ -roll swap (compile) (}) } : does +\end{verbatim} +It is especially useful for defining custom defining words. For instance, {\tt constant} and {\tt 2constant} may be defined with the aid of {\tt does} and {\tt create}: +\begin{verbatim} +{ 1 'nop does create } : constant +{ 2 'nop does create } : 2constant +\end{verbatim} +Of course, non-trivial actions may be performed by the words defined by means of such custom defining words. For instance, +\begin{verbatim} +{ 1 { type space } does create } : says +"hello" says hello +"unknown error" says error +{ hello error } : test +test +\end{verbatim} +will print ``{\tt hello unknown error ok}'', because {\tt hello} is defined by means of a custom defining word {\tt says} to print ``{\tt hello}'' whenever invoked, and similarly {\tt error} prints ``{\tt unknown error}'' when invoked. The above definitions are essentially equivalent to +\begin{verbatim} +{ ."hello" } : hello +{ ."unknown error" } : error +\end{verbatim} +However, custom defining words may perform more sophisticated actions when invoked, and preprocess their arguments at compile time. For instance, the message can be computed in a non-trivial fashion: +\begin{verbatim} +"Hello, " "world!" $+ says hw +\end{verbatim} +defines word {\tt hw}, which prints ``Hello, world!'' when invoked. The string with this message is computed once at compile time (when {\tt says} is invoked), not at execution time (when {\tt hw} is invoked). + +\mysection{Cell manipulation} +We have discussed the basic Fift primitives not related to TVM or the TON Blockchain so far. Now we will turn to TON-specific words, used to manipulate {\em Cell\/}s. + +\mysubsection{Slice literals}\label{p:slice.lit} +Recall that a (TVM) {\em Cell\/} consists of at most 1023 data bits and at most four references to other {\em Cell\/}s, a {\em Slice\/} is a read-only view of a portion of a {\em Cell}, and a {\em Builder\/} is used to create new {\em Cell\/}s. Fift has special provisions for defining {\em Slice\/} literals (i.e., unnamed constants), which can also be transformed into {\em Cells\/} if necessary. + +{\em Slice\/} literals are introduced by means of active prefix words {\tt x\{} and {\tt b\{}: +\begin{itemize} +\item {\tt b\{$\langle\textit{binary-data}\rangle$\}} ( -- $s$), creates a {\em Slice}~$s$ that contains no references and up to 1023 data bits specified in $\langle\textit{binary-data}\rangle$, which must be a string consisting only of the characters `{\tt 0}' and `{\tt 1}'. +\item {\tt x\{$\langle\textit{hex-data}\rangle$\}} ( -- $s$), creates a {\em Slice}~$s$ that contains no references and up to 1023 data bits specified in $\langle\textit{hex-data}\rangle$. More precisely, each hex digit from $\langle\textit{hex-data}\rangle$ is transformed into four binary digits in the usual fashion. After that, if the last character of $\langle\textit{hex-data}\rangle$ is an underscore {\tt \_}, then all trailing binary zeroes and the binary one immediately preceding them are removed from the resulting binary string (cf.~\cite[1.0]{TVM} for more details). +\end{itemize} +In this way, {\tt b\{00011101\}} and {\tt x\{1d\}} both push the same {\em Slice} consisting of eight data bits and no references. Similarly, {\tt b\{111010\}} and {\tt x\{EA\_\}} push the same {\em Slice\/} consisting of six data bits. An empty {\em Slice} can be represented as {\tt b\{\}} or {\tt x\{\}}. + +If one wishes to define constant {\em Slice}\/s with some {\em Cell\/} references, the following words might be used: +\begin{itemize} +\item {\tt |\_} ($s$ $s'$ -- $s''$), given two {\em Slice}\/s $s$ and $s'$, creates a new {\em Slice} $s''$, which is obtained from $s$ by appending a new reference to a {\em Cell\/} containing $s'$. +\item {\tt |+} ($s$ $s'$ -- $s''$), concatenates two {\em Slice}\/s $s$ and $s'$. This means that the data bits of the new {\em Slice} $s''$ are obtained by concatenating the data bits of $s$ and $s'$, and the list of {\em Cell\/} references of $s''$ is constructed similarly by concatenating the corresponding lists for $s$ and $s'$. +\end{itemize} + +\mysubsection{Builder primitives}\label{p:builder.ops} +The following words can be used to manipulate {\em Builder\/}s, which can later be used to construct new {\em Cell\/}s: +\begin{itemize} +\item {\tt } ($b$ -- $c$), transforms a {\em Builder\/}~$b$ into a new {\em Cell\/}~$c$ containing the same data as~$b$. +\item {\tt i,} ($b$ $x$ $y$ -- $b'$), appends the big-endian binary representation of a signed $y$-bit integer~$x$ to {\em Builder\/}~$b$, where $0\leq y\leq 257$. If there is not enough room in $b$ (i.e., if $b$ already contains more than $1023-y$ data bits), or if {\em Integer\/}~$x$ does not fit into $y$ bits, an exception is thrown. +\item {\tt u,} ($b$ $x$ $y$ -- $b'$), appends the big-endian binary representation of an unsigned $y$-bit integer~$x$ to {\em Builder\/}~$b$, where $0\leq y\leq 256$. If the operation is impossible, an exception is thrown. +\item {\tt ref,} ($b$ $c$ -- $b'$), appends to {\em Builder\/}~$b$ a reference to {\em Cell\/}~$c$. If $b$ already contains four references, an exception is thrown. +\item {\tt s,} ($b$ $s$ -- $b'$), appends data bits and references taken from {\em Slice\/}~$s$ to {\em Builder\/}~$b$. +\item {\tt sr,} ($b$ $s$ -- $b'$), constructs a new {\em Cell\/} containing all data and references from {\em Slice\/}~$s$, and appends a reference to this cell to {\em Builder\/}~$b$. Equivalent to {\tt ref,}. +\item {\tt \$,} ($b$ $S$ -- $b'$), appends {\em String\/}~$S$ to {\em Builder\/}~$b$. The string is interpreted as a binary string of length $8n$, where $n$ is the number of bytes in the UTF-8 representation of~$S$. +\item {\tt B,} ($b$ $B$ -- $b'$), appends {\em Bytes\/}~$B$ to {\em Builder\/}~$b$. +\item {\tt b+} ($b$ $b'$ -- $b''$), concatenates two {\em Builders\/} $b$ and~$b'$. +\item {\tt bbits} ($b$ -- $x$), returns the number of data bits already stored in {\em Builder\/}~$b$. The result $x$ is an {\em Integer\/} in the range $0\dots1023$. +\item {\tt brefs} ($b$ -- $x$), returns the number of references already stored in {\em Builder\/}~$b$. The result $x$ is an {\em Integer\/} in the range $0\dots4$. +\item {\tt bbitrefs} ($b$ -- $x$ $y$), returns both the number of data bits $x$ and the number of references $y$ already stored in {\em Builder\/}~$b$. +\item {\tt brembits} ($b$ -- $x$), returns the maximum number of additional data bits that can be stored in {\em Builder\/}~$b$. Equivalent to {\tt bbits 1023 swap -}. +\item {\tt bremrefs} ($b$ -- $x$), returns the maximum number of additional cell references that can be stored in {\em Builder\/}~$b$. +\item {\tt brembitrefs} ($b$ -- $x$ $y$), returns both the maximum number of additional data bits $0\leq x\leq 1023$ and the maximum number of additional cell references $0\leq y\leq 4$ that can be stored in {\em Builder\/}~$b$. +\end{itemize} + +The resulting {\em Builder\/} may be inspected by means of the non-destructive stack dump primitive {\tt .s}, or by the phrase {\tt b> } : mkTest +17239 -1000000001 mkTest +} ($s$ -- ), throws an exception if {\em Slice\/}~$s$ is non-empty. It usually marks the end of the deserialization of a cell, checking whether there are any unprocessed data bits or references left. +\item {\tt i@} ($s$ $x$ -- $y$), fetches a signed big-endian $x$-bit integer from the first $x$ bits of {\em Slice}~$s$. If $s$ contains less than $x$ data bits, an exception is thrown. +\item {\tt i@+} ($s$ $x$ -- $y$ $s'$), fetches a signed big-endian $x$-bit integer from the first $x$ bits of {\em Slice}~$s$ similarly to {\tt i@}, but returns the remainder of $s$ as well. +\item {\tt i@?} ($s$ $x$ -- $y$ $-1$ or $0$), fetches a signed integer from a {\em Slice\/} similarly to {\tt i@}, but pushes integer $-1$ afterwards on success. If there are less than $x$ bits left in $s$, pushes integer $0$ to indicate failure. +\item {\tt i@?+} ($s$ $x$ -- $y$ $s'$ $-1$ or $s$ $0$), fetches a signed integer from {\em Slice\/}~$s$ and computes the remainder of this {\em Slice\/} similarly to {\tt i@+}, but pushes $-1$ afterwards to indicate success. On failure, pushes the unchanged {\em Slice\/}~$s$ and $0$ to indicate failure. +\item {\tt u@}, {\tt u@+}, {\tt u@?}, {\tt u@?+}, counterparts of {\tt i@}, {\tt i@+}, {\tt i@?}, {\tt i@?+} for deserializing unsigned integers. +\item {\tt B@} ($s$ $x$ -- $B$), fetches first $x$ bytes (i.e., $8x$ bits) from {\em Slice}~$s$, and returns them as a {\em Bytes\/} value~$B$. If there are not enough data bits in $s$, throws an exception. +\item {\tt B@+} ($s$ $x$ -- $B$ $s'$), similar to {\tt B@}, but returns the remainder of {\em Slice\/} $s$ as well. +\item {\tt B@?} ($s$ $x$ -- $B$ $-1$ or $0$), similar to {\tt B@}, but uses a flag to indicate failure instead of throwing an exception. +\item {\tt B@?+} ($s$ $x$ -- $B$ $s'$ $-1$ or $s$ $0$), similar to {\tt B@+}, but uses a flag to indicate failure instead of throwing an exception. +\item {\tt \$@}, {\tt \$@+}, {\tt \$@?}, {\tt \$@?+}, counterparts of {\tt B@}, {\tt B@+}, {\tt B@?}, {\tt B@?+}, returning the result as a (UTF-8) {\em String} instead of a {\em Bytes} value. These primitives do not check whether the byte sequence read is a valid UTF-8 string. +\item {\tt ref@} ($s$ -- $c$), fetches the first reference from {\em Slice\/}~$s$ and returns the {\em Cell}~$c$ referred to. If there are no references left, throws an exception. +\item {\tt ref@+} ($s$ -- $s'$ $c$), similar to {\tt ref@}, but returns the remainder of $s$ as well. +\item {\tt ref@?} ($s$ -- $c$ $-1$ or $0$), similar to {\tt ref@}, but uses a flag to indicate failure instead of throwing an exception. +\item {\tt ref@?+} ($s$ -- $s'$ $c$ $-1$ or $s$ $0$), similar to {\tt ref@+}, but uses a flag to indicate failure instead of throwing an exception. +\item {\tt empty?} ($s$ -- $?$), checks whether a {\em Slice\/} is empty (i.e., has no data bits and no references left), and returns $-1$ or $0$ accordingly. +\item {\tt remaining} ($s$ -- $x$ $y$), returns both the number of data bits $x$ and the number of cell references $y$ remaining in {\em Slice\/}~$s$. +\item {\tt sbits} ($s$ -- $x$), returns the number of data bits $x$ remaining in {\em Slice\/}~$s$. +\item {\tt srefs} ($s$ -- $x$), returns the number of cell references $x$ remaining in {\em Slice\/}~$s$. +\item {\tt sbitrefs} ($s$ -- $x$ $y$), returns both the number of data bits $x$ and the number of cell references $y$ remaining in {\em Slice\/}~$s$. Equivalent to {\tt remaining}. +\item {\tt \$>s} ($S$ -- $s$), transforms {\em String}~$S$ into a {\em Slice}. Equivalent to {\tt c} ($s$ -- $c$), creates a {\em Cell}~$c$ directly from a {\em Slice}~$s$. Equivalent to {\tt }. +\item {\tt csr.} ($s$ -- ), recursively prints a {\em Slice}~$s$. On the first line, the data bits of $s$ are displayed in hexadecimal form embedded into an {\tt x\{\dots\}} construct similar to the one used for {\em Slice\/} literals (cf.~\ptref{p:slice.lit}). On the next lines, the cells referred to by $s$ are printed with larger indentation. +\end{itemize} +For instance, values of the TL-B type {\tt Test} discussed in~\ptref{p:builder.ops} +\begin{verbatim} +test#4a first:uint16 second:int32 = Test; +\end{verbatim} +may be deserialized as follows: +\begin{verbatim} +{ abort"constructor tag mismatch" + 16 u@+ 32 i@+ s> } : unpackTest +x{4A4357C46535FF} s>c unpackTest swap . . +\end{verbatim} +prints ``{\tt 17239 -1000000001 ok}'' as expected. + +Of course, if one needs to check constructor tags often, a helper word can be defined for this purpose: +\begin{verbatim} +{ dup remaining abort"references in constructor tag" + tuck u@ -rot u@+ -rot <> abort"constructor tag mismatch" +} : tag? +{ } : unpackTest +x{4A4357C46535FF} s>c unpackTest swap . . +\end{verbatim} +We can do even better with the aid of active prefix words (cf.~\ptref{p:active.words} and~\ptref{p:def.active}): +\begin{verbatim} +{ dup remaining abort"references in constructor tag" + dup 256 > abort"constructor tag too long" + tuck u@ 2 { -rot u@+ -rot <> abort"constructor tag mismatch" } +} : (tagchk) +{ [compile] x{ 2drop (tagchk) } ::_ ?x{ +{ [compile] b{ 2drop (tagchk) } ::_ ?b{ +{ } : unpackTest +x{4A4357C46535FF} s>c unpackTest swap . . +\end{verbatim} +A shorter but less efficient solution would be to reuse the previously defined {\tt tag?}: +\begin{verbatim} +{ [compile] x{ drop ' tag? } ::_ ?x{ +{ [compile] b{ drop ' tag? } ::_ ?b{ +x{11EF55AA} ?x{11E} dup csr. +?b{110} csr. +\end{verbatim} +first outputs ``{\tt x\{F55AA\}}'', and then throws an exception with the message ``constructor tag mismatch''. + +\mysubsection{Cell hash operations}\label{p:hash.ops} +There are few words that operate on {\em Cell\/}s directly. The most important of them computes the {\em ($\Sha$-based) representation hash\/} of a given cell (cf.~\cite[3.1]{TVM}), which can be roughly described as the $\Sha$ hash of the cell's data bits concatenated with recursively computed hashes of the cells referred to by this cell: +\begin{itemize} +\item {\tt hash} ($c$ -- $B$), computes the $\Sha$-based representation hash of {\em Cell\/}~$c$ (cf.~\cite[3.1]{TVM}), which unambiguously defines $c$ and all its descendants (provided there are no collisions for $\Sha$). The result is returned as a {\em Bytes\/} value consisting of exactly 32 bytes. +\item {\tt shash} ($s$ -- $B$), computes the $\Sha$-based representation hash of a {\em Slice\/} by first transforming it into a cell. Equivalent to {\tt s>c hash}. +\end{itemize} + +\mysubsection{Bag-of-cells operations}\label{p:boc.ops} +A {\em bag of cells\/} is a collection of one or more cells along with all their descendants. It can usually be serialized into a sequence of bytes (represented by a {\em Bytes\/} value in Fift) and then saved into a file or transferred by network. Afterwards, it can be deserialized to recover the original cells. The TON Blockchain systematically represents different data structures (including the TON Blockchain blocks) as a tree of cells according to a certain TL-B scheme (cf.~\cite{TBC}, where this scheme is explained in detail), and then these trees of cells are routinely imported into bags of cells and serialized into binary files. + +Fift words for manipulating bags of cells include: +\begin{itemize} +\item {\tt B>boc} ($B$ -- $c$), deserializes a ``standard'' bag of cells (i.e., a bag of cells with exactly one root cell) represented by {\em Bytes} $B$, and returns the root {\em Cell\/} $c$. +\item {\tt boc+>B} ($c$ $x$ -- $B$), creates and serializes a ``standard'' bag of cells, containing one root {\em Cell\/}~$c$ along with all its descendants. An {\em Integer\/} parameter $0\leq x\leq 31$ is used to pass flags indicating the additional options for bag-of-cells serialization, with individual bits having the following effect: + \begin{itemize} + \item $+1$ enables bag-of-cells index creation (useful for lazy deserialization of large bags of cells). + \item $+2$ includes the CRC32-C of all data into the serialization (useful for checking data integrity). + \item $+4$ explicitly stores the hash of the root cell into the serialization (so that it can be quickly recovered afterwards without a complete deserialization). + \item $+8$ stores hashes of some intermediate (non-leaf) cells (useful for lazy deserialization of large bags of cells). + \item $+16$ stores cell cache bits to control caching of deserialized cells. + \end{itemize} +Typical values of $x$ are $x=0$ or $x=2$ for very small bags of cells (e.g., TON Blockchain external messages) and $x=31$ for large bags of cells (e.g., TON Blockchain blocks). +\item {\tt boc>B} ($c$ -- $B$), serializes a small ``standard'' bag of cells with root {\em Cell\/}~$c$ and all its descendants. Equivalent to {\tt 0 boc+>B}. +\end{itemize} +For instance, the cell created in~\ptref{p:builder.ops} with a value of TL-B {\tt Test} type may be serialized as follows: +\begin{verbatim} +{ } : mkTest +17239 -1000000001 mkTest boc>B Bx. +\end{verbatim} +outputs ``{\tt B5EE9C7201040101000000000900000E4A4357C46535FF ok}''. Here {\tt Bx.} is the word that prints the hexadecimal representation of a {\em Bytes} value. + +\mysubsection{Binary file I/O and Bytes manipulation}\label{p:bytes.ops} +The following words can be used to manipulate values of type {\em Bytes\/} (arbitrary byte sequences) and to read them from or write them into binary files: +\begin{itemize} +\item {\tt B\{$\langle{\textit{hex-digits}}\rangle$\}} ( -- $B$), pushes a {\em Bytes} literal containing data represented by an even number of hexadecimal digits. +\item {\tt Bx.} ($B$ -- ), prints the hexadecimal representation of a {\em Bytes\/} value. Each byte is represented by exactly two uppercase hexadecimal digits. +\item {\tt file>B} ($S$ -- $B$), reads the (binary) file with the name specified in {\em String\/}~$S$ and returns its contents as a {\em Bytes\/} value. If the file does not exist, an exception is thrown. +\item {\tt B>file} ($B$ $S$ -- ), creates a new (binary) file with the name specified in {\em String\/}~$S$ and writes data from {\em Bytes}~$B$ into the new file. If the specified file already exists, it is overwritten. +\item {\tt file-exists?} ($S$ -- $?$), checks whether the file with the name specified in {\em String\/}~$S$ exists. +\end{itemize} +For instance, the bag of cells created in the example in~\ptref{p:boc.ops} can be saved to disk as {\tt sample.boc} as follows: +\begin{verbatim} +{ } : mkTest +17239 -1000000001 mkTest boc>B "sample.boc" B>file +\end{verbatim} +It can be loaded and deserialized afterwards (even in another Fift session) by means of {\tt file>B} and {\tt B>boc}: +\begin{verbatim} +{ abort"constructor tag mismatch" + 16 u@+ 32 i@+ s> } : unpackTest +"sample.boc" file>B B>boc unpackTest swap . . +\end{verbatim} +prints ``{\tt 17239 -1000000001 ok}''. + +Additionally, there are several words for directly packing (serializing) data into {\em Bytes\/} values, and unpacking (deserializing) them afterwards. They can be combined with {\tt B>file} and {\tt file>B} to save data directly into binary files, and load them afterwards. +\begin{itemize} +\item {\tt Blen} ($B$ -- $x$), returns the length of a {\em Bytes\/} value~$B$ in bytes. +\item {\tt Bhash} ($B$ -- $B'$), computes the $\Sha$ hash of a {\em Bytes\/} value. The hash is returned as a 32-byte {\em Bytes\/} value. +\item {\tt B=} ($B$ $B'$ -- $?$), checks whether two {\em Bytes\/} sequences are equal. +\item {\tt Bcmp} ($B$ $B'$ -- $x$), lexicographically compares two {\em Bytes\/} sequences, and returns $-1$, $0$, or $1$, depending on the comparison result. +\item {\tt B>i@} ($B$ $x$ -- $y$), deserializes the first $x/8$ bytes of a {\em Bytes} value~$B$ as a signed big-endian $x$-bit {\em Integer}~$y$. +\item {\tt B>i@+} ($B$ $x$ -- $B'$ $y$), deserializes the first $x/8$ bytes of $B$ as a signed big-endian $x$-bit {\em Integer}~$y$ similarly to {\tt B>i@}, but also returns the remaining bytes of~$B$. +\item {\tt B>u@}, {\tt B>u@+}, variants of {\tt B>i@} and {\tt B>i@+} deserializing unsigned integers. +\item {\tt B>Li@}, {\tt B>Li@+}, {\tt B>Lu@}, {\tt B>Lu@+}, little-endian variants of {\tt B>i@}, {\tt B>i@+}, {\tt B>u@}, {\tt B>u@+}. +\item {\tt B|} ($B$ $x$ -- $B'$ $B''$), cuts the first $x$ bytes from a {\em Bytes} value~$B$, and returns both the first $x$ bytes ($B'$) and the remainder ($B''$) as new {\em Bytes} values. +\item {\tt i>B} ($x$ $y$ -- $B$), stores a signed big-endian $y$-bit {\em Integer}~$x$ into a {\em Bytes} value $B$ consisting of exactly $y/8$ bytes. Integer $y$ must be a multiple of eight in the range $0\ldots256$. +\item {\tt u>B} ($x$ $y$ -- $B$), stores an unsigned big-endian $y$-bit {\em Integer}~$x$ into a {\em Bytes} value $B$ consisting of exactly $y/8$ bytes, similarly to {\tt i>B}. +\item {\tt Li>B}, {\tt Lu>B}, little-endian variants of {\tt i>B} and {\tt u>B}. +\item {\tt B+} ($B'$ $B''$ -- $B$), concatenates two {\em Bytes\/} sequences. +\end{itemize} + +\mysection{TON-specific operations} +This chapter describes the TON-specific Fift words, with the exception of the words used for {\em Cell\/} manipulation, already discussed in the previous chapter. + +\mysubsection{Ed25519 cryptography}\label{p:ed25519.ops} +Fift offers an interface to the same Ed25519 elliptic curve cryptography used by TVM, described in Appendix~$A$ of~\cite{TBC}: +\begin{itemize} +\item {\tt now} ( -- $x$), returns the current Unixtime as an {\em Integer}. +\item {\tt newkeypair} ( -- $B$ $B'$), generates a new Ed25519 private/public key pair, and returns both the private key $B$ and the public key $B'$ as 32-byte {\em Bytes\/} values. The quality of the keys is good enough for testing purposes. Real applications must feed enough entropy into OpenSSL PRNG before generating Ed25519 keypairs. +\item {\tt priv>pub} ($B$ -- $B'$), computes the public key corresponding to a private Ed25519 key. Both the public key~$B'$ and the private key~$B$ are represented by 32-byte {\em Bytes\/} values. +\item {\tt ed25519\_sign} ($B$ $B'$ -- $B''$), signs data $B$ with the Ed25519 private key $B'$ (a 32-byte {\em Bytes\/} value) and returns the signature as a 64-byte {\em Bytes\/} value~$B''$. +\item {\tt ed25519\_sign\_uint} ($x$ $B'$ -- $B''$), converts a big-endian unsigned 256-bit integer $x$ into a 32-byte sequence and signs it using the Ed25519 private key $B'$ similarly to {\tt ed25519\_sign}. Equivalent to {\tt swap 256 u>B swap ed25519\_sign}. The integer $x$ to be signed is typically computed as the hash of some data. +\item {\tt ed25519\_chksign} ($B$ $B'$ $B''$ -- $?$), checks whether $B'$ is a valid Ed25519 signature of data $B$ with the public key $B''$. +\end{itemize} + +\mysubsection{Smart-contract address parser}\label{p:smc.addr.ops} +Two special words can be used to parse TON smart-contract addresses in human-readable (base64 or base64url) forms: +\begin{itemize} +\item {\tt smca>\$} ($x$ $y$ $z$ -- $S$), packs a standard TON smart-contract address with workchain $x$ (a signed 32-bit {\em Integer}) and in-workchain address $y$ (an unsigned 256-bit {\em Integer\/}) into a 48-character string $S$ (the human-readable representation of the address) according to flags~$z$. Possible individual flags in~$z$ are: $+1$ for non-bounceable addresses, $+2$ for testnet-only addresses, and $+4$ for base64url output instead of base64. +\item {\tt \$>smca} ($S$ -- $x$ $y$ $z$ $-1$ or $0$), unpacks a standard TON smart-contract address from its human-readable string representation~$S$. On success, returns the signed 32-bit workchain $x$, the unsigned 256-bit in-workchain address $y$, the flags $z$ (where $+1$ means that the address is non-bounceable, $+2$ that the address is testnet-only), and $-1$. On failure, pushes $0$. +\end{itemize} +A sample human-readable smart-contract address could be deserialized and displayed as follows: +\begin{verbatim} +"Ef9Tj6fMJP-OqhAdhKXxq36DL-HYSzCc3-9O6UNzqsgPfYFX" +$>smca 0= abort"bad address" +rot . swap x. . cr +\end{verbatim} +outputs ``{\tt -1 538fa7\dots 0f7d 0}'', meaning that the specified address is in workchain $-1$ (the masterchain of the TON Blockchain), and that the 256-bit address inside workchain $-1$ is 0x538\dots f7d. + +\mysubsection{Dictionary manipulation}\label{p:hashmap.ops} +Fift has several words for {\em hashmap\/} or {\em (TVM) dictionary\/} manipulation, corresponding to values of TL-B type {\tt HashmapE $n$ $X$} as described in~\cite[3.3]{TVM}. These (TVM) dictionaries are not to be confused with the Fift dictionary, which is a completely different thing. A dictionary of TL-B type {\tt HashmapE $n$ $X$} is essentially a key-value collection with distinct $n$-bit keys (where $0\leq n\leq 1023$) and values of an arbitrary TL-B type $X$. Dictionaries are represented by trees of cells (the complete layout may be found in \cite[3.3]{TVM}) and stored as values of type {\em Cell\/} or {\em Slice\/} in the Fift stack. +\begin{itemize} +\item {\tt dictnew} ( -- $s$), pushes a {\em Slice\/} that represents a new empty dictionary. +\item {\tt idict!} ($v$ $x$ $s$ $n$ -- $s'$ $-1$ or $s$ $0$), adds a new value $v$ (represented by a {\em Slice}) with key given by signed big-endian $n$-bit integer $x$ into dictionary $s$ with $n$-bit keys, and returns the new dictionary $s'$ and $-1$ on success. Otherwise the unchanged dictionary $s$ and $0$ are returned. +\item {\tt idict!+} ($v$ $x$ $s$ $n$ -- $s'$ $-1$ or $s$ $0$), adds a new key-value pair $(x,v)$ into dictionary $s$ similarly to {\tt idict!}, but fails if the key already exists by returning the unchanged dictionary $s$ and $0$. +\item {\tt b>idict!}, {\tt b>idict!+}, variants of {\tt idict!} and {\tt idict!+} accepting the new value $v$ in a {\em Builder\/} instead of a {\em Slice}. +\item {\tt udict!}, {\tt udict!+}, {\tt b>udict!}, {\tt b>udict!+}, variants of {\tt idict!}, {\tt idict!+}, {\tt b>idict!}, {\tt b>idict!+}, but with an unsigned $n$-bit integer $x$ used as a key. +\item {\tt idict@} ($x$ $s$ $n$ -- $v$ $-1$ or $0$), looks up the key represented by signed big-endian $n$-bit {\em Integer\/}~$x$ in the dictionary represented by {\em Slice\/}~$s$. If the key is found, returns the corresponding value as a {\em Slice\/}~$v$ and $-1$. Otherwise returns $0$. +\item {\tt udict@} ($x$ $s$ $n$ -- $v$ $-1$ or $0$), similar to {\tt idict@}, but with an {\em un}signed big-endian $n$-bit {\em Integer\/}~$x$ used as a key. +\item {\tt dictmap} ($s$ $n$ $e$ -- $s'$), applies execution token $e$ (i.e., an anonymous function) to each of the key-value pairs stored in a dictionary $s$ with $n$-bit keys. The execution token is executed once for each key-value pair, with a {\em Builder\/} $b$ and a {\em Slice\/} $v$ (containing the value) pushed into the stack before executing $e$. After the execution $e$ must leave in the stack either a modified {\em Builder\/} $b'$ (containing all data from~$b$ along with the new value $v'$) and $-1$, or $0$ indicating failure. In the latter case, the corresponding key is omitted from the new dictionary. +\item {\tt dictmerge} ($s$ $s'$ $n$ $e$ -- $s''$), combines two dictionaries $s$ and $s'$ with $n$-bit keys into one dictionary $s''$ with the same keys. If a key is present in only one of the dictionaries $s$ and $s'$, this key and the corresponding value are copied verbatim to the new dictionary $s''$. Otherwise the execution token (anonymous function) $e$ is invoked to merge the two values $v$ and $v'$ corresponding to the same key $k$ in $s$ and $s'$, respectively. Before $e$ is invoked, a {\em Builder\/}~$b$ and two {\em Slice}s $v$ and $v'$ representing the two values to be merged are pushed. After the execution $e$ leaves either a modified {\em Builder\/}~$b'$ (containing the original data from $b$ along with the combined value) and $-1$, or $0$ on failure. In the latter case, the corresponding key is omitted from the new dictionary. +\end{itemize} +Fift also offers some support for prefix dictionaries: +\begin{itemize} +\item {\tt pfxdict!} ($v$ $k$ $s$ $n$ -- $s'$ $-1$ or $s$ $0$), adds key-value pair $(k,v)$, both represented by {\em Slice\/}s, into a prefix dictionary $s$ with keys of length at most~$n$. On success, returns the modified dictionary $s'$ and $-1$. On failure, returns the original dictionary $s$ and $0$. +\item {\tt pfxdict!+} ($v$ $k$ $s$ $n$ -- $s'$ $-1$ or $s$ $0$), adds key-value pair $(k,v)$ into prefix dictionary $s$ similarly to {\tt pfxdict!}, but fails if the key already exists. +\item {\tt pfxdict@} ($k$ $s$ $n$ -- $v$ $-1$ or $0$), looks up key $k$ (represented by a {\em Slice\/}) in the prefix dictionary $s$ with the length of keys limited by $n$ bits. On success, returns the value found $v$ and $-1$. On failure, returns $0$. +\end{itemize} + +\mysubsection{Invoking TVM from Fift}\label{p:tvm.ops} +TVM can be linked with the Fift interpreter. In this case, several Fift primitives become available that can be used to invoke TVM with arguments provided from Fift. The arguments can be prepared in the Fift stack, which is passed in its entirety to the new instance of TVM. The resulting stack and the exit code are passed back to Fift and can be examined afterwards. +\begin{itemize} +\item {\tt runvmcode} (\dots $s$ -- \dots $x$), invokes a new instance of TVM with the current continuation {\tt cc} initialized from {\em Slice\/} $s$, thus executing code~$s$ in TVM. The original Fift stack (without $s$) is passed in its entirety as the initial stack of TVM. When TVM terminates, its resulting stack is used as the new Fift stack, with the exit code $x$ pushed at its top. If $x$ is non-zero, indicating that TVM has been terminated by an unhandled exception, the next stack entry from the top contains the parameter of this exception, and $x$ is the exception code. All other entries are removed from the stack in this case. +\item {\tt runvmdict} (\dots $s$ -- \dots $x$), invokes a new instance of TVM with the current continuation {\tt cc} initialized from {\em Slice\/} $s$ similarly to {\tt runvmcode}, but also initializes the special register {\tt c3} with the same value, and pushes a zero into the initial TVM stack before the TVM execution begins. In a typical application {\em Slice\/}~$s$ consists of a subroutine selection code that uses the top-of-stack {\em Integer\/} to select the subroutine to be executed, thus enabling the definition and execution of several mutually-recursive subroutines (cf.~\cite[4.6]{TVM} and~\ptref{p:asm.prog}). The selector equal to zero corresponds to the {\tt main()} subroutine in a large TVM program. +\item {\tt runvm} (\dots $s$ $c$ -- \dots $x$ $c'$), invokes a new instance of TVM with both the current continuation {\tt cc} and the special register {\tt c3} initialized from {\em Slice\/}~$s$, and pushes a zero into the initial TVM stack similarly to {\tt runvmdict}, but also initializes special register {\tt c4} (the ``root of persistent data'', cf.~\cite[1.4]{TVM}) with {\em Cell\/}~$c$. The final value of {\tt c4} is returned at the top of the final Fift stack as another {\em Cell\/}~$c'$. In this way one can emulate the execution of smart contracts that inspect or modify their persistent storage. +\item {\tt gasrunvmcode} (\dots $s$ $z$ -- \dots $x$ $z'$), a gas-aware version of {\tt runvmcode} that accepts an extra {\em Integer\/} argument $z$ (the original gas limit) at the top of the stack, and returns the gas consumed by this TVM run as a new top-of-stack {\em Integer\/} value~$z'$. +\item {\tt gasrunvmdict} (\dots $s$ $z$ -- \dots $x$ $z'$), a gas-aware version of {\tt runvmdict}. +\item {\tt gasrunvm} (\dots $s$ $c$ $z$ -- \dots $x$ $c'$ $z'$), a gas-aware version of {\tt runvm}. +\end{itemize} +For example, one can create an instance of TVM running some simple code as follows: +\begin{verbatim} +2 3 9 x{1221} runvmcode .s +\end{verbatim} +The TVM stack is initialized by three integers $2$, $3$, and $9$ (in this order; $9$ is the topmost entry), and then the {\em Slice\/} {\tt x\{1221\}} containing 16 data bits and no references is transformed into a TVM continuation and executed. By consulting Appendix~A of~\cite{TVM}, we see that {\tt x\{12\}} is the code of the TVM instruction {\tt XCHG s1, s2}, and that {\tt x\{21\}} is the code of the TVM instruction {\tt OVER} (not to be confused with the Fift primitive {\tt over}, which incidentally has the same effect on the stack). The result of the above execution is: +\begin{verbatim} +execute XCHG s1,s2 +execute OVER +execute implicit RET +3 2 9 2 0 + ok +\end{verbatim} +Here $0$ is the exit code (indicating successful TVM termination), and {\tt 3 2 9 2} is the final TVM stack state. + +If an unhandled exception is generated during the TVM execution, the code of this exception is returned as the exit code: +\begin{verbatim} +2 3 9 x{122} runvmcode .s +\end{verbatim} +produces +\begin{verbatim} +execute XCHG s1,s2 +handling exception code 6: invalid or too short opcode +default exception handler, terminating vm with exit code 6 +0 6 + ok +\end{verbatim} +Notice that TVM is executed with internal logging enabled, and its log is displayed in the standard output. + +Simple TVM programs may be represented by {\em Slice\/} literals with the aid of the {\tt x\{\dots\}} construct similarly to the above examples. More sophisticated programs are usually created with the aid of the Fift assembler as explained in the next chapter. + +\mysection{Using the Fift assembler} +The {\em Fift assembler\/} is a short program (currently less than 30KiB) written completely in Fift that transforms human-readable mnemonics of TVM instructions into their binary representation. For instance, one could write {\tt <\{ s1 s2 XCHG OVER \}>s} instead of {\tt x\{1221\}} in the example discussed in~\ptref{p:tvm.ops}, provided the Fift assembler has been loaded beforehand (usually by the phrase {\tt "Asm.fif" include}). + +\mysubsection{Loading the Fift assembler}\label{p:asm.load} +The Fift assembler is usually located in file {\tt Asm.fif} in the Fift library directory (which usually contains standard Fift library files such as {\tt Fift.fif}). It is typically loaded by putting the phrase {\tt "Asm.fif" include} at the very beginning of a program that needs to use Fift assembler: +\begin{itemize} +\item {\tt include} ($S$ -- ), loads and interprets a Fift source file from the path given by {\em String\/}~$S$. If the filename $S$ does not begin with a slash, the Fift include search path, typically taken from the {\tt FIFTPATH} environment variable or the {\tt -I} command-line argument of the Fift interpreter (and equal to {\tt /usr/lib/fift} if both are absent), is used to locate~$S$. +\end{itemize} +The current implementation of the Fift assembler makes heavy use of custom defining words (cf.~\ptref{p:custom.defw}); its source can be studied as a good example of how defining words might be used to write very compact Fift programs (cf.\ also the original edition of~\cite{Brodie}, where a simple 8080 Forth assembler is discussed). + +In the future, almost all of the words defined by the Fift assembler will be moved to a separate vocabulary (namespace). Currently they are defined in the global namespace, because Fift does not support namespaces yet. + +\mysubsection{Fift assembler basics} +The Fift assembler inherits from Fift its postfix operation notation, i.e., the arguments or parameters are written before the corresponding instructions. For instance, the TVM assembler instruction represented as {\tt XCHG s1,s2} in \cite{TVM} is represented in the Fift assembler as {\tt s1 s2 XCHG}. + +Fift assembler code is usually opened by a special opening word, such as {\tt <\{}, and terminated by a closing word, such as {\tt \}>} or {\tt \}>s}. For instance, +\begin{verbatim} +"Asm.fif" include +<{ s1 s2 XCHG OVER }>s +csr. +\end{verbatim} +compiles two TVM instructions {\tt XCHG s1,s2} and {\tt OVER}, and returns the result as a {\em Slice\/} (because {\tt \}>s} is used). The resulting {\em Slice\/} is displayed by {\tt csr.}, yielding +\begin{verbatim} +x{1221} +\end{verbatim} +One can use Appendix A of \cite{TVM} and verify that {\tt x\{12\}} is indeed the (codepage zero) code of the TVM instruction {\tt XCHG s1,s2}, and that {\tt x\{21\}} is the code of the TVM instruction {\tt OVER} (not to be confused with Fift primitive {\tt over}). + +In the future, we will assume that the Fift assember is already loaded and omit the phrase {\tt "Asm.fif" include} from our examples. + +The Fift assembler uses the Fift stack in a straightforward fashion, using the top several stack entries to hold a {\em Builder\/} with the code being assembled, and the arguments to TVM instructions. For example: +\begin{itemize} +\item {\tt <\{} ( -- $b$), begins a portion of Fift assembler code by pushing an empty {\em Builder\/} into the Fift stack (and potentially switching the namespace to the one containing all Fift assembler-specific words). Approximately equivalent to {\tt } ($b$ -- $b'$), terminates a portion of Fift assembler code and returns the assembled portion as a {\em Builder\/} (and potentially recovers the original namespace). Approximately equivalent to {\tt nop} in most situations. +\item {\tt \}>c} ($b$ -- $c$), terminates a portion of Fift assembler code and returns the assembled portion as a {\em Cell\/} (and potentially recovers the original namespace). Approximately equivalent to {\tt b>}. +\item {\tt \}>s} ($b$ -- $s$), terminates a portion of Fift assembler code similarly to {\tt \}>}, but returns the assembled portion as a {\em Slice\/}. Equivalent to {\tt \}>c s} is a valid way to assemble a {\tt PUSHINT 4063} instruction, because $239\cdot17=4063$. Notice that the multiplication is performed by Fift during assemble time, not during the TVM runtime. The latter computation might be performed by means of {\tt <\{ 239 INT 17 INT MUL \}>s}: +\begin{verbatim} +<{ 239 17 * INT }>s dup csr. runvmcode .s 2drop +<{ 239 INT 17 INT MUL }>s dup csr. runvmcode .s 2drop +\end{verbatim} +produces +\begin{verbatim} +x{810FDF} +execute PUSHINT 4063 +execute implicit RET +4063 0 + ok +x{8100EF8011A8} +execute PUSHINT 239 +execute PUSHINT 17 +execute MUL +execute implicit RET +4063 0 + ok +\end{verbatim} +Notice that the Fift assembler chooses the shortest encoding of the {\tt PUSHINT}~$x$ instruction depending on its argument~$x$. + +\mysubsection{Immediate arguments} +Some TVM instructions (such as {\tt PUSHINT}) accept immediate arguments. These arguments are usually passed to the Fift word assembling the corresponding instruction in the Fift stack. Integer immediate arguments are usually represented by {\em Integer\/}s, cells by {\em Cell\/}s, continuations by {\em Builder\/}s and {\em Cell\/}s, and cell slices by {\em Slice\/}s. For instance, {\tt 17 ADDCONST} assembles TVM instruction {\tt ADDCONST 17}, and {\tt x\{ABCD\_\} PUSHSLICE} assembles {\tt PUSHSLICE xABCD\_}: +\begin{verbatim} +239 <{ 17 ADDCONST x{ABCD_} PUSHSLICE }>s dup csr. +runvmcode . swap . csr. +\end{verbatim} +produces +\begin{verbatim} +x{A6118B2ABCD0} +execute ADDINT 17 +execute PUSHSLICE xABCD_ +execute implicit RET +0 256 x{ABCD_} +\end{verbatim} + +On some occasions, the Fift assembler pretends to be able to accept immediate arguments that are out of range for the corresponding TVM instruction. For instance, {\tt ADDCONST $x$} is defined only for $-128\leq x<128$, but the Fift assembler accepts {\tt 239 ADDCONST}: +\begin{verbatim} +17 <{ 239 ADDCONST }>s dup csr. runvmcode .s +\end{verbatim} +produces +\begin{verbatim} +x{8100EFA0} +execute PUSHINT 239 +execute ADD +execute implicit RET +256 0 +\end{verbatim} +We can see that ``{\tt ADDCONST 239}'' has been tacitly replaced by {\tt PUSHINT 239} and {\tt ADD}. This feature is convenient when the immediate argument to {\tt ADDCONST} is itself a result of a Fift computation, and it is difficult to estimate whether it will always fit into the required range. + +In some cases, there are several versions of the same TVM instructions, one accepting an immediate argument and another without any arguments. For instance, there are both {\tt LSHIFT $n$} and {\tt LSHIFT} instructions. In the Fift assembler, such variants are assigned distinct mnemonics. In particular, {\tt LSHIFT $n$} is represented by {\tt $n$ LSHIFT\#}, and {\tt LSHIFT} is represented by itself. + +\mysubsection{Immediate continuations}\label{p:imm.cont} +When an immediate argument is a continuation, it is convenient to create the corresponding {\em Builder\/} in the Fift stack by means of a nested {\tt <\{} \dots {\tt \}>} construct. For instance, TVM assembler instructions +\begin{verbatim} +PUSHINT 1 +SWAP +PUSHCONT { + MULCONST 10 +} +REPEAT +\end{verbatim} +can be assembled and executed by +\begin{verbatim} +7 +<{ 1 INT SWAP <{ 10 MULCONST }> PUSHCONT REPEAT }>s dup csr. +runvmcode drop . +\end{verbatim} +producing +\begin{verbatim} +x{710192A70AE4} +execute PUSHINT 1 +execute SWAP +execute PUSHCONT xA70A +execute REPEAT +repeat 7 more times +execute MULINT 10 +execute implicit RET +repeat 6 more times +... +repeat 1 more times +execute MULINT 10 +execute implicit RET +repeat 0 more times +execute implicit RET +10000000 +\end{verbatim} + +More convenient ways to use literal continuations created by means of the Fift assembler exist. For instance, the above example can be also assembled by +\begin{verbatim} +<{ 1 INT SWAP CONT:<{ 10 MULCONST }> REPEAT }>s csr. +\end{verbatim} +or even +\begin{verbatim} +<{ 1 INT SWAP REPEAT:<{ 10 MULCONST }> }>s csr. +\end{verbatim} +both producing ``{\tt x\{710192A70AE4\} ok}''. + +Incidentally, a better way of implementing the above loop is by means of {\tt REPEATEND}: +\begin{verbatim} +7 <{ 1 INT SWAP REPEATEND 10 MULCONST }>s dup csr. +runvmcode drop . +\end{verbatim} +or +\begin{verbatim} +7 <{ 1 INT SWAP REPEAT: 10 MULCONST }>s dup csr. +runvmcode drop . +\end{verbatim} +both produce ``{\tt x\{7101E7A70A\}}'' and output ``{\tt 10000000}'' after seven iterations of the loop. + +Notice that several TVM instructions that store a continuation in a separate cell reference (such as {\tt JMPREF}) accept their argument in a {\em Cell}, not in a {\em Builder}. In such situations, the {\tt <\{ \dots\ \}>c} construct can be used to produce this immediate argument. + +\mysubsection{Control flow: loops and conditionals} +Almost all TVM control flow instructions---such as {\tt IF}, {\tt IFNOT}, {\tt IFRET}, {\tt IFNOTRET}, {\tt IFELSE}, {\tt WHILE}, {\tt WHILEEND}, {\tt REPEAT}, {\tt REPEATEND}, {\tt UNTIL}, and {\tt UNTILEND}---can be assembled similarly to {\tt REPEAT} and {\tt REPEATEND} in the examples of~\ptref{p:imm.cont} when applied to literal continuations. For instance, TVM assembler code +\begin{verbatim} +DUP +PUSHINT 1 +AND +PUSHCONT { + MULCONST 3 + INC +} +PUSHCONT { + RSHIFT 1 +} +IFELSE +\end{verbatim} +which computes $3n+1$ or $n/2$ depending on whether its argument $n$ is odd or even, can be assembled and applied to $n=7$ by +\begin{verbatim} +<{ DUP 1 INT AND + IF:<{ 3 MULCONST INC }>ELSE<{ 1 RSHIFT# }> +}>s dup csr. +7 swap runvmcode drop . +\end{verbatim} +producing +\begin{verbatim} +x{2071B093A703A492AB00E2} + ok +execute DUP +execute PUSHINT 1 +execute AND +execute PUSHCONT xA703A4 +execute PUSHCONT xAB00 +execute IFELSE +execute MULINT 3 +execute INC +execute implicit RET +execute implicit RET +22 ok +\end{verbatim} +Of course, a more compact and efficient way to implement this conditional expression would be +\begin{verbatim} +<{ DUP 1 INT AND + IF:<{ 3 MULCONST INC }>ELSE: 1 RSHIFT# +}>s dup csr. +\end{verbatim} +or +\begin{verbatim} +<{ DUP 1 INT AND + CONT:<{ 3 MULCONST INC }> IFJMP + 1 RSHIFT# +}>s dup csr. +\end{verbatim} +both producing the same code ``{\tt x\{2071B093A703A4DCAB00\}}''. + +Fift assembler words that can be used to produce such ``high-level'' conditionals and loops include {\tt IF:<\{}, {\tt IFNOT:<\{}, {\tt IFJMP:<\{}, {\tt \}>ELSE<\{}, {\tt \}>ELSE:}, {\tt \}>IF}, {\tt REPEAT:<\{}, {\tt UNTIL:<\{}, {\tt WHILE:<\{}, {\tt \}>DO<\{}, {\tt \}>DO:}, {\tt AGAIN:<\{}, {\tt \}>AGAIN}, {\tt \}>REPEAT}, and {\tt \}>UNTIL}. Their complete list can be found in the source file {\tt Asm.fif}. For instance, an UNTIL loop can be created by {\tt UNTIL:<\{ \dots\ \}>} or {\tt <\{ \dots\ \}>UNTIL}, and a WHILE loop by {\tt WHILE:<\{ \dots\ \}>DO<\{ \dots\ \}>}. + +If we choose to keep a conditional branch in a separate cell, we can use the {\tt <\{ \dots\ \}>c} construct along with instructions such as {\tt IFJMPREF}: +\begin{verbatim} +<{ DUP 1 INT AND + <{ 3 MULCONST INC }>c IFJMPREF + 1 RSHIFT# +}>s dup csr. +3 swap runvmcode .s +\end{verbatim} +has the same effect as the code from the previous example when executed, but it is contained in two separate cells: +\begin{verbatim} +x{2071B0E302AB00} + x{A703A4} +execute DUP +execute PUSHINT 1 +execute AND +execute IFJMPREF (2946....A1DD) +execute MULINT 3 +execute INC +execute implicit RET +10 0 +\end{verbatim} + +\mysubsection{Macro definitions} +Because TVM instructions are implemented in the Fift assembler using Fift words that have a predictable effect on the Fift stack, the Fift assembler is automatically a macro assembler, supporting macro definitions. For instance, suppose that we wish to define a macro definition {\tt RANGE $x$ $y$}, which checks whether the TVM top-of-stack value is between integer literals $x$ and $y$ (inclusive). This macro definition can be implemented as follows: +\begin{verbatim} +{ 2dup > ' swap if + rot DUP rot GEQINT SWAP swap LEQINT AND +} : RANGE +<{ DUP 17 239 RANGE IFNOT: DROP ZERO }>s dup csr. +66 swap runvmcode drop . +\end{verbatim} +which produces +\begin{verbatim} +x{2020C210018100F0B9B0DC3070} +execute DUP +execute DUP +execute GTINT 16 +execute SWAP +execute PUSHINT 240 +execute LESS +execute AND +execute IFRET +66 +\end{verbatim} +Notice that {\tt GEQINT} and {\tt LEQINT} are themselves macro definitions defined in {\tt Asm.fif}, because they do not correspond directly to TVM instructions. For instance, {\tt $x$ GEQINT} corresponds to the TVM instruction {\tt GTINT $x-1$}. + +Incidentally, the above code can be shortened by two bytes by replacing {\tt IFNOT: DROP ZERO} with {\tt AND}. + +\mysubsection{Larger programs and subroutines}\label{p:asm.prog} +Larger TVM programs, such as TON Blockchain smart contracts, typically consist of several mutually recursive subroutines, with one or several of them selected as top-level subroutines (called {\tt main()} or {\tt recv\_internal()} for smart contracts). The execution starts from one of the top-level subroutines, which is free to call any of the other defined subroutines, which in turn can call whatever other subroutines they need. + +Such TVM programs are implemented by means of a selector function, which accepts an extra integer argument in the TVM stack; this integer selects the actual subroutine to be invoked (cf.~\cite[4.6]{TVM}). Before execution, the code of this selector function is loaded both into special register {\tt c3} and into the current continuation {\tt cc}. The selector of the main function (usually zero) is pushed into the initial stack, and the TVM execution is started. Afterwards a subroutine can be invoked by means of a suitable TVM instruction, such as {\tt CALLDICT $n$}, where $n$ is the (integer) selector of the subroutine to be called. + +The Fift assembler offers several words facilitating the implementation of such large TVM programs. In particular, subroutines can be defined separately and assigned symbolic names (instead of numeric selectors), which can be used to call them afterwards. The Fift assembler automatically creates a selector function from these separate subroutines and returns it as the top-level assembly result. + +Here is a simple example of such a program consisting of several subroutines. This program computes the complex number $(5+i)^4\cdot(239-i)$: +\begin{verbatim} +"Asm.fif" include + +PROGRAM{ + +NEWPROC add +NEWPROC sub +NEWPROC mul + +sub <{ s3 s3 XCHG2 SUB s2 XCHG0 SUB }>s PROC + +// compute (5+i)^4 * (239-i) +main PROC:<{ + 5 INT 1 INT // 5+i + 2DUP + mul CALL + 2DUP + mul CALL + 239 INT -1 INT + mul JMP +}> + +add PROC:<{ + s1 s2 XCHG + ADD -ROT ADD SWAP +}> + +// a b c d -- ac-bd ad+bc : complex number multiplication +mul PROC:<{ + s3 s1 PUSH2 // a b c d a c + MUL // a b c d ac + s3 s1 PUSH2 // a b c d ac b d + MUL // a b c d ac bd + SUB // a b c d ac-bd + s4 s4 XCHG2 // ac-bd b c a d + MUL // ac-bd b c ad + -ROT MUL ADD +}> + +}END>s +dup csr. +runvmdict .s +\end{verbatim} +This program produces: +\begin{verbatim} +x{FF00F4A40EF4A0F20B} + x{D9_} + x{2_} + x{1D5C573C00D73C00E0403BDFFC5000E_} + x{04A81668006_} + x{2_} + x{140CE840A86_} + x{14CC6A14CC6A2854112A166A282_} +implicit PUSH 0 at start +execute SETCP 0 +execute DICTPUSHCONST 14 (xC_,1) +execute DICTIGETJMP +execute PUSHINT 5 +execute PUSHINT 1 +execute 2DUP +execute CALLDICT 3 +execute SETCP 0 +execute DICTPUSHCONST 14 (xC_,1) +execute DICTIGETJMP +execute PUSH2 s3,s1 +execute MUL +... +execute ROTREV +execute MUL +execute ADD +execute implicit RET +114244 114244 0 +\end{verbatim} + +Some observations and comments based on the previous example follow: +\begin{itemize} +\item A TVM program is opened by {\tt PROGRAM\{} and closed by either {\tt \}END>c} (which returns the assembled program as a {\em Cell\/}) or {\tt \}END>s} (which returns a {\em Slice\/}). +\item A new subroutine is declared by means of the phrase {\tt NEWPROC $\langle\textit{name}\rangle$}. This declaration assigns the next positive integer as a selector for the newly-declared subroutine, and stores this integer into the constant $\langle\textit{name}\rangle$. For instance, the above declarations define {\tt add}, {\tt sub}, and {\tt mul} as integer constants equal to 1, 2, and 3, respectively. +\item Some subroutines are predeclared and do not need to be declared again by {\tt NEWPROC}. For instance, {\tt main} is a subroutine identifier bound to the integer constant (selector) 0. +\item Other predefined subroutine selectors such as {\tt recv\_internal} (equal to $0$) or {\tt recv\_external} (equal to $-1$), useful for implementing TON Blockchain smart contracts (cf.~\cite[4.4]{TBC}), can be declared by means of {\tt constant} (e.g., {\tt -1 constant recv\_external}). +\item A subroutine can be defined either with the aid of the word {\tt PROC}, which accepts the integer selector of the subroutine and the {\em Slice} containing the code for this subroutine, or with the aid of the construct {\tt $\langle\textit{selector}\rangle$ PROC:<\{ \dots\ \}>}, convenient for defining larger subroutines. +\item {\tt CALLDICT} and {\tt JMPDICT} instructions may be assembled with the aid of the words {\tt CALL} and {\tt JMP}, which accept the integer selector of the subroutine to be called as an immediate argument passed in the Fift stack. +\item The current implementation of the Fift assembler collects all subroutines into a dictionary with 14-bit signed integer keys. Therefore, all subroutine selectors must be in the range $-2^{13}\dots 2^{13}-1$. +\item If a subroutine with an unknown selector is called during runtime, an exception with code $11$ is thrown by the code automatically inserted by the Fift assembler. This code also automatically selects codepage zero for instruction encoding by means of a {\tt SETCP0} instruction. +\item The Fift assembler checks that all subroutines declared by {\tt NEWPROC} are actually defined by {\tt PROC} or {\tt PROC:<\{} before the end of the program. It also checks that a subroutine is not redefined. +\end{itemize} + +One should bear in mind that very simple programs (including the simplest smart contracts) may be made more compact by eliminating this general subroutine selection machinery in favor of custom subroutine selection code and removing unused subroutines. For instance, the above example can be transformed into +\begin{verbatim} +<{ 11 THROWIF + CONT:<{ s3 s1 PUSH2 MUL s3 s1 PUSH2 MUL SUB + s4 s4 XCHG2 MUL -ROT MUL ADD }> + 5 INT 1 INT 2DUP s4 PUSH CALLX + 2DUP s4 PUSH CALLX + ROT 239 INT -1 INT ROT JMPX +}>s +dup csr. +runvmdict .s +\end{verbatim} +which produces +\begin{verbatim} +x{F24B9D5331A85331A8A15044A859A8A075715C24D85C24D8588100EF7F58D9} +implicit PUSH 0 at start +execute THROWIF 11 +execute PUSHCONT x5331A85331A8A15044A859A8A0 +execute PUSHINT 5 +execute PUSHINT 1 +execute 2DUP +execute PUSH s4 +execute EXECUTE +execute PUSH2 s3,s1 +execute MUL +... +execute XCHG2 s4,s4 +execute MUL +execute ROTREV +execute MUL +execute ADD +execute implicit RET +114244 114244 0 +\end{verbatim} + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% +% +% bibliography +% +% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +\clearpage +\markbothsame{\textsc{References}} + +\begin{thebibliography}{2} +\bibitem{Brodie} + {\sc L.~Brodie}, {\sl Starting Forth: Introduction to the FORTH Language and Operating System for Beginners and Professionals}, 2nd edition, Prentice Hall, 1987. Available at \url{https://www.forth.com/starting-forth/}. + +\bibitem{Brodie2} + {\sc L.~Brodie}, {\sl Thinking Forth: A language and philosophy for solving problems}, Prentice Hall, 1984. Available at \url{http://thinking-forth.sourceforge.net/}. + +\bibitem{TON} + {\sc N.~Durov}, {\sl Telegram Open Network}, 2017. + +\bibitem{TVM} + {\sc N.~Durov}, {\sl Telegram Open Network Virtual Machine}, 2018. + +\bibitem{TBC} + {\sc N.~Durov}, {\sl Telegram Open Network Blockchain}, 2018. +\end{thebibliography} + +\clearpage +\appendix\myappendix{List of Fift words}\label{app:words} +This Appendix provides an alphabetic list of almost all Fift words---including primitives and definitions from the standard library {\tt Fift.fif}, but excluding Fift assembler words defined in {\tt Asm.fif} (because the Fift assembler is simply an application from the perspective of Fift). Some experimental words have been omitted from this list. Other words may have been added to or removed from Fift after this text was written. The list of all words available in your Fift interpreter may be inspected by executing {\tt words}. + +Each word is described by its name, followed by its {\em stack notation\/} in parentheses, indicating several values near the top of the Fift stack before and after the execution of the word; all deeper stack entries are usually assumed to be left intact. After that, a text description of the word's effect is provided. If the word has been discussed in a previous section of this document, a reference to this section is included. + +Active words and active prefix words that parse a portion of the input stream immediately after their occurrence are listed here in a modified way. Firstly, these words are listed alongside the portion of the input that they parse; the segment of each entry that is actually a Fift word is underlined for emphasis. Secondly, their stack effect is usually described from the user's perspective, and reflects the actions performed during the execution phase of the encompassing blocks and word definitions. + +For example, the active prefix word {\tt B\{}, used for defining {\em Bytes\/} literals (cf.~\ptref{p:bytes.ops}), is listed as {\tt \underline{B\{}$\langle\textit{hex-digits}\rangle$\}}, and its stack effect is shown as ( -- $B$) instead of ( -- $B$ $1$ $e$), even though the real effect of the execution of the active word {\tt B\{} during the compilation phase of an encompassing block or word definition is the latter one (cf.~\ptref{p:active.words}). + +\begin{itemize} +\item {\tt !} ($x$ $p$ -- ), stores new value~$x$ into {\em Box\/}~$p$, cf.~\ptref{p:variables}. +\item {\tt\underline{"}$\langle\textit{string}\rangle$"} ( -- $S$), pushes a {\em String\/} literal into the stack, cf.~\ptref{p:string.lit} and~\ptref{p:string.ops}. +\item {\tt \#} ($x$ $S$ -- $x'$ $S'$), performs one step of the conversion of {\em Integer\/}~$x$ into its decimal representation by appending to {\em String\/}~$S$ one decimal digit representing $x\bmod10$. The quotient $x':=\lfloor x/10\rfloor$ is returned as well. +\item {\tt \#>} ($S$ -- $S'$), finishes the conversion of an {\em Integer\/} into its human-readable representation (decimal or otherwise) started with {\tt <\#} by reversing {\em String~$S$}. Equivalent to {\tt \$reverse}. +\item {\tt \#s} ($x$ $S$ -- $x'$ $S'$), performs {\tt \#} one or more times until the quotient $x'$ becomes non-positive. Equivalent to {\tt \{ \# over 0<= \} until}. +\item {\tt \$\#} ( -- $x$), pushes the total number of command-line arguments passed to the Fift program, cf.~\ptref{p:cmdline.ops}. Defined only when the Fift interpreter is invoked in script mode (with the {\tt -s} command line argument). +\item {\tt\underline{\$(}$\langle\textit{string\/}\rangle$)} ( -- \dots), looks up the word {\tt \$$\langle\textit{string\/}\rangle$} during execution time and executes its current definition. Typically used to access the current values of command-line arguments, e.g., {\tt \$(2)} is essentially equivalent to {\tt @' \$2}. +\item {\tt \$()} ($x$ -- $S$), pushes the $x$-th command-line argument similarly to {\tt \$$n$}, but with {\em Integer\/}~$x\geq0$ taken from the stack, cf.~\ptref{p:cmdline.ops}. Defined only when the Fift interpreter is invoked in script mode (with the {\tt -s} command line argument). +\item {\tt \$+} ($S$ $S'$ -- $S.S'$), concatenates two strings, cf.~\ptref{p:string.ops}. +\item {\tt \$,} ($b$ $S$ -- $b'$), appends {\em String\/}~$S$ to {\em Builder\/}~$b$, cf.~\ptref{p:builder.ops}. The string is interpreted as a binary string of length $8n$, where $n$ is the number of bytes in the UTF-8 representation of~$S$. +\item {\tt \$$n$} ( -- $S$), pushes the $n$-th command-line argument as a {\em String}~$S$, cf.~\ptref{p:cmdline.ops}. For instance, {\tt \$0} pushes the name of the script being executed, {\tt \$1} the first command line argument, and so on. Defined only when the Fift interpreter is invoked in script mode (with the {\tt -s} command line argument). +\item {\tt \$=} ($S$ $S'$ -- $?$), returns $-1$ if strings $S$ and $S'$ are equal, $0$ otherwise, cf.~\ptref{p:string.cmp.ops}. Equivalent to {\tt \$cmp 0=}. +\item {\tt \$>s} ($S$ -- $s$), transforms the {\em String\/}~$S$ into a {\em Slice}, cf.~\ptref{p:slice.ops}. Equivalent to {\tt smca} ($S$ -- $x$ $y$ $z$ $-1$ or $0$), unpacks a standard TON smart-contract address from its human-readable string representation~$S$, cf.~\ptref{p:smc.addr.ops}. On success, returns the signed 32-bit workchain $x$, the unsigned 256-bit in-workchain address $y$, the flags $z$ (where $+1$ means that the address is non-bounceable, $+2$ that the address is testnet-only), and $-1$. On failure, pushes $0$. +\item {\tt \$@} ($s$ $x$ -- $S$), fetches the first $x$ bytes (i.e., $8x$ bits) from {\em Slice}~$s$, and returns them as a UTF-8 {\em String\/}~$S$, cf.~\ptref{p:slice.ops}. If there are not enough data bits in $s$, throws an exception. +\item {\tt \$@+} ($s$ $x$ -- $S$ $s'$), similar to {\tt \$@}, but returns the remainder of {\em Slice\/} $s$ as well, cf.~\ptref{p:slice.ops}. +\item {\tt \$@?} ($s$ $x$ -- $S$ $-1$ or $0$), similar to {\tt \$@}, but uses a flag to indicate failure instead of throwing an exception, cf.~\ptref{p:slice.ops}. +\item {\tt \$@?+} ($s$ $x$ -- $S$ $s'$ $-1$ or $s$ $0$), similar to {\tt \$@+}, but uses a flag to indicate failure instead of throwing an exception, cf.~\ptref{p:slice.ops}. +\item {\tt \$cmp} ($S$ $S'$ -- $x$), returns $0$ if strings $S$ and $S'$ are equal, $-1$ if $S$ is lexicographically less than $S'$, and $1$ if $S$ is lexicographically greater than $S'$, cf.~\ptref{p:string.cmp.ops}. +\item {\tt \$len} ($S$ -- $x$), computes the byte length (not the UTF-8 character length!) of a string, cf.~\ptref{p:string.ops}. +\item {\tt \$reverse} ($S$ -- $S'$), reverses the order of UTF-8 characters in {\em String\/}~$S$. If $S$ is not a valid UTF-8 string, the return value is undefined and may be also invalid. +\item {\tt \%1<{<}} ($x$ $y$ -- $z$), computes $z:=x\bmod 2^y=x\&(2^y-1)$ for two {\em Integer\/}s $x$ and $0\leq y\leq 256$. +\item {\tt \underline{'} $\langle\textit{word-name}\rangle$} ( -- $e$), returns the execution token equal to the current (compile-time) definition of $\langle\textit{word-name}\rangle$, cf.~\ptref{p:blocks}. If the specified word is not found, throws an exception. +\item {\tt 'nop} ( -- $e$), pushes the default definition of {\tt nop}---an execution token that does nothing when executed, cf.~\ptref{p:dict.lookup}. +\item {\tt \underline{(')} $\langle\textit{word-name}\rangle$} ( -- $e$), similar to {\tt '}, but returns the definition of the specified word at execution time, performing a dictionary lookup each time it is invoked, cf.~\ptref{p:dict.lookup}. May be used to recover the current values of constants inside word definitions and other blocks by using the phrase {\tt (') $\langle\textit{word-name}\rangle$ execute}. +\item {\tt (-trailing)} ($S$ $x$ -- $S'$), removes from {\em String}~$S$ all trailing characters with UTF-8 codepoint~$x$. +\item {\tt (.)} ($x$ -- $S$), returns the {\em String\/} with the decimal representation of {\em Integer\/}~$x$. Equivalent to {\tt dup abs <\# \#s rot sign \#> nip}. +\item {\tt (atom)} ($S$ $x$ -- $a$ $-1$ or $0$), returns the only {\em Atom\/} $a$ with the name given by {\em String\/}~$S$, cf.~\ptref{p:atoms}. If there is no such {\em Atom\/} yet, either creates it (if {\em Integer\/}~$x$ is non-zero) or returns a single zero to indicate failure (if $x$ is zero). +\item {\tt (b.)} ($x$ -- $S$), returns the {\em String\/} with the binary representation of {\em Integer\/}~$x$. +\item {\tt (compile)} ($l$ $x_1$ \dots $x_n$ $n$ $e$ -- $l'$), extends {\em WordList\/}~$l$ so that it would push $0\leq n\leq 255$ values $x_1$, \ldots, $x_n$ into the stack and execute the execution token $e$ when invoked, where $0\leq n\leq 255$ is an {\em Integer}, cf.~\ptref{p:wordlist.ops}. If $e$ is equal to the special value {\tt 'nop}, the last step is omitted. +\item {\tt (create)} ($e$ $S$ $x$ -- ), creates a new word with the name equal to {\em String\/}~$S$ and definition equal to {\em WordDef\/}~$e$, using flags passed in {\em Integer\/} $0\leq x\leq 3$, cf.~\ptref{p:dict.create}. If bit $+1$ is set in $x$, creates an active word; if bit $+2$ is set in $x$, creates a prefix word. +\item {\tt (def?)} ($S$ -- $?$), checks whether the word $S$ is defined. +\item {\tt (dump)} ($x$ -- $S$), returns a {\em String\/} with a dump of the topmost stack value~$x$, in the same format as employed by {\tt .dump}. +\item {\tt (execute)} ($x_1$ \dots $x_n$ $n$ $e$ -- \dots), executes execution token $e$, but first checks that there are at least $0\leq n\leq 255$ values in the stack apart from $n$ and $e$ themselves. It is a counterpart of {\tt (compile)} that may be used to immediately ``execute'' (perform the intended runtime action of) an active word after its immediate execution, as explained in~\ptref{p:active.words}. +\item {\tt (forget)} ($S$ -- ), forgets the word with the name specified in {\em String}~$S$, cf.~\ptref{p:dict.create}. If the word is not found, throws an exception. +\item {\tt (number)} ($S$ -- $0$ or $x$ $1$ or $x$ $y$ $2$), attempts to parse the {\em String\/} $S$ as an integer or fractional literal, cf.~\ptref{p:string.ops} and~\ptref{p:int.lit}. On failure, returns a single $0$. On success, returns $x$ $1$ if $S$ is a valid integer literal with value $x$, or $x$ $y$ $2$ if $S$ is a valid fractional literal with value $x/y$. +\item {\tt (x.)} ($x$ -- $S$), returns the {\em String\/} with the hexadecimal representation of {\em Integer\/}~$x$. +\item {\tt (\{)} ( -- $l$), pushes an empty {\em WordList\/} into the stack, cf.~\ptref{p:wordlist.ops} +\item {\tt (\})} ($l$ -- $e$), transforms a {\em WordList\/} into an execution token ({\em WordDef\/}), making all further modifications impossible, cf.~\ptref{p:wordlist.ops}. +\item {\tt *} ($x$ $y$ -- $xy$), computes the product $xy$ of two {\em Integer\/}s $x$ and $y$, cf.~\ptref{p:arith.op}. +\item {\tt */} ($x$ $y$ $z$ -- $\lfloor xy/z\rfloor$), ``multiply-then-divide'': multiplies two integers $x$ and $y$ producing a 513-bit intermediate result, then divides the product by $z$, cf.~\ptref{p:arith.op}. +\item {\tt */c} ($x$ $y$ $z$ -- $\lceil xy/z\rceil$), ``multiply-then-divide'' with ceiling rounding: multiplies two integers $x$ and $y$ producing a 513-bit intermediate result, then divides the product by $z$, cf.~\ptref{p:arith.op}. +\item {\tt */cmod} ($x$ $y$ $z$ -- $q$ $r$), similar to {\tt */c}, but computes both the quotient $q:=\lceil xy/z\rceil$ and the remainder $r:=xy-qz$, cf.~\ptref{p:arith.op}. +\item {\tt */mod} ($x$ $y$ $z$ -- $q$ $r$), similar to {\tt */}, but computes both the quotient $q:=\lfloor xy/z\rfloor$ and the remainder $r:=xy-qz$, cf.~\ptref{p:arith.op}. +\item {\tt */r} ($x$ $y$ $z$ -- $q:=\lfloor xy/z+1/2\rfloor$), ``multiply-then-divide'' with nearest-integer rounding: multiplies two integers $x$ and $y$ with 513-bit intermediate result, then divides the product by $z$, cf.~\ptref{p:arith.op}. +\item {\tt */rmod} ($x$ $y$ $z$ -- $q$ $r$), similar to {\tt */r}, but computes both the quotient $q:=\lfloor xy/z+1/2\rfloor$ and the remainder $r:=xy-qz$, cf.~\ptref{p:arith.op}. +\item {\tt *>{}>} ($x$ $y$ $z$ -- $q$), similar to {\tt */}, but with division replaced with a right shift, cf.~\ptref{p:arith.op}. Computes $q:=\lfloor xy/2^z\rfloor$ for $0\leq z\leq 256$. Equivalent to {\tt 1<{}< */}. +\item {\tt *>{}>c} ($x$ $y$ $z$ -- $q$), similar to {\tt */c}, but with division replaced with a right shift, cf.~\ptref{p:arith.op}. Computes $q:=\lceil xy/2^z\rceil$ for $0\leq z\leq 256$. Equivalent to {\tt 1<{}< */c}. +\item {\tt *>{}>r} ($x$ $y$ $z$ -- $q$), similar to {\tt */r}, but with division replaced with a right shift, cf.~\ptref{p:arith.op}. Computes $q:=\lfloor xy/2^z+1/2\rfloor$ for $0\leq z\leq 256$. Equivalent to {\tt 1<{}< */r}. +\item {\tt *mod} ($x$ $y$ $z$ -- $r$), similar to {\tt */mod}, but computes only the remainder $r:=xy-qz$, where $q:=\lfloor xy/z\rfloor$. Equivalent to {\tt */mod nip}. +\item {\tt +} ($x$ $y$ -- $x+y$), computes the sum $x+y$ of two {\em Integer\/}s $x$ and $y$, cf.~\ptref{p:arith.op}. +\item {\tt +!} ($x$ $p$ -- ), increases the integer value stored in {\em Box\/}~$p$ by {\em Integer\/}~$x$, cf.~\ptref{p:variables}. Equivalent to {\tt tuck @ + swap !}. +\item {\tt\underline{+"}$\langle\textit{string}\rangle$"} ($S$ -- $S'$), concatenates {\em String\/}~$S$ with a string literal, cf.~\ptref{p:string.ops}. Equivalent to {\tt "$\langle\textit{string}\rangle$" \$+}. +\item {\tt ,} ($t$ $x$ -- $t'$), appends $x$ to the end of {\em Tuple\/}~$t$, and returns the resulting {\em Tuple\/}~$t'$, cf.~\ptref{p:tuples}. +\item {\tt -} ($x$ $y$ -- $x-y$), computes the difference $x-y$ of two {\em Integer\/}s $x$ and $y$, cf.~\ptref{p:arith.op}. +\item {\tt -!} ($x$ $p$ -- ), decreases the integer value stored in {\em Box\/}~$p$ by {\em Integer\/}~$x$. Equivalent to {\tt swap negate swap +!}. +\item {\tt -1} ( -- $-1$), pushes {\em Integer\/} $-1$. +\item {\tt -1<{}<} ($x$ -- $-2^x$), computes $-2^x$ for $0\leq x\leq 256$. Approximately equivalent to {\tt 1<{}< negate} or {\tt -1 swap <{}<}, but works for $x=256$ as well. +\item {\tt -roll} ($x_n$ \dots $x_0$ $n$ -- $x_0$ $x_n$ \dots $x_1$), rotates the top $n$ stack entries in the opposite direction, where $n\geq0$ is also passed in the stack, cf.~\ptref{p:stack.ops}. In particular, {\tt 1 -roll} is equivalent to {\tt swap}, and {\tt 2 -roll} to {\tt -rot}. +\item {\tt -rot} ($x$ $y$ $z$ -- $z$ $x$ $y$), rotates the three topmost stack entries in the opposite direction, cf.~\ptref{p:stack.ops}. Equivalent to {\tt rot rot}. +\item {\tt -trailing} ($S$ -- $S'$), removes from {\em String\/}~$S$ all trailing spaces. Equivalent to {\tt bl (-trailing)}. +\item {\tt -trailing0} ($S$ -- $S'$), removes from {\em String\/}~$S$ all trailing `{\tt 0}' characters. Equivalent to {\tt char 0 (-trailing)}. +\item {\tt .} ($x$ -- ), prints the decimal representation of {\em Integer\/}~$x$, followed by a single space, cf.~\ptref{p:arith.op}. Equivalent to {\tt .\_ space}. +\item {\tt\underline{."}$\langle\textit{string}\rangle$"} ( -- ), prints a constant string into the standard output, cf.~\ptref{p:string.ops}. +\item {\tt .\_} ($x$ -- ), prints the decimal representation of {\em Integer\/}~$x$ without any spaces. Equivalent to {\tt (.)~type}. +\item {\tt .dump} ($x$ -- ), dumps the topmost stack entry in the same way as {\tt .s} dumps all stack elements, cf.~\ptref{p:tuples}. Equivalent to {\tt (dump) type space}. +\item {\tt .l} ($l$ -- ), prints a Lisp-style list~$l$, cf.~\ptref{p:lists}. +\item {\tt .s} ( -- ), dumps all stack entries starting from the deepest, leaving them intact, cf.~\ptref{p:stack.ops}. Human-readable representations of stack entries are output separated by spaces, followed by an end-of-line character. +\item {\tt .sl} ( -- ), dumps all stack entries leaving them intact similarly to {\tt .s}, but showing each entry as a List-style list~$l$ as {\tt .l} does. +\item {\tt .tc} ( -- ), outputs the total number of allocated cells into the standard error stream. +\item {\tt /} ($x$ $y$ -- $q:=\lfloor x/y\rfloor$), computes the floor-rounded quotient $\lfloor x/y\rfloor$ of two {\em Integer\/}s, cf.~\ptref{p:arith.op}. +\item {\tt \underline{/*} $\langle\textit{multiline-comment}\rangle$ */} ( -- ), skips a multi-line comment delimited by word ``{\tt */}'' (followed by a blank or an end-of-line character), cf.~\ptref{p:comments}. +\item {\tt \underline{//} $\langle\textit{comment-to-eol}\rangle$} ( -- ), skips a single-line comment until the end of the current line, cf.~\ptref{p:comments}. +\item {\tt /c} ($x$ $y$ -- $q:=\lceil x/y\rceil$), computes the ceiling-rounded quotient $\lceil x/y\rceil$ of two {\em Integer\/}s, cf.~\ptref{p:arith.op}. +\item {\tt /cmod} ($x$ $y$ -- $q$ $r$), computes both the ceiling-rounded quotient $q:=\lceil x/y\rceil$ and the remainder $r:=x-qy$, cf.~\ptref{p:arith.op}. +\item {\tt /mod} ($x$ $y$ -- $q$ $r$), computes both the floor-rounded quotient $q:=\lfloor x/y\rfloor$ and the remainder $r:=x-qy$, cf.~\ptref{p:arith.op}. +\item {\tt /r} ($x$ $y$ -- $q$), computes the nearest-integer-rounded quotient $\lfloor x/y+1/2\rfloor$ of two {\em Integer\/}s, cf.~\ptref{p:arith.op}. +\item {\tt /rmod} ($x$ $y$ -- $q$ $r$), computes both the nearest-integer-rounded quotient $q:=\lfloor x/y+1/2\rfloor$ and the remainder $r:=x-qy$, cf.~\ptref{p:arith.op}. +\item {\tt 0} ( -- $0$), pushes {\em Integer\/} $0$. +\item {\tt 0!} ($p$ -- ), stores {\em Integer\/} $0$ into {\em Box\/}~$p$, cf.~\ptref{p:variables}. Equivalent to {\tt 0 swap !}. +\item {\tt 0<} ($x$ -- $?$), checks whether $x<0$ (i.e., pushes $-1$ if $x$ is negative, $0$ otherwise), cf.~\ptref{p:int.comp}. Equivalent to {\tt 0 <}. +\item {\tt 0<=} ($x$ -- $?$), checks whether $x\leq0$ (i.e., pushes $-1$ if $x$ is non-positive, $0$ otherwise), cf.~\ptref{p:int.comp}. Equivalent to {\tt 0 <=}. +\item {\tt 0<>} ($x$ -- $?$), checks whether $x\neq0$ (i.e., pushes $-1$ if $x$ is non-zero, $0$ otherwise), cf.~\ptref{p:int.comp}. Equivalent to {\tt 0 <>}. +\item {\tt 0=} ($x$ -- $?$), checks whether $x=0$ (i.e., pushes $-1$ if $x$ is zero, $0$ otherwise), cf.~\ptref{p:int.comp}. Equivalent to {\tt 0 =}. +\item {\tt 0>} ($x$ -- $?$), checks whether $x>0$ (i.e., pushes $-1$ if $x$ is positive, $0$ otherwise), cf.~\ptref{p:int.comp}. Equivalent to {\tt 0 >}. +\item {\tt 0>=} ($x$ -- $?$), checks whether $x\geq0$ (i.e., pushes $-1$ if $x$ is non-negative, $0$ otherwise), cf.~\ptref{p:int.comp}. Equivalent to {\tt 0 >=}. +\item {\tt 1} ( -- $1$), pushes {\em Integer\/} $1$. +\item {\tt 1+} ($x$ -- $x+1$), computes $x+1$. Equivalent to {\tt 1 +}. +\item {\tt 1+!} ($p$ -- ), increases the integer value stored in {\em Box\/}~$p$ by one, cf.~\ptref{p:variables}. Equivalent to {\tt 1 swap +!}. +\item {\tt 1-} ($x$ -- $x-1$), computes $x-1$. Equivalent to {\tt 1 -}. +\item {\tt 1-!} ($p$ -- ), decreases the integer value stored in {\em Box\/}~$p$ by one. Equivalent to {\tt -1 swap +!}. +\item {\tt 1<{}<} ($x$ -- $2^x$), computes $2^x$ for $0\leq x\leq 255$. Equivalent to {\tt 1 swap <{}<}. +\item {\tt 1<{}<1-} ($x$ -- $2^x-1$), computes $2^x-1$ for $0\leq x\leq 256$. Almost equivalent to {\tt 1<{}< 1-}, but works for $x=256$. +\item {\tt 2} ( -- $2$), pushes {\em Integer\/} $2$. +\item {\tt 2*} ($x$ -- $2x$), computes $2x$. Equivalent to {\tt 2 *}. +\item {\tt 2+} ($x$ -- $x+2$), computes $x+2$. Equivalent to {\tt 2 +}. +\item {\tt 2-} ($x$ -- $x-2$), computes $x-2$. Equivalent to {\tt 2 -}. +\item {\tt 2/} ($x$ -- $\lfloor x/2\rfloor$), computes $\lfloor x/2\rfloor$. Equivalent to {\tt 2 /} or to {\tt 1 >{}>}. +\item {\tt \underline{2=:} $\langle\textit{word-name}\rangle$} ($x$ $y$ -- ), an active variant of {\tt 2constant}: defines a new ordinary word $\langle\textit{word-name}\rangle$ that would push the given values $x$ and $y$ when invoked, cf.~\ptref{p:constants}. +\item {\tt 2constant} ($x$ $y$ -- ), scans a blank-delimited word name $S$ from the remainder of the input, and defines a new ordinary word $S$ as a double constant, which will push the given values $x$ and $y$ (of arbitrary types) when invoked, cf.~\ptref{p:dict.create}. +\item {\tt 2drop} ($x$ $y$ -- ), removes the two topmost stack entries, cf.~\ptref{p:stack.ops}. Equivalent to {\tt drop drop}. +\item {\tt 2dup} ($x$ $y$ -- $x$ $y$ $x$ $y$), duplicates the topmost pair of stack entries, cf.~\ptref{p:stack.ops}. Equivalent to {\tt over over}. +\item {\tt 2over} ($x$ $y$ $z$ $w$ -- $x$ $y$ $z$ $w$ $x$ $y$), duplicates the second topmost pair of stack entries. +\item {\tt 2swap} ($a$ $b$ $c$ $d$ -- $c$ $d$ $a$ $b$), interchanges the two topmost pairs of stack entries, cf.~\ptref{p:stack.ops}. +\item {\tt \underline{:} $\langle\textit{word-name}\rangle$} ($e$ -- ), defines a new ordinary word $\langle\textit{word-name}\rangle$ in the dictionary using {\em WordDef\/} $e$ as its definition, cf.~\ptref{p:dict.create}. If the specified word is already present in the dictionary, it is tacitly redefined. +\item {\tt \underline{::} $\langle\textit{word-name}\rangle$} ($e$ -- ), defines a new active word $\langle\textit{word-name}\rangle$ in the dictionary using {\em WordDef\/} $e$ as its definition, cf.~\ptref{p:dict.create}. If the specified word is already present in the dictionary, it is tacitly redefined. +\item {\tt \underline{::\_} $\langle\textit{word-name}\rangle$} ($e$ -- ), defines a new active prefix word $\langle\textit{word-name}\rangle$ in the dictionary using {\em WordDef\/} $e$ as its definition, cf.~\ptref{p:dict.create}. If the specified word is already present in the dictionary, it is tacitly redefined. +\item {\tt \underline{:\_} $\langle\textit{word-name}\rangle$} ($e$ -- ), defines a new ordinary prefix word $\langle\textit{word-name}\rangle$ in the dictionary using {\em WordDef\/} $e$ as its definition, cf.~\ptref{p:dict.create}. If the specified word is already present in the dictionary, it is tacitly redefined. +\item {\tt <} ($x$ $y$ -- $?$), checks whether $x} ($x$ $y$ -- $?$), checks whether $x\neq y$ (i.e., pushes $-1$ if {\em Integer\/}s~$x$ and~$y$ are not equal, $0$ otherwise), cf.~\ptref{p:int.comp}. +\item {\tt } ($x$ $y$ -- $?$), checks whether $x>y$ (i.e., pushes $-1$ if {\em Integer\/}~$x$ is greater than {\em Integer\/}~$y$, $0$ otherwise), cf.~\ptref{p:int.comp}. +\item {\tt >=} ($x$ $y$ -- $?$), checks whether $x\geq y$ (i.e., pushes $-1$ if {\em Integer\/}~$x$ is greater than or equal to {\em Integer\/}~$y$, $0$ otherwise), cf.~\ptref{p:int.comp}. +\item {\tt >{}>} ($x$ $y$ -- $q:=\lfloor x\cdot 2^{-y}\rfloor$), computes an arithmetic right shift of binary number $x$ by $0\leq y\leq 256$ positions, cf.~\ptref{p:arith.op}. Equivalent to {\tt 1<{}< /}. +\item {\tt >{}>c} ($x$ $y$ -- $q:=\lceil x\cdot 2^{-y}\rceil$), computes the ceiling-rounded quotient $q$ of $x$ by $2^y$ for $0\leq y\leq 256$, cf.~\ptref{p:arith.op}. Equivalent to {\tt 1<{}< /c}. +\item {\tt >{}>r} ($x$ $y$ -- $q:=\lfloor x\cdot 2^{-y}+1/2\rfloor$), computes the nearest-integer-rounded quotient $q$ of $x$ by $2^y$ for $0\leq y\leq 256$, cf.~\ptref{p:arith.op}. Equivalent to {\tt 1<{}< /r}. +\item {\tt ?dup} ($x$ -- $x$ $x$ or $0$), duplicates an {\em Integer\/} $x$, but only if it is non-zero, cf.~\ptref{p:stack.ops}. Otherwise leaves it intact. +\item {\tt @} ($p$ -- $x$), fetches the value currently stored in {\em Box\/}~$p$, cf.~\ptref{p:variables}. +\item {\tt \underline{@'} $\langle\textit{word-name}\rangle$} ( -- $e$), recovers the definition of the specified word at execution time, performing a dictionary lookup each time it is invoked, and then executes this definition, cf.~\ptref{p:constants} and~\ptref{p:dict.lookup}. May be used to recover current values of constants inside word definitions and other blocks by using the phrase {\tt @' $\langle\textit{word-name}\rangle$}, equivalent to {\tt (') $\langle\textit{word-name}\rangle$ execute}. +\item {\tt B+} ($B'$ $B''$ -- $B$), concatenates two {\em Bytes\/} values, cf.~\ptref{p:bytes.ops}. +\item {\tt B,} ($b$ $B$ -- $b'$), appends {\em Bytes\/}~$B$ to {\em Builder\/}~$b$, cf.~\ptref{p:builder.ops}. If there is no room in $b$ for $B$, throws an exception. +\item {\tt B=} ($B$ $B'$ -- $?$), checks whether two {\em Bytes\/} sequences are equal, and returns $-1$ or $0$ depending on the comparison outcome, cf.~\ptref{p:bytes.ops}. +\item {\tt B>Li@} ($B$ $x$ -- $y$), deserializes the first $x/8$ bytes of a {\tt Bytes} value~$B$ as a signed little-endian $x$-bit {\em Integer}~$y$, cf.~\ptref{p:bytes.ops}. +\item {\tt B>Li@+} ($B$ $x$ -- $B'$ $y$), deserializes the first $x/8$ bytes of $B$ as a signed little-endian $x$-bit {\em Integer}~$y$ similarly to {\tt B>Li@}, but also returns the remaining bytes of~$B$, cf.~\ptref{p:bytes.ops}. +\item {\tt B>Lu@} ($B$ $x$ -- $y$), deserializes the first $x/8$ bytes of a {\tt Bytes} value~$B$ as an unsigned little-endian $x$-bit {\em Integer}~$y$, cf.~\ptref{p:bytes.ops}. +\item {\tt B>Lu@+} ($B$ $x$ -- $B'$ $y$), deserializes the first $x/8$ bytes of $B$ as an unsigned little-endian $x$-bit {\em Integer}~$y$ similarly to {\tt B>Lu@}, but also returns the remaining bytes of~$B$, cf.~\ptref{p:bytes.ops}. +\item {\tt B>boc} ($B$ -- $c$), deserializes a ``standard'' bag of cells (i.e., a bag of cells with exactly one root cell) represented by {\em Bytes\/}~$B$, and returns the root {\em Cell\/}~$c$, cf.~\ptref{p:boc.ops}. +\item {\tt B>file} ($B$ $S$ -- ), creates a new (binary) file with the name specified in {\em String\/}~$S$ and writes data from {\em Bytes}~$B$ into the new file, cf.~\ptref{p:bytes.ops}. If the specified file already exists, it is overwritten. +\item {\tt B>i@} ($B$ $x$ -- $y$), deserializes the first $x/8$ bytes of a {\tt Bytes} value~$B$ as a signed big-endian $x$-bit {\em Integer}~$y$, cf.~\ptref{p:bytes.ops}. +\item {\tt B>i@+} ($B$ $x$ -- $B'$ $y$), deserializes the first $x/8$ bytes of $B$ as a signed big-endian $x$-bit {\em Integer}~$y$ similarly to {\tt B>i@}, but also returns the remaining bytes of~$B$, cf.~\ptref{p:bytes.ops}. +\item {\tt B>u@} ($B$ $x$ -- $y$), deserializes the first $x/8$ bytes of a {\tt Bytes} value~$B$ as an unsigned big-endian $x$-bit {\em Integer}~$y$, cf.~\ptref{p:bytes.ops}. +\item {\tt B>u@+} ($B$ $x$ -- $B'$ $y$), deserializes the first $x/8$ bytes of $B$ as an unsigned big-endian $x$-bit {\em Integer}~$y$ similarly to {\tt B>u@}, but also returns the remaining bytes of~$B$, cf.~\ptref{p:bytes.ops}. +\item {\tt B@} ($s$ $x$ -- $B$), fetches the first $x$ bytes (i.e., $8x$ bits) from {\em Slice}~$s$, and returns them as a {\em Bytes\/} value~$B$, cf.~\ptref{p:slice.ops}. If there are not enough data bits in $s$, throws an exception. +\item {\tt B@+} ($s$ $x$ -- $B$ $s'$), similar to {\tt B@}, but returns the remainder of {\em Slice\/} $s$ as well, cf.~\ptref{p:slice.ops}. +\item {\tt B@?} ($s$ $x$ -- $B$ $-1$ or $0$), similar to {\tt B@}, but uses a flag to indicate failure instead of throwing an exception, cf.~\ptref{p:slice.ops}. +\item {\tt B@?+} ($s$ $x$ -- $B$ $s'$ $-1$ or $s$ $0$), similar to {\tt B@+}, but uses a flag to indicate failure instead of throwing an exception, cf.~\ptref{p:slice.ops}. +\item {\tt Bcmp} ($B$ $B'$ -- $x$), lexicographically compares two {\em Bytes\/} sequences, and returns $-1$, $0$, or $1$, depending on the comparison result, cf.~\ptref{p:bytes.ops}. +\item {\tt Bhash} ($B$ -- $B'$), computes the $\Sha$ hash of a {\em Bytes\/} value, cf.~\ptref{p:bytes.ops}. The hash is returned as a 32-byte {\em Bytes\/} value. +\item {\tt Blen} ($B$ -- $x$), returns the length of a {\em Bytes\/} value~$B$ in bytes, cf.~\ptref{p:bytes.ops}. +\item {\tt Bx.} ($B$ -- ), prints the hexadecimal representation of a {\em Bytes\/} value, cf.~\ptref{p:bytes.ops}. Each byte is represented by exactly two uppercase hexadecimal digits. +\item {\tt \underline{B\{}$\langle{\textit{hex-digits}}\rangle$\}} ( -- $B$), pushes a {\em Bytes\/} literal containing data represented by an even number of hexadecimal digits, cf.~\ptref{p:bytes.ops}. +\item {\tt B|} ($B$ $x$ -- $B'$ $B''$), cuts the first $x$ bytes from a {\em Bytes\/} value~$B$, and returns both the first $x$ bytes ($B'$) and the remainder ($B''$) as new {\em Bytes\/} values, cf.~\ptref{p:bytes.ops}. +\item {\tt Li>B} ($x$ $y$ -- $B$), stores a signed little-endian $y$-bit {\em Integer\/}~$x$ into a {\em Bytes\/} value $B$ consisting of exactly $y/8$ bytes. Integer $y$ must be a multiple of eight in the range $0\ldots256$, cf.~\ptref{p:bytes.ops}. +\item {\tt Lu>B} ($x$ $y$ -- $B$), stores an unsigned little-endian $y$-bit {\em Integer\/}~$x$ into a {\em Bytes\/} value $B$ consisting of exactly $y/8$ bytes. Integer $y$ must be a multiple of eight in the range $0\ldots256$, cf.~\ptref{p:bytes.ops}. +\item {\tt \underline{[}} ( -- ), opens an internal interpreter session even if {\tt state} is greater than zero, i.e., all subsequent words are executed immediately instead of being compiled. +\item {\tt []} ($t$ $i$ -- $x$), returns the $(i+1)$-st component $t_{i+1}$ of {\em Tuple\/}~$t$, where $0\leq i<|t|$, cf.~\ptref{p:tuples}. +\item {\tt \underline{[compile]} $\langle\textit{word-name}\rangle$} ( -- ), compiles $\langle\textit{word-name}\rangle$ as if it were an ordinary word, even if it is active, cf.~\ptref{p:dict.lookup}. Essentially equivalent to {\tt ' $\langle\textit{word-name}\rangle$ execute}. +\item {\tt \underline{]}} ($x_1$ \dots $x_n$ $n$ -- ), closes an internal interpreter session opened by {\tt [} and invokes {\tt (compile)} or {\tt (execute)} afterwards depending on whether {\tt state} is greater than zero. For instance, {\tt \{ [ 2 3 + 1 ] * \}} is equivalent to {\tt \{ 5 * \}}. +\item {\tt \underline{`}$\langle\textit{word\/}\rangle$} ( -- $a$), introduces an {\em Atom\/} literal, equal to the only {\em Atom\/} with the name equal to $\langle\textit{word\/}\rangle$, cf.~\ptref{p:atoms}. Equivalent to {\tt "$\langle\textit{word\/}\rangle$" atom}. +\item {\tt abort} ($S$ -- ), throws an exception with an error message taken from {\em String\/}~$S$, cf.~\ptref{p:exception.ops}. +\item {\tt \underline{abort"}$\langle\textit{message}\rangle$"} ($x$ -- ), throws an exception with the error message $\langle\textit{message}\rangle$ if the {\em Integer\/}~$x$ is non-zero, cf.~\ptref{p:exception.ops}. +\item {\tt abs} ($x$ -- $|x|$), computes the absolute value $|x|=\max(x,-x)$ of {\em Integer\/}~$x$. Equivalent to {\tt dup negate max}. +\item {\tt allot} ($n$ -- $t$), creates a new array, i.e., a {\em Tuple\/} that consists of $n$ new empty {\em Box\/}es, cf.~\ptref{p:tuples}. Equivalent to {\tt | \{ hole , \} rot times}. +\item {\tt and} ($x$ $y$ -- $x\& y$), computes the bitwise AND of two {\em Integer\/}s, cf.~\ptref{p:arith.op}. +\item {\tt anon} ( -- $a$), creates a new unique anonymous {\em Atom}, cf.~\ptref{p:atoms}. +\item {\tt atom} ($S$ -- $a$), returns the only {\em Atom\/}~$a$ with the name~$S$, creating such an atom if necessary, cf.~\ptref{p:atoms}. Equivalent to {\tt true (atom) drop}. +\item {\tt atom?} ($u$ -- $?$), checks whether $u$ is an {\em Atom}, cf.~\ptref{p:atoms}. +\item {\tt b+} ($b$ $b'$ -- $b''$), concatenates two {\em Builders\/} $b$ and~$b'$, cf.~\ptref{p:builder.ops}. +\item {\tt b.} ($x$ -- ), prints the binary representation of an {\em Integer\/}~$x$, followed by a single space. Equivalent to {\tt b.\_ space}. +\item {\tt b.\_} ($x$ -- ), prints the binary representation of an {\em Integer\/}~$x$ without any spaces. Equivalent to {\tt (b.)~type}. +\item {\tt b>} ($b$ -- $c$), transforms a {\em Builder\/}~$b$ into a new {\em Cell\/}~$c$ containing the same data as~$b$, cf.~\ptref{p:builder.ops}. +\item {\tt b>idict!} ($v$ $x$ $s$ $n$ -- $s'$ $-1$ or $s$ $0$), adds a new value $v$ (represented by a {\em Builder}) with key given by signed big-endian $n$-bit integer $x$ into dictionary $s$ with $n$-bit keys, and returns the new dictionary $s'$ and $-1$ on success, cf.~\ptref{p:hashmap.ops}. Otherwise the unchanged dictionary $s$ and $0$ are returned. +\item {\tt b>idict!+} ($v$ $x$ $s$ $n$ -- $s'$ $-1$ or $s$ $0$), adds a new key-value pair $(x,v)$ into dictionary $s$ similarly to {\tt b>idict!}, but fails if the key already exists by returning the unchanged dictionary $s$ and $0$, cf.~\ptref{p:hashmap.ops}. +\item {\tt b>udict!} ($v$ $x$ $s$ $n$ -- $s'$ $-1$ or $s$ $0$), adds a new value $v$ (represented by a {\em Builder}) with key given by unsigned big-endian $n$-bit integer $x$ into dictionary $s$ with $n$-bit keys, and returns the new dictionary $s'$ and $-1$ on success, cf.~\ptref{p:hashmap.ops}. Otherwise the unchanged dictionary $s$ and $0$ are returned. +\item {\tt b>udict!+} ($v$ $x$ $s$ $n$ -- $s'$ $-1$ or $s$ $0$), adds a new key-value pair $(x,v)$ into dictionary $s$ similarly to {\tt b>udict!}, but fails if the key already exists by returning the unchanged dictionary $s$ and $0$, cf.~\ptref{p:hashmap.ops}. +\item {\tt bbitrefs} ($b$ -- $x$ $y$), returns both the number of data bits $x$ and the number of references $y$ already stored in {\em Builder\/}~$b$, cf.~\ptref{p:builder.ops}. +\item {\tt bbits} ($b$ -- $x$), returns the number of data bits already stored in {\em Builder\/}~$b$. The result $x$ is an {\em Integer\/} in the range $0\dots1023$, cf.~\ptref{p:builder.ops}. +\item {\tt bl} ( -- $x$), pushes the Unicode codepoint of a space, i.e., 32, cf.~\ptref{p:string.ops}. +\item {\tt boc+>B} ($c$ $x$ -- $B$), creates and serializes a ``standard'' bag of cells, containing one root {\em Cell\/}~$c$ along with all its descendants, cf.~\ptref{p:boc.ops}. An {\em Integer\/} parameter $0\leq x\leq 31$ is used to pass flags indicating the additional options for bag-of-cells serialization, with individual bits having the following effect: + \begin{itemize} + \item $+1$ enables bag-of-cells index creation (useful for lazy deserialization of large bags of cells). + \item $+2$ includes the CRC32-C of all data into the serialization (useful for checking data integrity). + \item $+4$ explicitly stores the hash of the root cell into the serialization (so that it can be quickly recovered afterwards without a complete deserialization). + \item $+8$ stores hashes of some intermediate (non-leaf) cells (useful for lazy deserialization of large bags of cells). + \item $+16$ stores cell cache bits to control caching of deserialized cells. + \end{itemize} +Typical values of $x$ are $x=0$ or $x=2$ for very small bags of cells (e.g., TON Blockchain external messages) and $x=31$ for large bags of cells (e.g., TON Blockchain blocks). +\item {\tt boc>B} ($c$ -- $B$), serializes a small ``standard'' bag of cells with root {\em Cell\/}~$c$ and all its descendants, cf.~\ptref{p:boc.ops}. Equivalent to {\tt 0 boc+>B}. +\item {\tt box} ($x$ -- $p$), creates a new {\em Box\/} containing specified value~$x$, cf.~\ptref{p:variables}. Equivalent to {\tt hole tuck !}. +\item {\tt brefs} ($b$ -- $x$), returns the number of references already stored in {\em Builder\/}~$b$, cf.~\ptref{p:builder.ops}. The result $x$ is an {\em Integer\/} in the range $0\dots4$. +\item {\tt brembitrefs} ($b$ -- $x$ $y$), returns both the maximum number of additional data bits $0\leq x\leq 1023$ and the maximum number of additional cell references $0\leq y\leq 4$ that can be stored in {\em Builder\/}~$b$, cf.~\ptref{p:builder.ops}. +\item {\tt brembits} ($b$ -- $x$), returns the maximum number of additional data bits that can be stored in {\em Builder\/}~$b$, cf.~\ptref{p:builder.ops}. Equivalent to {\tt bbits 1023 swap -}. +\item {\tt bremrefs} ($b$ -- $x$), returns the maximum number of additional cell references that can be stored in {\em Builder\/}~$b$, cf.~\ptref{p:builder.ops}. +\item {\tt bye} ( -- ), quits the Fift interpreter to the operating system with a zero exit code, cf.~\ptref{p:exit.fift}. Equivalent to {\tt 0 halt}. +\item {\tt \underline{b\{}$\langle\textit{binary-data}\rangle$\}} ( -- $s$), creates a {\em Slice}~$s$ that contains no references and up to 1023 data bits specified in $\langle\textit{binary-data}\rangle$, which must be a string consisting only of the characters `{\tt 0}' and `{\tt 1}', cf.~\ptref{p:slice.lit}. +\item {\tt caddr} ($l$ -- $h''$), returns the third element of a list. Equivalent to {\tt cddr car}. +\item {\tt cadr} ($l$ -- $h'$), returns the second element of a list, cf.~\ptref{p:lists}. Equivalent to {\tt cdr car}. +\item {\tt car} ($l$ -- $h$), returns the head of a list, cf.~\ptref{p:lists}. Equivalent to {\tt first}. +\item {\tt cddr} ($l$ -- $t'$), returns the tail of the tail of a list. Equivalent to {\tt cdr cdr}. +\item {\tt cdr} ($l$ -- $t$), returns the tail of a list, cf.~\ptref{p:lists}. Equivalent to {\tt second}. +\item {\tt \underline{char} $\langle\textit{string}\rangle$} ( -- $x$), pushes an {\em Integer\/} with the Unicode codepoint of the first character of {\tt $\langle\textit{string}\rangle$}, cf.~\ptref{p:string.ops}. For instance, {\tt char *} is equivalent to {\tt 42}. +\item {\tt chr} ($x$ -- $S$), returns a new {\em String\/}~$S$ consisting of one UTF-8 encoded character with Unicode codepoint~$x$. +\item {\tt cmp} ($x$ $y$ -- $z$), compares two {\em Integer\/}s $x$ and~$y$, and pushes $1$ if $x>y$, $-1$ if $xB swap ed25519\_sign}. The integer $x$ to be signed is typically computed as the hash of some data. +\item {\tt emit} ($x$ -- ), prints a UTF-8 encoded character with Unicode codepoint given by {\em Integer\/} $x$ into the standard output, cf.~\ptref{p:string.ops}. For instance, {\tt 42 emit} prints an asterisk ``{\tt *}'', and {\tt 916 emit} prints a Greek Delta ``{\tt $\Delta$}''. Equivalent to {\tt chr type}. +\item {\tt empty?} ($s$ -- $?$), checks whether a {\em Slice\/} is empty (i.e., has no data bits and no references left), and returns $-1$ or $0$ accordingly, cf.~\ptref{p:slice.ops}. +\item {\tt eq?} ($u$ $v$ -- $?$), checks whether $u$ and $v$ are equal {\em Integer\/}s, {\em Atom\/}s, or {\em Null\/}s, cf.~\ptref{p:atoms}. If they are not equal, or if they are of different types, or not of one of the types listed, returns zero. +\item {\tt exch} ($x_n$ \dots $x_0$ $n$ -- $x_0$ \dots $x_n$), interchanges the top of the stack with the $n$-th stack entry from the top, where $n\geq0$ is also taken from the stack, cf.~\ptref{p:stack.ops}. In particular, {\tt 1 exch} is equivalent to {\tt swap}, and {\tt 2 exch} to {\tt swap rot}. +\item {\tt exch2} (\dots $n$ $m$ -- \dots), interchanges the $n$-th stack entry from the top with the $m$-th stack entry from the top, where $n\geq0$, $m\geq0$ are taken from the stack, cf.~\ptref{p:stack.ops}. +\item {\tt execute} ($e$ -- \dots), executes the execution token ({\em WordDef\/}) $e$, cf.~\ptref{p:blocks}. +\item {\tt explode} ($t$ -- $x_1$ \dots $x_n$ $n$), unpacks a {\em Tuple\/}~$t=(x_1,\ldots,x_n)$ of unknown length~$n$, and returns that length, cf.~\ptref{p:tuples}. +\item {\tt false} ( -- $0$), pushes $0$ into the stack, cf.~\ptref{p:bool}. Equivalent to {\tt 0}. +\item {\tt file-exists?} ($S$ -- $?$), checks whether the file with the name specified in {\em String\/}~$S$ exists, cf.~\ptref{p:bytes.ops}. +\item {\tt file>B} ($S$ -- $B$), reads the (binary) file with the name specified in {\em String\/}~$S$ and returns its contents as a {\em Bytes\/} value, cf.~\ptref{p:bytes.ops}. If the file does not exist, an exception is thrown. +\item {\tt find} ($S$ -- $e$ $-1$ or $e$ $1$ or $0$), looks up {\em String\/} $S$ in the dictionary and returns its definition as a {\em WordDef\/}~$e$ if found, followed by $-1$ for ordinary words or $1$ for active words, cf.~\ptref{p:dict.lookup}. Otherwise pushes $0$. +\item {\tt first} ($t$ -- $x$), returns the first component of a {\em Tuple}, cf.~\ptref{p:tuples}. Equivalent to {\tt 0 []}. +\item {\tt fits} ($x$ $y$ -- $?$), checks whether {\em Integer\/}~$x$ is a signed $y$-bit integer (i.e., whether $-2^{y-1}\leq x<2^{y-1}$ for $0\leq y\leq 1023$), and returns $-1$ or $0$ accordingly. +\item {\tt forget} ( -- ), forgets (removes from the dictionary) the definition of the next word scanned from the input, cf.~\ptref{p:dict.create}. +\item {\tt gasrunvm} (\dots $s$ $c$ $z$ -- \dots $x$ $c'$ $z'$), a gas-aware version of {\tt runvm}, cf.~\ptref{p:tvm.ops}: invokes a new instance of TVM with both the current continuation {\tt cc} and the special register {\tt c3} initialized from {\em Slice\/}~$s$, and pushes a zero into the initial TVM stack similarly to {\tt runvmdict}, but also initializes special register {\tt c4} (the ``root of persistent data'', cf.~\cite[1.4]{TVM}) with {\em Cell\/}~$c$. Then starts the new TVM instance with the gas limit set to $z$. The actually consumed gas $z'$ is returned at the top of the final Fift stack, and the final value of {\tt c4} is returned immediately below the top of the final Fift stack as another {\em Cell\/}~$c'$. +\item {\tt gasrunvmcode} (\dots $s$ $z$ -- \dots $x$ $z'$), a gas-aware version of {\tt runvmcode}, cf.~\ptref{p:tvm.ops}: invokes a new instance of TVM with the current continuation {\tt cc} initialized from {\em Slice\/} $s$ and with the gas limit set to $z$, thus executing code~$s$ in TVM. The original Fift stack (without $s$) is passed in its entirety as the initial stack of the new TVM instance. When TVM terminates, its resulting stack is used as the new Fift stack, with the exit code $x$ and the actually consumed gas $z'$ pushed at its top. If $x$ is non-zero, indicating that TVM has been terminated by an unhandled exception, the next stack entry from the top contains the parameter of this exception, and $x$ is the exception code. All other entries are removed from the stack in this case. +\item {\tt gasrunvmdict} (\dots $s$ $z$ -- \dots $x$ $z'$), a gas-aware version of {\tt runvmdict}, cf.~\ptref{p:tvm.ops}: invokes a new instance of TVM with the current continuation {\tt cc} initialized from {\em Slice\/} $s$ and sets the gas limit to $z$ similarly to {\tt gasrunvmcode}, but also initializes the special register {\tt c3} with the same value, and pushes a zero into the initial TVM stack before the TVM execution begins. The actually consumed gas is returned as an {\em Integer\/} $z'$. In a typical application {\em Slice\/}~$s$ consists of a subroutine selection code that uses the top-of-stack {\em Integer\/} to select the subroutine to be executed, thus enabling the definition and execution of several mutually-recursive subroutines (cf.~\cite[4.6]{TVM} and~\ptref{p:asm.prog}). The selector equal to zero corresponds to the {\tt main()} subroutine in a large TVM program. +\item {\tt halt} ($x$ -- ), quits to the operating system similarly to {\tt bye}, but uses {\em Integer\/} $x$ as the exit code, cf.~\ptref{p:exit.fift}. +\item {\tt hash} ($c$ -- $B$), computes the $\Sha$-based representation hash of {\em Cell\/}~$c$ (cf.~\cite[3.1]{TVM}), which unambiguously defines $c$ and all its descendants (provided there are no collisions for $\Sha$), cf.~\ptref{p:hash.ops}. The result is returned as a {\em Bytes\/} value consisting of exactly 32 bytes. +\item {\tt hold} ($S$ $x$ -- $S'$), appends to {\em String\/}~$S$ one UTF-8 encoded character with Unicode codepoint~$x$. Equivalent to {\tt chr \$+}. +\item {\tt hole} ( -- $p$), creates a new {\em Box\/}~$p$ that does not hold any value, cf.~\ptref{p:variables}. Equivalent to {\tt null box}. +\item {\tt i,} ($b$ $x$ $y$ -- $b'$), appends the big-endian binary representation of a signed $y$-bit integer~$x$ to {\em Builder\/}~$b$, where $0\leq y\leq 257$, cf.~\ptref{p:builder.ops}. If there is not enough room in $b$ (i.e., if $b$ already contains more than $1023-y$ data bits), or if {\em Integer\/}~$x$ does not fit into $y$ bits, an exception is thrown. +\item {\tt i>B} ($x$ $y$ -- $B$), stores a signed big-endian $y$-bit {\em Integer\/}~$x$ into a {\em Bytes\/} value $B$ consisting of exactly $y/8$ bytes. Integer $y$ must be a multiple of eight in the range $0\ldots256$, cf.~\ptref{p:bytes.ops}. +\item {\tt i@} ($s$ $x$ -- $y$), fetches a signed big-endian $x$-bit integer from the first $x$ bits of {\em Slice}~$s$, cf.~\ptref{p:slice.ops}. If $s$ contains less than $x$ data bits, an exception is thrown. +\item {\tt i@+} ($s$ $x$ -- $y$ $s'$), fetches a signed big-endian $x$-bit integer from the first $x$ bits of {\em Slice}~$s$ similarly to {\tt i@}, but returns the remainder of $s$ as well, cf.~\ptref{p:slice.ops}. +\item {\tt i@?} ($s$ $x$ -- $y$ $-1$ or $0$), fetches a signed big-endian integer from a {\em Slice\/} similarly to {\tt i@}, but pushes integer $-1$ afterwards on success, cf.~\ptref{p:slice.ops}. If there are less than $x$ bits left in $s$, pushes integer $0$ to indicate failure. +\item {\tt i@?+} ($s$ $x$ -- $y$ $s'$ $-1$ or $s$ $0$), fetches a signed big-endian integer from {\em Slice\/}~$s$ and computes the remainder of this {\em Slice\/} similarly to {\tt i@+}, but pushes $-1$ afterwards to indicate success, cf.~\ptref{p:slice.ops}. On failure, pushes the unchanged {\em Slice\/}~$s$ and $0$ to indicate failure. +\item {\tt idict!} ($v$ $x$ $s$ $n$ -- $s'$ $-1$ or $s$ $0$), adds a new value $v$ (represented by a {\em Slice\/}) with key given by signed big-endian $n$-bit integer $x$ into dictionary $s$ with $n$-bit keys, and returns the new dictionary $s'$ and $-1$ on success, cf.~\ptref{p:hashmap.ops}. Otherwise the unchanged dictionary $s$ and $0$ are returned. +\item {\tt idict!+} ($v$ $x$ $s$ $n$ -- $s'$ $-1$ or $s$ $0$), adds a new key-value pair $(x,v)$ into dictionary $s$ similarly to {\tt idict!}, but fails if the key already exists by returning the unchanged dictionary $s$ and $0$, cf.~\ptref{p:hashmap.ops}. +\item {\tt idict@} ($x$ $s$ $n$ -- $v$ $-1$ or $0$), looks up key represented by signed big-endian $n$-bit {\em Integer\/}~$x$ in the dictionary represented by {\em Slice\/}~$s$, cf.~\ptref{p:hashmap.ops}. If the key is found, returns the corresponding value as a {\em Slice\/}~$v$ and $-1$. Otherwise returns $0$. +\item {\tt if} ($x$ $e$ -- ), executes execution token (i.e., a {\em WordDef\/}) $e$, but only if {\em Integer\/} $x$ is non-zero, cf.~\ptref{p:cond.ops}. +\item {\tt ifnot} ($x$ $e$ -- ), executes execution token $e$, but only if {\em Integer\/} $x$ is zero, cf.~\ptref{p:cond.ops}. +\item {\tt include} ($S$ -- ), loads and interprets a Fift source file from the path given by {\em String\/}~$S$, cf.~\ptref{p:asm.load}. If the filename $S$ does not begin with a slash, the Fift include search path, typically taken from the {\tt FIFTPATH} environment variable or the {\tt -I} command-line argument of the Fift interpreter (and equal to {\tt /usr/lib/fift} if both are absent), is used to locate~$S$. +\item {\tt list} ($x_1$ \dots $x_n$ $n$ -- $l$), constructs a list $l$ of length~$n$ with elements $x_1$, \ldots, $x_n$, in that order, cf.~\ptref{p:lists}. Equivalent to {\tt null ' cons rot times}. +\item {\tt max} ($x$ $y$ -- $z$), computes the maximum $z:=\max(x,y)$ of two {\em Integer\/}s $x$ and~$y$. Equivalent to {\tt minmax nip}. +\item {\tt min} ($x$ $y$ -- $z$), computes the minimum $z:=\min(x,y)$ of two {\em Integer\/}s $x$ and~$y$. Equivalent to {\tt minmax drop}. +\item {\tt minmax} ($x$ $y$ -- $z$ $t$), computes both the minimum $z:=\min(x,y)$ and the maximum $t:=\max(x,y)$ of two {\em Integer\/}s $x$ and~$y$. +\item {\tt mod} ($x$ $y$ -- $r:=x\bmod y$), computes the remainder $x\bmod y=x-y\cdot\lfloor x/y\rfloor$ of division of $x$ by $y$, cf.~\ptref{p:arith.op}. +\item {\tt negate} ($x$ -- $-x$), changes the sign of an {\em Integer}, cf.~\ptref{p:arith.op}. +\item {\tt newkeypair} ( -- $B$ $B'$), generates a new Ed25519 private/public key pair, and returns both the private key $B$ and the public key $B'$ as 32-byte {\em Bytes\/} values, cf.~\ptref{p:ed25519.ops}. The quality of the keys is good enough for testing purposes. Real applications must feed enough entropy into OpenSSL PRNG before generating Ed25519 keypairs. +\item {\tt nil} ( -- $t$), pushes the empty {\em Tuple\/}~$t=()$. Equivalent to {\tt 0 tuple}. +\item {\tt nip} ($x$ $y$ -- $y$), removes the second stack entry from the top, cf.~\ptref{p:stack.ops}. Equivalent to {\tt swap drop}. +\item {\tt nop} ( -- ), does nothing, cf.~\ptref{p:dict.lookup}. +\item {\tt not} ($x$ -- $-1-x$), computes the bitwise complement of an {\em Integer}, cf.~\ptref{p:arith.op}. +\item {\tt now} ( -- $x$), returns the current Unixtime as an {\em Integer}, cf.~\ptref{p:ed25519.ops}. +\item {\tt null} ( -- $\bot$), pushes the {\em Null\/} value, cf.~\ptref{p:lists} +\item {\tt null!} ($p$ -- ), stores a {\em Null\/} value into {\em Box\/}~$p$. Equivalent to {\tt null swap !}. +\item {\tt null?} ($x$ -- $?$), checks whether $x$ is {\em Null}, cf.~\ptref{p:lists}. +\item {\tt or} ($x$ $y$ -- $x|y$), computes the bitwise OR of two {\em Integer\/}s, cf.~\ptref{p:arith.op}. +\item {\tt over} ($x$ $y$ -- $x$ $y$ $x$), creates a copy of the second stack entry from the top over the top-of-stack entry, cf.~\ptref{p:stack.ops}. +\item {\tt pair} ($x$ $y$ -- $t$), creates new pair $t=(x,y)$, cf.~\ptref{p:tuples}. Equivalent to {\tt 2 tuple} or to {\tt | rot , swap ,}. +\item {\tt pfxdict!} ($v$ $k$ $s$ $n$ -- $s'$ $-1$ or $s$ $0$), adds key-value pair $(k,v)$, both represented by {\em Slice\/}s, into a prefix dictionary $s$ with keys of length at most~$n$, cf.~\ptref{p:hashmap.ops}. On success, returns the modified dictionary $s'$ and $-1$. On failure, returns the original dictionary $s$ and $0$. +\item {\tt pfxdict!+} ($v$ $k$ $s$ $n$ -- $s'$ $-1$ or $s$ $0$), adds key-value pair $(k,v)$ into prefix dictionary $s$ similarly to {\tt pfxdict!}, but fails if the key already exists, cf.~\ptref{p:hashmap.ops}. +\item {\tt pfxdict@} ($k$ $s$ $n$ -- $v$ $-1$ or $0$), looks up key $k$ (represented by a {\em Slice\/}) in the prefix dictionary $s$ with the length of keys limited by $n$ bits, cf.~\ptref{p:hashmap.ops}. On success, returns the value found as a {\em Slice\/} $v$ and $-1$. On failure, returns $0$. +\item {\tt pick} ($x_n$ \dots $x_0$ $n$ -- $x_n$ \dots $x_0$ $x_n$), creates a copy of the $n$-th entry from the top of the stack, where $n\geq0$ is also passed in the stack, cf.~\ptref{p:stack.ops}. In particular, {\tt 0 pick} is equivalent to {\tt dup}, and {\tt 1 pick} to {\tt over}. +\item {\tt priv>pub} ($B$ -- $B'$), computes the public key corresponding to a private Ed25519 key, cf.~\ptref{p:ed25519.ops}. Both the public key~$B'$ and the private key~$B$ are represented by 32-byte {\em Bytes\/} values. +\item {\tt quit} ($\dots$ -- ), exits to the topmost level of the Fift interpreter (without printing an {\tt ok} in interactive mode) and clears the stack, cf.~\ptref{p:exit.fift}. +\item {\tt ref,} ($b$ $c$ -- $b'$), appends to {\em Builder\/}~$b$ a reference to {\em Cell\/}~$c$, cf.~\ptref{p:builder.ops}. If $b$ already contains four references, an exception is thrown. +\item {\tt ref@} ($s$ -- $c$), fetches the first reference from the {\em Slice\/}~$s$ and returns the {\em Cell}~$c$ referred to, cf.~\ptref{p:slice.ops}. If there are no references left, throws an exception. +\item {\tt ref@+} ($s$ -- $s'$ $c$), fetches the first reference from the {\em Slice\/} $s$ similarly to {\tt ref@}, but returns the remainder of $s$ as well, cf.~\ptref{p:slice.ops}. +\item {\tt ref@?} ($s$ -- $c$ $-1$ or $0$), fetches the first reference from the {\em Slice}~$s$ similarly to {\tt ref@}, but uses a flag to indicate failure instead of throwing an exception, cf.~\ptref{p:slice.ops}. +\item {\tt ref@?+} ($s$ -- $s'$ $c$ $-1$ or $s$ $0$), similar to {\tt ref@+}, but uses a flag to indicate failure instead of throwing an exception, cf.~\ptref{p:slice.ops}. +\item {\tt remaining} ($s$ -- $x$ $y$), returns both the number of data bits $x$ and the number of cell references $y$ remaining in the {\em Slice}~$s$, cf.~\ptref{p:slice.ops}. +\item {\tt reverse} ($x_1$ \dots $x_n$ $y_1$ \dots $y_m$ $n$ $m$ -- $x_n$ \dots $x_1$ $y_1$ \dots $y_m$), reverses the order of $n$ stack entries located immediately below the topmost $m$ elements, where both $0\leq m,n\leq 255$ are passed in the stack. +\item {\tt roll} ($x_n$ \dots $x_0$ $n$ -- $x_{n-1}$ \dots $x_0$ $x_n$), rotates the top $n$ stack entries, where $n\geq0$ is also passed in the stack, cf.~\ptref{p:stack.ops}. In particular, {\tt 1 roll} is equivalent to {\tt swap}, and {\tt 2 roll} to {\tt rot}. +\item {\tt rot} ($x$ $y$ $z$ -- $y$ $z$ $x$), rotates the three topmost stack entries. +\item {\tt runvmcode} (\dots $s$ -- \dots $x$), invokes a new instance of TVM with the current continuation {\tt cc} initialized from {\em Slice\/} $s$, thus executing code~$s$ in TVM, cf.~\ptref{p:tvm.ops}. The original Fift stack (without $s$) is passed in its entirety as the initial stack of the new TVM instance. When TVM terminates, its resulting stack is used as the new Fift stack, with the exit code $x$ pushed at its top. If $x$ is non-zero, indicating that TVM has been terminated by an unhandled exception, the next stack entry from the top contains the parameter of this exception, and $x$ is the exception code. All other entries are removed from the stack in this case. +\item {\tt runvmdict} (\dots $s$ -- \dots $x$), invokes a new instance of TVM with the current continuation {\tt cc} initialized from {\em Slice\/} $s$ similarly to {\tt runvmcode}, but also initializes the special register {\tt c3} with the same value, and pushes a zero into the initial TVM stack before start, cf.~\ptref{p:tvm.ops}. In a typical application {\em Slice\/}~$s$ consists of a subroutine selection code that uses the top-of-stack {\em Integer\/} to select the subroutine to be executed, thus enabling the definition and execution of several mutually-recursive subroutines (cf.~\cite[4.6]{TVM} and~\ptref{p:asm.prog}). The selector equal to zero corresponds to the {\tt main()} subroutine in a large TVM program. +\item {\tt runvm} (\dots $s$ $c$ -- \dots $x$ $c'$), invokes a new instance of TVM with both the current continuation {\tt cc} and the special register {\tt c3} initialized from {\em Slice\/}~$s$, and pushes a zero into the initial TVM stack similarly to {\tt runvmdict}, but also initializes special register {\tt c4} (the ``root of persistent data'', cf.~\cite[1.4]{TVM}) with {\em Cell\/}~$c$, cf.~\ptref{p:tvm.ops}. The final value of {\tt c4} is returned at the top of the final Fift stack as another {\em Cell\/}~$c'$. In this way one can emulate the execution of smart contracts that inspect or modify their persistent storage. +\item {\tt s,} ($b$ $s$ -- $b'$), appends data bits and references taken from {\em Slice}~$s$ to {\em Builder}~$b$, cf.~\ptref{p:builder.ops}. +\item {\tt s>} ($s$ -- ), throws an exception if {\em Slice\/}~$s$ is non-empty, cf.~\ptref{p:slice.ops}. It usually marks the end of the deserialization of a cell, checking whether there are any unprocessed data bits or references left. +\item {\tt s>c} ($s$ -- $c$), creates a {\em Cell}~$c$ directly from a {\em Slice}~$s$, cf.~\ptref{p:slice.ops}. Equivalent to {\tt }. +\item {\tt sbitrefs} ($s$ -- $x$ $y$), returns both the number of data bits $x$ and the number of cell references $y$ remaining in {\em Slice}~$s$, cf.~\ptref{p:slice.ops}. Equivalent to {\tt remaining}. +\item {\tt sbits} ($s$ -- $x$), returns the number of data bits $x$ remaining in {\em Slice}~$s$, cf.~\ptref{p:slice.ops}. +\item {\tt second} ($t$ -- $x$), returns the second component of a {\em Tuple}, cf.~\ptref{p:tuples}. Equivalent to {\tt 1 []}. +\item {\tt sgn} ($x$ -- $y$), computes the sign of an {\em Integer\/} $x$ (i.e., pushes $1$ if $x>0$, $-1$ if $x<0$, and $0$ if $x=0$), cf.~\ptref{p:int.comp}. Equivalent to {\tt 0 cmp}. +\item {\tt shash} ($s$ -- $B$), computes the $\Sha$-based representation hash of a {\em Slice\/} by first transforming it into a cell, cf.~\ptref{p:hash.ops}. Equivalent to {\tt s>c hash}. +\item {\tt sign} ($S$ $x$ -- $S'$), appends a minus sign ``{\tt -}'' to {\em String\/}~$S$ if {\em Integer\/}~$x$ is negative. Otherwise leaves $S$ intact. +\item {\tt single} ($x$ -- $t$), creates new singleton $t=(x)$, i.e., a one-element {\em Tuple}. Equivalent to {\tt 1 tuple}. +\item {\tt skipspc} ( -- ), skips blank characters from the current input line until a non-blank or an end-of-line character is found. +\item {\tt smca>\$} ($x$ $y$ $z$ -- $S$), packs a standard TON smart-contract address with workchain $x$ (a signed 32-bit {\em Integer\/}) and in-workchain address $y$ (an unsigned 256-bit {\em Integer\/}) into a 48-character string $S$ (the human-readable representation of the address) according to flags~$z$, cf.~\ptref{p:smc.addr.ops}. Possible individual flags in~$z$ are: $+1$ for non-bounceable addresses, $+2$ for testnet-only addresses, and $+4$ for base64url output instead of base64. +\item {\tt space} ( -- ), outputs a single space. Equivalent to {\tt bl emit} or to {\tt ." "}. +\item {\tt sr,} ($b$ $s$ -- $b'$), constructs a new {\em Cell\/} containing all data and references from {\em Slice\/}~$s$, and appends a reference to this cell to {\em Builder}~$b$, cf.~\ptref{p:builder.ops}. Equivalent to {\tt s>c ref,}. +\item {\tt srefs} ($s$ -- $x$), returns the number of cell references $x$ remaining in {\em Slice}~$s$, cf.~\ptref{p:slice.ops}. +\item {\tt swap} ($x$ $y$ -- $y$ $x$), interchanges the two topmost stack entries, cf.~\ptref{p:stack.ops}. +\item {\tt ten} ( -- $10$), pushes {\em Integer\/} constant 10. +\item {\tt third} ($t$ -- $x$), returns the third component of a {\em Tuple}, cf.~\ptref{p:tuples}. Equivalent to {\tt 2 []}. +\item {\tt times} ($e$ $n$ -- ), executes execution token ({\em WordDef\/}) $e$ exactly $n$ times, if $n\geq0$, cf.~\ptref{p:simple.loops}. If $n$ is negative, throws an exception. +\item {\tt triple} ($x$ $y$ $z$ -- $t$), creates new triple $t=(x,y,z)$, cf.~\ptref{p:tuples}. Equivalent to {\tt 3 tuple}. +\item {\tt true} ( -- $-1$), pushes $-1$ into the stack, cf.~\ptref{p:bool}. Equivalent to {\tt -1}. +\item {\tt tuck} ($x$ $y$ -- $y$ $x$ $y$), equivalent to {\tt swap over}, cf.~\ptref{p:stack.ops}. +\item {\tt tuple} ($x_1$ \dots $x_n$ $n$ -- $t$), creates new {\em Tuple\/} $t:=(x_1,\ldots,x_n)$ from $n\geq0$ topmost stack values, cf.~\ptref{p:tuples}. Equivalent to {\tt dup 1 reverse | \{ swap , \} rot times}, but more efficient. +\item {\tt tuple?} ($t$ -- $?$), checks whether $t$ is a {\em Tuple}, and returns $-1$ or $0$ accordingly. +\item {\tt type} ($s$ -- ), prints a {\em String\/} $s$ taken from the top of the stack into the standard output, cf.~\ptref{p:string.ops}. +\item {\tt u,} ($b$ $x$ $y$ -- $b'$), appends the big-endian binary representation of an unsigned $y$-bit integer~$x$ to {\em Builder\/}~$b$, where $0\leq y\leq 256$, cf.~\ptref{p:builder.ops}. If the operation is impossible, an exception is thrown. +\item {\tt u>B} ($x$ $y$ -- $B$), stores an unsigned big-endian $y$-bit {\em Integer\/}~$x$ into a {\em Bytes\/} value $B$ consisting of exactly $y/8$ bytes. Integer $y$ must be a multiple of eight in the range $0\ldots256$, cf.~\ptref{p:bytes.ops}. +\item {\tt u@} ($s$ $x$ -- $y$), fetches an unsigned big-endian $x$-bit integer from the first $x$ bits of {\em Slice}~$s$, cf.~\ptref{p:slice.ops}. If $s$ contains less than $x$ data bits, an exception is thrown. +\item {\tt u@+} ($s$ $x$ -- $y$ $s'$), fetches an unsigned big-endian $x$-bit integer from the first $x$ bits of {\em Slice}~$s$ similarly to {\tt u@}, but returns the remainder of $s$ as well, cf.~\ptref{p:slice.ops}. +\item {\tt u@?} ($s$ $x$ -- $y$ $-1$ or $0$), fetches an unsigned big-endian integer from a {\em Slice\/} similarly to {\tt u@}, but pushes integer $-1$ afterwards on success, cf.~\ptref{p:slice.ops}. If there are less than $x$ bits left in $s$, pushes integer $0$ to indicate failure. +\item {\tt u@?+} ($s$ $x$ -- $y$ $s'$ $-1$ or $s$ $0$), fetches an unsigned big-endian integer from {\em Slice\/}~$s$ and computes the remainder of this {\em Slice\/} similarly to {\tt u@+}, but pushes $-1$ afterwards to indicate success, cf.~\ptref{p:slice.ops}. On failure, pushes the unchanged {\em Slice\/}~$s$ and $0$ to indicate failure. +\item {\tt udict!} ($v$ $x$ $s$ $n$ -- $s'$ $-1$ or $s$ $0$), adds a new value $v$ (represented by a {\em Slice\/}) with key given by big-endian unsigned $n$-bit integer $x$ into dictionary $s$ with $n$-bit keys, and returns the new dictionary $s'$ and $-1$ on success, cf.~\ptref{p:hashmap.ops}. Otherwise the unchanged dictionary $s$ and $0$ are returned. +\item {\tt udict!+} ($v$ $x$ $s$ $n$ -- $s'$ $-1$ or $s$ $0$), adds a new key-value pair $(x,v)$ into dictionary $s$ similarly to {\tt udict!}, but fails if the key already exists by returning the unchanged dictionary $s$ and $0$, cf.~\ptref{p:hashmap.ops}. +\item {\tt udict@} ($x$ $s$ $n$ -- $v$ $-1$ or $0$), looks up key represented by unsigned big-endian $n$-bit {\em Integer\/}~$x$ in the dictionary represented by {\em Slice\/}~$s$, cf.~\ptref{p:hashmap.ops}. If the key is found, returns the corresponding value as a {\em Slice\/}~$v$ and $-1$. Otherwise returns $0$. +\item {\tt ufits} ($x$ $y$ -- $?$), checks whether {\em Integer\/}~$x$ is an unsigned $y$-bit integer (i.e., whether $0\leq x<2^y$ for $0\leq y\leq 1023$), and returns $-1$ or $0$ accordingly. +\item {\tt uncons} ($l$ -- $h$ $t$), decomposes a non-empty list into its head and its tail, cf.~\ptref{p:lists}. Equivalent to {\tt unpair}. +\item {\tt \underline{undef?} $\langle\textit{word-name\/}\rangle$} ( -- $?$), checks whether the word $\langle\textit{word-name\/}\rangle$ is undefined at execution time, and returns $-1$ or $0$ accordingly. +\item {\tt unpair} ($t$ -- $x$ $y$), unpacks a pair $t=(x,y)$, cf.~\ptref{p:tuples}. Equivalent to {\tt 2 untuple}. +\item {\tt unsingle} ($t$ -- $x$), unpacks a singleton $t=(x)$. Equivalent to {\tt 1 untuple}. +\item {\tt until} ($e$ -- ), an until loop, cf.~\ptref{p:loops}: executes {\em WordDef\/}~$e$, then removes the top-of-stack integer and checks whether it is zero. If it is, then begins a new iteration of the loop by executing $e$. Otherwise exits the loop. +\item {\tt untriple} ($t$ -- $x$ $y$ $z$), unpacks a triple $t=(x,y,z)$, cf.~\ptref{p:tuples}. Equivalent to {\tt 3 untuple}. +\item {\tt untuple} ($t$ $n$ -- $x_1$ \dots $x_n$), returns all components of a {\em Tuple\/}~$t=(x_1,\ldots,x_n)$, but only if its length is equal to~$n$, cf.~\ptref{p:tuples}. Otherwise throws an exception. +\item {\tt variable} ( -- ), scans a blank-delimited word name $S$ from the remainder of the input, allocates an empty {\em Box}, and defines a new ordinary word $S$ as a constant, which will push the new {\em Box\/} when invoked, cf.~\ptref{p:variables}. Equivalent to {\tt hole constant}. +\item {\tt while} ($e$ $e'$ -- ), a while loop, cf.~\ptref{p:loops}: executes {\em WordDef\/}~$e$, then removes and checks the top-of-stack integer. If it is zero, exits the loop. Otherwise executes {\em WordDef\/}~$e'$, then begins a new loop iteration by executing $e$ and checking the exit condition afterwards. +\item {\tt word} ($x$ -- $s$), parses a word delimited by the character with the Unicode codepoint $x$ from the remainder of the current input line and pushes the result as a {\em String}, cf.~\ptref{p:string.ops}. For instance, {\tt bl word abracadabra type} will print the string ``{\tt abracadabra}''. If $x=0$, skips leading spaces, and then scans until the end of the current input line. If $x=32$, skips leading spaces before parsing the next word. +\item {\tt words} ( -- ), prints the names of all words currently defined in the dictionary, cf.~\ptref{p:dict.lookup}. +\item {\tt x.} ($x$ -- ), prints the hexadecimal representation (without the {\tt 0x} prefix) of an {\em Integer\/}~$x$, followed by a single space. Equivalent to {\tt x.\_ space}. +\item {\tt x.\_} ($x$ -- ), prints the hexadecimal representation (without the {\tt 0x} prefix) of an {\em Integer\/}~$x$ without any spaces. Equivalent to {\tt (x.)~type}. +\item {\tt xor} ($x$ $y$ -- $x\oplus y$), computes the bitwise eXclusive OR of two {\em Integer\/}s, cf.~\ptref{p:arith.op}. +\item {\tt \underline{x\{}$\langle\textit{hex-data}\rangle$\}} ( -- $s$), creates a {\em Slice}~$s$ that contains no references and up to 1023 data bits specified in $\langle\textit{hex-data}\rangle$, cf.~\ptref{p:slice.lit}. More precisely, each hex digit from $\langle\textit{hex-data}\rangle$ is transformed into four binary digits in the usual fashion. After that, if the last character of $\langle\textit{hex-data}\rangle$ is an underscore {\tt \_}, then all trailing binary zeroes and the binary one immediately preceding them are removed from the resulting binary string (cf.~\cite[1.0]{TVM} for more details). For instance, {\tt x\{6C\_\}} is equivalent to {\tt b\{01101\}}. +\item {\tt \underline{\{}} ( -- $l$), an active word that increases internal variable {\tt state} by one and pushes a new empty {\em WordList\/} into the stack, cf.~\ptref{p:wordlist.ops}. +\item {\tt |} ( -- $t$), creates an empty {\em Tuple\/} $t=()$, cf.~\ptref{p:tuples}. Equivalent to {\tt nil} and to {\tt 0 tuple}. +\item {\tt |+} ($s$ $s'$ -- $s''$), concatenates two {\em Slice}\/s $s$ and $s'$, cf.~\ptref{p:slice.lit}. This means that the data bits of the new {\em Slice\/} $s''$ are obtained by concatenating the data bits of $s$ and $s'$, and the list of {\em Cell\/} references of $s''$ is constructed similarly by concatenating the corresponding lists for $s$ and $s'$. Equivalent to {\tt c ref, b> \LT(e')\quad\text{whenever $e\succ e'$ (i.e., $e$ logically depends on $e'$),} +\end{equation} +without insisting that $\LT(e)$ be the smallest non-negative integer with this property. In such cases we can speak about {\em relaxed\/} logical time, as opposed to the {\em strict\/} logical time defined above (cf.~\ptref{sp:logic.time}). Notice, however, that the condition~\eqref{eq:lt.fund.ineq} is a fundamental property of logical time and cannot be relaxed further. + +\nxsubpoint\label{sp:logic.time.interval}\emb{Logical time intervals} +It makes sense to assign to some events or collections of events $C$ an {\em interval\/} of logical times $\LT^\bullet(C)=[\LT^-(C),\LT^+(C))$, meaning that the collection of events $C$ took place in the specified ``interval'' of logical times, where $\LT^-(C)<\LT^+(C)$ are some integers (64-bit integers in practice). In this case, we can say that $C$ {\em begins\/} at logical time $\LT^-(C)$, and {\em ends\/} at logical time $\LT^+(C)$. + + By default, we assume $\LT^+(e)=\LT(e)+1$ and $\LT^-(e)=\LT(e)$ for simple or ``atomic'' events, assuming that they last exactly one unit of logical time. In general, if we have a single value $\LT(C)$ as well as logical time interval $\LT^\bullet(C)=[\LT^-(C),\LT^+(C))$, we always require that +\begin{equation} + \LT(C)\in[\LT^-(C),\LT^+(C)) +\end{equation} +or, equivalently, +\begin{equation} + \LT^-(C)\leq\LT(C)<\LT^+(C) +\end{equation} +In most cases, we choose $\LT(C)=\LT^-(C)$. + +\nxsubpoint\label{sp:lt.int.cond}\emb{Requirements for logical time intervals} +The three principal requirements for logical time intervals are: +\begin{itemize} +\item $0\leq\LT^-(C)<\LT^+(C)$ are non-negative integers for any collection of events~$C$. +\item If $e'\prec e$ (i.e., if an atomic event $e$ logically depends on another atomic event $e'$), then $\LT^\bullet(e')<\LT^\bullet(e)$ (i.e., $\LT^+(e')\leq\LT^-(e)$). +\item If $C\supset D$ (i.e., if a collection of events $C$ contains another collection of events $D$), then $\LT^\bullet(C)\supset\LT^\bullet(D)$, i.e., +\begin{equation} + \LT^-(C)\leq\LT^-(D)<\LT^+(D)\leq\LT^+(C) +\end{equation} +In particular, if $C$ consists of atomic events $e_1$, \dots, $e_n$, then $\LT^-(C)\leq\inf_i\LT^-(e_i)\leq\inf_i\LT(e_i)$ and $\LT^+(C)\geq\sup_i\LT^+(e_i)\geq 1+\sup_i\LT(e_i)$. +\end{itemize} + +\nxsubpoint\emb{Strict, or minimal, logical time intervals} +One can assign to any finite collection of atomic events $E=\{e\}$ related by a causality relation (partial order) $\prec$, and all subsets $C\subset E$, {\em minimal\/} logical time intervals. That is, among all assignments of logical time intervals satisfying the conditions listed in \ptref{sp:lt.int.cond}, we choose the one having all $\LT^+(C)-\LT^-(C)$ as small as possible, and if several assignments with this property exist, we choose the one that has the minimum $\LT^-(C)$ as well. + +Such an assignment can be achieved by first assigning logical time $\LT(e)$ to all atomic events $e\in E$ as described in \ptref{sp:logic.time}, then setting $\LT^-(C):=\inf_{e\in C}\LT(e)$ and $\LT^+(C):=1+\sup_{e\in C}\LT(e)$ for any $C\subset E$. + +In most cases when we need to assign logical time intervals, we use the minimal logical time intervals just described. + +\nxsubpoint\label{sp:lt.ton.blkch}\emb{Logical time in the TON Blockchain} +The TON Blockchain assigns logical time and logical time intervals to several of its components. + +For instance, each outbound message created in a transaction is assigned its {\em logical creation time}; for this purpose, the creation of an outbound message is considered an atomic event, logically dependent on the previous message created by the same transaction, as well as on the previous transaction of the same account, on the inbound message processed by the same transaction, and on all events contained in the blocks referred to by hashes contained in the block with the same transaction. As a consequence, {\em outbound messages created by the same smart contract have strictly increasing logical creation times.} The transaction itself is considered a collection of atomic events, and is assigned a logical time interval (cf.~\ptref{sp:trans.lt} for a more precise description). + +Each block is a collection of transaction and message creation events, so it is assigned a logical time interval, explicitly mentioned in the header of the block. + +\mysubsection{Total blockchain state} +This section discusses the total state of the TON Blockchain, as well as the states of separate shardchains and the masterchain. For example, the precise definition of the state of the neighboring shardchains becomes crucial for correctly formalizing the consistency condition asserting that the validators for a shardchain must import the oldest messages from the union of {\em OutMsgQueue\/}s taken from the states of all neighboring shardchains (cf.~\ptref{sp:monot.import}). + +\nxsubpoint\emb{Total state defined by a masterchain block} +Every masterchain block contains a list of all currently active shards and of the latest blocks for each of them. In this respect, {\em every masterchain block defines the corresponding total state of the TON Blockchain, since it fixes the state of every shardchain, and of the masterchain as well.} + +An important requirement imposed on this list of the latest blocks for all shardchain blocks is that, if a masterchain block $B$ lists $S$ as the latest block of some shardchain, and a newer masterchain block $B'$, with $B$ as one of its antecessors, lists $S'$ as the latest block of the same shardchain, then $S$ must be one of the antecessors of $S'$.\footnote{In order to express this condition correctly in the presence of dynamic sharding, one should fix some account $\xi$, and consider the latest blocks $S$ and $S'$ of the shardchains containing $\xi$ in the shard configurations of both $B$ and $B'$, since the shards containing $\xi$ might be different in $B$ and $B'$.} This condition makes the total state of the TON blockchain defined by a subsequent masterchain block $B'$ compatible with the total state defined by a previous block $B$. + +\nxsubpoint\label{sp:shard.total.state}\emb{Total state defined to by a shardchain block} +Every shardchain block contains the hash of the most recent masterchain block in its header. Consequently, all the blocks referred to in that masterchain block, along with their antecessors, are considered ``known'' or ``visible'' to the shardchain block, and no other blocks are visible to it, with the sole exception of its antecessors inside its proper shardchain. + +In particular, when we say that a block {\em must\/} import in its {\em InMsgDescr\/} the messages from the {\em OutMsgQueue\/} of the states of all neighboring shardchains, it means that precisely the blocks of other shardchains visible to that block must be taken into account, and at the same time the block cannot contain messages from ``invisible'' blocks, even if they are otherwise correct. + +\mysubsection{Configurable parameters and smart contracts}\label{p:conf.params} +Recall that the TON Blockchain has several so-called ``configurable parameters'' (cf.~\cite{TON}), which are either certain values or certain smart contracts residing in the masterchain. This section discusses the storage of and access to these configurable parameters. + +\nxsubpoint\emb{Examples of configurable parameters} +The properties of the blockchain controlled by configurable parameters include: +\begin{itemize} +\item The minimum stake for validators. +\item The maximum size of the group of elected validators. +\item The maximum number of blocks for which the same group of validators are responsible. +\item The validator election process. +\item The validator punishing process. +\item The currently active and the next elected set of validators. +\item The process of changing configurable parameters, and the address of the smart contract $\gamma$ responsible for holding the values of the configurable parameters and for modifying their values. +\end{itemize} + +\nxsubpoint\emb{Location of the values of configurable parameters} +The configurable parameters are kept in the persistent data of a special configuration smart contract $\gamma$ residing in the masterchain of the TON Blockchain. More precisely, the first reference of the root cell of the persistent data of that smart contract is a dictionary mapping 64-bit keys (parameter numbers) to the values of the corresponding parameters; each value is serialized into a cell slice according to the type of that value. If a value is a ``smart contract'' (necessarily residing in the masterchain), its 256-bit account address is used instead. + +\nxsubpoint\label{sp:conf.par.qa}\emb{Quick access through the header of masterchain blocks} +To simplify access to the current values of configurable parameters, and to shorten the Merkle proofs containing references to them, the header of each masterchain block contains the address of smart contract $\gamma$. It also contains a direct cell reference to the dictionary containing all values of configurable parameters, which lies in the persistent data of~$\gamma$. Additional consistency conditions ensure that this reference coincides with the one obtained by inspecting the final state of smart contract~$\gamma$. + +\nxsubpoint\emb{Getting values of configurable parameters by get methods} +The configuration smart contract $\gamma$ provides access to some of configurable parameters by means of ``get methods''. These special methods of the smart contract do not change its state, but instead return required data in the TVM stack. + +\nxsubpoint\emb{Getting values of configurable parameters by get messages} +Similarly, the configuration smart contract $\gamma$ may define some ``ordinary'' methods (i.e., special inbound messages) to request the values of certain configuration parameters, which will be sent in the outbound messages generated by the transaction processing such an inbound message. This may be useful for some other fundamental smart contracts that need to know the values of certain configuration parameters. + +\nxsubpoint\emb{Values obtained by get methods may be different from those obtained through the block header} +Notice that the state of the configuration smart contract~$\gamma$, including the values of configurable parameters, may change several times inside a masterchain block, if there are several transactions processed by~$\gamma$ in that block. As a consequence, the values obtained by invoking get methods of~$\gamma$, or sending get messages to $\gamma$, may be different from those obtained by inspecting the reference in the block header (cf.~\ptref{sp:conf.par.qa}), which refers to the {\em final\/} state of the configurable parameters in the block. + +\nxsubpoint\label{sp:conf.par.change}\emb{Changing the values of configurable parameters} +The procedure for changing the values of configurable parameters is defined in the code of smart contract~$\gamma$. For most configurable parameters, called {\em ordinary}, any validator may suggest a new value by sending a special message with the number of the parameter and its proposed value to~$\gamma$. If the suggested value is valid, further voting messages from the validators are collected by the smart contract, and if more than two-thirds each of the current and next sets of validators support the proposal, the value is changed. + +Some parameters, such as the current set of validators, cannot be changed in this way. Instead, the current configuration contains a parameter with the address of smart contract $\nu$ responsible for electing the next set of validators, and smart contract $\gamma$ accepts messages only from this smart contract $\nu$ to modify the value of the configuration parameter containing the current set of validators. + +\nxsubpoint\emb{Changing the validator election procedure} +If the validator election procedure ever needs to be changed, this can be accomplished by first committing a new validator election smart contract into the masterchain, and then changing the ordinary configurable parameter containing the address $\nu$ of the validator election smart contract. This will require two-thirds of the validators to accept the proposal in a vote as described above in~\ptref{sp:conf.par.change}. + +\nxsubpoint\emb{Changing the procedure of changing configurable parameters} +Similarly, the address of the configuration smart contract itself is a configurable parameter and may be changed in this fashion. In this way, most fundamental parameters and smart contracts of the TON Blockchain may be modified in any direction agreed upon by the qualified majority of the validators. + +\nxsubpoint\emb{Initial values of the configurable parameters} +The initial values of most configurable parameters appear in block zero of the masterchain as part of the masterchain's initial state, which is explicitly present with no omissions in this block. The code of all fundamental smart contracts is also present in the initial state. In this way, the original ``constitution'' and configuration of the TON Blockchain, including the original set of validators, is made explicit in block zero. + +\mysubsection{New smart contracts and their addresses}\label{p:acc.create} +This section discusses the creation and initialization of new smart contracts---in particular, the origin of their initial code, persistent data, and balance. It also discusses the assignment of account addresses to new smart contracts. + +\nxsubpoint\emb{Description valid only for masterchain and basic workchain} +The mechanisms for creating new smart contracts and assigning their addresses described in this section are valid only for the basic workchain and the masterchain. Other workchains may define their own mechanisms for dealing with these problems. + +\nxsubpoint\label{sp:crypto.to.uninit}\emb{Transferring cryptocurrency to uninitialized accounts} +First of all, {\em it is possible to send messages, including value-bearing messages, to previously unmentioned accounts.} If an inbound message arrives at a shardchain with a destination address $\eta$ corresponding to an undefined account, it is processed by a transaction as if the code of the smart contract were empty (i.e., consisting of an implicit \texttt{RET}). If the message is value-bearing, this leads to the creation of an ``uninitialized account'', which may have a non-zero balance (if value-bearing messages have been sent to it),\footnote{Value-bearing messages with the {\tt bounce} flag set will not be accepted by an uninitialized account, but will be ``bounced'' back.} but has no code and no data. Because even an uninitialized account occupies some persistent storage (needed to hold its balance), some small persistent-storage payments will be exacted from time to time from the account's balance, until it becomes negative. + +\nxsubpoint\label{sp:constr.msg}\emb{Initializing smart contracts by constructor messages} +An account, or smart contract, is created by sending a special {\em constructor message\/} $M$ to its address $\eta$. The body of such a message contains the tree of cells with the initial code of the smart contract (which may be replaced by its hash in some situations), and the initial data of the smart contract (maybe empty; it can be replaced by its hash). The hash of the code and of the data contained in the constructor message must coincide with the address $\eta$ of the smart contract; otherwise, it is rejected. + +After the code and data of the smart contract are initialized from the body of the constructor message, the remainder of the constructor message is processed by a transaction (the {\em creating transaction} for smart contract $\eta$) by invoking TVM in a manner similar to that used for processing ordinary inbound messages. + +\nxsubpoint\emb{Initial balance of a smart contract} +Notice that the constructor message usually must bear some value, which will be transferred to the balance of the newly-created smart contract; otherwise, the new smart contract would have a balance of zero and would not be able to pay for storing its code and data in the blockchain. The minimum balance required from a newly-created smart contract is a linear (more precisely, affine) function of the storage it uses. The coefficients of this function may depend on the workchain; in particular, they are higher in the masterchain than in the basic workchain. + +\nxsubpoint\emb{Creating smart contracts by external constructor messages} +In some cases, it is necessary to create a smart contract by a constructor message that cannot bear any value---for instance, by a constructor message ``from nowhere'' (an external inbound message). Then one should first transfer a sufficient amount of funds to the uninitialized smart contract as explained in~\ptref{sp:crypto.to.uninit}, and only then send a constructor message ``from nowhere''. + +\nxsubpoint\label{sp:ex.simple.wallet}\emb{Example: creating a cryptocurrency wallet smart contract} +An example of the above situation is provided by cryptocurrency wallet applications for human users, which must create a special wallet smart contract in the blockchain in which to keep the user's funds. This can be achieved as follows: +\begin{itemize} +\item The cryptocurrency wallet application generates a new cryptographic public/private key pair (typically for Ed25519 elliptic curve cryptography, supported by special TVM primitives) for signing the user's future transactions. +\item The cryptocurrency wallet application knows the code of the smart contract to be created (which typically is the same for all users), as well as the data, which typically consists of the public key of the wallet (or of its hash) and is generated at the very beginning. The hash of this information is the address~$\xi$ of the wallet smart contract to be created. +\item The wallet application may display the user's address $\xi$, and the user may start to receive funds to her uninitialized account $\xi$---for example, by buying some cryptocurrency at an exchange, or by asking a friend to transfer a small sum. +\item The wallet application can inspect the shardchain containing account $\xi$ (in the case of a basic workchain account) or the masterchain (in the case of a masterchain account), either by itself or using a blockchain explorer, and check the balance of~$\xi$. +\item If the balance is sufficient, the wallet application may create and sign (with the user's private key) the constructor message (``from nowhere''), and submit it for inclusion to the validators or the collators for the corresponding blockchain. +\item Once the constructor message is included into a block of the blockchain and processed by a transaction, the wallet smart contract is finally created. +\item When the user wants to transfer some funds to some other user or smart contract $\eta$, or wants to send a value-bearing message to $\eta$, she uses her wallet application to create the message $m$ that she wants her wallet smart contract $\xi$ to send to $\eta$, envelope $m$ into a special ``message from nowhere'' $m'$ with destination $\xi$, and sign $m'$ with her private key. Some provisions against replay attacks must be made, as explained in~\ptref{sp:msg.uniq}. +\item The wallet smart contract receives message $m'$ and checks the validity of the signature with the aid of the public key stored in its persistent data. If the signature is correct, it extracts embedded message $m$ from $m'$ and sends it to its intended destination $\eta$, with the indicated amount of funds attached to it. +\item If the user does not need to immediately start transferring funds, but only wants to passively receive some funds, she may keep her account uninitialized as long as she wants (provided the persistent storage payments do not lead to the exhaustion of its balance), thus minimizing the storage profile and persistent storage payments of the account. +\item Notice that the wallet application may create for the human user the illusion that the funds are kept in the application itself, and provide an interface to transfer funds or send arbitrary messages ``directly'' from the user's account~$\xi$. In reality, all these operations will be performed by the user's wallet smart contract, which effectively acts as a proxy for such requests. We see that a cryptocurrency wallet is a simple example of a {\em mixed\/} application, having an on-chain part (the wallet smart contract, used as a proxy for outbound messages) and an off-chain part (the external wallet application running on a user's device and keeping the private account key). +\end{itemize} +Of course, this is just one way of dealing with the simplest user wallet smart contracts. One can create multi-signature wallet smart contracts, or create a shared wallet with internal balances kept inside it for each of its individual users, and so on. + +\nxsubpoint\emb{Smart contracts may be created by other smart contracts} +Notice that a smart contract may generate and send a constructor message while processing any transaction. In this way, smart contracts may automatically create new smart contracts, if they need to, without any human intervention. + +\nxsubpoint\emb{Smart contracts may be created by wallet smart contracts} +On the other hand, a user may compile the code for her new smart contract~$\nu$, generate the corresponding constructor message~$m$, and use the wallet application to force her wallet smart contract~$\xi$ to send message $m$ to~$\nu$ with an adequate amount of funds, thus creating the new smart contract~$\nu$. + +\mysubsection{Modification and removal of smart contracts} +This section explains how the code and state of a smart contract may be changed, and how and when a smart contract may be destroyed. + +\nxsubpoint\emb{Modification of the data of a smart contract} +The persistent data of a smart contract is usually modified as a result of executing the code of the smart contract in TVM while processing a transaction, triggered by an inbound message to the smart contract. More specifically, the code of the smart contract has access to the old persistent storage of the smart contract via TVM control register \texttt{c4}, and may modify the persistent storage by storing another value into~\texttt{c4} before normal termination. + +Normally, there are no other ways to modify the data of an existing smart contract. If the code of the smart contract does not provide any ways to modify the persistent data (e.g., if it is a simple wallet smart contract as described in~\ptref{sp:ex.simple.wallet}, which initializes the persistent data with the user's public key and does not intend to ever change it), then it will be effectively immutable---unless the code of the smart contract is modified first. + +\nxsubpoint\emb{Modification of the code of a smart contract} +Similarly, the code of an existing smart contract may be modified only if some provisions for such an upgrade are present in the current code. The code is modified by invoking TVM primitive \texttt{SETCODE}, which sets the root of the code for the current smart contract from the top value in the TVM stack. The modification is applied only after the normal termination of the current transaction. + +Typically, if the developer of a smart contract wants to be able to upgrade its code in the future, she provides a special ``code upgrade method'' in the original code of the smart contract, which invokes \texttt{SETCODE} in response to certain inbound ``code upgrade'' messages, using the new code sent in the message itself as an argument to \texttt{SETCODE}. Some provisions must be made to protect the smart contract from unauthorized replacement of the code; otherwise, control of the smart contract and the funds on its balance could be lost. For example, code upgrade messages might be accepted only from a trusted source address, or they might be protected by requiring a valid cryptographic signature and a correct sequence number. + +\nxsubpoint\emb{Keeping the code or data of the smart contract outside the blockchain} +The code or data of the smart contract may be kept outside the blockchain and be represented only by their hashes. In such cases, only empty inbound messages may be processed, as well as messages carrying a correct copy of the smart-contract code (or its portion relevant for processing the specific message) and its data inside special fields. An example of such a situation is given by the uninitialized smart contracts and constructor messages described in~\ptref{p:acc.create}. + +\nxsubpoint\emb{Using code libraries} +Some smart contracts may share the same code, but use different data. One example of this is wallet smart contracts (cf.~\ptref{sp:ex.simple.wallet}), which are likely to use the same code (throughout all wallets created by the same software), but with different data (because each wallet must use its own pair of cryptographic keys). In this case, the code for all the wallet smart contracts is best committed by the developer into a shared {\em library}; this library would reside in the masterchain, and be referred to by its hash using a special ``external library cell reference'' as the root of the code of each wallet smart contract (or as a subtree inside that code). + +Notice that even if the library code becomes unavalable---for example, because its developer stops paying for its storage in the masterchain---it is still possible to use the smart contracts referring to this library, either by committing the library again into the masterchain, or by including its relevant parts inside a message sent to the smart contract. This external cell reference resolution mechanism is discussed in more detail later in~\ptref{sp:lib.env}. + +\nxsubpoint\emb{Destroying smart contracts} +Notice that a smart contract cannot really be destroyed until its balance becomes zero or negative. It may become negative as a result of collecting persistent storage payments, or after sending a value-bearing outbound message transferring almost all of its previous balance. + +For example, a user may decide to transfer all remaining funds from her wallet to another wallet or smart contract. This may be useful, for instance, if one wants to upgrade the wallet, but the wallet smart contract does not have any provisions for future upgrades; then one can simply create a new wallet and transfer all funds to it. + +\nxsubpoint\emb{Frozen accounts} +When the balance of an account becomes non-positive after a transaction, or smaller than a certain workchain-dependent minimum, the account is {\em frozen\/} by replacing all its code and data by a single 32-byte hash. This hash is kept afterwards for some time (e.g., a couple of months) to prevent recreation of the smart contract by its original creating transaction (which still has the correct hash, equal to the account address), and to allow its owner to recreate the account by transferring some funds and sending a message containing the account's code and data, to be reinstated in the blockchain. In this respect, frozen accounts are similar to uninitialized accounts; however, the hash of the correct code and data for a frozen account is not necessarily equal to the account address, but is kept separately. + +Notice that frozen accounts may have a negative balance, indicating that persistent storage payments are due. An account cannot be unfrozen until its balance becomes positive and larger than a prescribed minimum value. + +\clearpage +\mysection{Message forwarding and delivery guarantees} +This chapter discusses the forwarding of messages inside the TON Blockchain, including the Hypercube Routing (HR) and Instant Hypercube Routing (IHR) protocols. It also describes the provisions required to implement the message delivery guarantees and the FIFO ordering guarantee. + +\mysubsection{Message addresses and next-hop computation} +This section explains the computation of transit and next-hop addresses by the variant of the hypercube routing algorithm employed in TON Blockchain. The hypercube routing protocol itself, which uses the concepts and next-hop address computation algorithm introduced in this section, is presented in the next section. + +\nxsubpoint\emb{Account addresses} +The {\em source address\/} and {\em destination address\/} are always present in any message. Normally, they are {\em (full) account addresses}. A full account address consists of a $\workchainid$ (a signed 32-bit big-endian integer defining a workchain), followed by a (usually) 256-bit {\em internal address\/} or {\em account identifier\/} $\accountid$ (which may also be interpreted as an unsigned big-endian integer) defining the account within the chosen workchain. + +Different workchains may use account identifiers that are shorter or longer than the ``standard'' 256 bits used in the masterchain ($\workchainid=-1$) and in the basic workchain ($\workchainid=0$). To this end, the masterchain state contains a list of all workchains defined so far, along with their account identifier lengths. An important restriction is that the $\accountid$ for any workchain must be at least 64 bits long. + +In what follows, we often consider only the case of 256-bit account addresses for simplicity. Only the first 64 bits of the $\accountid$ are relevant for the purposes of message routing and shardchain splitting. + +\nxsubpoint\emb{Source and destination addresses of a message} +Any message has both a {\em source address\/} and a {\em destination address}. Its source address is the address of the account (smart contract) that has created the message while processing some transaction; the source address cannot be changed or set arbitrarily, and smart contracts heavily rely on this property. By contrast, when a message is created, any well-formed destination address may be chosen; after that, the destination address cannot be changed. + +\nxsubpoint\emb{External messages with no source or destination address} +Some messages can have no source or no destination address (though at least one of them must be present), as indicated by special flags in the message header. Such messages are the {\em external messages} intended for the interaction of the TON Blockchain with the outside world---human users and their cryptowallet applications, off-chain and mixed applications and services, other blockchains, and so on. + +External messages are never routed inside the TON Blockchain. Instead, ``messages from nowhere'' (i.e., with no source address) are directly included into the \textit{InMsgDescr\/} of a destination shardchain block (provided some conditions are met) and processed by a transaction in that very block. Similarly, ``messages to nowhere'' (i.e., with no TON Blockchain destination address), also known as {\em log messages}, are also present only in the block containing the transaction that generated such a message.\footnote{``Messages to nowhere'' may have some special fields in their body indicating their destination outside the TON Blockchain---for instance, an account in some other blockchain, or an IP address and port---which may be interpreted by the third-party software appropriately. Such fields are ignored by the TON Blockchain.} + +Therefore, external messages are almost irrelevant for the discussion of message routing and message delivery guarantees. In fact, the message delivery guarantees for outbound external messages are trivial (at most, the message must be included into the \textit{LogMsg} part of the block), and for inbound external messages there are none, since the validators of a shardchain block are free to include or ignore suggested inbound external messages at their discretion (e.g., according to the processing fee offered by the message).\footnote{The problem of bypassing possible validator censorship---which could happen, for instance, if all validators conspire not to include external messages sent to accounts belonging to some set of blacklisted accounts---is dealt with separately elsewhere. The main idea is that the validators may be forced to promise to include a message with a known hash in a future block, without knowing anything about the identity of the sender or the receiver; they will have to keep this promise afterwards when the message itself with pre-agreed hash is presented.} + +In what follows, we focus on ``usual'' or ``internal'' messages, which have both a source and a destination address. + +\nxsubpoint\emb{Transit and next-hop addresses} +When a message needs to be routed through intermediate shardchains before reaching its intended destination, it is assigned a {\em transit address\/} and a {\em next-hop address\/} in addition to the (immutable) source and destination addresses. When a copy of the message resides inside a transit shardchain awaiting its relay to its next hop, the {\em transit address\/} is its intermediate address lying in the transit shardchain, as if belonging to a special message-relay smart contract whose only job is to relay the unchanged message to the next shardchain on the route. The {\em next-hop address\/} is the address in a neighboring shardchain (or, on some rare occasions, in the same shardchain) to which the message needs to be relayed. After the message is relayed, the next-hop address usually becomes the transit address of the copy of the message included in the next shardchain. + +Immediately after an outbound message is created in a shardchain (or in the masterchain), its transit address is set to its source address.\footnote{However, the internal routing process described in~\ptref{sp:hr.int.route} is applied immediately after that, which may further modify the transit address.} + +\nxsubpoint\label{sp:hr.next.hop}\emb{Computation of the next-hop address for hypercube routing} +The TON Blockchain employs a variant of hypercube routing. This means that the next-hop address is computed from the transit address (originally equal to the source address) as follows: + +\begin{enumerate} +\item The (big-endian signed) 32-bit $\workchainid$ components of both the transit address and destination address are split into groups of $n_1$ bits (currently, $n_1=32$), and they are scanned from the left (i.e., the most significant bits) to the right. If one of the groups in the transit address differs from the corresponding group in the destination address, then the value of this group in the transit address is replaced by its value in the destination address to compute the next-hop address. +\item If the $\workchainid$ parts of the transit and destination addresses match, then a similar process is applied to the $\accountid$ parts of the addresses: The $\accountid$ parts, or rather their first (most significant) 64 bits, are split into groups of $n_2$ bits (currently, $n_2=4$ bit groups are used, corresponding to the hexadecimal digits of the address) starting from the most significant bit, and are compared starting from the left. The first group that differs is replaced in the transit address with its value in the destination address to compute the next-hop address. +\item If the first 64 bits of the $\accountid$ parts of the transit and destination addresses match as well, then the destination account belongs to the current shardchain, and the message should not be forwarded outside the current shardchain at all. Instead, it must be processed by a transaction inside it. +\end{enumerate} + +\nxsubpoint\label{sp:nh.notat}\emb{Notation for the next-hop address} +We denote by +\begin{equation} + \NextHop(\xi,\eta) +\end{equation} +the next-hop address computed for current (source or transit) address $\xi$ and destination address $\eta$. + +\nxsubpoint\label{sp:nh.anycast}\emb{Support for anycast addresses} +``Large'' smart contracts, which can have separate instances in different shardchains, may be reached using {\em anycast destination addresses}. These addresses are supported as follows. + +An anycast address $(\eta,d)$ consists of a usual address $\eta$ along with its ``splitting depth'' $d\leq 31$. The idea is that the message may be delivered to any address differing from $\eta$ only in the first $d$ bits of the internal address part (i.e., not including the workchain identifier, which must match exactly). This is achieved as follows: +\begin{itemize} +\item The effective destination address $\tilde\eta$ is computed from $(\eta,d)$ by replacing the first $d$ bits of the internal address part of $\eta$ with the corresponding bits taken from the source address $\xi$. +\item All computations of $\NextHop(\nu,\eta)$ are replaced by $\NextHop(\nu,\tilde\eta)$, for $\nu=\xi$ as well as for all other intermediate addresses $\nu$. In this way, Hypercube Routing or Instant Hypercube Routing will ultimately deliver the message to the shardchain containing $\tilde\eta$. +\item When the message is processed in its destination shardchain (the one containing address $\tilde\eta$), it may be processed by an account $\eta'$ of the same shardchain differing from $\eta$ and $\tilde\eta$ only in the first $d$ bits of the internal address part. More precisely, if the common shard address prefix is $s$, so that only internal addresses starting with binary string $s$ belong to the destination shard, then $\eta'$ is computed from $\eta$ by replacing the first $\min(d,|s|)$ bits of the internal address part of $\eta$ with the corresponding bits of~$s$. +\end{itemize} +That said, we tacitly ignore the existence of anycast addresses and the additional processing they require in the following discussions. + +\nxsubpoint\label{sp:nh.hamming.opt}\emb{Hamming optimality of the next-hop address algorithm} +Notice that the specific hypercube routing next-hop computation algorithm explained in~\ptref{sp:hr.next.hop} may potentially be replaced by another algorithm, provided it satisfies certain properties. One of these properties is the {\em Hamming optimality}, meaning that the Hamming ($L_1$) distance from $\xi$ to $\eta$ equals the sum of Hamming distances from $\xi$ to $\NextHop(\xi,\eta)$ and from $\NextHop(\xi,\eta)$ to $\eta$: +\begin{equation}\label{eq:hamm.opt} + {\|\xi-\eta\|}_1=\bigl\|\xi-\NextHop(\xi,\eta)\bigr\|_1+\bigl\|\NextHop(\xi,\eta)-\eta\bigr\|_1 +\end{equation} +Here ${\|\xi-\eta\|}_1$ is the {\em Hamming distance\/} between $\xi$ and $\eta$, equal to the number of bit positions in which $\xi$ and $\eta$ differ:\footnote{When the addresses involved are of different lengths (e.g., because they belong to different workchains), one should consider only the first 96 bits of the addresses in the above formula.} +\begin{equation} + {\|\xi-\eta\|}_1=\sum_i|\xi_i-\eta_i| +\end{equation} + +Notice that in general one should expect only an inequality in \eqref{eq:hamm.opt}, following from the triangle inequality for the $L_1$-metric. Hamming optimality essentially means that $\NextHop(\xi,\eta)$ lies on one of the (Hamming) shortest paths from $\xi$ to $\eta$. It can also be expressed by saying that $\nu=\NextHop(\xi,\eta)$ is always obtained from $\xi$ by changing the values of bits at some positions to their values in $\eta$: for any bit position $i$, we have $\nu_i=\xi_i$ or $\nu_i=\eta_i$.\footnote{Instead of Hamming optimality, we might have considered the equivalent property of {\em Kademlia optimality}, written for the Kademlia (or weighted $L_1$) distance as given by $\|\xi-\eta\|_K:=\sum_i2^{-i}|\xi_i-\eta_i|$ instead of the Hamming distance.} + +\nxsubpoint\emb{Non-stopping of $\NextHop$} +Another important property of the $\NextHop$ is its {\em non-stopping}, meaning that $\NextHop(\xi,\eta)=\xi$ is possible only when $\xi=\eta$. In other words, if we have not yet arrived at $\eta$, the next hop cannot coincide with our current position. + +This property implies that the path from $\xi$ to $\eta$---i.e., the sequence of intermediate addresses $\xi^{(0)}:=\xi$, $\xi^{(n)}:=\NextHop(\xi^{(n-1)},\eta)$---will gradually stabilize at $\eta$: for some $N\geq0$, we have $\xi^{(n)}=\eta$ for all $n\geq N$. Indeed, one can always take $N:={\|\xi-\eta\|}_1$. + +\nxsubpoint\label{sp:path.conv}\emb{Convexity of the HR path with respect to sharding} +A consequence of Hamming optimality property~\eqref{eq:hamm.opt} is what we call the {\em convexity\/} of the path from $\xi$ to $\eta$ with respect to sharding. Namely, if $\xi^{(0)}:=\xi$, $\xi^{(n)}:=\NextHop(\xi^{(n-1)},\eta)$ is the computed path from $\xi$ to $\eta$, and $N$ is the first index such that $\xi^{(N)}=\eta$, and $S$ is a shard of some workchain in any shard configuration, then the indices $i$ with $\xi^{(i)}$ residing in shard~$S$ constitute a subinterval in $[0,N]$. In other words, if integers $0\leq i\leq j\leq k\leq N$ are such that $\xi^{(i)}$, $\xi^{(k)}\in S$, then $\xi^{(j)}\in S$ as well. + +This convexity property is important for some proofs related to message forwarding in the presence of dynamic sharding. + +\nxsubpoint\label{sp:hr.int.route}\emb{Internal routing} +Notice that the next-hop address computed according to the rules defined in~\ptref{sp:hr.next.hop} may belong to the same shardchain as the current one (i.e., the one containing the transit address). In that case, the ``internal routing'' occurs immediately, the transit address is replaced by the value of the computed next-hop address, and the next-hop address computation step is repeated until a next-hop address lying outside the current shardchain is obtained. The message is then kept in the transit output queue according to its computed next-hop address, with its last computed transit address as the ``intermediate owner'' of the transit message. If the current shardchain splits into two shardchains before the message is forwarded further, it is the shardchain containing the intermediate owner that inherits this transit message. + +Alternatively, we might go on computing the next-hop addresses only to find out that the destination address already belongs to the current shardchain. In that case, the message will be processed (by a transaction) inside this shardchain instead of being forwarded further. + +\nxsubpoint\emb{Neighboring shardchains} +Two shards in a shard configuration---or the two corresponding shardchains---are said to be {\em neighbors}, or {\em neighboring shardchains}, if one of them contains a next-hop address for at least one combination of allowed source and destination addresses, while the other contains the transit address for the same combination. In other words, two shardchains are neighbors if a message can be forwarded directly from one of them into the other via Hypercube Routing. + +The masterchain is also included in this definition, as if it were the only shardchain of the workchain with $\workchainid=-1$. In this respect, it is a neighbor of all the other shardchains. + +\nxsubpoint\emb{Any shard is a neighbor of itself} +Notice that a shardchain is always considered a neighbor of itself. This may seem redundant, because we always repeat the next-hop computation described in~\ptref{sp:hr.next.hop} until we obtain a next-hop address outside the current shardchain (cf.~\ptref{sp:hr.int.route}). However, there are at least two reasons for such an arrangement: +\begin{itemize} +\item Some messages have the source and the destination address inside the same shardchain, at least when the message is created. However, if such a message is not processed immediately in the same block where it has been created, it must be added to the outbound message queue of its shardchain, and be imported as an inbound message (with an entry in the {\em InMsgDescr}) in one of the subsequent blocks of the same shardchain.\footnote{Notice that the next-hop and internal-routing computations are still applied to such messages, since the current shardchain may be split before the message is processed. In this case, the new sub-shardchain containing the destination address will inherit the message.} +\item Alternatively, the next-hop address may originally be in some other shardchain that later gets merged with the current shardchain, so that the next hop becomes inside the same shardchain. Then the message will have to be imported from the outbound message queue of the merged shardchain, and forwarded or processed accordingly to its next-hop address, even though they reside now inside the same shardchain. +\end{itemize} + +\nxsubpoint\label{sp:isp.hr}\emb{Hypercube Routing and the ISP} +Ultimately, the Infinite Sharding Paradigm (ISP) applies here: a shardchain should be considered a provisional union of accountchains, grouped together solely to minimize the block generation and transmission overhead. + +The forwarding of a message runs through several intermediate account\-chains, some of which can happen to lie in the same shard. In this case, once a message reaches an accountchain lying in this shard, it is immediately (``internally'') routed inside that shard until the last accountchain lying in the same shard is reached (cf.~\ptref{sp:hr.int.route}). Then the message is enqueued in the output queue of that last accountchain.\footnote{We may define the (virtual) output queue of an account(chain) as the subset of the {\em OutMsgQueue\/} of the shard currently containing that account that consists of messages with transit addresses equal to the address of the account.} + +\nxsubpoint\label{sp:repr.interm.addr}\emb{Representation of transit and next-hop addresses} +Notice that the transit and next-hop addresses differ from the source address only in the $\workchainid$ and in the first (most significant) 64 bits of the account address. Therefore, they may be represented by 96-bit strings. Furthermore, their $\workchainid$ usually coincides with the $\workchainid$ of either the source address or the destination address; a couple of bits may be used to indicate this situation, thus further reducing the space required to represent the transit and next-hop addresses. + +In fact, the required storage may be reduced even further by observing that the specific hypercube routing algorithm described in~\ptref{sp:hr.next.hop} always generates intermediate (i.e., transit and next-hop) addresses that coincide with the destination address in their first $k$ bits, and with the source address in their remaining bits. Therefore, one might use just the values $0\leq k_{\text{tr}},k_{\text{nh}}\leq 96$ to fully specify the transit and next-hop addresses. One might also notice that $k':=k_{\text{nh}}$ turns out to be a fixed function of $k:=k_{\text{tr}}$ (for instance, $k'=k+n_2=k+4$ for $k\geq32$), and therefore include only one 7-bit value of~$k$ in the serialization. + +Such optimizations have the obvious disadvantage that they rely too much on the specific routing algorithm used, which can be changed in the future, so they are used in~\ptref{sp:tl.msg.env} with a provision to specify more general intermediate addresses if necessary. + +\nxsubpoint\label{sp:msg.env}\emb{Message envelopes} +The transit and next-hop addresses of a forwarded message are not included in the message itself, but are kept in a special {\em message envelope}, which is a cell (or a cell slice) containing the transit and next-hop addresses with the above optimizations, some other information relevant for forwarding and processing, and a reference to a cell containing the unmodified original message. In this way, a message can easily be ``extracted'' from its original envelope (e.g., the one present in the {\em InMsgDescr}) and be put into another envelope (e.g., before being included into the {\em OutMsgQueue}). + +In the representation of a block as a tree, or rather a DAG, of cells, the two different envelopes will contain references to a shared cell with the original message. If the message is large, this arrangement avoids the need to keep more than one copy of the message in the block. + +\mysubsection{Hypercube Routing protocol} +This section exposes the details of the hypercube routing protocol employed by the TON Blockchain to achieve guaranteed delivery of messages between smart contracts residing in arbitrary shardchains. For the purposes of this document, we will refer to the variant of hypercube routing employed by the TON Blockchain as Hypercube Routing (HR). + +\nxsubpoint\label{sp:msg.uniq}\emb{Message uniqueness} +Before continuing, let us observe that any (internal) message is {\em unique}. Recall that a message contains its full source address along with its logical creation time, and all outbound messages created by the same smart contract have strictly increasing logical creation times (cf.~\ptref{sp:lt.ton.blkch}); therefore, the combination of the full source address and the logical creation time uniquely defines the message. Since we assume the chosen hash function $\Sha$ to be collision resistant, {\em a message is uniquely determined by its hash}, so we can identify two messages if we know that their hashes coincide. + +This does not extend to external messages ``from nowhere'', which have no source addresses. Special care must be taken to prevent replay attacks related to such messages, especially by designers of user wallet smart contracts. One possible solution is to include a sequence number in the body of such messages, and keep the count of external messages already processed inside the smart-contract persistent data, refusing to process an external message if its sequence number differs from this count. + +\nxsubpoint\label{sp:msg.hash.ident}\emb{Identifying messages with equal hashes} +The TON Blockchain assumes that two messages with the same hashes coincide, and treats either of them as a redundant copy of the other. As explained above in~\ptref{sp:msg.uniq}, this does not lead to any unexpected effects for internal messages. However, if one sends two coinciding ``messages from nowhere'' to a smart contract, it may happen that only one of them will be delivered---or both. If their action is not supposed to be idempotent (i.e., if processing the message twice has a different effect from processing it once), some provisions should be made to distinguish the two messages, for instance by including a sequence number in them. + +In particular, the {\em InMsgDescr\/} and {\em OutMsgDescr\/} use the (unenveloped) message hash as a key, tacitly assuming that distinct messages have distinct hashes. In this way, one can trace the path and the fate of a message across different shardchains by looking up the message hash in the {\em InMsgDescr\/} and {\em OutMsgDescr\/} of different blocks. + +\nxsubpoint\label{sp:out.msg.q}\emb{The structure of {\em OutMsgQueue}} +Recall that the outbound messages --- both those created inside the shardchain, and transit messages previously imported from a neighboring shardchain to be relayed to the next-hop shardchain --- are accumulated in the {\em OutMsgQueue}, which is part of the {\em state\/} of the shardchain (cf.~\ptref{sp:outmsgq}). In contrast with {\em InMsgDescr\/} and {\em OutMsgDescr}, the key in {\em OutMsgQueue} is not the message hash, but its next-hop address---or at least its first 96 bits---concatenated with the message hash. + +Furthermore, the {\em OutMsgQueue\/} is not just a dictionary (hashmap), mapping its keys into (enveloped) messages. Rather, it is a {\em min-augmented dictionary with respect to the logical creation time}, meaning that each node of the Patricia tree representing {\em OutMsgQueue\/} has an additional value (in this case, an unsigned 64-bit integer), and that this augmentation value in each fork node is set to be equal to the minimum of the augmentation values of its children. The augmentation value of a leaf equals the logical creation time of the message contained in that leaf; it need not be stored explicitly. + +\nxsubpoint\emb{Inspecting the {\em OutMsgQueue\/} of a neighbor} +Such a structure for the {\em OutMsgQueue\/} enables the validators of a neighboring shardchain to inspect it to find its part (Patricia subtree) relevant to them (i.e., consisting of messages with the next-hop address belonging to the neighboring shard in question---or having the next-hop address with a given binary prefix), as well as quickly compute the ``oldest'' (i.e., with the minimum logical creation time) message in that part. + +Furthermore, the shard validators do not even need to track the total state of all their neighboring shardchains---they only need to keep and update a copy of their {\em OutMsgQueue}, or even of its subtree related to them. + +\nxsubpoint\label{sp:monot.import} +\emb{Logical time monotonicity: importing the oldest message from the neighbors} +The first fundamental local condition of message forwarding, called {\em (message import) (logical time) monotonicity condition}, may be summarized as follows: +\begin{quote} + While importing messages into the {\em InMsgDescr\/} of a shardchain block from the {\em OutMsgQueue\/}s of its neighboring shardchains, the validators must import the messages in the increasing order of their logical time; in the case of a tie, the message with the smaller hash is imported first. +\end{quote} +More precisely, each shardchain block contains the hash of a masterchain block (assumed to be ``the latest'' masterchain block at the time of the shardchain block's creation), which in turn contains the hashes of the most recent shardchain blocks. In this way, each shardchain block indirectly ``knows'' the most recent state of all other shardchains, and especially its neighboring shardchains, including their {\em OutMsgQueue\/}s.\footnote{In particular, if the hash of a recent block of a neighboring shardchain is not yet reflected in the latest masterchain block, its modifications to {\em OutMsgQueue\/} must not be taken into account.} + +Now an alternative equivalent formulation of the monotonicity condition is as follows: +\begin{quote} +If a message is imported into the {\em InMsgDescr\/} of the new block, its logical creation time cannot be greater than that of any message left unimported in the {\em OutMsgQueue\/} of the most recent state of any of the neighboring shardchains. +\end{quote} +It is this form of the monotonicity condition that appears in the local consistency conditions of the TON Blockchain blocks and is enforced by the validators. + +\nxsubpoint\emb{Witnesses to violations of the message import logical time monotonicity condition} +Notice that if this condition is not fulfilled, a small Merkle proof witnessing its failure may be constructed. Such a proof will contain: +\begin{itemize} +\item A path in the {\em OutMsgQueue\/} of a neighbor from the root to a certain message $m$ with small logical creation time. +\item A path in the {\em InMsgDescr\/} of the block under consideration showing that the key equal to $\Hash(m)$ is absent in {\em InMsgDescr} (i.e., that $m$ has not been included in the current block). +\item A proof that $m$ has not been included in a preceding block of the same shardchain, using the block header information containing the smallest and the largest logical time of all messages imported into the block (cf. \ptref{sp:msg.deliver.chk}--\ptref{sp:hr.ihr.deliver.chk} for more information). +\item A path in {\em InMsgDescr\/} to another included message $m'$, such that either $\LT(m')>\LT(m)$, or $\LT(m')=\LT(m)$ and $\Hash(m')>\Hash(m)$. +\end{itemize} + +\nxsubpoint\label{sp:omsgq.del}\emb{Deleting a message from {\em OutMsgQueue}} +A message must be deleted from {\em OutMsgQueue\/} sooner or later; otherwise, the storage used by {\em OutMsgQueue\/} would grow to infinity. To this end, several ``garbage collection rules'' are introduced. They allow the deletion of a message from {\em OutMsgQueue\/} during the evaluation of a block only if an explicit special ``delivery record'' is present in the {\em OutMsgDescr\/} of that block. This record contains either a reference to the neighboring shardchain block that has included the message into its {\em InMsgDescr\/} (the hash of the block is sufficient, but collated material for the block may contain the relevant Merkle proof), or a Merkle proof of the fact that the message has been delivered to its final destination via Instant Hypercube Routing. + +\nxsubpoint\emb{Guaranteed message delivery via Hypercube Routing} +In this way, a message cannot be deleted from the outbound message queue unless it has been either relayed to its next-hop shardchain or delivered to its final destination (cf.~\ptref{sp:omsgq.del}). Meanwhile, the message import monotonicity condition (cf.~\ptref{sp:monot.import}) ensures that any message will sooner or later be relayed into the next shardchain, taking into account other conditions which require the validators to use at least half of the block's space or gas limits for importing inbound internal messages (otherwise the validators might choose to create empty blocks or import only external messages even in the presence of non-empty outbound message queues at their neighbors). + +\nxsubpoint\label{sp:msg.proc.order}\emb{Message processing order} +When several imported messages are processed by transactions inside a block, the {\em message processing order conditions\/} ensure that older messages are processed first. More precisely, if a block contains two transactions $t$ and $t'$ of the same account, which process inbound messages $m$ and $m'$, respectively, and $\LT(m)<\LT(m')$, then we must have $\LT(t)<\LT(t')$. + +\nxsubpoint\emb{FIFO guarantees of Hypercube Routing} +The message processing order conditions (cf.~\ptref{sp:msg.proc.order}), along with the message import monotonicity conditions (cf.~\ptref{sp:monot.import}), imply the {\em FIFO guarantees for Hypercube Routing}. Namely, if a smart contract $\xi$ creates two messages $m$ and $m'$ with the same destination $\eta$, and $m'$ is generated later than $m$ (meaning that $m\prec m'$, hence $\LT(m)<\LT(m')$), then $m$ will be processed by $\eta$ before $m'$. This is so because both messages will follow the same routing steps on the path from $\xi$ to $\eta$ (the Hypercube Routing algorithm described in~\ptref{sp:hr.next.hop} is deterministic), and in all outbound queues and inbound message descriptions $m'$ will appear ``after'' $m$.\footnote{This statement is not as trivial as it seems at first, because some of the shardchains involved may split or merge during the routing. A correct proof may be obtained by adopting the ISP perspective to HR as explained in~\ptref{sp:isp.hr} and observing that $m'$ will always be behind $m$, either in terms of the intermediate accountchain reached or, if they happen to be in the same accountchain, in terms of logical creation time. + +A crucial observation is that ``at any given moment of time'' (logically; a more precise description would be ``in the total state obtained after processing any causally closed subset $\cF$ of blocks''), the intermediate accountchains belonging to the same shard are contiguous on the path from $\xi$ to~$\eta$ (i.e., cannot have accountchains belonging to some other shard in between). This is a ``convexity property'' (cf.~\ptref{sp:path.conv}) of the Hypercube Routing algorithm described in~\ptref{sp:hr.next.hop}.} + +If message $m'$ can be delivered to $B$ via Instant Hypercube Routing, this is not necessarily true anymore. Therefore, a simple way of ensuring FIFO message delivery discipline between a pair of smart contracts consists in setting a special bit in the message header preventing its delivery via IHR. + +\nxsubpoint\emb{Delivery uniqueness guarantees of Hypercube Routing} +Notice that the message import monotonicity conditions also imply the {\em uniqueness\/} of the delivery of any message via Hypercube Routing---i.e., that it cannot be imported and processed by the destination smart contract more than once. We will see later in~\ptref{p:ihr.combine.deliv} that enforcing delivery uniqueness when both Hypercube Routing and Instant Hypercube Routing are active is more complicated. + +\nxsubpoint\label{sp:hr.overview}\emb{An overview of Hypercube Routing} +Let us summarize all routing steps performed to deliver an internal message $m$ created by source account $\xi_0$ to destination account~$\eta$. We denote by $\xi_{k+1}:=\NextHop(\xi_k,\eta)$, $k=0,1,2,\ldots$ the intermediate addresses dictated by HR for forwarding the message $m$ to its final destination $\eta$. Let $S_k$ be the shard containing $\xi_k$. +\begin{itemize} +\item{[Birth]} --- Message $m$ with destination $\eta$ is created by a transaction $t$ belonging to an account $\xi_0$ residing in some shardchain $S_0$. The logical creation time $\LT(m)$ is fixed at this point and included into the message $m$. +\item{[ImmediateProcessing?]} --- If the destination $\eta$ resides in the same shardchain $S_0$, the message may be processed in the same block it was generated in. In this case, $m$ is included into {\em OutMsgDescr\/} with a flag indicating it has been processed in this very block and need not be forwarded further. Another copy of $m$ is included into {\em InMsgDescr}, along with the usual data describing the processing of inbound messages. (Notice that $m$ is not included into the {\em OutMsgQueue\/} of $S_0$.) +\item{[InitialInternalRouting]} --- If $m$ either has a destination outside $S_0$, or is not processed in the same block where it was generated, the internal routing procedure described in~\ptref{sp:hr.int.route} is applied, until an index $k$ is found such that $\xi_k$ lies in $S_0$, but $\xi_{k+1}=\NextHop(\xi_k,\eta)$ does not (i.e., $S_k=S_0$, but $S_{k+1}\neq S_0$). Alternatively, this process stops if $\xi_k=\eta$ or $\xi_k$ coincides with $\eta$ in its first 96 bits. +\item{[OutboundQueuing]} --- The message $m$ is included into {\em OutMsgDescr\/} (with the key equal to its hash), with an envelope containing its transit address $\xi_k$ and next-hop address $\xi_{k+1}$ as explained in \ptref{sp:msg.env} and~\ptref{sp:repr.interm.addr}. The same enveloped message is also included in the {\em OutMsgQueue\/} of the state of $S_k$, with the key equal to the concatenation of the first 96 bits of its next-hop address $\xi_{k+1}$ (which may be equal to $\eta$ if $\eta$ belongs to $S_k$) and the message hash $\Hash(m)$. +\item{[QueueWait]} --- Message $m$ waits in the {\em OutMsgQueue\/} of shardchain $S_k$ to be forwarded further. In the meantime, shardchain $S_k$ may split or merge with other shardchains; in that case, the new shard $S'_k$ containing the transit address $\xi_k$ inherits $m$ in its {\em OutMsgQueue}. +\item{[ImportInbound]} --- At some point in the future, the validators for the shardchain $S_{k+1}$ containing the next-hop address $\xi_{k+1}$ scan the {\em OutMsgQueue\/} in the state of shardchain $S_k$ and decide to import message $m$ in keeping with the monotonicity condition (cf.~\ptref{sp:monot.import}) and other conditions. A new block for shardchain $S_{k+1}$ is generated, with an enveloped copy of $m$ included in its {\em InMsgDescr}. The entry in {\em InMsgDescr\/} contains also the {\em reason\/} for importing $m$ into this block, with a hash of the most recent block of shardchain $S'_k$, and the previous next-hop and transit addresses $\xi_k$ and $\xi_{k+1}$, so that the corresponding entry in the {\em OutMsgQueue\/} of $S'_k$ can be easily located. +\item{[Confirmation]} --- This entry in the {\em InMsgDescr\/} of $S_{k+1}$ also serves as a confirmation for $S'_k$. In a later block of $S'_k$, message $m$ must be removed from the {\em OutMsgQueue\/} of $S'_k$; this modification is reflected in a special entry in the {\em OutMsgDescr\/} of the block of $S'_k$ that performs this state modification. +\item{[Forwarding?]} --- If the final destination $\eta$ of~$m$ does not reside in $S_{k+1}$, the message is {\em forwarded}. Hypercube Routing is applied until some $\xi_l$, $l>k$, and $\xi_{l+1}=\NextHop(\xi_l,\eta)$ are obtained, such that $\xi_l$ lies in $S_{k+1}$, but $\xi_{l+1}$ does not (cf.~\ptref{sp:hr.int.route}). After that, a newly-enveloped copy of $m$ with transit address set to $\xi_l$ and next-hop address $\xi_{l+1}$ is included into both the {\em OutMsgDescr\/} of the current block of $S_{k+1}$ and the {\em OutMsgQueue\/} of the new state of $S_{k+1}$. The entry of $m$ in {\em InMsgDescr\/} contains a flag indicating that the message has been forwarded; the entry in {\em OutMsgDescr\/} contains the newly-enveloped message and a flag indicating that this is a forwarded message. Then all the steps starting from [OutboundQueueing] are repeated, for $l$ instead of~$k$. +\item{[Processing?]} --- If the final destination $\eta$ of $m$ resides in $S_{k+1}$, then the block of $S_{k+1}$ that imported the message must process it by a transaction $t$ included in the same block. In this case, {\em InMsgDescr\/} contains a reference to $t$ by its logical time $\LT(t)$, and a flag indicating that the message has been processed. +\end{itemize} + +The above message routing algorithm does not take into account some further modifications required to implement Instant Hypercube Routing (IHR). For instance, a message may be {\em discarded\/} after being imported (listed in {\em InMsgDescr}) into its final or intermediate shardchain block, because a proof of delivery via IHR to the final destination is presented. In this case, such a proof must be included into {\em InMsgDescr\/} to explain why the message was not forwarded further or processed. + +\mysubsection{Instant Hypercube Routing and combined delivery guarantees}\label{p:ihr.combine.deliv} +This section describes the Instant Hypercube Routing protocol, normally applied by TON Blockchain in parallel to the previously discussed Hypercube Routing protocol to achieve faster message delivery. However, when both Hypercube Routing and Instant Hypercube Routing are applied to the same message in parallel, achieving delivery and unique delivery guarantees is more complicated. This topic is also discussed in this section. + +\nxsubpoint\label{sp:ihr.overview}\emb{An overview of Instant Hypercube Routing} +Let us explain the major steps applied when the Instant Hypercube Routing (IHR) mechanism is applied to a message. (Notice that normally both the usual HR and IHR work in parallel for the same message; some provisions must be taken to guarantee the uniqueness of delivery of any message.) + +Consider the routing and delivery of the same message $m$ with source $\xi$ and destination $\eta$ as discussed in~\ptref{sp:hr.overview}: +\begin{itemize} +\item{[NetworkSend]} --- After the validators of $S_0$ have agreed on and signed the block containing the creating transaction $t$ for $m$, and observed that the destination $\eta$ of $m$ does not reside inside $S_0$, they may send a datagram (encrypted network message), containing the message $m$ along with a Merkle proof of its inclusion into the {\em OutMsgDescr\/} of the block just generated, to the validator group of the shardchain $T$ currently owning the destination~$\eta$. +\item{[NetworkReceive]} --- If the validators of shardchain $T$ receive such a message, they check its validity starting from the most recent masterchain block and the shardchain block hashes listed in it, including the most recent ``canonical'' block of shardchain $S_0$ as well. If the message is invalid, they silently discard it. If that block of shardchain $S_0$ has a larger sequence number than the one listed in the most recent masterchain block, they may either discard it or postpone the verification until the next masterchain block appears. +\item{[InclusionConditions]} --- The validators check inclusion conditions for message $m$. In particular, they must check that this message has not been delivered before, and that the {\em OutMsgQueue\/}s of the neighbors do not have unprocessed outbound messages with destinations in $T$ with smaller logical creation times than $\LT(m)$. +\item{[Deliver]} --- The validators deliver and process the message, by including it into the {\em InMsgDescr\/} of the current shardchain block along with a bit indicating that it is an IHR message, the Merkle proof of its inclusion into the {\em OutMsgDescr\/} of the original block, and the logical time of the transaction $t'$ processing this inbound message into the currently generated block. +\item{[Confirm]} --- Finally, the validators send encrypted datagrams to all the validator groups of the intermediate shardchains on the path from $\xi$ to $\eta$, containing a Merkle proof of the inclusion of message $m$ into the {\em InMsgDescr\/} of its final destination. The validators of an intermediate shardchain may use this proof to {\em discard\/} the copy of message $m$ travelling by the rules of HR, by importing the message into their {\em InMsgDescr\/} along with the Merkle proof of final delivery and setting a flag indicating that the message has been discarded. +\end{itemize} + +The overall procedure is even simpler than that for Hypercube Routing. Notice, however, that IHR comes with no delivery or FIFO guarantees: the network datagram may be lost in transit, or the validators of the destination shardchain may decide not to act on it, or they may discard it due to buffer overflow. This is the reason why IHR is used as a complement to HR, and not as a replacement. + +\nxsubpoint\emb{Overall eventual delivery guarantees} +Notice that the combination of HR and IHR guarantees the ultimate delivery of any internal message to its final destination. Indeed, the HR by itself is guaranteed to deliver any message eventually, and the HR for message $m$ can be cancelled at an intermediate stage only by a Merkle proof of delivery of $m$ to its final destination (via IHR). + +\nxsubpoint\emb{Overall unique delivery guarantees} +However, the {\em uniqueness\/} of message delivery for the combination of HR and IHR is more difficult to achieve. In particular, one must check the following conditions, and, if necessary, be able to provide short Merkle proofs that they do or don't hold: +\begin{itemize} +\item When a message $m$ is imported into its next intermediate shardchain block via HR, we must check that $m$ has not already been imported via HR. +\item When $m$ is imported and processed in its final destination shardchain, we must check that $m$ has not already been processed. If it has, there are three subcases: + \begin{itemize} + \item If $m$ is being considered for import via HR, and it has already been imported via HR, it must not be imported at all. + \item If $m$ is being considered for import via HR, and it has already been imported via IHR (but not HR), then it must be imported and immediately discarded (without being processed by a transaction). This is necessary to remove $m$ from the {\em OutMsgQueue\/} of its previous intermediate shardchain. + \item If $m$ is being considered for import via IHR, and it has already been imported via either IHR or HR, it must not be imported at all. + \end{itemize} +\end{itemize} + +\nxsubpoint\label{sp:msg.deliver.chk}\emb{Checking whether a message has already been delivered to its final destination} +Consider the following general algorithm for checking whether a message~$m$ has already been delivered to its final destination~$\eta$: One can simply scan the last several blocks belonging to the shardchain containing the destination address, starting from the latest block and working backwards through the previous block references. (If there are two previous blocks---i.e., if a shardchain merge event occurred at some point---one would follow the chain containing the destination address.) The {\em InMsgDescr\/} of each of these blocks can be checked for an entry with key $\Hash(m)$. If such an entry is found, the message $m$ has already been delivered, and we can easily construct a Merkle proof of this fact. If we do not find such an entry before arriving at a block $B$ with $\LT^+(B)<\LT(m)$, implying that $m$ could not be delivered in~$B$ or any of its predecessors, then the message $m$ definitely has not been delivered yet. + +The obvious disadvantage of this algorithm is that, if message $m$ is very old (and most likely delivered a long time ago), meaning that it has a small value of $\LT(m)$, then a large number of blocks will need to be scanned before yielding an answer. Furthermore, if the answer is negative, the size of the Merkle proof of this fact will increase linearly with the number of blocks scanned. + +\nxsubpoint\label{sp:ihr.deliver.chk}\emb{Checking whether an IHR message has already been delivered to its final destination} +To check whether an IHR message $m$ has already been delivered to its destination shardchain, we can apply the general algorithm described above (cf.~\ptref{sp:msg.deliver.chk}), modified to inspect only the last $c$ blocks for some small constant $c$ (say, $c=8$). If no conclusion can be reached after inspecting these blocks, then the validators for the destination shardchain may simply discard the IHR message instead of spending more resources on this check. + +\nxsubpoint\emb{Checking whether an HR message has already been delivered via HR to its final destination or an intermediate shardchain} +To check whether an HR-received message $m$ (or rather, a message $m$ being considered for import via HR) has already been imported via HR, we can use the following algorithm: Let $\xi_k$ be the transit address of $m$ (belonging to a neighboring shardchain $S_k$) and $\xi_{k+1}$ be its next-hop address (belonging to the shardchain under consideration). Since we are considering the inclusion of $m$, $m$ must be present in the {\em OutMsgQueue\/} of the most recent state of shardchain $S_k$, with $\xi_k$ and $\xi_{k+1}$ indicated in its envelope. In particular, (a) the message has been included into {\em OutMsgQueue}, and we may even know when, because the entry in {\em OutMsgQueue\/} sometimes contains the logical time of the block where it has been added, and (b) it has not yet been removed from {\em OutMsgQueue}. + +Now, the validators of the neighboring shardchain are required to remove a message from {\em OutMsgQueue\/} as soon as they observe that message (with transit and next-hop addresses $\xi_k$ and $\xi_{k+1}$ in its envelope) has been imported into the {\em InMsgDescr\/} of the message's next-hop shardchain. Therefore, (b) implies that the message could have been imported into the {\em InMsgDescr\/} of a preceding block only if this preceding block is very new (i.e., not yet known to the most recent neighboring shardchain block). Therefore, only a very limited number of preceding blocks (typically one or two, at most) need to be scanned by the algorithm described in~\ptref{sp:msg.deliver.chk} to conclude that the message has not yet been imported.\footnote{One must not only look up the key $\Hash(m)$ in the {\em InMsgDescr\/} of these blocks, but also check the intermediate addresses in the envelope of the corresponding entry, if found.} In fact, if this check is performed by the validators or collators for the current shardchain themselves, it can be optimized by keeping in memory the {\em InMsgDescr\/}s of the several latest blocks. + +\nxsubpoint\label{sp:hr.ihr.deliver.chk}\emb{Checking whether an HR message has already been delivered via IHR to its final destination} +Finally, to check whether an HR message has already been delivered to its final destination via IHR, one can use the general algorithm described in~\ptref{sp:msg.deliver.chk}. In contrast with \ptref{sp:ihr.deliver.chk}, we cannot abort the verification process after scanning a fixed number of the latest blocks in the destination shardchain, because HR messages cannot be dropped without a reason. + +Instead, we indirectly bound the number of blocks to be inspected by forbidding the inclusion of IHR message $m$ into a block $B$ of its destination shardchain if there are already more than, say, $c=8$ blocks $B'$ in the destination shardchain with $\LT^+(B')\geq\LT(m)$. + +Such a condition effectively restricts the time interval after the creation of message~$m$ in which it could have been delivered via IHR, so that only a small number of blocks of the destination shardchain (at most $c$) will need to be inspected. + +Notice that this condition nicely aligns with the modified algorithm described in~\ptref{sp:ihr.deliver.chk}, effectively forbidding the validators from importing the newly-received IHR message if more than $c=8$ steps are needed to check that it had not been imported already. + +\clearpage +\mysection{Messages, message descriptors, and queues}\label{sect:msg} +This chapter presents the internal layout of individual messages, message descriptors (such as {\em InMsgDescr\/} or {\em OutMsgDescr}), and message queues (such as {\em OutMsgQueue}). Enveloped messages (cf.~\ptref{sp:msg.env}) are also discussed here. + +Notice that most general conventions related to messages must be obeyed by all shardchains, even if they do not belong to the basic shardchain; otherwise, messaging and interaction between different workchains would not be possible. It is the {\em interpretation\/} of the message contents and the {\em processing\/} of messages, usually by some transactions, that differs between workchains. + +\mysubsection{Address, currency, and message layout} +This chapter begins with some general definitions, followed by the precise layout of addresses used for serializing source and destination addresses in a message. + +\nxsubpoint\label{sp:tl.std.def}\emb{Some standard definitions} +For the reader's convenience, we reproduce here several general TL-B definitions.\footnote{A description of an older version of TL may be found at \url{https://core.telegram.org/mtproto/TL}. Alternatively, an informal introduction to TL-B schemes may be found in \cite[3.3.4]{TVM}.} These definitions are used below in the discussion of address and message layout, but otherwise are not related to the TON Blockchain. +\begin{verbatim} +unit$_ = Unit; +true$_ = True; +// EMPTY False; +bool_false$0 = Bool; +bool_true$1 = Bool; +nothing$0 {X:Type} = Maybe X; +just$1 {X:Type} value:X = Maybe X; +left$0 {X:Type} {Y:Type} value:X = Either X Y; +right$1 {X:Type} {Y:Type} value:Y = Either X Y; +pair$_ {X:Type} {Y:Type} first:X second:Y = Both X Y; + +bit$_ _:(## 1) = Bit; +\end{verbatim} + +\nxsubpoint\label{sp:addr.tl}\emb{TL-B scheme for addresses} +The serialization of source and destination addresses is defined by the following TL-B scheme: +\begin{verbatim} +addr_none$00 = MsgAddressExt; +addr_extern$01 len:(## 8) external_address:(len * Bit) + = MsgAddressExt; +anycast_info$_ depth:(## 5) rewrite_pfx:(depth * Bit) = Anycast; +addr_std$10 anycast:(Maybe Anycast) + workchain_id:int8 address:uint256 = MsgAddressInt; +addr_var$11 anycast:(Maybe Anycast) addr_len:(## 9) + workchain_id:int32 address:(addr_len * Bit) = MsgAddressInt; +_ MsgAddressInt = MsgAddress; +_ MsgAddressExt = MsgAddress; +\end{verbatim} +The two last lines define type \texttt{MsgAddress} to be the internal union of types \texttt{MsgAddressInt} and \texttt{MsgAddressExt} (not to be confused with their external union \texttt{Either MsgAddressInt MsgAddressExt} as defined in~\ptref{sp:tl.std.def}), as if the preceding four lines had been repeated with the right-hand side replaced by \texttt{MsgAddress}. In this way, type \texttt{MsgAddress} has four constructors, and types \texttt{MsgAddressInt} and \texttt{MsgAddressExt} are both subtypes of \texttt{MsgAddress}. + +\nxsubpoint\emb{External addresses} +The first two constructors, \texttt{addr\_none} and \texttt{addr\_extern}, are used for source addresses of ``messages from nowhere'' (inbound external messages), and for destination addresses of ``messages to nowhere'' (outbound external messages). The \texttt{addr\_extern} constructor defines an ``external address'', which is ignored by the TON Blockchain software altogether (which treats \texttt{addr\_extern} as a longer variant of \texttt{addr\_none}), but may be used by external software for its own purposes. For example, a special external service may inspect the destination address of all outbound external messages found in all blocks of the TON Blockchain, and, if a special magic number is present in the \texttt{external\_address} field, parse the remainder as an IP address and UDP port or a (TON Network) ADNL address, and send a datagram with a copy of the message to the network address thus obtained. + +\nxsubpoint\emb{Internal addresses} +The two remaining constructors, \texttt{addr\_std} and \texttt{addr\_var}, represent internal addresses. The first of them, \texttt{addr\_std}, represents a signed 8-bit $\workchainid$ (sufficient for the masterchain and for the basic workchain) and a 256-bit internal address in the selected workchain. The second of them, \texttt{addr\_var}, represents addresses in workchains with a ``large'' $\workchainid$, or internal addresses of length not equal to 256. Both of these constructors have an optional \texttt{anycast} value, absent by default, which enables ``address rewriting'' when present.\footnote{{\em Address rewriting\/} is a feature used to implement ``anycast addresses'' employed by the so-called {\em large\/} or {\em global\/} smart contracts (cf.~\cite[2.3.18]{TON}), which can have instances in several shardchains. When address rewriting is enabled, a message may be routed to and processed by a smart contract with an address coinciding with the destination address up to the first $d$ bits, where $d\leq 32$ is the ``splitting depth'' of the smart contract indicated in the {\tt anycast.depth} field (cf.~\ptref{sp:nh.anycast}). Otherwise, the addresses must match exactly.} + +The validators must use \texttt{addr\_std} instead of \texttt{addr\_var} whenever possible, but must be ready to accept \texttt{addr\_var} in inbound messages. The \texttt{addr\_var} constructor is intended for future extensions. + +Notice that $\workchainid$ must be a valid workchain identifier enabled in the current masterchain configuration, and the length of the internal address must be in the range allowed for the indicated workchain. For example, one cannot use $\workchainid=0$ (basic workchain) or $\workchainid=-1$ (masterchain) with addresses that are not exactly 256 bits long. + +\nxsubpoint\emb{Representing Gram currency amounts} +Amounts of Grams are expressed with the aid of two types representing variable-length unsigned or signed integers, plus a type \texttt{Grams} explicitly dedicated to representing non-negative amounts of nanograms, as follows: +\begin{verbatim} +var_uint$_ {n:#} len:(#< n) value:(uint (len * 8)) + = VarUInteger n; +var_int$_ {n:#} len:(#< n) value:(int (len * 8)) + = VarInteger n; +nanograms$_ amount:(VarUInteger 16) = Grams; +\end{verbatim} +If one wants to represent $x$ nanograms, one selects an integer $\ell<16$ such that $x<2^{8\ell}$, and serializes first $\ell$ as an unsigned 4-bit integer, then $x$ itself as an unsigned $8\ell$-bit integer. Notice that four zero bits represent a zero amount of Grams. + +Recall (cf.~\cite[A]{TON}) that the original total supply of Grams is fixed at five billion (i.e., $5\cdot10^{18}<2^{63}$ nanograms), and is expected to grow very slowly. Therefore, all the amounts of Grams encountered in practice will fit in unsigned or even signed 64-bit integers. The validators may use the 64-bit integer representation of Grams in their internal computations; however, the serialization of these values the blockchain is another matter. + +\nxsubpoint\emb{Representing collections of arbitrary currencies} +Recall that the TON Blockchain allows its users to define arbitrary cryptocurrencies or tokens apart from the Gram, provided some conditions are met. Such additional cryptocurrencies are identified by 32-bit $\currencyid$s. The list of defined additional cryptocurrencies is a part of the blockchain configuration, stored in the masterchain. + +When some amounts of one or several such cryptocurrencies need to be represented, a dictionary (cf.~\cite[3.3]{TVM}) with 32-bit $\currencyid$s as keys and \texttt{VarUInteger 32} values is used: +\begin{verbatim} +extra_currencies$_ dict:(HashmapE 32 (VarUInteger 32)) + = ExtraCurrencyCollection; +currencies$_ grams:Grams other:ExtraCurrencyCollection + = CurrencyCollection; +\end{verbatim} +The value attached to an internal message is represented by a value of the \texttt{CurrencyCollection} type, which may describe a certain (non-negative) amount of (nano)grams as well as some additional currencies, if needed. Notice that if no additional currencies are required, \texttt{other} reduces to just one zero bit. + +\nxsubpoint\label{sp:msg.layout}\emb{Message layout} +A message consists of its {\em header\/} followed by its {\em body}, or {\em payload}. The body is essentially arbitrary, to be interpreted by the destination smart contract. The message header is standard and is organized as follows: +\begin{verbatim} +int_msg_info$0 ihr_disabled:Bool bounce:Bool + src:MsgAddressInt dest:MsgAddressInt + value:CurrencyCollection ihr_fee:Grams fwd_fee:Grams + created_lt:uint64 created_at:uint32 = CommonMsgInfo; +ext_in_msg_info$10 src:MsgAddressExt dest:MsgAddressInt + import_fee:Grams = CommonMsgInfo; +ext_out_msg_info$11 src:MsgAddressInt dest:MsgAddressExt + created_lt:uint64 created_at:uint32 = CommonMsgInfo; + +tick_tock$_ tick:Bool tock:Bool = TickTock; + +_ split_depth:(Maybe (## 5)) special:(Maybe TickTock) + code:(Maybe ^Cell) data:(Maybe ^Cell) + library:(Maybe ^Cell) = StateInit; + +message$_ {X:Type} info:CommonMsgInfo + init:(Maybe (Either StateInit ^StateInit)) + body:(Either X ^X) = Message X; +\end{verbatim} +The meaning of this scheme is as follows. + +Type \texttt{Message $X$} describes a message with the body (or payload) of type $X$. Its serialization starts with \texttt{info} of type \texttt{CommonMsgInfo}, which comes in three flavors: for internal messages, inbound external messages, and outbound external messages, respectively. All of them have a source address \texttt{src} and destination address \texttt{dest}, which are external or internal according to the chosen constructor. Apart from that, an internal message may bear some \texttt{value} in Grams and other defined currencies, and all messages generated inside the TON Blockchain have a logical creation time \texttt{created\_lt} (cf.~\ptref{sp:lt.ton.blkch}) and creation unixtime \texttt{created\_at}, both automatically set by the generating transaction. The creation unixtime equals the creation unixtime of the block containing the generating transaction. + +\nxsubpoint\emb{Forwarding and IHR fees. Total value of an internal message} +Internal messages define an \texttt{ihr\_fee} in Grams, which is subtracted from the value attached to the message and awarded to the validators of the destination shardchain if they include the message by the IHR mechanism. The \texttt{fwd\_fee} is the original total forwarding fee paid for using the HR mechanism; it is automatically computed from some configuration parameters and the size of the message at the time the message is generated. + +Notice that the total value carried by a newly-created internal outbound message equals the sum of \texttt{value}, \texttt{ihr\_fee}, and \texttt{fwd\_fee}. This sum is deducted from the balance of the source account. Of these components, only \texttt{value} is always credited to the destination account on message delivery. The \texttt{fwd\_fee} is collected by the validators on the HR path from the source to the destination, and the \texttt{ihr\_fee} is either collected by the validators of the destination shardchain (if the message is delivered via IHR), or credited to the destination account. + +\nxsubpoint\emb{Code and data portions contained in a message} +Apart from the common message information stored in \texttt{info}, a message can contain portions of the destination smart contract's code and data. This feature is used, for instance, in the so-called {\em constructor messages\/} (cf.~\ptref{sp:constr.msg}), which are simply internal or inbound external messages with \texttt{code} and possibly \texttt{data} fields defined in their \texttt{init} portions. If the hash of these fields is correct, and the destination smart contract has no code or data, the values from the message are used instead.\footnote{More precisely, the information from the \texttt{init} field of an inbound message is used either when the receiving account is uninitialized or frozen with the hash of {\em StateInit} equal to the one expected by the account, or when the receiving account is active, and its code or data is an external hash reference matching the hash of the code or data received in the {\em StateInit} of the message.} + +\nxsubpoint\emb{Using \texttt{code} and \texttt{data} for other purposes} +Workchains other than the masterchain and the basic workchain are free to use the trees of cells referred to in the \texttt{code}, \texttt{data}, and \texttt{library} fields for their own purposes. The messaging system itself makes no assumptions about their contents; they become relevant only when a message is processed by a transaction. + +\nxsubpoint\emb{Absence of an explicit gas price and gas limit} +Notice that messages do not have an explicit gas price and gas limit. Instead, the gas price is set globally by the validators for each workchain (it is a special configurable parameter), and the gas limit for each transaction has also a default value, which is a configurable parameter; the smart contract itself may lower the gas limit during its execution if so desired. + +For internal messages, the initial gas limit cannot exceed the Gram value of the message divided by the current gas price. For inbound external messages, the initial gas limit is very small, and the true gas limit is set by the receiving smart contract itself, when it {\em accepts\/} the inbound message by the corresponding TVM primitive. + +\nxsubpoint\emb{Deserialization of a message payload} +The payload, or body, of a message is deserialized by the receiving smart contract when executed by TVM. The messaging system itself makes no assumptions about the internal format of the payload. However, it makes sense to describe the serialization of supported inbound messages by TL or TL-B schemes with 32-bit constructor tags, so that the developers of other smart contracts will know the interface supported by a specific smart contract. + +A message is always serialized inside the blockchain as the last field in a cell. Therefore, the blockchain software may assume that whatever bits and references left unparsed after parsing the fields of a \texttt{Message} preceding \texttt{body} belong to the payload $\texttt{body}:X$, without knowing anything about the serialization of the type~$X$. + +\nxsubpoint\emb{Messages with empty payloads} +The payload of a message may happen to be an empty cell slice, having no data bits and no references. By convention, such messages are used for simple value transfers. The receiving smart contract is normally expected to process such messages quietly and to terminate successfully (with a zero exit code), although some smart contracts may perform non-trivial actions even when receiving a message with empty payload. For example, a smart contract may check the resulting balance, and, if it becomes sufficient for a previously postponed action, trigger this action. Alternatively, the smart contract might want to remember in its persistent storage the amount received and the corresponding sender, in order, for instance, to distribute some tokens later to each sender proportionally to the funds transferred. + +Notice that even if a smart contract makes no special provisions for messages with empty payloads and throws an exception while processing such messages, the received value (minus the gas payment) will still be added to the balance of the smart contract. + +\nxsubpoint\emb{Message source address and logical creation time determine its generating block} +Notice that {\em the source address and the logical creation time of an internal or an outbound external message uniquely determine the block in which the message has been generated}. Indeed, the source address determines the source shardchain, and the blocks of this shardchain are assigned non-intersecting logical time intervals, so only one of them may contain the indicated logical creation time. This is the reason why no explicit mention of the generating block is needed in messages. + +\nxsubpoint\label{sp:tl.msg.env}\emb{Enveloped messages} +{\em Message envelopes\/} are used for attaching routing information, such as the current (transit) address and the next-hop address, to inbound, transit, and outbound messages (cf.~\ptref{sp:msg.env}). The message itself is kept in a separate cell and referred to from the message envelope by a cell reference. +\begin{verbatim} +interm_addr_regular$0 use_src_bits:(#<= 96) + = IntermediateAddress; +interm_addr_simple$10 workchain_id:int8 addr_pfx:(64 * Bit) + = IntermediateAddress; +interm_addr_ext$11 workchain_id:int32 addr_pfx:(64 * Bit) + = IntermediateAddress; +msg_envelope cur_addr:IntermediateAddress + next_addr:IntermediateAddress fwd_fee_remaining:Grams + msg:^(Message Any) = MsgEnvelope; +\end{verbatim} +The \texttt{IntermediateAddress} type is used to describe the intermediate addresses of a message---that is, its current (or transit) address \texttt{cur\_addr}, and its next-hop address~\texttt{next\_addr}. The first constructor \texttt{interm\_addr\_regular} represents the intermediate address using the optimization described in~\ptref{sp:repr.interm.addr}, by storing the number of the first bits of the intermediate address that are the same as in the source address; the two other explicitly store the workchain identifier and the first 64 bits of the address inside that workchain (the remaining bits can be taken from the source address). The \texttt{fwd\_fee\_remaining} field is used to explicitly represent the maximum amount of message forwarding fees that can be deducted from the message value during the remaining HR steps; it cannot exceed the value of \texttt{fwd\_fee} indicated in the message itself. + +\mysubsection{Inbound message descriptors} +This section discusses {\em InMsgDescr}, the structure containing a description of all inbound messages imported into a block.\footnote{Strictly speaking, {\em InMsgDescr\/} is the {\em type\/} of this structure; we deliberately use the same notation to describe the only instance of this type in a block.} + +\nxsubpoint\label{sp:inb.msg.classes}\emb{Types and sources of inbound messages} +Each inbound message mentioned in {\em InMsgDescr\/} is described by a value of type {\em InMsg\/} (an ``inbound message descriptor''), which specifies the source of the message, the reason for its being imported into this block, and some information about its ``fate''---its processing by a transaction or forwarding inside the block. + +Inbound messages may be classified as follows: +\begin{itemize} +\item {\em Inbound external messages} --- Need no additional reason for being imported into the block, but must be immediately processed by a transaction in the same block. +\item {\em Internal IHR messages with destination addresses in this block} --- The reason for their being imported into the block includes a Merkle proof of their generation (i.e., their inclusion in {\em OutMsgDescr\/} of their original block). Such a message must be immediately delivered to its final destination and processed by a transaction. +\item {\em Internal messages with destinations in this block} --- The reason for their inclusion is their presence in {\em OutMsgQueue\/} of the most recent state of a neighboring shardchain,\footnote{Recall that a shardchain is considered a neighbor of itself.} or their presence in {\em OutMsgDescr} of this very block. This neighboring shardchain is completely determined by the transit address indicated in the forwarded message envelope, which is replicated in {\em InMsg\/} as well. The ``fate'' of this message is again described by a reference to the processing transaction inside the current block. +\item {\em Immediately routed internal messages} --- Essentially a subclass of the previous class of messages. In this case, the imported message is one of the outbound messages generated in this very block. +\item {\em Transit internal messages} --- Have the same reason for inclusion as the previous class of messages. However, they are not processed inside the block, but internally forwarded into {\em OutMsgDescr\/} and {\em OutMsgQueue}. This fact, along with a reference to the new envelope of the transit message, must be registered in {\em InMsg}. +\item {\em Discarded internal messages with destinations in this block} --- An internal message with a destination in this block may be imported and immediately discarded instead of being processed by a transaction if it has already been received and processed via IHR in a preceding block of this shardchain. In this case, a reference to the previous processing transaction must be provided. +\item {\em Discarded transit internal messages} --- Similarly, a transit message may be discarded immediately after import if it has already been delivered via IHR to its final destination. In this case, a Merkle proof of its processing in the final block (as an IHR message) is required. +\end{itemize} + +\nxsubpoint\label{sp:in.msg.d}\emb{Descriptor of an inbound message} +Each inbound message is described by an instance of the \texttt{InMsg} type, which has six constructors corresponding to the cases listed above in~\ptref{sp:inb.msg.classes}: +\begin{verbatim} +msg_import_ext$000 msg:^(Message Any) transaction:^Transaction + = InMsg; +msg_import_ihr$010 msg:^(Message Any) transaction:^Transaction + ihr_fee:Grams proof_created:^Cell = InMsg; +msg_import_imm$011 in_msg:^MsgEnvelope + transaction:^Transaction fwd_fee:Grams = InMsg; +msg_import_fin$100 in_msg:^MsgEnvelope + transaction:^Transaction fwd_fee:Grams = InMsg; +msg_import_tr$101 in_msg:^MsgEnvelope out_msg:^MsgEnvelope + transit_fee:Grams = InMsg; +msg_discard_fin$110 in_msg:^MsgEnvelope transaction_id:uint64 + fwd_fee:Grams = InMsg; +msg_discard_tr$111 in_msg:^MsgEnvelope transaction_id:uint64 + fwd_fee:Grams proof_delivered:^Cell = InMsg; +\end{verbatim} +Notice that the processing transaction is referred to in the first four constructors directly by a cell reference to \texttt{Transaction}, even though the logical time of the transaction \texttt{transaction\_lt:uint64} would suffice for this purpose. Internal consistency conditions ensure that the transaction referred to does belong to the destination smart contract indicated in the message, and that the inbound message processed by that transaction is indeed the one being described in this {\em InMsg\/} instance. + +Furthermore, notice that \texttt{msg\_import\_imm} could be distinguished from \texttt{msg\_import\_fin} by observing that it is the only case when the logical creation time of the message being processed is greater than or equal to the (minimal) logical time of the block importing the message. + +\nxsubpoint\label{sp:in.msg.fees}\emb{Collecting forwarding and transit fees from imported messages} +The {\em InMsg\/} structure is also used to indicate the forwarding and transit fees collected from inbound messages. The fee itself is indicated in \texttt{ihr\_fee}, \texttt{fwd\_fee}, or \texttt{transit\_fee} fields; it is absent only in inbound external messages, which use other mechanisms to reward the validators for importing them. The fees must satisfy the following internal consistency conditions: +\begin{itemize} +\item For external messages (\texttt{msg\_import\_ext}), there is no forwarding fee. +\item For IHR-imported internal messages (\texttt{msg\_import\_ihr}), the fee equals \texttt{ihr\_fee}, which must coincide with the \texttt{ihr\_fee} value indicated in the message itself. Notice that \texttt{fwd\_fee} or \texttt{fwd\_fee\_remaining} are never collected from IHR-imported messages. +\item For internal messages delivered to their destination (\texttt{msg\_import\_fin} and \texttt{msg\_import\_imm}), the fee equals the \texttt{fwd\_fee\_remaining} of the enveloped inbound message \texttt{in\_msg}. Note that it cannot exceed the \texttt{fwd\_fee} value indicated in the message itself. +\item For transit messages (\texttt{msg\_import\_tr}), the fee equals the difference between the \texttt{fwd\_fee\_remaining} values indicated in the \texttt{in\_msg} and \texttt{out\_msg} envelopes. +\item For discarded messages, the fee also equals the \texttt{fwd\_fee\_remaining} indicated in \texttt{in\_msg}. +\end{itemize} + +\nxsubpoint\label{sp:in.msg.val}\emb{Imported value of an inbound message} +Each imported message imports some value---a certain amount of one or more cryptocurrencies---into the block. This imported value is computed as follows: +\begin{itemize} +\item An external message imports no value. +\item An IHR-imported message imports its \texttt{value} plus its \texttt{ihr\_fee}. +\item A delivered or transit internal message imports its \texttt{value} plus its \texttt{ihr\_fee} plus the value of \texttt{fwd\_fee\_remaining} of its \texttt{in\_msg} envelope. +\item A discarded message imports the \texttt{fwd\_fee\_remaining} of its \texttt{in\_msg} envelope. +\end{itemize} +Notice that the forwarding and transit fees collected from an imported message do not exceed its imported value. + +\nxsubpoint\label{sp:aug.hm}\emb{Augmented hashmaps, or dictionaries} +Before continuing, let us discuss the serialization of {\em augmented\/} hashmaps, or dictionaries. + +Augmented hashmaps are key-value storage structures with $n$-bit keys and values of some type~$X$, similar to the ordinary hashmaps described in \cite[3.3]{TVM}. However, each intermediate node of the Patricia tree representing an augmented hashmap is {\em augmented\/} by a value of type $Y$. + +These augmentation values must satisfy certain {\em aggregation conditions}. Typically, $Y$ is an integer type, and the aggregation condition is that the augmentation value of a fork must equal the sum of the augmentation values of its two children. In general, a {\em fork evaluation function} $S:Y\times Y\to Y$ or $S:Y\to Y\to Y$ is used instead of the sum. The augmentation value of a leaf is usually computed from the value stored in that leaf by means of a {\em leaf evaluation function\/} $L:X\to Y$. The augmentation value of a leaf may be stored explicitly in the leaf along with the value; however, in most cases there is no need for this, because the leaf evaluation function $L$ is very simple. + +\nxsubpoint\label{sp:aug.hm.ser}\emb{Serialization of augmented hashmaps} +The serialization of augmented hashmaps with $n$-bit keys, values of type $X$, and augmentation values of type $Y$ is given by the following TL-B scheme, which is an extension of the one provided in~\cite[3.3.3]{TVM}: +\begin{verbatim} +ahm_edge#_ {n:#} {X:Type} {Y:Type} {l:#} {m:#} + label:(HmLabel ~l n) {n = (~m) + l} + node:(HashmapAugNode m X Y) = HashmapAug n X Y; +ahmn_leaf#_ {X:Type} {Y:Type} extra:Y value:X + = HashmapAugNode 0 X Y; +ahmn_fork#_ {n:#} {X:Type} {Y:Type} + left:^(HashmapAug n X Y) right:^(HashmapAug n X Y) extra:Y + = HashmapAugNode (n + 1) X Y; + +ahme_empty$0 {n:#} {X:Type} {Y:Type} extra:Y + = HashmapAugE n X Y; +ahme_root$1 {n:#} {X:Type} {Y:Type} root:^(HashmapAug n X Y) + extra:Y = HashmapAugE n X Y; +\end{verbatim} + +\nxsubpoint\label{sp:in.msg.augm}\emb{Augmentation of {\em InMsgDescr}} +The collection of inbound message descriptors is augmented by a vector of two currency values, representing the imported value and the forwarding and transit fees collected from a message or a collection of messages: +\begin{verbatim} +import_fees$_ fees_collected:Grams + value_imported:CurrencyCollection = ImportFees; +\end{verbatim} + +\nxsubpoint\label{sp:in.msg.descr}\emb{Structure of {\em InMsgDescr}} +Now the {\em InMsgDescr\/} itself is defined as an augmented hashmap, with 256-bit keys (equal to the representation hashes of imported messages), values of type \texttt{InMsg} (cf.~\ptref{sp:in.msg.d}), and augmentation values of type \texttt{ImportFees} (cf.~\ptref{sp:in.msg.augm}): +\begin{verbatim} +_ (HashmapAugE 256 InMsg ImportFees) = InMsgDescr; +\end{verbatim} +This TL-B notation uses an anonymous constructor \texttt{\_} to define \texttt{InMsgDescr} as a synonym for another type. + +\nxsubpoint\emb{Aggregation rules for {\em InMsgDescr}} +The fork evaluation and leaf evaluation functions (cf.~\ptref{sp:aug.hm}) are not included explicitly in the above notation, because the dependent types of TL-B are not expressive enough for this purpose. In words, the fork evaluation function is just the componentwise addition of two \texttt{ImportFees} instances, and the leaf evaluation function is defined by the rules listed in \ptref{sp:in.msg.fees} and \ptref{sp:in.msg.val}. In this way, the root of the Patricia tree representing an instance of {\em InMsgDescr\/} contains an {\em ImportFees\/} instance with the total value imported by all inbound messages, and with the total forwarding fees collected from them. + +\mysubsection{Outbound message queue and descriptors} +This section discusses {\em OutMsgDescr}, the structure representing all outbound messages of a block, along with their envelopes and brief descriptions of the reasons for including them into {\em OutMsgDescr}. This structure also describes all modifications of {\em OutMsgQueue}, which is a part of the shardchain state. + +\nxsubpoint\emb{Types of outbound messages} +Outbound messages may be classified as follows: +\begin{itemize} +\item {\em External outbound messages}, or ``messages to nowhere'' --- Generated by a transaction inside this block. The reason for including such a message into {\em OutMsgDescr\/} is simply a reference to its generating transaction. +\item {\em Immediately processed internal outbound messages} --- Generated and processed in this very block, and not included into {\em OutMsgQueue}. The reason for including such a message is a reference to its generating transaction, and its ``fate'' is described by a reference to the corresponding entry in {\em InMsgDescr}. +\item {\em Ordinary (internal) outbound messages} --- Generated in this block and included into {\em OutMsgQueue}. +\item {\em Transit (internal) outbound messages} --- Imported into the {\em InMsgDescr} of the same block and routed via HR until a next-hop address outside the current shard is obtained. +\end{itemize} + +\nxsubpoint\label{sp:msg.deque.recs}\emb{Message dequeueing records} +Apart from the above types of outbound messages, {\em OutMsgDescr\/} can contain special ``message dequeueing records'', which indicate that a message has been removed from the {\em OutMsgQueue\/} in this block. The reason for this removal is indicated in the message deletion record; it consists of a reference to the enveloped message being deleted, and of the logical time of the neighboring shardchain block that has this enveloped message in its {\em InMsgDescr}. + +Notice that on some occasions a message may be imported from the {\em OutMsgQueue\/} of the current shardchain, internally routed, and then included into {\em OutMsgDescr\/} and {\em OutMsgQueue\/} again with a different envelope.\footnote{This situation is rare and occurs only after shardchain merge events. Normally the messages imported from the {\em OutMsgQueue} of the same shardchain have destinations inside this shardchain, and are processed accordingly instead of being re-queued.} In this case, a variant of the transit outbound message description is used, which doubles as a message dequeueing record. + +\nxsubpoint\emb{Descriptor of an outbound message} +Each outbound message is described by an instance of {\em OutMsg}: +\begin{verbatim} +msg_export_ext$000 msg:^(Message Any) + transaction:^Transaction = OutMsg; +msg_export_imm$010 out_msg:^MsgEnvelope + transaction:^Transaction reimport:^InMsg = OutMsg; +msg_export_new$001 out_msg:^MsgEnvelope + transaction:^Transaction = OutMsg; +msg_export_tr$011 out_msg:^MsgEnvelope + imported:^InMsg = OutMsg; +msg_export_deq$110 out_msg:^MsgEnvelope + import_block_lt:uint64 = OutMsg; +msg_export_tr_req$111 out_msg:^MsgEnvelope + imported:^InMsg = OutMsg; +\end{verbatim} +The last two descriptions have the effect of removing (dequeueing) the message from {\em OutMsgQueue\/} instead of inserting it. The last one re-inserts the message into {\em OutMsgQueue\/} with a new envelope after performing the internal routing (cf.~\ptref{sp:hr.int.route}). + +\nxsubpoint\label{sp:exp.msg.val}\emb{Exported value of an outbound message} +Each outbound message described by an {\em OutMsg\/} exports some value---a certain amount of one or more cryptocurrencies---from the block. This exported value is computed as follows: +\begin{itemize} +\item An external outbound message exports no value. +\item An internal message, generated in this block, exports its {\tt value} plus its {\tt ihr\_fee} plus its {\tt fwd\_fee}. Notice that {\tt fwd\_fee} must be equal to the {\tt fwd\_fee\_remaining} indicated in the {\tt out\_msg} envelope. +\item A transit message exports its {\tt value} plus its {\tt ihr\_fee} plus the value of {\tt fwd\_fee\_remaining} of its {\tt out\_msg} envelope. +\item The same holds for {\tt msg\_export\_tr\_req}, the constructor of {\em OutMsg\/} used for re-inserted dequeued messages. +\item A message dequeueing record ({\tt msg\_export\_deq}; cf.~\ptref{sp:msg.deque.recs}) exports no value. +\end{itemize} + +\nxsubpoint\label{sp:out.msg.descr}\emb{Structure of {\em OutMsgDescr}} +The {\em OutMsgDescr\/} itself is simply an augmented hashmap (cf.~\ptref{sp:aug.hm}), with 256-bit keys (equal to the representation hash of the message), values of type {\em OutMsg}, and augmentation values of type {\em CurrencyCollection}: +\begin{verbatim} +_ (HashmapAugE 256 OutMsg CurrencyCollection) = OutMsgDescr; +\end{verbatim} +The augmentation is the {\em exported value\/} of the corresponding message, aggregated by means of the sum, and computed at the leaves as explained in~\ptref{sp:exp.msg.val}. In this way, the total exported value appears near the root of the Patricia tree representing {\em OutMsgDescr}. + +The most important consistency condition for {\em OutMsgDescr\/} is that its entry with key $k$ must be an {\em OutMsg\/} describing a message~$m$ with representation hash $\Hash^\flat(m)=k$. + +\nxsubpoint\label{sp:out.msg.queue}\emb{Structure of {\em OutMsgQueue}} +Recall (cf.~\ptref{sp:outmsgq}) that {\em OutMsgQueue\/} is a part of the blockchain state, not of a block. Therefore, a block contains only hash references to its initial and final state, and its newly-created cells. + +The structure of {\em OutMsgQueue\/} is simple: it is just an augmented hashmap with 352-bit keys and values of type {\em OutMsg}: +\begin{verbatim} +_ (HashmapAugE 352 OutMsg uint64) = OutMsgQueue; +\end{verbatim} +The key used for an outbound message $m$ is the concatenation of its 32-bit next-hop $\workchainid$, the first 64 bits of the next-hop address inside that workchain, and the representation hash $\Hash^\flat(m)$ of the message~$m$ itself. The augmentation is by the logical creation time $\LT(m)$ of message~$m$ at the leaves, and by the minimum of the augmentation values of the children at the forks. + +The most important consistency condition for {\em OutMsgQueue\/} is that the value at key $k$ must indeed contain an enveloped message with the expected next-hop address and representation hash. + +\nxsubpoint\emb{Consistency conditions for {\em OutMsg}} +Several internal consistency conditions are imposed on {\em OutMsg\/} instances present in {\em OutMsgDescr}. They include the following: +\begin{itemize} +\item Each of the first three constructors of outbound message descriptions includes a reference to the generating transaction. This transaction must belong to the source account of the message, it must contain a reference to the specified message as one of its outbound messages, and it must be recoverable by looking it up by its \texttt{account\_id} and \texttt{transaction\_id}. +\item {\tt msg\_export\_tr} and {\tt msg\_export\_tr\_req} must refer to an {\em InMsg\/} instance describing the same message (in a different original envelope). +\item If one of the first four constructors is used, the message must be absent in the initial {\em OutMsgQueue\/} of the block; otherwise, it must be present. +\item If \texttt{msg\_export\_deq} is used, the message must be absent in the final {\em OutMsgQueue\/} of the block; otherwise, it must be present. +\item If a message is not mentioned in {\em OutMsgDescr}, it must be the same in the initial and final {\em OutMsgQueue\/}s of the block. +\end{itemize} + +\clearpage +\mysection{Accounts and transactions} +This chapter discusses the layout of {\em accounts\/} (or {\em smart contracts}) and their {\em state\/} in the TON Blockchain. It also considers {\em transactions}, which are the only way to modify the state of an account, and to process inbound messages and generate new outbound messages. + +\mysubsection{Accounts and their states} +Recall that a {\em smart contract\/} and an {\em account\/} are the same thing in the context of the TON Blockchain, and that these terms can be used interchangeably, at least as long as only small (or ``usual'') smart contracts are considered. A {\em large\/} smart contract may employ several accounts lying in different shardchains of the same workchain for load balancing purposes. + +An account is {\em identified\/} by its full address, and is {\em completely described\/} by its state. In other words, there is nothing else in an account apart from its address and state. + +\nxsubpoint\emb{Account addresses} +In general, an account is completely identified by its {\em full address}, consisting of a 32-bit $\workchainid$, and the (usually 256-bit) {\em internal address\/} or {\em account identifier\/} $\accountid$ inside the chosen workchain. In the basic workchain ($\workchainid=0$) and in the masterchain ($\workchainid=-1$) the internal address is always 256-bit. In these workchains,\footnote{For simplicity, we sometimes treat the masterchain as just another workchain with $\workchainid=-1$.} $\accountid$ cannot be chosen arbitrarily, but must be equal to the hash of the initial code and data of the smart contract; otherwise, it will be impossible to initialize the account with the intended code and data (cf.~\ptref{sp:constr.msg}), and to do anything with the accumulated funds in the account balance. + +\nxsubpoint\emb{Zero account} +By convention, the {\em zero account\/} or {\em account with zero address\/} accumulates the processing, forwarding, and transit fees, as well as any other payments collected by the validators of the masterchain or a workchain. Furthermore, the zero account is a ``large smart contract'', meaning that each shardchain has its instance of the zero account, with the most significant bits of the address adjusted to lie in the shard. Any funds transferred to the zero account, intentionally or by accident, are effectively a gift for the validators. For example, a smart contract might destroy itself by sending all its funds to the zero account. + +\nxsubpoint\emb{Small and large smart contracts} +By default, smart contracts are ``small'', meaning that they have one account address belonging to exactly one shardchain at any given moment of time. However, one can create a ``large smart contract of splitting depth $d\,$'', meaning that up to $2^d$ instances of the smart contract may be created, with the first $d$ bits of the original address of the smart contract replaced by arbitrary bit sequences.\footnote{In fact, up to the first $d$ bits are replaced in such a way that each shard contains at most one instance of the large smart contract, and that shards $(w,s)$ with prefix $s$ of length $|s|\leq d$ contain exactly one instance.} One can send messages to such smart contracts using internal anycast addresses with {\tt anycast} set to $d$ (cf.~\ptref{sp:addr.tl}). Furthermore, the instances of the large smart contract are allowed to use this anycast address as the source address of their generated messages. + +An instance of a large smart contract is an account with non-zero {\em maximal splitting depth $d$}. + +\nxsubpoint\emb{The three kinds of accounts} +There are three kinds of accounts: +\begin{itemize} +\item {\em Uninitialized} --- The account only has a balance; its code and data have not yet been initialized. +\item {\em Active} --- The account's code and data have been initialized as well. +\item {\em Frozen} --- The account's code and data have been replaced by a hash, but the balance is still stored explicitly. The balance of a frozen account may effectively become negative, reflecting due storage payments. +\end{itemize} + +\nxsubpoint\emb{Storage profile of an account} +The {\em storage profile\/} of an account is a data structure describing the amount of persistent blockchain state storage used by that account. It describes the total amount of cells, data bits, and internal and external cell references used. +\begin{verbatim} +storage_used$_ cells:(VarUInteger 7) bits:(VarUInteger 7) + ext_refs:(VarUInteger 7) int_refs:(VarUInteger 7) + public_cells:(VarUInteger 7) = StorageUsed; +\end{verbatim} +The same type {\tt StorageUsed} may represent the storage profile of a message, as required, for instance, to compute {\tt fwd\_fee}, the total forwarding fee for Hypercube Routing. The storage profile of an account has some additional fields indicating the last time when the storage fees were exacted: +\begin{verbatim} +storage_info$_ used:StorageUsed last_paid:uint32 + due_payment:(Maybe Grams) = StorageInfo; +\end{verbatim} +The {\tt last\_paid} field contains either the unixtime of the most recent storage payment collected (usually this is the unixtime of the most recent transaction), or the unixtime when the account was created (again, by a transaction). The {\tt due\_payment} field, if present, accumulates the storage payments that could not be exacted from the balance of the account, represented by a strictly positive amount of nanograms; it can be present only for uninitialized or frozen accounts that have a balance of zero Grams (but may have non-zero balances in other cryptocurrencies). When {\tt due\_payment} becomes larger than the value of a configurable parameter of the blockchain, the account is destroyed altogether, and its balance, if any, is transferred to the zero account. + +\nxsubpoint\label{sp:acc.descr}\emb{Account description} +The state of an account is represented by an instance of type {\em Account}, described by the following TL-B scheme:\footnote{This scheme uses anonymous constructors and anonymous fields, both represented by an underscore~{\tt \_}.} +\begin{verbatim} +account_none$0 = Account; +account$1 addr:MsgAddressInt storage_stat:StorageInfo + storage:AccountStorage = Account; + +account_storage$_ last_trans_lt:uint64 + balance:CurrencyCollection state:AccountState + = AccountStorage; + +account_uninit$00 = AccountState; +account_active$1 _:StateInit = AccountState; +account_frozen$01 state_hash:uint256 = AccountState; + +acc_state_uninit$00 = AccountStatus; +acc_state_frozen$01 = AccountStatus; +acc_state_active$10 = AccountStatus; +acc_state_nonexist$11 = AccountStatus; + +tick_tock$_ tick:Bool tock:Bool = TickTock; + +_ split_depth:(Maybe (## 5)) special:(Maybe TickTock) + code:(Maybe ^Cell) data:(Maybe ^Cell) + library:(Maybe ^Cell) = StateInit; +\end{verbatim} +Notice that {\tt account\_frozen} contains the representation hash of an instance of {\em StateInit}, instead of that instance itself, which would otherwise be contained in an {\tt account\_active}; {\tt account\_uninit} is similar to {\tt account\_frozen}, but it does not contain an explicit {\tt state\_hash}, because it is assumed to be equal to the internal address of the account ($\accountid$), already present in the \texttt{addr} field. The {\tt split\_depth} field is present and non-zero only in instances of large smart contracts. The {\tt special} field may be present only in the masterchain---and within the masterchain, only in some {\em fundamental\/} smart contracts required for the whole system to function. + +The storage statistics kept in {\tt storage\_stat} reflect the total storage usage of cell slice {\tt storage}. In particular, the bits and cells used to store the {\tt balance} are also reflected in {\tt storage\_stat}. + +When a non-existent account needs to be represented, the {\tt account\_none} constructor is used. + +\nxsubpoint\label{sp:acc.as.msg}\emb{Account state as a message from an account to its future self} +Notice that the account state is very similar to a message sent from an account to its future self participating in the next transaction, for the following reasons: +\begin{itemize} +\item The account state does not change between two consecutive transactions of the same account, so it is completely similar in this respect to a message sent from the earlier transaction to the later one. +\item When a transaction is processed, its inputs are an inbound message and the previous account state; its outputs are outbound messages generated and the next account state. If we treat the state as a special kind of message, we see that every transaction has exactly two inputs (the account state and an inbound message) and at least one output. +\item Both a message and the account state can carry code and data in an instance of {\em StateInit}, and some value in their {\tt balance}. +\item An account is initialized by a {\em constructor message}, which essentially carries the future state and balance of the account. +\item On some occasions messages are converted into account states, and vice versa. For instance, when a shardchain merge event occurs, and two accounts that are instances of the same large contract need to be merged, one of them is converted into a message sent to the other one (cf.~\ptref{sp:merge.trs}). Similarly, when a shardchain split event occurs, and an instance of a large smart contract needs to be split into two, this is achieved by a special transaction that creates the new instance by means of a constructor message sent from the previously existing instance to the new one (cf.~\ptref{sp:split.trs}). +\item One may say that a message is involved in transferring some information {\em across space\/} (between different shardchains, or at least accountchains), while an account state transfers information {\em across time\/} (from the past to the future of the same account). +\end{itemize} + +\nxsubpoint\emb{Differences between messages and account states} +Of course, there are important differences, too. For example: +\begin{itemize} +\item The account state is transferred only ``in time'' (for a shardchain block to its successor), but never ``in space'' (from one shardchain to another). As a consequence, this transfer is done implicitly, without creating complete copies of the account state anywhere in the blockchain. +\item Storage payments collected by the validators for keeping the account state usually are considerably smaller than message forwarding fees for the same amount of data. +\item When an inbound message is delivered to an account, it is the code from the account that is invoked, not the code from the message. +\end{itemize} + +\nxsubpoint\label{sp:shard.accs}\emb{The combined state of all accounts in a shard} +The split part of the shardchain state (cf.~\ptref{sp:isp.blk.state} and~\ptref{sp:split.blk.part}) is given by +\begin{verbatim} +_ (HashmapAugE 256 Account CurrencyCollection) = ShardAccounts; +\end{verbatim} +This is simply a dictionary with 256-bit $\accountid$s as keys and corresponding account states as values, sum-augmented by the balances of the accounts. In this way the sum of balances of all accounts in a shardchain is computed, so that one can easily check the total amount of cryptocurrency ``stored'' in a shard. + +Internal consistency conditions ensure that the address of an account referred to by key $k$ in {\em SmartAccounts\/} is indeed equal to~$k$. An additional internal consistency condition requires that all keys $k$ begin with the shard prefix~$s$. + +\nxsubpoint\emb{Account owner and interface descriptions} +One may want to include some optional information in a controlled account. For example, an individual user or a company may want to add a text description field to their wallet account, with the user's or company's name or address (or their hash, if the information should not be made publicly available). Alternatively, a smart contract may offer a machine-readable or human-readable description of its supported methods and their intended application, which might be used by advanced wallet applications to construct drop-down menus and forms helping a human user to create valid messages to be sent to that smart contract. + +One way of including such information is to reserve, say, the second reference in the {\tt data} cell of the state of an account for a dictionary with 64-bit keys (corresponding to some identifiers of the standard types of extra data one might want to store) and corresponding values. Then a blockchain explorer would be able to extract the required value, along with a Merkle proof if necessary. + +A better way of doing this is by defining some {\em get methods\/} in the smart contract. + +\nxsubpoint\emb{Get methods of a smart contract} +{\em Get methods\/} are executed by a stand-alone instance of TVM with the account's code and data loaded into it. The required parameters are passed on the stack (say, a magic number indicating the field to be fetched or the specific get method to be invoked), and the results are returned on the TVM stack as well (say, a cell slice containing the serialization of a string with the account owner's name). + +As a bonus, get methods may be used to get answers to more sophisticated queries than just fetching a constant object. For instance, TON DNS registry smart contracts provide get methods to look up a domain string in the registry and return the corresponding record, if found. + +By convention, get methods use large {\em negative\/} 32-bit or 64-bit indices or magic numbers, and internal functions of a smart contract use consecutive {\em positive\/} indices, to be used in TVM's \texttt{CALLDICT} instruction. The {\tt main()} function of a smart contract, used to process inbound messages in ordinary transactions, always has index zero. + +\mysubsection{Transactions} +According to the Infinite Sharding Paradigm and the actor model, the three principal components of the TON Blockchain are {\em accounts\/} (along with their states), {\em messages,} and {\em transactions}. Previous sections have already discussed the first two; this section considers transactions. + +In contrast with messages, which have essentially the same headers throughout all workchains of the TON Blockchain, and accounts, which have at least some common parts (the address and the balance), our discussion of transactions is necessarily limited to the masterchain and the basic workchain. Other workchains may define completely different kinds of transactions. + +\nxsubpoint\label{sp:trans.lt}\emb{Logical time of a transaction} +Each transaction $t$ has a logical time interval $\LT^\bullet(t)=[\LT^-(t),\LT^+(t))$ assigned to it (cf.~\ptref{sp:lt.ton.blkch} and~\ptref{sp:logic.time.interval}). By convention, a transaction $t$ generating $n$ outbound messages $m_1$, \dots, $m_n$ is assigned a logical time interval of length $n+1$, so that +\begin{equation} + \LT^+(t)=\LT^-(t)+n+1\quad. +\end{equation} +We also set $\LT(t):=\LT^-(t)$, and assign the logical creation time of message $m_i$, where $1\leq i\leq n$, by +\begin{equation} + \LT(m_i)=\LT^-(m_i):=\LT^-(t)+i,\quad\LT^+(m_i):=\LT^-(m_i)+1\quad. +\end{equation} +In this way, each generated outbound message is assigned its own unit interval inside the logical time interval $\LT^\bullet(t)$ of transaction~$t$. + +\nxsubpoint\emb{Logical time uniquely identifies transactions and outbound messages of an account} +Recall that the conditions imposed on logical time imply that $\LT^-(t)\geq\LT^+(t')$ for any preceding transaction $t'$ of the same account~$\xi$, and that $\LT^-(t)>\LT(m)$ if $m$ is the inbound message processed by transaction~$t$. In this way, the logical time intervals of transactions of the same account do not intersect each other, and as a consequence, the logical time intervals of all outbound messages generated by an account do not intersect each other either. In other words, all $\LT(m)$ are different, when $m$ runs through all outbound messages of the same account~$\xi$. + +In this way, $\LT(t)$ and $\LT(m)$, when combined with an account identifier $\xi$, uniquely determine a transaction $t$ or an outbound message~$m$ of that account. Furthermore, if one has an ordered list of all transactions of an account along with their logical times, it is easy to find the transaction that generated a given outbound message~$m$, simply by looking up the transaction $t$ with logical time $\LT(t)$ nearest to~$\LT(m)$ from below. + +\nxsubpoint\label{sp:gen.comp.tr}% +\emb{Generic components of a transaction} +Each transaction $t$ contains or indirectly refers to the following data: +\begin{itemize} +\item The account $\xi$ to which the transaction belongs. +\item The logical time $\LT(t)$ of the transaction. +\item One or zero inbound messages $m$ processed by the transaction. +\item The number of generated outbound messages $n\geq0$. +\item The outbound messages $m_1$, \dots, $m_n$. +\item The initial state of account $\xi$ (including its balance). +\item The final state of account $\xi$ (including its balance). +\item The total fees collected by the validators. +\item A detailed description of the transaction containing all or some data needed to validate it, including the kind of the transaction (cf.~\ptref{sp:trans.kinds}) and some of the intermediate steps performed. +\end{itemize} +Of these components, all but the very last one are quite general and might appear in other workchains as well. + +\nxsubpoint\label{sp:trans.kinds}\emb{Kinds of transactions} +There are different kinds of transactions allowed in the masterchain and the shardchains. {\em Ordinary\/} transactions consist in the delivery of one inbound message to an account, and its processing by that account's code; this is the most common kind of transaction. Additionally, there are several kinds of {\em exotic\/} transactions. + +Altogether, there are six kinds of transactions: +\begin{itemize} +\item {\em Ordinary transactions\/} --- Belong to an account $\xi$. They process exactly one inbound message $m$ (described in {\em InMsgDescr\/} of the encompassing block) with destination~$\xi$, compute the new state of the account, and generate several outbound messages (registered in {\em OutMsgDescr}) with source~$\xi$. +\item {\em Storage transactions\/} --- Can be inserted by validators at their discretion. They do not process any inbound message and do not invoke any code. Their only effect is to collect storage payments from an account, affecting its storage statistics and its balance. If the resulting Gram balance of the account becomes less than a certain amount, the account may be frozen and its code and data replaced by their combined hash. +\item {\em Tick transactions\/} --- Automatically invoked for certain special accounts (smart contracts) in the masterchain that have the {\tt tick} flag set in their state, as the very first transactions in every masterchain block. They have no inbound message, but may generate outbound messages and change the account state. For instance, {\em validator elections\/} are performed by tick transactions of special smart contracts in the masterchain. +\item {\em Tock transactions\/} --- Similarly automatically invoked as the very last transactions in every masterchain block for certain special accounts. +\item {\em Split transactions\/} --- Invoked as the last transactions of shardchain blocks immediately preceding a shardchain split event. They are triggered automatically for instances of large smart contracts that need to produce a new instance after splitting. +\item {\em Merge transactions\/} --- Similarly invoked as the first transactions of shardchain blocks immediately after a shardchain merge event, if an instance of a large smart contract needs to be merged with another instance of the same smart contract. +\end{itemize} +Notice that out of these six kinds of transactions, only four can occur in the masterchain, and another subset of four can occur in the basic workchain. + +\nxsubpoint\emb{Phases of an ordinary transaction} +An ordinary transaction is performed in several {\em phases}, which may be thought of as several ``sub-transactions'' tightly bound into one: +\begin{itemize} +\item {\em Storage phase} --- Collects due storage payments for the account state (including smart-contract code and data, if present) up to the present time. The smart contract may be {\em frozen\/} as a result. If the smart contract did not exist before, the storage phase is absent. +\item {\em Credit phase} --- The account is credited with the value of the inbound message received. +\item {\em Computing phase} --- The code of the smart contract is invoked inside an instance of TVM with adequate parameters, including a copy of the inbound message and of the persistent data, and terminates with an exit code, the new persistent data, and an {\em action list} (which includes, for instance, outbound messages to be sent). The processing phase may lead to the creation of a new account (uninitialized or active), or to the activation of a previously uninitialized or frozen account. The {\em gas payment}, equal to the product of the gas price and the gas consumed, is exacted from the account balance. +\item {\em Action phase} --- If the smart contract has terminated successfully (with exit code 0 or 1), the actions from the list are performed. If it is impossible to perform all of them---for example, because of insufficient funds to transfer with an outbound message---then the transaction is aborted and the account state is rolled back. The transaction is also aborted if the smart contract did not terminate successfully, or if it was not possible to invoke the smart contract at all because it is uninitialized or frozen. +\item {\em Bounce phase} --- If the transaction has been aborted, and the inbound message has its {\tt bounce} flag set, then it is ``bounced'' by automatically generating an outbound message (with the {\tt bounce} flag clear) to its original sender. Almost all value of the original inbound message (minus gas payments and forwarding fees) is transferred to the generated message, which otherwise has an empty body. +\end{itemize} + +\nxsubpoint\emb{Bouncing inbound messages to non-existent accounts} +Notice that if an inbound message with its {\tt bounce} flag set is sent to a previously non-existent account, and the transaction is aborted (for instance, because there is no code and data with the correct hash in the inbound message, so the virtual machine could not be invoked at all), then the account is not created even as an uninitialized account, since it would have zero balance and no code and data anyways.\footnote{In particular, if a user mistakenly sends some funds to a non-existent address in a bounceable message, the funds will not be wasted, but rather will be returned (bounced) back. Therefore, a user wallet application should set the {\tt bounce} flag in all generated messages by default unless explicitly instructed otherwise. However, non-bounceable messages are indispensable in some situations (cf.~\ptref{sp:ex.simple.wallet}).} + +\nxsubpoint\label{sp:proc.in.msg}\emb{Processing of an inbound message is split between computing and action phases} +Notice that the processing of an inbound message is in fact split into two phases: the {\em computing\/} phase and the {\em action\/} phase. During the computing phase, the virtual machine is invoked and the necessary computations are performed, but no actions outside the virtual machine are taken. In other words, {\em the execution of a smart contract in TVM has no side effects}; there is no way for a smart contract to interact with the blockchain directly during its execution. Instead, TVM primitives such as {\tt SENDMSG} simply store the required action (e.g., the outbound message to be sent) into the action list being gradually accumulated in TVM control register \texttt{c5}. The actions themselves are postponed until the action phase, during which the user smart contract is not invoked at all. + +\nxsubpoint\label{sp:proc.in.msg.split}\emb{Reasons for splitting the processing into computation and action phases} +Some reasons for such an arrangement are: +\begin{itemize} +\item It is simpler to abort the transaction if the smart contract eventually terminates with an exit code other than 0 or 1. +\item The rules for processing output actions may be changed without modifying the virtual machine. (For instance, new output actions may be introduced.) +\item The virtual machine itself may be modified or even replaced by another one (for instance, in a new workchain) without changing the rules for processing output actions. +\item The execution of the smart contract inside the virtual machine is completely isolated from the blockchain and is a {\em pure computation}. As a consequence, this execution may be {\em virtualized\/} inside the virtual machine itself by means of TVM's {\tt RUNVM} primitive, a useful feature for validator smart contracts and for smart contracts controlling payment channels and other sidechains. Additionally, the virtual machine may be {\em emulated\/} inside itself or a stripped-down version of itself, a useful feature for validating the execution of smart contracts inside TVM.\footnote{A reference implementation of a TVM emulator running in a stripped-down version of TVM may be committed into the masterchain to be used when a disagreement between the validators on a specific run of TVM arises. In this way, flawed implementations of TVM may be detected. The reference implementation then serves as an authoritative source on the operational semantics of TVM. (Cf.~\cite[B.2]{TVM})} +\end{itemize} + +\nxsubpoint\emb{Storage, tick, and tock transactions} +Storage transactions are very similar to a stand-alone storage phase of an ordinary transaction. Tick and tock transactions are similar to ordinary transactions without credit and bounce phases, because there is no inbound message. + +\nxsubpoint\label{sp:split.trs}\emb{Split transactions} +Split transactions in fact consist of two transactions. If an account $\xi$ needs to be split into two accounts $\xi$ and $\xi'$: +\begin{itemize} +\item First a {\em split prepare transaction}, similar to a tock transaction (but in a shardchain instead of the masterchain), is issued for account $\xi$. It must be the last transaction for $\xi$ in a shardchain block. The output of the processing stage of a split prepare transaction consists not only of the new state of account $\xi$, but also of the new state of account $\xi'$, represented by a constructor message to~$\xi'$ (cf.~\ptref{sp:acc.as.msg}). +\item Then a {\em split install transaction\/} is added for account $\xi'$, with a reference to the corresponding split prepare transaction. The split install transaction must be the only transaction for a previously non-existent account $\xi'$ in the block. It effectively sets the state of $\xi'$ as defined by the split prepare transaction. +\end{itemize} + +\nxsubpoint\label{sp:merge.trs}\emb{Merge transactions} +Merge transactions also consist of two transactions each. If an account $\xi'$ needs to be merged into account $\xi$: +\begin{itemize} +\item First a {\em merge prepare transaction\/} is issued for $\xi'$, which converts all of its persistent state and balance into a special constructor message with destination $\xi$ (cf.~\ptref{sp:acc.as.msg}). +\item Then a {\em merge install transaction\/} for~$\xi$, referring to the corresponding merge prepare transaction, processes that constructor message. The merge install transaction is similar to a tick transaction in that it must be the first transaction for~$\xi$ in a block, but it is located in a shardchain block, not in the masterchain, and it has a special inbound message. +\end{itemize} + +\nxsubpoint\label{sp:trans.gen.tlb}\emb{Serialization of a general transaction} +Any transaction contains the fields listed in~\ptref{sp:gen.comp.tr}. As a consequence, there are some common components in all transactions: +\begin{verbatim} +transaction$_ account_addr:uint256 lt:uint64 outmsg_cnt:uint15 + orig_status:AccountStatus end_status:AccountStatus + in_msg:(Maybe ^(Message Any)) + out_msgs:(HashmapE 15 ^(Message Any)) + total_fees:Grams state_update:^(MERKLE_UPDATE Account) + description:^TransactionDescr = Transaction; + +!merkle_update#02 {X:Type} old_hash:uint256 new_hash:uint256 + old:^X new:^X = MERKLE_UPDATE X; +\end{verbatim} +The exclamation mark in the TL-B declaration of a {\tt merkle\_update} indicates special processing required for such values. In particular, they must be kept in a separate cell, which must be marked as {\em exotic} by a bit in its header (cf.~\cite[3.1]{TVM}). + +A full explanation of the serialization of {\em TransactionDescr}, which describes one transaction according to its kind listed in~\ptref{sp:trans.kinds}, can be found in~\ptref{p:trans.descr}. + +\nxsubpoint\emb{Representation of outbound messages generated by a transaction} +The outbound messages generated by a transaction $t$ are kept in a dictionary {\tt out\_msgs} with 15-bit keys equal to 0, 1, \dots, $n-1$, where $n=\texttt{outmsg\_cnt}$ is the number of generated outbound messages. Message $m_{i+1}$ with index $0\leq i x +acst_frozen$10 = AccStatusChange; // init -> frozen +acst_deleted$11 = AccStatusChange; // frozen -> deleted +\end{verbatim} + +\nxsubpoint\emb{Description of a credit phase} +The credit phase can result in the collection of some due payments: +\begin{verbatim} +tr_phase_credit$_ due_fees_collected:(Maybe Grams) + credit:CurrencyCollection = TrCreditPhase; +\end{verbatim} +The sum of {\tt due\_fees\_collected} and {\tt credit} must equal the value of the message received, plus its {\tt ihr\_fee} if the message has not been received via IHR (otherwise the {\tt ihr\_fee} is awarded to the validators). + +\nxsubpoint\emb{Description of a computing phase} +The computing phase consists in invoking TVM with correct inputs. On some occasions, TVM cannot be invoked at all (e.g., if the account is absent, not initialized, or frozen, and the inbound message being processed has no code or data fields or these fields have an incorrect hash); this is reflected by corresponding constructors. +\begin{verbatim} +tr_phase_compute_skipped$0 reason:ComputeSkipReason + = TrComputePhase; +tr_phase_compute_vm$1 success:Bool msg_state_used:Bool + account_activated:Bool gas_fees:Grams + _:^[ gas_used:(VarUInteger 7) + gas_limit:(VarUInteger 7) gas_credit:(Maybe (VarUInteger 3)) + mode:int8 exit_code:int32 exit_arg:(Maybe int32) + vm_steps:uint32 + vm_init_state_hash:uint256 vm_final_state_hash:uint256 ] + = TrComputePhase; +cskip_no_state$00 = ComputeSkipReason; +cskip_bad_state$01 = ComputeSkipReason; +cskip_no_gas$10 = ComputeSkipReason; +\end{verbatim} +The TL-B construct {\tt \_:\caret[...]} describes a reference to a cell containing the fields listed inside the square brackets. In this way, several fields can be moved from a cell containing a large record into a separate subcell. + +\nxsubpoint\emb{Skipped computing phase} +If the computing phase has been skipped, possible reasons include: +\begin{itemize} +\item The absence of funds to buy gas. +\item The absence of a state (i.e., smart-contract code and data) in both the account (non-existing, uninitialized, or frozen) and the message. +\item An invalid state passed in the message (i.e., the state's hash differs from the expected value) to a frozen or uninitialized account. +\end{itemize} + +\nxsubpoint\emb{Valid computing phase} +If there is no reason to skip the computing phase, TVM is invoked and the results of the computation are logged. Possible parameters are as follows: +\begin{itemize} +\item The {\tt success} flag is set if and only if {\tt exit\_code} is either 0 or 1. +\item The {\tt msg\_state\_used} parameter reflects whether the state passed in the message has been used. If it is set, the {\tt account\_activated} flag reflects whether this has resulted in the activation of a previously frozen, uninitialized or non-existent account. +\item The {\tt gas\_fees} parameter reflects the total gas fees collected by the validators for executing this transaction. It must be equal to the product of {\tt gas\_used} and {\tt gas\_price} from the current block header. +\item The {\tt gas\_limit} parameter reflects the gas limit for this instance of TVM. It equals the lesser of either the Grams credited in the credit phase from the value of the inbound message divided by the current gas price, or the global per-transaction gas limit. +\item The {\tt gas\_credit} parameter may be non-zero only for external inbound messages. It is the lesser of either the amount of gas that can be paid from the account balance or the maximum gas credit. +\item The {\tt exit\_code} and {\tt exit\_args} parameters represent the status values returned by TVM. +\item The {\tt vm\_init\_state\_hash} and {\tt vm\_final\_state\_hash} parameters are the representation hashes of the original and resulting states of TVM, and {\tt vm\_steps} is the total number of steps performed by TVM (usually equal to two plus the number of instructions executed, including implicit {\tt RET}s).\footnote{Notice that this record does not represent a change in the state of the account, because the transaction may still be aborted during the action phase. In that case, the new persistent data indirectly referenced by {\tt vm\_final\_state\_hash} will be discarded.} +\end{itemize} + +\nxsubpoint\emb{Description of the action phase} +The action phase occurs after a valid computation phase. It attempts to perform the actions stored by TVM during the computing phase into the {\em action list}. It may fail, because the action list may turn out to be too long, contain invalid actions, or contain actions that cannot be completed (for instance, because of insufficient funds to create an outbound message with the required value). +\begin{verbatim} +tr_phase_action$_ success:Bool valid:Bool no_funds:Bool + status_change:AccStatusChange + total_fwd_fees:(Maybe Grams) total_action_fees:(Maybe Grams) + result_code:int32 result_arg:(Maybe int32) tot_actions:int16 + spec_actions:int16 msgs_created:int16 + action_list_hash:uint256 tot_msg_size:StorageUsed + = TrActionPhase; +\end{verbatim} + +\nxsubpoint\emb{Description of the bounce phase} +\begin{verbatim} +tr_phase_bounce_negfunds$00 = TrBouncePhase; +tr_phase_bounce_nofunds$01 msg_size:StorageUsed + req_fwd_fees:Grams = TrBouncePhase; +tr_phase_bounce_ok$1 msg_size:StorageUsed + msg_fees:Grams fwd_fees:Grams = TrBouncePhase; +\end{verbatim} + +\nxsubpoint\emb{Description of an ordinary transaction} +\begin{verbatim} +trans_ord$0000 storage_ph:(Maybe TrStoragePhase) + credit_ph:(Maybe TrCreditPhase) + compute_ph:TrComputePhase action:(Maybe ^TrActionPhase) + aborted:Bool bounce:(Maybe TrBouncePhase) + destroyed:Bool + = TransactionDescr; +\end{verbatim} +Several consistency conditions are imposed on this structure: +\begin{itemize} +\item {\tt action} is absent if and only if the computing phase was unsuccessful. +\item The {\tt aborted} flag is set either if there is no action phase or if the action phase was unsuccessful. +\item The bounce phase occurs only if the {\tt aborted} flag is set and the inbound message was bounceable. +\end{itemize} + +\nxsubpoint\emb{Description of a storage transaction} +A storage transaction consists just of one stand-alone storage phase: +\begin{verbatim} +trans_storage$0001 storage_ph:TrStoragePhase + = TransactionDescr; +\end{verbatim} + +\nxsubpoint\emb{Description of tick and tock transactions} +Tick and tock transactions are similar to ordinary transactions without an inbound message, so there are no credit or bounce phases: +\begin{verbatim} +trans_tick_tock$001 is_tock:Bool storage:TrStoragePhase + compute_ph:TrComputePhase action:(Maybe ^TrActionPhase) + aborted:Bool destroyed:Bool = TransactionDescr; +\end{verbatim} + +\nxsubpoint\label{sp:split.trans}\emb{Split prepare and install transactions} +A split prepare transaction is similar to a tock transaction in a masterchain, but it must generate exactly one special constructor message; otherwise, the action phase is aborted. +\begin{verbatim} +split_merge_info$_ cur_shard_pfx_len:(## 6) + acc_split_depth:(## 6) this_addr:uint256 sibling_addr:uint256 + = SplitMergeInfo; +trans_split_prepare$0100 split_info:SplitMergeInfo + compute_ph:TrComputePhase action:(Maybe ^TrActionPhase) + aborted:Bool destroyed:Bool + = TransactionDescr; +trans_split_install$0101 split_info:SplitMergeInfo + prepare_transaction:^Transaction + installed:Bool = TransactionDescr; +\end{verbatim} +Notice that the split install transaction for the new sibling account $\xi'$ refers to its corresponding split prepare transaction of the previously existing account $\xi$. + +\nxsubpoint\label{sp:merge.trans}\emb{Merge prepare and install transactions} +A merge prepare transaction converts the state and balance of an account into a message, and a subsequent merge install transaction processes this state: +\begin{verbatim} +trans_merge_prepare$0110 split_info:SplitMergeInfo + storage_ph:TrStoragePhase aborted:Bool + = TransactionDescr; +trans_merge_install$0111 split_info:SplitMergeInfo + prepare_transaction:^Transaction + credit_ph:(Maybe TrCreditPhase) + compute_ph:TrComputePhase action:(Maybe ^TrActionPhase) + aborted:Bool destroyed:Bool + = TransactionDescr; +\end{verbatim} + +\mysubsection{Invoking smart contracts in TVM} +This section describes the exact parameters with which TVM is invoked during the computing phase of ordinary and other transactions. + +\nxsubpoint\label{sp:smc.code}\emb{Smart-contract code} +The {\em code\/} of a smart contract is normally a part of the account's persistent state, at least if the account is {\em active\/} (cf.~\ptref{sp:acc.descr}). However, a frozen or uninitialized (or non-existent) account has no persistent state, with the possible exception of the account's balance and the hash of its intended state (equal to the account address for uninitialized accounts). In this case, the code must be supplied in the {\tt init} field of the inbound message being processed by the transaction (cf.~\ptref{sp:msg.layout}). + +\nxsubpoint\emb{Smart-contract persistent data} +The {\em persistent data\/} of a smart contract is kept alongside its code, and remarks similar to those made above in~\ptref{sp:smc.code} apply. In this respect, the code and persistent data of a smart contract are just two parts of its persistent state, which differ only in the way they are treated by TVM during smart-contract execution. + +\nxsubpoint\label{sp:lib.env}\emb{Smart-contract library environment} +The {\em library environment\/} of a smart contract is a hashmap mapping 256-bit cell (representation) hashes into the corresponding cells themselves. When an external cell reference is accessed during the execution of a smart contract, the cell referred to is looked up in the library environment and the external cell reference is transparently replaced by the cell found. + +The library environment for an invocation of a smart contract is computed as follows: +\begin{enumerate} +\item The global library environment for the workchain in question is taken from the current state of the masterchain.\footnote{The most common way of creating shared libraries for TVM is to publish a reference to the root cell of the library in the masterchain.} +\item Next, it is augmented by the local library environment of the smart contract, stored in the {\tt library} field of the smart contract's state. Only 256-bit keys equal to the hashes of the corresponding value cells are taken into account. If a key is present in both the global and local library environments, the local environment takes precedence while merging the two library environments. +\item Finally, the message library stored in the {\tt library} field of the {\tt init} field of the inbound message is similarly taken into account. Notice, however, that if the account is frozen or uninitialized, the {\tt library} field of the message is part of the suggested state of the account, and is used instead of the local library environment in the previous step. The message library has lower precedence than both the local and the global library environments. +\end{enumerate} + +\nxsubpoint\label{sp:tvm.smc.init}\emb{The initial state of TVM} +A new instance of TVM is initialized prior to the execution of a smart contract as follows: +\begin{itemize} +\item The original {\tt cc} (current continuation) is initialized using the cell slice created from the cell {\tt code}, containing the code of the smart contract computed as described in~\ptref{sp:smc.code}. +\item The {\tt cp} (TVM codepage) is set to zero. If the smart contract wants to use another TVM codepage $x$, it must switch to it by using {\tt SETCODEPAGE $x$} as the first instruction of its code. +\item Control register {\tt c0} (return continuation) is initialized by extraordinary continuation {\tt ec\_quit} with parameter 0. When executed, this continuation leads to a termination of TVM with exit code 0. +\item Control register {\tt c1} (alternative return continuation) is initialized by extraordinary continuation {\tt ec\_quit} with parameter 1. When invoked, it leads to a termination of TVM with exit code 1. (Notice that terminating with exit code 0 or 1 is considered a successful termination.) +\item Control register {\tt c2} (exception handler) is initialized by extraordinary continuation {\tt ec\_quit\_exc}. When invoked, it takes the top integer from the stack (equal to the exception number) and terminates TVM with exit code equal to that integer. In this way, by default all exceptions terminate the smart-contract execution with exit code equal to the exception number. +\item Control register {\tt c3} (code dictionary) is initialized by the cell with the smart-contract code, similarly to the initial current continuation ({\tt cc}). +\item Control register {\tt c4} (root of persistent data) is initialized by the persistent data of the smart contract.\footnote{The persistent data of the smart contract need not be loaded in its entirety for this to occur. Instead the root is loaded, and TVM may load other cells by their references from the root only when they are accessed, thus providing a form of virtual memory.} +\item Control register {\tt c5} (root of actions) is initialized by an empty cell. The ``output action'' primitives of TVM, such as {\tt SENDMSG}, use {\tt c5} to accumulate the list of actions (e.g., outbound messages) to be performed upon successful termination of the smart contract (cf.~\ptref{sp:proc.in.msg} and~\ptref{sp:proc.in.msg.split}). +\item Control register {\tt c7} (root of temporary data) is initialized by a singleton {\em Tuple}, the only component of which is a {\em Tuple\/} containing an instance of {\em SmartContractInfo\/} with smart contract balance and other useful information (cf.~\ptref{sp:smc.info}). The smart contract may replace the temporary data, especially all components of the {\em Tuple\/} at {\tt c7} but the first one, with whatever other temporary data it may require. However, the original content of the {\em SmartContractInfo\/} at the first component of the {\em Tuple\/} held in {\tt c7} is inspected and sometimes modified by {\tt SENDMSG} TVM primitives and other ``output action'' primitives of TVM. +\item The {\em gas limits\/} $\texttt{gas}=(g_m,g_l,g_c,g_r)$ are initialized as follows: + \begin{itemize} + \item The {\em maximal gas limit\/} $g_m$ is set to the lesser of either the total Gram balance of the smart contract (after the the credit phase---i.e., combined with the value of the inbound message) divided by the current gas price, or the per-execution global gas limit.\footnote{Both the global gas limit and the gas price are configurable parameters determined by the current state of the masterchain.} + \item The {\em current gas limit\/} $g_l$ is set to the lesser of either the Gram value of the inbound message divided by the gas price, or the global per-execution gas limit. In this way, always $g_l\leq g_m$. For inbound external messages $g_l=0$, since they cannot carry any value. + \item The {\em gas credit\/} $g_c$ is set to zero for inbound internal messages, and to the lesser of either $g_m$ or a fixed small value (the default external message gas credit, a configurable parameter) for inbound external messages. + \item Finally, the {\em remaining gas limit\/} $g_r$ is automatically initialized by $g_l+g_c$. + \end{itemize} +\end{itemize} + +\nxsubpoint\label{sp:smc.stack.init}\emb{The initial stack of TVM for processing an internal message} +After TVM is initialized as described in~\ptref{sp:tvm.smc.init}, its stack is initialized by pushing the arguments to the {\tt main()} function of the smart contract as follows: +\begin{itemize} +\item The Gram balance $b$ of the smart contract (after crediting the value of the inbound message) is passed as an {\em Integer\/} amount of nanograms. +\item The Gram balance $b_m$ of inbound message $m$ is passed as an {\em Integer\/} amount of nanograms. +\item The inbound message $m$ is passed as a cell, which contains a serialized value of type {\em Message $X$}, where $X$ is the type of the message body. +\item The body $m_b:X$ of the inbound message, equal to the value of field {\tt body} of $m$, is passed as a cell slice. +\item Finally, the {\em function selector\/} $s$, an {\em Integer\/} normally equal to zero, is pushed into the stack. +\end{itemize} +After that, the code of the smart contract, equal to its initial value of {\tt c3}, is executed. It selects the correct function according to $s$, which is expected to process the remaining arguments to the function and terminate afterwards. + +\nxsubpoint\emb{Processing an inbound external message} +An inbound external message is processed similarly to \ptref{sp:tvm.smc.init} and~\ptref{sp:smc.stack.init}, with the following modifications: +\begin{itemize} +\item The function selector $s$ is set to $-1$, not to 0. +\item The Gram balance $b_m$ of inbound message is always 0. +\item The initial current gas limit $g_l$ is always 0. However, the initial gas credit $g_c>0$. +\end{itemize} +The smart contract must terminate with $g_c=0$ or $g_r\geq g_c$; otherwise, the transaction and the block containing it are invalid. Validators or collators suggesting a block candidate must never include transactions processing inbound external messages that are invalid. + +\nxsubpoint\emb{Processing tick and tock transactions} +The TVM stack for processing tick and tock transactions (cf.~\ptref{sp:trans.kinds}) is initialized by pushing the following values: +\begin{itemize} +\item The Gram balance $b$ of the current account in nanograms (an {\em Integer}). +\item The 256-bit address $\xi$ of the current account inside the masterchain, represented by an unsigned {\em Integer}. +\item An integer equal to $0$ for tick transactions and to $-1$ for tock transactions. +\item The function selector $s$, equal to $-2$. +\end{itemize} + +\nxsubpoint\emb{Processing split prepare transactions} +For processing split prepare transactions (cf.~\ptref{sp:split.trans}), the TVM stack is initialized by pushing the following values: +\begin{itemize} +\item The Gram balance $b$ of the current account. +\item A {\em Slice\/} containing {\em SplitMergeInfo\/} (cf.~\ptref{sp:split.trans}). +\item The 256-bit address $\xi$ of the current account. +\item The 256-bit address $\tilde\xi$ of the sibling account. +\item An integer $0\leq d\leq 63$, equal to the position of the only bit in which $\xi$ and $\tilde\xi$ differ. +\item The function selector $s$, equal to $-3$. +\end{itemize} + +\nxsubpoint\emb{Processing merge install transactions} +For processing merge install transactions (cf.~\ptref{sp:merge.trans}), the TVM stack is initialized by pushing the following values: +\begin{itemize} +\item The Gram balance $b$ of the current account (already combined with the Gram balance of the sibling account). +\item The Gram balance $b'$ of the sibling account, taken from the inbound message $m$. +\item The message $m$ from the sibling account, automatically generated by a merge prepare transaction. Its {\tt init} field contains the final state $\tilde S$ of the sibling account. +\item The state $\tilde S$ of the sibling account, represented by a {\em StateInit} (cf.~\ptref{sp:msg.layout}). +\item A {\em Slice\/} containing {\em SplitMergeInfo\/} (cf.~\ptref{sp:split.trans}). +\item The 256-bit address $\xi$ of the current account. +\item The 256-bit address $\tilde\xi$ of the sibling account. +\item An integer $0\leq d\leq 63$, equal to the position of the only bit in which $\xi$ and $\tilde\xi$ differ. +\item The function selector $s$, equal to $-4$. +\end{itemize} + +\nxsubpoint\label{sp:smc.info}\emb{Smart-contract information} +The smart-contract information structure {\em SmartContractInfo}, passed in the first component of the {\em Tuple\/} contained in control register {\tt c7}, is also a {\em Tuple} containing the following data: +\begin{verbatim} +[ magic:0x076ef1ea actions:Integer msgs_sent:Integer + unixtime:Integer block_lt:Integer trans_lt:Integer + rand_seed:Integer balance_remaining:[Integer (Maybe Cell)] + myself:MsgAddressInt global_config:(Maybe Cell) +] = SmartContractInfo; +\end{verbatim} +In other words, the first component of this tuple is an {\em Integer\/} {\tt magic\/} always equal to {\tt 0x076ef1ea}, the second component is an {\em Integer\/} {\tt actions}, originally initialized by zero, but incremented by one whenever an output action is installed by a non-{\tt RAW} output action primitive of the TVM, and so on. The remaining balance is represented by a pair, i.e., a two-component {\em Tuple}: the first component is the nanogram balance, and the second component is a dictionary with 16-bit keys representing all other currencies, if any. + +The {\tt rand\_seed} field (an unsigned 256-bit integer) here is initialized deterministically starting from the {\tt rand\_seed} of the block, the account address, the hash of the inbound message being processed (if any), and the transaction logical time {\tt trans\_lt}. + +\nxsubpoint\label{sp:out.act.ser}\emb{Serialization of output actions} +The {\em output actions\/} of a smart contract are accumulated in a linked list stored in control register {\tt c5}. The list of output actions is serialized as a value of type {\em OutList $n$}, where $n$ is the length of the list: +\begin{verbatim} +out_list_empty$_ = OutList 0; +out_list$_ {n:#} prev:^(OutList n) action:OutAction + = OutList (n + 1); +action_send_msg#0ec3c86d out_msg:^(Message Any) = OutAction; +action_set_code#ad4de08e new_code:^Cell = OutAction; +\end{verbatim} + +\clearpage +\mysection{Block layout}\label{sect:block.layout} +This chapter presents the block layout used by the TON Blockchain, combining the data structures described separately in previous chapters to produce a complete description of a shardchain block. In addition to the TL-B schemes that define the representation of a shardchain block by a tree of cells, this chapter describes exact serialization formats for the resulting bags (collections) of cells, which are necessary to represent a shardchain block as a file. + +Masterchain blocks are similar to shardchain blocks, but have some additional fields. The necessary modifications are discussed separately in~\ptref{p:master.layout}. + +\mysubsection{Shardchain block layout}\label{p:shard.layout} +This section lists the data structures that must be contained in a shardchain block and in the shardchain state, and concludes by presenting a formal TL-B scheme for a shardchain block. + +\nxsubpoint\label{sp:shard.state.comp}\emb{Components of the shardchain state} +The shardchain state consists of: +\begin{itemize} +\item {\em ShardAccounts}, the split part of the shardchain state (cf.~\ptref{sp:split.blk.part}) containing the state of all accounts assigned to this shard (cf.~\ptref{sp:shard.accs}). +\item {\em OutMsgQueue}, the output message queue of the shardchain (cf.~\ptref{sp:out.msg.queue}). +\item {\em SharedLibraries}, the description of all shared libraries of the shardchain (for now, non-empty only in the masterchain). +\item The logical time and the unixtime of the last modification of the state. +\item The total balance of the shard. +\item A hash reference to the most recent masterchain block, indirectly describing the state of the masterchain and, through it, the state of all other shardchains of the TON Blockchain (cf.~\ptref{sp:shard.total.state}). +\end{itemize} + +\nxsubpoint\label{sp:shard.blk.comp}\emb{Components of a shardchain block} +A shardchain block must contain: +\begin{itemize} +\item A list of {\em validator signatures\/} (cf.~\ptref{sp:val.sign}), which is external with respect to all other contents of the block. +\item {\em BlockHeader}, containing general information about the block (cf.~\ptref{sp:blk.hdr}) +\item Hash references to the immediately preceding block or blocks of the same shardchain, and to the most recent masterchain block. +\item {\em InMsgDescr\/} and {\em OutMsgDescr}, the inbound and outbound message descriptors (cf.~\ptref{sp:in.msg.descr} and~\ptref{sp:out.msg.descr}). +\item {\em ShardAccountBlocks}, the collection of all transactions processed in the block (cf.~\ptref{sp:all.transact}) along with all updates of the states of the accounts assigned to the shard. This is the {\em split\/} part of the shardchain block (cf.~\ptref{sp:split.blk.part}). +\item The {\em value flow}, describing the total value imported from the preceding blocks of the same shardchain and from inbound messages, the total value exported by outbound message, the total fees collected by validators, and the total value remaining in the shard. +\item A {\em Merkle update\/} (cf.~\cite[3.1]{TVM}) of the shardchain state. Such a Merkle update contains the hashes of the initial and final shardchain states with respect to the block, along with all new cells of the final state that have been created while processing the block.\footnote{In principle, an experimental version of TON Blockchain might choose to keep only the hashes of the initial and final states of the shardchain. The Merkle update increases the block size, but it is handy for full nodes that want to keep and update their copy of the shardchain state. Otherwise, the full nodes would have to repeat all the computations contained in a block to compute the updated state of the shardchain by themselves.} +\end{itemize} + +\nxsubpoint\emb{Common parts of the block layout for all workchains} +Recall that different workchains may define their own rules for processing messages, other types of transactions, other components of the state, and other ways to serialize all this data. However, some components of the block and its state must be common for all workchains in order to maintain the interoperability between different workchains. Such common components include: +\begin{itemize} +\item {\em OutMsgQueue}, the outbound message queue of a shardchain, which is scanned by neighboring shardchains for messages addressed to them. +\item The outer structure of {\em InMsgDescr\/} as a hashmap with 256-bit keys equal to the hashes of the imported messages. (The inbound message descriptors themselves need not have the same structure.) +\item Some fields in the block header identifying the shardchain and the block, along with the paths from the block header to the other information indicated in this list. +\item The value flow information. +\end{itemize} + +\nxsubpoint\label{sp:shard.state.tlb}\emb{TL-B scheme for the shardchain state} +The shardchain state (cf.~\ptref{sp:isp.blk.state} and~\ptref{sp:shard.state.comp}) is serialized according to the following TL-B scheme: +\begin{verbatim} +ext_blk_ref$_ start_lt:uint64 end_lt:uint64 + seq_no:uint32 hash:uint256 = ExtBlkRef; + +master_info$_ master:ExtBlkRef = BlkMasterInfo; + +shard_ident$00 shard_pfx_bits:(## 6) + workchain_id:int32 shard_prefix:uint64 = ShardIdent; + +shard_state shard_id:ShardIdent + out_msg_queue:OutMsgQueue + total_balance:CurrencyCollection + total_validator_fees:CurrencyCollection + accounts:ShardAccounts + libraries:(HashmapE 256 LibDescr) + master_ref:(Maybe BlkMasterInfo) + custom:(Maybe ^McStateExtra) + = ShardState; +\end{verbatim} +The field {\tt custom} is usually present only in the masterchain and contains all the masterchain-specific data. However, other workchains may use the same cell reference to refer to their specific state data. + +\nxsubpoint\emb{Shared libraries description} +Shared libraries currently can be present only in masterchain blocks. They are described by an instance of $\textit{HashmapE}(256,\textit{LibDescr})$, where the 256-bit key is the representation hash of the library, and \textit{LibDescr\/} describes one library: +\begin{verbatim} +shared_lib_descr$00 lib:^Cell publishers:(Hashmap 256 True) + = LibDescr; +\end{verbatim} +Here {\tt publishers} is a hashmap with keys equal to the addresses of all accounts that have published the corresponding shared library. The shared library is preserved as long as at least one account keeps it in its published libraries collection. + +\nxsubpoint\emb{TL-B scheme for an unsigned shardchain block} +The precise format of an {\em unsigned\/} (cf.~\ptref{sp:val.sign}) shardchain block is given by the following TL-B scheme: +\begin{verbatim} +block_info version:uint32 + not_master:(## 1) + after_merge:(## 1) before_split:(## 1) flags:(## 13) + seq_no:# vert_seq_no:# + shard:ShardIdent gen_utime:uint32 + start_lt:uint64 end_lt:uint64 + master_ref:not_master?^BlkMasterInfo + prev_ref:seq_no?^(BlkPrevInfo after_merge) + prev_vert_ref:vert_seq_no?^(BlkPrevInfo 0) + = BlockInfo; + +prev_blk_info#_ {merged:#} prev:ExtBlkRef + prev_alt:merged?ExtBlkRef = BlkPrevInfo merged; + +unsigned_block info:^BlockInfo value_flow:^ValueFlow + state_update:^(MERKLE_UPDATE ShardState) + extra:^BlockExtra = Block; + +block_extra in_msg_descr:^InMsgDescr + out_msg_descr:^OutMsgDescr + account_blocks:ShardAccountBlocks + rand_seed:uint256 + custom:(Maybe ^McBlockExtra) = BlockExtra; +\end{verbatim} +The field {\tt custom} is usually present only in the masterchain and contains all the masterchain-specific data. However, other workchains may use the same cell reference to refer to their specific block data. + +\nxsubpoint\emb{Description of total value flow through a block} +The total value flow through a block is serialized according to the following TL-B scheme: +\begin{verbatim} +value_flow _:^[ from_prev_blk:CurrencyCollection + to_next_blk:CurrencyCollection + imported:CurrencyCollection + exported:CurrencyCollection ] + fees_collected:CurrencyCollection + _:^[ + fees_imported:CurrencyCollection + created:CurrencyCollection + minted:CurrencyCollection + ] = ValueFlow; +\end{verbatim} +Recall that \texttt{\_:\caret[}\dots\texttt{]} is a TL-B construction indicating that a group of fields has been moved into a separate cell. The last three fields may be non-zero only in masterchain blocks. + +\nxsubpoint\label{sp:signed.blk}\emb{Signed shardchain block} +A signed shardchain block is just an unsigned block augmented by a collection of validator signatures: +\begin{verbatim} +ed25519_signature#5 R:uint256 s:uint256 = CryptoSignature; + +signed_block block:^Block blk_serialize_hash:uint256 + signatures:(HashmapE 64 CryptoSignature) + = SignedBlock; +\end{verbatim} +The {\em serialization hash\/} {\tt blk\_serialize\_hash} of the unsigned block {\tt block} is essentially a hash of a specific serialization of the block into an octet string (cf.~\ptref{sp:blk.ser.hash} for a more detailed explanation). The signatures collected in {\tt signatures} are Ed25519-signatures (cf.~\ptref{p:ed25519}) made with a validator's private keys of the $\Sha$ of the concatenation of the 256-bit representation hash of the block {\tt block} and of its 256-bit serialization hash {\tt blk\_serialize\_hash}. The 64-bit keys in dictionary {\tt signatures} represent the first 64 bits of the public keys of the corresponding validators. + +\nxsubpoint\emb{Serialization of a signed block} +The overall procedure of serializing and signing a block may be described as follows: +\begin{enumerate} +\item An unsigned block $B$ is generated, transformed into a complete bag of cells (cf.~\ptref{sp:boc.complete}), and serialized into an octet string $S_B$. +\item Validators sign the 256-bit combined hash +\begin{equation} + H_B:=\Sha\bigl(\Hash_\infty(B).\Hash_M(S_B)\bigr) +\end{equation} +of the representation hash of $B$ and of the Merkle hash of its serialization~$S_B$. +\item A signed shardchain block $\tilde B$ is generated from $B$ and these validator signatures as described above (cf.~\ptref{sp:signed.blk}). +\item This signed block $\tilde B$ is transformed into an incomplete bag of cells, which contains only the validator signatures, but the unsigned block itself is absent from this bag of cells, being its only absent cell. +\item This incomplete bag of cells is serialized, and its serialization is prepended to the previously constructed serialization of the unsigned block. +\end{enumerate} +The result is the serialization of the signed block into an octet string. It may be propagated by network or stored into a disk file. + +\mysubsection{Masterchain block layout}\label{p:master.layout} +Masterchain blocks are very similar to shardchain blocks of the basic work\-chain. This section lists some of the modifications needed to obtain the description of a masterchain block from the description of a shardchain block given in~\ptref{p:shard.layout}. + +\nxsubpoint\emb{Additional components present in the masterchain state} +In addition to the components listed in~\ptref{sp:shard.state.comp}, the masterchain state must contain: +\begin{itemize} +\item {\em ShardHashes\/} --- Describes the current shard configuration, and contains the hashes of the latest blocks of the corresponding shardchains. +\item {\em ShardFees\/} --- Describes the total fees collected by the validators of each shardchain. +\item {\em ShardSplitMerge\/} --- Describes future shard split/merge events. It is serialized as a part of {\em ShardHashes}. +\item {\em ConfigParams\/} --- Describes the values of all configurable parameters of the TON Blockchain. +\end{itemize} + +\nxsubpoint\emb{Additional components present in masterchain blocks} +In addition to the components listed in~\ptref{sp:shard.blk.comp}, each masterchain block must contain: +\begin{itemize} +\item {\em ShardHashes\/} --- Describes the current shard configuration, and contains the hashes of the latest blocks of the corresponding shardchains. (Notice that this component is also present in the masterchain state.) +\end{itemize} + +\nxsubpoint\label{sp:shard.hashes}\emb{Description of {\em ShardHashes}} +{\em ShardHashes\/} is represented by a dictionary with 32-bit $\workchainid$s as keys, and ``shard binary trees'', represented by TL-B type {\em BinTree ShardDescr}, as values. Each leaf of this shard binary tree contains a value of type {\em ShardDescr}, which describes a single shard by indicating the sequence number {\tt seq\_no}, the logical time {\tt lt}, and the hash {\tt hash} of the latest (signed) block of the corresponding shardchain. +\begin{verbatim} +bt_leaf$0 {X:Type} leaf:X = BinTree X; +bt_fork$1 {X:Type} left:^(BinTree X) right:^(BinTree X) + = BinTree X; + +fsm_none$0 = FutureSplitMerge; +fsm_split$10 mc_seqno:uint32 = FutureSplitMerge; +fsm_merge$11 mc_seqno:uint32 = FutureSplitMerge; + +shard_descr$_ seq_no:uint32 lt:uint64 hash:uint256 + split_merge_at:FutureSplitMerge = ShardDescr; + +_ (HashmapE 32 ^(BinTree ShardDescr)) = ShardHashes; +\end{verbatim} +Fields {\tt mc\_seqno} of {\tt fsm\_split} and {\tt fsm\_merge} are used to signal future shard merge or split events. Shardchain blocks referring to masterchain blocks with sequence numbers up to, but not including, the one indicated in {\tt mc\_seqno} are generated in the usual way. Once the indicated sequence number is reached, a shard merge or split event must occur. + +Notice that the masterchain itself is omitted from {\em ShardHashes\/} (i.e., 32-bit index $-1$ is absent from this dictionary). + +\nxsubpoint\emb{Description of {\em ShardFees}} +{\em ShardFees\/} is a masterchain structure used to reflect the total fees collected so far by the validators of a shardchain. The total fees reflected in this structure are accumulated in the masterchain by crediting them to a special account, whose address is a configurable parameter. Typically this account is the smart contract that computes and distributes the rewards to all validators. +\begin{verbatim} +bta_leaf$0 {X:Type} {Y:Type} leaf:X extra:Y = BinTreeAug X Y; +bta_fork$1 {X:Type} {Y:Type} left:^(BinTreeAug X Y) + right:^(BinTreeAug X Y) extra:Y = BinTreeAug X Y; + +_ (HashmapAugE 32 ^(BinTreeAug True CurrencyCollection) + CurrencyCollection) = ShardFees; +\end{verbatim} +The structure of {\em ShardFees\/} is similar to that of {\em ShardHashes\/} (cf.~\ptref{sp:shard.hashes}), but the dictionary and binary trees involved are augmented by currency values, equal to the {\tt total\_validator\_fees} values of the final states of the corresponding shardchain blocks. The value aggregated at the root of {\em ShardFees\/} is added together with the {\tt total\_validator\_fees} of the masterchain state, yielding the total TON Blockchain validator fees. The increase of the value aggregated at the root of {\em ShardFees\/} from the initial to the final state of a masterchain block is reflected in the {\tt fees\_imported} in the value flow of that masterchain block. + +\nxsubpoint\emb{Description of {\em ConfigParams}} +Recall that the {\em configurable parameters\/} or the {\em configuration dictionary\/} is a dictionary {\tt config} with 32-bit keys kept inside the first cell reference of the persistent data of the configuration smart contract $\gamma$ (cf.~\ptref{p:conf.params}). The address $\gamma$ of the configuration smart contract and a copy of the configuration dictionary are duplicated in fields {\tt config\_addr} and {\tt config} of a {\em ConfigParams\/} structure, explicitly included into masterchain state to facilitate access to the current values of the configurable parameters (cf.~\ptref{sp:conf.par.qa}): +\begin{verbatim} +_ config_addr:uint256 config:^(Hashmap 32 ^Cell) + = ConfigParams; +\end{verbatim} + +\nxsubpoint\emb{Masterchain state data} +The data specific to the masterchain state is collected into {\em McStateExtra}, already mentioned in~\ptref{sp:shard.state.tlb}: +\begin{verbatim} +masterchain_state_extra#cc1f + shard_hashes:ShardHashes + shard_fees:ShardFees + config:ConfigParams += McStateExtra; +\end{verbatim} + +\nxsubpoint\emb{Masterchain block data} +Similarly, the data specific to the masterchain blocks is collected into {\em McBlockExtra\/}: +\begin{verbatim} +masterchain_block_extra#cc9f + shard_hashes:ShardHashes += McBlockExtra; +\end{verbatim} + +\mysubsection{Serialization of a bag of cells} +The description provided in the previous section defines the way a shardchain block is represented as a tree of cells. However, this tree of cells needs to be serialized into a file, suitable for disk storage or network transfer. This section discusses the standard ways of serializing a tree, a DAG, or a bag of cells into an octet string. + +\nxsubpoint\emb{Transforming a tree of cells into a bag of cells} +Recall that values of arbitrary (dependent) algebraic data types are represented in the TON Blockchain by {\em trees of cells}. Such a tree of cells is transformed into a directed acyclic graph, or {\em DAG}, of cells, by identifying identical cells in the tree. After that, we might replace each of the references of each cell by the 32-byte representation hash of the cell referred to and obtain a {\em bag of cells}. By convention, the root of the original tree of cells is a marked element of the resulting bag of cells, so that anybody receiving this bag of cells and knowing the marked element can reconstruct the original DAG of cells, hence also the original tree of cells. + +\nxsubpoint\label{sp:boc.complete}\emb{Complete bags of cells} +Let us say that a bag of cells is {\em complete\/} if it contains all cells referred to by any of its cells. In other words, a complete bag of cells does not have any ``unresolved'' hash references to cells outside that bag of cells. In most cases, we need to serialize only complete bags of cells. + +\nxsubpoint\emb{Internal references inside a bag of cells} +Let us say that a reference of a cell $c$ belonging to a bag of cells $B$ is {\em internal (with respect to $B$)\/} if the cell $c_i$ referred to by this reference belongs to~$B$ as well. Otherwise, the reference is called {\em external}. A bag of cells is complete if and only if all references of its constituent cells are internal. + +\nxsubpoint\emb{Assigning indices to the cells from a bag of cells} +Let $c_0$, \dots, $c_{n-1}$ be the $n$ distinct cells belonging to a bag of cells~$B$. We can list these cells in some order, and then assign indices from $0$ to $n-1$, so that cell $c_i$ gets index $i$. Some options for ordering cells are: +\begin{itemize} +\item Order cells by their representation hash. Then $\Hash(c_i)<\Hash(c_j)$ whenever $i256$, let $n=2^k$ be the largest power of two less than $|s|$ (i.e., $k:=\lfloor\log_2(|s|-1)\rfloor$, $n:=2^k$). If $s'$ is the prefix of $s$ of length $n$, and $s''$ is the suffix of $s$ of length $|s|-n$, so that $s$ is the concatenation $s'.s''$ of $s'$ and $s''$, we define +\begin{equation} + \Hash_M(s):=\Sha\bigl(\Int_{64}(|s|).\Hash_M(s').\Hash_M(s'')\bigr) +\end{equation} +In other words, we concatenate the 64-bit big-endian representation of $|s|$ and the recursively computed Merkle hashes of $s'$ and $s''$, and compute $\Sha$ of the resulting string. +\end{itemize} +One can check that $\Hash_M(s)=\Hash_M(t)$ for octet strings $s$ and $t$ of length less than $2^{64}-2^{56}$ implies $s=t$ unless a hash collision for $\Sha$ has been found. + +\nxsubpoint\label{sp:blk.ser.hash}\emb{The serialization hash of a block} +The construction of~\ptref{sp:merkle.octet.str} is applied in particular to the serialization of the bag of cells representing an unsigned shardchain or masterchain block. The validators sign not only the representation hash of the unsigned block, but also the ``serialization hash'' of the unsigned block, defined as $\Hash_M$ of the serialization of the unsigned block. In this way, the validators certify that this octet string is indeed a serialization of the corresponding block. + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% +% +% bibliography +% +% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +\clearpage +\markbothsame{\textsc{References}} + +\begin{thebibliography}{2} + +\bibitem{Curve25519} + {\sc Daniel J.~Bernstein}, {\sl Curve25519: New Diffie--Hellman Speed Records} (2006), in: M.~Yung, Ye.~Dodis, A.~Kiayas et al, {\it Public Key Cryptography}, Lecture Notes in Computer Science {\bf 3958}, pp.~207--228. Available at \url{https://cr.yp.to/ecdh/curve25519-20060209.pdf}. + +\bibitem{Ed25519} + {\sc Daniel J.~Bernstein, Niels Duif, Tanja Lange et al.}, {\sl High-speed high-security signatures} (2012), {\it Journal of Cryptographic Engineering} {\bf 2} (2), pp.~77--89. Available at \url{https://ed25519.cr.yp.to/ed25519-20110926.pdf}. + +\bibitem{TON} + {\sc N.~Durov}, {\sl Telegram Open Network}, 2017. + +\bibitem{TVM} + {\sc N.~Durov}, {\sl Telegram Open Network Virtual Machine}, 2018. + +\end{thebibliography} + +\clearpage +\appendix\mysection{Elliptic curve cryptography}\label{app:ecc} +This appendix contains a formal description of the elliptic curve cryptography currently used in TON, particularly in the TON Blockchain and the TON Network. + +TON uses two forms of elliptic curve cryptography: Ed25519 is used for cryptographic Schnorr signatures, while Curve25519 is used for asymmetric cryptography. These curves are used in the standard way (as defined in the original articles \cite{Curve25519} and \cite{Ed25519} by D.~Bernstein and RFCs 7748 and 8032); however, some serialization details specific to TON must be explained. One unique adaptation of these curves for TON is that TON supports automatic conversion of Ed25519 keys into Curve25519 keys, so that the same keys can be used for signatures and for asymmetric cryptography. + +\mysubsection{Elliptic curves} +Some general facts on elliptic curves over finite fields, relevant for elliptic curve cryptography, are collected in this section. + +\nxsubpoint\emb{Finite fields} +We consider elliptic curves over finite fields. For the purposes of the Curve25519 and Ed25519 algorithms, we will be mostly concerned with elliptic curves over the finite prime field $k:=\bbF_p$ of residues modulo~$p$, where $p=2^{255}-19$ is a prime number, and over finite extensions $\bbF_q$ of $\bbF_p$, especially the quadratic extension $\bbF_{p^2}$.\footnote{Arithmetic modulo $p$ for a modulus $p$ near a power of two can be implemented very efficiently. On the other hand, residues modulo $2^{255}-19$ can be represented by 255-bit integers. This is the reason this particular value of $p$ has been chosen by D.~Bernstein.} + +\nxsubpoint\emb{Elliptic curves} +An {\em elliptic curve\/} $E=(E,O)$ over a field $k$ is a geometrically integral smooth projective curve $E/k$ of genus $g=1$, along with a marked $k$-rational point $O\in E(k)$. It is well-known that an elliptic curve $E$ over a field $k$ can be represented in (generalized) Weierstrass form: +\begin{equation}\label{eq:gen.weier.form} + y^2+a_1xy+a_3y=x^3+a_2x^2+a_4x+a_6\quad\text{for some $a_1$, \dots, $a_6\in k$.} +\end{equation} +More precisely, this is only the affine part of the elliptic curve, written in coordinates $(x,y)$. For any field extension $K$ of $k$, $E(K)$ consists of all solutions $(x,y)\in K^2$ of equation~\eqref{eq:gen.weier.form}, called {\em finite points of $E(K)$}, along with a point at infinity, which is the marked point $O$. + +\nxsubpoint\emb{Weierstrass form in homogeneous coordinates} +In homogeneous coordinates $[X:Y:Z]$, \eqref{eq:gen.weier.form} corresponds to +\begin{equation}\label{eq:h.gen.weier.form} + Y^2Z+a_1XYZ+a_3YZ^2=X^3+a_2X^2Z+a_4XZ^2+a_6Z^3 +\end{equation} +When $Z\neq0$, we can set $x:=X/Z$, $y:=Y/Z$, and obtain a solution $(x,y)$ of \eqref{eq:gen.weier.form} (i.e., a finite point of $E$). On the other hand, the only solution (up to proportionality) of \eqref{eq:h.gen.weier.form} with $Z=0$ is $[0:1:0]$; this is the point at infinity~$O$. + +\nxsubpoint\emb{Standard Weierstrass form} +When the characteristic $\charact k$ of field $k$ is $\neq2$, $3$, the Weierstrass form of \eqref{eq:gen.weier.form} or \eqref{eq:h.gen.weier.form} can be simplified with the aid of linear transformations $y':=y+a_1x/2+a_3/2$, $x':=x+a_2/3$, thus making $a_1=a_3=a_2=0$ and obtaining +\begin{equation}\label{eq:weier.form} + y^2=x^3+a_4x+a_6 +\end{equation} +and +\begin{equation}\label{eq:h.weier.form} + Y^2Z=X^3+a_4XZ^2+a_6Z^3 +\end{equation} +Such an equation defines an elliptic curve if and only if the cubic polynomial $P(x):=x^3+a_4x+a_6$ has no multiple roots, i.e., if the discriminant $D:=-4a_4^3-27a_6^2$ is non-zero. + +\nxsubpoint\emb{Addition of points on elliptic curve~$E$} +Let $K$ be a field extension of field~$k$, and let $E=(E,O)$ be any elliptic curve in Weierstrass form defined over~$k$. Then any line $l\subset\bbP^2_K$ intersects the elliptic curve $E_{(K)}$ (which is the base change of curve $E$ to field $K$, i.e., the curve defined by the same equations over a larger field $K$) at exactly three points $P$, $Q$, $R$, considered with multiplicities. We define the {\em addition of points\/} on elliptic curve $E$ (or rather the addition of its $K$-valued points~$E(K)$) by requiring that +\begin{equation} + P+Q+R=O\quad\text{whenever $\{P,Q,R\}=l\cap E$ for some line $l\subset\bbP^2_K$.} +\end{equation} +It is well-known that this requirement defines a unique commutative law $[+]:E\times_k E\to E$ on the points of the elliptic curve $E$, having $O$ as its neutral element. When elliptic curve~$E$ is represented by its Weierstrass form~\eqref{eq:gen.weier.form}, one can write explicit formulas expressing the coordinates $x_{P+Q}$, $y_{P+Q}$ of the sum $P+Q$ of two $K$-valued points $P$, $Q\in E(K)$ of elliptic curve $E$ as rational functions of the coordinates $x_P$, $y_P$, $x_Q$, $y_Q\in K$ of points $P$ and $Q$ and of the coefficients $a_i\in k$ of~\eqref{eq:gen.weier.form}. + +\nxsubpoint\emb{Power maps} +Since $E(K)$ is an abelian group, one can define {\em multiples\/} or {\em powers\/} $[n]X$ for any point $X\in E(K)$ and any integer $n\in\bbZ$. If $n=0$, then $[0]X=O$; if $n>0$, then $[n]X=[n-1]X+X$; if $n<0$, then $[n]X=-[-n]X$. The map $[n]=[n]_E:E\to E$ for $n\neq0$ is an {\em isogeny}, meaning that it is a non-constant homomorphism for the group law of $E$: +\begin{equation} + [n](P+Q)=[n]P+[n]Q\quad\text{for any $P$, $Q\in E(K)$ and $n\in\bbZ$.} +\end{equation} +In particular, $[-1]_E:E\to E$, $P\mapsto -P$, is an involutive automorphism of elliptic curve~$E$. If $E$ is in Weierstrass form, $[-1]_E$ maps $(x,y)\mapsto(x,-y)$, and two points $P$, $Q\in E(\bbF_q)$ have equal $x$-coordinates if and only if $Q=\pm P$. + +\nxsubpoint\emb{The order of the group of rational points of~$E$} +Let $E$ be an elliptic curve defined over a finite base field $k$, and let $K=\bbF_q$ be a finite extension of~$k$. Then $E(\bbF_q)$ is a finite abelian group. By a well-known result of Hasse, the order $n$ of this group is not too distant from $q$: +\begin{equation}\label{eq:weil.ec} + n=|E(\bbF_q)|=q-t+1\quad\text{where $t^2\leq 4q$, i.e., $|t|\leq2\sqrt{q}$.} +\end{equation} +We will be mostly interested in the case $K=k=\bbF_p$, with $q=p$ a prime number. + +\nxsubpoint\emb{Cyclic subgroups of large prime order} +Elliptic curve cryptography is usually performed using elliptic curves that admit a (necessarily cyclic) subgroup $C\subset E(\bbF_q)$ of prime order $\ell$. Equivalently, a rational point $G\in E(\bbF_q)$ of prime order $\ell$ may be given; then $C$ can be recovered as the cyclic subgroup $\langle G\rangle$ generated by~$G$. In order to verify that a point $G\in E(\bbF_q)$ generates a cyclic group of prime order~$\ell$, one can check that $G\neq O$, but $[\ell]G=O$. + +By the Legendre theorem, $\ell$ is necessarily a divisor of the order $n=|E(\bbF_q)|$ of the finite abelian group~$E(\bbF_q)$: +\begin{equation} + n=|E(\bbF_q)|=c\ell\quad\text{for some integer $c\geq1$} +\end{equation} +The integer $c$ is called the {\em cofactor}; one usually wants the cofactor to be as small as possible, so as to make $\ell=n/c$ as large as possible. Recall that $n$ always has the same order of magnitude as $q$ by~\eqref{eq:weil.ec}, so it cannot be changed much by varying $E$ once $q$ is fixed. + +\nxsubpoint\label{sp:ecc.data}\emb{Data for elliptic curve cryptography} +In order to define specific elliptic curve cryptography, one must fix +a finite base field $\bbF_q$ (if $q=p$ is a prime, it is sufficient to fix prime~$p$), an elliptic curve $E/\bbF_q$ (usually represented by the coefficients of its Weierstrass form \eqref{eq:weier.form} or~\eqref{eq:gen.weier.form}), the base point $O$ (which usually is the point at infinity of an elliptic curve written in Weierstrass form), and the generator $G\in E(\bbF_q)$ (usually determined by its coordinates $(x,y)$ with respect to the equation of the elliptic curve) of a cyclic subgroup of large prime order~$\ell$. Prime number $\ell$ and the cofactor $c$ are usually also part of the elliptic cryptography data. + +\nxsubpoint\emb{Main operations of elliptic curve cryptography} +Elliptic curve cryptography usually deals with a fixed cyclic subgroup $C$ of a large prime order $\ell$ inside the group of points of an elliptic curve $E$ over a finite field~$\bbF_q$. A generator $G$ of $C$ is usually fixed. It is usually assumed that, given a point $X$ of $C$, one cannot find its ``discrete logarithm base $G$'' (i.e., a residue $n$ modulo $\ell$ such that $X=[n]G$) faster than in $O(\sqrt\ell)$ operations. The most important operations used in elliptic curve cryptography are the addition of points from $C\subset E(\bbF_q)$ and the computation of their powers, or multiples. + +\nxsubpoint\emb{Private and public keys for elliptic curve cryptography} +Usually a private key for elliptic curve cryptography described by the data listed in~\ptref{sp:ecc.data} is a ``random'' integer $0

<5G2M1WZP5 zzqSuO91lJod(SBDDw<*sX(+F6Uq~YAeYV#2A;XQu_p=N5X+#cmu19Qk>QAnV=k!?wbk5I;tDWgFc}0NkvC*G=V+Yh1cyeJVq~9czZiDXe+S=VfL2g`LWo8om z$Y~FQc6MFjV-t1Y`^D9XMwY*U_re2R?&(O~68T&D4S{X`6JYU-pz=}ew-)V0AOUT1 zVOkHAB-8uBcRjLvz<9HS#a@X*Kc@|W)nyiSgi|u5$Md|P()%2(?olGg@ypoJwp6>m z*dnfjjWC>?_1p;%1brqZyDRR;8EntVA92EJ3ByOxj6a+bhPl z;a?m4rQAV1@QU^#M1HX)0+}A<7TCO`ZR_RzF}X9-M>cRLyN4C+lCk2)kT^3gN^`IT zNP~fAm(wyIoR+l^lQDA(e1Yv}&$I!n?&*p6?lZcQ+vGLLd~fM)qt}wsbf3r=tmVYe zl)ntf#E!P7wlakP9MXS7m0nsAmqxZ*)#j;M&0De`oNmFgi$ov#!`6^4)iQyxg5Iuj zjLAhzQ)r`^hf7`*1`Rh`X;LVBtDSz@0T?kkT1o!ijeyTGt5vc^Cd*tmNgiNo^EaWvaC8$e+nb_{W01j3%=1Y&92YacjCi>eNbwk%-gPQ@H-+4xskQ}f_c=jg^S-# zYFBDf)2?@5cy@^@FHK5$YdAK9cI;!?Jgd}25lOW%xbCJ>By3=HiK@1EM+I46A)Lsd zeT|ZH;KlCml=@;5+hfYf>QNOr^XNH%J-lvev)$Omy8MZ`!{`j>(J5cG&ZXXgv)TaF zg;cz99i$4CX_@3MIb?GL0s*8J=3`#P(jXF(_(6DXZjc@(@h&=M&JG)9&Te1?(^XMW zjjC_70|b=9hB6pKQi`S^Ls7JyJw^@P>Ko^&q8F&?>6i;#CbxUiLz1ZH4lNyd@QACd zu>{!sqjB!2Dg}pbAXD>d!3jW}=5aN0b;rw*W>*PAxm7D)aw(c*RX2@bTGEI|RRp}vw7;NR2wa;rXN{L{Q#=Fa z$x@ms6pqb>!8AuV(prv>|aU8oWV={C&$c zMa=p=CDNOC2tISZcd8~18GN5oTbKY+Vrq;3_obJlfSKRMk;Hdp1`y`&LNSOqeauR_ z^j*Ojl3Ohzb5-a49A8s|UnM*NM8tg}BJXdci5%h&;$afbmRpN0&~9rCnBA`#lG!p zc{(9Y?A0Y9yo?wSYn>iigf~KP$0*@bGZ>*YM4&D;@{<%Gg5^uUJGRrV4 z(aZOGB&{_0f*O=Oi0k{@8vN^BU>s3jJRS&CJOl3o|BE{FAA&a#2YYiX3pZz@|Go-F z|Fly;7eX2OTs>R}<`4RwpHFs9nwh)B28*o5qK1Ge=_^w0m`uJOv!=&!tzt#Save(C zgKU=Bsgql|`ui(e1KVxR`?>Dx>(rD1$iWp&m`v)3A!j5(6vBm*z|aKm*T*)mo(W;R zNGo2`KM!^SS7+*9YxTm6YMm_oSrLceqN*nDOAtagULuZl5Q<7mOnB@Hq&P|#9y{5B z!2x+2s<%Cv2Aa0+u{bjZXS);#IFPk(Ph-K7K?3i|4ro> zRbqJoiOEYo(Im^((r}U4b8nvo_>4<`)ut`24?ILnglT;Pd&U}$lV3U$F9#PD(O=yV zgNNA=GW|(E=&m_1;uaNmipQe?pon4{T=zK!N!2_CJL0E*R^XXIKf*wi!>@l}3_P9Z zF~JyMbW!+n-+>!u=A1ESxzkJy$DRuG+$oioG7(@Et|xVbJ#BCt;J43Nvj@MKvTxzy zMmjNuc#LXBxFAwIGZJk~^!q$*`FME}yKE8d1f5Mp}KHNq(@=Z8YxV}0@;YS~|SpGg$_jG7>_8WWYcVx#4SxpzlV9N4aO>K{c z$P?a_fyDzGX$Of3@ykvedGd<@-R;M^Shlj*SswJLD+j@hi_&_>6WZ}#AYLR0iWMK|A zH_NBeu(tMyG=6VO-=Pb>-Q#$F*or}KmEGg*-n?vWQREURdB#+6AvOj*I%!R-4E_2$ zU5n9m>RWs|Wr;h2DaO&mFBdDb-Z{APGQx$(L`if?C|njd*fC=rTS%{o69U|meRvu?N;Z|Y zbT|ojL>j;q*?xXmnHH#3R4O-59NV1j=uapkK7}6@Wo*^Nd#(;$iuGsb;H315xh3pl zHaJ>h-_$hdNl{+|Zb%DZH%ES;*P*v0#}g|vrKm9;j-9e1M4qX@zkl&5OiwnCz=tb6 zz<6HXD+rGIVpGtkb{Q^LIgExOm zz?I|oO9)!BOLW#krLmWvX5(k!h{i>ots*EhpvAE;06K|u_c~y{#b|UxQ*O@Ks=bca z^_F0a@61j3I(Ziv{xLb8AXQj3;R{f_l6a#H5ukg5rxwF9A$?Qp-Mo54`N-SKc}fWp z0T)-L@V$$&my;l#Ha{O@!fK4-FSA)L&3<${Hcwa7ue`=f&YsXY(NgeDU#sRlT3+9J z6;(^(sjSK@3?oMo$%L-nqy*E;3pb0nZLx6 z;h5)T$y8GXK1DS-F@bGun8|J(v-9o=42&nLJy#}M5D0T^5VWBNn$RpC zZzG6Bt66VY4_?W=PX$DMpKAI!d`INr) zkMB{XPQ<52rvWVQqgI0OL_NWxoe`xxw&X8yVftdODPj5|t}S6*VMqN$-h9)1MBe0N zYq?g0+e8fJCoAksr0af1)FYtz?Me!Cxn`gUx&|T;)695GG6HF7!Kg1zzRf_{VWv^bo81v4$?F6u2g|wxHc6eJQAg&V z#%0DnWm2Rmu71rPJ8#xFUNFC*V{+N_qqFH@gYRLZ6C?GAcVRi>^n3zQxORPG)$-B~ z%_oB?-%Zf7d*Fe;cf%tQwcGv2S?rD$Z&>QC2X^vwYjnr5pa5u#38cHCt4G3|efuci z@3z=#A13`+ztmp;%zjXwPY_aq-;isu*hecWWX_=Z8paSqq7;XYnUjK*T>c4~PR4W7 z#C*%_H&tfGx`Y$w7`dXvVhmovDnT>btmy~SLf>>~84jkoQ%cv=MMb+a{JV&t0+1`I z32g_Y@yDhKe|K^PevP~MiiVl{Ou7^Mt9{lOnXEQ`xY^6L8D$705GON{!1?1&YJEl#fTf5Z)da=yiEQ zGgtC-soFGOEBEB~ZF_{7b(76En>d}mI~XIwNw{e>=Fv)sgcw@qOsykWr?+qAOZSVrQfg}TNI ztKNG)1SRrAt6#Q?(me%)>&A_^DM`pL>J{2xu>xa$3d@90xR61TQDl@fu%_85DuUUA za9tn64?At;{`BAW6oykwntxHeDpXsV#{tmt5RqdN7LtcF4vR~_kZNT|wqyR#z^Xcd zFdymVRZvyLfTpBT>w9<)Ozv@;Yk@dOSVWbbtm^y@@C>?flP^EgQPAwsy75bveo=}T zFxl(f)s)j(0#N_>Or(xEuV(n$M+`#;Pc$1@OjXEJZumkaekVqgP_i}p`oTx;terTx zZpT+0dpUya2hqlf`SpXN{}>PfhajNk_J0`H|2<5E;U5Vh4F8er z;RxLSFgpGhkU>W?IwdW~NZTyOBrQ84H7_?gviIf71l`EETodG9a1!8e{jW?DpwjL? zGEM&eCzwoZt^P*8KHZ$B<%{I}>46IT%jJ3AnnB5P%D2E2Z_ z1M!vr#8r}1|KTqWA4%67ZdbMW2YJ81b(KF&SQ2L1Qn(y-=J${p?xLMx3W7*MK;LFQ z6Z`aU;;mTL4XrrE;HY*Rkh6N%?qviUGNAKiCB~!P}Z->IpO6E(gGd7I#eDuT7j|?nZ zK}I(EJ>$Kb&@338M~O+em9(L!+=0zBR;JAQesx|3?Ok90)D1aS9P?yTh6Poh8Cr4X zk3zc=f2rE7jj+aP7nUsr@~?^EGP>Q>h#NHS?F{Cn`g-gD<8F&dqOh-0sa%pfL`b+1 zUsF*4a~)KGb4te&K0}bE>z3yb8% zibb5Q%Sfiv7feb1r0tfmiMv z@^4XYwg@KZI=;`wC)`1jUA9Kv{HKe2t$WmRcR4y8)VAFjRi zaz&O7Y2tDmc5+SX(bj6yGHYk$dBkWc96u3u&F)2yEE~*i0F%t9Kg^L6MJSb&?wrXi zGSc;_rln$!^ybwYBeacEFRsVGq-&4uC{F)*Y;<0y7~USXswMo>j4?~5%Zm!m@i@-> zXzi82sa-vpU{6MFRktJy+E0j#w`f`>Lbog{zP|9~hg(r{RCa!uGe>Yl536cn$;ouH za#@8XMvS-kddc1`!1LVq;h57~zV`7IYR}pp3u!JtE6Q67 zq3H9ZUcWPm2V4IukS}MCHSdF0qg2@~ufNx9+VMjQP&exiG_u9TZAeAEj*jw($G)zL zq9%#v{wVyOAC4A~AF=dPX|M}MZV)s(qI9@aIK?Pe+~ch|>QYb+78lDF*Nxz2-vpRbtQ*F4$0fDbvNM#CCatgQ@z1+EZWrt z2dZfywXkiW=no5jus-92>gXn5rFQ-COvKyegmL=4+NPzw6o@a?wGE-1Bt;pCHe;34K%Z z-FnOb%!nH;)gX+!a3nCk?5(f1HaWZBMmmC@lc({dUah+E;NOros{?ui1zPC-Q0);w zEbJmdE$oU$AVGQPdm{?xxI_0CKNG$LbY*i?YRQ$(&;NiA#h@DCxC(U@AJ$Yt}}^xt-EC_ z4!;QlLkjvSOhdx!bR~W|Ezmuf6A#@T`2tsjkr>TvW*lFCMY>Na_v8+{Y|=MCu1P8y z89vPiH5+CKcG-5lzk0oY>~aJC_0+4rS@c@ZVKLAp`G-sJB$$)^4*A!B zmcf}lIw|VxV9NSoJ8Ag3CwN&d7`|@>&B|l9G8tXT^BDHOUPrtC70NgwN4${$k~d_4 zJ@eo6%YQnOgq$th?0{h`KnqYa$Nz@vlHw<%!C5du6<*j1nwquk=uY}B8r7f|lY+v7 zm|JU$US08ugor8E$h3wH$c&i~;guC|3-tqJy#T;v(g( zBZtPMSyv%jzf->435yM(-UfyHq_D=6;ouL4!ZoD+xI5uCM5ay2m)RPmm$I}h>()hS zO!0gzMxc`BPkUZ)WXaXam%1;)gedA7SM8~8yIy@6TPg!hR0=T>4$Zxd)j&P-pXeSF z9W`lg6@~YDhd19B9ETv(%er^Xp8Yj@AuFVR_8t*KS;6VHkEDKI#!@l!l3v6`W1`1~ zP{C@keuV4Q`Rjc08lx?zmT$e$!3esc9&$XZf4nRL(Z*@keUbk!GZi(2Bmyq*saOD? z3Q$V<*P-X1p2}aQmuMw9nSMbOzuASsxten7DKd6A@ftZ=NhJ(0IM|Jr<91uAul4JR zADqY^AOVT3a(NIxg|U;fyc#ZnSzw2cr}#a5lZ38>nP{05D)7~ad7JPhw!LqOwATXtRhK!w0X4HgS1i<%AxbFmGJx9?sEURV+S{k~g zGYF$IWSlQonq6}e;B(X(sIH|;52+(LYW}v_gBcp|x%rEAVB`5LXg_d5{Q5tMDu0_2 z|LOm$@K2?lrLNF=mr%YP|U-t)~9bqd+wHb4KuPmNK<}PK6e@aosGZK57=Zt+kcszVOSbe;`E^dN! ze7`ha3WUUU7(nS0{?@!}{0+-VO4A{7+nL~UOPW9_P(6^GL0h${SLtqG!} zKl~Ng5#@Sy?65wk9z*3SA`Dpd4b4T^@C8Fhd8O)k_4%0RZL5?#b~jmgU+0|DB%0Z) zql-cPC>A9HPjdOTpPC` zQwvF}uB5kG$Xr4XnaH#ruSjM*xG?_hT7y3G+8Ox`flzU^QIgb_>2&-f+XB6MDr-na zSi#S+c!ToK84<&m6sCiGTd^8pNdXo+$3^l3FL_E`0 z>8it5YIDxtTp2Tm(?}FX^w{fbfgh7>^8mtvN>9fWgFN_*a1P`Gz*dyOZF{OV7BC#j zQV=FQM5m>47xXgapI$WbPM5V`V<7J9tD)oz@d~MDoM`R^Y6-Na(lO~uvZlpu?;zw6 zVO1faor3dg#JEb5Q*gz4<W8tgC3nE2BG2jeIQs1)<{In&7hJ39x=;ih;CJDy)>0S1at*7n?Wr0ahYCpFjZ|@u91Zl7( zv;CSBRC65-6f+*JPf4p1UZ)k=XivKTX6_bWT~7V#rq0Xjas6hMO!HJN8GdpBKg_$B zwDHJF6;z?h<;GXFZan8W{XFNPpOj!(&I1`&kWO86p?Xz`a$`7qV7Xqev|7nn_lQuX ziGpU1MMYt&5dE2A62iX3;*0WzNB9*nSTzI%62A+N?f?;S>N@8M=|ef3gtQTIA*=yq zQAAjOqa!CkHOQo4?TsqrrsJLclXcP?dlAVv?v`}YUjo1Htt;6djP@NPFH+&p1I+f_ z)Y279{7OWomY8baT(4TAOlz1OyD{4P?(DGv3XyJTA2IXe=kqD)^h(@*E3{I~w;ws8 z)ZWv7E)pbEM zd3MOXRH3mQhks9 zv6{s;k0y5vrcjXaVfw8^>YyPo=oIqd5IGI{)+TZq5Z5O&hXAw%ZlL}^6FugH;-%vP zAaKFtt3i^ag226=f0YjzdPn6|4(C2sC5wHFX{7QF!tG1E-JFA`>eZ`}$ymcRJK?0c zN363o{&ir)QySOFY0vcu6)kX#;l??|7o{HBDVJN+17rt|w3;(C_1b>d;g9Gp=8YVl zYTtA52@!7AUEkTm@P&h#eg+F*lR zQ7iotZTcMR1frJ0*V@Hw__~CL>_~2H2cCtuzYIUD24=Cv!1j6s{QS!v=PzwQ(a0HS zBKx04KA}-Ue+%9d`?PG*hIij@54RDSQpA7|>qYVIrK_G6%6;#ZkR}NjUgmGju)2F`>|WJoljo)DJgZr4eo1k1i1+o z1D{>^RlpIY8OUaOEf5EBu%a&~c5aWnqM zxBpJq98f=%M^{4mm~5`CWl%)nFR64U{(chmST&2jp+-r z3675V<;Qi-kJud%oWnCLdaU-)xTnMM%rx%Jw6v@=J|Ir=4n-1Z23r-EVf91CGMGNz zb~wyv4V{H-hkr3j3WbGnComiqmS0vn?n?5v2`Vi>{Ip3OZUEPN7N8XeUtF)Ry6>y> zvn0BTLCiqGroFu|m2zG-;Xb6;W`UyLw)@v}H&(M}XCEVXZQoWF=Ykr5lX3XWwyNyF z#jHv)A*L~2BZ4lX?AlN3X#axMwOC)PoVy^6lCGse9bkGjb=qz%kDa6}MOmSwK`cVO zt(e*MW-x}XtU?GY5}9{MKhRhYOlLhJE5=ca+-RmO04^ z66z{40J=s=ey9OCdc(RCzy zd7Zr1%!y3}MG(D=wM_ebhXnJ@MLi7cImDkhm0y{d-Vm81j`0mbi4lF=eirlr)oW~a zCd?26&j^m4AeXEsIUXiTal)+SPM4)HX%%YWF1?(FV47BaA`h9m67S9x>hWMVHx~Hg z1meUYoLL(p@b3?x|9DgWeI|AJ`Ia84*P{Mb%H$ZRROouR4wZhOPX15=KiBMHl!^JnCt$Az`KiH^_d>cev&f zaG2>cWf$=A@&GP~DubsgYb|L~o)cn5h%2`i^!2)bzOTw2UR!>q5^r&2Vy}JaWFUQE04v>2;Z@ZPwXr?y&G(B^@&y zsd6kC=hHdKV>!NDLIj+3rgZJ|dF`%N$DNd;B)9BbiT9Ju^Wt%%u}SvfM^=|q-nxDG zuWCQG9e#~Q5cyf8@y76#kkR^}{c<_KnZ0QsZcAT|YLRo~&tU|N@BjxOuy`#>`X~Q< z?R?-Gsk$$!oo(BveQLlUrcL#eirhgBLh`qHEMg`+sR1`A=1QX7)ZLMRT+GBy?&mM8 zQG^z-!Oa&J-k7I(3_2#Q6Bg=NX<|@X&+YMIOzfEO2$6Mnh}YV!m!e^__{W@-CTprr zbdh3f=BeCD$gHwCrmwgM3LAv3!Mh$wM)~KWzp^w)Cu6roO7uUG5z*}i0_0j47}pK; ztN530`ScGatLOL06~zO)Qmuv`h!gq5l#wx(EliKe&rz-5qH(hb1*fB#B+q`9=jLp@ zOa2)>JTl7ovxMbrif`Xe9;+fqB1K#l=Dv!iT;xF zdkCvS>C5q|O;}ns3AgoE({Ua-zNT-9_5|P0iANmC6O76Sq_(AN?UeEQJ>#b54fi3k zFmh+P%b1x3^)0M;QxXLP!BZ^h|AhOde*{9A=f3|Xq*JAs^Y{eViF|=EBfS6L%k4ip zk+7M$gEKI3?bQg?H3zaE@;cyv9kv;cqK$VxQbFEsy^iM{XXW0@2|DOu$!-k zSFl}Y=jt-VaT>Cx*KQnHTyXt}f9XswFB9ibYh+k2J!ofO+nD?1iw@mwtrqI4_i?nE zhLkPp41ED62me}J<`3RN80#vjW;wt`pP?%oQ!oqy7`miL>d-35a=qotK$p{IzeSk# ze_$CFYp_zIkrPFVaW^s#U4xT1lI^A0IBe~Y<4uS%zSV=wcuLr%gQT=&5$&K*bwqx| zWzCMiz>7t^Et@9CRUm9E+@hy~sBpm9fri$sE1zgLU((1?Yg{N1Sars=DiW&~Zw=3I zi7y)&oTC?UWD2w97xQ&5vx zRXEBGeJ(I?Y}eR0_O{$~)bMJRTsNUPIfR!xU9PE7A>AMNr_wbrFK>&vVw=Y;RH zO$mlpmMsQ}-FQ2cSj7s7GpC+~^Q~dC?y>M}%!-3kq(F3hGWo9B-Gn02AwUgJ>Z-pKOaj zysJBQx{1>Va=*e@sLb2z&RmQ7ira;aBijM-xQ&cpR>X3wP^foXM~u1>sv9xOjzZpX z0K;EGouSYD~oQ&lAafj3~EaXfFShC+>VsRlEMa9cg9i zFxhCKO}K0ax6g4@DEA?dg{mo>s+~RPI^ybb^u--^nTF>**0l5R9pocwB?_K)BG_)S zyLb&k%XZhBVr7U$wlhMqwL)_r&&n%*N$}~qijbkfM|dIWP{MyLx}X&}ES?}7i;9bW zmTVK@zR)7kE2+L42Q`n4m0VVg5l5(W`SC9HsfrLZ=v%lpef=Gj)W59VTLe+Z$8T8i z4V%5+T0t8LnM&H>Rsm5C%qpWBFqgTwL{=_4mE{S3EnBXknM&u8n}A^IIM4$s3m(Rd z>zq=CP-!9p9es2C*)_hoL@tDYABn+o#*l;6@7;knWIyDrt5EuakO99S$}n((Fj4y} zD!VvuRzghcE{!s;jC*<_H$y6!6QpePo2A3ZbX*ZzRnQq*b%KK^NF^z96CHaWmzU@f z#j;y?X=UP&+YS3kZx7;{ zDA{9(wfz7GF`1A6iB6fnXu0?&d|^p|6)%3$aG0Uor~8o? z*e}u#qz7Ri?8Uxp4m_u{a@%bztvz-BzewR6bh*1Xp+G=tQGpcy|4V_&*aOqu|32CM zz3r*E8o8SNea2hYJpLQ-_}R&M9^%@AMx&`1H8aDx4j%-gE+baf2+9zI*+Pmt+v{39 zDZ3Ix_vPYSc;Y;yn68kW4CG>PE5RoaV0n@#eVmk?p$u&Fy&KDTy!f^Hy6&^-H*)#u zdrSCTJPJw?(hLf56%2;_3n|ujUSJOU8VPOTlDULwt0jS@j^t1WS z!n7dZIoT+|O9hFUUMbID4Ec$!cc($DuQWkocVRcYSikFeM&RZ=?BW)mG4?fh#)KVG zcJ!<=-8{&MdE)+}?C8s{k@l49I|Zwswy^ZN3;E!FKyglY~Aq?4m74P-0)sMTGXqd5(S<-(DjjM z&7dL-Mr8jhUCAG$5^mI<|%`;JI5FVUnNj!VO2?Jiqa|c2;4^n!R z`5KK0hyB*F4w%cJ@Un6GC{mY&r%g`OX|1w2$B7wxu97%<@~9>NlXYd9RMF2UM>(z0 zouu4*+u+1*k;+nFPk%ly!nuMBgH4sL5Z`@Rok&?Ef=JrTmvBAS1h?C0)ty5+yEFRz zY$G=coQtNmT@1O5uk#_MQM1&bPPnspy5#>=_7%WcEL*n$;sSAZcXxMpcXxLe;_mLA z5F_paad+bGZV*oh@8h0(|D2P!q# zTHjmiphJ=AazSeKQPkGOR-D8``LjzToyx{lfK-1CDD6M7?pMZOdLKFtjZaZMPk4}k zW)97Fh(Z+_Fqv(Q_CMH-YYi?fR5fBnz7KOt0*t^cxmDoIokc=+`o# zrud|^h_?KW=Gv%byo~(Ln@({?3gnd?DUf-j2J}|$Mk>mOB+1{ZQ8HgY#SA8END(Zw z3T+W)a&;OO54~m}ffemh^oZ!Vv;!O&yhL0~hs(p^(Yv=(3c+PzPXlS5W79Er8B1o* z`c`NyS{Zj_mKChj+q=w)B}K za*zzPhs?c^`EQ;keH{-OXdXJet1EsQ)7;{3eF!-t^4_Srg4(Ot7M*E~91gwnfhqaM zNR7dFaWm7MlDYWS*m}CH${o?+YgHiPC|4?X?`vV+ws&Hf1ZO-w@OGG^o4|`b{bLZj z&9l=aA-Y(L11!EvRjc3Zpxk7lc@yH1e$a}8$_-r$)5++`_eUr1+dTb@ zU~2P1HM#W8qiNN3b*=f+FfG1!rFxnNlGx{15}BTIHgxO>Cq4 z;#9H9YjH%>Z2frJDJ8=xq>Z@H%GxXosS@Z>cY9ppF+)e~t_hWXYlrO6)0p7NBMa`+ z^L>-#GTh;k_XnE)Cgy|0Dw;(c0* zSzW14ZXozu)|I@5mRFF1eO%JM=f~R1dkNpZM+Jh(?&Zje3NgM{2ezg1N`AQg5%+3Y z64PZ0rPq6;_)Pj-hyIOgH_Gh`1$j1!jhml7ksHA1`CH3FDKiHLz+~=^u@kUM{ilI5 z^FPiJ7mSrzBs9{HXi2{sFhl5AyqwUnU{sPcUD{3+l-ZHAQ)C;c$=g1bdoxeG(5N01 zZy=t8i{*w9m?Y>V;uE&Uy~iY{pY4AV3_N;RL_jT_QtLFx^KjcUy~q9KcLE3$QJ{!)@$@En{UGG7&}lc*5Kuc^780;7Bj;)X?1CSy*^^ zPP^M)Pr5R>mvp3_hmCtS?5;W^e@5BjE>Cs<`lHDxj<|gtOK4De?Sf0YuK5GX9G93i zMYB{8X|hw|T6HqCf7Cv&r8A$S@AcgG1cF&iJ5=%+x;3yB`!lQ}2Hr(DE8=LuNb~Vs z=FO&2pdc16nD$1QL7j+!U^XWTI?2qQKt3H8=beVTdHHa9=MiJ&tM1RRQ-=+vy!~iz zj3O{pyRhCQ+b(>jC*H)J)%Wq}p>;?@W*Eut@P&?VU+Sdw^4kE8lvX|6czf{l*~L;J zFm*V~UC;3oQY(ytD|D*%*uVrBB}BbAfjK&%S;z;7$w68(8PV_whC~yvkZmX)xD^s6 z{$1Q}q;99W?*YkD2*;)tRCS{q2s@JzlO~<8x9}X<0?hCD5vpydvOw#Z$2;$@cZkYrp83J0PsS~!CFtY%BP=yxG?<@#{7%2sy zOc&^FJxsUYN36kSY)d7W=*1-{7ghPAQAXwT7z+NlESlkUH&8ODlpc8iC*iQ^MAe(B z?*xO4i{zFz^G=^G#9MsLKIN64rRJykiuIVX5~0#vAyDWc9-=6BDNT_aggS2G{B>dD ze-B%d3b6iCfc5{@yz$>=@1kdK^tX9qh0=ocv@9$ai``a_ofxT=>X7_Y0`X}a^M?d# z%EG)4@`^Ej_=%0_J-{ga!gFtji_byY&Vk@T1c|ucNAr(JNr@)nCWj?QnCyvXg&?FW;S-VOmNL6^km_dqiVjJuIASVGSFEos@EVF7St$WE&Z%)`Q##+0 zjaZ=JI1G@0!?l|^+-ZrNd$WrHBi)DA0-Eke>dp=_XpV<%CO_Wf5kQx}5e<90dt>8k zAi00d0rQ821nA>B4JHN7U8Zz=0;9&U6LOTKOaC1FC8GgO&kc=_wHIOGycL@c*$`ce703t%>S}mvxEnD-V!;6c`2(p74V7D0No1Xxt`urE66$0(ThaAZ1YVG#QP$ zy~NN%kB*zhZ2Y!kjn826pw4bh)75*e!dse+2Db(;bN34Uq7bLpr47XTX{8UEeC?2i z*{$`3dP}32${8pF$!$2Vq^gY|#w+VA_|o(oWmQX8^iw#n_crb(K3{69*iU?<%C-%H zuKi)3M1BhJ@3VW>JA`M>L~5*_bxH@Euy@niFrI$82C1}fwR$p2E&ZYnu?jlS}u7W9AyfdXh2pM>78bIt3 z)JBh&XE@zA!kyCDfvZ1qN^np20c1u#%P6;6tU&dx0phT1l=(mw7`u!-0e=PxEjDds z9E}{E!7f9>jaCQhw)&2TtG-qiD)lD(4jQ!q{`x|8l&nmtHkdul# zy+CIF8lKbp9_w{;oR+jSLtTfE+B@tOd6h=QePP>rh4@~!8c;Hlg9m%%&?e`*Z?qz5-zLEWfi>`ord5uHF-s{^bexKAoMEV@9nU z^5nA{f{dW&g$)BAGfkq@r5D)jr%!Ven~Q58c!Kr;*Li#`4Bu_?BU0`Y`nVQGhNZk@ z!>Yr$+nB=`z#o2nR0)V3M7-eVLuY`z@6CT#OTUXKnxZn$fNLPv7w1y7eGE=Qv@Hey`n;`U=xEl|q@CCV^#l)s0ZfT+mUf z^(j5r4)L5i2jnHW4+!6Si3q_LdOLQi<^fu?6WdohIkn79=jf%Fs3JkeXwF(?_tcF? z?z#j6iXEd(wJy4|p6v?xNk-)iIf2oX5^^Y3q3ziw16p9C6B;{COXul%)`>nuUoM*q zzmr|NJ5n)+sF$!yH5zwp=iM1#ZR`O%L83tyog-qh1I z0%dcj{NUs?{myT~33H^(%0QOM>-$hGFeP;U$puxoJ>>o-%Lk*8X^rx1>j|LtH$*)>1C!Pv&gd16%`qw5LdOIUbkNhaBBTo}5iuE%K&ZV^ zAr_)kkeNKNYJRgjsR%vexa~&8qMrQYY}+RbZ)egRg9_$vkoyV|Nc&MH@8L)`&rpqd zXnVaI@~A;Z^c3+{x=xgdhnocA&OP6^rr@rTvCnhG6^tMox$ulw2U7NgUtW%|-5VeH z_qyd47}1?IbuKtqNbNx$HR`*+9o=8`%vM8&SIKbkX9&%TS++x z5|&6P<%=F$C?owUI`%uvUq^yW0>`>yz!|WjzsoB9dT;2Dx8iSuK%%_XPgy0dTD4kd zDXF@&O_vBVVKQq(9YTClUPM30Sk7B!v7nOyV`XC!BA;BIVwphh+c)?5VJ^(C;GoQ$ zvBxr7_p*k$T%I1ke}`U&)$uf}I_T~#3XTi53OX)PoXVgxEcLJgZG^i47U&>LY(l%_ z;9vVDEtuMCyu2fqZeez|RbbIE7@)UtJvgAcVwVZNLccswxm+*L&w`&t=ttT=sv6Aq z!HouSc-24Y9;0q$>jX<1DnnGmAsP))- z^F~o99gHZw`S&Aw7e4id6Lg7kMk-e)B~=tZ!kE7sGTOJ)8@q}np@j7&7Sy{2`D^FH zI7aX%06vKsfJ168QnCM2=l|i>{I{%@gcr>ExM0Dw{PX6ozEuqFYEt z087%MKC;wVsMV}kIiuu9Zz9~H!21d!;Cu#b;hMDIP7nw3xSX~#?5#SSjyyg+Y@xh| z%(~fv3`0j#5CA2D8!M2TrG=8{%>YFr(j)I0DYlcz(2~92?G*?DeuoadkcjmZszH5& zKI@Lis%;RPJ8mNsbrxH@?J8Y2LaVjUIhRUiO-oqjy<&{2X~*f|)YxnUc6OU&5iac= z*^0qwD~L%FKiPmlzi&~a*9sk2$u<7Al=_`Ox^o2*kEv?p`#G(p(&i|ot8}T;8KLk- zPVf_4A9R`5^e`Om2LV*cK59EshYXse&IoByj}4WZaBomoHAPKqxRKbPcD`lMBI)g- zeMRY{gFaUuecSD6q!+b5(?vAnf>c`Z(8@RJy%Ulf?W~xB1dFAjw?CjSn$ph>st5bc zUac1aD_m6{l|$#g_v6;=32(mwpveQDWhmjR7{|B=$oBhz`7_g7qNp)n20|^^op3 zSfTdWV#Q>cb{CMKlWk91^;mHap{mk)o?udk$^Q^^u@&jd zfZ;)saW6{e*yoL6#0}oVPb2!}r{pAUYtn4{P~ES9tTfC5hXZnM{HrC8^=Pof{G4%Bh#8 ze~?C9m*|fd8MK;{L^!+wMy>=f^8b&y?yr6KnTq28$pFMBW9Oy7!oV5z|VM$s-cZ{I|Xf@}-)1=$V&x7e;9v81eiTi4O5-vs?^5pCKy2l>q);!MA zS!}M48l$scB~+Umz}7NbwyTn=rqt@`YtuwiQSMvCMFk2$83k50Q>OK5&fe*xCddIm)3D0I6vBU<+!3=6?(OhkO|b4fE_-j zimOzyfBB_*7*p8AmZi~X2bgVhyPy>KyGLAnOpou~sx9)S9%r)5dE%ADs4v%fFybDa_w*0?+>PsEHTbhKK^G=pFz z@IxLTCROWiKy*)cV3y%0FwrDvf53Ob_XuA1#tHbyn%Ko!1D#sdhBo`;VC*e1YlhrC z?*y3rp86m#qI|qeo8)_xH*G4q@70aXN|SP+6MQ!fJQqo1kwO_v7zqvUfU=Gwx`CR@ zRFb*O8+54%_8tS(ADh}-hUJzE`s*8wLI>1c4b@$al)l}^%GuIXjzBK!EWFO8W`>F^ ze7y#qPS0NI7*aU)g$_ziF(1ft;2<}6Hfz10cR8P}67FD=+}MfhrpOkF3hFhQu;Q1y zu%=jJHTr;0;oC94Hi@LAF5quAQ(rJG(uo%BiRQ@8U;nhX)j0i?0SL2g-A*YeAqF>RVCBOTrn{0R27vu}_S zS>tX4!#&U4W;ikTE!eFH+PKw%p+B(MR2I%n#+m0{#?qRP_tR@zpgCb=4rcrL!F=;A zh%EIF8m6%JG+qb&mEfuFTLHSxUAZEvC-+kvZKyX~SA3Umt`k}}c!5dy?-sLIM{h@> z!2=C)@nx>`;c9DdwZ&zeUc(7t<21D7qBj!|1^Mp1eZ6)PuvHx+poKSDCSBMFF{bKy z;9*&EyKitD99N}%mK8431rvbT+^%|O|HV23{;RhmS{$5tf!bIPoH9RKps`-EtoW5h zo6H_!s)Dl}2gCeGF6>aZtah9iLuGd19^z0*OryPNt{70RvJSM<#Ox9?HxGg04}b^f zrVEPceD%)#0)v5$YDE?f`73bQ6TA6wV;b^x*u2Ofe|S}+q{s5gr&m~4qGd!wOu|cZ||#h_u=k*fB;R6&k?FoM+c&J;ISg70h!J7*xGus)ta4veTdW)S^@sU@ z4$OBS=a~@F*V0ECic;ht4@?Jw<9kpjBgHfr2FDPykCCz|v2)`JxTH55?b3IM={@DU z!^|9nVO-R#s{`VHypWyH0%cs;0GO3E;It6W@0gX6wZ%W|Dzz&O%m17pa19db(er}C zUId1a4#I+Ou8E1MU$g=zo%g7K(=0Pn$)Rk z<4T2u<0rD)*j+tcy2XvY+0 z0d2pqm4)4lDewsAGThQi{2Kc3&C=|OQF!vOd#WB_`4gG3@inh-4>BoL!&#ij8bw7? zqjFRDaQz!J-YGitV4}$*$hg`vv%N)@#UdzHFI2E<&_@0Uw@h_ZHf}7)G;_NUD3@18 zH5;EtugNT0*RXVK*by>WS>jaDDfe!A61Da=VpIK?mcp^W?!1S2oah^wowRnrYjl~`lgP-mv$?yb6{{S55CCu{R z$9;`dyf0Y>uM1=XSl_$01Lc1Iy68IosWN8Q9Op=~I(F<0+_kKfgC*JggjxNgK6 z-3gQm6;sm?J&;bYe&(dx4BEjvq}b`OT^RqF$J4enP1YkeBK#>l1@-K`ajbn05`0J?0daOtnzh@l3^=BkedW1EahZlRp;`j*CaT;-21&f2wU z+Nh-gc4I36Cw+;3UAc<%ySb`#+c@5y ze~en&bYV|kn?Cn|@fqmGxgfz}U!98$=drjAkMi`43I4R%&H0GKEgx-=7PF}y`+j>r zg&JF`jomnu2G{%QV~Gf_-1gx<3Ky=Md9Q3VnK=;;u0lyTBCuf^aUi?+1+`4lLE6ZK zT#(Bf`5rmr(tgTbIt?yA@y`(Ar=f>-aZ}T~>G32EM%XyFvhn&@PWCm#-<&ApLDCXT zD#(9m|V(OOo7PmE@`vD4$S5;+9IQm19dd zvMEU`)E1_F+0o0-z>YCWqg0u8ciIknU#{q02{~YX)gc_u;8;i233D66pf(IkTDxeN zL=4z2)?S$TV9=ORVr&AkZMl<4tTh(v;Ix1{`pPVqI3n2ci&4Dg+W|N8TBUfZ*WeLF zqCH_1Q0W&f9T$lx3CFJ$o@Lz$99 zW!G&@zFHxTaP!o#z^~xgF|(vrHz8R_r9eo;TX9}2ZyjslrtH=%6O)?1?cL&BT(Amp zTGFU1%%#xl&6sH-UIJk_PGk_McFn7=%yd6tAjm|lnmr8bE2le3I~L{0(ffo}TQjyo zHZZI{-}{E4ohYTlZaS$blB!h$Jq^Rf#(ch}@S+Ww&$b);8+>g84IJcLU%B-W?+IY& zslcZIR>+U4v3O9RFEW;8NpCM0w1ROG84=WpKxQ^R`{=0MZCubg3st z48AyJNEvyxn-jCPTlTwp4EKvyEwD3e%kpdY?^BH0!3n6Eb57_L%J1=a*3>|k68A}v zaW`*4YitylfD}ua8V)vb79)N_Ixw_mpp}yJGbNu+5YYOP9K-7nf*jA1#<^rb4#AcS zKg%zCI)7cotx}L&J8Bqo8O1b0q;B1J#B5N5Z$Zq=wX~nQFgUfAE{@u0+EnmK{1hg> zC{vMfFLD;L8b4L+B51&LCm|scVLPe6h02rws@kGv@R+#IqE8>Xn8i|vRq_Z`V;x6F zNeot$1Zsu`lLS92QlLWF54za6vOEKGYQMdX($0JN*cjG7HP&qZ#3+bEN$8O_PfeAb z0R5;=zXac2IZ?fxu59?Nka;1lKm|;0)6|#RxkD05P5qz;*AL@ig!+f=lW5^Jbag%2 z%9@iM0ph$WFlxS!`p31t92z~TB}P-*CS+1Oo_g;7`6k(Jyj8m8U|Q3Sh7o-Icp4kV zK}%qri5>?%IPfamXIZ8pXbm-#{ytiam<{a5A+3dVP^xz!Pvirsq7Btv?*d7eYgx7q zWFxrzb3-%^lDgMc=Vl7^={=VDEKabTG?VWqOngE`Kt7hs236QKidsoeeUQ_^FzsXjprCDd@pW25rNx#6x&L6ZEpoX9Ffzv@olnH3rGOSW( zG-D|cV0Q~qJ>-L}NIyT?T-+x+wU%;+_GY{>t(l9dI%Ximm+Kmwhee;FK$%{dnF;C% zFjM2&$W68Sz#d*wtfX?*WIOXwT;P6NUw}IHdk|)fw*YnGa0rHx#paG!m=Y6GkS4VX zX`T$4eW9k1W!=q8!(#8A9h67fw))k_G)Q9~Q1e3f`aV@kbcSv7!priDUN}gX(iXTy zr$|kU0Vn%*ylmyDCO&G0Z3g>%JeEPFAW!5*H2Ydl>39w3W+gEUjL&vrRs(xGP{(ze zy7EMWF14@Qh>X>st8_029||TP0>7SG9on_xxeR2Iam3G~Em$}aGsNt$iES9zFa<3W zxtOF*!G@=PhfHO!=9pVPXMUVi30WmkPoy$02w}&6A7mF)G6-`~EVq5CwD2`9Zu`kd)52``#V zNSb`9dG~8(dooi1*-aSMf!fun7Sc`-C$-E(3BoSC$2kKrVcI!&yC*+ff2+C-@!AT_ zsvlAIV+%bRDfd{R*TMF><1&_a%@yZ0G0lg2K;F>7b+7A6pv3-S7qWIgx+Z?dt8}|S z>Qbb6x(+^aoV7FQ!Ph8|RUA6vXWQH*1$GJC+wXLXizNIc9p2yLzw9 z0=MdQ!{NnOwIICJc8!+Jp!zG}**r#E!<}&Te&}|B4q;U57$+pQI^}{qj669zMMe_I z&z0uUCqG%YwtUc8HVN7?0GHpu=bL7&{C>hcd5d(iFV{I5c~jpX&!(a{yS*4MEoYXh z*X4|Y@RVfn;piRm-C%b@{0R;aXrjBtvx^HO;6(>i*RnoG0Rtcd25BT6edxTNOgUAOjn zJ2)l{ipj8IP$KID2}*#F=M%^n&=bA0tY98@+2I+7~A&T-tw%W#3GV>GTmkHaqftl)#+E zMU*P(Rjo>8%P@_@#UNq(_L{}j(&-@1iY0TRizhiATJrnvwSH0v>lYfCI2ex^><3$q znzZgpW0JlQx?JB#0^^s-Js1}}wKh6f>(e%NrMwS`Q(FhazkZb|uyB@d%_9)_xb$6T zS*#-Bn)9gmobhAtvBmL+9H-+0_0US?g6^TOvE8f3v=z3o%NcPjOaf{5EMRnn(_z8- z$|m0D$FTU zDy;21v-#0i)9%_bZ7eo6B9@Q@&XprR&oKl4m>zIj-fiRy4Dqy@VVVs?rscG| zmzaDQ%>AQTi<^vYCmv#KOTd@l7#2VIpsj?nm_WfRZzJako`^uU%Nt3e;cU*y*|$7W zLm%fX#i_*HoUXu!NI$ey>BA<5HQB=|nRAwK!$L#n-Qz;~`zACig0PhAq#^5QS<8L2 zS3A+8%vbVMa7LOtTEM?55apt(DcWh#L}R^P2AY*c8B}Cx=6OFAdMPj1f>k3#^#+Hk z6uW1WJW&RlBRh*1DLb7mJ+KO>!t^t8hX1#_Wk`gjDio9)9IGbyCAGI4DJ~orK+YRv znjxRMtshZQHc$#Y-<-JOV6g^Cr@odj&Xw5B(FmI)*qJ9NHmIz_r{t)TxyB`L-%q5l ztzHgD;S6cw?7Atg*6E1!c6*gPRCb%t7D%z<(xm+K{%EJNiI2N0l8ud0Ch@_av_RW? zIr!nO4dL5466WslE6MsfMss7<)-S!e)2@r2o=7_W)OO`~CwklRWzHTfpB)_HYwgz=BzLhgZ9S<{nLBOwOIgJU=94uj6r!m>Xyn9>&xP+=5!zG_*yEoRgM0`aYts z^)&8(>z5C-QQ*o_s(8E4*?AX#S^0)aqB)OTyX>4BMy8h(cHjA8ji1PRlox@jB*1n? zDIfyDjzeg91Ao(;Q;KE@zei$}>EnrF6I}q&Xd=~&$WdDsyH0H7fJX|E+O~%LS*7^Q zYzZ4`pBdY{b7u72gZm6^5~O-57HwzwAz{)NvVaowo`X02tL3PpgLjwA`^i9F^vSpN zAqH3mRjG8VeJNHZ(1{%!XqC+)Z%D}58Qel{_weSEHoygT9pN@i zi=G;!Vj6XQk2tuJC>lza%ywz|`f7TIz*EN2Gdt!s199Dr4Tfd_%~fu8gXo~|ogt5Q zlEy_CXEe^BgsYM^o@L?s33WM14}7^T(kqohOX_iN@U?u;$l|rAvn{rwy>!yfZw13U zB@X9)qt&4;(C6dP?yRsoTMI!j-f1KC!<%~i1}u7yLXYn)(#a;Z6~r>hp~kfP));mi zcG%kdaB9H)z9M=H!f>kM->fTjRVOELNwh1amgKQT=I8J66kI)u_?0@$$~5f`u%;zl zC?pkr^p2Fe=J~WK%4ItSzKA+QHqJ@~m|Cduv=Q&-P8I5rQ-#G@bYH}YJr zUS(~(w|vKyU(T(*py}jTUp%I%{2!W!K(i$uvotcPjVddW z8_5HKY!oBCwGZcs-q`4Yt`Zk~>K?mcxg51wkZlX5e#B08I75F7#dgn5yf&Hrp`*%$ zQ;_Qg>TYRzBe$x=T(@WI9SC!ReSas9vDm(yslQjBJZde5z8GDU``r|N(MHcxNopGr z_}u39W_zwWDL*XYYt>#Xo!9kL#97|EAGyGBcRXtLTd59x%m=3i zL^9joWYA)HfL15l9%H?q`$mY27!<9$7GH(kxb%MV>`}hR4a?+*LH6aR{dzrX@?6X4 z3e`9L;cjqYb`cJmophbm(OX0b)!AFG?5`c#zLagzMW~o)?-!@e80lvk!p#&CD8u5_r&wp4O0zQ>y!k5U$h_K;rWGk=U)zX!#@Q%|9g*A zWx)qS1?fq6X<$mQTB$#3g;;5tHOYuAh;YKSBz%il3Ui6fPRv#v62SsrCdMRTav)Sg zTq1WOu&@v$Ey;@^+_!)cf|w_X<@RC>!=~+A1-65O0bOFYiH-)abINwZvFB;hJjL_$ z(9iScmUdMp2O$WW!520Hd0Q^Yj?DK%YgJD^ez$Z^?@9@Ab-=KgW@n8nC&88)TDC+E zlJM)L3r+ZJfZW_T$;Imq*#2<(j+FIk8ls7)WJ6CjUu#r5PoXxQs4b)mZza<8=v{o)VlLRM<9yw^0En#tXAj`Sylxvki{<1DPe^ zhjHwx^;c8tb?Vr$6ZB;$Ff$+3(*oinbwpN-#F)bTsXq@Sm?43MC#jQ~`F|twI=7oC zH4TJtu#;ngRA|Y~w5N=UfMZi?s0%ZmKUFTAye&6Y*y-%c1oD3yQ%IF2q2385Zl+=> zfz=o`Bedy|U;oxbyb^rB9ixG{Gb-{h$U0hVe`J;{ql!s_OJ_>>eoQn(G6h7+b^P48 zG<=Wg2;xGD-+d@UMZ!c;0>#3nws$9kIDkK13IfloGT@s14AY>&>>^#>`PT7GV$2Hp zN<{bN*ztlZu_%W=&3+=#3bE(mka6VoHEs~0BjZ$+=0`a@R$iaW)6>wp2w)=v2@|2d z%?34!+iOc5S@;AAC4hELWLH56RGxo4jw8MDMU0Wk2k_G}=Vo(>eRFo(g3@HjG|`H3 zm8b*dK=moM*oB<)*A$M9!!5o~4U``e)wxavm@O_R(`P|u%9^LGi(_%IF<6o;NLp*0 zKsfZ0#24GT8(G`i4UvoMh$^;kOhl?`0yNiyrC#HJH=tqOH^T_d<2Z+ zeN>Y9Zn!X4*DMCK^o75Zk2621bdmV7Rx@AX^alBG4%~;G_vUoxhfhFRlR&+3WwF^T zaL)8xPq|wCZoNT^>3J0K?e{J-kl+hu2rZI>CUv#-z&u@`hjeb+bBZ>bcciQVZ{SbW zez04s9oFEgc8Z+Kp{XFX`MVf-s&w9*dx7wLen(_@y34}Qz@&`$2+osqfxz4&d}{Ql z*g1ag00Gu+$C`0avds{Q65BfGsu9`_`dML*rX~hyWIe$T>CsPRoLIr%MTk3pJ^2zH1qub1MBzPG}PO;Wmav9w%F7?%l=xIf#LlP`! z_Nw;xBQY9anH5-c8A4mME}?{iewjz(Sq-29r{fV;Fc>fv%0!W@(+{={Xl-sJ6aMoc z)9Q+$bchoTGTyWU_oI19!)bD=IG&OImfy;VxNXoIO2hYEfO~MkE#IXTK(~?Z&!ae! zl8z{D&2PC$Q*OBC(rS~-*-GHNJ6AC$@eve>LB@Iq;jbBZj`wk4|LGogE||Ie=M5g= z9d`uYQ1^Sr_q2wmZE>w2WG)!F%^KiqyaDtIAct?}D~JP4shTJy5Bg+-(EA8aXaxbd~BKMtTf2iQ69jD1o* zZF9*S3!v-TdqwK$%&?91Sh2=e63;X0Lci@n7y3XOu2ofyL9^-I767eHESAq{m+@*r zbVDx!FQ|AjT;!bYsXv8ilQjy~Chiu&HNhFXt3R_6kMC8~ChEFqG@MWu#1Q1#=~#ix zrkHpJre_?#r=N0wv`-7cHHqU`phJX2M_^{H0~{VP79Dv{6YP)oA1&TSfKPEPZn2)G z9o{U1huZBLL;Tp_0OYw@+9z(jkrwIGdUrOhKJUbwy?WBt zlIK)*K0lQCY0qZ!$%1?3A#-S70F#YyUnmJF*`xx?aH5;gE5pe-15w)EB#nuf6B*c~ z8Z25NtY%6Wlb)bUA$w%HKs5$!Z*W?YKV-lE0@w^{4vw;J>=rn?u!rv$&eM+rpU6rc=j9>N2Op+C{D^mospMCjF2ZGhe4eADA#skp2EA26%p3Ex9wHW8l&Y@HX z$Qv)mHM}4*@M*#*ll5^hE9M^=q~eyWEai*P;4z<9ZYy!SlNE5nlc7gm;M&Q zKhKE4d*%A>^m0R?{N}y|i6i^k>^n4(wzKvlQeHq{l&JuFD~sTsdhs`(?lFK@Q{pU~ zb!M3c@*3IwN1RUOVjY5>uT+s-2QLWY z4T2>fiSn>>Fob+%B868-v9D@AfWr#M8eM6w#eAlhc#zk6jkLxGBGk`E3$!A@*am!R zy>29&ptYK6>cvP`b!syNp)Q$0UOW|-O@)8!?94GOYF_}+zlW%fCEl|Tep_zx05g6q z>tp47e-&R*hSNe{6{H!mL?+j$c^TXT{C&@T-xIaesNCl05 z9SLb@q&mSb)I{VXMaiWa3PWj=Ed!>*GwUe;^|uk=Pz$njNnfFY^MM>E?zqhf6^{}0 zx&~~dA5#}1ig~7HvOQ#;d9JZBeEQ+}-~v$at`m!(ai z$w(H&mWCC~;PQ1$%iuz3`>dWeb3_p}X>L2LK%2l59Tyc}4m0>9A!8rhoU3m>i2+hl zx?*qs*c^j}+WPs>&v1%1Ko8_ivAGIn@QK7A`hDz-Emkcgv2@wTbYhkiwX2l=xz*XG zaiNg+j4F-I>9v+LjosI-QECrtKjp&0T@xIMKVr+&)gyb4@b3y?2CA?=ooN zT#;rU86WLh(e@#mF*rk(NV-qSIZyr z$6!ZUmzD)%yO-ot`rw3rp6?*_l*@Z*IB0xn4|BGPWHNc-1ZUnNSMWmDh=EzWJRP`) zl%d%J613oXzh5;VY^XWJi{lB`f#u+ThvtP7 zq(HK<4>tw(=yzSBWtYO}XI`S1pMBe3!jFxBHIuwJ(@%zdQFi1Q_hU2eDuHqXte7Ki zOV55H2D6u#4oTfr7|u*3p75KF&jaLEDpxk!4*bhPc%mpfj)Us3XIG3 zIKMX^s^1wt8YK7Ky^UOG=w!o5e7W-<&c|fw2{;Q11vm@J{)@N3-p1U>!0~sKWHaL= zWV(0}1IIyt1p%=_-Fe5Kfzc71wg}`RDDntVZv;4!=&XXF-$48jS0Sc;eDy@Sg;+{A zFStc{dXT}kcIjMXb4F7MbX~2%i;UrBxm%qmLKb|2=?uPr00-$MEUIGR5+JG2l2Nq` zkM{{1RO_R)+8oQ6x&-^kCj)W8Z}TJjS*Wm4>hf+4#VJP)OBaDF%3pms7DclusBUw} z{ND#!*I6h85g6DzNvdAmnwWY{&+!KZM4DGzeHI?MR@+~|su0{y-5-nICz_MIT_#FE zm<5f3zlaKq!XyvY3H`9s&T};z!cK}G%;~!rpzk9-6L}4Rg7vXtKFsl}@sT#U#7)x- z7UWue5sa$R>N&b{J61&gvKcKlozH*;OjoDR+elkh|4bJ!_3AZNMOu?n9&|L>OTD78 z^i->ah_Mqc|Ev)KNDzfu1P3grBIM#%`QZqj5W{qu(HocQhjyS;UINoP`{J+DvV?|1 z_sw6Yr3z6%e7JKVDY<$P=M)dbk@~Yw9|2!Cw!io3%j92wTD!c^e9Vj+7VqXo3>u#= zv#M{HHJ=e$X5vQ>>ML?E8#UlmvJgTnb73{PSPTf*0)mcj6C z{KsfUbDK|F$E(k;ER%8HMdDi`=BfpZzP3cl5yJHu;v^o2FkHNk;cXc17tL8T!CsYI zfeZ6sw@;8ia|mY_AXjCS?kUfxdjDB28)~Tz1dGE|{VfBS9`0m2!m1yG?hR})er^pl4c@9Aq+|}ZlDaHL)K$O| z%9Jp-imI-Id0|(d5{v~w6mx)tUKfbuVD`xNt04Mry%M+jXzE>4(TBsx#&=@wT2Vh) z1yeEY&~17>0%P(eHP0HB^|7C+WJxQBTG$uyOWY@iDloRIb-Cf!p<{WQHR!422#F34 zG`v|#CJ^G}y9U*7jgTlD{D&y$Iv{6&PYG>{Ixg$pGk?lWrE#PJ8KunQC@}^6OP!|< zS;}p3to{S|uZz%kKe|;A0bL0XxPB&Q{J(9PyX`+Kr`k~r2}yP^ND{8!v7Q1&vtk& z2Y}l@J@{|2`oA%sxvM9i0V+8IXrZ4;tey)d;LZI70Kbim<4=WoTPZy=Yd|34v#$Kh zx|#YJ8s`J>W&jt#GcMpx84w2Z3ur-rK7gf-p5cE)=w1R2*|0mj12hvapuUWM0b~dG zMg9p8FmAZI@i{q~0@QuY44&mMUNXd7z>U58shA3o`p5eVLpq>+{(<3->DWuSFVZwC zxd50Uz(w~LxC4}bgag#q#NNokK@yNc+Q|Ap!u>Ddy+df>v;j@I12CDNN9do+0^n8p zMQs7X#+FVF0C5muGfN{r0|Nkql%BQT|K(DDNdR2pzM=_ea5+GO|J67`05AV92t@4l z0Qno0078PIHdaQGHZ~Scw!dzgqjK~3B7kf>BcP__&lLyU(cu3B^uLo%{j|Mb0NR)tkeT7Hcwp4O# z)yzu>cvG(d9~0a^)eZ;;%3ksk@F&1eEBje~ zW+-_s)&RgiweQc!otF>4%vbXKaOU41{!hw?|2`Ld3I8$&#WOsq>EG)1ANb!{N4z9@ zsU!bPG-~-bqCeIDzo^Q;gnucB{tRzm{ZH^Orphm2U+REA!*<*J6YQV83@&xoDl%#wnl5qcBqCcAF-vX5{30}(oJrnSH z{RY85hylK2dMOh2%oO1J8%)0?8TOL%rS8)+CsDv}aQ>4D)Jv+DLK)9gI^n-T^$)Tc zFPUD75qJm!Y-KBqj;JP4dV4 z`X{lGmn<)1IGz330}s}Jrjtf{(lnuuNHe5(ezA(pYa=1|Ff-LhPFK8 zyJh_b{yzu0yll6ZkpRzRjezyYivjyjW7QwO;@6X`m;2Apn2EK2!~7S}-*=;5*7K$B z`x(=!^?zgj(-`&ApZJXI09aDLXaT@<;CH=?fBOY5d|b~wBA@@p^K#nxr`)?i?SqTupI_PJ(A3cx`z~9mX_*)>L F{|7XC?P&l2 literal 0 HcmV?d00001 diff --git a/example/android/test/gradle/wrapper/gradle-wrapper.properties b/example/android/test/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 00000000..a6381559 --- /dev/null +++ b/example/android/test/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,6 @@ +#Mon May 27 16:26:41 MSK 2019 +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-5.1.1-all.zip diff --git a/example/android/test/gradlew b/example/android/test/gradlew new file mode 100755 index 00000000..cccdd3d5 --- /dev/null +++ b/example/android/test/gradlew @@ -0,0 +1,172 @@ +#!/usr/bin/env sh + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >/dev/null +APP_HOME="`pwd -P`" +cd "$SAVED" >/dev/null + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS="" + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn () { + echo "$*" +} + +die () { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; + NONSTOP* ) + nonstop=true + ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin, switch paths to Windows format before running java +if $cygwin ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + JAVACMD=`cygpath --unix "$JAVACMD"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=$((i+1)) + done + case $i in + (0) set -- ;; + (1) set -- "$args0" ;; + (2) set -- "$args0" "$args1" ;; + (3) set -- "$args0" "$args1" "$args2" ;; + (4) set -- "$args0" "$args1" "$args2" "$args3" ;; + (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Escape application args +save () { + for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done + echo " " +} +APP_ARGS=$(save "$@") + +# Collect all arguments for the java command, following the shell quoting and substitution rules +eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" + +# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong +if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then + cd "$(dirname "$0")" +fi + +exec "$JAVACMD" "$@" diff --git a/example/android/test/gradlew.bat b/example/android/test/gradlew.bat new file mode 100644 index 00000000..f9553162 --- /dev/null +++ b/example/android/test/gradlew.bat @@ -0,0 +1,84 @@ +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS= + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto init + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto init + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:init +@rem Get command-line arguments, handling Windows variants + +if not "%OS%" == "Windows_NT" goto win9xME_args + +:win9xME_args +@rem Slurp the command line arguments. +set CMD_LINE_ARGS= +set _SKIP=2 + +:win9xME_args_slurp +if "x%~1" == "x" goto execute + +set CMD_LINE_ARGS=%* + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/example/android/test/settings.gradle b/example/android/test/settings.gradle new file mode 100644 index 00000000..8a82dd6c --- /dev/null +++ b/example/android/test/settings.gradle @@ -0,0 +1 @@ +include ':ton' diff --git a/example/android/test/ton/.gitignore b/example/android/test/ton/.gitignore new file mode 100644 index 00000000..796b96d1 --- /dev/null +++ b/example/android/test/ton/.gitignore @@ -0,0 +1 @@ +/build diff --git a/example/android/test/ton/build.gradle b/example/android/test/ton/build.gradle new file mode 100644 index 00000000..022466f4 --- /dev/null +++ b/example/android/test/ton/build.gradle @@ -0,0 +1,44 @@ +apply plugin: 'com.android.library' +apply plugin: 'kotlin-android' +apply plugin: 'kotlin-android-extensions' + +android { + compileSdkVersion 28 + + + defaultConfig { + minSdkVersion 16 + targetSdkVersion 28 + versionCode 1 + versionName "1.0" + + testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" + + } + + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' + } + } + + sourceSets { + main { + jni.srcDirs = [] + jniLibs.srcDirs 'src/cpp/prebuilt' + } + } + +} + +dependencies { + implementation fileTree(dir: 'libs', include: ['*.jar']) + + implementation 'com.android.support:appcompat-v7:28.0.0' + testImplementation 'junit:junit:4.12' + androidTestImplementation 'junit:junit:4.12' + androidTestImplementation 'com.android.support.test:runner:1.0.2' + androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' + androidTestImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.1.1' +} diff --git a/example/android/test/ton/proguard-rules.pro b/example/android/test/ton/proguard-rules.pro new file mode 100644 index 00000000..f1b42451 --- /dev/null +++ b/example/android/test/ton/proguard-rules.pro @@ -0,0 +1,21 @@ +# Add project specific ProGuard rules here. +# You can control the set of applied configuration files using the +# proguardFiles setting in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile diff --git a/example/android/test/ton/src/androidTest/java/drinkless/org/ton/TonTest.kt b/example/android/test/ton/src/androidTest/java/drinkless/org/ton/TonTest.kt new file mode 100644 index 00000000..5696ad5c --- /dev/null +++ b/example/android/test/ton/src/androidTest/java/drinkless/org/ton/TonTest.kt @@ -0,0 +1,81 @@ +package drinkless.org.ton + +import android.content.Context +import android.support.test.InstrumentationRegistry +import android.support.test.InstrumentationRegistry.getContext +import android.support.test.filters.LargeTest +import android.support.test.filters.SmallTest +import android.support.test.runner.AndroidJUnit4 +import drinkless.org.ton.Client +import drinkless.org.ton.TonApi + +import org.junit.Test +import org.junit.runner.RunWith + +import org.junit.Assert.* +import kotlin.coroutines.resume +import kotlin.coroutines.resumeWithException +import kotlin.coroutines.suspendCoroutine +import kotlinx.coroutines.*; + +class ClientKotlin { + val client = Client.create(null, null, null); + suspend fun send(query: TonApi.Function) : TonApi.Object { + return suspendCoroutine { cont -> + client.send(query, { + cont.resume(it) + },null) + } + } +} + +@RunWith(AndroidJUnit4::class) +@SmallTest +class TonTest { + val config = """{ + "@type": "config.global", + "liteclients": [ + { + "@type": "liteclient.config.global", + "ip": 1137658550, + "port": 4924, + "id": { + "@type": "pub.ed25519", + "key": "peJTw/arlRfssgTuf9BMypJzqOi7SXEqSPSWiEw2U1M=" + } + } + ] +}""" + @Test + fun createTestWallet() { + val client = ClientKotlin() + val dir = getContext().getExternalFilesDir(null).toString() + "/"; + runBlocking { + client.send(TonApi.Init(TonApi.Options(config, dir))) + val key = client.send(TonApi.CreateNewKey("local password".toByteArray(), "mnemonic password".toByteArray())) as TonApi.Key + val walletAddress = client.send(TonApi.TestWalletGetAccountAddress(TonApi.TestWalletInitialAccountState(key.publicKey))) as TonApi.AccountAddress; + val testGiverState = client.send(TonApi.TestGiverGetAccountState()) as TonApi.TestGiverAccountState + + client.send(TonApi.TestGiverSendGrams(walletAddress, testGiverState.seqno, 6660000000)) as TonApi.Ok + + while ((client.send(TonApi.GenericGetAccountState(walletAddress)) as TonApi.GenericAccountStateUninited).accountState.balance <= 0L) { + delay(1000L) + } + + val inputKey = TonApi.InputKey(key, "local password".toByteArray()); + client.send(TonApi.TestWalletInit(inputKey)) as TonApi.Ok + + while (client.send(TonApi.GenericGetAccountState(walletAddress)) !is TonApi.GenericAccountStateTestWallet) { + delay(1000L) + } + + val state = client.send(TonApi.GenericGetAccountState(walletAddress)) as TonApi.GenericAccountStateTestWallet + val balance = state.accountState.balance + client.send(TonApi.GenericSendGrams(inputKey, walletAddress, walletAddress, 10)) as TonApi.Ok + while ((client.send(TonApi.GenericGetAccountState(walletAddress)) as TonApi.GenericAccountStateTestWallet).accountState.balance == balance) { + delay(1000L) + } + } + } +} + diff --git a/example/android/test/ton/src/androidTest/java/drinkless/org/ton/TonTestJava.java b/example/android/test/ton/src/androidTest/java/drinkless/org/ton/TonTestJava.java new file mode 100644 index 00000000..1a9f2ade --- /dev/null +++ b/example/android/test/ton/src/androidTest/java/drinkless/org/ton/TonTestJava.java @@ -0,0 +1,161 @@ +package drinkless.org.ton; + + +import java.util.concurrent.CountDownLatch; + +import android.support.test.runner.AndroidJUnit4; +import android.util.Log; +import drinkless.org.ton.Client; +import drinkless.org.ton.TonApi; +import org.junit.Test; +import org.junit.runner.RunWith; + +import static android.support.test.InstrumentationRegistry.getContext; + +@RunWith(AndroidJUnit4.class) +public class TonTestJava { + class JavaClient { + Client client = Client.create(null, null, null); + + public Object send(TonApi.Function query) { + Object[] result = new Object[1]; + CountDownLatch countDownLatch = new CountDownLatch(1); + + class Callback implements Client.ResultHandler { + Object[] result; + CountDownLatch countDownLatch; + + Callback(Object[] result, CountDownLatch countDownLatch) { + this.result = result; + this.countDownLatch = countDownLatch; + } + + public void onResult(TonApi.Object object) { + if (object instanceof TonApi.Error) { + appendLog(((TonApi.Error) object).message); + } else { + result[0] = object; + } + if (countDownLatch != null) { + countDownLatch.countDown(); + } + } + } + + client.send(query, new Callback(result, countDownLatch) , null); + if (countDownLatch != null) { + try { + countDownLatch.await(); + } catch (Throwable e) { + appendLog(e.toString()); + } + } + return result[0]; + } + } + + String config = + "{" + + "\"@type\": \"config.global\"," + + "\"liteclients\": [" + + "{" + + "\"@type\": \"liteclient.config.global\"," + + "\"ip\": 1137658550," + + "\"port\": 4924," + + "\"id\": {" + + "\"@type\": \"pub.ed25519\"," + + "\"key\": \"peJTw/arlRfssgTuf9BMypJzqOi7SXEqSPSWiEw2U1M=\"" + + "}" + + "}" + + "]" + + "}"; + + private void appendLog(String log) { + Log.w("XX", log); + } + + @Test + public void createTestWallet() { + appendLog("start..."); + + JavaClient client = new JavaClient(); + Object result = client.send(new TonApi.Init(new TonApi.Options(config, getContext().getExternalFilesDir(null) + "/"))); + if (!(result instanceof TonApi.Ok)) { + appendLog("failed to set config"); + return; + } + appendLog("config set ok"); + TonApi.Key key = (TonApi.Key) client.send(new TonApi.CreateNewKey("local password".getBytes(), "mnemonic password".getBytes())); + appendLog("got private key"); + TonApi.AccountAddress walletAddress = (TonApi.AccountAddress) client.send(new TonApi.TestWalletGetAccountAddress(new TonApi.TestWalletInitialAccountState(key.publicKey))); + appendLog("got account address"); + appendLog("sending grams..."); + TonApi.TestGiverAccountState testGiverState = (TonApi.TestGiverAccountState) client.send(new TonApi.TestGiverGetAccountState()); + result = client.send(new TonApi.TestGiverSendGrams(walletAddress, testGiverState.seqno, 6660000000L)); + if (!(result instanceof TonApi.Ok)) { + appendLog("failed to send grams"); + return; + } + appendLog("grams sent, getting balance"); + + while (true) { + TonApi.GenericAccountStateUninited accountStateUninited = (TonApi.GenericAccountStateUninited) client.send(new TonApi.GenericGetAccountState(walletAddress)); + if (accountStateUninited == null || accountStateUninited.accountState.balance <= 0L) { + try { + Thread.sleep(1000); + } catch (Throwable e) { + appendLog(e.toString()); + } + } else { + appendLog(String.format("balance = %d", accountStateUninited.accountState.balance)); + break; + } + } + + TonApi.InputKey inputKey = new TonApi.InputKey(key, "local password".getBytes()); + result = client.send(new TonApi.TestWalletInit(inputKey)); + if (!(result instanceof TonApi.Ok)) { + return; + } + appendLog("init test wallet ok, getting state..."); + + while (true) { + TonApi.GenericAccountState accountState = (TonApi.GenericAccountState) client.send(new TonApi.GenericGetAccountState(walletAddress)); + if (!(accountState instanceof TonApi.GenericAccountStateTestWallet)) { + try { + Thread.sleep(1000); + } catch (Throwable e) { + appendLog(e.toString()); + } + } else { + appendLog("got account state"); + break; + } + } + + appendLog("sending grams..."); + TonApi.GenericAccountStateTestWallet state = (TonApi.GenericAccountStateTestWallet) client.send(new TonApi.GenericGetAccountState(walletAddress)); + long balance = state.accountState.balance; + result = client.send(new TonApi.GenericSendGrams(inputKey, walletAddress, walletAddress, 10)); + if (!(result instanceof TonApi.Ok)) { + return; + } + appendLog(String.format("grams sent, current balance %d, receving...", balance)); + + while (true) { + TonApi.GenericAccountStateTestWallet wallet = (TonApi.GenericAccountStateTestWallet) client.send(new TonApi.GenericGetAccountState(walletAddress)); + if (wallet == null || wallet.accountState.balance == balance) { + try { + Thread.sleep(1000); + } catch (Throwable e) { + appendLog(e.toString()); + } + } else { + appendLog(String.format("grams received, balance = %d", balance)); + break; + } + } + appendLog("OK"); + + } +} diff --git a/example/android/test/ton/src/main/AndroidManifest.xml b/example/android/test/ton/src/main/AndroidManifest.xml new file mode 100644 index 00000000..f2c0d2d5 --- /dev/null +++ b/example/android/test/ton/src/main/AndroidManifest.xml @@ -0,0 +1,4 @@ + + + diff --git a/example/android/test/ton/src/main/java/drinkless/org/ton/Client.java b/example/android/test/ton/src/main/java/drinkless/org/ton/Client.java new file mode 100644 index 00000000..79534c5b --- /dev/null +++ b/example/android/test/ton/src/main/java/drinkless/org/ton/Client.java @@ -0,0 +1,290 @@ +// +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2019 +// +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +package drinkless.org.ton; + +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicLong; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReadWriteLock; +import java.util.concurrent.locks.ReentrantReadWriteLock; + +/** + * Main class for interaction with the TDLib. + */ +public final class Client implements Runnable { + static { + System.loadLibrary("native-lib"); + } + /** + * Interface for handler for results of queries to TDLib and incoming updates from TDLib. + */ + public interface ResultHandler { + /** + * Callback called on result of query to TDLib or incoming update from TDLib. + * + * @param object Result of query or update of type TonApi.Update about new events. + */ + void onResult(TonApi.Object object); + } + + /** + * Interface for handler of exceptions thrown while invoking ResultHandler. + * By default, all such exceptions are ignored. + * All exceptions thrown from ExceptionHandler are ignored. + */ + public interface ExceptionHandler { + /** + * Callback called on exceptions thrown while invoking ResultHandler. + * + * @param e Exception thrown by ResultHandler. + */ + void onException(Throwable e); + } + + /** + * Sends a request to the TDLib. + * + * @param query Object representing a query to the TDLib. + * @param resultHandler Result handler with onResult method which will be called with result + * of the query or with TonApi.Error as parameter. If it is null, nothing + * will be called. + * @param exceptionHandler Exception handler with onException method which will be called on + * exception thrown from resultHandler. If it is null, then + * defaultExceptionHandler will be called. + * @throws NullPointerException if query is null. + */ + public void send(TonApi.Function query, ResultHandler resultHandler, ExceptionHandler exceptionHandler) { + if (query == null) { + throw new NullPointerException("query is null"); + } + + readLock.lock(); + try { + if (isClientDestroyed) { + if (resultHandler != null) { + handleResult(new TonApi.Error(500, "Client is closed"), resultHandler, exceptionHandler); + } + return; + } + + long queryId = currentQueryId.incrementAndGet(); + handlers.put(queryId, new Handler(resultHandler, exceptionHandler)); + nativeClientSend(nativeClientId, queryId, query); + } finally { + readLock.unlock(); + } + } + + /** + * Sends a request to the TDLib with an empty ExceptionHandler. + * + * @param query Object representing a query to the TDLib. + * @param resultHandler Result handler with onResult method which will be called with result + * of the query or with TonApi.Error as parameter. If it is null, then + * defaultExceptionHandler will be called. + * @throws NullPointerException if query is null. + */ + public void send(TonApi.Function query, ResultHandler resultHandler) { + send(query, resultHandler, null); + } + + /** + * Synchronously executes a TDLib request. Only a few marked accordingly requests can be executed synchronously. + * + * @param query Object representing a query to the TDLib. + * @return request result. + * @throws NullPointerException if query is null. + */ + public static TonApi.Object execute(TonApi.Function query) { + if (query == null) { + throw new NullPointerException("query is null"); + } + return nativeClientExecute(query); + } + + /** + * Replaces handler for incoming updates from the TDLib. + * + * @param updatesHandler Handler with onResult method which will be called for every incoming + * update from the TDLib. + * @param exceptionHandler Exception handler with onException method which will be called on + * exception thrown from updatesHandler, if it is null, defaultExceptionHandler will be invoked. + */ + public void setUpdatesHandler(ResultHandler updatesHandler, ExceptionHandler exceptionHandler) { + handlers.put(0L, new Handler(updatesHandler, exceptionHandler)); + } + + /** + * Replaces handler for incoming updates from the TDLib. Sets empty ExceptionHandler. + * + * @param updatesHandler Handler with onResult method which will be called for every incoming + * update from the TDLib. + */ + public void setUpdatesHandler(ResultHandler updatesHandler) { + setUpdatesHandler(updatesHandler, null); + } + + /** + * Replaces default exception handler to be invoked on exceptions thrown from updatesHandler and all other ResultHandler. + * + * @param defaultExceptionHandler Default exception handler. If null Exceptions are ignored. + */ + public void setDefaultExceptionHandler(Client.ExceptionHandler defaultExceptionHandler) { + this.defaultExceptionHandler = defaultExceptionHandler; + } + + /** + * Overridden method from Runnable, do not call it directly. + */ + @Override + public void run() { + while (!stopFlag) { + receiveQueries(300.0 /*seconds*/); + } + } + + /** + * Creates new Client. + * + * @param updatesHandler Handler for incoming updates. + * @param updatesExceptionHandler Handler for exceptions thrown from updatesHandler. If it is null, exceptions will be iggnored. + * @param defaultExceptionHandler Default handler for exceptions thrown from all ResultHandler. If it is null, exceptions will be iggnored. + * @return created Client + */ + public static Client create(ResultHandler updatesHandler, ExceptionHandler updatesExceptionHandler, ExceptionHandler defaultExceptionHandler) { + Client client = new Client(updatesHandler, updatesExceptionHandler, defaultExceptionHandler); + new Thread(client, "TDLib thread").start(); + return client; + } + + /** + * Closes Client. + */ + public void close() { + writeLock.lock(); + try { + if (isClientDestroyed) { + return; + } + if (!stopFlag) { + //send(new TonApi.Close(), null); + } + isClientDestroyed = true; + while (!stopFlag) { + Thread.yield(); + } + while (handlers.size() != 1) { + receiveQueries(300.0); + } + destroyNativeClient(nativeClientId); + } finally { + writeLock.unlock(); + } + } + + private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock(); + private final Lock readLock = readWriteLock.readLock(); + private final Lock writeLock = readWriteLock.writeLock(); + + private volatile boolean stopFlag = false; + private volatile boolean isClientDestroyed = false; + private final long nativeClientId; + + private final ConcurrentHashMap handlers = new ConcurrentHashMap(); + private final AtomicLong currentQueryId = new AtomicLong(); + + private volatile ExceptionHandler defaultExceptionHandler = null; + + private static final int MAX_EVENTS = 1000; + private final long[] eventIds = new long[MAX_EVENTS]; + private final TonApi.Object[] events = new TonApi.Object[MAX_EVENTS]; + + private static class Handler { + final ResultHandler resultHandler; + final ExceptionHandler exceptionHandler; + + Handler(ResultHandler resultHandler, ExceptionHandler exceptionHandler) { + this.resultHandler = resultHandler; + this.exceptionHandler = exceptionHandler; + } + } + + private Client(ResultHandler updatesHandler, ExceptionHandler updateExceptionHandler, ExceptionHandler defaultExceptionHandler) { + nativeClientId = createNativeClient(); + handlers.put(0L, new Handler(updatesHandler, updateExceptionHandler)); + this.defaultExceptionHandler = defaultExceptionHandler; + } + + @Override + protected void finalize() throws Throwable { + try { + close(); + } finally { + super.finalize(); + } + } + + private void processResult(long id, TonApi.Object object) { + /* + if (object instanceof TonApi.UpdateAuthorizationState) { + if (((TonApi.UpdateAuthorizationState) object).authorizationState instanceof TonApi.AuthorizationStateClosed) { + stopFlag = true; + } + } + */ + Handler handler; + if (id == 0) { + // update handler stays forever + handler = handlers.get(id); + } else { + handler = handlers.remove(id); + } + if (handler == null) { + return; + } + + handleResult(object, handler.resultHandler, handler.exceptionHandler); + } + + private void handleResult(TonApi.Object object, ResultHandler resultHandler, ExceptionHandler exceptionHandler) { + if (resultHandler == null) { + return; + } + + try { + resultHandler.onResult(object); + } catch (Throwable cause) { + if (exceptionHandler == null) { + exceptionHandler = defaultExceptionHandler; + } + if (exceptionHandler != null) { + try { + exceptionHandler.onException(cause); + } catch (Throwable ignored) { + } + } + } + } + + private void receiveQueries(double timeout) { + int resultN = nativeClientReceive(nativeClientId, eventIds, events, timeout); + for (int i = 0; i < resultN; i++) { + processResult(eventIds[i], events[i]); + events[i] = null; + } + } + + private static native long createNativeClient(); + + private static native void nativeClientSend(long nativeClientId, long eventId, TonApi.Function function); + + private static native int nativeClientReceive(long nativeClientId, long[] eventIds, TonApi.Object[] events, double timeout); + + private static native TonApi.Object nativeClientExecute(TonApi.Function function); + + private static native void destroyNativeClient(long nativeClientId); +} diff --git a/example/android/test/ton/src/main/res/values/strings.xml b/example/android/test/ton/src/main/res/values/strings.xml new file mode 100644 index 00000000..b9200763 --- /dev/null +++ b/example/android/test/ton/src/main/res/values/strings.xml @@ -0,0 +1,3 @@ + + tonlib + diff --git a/example/android/test/ton/src/test/java/drinkless/org/ton/ExampleUnitTest.java b/example/android/test/ton/src/test/java/drinkless/org/ton/ExampleUnitTest.java new file mode 100644 index 00000000..f25b76e6 --- /dev/null +++ b/example/android/test/ton/src/test/java/drinkless/org/ton/ExampleUnitTest.java @@ -0,0 +1,17 @@ +package drinkless.org.tonlib; + +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Example local unit test, which will execute on the development machine (host). + * + * @see Testing documentation + */ +public class ExampleUnitTest { + @Test + public void addition_isCorrect() { + assertEquals(4, 2 + 2); + } +} \ No newline at end of file diff --git a/example/android/third_party/crypto/arm/include/internal/__DECC_INCLUDE_EPILOGUE.H b/example/android/third_party/crypto/arm/include/internal/__DECC_INCLUDE_EPILOGUE.H new file mode 100644 index 00000000..c350018a --- /dev/null +++ b/example/android/third_party/crypto/arm/include/internal/__DECC_INCLUDE_EPILOGUE.H @@ -0,0 +1,16 @@ +/* + * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +/* + * This file is only used by HP C on VMS, and is included automatically + * after each header file from this directory + */ + +/* restore state. Must correspond to the save in __decc_include_prologue.h */ +#pragma names restore diff --git a/example/android/third_party/crypto/arm/include/internal/__DECC_INCLUDE_PROLOGUE.H b/example/android/third_party/crypto/arm/include/internal/__DECC_INCLUDE_PROLOGUE.H new file mode 100644 index 00000000..9a9c777f --- /dev/null +++ b/example/android/third_party/crypto/arm/include/internal/__DECC_INCLUDE_PROLOGUE.H @@ -0,0 +1,20 @@ +/* + * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +/* + * This file is only used by HP C on VMS, and is included automatically + * after each header file from this directory + */ + +/* save state */ +#pragma names save +/* have the compiler shorten symbols larger than 31 chars to 23 chars + * followed by a 8 hex char CRC + */ +#pragma names as_is,shortened diff --git a/example/android/third_party/crypto/arm/include/internal/bio.h b/example/android/third_party/crypto/arm/include/internal/bio.h new file mode 100644 index 00000000..c343b276 --- /dev/null +++ b/example/android/third_party/crypto/arm/include/internal/bio.h @@ -0,0 +1,33 @@ +/* + * Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#include + +struct bio_method_st { + int type; + char *name; + int (*bwrite) (BIO *, const char *, size_t, size_t *); + int (*bwrite_old) (BIO *, const char *, int); + int (*bread) (BIO *, char *, size_t, size_t *); + int (*bread_old) (BIO *, char *, int); + int (*bputs) (BIO *, const char *); + int (*bgets) (BIO *, char *, int); + long (*ctrl) (BIO *, int, long, void *); + int (*create) (BIO *); + int (*destroy) (BIO *); + long (*callback_ctrl) (BIO *, int, BIO_info_cb *); +}; + +void bio_free_ex_data(BIO *bio); +void bio_cleanup(void); + + +/* Old style to new style BIO_METHOD conversion functions */ +int bwrite_conv(BIO *bio, const char *data, size_t datal, size_t *written); +int bread_conv(BIO *bio, char *data, size_t datal, size_t *read); diff --git a/example/android/third_party/crypto/arm/include/internal/comp.h b/example/android/third_party/crypto/arm/include/internal/comp.h new file mode 100644 index 00000000..ac6e38b4 --- /dev/null +++ b/example/android/third_party/crypto/arm/include/internal/comp.h @@ -0,0 +1,12 @@ +/* + * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#include + +void comp_zlib_cleanup_int(void); diff --git a/example/android/third_party/crypto/arm/include/internal/conf.h b/example/android/third_party/crypto/arm/include/internal/conf.h new file mode 100644 index 00000000..dc1e7250 --- /dev/null +++ b/example/android/third_party/crypto/arm/include/internal/conf.h @@ -0,0 +1,23 @@ +/* + * Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_INTERNAL_CONF_H +# define HEADER_INTERNAL_CONF_H + +#include + +struct ossl_init_settings_st { + char *appname; +}; + +void openssl_config_int(const char *appname); +void openssl_no_config_int(void); +void conf_modules_free_int(void); + +#endif diff --git a/example/android/third_party/crypto/arm/include/internal/constant_time_locl.h b/example/android/third_party/crypto/arm/include/internal/constant_time_locl.h new file mode 100644 index 00000000..82ff7465 --- /dev/null +++ b/example/android/third_party/crypto/arm/include/internal/constant_time_locl.h @@ -0,0 +1,327 @@ +/* + * Copyright 2014-2018 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_CONSTANT_TIME_LOCL_H +# define HEADER_CONSTANT_TIME_LOCL_H + +# include +# include +# include /* For 'ossl_inline' */ + +/*- + * The boolean methods return a bitmask of all ones (0xff...f) for true + * and 0 for false. This is useful for choosing a value based on the result + * of a conditional in constant time. For example, + * if (a < b) { + * c = a; + * } else { + * c = b; + * } + * can be written as + * unsigned int lt = constant_time_lt(a, b); + * c = constant_time_select(lt, a, b); + */ + +/* Returns the given value with the MSB copied to all the other bits. */ +static ossl_inline unsigned int constant_time_msb(unsigned int a); +/* Convenience method for uint32_t. */ +static ossl_inline uint32_t constant_time_msb_32(uint32_t a); +/* Convenience method for uint64_t. */ +static ossl_inline uint64_t constant_time_msb_64(uint64_t a); + +/* Returns 0xff..f if a < b and 0 otherwise. */ +static ossl_inline unsigned int constant_time_lt(unsigned int a, + unsigned int b); +/* Convenience method for getting an 8-bit mask. */ +static ossl_inline unsigned char constant_time_lt_8(unsigned int a, + unsigned int b); +/* Convenience method for uint64_t. */ +static ossl_inline uint64_t constant_time_lt_64(uint64_t a, uint64_t b); + +/* Returns 0xff..f if a >= b and 0 otherwise. */ +static ossl_inline unsigned int constant_time_ge(unsigned int a, + unsigned int b); +/* Convenience method for getting an 8-bit mask. */ +static ossl_inline unsigned char constant_time_ge_8(unsigned int a, + unsigned int b); + +/* Returns 0xff..f if a == 0 and 0 otherwise. */ +static ossl_inline unsigned int constant_time_is_zero(unsigned int a); +/* Convenience method for getting an 8-bit mask. */ +static ossl_inline unsigned char constant_time_is_zero_8(unsigned int a); +/* Convenience method for getting a 32-bit mask. */ +static ossl_inline uint32_t constant_time_is_zero_32(uint32_t a); + +/* Returns 0xff..f if a == b and 0 otherwise. */ +static ossl_inline unsigned int constant_time_eq(unsigned int a, + unsigned int b); +/* Convenience method for getting an 8-bit mask. */ +static ossl_inline unsigned char constant_time_eq_8(unsigned int a, + unsigned int b); +/* Signed integers. */ +static ossl_inline unsigned int constant_time_eq_int(int a, int b); +/* Convenience method for getting an 8-bit mask. */ +static ossl_inline unsigned char constant_time_eq_int_8(int a, int b); + +/*- + * Returns (mask & a) | (~mask & b). + * + * When |mask| is all 1s or all 0s (as returned by the methods above), + * the select methods return either |a| (if |mask| is nonzero) or |b| + * (if |mask| is zero). + */ +static ossl_inline unsigned int constant_time_select(unsigned int mask, + unsigned int a, + unsigned int b); +/* Convenience method for unsigned chars. */ +static ossl_inline unsigned char constant_time_select_8(unsigned char mask, + unsigned char a, + unsigned char b); + +/* Convenience method for uint32_t. */ +static ossl_inline uint32_t constant_time_select_32(uint32_t mask, uint32_t a, + uint32_t b); + +/* Convenience method for uint64_t. */ +static ossl_inline uint64_t constant_time_select_64(uint64_t mask, uint64_t a, + uint64_t b); +/* Convenience method for signed integers. */ +static ossl_inline int constant_time_select_int(unsigned int mask, int a, + int b); + + +static ossl_inline unsigned int constant_time_msb(unsigned int a) +{ + return 0 - (a >> (sizeof(a) * 8 - 1)); +} + + +static ossl_inline uint32_t constant_time_msb_32(uint32_t a) +{ + return 0 - (a >> 31); +} + +static ossl_inline uint64_t constant_time_msb_64(uint64_t a) +{ + return 0 - (a >> 63); +} + +static ossl_inline size_t constant_time_msb_s(size_t a) +{ + return 0 - (a >> (sizeof(a) * 8 - 1)); +} + +static ossl_inline unsigned int constant_time_lt(unsigned int a, + unsigned int b) +{ + return constant_time_msb(a ^ ((a ^ b) | ((a - b) ^ b))); +} + +static ossl_inline size_t constant_time_lt_s(size_t a, size_t b) +{ + return constant_time_msb_s(a ^ ((a ^ b) | ((a - b) ^ b))); +} + +static ossl_inline unsigned char constant_time_lt_8(unsigned int a, + unsigned int b) +{ + return (unsigned char)constant_time_lt(a, b); +} + +static ossl_inline uint64_t constant_time_lt_64(uint64_t a, uint64_t b) +{ + return constant_time_msb_64(a ^ ((a ^ b) | ((a - b) ^ b))); +} + +static ossl_inline unsigned int constant_time_ge(unsigned int a, + unsigned int b) +{ + return ~constant_time_lt(a, b); +} + +static ossl_inline size_t constant_time_ge_s(size_t a, size_t b) +{ + return ~constant_time_lt_s(a, b); +} + +static ossl_inline unsigned char constant_time_ge_8(unsigned int a, + unsigned int b) +{ + return (unsigned char)constant_time_ge(a, b); +} + +static ossl_inline unsigned char constant_time_ge_8_s(size_t a, size_t b) +{ + return (unsigned char)constant_time_ge_s(a, b); +} + +static ossl_inline unsigned int constant_time_is_zero(unsigned int a) +{ + return constant_time_msb(~a & (a - 1)); +} + +static ossl_inline size_t constant_time_is_zero_s(size_t a) +{ + return constant_time_msb_s(~a & (a - 1)); +} + +static ossl_inline unsigned char constant_time_is_zero_8(unsigned int a) +{ + return (unsigned char)constant_time_is_zero(a); +} + +static ossl_inline uint32_t constant_time_is_zero_32(uint32_t a) +{ + return constant_time_msb_32(~a & (a - 1)); +} + +static ossl_inline unsigned int constant_time_eq(unsigned int a, + unsigned int b) +{ + return constant_time_is_zero(a ^ b); +} + +static ossl_inline size_t constant_time_eq_s(size_t a, size_t b) +{ + return constant_time_is_zero_s(a ^ b); +} + +static ossl_inline unsigned char constant_time_eq_8(unsigned int a, + unsigned int b) +{ + return (unsigned char)constant_time_eq(a, b); +} + +static ossl_inline unsigned char constant_time_eq_8_s(size_t a, size_t b) +{ + return (unsigned char)constant_time_eq_s(a, b); +} + +static ossl_inline unsigned int constant_time_eq_int(int a, int b) +{ + return constant_time_eq((unsigned)(a), (unsigned)(b)); +} + +static ossl_inline unsigned char constant_time_eq_int_8(int a, int b) +{ + return constant_time_eq_8((unsigned)(a), (unsigned)(b)); +} + +static ossl_inline unsigned int constant_time_select(unsigned int mask, + unsigned int a, + unsigned int b) +{ + return (mask & a) | (~mask & b); +} + +static ossl_inline size_t constant_time_select_s(size_t mask, + size_t a, + size_t b) +{ + return (mask & a) | (~mask & b); +} + +static ossl_inline unsigned char constant_time_select_8(unsigned char mask, + unsigned char a, + unsigned char b) +{ + return (unsigned char)constant_time_select(mask, a, b); +} + +static ossl_inline int constant_time_select_int(unsigned int mask, int a, + int b) +{ + return (int)constant_time_select(mask, (unsigned)(a), (unsigned)(b)); +} + +static ossl_inline int constant_time_select_int_s(size_t mask, int a, int b) +{ + return (int)constant_time_select((unsigned)mask, (unsigned)(a), + (unsigned)(b)); +} + +static ossl_inline uint32_t constant_time_select_32(uint32_t mask, uint32_t a, + uint32_t b) +{ + return (mask & a) | (~mask & b); +} + +static ossl_inline uint64_t constant_time_select_64(uint64_t mask, uint64_t a, + uint64_t b) +{ + return (mask & a) | (~mask & b); +} + +/* + * mask must be 0xFFFFFFFF or 0x00000000. + * + * if (mask) { + * uint32_t tmp = *a; + * + * *a = *b; + * *b = tmp; + * } + */ +static ossl_inline void constant_time_cond_swap_32(uint32_t mask, uint32_t *a, + uint32_t *b) +{ + uint32_t xor = *a ^ *b; + + xor &= mask; + *a ^= xor; + *b ^= xor; +} + +/* + * mask must be 0xFFFFFFFF or 0x00000000. + * + * if (mask) { + * uint64_t tmp = *a; + * + * *a = *b; + * *b = tmp; + * } + */ +static ossl_inline void constant_time_cond_swap_64(uint64_t mask, uint64_t *a, + uint64_t *b) +{ + uint64_t xor = *a ^ *b; + + xor &= mask; + *a ^= xor; + *b ^= xor; +} + +/* + * table is a two dimensional array of bytes. Each row has rowsize elements. + * Copies row number idx into out. rowsize and numrows are not considered + * private. + */ +static ossl_inline void constant_time_lookup(void *out, + const void *table, + size_t rowsize, + size_t numrows, + size_t idx) +{ + size_t i, j; + const unsigned char *tablec = (const unsigned char *)table; + unsigned char *outc = (unsigned char *)out; + unsigned char mask; + + memset(out, 0, rowsize); + + /* Note idx may underflow - but that is well defined */ + for (i = 0; i < numrows; i++, idx--) { + mask = (unsigned char)constant_time_is_zero_s(idx); + for (j = 0; j < rowsize; j++) + *(outc + j) |= constant_time_select_8(mask, *(tablec++), 0); + } +} + +#endif /* HEADER_CONSTANT_TIME_LOCL_H */ diff --git a/example/android/third_party/crypto/arm/include/internal/cryptlib.h b/example/android/third_party/crypto/arm/include/internal/cryptlib.h new file mode 100644 index 00000000..329ef620 --- /dev/null +++ b/example/android/third_party/crypto/arm/include/internal/cryptlib.h @@ -0,0 +1,96 @@ +/* + * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_CRYPTLIB_H +# define HEADER_CRYPTLIB_H + +# include +# include + +# ifdef OPENSSL_USE_APPLINK +# undef BIO_FLAGS_UPLINK +# define BIO_FLAGS_UPLINK 0x8000 +# include "ms/uplink.h" +# endif + +# include +# include +# include +# include +# include "internal/nelem.h" + +#ifdef NDEBUG +# define ossl_assert(x) ((x) != 0) +#else +__owur static ossl_inline int ossl_assert_int(int expr, const char *exprstr, + const char *file, int line) +{ + if (!expr) + OPENSSL_die(exprstr, file, line); + + return expr; +} + +# define ossl_assert(x) ossl_assert_int((x) != 0, "Assertion failed: "#x, \ + __FILE__, __LINE__) + +#endif + +typedef struct ex_callback_st EX_CALLBACK; + +DEFINE_STACK_OF(EX_CALLBACK) + +typedef struct app_mem_info_st APP_INFO; + +typedef struct mem_st MEM; +DEFINE_LHASH_OF(MEM); + +# define OPENSSL_CONF "openssl.cnf" + +# ifndef OPENSSL_SYS_VMS +# define X509_CERT_AREA OPENSSLDIR +# define X509_CERT_DIR OPENSSLDIR "/certs" +# define X509_CERT_FILE OPENSSLDIR "/cert.pem" +# define X509_PRIVATE_DIR OPENSSLDIR "/private" +# define CTLOG_FILE OPENSSLDIR "/ct_log_list.cnf" +# else +# define X509_CERT_AREA "OSSL$DATAROOT:[000000]" +# define X509_CERT_DIR "OSSL$DATAROOT:[CERTS]" +# define X509_CERT_FILE "OSSL$DATAROOT:[000000]cert.pem" +# define X509_PRIVATE_DIR "OSSL$DATAROOT:[PRIVATE]" +# define CTLOG_FILE "OSSL$DATAROOT:[000000]ct_log_list.cnf" +# endif + +# define X509_CERT_DIR_EVP "SSL_CERT_DIR" +# define X509_CERT_FILE_EVP "SSL_CERT_FILE" +# define CTLOG_FILE_EVP "CTLOG_FILE" + +/* size of string representations */ +# define DECIMAL_SIZE(type) ((sizeof(type)*8+2)/3+1) +# define HEX_SIZE(type) (sizeof(type)*2) + +void OPENSSL_cpuid_setup(void); +extern unsigned int OPENSSL_ia32cap_P[]; +void OPENSSL_showfatal(const char *fmta, ...); +void crypto_cleanup_all_ex_data_int(void); +int openssl_init_fork_handlers(void); + +char *ossl_safe_getenv(const char *name); + +extern CRYPTO_RWLOCK *memdbg_lock; +int openssl_strerror_r(int errnum, char *buf, size_t buflen); +# if !defined(OPENSSL_NO_STDIO) +FILE *openssl_fopen(const char *filename, const char *mode); +# else +void *openssl_fopen(const char *filename, const char *mode); +# endif + +uint32_t OPENSSL_rdtsc(void); + +#endif diff --git a/example/android/third_party/crypto/arm/include/internal/dane.h b/example/android/third_party/crypto/arm/include/internal/dane.h new file mode 100644 index 00000000..a1cb5488 --- /dev/null +++ b/example/android/third_party/crypto/arm/include/internal/dane.h @@ -0,0 +1,103 @@ +/* + * Copyright 2015-2016 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_INTERNAL_DANE_H +#define HEADER_INTERNAL_DANE_H + +#include + +/*- + * Certificate usages: + * https://tools.ietf.org/html/rfc6698#section-2.1.1 + */ +#define DANETLS_USAGE_PKIX_TA 0 +#define DANETLS_USAGE_PKIX_EE 1 +#define DANETLS_USAGE_DANE_TA 2 +#define DANETLS_USAGE_DANE_EE 3 +#define DANETLS_USAGE_LAST DANETLS_USAGE_DANE_EE + +/*- + * Selectors: + * https://tools.ietf.org/html/rfc6698#section-2.1.2 + */ +#define DANETLS_SELECTOR_CERT 0 +#define DANETLS_SELECTOR_SPKI 1 +#define DANETLS_SELECTOR_LAST DANETLS_SELECTOR_SPKI + +/*- + * Matching types: + * https://tools.ietf.org/html/rfc6698#section-2.1.3 + */ +#define DANETLS_MATCHING_FULL 0 +#define DANETLS_MATCHING_2256 1 +#define DANETLS_MATCHING_2512 2 +#define DANETLS_MATCHING_LAST DANETLS_MATCHING_2512 + +typedef struct danetls_record_st { + uint8_t usage; + uint8_t selector; + uint8_t mtype; + unsigned char *data; + size_t dlen; + EVP_PKEY *spki; +} danetls_record; + +DEFINE_STACK_OF(danetls_record) + +/* + * Shared DANE context + */ +struct dane_ctx_st { + const EVP_MD **mdevp; /* mtype -> digest */ + uint8_t *mdord; /* mtype -> preference */ + uint8_t mdmax; /* highest supported mtype */ + unsigned long flags; /* feature bitmask */ +}; + +/* + * Per connection DANE state + */ +struct ssl_dane_st { + struct dane_ctx_st *dctx; + STACK_OF(danetls_record) *trecs; + STACK_OF(X509) *certs; /* DANE-TA(2) Cert(0) Full(0) certs */ + danetls_record *mtlsa; /* Matching TLSA record */ + X509 *mcert; /* DANE matched cert */ + uint32_t umask; /* Usages present */ + int mdpth; /* Depth of matched cert */ + int pdpth; /* Depth of PKIX trust */ + unsigned long flags; /* feature bitmask */ +}; + +#define DANETLS_ENABLED(dane) \ + ((dane) != NULL && sk_danetls_record_num((dane)->trecs) > 0) + +#define DANETLS_USAGE_BIT(u) (((uint32_t)1) << u) + +#define DANETLS_PKIX_TA_MASK (DANETLS_USAGE_BIT(DANETLS_USAGE_PKIX_TA)) +#define DANETLS_PKIX_EE_MASK (DANETLS_USAGE_BIT(DANETLS_USAGE_PKIX_EE)) +#define DANETLS_DANE_TA_MASK (DANETLS_USAGE_BIT(DANETLS_USAGE_DANE_TA)) +#define DANETLS_DANE_EE_MASK (DANETLS_USAGE_BIT(DANETLS_USAGE_DANE_EE)) + +#define DANETLS_PKIX_MASK (DANETLS_PKIX_TA_MASK | DANETLS_PKIX_EE_MASK) +#define DANETLS_DANE_MASK (DANETLS_DANE_TA_MASK | DANETLS_DANE_EE_MASK) +#define DANETLS_TA_MASK (DANETLS_PKIX_TA_MASK | DANETLS_DANE_TA_MASK) +#define DANETLS_EE_MASK (DANETLS_PKIX_EE_MASK | DANETLS_DANE_EE_MASK) + +#define DANETLS_HAS_PKIX(dane) ((dane) && ((dane)->umask & DANETLS_PKIX_MASK)) +#define DANETLS_HAS_DANE(dane) ((dane) && ((dane)->umask & DANETLS_DANE_MASK)) +#define DANETLS_HAS_TA(dane) ((dane) && ((dane)->umask & DANETLS_TA_MASK)) +#define DANETLS_HAS_EE(dane) ((dane) && ((dane)->umask & DANETLS_EE_MASK)) + +#define DANETLS_HAS_PKIX_TA(dane) ((dane)&&((dane)->umask & DANETLS_PKIX_TA_MASK)) +#define DANETLS_HAS_PKIX_EE(dane) ((dane)&&((dane)->umask & DANETLS_PKIX_EE_MASK)) +#define DANETLS_HAS_DANE_TA(dane) ((dane)&&((dane)->umask & DANETLS_DANE_TA_MASK)) +#define DANETLS_HAS_DANE_EE(dane) ((dane)&&((dane)->umask & DANETLS_DANE_EE_MASK)) + +#endif /* HEADER_INTERNAL_DANE_H */ diff --git a/example/android/third_party/crypto/arm/include/internal/dso.h b/example/android/third_party/crypto/arm/include/internal/dso.h new file mode 100644 index 00000000..eb5f7d53 --- /dev/null +++ b/example/android/third_party/crypto/arm/include/internal/dso.h @@ -0,0 +1,165 @@ +/* + * Copyright 2000-2018 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_DSO_H +# define HEADER_DSO_H + +# include +# include "internal/dsoerr.h" + +/* These values are used as commands to DSO_ctrl() */ +# define DSO_CTRL_GET_FLAGS 1 +# define DSO_CTRL_SET_FLAGS 2 +# define DSO_CTRL_OR_FLAGS 3 + +/* + * By default, DSO_load() will translate the provided filename into a form + * typical for the platform using the dso_name_converter function of the + * method. Eg. win32 will transform "blah" into "blah.dll", and dlfcn will + * transform it into "libblah.so". This callback could even utilise the + * DSO_METHOD's converter too if it only wants to override behaviour for + * one or two possible DSO methods. However, the following flag can be + * set in a DSO to prevent *any* native name-translation at all - eg. if + * the caller has prompted the user for a path to a driver library so the + * filename should be interpreted as-is. + */ +# define DSO_FLAG_NO_NAME_TRANSLATION 0x01 +/* + * An extra flag to give if only the extension should be added as + * translation. This is obviously only of importance on Unix and other + * operating systems where the translation also may prefix the name with + * something, like 'lib', and ignored everywhere else. This flag is also + * ignored if DSO_FLAG_NO_NAME_TRANSLATION is used at the same time. + */ +# define DSO_FLAG_NAME_TRANSLATION_EXT_ONLY 0x02 + +/* + * Don't unload the DSO when we call DSO_free() + */ +# define DSO_FLAG_NO_UNLOAD_ON_FREE 0x04 + +/* + * This flag loads the library with public symbols. Meaning: The exported + * symbols of this library are public to all libraries loaded after this + * library. At the moment only implemented in unix. + */ +# define DSO_FLAG_GLOBAL_SYMBOLS 0x20 + +typedef void (*DSO_FUNC_TYPE) (void); + +typedef struct dso_st DSO; +typedef struct dso_meth_st DSO_METHOD; + +/* + * The function prototype used for method functions (or caller-provided + * callbacks) that transform filenames. They are passed a DSO structure + * pointer (or NULL if they are to be used independently of a DSO object) and + * a filename to transform. They should either return NULL (if there is an + * error condition) or a newly allocated string containing the transformed + * form that the caller will need to free with OPENSSL_free() when done. + */ +typedef char *(*DSO_NAME_CONVERTER_FUNC)(DSO *, const char *); +/* + * The function prototype used for method functions (or caller-provided + * callbacks) that merge two file specifications. They are passed a DSO + * structure pointer (or NULL if they are to be used independently of a DSO + * object) and two file specifications to merge. They should either return + * NULL (if there is an error condition) or a newly allocated string + * containing the result of merging that the caller will need to free with + * OPENSSL_free() when done. Here, merging means that bits and pieces are + * taken from each of the file specifications and added together in whatever + * fashion that is sensible for the DSO method in question. The only rule + * that really applies is that if the two specification contain pieces of the + * same type, the copy from the first string takes priority. One could see + * it as the first specification is the one given by the user and the second + * being a bunch of defaults to add on if they're missing in the first. + */ +typedef char *(*DSO_MERGER_FUNC)(DSO *, const char *, const char *); + +DSO *DSO_new(void); +int DSO_free(DSO *dso); +int DSO_flags(DSO *dso); +int DSO_up_ref(DSO *dso); +long DSO_ctrl(DSO *dso, int cmd, long larg, void *parg); + +/* + * These functions can be used to get/set the platform-independent filename + * used for a DSO. NB: set will fail if the DSO is already loaded. + */ +const char *DSO_get_filename(DSO *dso); +int DSO_set_filename(DSO *dso, const char *filename); +/* + * This function will invoke the DSO's name_converter callback to translate a + * filename, or if the callback isn't set it will instead use the DSO_METHOD's + * converter. If "filename" is NULL, the "filename" in the DSO itself will be + * used. If the DSO_FLAG_NO_NAME_TRANSLATION flag is set, then the filename is + * simply duplicated. NB: This function is usually called from within a + * DSO_METHOD during the processing of a DSO_load() call, and is exposed so + * that caller-created DSO_METHODs can do the same thing. A non-NULL return + * value will need to be OPENSSL_free()'d. + */ +char *DSO_convert_filename(DSO *dso, const char *filename); +/* + * This function will invoke the DSO's merger callback to merge two file + * specifications, or if the callback isn't set it will instead use the + * DSO_METHOD's merger. A non-NULL return value will need to be + * OPENSSL_free()'d. + */ +char *DSO_merge(DSO *dso, const char *filespec1, const char *filespec2); + +/* + * The all-singing all-dancing load function, you normally pass NULL for the + * first and third parameters. Use DSO_up_ref and DSO_free for subsequent + * reference count handling. Any flags passed in will be set in the + * constructed DSO after its init() function but before the load operation. + * If 'dso' is non-NULL, 'flags' is ignored. + */ +DSO *DSO_load(DSO *dso, const char *filename, DSO_METHOD *meth, int flags); + +/* This function binds to a function inside a shared library. */ +DSO_FUNC_TYPE DSO_bind_func(DSO *dso, const char *symname); + +/* + * This method is the default, but will beg, borrow, or steal whatever method + * should be the default on any particular platform (including + * DSO_METH_null() if necessary). + */ +DSO_METHOD *DSO_METHOD_openssl(void); + +/* + * This function writes null-terminated pathname of DSO module containing + * 'addr' into 'sz' large caller-provided 'path' and returns the number of + * characters [including trailing zero] written to it. If 'sz' is 0 or + * negative, 'path' is ignored and required amount of characters [including + * trailing zero] to accommodate pathname is returned. If 'addr' is NULL, then + * pathname of cryptolib itself is returned. Negative or zero return value + * denotes error. + */ +int DSO_pathbyaddr(void *addr, char *path, int sz); + +/* + * Like DSO_pathbyaddr() but instead returns a handle to the DSO for the symbol + * or NULL on error. + */ +DSO *DSO_dsobyaddr(void *addr, int flags); + +/* + * This function should be used with caution! It looks up symbols in *all* + * loaded modules and if module gets unloaded by somebody else attempt to + * dereference the pointer is doomed to have fatal consequences. Primary + * usage for this function is to probe *core* system functionality, e.g. + * check if getnameinfo(3) is available at run-time without bothering about + * OS-specific details such as libc.so.versioning or where does it actually + * reside: in libc itself or libsocket. + */ +void *DSO_global_lookup(const char *name); + +int ERR_load_DSO_strings(void); + +#endif diff --git a/example/android/third_party/crypto/arm/include/internal/dsoerr.h b/example/android/third_party/crypto/arm/include/internal/dsoerr.h new file mode 100644 index 00000000..a54a1854 --- /dev/null +++ b/example/android/third_party/crypto/arm/include/internal/dsoerr.h @@ -0,0 +1,83 @@ +/* + * Generated by util/mkerr.pl DO NOT EDIT + * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_DSOERR_H +# define HEADER_DSOERR_H + +# include + +# ifndef OPENSSL_NO_DSO + +# ifdef __cplusplus +extern "C" +# endif +int ERR_load_DSO_strings(void); + +/* + * DSO function codes. + */ +# define DSO_F_DLFCN_BIND_FUNC 100 +# define DSO_F_DLFCN_LOAD 102 +# define DSO_F_DLFCN_MERGER 130 +# define DSO_F_DLFCN_NAME_CONVERTER 123 +# define DSO_F_DLFCN_UNLOAD 103 +# define DSO_F_DL_BIND_FUNC 104 +# define DSO_F_DL_LOAD 106 +# define DSO_F_DL_MERGER 131 +# define DSO_F_DL_NAME_CONVERTER 124 +# define DSO_F_DL_UNLOAD 107 +# define DSO_F_DSO_BIND_FUNC 108 +# define DSO_F_DSO_CONVERT_FILENAME 126 +# define DSO_F_DSO_CTRL 110 +# define DSO_F_DSO_FREE 111 +# define DSO_F_DSO_GET_FILENAME 127 +# define DSO_F_DSO_GLOBAL_LOOKUP 139 +# define DSO_F_DSO_LOAD 112 +# define DSO_F_DSO_MERGE 132 +# define DSO_F_DSO_NEW_METHOD 113 +# define DSO_F_DSO_PATHBYADDR 105 +# define DSO_F_DSO_SET_FILENAME 129 +# define DSO_F_DSO_UP_REF 114 +# define DSO_F_VMS_BIND_SYM 115 +# define DSO_F_VMS_LOAD 116 +# define DSO_F_VMS_MERGER 133 +# define DSO_F_VMS_UNLOAD 117 +# define DSO_F_WIN32_BIND_FUNC 101 +# define DSO_F_WIN32_GLOBALLOOKUP 142 +# define DSO_F_WIN32_JOINER 135 +# define DSO_F_WIN32_LOAD 120 +# define DSO_F_WIN32_MERGER 134 +# define DSO_F_WIN32_NAME_CONVERTER 125 +# define DSO_F_WIN32_PATHBYADDR 109 +# define DSO_F_WIN32_SPLITTER 136 +# define DSO_F_WIN32_UNLOAD 121 + +/* + * DSO reason codes. + */ +# define DSO_R_CTRL_FAILED 100 +# define DSO_R_DSO_ALREADY_LOADED 110 +# define DSO_R_EMPTY_FILE_STRUCTURE 113 +# define DSO_R_FAILURE 114 +# define DSO_R_FILENAME_TOO_BIG 101 +# define DSO_R_FINISH_FAILED 102 +# define DSO_R_INCORRECT_FILE_SYNTAX 115 +# define DSO_R_LOAD_FAILED 103 +# define DSO_R_NAME_TRANSLATION_FAILED 109 +# define DSO_R_NO_FILENAME 111 +# define DSO_R_NULL_HANDLE 104 +# define DSO_R_SET_FILENAME_FAILED 112 +# define DSO_R_STACK_ERROR 105 +# define DSO_R_SYM_FAILURE 106 +# define DSO_R_UNLOAD_FAILED 107 +# define DSO_R_UNSUPPORTED 108 + +# endif +#endif diff --git a/example/android/third_party/crypto/arm/include/internal/err.h b/example/android/third_party/crypto/arm/include/internal/err.h new file mode 100644 index 00000000..d46b8bdb --- /dev/null +++ b/example/android/third_party/crypto/arm/include/internal/err.h @@ -0,0 +1,15 @@ +/* + * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef INTERNAL_ERR_H +# define INTERNAL_ERR_H + +void err_free_strings_int(void); + +#endif diff --git a/example/android/third_party/crypto/arm/include/internal/nelem.h b/example/android/third_party/crypto/arm/include/internal/nelem.h new file mode 100644 index 00000000..d65a21a9 --- /dev/null +++ b/example/android/third_party/crypto/arm/include/internal/nelem.h @@ -0,0 +1,14 @@ +/* + * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_NELEM_H +# define HEADER_NELEM_H + +# define OSSL_NELEM(x) (sizeof(x)/sizeof((x)[0])) +#endif diff --git a/example/android/third_party/crypto/arm/include/internal/numbers.h b/example/android/third_party/crypto/arm/include/internal/numbers.h new file mode 100644 index 00000000..31931df3 --- /dev/null +++ b/example/android/third_party/crypto/arm/include/internal/numbers.h @@ -0,0 +1,68 @@ +/* + * Copyright 2015-2016 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_NUMBERS_H +# define HEADER_NUMBERS_H + +# include + +# if (-1 & 3) == 0x03 /* Two's complement */ + +# define __MAXUINT__(T) ((T) -1) +# define __MAXINT__(T) ((T) ((((T) 1) << ((sizeof(T) * CHAR_BIT) - 1)) ^ __MAXUINT__(T))) +# define __MININT__(T) (-__MAXINT__(T) - 1) + +# elif (-1 & 3) == 0x02 /* One's complement */ + +# define __MAXUINT__(T) (((T) -1) + 1) +# define __MAXINT__(T) ((T) ((((T) 1) << ((sizeof(T) * CHAR_BIT) - 1)) ^ __MAXUINT__(T))) +# define __MININT__(T) (-__MAXINT__(T)) + +# elif (-1 & 3) == 0x01 /* Sign/magnitude */ + +# define __MAXINT__(T) ((T) (((((T) 1) << ((sizeof(T) * CHAR_BIT) - 2)) - 1) | (((T) 1) << ((sizeof(T) * CHAR_BIT) - 2)))) +# define __MAXUINT__(T) ((T) (__MAXINT__(T) | (((T) 1) << ((sizeof(T) * CHAR_BIT) - 1)))) +# define __MININT__(T) (-__MAXINT__(T)) + +# else + +# error "do not know the integer encoding on this architecture" + +# endif + +# ifndef INT8_MAX +# define INT8_MIN __MININT__(int8_t) +# define INT8_MAX __MAXINT__(int8_t) +# define UINT8_MAX __MAXUINT__(uint8_t) +# endif + +# ifndef INT16_MAX +# define INT16_MIN __MININT__(int16_t) +# define INT16_MAX __MAXINT__(int16_t) +# define UINT16_MAX __MAXUINT__(uint16_t) +# endif + +# ifndef INT32_MAX +# define INT32_MIN __MININT__(int32_t) +# define INT32_MAX __MAXINT__(int32_t) +# define UINT32_MAX __MAXUINT__(uint32_t) +# endif + +# ifndef INT64_MAX +# define INT64_MIN __MININT__(int64_t) +# define INT64_MAX __MAXINT__(int64_t) +# define UINT64_MAX __MAXUINT__(uint64_t) +# endif + +# ifndef SIZE_MAX +# define SIZE_MAX __MAXUINT__(size_t) +# endif + +#endif + diff --git a/example/android/third_party/crypto/arm/include/internal/o_dir.h b/example/android/third_party/crypto/arm/include/internal/o_dir.h new file mode 100644 index 00000000..e7b55e0c --- /dev/null +++ b/example/android/third_party/crypto/arm/include/internal/o_dir.h @@ -0,0 +1,52 @@ +/* + * Copyright 2004-2018 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +/* + * This file is dual-licensed and is also available under the following + * terms: + * + * Copyright (c) 2004, Richard Levitte + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef O_DIR_H +# define O_DIR_H + +typedef struct OPENSSL_dir_context_st OPENSSL_DIR_CTX; + +/* + * returns NULL on error or end-of-directory. If it is end-of-directory, + * errno will be zero + */ +const char *OPENSSL_DIR_read(OPENSSL_DIR_CTX **ctx, const char *directory); +/* returns 1 on success, 0 on error */ +int OPENSSL_DIR_end(OPENSSL_DIR_CTX **ctx); + +#endif /* LPDIR_H */ diff --git a/example/android/third_party/crypto/arm/include/internal/o_str.h b/example/android/third_party/crypto/arm/include/internal/o_str.h new file mode 100644 index 00000000..86403c9e --- /dev/null +++ b/example/android/third_party/crypto/arm/include/internal/o_str.h @@ -0,0 +1,17 @@ +/* + * Copyright 2003-2016 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_O_STR_H +# define HEADER_O_STR_H + +# include /* to get size_t */ + +int OPENSSL_memcmp(const void *p1, const void *p2, size_t n); + +#endif diff --git a/example/android/third_party/crypto/arm/include/internal/refcount.h b/example/android/third_party/crypto/arm/include/internal/refcount.h new file mode 100644 index 00000000..75d70a64 --- /dev/null +++ b/example/android/third_party/crypto/arm/include/internal/refcount.h @@ -0,0 +1,140 @@ +/* + * Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ +#ifndef HEADER_INTERNAL_REFCOUNT_H +# define HEADER_INTERNAL_REFCOUNT_H + +/* Used to checking reference counts, most while doing perl5 stuff :-) */ +# if defined(OPENSSL_NO_STDIO) +# if defined(REF_PRINT) +# error "REF_PRINT requires stdio" +# endif +# endif + +# if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L \ + && !defined(__STDC_NO_ATOMICS__) +# include +# define HAVE_C11_ATOMICS +# endif + +# if defined(HAVE_C11_ATOMICS) && defined(ATOMIC_INT_LOCK_FREE) \ + && ATOMIC_INT_LOCK_FREE > 0 + +# define HAVE_ATOMICS 1 + +typedef _Atomic int CRYPTO_REF_COUNT; + +static inline int CRYPTO_UP_REF(_Atomic int *val, int *ret, void *lock) +{ + *ret = atomic_fetch_add_explicit(val, 1, memory_order_relaxed) + 1; + return 1; +} + +/* + * Changes to shared structure other than reference counter have to be + * serialized. And any kind of serialization implies a release fence. This + * means that by the time reference counter is decremented all other + * changes are visible on all processors. Hence decrement itself can be + * relaxed. In case it hits zero, object will be destructed. Since it's + * last use of the object, destructor programmer might reason that access + * to mutable members doesn't have to be serialized anymore, which would + * otherwise imply an acquire fence. Hence conditional acquire fence... + */ +static inline int CRYPTO_DOWN_REF(_Atomic int *val, int *ret, void *lock) +{ + *ret = atomic_fetch_sub_explicit(val, 1, memory_order_relaxed) - 1; + if (*ret == 0) + atomic_thread_fence(memory_order_acquire); + return 1; +} + +# elif defined(__GNUC__) && defined(__ATOMIC_RELAXED) && __GCC_ATOMIC_INT_LOCK_FREE > 0 + +# define HAVE_ATOMICS 1 + +typedef int CRYPTO_REF_COUNT; + +static __inline__ int CRYPTO_UP_REF(int *val, int *ret, void *lock) +{ + *ret = __atomic_fetch_add(val, 1, __ATOMIC_RELAXED) + 1; + return 1; +} + +static __inline__ int CRYPTO_DOWN_REF(int *val, int *ret, void *lock) +{ + *ret = __atomic_fetch_sub(val, 1, __ATOMIC_RELAXED) - 1; + if (*ret == 0) + __atomic_thread_fence(__ATOMIC_ACQUIRE); + return 1; +} + +# elif defined(_MSC_VER) && _MSC_VER>=1200 + +# define HAVE_ATOMICS 1 + +typedef volatile int CRYPTO_REF_COUNT; + +# if (defined(_M_ARM) && _M_ARM>=7) || defined(_M_ARM64) +# include +# if defined(_M_ARM64) && !defined(_ARM_BARRIER_ISH) +# define _ARM_BARRIER_ISH _ARM64_BARRIER_ISH +# endif + +static __inline int CRYPTO_UP_REF(volatile int *val, int *ret, void *lock) +{ + *ret = _InterlockedExchangeAdd_nf(val, 1) + 1; + return 1; +} + +static __inline int CRYPTO_DOWN_REF(volatile int *val, int *ret, void *lock) +{ + *ret = _InterlockedExchangeAdd_nf(val, -1) - 1; + if (*ret == 0) + __dmb(_ARM_BARRIER_ISH); + return 1; +} +# else +# pragma intrinsic(_InterlockedExchangeAdd) + +static __inline int CRYPTO_UP_REF(volatile int *val, int *ret, void *lock) +{ + *ret = _InterlockedExchangeAdd(val, 1) + 1; + return 1; +} + +static __inline int CRYPTO_DOWN_REF(volatile int *val, int *ret, void *lock) +{ + *ret = _InterlockedExchangeAdd(val, -1) - 1; + return 1; +} +# endif + +# else + +typedef int CRYPTO_REF_COUNT; + +# define CRYPTO_UP_REF(val, ret, lock) CRYPTO_atomic_add(val, 1, ret, lock) +# define CRYPTO_DOWN_REF(val, ret, lock) CRYPTO_atomic_add(val, -1, ret, lock) + +# endif + +# if !defined(NDEBUG) && !defined(OPENSSL_NO_STDIO) +# define REF_ASSERT_ISNT(test) \ + (void)((test) ? (OPENSSL_die("refcount error", __FILE__, __LINE__), 1) : 0) +# else +# define REF_ASSERT_ISNT(i) +# endif + +# ifdef REF_PRINT +# define REF_PRINT_COUNT(a, b) \ + fprintf(stderr, "%p:%4d:%s\n", b, b->references, a) +# else +# define REF_PRINT_COUNT(a, b) +# endif + +#endif diff --git a/example/android/third_party/crypto/arm/include/internal/sockets.h b/example/android/third_party/crypto/arm/include/internal/sockets.h new file mode 100644 index 00000000..a6026dad --- /dev/null +++ b/example/android/third_party/crypto/arm/include/internal/sockets.h @@ -0,0 +1,159 @@ +/* + * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + + +#ifndef HEADER_INTERNAL_SOCKETS +# define HEADER_INTERNAL_SOCKETS + +# if defined(OPENSSL_SYS_VXWORKS) || defined(OPENSSL_SYS_UEFI) +# define NO_SYS_PARAM_H +# endif +# ifdef WIN32 +# define NO_SYS_UN_H +# endif +# ifdef OPENSSL_SYS_VMS +# define NO_SYS_PARAM_H +# define NO_SYS_UN_H +# endif + +# ifdef OPENSSL_NO_SOCK + +# elif defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) +# if defined(__DJGPP__) +# include +# include +# include +# include +# elif defined(_WIN32_WCE) && _WIN32_WCE<410 +# define getservbyname _masked_declaration_getservbyname +# endif +# if !defined(IPPROTO_IP) + /* winsock[2].h was included already? */ +# include +# endif +# ifdef getservbyname + /* this is used to be wcecompat/include/winsock_extras.h */ +# undef getservbyname +struct servent *PASCAL getservbyname(const char *, const char *); +# endif + +# ifdef _WIN64 +/* + * Even though sizeof(SOCKET) is 8, it's safe to cast it to int, because + * the value constitutes an index in per-process table of limited size + * and not a real pointer. And we also depend on fact that all processors + * Windows run on happen to be two's-complement, which allows to + * interchange INVALID_SOCKET and -1. + */ +# define socket(d,t,p) ((int)socket(d,t,p)) +# define accept(s,f,l) ((int)accept(s,f,l)) +# endif + +# else + +# ifndef NO_SYS_PARAM_H +# include +# endif +# ifdef OPENSSL_SYS_VXWORKS +# include +# endif + +# include +# if defined(OPENSSL_SYS_VMS_NODECC) +# include +# include +# include +# else +# include +# ifndef NO_SYS_UN_H +# ifdef OPENSSL_SYS_VXWORKS +# include +# else +# include +# endif +# ifndef UNIX_PATH_MAX +# define UNIX_PATH_MAX sizeof(((struct sockaddr_un *)NULL)->sun_path) +# endif +# endif +# ifdef FILIO_H +# include /* FIONBIO in some SVR4, e.g. unixware, solaris */ +# endif +# include +# include +# include +# endif + +# ifdef OPENSSL_SYS_AIX +# include +# endif + +# ifndef VMS +# include +# else +# if !defined(TCPIP_TYPE_SOCKETSHR) && defined(__VMS_VER) && (__VMS_VER > 70000000) + /* ioctl is only in VMS > 7.0 and when socketshr is not used */ +# include +# endif +# include +# if defined(TCPIP_TYPE_SOCKETSHR) +# include +# endif +# endif + +# ifndef INVALID_SOCKET +# define INVALID_SOCKET (-1) +# endif +# endif + +/* + * Some IPv6 implementations are broken, you can disable them in known + * bad versions. + */ +# if !defined(OPENSSL_USE_IPV6) +# if defined(AF_INET6) +# define OPENSSL_USE_IPV6 1 +# else +# define OPENSSL_USE_IPV6 0 +# endif +# endif + +# define get_last_socket_error() errno +# define clear_socket_error() errno=0 + +# if defined(OPENSSL_SYS_WINDOWS) +# undef get_last_socket_error +# undef clear_socket_error +# define get_last_socket_error() WSAGetLastError() +# define clear_socket_error() WSASetLastError(0) +# define readsocket(s,b,n) recv((s),(b),(n),0) +# define writesocket(s,b,n) send((s),(b),(n),0) +# elif defined(__DJGPP__) +# define WATT32 +# define WATT32_NO_OLDIES +# define closesocket(s) close_s(s) +# define readsocket(s,b,n) read_s(s,b,n) +# define writesocket(s,b,n) send(s,b,n,0) +# elif defined(OPENSSL_SYS_VMS) +# define ioctlsocket(a,b,c) ioctl(a,b,c) +# define closesocket(s) close(s) +# define readsocket(s,b,n) recv((s),(b),(n),0) +# define writesocket(s,b,n) send((s),(b),(n),0) +# elif defined(OPENSSL_SYS_VXWORKS) +# define ioctlsocket(a,b,c) ioctl((a),(b),(int)(c)) +# define closesocket(s) close(s) +# define readsocket(s,b,n) read((s),(b),(n)) +# define writesocket(s,b,n) write((s),(char *)(b),(n)) +# else +# define ioctlsocket(a,b,c) ioctl(a,b,c) +# define closesocket(s) close(s) +# define readsocket(s,b,n) read((s),(b),(n)) +# define writesocket(s,b,n) write((s),(b),(n)) +# endif + +#endif diff --git a/example/android/third_party/crypto/arm/include/internal/sslconf.h b/example/android/third_party/crypto/arm/include/internal/sslconf.h new file mode 100644 index 00000000..d538f861 --- /dev/null +++ b/example/android/third_party/crypto/arm/include/internal/sslconf.h @@ -0,0 +1,20 @@ +/* + * Copyright 2018 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_SSLCONF_H +# define HEADER_SSLCONF_H + +typedef struct ssl_conf_cmd_st SSL_CONF_CMD; + +const SSL_CONF_CMD *conf_ssl_get(size_t idx, const char **name, size_t *cnt); +int conf_ssl_name_find(const char *name, size_t *idx); +void conf_ssl_get_cmd(const SSL_CONF_CMD *cmd, size_t idx, char **cmdstr, + char **arg); + +#endif diff --git a/example/android/third_party/crypto/arm/include/internal/thread_once.h b/example/android/third_party/crypto/arm/include/internal/thread_once.h new file mode 100644 index 00000000..22424435 --- /dev/null +++ b/example/android/third_party/crypto/arm/include/internal/thread_once.h @@ -0,0 +1,45 @@ +/* + * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#include + +#define DEFINE_RUN_ONCE(init) \ + static int init(void); \ + int init##_ossl_ret_ = 0; \ + void init##_ossl_(void) \ + { \ + init##_ossl_ret_ = init(); \ + } \ + static int init(void) +#define DECLARE_RUN_ONCE(init) \ + extern int init##_ossl_ret_; \ + void init##_ossl_(void); + +#define DEFINE_RUN_ONCE_STATIC(init) \ + static int init(void); \ + static int init##_ossl_ret_ = 0; \ + static void init##_ossl_(void) \ + { \ + init##_ossl_ret_ = init(); \ + } \ + static int init(void) + +/* + * RUN_ONCE - use CRYPTO_THREAD_run_once, and check if the init succeeded + * @once: pointer to static object of type CRYPTO_ONCE + * @init: function name that was previously given to DEFINE_RUN_ONCE, + * DEFINE_RUN_ONCE_STATIC or DECLARE_RUN_ONCE. This function + * must return 1 for success or 0 for failure. + * + * The return value is 1 on success (*) or 0 in case of error. + * + * (*) by convention, since the init function must return 1 on success. + */ +#define RUN_ONCE(once, init) \ + (CRYPTO_THREAD_run_once(once, init##_ossl_) ? init##_ossl_ret_ : 0) diff --git a/example/android/third_party/crypto/arm/include/internal/tsan_assist.h b/example/android/third_party/crypto/arm/include/internal/tsan_assist.h new file mode 100644 index 00000000..f30ffe39 --- /dev/null +++ b/example/android/third_party/crypto/arm/include/internal/tsan_assist.h @@ -0,0 +1,144 @@ +/* + * Copyright 2018 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +/* + * Contemporary compilers implement lock-free atomic memory access + * primitives that facilitate writing "thread-opportunistic" or even real + * multi-threading low-overhead code. "Thread-opportunistic" is when + * exact result is not required, e.g. some statistics, or execution flow + * doesn't have to be unambiguous. Simplest example is lazy "constant" + * initialization when one can synchronize on variable itself, e.g. + * + * if (var == NOT_YET_INITIALIZED) + * var = function_returning_same_value(); + * + * This does work provided that loads and stores are single-instuction + * operations (and integer ones are on *all* supported platforms), but + * it upsets Thread Sanitizer. Suggested solution is + * + * if (tsan_load(&var) == NOT_YET_INITIALIZED) + * tsan_store(&var, function_returning_same_value()); + * + * Production machine code would be the same, so one can wonder why + * bother. Having Thread Sanitizer accept "thread-opportunistic" code + * allows to move on trouble-shooting real bugs. + * + * Resolving Thread Sanitizer nits was the initial purpose for this module, + * but it was later extended with more nuanced primitives that are useful + * even in "non-opportunistic" scenarios. Most notably verifying if a shared + * structure is fully initialized and bypassing the initialization lock. + * It's suggested to view macros defined in this module as "annotations" for + * thread-safe lock-free code, "Thread-Safe ANnotations"... + * + * It's assumed that ATOMIC_{LONG|INT}_LOCK_FREE are assigned same value as + * ATOMIC_POINTER_LOCK_FREE. And check for >= 2 ensures that corresponding + * code is inlined. It should be noted that statistics counters become + * accurate in such case. + * + * Special note about TSAN_QUALIFIER. It might be undesired to use it in + * a shared header. Because whether operation on specific variable or member + * is atomic or not might be irrelevant in other modules. In such case one + * can use TSAN_QUALIFIER in cast specifically when it has to count. + */ + +#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L \ + && !defined(__STDC_NO_ATOMICS__) +# include + +# if defined(ATOMIC_POINTER_LOCK_FREE) \ + && ATOMIC_POINTER_LOCK_FREE >= 2 +# define TSAN_QUALIFIER _Atomic +# define tsan_load(ptr) atomic_load_explicit((ptr), memory_order_relaxed) +# define tsan_store(ptr, val) atomic_store_explicit((ptr), (val), memory_order_relaxed) +# define tsan_counter(ptr) atomic_fetch_add_explicit((ptr), 1, memory_order_relaxed) +# define tsan_decr(ptr) atomic_fetch_add_explicit((ptr), -1, memory_order_relaxed) +# define tsan_ld_acq(ptr) atomic_load_explicit((ptr), memory_order_acquire) +# define tsan_st_rel(ptr, val) atomic_store_explicit((ptr), (val), memory_order_release) +# endif + +#elif defined(__GNUC__) && defined(__ATOMIC_RELAXED) + +# if defined(__GCC_ATOMIC_POINTER_LOCK_FREE) \ + && __GCC_ATOMIC_POINTER_LOCK_FREE >= 2 +# define TSAN_QUALIFIER volatile +# define tsan_load(ptr) __atomic_load_n((ptr), __ATOMIC_RELAXED) +# define tsan_store(ptr, val) __atomic_store_n((ptr), (val), __ATOMIC_RELAXED) +# define tsan_counter(ptr) __atomic_fetch_add((ptr), 1, __ATOMIC_RELAXED) +# define tsan_decr(ptr) __atomic_fetch_add((ptr), -1, __ATOMIC_RELAXED) +# define tsan_ld_acq(ptr) __atomic_load_n((ptr), __ATOMIC_ACQUIRE) +# define tsan_st_rel(ptr, val) __atomic_store_n((ptr), (val), __ATOMIC_RELEASE) +# endif + +#elif defined(_MSC_VER) && _MSC_VER>=1200 \ + && (defined(_M_IX86) || defined(_M_AMD64) || defined(_M_X64) || \ + defined(_M_ARM64) || (defined(_M_ARM) && _M_ARM >= 7)) +/* + * There is subtle dependency on /volatile: command-line option. + * "ms" implies same semantic as memory_order_acquire for loads and + * memory_order_release for stores, while "iso" - memory_order_relaxed for + * either. Real complication is that defaults are different on x86 and ARM. + * There is explanation for that, "ms" is backward compatible with earlier + * compiler versions, while multi-processor ARM can be viewed as brand new + * platform to MSC and its users, and with non-relaxed semantic taking toll + * with additional instructions and penalties, it kind of makes sense to + * default to "iso"... + */ +# define TSAN_QUALIFIER volatile +# if defined(_M_ARM) || defined(_M_ARM64) +# define _InterlockedExchangeAdd _InterlockedExchangeAdd_nf +# pragma intrinsic(_InterlockedExchangeAdd_nf) +# pragma intrinsic(__iso_volatile_load32, __iso_volatile_store32) +# ifdef _WIN64 +# define _InterlockedExchangeAdd64 _InterlockedExchangeAdd64_nf +# pragma intrinsic(_InterlockedExchangeAdd64_nf) +# pragma intrinsic(__iso_volatile_load64, __iso_volatile_store64) +# define tsan_load(ptr) (sizeof(*(ptr)) == 8 ? __iso_volatile_load64(ptr) \ + : __iso_volatile_load32(ptr)) +# define tsan_store(ptr, val) (sizeof(*(ptr)) == 8 ? __iso_volatile_store64((ptr), (val)) \ + : __iso_volatile_store32((ptr), (val))) +# else +# define tsan_load(ptr) __iso_volatile_load32(ptr) +# define tsan_store(ptr, val) __iso_volatile_store32((ptr), (val)) +# endif +# else +# define tsan_load(ptr) (*(ptr)) +# define tsan_store(ptr, val) (*(ptr) = (val)) +# endif +# pragma intrinsic(_InterlockedExchangeAdd) +# ifdef _WIN64 +# pragma intrinsic(_InterlockedExchangeAdd64) +# define tsan_counter(ptr) (sizeof(*(ptr)) == 8 ? _InterlockedExchangeAdd64((ptr), 1) \ + : _InterlockedExchangeAdd((ptr), 1)) +# define tsan_decr(ptr) (sizeof(*(ptr)) == 8 ? _InterlockedExchangeAdd64((ptr), -1) \ + : _InterlockedExchangeAdd((ptr), -1)) +# else +# define tsan_counter(ptr) _InterlockedExchangeAdd((ptr), 1) +# define tsan_decr(ptr) _InterlockedExchangeAdd((ptr), -1) +# endif +# if !defined(_ISO_VOLATILE) +# define tsan_ld_acq(ptr) (*(ptr)) +# define tsan_st_rel(ptr, val) (*(ptr) = (val)) +# endif + +#endif + +#ifndef TSAN_QUALIFIER + +# define TSAN_QUALIFIER volatile +# define tsan_load(ptr) (*(ptr)) +# define tsan_store(ptr, val) (*(ptr) = (val)) +# define tsan_counter(ptr) ((*(ptr))++) +# define tsan_decr(ptr) ((*(ptr))--) +/* + * Lack of tsan_ld_acq and tsan_ld_rel means that compiler support is not + * sophisticated enough to support them. Code that relies on them should be + * protected with #ifdef tsan_ld_acq with locked fallback. + */ + +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/__DECC_INCLUDE_EPILOGUE.H b/example/android/third_party/crypto/arm/include/openssl/__DECC_INCLUDE_EPILOGUE.H new file mode 100644 index 00000000..c350018a --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/__DECC_INCLUDE_EPILOGUE.H @@ -0,0 +1,16 @@ +/* + * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +/* + * This file is only used by HP C on VMS, and is included automatically + * after each header file from this directory + */ + +/* restore state. Must correspond to the save in __decc_include_prologue.h */ +#pragma names restore diff --git a/example/android/third_party/crypto/arm/include/openssl/__DECC_INCLUDE_PROLOGUE.H b/example/android/third_party/crypto/arm/include/openssl/__DECC_INCLUDE_PROLOGUE.H new file mode 100644 index 00000000..9a9c777f --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/__DECC_INCLUDE_PROLOGUE.H @@ -0,0 +1,20 @@ +/* + * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +/* + * This file is only used by HP C on VMS, and is included automatically + * after each header file from this directory + */ + +/* save state */ +#pragma names save +/* have the compiler shorten symbols larger than 31 chars to 23 chars + * followed by a 8 hex char CRC + */ +#pragma names as_is,shortened diff --git a/example/android/third_party/crypto/arm/include/openssl/aes.h b/example/android/third_party/crypto/arm/include/openssl/aes.h new file mode 100644 index 00000000..245c552a --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/aes.h @@ -0,0 +1,92 @@ +/* + * Copyright 2002-2016 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_AES_H +# define HEADER_AES_H + +# include + +# include +# ifdef __cplusplus +extern "C" { +# endif + +# define AES_ENCRYPT 1 +# define AES_DECRYPT 0 + +/* + * Because array size can't be a const in C, the following two are macros. + * Both sizes are in bytes. + */ +# define AES_MAXNR 14 +# define AES_BLOCK_SIZE 16 + +/* This should be a hidden type, but EVP requires that the size be known */ +struct aes_key_st { +# ifdef AES_LONG + unsigned long rd_key[4 * (AES_MAXNR + 1)]; +# else + unsigned int rd_key[4 * (AES_MAXNR + 1)]; +# endif + int rounds; +}; +typedef struct aes_key_st AES_KEY; + +const char *AES_options(void); + +int AES_set_encrypt_key(const unsigned char *userKey, const int bits, + AES_KEY *key); +int AES_set_decrypt_key(const unsigned char *userKey, const int bits, + AES_KEY *key); + +void AES_encrypt(const unsigned char *in, unsigned char *out, + const AES_KEY *key); +void AES_decrypt(const unsigned char *in, unsigned char *out, + const AES_KEY *key); + +void AES_ecb_encrypt(const unsigned char *in, unsigned char *out, + const AES_KEY *key, const int enc); +void AES_cbc_encrypt(const unsigned char *in, unsigned char *out, + size_t length, const AES_KEY *key, + unsigned char *ivec, const int enc); +void AES_cfb128_encrypt(const unsigned char *in, unsigned char *out, + size_t length, const AES_KEY *key, + unsigned char *ivec, int *num, const int enc); +void AES_cfb1_encrypt(const unsigned char *in, unsigned char *out, + size_t length, const AES_KEY *key, + unsigned char *ivec, int *num, const int enc); +void AES_cfb8_encrypt(const unsigned char *in, unsigned char *out, + size_t length, const AES_KEY *key, + unsigned char *ivec, int *num, const int enc); +void AES_ofb128_encrypt(const unsigned char *in, unsigned char *out, + size_t length, const AES_KEY *key, + unsigned char *ivec, int *num); +/* NB: the IV is _two_ blocks long */ +void AES_ige_encrypt(const unsigned char *in, unsigned char *out, + size_t length, const AES_KEY *key, + unsigned char *ivec, const int enc); +/* NB: the IV is _four_ blocks long */ +void AES_bi_ige_encrypt(const unsigned char *in, unsigned char *out, + size_t length, const AES_KEY *key, + const AES_KEY *key2, const unsigned char *ivec, + const int enc); + +int AES_wrap_key(AES_KEY *key, const unsigned char *iv, + unsigned char *out, + const unsigned char *in, unsigned int inlen); +int AES_unwrap_key(AES_KEY *key, const unsigned char *iv, + unsigned char *out, + const unsigned char *in, unsigned int inlen); + + +# ifdef __cplusplus +} +# endif + +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/asn1.h b/example/android/third_party/crypto/arm/include/openssl/asn1.h new file mode 100644 index 00000000..9522eec1 --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/asn1.h @@ -0,0 +1,886 @@ +/* + * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_ASN1_H +# define HEADER_ASN1_H + +# include +# include +# include +# include +# include +# include +# include + +# include +# if OPENSSL_API_COMPAT < 0x10100000L +# include +# endif + +# ifdef OPENSSL_BUILD_SHLIBCRYPTO +# undef OPENSSL_EXTERN +# define OPENSSL_EXTERN OPENSSL_EXPORT +# endif + +#ifdef __cplusplus +extern "C" { +#endif + +# define V_ASN1_UNIVERSAL 0x00 +# define V_ASN1_APPLICATION 0x40 +# define V_ASN1_CONTEXT_SPECIFIC 0x80 +# define V_ASN1_PRIVATE 0xc0 + +# define V_ASN1_CONSTRUCTED 0x20 +# define V_ASN1_PRIMITIVE_TAG 0x1f +# define V_ASN1_PRIMATIVE_TAG /*compat*/ V_ASN1_PRIMITIVE_TAG + +# define V_ASN1_APP_CHOOSE -2/* let the recipient choose */ +# define V_ASN1_OTHER -3/* used in ASN1_TYPE */ +# define V_ASN1_ANY -4/* used in ASN1 template code */ + +# define V_ASN1_UNDEF -1 +/* ASN.1 tag values */ +# define V_ASN1_EOC 0 +# define V_ASN1_BOOLEAN 1 /**/ +# define V_ASN1_INTEGER 2 +# define V_ASN1_BIT_STRING 3 +# define V_ASN1_OCTET_STRING 4 +# define V_ASN1_NULL 5 +# define V_ASN1_OBJECT 6 +# define V_ASN1_OBJECT_DESCRIPTOR 7 +# define V_ASN1_EXTERNAL 8 +# define V_ASN1_REAL 9 +# define V_ASN1_ENUMERATED 10 +# define V_ASN1_UTF8STRING 12 +# define V_ASN1_SEQUENCE 16 +# define V_ASN1_SET 17 +# define V_ASN1_NUMERICSTRING 18 /**/ +# define V_ASN1_PRINTABLESTRING 19 +# define V_ASN1_T61STRING 20 +# define V_ASN1_TELETEXSTRING 20/* alias */ +# define V_ASN1_VIDEOTEXSTRING 21 /**/ +# define V_ASN1_IA5STRING 22 +# define V_ASN1_UTCTIME 23 +# define V_ASN1_GENERALIZEDTIME 24 /**/ +# define V_ASN1_GRAPHICSTRING 25 /**/ +# define V_ASN1_ISO64STRING 26 /**/ +# define V_ASN1_VISIBLESTRING 26/* alias */ +# define V_ASN1_GENERALSTRING 27 /**/ +# define V_ASN1_UNIVERSALSTRING 28 /**/ +# define V_ASN1_BMPSTRING 30 + +/* + * NB the constants below are used internally by ASN1_INTEGER + * and ASN1_ENUMERATED to indicate the sign. They are *not* on + * the wire tag values. + */ + +# define V_ASN1_NEG 0x100 +# define V_ASN1_NEG_INTEGER (2 | V_ASN1_NEG) +# define V_ASN1_NEG_ENUMERATED (10 | V_ASN1_NEG) + +/* For use with d2i_ASN1_type_bytes() */ +# define B_ASN1_NUMERICSTRING 0x0001 +# define B_ASN1_PRINTABLESTRING 0x0002 +# define B_ASN1_T61STRING 0x0004 +# define B_ASN1_TELETEXSTRING 0x0004 +# define B_ASN1_VIDEOTEXSTRING 0x0008 +# define B_ASN1_IA5STRING 0x0010 +# define B_ASN1_GRAPHICSTRING 0x0020 +# define B_ASN1_ISO64STRING 0x0040 +# define B_ASN1_VISIBLESTRING 0x0040 +# define B_ASN1_GENERALSTRING 0x0080 +# define B_ASN1_UNIVERSALSTRING 0x0100 +# define B_ASN1_OCTET_STRING 0x0200 +# define B_ASN1_BIT_STRING 0x0400 +# define B_ASN1_BMPSTRING 0x0800 +# define B_ASN1_UNKNOWN 0x1000 +# define B_ASN1_UTF8STRING 0x2000 +# define B_ASN1_UTCTIME 0x4000 +# define B_ASN1_GENERALIZEDTIME 0x8000 +# define B_ASN1_SEQUENCE 0x10000 +/* For use with ASN1_mbstring_copy() */ +# define MBSTRING_FLAG 0x1000 +# define MBSTRING_UTF8 (MBSTRING_FLAG) +# define MBSTRING_ASC (MBSTRING_FLAG|1) +# define MBSTRING_BMP (MBSTRING_FLAG|2) +# define MBSTRING_UNIV (MBSTRING_FLAG|4) +# define SMIME_OLDMIME 0x400 +# define SMIME_CRLFEOL 0x800 +# define SMIME_STREAM 0x1000 + struct X509_algor_st; +DEFINE_STACK_OF(X509_ALGOR) + +# define ASN1_STRING_FLAG_BITS_LEFT 0x08/* Set if 0x07 has bits left value */ +/* + * This indicates that the ASN1_STRING is not a real value but just a place + * holder for the location where indefinite length constructed data should be + * inserted in the memory buffer + */ +# define ASN1_STRING_FLAG_NDEF 0x010 + +/* + * This flag is used by the CMS code to indicate that a string is not + * complete and is a place holder for content when it had all been accessed. + * The flag will be reset when content has been written to it. + */ + +# define ASN1_STRING_FLAG_CONT 0x020 +/* + * This flag is used by ASN1 code to indicate an ASN1_STRING is an MSTRING + * type. + */ +# define ASN1_STRING_FLAG_MSTRING 0x040 +/* String is embedded and only content should be freed */ +# define ASN1_STRING_FLAG_EMBED 0x080 +/* String should be parsed in RFC 5280's time format */ +# define ASN1_STRING_FLAG_X509_TIME 0x100 +/* This is the base type that holds just about everything :-) */ +struct asn1_string_st { + int length; + int type; + unsigned char *data; + /* + * The value of the following field depends on the type being held. It + * is mostly being used for BIT_STRING so if the input data has a + * non-zero 'unused bits' value, it will be handled correctly + */ + long flags; +}; + +/* + * ASN1_ENCODING structure: this is used to save the received encoding of an + * ASN1 type. This is useful to get round problems with invalid encodings + * which can break signatures. + */ + +typedef struct ASN1_ENCODING_st { + unsigned char *enc; /* DER encoding */ + long len; /* Length of encoding */ + int modified; /* set to 1 if 'enc' is invalid */ +} ASN1_ENCODING; + +/* Used with ASN1 LONG type: if a long is set to this it is omitted */ +# define ASN1_LONG_UNDEF 0x7fffffffL + +# define STABLE_FLAGS_MALLOC 0x01 +/* + * A zero passed to ASN1_STRING_TABLE_new_add for the flags is interpreted + * as "don't change" and STABLE_FLAGS_MALLOC is always set. By setting + * STABLE_FLAGS_MALLOC only we can clear the existing value. Use the alias + * STABLE_FLAGS_CLEAR to reflect this. + */ +# define STABLE_FLAGS_CLEAR STABLE_FLAGS_MALLOC +# define STABLE_NO_MASK 0x02 +# define DIRSTRING_TYPE \ + (B_ASN1_PRINTABLESTRING|B_ASN1_T61STRING|B_ASN1_BMPSTRING|B_ASN1_UTF8STRING) +# define PKCS9STRING_TYPE (DIRSTRING_TYPE|B_ASN1_IA5STRING) + +typedef struct asn1_string_table_st { + int nid; + long minsize; + long maxsize; + unsigned long mask; + unsigned long flags; +} ASN1_STRING_TABLE; + +DEFINE_STACK_OF(ASN1_STRING_TABLE) + +/* size limits: this stuff is taken straight from RFC2459 */ + +# define ub_name 32768 +# define ub_common_name 64 +# define ub_locality_name 128 +# define ub_state_name 128 +# define ub_organization_name 64 +# define ub_organization_unit_name 64 +# define ub_title 64 +# define ub_email_address 128 + +/* + * Declarations for template structures: for full definitions see asn1t.h + */ +typedef struct ASN1_TEMPLATE_st ASN1_TEMPLATE; +typedef struct ASN1_TLC_st ASN1_TLC; +/* This is just an opaque pointer */ +typedef struct ASN1_VALUE_st ASN1_VALUE; + +/* Declare ASN1 functions: the implement macro in in asn1t.h */ + +# define DECLARE_ASN1_FUNCTIONS(type) DECLARE_ASN1_FUNCTIONS_name(type, type) + +# define DECLARE_ASN1_ALLOC_FUNCTIONS(type) \ + DECLARE_ASN1_ALLOC_FUNCTIONS_name(type, type) + +# define DECLARE_ASN1_FUNCTIONS_name(type, name) \ + DECLARE_ASN1_ALLOC_FUNCTIONS_name(type, name) \ + DECLARE_ASN1_ENCODE_FUNCTIONS(type, name, name) + +# define DECLARE_ASN1_FUNCTIONS_fname(type, itname, name) \ + DECLARE_ASN1_ALLOC_FUNCTIONS_name(type, name) \ + DECLARE_ASN1_ENCODE_FUNCTIONS(type, itname, name) + +# define DECLARE_ASN1_ENCODE_FUNCTIONS(type, itname, name) \ + type *d2i_##name(type **a, const unsigned char **in, long len); \ + int i2d_##name(type *a, unsigned char **out); \ + DECLARE_ASN1_ITEM(itname) + +# define DECLARE_ASN1_ENCODE_FUNCTIONS_const(type, name) \ + type *d2i_##name(type **a, const unsigned char **in, long len); \ + int i2d_##name(const type *a, unsigned char **out); \ + DECLARE_ASN1_ITEM(name) + +# define DECLARE_ASN1_NDEF_FUNCTION(name) \ + int i2d_##name##_NDEF(name *a, unsigned char **out); + +# define DECLARE_ASN1_FUNCTIONS_const(name) \ + DECLARE_ASN1_ALLOC_FUNCTIONS(name) \ + DECLARE_ASN1_ENCODE_FUNCTIONS_const(name, name) + +# define DECLARE_ASN1_ALLOC_FUNCTIONS_name(type, name) \ + type *name##_new(void); \ + void name##_free(type *a); + +# define DECLARE_ASN1_PRINT_FUNCTION(stname) \ + DECLARE_ASN1_PRINT_FUNCTION_fname(stname, stname) + +# define DECLARE_ASN1_PRINT_FUNCTION_fname(stname, fname) \ + int fname##_print_ctx(BIO *out, stname *x, int indent, \ + const ASN1_PCTX *pctx); + +# define D2I_OF(type) type *(*)(type **,const unsigned char **,long) +# define I2D_OF(type) int (*)(type *,unsigned char **) +# define I2D_OF_const(type) int (*)(const type *,unsigned char **) + +# define CHECKED_D2I_OF(type, d2i) \ + ((d2i_of_void*) (1 ? d2i : ((D2I_OF(type))0))) +# define CHECKED_I2D_OF(type, i2d) \ + ((i2d_of_void*) (1 ? i2d : ((I2D_OF(type))0))) +# define CHECKED_NEW_OF(type, xnew) \ + ((void *(*)(void)) (1 ? xnew : ((type *(*)(void))0))) +# define CHECKED_PTR_OF(type, p) \ + ((void*) (1 ? p : (type*)0)) +# define CHECKED_PPTR_OF(type, p) \ + ((void**) (1 ? p : (type**)0)) + +# define TYPEDEF_D2I_OF(type) typedef type *d2i_of_##type(type **,const unsigned char **,long) +# define TYPEDEF_I2D_OF(type) typedef int i2d_of_##type(type *,unsigned char **) +# define TYPEDEF_D2I2D_OF(type) TYPEDEF_D2I_OF(type); TYPEDEF_I2D_OF(type) + +TYPEDEF_D2I2D_OF(void); + +/*- + * The following macros and typedefs allow an ASN1_ITEM + * to be embedded in a structure and referenced. Since + * the ASN1_ITEM pointers need to be globally accessible + * (possibly from shared libraries) they may exist in + * different forms. On platforms that support it the + * ASN1_ITEM structure itself will be globally exported. + * Other platforms will export a function that returns + * an ASN1_ITEM pointer. + * + * To handle both cases transparently the macros below + * should be used instead of hard coding an ASN1_ITEM + * pointer in a structure. + * + * The structure will look like this: + * + * typedef struct SOMETHING_st { + * ... + * ASN1_ITEM_EXP *iptr; + * ... + * } SOMETHING; + * + * It would be initialised as e.g.: + * + * SOMETHING somevar = {...,ASN1_ITEM_ref(X509),...}; + * + * and the actual pointer extracted with: + * + * const ASN1_ITEM *it = ASN1_ITEM_ptr(somevar.iptr); + * + * Finally an ASN1_ITEM pointer can be extracted from an + * appropriate reference with: ASN1_ITEM_rptr(X509). This + * would be used when a function takes an ASN1_ITEM * argument. + * + */ + +# ifndef OPENSSL_EXPORT_VAR_AS_FUNCTION + +/* ASN1_ITEM pointer exported type */ +typedef const ASN1_ITEM ASN1_ITEM_EXP; + +/* Macro to obtain ASN1_ITEM pointer from exported type */ +# define ASN1_ITEM_ptr(iptr) (iptr) + +/* Macro to include ASN1_ITEM pointer from base type */ +# define ASN1_ITEM_ref(iptr) (&(iptr##_it)) + +# define ASN1_ITEM_rptr(ref) (&(ref##_it)) + +# define DECLARE_ASN1_ITEM(name) \ + OPENSSL_EXTERN const ASN1_ITEM name##_it; + +# else + +/* + * Platforms that can't easily handle shared global variables are declared as + * functions returning ASN1_ITEM pointers. + */ + +/* ASN1_ITEM pointer exported type */ +typedef const ASN1_ITEM *ASN1_ITEM_EXP (void); + +/* Macro to obtain ASN1_ITEM pointer from exported type */ +# define ASN1_ITEM_ptr(iptr) (iptr()) + +/* Macro to include ASN1_ITEM pointer from base type */ +# define ASN1_ITEM_ref(iptr) (iptr##_it) + +# define ASN1_ITEM_rptr(ref) (ref##_it()) + +# define DECLARE_ASN1_ITEM(name) \ + const ASN1_ITEM * name##_it(void); + +# endif + +/* Parameters used by ASN1_STRING_print_ex() */ + +/* + * These determine which characters to escape: RFC2253 special characters, + * control characters and MSB set characters + */ + +# define ASN1_STRFLGS_ESC_2253 1 +# define ASN1_STRFLGS_ESC_CTRL 2 +# define ASN1_STRFLGS_ESC_MSB 4 + +/* + * This flag determines how we do escaping: normally RC2253 backslash only, + * set this to use backslash and quote. + */ + +# define ASN1_STRFLGS_ESC_QUOTE 8 + +/* These three flags are internal use only. */ + +/* Character is a valid PrintableString character */ +# define CHARTYPE_PRINTABLESTRING 0x10 +/* Character needs escaping if it is the first character */ +# define CHARTYPE_FIRST_ESC_2253 0x20 +/* Character needs escaping if it is the last character */ +# define CHARTYPE_LAST_ESC_2253 0x40 + +/* + * NB the internal flags are safely reused below by flags handled at the top + * level. + */ + +/* + * If this is set we convert all character strings to UTF8 first + */ + +# define ASN1_STRFLGS_UTF8_CONVERT 0x10 + +/* + * If this is set we don't attempt to interpret content: just assume all + * strings are 1 byte per character. This will produce some pretty odd + * looking output! + */ + +# define ASN1_STRFLGS_IGNORE_TYPE 0x20 + +/* If this is set we include the string type in the output */ +# define ASN1_STRFLGS_SHOW_TYPE 0x40 + +/* + * This determines which strings to display and which to 'dump' (hex dump of + * content octets or DER encoding). We can only dump non character strings or + * everything. If we don't dump 'unknown' they are interpreted as character + * strings with 1 octet per character and are subject to the usual escaping + * options. + */ + +# define ASN1_STRFLGS_DUMP_ALL 0x80 +# define ASN1_STRFLGS_DUMP_UNKNOWN 0x100 + +/* + * These determine what 'dumping' does, we can dump the content octets or the + * DER encoding: both use the RFC2253 #XXXXX notation. + */ + +# define ASN1_STRFLGS_DUMP_DER 0x200 + +/* + * This flag specifies that RC2254 escaping shall be performed. + */ +#define ASN1_STRFLGS_ESC_2254 0x400 + +/* + * All the string flags consistent with RFC2253, escaping control characters + * isn't essential in RFC2253 but it is advisable anyway. + */ + +# define ASN1_STRFLGS_RFC2253 (ASN1_STRFLGS_ESC_2253 | \ + ASN1_STRFLGS_ESC_CTRL | \ + ASN1_STRFLGS_ESC_MSB | \ + ASN1_STRFLGS_UTF8_CONVERT | \ + ASN1_STRFLGS_DUMP_UNKNOWN | \ + ASN1_STRFLGS_DUMP_DER) + +DEFINE_STACK_OF(ASN1_INTEGER) + +DEFINE_STACK_OF(ASN1_GENERALSTRING) + +DEFINE_STACK_OF(ASN1_UTF8STRING) + +typedef struct asn1_type_st { + int type; + union { + char *ptr; + ASN1_BOOLEAN boolean; + ASN1_STRING *asn1_string; + ASN1_OBJECT *object; + ASN1_INTEGER *integer; + ASN1_ENUMERATED *enumerated; + ASN1_BIT_STRING *bit_string; + ASN1_OCTET_STRING *octet_string; + ASN1_PRINTABLESTRING *printablestring; + ASN1_T61STRING *t61string; + ASN1_IA5STRING *ia5string; + ASN1_GENERALSTRING *generalstring; + ASN1_BMPSTRING *bmpstring; + ASN1_UNIVERSALSTRING *universalstring; + ASN1_UTCTIME *utctime; + ASN1_GENERALIZEDTIME *generalizedtime; + ASN1_VISIBLESTRING *visiblestring; + ASN1_UTF8STRING *utf8string; + /* + * set and sequence are left complete and still contain the set or + * sequence bytes + */ + ASN1_STRING *set; + ASN1_STRING *sequence; + ASN1_VALUE *asn1_value; + } value; +} ASN1_TYPE; + +DEFINE_STACK_OF(ASN1_TYPE) + +typedef STACK_OF(ASN1_TYPE) ASN1_SEQUENCE_ANY; + +DECLARE_ASN1_ENCODE_FUNCTIONS_const(ASN1_SEQUENCE_ANY, ASN1_SEQUENCE_ANY) +DECLARE_ASN1_ENCODE_FUNCTIONS_const(ASN1_SEQUENCE_ANY, ASN1_SET_ANY) + +/* This is used to contain a list of bit names */ +typedef struct BIT_STRING_BITNAME_st { + int bitnum; + const char *lname; + const char *sname; +} BIT_STRING_BITNAME; + +# define B_ASN1_TIME \ + B_ASN1_UTCTIME | \ + B_ASN1_GENERALIZEDTIME + +# define B_ASN1_PRINTABLE \ + B_ASN1_NUMERICSTRING| \ + B_ASN1_PRINTABLESTRING| \ + B_ASN1_T61STRING| \ + B_ASN1_IA5STRING| \ + B_ASN1_BIT_STRING| \ + B_ASN1_UNIVERSALSTRING|\ + B_ASN1_BMPSTRING|\ + B_ASN1_UTF8STRING|\ + B_ASN1_SEQUENCE|\ + B_ASN1_UNKNOWN + +# define B_ASN1_DIRECTORYSTRING \ + B_ASN1_PRINTABLESTRING| \ + B_ASN1_TELETEXSTRING|\ + B_ASN1_BMPSTRING|\ + B_ASN1_UNIVERSALSTRING|\ + B_ASN1_UTF8STRING + +# define B_ASN1_DISPLAYTEXT \ + B_ASN1_IA5STRING| \ + B_ASN1_VISIBLESTRING| \ + B_ASN1_BMPSTRING|\ + B_ASN1_UTF8STRING + +DECLARE_ASN1_FUNCTIONS_fname(ASN1_TYPE, ASN1_ANY, ASN1_TYPE) + +int ASN1_TYPE_get(const ASN1_TYPE *a); +void ASN1_TYPE_set(ASN1_TYPE *a, int type, void *value); +int ASN1_TYPE_set1(ASN1_TYPE *a, int type, const void *value); +int ASN1_TYPE_cmp(const ASN1_TYPE *a, const ASN1_TYPE *b); + +ASN1_TYPE *ASN1_TYPE_pack_sequence(const ASN1_ITEM *it, void *s, ASN1_TYPE **t); +void *ASN1_TYPE_unpack_sequence(const ASN1_ITEM *it, const ASN1_TYPE *t); + +ASN1_OBJECT *ASN1_OBJECT_new(void); +void ASN1_OBJECT_free(ASN1_OBJECT *a); +int i2d_ASN1_OBJECT(const ASN1_OBJECT *a, unsigned char **pp); +ASN1_OBJECT *d2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp, + long length); + +DECLARE_ASN1_ITEM(ASN1_OBJECT) + +DEFINE_STACK_OF(ASN1_OBJECT) + +ASN1_STRING *ASN1_STRING_new(void); +void ASN1_STRING_free(ASN1_STRING *a); +void ASN1_STRING_clear_free(ASN1_STRING *a); +int ASN1_STRING_copy(ASN1_STRING *dst, const ASN1_STRING *str); +ASN1_STRING *ASN1_STRING_dup(const ASN1_STRING *a); +ASN1_STRING *ASN1_STRING_type_new(int type); +int ASN1_STRING_cmp(const ASN1_STRING *a, const ASN1_STRING *b); + /* + * Since this is used to store all sorts of things, via macros, for now, + * make its data void * + */ +int ASN1_STRING_set(ASN1_STRING *str, const void *data, int len); +void ASN1_STRING_set0(ASN1_STRING *str, void *data, int len); +int ASN1_STRING_length(const ASN1_STRING *x); +void ASN1_STRING_length_set(ASN1_STRING *x, int n); +int ASN1_STRING_type(const ASN1_STRING *x); +DEPRECATEDIN_1_1_0(unsigned char *ASN1_STRING_data(ASN1_STRING *x)) +const unsigned char *ASN1_STRING_get0_data(const ASN1_STRING *x); + +DECLARE_ASN1_FUNCTIONS(ASN1_BIT_STRING) +int ASN1_BIT_STRING_set(ASN1_BIT_STRING *a, unsigned char *d, int length); +int ASN1_BIT_STRING_set_bit(ASN1_BIT_STRING *a, int n, int value); +int ASN1_BIT_STRING_get_bit(const ASN1_BIT_STRING *a, int n); +int ASN1_BIT_STRING_check(const ASN1_BIT_STRING *a, + const unsigned char *flags, int flags_len); + +int ASN1_BIT_STRING_name_print(BIO *out, ASN1_BIT_STRING *bs, + BIT_STRING_BITNAME *tbl, int indent); +int ASN1_BIT_STRING_num_asc(const char *name, BIT_STRING_BITNAME *tbl); +int ASN1_BIT_STRING_set_asc(ASN1_BIT_STRING *bs, const char *name, int value, + BIT_STRING_BITNAME *tbl); + +DECLARE_ASN1_FUNCTIONS(ASN1_INTEGER) +ASN1_INTEGER *d2i_ASN1_UINTEGER(ASN1_INTEGER **a, const unsigned char **pp, + long length); +ASN1_INTEGER *ASN1_INTEGER_dup(const ASN1_INTEGER *x); +int ASN1_INTEGER_cmp(const ASN1_INTEGER *x, const ASN1_INTEGER *y); + +DECLARE_ASN1_FUNCTIONS(ASN1_ENUMERATED) + +int ASN1_UTCTIME_check(const ASN1_UTCTIME *a); +ASN1_UTCTIME *ASN1_UTCTIME_set(ASN1_UTCTIME *s, time_t t); +ASN1_UTCTIME *ASN1_UTCTIME_adj(ASN1_UTCTIME *s, time_t t, + int offset_day, long offset_sec); +int ASN1_UTCTIME_set_string(ASN1_UTCTIME *s, const char *str); +int ASN1_UTCTIME_cmp_time_t(const ASN1_UTCTIME *s, time_t t); + +int ASN1_GENERALIZEDTIME_check(const ASN1_GENERALIZEDTIME *a); +ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_set(ASN1_GENERALIZEDTIME *s, + time_t t); +ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_adj(ASN1_GENERALIZEDTIME *s, + time_t t, int offset_day, + long offset_sec); +int ASN1_GENERALIZEDTIME_set_string(ASN1_GENERALIZEDTIME *s, const char *str); + +int ASN1_TIME_diff(int *pday, int *psec, + const ASN1_TIME *from, const ASN1_TIME *to); + +DECLARE_ASN1_FUNCTIONS(ASN1_OCTET_STRING) +ASN1_OCTET_STRING *ASN1_OCTET_STRING_dup(const ASN1_OCTET_STRING *a); +int ASN1_OCTET_STRING_cmp(const ASN1_OCTET_STRING *a, + const ASN1_OCTET_STRING *b); +int ASN1_OCTET_STRING_set(ASN1_OCTET_STRING *str, const unsigned char *data, + int len); + +DECLARE_ASN1_FUNCTIONS(ASN1_VISIBLESTRING) +DECLARE_ASN1_FUNCTIONS(ASN1_UNIVERSALSTRING) +DECLARE_ASN1_FUNCTIONS(ASN1_UTF8STRING) +DECLARE_ASN1_FUNCTIONS(ASN1_NULL) +DECLARE_ASN1_FUNCTIONS(ASN1_BMPSTRING) + +int UTF8_getc(const unsigned char *str, int len, unsigned long *val); +int UTF8_putc(unsigned char *str, int len, unsigned long value); + +DECLARE_ASN1_FUNCTIONS_name(ASN1_STRING, ASN1_PRINTABLE) + +DECLARE_ASN1_FUNCTIONS_name(ASN1_STRING, DIRECTORYSTRING) +DECLARE_ASN1_FUNCTIONS_name(ASN1_STRING, DISPLAYTEXT) +DECLARE_ASN1_FUNCTIONS(ASN1_PRINTABLESTRING) +DECLARE_ASN1_FUNCTIONS(ASN1_T61STRING) +DECLARE_ASN1_FUNCTIONS(ASN1_IA5STRING) +DECLARE_ASN1_FUNCTIONS(ASN1_GENERALSTRING) +DECLARE_ASN1_FUNCTIONS(ASN1_UTCTIME) +DECLARE_ASN1_FUNCTIONS(ASN1_GENERALIZEDTIME) +DECLARE_ASN1_FUNCTIONS(ASN1_TIME) + +DECLARE_ASN1_ITEM(ASN1_OCTET_STRING_NDEF) + +ASN1_TIME *ASN1_TIME_set(ASN1_TIME *s, time_t t); +ASN1_TIME *ASN1_TIME_adj(ASN1_TIME *s, time_t t, + int offset_day, long offset_sec); +int ASN1_TIME_check(const ASN1_TIME *t); +ASN1_GENERALIZEDTIME *ASN1_TIME_to_generalizedtime(const ASN1_TIME *t, + ASN1_GENERALIZEDTIME **out); +int ASN1_TIME_set_string(ASN1_TIME *s, const char *str); +int ASN1_TIME_set_string_X509(ASN1_TIME *s, const char *str); +int ASN1_TIME_to_tm(const ASN1_TIME *s, struct tm *tm); +int ASN1_TIME_normalize(ASN1_TIME *s); +int ASN1_TIME_cmp_time_t(const ASN1_TIME *s, time_t t); +int ASN1_TIME_compare(const ASN1_TIME *a, const ASN1_TIME *b); + +int i2a_ASN1_INTEGER(BIO *bp, const ASN1_INTEGER *a); +int a2i_ASN1_INTEGER(BIO *bp, ASN1_INTEGER *bs, char *buf, int size); +int i2a_ASN1_ENUMERATED(BIO *bp, const ASN1_ENUMERATED *a); +int a2i_ASN1_ENUMERATED(BIO *bp, ASN1_ENUMERATED *bs, char *buf, int size); +int i2a_ASN1_OBJECT(BIO *bp, const ASN1_OBJECT *a); +int a2i_ASN1_STRING(BIO *bp, ASN1_STRING *bs, char *buf, int size); +int i2a_ASN1_STRING(BIO *bp, const ASN1_STRING *a, int type); +int i2t_ASN1_OBJECT(char *buf, int buf_len, const ASN1_OBJECT *a); + +int a2d_ASN1_OBJECT(unsigned char *out, int olen, const char *buf, int num); +ASN1_OBJECT *ASN1_OBJECT_create(int nid, unsigned char *data, int len, + const char *sn, const char *ln); + +int ASN1_INTEGER_get_int64(int64_t *pr, const ASN1_INTEGER *a); +int ASN1_INTEGER_set_int64(ASN1_INTEGER *a, int64_t r); +int ASN1_INTEGER_get_uint64(uint64_t *pr, const ASN1_INTEGER *a); +int ASN1_INTEGER_set_uint64(ASN1_INTEGER *a, uint64_t r); + +int ASN1_INTEGER_set(ASN1_INTEGER *a, long v); +long ASN1_INTEGER_get(const ASN1_INTEGER *a); +ASN1_INTEGER *BN_to_ASN1_INTEGER(const BIGNUM *bn, ASN1_INTEGER *ai); +BIGNUM *ASN1_INTEGER_to_BN(const ASN1_INTEGER *ai, BIGNUM *bn); + +int ASN1_ENUMERATED_get_int64(int64_t *pr, const ASN1_ENUMERATED *a); +int ASN1_ENUMERATED_set_int64(ASN1_ENUMERATED *a, int64_t r); + + +int ASN1_ENUMERATED_set(ASN1_ENUMERATED *a, long v); +long ASN1_ENUMERATED_get(const ASN1_ENUMERATED *a); +ASN1_ENUMERATED *BN_to_ASN1_ENUMERATED(const BIGNUM *bn, ASN1_ENUMERATED *ai); +BIGNUM *ASN1_ENUMERATED_to_BN(const ASN1_ENUMERATED *ai, BIGNUM *bn); + +/* General */ +/* given a string, return the correct type, max is the maximum length */ +int ASN1_PRINTABLE_type(const unsigned char *s, int max); + +unsigned long ASN1_tag2bit(int tag); + +/* SPECIALS */ +int ASN1_get_object(const unsigned char **pp, long *plength, int *ptag, + int *pclass, long omax); +int ASN1_check_infinite_end(unsigned char **p, long len); +int ASN1_const_check_infinite_end(const unsigned char **p, long len); +void ASN1_put_object(unsigned char **pp, int constructed, int length, + int tag, int xclass); +int ASN1_put_eoc(unsigned char **pp); +int ASN1_object_size(int constructed, int length, int tag); + +/* Used to implement other functions */ +void *ASN1_dup(i2d_of_void *i2d, d2i_of_void *d2i, void *x); + +# define ASN1_dup_of(type,i2d,d2i,x) \ + ((type*)ASN1_dup(CHECKED_I2D_OF(type, i2d), \ + CHECKED_D2I_OF(type, d2i), \ + CHECKED_PTR_OF(type, x))) + +# define ASN1_dup_of_const(type,i2d,d2i,x) \ + ((type*)ASN1_dup(CHECKED_I2D_OF(const type, i2d), \ + CHECKED_D2I_OF(type, d2i), \ + CHECKED_PTR_OF(const type, x))) + +void *ASN1_item_dup(const ASN1_ITEM *it, void *x); + +/* ASN1 alloc/free macros for when a type is only used internally */ + +# define M_ASN1_new_of(type) (type *)ASN1_item_new(ASN1_ITEM_rptr(type)) +# define M_ASN1_free_of(x, type) \ + ASN1_item_free(CHECKED_PTR_OF(type, x), ASN1_ITEM_rptr(type)) + +# ifndef OPENSSL_NO_STDIO +void *ASN1_d2i_fp(void *(*xnew) (void), d2i_of_void *d2i, FILE *in, void **x); + +# define ASN1_d2i_fp_of(type,xnew,d2i,in,x) \ + ((type*)ASN1_d2i_fp(CHECKED_NEW_OF(type, xnew), \ + CHECKED_D2I_OF(type, d2i), \ + in, \ + CHECKED_PPTR_OF(type, x))) + +void *ASN1_item_d2i_fp(const ASN1_ITEM *it, FILE *in, void *x); +int ASN1_i2d_fp(i2d_of_void *i2d, FILE *out, void *x); + +# define ASN1_i2d_fp_of(type,i2d,out,x) \ + (ASN1_i2d_fp(CHECKED_I2D_OF(type, i2d), \ + out, \ + CHECKED_PTR_OF(type, x))) + +# define ASN1_i2d_fp_of_const(type,i2d,out,x) \ + (ASN1_i2d_fp(CHECKED_I2D_OF(const type, i2d), \ + out, \ + CHECKED_PTR_OF(const type, x))) + +int ASN1_item_i2d_fp(const ASN1_ITEM *it, FILE *out, void *x); +int ASN1_STRING_print_ex_fp(FILE *fp, const ASN1_STRING *str, unsigned long flags); +# endif + +int ASN1_STRING_to_UTF8(unsigned char **out, const ASN1_STRING *in); + +void *ASN1_d2i_bio(void *(*xnew) (void), d2i_of_void *d2i, BIO *in, void **x); + +# define ASN1_d2i_bio_of(type,xnew,d2i,in,x) \ + ((type*)ASN1_d2i_bio( CHECKED_NEW_OF(type, xnew), \ + CHECKED_D2I_OF(type, d2i), \ + in, \ + CHECKED_PPTR_OF(type, x))) + +void *ASN1_item_d2i_bio(const ASN1_ITEM *it, BIO *in, void *x); +int ASN1_i2d_bio(i2d_of_void *i2d, BIO *out, unsigned char *x); + +# define ASN1_i2d_bio_of(type,i2d,out,x) \ + (ASN1_i2d_bio(CHECKED_I2D_OF(type, i2d), \ + out, \ + CHECKED_PTR_OF(type, x))) + +# define ASN1_i2d_bio_of_const(type,i2d,out,x) \ + (ASN1_i2d_bio(CHECKED_I2D_OF(const type, i2d), \ + out, \ + CHECKED_PTR_OF(const type, x))) + +int ASN1_item_i2d_bio(const ASN1_ITEM *it, BIO *out, void *x); +int ASN1_UTCTIME_print(BIO *fp, const ASN1_UTCTIME *a); +int ASN1_GENERALIZEDTIME_print(BIO *fp, const ASN1_GENERALIZEDTIME *a); +int ASN1_TIME_print(BIO *fp, const ASN1_TIME *a); +int ASN1_STRING_print(BIO *bp, const ASN1_STRING *v); +int ASN1_STRING_print_ex(BIO *out, const ASN1_STRING *str, unsigned long flags); +int ASN1_buf_print(BIO *bp, const unsigned char *buf, size_t buflen, int off); +int ASN1_bn_print(BIO *bp, const char *number, const BIGNUM *num, + unsigned char *buf, int off); +int ASN1_parse(BIO *bp, const unsigned char *pp, long len, int indent); +int ASN1_parse_dump(BIO *bp, const unsigned char *pp, long len, int indent, + int dump); +const char *ASN1_tag2str(int tag); + +/* Used to load and write Netscape format cert */ + +int ASN1_UNIVERSALSTRING_to_string(ASN1_UNIVERSALSTRING *s); + +int ASN1_TYPE_set_octetstring(ASN1_TYPE *a, unsigned char *data, int len); +int ASN1_TYPE_get_octetstring(const ASN1_TYPE *a, unsigned char *data, int max_len); +int ASN1_TYPE_set_int_octetstring(ASN1_TYPE *a, long num, + unsigned char *data, int len); +int ASN1_TYPE_get_int_octetstring(const ASN1_TYPE *a, long *num, + unsigned char *data, int max_len); + +void *ASN1_item_unpack(const ASN1_STRING *oct, const ASN1_ITEM *it); + +ASN1_STRING *ASN1_item_pack(void *obj, const ASN1_ITEM *it, + ASN1_OCTET_STRING **oct); + +void ASN1_STRING_set_default_mask(unsigned long mask); +int ASN1_STRING_set_default_mask_asc(const char *p); +unsigned long ASN1_STRING_get_default_mask(void); +int ASN1_mbstring_copy(ASN1_STRING **out, const unsigned char *in, int len, + int inform, unsigned long mask); +int ASN1_mbstring_ncopy(ASN1_STRING **out, const unsigned char *in, int len, + int inform, unsigned long mask, + long minsize, long maxsize); + +ASN1_STRING *ASN1_STRING_set_by_NID(ASN1_STRING **out, + const unsigned char *in, int inlen, + int inform, int nid); +ASN1_STRING_TABLE *ASN1_STRING_TABLE_get(int nid); +int ASN1_STRING_TABLE_add(int, long, long, unsigned long, unsigned long); +void ASN1_STRING_TABLE_cleanup(void); + +/* ASN1 template functions */ + +/* Old API compatible functions */ +ASN1_VALUE *ASN1_item_new(const ASN1_ITEM *it); +void ASN1_item_free(ASN1_VALUE *val, const ASN1_ITEM *it); +ASN1_VALUE *ASN1_item_d2i(ASN1_VALUE **val, const unsigned char **in, + long len, const ASN1_ITEM *it); +int ASN1_item_i2d(ASN1_VALUE *val, unsigned char **out, const ASN1_ITEM *it); +int ASN1_item_ndef_i2d(ASN1_VALUE *val, unsigned char **out, + const ASN1_ITEM *it); + +void ASN1_add_oid_module(void); +void ASN1_add_stable_module(void); + +ASN1_TYPE *ASN1_generate_nconf(const char *str, CONF *nconf); +ASN1_TYPE *ASN1_generate_v3(const char *str, X509V3_CTX *cnf); +int ASN1_str2mask(const char *str, unsigned long *pmask); + +/* ASN1 Print flags */ + +/* Indicate missing OPTIONAL fields */ +# define ASN1_PCTX_FLAGS_SHOW_ABSENT 0x001 +/* Mark start and end of SEQUENCE */ +# define ASN1_PCTX_FLAGS_SHOW_SEQUENCE 0x002 +/* Mark start and end of SEQUENCE/SET OF */ +# define ASN1_PCTX_FLAGS_SHOW_SSOF 0x004 +/* Show the ASN1 type of primitives */ +# define ASN1_PCTX_FLAGS_SHOW_TYPE 0x008 +/* Don't show ASN1 type of ANY */ +# define ASN1_PCTX_FLAGS_NO_ANY_TYPE 0x010 +/* Don't show ASN1 type of MSTRINGs */ +# define ASN1_PCTX_FLAGS_NO_MSTRING_TYPE 0x020 +/* Don't show field names in SEQUENCE */ +# define ASN1_PCTX_FLAGS_NO_FIELD_NAME 0x040 +/* Show structure names of each SEQUENCE field */ +# define ASN1_PCTX_FLAGS_SHOW_FIELD_STRUCT_NAME 0x080 +/* Don't show structure name even at top level */ +# define ASN1_PCTX_FLAGS_NO_STRUCT_NAME 0x100 + +int ASN1_item_print(BIO *out, ASN1_VALUE *ifld, int indent, + const ASN1_ITEM *it, const ASN1_PCTX *pctx); +ASN1_PCTX *ASN1_PCTX_new(void); +void ASN1_PCTX_free(ASN1_PCTX *p); +unsigned long ASN1_PCTX_get_flags(const ASN1_PCTX *p); +void ASN1_PCTX_set_flags(ASN1_PCTX *p, unsigned long flags); +unsigned long ASN1_PCTX_get_nm_flags(const ASN1_PCTX *p); +void ASN1_PCTX_set_nm_flags(ASN1_PCTX *p, unsigned long flags); +unsigned long ASN1_PCTX_get_cert_flags(const ASN1_PCTX *p); +void ASN1_PCTX_set_cert_flags(ASN1_PCTX *p, unsigned long flags); +unsigned long ASN1_PCTX_get_oid_flags(const ASN1_PCTX *p); +void ASN1_PCTX_set_oid_flags(ASN1_PCTX *p, unsigned long flags); +unsigned long ASN1_PCTX_get_str_flags(const ASN1_PCTX *p); +void ASN1_PCTX_set_str_flags(ASN1_PCTX *p, unsigned long flags); + +ASN1_SCTX *ASN1_SCTX_new(int (*scan_cb) (ASN1_SCTX *ctx)); +void ASN1_SCTX_free(ASN1_SCTX *p); +const ASN1_ITEM *ASN1_SCTX_get_item(ASN1_SCTX *p); +const ASN1_TEMPLATE *ASN1_SCTX_get_template(ASN1_SCTX *p); +unsigned long ASN1_SCTX_get_flags(ASN1_SCTX *p); +void ASN1_SCTX_set_app_data(ASN1_SCTX *p, void *data); +void *ASN1_SCTX_get_app_data(ASN1_SCTX *p); + +const BIO_METHOD *BIO_f_asn1(void); + +BIO *BIO_new_NDEF(BIO *out, ASN1_VALUE *val, const ASN1_ITEM *it); + +int i2d_ASN1_bio_stream(BIO *out, ASN1_VALUE *val, BIO *in, int flags, + const ASN1_ITEM *it); +int PEM_write_bio_ASN1_stream(BIO *out, ASN1_VALUE *val, BIO *in, int flags, + const char *hdr, const ASN1_ITEM *it); +int SMIME_write_ASN1(BIO *bio, ASN1_VALUE *val, BIO *data, int flags, + int ctype_nid, int econt_nid, + STACK_OF(X509_ALGOR) *mdalgs, const ASN1_ITEM *it); +ASN1_VALUE *SMIME_read_ASN1(BIO *bio, BIO **bcont, const ASN1_ITEM *it); +int SMIME_crlf_copy(BIO *in, BIO *out, int flags); +int SMIME_text(BIO *in, BIO *out); + +const ASN1_ITEM *ASN1_ITEM_lookup(const char *name); +const ASN1_ITEM *ASN1_ITEM_get(size_t i); + +# ifdef __cplusplus +} +# endif +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/asn1_mac.h b/example/android/third_party/crypto/arm/include/openssl/asn1_mac.h new file mode 100644 index 00000000..7ac1782a --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/asn1_mac.h @@ -0,0 +1,10 @@ +/* + * Copyright 2015-2016 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#error "This file is obsolete; please update your software." diff --git a/example/android/third_party/crypto/arm/include/openssl/asn1err.h b/example/android/third_party/crypto/arm/include/openssl/asn1err.h new file mode 100644 index 00000000..5a91126d --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/asn1err.h @@ -0,0 +1,252 @@ +/* + * Generated by util/mkerr.pl DO NOT EDIT + * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_ASN1ERR_H +# define HEADER_ASN1ERR_H + +# ifdef __cplusplus +extern "C" +# endif +int ERR_load_ASN1_strings(void); + +/* + * ASN1 function codes. + */ +# define ASN1_F_A2D_ASN1_OBJECT 100 +# define ASN1_F_A2I_ASN1_INTEGER 102 +# define ASN1_F_A2I_ASN1_STRING 103 +# define ASN1_F_APPEND_EXP 176 +# define ASN1_F_ASN1_BIO_INIT 113 +# define ASN1_F_ASN1_BIT_STRING_SET_BIT 183 +# define ASN1_F_ASN1_CB 177 +# define ASN1_F_ASN1_CHECK_TLEN 104 +# define ASN1_F_ASN1_COLLECT 106 +# define ASN1_F_ASN1_D2I_EX_PRIMITIVE 108 +# define ASN1_F_ASN1_D2I_FP 109 +# define ASN1_F_ASN1_D2I_READ_BIO 107 +# define ASN1_F_ASN1_DIGEST 184 +# define ASN1_F_ASN1_DO_ADB 110 +# define ASN1_F_ASN1_DO_LOCK 233 +# define ASN1_F_ASN1_DUP 111 +# define ASN1_F_ASN1_ENC_SAVE 115 +# define ASN1_F_ASN1_EX_C2I 204 +# define ASN1_F_ASN1_FIND_END 190 +# define ASN1_F_ASN1_GENERALIZEDTIME_ADJ 216 +# define ASN1_F_ASN1_GENERATE_V3 178 +# define ASN1_F_ASN1_GET_INT64 224 +# define ASN1_F_ASN1_GET_OBJECT 114 +# define ASN1_F_ASN1_GET_UINT64 225 +# define ASN1_F_ASN1_I2D_BIO 116 +# define ASN1_F_ASN1_I2D_FP 117 +# define ASN1_F_ASN1_ITEM_D2I_FP 206 +# define ASN1_F_ASN1_ITEM_DUP 191 +# define ASN1_F_ASN1_ITEM_EMBED_D2I 120 +# define ASN1_F_ASN1_ITEM_EMBED_NEW 121 +# define ASN1_F_ASN1_ITEM_FLAGS_I2D 118 +# define ASN1_F_ASN1_ITEM_I2D_BIO 192 +# define ASN1_F_ASN1_ITEM_I2D_FP 193 +# define ASN1_F_ASN1_ITEM_PACK 198 +# define ASN1_F_ASN1_ITEM_SIGN 195 +# define ASN1_F_ASN1_ITEM_SIGN_CTX 220 +# define ASN1_F_ASN1_ITEM_UNPACK 199 +# define ASN1_F_ASN1_ITEM_VERIFY 197 +# define ASN1_F_ASN1_MBSTRING_NCOPY 122 +# define ASN1_F_ASN1_OBJECT_NEW 123 +# define ASN1_F_ASN1_OUTPUT_DATA 214 +# define ASN1_F_ASN1_PCTX_NEW 205 +# define ASN1_F_ASN1_PRIMITIVE_NEW 119 +# define ASN1_F_ASN1_SCTX_NEW 221 +# define ASN1_F_ASN1_SIGN 128 +# define ASN1_F_ASN1_STR2TYPE 179 +# define ASN1_F_ASN1_STRING_GET_INT64 227 +# define ASN1_F_ASN1_STRING_GET_UINT64 230 +# define ASN1_F_ASN1_STRING_SET 186 +# define ASN1_F_ASN1_STRING_TABLE_ADD 129 +# define ASN1_F_ASN1_STRING_TO_BN 228 +# define ASN1_F_ASN1_STRING_TYPE_NEW 130 +# define ASN1_F_ASN1_TEMPLATE_EX_D2I 132 +# define ASN1_F_ASN1_TEMPLATE_NEW 133 +# define ASN1_F_ASN1_TEMPLATE_NOEXP_D2I 131 +# define ASN1_F_ASN1_TIME_ADJ 217 +# define ASN1_F_ASN1_TYPE_GET_INT_OCTETSTRING 134 +# define ASN1_F_ASN1_TYPE_GET_OCTETSTRING 135 +# define ASN1_F_ASN1_UTCTIME_ADJ 218 +# define ASN1_F_ASN1_VERIFY 137 +# define ASN1_F_B64_READ_ASN1 209 +# define ASN1_F_B64_WRITE_ASN1 210 +# define ASN1_F_BIO_NEW_NDEF 208 +# define ASN1_F_BITSTR_CB 180 +# define ASN1_F_BN_TO_ASN1_STRING 229 +# define ASN1_F_C2I_ASN1_BIT_STRING 189 +# define ASN1_F_C2I_ASN1_INTEGER 194 +# define ASN1_F_C2I_ASN1_OBJECT 196 +# define ASN1_F_C2I_IBUF 226 +# define ASN1_F_C2I_UINT64_INT 101 +# define ASN1_F_COLLECT_DATA 140 +# define ASN1_F_D2I_ASN1_OBJECT 147 +# define ASN1_F_D2I_ASN1_UINTEGER 150 +# define ASN1_F_D2I_AUTOPRIVATEKEY 207 +# define ASN1_F_D2I_PRIVATEKEY 154 +# define ASN1_F_D2I_PUBLICKEY 155 +# define ASN1_F_DO_BUF 142 +# define ASN1_F_DO_CREATE 124 +# define ASN1_F_DO_DUMP 125 +# define ASN1_F_DO_TCREATE 222 +# define ASN1_F_I2A_ASN1_OBJECT 126 +# define ASN1_F_I2D_ASN1_BIO_STREAM 211 +# define ASN1_F_I2D_ASN1_OBJECT 143 +# define ASN1_F_I2D_DSA_PUBKEY 161 +# define ASN1_F_I2D_EC_PUBKEY 181 +# define ASN1_F_I2D_PRIVATEKEY 163 +# define ASN1_F_I2D_PUBLICKEY 164 +# define ASN1_F_I2D_RSA_PUBKEY 165 +# define ASN1_F_LONG_C2I 166 +# define ASN1_F_NDEF_PREFIX 127 +# define ASN1_F_NDEF_SUFFIX 136 +# define ASN1_F_OID_MODULE_INIT 174 +# define ASN1_F_PARSE_TAGGING 182 +# define ASN1_F_PKCS5_PBE2_SET_IV 167 +# define ASN1_F_PKCS5_PBE2_SET_SCRYPT 231 +# define ASN1_F_PKCS5_PBE_SET 202 +# define ASN1_F_PKCS5_PBE_SET0_ALGOR 215 +# define ASN1_F_PKCS5_PBKDF2_SET 219 +# define ASN1_F_PKCS5_SCRYPT_SET 232 +# define ASN1_F_SMIME_READ_ASN1 212 +# define ASN1_F_SMIME_TEXT 213 +# define ASN1_F_STABLE_GET 138 +# define ASN1_F_STBL_MODULE_INIT 223 +# define ASN1_F_UINT32_C2I 105 +# define ASN1_F_UINT32_NEW 139 +# define ASN1_F_UINT64_C2I 112 +# define ASN1_F_UINT64_NEW 141 +# define ASN1_F_X509_CRL_ADD0_REVOKED 169 +# define ASN1_F_X509_INFO_NEW 170 +# define ASN1_F_X509_NAME_ENCODE 203 +# define ASN1_F_X509_NAME_EX_D2I 158 +# define ASN1_F_X509_NAME_EX_NEW 171 +# define ASN1_F_X509_PKEY_NEW 173 + +/* + * ASN1 reason codes. + */ +# define ASN1_R_ADDING_OBJECT 171 +# define ASN1_R_ASN1_PARSE_ERROR 203 +# define ASN1_R_ASN1_SIG_PARSE_ERROR 204 +# define ASN1_R_AUX_ERROR 100 +# define ASN1_R_BAD_OBJECT_HEADER 102 +# define ASN1_R_BMPSTRING_IS_WRONG_LENGTH 214 +# define ASN1_R_BN_LIB 105 +# define ASN1_R_BOOLEAN_IS_WRONG_LENGTH 106 +# define ASN1_R_BUFFER_TOO_SMALL 107 +# define ASN1_R_CIPHER_HAS_NO_OBJECT_IDENTIFIER 108 +# define ASN1_R_CONTEXT_NOT_INITIALISED 217 +# define ASN1_R_DATA_IS_WRONG 109 +# define ASN1_R_DECODE_ERROR 110 +# define ASN1_R_DEPTH_EXCEEDED 174 +# define ASN1_R_DIGEST_AND_KEY_TYPE_NOT_SUPPORTED 198 +# define ASN1_R_ENCODE_ERROR 112 +# define ASN1_R_ERROR_GETTING_TIME 173 +# define ASN1_R_ERROR_LOADING_SECTION 172 +# define ASN1_R_ERROR_SETTING_CIPHER_PARAMS 114 +# define ASN1_R_EXPECTING_AN_INTEGER 115 +# define ASN1_R_EXPECTING_AN_OBJECT 116 +# define ASN1_R_EXPLICIT_LENGTH_MISMATCH 119 +# define ASN1_R_EXPLICIT_TAG_NOT_CONSTRUCTED 120 +# define ASN1_R_FIELD_MISSING 121 +# define ASN1_R_FIRST_NUM_TOO_LARGE 122 +# define ASN1_R_HEADER_TOO_LONG 123 +# define ASN1_R_ILLEGAL_BITSTRING_FORMAT 175 +# define ASN1_R_ILLEGAL_BOOLEAN 176 +# define ASN1_R_ILLEGAL_CHARACTERS 124 +# define ASN1_R_ILLEGAL_FORMAT 177 +# define ASN1_R_ILLEGAL_HEX 178 +# define ASN1_R_ILLEGAL_IMPLICIT_TAG 179 +# define ASN1_R_ILLEGAL_INTEGER 180 +# define ASN1_R_ILLEGAL_NEGATIVE_VALUE 226 +# define ASN1_R_ILLEGAL_NESTED_TAGGING 181 +# define ASN1_R_ILLEGAL_NULL 125 +# define ASN1_R_ILLEGAL_NULL_VALUE 182 +# define ASN1_R_ILLEGAL_OBJECT 183 +# define ASN1_R_ILLEGAL_OPTIONAL_ANY 126 +# define ASN1_R_ILLEGAL_OPTIONS_ON_ITEM_TEMPLATE 170 +# define ASN1_R_ILLEGAL_PADDING 221 +# define ASN1_R_ILLEGAL_TAGGED_ANY 127 +# define ASN1_R_ILLEGAL_TIME_VALUE 184 +# define ASN1_R_ILLEGAL_ZERO_CONTENT 222 +# define ASN1_R_INTEGER_NOT_ASCII_FORMAT 185 +# define ASN1_R_INTEGER_TOO_LARGE_FOR_LONG 128 +# define ASN1_R_INVALID_BIT_STRING_BITS_LEFT 220 +# define ASN1_R_INVALID_BMPSTRING_LENGTH 129 +# define ASN1_R_INVALID_DIGIT 130 +# define ASN1_R_INVALID_MIME_TYPE 205 +# define ASN1_R_INVALID_MODIFIER 186 +# define ASN1_R_INVALID_NUMBER 187 +# define ASN1_R_INVALID_OBJECT_ENCODING 216 +# define ASN1_R_INVALID_SCRYPT_PARAMETERS 227 +# define ASN1_R_INVALID_SEPARATOR 131 +# define ASN1_R_INVALID_STRING_TABLE_VALUE 218 +# define ASN1_R_INVALID_UNIVERSALSTRING_LENGTH 133 +# define ASN1_R_INVALID_UTF8STRING 134 +# define ASN1_R_INVALID_VALUE 219 +# define ASN1_R_LIST_ERROR 188 +# define ASN1_R_MIME_NO_CONTENT_TYPE 206 +# define ASN1_R_MIME_PARSE_ERROR 207 +# define ASN1_R_MIME_SIG_PARSE_ERROR 208 +# define ASN1_R_MISSING_EOC 137 +# define ASN1_R_MISSING_SECOND_NUMBER 138 +# define ASN1_R_MISSING_VALUE 189 +# define ASN1_R_MSTRING_NOT_UNIVERSAL 139 +# define ASN1_R_MSTRING_WRONG_TAG 140 +# define ASN1_R_NESTED_ASN1_STRING 197 +# define ASN1_R_NESTED_TOO_DEEP 201 +# define ASN1_R_NON_HEX_CHARACTERS 141 +# define ASN1_R_NOT_ASCII_FORMAT 190 +# define ASN1_R_NOT_ENOUGH_DATA 142 +# define ASN1_R_NO_CONTENT_TYPE 209 +# define ASN1_R_NO_MATCHING_CHOICE_TYPE 143 +# define ASN1_R_NO_MULTIPART_BODY_FAILURE 210 +# define ASN1_R_NO_MULTIPART_BOUNDARY 211 +# define ASN1_R_NO_SIG_CONTENT_TYPE 212 +# define ASN1_R_NULL_IS_WRONG_LENGTH 144 +# define ASN1_R_OBJECT_NOT_ASCII_FORMAT 191 +# define ASN1_R_ODD_NUMBER_OF_CHARS 145 +# define ASN1_R_SECOND_NUMBER_TOO_LARGE 147 +# define ASN1_R_SEQUENCE_LENGTH_MISMATCH 148 +# define ASN1_R_SEQUENCE_NOT_CONSTRUCTED 149 +# define ASN1_R_SEQUENCE_OR_SET_NEEDS_CONFIG 192 +# define ASN1_R_SHORT_LINE 150 +# define ASN1_R_SIG_INVALID_MIME_TYPE 213 +# define ASN1_R_STREAMING_NOT_SUPPORTED 202 +# define ASN1_R_STRING_TOO_LONG 151 +# define ASN1_R_STRING_TOO_SHORT 152 +# define ASN1_R_THE_ASN1_OBJECT_IDENTIFIER_IS_NOT_KNOWN_FOR_THIS_MD 154 +# define ASN1_R_TIME_NOT_ASCII_FORMAT 193 +# define ASN1_R_TOO_LARGE 223 +# define ASN1_R_TOO_LONG 155 +# define ASN1_R_TOO_SMALL 224 +# define ASN1_R_TYPE_NOT_CONSTRUCTED 156 +# define ASN1_R_TYPE_NOT_PRIMITIVE 195 +# define ASN1_R_UNEXPECTED_EOC 159 +# define ASN1_R_UNIVERSALSTRING_IS_WRONG_LENGTH 215 +# define ASN1_R_UNKNOWN_FORMAT 160 +# define ASN1_R_UNKNOWN_MESSAGE_DIGEST_ALGORITHM 161 +# define ASN1_R_UNKNOWN_OBJECT_TYPE 162 +# define ASN1_R_UNKNOWN_PUBLIC_KEY_TYPE 163 +# define ASN1_R_UNKNOWN_SIGNATURE_ALGORITHM 199 +# define ASN1_R_UNKNOWN_TAG 194 +# define ASN1_R_UNSUPPORTED_ANY_DEFINED_BY_TYPE 164 +# define ASN1_R_UNSUPPORTED_CIPHER 228 +# define ASN1_R_UNSUPPORTED_PUBLIC_KEY_TYPE 167 +# define ASN1_R_UNSUPPORTED_TYPE 196 +# define ASN1_R_WRONG_INTEGER_TYPE 225 +# define ASN1_R_WRONG_PUBLIC_KEY_TYPE 200 +# define ASN1_R_WRONG_TAG 168 + +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/asn1t.h b/example/android/third_party/crypto/arm/include/openssl/asn1t.h new file mode 100644 index 00000000..a450ba0d --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/asn1t.h @@ -0,0 +1,945 @@ +/* + * Copyright 2000-2016 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_ASN1T_H +# define HEADER_ASN1T_H + +# include +# include +# include + +# ifdef OPENSSL_BUILD_SHLIBCRYPTO +# undef OPENSSL_EXTERN +# define OPENSSL_EXTERN OPENSSL_EXPORT +# endif + +/* ASN1 template defines, structures and functions */ + +#ifdef __cplusplus +extern "C" { +#endif + +# ifndef OPENSSL_EXPORT_VAR_AS_FUNCTION + +/* Macro to obtain ASN1_ADB pointer from a type (only used internally) */ +# define ASN1_ADB_ptr(iptr) ((const ASN1_ADB *)(iptr)) + +/* Macros for start and end of ASN1_ITEM definition */ + +# define ASN1_ITEM_start(itname) \ + const ASN1_ITEM itname##_it = { + +# define static_ASN1_ITEM_start(itname) \ + static const ASN1_ITEM itname##_it = { + +# define ASN1_ITEM_end(itname) \ + }; + +# else + +/* Macro to obtain ASN1_ADB pointer from a type (only used internally) */ +# define ASN1_ADB_ptr(iptr) ((const ASN1_ADB *)((iptr)())) + +/* Macros for start and end of ASN1_ITEM definition */ + +# define ASN1_ITEM_start(itname) \ + const ASN1_ITEM * itname##_it(void) \ + { \ + static const ASN1_ITEM local_it = { + +# define static_ASN1_ITEM_start(itname) \ + static ASN1_ITEM_start(itname) + +# define ASN1_ITEM_end(itname) \ + }; \ + return &local_it; \ + } + +# endif + +/* Macros to aid ASN1 template writing */ + +# define ASN1_ITEM_TEMPLATE(tname) \ + static const ASN1_TEMPLATE tname##_item_tt + +# define ASN1_ITEM_TEMPLATE_END(tname) \ + ;\ + ASN1_ITEM_start(tname) \ + ASN1_ITYPE_PRIMITIVE,\ + -1,\ + &tname##_item_tt,\ + 0,\ + NULL,\ + 0,\ + #tname \ + ASN1_ITEM_end(tname) +# define static_ASN1_ITEM_TEMPLATE_END(tname) \ + ;\ + static_ASN1_ITEM_start(tname) \ + ASN1_ITYPE_PRIMITIVE,\ + -1,\ + &tname##_item_tt,\ + 0,\ + NULL,\ + 0,\ + #tname \ + ASN1_ITEM_end(tname) + +/* This is a ASN1 type which just embeds a template */ + +/*- + * This pair helps declare a SEQUENCE. We can do: + * + * ASN1_SEQUENCE(stname) = { + * ... SEQUENCE components ... + * } ASN1_SEQUENCE_END(stname) + * + * This will produce an ASN1_ITEM called stname_it + * for a structure called stname. + * + * If you want the same structure but a different + * name then use: + * + * ASN1_SEQUENCE(itname) = { + * ... SEQUENCE components ... + * } ASN1_SEQUENCE_END_name(stname, itname) + * + * This will create an item called itname_it using + * a structure called stname. + */ + +# define ASN1_SEQUENCE(tname) \ + static const ASN1_TEMPLATE tname##_seq_tt[] + +# define ASN1_SEQUENCE_END(stname) ASN1_SEQUENCE_END_name(stname, stname) + +# define static_ASN1_SEQUENCE_END(stname) static_ASN1_SEQUENCE_END_name(stname, stname) + +# define ASN1_SEQUENCE_END_name(stname, tname) \ + ;\ + ASN1_ITEM_start(tname) \ + ASN1_ITYPE_SEQUENCE,\ + V_ASN1_SEQUENCE,\ + tname##_seq_tt,\ + sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\ + NULL,\ + sizeof(stname),\ + #tname \ + ASN1_ITEM_end(tname) + +# define static_ASN1_SEQUENCE_END_name(stname, tname) \ + ;\ + static_ASN1_ITEM_start(tname) \ + ASN1_ITYPE_SEQUENCE,\ + V_ASN1_SEQUENCE,\ + tname##_seq_tt,\ + sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\ + NULL,\ + sizeof(stname),\ + #stname \ + ASN1_ITEM_end(tname) + +# define ASN1_NDEF_SEQUENCE(tname) \ + ASN1_SEQUENCE(tname) + +# define ASN1_NDEF_SEQUENCE_cb(tname, cb) \ + ASN1_SEQUENCE_cb(tname, cb) + +# define ASN1_SEQUENCE_cb(tname, cb) \ + static const ASN1_AUX tname##_aux = {NULL, 0, 0, 0, cb, 0}; \ + ASN1_SEQUENCE(tname) + +# define ASN1_BROKEN_SEQUENCE(tname) \ + static const ASN1_AUX tname##_aux = {NULL, ASN1_AFLG_BROKEN, 0, 0, 0, 0}; \ + ASN1_SEQUENCE(tname) + +# define ASN1_SEQUENCE_ref(tname, cb) \ + static const ASN1_AUX tname##_aux = {NULL, ASN1_AFLG_REFCOUNT, offsetof(tname, references), offsetof(tname, lock), cb, 0}; \ + ASN1_SEQUENCE(tname) + +# define ASN1_SEQUENCE_enc(tname, enc, cb) \ + static const ASN1_AUX tname##_aux = {NULL, ASN1_AFLG_ENCODING, 0, 0, cb, offsetof(tname, enc)}; \ + ASN1_SEQUENCE(tname) + +# define ASN1_NDEF_SEQUENCE_END(tname) \ + ;\ + ASN1_ITEM_start(tname) \ + ASN1_ITYPE_NDEF_SEQUENCE,\ + V_ASN1_SEQUENCE,\ + tname##_seq_tt,\ + sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\ + NULL,\ + sizeof(tname),\ + #tname \ + ASN1_ITEM_end(tname) +# define static_ASN1_NDEF_SEQUENCE_END(tname) \ + ;\ + static_ASN1_ITEM_start(tname) \ + ASN1_ITYPE_NDEF_SEQUENCE,\ + V_ASN1_SEQUENCE,\ + tname##_seq_tt,\ + sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\ + NULL,\ + sizeof(tname),\ + #tname \ + ASN1_ITEM_end(tname) + +# define ASN1_BROKEN_SEQUENCE_END(stname) ASN1_SEQUENCE_END_ref(stname, stname) +# define static_ASN1_BROKEN_SEQUENCE_END(stname) \ + static_ASN1_SEQUENCE_END_ref(stname, stname) + +# define ASN1_SEQUENCE_END_enc(stname, tname) ASN1_SEQUENCE_END_ref(stname, tname) + +# define ASN1_SEQUENCE_END_cb(stname, tname) ASN1_SEQUENCE_END_ref(stname, tname) +# define static_ASN1_SEQUENCE_END_cb(stname, tname) static_ASN1_SEQUENCE_END_ref(stname, tname) + +# define ASN1_SEQUENCE_END_ref(stname, tname) \ + ;\ + ASN1_ITEM_start(tname) \ + ASN1_ITYPE_SEQUENCE,\ + V_ASN1_SEQUENCE,\ + tname##_seq_tt,\ + sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\ + &tname##_aux,\ + sizeof(stname),\ + #tname \ + ASN1_ITEM_end(tname) +# define static_ASN1_SEQUENCE_END_ref(stname, tname) \ + ;\ + static_ASN1_ITEM_start(tname) \ + ASN1_ITYPE_SEQUENCE,\ + V_ASN1_SEQUENCE,\ + tname##_seq_tt,\ + sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\ + &tname##_aux,\ + sizeof(stname),\ + #stname \ + ASN1_ITEM_end(tname) + +# define ASN1_NDEF_SEQUENCE_END_cb(stname, tname) \ + ;\ + ASN1_ITEM_start(tname) \ + ASN1_ITYPE_NDEF_SEQUENCE,\ + V_ASN1_SEQUENCE,\ + tname##_seq_tt,\ + sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\ + &tname##_aux,\ + sizeof(stname),\ + #stname \ + ASN1_ITEM_end(tname) + +/*- + * This pair helps declare a CHOICE type. We can do: + * + * ASN1_CHOICE(chname) = { + * ... CHOICE options ... + * ASN1_CHOICE_END(chname) + * + * This will produce an ASN1_ITEM called chname_it + * for a structure called chname. The structure + * definition must look like this: + * typedef struct { + * int type; + * union { + * ASN1_SOMETHING *opt1; + * ASN1_SOMEOTHER *opt2; + * } value; + * } chname; + * + * the name of the selector must be 'type'. + * to use an alternative selector name use the + * ASN1_CHOICE_END_selector() version. + */ + +# define ASN1_CHOICE(tname) \ + static const ASN1_TEMPLATE tname##_ch_tt[] + +# define ASN1_CHOICE_cb(tname, cb) \ + static const ASN1_AUX tname##_aux = {NULL, 0, 0, 0, cb, 0}; \ + ASN1_CHOICE(tname) + +# define ASN1_CHOICE_END(stname) ASN1_CHOICE_END_name(stname, stname) + +# define static_ASN1_CHOICE_END(stname) static_ASN1_CHOICE_END_name(stname, stname) + +# define ASN1_CHOICE_END_name(stname, tname) ASN1_CHOICE_END_selector(stname, tname, type) + +# define static_ASN1_CHOICE_END_name(stname, tname) static_ASN1_CHOICE_END_selector(stname, tname, type) + +# define ASN1_CHOICE_END_selector(stname, tname, selname) \ + ;\ + ASN1_ITEM_start(tname) \ + ASN1_ITYPE_CHOICE,\ + offsetof(stname,selname) ,\ + tname##_ch_tt,\ + sizeof(tname##_ch_tt) / sizeof(ASN1_TEMPLATE),\ + NULL,\ + sizeof(stname),\ + #stname \ + ASN1_ITEM_end(tname) + +# define static_ASN1_CHOICE_END_selector(stname, tname, selname) \ + ;\ + static_ASN1_ITEM_start(tname) \ + ASN1_ITYPE_CHOICE,\ + offsetof(stname,selname) ,\ + tname##_ch_tt,\ + sizeof(tname##_ch_tt) / sizeof(ASN1_TEMPLATE),\ + NULL,\ + sizeof(stname),\ + #stname \ + ASN1_ITEM_end(tname) + +# define ASN1_CHOICE_END_cb(stname, tname, selname) \ + ;\ + ASN1_ITEM_start(tname) \ + ASN1_ITYPE_CHOICE,\ + offsetof(stname,selname) ,\ + tname##_ch_tt,\ + sizeof(tname##_ch_tt) / sizeof(ASN1_TEMPLATE),\ + &tname##_aux,\ + sizeof(stname),\ + #stname \ + ASN1_ITEM_end(tname) + +/* This helps with the template wrapper form of ASN1_ITEM */ + +# define ASN1_EX_TEMPLATE_TYPE(flags, tag, name, type) { \ + (flags), (tag), 0,\ + #name, ASN1_ITEM_ref(type) } + +/* These help with SEQUENCE or CHOICE components */ + +/* used to declare other types */ + +# define ASN1_EX_TYPE(flags, tag, stname, field, type) { \ + (flags), (tag), offsetof(stname, field),\ + #field, ASN1_ITEM_ref(type) } + +/* implicit and explicit helper macros */ + +# define ASN1_IMP_EX(stname, field, type, tag, ex) \ + ASN1_EX_TYPE(ASN1_TFLG_IMPLICIT | (ex), tag, stname, field, type) + +# define ASN1_EXP_EX(stname, field, type, tag, ex) \ + ASN1_EX_TYPE(ASN1_TFLG_EXPLICIT | (ex), tag, stname, field, type) + +/* Any defined by macros: the field used is in the table itself */ + +# ifndef OPENSSL_EXPORT_VAR_AS_FUNCTION +# define ASN1_ADB_OBJECT(tblname) { ASN1_TFLG_ADB_OID, -1, 0, #tblname, (const ASN1_ITEM *)&(tblname##_adb) } +# define ASN1_ADB_INTEGER(tblname) { ASN1_TFLG_ADB_INT, -1, 0, #tblname, (const ASN1_ITEM *)&(tblname##_adb) } +# else +# define ASN1_ADB_OBJECT(tblname) { ASN1_TFLG_ADB_OID, -1, 0, #tblname, tblname##_adb } +# define ASN1_ADB_INTEGER(tblname) { ASN1_TFLG_ADB_INT, -1, 0, #tblname, tblname##_adb } +# endif +/* Plain simple type */ +# define ASN1_SIMPLE(stname, field, type) ASN1_EX_TYPE(0,0, stname, field, type) +/* Embedded simple type */ +# define ASN1_EMBED(stname, field, type) ASN1_EX_TYPE(ASN1_TFLG_EMBED,0, stname, field, type) + +/* OPTIONAL simple type */ +# define ASN1_OPT(stname, field, type) ASN1_EX_TYPE(ASN1_TFLG_OPTIONAL, 0, stname, field, type) +# define ASN1_OPT_EMBED(stname, field, type) ASN1_EX_TYPE(ASN1_TFLG_OPTIONAL|ASN1_TFLG_EMBED, 0, stname, field, type) + +/* IMPLICIT tagged simple type */ +# define ASN1_IMP(stname, field, type, tag) ASN1_IMP_EX(stname, field, type, tag, 0) +# define ASN1_IMP_EMBED(stname, field, type, tag) ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_EMBED) + +/* IMPLICIT tagged OPTIONAL simple type */ +# define ASN1_IMP_OPT(stname, field, type, tag) ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL) +# define ASN1_IMP_OPT_EMBED(stname, field, type, tag) ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL|ASN1_TFLG_EMBED) + +/* Same as above but EXPLICIT */ + +# define ASN1_EXP(stname, field, type, tag) ASN1_EXP_EX(stname, field, type, tag, 0) +# define ASN1_EXP_EMBED(stname, field, type, tag) ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_EMBED) +# define ASN1_EXP_OPT(stname, field, type, tag) ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL) +# define ASN1_EXP_OPT_EMBED(stname, field, type, tag) ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL|ASN1_TFLG_EMBED) + +/* SEQUENCE OF type */ +# define ASN1_SEQUENCE_OF(stname, field, type) \ + ASN1_EX_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, stname, field, type) + +/* OPTIONAL SEQUENCE OF */ +# define ASN1_SEQUENCE_OF_OPT(stname, field, type) \ + ASN1_EX_TYPE(ASN1_TFLG_SEQUENCE_OF|ASN1_TFLG_OPTIONAL, 0, stname, field, type) + +/* Same as above but for SET OF */ + +# define ASN1_SET_OF(stname, field, type) \ + ASN1_EX_TYPE(ASN1_TFLG_SET_OF, 0, stname, field, type) + +# define ASN1_SET_OF_OPT(stname, field, type) \ + ASN1_EX_TYPE(ASN1_TFLG_SET_OF|ASN1_TFLG_OPTIONAL, 0, stname, field, type) + +/* Finally compound types of SEQUENCE, SET, IMPLICIT, EXPLICIT and OPTIONAL */ + +# define ASN1_IMP_SET_OF(stname, field, type, tag) \ + ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF) + +# define ASN1_EXP_SET_OF(stname, field, type, tag) \ + ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF) + +# define ASN1_IMP_SET_OF_OPT(stname, field, type, tag) \ + ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF|ASN1_TFLG_OPTIONAL) + +# define ASN1_EXP_SET_OF_OPT(stname, field, type, tag) \ + ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF|ASN1_TFLG_OPTIONAL) + +# define ASN1_IMP_SEQUENCE_OF(stname, field, type, tag) \ + ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF) + +# define ASN1_IMP_SEQUENCE_OF_OPT(stname, field, type, tag) \ + ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF|ASN1_TFLG_OPTIONAL) + +# define ASN1_EXP_SEQUENCE_OF(stname, field, type, tag) \ + ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF) + +# define ASN1_EXP_SEQUENCE_OF_OPT(stname, field, type, tag) \ + ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF|ASN1_TFLG_OPTIONAL) + +/* EXPLICIT using indefinite length constructed form */ +# define ASN1_NDEF_EXP(stname, field, type, tag) \ + ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_NDEF) + +/* EXPLICIT OPTIONAL using indefinite length constructed form */ +# define ASN1_NDEF_EXP_OPT(stname, field, type, tag) \ + ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL|ASN1_TFLG_NDEF) + +/* Macros for the ASN1_ADB structure */ + +# define ASN1_ADB(name) \ + static const ASN1_ADB_TABLE name##_adbtbl[] + +# ifndef OPENSSL_EXPORT_VAR_AS_FUNCTION + +# define ASN1_ADB_END(name, flags, field, adb_cb, def, none) \ + ;\ + static const ASN1_ADB name##_adb = {\ + flags,\ + offsetof(name, field),\ + adb_cb,\ + name##_adbtbl,\ + sizeof(name##_adbtbl) / sizeof(ASN1_ADB_TABLE),\ + def,\ + none\ + } + +# else + +# define ASN1_ADB_END(name, flags, field, adb_cb, def, none) \ + ;\ + static const ASN1_ITEM *name##_adb(void) \ + { \ + static const ASN1_ADB internal_adb = \ + {\ + flags,\ + offsetof(name, field),\ + adb_cb,\ + name##_adbtbl,\ + sizeof(name##_adbtbl) / sizeof(ASN1_ADB_TABLE),\ + def,\ + none\ + }; \ + return (const ASN1_ITEM *) &internal_adb; \ + } \ + void dummy_function(void) + +# endif + +# define ADB_ENTRY(val, template) {val, template} + +# define ASN1_ADB_TEMPLATE(name) \ + static const ASN1_TEMPLATE name##_tt + +/* + * This is the ASN1 template structure that defines a wrapper round the + * actual type. It determines the actual position of the field in the value + * structure, various flags such as OPTIONAL and the field name. + */ + +struct ASN1_TEMPLATE_st { + unsigned long flags; /* Various flags */ + long tag; /* tag, not used if no tagging */ + unsigned long offset; /* Offset of this field in structure */ + const char *field_name; /* Field name */ + ASN1_ITEM_EXP *item; /* Relevant ASN1_ITEM or ASN1_ADB */ +}; + +/* Macro to extract ASN1_ITEM and ASN1_ADB pointer from ASN1_TEMPLATE */ + +# define ASN1_TEMPLATE_item(t) (t->item_ptr) +# define ASN1_TEMPLATE_adb(t) (t->item_ptr) + +typedef struct ASN1_ADB_TABLE_st ASN1_ADB_TABLE; +typedef struct ASN1_ADB_st ASN1_ADB; + +struct ASN1_ADB_st { + unsigned long flags; /* Various flags */ + unsigned long offset; /* Offset of selector field */ + int (*adb_cb)(long *psel); /* Application callback */ + const ASN1_ADB_TABLE *tbl; /* Table of possible types */ + long tblcount; /* Number of entries in tbl */ + const ASN1_TEMPLATE *default_tt; /* Type to use if no match */ + const ASN1_TEMPLATE *null_tt; /* Type to use if selector is NULL */ +}; + +struct ASN1_ADB_TABLE_st { + long value; /* NID for an object or value for an int */ + const ASN1_TEMPLATE tt; /* item for this value */ +}; + +/* template flags */ + +/* Field is optional */ +# define ASN1_TFLG_OPTIONAL (0x1) + +/* Field is a SET OF */ +# define ASN1_TFLG_SET_OF (0x1 << 1) + +/* Field is a SEQUENCE OF */ +# define ASN1_TFLG_SEQUENCE_OF (0x2 << 1) + +/* + * Special case: this refers to a SET OF that will be sorted into DER order + * when encoded *and* the corresponding STACK will be modified to match the + * new order. + */ +# define ASN1_TFLG_SET_ORDER (0x3 << 1) + +/* Mask for SET OF or SEQUENCE OF */ +# define ASN1_TFLG_SK_MASK (0x3 << 1) + +/* + * These flags mean the tag should be taken from the tag field. If EXPLICIT + * then the underlying type is used for the inner tag. + */ + +/* IMPLICIT tagging */ +# define ASN1_TFLG_IMPTAG (0x1 << 3) + +/* EXPLICIT tagging, inner tag from underlying type */ +# define ASN1_TFLG_EXPTAG (0x2 << 3) + +# define ASN1_TFLG_TAG_MASK (0x3 << 3) + +/* context specific IMPLICIT */ +# define ASN1_TFLG_IMPLICIT (ASN1_TFLG_IMPTAG|ASN1_TFLG_CONTEXT) + +/* context specific EXPLICIT */ +# define ASN1_TFLG_EXPLICIT (ASN1_TFLG_EXPTAG|ASN1_TFLG_CONTEXT) + +/* + * If tagging is in force these determine the type of tag to use. Otherwise + * the tag is determined by the underlying type. These values reflect the + * actual octet format. + */ + +/* Universal tag */ +# define ASN1_TFLG_UNIVERSAL (0x0<<6) +/* Application tag */ +# define ASN1_TFLG_APPLICATION (0x1<<6) +/* Context specific tag */ +# define ASN1_TFLG_CONTEXT (0x2<<6) +/* Private tag */ +# define ASN1_TFLG_PRIVATE (0x3<<6) + +# define ASN1_TFLG_TAG_CLASS (0x3<<6) + +/* + * These are for ANY DEFINED BY type. In this case the 'item' field points to + * an ASN1_ADB structure which contains a table of values to decode the + * relevant type + */ + +# define ASN1_TFLG_ADB_MASK (0x3<<8) + +# define ASN1_TFLG_ADB_OID (0x1<<8) + +# define ASN1_TFLG_ADB_INT (0x1<<9) + +/* + * This flag when present in a SEQUENCE OF, SET OF or EXPLICIT causes + * indefinite length constructed encoding to be used if required. + */ + +# define ASN1_TFLG_NDEF (0x1<<11) + +/* Field is embedded and not a pointer */ +# define ASN1_TFLG_EMBED (0x1 << 12) + +/* This is the actual ASN1 item itself */ + +struct ASN1_ITEM_st { + char itype; /* The item type, primitive, SEQUENCE, CHOICE + * or extern */ + long utype; /* underlying type */ + const ASN1_TEMPLATE *templates; /* If SEQUENCE or CHOICE this contains + * the contents */ + long tcount; /* Number of templates if SEQUENCE or CHOICE */ + const void *funcs; /* functions that handle this type */ + long size; /* Structure size (usually) */ + const char *sname; /* Structure name */ +}; + +/*- + * These are values for the itype field and + * determine how the type is interpreted. + * + * For PRIMITIVE types the underlying type + * determines the behaviour if items is NULL. + * + * Otherwise templates must contain a single + * template and the type is treated in the + * same way as the type specified in the template. + * + * For SEQUENCE types the templates field points + * to the members, the size field is the + * structure size. + * + * For CHOICE types the templates field points + * to each possible member (typically a union) + * and the 'size' field is the offset of the + * selector. + * + * The 'funcs' field is used for application + * specific functions. + * + * The EXTERN type uses a new style d2i/i2d. + * The new style should be used where possible + * because it avoids things like the d2i IMPLICIT + * hack. + * + * MSTRING is a multiple string type, it is used + * for a CHOICE of character strings where the + * actual strings all occupy an ASN1_STRING + * structure. In this case the 'utype' field + * has a special meaning, it is used as a mask + * of acceptable types using the B_ASN1 constants. + * + * NDEF_SEQUENCE is the same as SEQUENCE except + * that it will use indefinite length constructed + * encoding if requested. + * + */ + +# define ASN1_ITYPE_PRIMITIVE 0x0 + +# define ASN1_ITYPE_SEQUENCE 0x1 + +# define ASN1_ITYPE_CHOICE 0x2 + +# define ASN1_ITYPE_EXTERN 0x4 + +# define ASN1_ITYPE_MSTRING 0x5 + +# define ASN1_ITYPE_NDEF_SEQUENCE 0x6 + +/* + * Cache for ASN1 tag and length, so we don't keep re-reading it for things + * like CHOICE + */ + +struct ASN1_TLC_st { + char valid; /* Values below are valid */ + int ret; /* return value */ + long plen; /* length */ + int ptag; /* class value */ + int pclass; /* class value */ + int hdrlen; /* header length */ +}; + +/* Typedefs for ASN1 function pointers */ +typedef int ASN1_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len, + const ASN1_ITEM *it, int tag, int aclass, char opt, + ASN1_TLC *ctx); + +typedef int ASN1_ex_i2d(ASN1_VALUE **pval, unsigned char **out, + const ASN1_ITEM *it, int tag, int aclass); +typedef int ASN1_ex_new_func(ASN1_VALUE **pval, const ASN1_ITEM *it); +typedef void ASN1_ex_free_func(ASN1_VALUE **pval, const ASN1_ITEM *it); + +typedef int ASN1_ex_print_func(BIO *out, ASN1_VALUE **pval, + int indent, const char *fname, + const ASN1_PCTX *pctx); + +typedef int ASN1_primitive_i2c(ASN1_VALUE **pval, unsigned char *cont, + int *putype, const ASN1_ITEM *it); +typedef int ASN1_primitive_c2i(ASN1_VALUE **pval, const unsigned char *cont, + int len, int utype, char *free_cont, + const ASN1_ITEM *it); +typedef int ASN1_primitive_print(BIO *out, ASN1_VALUE **pval, + const ASN1_ITEM *it, int indent, + const ASN1_PCTX *pctx); + +typedef struct ASN1_EXTERN_FUNCS_st { + void *app_data; + ASN1_ex_new_func *asn1_ex_new; + ASN1_ex_free_func *asn1_ex_free; + ASN1_ex_free_func *asn1_ex_clear; + ASN1_ex_d2i *asn1_ex_d2i; + ASN1_ex_i2d *asn1_ex_i2d; + ASN1_ex_print_func *asn1_ex_print; +} ASN1_EXTERN_FUNCS; + +typedef struct ASN1_PRIMITIVE_FUNCS_st { + void *app_data; + unsigned long flags; + ASN1_ex_new_func *prim_new; + ASN1_ex_free_func *prim_free; + ASN1_ex_free_func *prim_clear; + ASN1_primitive_c2i *prim_c2i; + ASN1_primitive_i2c *prim_i2c; + ASN1_primitive_print *prim_print; +} ASN1_PRIMITIVE_FUNCS; + +/* + * This is the ASN1_AUX structure: it handles various miscellaneous + * requirements. For example the use of reference counts and an informational + * callback. The "informational callback" is called at various points during + * the ASN1 encoding and decoding. It can be used to provide minor + * customisation of the structures used. This is most useful where the + * supplied routines *almost* do the right thing but need some extra help at + * a few points. If the callback returns zero then it is assumed a fatal + * error has occurred and the main operation should be abandoned. If major + * changes in the default behaviour are required then an external type is + * more appropriate. + */ + +typedef int ASN1_aux_cb(int operation, ASN1_VALUE **in, const ASN1_ITEM *it, + void *exarg); + +typedef struct ASN1_AUX_st { + void *app_data; + int flags; + int ref_offset; /* Offset of reference value */ + int ref_lock; /* Lock type to use */ + ASN1_aux_cb *asn1_cb; + int enc_offset; /* Offset of ASN1_ENCODING structure */ +} ASN1_AUX; + +/* For print related callbacks exarg points to this structure */ +typedef struct ASN1_PRINT_ARG_st { + BIO *out; + int indent; + const ASN1_PCTX *pctx; +} ASN1_PRINT_ARG; + +/* For streaming related callbacks exarg points to this structure */ +typedef struct ASN1_STREAM_ARG_st { + /* BIO to stream through */ + BIO *out; + /* BIO with filters appended */ + BIO *ndef_bio; + /* Streaming I/O boundary */ + unsigned char **boundary; +} ASN1_STREAM_ARG; + +/* Flags in ASN1_AUX */ + +/* Use a reference count */ +# define ASN1_AFLG_REFCOUNT 1 +/* Save the encoding of structure (useful for signatures) */ +# define ASN1_AFLG_ENCODING 2 +/* The Sequence length is invalid */ +# define ASN1_AFLG_BROKEN 4 + +/* operation values for asn1_cb */ + +# define ASN1_OP_NEW_PRE 0 +# define ASN1_OP_NEW_POST 1 +# define ASN1_OP_FREE_PRE 2 +# define ASN1_OP_FREE_POST 3 +# define ASN1_OP_D2I_PRE 4 +# define ASN1_OP_D2I_POST 5 +# define ASN1_OP_I2D_PRE 6 +# define ASN1_OP_I2D_POST 7 +# define ASN1_OP_PRINT_PRE 8 +# define ASN1_OP_PRINT_POST 9 +# define ASN1_OP_STREAM_PRE 10 +# define ASN1_OP_STREAM_POST 11 +# define ASN1_OP_DETACHED_PRE 12 +# define ASN1_OP_DETACHED_POST 13 + +/* Macro to implement a primitive type */ +# define IMPLEMENT_ASN1_TYPE(stname) IMPLEMENT_ASN1_TYPE_ex(stname, stname, 0) +# define IMPLEMENT_ASN1_TYPE_ex(itname, vname, ex) \ + ASN1_ITEM_start(itname) \ + ASN1_ITYPE_PRIMITIVE, V_##vname, NULL, 0, NULL, ex, #itname \ + ASN1_ITEM_end(itname) + +/* Macro to implement a multi string type */ +# define IMPLEMENT_ASN1_MSTRING(itname, mask) \ + ASN1_ITEM_start(itname) \ + ASN1_ITYPE_MSTRING, mask, NULL, 0, NULL, sizeof(ASN1_STRING), #itname \ + ASN1_ITEM_end(itname) + +# define IMPLEMENT_EXTERN_ASN1(sname, tag, fptrs) \ + ASN1_ITEM_start(sname) \ + ASN1_ITYPE_EXTERN, \ + tag, \ + NULL, \ + 0, \ + &fptrs, \ + 0, \ + #sname \ + ASN1_ITEM_end(sname) + +/* Macro to implement standard functions in terms of ASN1_ITEM structures */ + +# define IMPLEMENT_ASN1_FUNCTIONS(stname) IMPLEMENT_ASN1_FUNCTIONS_fname(stname, stname, stname) + +# define IMPLEMENT_ASN1_FUNCTIONS_name(stname, itname) IMPLEMENT_ASN1_FUNCTIONS_fname(stname, itname, itname) + +# define IMPLEMENT_ASN1_FUNCTIONS_ENCODE_name(stname, itname) \ + IMPLEMENT_ASN1_FUNCTIONS_ENCODE_fname(stname, itname, itname) + +# define IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(stname) \ + IMPLEMENT_ASN1_ALLOC_FUNCTIONS_pfname(static, stname, stname, stname) + +# define IMPLEMENT_ASN1_ALLOC_FUNCTIONS(stname) \ + IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, stname, stname) + +# define IMPLEMENT_ASN1_ALLOC_FUNCTIONS_pfname(pre, stname, itname, fname) \ + pre stname *fname##_new(void) \ + { \ + return (stname *)ASN1_item_new(ASN1_ITEM_rptr(itname)); \ + } \ + pre void fname##_free(stname *a) \ + { \ + ASN1_item_free((ASN1_VALUE *)a, ASN1_ITEM_rptr(itname)); \ + } + +# define IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname) \ + stname *fname##_new(void) \ + { \ + return (stname *)ASN1_item_new(ASN1_ITEM_rptr(itname)); \ + } \ + void fname##_free(stname *a) \ + { \ + ASN1_item_free((ASN1_VALUE *)a, ASN1_ITEM_rptr(itname)); \ + } + +# define IMPLEMENT_ASN1_FUNCTIONS_fname(stname, itname, fname) \ + IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(stname, itname, fname) \ + IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname) + +# define IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(stname, itname, fname) \ + stname *d2i_##fname(stname **a, const unsigned char **in, long len) \ + { \ + return (stname *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, ASN1_ITEM_rptr(itname));\ + } \ + int i2d_##fname(stname *a, unsigned char **out) \ + { \ + return ASN1_item_i2d((ASN1_VALUE *)a, out, ASN1_ITEM_rptr(itname));\ + } + +# define IMPLEMENT_ASN1_NDEF_FUNCTION(stname) \ + int i2d_##stname##_NDEF(stname *a, unsigned char **out) \ + { \ + return ASN1_item_ndef_i2d((ASN1_VALUE *)a, out, ASN1_ITEM_rptr(stname));\ + } + +# define IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(stname) \ + static stname *d2i_##stname(stname **a, \ + const unsigned char **in, long len) \ + { \ + return (stname *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, \ + ASN1_ITEM_rptr(stname)); \ + } \ + static int i2d_##stname(stname *a, unsigned char **out) \ + { \ + return ASN1_item_i2d((ASN1_VALUE *)a, out, \ + ASN1_ITEM_rptr(stname)); \ + } + +/* + * This includes evil casts to remove const: they will go away when full ASN1 + * constification is done. + */ +# define IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(stname, itname, fname) \ + stname *d2i_##fname(stname **a, const unsigned char **in, long len) \ + { \ + return (stname *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, ASN1_ITEM_rptr(itname));\ + } \ + int i2d_##fname(const stname *a, unsigned char **out) \ + { \ + return ASN1_item_i2d((ASN1_VALUE *)a, out, ASN1_ITEM_rptr(itname));\ + } + +# define IMPLEMENT_ASN1_DUP_FUNCTION(stname) \ + stname * stname##_dup(stname *x) \ + { \ + return ASN1_item_dup(ASN1_ITEM_rptr(stname), x); \ + } + +# define IMPLEMENT_ASN1_PRINT_FUNCTION(stname) \ + IMPLEMENT_ASN1_PRINT_FUNCTION_fname(stname, stname, stname) + +# define IMPLEMENT_ASN1_PRINT_FUNCTION_fname(stname, itname, fname) \ + int fname##_print_ctx(BIO *out, stname *x, int indent, \ + const ASN1_PCTX *pctx) \ + { \ + return ASN1_item_print(out, (ASN1_VALUE *)x, indent, \ + ASN1_ITEM_rptr(itname), pctx); \ + } + +# define IMPLEMENT_ASN1_FUNCTIONS_const(name) \ + IMPLEMENT_ASN1_FUNCTIONS_const_fname(name, name, name) + +# define IMPLEMENT_ASN1_FUNCTIONS_const_fname(stname, itname, fname) \ + IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(stname, itname, fname) \ + IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname) + +/* external definitions for primitive types */ + +DECLARE_ASN1_ITEM(ASN1_BOOLEAN) +DECLARE_ASN1_ITEM(ASN1_TBOOLEAN) +DECLARE_ASN1_ITEM(ASN1_FBOOLEAN) +DECLARE_ASN1_ITEM(ASN1_SEQUENCE) +DECLARE_ASN1_ITEM(CBIGNUM) +DECLARE_ASN1_ITEM(BIGNUM) +DECLARE_ASN1_ITEM(INT32) +DECLARE_ASN1_ITEM(ZINT32) +DECLARE_ASN1_ITEM(UINT32) +DECLARE_ASN1_ITEM(ZUINT32) +DECLARE_ASN1_ITEM(INT64) +DECLARE_ASN1_ITEM(ZINT64) +DECLARE_ASN1_ITEM(UINT64) +DECLARE_ASN1_ITEM(ZUINT64) + +# if OPENSSL_API_COMPAT < 0x10200000L +/* + * LONG and ZLONG are strongly discouraged for use as stored data, as the + * underlying C type (long) differs in size depending on the architecture. + * They are designed with 32-bit longs in mind. + */ +DECLARE_ASN1_ITEM(LONG) +DECLARE_ASN1_ITEM(ZLONG) +# endif + +DEFINE_STACK_OF(ASN1_VALUE) + +/* Functions used internally by the ASN1 code */ + +int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it); +void ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it); + +int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len, + const ASN1_ITEM *it, int tag, int aclass, char opt, + ASN1_TLC *ctx); + +int ASN1_item_ex_i2d(ASN1_VALUE **pval, unsigned char **out, + const ASN1_ITEM *it, int tag, int aclass); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/async.h b/example/android/third_party/crypto/arm/include/openssl/async.h new file mode 100644 index 00000000..7052b890 --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/async.h @@ -0,0 +1,76 @@ +/* + * Copyright 2015-2018 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#include + +#ifndef HEADER_ASYNC_H +# define HEADER_ASYNC_H + +#if defined(_WIN32) +# if defined(BASETYPES) || defined(_WINDEF_H) +/* application has to include to use this */ +#define OSSL_ASYNC_FD HANDLE +#define OSSL_BAD_ASYNC_FD INVALID_HANDLE_VALUE +# endif +#else +#define OSSL_ASYNC_FD int +#define OSSL_BAD_ASYNC_FD -1 +#endif +# include + + +# ifdef __cplusplus +extern "C" { +# endif + +typedef struct async_job_st ASYNC_JOB; +typedef struct async_wait_ctx_st ASYNC_WAIT_CTX; + +#define ASYNC_ERR 0 +#define ASYNC_NO_JOBS 1 +#define ASYNC_PAUSE 2 +#define ASYNC_FINISH 3 + +int ASYNC_init_thread(size_t max_size, size_t init_size); +void ASYNC_cleanup_thread(void); + +#ifdef OSSL_ASYNC_FD +ASYNC_WAIT_CTX *ASYNC_WAIT_CTX_new(void); +void ASYNC_WAIT_CTX_free(ASYNC_WAIT_CTX *ctx); +int ASYNC_WAIT_CTX_set_wait_fd(ASYNC_WAIT_CTX *ctx, const void *key, + OSSL_ASYNC_FD fd, + void *custom_data, + void (*cleanup)(ASYNC_WAIT_CTX *, const void *, + OSSL_ASYNC_FD, void *)); +int ASYNC_WAIT_CTX_get_fd(ASYNC_WAIT_CTX *ctx, const void *key, + OSSL_ASYNC_FD *fd, void **custom_data); +int ASYNC_WAIT_CTX_get_all_fds(ASYNC_WAIT_CTX *ctx, OSSL_ASYNC_FD *fd, + size_t *numfds); +int ASYNC_WAIT_CTX_get_changed_fds(ASYNC_WAIT_CTX *ctx, OSSL_ASYNC_FD *addfd, + size_t *numaddfds, OSSL_ASYNC_FD *delfd, + size_t *numdelfds); +int ASYNC_WAIT_CTX_clear_fd(ASYNC_WAIT_CTX *ctx, const void *key); +#endif + +int ASYNC_is_capable(void); + +int ASYNC_start_job(ASYNC_JOB **job, ASYNC_WAIT_CTX *ctx, int *ret, + int (*func)(void *), void *args, size_t size); +int ASYNC_pause_job(void); + +ASYNC_JOB *ASYNC_get_current_job(void); +ASYNC_WAIT_CTX *ASYNC_get_wait_ctx(ASYNC_JOB *job); +void ASYNC_block_pause(void); +void ASYNC_unblock_pause(void); + + +# ifdef __cplusplus +} +# endif +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/asyncerr.h b/example/android/third_party/crypto/arm/include/openssl/asyncerr.h new file mode 100644 index 00000000..5497ba75 --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/asyncerr.h @@ -0,0 +1,38 @@ +/* + * Generated by util/mkerr.pl DO NOT EDIT + * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_ASYNCERR_H +# define HEADER_ASYNCERR_H + +# ifdef __cplusplus +extern "C" +# endif +int ERR_load_ASYNC_strings(void); + +/* + * ASYNC function codes. + */ +# define ASYNC_F_ASYNC_CTX_NEW 100 +# define ASYNC_F_ASYNC_INIT_THREAD 101 +# define ASYNC_F_ASYNC_JOB_NEW 102 +# define ASYNC_F_ASYNC_PAUSE_JOB 103 +# define ASYNC_F_ASYNC_START_FUNC 104 +# define ASYNC_F_ASYNC_START_JOB 105 +# define ASYNC_F_ASYNC_WAIT_CTX_SET_WAIT_FD 106 + +/* + * ASYNC reason codes. + */ +# define ASYNC_R_FAILED_TO_SET_POOL 101 +# define ASYNC_R_FAILED_TO_SWAP_CONTEXT 102 +# define ASYNC_R_INIT_FAILED 105 +# define ASYNC_R_INVALID_POOL_SIZE 103 + +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/bio.h b/example/android/third_party/crypto/arm/include/openssl/bio.h new file mode 100644 index 00000000..2888b42d --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/bio.h @@ -0,0 +1,804 @@ +/* + * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_BIO_H +# define HEADER_BIO_H + +# include + +# ifndef OPENSSL_NO_STDIO +# include +# endif +# include + +# include +# include + +# ifndef OPENSSL_NO_SCTP +# include +# endif + +#ifdef __cplusplus +extern "C" { +#endif + +/* There are the classes of BIOs */ +# define BIO_TYPE_DESCRIPTOR 0x0100 /* socket, fd, connect or accept */ +# define BIO_TYPE_FILTER 0x0200 +# define BIO_TYPE_SOURCE_SINK 0x0400 + +/* These are the 'types' of BIOs */ +# define BIO_TYPE_NONE 0 +# define BIO_TYPE_MEM ( 1|BIO_TYPE_SOURCE_SINK) +# define BIO_TYPE_FILE ( 2|BIO_TYPE_SOURCE_SINK) + +# define BIO_TYPE_FD ( 4|BIO_TYPE_SOURCE_SINK|BIO_TYPE_DESCRIPTOR) +# define BIO_TYPE_SOCKET ( 5|BIO_TYPE_SOURCE_SINK|BIO_TYPE_DESCRIPTOR) +# define BIO_TYPE_NULL ( 6|BIO_TYPE_SOURCE_SINK) +# define BIO_TYPE_SSL ( 7|BIO_TYPE_FILTER) +# define BIO_TYPE_MD ( 8|BIO_TYPE_FILTER) +# define BIO_TYPE_BUFFER ( 9|BIO_TYPE_FILTER) +# define BIO_TYPE_CIPHER (10|BIO_TYPE_FILTER) +# define BIO_TYPE_BASE64 (11|BIO_TYPE_FILTER) +# define BIO_TYPE_CONNECT (12|BIO_TYPE_SOURCE_SINK|BIO_TYPE_DESCRIPTOR) +# define BIO_TYPE_ACCEPT (13|BIO_TYPE_SOURCE_SINK|BIO_TYPE_DESCRIPTOR) + +# define BIO_TYPE_NBIO_TEST (16|BIO_TYPE_FILTER)/* server proxy BIO */ +# define BIO_TYPE_NULL_FILTER (17|BIO_TYPE_FILTER) +# define BIO_TYPE_BIO (19|BIO_TYPE_SOURCE_SINK)/* half a BIO pair */ +# define BIO_TYPE_LINEBUFFER (20|BIO_TYPE_FILTER) +# define BIO_TYPE_DGRAM (21|BIO_TYPE_SOURCE_SINK|BIO_TYPE_DESCRIPTOR) +# define BIO_TYPE_ASN1 (22|BIO_TYPE_FILTER) +# define BIO_TYPE_COMP (23|BIO_TYPE_FILTER) +# ifndef OPENSSL_NO_SCTP +# define BIO_TYPE_DGRAM_SCTP (24|BIO_TYPE_SOURCE_SINK|BIO_TYPE_DESCRIPTOR) +# endif + +#define BIO_TYPE_START 128 + +/* + * BIO_FILENAME_READ|BIO_CLOSE to open or close on free. + * BIO_set_fp(in,stdin,BIO_NOCLOSE); + */ +# define BIO_NOCLOSE 0x00 +# define BIO_CLOSE 0x01 + +/* + * These are used in the following macros and are passed to BIO_ctrl() + */ +# define BIO_CTRL_RESET 1/* opt - rewind/zero etc */ +# define BIO_CTRL_EOF 2/* opt - are we at the eof */ +# define BIO_CTRL_INFO 3/* opt - extra tit-bits */ +# define BIO_CTRL_SET 4/* man - set the 'IO' type */ +# define BIO_CTRL_GET 5/* man - get the 'IO' type */ +# define BIO_CTRL_PUSH 6/* opt - internal, used to signify change */ +# define BIO_CTRL_POP 7/* opt - internal, used to signify change */ +# define BIO_CTRL_GET_CLOSE 8/* man - set the 'close' on free */ +# define BIO_CTRL_SET_CLOSE 9/* man - set the 'close' on free */ +# define BIO_CTRL_PENDING 10/* opt - is their more data buffered */ +# define BIO_CTRL_FLUSH 11/* opt - 'flush' buffered output */ +# define BIO_CTRL_DUP 12/* man - extra stuff for 'duped' BIO */ +# define BIO_CTRL_WPENDING 13/* opt - number of bytes still to write */ +# define BIO_CTRL_SET_CALLBACK 14/* opt - set callback function */ +# define BIO_CTRL_GET_CALLBACK 15/* opt - set callback function */ + +# define BIO_CTRL_PEEK 29/* BIO_f_buffer special */ +# define BIO_CTRL_SET_FILENAME 30/* BIO_s_file special */ + +/* dgram BIO stuff */ +# define BIO_CTRL_DGRAM_CONNECT 31/* BIO dgram special */ +# define BIO_CTRL_DGRAM_SET_CONNECTED 32/* allow for an externally connected + * socket to be passed in */ +# define BIO_CTRL_DGRAM_SET_RECV_TIMEOUT 33/* setsockopt, essentially */ +# define BIO_CTRL_DGRAM_GET_RECV_TIMEOUT 34/* getsockopt, essentially */ +# define BIO_CTRL_DGRAM_SET_SEND_TIMEOUT 35/* setsockopt, essentially */ +# define BIO_CTRL_DGRAM_GET_SEND_TIMEOUT 36/* getsockopt, essentially */ + +# define BIO_CTRL_DGRAM_GET_RECV_TIMER_EXP 37/* flag whether the last */ +# define BIO_CTRL_DGRAM_GET_SEND_TIMER_EXP 38/* I/O operation tiemd out */ + +/* #ifdef IP_MTU_DISCOVER */ +# define BIO_CTRL_DGRAM_MTU_DISCOVER 39/* set DF bit on egress packets */ +/* #endif */ + +# define BIO_CTRL_DGRAM_QUERY_MTU 40/* as kernel for current MTU */ +# define BIO_CTRL_DGRAM_GET_FALLBACK_MTU 47 +# define BIO_CTRL_DGRAM_GET_MTU 41/* get cached value for MTU */ +# define BIO_CTRL_DGRAM_SET_MTU 42/* set cached value for MTU. + * want to use this if asking + * the kernel fails */ + +# define BIO_CTRL_DGRAM_MTU_EXCEEDED 43/* check whether the MTU was + * exceed in the previous write + * operation */ + +# define BIO_CTRL_DGRAM_GET_PEER 46 +# define BIO_CTRL_DGRAM_SET_PEER 44/* Destination for the data */ + +# define BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT 45/* Next DTLS handshake timeout + * to adjust socket timeouts */ +# define BIO_CTRL_DGRAM_SET_DONT_FRAG 48 + +# define BIO_CTRL_DGRAM_GET_MTU_OVERHEAD 49 + +/* Deliberately outside of OPENSSL_NO_SCTP - used in bss_dgram.c */ +# define BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE 50 +# ifndef OPENSSL_NO_SCTP +/* SCTP stuff */ +# define BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY 51 +# define BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY 52 +# define BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD 53 +# define BIO_CTRL_DGRAM_SCTP_GET_SNDINFO 60 +# define BIO_CTRL_DGRAM_SCTP_SET_SNDINFO 61 +# define BIO_CTRL_DGRAM_SCTP_GET_RCVINFO 62 +# define BIO_CTRL_DGRAM_SCTP_SET_RCVINFO 63 +# define BIO_CTRL_DGRAM_SCTP_GET_PRINFO 64 +# define BIO_CTRL_DGRAM_SCTP_SET_PRINFO 65 +# define BIO_CTRL_DGRAM_SCTP_SAVE_SHUTDOWN 70 +# endif + +# define BIO_CTRL_DGRAM_SET_PEEK_MODE 71 + +/* modifiers */ +# define BIO_FP_READ 0x02 +# define BIO_FP_WRITE 0x04 +# define BIO_FP_APPEND 0x08 +# define BIO_FP_TEXT 0x10 + +# define BIO_FLAGS_READ 0x01 +# define BIO_FLAGS_WRITE 0x02 +# define BIO_FLAGS_IO_SPECIAL 0x04 +# define BIO_FLAGS_RWS (BIO_FLAGS_READ|BIO_FLAGS_WRITE|BIO_FLAGS_IO_SPECIAL) +# define BIO_FLAGS_SHOULD_RETRY 0x08 +# ifndef BIO_FLAGS_UPLINK +/* + * "UPLINK" flag denotes file descriptors provided by application. It + * defaults to 0, as most platforms don't require UPLINK interface. + */ +# define BIO_FLAGS_UPLINK 0 +# endif + +# define BIO_FLAGS_BASE64_NO_NL 0x100 + +/* + * This is used with memory BIOs: + * BIO_FLAGS_MEM_RDONLY means we shouldn't free up or change the data in any way; + * BIO_FLAGS_NONCLEAR_RST means we shouldn't clear data on reset. + */ +# define BIO_FLAGS_MEM_RDONLY 0x200 +# define BIO_FLAGS_NONCLEAR_RST 0x400 + +typedef union bio_addr_st BIO_ADDR; +typedef struct bio_addrinfo_st BIO_ADDRINFO; + +int BIO_get_new_index(void); +void BIO_set_flags(BIO *b, int flags); +int BIO_test_flags(const BIO *b, int flags); +void BIO_clear_flags(BIO *b, int flags); + +# define BIO_get_flags(b) BIO_test_flags(b, ~(0x0)) +# define BIO_set_retry_special(b) \ + BIO_set_flags(b, (BIO_FLAGS_IO_SPECIAL|BIO_FLAGS_SHOULD_RETRY)) +# define BIO_set_retry_read(b) \ + BIO_set_flags(b, (BIO_FLAGS_READ|BIO_FLAGS_SHOULD_RETRY)) +# define BIO_set_retry_write(b) \ + BIO_set_flags(b, (BIO_FLAGS_WRITE|BIO_FLAGS_SHOULD_RETRY)) + +/* These are normally used internally in BIOs */ +# define BIO_clear_retry_flags(b) \ + BIO_clear_flags(b, (BIO_FLAGS_RWS|BIO_FLAGS_SHOULD_RETRY)) +# define BIO_get_retry_flags(b) \ + BIO_test_flags(b, (BIO_FLAGS_RWS|BIO_FLAGS_SHOULD_RETRY)) + +/* These should be used by the application to tell why we should retry */ +# define BIO_should_read(a) BIO_test_flags(a, BIO_FLAGS_READ) +# define BIO_should_write(a) BIO_test_flags(a, BIO_FLAGS_WRITE) +# define BIO_should_io_special(a) BIO_test_flags(a, BIO_FLAGS_IO_SPECIAL) +# define BIO_retry_type(a) BIO_test_flags(a, BIO_FLAGS_RWS) +# define BIO_should_retry(a) BIO_test_flags(a, BIO_FLAGS_SHOULD_RETRY) + +/* + * The next three are used in conjunction with the BIO_should_io_special() + * condition. After this returns true, BIO *BIO_get_retry_BIO(BIO *bio, int + * *reason); will walk the BIO stack and return the 'reason' for the special + * and the offending BIO. Given a BIO, BIO_get_retry_reason(bio) will return + * the code. + */ +/* + * Returned from the SSL bio when the certificate retrieval code had an error + */ +# define BIO_RR_SSL_X509_LOOKUP 0x01 +/* Returned from the connect BIO when a connect would have blocked */ +# define BIO_RR_CONNECT 0x02 +/* Returned from the accept BIO when an accept would have blocked */ +# define BIO_RR_ACCEPT 0x03 + +/* These are passed by the BIO callback */ +# define BIO_CB_FREE 0x01 +# define BIO_CB_READ 0x02 +# define BIO_CB_WRITE 0x03 +# define BIO_CB_PUTS 0x04 +# define BIO_CB_GETS 0x05 +# define BIO_CB_CTRL 0x06 + +/* + * The callback is called before and after the underling operation, The + * BIO_CB_RETURN flag indicates if it is after the call + */ +# define BIO_CB_RETURN 0x80 +# define BIO_CB_return(a) ((a)|BIO_CB_RETURN) +# define BIO_cb_pre(a) (!((a)&BIO_CB_RETURN)) +# define BIO_cb_post(a) ((a)&BIO_CB_RETURN) + +typedef long (*BIO_callback_fn)(BIO *b, int oper, const char *argp, int argi, + long argl, long ret); +typedef long (*BIO_callback_fn_ex)(BIO *b, int oper, const char *argp, + size_t len, int argi, + long argl, int ret, size_t *processed); +BIO_callback_fn BIO_get_callback(const BIO *b); +void BIO_set_callback(BIO *b, BIO_callback_fn callback); + +BIO_callback_fn_ex BIO_get_callback_ex(const BIO *b); +void BIO_set_callback_ex(BIO *b, BIO_callback_fn_ex callback); + +char *BIO_get_callback_arg(const BIO *b); +void BIO_set_callback_arg(BIO *b, char *arg); + +typedef struct bio_method_st BIO_METHOD; + +const char *BIO_method_name(const BIO *b); +int BIO_method_type(const BIO *b); + +typedef int BIO_info_cb(BIO *, int, int); +typedef BIO_info_cb bio_info_cb; /* backward compatibility */ + +DEFINE_STACK_OF(BIO) + +/* Prefix and suffix callback in ASN1 BIO */ +typedef int asn1_ps_func (BIO *b, unsigned char **pbuf, int *plen, + void *parg); + +# ifndef OPENSSL_NO_SCTP +/* SCTP parameter structs */ +struct bio_dgram_sctp_sndinfo { + uint16_t snd_sid; + uint16_t snd_flags; + uint32_t snd_ppid; + uint32_t snd_context; +}; + +struct bio_dgram_sctp_rcvinfo { + uint16_t rcv_sid; + uint16_t rcv_ssn; + uint16_t rcv_flags; + uint32_t rcv_ppid; + uint32_t rcv_tsn; + uint32_t rcv_cumtsn; + uint32_t rcv_context; +}; + +struct bio_dgram_sctp_prinfo { + uint16_t pr_policy; + uint32_t pr_value; +}; +# endif + +/* + * #define BIO_CONN_get_param_hostname BIO_ctrl + */ + +# define BIO_C_SET_CONNECT 100 +# define BIO_C_DO_STATE_MACHINE 101 +# define BIO_C_SET_NBIO 102 +/* # define BIO_C_SET_PROXY_PARAM 103 */ +# define BIO_C_SET_FD 104 +# define BIO_C_GET_FD 105 +# define BIO_C_SET_FILE_PTR 106 +# define BIO_C_GET_FILE_PTR 107 +# define BIO_C_SET_FILENAME 108 +# define BIO_C_SET_SSL 109 +# define BIO_C_GET_SSL 110 +# define BIO_C_SET_MD 111 +# define BIO_C_GET_MD 112 +# define BIO_C_GET_CIPHER_STATUS 113 +# define BIO_C_SET_BUF_MEM 114 +# define BIO_C_GET_BUF_MEM_PTR 115 +# define BIO_C_GET_BUFF_NUM_LINES 116 +# define BIO_C_SET_BUFF_SIZE 117 +# define BIO_C_SET_ACCEPT 118 +# define BIO_C_SSL_MODE 119 +# define BIO_C_GET_MD_CTX 120 +/* # define BIO_C_GET_PROXY_PARAM 121 */ +# define BIO_C_SET_BUFF_READ_DATA 122/* data to read first */ +# define BIO_C_GET_CONNECT 123 +# define BIO_C_GET_ACCEPT 124 +# define BIO_C_SET_SSL_RENEGOTIATE_BYTES 125 +# define BIO_C_GET_SSL_NUM_RENEGOTIATES 126 +# define BIO_C_SET_SSL_RENEGOTIATE_TIMEOUT 127 +# define BIO_C_FILE_SEEK 128 +# define BIO_C_GET_CIPHER_CTX 129 +# define BIO_C_SET_BUF_MEM_EOF_RETURN 130/* return end of input + * value */ +# define BIO_C_SET_BIND_MODE 131 +# define BIO_C_GET_BIND_MODE 132 +# define BIO_C_FILE_TELL 133 +# define BIO_C_GET_SOCKS 134 +# define BIO_C_SET_SOCKS 135 + +# define BIO_C_SET_WRITE_BUF_SIZE 136/* for BIO_s_bio */ +# define BIO_C_GET_WRITE_BUF_SIZE 137 +# define BIO_C_MAKE_BIO_PAIR 138 +# define BIO_C_DESTROY_BIO_PAIR 139 +# define BIO_C_GET_WRITE_GUARANTEE 140 +# define BIO_C_GET_READ_REQUEST 141 +# define BIO_C_SHUTDOWN_WR 142 +# define BIO_C_NREAD0 143 +# define BIO_C_NREAD 144 +# define BIO_C_NWRITE0 145 +# define BIO_C_NWRITE 146 +# define BIO_C_RESET_READ_REQUEST 147 +# define BIO_C_SET_MD_CTX 148 + +# define BIO_C_SET_PREFIX 149 +# define BIO_C_GET_PREFIX 150 +# define BIO_C_SET_SUFFIX 151 +# define BIO_C_GET_SUFFIX 152 + +# define BIO_C_SET_EX_ARG 153 +# define BIO_C_GET_EX_ARG 154 + +# define BIO_C_SET_CONNECT_MODE 155 + +# define BIO_set_app_data(s,arg) BIO_set_ex_data(s,0,arg) +# define BIO_get_app_data(s) BIO_get_ex_data(s,0) + +# define BIO_set_nbio(b,n) BIO_ctrl(b,BIO_C_SET_NBIO,(n),NULL) + +# ifndef OPENSSL_NO_SOCK +/* IP families we support, for BIO_s_connect() and BIO_s_accept() */ +/* Note: the underlying operating system may not support some of them */ +# define BIO_FAMILY_IPV4 4 +# define BIO_FAMILY_IPV6 6 +# define BIO_FAMILY_IPANY 256 + +/* BIO_s_connect() */ +# define BIO_set_conn_hostname(b,name) BIO_ctrl(b,BIO_C_SET_CONNECT,0, \ + (char *)(name)) +# define BIO_set_conn_port(b,port) BIO_ctrl(b,BIO_C_SET_CONNECT,1, \ + (char *)(port)) +# define BIO_set_conn_address(b,addr) BIO_ctrl(b,BIO_C_SET_CONNECT,2, \ + (char *)(addr)) +# define BIO_set_conn_ip_family(b,f) BIO_int_ctrl(b,BIO_C_SET_CONNECT,3,f) +# define BIO_get_conn_hostname(b) ((const char *)BIO_ptr_ctrl(b,BIO_C_GET_CONNECT,0)) +# define BIO_get_conn_port(b) ((const char *)BIO_ptr_ctrl(b,BIO_C_GET_CONNECT,1)) +# define BIO_get_conn_address(b) ((const BIO_ADDR *)BIO_ptr_ctrl(b,BIO_C_GET_CONNECT,2)) +# define BIO_get_conn_ip_family(b) BIO_ctrl(b,BIO_C_GET_CONNECT,3,NULL) +# define BIO_set_conn_mode(b,n) BIO_ctrl(b,BIO_C_SET_CONNECT_MODE,(n),NULL) + +/* BIO_s_accept() */ +# define BIO_set_accept_name(b,name) BIO_ctrl(b,BIO_C_SET_ACCEPT,0, \ + (char *)(name)) +# define BIO_set_accept_port(b,port) BIO_ctrl(b,BIO_C_SET_ACCEPT,1, \ + (char *)(port)) +# define BIO_get_accept_name(b) ((const char *)BIO_ptr_ctrl(b,BIO_C_GET_ACCEPT,0)) +# define BIO_get_accept_port(b) ((const char *)BIO_ptr_ctrl(b,BIO_C_GET_ACCEPT,1)) +# define BIO_get_peer_name(b) ((const char *)BIO_ptr_ctrl(b,BIO_C_GET_ACCEPT,2)) +# define BIO_get_peer_port(b) ((const char *)BIO_ptr_ctrl(b,BIO_C_GET_ACCEPT,3)) +/* #define BIO_set_nbio(b,n) BIO_ctrl(b,BIO_C_SET_NBIO,(n),NULL) */ +# define BIO_set_nbio_accept(b,n) BIO_ctrl(b,BIO_C_SET_ACCEPT,2,(n)?(void *)"a":NULL) +# define BIO_set_accept_bios(b,bio) BIO_ctrl(b,BIO_C_SET_ACCEPT,3, \ + (char *)(bio)) +# define BIO_set_accept_ip_family(b,f) BIO_int_ctrl(b,BIO_C_SET_ACCEPT,4,f) +# define BIO_get_accept_ip_family(b) BIO_ctrl(b,BIO_C_GET_ACCEPT,4,NULL) + +/* Aliases kept for backward compatibility */ +# define BIO_BIND_NORMAL 0 +# define BIO_BIND_REUSEADDR BIO_SOCK_REUSEADDR +# define BIO_BIND_REUSEADDR_IF_UNUSED BIO_SOCK_REUSEADDR +# define BIO_set_bind_mode(b,mode) BIO_ctrl(b,BIO_C_SET_BIND_MODE,mode,NULL) +# define BIO_get_bind_mode(b) BIO_ctrl(b,BIO_C_GET_BIND_MODE,0,NULL) + +/* BIO_s_accept() and BIO_s_connect() */ +# define BIO_do_connect(b) BIO_do_handshake(b) +# define BIO_do_accept(b) BIO_do_handshake(b) +# endif /* OPENSSL_NO_SOCK */ + +# define BIO_do_handshake(b) BIO_ctrl(b,BIO_C_DO_STATE_MACHINE,0,NULL) + +/* BIO_s_datagram(), BIO_s_fd(), BIO_s_socket(), BIO_s_accept() and BIO_s_connect() */ +# define BIO_set_fd(b,fd,c) BIO_int_ctrl(b,BIO_C_SET_FD,c,fd) +# define BIO_get_fd(b,c) BIO_ctrl(b,BIO_C_GET_FD,0,(char *)(c)) + +/* BIO_s_file() */ +# define BIO_set_fp(b,fp,c) BIO_ctrl(b,BIO_C_SET_FILE_PTR,c,(char *)(fp)) +# define BIO_get_fp(b,fpp) BIO_ctrl(b,BIO_C_GET_FILE_PTR,0,(char *)(fpp)) + +/* BIO_s_fd() and BIO_s_file() */ +# define BIO_seek(b,ofs) (int)BIO_ctrl(b,BIO_C_FILE_SEEK,ofs,NULL) +# define BIO_tell(b) (int)BIO_ctrl(b,BIO_C_FILE_TELL,0,NULL) + +/* + * name is cast to lose const, but might be better to route through a + * function so we can do it safely + */ +# ifdef CONST_STRICT +/* + * If you are wondering why this isn't defined, its because CONST_STRICT is + * purely a compile-time kludge to allow const to be checked. + */ +int BIO_read_filename(BIO *b, const char *name); +# else +# define BIO_read_filename(b,name) (int)BIO_ctrl(b,BIO_C_SET_FILENAME, \ + BIO_CLOSE|BIO_FP_READ,(char *)(name)) +# endif +# define BIO_write_filename(b,name) (int)BIO_ctrl(b,BIO_C_SET_FILENAME, \ + BIO_CLOSE|BIO_FP_WRITE,name) +# define BIO_append_filename(b,name) (int)BIO_ctrl(b,BIO_C_SET_FILENAME, \ + BIO_CLOSE|BIO_FP_APPEND,name) +# define BIO_rw_filename(b,name) (int)BIO_ctrl(b,BIO_C_SET_FILENAME, \ + BIO_CLOSE|BIO_FP_READ|BIO_FP_WRITE,name) + +/* + * WARNING WARNING, this ups the reference count on the read bio of the SSL + * structure. This is because the ssl read BIO is now pointed to by the + * next_bio field in the bio. So when you free the BIO, make sure you are + * doing a BIO_free_all() to catch the underlying BIO. + */ +# define BIO_set_ssl(b,ssl,c) BIO_ctrl(b,BIO_C_SET_SSL,c,(char *)(ssl)) +# define BIO_get_ssl(b,sslp) BIO_ctrl(b,BIO_C_GET_SSL,0,(char *)(sslp)) +# define BIO_set_ssl_mode(b,client) BIO_ctrl(b,BIO_C_SSL_MODE,client,NULL) +# define BIO_set_ssl_renegotiate_bytes(b,num) \ + BIO_ctrl(b,BIO_C_SET_SSL_RENEGOTIATE_BYTES,num,NULL) +# define BIO_get_num_renegotiates(b) \ + BIO_ctrl(b,BIO_C_GET_SSL_NUM_RENEGOTIATES,0,NULL) +# define BIO_set_ssl_renegotiate_timeout(b,seconds) \ + BIO_ctrl(b,BIO_C_SET_SSL_RENEGOTIATE_TIMEOUT,seconds,NULL) + +/* defined in evp.h */ +/* #define BIO_set_md(b,md) BIO_ctrl(b,BIO_C_SET_MD,1,(char *)(md)) */ + +# define BIO_get_mem_data(b,pp) BIO_ctrl(b,BIO_CTRL_INFO,0,(char *)(pp)) +# define BIO_set_mem_buf(b,bm,c) BIO_ctrl(b,BIO_C_SET_BUF_MEM,c,(char *)(bm)) +# define BIO_get_mem_ptr(b,pp) BIO_ctrl(b,BIO_C_GET_BUF_MEM_PTR,0, \ + (char *)(pp)) +# define BIO_set_mem_eof_return(b,v) \ + BIO_ctrl(b,BIO_C_SET_BUF_MEM_EOF_RETURN,v,NULL) + +/* For the BIO_f_buffer() type */ +# define BIO_get_buffer_num_lines(b) BIO_ctrl(b,BIO_C_GET_BUFF_NUM_LINES,0,NULL) +# define BIO_set_buffer_size(b,size) BIO_ctrl(b,BIO_C_SET_BUFF_SIZE,size,NULL) +# define BIO_set_read_buffer_size(b,size) BIO_int_ctrl(b,BIO_C_SET_BUFF_SIZE,size,0) +# define BIO_set_write_buffer_size(b,size) BIO_int_ctrl(b,BIO_C_SET_BUFF_SIZE,size,1) +# define BIO_set_buffer_read_data(b,buf,num) BIO_ctrl(b,BIO_C_SET_BUFF_READ_DATA,num,buf) + +/* Don't use the next one unless you know what you are doing :-) */ +# define BIO_dup_state(b,ret) BIO_ctrl(b,BIO_CTRL_DUP,0,(char *)(ret)) + +# define BIO_reset(b) (int)BIO_ctrl(b,BIO_CTRL_RESET,0,NULL) +# define BIO_eof(b) (int)BIO_ctrl(b,BIO_CTRL_EOF,0,NULL) +# define BIO_set_close(b,c) (int)BIO_ctrl(b,BIO_CTRL_SET_CLOSE,(c),NULL) +# define BIO_get_close(b) (int)BIO_ctrl(b,BIO_CTRL_GET_CLOSE,0,NULL) +# define BIO_pending(b) (int)BIO_ctrl(b,BIO_CTRL_PENDING,0,NULL) +# define BIO_wpending(b) (int)BIO_ctrl(b,BIO_CTRL_WPENDING,0,NULL) +/* ...pending macros have inappropriate return type */ +size_t BIO_ctrl_pending(BIO *b); +size_t BIO_ctrl_wpending(BIO *b); +# define BIO_flush(b) (int)BIO_ctrl(b,BIO_CTRL_FLUSH,0,NULL) +# define BIO_get_info_callback(b,cbp) (int)BIO_ctrl(b,BIO_CTRL_GET_CALLBACK,0, \ + cbp) +# define BIO_set_info_callback(b,cb) (int)BIO_callback_ctrl(b,BIO_CTRL_SET_CALLBACK,cb) + +/* For the BIO_f_buffer() type */ +# define BIO_buffer_get_num_lines(b) BIO_ctrl(b,BIO_CTRL_GET,0,NULL) +# define BIO_buffer_peek(b,s,l) BIO_ctrl(b,BIO_CTRL_PEEK,(l),(s)) + +/* For BIO_s_bio() */ +# define BIO_set_write_buf_size(b,size) (int)BIO_ctrl(b,BIO_C_SET_WRITE_BUF_SIZE,size,NULL) +# define BIO_get_write_buf_size(b,size) (size_t)BIO_ctrl(b,BIO_C_GET_WRITE_BUF_SIZE,size,NULL) +# define BIO_make_bio_pair(b1,b2) (int)BIO_ctrl(b1,BIO_C_MAKE_BIO_PAIR,0,b2) +# define BIO_destroy_bio_pair(b) (int)BIO_ctrl(b,BIO_C_DESTROY_BIO_PAIR,0,NULL) +# define BIO_shutdown_wr(b) (int)BIO_ctrl(b, BIO_C_SHUTDOWN_WR, 0, NULL) +/* macros with inappropriate type -- but ...pending macros use int too: */ +# define BIO_get_write_guarantee(b) (int)BIO_ctrl(b,BIO_C_GET_WRITE_GUARANTEE,0,NULL) +# define BIO_get_read_request(b) (int)BIO_ctrl(b,BIO_C_GET_READ_REQUEST,0,NULL) +size_t BIO_ctrl_get_write_guarantee(BIO *b); +size_t BIO_ctrl_get_read_request(BIO *b); +int BIO_ctrl_reset_read_request(BIO *b); + +/* ctrl macros for dgram */ +# define BIO_ctrl_dgram_connect(b,peer) \ + (int)BIO_ctrl(b,BIO_CTRL_DGRAM_CONNECT,0, (char *)(peer)) +# define BIO_ctrl_set_connected(b,peer) \ + (int)BIO_ctrl(b, BIO_CTRL_DGRAM_SET_CONNECTED, 0, (char *)(peer)) +# define BIO_dgram_recv_timedout(b) \ + (int)BIO_ctrl(b, BIO_CTRL_DGRAM_GET_RECV_TIMER_EXP, 0, NULL) +# define BIO_dgram_send_timedout(b) \ + (int)BIO_ctrl(b, BIO_CTRL_DGRAM_GET_SEND_TIMER_EXP, 0, NULL) +# define BIO_dgram_get_peer(b,peer) \ + (int)BIO_ctrl(b, BIO_CTRL_DGRAM_GET_PEER, 0, (char *)(peer)) +# define BIO_dgram_set_peer(b,peer) \ + (int)BIO_ctrl(b, BIO_CTRL_DGRAM_SET_PEER, 0, (char *)(peer)) +# define BIO_dgram_get_mtu_overhead(b) \ + (unsigned int)BIO_ctrl((b), BIO_CTRL_DGRAM_GET_MTU_OVERHEAD, 0, NULL) + +#define BIO_get_ex_new_index(l, p, newf, dupf, freef) \ + CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_BIO, l, p, newf, dupf, freef) +int BIO_set_ex_data(BIO *bio, int idx, void *data); +void *BIO_get_ex_data(BIO *bio, int idx); +uint64_t BIO_number_read(BIO *bio); +uint64_t BIO_number_written(BIO *bio); + +/* For BIO_f_asn1() */ +int BIO_asn1_set_prefix(BIO *b, asn1_ps_func *prefix, + asn1_ps_func *prefix_free); +int BIO_asn1_get_prefix(BIO *b, asn1_ps_func **pprefix, + asn1_ps_func **pprefix_free); +int BIO_asn1_set_suffix(BIO *b, asn1_ps_func *suffix, + asn1_ps_func *suffix_free); +int BIO_asn1_get_suffix(BIO *b, asn1_ps_func **psuffix, + asn1_ps_func **psuffix_free); + +const BIO_METHOD *BIO_s_file(void); +BIO *BIO_new_file(const char *filename, const char *mode); +# ifndef OPENSSL_NO_STDIO +BIO *BIO_new_fp(FILE *stream, int close_flag); +# endif +BIO *BIO_new(const BIO_METHOD *type); +int BIO_free(BIO *a); +void BIO_set_data(BIO *a, void *ptr); +void *BIO_get_data(BIO *a); +void BIO_set_init(BIO *a, int init); +int BIO_get_init(BIO *a); +void BIO_set_shutdown(BIO *a, int shut); +int BIO_get_shutdown(BIO *a); +void BIO_vfree(BIO *a); +int BIO_up_ref(BIO *a); +int BIO_read(BIO *b, void *data, int dlen); +int BIO_read_ex(BIO *b, void *data, size_t dlen, size_t *readbytes); +int BIO_gets(BIO *bp, char *buf, int size); +int BIO_write(BIO *b, const void *data, int dlen); +int BIO_write_ex(BIO *b, const void *data, size_t dlen, size_t *written); +int BIO_puts(BIO *bp, const char *buf); +int BIO_indent(BIO *b, int indent, int max); +long BIO_ctrl(BIO *bp, int cmd, long larg, void *parg); +long BIO_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp); +void *BIO_ptr_ctrl(BIO *bp, int cmd, long larg); +long BIO_int_ctrl(BIO *bp, int cmd, long larg, int iarg); +BIO *BIO_push(BIO *b, BIO *append); +BIO *BIO_pop(BIO *b); +void BIO_free_all(BIO *a); +BIO *BIO_find_type(BIO *b, int bio_type); +BIO *BIO_next(BIO *b); +void BIO_set_next(BIO *b, BIO *next); +BIO *BIO_get_retry_BIO(BIO *bio, int *reason); +int BIO_get_retry_reason(BIO *bio); +void BIO_set_retry_reason(BIO *bio, int reason); +BIO *BIO_dup_chain(BIO *in); + +int BIO_nread0(BIO *bio, char **buf); +int BIO_nread(BIO *bio, char **buf, int num); +int BIO_nwrite0(BIO *bio, char **buf); +int BIO_nwrite(BIO *bio, char **buf, int num); + +long BIO_debug_callback(BIO *bio, int cmd, const char *argp, int argi, + long argl, long ret); + +const BIO_METHOD *BIO_s_mem(void); +const BIO_METHOD *BIO_s_secmem(void); +BIO *BIO_new_mem_buf(const void *buf, int len); +# ifndef OPENSSL_NO_SOCK +const BIO_METHOD *BIO_s_socket(void); +const BIO_METHOD *BIO_s_connect(void); +const BIO_METHOD *BIO_s_accept(void); +# endif +const BIO_METHOD *BIO_s_fd(void); +const BIO_METHOD *BIO_s_log(void); +const BIO_METHOD *BIO_s_bio(void); +const BIO_METHOD *BIO_s_null(void); +const BIO_METHOD *BIO_f_null(void); +const BIO_METHOD *BIO_f_buffer(void); +const BIO_METHOD *BIO_f_linebuffer(void); +const BIO_METHOD *BIO_f_nbio_test(void); +# ifndef OPENSSL_NO_DGRAM +const BIO_METHOD *BIO_s_datagram(void); +int BIO_dgram_non_fatal_error(int error); +BIO *BIO_new_dgram(int fd, int close_flag); +# ifndef OPENSSL_NO_SCTP +const BIO_METHOD *BIO_s_datagram_sctp(void); +BIO *BIO_new_dgram_sctp(int fd, int close_flag); +int BIO_dgram_is_sctp(BIO *bio); +int BIO_dgram_sctp_notification_cb(BIO *b, + void (*handle_notifications) (BIO *bio, + void *context, + void *buf), + void *context); +int BIO_dgram_sctp_wait_for_dry(BIO *b); +int BIO_dgram_sctp_msg_waiting(BIO *b); +# endif +# endif + +# ifndef OPENSSL_NO_SOCK +int BIO_sock_should_retry(int i); +int BIO_sock_non_fatal_error(int error); +# endif + +int BIO_fd_should_retry(int i); +int BIO_fd_non_fatal_error(int error); +int BIO_dump_cb(int (*cb) (const void *data, size_t len, void *u), + void *u, const char *s, int len); +int BIO_dump_indent_cb(int (*cb) (const void *data, size_t len, void *u), + void *u, const char *s, int len, int indent); +int BIO_dump(BIO *b, const char *bytes, int len); +int BIO_dump_indent(BIO *b, const char *bytes, int len, int indent); +# ifndef OPENSSL_NO_STDIO +int BIO_dump_fp(FILE *fp, const char *s, int len); +int BIO_dump_indent_fp(FILE *fp, const char *s, int len, int indent); +# endif +int BIO_hex_string(BIO *out, int indent, int width, unsigned char *data, + int datalen); + +# ifndef OPENSSL_NO_SOCK +BIO_ADDR *BIO_ADDR_new(void); +int BIO_ADDR_rawmake(BIO_ADDR *ap, int family, + const void *where, size_t wherelen, unsigned short port); +void BIO_ADDR_free(BIO_ADDR *); +void BIO_ADDR_clear(BIO_ADDR *ap); +int BIO_ADDR_family(const BIO_ADDR *ap); +int BIO_ADDR_rawaddress(const BIO_ADDR *ap, void *p, size_t *l); +unsigned short BIO_ADDR_rawport(const BIO_ADDR *ap); +char *BIO_ADDR_hostname_string(const BIO_ADDR *ap, int numeric); +char *BIO_ADDR_service_string(const BIO_ADDR *ap, int numeric); +char *BIO_ADDR_path_string(const BIO_ADDR *ap); + +const BIO_ADDRINFO *BIO_ADDRINFO_next(const BIO_ADDRINFO *bai); +int BIO_ADDRINFO_family(const BIO_ADDRINFO *bai); +int BIO_ADDRINFO_socktype(const BIO_ADDRINFO *bai); +int BIO_ADDRINFO_protocol(const BIO_ADDRINFO *bai); +const BIO_ADDR *BIO_ADDRINFO_address(const BIO_ADDRINFO *bai); +void BIO_ADDRINFO_free(BIO_ADDRINFO *bai); + +enum BIO_hostserv_priorities { + BIO_PARSE_PRIO_HOST, BIO_PARSE_PRIO_SERV +}; +int BIO_parse_hostserv(const char *hostserv, char **host, char **service, + enum BIO_hostserv_priorities hostserv_prio); +enum BIO_lookup_type { + BIO_LOOKUP_CLIENT, BIO_LOOKUP_SERVER +}; +int BIO_lookup(const char *host, const char *service, + enum BIO_lookup_type lookup_type, + int family, int socktype, BIO_ADDRINFO **res); +int BIO_lookup_ex(const char *host, const char *service, + int lookup_type, int family, int socktype, int protocol, + BIO_ADDRINFO **res); +int BIO_sock_error(int sock); +int BIO_socket_ioctl(int fd, long type, void *arg); +int BIO_socket_nbio(int fd, int mode); +int BIO_sock_init(void); +# if OPENSSL_API_COMPAT < 0x10100000L +# define BIO_sock_cleanup() while(0) continue +# endif +int BIO_set_tcp_ndelay(int sock, int turn_on); + +DEPRECATEDIN_1_1_0(struct hostent *BIO_gethostbyname(const char *name)) +DEPRECATEDIN_1_1_0(int BIO_get_port(const char *str, unsigned short *port_ptr)) +DEPRECATEDIN_1_1_0(int BIO_get_host_ip(const char *str, unsigned char *ip)) +DEPRECATEDIN_1_1_0(int BIO_get_accept_socket(char *host_port, int mode)) +DEPRECATEDIN_1_1_0(int BIO_accept(int sock, char **ip_port)) + +union BIO_sock_info_u { + BIO_ADDR *addr; +}; +enum BIO_sock_info_type { + BIO_SOCK_INFO_ADDRESS +}; +int BIO_sock_info(int sock, + enum BIO_sock_info_type type, union BIO_sock_info_u *info); + +# define BIO_SOCK_REUSEADDR 0x01 +# define BIO_SOCK_V6_ONLY 0x02 +# define BIO_SOCK_KEEPALIVE 0x04 +# define BIO_SOCK_NONBLOCK 0x08 +# define BIO_SOCK_NODELAY 0x10 + +int BIO_socket(int domain, int socktype, int protocol, int options); +int BIO_connect(int sock, const BIO_ADDR *addr, int options); +int BIO_bind(int sock, const BIO_ADDR *addr, int options); +int BIO_listen(int sock, const BIO_ADDR *addr, int options); +int BIO_accept_ex(int accept_sock, BIO_ADDR *addr, int options); +int BIO_closesocket(int sock); + +BIO *BIO_new_socket(int sock, int close_flag); +BIO *BIO_new_connect(const char *host_port); +BIO *BIO_new_accept(const char *host_port); +# endif /* OPENSSL_NO_SOCK*/ + +BIO *BIO_new_fd(int fd, int close_flag); + +int BIO_new_bio_pair(BIO **bio1, size_t writebuf1, + BIO **bio2, size_t writebuf2); +/* + * If successful, returns 1 and in *bio1, *bio2 two BIO pair endpoints. + * Otherwise returns 0 and sets *bio1 and *bio2 to NULL. Size 0 uses default + * value. + */ + +void BIO_copy_next_retry(BIO *b); + +/* + * long BIO_ghbn_ctrl(int cmd,int iarg,char *parg); + */ + +# define ossl_bio__attr__(x) +# if defined(__GNUC__) && defined(__STDC_VERSION__) \ + && !defined(__APPLE__) + /* + * Because we support the 'z' modifier, which made its appearance in C99, + * we can't use __attribute__ with pre C99 dialects. + */ +# if __STDC_VERSION__ >= 199901L +# undef ossl_bio__attr__ +# define ossl_bio__attr__ __attribute__ +# if __GNUC__*10 + __GNUC_MINOR__ >= 44 +# define ossl_bio__printf__ __gnu_printf__ +# else +# define ossl_bio__printf__ __printf__ +# endif +# endif +# endif +int BIO_printf(BIO *bio, const char *format, ...) +ossl_bio__attr__((__format__(ossl_bio__printf__, 2, 3))); +int BIO_vprintf(BIO *bio, const char *format, va_list args) +ossl_bio__attr__((__format__(ossl_bio__printf__, 2, 0))); +int BIO_snprintf(char *buf, size_t n, const char *format, ...) +ossl_bio__attr__((__format__(ossl_bio__printf__, 3, 4))); +int BIO_vsnprintf(char *buf, size_t n, const char *format, va_list args) +ossl_bio__attr__((__format__(ossl_bio__printf__, 3, 0))); +# undef ossl_bio__attr__ +# undef ossl_bio__printf__ + + +BIO_METHOD *BIO_meth_new(int type, const char *name); +void BIO_meth_free(BIO_METHOD *biom); +int (*BIO_meth_get_write(const BIO_METHOD *biom)) (BIO *, const char *, int); +int (*BIO_meth_get_write_ex(const BIO_METHOD *biom)) (BIO *, const char *, size_t, + size_t *); +int BIO_meth_set_write(BIO_METHOD *biom, + int (*write) (BIO *, const char *, int)); +int BIO_meth_set_write_ex(BIO_METHOD *biom, + int (*bwrite) (BIO *, const char *, size_t, size_t *)); +int (*BIO_meth_get_read(const BIO_METHOD *biom)) (BIO *, char *, int); +int (*BIO_meth_get_read_ex(const BIO_METHOD *biom)) (BIO *, char *, size_t, size_t *); +int BIO_meth_set_read(BIO_METHOD *biom, + int (*read) (BIO *, char *, int)); +int BIO_meth_set_read_ex(BIO_METHOD *biom, + int (*bread) (BIO *, char *, size_t, size_t *)); +int (*BIO_meth_get_puts(const BIO_METHOD *biom)) (BIO *, const char *); +int BIO_meth_set_puts(BIO_METHOD *biom, + int (*puts) (BIO *, const char *)); +int (*BIO_meth_get_gets(const BIO_METHOD *biom)) (BIO *, char *, int); +int BIO_meth_set_gets(BIO_METHOD *biom, + int (*gets) (BIO *, char *, int)); +long (*BIO_meth_get_ctrl(const BIO_METHOD *biom)) (BIO *, int, long, void *); +int BIO_meth_set_ctrl(BIO_METHOD *biom, + long (*ctrl) (BIO *, int, long, void *)); +int (*BIO_meth_get_create(const BIO_METHOD *bion)) (BIO *); +int BIO_meth_set_create(BIO_METHOD *biom, int (*create) (BIO *)); +int (*BIO_meth_get_destroy(const BIO_METHOD *biom)) (BIO *); +int BIO_meth_set_destroy(BIO_METHOD *biom, int (*destroy) (BIO *)); +long (*BIO_meth_get_callback_ctrl(const BIO_METHOD *biom)) + (BIO *, int, BIO_info_cb *); +int BIO_meth_set_callback_ctrl(BIO_METHOD *biom, + long (*callback_ctrl) (BIO *, int, + BIO_info_cb *)); + +# ifdef __cplusplus +} +# endif +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/bioerr.h b/example/android/third_party/crypto/arm/include/openssl/bioerr.h new file mode 100644 index 00000000..f119a59c --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/bioerr.h @@ -0,0 +1,120 @@ +/* + * Generated by util/mkerr.pl DO NOT EDIT + * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_BIOERR_H +# define HEADER_BIOERR_H + +# ifdef __cplusplus +extern "C" +# endif +int ERR_load_BIO_strings(void); + +/* + * BIO function codes. + */ +# define BIO_F_ACPT_STATE 100 +# define BIO_F_ADDRINFO_WRAP 148 +# define BIO_F_ADDR_STRINGS 134 +# define BIO_F_BIO_ACCEPT 101 +# define BIO_F_BIO_ACCEPT_EX 137 +# define BIO_F_BIO_ACCEPT_NEW 152 +# define BIO_F_BIO_ADDR_NEW 144 +# define BIO_F_BIO_BIND 147 +# define BIO_F_BIO_CALLBACK_CTRL 131 +# define BIO_F_BIO_CONNECT 138 +# define BIO_F_BIO_CONNECT_NEW 153 +# define BIO_F_BIO_CTRL 103 +# define BIO_F_BIO_GETS 104 +# define BIO_F_BIO_GET_HOST_IP 106 +# define BIO_F_BIO_GET_NEW_INDEX 102 +# define BIO_F_BIO_GET_PORT 107 +# define BIO_F_BIO_LISTEN 139 +# define BIO_F_BIO_LOOKUP 135 +# define BIO_F_BIO_LOOKUP_EX 143 +# define BIO_F_BIO_MAKE_PAIR 121 +# define BIO_F_BIO_METH_NEW 146 +# define BIO_F_BIO_NEW 108 +# define BIO_F_BIO_NEW_DGRAM_SCTP 145 +# define BIO_F_BIO_NEW_FILE 109 +# define BIO_F_BIO_NEW_MEM_BUF 126 +# define BIO_F_BIO_NREAD 123 +# define BIO_F_BIO_NREAD0 124 +# define BIO_F_BIO_NWRITE 125 +# define BIO_F_BIO_NWRITE0 122 +# define BIO_F_BIO_PARSE_HOSTSERV 136 +# define BIO_F_BIO_PUTS 110 +# define BIO_F_BIO_READ 111 +# define BIO_F_BIO_READ_EX 105 +# define BIO_F_BIO_READ_INTERN 120 +# define BIO_F_BIO_SOCKET 140 +# define BIO_F_BIO_SOCKET_NBIO 142 +# define BIO_F_BIO_SOCK_INFO 141 +# define BIO_F_BIO_SOCK_INIT 112 +# define BIO_F_BIO_WRITE 113 +# define BIO_F_BIO_WRITE_EX 119 +# define BIO_F_BIO_WRITE_INTERN 128 +# define BIO_F_BUFFER_CTRL 114 +# define BIO_F_CONN_CTRL 127 +# define BIO_F_CONN_STATE 115 +# define BIO_F_DGRAM_SCTP_NEW 149 +# define BIO_F_DGRAM_SCTP_READ 132 +# define BIO_F_DGRAM_SCTP_WRITE 133 +# define BIO_F_DOAPR_OUTCH 150 +# define BIO_F_FILE_CTRL 116 +# define BIO_F_FILE_READ 130 +# define BIO_F_LINEBUFFER_CTRL 129 +# define BIO_F_LINEBUFFER_NEW 151 +# define BIO_F_MEM_WRITE 117 +# define BIO_F_NBIOF_NEW 154 +# define BIO_F_SLG_WRITE 155 +# define BIO_F_SSL_NEW 118 + +/* + * BIO reason codes. + */ +# define BIO_R_ACCEPT_ERROR 100 +# define BIO_R_ADDRINFO_ADDR_IS_NOT_AF_INET 141 +# define BIO_R_AMBIGUOUS_HOST_OR_SERVICE 129 +# define BIO_R_BAD_FOPEN_MODE 101 +# define BIO_R_BROKEN_PIPE 124 +# define BIO_R_CONNECT_ERROR 103 +# define BIO_R_GETHOSTBYNAME_ADDR_IS_NOT_AF_INET 107 +# define BIO_R_GETSOCKNAME_ERROR 132 +# define BIO_R_GETSOCKNAME_TRUNCATED_ADDRESS 133 +# define BIO_R_GETTING_SOCKTYPE 134 +# define BIO_R_INVALID_ARGUMENT 125 +# define BIO_R_INVALID_SOCKET 135 +# define BIO_R_IN_USE 123 +# define BIO_R_LENGTH_TOO_LONG 102 +# define BIO_R_LISTEN_V6_ONLY 136 +# define BIO_R_LOOKUP_RETURNED_NOTHING 142 +# define BIO_R_MALFORMED_HOST_OR_SERVICE 130 +# define BIO_R_NBIO_CONNECT_ERROR 110 +# define BIO_R_NO_ACCEPT_ADDR_OR_SERVICE_SPECIFIED 143 +# define BIO_R_NO_HOSTNAME_OR_SERVICE_SPECIFIED 144 +# define BIO_R_NO_PORT_DEFINED 113 +# define BIO_R_NO_SUCH_FILE 128 +# define BIO_R_NULL_PARAMETER 115 +# define BIO_R_UNABLE_TO_BIND_SOCKET 117 +# define BIO_R_UNABLE_TO_CREATE_SOCKET 118 +# define BIO_R_UNABLE_TO_KEEPALIVE 137 +# define BIO_R_UNABLE_TO_LISTEN_SOCKET 119 +# define BIO_R_UNABLE_TO_NODELAY 138 +# define BIO_R_UNABLE_TO_REUSEADDR 139 +# define BIO_R_UNAVAILABLE_IP_FAMILY 145 +# define BIO_R_UNINITIALIZED 120 +# define BIO_R_UNKNOWN_INFO_TYPE 140 +# define BIO_R_UNSUPPORTED_IP_FAMILY 146 +# define BIO_R_UNSUPPORTED_METHOD 121 +# define BIO_R_UNSUPPORTED_PROTOCOL_FAMILY 131 +# define BIO_R_WRITE_TO_READ_ONLY_BIO 126 +# define BIO_R_WSASTARTUP 122 + +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/blowfish.h b/example/android/third_party/crypto/arm/include/openssl/blowfish.h new file mode 100644 index 00000000..cd3e460e --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/blowfish.h @@ -0,0 +1,61 @@ +/* + * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_BLOWFISH_H +# define HEADER_BLOWFISH_H + +# include + +# ifndef OPENSSL_NO_BF +# include +# ifdef __cplusplus +extern "C" { +# endif + +# define BF_ENCRYPT 1 +# define BF_DECRYPT 0 + +/*- + * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + * ! BF_LONG has to be at least 32 bits wide. ! + * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + */ +# define BF_LONG unsigned int + +# define BF_ROUNDS 16 +# define BF_BLOCK 8 + +typedef struct bf_key_st { + BF_LONG P[BF_ROUNDS + 2]; + BF_LONG S[4 * 256]; +} BF_KEY; + +void BF_set_key(BF_KEY *key, int len, const unsigned char *data); + +void BF_encrypt(BF_LONG *data, const BF_KEY *key); +void BF_decrypt(BF_LONG *data, const BF_KEY *key); + +void BF_ecb_encrypt(const unsigned char *in, unsigned char *out, + const BF_KEY *key, int enc); +void BF_cbc_encrypt(const unsigned char *in, unsigned char *out, long length, + const BF_KEY *schedule, unsigned char *ivec, int enc); +void BF_cfb64_encrypt(const unsigned char *in, unsigned char *out, + long length, const BF_KEY *schedule, + unsigned char *ivec, int *num, int enc); +void BF_ofb64_encrypt(const unsigned char *in, unsigned char *out, + long length, const BF_KEY *schedule, + unsigned char *ivec, int *num); +const char *BF_options(void); + +# ifdef __cplusplus +} +# endif +# endif + +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/bn.h b/example/android/third_party/crypto/arm/include/openssl/bn.h new file mode 100644 index 00000000..8af05d00 --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/bn.h @@ -0,0 +1,539 @@ +/* + * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. + * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_BN_H +# define HEADER_BN_H + +# include +# ifndef OPENSSL_NO_STDIO +# include +# endif +# include +# include +# include +# include + +#ifdef __cplusplus +extern "C" { +#endif + +/* + * 64-bit processor with LP64 ABI + */ +# ifdef SIXTY_FOUR_BIT_LONG +# define BN_ULONG unsigned long +# define BN_BYTES 8 +# endif + +/* + * 64-bit processor other than LP64 ABI + */ +# ifdef SIXTY_FOUR_BIT +# define BN_ULONG unsigned long long +# define BN_BYTES 8 +# endif + +# ifdef THIRTY_TWO_BIT +# define BN_ULONG unsigned int +# define BN_BYTES 4 +# endif + +# define BN_BITS2 (BN_BYTES * 8) +# define BN_BITS (BN_BITS2 * 2) +# define BN_TBIT ((BN_ULONG)1 << (BN_BITS2 - 1)) + +# define BN_FLG_MALLOCED 0x01 +# define BN_FLG_STATIC_DATA 0x02 + +/* + * avoid leaking exponent information through timing, + * BN_mod_exp_mont() will call BN_mod_exp_mont_consttime, + * BN_div() will call BN_div_no_branch, + * BN_mod_inverse() will call BN_mod_inverse_no_branch. + */ +# define BN_FLG_CONSTTIME 0x04 +# define BN_FLG_SECURE 0x08 + +# if OPENSSL_API_COMPAT < 0x00908000L +/* deprecated name for the flag */ +# define BN_FLG_EXP_CONSTTIME BN_FLG_CONSTTIME +# define BN_FLG_FREE 0x8000 /* used for debugging */ +# endif + +void BN_set_flags(BIGNUM *b, int n); +int BN_get_flags(const BIGNUM *b, int n); + +/* Values for |top| in BN_rand() */ +#define BN_RAND_TOP_ANY -1 +#define BN_RAND_TOP_ONE 0 +#define BN_RAND_TOP_TWO 1 + +/* Values for |bottom| in BN_rand() */ +#define BN_RAND_BOTTOM_ANY 0 +#define BN_RAND_BOTTOM_ODD 1 + +/* + * get a clone of a BIGNUM with changed flags, for *temporary* use only (the + * two BIGNUMs cannot be used in parallel!). Also only for *read only* use. The + * value |dest| should be a newly allocated BIGNUM obtained via BN_new() that + * has not been otherwise initialised or used. + */ +void BN_with_flags(BIGNUM *dest, const BIGNUM *b, int flags); + +/* Wrapper function to make using BN_GENCB easier */ +int BN_GENCB_call(BN_GENCB *cb, int a, int b); + +BN_GENCB *BN_GENCB_new(void); +void BN_GENCB_free(BN_GENCB *cb); + +/* Populate a BN_GENCB structure with an "old"-style callback */ +void BN_GENCB_set_old(BN_GENCB *gencb, void (*callback) (int, int, void *), + void *cb_arg); + +/* Populate a BN_GENCB structure with a "new"-style callback */ +void BN_GENCB_set(BN_GENCB *gencb, int (*callback) (int, int, BN_GENCB *), + void *cb_arg); + +void *BN_GENCB_get_arg(BN_GENCB *cb); + +# define BN_prime_checks 0 /* default: select number of iterations based + * on the size of the number */ + +/* + * BN_prime_checks_for_size() returns the number of Miller-Rabin iterations + * that will be done for checking that a random number is probably prime. The + * error rate for accepting a composite number as prime depends on the size of + * the prime |b|. The error rates used are for calculating an RSA key with 2 primes, + * and so the level is what you would expect for a key of double the size of the + * prime. + * + * This table is generated using the algorithm of FIPS PUB 186-4 + * Digital Signature Standard (DSS), section F.1, page 117. + * (https://dx.doi.org/10.6028/NIST.FIPS.186-4) + * + * The following magma script was used to generate the output: + * securitybits:=125; + * k:=1024; + * for t:=1 to 65 do + * for M:=3 to Floor(2*Sqrt(k-1)-1) do + * S:=0; + * // Sum over m + * for m:=3 to M do + * s:=0; + * // Sum over j + * for j:=2 to m do + * s+:=(RealField(32)!2)^-(j+(k-1)/j); + * end for; + * S+:=2^(m-(m-1)*t)*s; + * end for; + * A:=2^(k-2-M*t); + * B:=8*(Pi(RealField(32))^2-6)/3*2^(k-2)*S; + * pkt:=2.00743*Log(2)*k*2^-k*(A+B); + * seclevel:=Floor(-Log(2,pkt)); + * if seclevel ge securitybits then + * printf "k: %5o, security: %o bits (t: %o, M: %o)\n",k,seclevel,t,M; + * break; + * end if; + * end for; + * if seclevel ge securitybits then break; end if; + * end for; + * + * It can be run online at: + * http://magma.maths.usyd.edu.au/calc + * + * And will output: + * k: 1024, security: 129 bits (t: 6, M: 23) + * + * k is the number of bits of the prime, securitybits is the level we want to + * reach. + * + * prime length | RSA key size | # MR tests | security level + * -------------+--------------|------------+--------------- + * (b) >= 6394 | >= 12788 | 3 | 256 bit + * (b) >= 3747 | >= 7494 | 3 | 192 bit + * (b) >= 1345 | >= 2690 | 4 | 128 bit + * (b) >= 1080 | >= 2160 | 5 | 128 bit + * (b) >= 852 | >= 1704 | 5 | 112 bit + * (b) >= 476 | >= 952 | 5 | 80 bit + * (b) >= 400 | >= 800 | 6 | 80 bit + * (b) >= 347 | >= 694 | 7 | 80 bit + * (b) >= 308 | >= 616 | 8 | 80 bit + * (b) >= 55 | >= 110 | 27 | 64 bit + * (b) >= 6 | >= 12 | 34 | 64 bit + */ + +# define BN_prime_checks_for_size(b) ((b) >= 3747 ? 3 : \ + (b) >= 1345 ? 4 : \ + (b) >= 476 ? 5 : \ + (b) >= 400 ? 6 : \ + (b) >= 347 ? 7 : \ + (b) >= 308 ? 8 : \ + (b) >= 55 ? 27 : \ + /* b >= 6 */ 34) + +# define BN_num_bytes(a) ((BN_num_bits(a)+7)/8) + +int BN_abs_is_word(const BIGNUM *a, const BN_ULONG w); +int BN_is_zero(const BIGNUM *a); +int BN_is_one(const BIGNUM *a); +int BN_is_word(const BIGNUM *a, const BN_ULONG w); +int BN_is_odd(const BIGNUM *a); + +# define BN_one(a) (BN_set_word((a),1)) + +void BN_zero_ex(BIGNUM *a); + +# if OPENSSL_API_COMPAT >= 0x00908000L +# define BN_zero(a) BN_zero_ex(a) +# else +# define BN_zero(a) (BN_set_word((a),0)) +# endif + +const BIGNUM *BN_value_one(void); +char *BN_options(void); +BN_CTX *BN_CTX_new(void); +BN_CTX *BN_CTX_secure_new(void); +void BN_CTX_free(BN_CTX *c); +void BN_CTX_start(BN_CTX *ctx); +BIGNUM *BN_CTX_get(BN_CTX *ctx); +void BN_CTX_end(BN_CTX *ctx); +int BN_rand(BIGNUM *rnd, int bits, int top, int bottom); +int BN_priv_rand(BIGNUM *rnd, int bits, int top, int bottom); +int BN_rand_range(BIGNUM *rnd, const BIGNUM *range); +int BN_priv_rand_range(BIGNUM *rnd, const BIGNUM *range); +int BN_pseudo_rand(BIGNUM *rnd, int bits, int top, int bottom); +int BN_pseudo_rand_range(BIGNUM *rnd, const BIGNUM *range); +int BN_num_bits(const BIGNUM *a); +int BN_num_bits_word(BN_ULONG l); +int BN_security_bits(int L, int N); +BIGNUM *BN_new(void); +BIGNUM *BN_secure_new(void); +void BN_clear_free(BIGNUM *a); +BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b); +void BN_swap(BIGNUM *a, BIGNUM *b); +BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret); +int BN_bn2bin(const BIGNUM *a, unsigned char *to); +int BN_bn2binpad(const BIGNUM *a, unsigned char *to, int tolen); +BIGNUM *BN_lebin2bn(const unsigned char *s, int len, BIGNUM *ret); +int BN_bn2lebinpad(const BIGNUM *a, unsigned char *to, int tolen); +BIGNUM *BN_mpi2bn(const unsigned char *s, int len, BIGNUM *ret); +int BN_bn2mpi(const BIGNUM *a, unsigned char *to); +int BN_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b); +int BN_usub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b); +int BN_uadd(BIGNUM *r, const BIGNUM *a, const BIGNUM *b); +int BN_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b); +int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx); +int BN_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx); +/** BN_set_negative sets sign of a BIGNUM + * \param b pointer to the BIGNUM object + * \param n 0 if the BIGNUM b should be positive and a value != 0 otherwise + */ +void BN_set_negative(BIGNUM *b, int n); +/** BN_is_negative returns 1 if the BIGNUM is negative + * \param b pointer to the BIGNUM object + * \return 1 if a < 0 and 0 otherwise + */ +int BN_is_negative(const BIGNUM *b); + +int BN_div(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m, const BIGNUM *d, + BN_CTX *ctx); +# define BN_mod(rem,m,d,ctx) BN_div(NULL,(rem),(m),(d),(ctx)) +int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx); +int BN_mod_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m, + BN_CTX *ctx); +int BN_mod_add_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, + const BIGNUM *m); +int BN_mod_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m, + BN_CTX *ctx); +int BN_mod_sub_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, + const BIGNUM *m); +int BN_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m, + BN_CTX *ctx); +int BN_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx); +int BN_mod_lshift1(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx); +int BN_mod_lshift1_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *m); +int BN_mod_lshift(BIGNUM *r, const BIGNUM *a, int n, const BIGNUM *m, + BN_CTX *ctx); +int BN_mod_lshift_quick(BIGNUM *r, const BIGNUM *a, int n, const BIGNUM *m); + +BN_ULONG BN_mod_word(const BIGNUM *a, BN_ULONG w); +BN_ULONG BN_div_word(BIGNUM *a, BN_ULONG w); +int BN_mul_word(BIGNUM *a, BN_ULONG w); +int BN_add_word(BIGNUM *a, BN_ULONG w); +int BN_sub_word(BIGNUM *a, BN_ULONG w); +int BN_set_word(BIGNUM *a, BN_ULONG w); +BN_ULONG BN_get_word(const BIGNUM *a); + +int BN_cmp(const BIGNUM *a, const BIGNUM *b); +void BN_free(BIGNUM *a); +int BN_is_bit_set(const BIGNUM *a, int n); +int BN_lshift(BIGNUM *r, const BIGNUM *a, int n); +int BN_lshift1(BIGNUM *r, const BIGNUM *a); +int BN_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx); + +int BN_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, + const BIGNUM *m, BN_CTX *ctx); +int BN_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, + const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx); +int BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p, + const BIGNUM *m, BN_CTX *ctx, + BN_MONT_CTX *in_mont); +int BN_mod_exp_mont_word(BIGNUM *r, BN_ULONG a, const BIGNUM *p, + const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx); +int BN_mod_exp2_mont(BIGNUM *r, const BIGNUM *a1, const BIGNUM *p1, + const BIGNUM *a2, const BIGNUM *p2, const BIGNUM *m, + BN_CTX *ctx, BN_MONT_CTX *m_ctx); +int BN_mod_exp_simple(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, + const BIGNUM *m, BN_CTX *ctx); + +int BN_mask_bits(BIGNUM *a, int n); +# ifndef OPENSSL_NO_STDIO +int BN_print_fp(FILE *fp, const BIGNUM *a); +# endif +int BN_print(BIO *bio, const BIGNUM *a); +int BN_reciprocal(BIGNUM *r, const BIGNUM *m, int len, BN_CTX *ctx); +int BN_rshift(BIGNUM *r, const BIGNUM *a, int n); +int BN_rshift1(BIGNUM *r, const BIGNUM *a); +void BN_clear(BIGNUM *a); +BIGNUM *BN_dup(const BIGNUM *a); +int BN_ucmp(const BIGNUM *a, const BIGNUM *b); +int BN_set_bit(BIGNUM *a, int n); +int BN_clear_bit(BIGNUM *a, int n); +char *BN_bn2hex(const BIGNUM *a); +char *BN_bn2dec(const BIGNUM *a); +int BN_hex2bn(BIGNUM **a, const char *str); +int BN_dec2bn(BIGNUM **a, const char *str); +int BN_asc2bn(BIGNUM **a, const char *str); +int BN_gcd(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx); +int BN_kronecker(const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx); /* returns + * -2 for + * error */ +BIGNUM *BN_mod_inverse(BIGNUM *ret, + const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx); +BIGNUM *BN_mod_sqrt(BIGNUM *ret, + const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx); + +void BN_consttime_swap(BN_ULONG swap, BIGNUM *a, BIGNUM *b, int nwords); + +/* Deprecated versions */ +DEPRECATEDIN_0_9_8(BIGNUM *BN_generate_prime(BIGNUM *ret, int bits, int safe, + const BIGNUM *add, + const BIGNUM *rem, + void (*callback) (int, int, + void *), + void *cb_arg)) +DEPRECATEDIN_0_9_8(int + BN_is_prime(const BIGNUM *p, int nchecks, + void (*callback) (int, int, void *), + BN_CTX *ctx, void *cb_arg)) +DEPRECATEDIN_0_9_8(int + BN_is_prime_fasttest(const BIGNUM *p, int nchecks, + void (*callback) (int, int, void *), + BN_CTX *ctx, void *cb_arg, + int do_trial_division)) + +/* Newer versions */ +int BN_generate_prime_ex(BIGNUM *ret, int bits, int safe, const BIGNUM *add, + const BIGNUM *rem, BN_GENCB *cb); +int BN_is_prime_ex(const BIGNUM *p, int nchecks, BN_CTX *ctx, BN_GENCB *cb); +int BN_is_prime_fasttest_ex(const BIGNUM *p, int nchecks, BN_CTX *ctx, + int do_trial_division, BN_GENCB *cb); + +int BN_X931_generate_Xpq(BIGNUM *Xp, BIGNUM *Xq, int nbits, BN_CTX *ctx); + +int BN_X931_derive_prime_ex(BIGNUM *p, BIGNUM *p1, BIGNUM *p2, + const BIGNUM *Xp, const BIGNUM *Xp1, + const BIGNUM *Xp2, const BIGNUM *e, BN_CTX *ctx, + BN_GENCB *cb); +int BN_X931_generate_prime_ex(BIGNUM *p, BIGNUM *p1, BIGNUM *p2, BIGNUM *Xp1, + BIGNUM *Xp2, const BIGNUM *Xp, const BIGNUM *e, + BN_CTX *ctx, BN_GENCB *cb); + +BN_MONT_CTX *BN_MONT_CTX_new(void); +int BN_mod_mul_montgomery(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, + BN_MONT_CTX *mont, BN_CTX *ctx); +int BN_to_montgomery(BIGNUM *r, const BIGNUM *a, BN_MONT_CTX *mont, + BN_CTX *ctx); +int BN_from_montgomery(BIGNUM *r, const BIGNUM *a, BN_MONT_CTX *mont, + BN_CTX *ctx); +void BN_MONT_CTX_free(BN_MONT_CTX *mont); +int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx); +BN_MONT_CTX *BN_MONT_CTX_copy(BN_MONT_CTX *to, BN_MONT_CTX *from); +BN_MONT_CTX *BN_MONT_CTX_set_locked(BN_MONT_CTX **pmont, CRYPTO_RWLOCK *lock, + const BIGNUM *mod, BN_CTX *ctx); + +/* BN_BLINDING flags */ +# define BN_BLINDING_NO_UPDATE 0x00000001 +# define BN_BLINDING_NO_RECREATE 0x00000002 + +BN_BLINDING *BN_BLINDING_new(const BIGNUM *A, const BIGNUM *Ai, BIGNUM *mod); +void BN_BLINDING_free(BN_BLINDING *b); +int BN_BLINDING_update(BN_BLINDING *b, BN_CTX *ctx); +int BN_BLINDING_convert(BIGNUM *n, BN_BLINDING *b, BN_CTX *ctx); +int BN_BLINDING_invert(BIGNUM *n, BN_BLINDING *b, BN_CTX *ctx); +int BN_BLINDING_convert_ex(BIGNUM *n, BIGNUM *r, BN_BLINDING *b, BN_CTX *); +int BN_BLINDING_invert_ex(BIGNUM *n, const BIGNUM *r, BN_BLINDING *b, + BN_CTX *); + +int BN_BLINDING_is_current_thread(BN_BLINDING *b); +void BN_BLINDING_set_current_thread(BN_BLINDING *b); +int BN_BLINDING_lock(BN_BLINDING *b); +int BN_BLINDING_unlock(BN_BLINDING *b); + +unsigned long BN_BLINDING_get_flags(const BN_BLINDING *); +void BN_BLINDING_set_flags(BN_BLINDING *, unsigned long); +BN_BLINDING *BN_BLINDING_create_param(BN_BLINDING *b, + const BIGNUM *e, BIGNUM *m, BN_CTX *ctx, + int (*bn_mod_exp) (BIGNUM *r, + const BIGNUM *a, + const BIGNUM *p, + const BIGNUM *m, + BN_CTX *ctx, + BN_MONT_CTX *m_ctx), + BN_MONT_CTX *m_ctx); + +DEPRECATEDIN_0_9_8(void BN_set_params(int mul, int high, int low, int mont)) +DEPRECATEDIN_0_9_8(int BN_get_params(int which)) /* 0, mul, 1 high, 2 low, 3 + * mont */ + +BN_RECP_CTX *BN_RECP_CTX_new(void); +void BN_RECP_CTX_free(BN_RECP_CTX *recp); +int BN_RECP_CTX_set(BN_RECP_CTX *recp, const BIGNUM *rdiv, BN_CTX *ctx); +int BN_mod_mul_reciprocal(BIGNUM *r, const BIGNUM *x, const BIGNUM *y, + BN_RECP_CTX *recp, BN_CTX *ctx); +int BN_mod_exp_recp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, + const BIGNUM *m, BN_CTX *ctx); +int BN_div_recp(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m, + BN_RECP_CTX *recp, BN_CTX *ctx); + +# ifndef OPENSSL_NO_EC2M + +/* + * Functions for arithmetic over binary polynomials represented by BIGNUMs. + * The BIGNUM::neg property of BIGNUMs representing binary polynomials is + * ignored. Note that input arguments are not const so that their bit arrays + * can be expanded to the appropriate size if needed. + */ + +/* + * r = a + b + */ +int BN_GF2m_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b); +# define BN_GF2m_sub(r, a, b) BN_GF2m_add(r, a, b) +/* + * r=a mod p + */ +int BN_GF2m_mod(BIGNUM *r, const BIGNUM *a, const BIGNUM *p); +/* r = (a * b) mod p */ +int BN_GF2m_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, + const BIGNUM *p, BN_CTX *ctx); +/* r = (a * a) mod p */ +int BN_GF2m_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx); +/* r = (1 / b) mod p */ +int BN_GF2m_mod_inv(BIGNUM *r, const BIGNUM *b, const BIGNUM *p, BN_CTX *ctx); +/* r = (a / b) mod p */ +int BN_GF2m_mod_div(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, + const BIGNUM *p, BN_CTX *ctx); +/* r = (a ^ b) mod p */ +int BN_GF2m_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, + const BIGNUM *p, BN_CTX *ctx); +/* r = sqrt(a) mod p */ +int BN_GF2m_mod_sqrt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, + BN_CTX *ctx); +/* r^2 + r = a mod p */ +int BN_GF2m_mod_solve_quad(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, + BN_CTX *ctx); +# define BN_GF2m_cmp(a, b) BN_ucmp((a), (b)) +/*- + * Some functions allow for representation of the irreducible polynomials + * as an unsigned int[], say p. The irreducible f(t) is then of the form: + * t^p[0] + t^p[1] + ... + t^p[k] + * where m = p[0] > p[1] > ... > p[k] = 0. + */ +/* r = a mod p */ +int BN_GF2m_mod_arr(BIGNUM *r, const BIGNUM *a, const int p[]); +/* r = (a * b) mod p */ +int BN_GF2m_mod_mul_arr(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, + const int p[], BN_CTX *ctx); +/* r = (a * a) mod p */ +int BN_GF2m_mod_sqr_arr(BIGNUM *r, const BIGNUM *a, const int p[], + BN_CTX *ctx); +/* r = (1 / b) mod p */ +int BN_GF2m_mod_inv_arr(BIGNUM *r, const BIGNUM *b, const int p[], + BN_CTX *ctx); +/* r = (a / b) mod p */ +int BN_GF2m_mod_div_arr(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, + const int p[], BN_CTX *ctx); +/* r = (a ^ b) mod p */ +int BN_GF2m_mod_exp_arr(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, + const int p[], BN_CTX *ctx); +/* r = sqrt(a) mod p */ +int BN_GF2m_mod_sqrt_arr(BIGNUM *r, const BIGNUM *a, + const int p[], BN_CTX *ctx); +/* r^2 + r = a mod p */ +int BN_GF2m_mod_solve_quad_arr(BIGNUM *r, const BIGNUM *a, + const int p[], BN_CTX *ctx); +int BN_GF2m_poly2arr(const BIGNUM *a, int p[], int max); +int BN_GF2m_arr2poly(const int p[], BIGNUM *a); + +# endif + +/* + * faster mod functions for the 'NIST primes' 0 <= a < p^2 + */ +int BN_nist_mod_192(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx); +int BN_nist_mod_224(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx); +int BN_nist_mod_256(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx); +int BN_nist_mod_384(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx); +int BN_nist_mod_521(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx); + +const BIGNUM *BN_get0_nist_prime_192(void); +const BIGNUM *BN_get0_nist_prime_224(void); +const BIGNUM *BN_get0_nist_prime_256(void); +const BIGNUM *BN_get0_nist_prime_384(void); +const BIGNUM *BN_get0_nist_prime_521(void); + +int (*BN_nist_mod_func(const BIGNUM *p)) (BIGNUM *r, const BIGNUM *a, + const BIGNUM *field, BN_CTX *ctx); + +int BN_generate_dsa_nonce(BIGNUM *out, const BIGNUM *range, + const BIGNUM *priv, const unsigned char *message, + size_t message_len, BN_CTX *ctx); + +/* Primes from RFC 2409 */ +BIGNUM *BN_get_rfc2409_prime_768(BIGNUM *bn); +BIGNUM *BN_get_rfc2409_prime_1024(BIGNUM *bn); + +/* Primes from RFC 3526 */ +BIGNUM *BN_get_rfc3526_prime_1536(BIGNUM *bn); +BIGNUM *BN_get_rfc3526_prime_2048(BIGNUM *bn); +BIGNUM *BN_get_rfc3526_prime_3072(BIGNUM *bn); +BIGNUM *BN_get_rfc3526_prime_4096(BIGNUM *bn); +BIGNUM *BN_get_rfc3526_prime_6144(BIGNUM *bn); +BIGNUM *BN_get_rfc3526_prime_8192(BIGNUM *bn); + +# if OPENSSL_API_COMPAT < 0x10100000L +# define get_rfc2409_prime_768 BN_get_rfc2409_prime_768 +# define get_rfc2409_prime_1024 BN_get_rfc2409_prime_1024 +# define get_rfc3526_prime_1536 BN_get_rfc3526_prime_1536 +# define get_rfc3526_prime_2048 BN_get_rfc3526_prime_2048 +# define get_rfc3526_prime_3072 BN_get_rfc3526_prime_3072 +# define get_rfc3526_prime_4096 BN_get_rfc3526_prime_4096 +# define get_rfc3526_prime_6144 BN_get_rfc3526_prime_6144 +# define get_rfc3526_prime_8192 BN_get_rfc3526_prime_8192 +# endif + +int BN_bntest_rand(BIGNUM *rnd, int bits, int top, int bottom); + + +# ifdef __cplusplus +} +# endif +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/bnerr.h b/example/android/third_party/crypto/arm/include/openssl/bnerr.h new file mode 100644 index 00000000..8a022cc0 --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/bnerr.h @@ -0,0 +1,96 @@ +/* + * Generated by util/mkerr.pl DO NOT EDIT + * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_BNERR_H +# define HEADER_BNERR_H + +# ifdef __cplusplus +extern "C" +# endif +int ERR_load_BN_strings(void); + +/* + * BN function codes. + */ +# define BN_F_BNRAND 127 +# define BN_F_BNRAND_RANGE 138 +# define BN_F_BN_BLINDING_CONVERT_EX 100 +# define BN_F_BN_BLINDING_CREATE_PARAM 128 +# define BN_F_BN_BLINDING_INVERT_EX 101 +# define BN_F_BN_BLINDING_NEW 102 +# define BN_F_BN_BLINDING_UPDATE 103 +# define BN_F_BN_BN2DEC 104 +# define BN_F_BN_BN2HEX 105 +# define BN_F_BN_COMPUTE_WNAF 142 +# define BN_F_BN_CTX_GET 116 +# define BN_F_BN_CTX_NEW 106 +# define BN_F_BN_CTX_START 129 +# define BN_F_BN_DIV 107 +# define BN_F_BN_DIV_RECP 130 +# define BN_F_BN_EXP 123 +# define BN_F_BN_EXPAND_INTERNAL 120 +# define BN_F_BN_GENCB_NEW 143 +# define BN_F_BN_GENERATE_DSA_NONCE 140 +# define BN_F_BN_GENERATE_PRIME_EX 141 +# define BN_F_BN_GF2M_MOD 131 +# define BN_F_BN_GF2M_MOD_EXP 132 +# define BN_F_BN_GF2M_MOD_MUL 133 +# define BN_F_BN_GF2M_MOD_SOLVE_QUAD 134 +# define BN_F_BN_GF2M_MOD_SOLVE_QUAD_ARR 135 +# define BN_F_BN_GF2M_MOD_SQR 136 +# define BN_F_BN_GF2M_MOD_SQRT 137 +# define BN_F_BN_LSHIFT 145 +# define BN_F_BN_MOD_EXP2_MONT 118 +# define BN_F_BN_MOD_EXP_MONT 109 +# define BN_F_BN_MOD_EXP_MONT_CONSTTIME 124 +# define BN_F_BN_MOD_EXP_MONT_WORD 117 +# define BN_F_BN_MOD_EXP_RECP 125 +# define BN_F_BN_MOD_EXP_SIMPLE 126 +# define BN_F_BN_MOD_INVERSE 110 +# define BN_F_BN_MOD_INVERSE_NO_BRANCH 139 +# define BN_F_BN_MOD_LSHIFT_QUICK 119 +# define BN_F_BN_MOD_SQRT 121 +# define BN_F_BN_MONT_CTX_NEW 149 +# define BN_F_BN_MPI2BN 112 +# define BN_F_BN_NEW 113 +# define BN_F_BN_POOL_GET 147 +# define BN_F_BN_RAND 114 +# define BN_F_BN_RAND_RANGE 122 +# define BN_F_BN_RECP_CTX_NEW 150 +# define BN_F_BN_RSHIFT 146 +# define BN_F_BN_SET_WORDS 144 +# define BN_F_BN_STACK_PUSH 148 +# define BN_F_BN_USUB 115 + +/* + * BN reason codes. + */ +# define BN_R_ARG2_LT_ARG3 100 +# define BN_R_BAD_RECIPROCAL 101 +# define BN_R_BIGNUM_TOO_LONG 114 +# define BN_R_BITS_TOO_SMALL 118 +# define BN_R_CALLED_WITH_EVEN_MODULUS 102 +# define BN_R_DIV_BY_ZERO 103 +# define BN_R_ENCODING_ERROR 104 +# define BN_R_EXPAND_ON_STATIC_BIGNUM_DATA 105 +# define BN_R_INPUT_NOT_REDUCED 110 +# define BN_R_INVALID_LENGTH 106 +# define BN_R_INVALID_RANGE 115 +# define BN_R_INVALID_SHIFT 119 +# define BN_R_NOT_A_SQUARE 111 +# define BN_R_NOT_INITIALIZED 107 +# define BN_R_NO_INVERSE 108 +# define BN_R_NO_SOLUTION 116 +# define BN_R_PRIVATE_KEY_TOO_LARGE 117 +# define BN_R_P_IS_NOT_PRIME 112 +# define BN_R_TOO_MANY_ITERATIONS 113 +# define BN_R_TOO_MANY_TEMPORARY_VARIABLES 109 + +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/buffer.h b/example/android/third_party/crypto/arm/include/openssl/buffer.h new file mode 100644 index 00000000..d2765766 --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/buffer.h @@ -0,0 +1,58 @@ +/* + * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_BUFFER_H +# define HEADER_BUFFER_H + +# include +# ifndef HEADER_CRYPTO_H +# include +# endif +# include + + +#ifdef __cplusplus +extern "C" { +#endif + +# include +# include + +/* + * These names are outdated as of OpenSSL 1.1; a future release + * will move them to be deprecated. + */ +# define BUF_strdup(s) OPENSSL_strdup(s) +# define BUF_strndup(s, size) OPENSSL_strndup(s, size) +# define BUF_memdup(data, size) OPENSSL_memdup(data, size) +# define BUF_strlcpy(dst, src, size) OPENSSL_strlcpy(dst, src, size) +# define BUF_strlcat(dst, src, size) OPENSSL_strlcat(dst, src, size) +# define BUF_strnlen(str, maxlen) OPENSSL_strnlen(str, maxlen) + +struct buf_mem_st { + size_t length; /* current number of bytes */ + char *data; + size_t max; /* size of buffer */ + unsigned long flags; +}; + +# define BUF_MEM_FLAG_SECURE 0x01 + +BUF_MEM *BUF_MEM_new(void); +BUF_MEM *BUF_MEM_new_ex(unsigned long flags); +void BUF_MEM_free(BUF_MEM *a); +size_t BUF_MEM_grow(BUF_MEM *str, size_t len); +size_t BUF_MEM_grow_clean(BUF_MEM *str, size_t len); +void BUF_reverse(unsigned char *out, const unsigned char *in, size_t siz); + + +# ifdef __cplusplus +} +# endif +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/buffererr.h b/example/android/third_party/crypto/arm/include/openssl/buffererr.h new file mode 100644 index 00000000..3aee1323 --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/buffererr.h @@ -0,0 +1,30 @@ +/* + * Generated by util/mkerr.pl DO NOT EDIT + * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_BUFERR_H +# define HEADER_BUFERR_H + +# ifdef __cplusplus +extern "C" +# endif +int ERR_load_BUF_strings(void); + +/* + * BUF function codes. + */ +# define BUF_F_BUF_MEM_GROW 100 +# define BUF_F_BUF_MEM_GROW_CLEAN 105 +# define BUF_F_BUF_MEM_NEW 101 + +/* + * BUF reason codes. + */ + +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/camellia.h b/example/android/third_party/crypto/arm/include/openssl/camellia.h new file mode 100644 index 00000000..151f3c13 --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/camellia.h @@ -0,0 +1,83 @@ +/* + * Copyright 2006-2016 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_CAMELLIA_H +# define HEADER_CAMELLIA_H + +# include + +# ifndef OPENSSL_NO_CAMELLIA +# include +#ifdef __cplusplus +extern "C" { +#endif + +# define CAMELLIA_ENCRYPT 1 +# define CAMELLIA_DECRYPT 0 + +/* + * Because array size can't be a const in C, the following two are macros. + * Both sizes are in bytes. + */ + +/* This should be a hidden type, but EVP requires that the size be known */ + +# define CAMELLIA_BLOCK_SIZE 16 +# define CAMELLIA_TABLE_BYTE_LEN 272 +# define CAMELLIA_TABLE_WORD_LEN (CAMELLIA_TABLE_BYTE_LEN / 4) + +typedef unsigned int KEY_TABLE_TYPE[CAMELLIA_TABLE_WORD_LEN]; /* to match + * with WORD */ + +struct camellia_key_st { + union { + double d; /* ensures 64-bit align */ + KEY_TABLE_TYPE rd_key; + } u; + int grand_rounds; +}; +typedef struct camellia_key_st CAMELLIA_KEY; + +int Camellia_set_key(const unsigned char *userKey, const int bits, + CAMELLIA_KEY *key); + +void Camellia_encrypt(const unsigned char *in, unsigned char *out, + const CAMELLIA_KEY *key); +void Camellia_decrypt(const unsigned char *in, unsigned char *out, + const CAMELLIA_KEY *key); + +void Camellia_ecb_encrypt(const unsigned char *in, unsigned char *out, + const CAMELLIA_KEY *key, const int enc); +void Camellia_cbc_encrypt(const unsigned char *in, unsigned char *out, + size_t length, const CAMELLIA_KEY *key, + unsigned char *ivec, const int enc); +void Camellia_cfb128_encrypt(const unsigned char *in, unsigned char *out, + size_t length, const CAMELLIA_KEY *key, + unsigned char *ivec, int *num, const int enc); +void Camellia_cfb1_encrypt(const unsigned char *in, unsigned char *out, + size_t length, const CAMELLIA_KEY *key, + unsigned char *ivec, int *num, const int enc); +void Camellia_cfb8_encrypt(const unsigned char *in, unsigned char *out, + size_t length, const CAMELLIA_KEY *key, + unsigned char *ivec, int *num, const int enc); +void Camellia_ofb128_encrypt(const unsigned char *in, unsigned char *out, + size_t length, const CAMELLIA_KEY *key, + unsigned char *ivec, int *num); +void Camellia_ctr128_encrypt(const unsigned char *in, unsigned char *out, + size_t length, const CAMELLIA_KEY *key, + unsigned char ivec[CAMELLIA_BLOCK_SIZE], + unsigned char ecount_buf[CAMELLIA_BLOCK_SIZE], + unsigned int *num); + +# ifdef __cplusplus +} +# endif +# endif + +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/cast.h b/example/android/third_party/crypto/arm/include/openssl/cast.h new file mode 100644 index 00000000..2cc89ae0 --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/cast.h @@ -0,0 +1,53 @@ +/* + * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_CAST_H +# define HEADER_CAST_H + +# include + +# ifndef OPENSSL_NO_CAST +# ifdef __cplusplus +extern "C" { +# endif + +# define CAST_ENCRYPT 1 +# define CAST_DECRYPT 0 + +# define CAST_LONG unsigned int + +# define CAST_BLOCK 8 +# define CAST_KEY_LENGTH 16 + +typedef struct cast_key_st { + CAST_LONG data[32]; + int short_key; /* Use reduced rounds for short key */ +} CAST_KEY; + +void CAST_set_key(CAST_KEY *key, int len, const unsigned char *data); +void CAST_ecb_encrypt(const unsigned char *in, unsigned char *out, + const CAST_KEY *key, int enc); +void CAST_encrypt(CAST_LONG *data, const CAST_KEY *key); +void CAST_decrypt(CAST_LONG *data, const CAST_KEY *key); +void CAST_cbc_encrypt(const unsigned char *in, unsigned char *out, + long length, const CAST_KEY *ks, unsigned char *iv, + int enc); +void CAST_cfb64_encrypt(const unsigned char *in, unsigned char *out, + long length, const CAST_KEY *schedule, + unsigned char *ivec, int *num, int enc); +void CAST_ofb64_encrypt(const unsigned char *in, unsigned char *out, + long length, const CAST_KEY *schedule, + unsigned char *ivec, int *num); + +# ifdef __cplusplus +} +# endif +# endif + +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/cmac.h b/example/android/third_party/crypto/arm/include/openssl/cmac.h new file mode 100644 index 00000000..3535a9ab --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/cmac.h @@ -0,0 +1,41 @@ +/* + * Copyright 2010-2016 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_CMAC_H +# define HEADER_CMAC_H + +# ifndef OPENSSL_NO_CMAC + +#ifdef __cplusplus +extern "C" { +#endif + +# include + +/* Opaque */ +typedef struct CMAC_CTX_st CMAC_CTX; + +CMAC_CTX *CMAC_CTX_new(void); +void CMAC_CTX_cleanup(CMAC_CTX *ctx); +void CMAC_CTX_free(CMAC_CTX *ctx); +EVP_CIPHER_CTX *CMAC_CTX_get0_cipher_ctx(CMAC_CTX *ctx); +int CMAC_CTX_copy(CMAC_CTX *out, const CMAC_CTX *in); + +int CMAC_Init(CMAC_CTX *ctx, const void *key, size_t keylen, + const EVP_CIPHER *cipher, ENGINE *impl); +int CMAC_Update(CMAC_CTX *ctx, const void *data, size_t dlen); +int CMAC_Final(CMAC_CTX *ctx, unsigned char *out, size_t *poutlen); +int CMAC_resume(CMAC_CTX *ctx); + +#ifdef __cplusplus +} +#endif + +# endif +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/cms.h b/example/android/third_party/crypto/arm/include/openssl/cms.h new file mode 100644 index 00000000..ddf37e56 --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/cms.h @@ -0,0 +1,342 @@ +/* + * Copyright 2008-2018 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_CMS_H +# define HEADER_CMS_H + +# include + +# ifndef OPENSSL_NO_CMS +# include +# include +# include +# ifdef __cplusplus +extern "C" { +# endif + +typedef struct CMS_ContentInfo_st CMS_ContentInfo; +typedef struct CMS_SignerInfo_st CMS_SignerInfo; +typedef struct CMS_CertificateChoices CMS_CertificateChoices; +typedef struct CMS_RevocationInfoChoice_st CMS_RevocationInfoChoice; +typedef struct CMS_RecipientInfo_st CMS_RecipientInfo; +typedef struct CMS_ReceiptRequest_st CMS_ReceiptRequest; +typedef struct CMS_Receipt_st CMS_Receipt; +typedef struct CMS_RecipientEncryptedKey_st CMS_RecipientEncryptedKey; +typedef struct CMS_OtherKeyAttribute_st CMS_OtherKeyAttribute; + +DEFINE_STACK_OF(CMS_SignerInfo) +DEFINE_STACK_OF(CMS_RecipientEncryptedKey) +DEFINE_STACK_OF(CMS_RecipientInfo) +DEFINE_STACK_OF(CMS_RevocationInfoChoice) +DECLARE_ASN1_FUNCTIONS(CMS_ContentInfo) +DECLARE_ASN1_FUNCTIONS(CMS_ReceiptRequest) +DECLARE_ASN1_PRINT_FUNCTION(CMS_ContentInfo) + +# define CMS_SIGNERINFO_ISSUER_SERIAL 0 +# define CMS_SIGNERINFO_KEYIDENTIFIER 1 + +# define CMS_RECIPINFO_NONE -1 +# define CMS_RECIPINFO_TRANS 0 +# define CMS_RECIPINFO_AGREE 1 +# define CMS_RECIPINFO_KEK 2 +# define CMS_RECIPINFO_PASS 3 +# define CMS_RECIPINFO_OTHER 4 + +/* S/MIME related flags */ + +# define CMS_TEXT 0x1 +# define CMS_NOCERTS 0x2 +# define CMS_NO_CONTENT_VERIFY 0x4 +# define CMS_NO_ATTR_VERIFY 0x8 +# define CMS_NOSIGS \ + (CMS_NO_CONTENT_VERIFY|CMS_NO_ATTR_VERIFY) +# define CMS_NOINTERN 0x10 +# define CMS_NO_SIGNER_CERT_VERIFY 0x20 +# define CMS_NOVERIFY 0x20 +# define CMS_DETACHED 0x40 +# define CMS_BINARY 0x80 +# define CMS_NOATTR 0x100 +# define CMS_NOSMIMECAP 0x200 +# define CMS_NOOLDMIMETYPE 0x400 +# define CMS_CRLFEOL 0x800 +# define CMS_STREAM 0x1000 +# define CMS_NOCRL 0x2000 +# define CMS_PARTIAL 0x4000 +# define CMS_REUSE_DIGEST 0x8000 +# define CMS_USE_KEYID 0x10000 +# define CMS_DEBUG_DECRYPT 0x20000 +# define CMS_KEY_PARAM 0x40000 +# define CMS_ASCIICRLF 0x80000 + +const ASN1_OBJECT *CMS_get0_type(const CMS_ContentInfo *cms); + +BIO *CMS_dataInit(CMS_ContentInfo *cms, BIO *icont); +int CMS_dataFinal(CMS_ContentInfo *cms, BIO *bio); + +ASN1_OCTET_STRING **CMS_get0_content(CMS_ContentInfo *cms); +int CMS_is_detached(CMS_ContentInfo *cms); +int CMS_set_detached(CMS_ContentInfo *cms, int detached); + +# ifdef HEADER_PEM_H +DECLARE_PEM_rw_const(CMS, CMS_ContentInfo) +# endif +int CMS_stream(unsigned char ***boundary, CMS_ContentInfo *cms); +CMS_ContentInfo *d2i_CMS_bio(BIO *bp, CMS_ContentInfo **cms); +int i2d_CMS_bio(BIO *bp, CMS_ContentInfo *cms); + +BIO *BIO_new_CMS(BIO *out, CMS_ContentInfo *cms); +int i2d_CMS_bio_stream(BIO *out, CMS_ContentInfo *cms, BIO *in, int flags); +int PEM_write_bio_CMS_stream(BIO *out, CMS_ContentInfo *cms, BIO *in, + int flags); +CMS_ContentInfo *SMIME_read_CMS(BIO *bio, BIO **bcont); +int SMIME_write_CMS(BIO *bio, CMS_ContentInfo *cms, BIO *data, int flags); + +int CMS_final(CMS_ContentInfo *cms, BIO *data, BIO *dcont, + unsigned int flags); + +CMS_ContentInfo *CMS_sign(X509 *signcert, EVP_PKEY *pkey, + STACK_OF(X509) *certs, BIO *data, + unsigned int flags); + +CMS_ContentInfo *CMS_sign_receipt(CMS_SignerInfo *si, + X509 *signcert, EVP_PKEY *pkey, + STACK_OF(X509) *certs, unsigned int flags); + +int CMS_data(CMS_ContentInfo *cms, BIO *out, unsigned int flags); +CMS_ContentInfo *CMS_data_create(BIO *in, unsigned int flags); + +int CMS_digest_verify(CMS_ContentInfo *cms, BIO *dcont, BIO *out, + unsigned int flags); +CMS_ContentInfo *CMS_digest_create(BIO *in, const EVP_MD *md, + unsigned int flags); + +int CMS_EncryptedData_decrypt(CMS_ContentInfo *cms, + const unsigned char *key, size_t keylen, + BIO *dcont, BIO *out, unsigned int flags); + +CMS_ContentInfo *CMS_EncryptedData_encrypt(BIO *in, const EVP_CIPHER *cipher, + const unsigned char *key, + size_t keylen, unsigned int flags); + +int CMS_EncryptedData_set1_key(CMS_ContentInfo *cms, const EVP_CIPHER *ciph, + const unsigned char *key, size_t keylen); + +int CMS_verify(CMS_ContentInfo *cms, STACK_OF(X509) *certs, + X509_STORE *store, BIO *dcont, BIO *out, unsigned int flags); + +int CMS_verify_receipt(CMS_ContentInfo *rcms, CMS_ContentInfo *ocms, + STACK_OF(X509) *certs, + X509_STORE *store, unsigned int flags); + +STACK_OF(X509) *CMS_get0_signers(CMS_ContentInfo *cms); + +CMS_ContentInfo *CMS_encrypt(STACK_OF(X509) *certs, BIO *in, + const EVP_CIPHER *cipher, unsigned int flags); + +int CMS_decrypt(CMS_ContentInfo *cms, EVP_PKEY *pkey, X509 *cert, + BIO *dcont, BIO *out, unsigned int flags); + +int CMS_decrypt_set1_pkey(CMS_ContentInfo *cms, EVP_PKEY *pk, X509 *cert); +int CMS_decrypt_set1_key(CMS_ContentInfo *cms, + unsigned char *key, size_t keylen, + const unsigned char *id, size_t idlen); +int CMS_decrypt_set1_password(CMS_ContentInfo *cms, + unsigned char *pass, ossl_ssize_t passlen); + +STACK_OF(CMS_RecipientInfo) *CMS_get0_RecipientInfos(CMS_ContentInfo *cms); +int CMS_RecipientInfo_type(CMS_RecipientInfo *ri); +EVP_PKEY_CTX *CMS_RecipientInfo_get0_pkey_ctx(CMS_RecipientInfo *ri); +CMS_ContentInfo *CMS_EnvelopedData_create(const EVP_CIPHER *cipher); +CMS_RecipientInfo *CMS_add1_recipient_cert(CMS_ContentInfo *cms, + X509 *recip, unsigned int flags); +int CMS_RecipientInfo_set0_pkey(CMS_RecipientInfo *ri, EVP_PKEY *pkey); +int CMS_RecipientInfo_ktri_cert_cmp(CMS_RecipientInfo *ri, X509 *cert); +int CMS_RecipientInfo_ktri_get0_algs(CMS_RecipientInfo *ri, + EVP_PKEY **pk, X509 **recip, + X509_ALGOR **palg); +int CMS_RecipientInfo_ktri_get0_signer_id(CMS_RecipientInfo *ri, + ASN1_OCTET_STRING **keyid, + X509_NAME **issuer, + ASN1_INTEGER **sno); + +CMS_RecipientInfo *CMS_add0_recipient_key(CMS_ContentInfo *cms, int nid, + unsigned char *key, size_t keylen, + unsigned char *id, size_t idlen, + ASN1_GENERALIZEDTIME *date, + ASN1_OBJECT *otherTypeId, + ASN1_TYPE *otherType); + +int CMS_RecipientInfo_kekri_get0_id(CMS_RecipientInfo *ri, + X509_ALGOR **palg, + ASN1_OCTET_STRING **pid, + ASN1_GENERALIZEDTIME **pdate, + ASN1_OBJECT **potherid, + ASN1_TYPE **pothertype); + +int CMS_RecipientInfo_set0_key(CMS_RecipientInfo *ri, + unsigned char *key, size_t keylen); + +int CMS_RecipientInfo_kekri_id_cmp(CMS_RecipientInfo *ri, + const unsigned char *id, size_t idlen); + +int CMS_RecipientInfo_set0_password(CMS_RecipientInfo *ri, + unsigned char *pass, + ossl_ssize_t passlen); + +CMS_RecipientInfo *CMS_add0_recipient_password(CMS_ContentInfo *cms, + int iter, int wrap_nid, + int pbe_nid, + unsigned char *pass, + ossl_ssize_t passlen, + const EVP_CIPHER *kekciph); + +int CMS_RecipientInfo_decrypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri); +int CMS_RecipientInfo_encrypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri); + +int CMS_uncompress(CMS_ContentInfo *cms, BIO *dcont, BIO *out, + unsigned int flags); +CMS_ContentInfo *CMS_compress(BIO *in, int comp_nid, unsigned int flags); + +int CMS_set1_eContentType(CMS_ContentInfo *cms, const ASN1_OBJECT *oid); +const ASN1_OBJECT *CMS_get0_eContentType(CMS_ContentInfo *cms); + +CMS_CertificateChoices *CMS_add0_CertificateChoices(CMS_ContentInfo *cms); +int CMS_add0_cert(CMS_ContentInfo *cms, X509 *cert); +int CMS_add1_cert(CMS_ContentInfo *cms, X509 *cert); +STACK_OF(X509) *CMS_get1_certs(CMS_ContentInfo *cms); + +CMS_RevocationInfoChoice *CMS_add0_RevocationInfoChoice(CMS_ContentInfo *cms); +int CMS_add0_crl(CMS_ContentInfo *cms, X509_CRL *crl); +int CMS_add1_crl(CMS_ContentInfo *cms, X509_CRL *crl); +STACK_OF(X509_CRL) *CMS_get1_crls(CMS_ContentInfo *cms); + +int CMS_SignedData_init(CMS_ContentInfo *cms); +CMS_SignerInfo *CMS_add1_signer(CMS_ContentInfo *cms, + X509 *signer, EVP_PKEY *pk, const EVP_MD *md, + unsigned int flags); +EVP_PKEY_CTX *CMS_SignerInfo_get0_pkey_ctx(CMS_SignerInfo *si); +EVP_MD_CTX *CMS_SignerInfo_get0_md_ctx(CMS_SignerInfo *si); +STACK_OF(CMS_SignerInfo) *CMS_get0_SignerInfos(CMS_ContentInfo *cms); + +void CMS_SignerInfo_set1_signer_cert(CMS_SignerInfo *si, X509 *signer); +int CMS_SignerInfo_get0_signer_id(CMS_SignerInfo *si, + ASN1_OCTET_STRING **keyid, + X509_NAME **issuer, ASN1_INTEGER **sno); +int CMS_SignerInfo_cert_cmp(CMS_SignerInfo *si, X509 *cert); +int CMS_set1_signers_certs(CMS_ContentInfo *cms, STACK_OF(X509) *certs, + unsigned int flags); +void CMS_SignerInfo_get0_algs(CMS_SignerInfo *si, EVP_PKEY **pk, + X509 **signer, X509_ALGOR **pdig, + X509_ALGOR **psig); +ASN1_OCTET_STRING *CMS_SignerInfo_get0_signature(CMS_SignerInfo *si); +int CMS_SignerInfo_sign(CMS_SignerInfo *si); +int CMS_SignerInfo_verify(CMS_SignerInfo *si); +int CMS_SignerInfo_verify_content(CMS_SignerInfo *si, BIO *chain); + +int CMS_add_smimecap(CMS_SignerInfo *si, STACK_OF(X509_ALGOR) *algs); +int CMS_add_simple_smimecap(STACK_OF(X509_ALGOR) **algs, + int algnid, int keysize); +int CMS_add_standard_smimecap(STACK_OF(X509_ALGOR) **smcap); + +int CMS_signed_get_attr_count(const CMS_SignerInfo *si); +int CMS_signed_get_attr_by_NID(const CMS_SignerInfo *si, int nid, + int lastpos); +int CMS_signed_get_attr_by_OBJ(const CMS_SignerInfo *si, const ASN1_OBJECT *obj, + int lastpos); +X509_ATTRIBUTE *CMS_signed_get_attr(const CMS_SignerInfo *si, int loc); +X509_ATTRIBUTE *CMS_signed_delete_attr(CMS_SignerInfo *si, int loc); +int CMS_signed_add1_attr(CMS_SignerInfo *si, X509_ATTRIBUTE *attr); +int CMS_signed_add1_attr_by_OBJ(CMS_SignerInfo *si, + const ASN1_OBJECT *obj, int type, + const void *bytes, int len); +int CMS_signed_add1_attr_by_NID(CMS_SignerInfo *si, + int nid, int type, + const void *bytes, int len); +int CMS_signed_add1_attr_by_txt(CMS_SignerInfo *si, + const char *attrname, int type, + const void *bytes, int len); +void *CMS_signed_get0_data_by_OBJ(CMS_SignerInfo *si, const ASN1_OBJECT *oid, + int lastpos, int type); + +int CMS_unsigned_get_attr_count(const CMS_SignerInfo *si); +int CMS_unsigned_get_attr_by_NID(const CMS_SignerInfo *si, int nid, + int lastpos); +int CMS_unsigned_get_attr_by_OBJ(const CMS_SignerInfo *si, + const ASN1_OBJECT *obj, int lastpos); +X509_ATTRIBUTE *CMS_unsigned_get_attr(const CMS_SignerInfo *si, int loc); +X509_ATTRIBUTE *CMS_unsigned_delete_attr(CMS_SignerInfo *si, int loc); +int CMS_unsigned_add1_attr(CMS_SignerInfo *si, X509_ATTRIBUTE *attr); +int CMS_unsigned_add1_attr_by_OBJ(CMS_SignerInfo *si, + const ASN1_OBJECT *obj, int type, + const void *bytes, int len); +int CMS_unsigned_add1_attr_by_NID(CMS_SignerInfo *si, + int nid, int type, + const void *bytes, int len); +int CMS_unsigned_add1_attr_by_txt(CMS_SignerInfo *si, + const char *attrname, int type, + const void *bytes, int len); +void *CMS_unsigned_get0_data_by_OBJ(CMS_SignerInfo *si, ASN1_OBJECT *oid, + int lastpos, int type); + +# ifdef HEADER_X509V3_H + +int CMS_get1_ReceiptRequest(CMS_SignerInfo *si, CMS_ReceiptRequest **prr); +CMS_ReceiptRequest *CMS_ReceiptRequest_create0(unsigned char *id, int idlen, + int allorfirst, + STACK_OF(GENERAL_NAMES) + *receiptList, STACK_OF(GENERAL_NAMES) + *receiptsTo); +int CMS_add1_ReceiptRequest(CMS_SignerInfo *si, CMS_ReceiptRequest *rr); +void CMS_ReceiptRequest_get0_values(CMS_ReceiptRequest *rr, + ASN1_STRING **pcid, + int *pallorfirst, + STACK_OF(GENERAL_NAMES) **plist, + STACK_OF(GENERAL_NAMES) **prto); +# endif +int CMS_RecipientInfo_kari_get0_alg(CMS_RecipientInfo *ri, + X509_ALGOR **palg, + ASN1_OCTET_STRING **pukm); +STACK_OF(CMS_RecipientEncryptedKey) +*CMS_RecipientInfo_kari_get0_reks(CMS_RecipientInfo *ri); + +int CMS_RecipientInfo_kari_get0_orig_id(CMS_RecipientInfo *ri, + X509_ALGOR **pubalg, + ASN1_BIT_STRING **pubkey, + ASN1_OCTET_STRING **keyid, + X509_NAME **issuer, + ASN1_INTEGER **sno); + +int CMS_RecipientInfo_kari_orig_id_cmp(CMS_RecipientInfo *ri, X509 *cert); + +int CMS_RecipientEncryptedKey_get0_id(CMS_RecipientEncryptedKey *rek, + ASN1_OCTET_STRING **keyid, + ASN1_GENERALIZEDTIME **tm, + CMS_OtherKeyAttribute **other, + X509_NAME **issuer, ASN1_INTEGER **sno); +int CMS_RecipientEncryptedKey_cert_cmp(CMS_RecipientEncryptedKey *rek, + X509 *cert); +int CMS_RecipientInfo_kari_set0_pkey(CMS_RecipientInfo *ri, EVP_PKEY *pk); +EVP_CIPHER_CTX *CMS_RecipientInfo_kari_get0_ctx(CMS_RecipientInfo *ri); +int CMS_RecipientInfo_kari_decrypt(CMS_ContentInfo *cms, + CMS_RecipientInfo *ri, + CMS_RecipientEncryptedKey *rek); + +int CMS_SharedInfo_encode(unsigned char **pder, X509_ALGOR *kekalg, + ASN1_OCTET_STRING *ukm, int keylen); + +/* Backward compatibility for spelling errors. */ +# define CMS_R_UNKNOWN_DIGEST_ALGORITM CMS_R_UNKNOWN_DIGEST_ALGORITHM +# define CMS_R_UNSUPPORTED_RECPIENTINFO_TYPE \ + CMS_R_UNSUPPORTED_RECIPIENTINFO_TYPE + +# ifdef __cplusplus +} +# endif +# endif +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/cmserr.h b/example/android/third_party/crypto/arm/include/openssl/cmserr.h new file mode 100644 index 00000000..3f8ae26d --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/cmserr.h @@ -0,0 +1,196 @@ +/* + * Generated by util/mkerr.pl DO NOT EDIT + * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_CMSERR_H +# define HEADER_CMSERR_H + +# include + +# ifndef OPENSSL_NO_CMS + +# ifdef __cplusplus +extern "C" +# endif +int ERR_load_CMS_strings(void); + +/* + * CMS function codes. + */ +# define CMS_F_CHECK_CONTENT 99 +# define CMS_F_CMS_ADD0_CERT 164 +# define CMS_F_CMS_ADD0_RECIPIENT_KEY 100 +# define CMS_F_CMS_ADD0_RECIPIENT_PASSWORD 165 +# define CMS_F_CMS_ADD1_RECEIPTREQUEST 158 +# define CMS_F_CMS_ADD1_RECIPIENT_CERT 101 +# define CMS_F_CMS_ADD1_SIGNER 102 +# define CMS_F_CMS_ADD1_SIGNINGTIME 103 +# define CMS_F_CMS_COMPRESS 104 +# define CMS_F_CMS_COMPRESSEDDATA_CREATE 105 +# define CMS_F_CMS_COMPRESSEDDATA_INIT_BIO 106 +# define CMS_F_CMS_COPY_CONTENT 107 +# define CMS_F_CMS_COPY_MESSAGEDIGEST 108 +# define CMS_F_CMS_DATA 109 +# define CMS_F_CMS_DATAFINAL 110 +# define CMS_F_CMS_DATAINIT 111 +# define CMS_F_CMS_DECRYPT 112 +# define CMS_F_CMS_DECRYPT_SET1_KEY 113 +# define CMS_F_CMS_DECRYPT_SET1_PASSWORD 166 +# define CMS_F_CMS_DECRYPT_SET1_PKEY 114 +# define CMS_F_CMS_DIGESTALGORITHM_FIND_CTX 115 +# define CMS_F_CMS_DIGESTALGORITHM_INIT_BIO 116 +# define CMS_F_CMS_DIGESTEDDATA_DO_FINAL 117 +# define CMS_F_CMS_DIGEST_VERIFY 118 +# define CMS_F_CMS_ENCODE_RECEIPT 161 +# define CMS_F_CMS_ENCRYPT 119 +# define CMS_F_CMS_ENCRYPTEDCONTENT_INIT 179 +# define CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO 120 +# define CMS_F_CMS_ENCRYPTEDDATA_DECRYPT 121 +# define CMS_F_CMS_ENCRYPTEDDATA_ENCRYPT 122 +# define CMS_F_CMS_ENCRYPTEDDATA_SET1_KEY 123 +# define CMS_F_CMS_ENVELOPEDDATA_CREATE 124 +# define CMS_F_CMS_ENVELOPEDDATA_INIT_BIO 125 +# define CMS_F_CMS_ENVELOPED_DATA_INIT 126 +# define CMS_F_CMS_ENV_ASN1_CTRL 171 +# define CMS_F_CMS_FINAL 127 +# define CMS_F_CMS_GET0_CERTIFICATE_CHOICES 128 +# define CMS_F_CMS_GET0_CONTENT 129 +# define CMS_F_CMS_GET0_ECONTENT_TYPE 130 +# define CMS_F_CMS_GET0_ENVELOPED 131 +# define CMS_F_CMS_GET0_REVOCATION_CHOICES 132 +# define CMS_F_CMS_GET0_SIGNED 133 +# define CMS_F_CMS_MSGSIGDIGEST_ADD1 162 +# define CMS_F_CMS_RECEIPTREQUEST_CREATE0 159 +# define CMS_F_CMS_RECEIPT_VERIFY 160 +# define CMS_F_CMS_RECIPIENTINFO_DECRYPT 134 +# define CMS_F_CMS_RECIPIENTINFO_ENCRYPT 169 +# define CMS_F_CMS_RECIPIENTINFO_KARI_ENCRYPT 178 +# define CMS_F_CMS_RECIPIENTINFO_KARI_GET0_ALG 175 +# define CMS_F_CMS_RECIPIENTINFO_KARI_GET0_ORIG_ID 173 +# define CMS_F_CMS_RECIPIENTINFO_KARI_GET0_REKS 172 +# define CMS_F_CMS_RECIPIENTINFO_KARI_ORIG_ID_CMP 174 +# define CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT 135 +# define CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT 136 +# define CMS_F_CMS_RECIPIENTINFO_KEKRI_GET0_ID 137 +# define CMS_F_CMS_RECIPIENTINFO_KEKRI_ID_CMP 138 +# define CMS_F_CMS_RECIPIENTINFO_KTRI_CERT_CMP 139 +# define CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT 140 +# define CMS_F_CMS_RECIPIENTINFO_KTRI_ENCRYPT 141 +# define CMS_F_CMS_RECIPIENTINFO_KTRI_GET0_ALGS 142 +# define CMS_F_CMS_RECIPIENTINFO_KTRI_GET0_SIGNER_ID 143 +# define CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT 167 +# define CMS_F_CMS_RECIPIENTINFO_SET0_KEY 144 +# define CMS_F_CMS_RECIPIENTINFO_SET0_PASSWORD 168 +# define CMS_F_CMS_RECIPIENTINFO_SET0_PKEY 145 +# define CMS_F_CMS_SD_ASN1_CTRL 170 +# define CMS_F_CMS_SET1_IAS 176 +# define CMS_F_CMS_SET1_KEYID 177 +# define CMS_F_CMS_SET1_SIGNERIDENTIFIER 146 +# define CMS_F_CMS_SET_DETACHED 147 +# define CMS_F_CMS_SIGN 148 +# define CMS_F_CMS_SIGNED_DATA_INIT 149 +# define CMS_F_CMS_SIGNERINFO_CONTENT_SIGN 150 +# define CMS_F_CMS_SIGNERINFO_SIGN 151 +# define CMS_F_CMS_SIGNERINFO_VERIFY 152 +# define CMS_F_CMS_SIGNERINFO_VERIFY_CERT 153 +# define CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT 154 +# define CMS_F_CMS_SIGN_RECEIPT 163 +# define CMS_F_CMS_STREAM 155 +# define CMS_F_CMS_UNCOMPRESS 156 +# define CMS_F_CMS_VERIFY 157 +# define CMS_F_KEK_UNWRAP_KEY 180 + +/* + * CMS reason codes. + */ +# define CMS_R_ADD_SIGNER_ERROR 99 +# define CMS_R_CERTIFICATE_ALREADY_PRESENT 175 +# define CMS_R_CERTIFICATE_HAS_NO_KEYID 160 +# define CMS_R_CERTIFICATE_VERIFY_ERROR 100 +# define CMS_R_CIPHER_INITIALISATION_ERROR 101 +# define CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR 102 +# define CMS_R_CMS_DATAFINAL_ERROR 103 +# define CMS_R_CMS_LIB 104 +# define CMS_R_CONTENTIDENTIFIER_MISMATCH 170 +# define CMS_R_CONTENT_NOT_FOUND 105 +# define CMS_R_CONTENT_TYPE_MISMATCH 171 +# define CMS_R_CONTENT_TYPE_NOT_COMPRESSED_DATA 106 +# define CMS_R_CONTENT_TYPE_NOT_ENVELOPED_DATA 107 +# define CMS_R_CONTENT_TYPE_NOT_SIGNED_DATA 108 +# define CMS_R_CONTENT_VERIFY_ERROR 109 +# define CMS_R_CTRL_ERROR 110 +# define CMS_R_CTRL_FAILURE 111 +# define CMS_R_DECRYPT_ERROR 112 +# define CMS_R_ERROR_GETTING_PUBLIC_KEY 113 +# define CMS_R_ERROR_READING_MESSAGEDIGEST_ATTRIBUTE 114 +# define CMS_R_ERROR_SETTING_KEY 115 +# define CMS_R_ERROR_SETTING_RECIPIENTINFO 116 +# define CMS_R_INVALID_ENCRYPTED_KEY_LENGTH 117 +# define CMS_R_INVALID_KEY_ENCRYPTION_PARAMETER 176 +# define CMS_R_INVALID_KEY_LENGTH 118 +# define CMS_R_MD_BIO_INIT_ERROR 119 +# define CMS_R_MESSAGEDIGEST_ATTRIBUTE_WRONG_LENGTH 120 +# define CMS_R_MESSAGEDIGEST_WRONG_LENGTH 121 +# define CMS_R_MSGSIGDIGEST_ERROR 172 +# define CMS_R_MSGSIGDIGEST_VERIFICATION_FAILURE 162 +# define CMS_R_MSGSIGDIGEST_WRONG_LENGTH 163 +# define CMS_R_NEED_ONE_SIGNER 164 +# define CMS_R_NOT_A_SIGNED_RECEIPT 165 +# define CMS_R_NOT_ENCRYPTED_DATA 122 +# define CMS_R_NOT_KEK 123 +# define CMS_R_NOT_KEY_AGREEMENT 181 +# define CMS_R_NOT_KEY_TRANSPORT 124 +# define CMS_R_NOT_PWRI 177 +# define CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE 125 +# define CMS_R_NO_CIPHER 126 +# define CMS_R_NO_CONTENT 127 +# define CMS_R_NO_CONTENT_TYPE 173 +# define CMS_R_NO_DEFAULT_DIGEST 128 +# define CMS_R_NO_DIGEST_SET 129 +# define CMS_R_NO_KEY 130 +# define CMS_R_NO_KEY_OR_CERT 174 +# define CMS_R_NO_MATCHING_DIGEST 131 +# define CMS_R_NO_MATCHING_RECIPIENT 132 +# define CMS_R_NO_MATCHING_SIGNATURE 166 +# define CMS_R_NO_MSGSIGDIGEST 167 +# define CMS_R_NO_PASSWORD 178 +# define CMS_R_NO_PRIVATE_KEY 133 +# define CMS_R_NO_PUBLIC_KEY 134 +# define CMS_R_NO_RECEIPT_REQUEST 168 +# define CMS_R_NO_SIGNERS 135 +# define CMS_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE 136 +# define CMS_R_RECEIPT_DECODE_ERROR 169 +# define CMS_R_RECIPIENT_ERROR 137 +# define CMS_R_SIGNER_CERTIFICATE_NOT_FOUND 138 +# define CMS_R_SIGNFINAL_ERROR 139 +# define CMS_R_SMIME_TEXT_ERROR 140 +# define CMS_R_STORE_INIT_ERROR 141 +# define CMS_R_TYPE_NOT_COMPRESSED_DATA 142 +# define CMS_R_TYPE_NOT_DATA 143 +# define CMS_R_TYPE_NOT_DIGESTED_DATA 144 +# define CMS_R_TYPE_NOT_ENCRYPTED_DATA 145 +# define CMS_R_TYPE_NOT_ENVELOPED_DATA 146 +# define CMS_R_UNABLE_TO_FINALIZE_CONTEXT 147 +# define CMS_R_UNKNOWN_CIPHER 148 +# define CMS_R_UNKNOWN_DIGEST_ALGORITHM 149 +# define CMS_R_UNKNOWN_ID 150 +# define CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM 151 +# define CMS_R_UNSUPPORTED_CONTENT_TYPE 152 +# define CMS_R_UNSUPPORTED_KEK_ALGORITHM 153 +# define CMS_R_UNSUPPORTED_KEY_ENCRYPTION_ALGORITHM 179 +# define CMS_R_UNSUPPORTED_RECIPIENTINFO_TYPE 155 +# define CMS_R_UNSUPPORTED_RECIPIENT_TYPE 154 +# define CMS_R_UNSUPPORTED_TYPE 156 +# define CMS_R_UNWRAP_ERROR 157 +# define CMS_R_UNWRAP_FAILURE 180 +# define CMS_R_VERIFICATION_FAILURE 158 +# define CMS_R_WRAP_ERROR 159 + +# endif +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/comp.h b/example/android/third_party/crypto/arm/include/openssl/comp.h new file mode 100644 index 00000000..d814d3cf --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/comp.h @@ -0,0 +1,53 @@ +/* + * Copyright 2015-2018 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_COMP_H +# define HEADER_COMP_H + +# include + +# ifndef OPENSSL_NO_COMP +# include +# include +# ifdef __cplusplus +extern "C" { +# endif + + + +COMP_CTX *COMP_CTX_new(COMP_METHOD *meth); +const COMP_METHOD *COMP_CTX_get_method(const COMP_CTX *ctx); +int COMP_CTX_get_type(const COMP_CTX* comp); +int COMP_get_type(const COMP_METHOD *meth); +const char *COMP_get_name(const COMP_METHOD *meth); +void COMP_CTX_free(COMP_CTX *ctx); + +int COMP_compress_block(COMP_CTX *ctx, unsigned char *out, int olen, + unsigned char *in, int ilen); +int COMP_expand_block(COMP_CTX *ctx, unsigned char *out, int olen, + unsigned char *in, int ilen); + +COMP_METHOD *COMP_zlib(void); + +#if OPENSSL_API_COMPAT < 0x10100000L +#define COMP_zlib_cleanup() while(0) continue +#endif + +# ifdef HEADER_BIO_H +# ifdef ZLIB +const BIO_METHOD *BIO_f_zlib(void); +# endif +# endif + + +# ifdef __cplusplus +} +# endif +# endif +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/comperr.h b/example/android/third_party/crypto/arm/include/openssl/comperr.h new file mode 100644 index 00000000..edea63a6 --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/comperr.h @@ -0,0 +1,40 @@ +/* + * Generated by util/mkerr.pl DO NOT EDIT + * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_COMPERR_H +# define HEADER_COMPERR_H + +# include + +# ifndef OPENSSL_NO_COMP + +# ifdef __cplusplus +extern "C" +# endif +int ERR_load_COMP_strings(void); + +/* + * COMP function codes. + */ +# define COMP_F_BIO_ZLIB_FLUSH 99 +# define COMP_F_BIO_ZLIB_NEW 100 +# define COMP_F_BIO_ZLIB_READ 101 +# define COMP_F_BIO_ZLIB_WRITE 102 +# define COMP_F_COMP_CTX_NEW 103 + +/* + * COMP reason codes. + */ +# define COMP_R_ZLIB_DEFLATE_ERROR 99 +# define COMP_R_ZLIB_INFLATE_ERROR 100 +# define COMP_R_ZLIB_NOT_SUPPORTED 101 + +# endif +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/conf.h b/example/android/third_party/crypto/arm/include/openssl/conf.h new file mode 100644 index 00000000..7336cd2f --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/conf.h @@ -0,0 +1,168 @@ +/* + * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_CONF_H +# define HEADER_CONF_H + +# include +# include +# include +# include +# include +# include + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct { + char *section; + char *name; + char *value; +} CONF_VALUE; + +DEFINE_STACK_OF(CONF_VALUE) +DEFINE_LHASH_OF(CONF_VALUE); + +struct conf_st; +struct conf_method_st; +typedef struct conf_method_st CONF_METHOD; + +struct conf_method_st { + const char *name; + CONF *(*create) (CONF_METHOD *meth); + int (*init) (CONF *conf); + int (*destroy) (CONF *conf); + int (*destroy_data) (CONF *conf); + int (*load_bio) (CONF *conf, BIO *bp, long *eline); + int (*dump) (const CONF *conf, BIO *bp); + int (*is_number) (const CONF *conf, char c); + int (*to_int) (const CONF *conf, char c); + int (*load) (CONF *conf, const char *name, long *eline); +}; + +/* Module definitions */ + +typedef struct conf_imodule_st CONF_IMODULE; +typedef struct conf_module_st CONF_MODULE; + +DEFINE_STACK_OF(CONF_MODULE) +DEFINE_STACK_OF(CONF_IMODULE) + +/* DSO module function typedefs */ +typedef int conf_init_func (CONF_IMODULE *md, const CONF *cnf); +typedef void conf_finish_func (CONF_IMODULE *md); + +# define CONF_MFLAGS_IGNORE_ERRORS 0x1 +# define CONF_MFLAGS_IGNORE_RETURN_CODES 0x2 +# define CONF_MFLAGS_SILENT 0x4 +# define CONF_MFLAGS_NO_DSO 0x8 +# define CONF_MFLAGS_IGNORE_MISSING_FILE 0x10 +# define CONF_MFLAGS_DEFAULT_SECTION 0x20 + +int CONF_set_default_method(CONF_METHOD *meth); +void CONF_set_nconf(CONF *conf, LHASH_OF(CONF_VALUE) *hash); +LHASH_OF(CONF_VALUE) *CONF_load(LHASH_OF(CONF_VALUE) *conf, const char *file, + long *eline); +# ifndef OPENSSL_NO_STDIO +LHASH_OF(CONF_VALUE) *CONF_load_fp(LHASH_OF(CONF_VALUE) *conf, FILE *fp, + long *eline); +# endif +LHASH_OF(CONF_VALUE) *CONF_load_bio(LHASH_OF(CONF_VALUE) *conf, BIO *bp, + long *eline); +STACK_OF(CONF_VALUE) *CONF_get_section(LHASH_OF(CONF_VALUE) *conf, + const char *section); +char *CONF_get_string(LHASH_OF(CONF_VALUE) *conf, const char *group, + const char *name); +long CONF_get_number(LHASH_OF(CONF_VALUE) *conf, const char *group, + const char *name); +void CONF_free(LHASH_OF(CONF_VALUE) *conf); +#ifndef OPENSSL_NO_STDIO +int CONF_dump_fp(LHASH_OF(CONF_VALUE) *conf, FILE *out); +#endif +int CONF_dump_bio(LHASH_OF(CONF_VALUE) *conf, BIO *out); + +DEPRECATEDIN_1_1_0(void OPENSSL_config(const char *config_name)) + +#if OPENSSL_API_COMPAT < 0x10100000L +# define OPENSSL_no_config() \ + OPENSSL_init_crypto(OPENSSL_INIT_NO_LOAD_CONFIG, NULL) +#endif + +/* + * New conf code. The semantics are different from the functions above. If + * that wasn't the case, the above functions would have been replaced + */ + +struct conf_st { + CONF_METHOD *meth; + void *meth_data; + LHASH_OF(CONF_VALUE) *data; +}; + +CONF *NCONF_new(CONF_METHOD *meth); +CONF_METHOD *NCONF_default(void); +CONF_METHOD *NCONF_WIN32(void); +void NCONF_free(CONF *conf); +void NCONF_free_data(CONF *conf); + +int NCONF_load(CONF *conf, const char *file, long *eline); +# ifndef OPENSSL_NO_STDIO +int NCONF_load_fp(CONF *conf, FILE *fp, long *eline); +# endif +int NCONF_load_bio(CONF *conf, BIO *bp, long *eline); +STACK_OF(CONF_VALUE) *NCONF_get_section(const CONF *conf, + const char *section); +char *NCONF_get_string(const CONF *conf, const char *group, const char *name); +int NCONF_get_number_e(const CONF *conf, const char *group, const char *name, + long *result); +#ifndef OPENSSL_NO_STDIO +int NCONF_dump_fp(const CONF *conf, FILE *out); +#endif +int NCONF_dump_bio(const CONF *conf, BIO *out); + +#define NCONF_get_number(c,g,n,r) NCONF_get_number_e(c,g,n,r) + +/* Module functions */ + +int CONF_modules_load(const CONF *cnf, const char *appname, + unsigned long flags); +int CONF_modules_load_file(const char *filename, const char *appname, + unsigned long flags); +void CONF_modules_unload(int all); +void CONF_modules_finish(void); +#if OPENSSL_API_COMPAT < 0x10100000L +# define CONF_modules_free() while(0) continue +#endif +int CONF_module_add(const char *name, conf_init_func *ifunc, + conf_finish_func *ffunc); + +const char *CONF_imodule_get_name(const CONF_IMODULE *md); +const char *CONF_imodule_get_value(const CONF_IMODULE *md); +void *CONF_imodule_get_usr_data(const CONF_IMODULE *md); +void CONF_imodule_set_usr_data(CONF_IMODULE *md, void *usr_data); +CONF_MODULE *CONF_imodule_get_module(const CONF_IMODULE *md); +unsigned long CONF_imodule_get_flags(const CONF_IMODULE *md); +void CONF_imodule_set_flags(CONF_IMODULE *md, unsigned long flags); +void *CONF_module_get_usr_data(CONF_MODULE *pmod); +void CONF_module_set_usr_data(CONF_MODULE *pmod, void *usr_data); + +char *CONF_get1_default_config_file(void); + +int CONF_parse_list(const char *list, int sep, int nospc, + int (*list_cb) (const char *elem, int len, void *usr), + void *arg); + +void OPENSSL_load_builtin_modules(void); + + +# ifdef __cplusplus +} +# endif +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/conf_api.h b/example/android/third_party/crypto/arm/include/openssl/conf_api.h new file mode 100644 index 00000000..a0275ad7 --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/conf_api.h @@ -0,0 +1,40 @@ +/* + * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_CONF_API_H +# define HEADER_CONF_API_H + +# include +# include + +#ifdef __cplusplus +extern "C" { +#endif + +/* Up until OpenSSL 0.9.5a, this was new_section */ +CONF_VALUE *_CONF_new_section(CONF *conf, const char *section); +/* Up until OpenSSL 0.9.5a, this was get_section */ +CONF_VALUE *_CONF_get_section(const CONF *conf, const char *section); +/* Up until OpenSSL 0.9.5a, this was CONF_get_section */ +STACK_OF(CONF_VALUE) *_CONF_get_section_values(const CONF *conf, + const char *section); + +int _CONF_add_string(CONF *conf, CONF_VALUE *section, CONF_VALUE *value); +char *_CONF_get_string(const CONF *conf, const char *section, + const char *name); +long _CONF_get_number(const CONF *conf, const char *section, + const char *name); + +int _CONF_new_data(CONF *conf); +void _CONF_free_data(CONF *conf); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/conferr.h b/example/android/third_party/crypto/arm/include/openssl/conferr.h new file mode 100644 index 00000000..d1c92f45 --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/conferr.h @@ -0,0 +1,72 @@ +/* + * Generated by util/mkerr.pl DO NOT EDIT + * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_CONFERR_H +# define HEADER_CONFERR_H + +# ifdef __cplusplus +extern "C" +# endif +int ERR_load_CONF_strings(void); + +/* + * CONF function codes. + */ +# define CONF_F_CONF_DUMP_FP 104 +# define CONF_F_CONF_LOAD 100 +# define CONF_F_CONF_LOAD_FP 103 +# define CONF_F_CONF_PARSE_LIST 119 +# define CONF_F_DEF_LOAD 120 +# define CONF_F_DEF_LOAD_BIO 121 +# define CONF_F_GET_NEXT_FILE 107 +# define CONF_F_MODULE_ADD 122 +# define CONF_F_MODULE_INIT 115 +# define CONF_F_MODULE_LOAD_DSO 117 +# define CONF_F_MODULE_RUN 118 +# define CONF_F_NCONF_DUMP_BIO 105 +# define CONF_F_NCONF_DUMP_FP 106 +# define CONF_F_NCONF_GET_NUMBER_E 112 +# define CONF_F_NCONF_GET_SECTION 108 +# define CONF_F_NCONF_GET_STRING 109 +# define CONF_F_NCONF_LOAD 113 +# define CONF_F_NCONF_LOAD_BIO 110 +# define CONF_F_NCONF_LOAD_FP 114 +# define CONF_F_NCONF_NEW 111 +# define CONF_F_PROCESS_INCLUDE 116 +# define CONF_F_SSL_MODULE_INIT 123 +# define CONF_F_STR_COPY 101 + +/* + * CONF reason codes. + */ +# define CONF_R_ERROR_LOADING_DSO 110 +# define CONF_R_LIST_CANNOT_BE_NULL 115 +# define CONF_R_MISSING_CLOSE_SQUARE_BRACKET 100 +# define CONF_R_MISSING_EQUAL_SIGN 101 +# define CONF_R_MISSING_INIT_FUNCTION 112 +# define CONF_R_MODULE_INITIALIZATION_ERROR 109 +# define CONF_R_NO_CLOSE_BRACE 102 +# define CONF_R_NO_CONF 105 +# define CONF_R_NO_CONF_OR_ENVIRONMENT_VARIABLE 106 +# define CONF_R_NO_SECTION 107 +# define CONF_R_NO_SUCH_FILE 114 +# define CONF_R_NO_VALUE 108 +# define CONF_R_NUMBER_TOO_LARGE 121 +# define CONF_R_RECURSIVE_DIRECTORY_INCLUDE 111 +# define CONF_R_SSL_COMMAND_SECTION_EMPTY 117 +# define CONF_R_SSL_COMMAND_SECTION_NOT_FOUND 118 +# define CONF_R_SSL_SECTION_EMPTY 119 +# define CONF_R_SSL_SECTION_NOT_FOUND 120 +# define CONF_R_UNABLE_TO_CREATE_NEW_SECTION 103 +# define CONF_R_UNKNOWN_MODULE_NAME 113 +# define CONF_R_VARIABLE_EXPANSION_TOO_LONG 116 +# define CONF_R_VARIABLE_HAS_NO_VALUE 104 + +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/crypto.h b/example/android/third_party/crypto/arm/include/openssl/crypto.h new file mode 100644 index 00000000..7e50b1bf --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/crypto.h @@ -0,0 +1,445 @@ +/* + * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. + * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_CRYPTO_H +# define HEADER_CRYPTO_H + +# include +# include + +# include + +# ifndef OPENSSL_NO_STDIO +# include +# endif + +# include +# include +# include +# include +# include + +# ifdef CHARSET_EBCDIC +# include +# endif + +/* + * Resolve problems on some operating systems with symbol names that clash + * one way or another + */ +# include + +# if OPENSSL_API_COMPAT < 0x10100000L +# include +# endif + +#ifdef __cplusplus +extern "C" { +#endif + +# if OPENSSL_API_COMPAT < 0x10100000L +# define SSLeay OpenSSL_version_num +# define SSLeay_version OpenSSL_version +# define SSLEAY_VERSION_NUMBER OPENSSL_VERSION_NUMBER +# define SSLEAY_VERSION OPENSSL_VERSION +# define SSLEAY_CFLAGS OPENSSL_CFLAGS +# define SSLEAY_BUILT_ON OPENSSL_BUILT_ON +# define SSLEAY_PLATFORM OPENSSL_PLATFORM +# define SSLEAY_DIR OPENSSL_DIR + +/* + * Old type for allocating dynamic locks. No longer used. Use the new thread + * API instead. + */ +typedef struct { + int dummy; +} CRYPTO_dynlock; + +# endif /* OPENSSL_API_COMPAT */ + +typedef void CRYPTO_RWLOCK; + +CRYPTO_RWLOCK *CRYPTO_THREAD_lock_new(void); +int CRYPTO_THREAD_read_lock(CRYPTO_RWLOCK *lock); +int CRYPTO_THREAD_write_lock(CRYPTO_RWLOCK *lock); +int CRYPTO_THREAD_unlock(CRYPTO_RWLOCK *lock); +void CRYPTO_THREAD_lock_free(CRYPTO_RWLOCK *lock); + +int CRYPTO_atomic_add(int *val, int amount, int *ret, CRYPTO_RWLOCK *lock); + +/* + * The following can be used to detect memory leaks in the library. If + * used, it turns on malloc checking + */ +# define CRYPTO_MEM_CHECK_OFF 0x0 /* Control only */ +# define CRYPTO_MEM_CHECK_ON 0x1 /* Control and mode bit */ +# define CRYPTO_MEM_CHECK_ENABLE 0x2 /* Control and mode bit */ +# define CRYPTO_MEM_CHECK_DISABLE 0x3 /* Control only */ + +struct crypto_ex_data_st { + STACK_OF(void) *sk; +}; +DEFINE_STACK_OF(void) + +/* + * Per class, we have a STACK of function pointers. + */ +# define CRYPTO_EX_INDEX_SSL 0 +# define CRYPTO_EX_INDEX_SSL_CTX 1 +# define CRYPTO_EX_INDEX_SSL_SESSION 2 +# define CRYPTO_EX_INDEX_X509 3 +# define CRYPTO_EX_INDEX_X509_STORE 4 +# define CRYPTO_EX_INDEX_X509_STORE_CTX 5 +# define CRYPTO_EX_INDEX_DH 6 +# define CRYPTO_EX_INDEX_DSA 7 +# define CRYPTO_EX_INDEX_EC_KEY 8 +# define CRYPTO_EX_INDEX_RSA 9 +# define CRYPTO_EX_INDEX_ENGINE 10 +# define CRYPTO_EX_INDEX_UI 11 +# define CRYPTO_EX_INDEX_BIO 12 +# define CRYPTO_EX_INDEX_APP 13 +# define CRYPTO_EX_INDEX_UI_METHOD 14 +# define CRYPTO_EX_INDEX_DRBG 15 +# define CRYPTO_EX_INDEX__COUNT 16 + +/* + * This is the default callbacks, but we can have others as well: this is + * needed in Win32 where the application malloc and the library malloc may + * not be the same. + */ +#define OPENSSL_malloc_init() \ + CRYPTO_set_mem_functions(CRYPTO_malloc, CRYPTO_realloc, CRYPTO_free) + +int CRYPTO_mem_ctrl(int mode); + +# define OPENSSL_malloc(num) \ + CRYPTO_malloc(num, OPENSSL_FILE, OPENSSL_LINE) +# define OPENSSL_zalloc(num) \ + CRYPTO_zalloc(num, OPENSSL_FILE, OPENSSL_LINE) +# define OPENSSL_realloc(addr, num) \ + CRYPTO_realloc(addr, num, OPENSSL_FILE, OPENSSL_LINE) +# define OPENSSL_clear_realloc(addr, old_num, num) \ + CRYPTO_clear_realloc(addr, old_num, num, OPENSSL_FILE, OPENSSL_LINE) +# define OPENSSL_clear_free(addr, num) \ + CRYPTO_clear_free(addr, num, OPENSSL_FILE, OPENSSL_LINE) +# define OPENSSL_free(addr) \ + CRYPTO_free(addr, OPENSSL_FILE, OPENSSL_LINE) +# define OPENSSL_memdup(str, s) \ + CRYPTO_memdup((str), s, OPENSSL_FILE, OPENSSL_LINE) +# define OPENSSL_strdup(str) \ + CRYPTO_strdup(str, OPENSSL_FILE, OPENSSL_LINE) +# define OPENSSL_strndup(str, n) \ + CRYPTO_strndup(str, n, OPENSSL_FILE, OPENSSL_LINE) +# define OPENSSL_secure_malloc(num) \ + CRYPTO_secure_malloc(num, OPENSSL_FILE, OPENSSL_LINE) +# define OPENSSL_secure_zalloc(num) \ + CRYPTO_secure_zalloc(num, OPENSSL_FILE, OPENSSL_LINE) +# define OPENSSL_secure_free(addr) \ + CRYPTO_secure_free(addr, OPENSSL_FILE, OPENSSL_LINE) +# define OPENSSL_secure_clear_free(addr, num) \ + CRYPTO_secure_clear_free(addr, num, OPENSSL_FILE, OPENSSL_LINE) +# define OPENSSL_secure_actual_size(ptr) \ + CRYPTO_secure_actual_size(ptr) + +size_t OPENSSL_strlcpy(char *dst, const char *src, size_t siz); +size_t OPENSSL_strlcat(char *dst, const char *src, size_t siz); +size_t OPENSSL_strnlen(const char *str, size_t maxlen); +char *OPENSSL_buf2hexstr(const unsigned char *buffer, long len); +unsigned char *OPENSSL_hexstr2buf(const char *str, long *len); +int OPENSSL_hexchar2int(unsigned char c); + +# define OPENSSL_MALLOC_MAX_NELEMS(type) (((1U<<(sizeof(int)*8-1))-1)/sizeof(type)) + +unsigned long OpenSSL_version_num(void); +const char *OpenSSL_version(int type); +# define OPENSSL_VERSION 0 +# define OPENSSL_CFLAGS 1 +# define OPENSSL_BUILT_ON 2 +# define OPENSSL_PLATFORM 3 +# define OPENSSL_DIR 4 +# define OPENSSL_ENGINES_DIR 5 + +int OPENSSL_issetugid(void); + +typedef void CRYPTO_EX_new (void *parent, void *ptr, CRYPTO_EX_DATA *ad, + int idx, long argl, void *argp); +typedef void CRYPTO_EX_free (void *parent, void *ptr, CRYPTO_EX_DATA *ad, + int idx, long argl, void *argp); +typedef int CRYPTO_EX_dup (CRYPTO_EX_DATA *to, const CRYPTO_EX_DATA *from, + void *from_d, int idx, long argl, void *argp); +__owur int CRYPTO_get_ex_new_index(int class_index, long argl, void *argp, + CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func, + CRYPTO_EX_free *free_func); +/* No longer use an index. */ +int CRYPTO_free_ex_index(int class_index, int idx); + +/* + * Initialise/duplicate/free CRYPTO_EX_DATA variables corresponding to a + * given class (invokes whatever per-class callbacks are applicable) + */ +int CRYPTO_new_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad); +int CRYPTO_dup_ex_data(int class_index, CRYPTO_EX_DATA *to, + const CRYPTO_EX_DATA *from); + +void CRYPTO_free_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad); + +/* + * Get/set data in a CRYPTO_EX_DATA variable corresponding to a particular + * index (relative to the class type involved) + */ +int CRYPTO_set_ex_data(CRYPTO_EX_DATA *ad, int idx, void *val); +void *CRYPTO_get_ex_data(const CRYPTO_EX_DATA *ad, int idx); + +# if OPENSSL_API_COMPAT < 0x10100000L +/* + * This function cleans up all "ex_data" state. It mustn't be called under + * potential race-conditions. + */ +# define CRYPTO_cleanup_all_ex_data() while(0) continue + +/* + * The old locking functions have been removed completely without compatibility + * macros. This is because the old functions either could not properly report + * errors, or the returned error values were not clearly documented. + * Replacing the locking functions with no-ops would cause race condition + * issues in the affected applications. It is far better for them to fail at + * compile time. + * On the other hand, the locking callbacks are no longer used. Consequently, + * the callback management functions can be safely replaced with no-op macros. + */ +# define CRYPTO_num_locks() (1) +# define CRYPTO_set_locking_callback(func) +# define CRYPTO_get_locking_callback() (NULL) +# define CRYPTO_set_add_lock_callback(func) +# define CRYPTO_get_add_lock_callback() (NULL) + +/* + * These defines where used in combination with the old locking callbacks, + * they are not called anymore, but old code that's not called might still + * use them. + */ +# define CRYPTO_LOCK 1 +# define CRYPTO_UNLOCK 2 +# define CRYPTO_READ 4 +# define CRYPTO_WRITE 8 + +/* This structure is no longer used */ +typedef struct crypto_threadid_st { + int dummy; +} CRYPTO_THREADID; +/* Only use CRYPTO_THREADID_set_[numeric|pointer]() within callbacks */ +# define CRYPTO_THREADID_set_numeric(id, val) +# define CRYPTO_THREADID_set_pointer(id, ptr) +# define CRYPTO_THREADID_set_callback(threadid_func) (0) +# define CRYPTO_THREADID_get_callback() (NULL) +# define CRYPTO_THREADID_current(id) +# define CRYPTO_THREADID_cmp(a, b) (-1) +# define CRYPTO_THREADID_cpy(dest, src) +# define CRYPTO_THREADID_hash(id) (0UL) + +# if OPENSSL_API_COMPAT < 0x10000000L +# define CRYPTO_set_id_callback(func) +# define CRYPTO_get_id_callback() (NULL) +# define CRYPTO_thread_id() (0UL) +# endif /* OPENSSL_API_COMPAT < 0x10000000L */ + +# define CRYPTO_set_dynlock_create_callback(dyn_create_function) +# define CRYPTO_set_dynlock_lock_callback(dyn_lock_function) +# define CRYPTO_set_dynlock_destroy_callback(dyn_destroy_function) +# define CRYPTO_get_dynlock_create_callback() (NULL) +# define CRYPTO_get_dynlock_lock_callback() (NULL) +# define CRYPTO_get_dynlock_destroy_callback() (NULL) +# endif /* OPENSSL_API_COMPAT < 0x10100000L */ + +int CRYPTO_set_mem_functions( + void *(*m) (size_t, const char *, int), + void *(*r) (void *, size_t, const char *, int), + void (*f) (void *, const char *, int)); +int CRYPTO_set_mem_debug(int flag); +void CRYPTO_get_mem_functions( + void *(**m) (size_t, const char *, int), + void *(**r) (void *, size_t, const char *, int), + void (**f) (void *, const char *, int)); + +void *CRYPTO_malloc(size_t num, const char *file, int line); +void *CRYPTO_zalloc(size_t num, const char *file, int line); +void *CRYPTO_memdup(const void *str, size_t siz, const char *file, int line); +char *CRYPTO_strdup(const char *str, const char *file, int line); +char *CRYPTO_strndup(const char *str, size_t s, const char *file, int line); +void CRYPTO_free(void *ptr, const char *file, int line); +void CRYPTO_clear_free(void *ptr, size_t num, const char *file, int line); +void *CRYPTO_realloc(void *addr, size_t num, const char *file, int line); +void *CRYPTO_clear_realloc(void *addr, size_t old_num, size_t num, + const char *file, int line); + +int CRYPTO_secure_malloc_init(size_t sz, int minsize); +int CRYPTO_secure_malloc_done(void); +void *CRYPTO_secure_malloc(size_t num, const char *file, int line); +void *CRYPTO_secure_zalloc(size_t num, const char *file, int line); +void CRYPTO_secure_free(void *ptr, const char *file, int line); +void CRYPTO_secure_clear_free(void *ptr, size_t num, + const char *file, int line); +int CRYPTO_secure_allocated(const void *ptr); +int CRYPTO_secure_malloc_initialized(void); +size_t CRYPTO_secure_actual_size(void *ptr); +size_t CRYPTO_secure_used(void); + +void OPENSSL_cleanse(void *ptr, size_t len); + +# ifndef OPENSSL_NO_CRYPTO_MDEBUG +# define OPENSSL_mem_debug_push(info) \ + CRYPTO_mem_debug_push(info, OPENSSL_FILE, OPENSSL_LINE) +# define OPENSSL_mem_debug_pop() \ + CRYPTO_mem_debug_pop() +int CRYPTO_mem_debug_push(const char *info, const char *file, int line); +int CRYPTO_mem_debug_pop(void); +void CRYPTO_get_alloc_counts(int *mcount, int *rcount, int *fcount); + +/*- + * Debugging functions (enabled by CRYPTO_set_mem_debug(1)) + * The flag argument has the following significance: + * 0: called before the actual memory allocation has taken place + * 1: called after the actual memory allocation has taken place + */ +void CRYPTO_mem_debug_malloc(void *addr, size_t num, int flag, + const char *file, int line); +void CRYPTO_mem_debug_realloc(void *addr1, void *addr2, size_t num, int flag, + const char *file, int line); +void CRYPTO_mem_debug_free(void *addr, int flag, + const char *file, int line); + +int CRYPTO_mem_leaks_cb(int (*cb) (const char *str, size_t len, void *u), + void *u); +# ifndef OPENSSL_NO_STDIO +int CRYPTO_mem_leaks_fp(FILE *); +# endif +int CRYPTO_mem_leaks(BIO *bio); +# endif + +/* die if we have to */ +ossl_noreturn void OPENSSL_die(const char *assertion, const char *file, int line); +# if OPENSSL_API_COMPAT < 0x10100000L +# define OpenSSLDie(f,l,a) OPENSSL_die((a),(f),(l)) +# endif +# define OPENSSL_assert(e) \ + (void)((e) ? 0 : (OPENSSL_die("assertion failed: " #e, OPENSSL_FILE, OPENSSL_LINE), 1)) + +int OPENSSL_isservice(void); + +int FIPS_mode(void); +int FIPS_mode_set(int r); + +void OPENSSL_init(void); +# ifdef OPENSSL_SYS_UNIX +void OPENSSL_fork_prepare(void); +void OPENSSL_fork_parent(void); +void OPENSSL_fork_child(void); +# endif + +struct tm *OPENSSL_gmtime(const time_t *timer, struct tm *result); +int OPENSSL_gmtime_adj(struct tm *tm, int offset_day, long offset_sec); +int OPENSSL_gmtime_diff(int *pday, int *psec, + const struct tm *from, const struct tm *to); + +/* + * CRYPTO_memcmp returns zero iff the |len| bytes at |a| and |b| are equal. + * It takes an amount of time dependent on |len|, but independent of the + * contents of |a| and |b|. Unlike memcmp, it cannot be used to put elements + * into a defined order as the return value when a != b is undefined, other + * than to be non-zero. + */ +int CRYPTO_memcmp(const void * in_a, const void * in_b, size_t len); + +/* Standard initialisation options */ +# define OPENSSL_INIT_NO_LOAD_CRYPTO_STRINGS 0x00000001L +# define OPENSSL_INIT_LOAD_CRYPTO_STRINGS 0x00000002L +# define OPENSSL_INIT_ADD_ALL_CIPHERS 0x00000004L +# define OPENSSL_INIT_ADD_ALL_DIGESTS 0x00000008L +# define OPENSSL_INIT_NO_ADD_ALL_CIPHERS 0x00000010L +# define OPENSSL_INIT_NO_ADD_ALL_DIGESTS 0x00000020L +# define OPENSSL_INIT_LOAD_CONFIG 0x00000040L +# define OPENSSL_INIT_NO_LOAD_CONFIG 0x00000080L +# define OPENSSL_INIT_ASYNC 0x00000100L +# define OPENSSL_INIT_ENGINE_RDRAND 0x00000200L +# define OPENSSL_INIT_ENGINE_DYNAMIC 0x00000400L +# define OPENSSL_INIT_ENGINE_OPENSSL 0x00000800L +# define OPENSSL_INIT_ENGINE_CRYPTODEV 0x00001000L +# define OPENSSL_INIT_ENGINE_CAPI 0x00002000L +# define OPENSSL_INIT_ENGINE_PADLOCK 0x00004000L +# define OPENSSL_INIT_ENGINE_AFALG 0x00008000L +/* OPENSSL_INIT_ZLIB 0x00010000L */ +# define OPENSSL_INIT_ATFORK 0x00020000L +/* OPENSSL_INIT_BASE_ONLY 0x00040000L */ +/* OPENSSL_INIT flag range 0xfff00000 reserved for OPENSSL_init_ssl() */ +/* Max OPENSSL_INIT flag value is 0x80000000 */ + +/* openssl and dasync not counted as builtin */ +# define OPENSSL_INIT_ENGINE_ALL_BUILTIN \ + (OPENSSL_INIT_ENGINE_RDRAND | OPENSSL_INIT_ENGINE_DYNAMIC \ + | OPENSSL_INIT_ENGINE_CRYPTODEV | OPENSSL_INIT_ENGINE_CAPI | \ + OPENSSL_INIT_ENGINE_PADLOCK) + + +/* Library initialisation functions */ +void OPENSSL_cleanup(void); +int OPENSSL_init_crypto(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings); +int OPENSSL_atexit(void (*handler)(void)); +void OPENSSL_thread_stop(void); + +/* Low-level control of initialization */ +OPENSSL_INIT_SETTINGS *OPENSSL_INIT_new(void); +# ifndef OPENSSL_NO_STDIO +int OPENSSL_INIT_set_config_appname(OPENSSL_INIT_SETTINGS *settings, + const char *config_file); +# endif +void OPENSSL_INIT_free(OPENSSL_INIT_SETTINGS *settings); + +# if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG) +# if defined(_WIN32) +# if defined(BASETYPES) || defined(_WINDEF_H) +/* application has to include in order to use this */ +typedef DWORD CRYPTO_THREAD_LOCAL; +typedef DWORD CRYPTO_THREAD_ID; + +typedef LONG CRYPTO_ONCE; +# define CRYPTO_ONCE_STATIC_INIT 0 +# endif +# else +# include +typedef pthread_once_t CRYPTO_ONCE; +typedef pthread_key_t CRYPTO_THREAD_LOCAL; +typedef pthread_t CRYPTO_THREAD_ID; + +# define CRYPTO_ONCE_STATIC_INIT PTHREAD_ONCE_INIT +# endif +# endif + +# if !defined(CRYPTO_ONCE_STATIC_INIT) +typedef unsigned int CRYPTO_ONCE; +typedef unsigned int CRYPTO_THREAD_LOCAL; +typedef unsigned int CRYPTO_THREAD_ID; +# define CRYPTO_ONCE_STATIC_INIT 0 +# endif + +int CRYPTO_THREAD_run_once(CRYPTO_ONCE *once, void (*init)(void)); + +int CRYPTO_THREAD_init_local(CRYPTO_THREAD_LOCAL *key, void (*cleanup)(void *)); +void *CRYPTO_THREAD_get_local(CRYPTO_THREAD_LOCAL *key); +int CRYPTO_THREAD_set_local(CRYPTO_THREAD_LOCAL *key, void *val); +int CRYPTO_THREAD_cleanup_local(CRYPTO_THREAD_LOCAL *key); + +CRYPTO_THREAD_ID CRYPTO_THREAD_get_current_id(void); +int CRYPTO_THREAD_compare_id(CRYPTO_THREAD_ID a, CRYPTO_THREAD_ID b); + + +# ifdef __cplusplus +} +# endif +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/cryptoerr.h b/example/android/third_party/crypto/arm/include/openssl/cryptoerr.h new file mode 100644 index 00000000..10723d04 --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/cryptoerr.h @@ -0,0 +1,56 @@ +/* + * Generated by util/mkerr.pl DO NOT EDIT + * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_CRYPTOERR_H +# define HEADER_CRYPTOERR_H + +# ifdef __cplusplus +extern "C" +# endif + +# include + +int ERR_load_CRYPTO_strings(void); + +/* + * CRYPTO function codes. + */ +# define CRYPTO_F_CMAC_CTX_NEW 120 +# define CRYPTO_F_CRYPTO_DUP_EX_DATA 110 +# define CRYPTO_F_CRYPTO_FREE_EX_DATA 111 +# define CRYPTO_F_CRYPTO_GET_EX_NEW_INDEX 100 +# define CRYPTO_F_CRYPTO_MEMDUP 115 +# define CRYPTO_F_CRYPTO_NEW_EX_DATA 112 +# define CRYPTO_F_CRYPTO_OCB128_COPY_CTX 121 +# define CRYPTO_F_CRYPTO_OCB128_INIT 122 +# define CRYPTO_F_CRYPTO_SET_EX_DATA 102 +# define CRYPTO_F_FIPS_MODE_SET 109 +# define CRYPTO_F_GET_AND_LOCK 113 +# define CRYPTO_F_OPENSSL_ATEXIT 114 +# define CRYPTO_F_OPENSSL_BUF2HEXSTR 117 +# define CRYPTO_F_OPENSSL_FOPEN 119 +# define CRYPTO_F_OPENSSL_HEXSTR2BUF 118 +# define CRYPTO_F_OPENSSL_INIT_CRYPTO 116 +# define CRYPTO_F_OPENSSL_LH_NEW 126 +# define CRYPTO_F_OPENSSL_SK_DEEP_COPY 127 +# define CRYPTO_F_OPENSSL_SK_DUP 128 +# define CRYPTO_F_PKEY_HMAC_INIT 123 +# define CRYPTO_F_PKEY_POLY1305_INIT 124 +# define CRYPTO_F_PKEY_SIPHASH_INIT 125 +# define CRYPTO_F_SK_RESERVE 129 + +/* + * CRYPTO reason codes. + */ +# define CRYPTO_R_FIPS_MODE_NOT_SUPPORTED 101 +# define CRYPTO_R_ILLEGAL_HEX_DIGIT 102 +# define CRYPTO_R_ODD_NUMBER_OF_DIGITS 103 + +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/ct.h b/example/android/third_party/crypto/arm/include/openssl/ct.h new file mode 100644 index 00000000..d4262fa0 --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/ct.h @@ -0,0 +1,476 @@ +/* + * Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_CT_H +# define HEADER_CT_H + +# include + +# ifndef OPENSSL_NO_CT +# include +# include +# include +# include +# ifdef __cplusplus +extern "C" { +# endif + + +/* Minimum RSA key size, from RFC6962 */ +# define SCT_MIN_RSA_BITS 2048 + +/* All hashes are SHA256 in v1 of Certificate Transparency */ +# define CT_V1_HASHLEN SHA256_DIGEST_LENGTH + +typedef enum { + CT_LOG_ENTRY_TYPE_NOT_SET = -1, + CT_LOG_ENTRY_TYPE_X509 = 0, + CT_LOG_ENTRY_TYPE_PRECERT = 1 +} ct_log_entry_type_t; + +typedef enum { + SCT_VERSION_NOT_SET = -1, + SCT_VERSION_V1 = 0 +} sct_version_t; + +typedef enum { + SCT_SOURCE_UNKNOWN, + SCT_SOURCE_TLS_EXTENSION, + SCT_SOURCE_X509V3_EXTENSION, + SCT_SOURCE_OCSP_STAPLED_RESPONSE +} sct_source_t; + +typedef enum { + SCT_VALIDATION_STATUS_NOT_SET, + SCT_VALIDATION_STATUS_UNKNOWN_LOG, + SCT_VALIDATION_STATUS_VALID, + SCT_VALIDATION_STATUS_INVALID, + SCT_VALIDATION_STATUS_UNVERIFIED, + SCT_VALIDATION_STATUS_UNKNOWN_VERSION +} sct_validation_status_t; + +DEFINE_STACK_OF(SCT) +DEFINE_STACK_OF(CTLOG) + +/****************************************** + * CT policy evaluation context functions * + ******************************************/ + +/* + * Creates a new, empty policy evaluation context. + * The caller is responsible for calling CT_POLICY_EVAL_CTX_free when finished + * with the CT_POLICY_EVAL_CTX. + */ +CT_POLICY_EVAL_CTX *CT_POLICY_EVAL_CTX_new(void); + +/* Deletes a policy evaluation context and anything it owns. */ +void CT_POLICY_EVAL_CTX_free(CT_POLICY_EVAL_CTX *ctx); + +/* Gets the peer certificate that the SCTs are for */ +X509* CT_POLICY_EVAL_CTX_get0_cert(const CT_POLICY_EVAL_CTX *ctx); + +/* + * Sets the certificate associated with the received SCTs. + * Increments the reference count of cert. + * Returns 1 on success, 0 otherwise. + */ +int CT_POLICY_EVAL_CTX_set1_cert(CT_POLICY_EVAL_CTX *ctx, X509 *cert); + +/* Gets the issuer of the aforementioned certificate */ +X509* CT_POLICY_EVAL_CTX_get0_issuer(const CT_POLICY_EVAL_CTX *ctx); + +/* + * Sets the issuer of the certificate associated with the received SCTs. + * Increments the reference count of issuer. + * Returns 1 on success, 0 otherwise. + */ +int CT_POLICY_EVAL_CTX_set1_issuer(CT_POLICY_EVAL_CTX *ctx, X509 *issuer); + +/* Gets the CT logs that are trusted sources of SCTs */ +const CTLOG_STORE *CT_POLICY_EVAL_CTX_get0_log_store(const CT_POLICY_EVAL_CTX *ctx); + +/* Sets the log store that is in use. It must outlive the CT_POLICY_EVAL_CTX. */ +void CT_POLICY_EVAL_CTX_set_shared_CTLOG_STORE(CT_POLICY_EVAL_CTX *ctx, + CTLOG_STORE *log_store); + +/* + * Gets the time, in milliseconds since the Unix epoch, that will be used as the + * current time when checking whether an SCT was issued in the future. + * Such SCTs will fail validation, as required by RFC6962. + */ +uint64_t CT_POLICY_EVAL_CTX_get_time(const CT_POLICY_EVAL_CTX *ctx); + +/* + * Sets the time to evaluate SCTs against, in milliseconds since the Unix epoch. + * If an SCT's timestamp is after this time, it will be interpreted as having + * been issued in the future. RFC6962 states that "TLS clients MUST reject SCTs + * whose timestamp is in the future", so an SCT will not validate in this case. + */ +void CT_POLICY_EVAL_CTX_set_time(CT_POLICY_EVAL_CTX *ctx, uint64_t time_in_ms); + +/***************** + * SCT functions * + *****************/ + +/* + * Creates a new, blank SCT. + * The caller is responsible for calling SCT_free when finished with the SCT. + */ +SCT *SCT_new(void); + +/* + * Creates a new SCT from some base64-encoded strings. + * The caller is responsible for calling SCT_free when finished with the SCT. + */ +SCT *SCT_new_from_base64(unsigned char version, + const char *logid_base64, + ct_log_entry_type_t entry_type, + uint64_t timestamp, + const char *extensions_base64, + const char *signature_base64); + +/* + * Frees the SCT and the underlying data structures. + */ +void SCT_free(SCT *sct); + +/* + * Free a stack of SCTs, and the underlying SCTs themselves. + * Intended to be compatible with X509V3_EXT_FREE. + */ +void SCT_LIST_free(STACK_OF(SCT) *a); + +/* + * Returns the version of the SCT. + */ +sct_version_t SCT_get_version(const SCT *sct); + +/* + * Set the version of an SCT. + * Returns 1 on success, 0 if the version is unrecognized. + */ +__owur int SCT_set_version(SCT *sct, sct_version_t version); + +/* + * Returns the log entry type of the SCT. + */ +ct_log_entry_type_t SCT_get_log_entry_type(const SCT *sct); + +/* + * Set the log entry type of an SCT. + * Returns 1 on success, 0 otherwise. + */ +__owur int SCT_set_log_entry_type(SCT *sct, ct_log_entry_type_t entry_type); + +/* + * Gets the ID of the log that an SCT came from. + * Ownership of the log ID remains with the SCT. + * Returns the length of the log ID. + */ +size_t SCT_get0_log_id(const SCT *sct, unsigned char **log_id); + +/* + * Set the log ID of an SCT to point directly to the *log_id specified. + * The SCT takes ownership of the specified pointer. + * Returns 1 on success, 0 otherwise. + */ +__owur int SCT_set0_log_id(SCT *sct, unsigned char *log_id, size_t log_id_len); + +/* + * Set the log ID of an SCT. + * This makes a copy of the log_id. + * Returns 1 on success, 0 otherwise. + */ +__owur int SCT_set1_log_id(SCT *sct, const unsigned char *log_id, + size_t log_id_len); + +/* + * Returns the timestamp for the SCT (epoch time in milliseconds). + */ +uint64_t SCT_get_timestamp(const SCT *sct); + +/* + * Set the timestamp of an SCT (epoch time in milliseconds). + */ +void SCT_set_timestamp(SCT *sct, uint64_t timestamp); + +/* + * Return the NID for the signature used by the SCT. + * For CT v1, this will be either NID_sha256WithRSAEncryption or + * NID_ecdsa_with_SHA256 (or NID_undef if incorrect/unset). + */ +int SCT_get_signature_nid(const SCT *sct); + +/* + * Set the signature type of an SCT + * For CT v1, this should be either NID_sha256WithRSAEncryption or + * NID_ecdsa_with_SHA256. + * Returns 1 on success, 0 otherwise. + */ +__owur int SCT_set_signature_nid(SCT *sct, int nid); + +/* + * Set *ext to point to the extension data for the SCT. ext must not be NULL. + * The SCT retains ownership of this pointer. + * Returns length of the data pointed to. + */ +size_t SCT_get0_extensions(const SCT *sct, unsigned char **ext); + +/* + * Set the extensions of an SCT to point directly to the *ext specified. + * The SCT takes ownership of the specified pointer. + */ +void SCT_set0_extensions(SCT *sct, unsigned char *ext, size_t ext_len); + +/* + * Set the extensions of an SCT. + * This takes a copy of the ext. + * Returns 1 on success, 0 otherwise. + */ +__owur int SCT_set1_extensions(SCT *sct, const unsigned char *ext, + size_t ext_len); + +/* + * Set *sig to point to the signature for the SCT. sig must not be NULL. + * The SCT retains ownership of this pointer. + * Returns length of the data pointed to. + */ +size_t SCT_get0_signature(const SCT *sct, unsigned char **sig); + +/* + * Set the signature of an SCT to point directly to the *sig specified. + * The SCT takes ownership of the specified pointer. + */ +void SCT_set0_signature(SCT *sct, unsigned char *sig, size_t sig_len); + +/* + * Set the signature of an SCT to be a copy of the *sig specified. + * Returns 1 on success, 0 otherwise. + */ +__owur int SCT_set1_signature(SCT *sct, const unsigned char *sig, + size_t sig_len); + +/* + * The origin of this SCT, e.g. TLS extension, OCSP response, etc. + */ +sct_source_t SCT_get_source(const SCT *sct); + +/* + * Set the origin of this SCT, e.g. TLS extension, OCSP response, etc. + * Returns 1 on success, 0 otherwise. + */ +__owur int SCT_set_source(SCT *sct, sct_source_t source); + +/* + * Returns a text string describing the validation status of |sct|. + */ +const char *SCT_validation_status_string(const SCT *sct); + +/* + * Pretty-prints an |sct| to |out|. + * It will be indented by the number of spaces specified by |indent|. + * If |logs| is not NULL, it will be used to lookup the CT log that the SCT came + * from, so that the log name can be printed. + */ +void SCT_print(const SCT *sct, BIO *out, int indent, const CTLOG_STORE *logs); + +/* + * Pretty-prints an |sct_list| to |out|. + * It will be indented by the number of spaces specified by |indent|. + * SCTs will be delimited by |separator|. + * If |logs| is not NULL, it will be used to lookup the CT log that each SCT + * came from, so that the log names can be printed. + */ +void SCT_LIST_print(const STACK_OF(SCT) *sct_list, BIO *out, int indent, + const char *separator, const CTLOG_STORE *logs); + +/* + * Gets the last result of validating this SCT. + * If it has not been validated yet, returns SCT_VALIDATION_STATUS_NOT_SET. + */ +sct_validation_status_t SCT_get_validation_status(const SCT *sct); + +/* + * Validates the given SCT with the provided context. + * Sets the "validation_status" field of the SCT. + * Returns 1 if the SCT is valid and the signature verifies. + * Returns 0 if the SCT is invalid or could not be verified. + * Returns -1 if an error occurs. + */ +__owur int SCT_validate(SCT *sct, const CT_POLICY_EVAL_CTX *ctx); + +/* + * Validates the given list of SCTs with the provided context. + * Sets the "validation_status" field of each SCT. + * Returns 1 if there are no invalid SCTs and all signatures verify. + * Returns 0 if at least one SCT is invalid or could not be verified. + * Returns a negative integer if an error occurs. + */ +__owur int SCT_LIST_validate(const STACK_OF(SCT) *scts, + CT_POLICY_EVAL_CTX *ctx); + + +/********************************* + * SCT parsing and serialisation * + *********************************/ + +/* + * Serialize (to TLS format) a stack of SCTs and return the length. + * "a" must not be NULL. + * If "pp" is NULL, just return the length of what would have been serialized. + * If "pp" is not NULL and "*pp" is null, function will allocate a new pointer + * for data that caller is responsible for freeing (only if function returns + * successfully). + * If "pp" is NULL and "*pp" is not NULL, caller is responsible for ensuring + * that "*pp" is large enough to accept all of the serialized data. + * Returns < 0 on error, >= 0 indicating bytes written (or would have been) + * on success. + */ +__owur int i2o_SCT_LIST(const STACK_OF(SCT) *a, unsigned char **pp); + +/* + * Convert TLS format SCT list to a stack of SCTs. + * If "a" or "*a" is NULL, a new stack will be created that the caller is + * responsible for freeing (by calling SCT_LIST_free). + * "**pp" and "*pp" must not be NULL. + * Upon success, "*pp" will point to after the last bytes read, and a stack + * will be returned. + * Upon failure, a NULL pointer will be returned, and the position of "*pp" is + * not defined. + */ +STACK_OF(SCT) *o2i_SCT_LIST(STACK_OF(SCT) **a, const unsigned char **pp, + size_t len); + +/* + * Serialize (to DER format) a stack of SCTs and return the length. + * "a" must not be NULL. + * If "pp" is NULL, just returns the length of what would have been serialized. + * If "pp" is not NULL and "*pp" is null, function will allocate a new pointer + * for data that caller is responsible for freeing (only if function returns + * successfully). + * If "pp" is NULL and "*pp" is not NULL, caller is responsible for ensuring + * that "*pp" is large enough to accept all of the serialized data. + * Returns < 0 on error, >= 0 indicating bytes written (or would have been) + * on success. + */ +__owur int i2d_SCT_LIST(const STACK_OF(SCT) *a, unsigned char **pp); + +/* + * Parses an SCT list in DER format and returns it. + * If "a" or "*a" is NULL, a new stack will be created that the caller is + * responsible for freeing (by calling SCT_LIST_free). + * "**pp" and "*pp" must not be NULL. + * Upon success, "*pp" will point to after the last bytes read, and a stack + * will be returned. + * Upon failure, a NULL pointer will be returned, and the position of "*pp" is + * not defined. + */ +STACK_OF(SCT) *d2i_SCT_LIST(STACK_OF(SCT) **a, const unsigned char **pp, + long len); + +/* + * Serialize (to TLS format) an |sct| and write it to |out|. + * If |out| is null, no SCT will be output but the length will still be returned. + * If |out| points to a null pointer, a string will be allocated to hold the + * TLS-format SCT. It is the responsibility of the caller to free it. + * If |out| points to an allocated string, the TLS-format SCT will be written + * to it. + * The length of the SCT in TLS format will be returned. + */ +__owur int i2o_SCT(const SCT *sct, unsigned char **out); + +/* + * Parses an SCT in TLS format and returns it. + * If |psct| is not null, it will end up pointing to the parsed SCT. If it + * already points to a non-null pointer, the pointer will be free'd. + * |in| should be a pointer to a string containing the TLS-format SCT. + * |in| will be advanced to the end of the SCT if parsing succeeds. + * |len| should be the length of the SCT in |in|. + * Returns NULL if an error occurs. + * If the SCT is an unsupported version, only the SCT's 'sct' and 'sct_len' + * fields will be populated (with |in| and |len| respectively). + */ +SCT *o2i_SCT(SCT **psct, const unsigned char **in, size_t len); + +/******************** + * CT log functions * + ********************/ + +/* + * Creates a new CT log instance with the given |public_key| and |name|. + * Takes ownership of |public_key| but copies |name|. + * Returns NULL if malloc fails or if |public_key| cannot be converted to DER. + * Should be deleted by the caller using CTLOG_free when no longer needed. + */ +CTLOG *CTLOG_new(EVP_PKEY *public_key, const char *name); + +/* + * Creates a new CTLOG instance with the base64-encoded SubjectPublicKeyInfo DER + * in |pkey_base64|. The |name| is a string to help users identify this log. + * Returns 1 on success, 0 on failure. + * Should be deleted by the caller using CTLOG_free when no longer needed. + */ +int CTLOG_new_from_base64(CTLOG ** ct_log, + const char *pkey_base64, const char *name); + +/* + * Deletes a CT log instance and its fields. + */ +void CTLOG_free(CTLOG *log); + +/* Gets the name of the CT log */ +const char *CTLOG_get0_name(const CTLOG *log); +/* Gets the ID of the CT log */ +void CTLOG_get0_log_id(const CTLOG *log, const uint8_t **log_id, + size_t *log_id_len); +/* Gets the public key of the CT log */ +EVP_PKEY *CTLOG_get0_public_key(const CTLOG *log); + +/************************** + * CT log store functions * + **************************/ + +/* + * Creates a new CT log store. + * Should be deleted by the caller using CTLOG_STORE_free when no longer needed. + */ +CTLOG_STORE *CTLOG_STORE_new(void); + +/* + * Deletes a CT log store and all of the CT log instances held within. + */ +void CTLOG_STORE_free(CTLOG_STORE *store); + +/* + * Finds a CT log in the store based on its log ID. + * Returns the CT log, or NULL if no match is found. + */ +const CTLOG *CTLOG_STORE_get0_log_by_id(const CTLOG_STORE *store, + const uint8_t *log_id, + size_t log_id_len); + +/* + * Loads a CT log list into a |store| from a |file|. + * Returns 1 if loading is successful, or 0 otherwise. + */ +__owur int CTLOG_STORE_load_file(CTLOG_STORE *store, const char *file); + +/* + * Loads the default CT log list into a |store|. + * See internal/cryptlib.h for the environment variable and file path that are + * consulted to find the default file. + * Returns 1 if loading is successful, or 0 otherwise. + */ +__owur int CTLOG_STORE_load_default_file(CTLOG_STORE *store); + +# ifdef __cplusplus +} +# endif +# endif +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/cterr.h b/example/android/third_party/crypto/arm/include/openssl/cterr.h new file mode 100644 index 00000000..764e1a22 --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/cterr.h @@ -0,0 +1,76 @@ +/* + * Generated by util/mkerr.pl DO NOT EDIT + * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_CTERR_H +# define HEADER_CTERR_H + +# include + +# ifndef OPENSSL_NO_CT + +# ifdef __cplusplus +extern "C" +# endif +int ERR_load_CT_strings(void); + +/* + * CT function codes. + */ +# define CT_F_CTLOG_NEW 117 +# define CT_F_CTLOG_NEW_FROM_BASE64 118 +# define CT_F_CTLOG_NEW_FROM_CONF 119 +# define CT_F_CTLOG_STORE_LOAD_CTX_NEW 122 +# define CT_F_CTLOG_STORE_LOAD_FILE 123 +# define CT_F_CTLOG_STORE_LOAD_LOG 130 +# define CT_F_CTLOG_STORE_NEW 131 +# define CT_F_CT_BASE64_DECODE 124 +# define CT_F_CT_POLICY_EVAL_CTX_NEW 133 +# define CT_F_CT_V1_LOG_ID_FROM_PKEY 125 +# define CT_F_I2O_SCT 107 +# define CT_F_I2O_SCT_LIST 108 +# define CT_F_I2O_SCT_SIGNATURE 109 +# define CT_F_O2I_SCT 110 +# define CT_F_O2I_SCT_LIST 111 +# define CT_F_O2I_SCT_SIGNATURE 112 +# define CT_F_SCT_CTX_NEW 126 +# define CT_F_SCT_CTX_VERIFY 128 +# define CT_F_SCT_NEW 100 +# define CT_F_SCT_NEW_FROM_BASE64 127 +# define CT_F_SCT_SET0_LOG_ID 101 +# define CT_F_SCT_SET1_EXTENSIONS 114 +# define CT_F_SCT_SET1_LOG_ID 115 +# define CT_F_SCT_SET1_SIGNATURE 116 +# define CT_F_SCT_SET_LOG_ENTRY_TYPE 102 +# define CT_F_SCT_SET_SIGNATURE_NID 103 +# define CT_F_SCT_SET_VERSION 104 + +/* + * CT reason codes. + */ +# define CT_R_BASE64_DECODE_ERROR 108 +# define CT_R_INVALID_LOG_ID_LENGTH 100 +# define CT_R_LOG_CONF_INVALID 109 +# define CT_R_LOG_CONF_INVALID_KEY 110 +# define CT_R_LOG_CONF_MISSING_DESCRIPTION 111 +# define CT_R_LOG_CONF_MISSING_KEY 112 +# define CT_R_LOG_KEY_INVALID 113 +# define CT_R_SCT_FUTURE_TIMESTAMP 116 +# define CT_R_SCT_INVALID 104 +# define CT_R_SCT_INVALID_SIGNATURE 107 +# define CT_R_SCT_LIST_INVALID 105 +# define CT_R_SCT_LOG_ID_MISMATCH 114 +# define CT_R_SCT_NOT_SET 106 +# define CT_R_SCT_UNSUPPORTED_VERSION 115 +# define CT_R_UNRECOGNIZED_SIGNATURE_NID 101 +# define CT_R_UNSUPPORTED_ENTRY_TYPE 102 +# define CT_R_UNSUPPORTED_VERSION 103 + +# endif +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/des.h b/example/android/third_party/crypto/arm/include/openssl/des.h new file mode 100644 index 00000000..be4abbdf --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/des.h @@ -0,0 +1,174 @@ +/* + * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_DES_H +# define HEADER_DES_H + +# include + +# ifndef OPENSSL_NO_DES +# ifdef __cplusplus +extern "C" { +# endif +# include + +typedef unsigned int DES_LONG; + +# ifdef OPENSSL_BUILD_SHLIBCRYPTO +# undef OPENSSL_EXTERN +# define OPENSSL_EXTERN OPENSSL_EXPORT +# endif + +typedef unsigned char DES_cblock[8]; +typedef /* const */ unsigned char const_DES_cblock[8]; +/* + * With "const", gcc 2.8.1 on Solaris thinks that DES_cblock * and + * const_DES_cblock * are incompatible pointer types. + */ + +typedef struct DES_ks { + union { + DES_cblock cblock; + /* + * make sure things are correct size on machines with 8 byte longs + */ + DES_LONG deslong[2]; + } ks[16]; +} DES_key_schedule; + +# define DES_KEY_SZ (sizeof(DES_cblock)) +# define DES_SCHEDULE_SZ (sizeof(DES_key_schedule)) + +# define DES_ENCRYPT 1 +# define DES_DECRYPT 0 + +# define DES_CBC_MODE 0 +# define DES_PCBC_MODE 1 + +# define DES_ecb2_encrypt(i,o,k1,k2,e) \ + DES_ecb3_encrypt((i),(o),(k1),(k2),(k1),(e)) + +# define DES_ede2_cbc_encrypt(i,o,l,k1,k2,iv,e) \ + DES_ede3_cbc_encrypt((i),(o),(l),(k1),(k2),(k1),(iv),(e)) + +# define DES_ede2_cfb64_encrypt(i,o,l,k1,k2,iv,n,e) \ + DES_ede3_cfb64_encrypt((i),(o),(l),(k1),(k2),(k1),(iv),(n),(e)) + +# define DES_ede2_ofb64_encrypt(i,o,l,k1,k2,iv,n) \ + DES_ede3_ofb64_encrypt((i),(o),(l),(k1),(k2),(k1),(iv),(n)) + +OPENSSL_DECLARE_GLOBAL(int, DES_check_key); /* defaults to false */ +# define DES_check_key OPENSSL_GLOBAL_REF(DES_check_key) + +const char *DES_options(void); +void DES_ecb3_encrypt(const_DES_cblock *input, DES_cblock *output, + DES_key_schedule *ks1, DES_key_schedule *ks2, + DES_key_schedule *ks3, int enc); +DES_LONG DES_cbc_cksum(const unsigned char *input, DES_cblock *output, + long length, DES_key_schedule *schedule, + const_DES_cblock *ivec); +/* DES_cbc_encrypt does not update the IV! Use DES_ncbc_encrypt instead. */ +void DES_cbc_encrypt(const unsigned char *input, unsigned char *output, + long length, DES_key_schedule *schedule, + DES_cblock *ivec, int enc); +void DES_ncbc_encrypt(const unsigned char *input, unsigned char *output, + long length, DES_key_schedule *schedule, + DES_cblock *ivec, int enc); +void DES_xcbc_encrypt(const unsigned char *input, unsigned char *output, + long length, DES_key_schedule *schedule, + DES_cblock *ivec, const_DES_cblock *inw, + const_DES_cblock *outw, int enc); +void DES_cfb_encrypt(const unsigned char *in, unsigned char *out, int numbits, + long length, DES_key_schedule *schedule, + DES_cblock *ivec, int enc); +void DES_ecb_encrypt(const_DES_cblock *input, DES_cblock *output, + DES_key_schedule *ks, int enc); + +/* + * This is the DES encryption function that gets called by just about every + * other DES routine in the library. You should not use this function except + * to implement 'modes' of DES. I say this because the functions that call + * this routine do the conversion from 'char *' to long, and this needs to be + * done to make sure 'non-aligned' memory access do not occur. The + * characters are loaded 'little endian'. Data is a pointer to 2 unsigned + * long's and ks is the DES_key_schedule to use. enc, is non zero specifies + * encryption, zero if decryption. + */ +void DES_encrypt1(DES_LONG *data, DES_key_schedule *ks, int enc); + +/* + * This functions is the same as DES_encrypt1() except that the DES initial + * permutation (IP) and final permutation (FP) have been left out. As for + * DES_encrypt1(), you should not use this function. It is used by the + * routines in the library that implement triple DES. IP() DES_encrypt2() + * DES_encrypt2() DES_encrypt2() FP() is the same as DES_encrypt1() + * DES_encrypt1() DES_encrypt1() except faster :-). + */ +void DES_encrypt2(DES_LONG *data, DES_key_schedule *ks, int enc); + +void DES_encrypt3(DES_LONG *data, DES_key_schedule *ks1, + DES_key_schedule *ks2, DES_key_schedule *ks3); +void DES_decrypt3(DES_LONG *data, DES_key_schedule *ks1, + DES_key_schedule *ks2, DES_key_schedule *ks3); +void DES_ede3_cbc_encrypt(const unsigned char *input, unsigned char *output, + long length, + DES_key_schedule *ks1, DES_key_schedule *ks2, + DES_key_schedule *ks3, DES_cblock *ivec, int enc); +void DES_ede3_cfb64_encrypt(const unsigned char *in, unsigned char *out, + long length, DES_key_schedule *ks1, + DES_key_schedule *ks2, DES_key_schedule *ks3, + DES_cblock *ivec, int *num, int enc); +void DES_ede3_cfb_encrypt(const unsigned char *in, unsigned char *out, + int numbits, long length, DES_key_schedule *ks1, + DES_key_schedule *ks2, DES_key_schedule *ks3, + DES_cblock *ivec, int enc); +void DES_ede3_ofb64_encrypt(const unsigned char *in, unsigned char *out, + long length, DES_key_schedule *ks1, + DES_key_schedule *ks2, DES_key_schedule *ks3, + DES_cblock *ivec, int *num); +char *DES_fcrypt(const char *buf, const char *salt, char *ret); +char *DES_crypt(const char *buf, const char *salt); +void DES_ofb_encrypt(const unsigned char *in, unsigned char *out, int numbits, + long length, DES_key_schedule *schedule, + DES_cblock *ivec); +void DES_pcbc_encrypt(const unsigned char *input, unsigned char *output, + long length, DES_key_schedule *schedule, + DES_cblock *ivec, int enc); +DES_LONG DES_quad_cksum(const unsigned char *input, DES_cblock output[], + long length, int out_count, DES_cblock *seed); +int DES_random_key(DES_cblock *ret); +void DES_set_odd_parity(DES_cblock *key); +int DES_check_key_parity(const_DES_cblock *key); +int DES_is_weak_key(const_DES_cblock *key); +/* + * DES_set_key (= set_key = DES_key_sched = key_sched) calls + * DES_set_key_checked if global variable DES_check_key is set, + * DES_set_key_unchecked otherwise. + */ +int DES_set_key(const_DES_cblock *key, DES_key_schedule *schedule); +int DES_key_sched(const_DES_cblock *key, DES_key_schedule *schedule); +int DES_set_key_checked(const_DES_cblock *key, DES_key_schedule *schedule); +void DES_set_key_unchecked(const_DES_cblock *key, DES_key_schedule *schedule); +void DES_string_to_key(const char *str, DES_cblock *key); +void DES_string_to_2keys(const char *str, DES_cblock *key1, DES_cblock *key2); +void DES_cfb64_encrypt(const unsigned char *in, unsigned char *out, + long length, DES_key_schedule *schedule, + DES_cblock *ivec, int *num, int enc); +void DES_ofb64_encrypt(const unsigned char *in, unsigned char *out, + long length, DES_key_schedule *schedule, + DES_cblock *ivec, int *num); + +# define DES_fixup_key_parity DES_set_odd_parity + +# ifdef __cplusplus +} +# endif +# endif + +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/dh.h b/example/android/third_party/crypto/arm/include/openssl/dh.h new file mode 100644 index 00000000..3527540c --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/dh.h @@ -0,0 +1,340 @@ +/* + * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_DH_H +# define HEADER_DH_H + +# include + +# ifndef OPENSSL_NO_DH +# include +# include +# include +# include +# if OPENSSL_API_COMPAT < 0x10100000L +# include +# endif +# include + +# ifdef __cplusplus +extern "C" { +# endif + +# ifndef OPENSSL_DH_MAX_MODULUS_BITS +# define OPENSSL_DH_MAX_MODULUS_BITS 10000 +# endif + +# define OPENSSL_DH_FIPS_MIN_MODULUS_BITS 1024 + +# define DH_FLAG_CACHE_MONT_P 0x01 + +# if OPENSSL_API_COMPAT < 0x10100000L +/* + * Does nothing. Previously this switched off constant time behaviour. + */ +# define DH_FLAG_NO_EXP_CONSTTIME 0x00 +# endif + +/* + * If this flag is set the DH method is FIPS compliant and can be used in + * FIPS mode. This is set in the validated module method. If an application + * sets this flag in its own methods it is its responsibility to ensure the + * result is compliant. + */ + +# define DH_FLAG_FIPS_METHOD 0x0400 + +/* + * If this flag is set the operations normally disabled in FIPS mode are + * permitted it is then the applications responsibility to ensure that the + * usage is compliant. + */ + +# define DH_FLAG_NON_FIPS_ALLOW 0x0400 + +/* Already defined in ossl_typ.h */ +/* typedef struct dh_st DH; */ +/* typedef struct dh_method DH_METHOD; */ + +DECLARE_ASN1_ITEM(DHparams) + +# define DH_GENERATOR_2 2 +/* #define DH_GENERATOR_3 3 */ +# define DH_GENERATOR_5 5 + +/* DH_check error codes */ +# define DH_CHECK_P_NOT_PRIME 0x01 +# define DH_CHECK_P_NOT_SAFE_PRIME 0x02 +# define DH_UNABLE_TO_CHECK_GENERATOR 0x04 +# define DH_NOT_SUITABLE_GENERATOR 0x08 +# define DH_CHECK_Q_NOT_PRIME 0x10 +# define DH_CHECK_INVALID_Q_VALUE 0x20 +# define DH_CHECK_INVALID_J_VALUE 0x40 + +/* DH_check_pub_key error codes */ +# define DH_CHECK_PUBKEY_TOO_SMALL 0x01 +# define DH_CHECK_PUBKEY_TOO_LARGE 0x02 +# define DH_CHECK_PUBKEY_INVALID 0x04 + +/* + * primes p where (p-1)/2 is prime too are called "safe"; we define this for + * backward compatibility: + */ +# define DH_CHECK_P_NOT_STRONG_PRIME DH_CHECK_P_NOT_SAFE_PRIME + +# define d2i_DHparams_fp(fp,x) \ + (DH *)ASN1_d2i_fp((char *(*)())DH_new, \ + (char *(*)())d2i_DHparams, \ + (fp), \ + (unsigned char **)(x)) +# define i2d_DHparams_fp(fp,x) \ + ASN1_i2d_fp(i2d_DHparams,(fp), (unsigned char *)(x)) +# define d2i_DHparams_bio(bp,x) \ + ASN1_d2i_bio_of(DH, DH_new, d2i_DHparams, bp, x) +# define i2d_DHparams_bio(bp,x) \ + ASN1_i2d_bio_of_const(DH,i2d_DHparams,bp,x) + +# define d2i_DHxparams_fp(fp,x) \ + (DH *)ASN1_d2i_fp((char *(*)())DH_new, \ + (char *(*)())d2i_DHxparams, \ + (fp), \ + (unsigned char **)(x)) +# define i2d_DHxparams_fp(fp,x) \ + ASN1_i2d_fp(i2d_DHxparams,(fp), (unsigned char *)(x)) +# define d2i_DHxparams_bio(bp,x) \ + ASN1_d2i_bio_of(DH, DH_new, d2i_DHxparams, bp, x) +# define i2d_DHxparams_bio(bp,x) \ + ASN1_i2d_bio_of_const(DH, i2d_DHxparams, bp, x) + +DH *DHparams_dup(DH *); + +const DH_METHOD *DH_OpenSSL(void); + +void DH_set_default_method(const DH_METHOD *meth); +const DH_METHOD *DH_get_default_method(void); +int DH_set_method(DH *dh, const DH_METHOD *meth); +DH *DH_new_method(ENGINE *engine); + +DH *DH_new(void); +void DH_free(DH *dh); +int DH_up_ref(DH *dh); +int DH_bits(const DH *dh); +int DH_size(const DH *dh); +int DH_security_bits(const DH *dh); +#define DH_get_ex_new_index(l, p, newf, dupf, freef) \ + CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_DH, l, p, newf, dupf, freef) +int DH_set_ex_data(DH *d, int idx, void *arg); +void *DH_get_ex_data(DH *d, int idx); + +/* Deprecated version */ +DEPRECATEDIN_0_9_8(DH *DH_generate_parameters(int prime_len, int generator, + void (*callback) (int, int, + void *), + void *cb_arg)) + +/* New version */ +int DH_generate_parameters_ex(DH *dh, int prime_len, int generator, + BN_GENCB *cb); + +int DH_check_params_ex(const DH *dh); +int DH_check_ex(const DH *dh); +int DH_check_pub_key_ex(const DH *dh, const BIGNUM *pub_key); +int DH_check_params(const DH *dh, int *ret); +int DH_check(const DH *dh, int *codes); +int DH_check_pub_key(const DH *dh, const BIGNUM *pub_key, int *codes); +int DH_generate_key(DH *dh); +int DH_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh); +int DH_compute_key_padded(unsigned char *key, const BIGNUM *pub_key, DH *dh); +DH *d2i_DHparams(DH **a, const unsigned char **pp, long length); +int i2d_DHparams(const DH *a, unsigned char **pp); +DH *d2i_DHxparams(DH **a, const unsigned char **pp, long length); +int i2d_DHxparams(const DH *a, unsigned char **pp); +# ifndef OPENSSL_NO_STDIO +int DHparams_print_fp(FILE *fp, const DH *x); +# endif +int DHparams_print(BIO *bp, const DH *x); + +/* RFC 5114 parameters */ +DH *DH_get_1024_160(void); +DH *DH_get_2048_224(void); +DH *DH_get_2048_256(void); + +/* Named parameters, currently RFC7919 */ +DH *DH_new_by_nid(int nid); +int DH_get_nid(const DH *dh); + +# ifndef OPENSSL_NO_CMS +/* RFC2631 KDF */ +int DH_KDF_X9_42(unsigned char *out, size_t outlen, + const unsigned char *Z, size_t Zlen, + ASN1_OBJECT *key_oid, + const unsigned char *ukm, size_t ukmlen, const EVP_MD *md); +# endif + +void DH_get0_pqg(const DH *dh, + const BIGNUM **p, const BIGNUM **q, const BIGNUM **g); +int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g); +void DH_get0_key(const DH *dh, + const BIGNUM **pub_key, const BIGNUM **priv_key); +int DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key); +const BIGNUM *DH_get0_p(const DH *dh); +const BIGNUM *DH_get0_q(const DH *dh); +const BIGNUM *DH_get0_g(const DH *dh); +const BIGNUM *DH_get0_priv_key(const DH *dh); +const BIGNUM *DH_get0_pub_key(const DH *dh); +void DH_clear_flags(DH *dh, int flags); +int DH_test_flags(const DH *dh, int flags); +void DH_set_flags(DH *dh, int flags); +ENGINE *DH_get0_engine(DH *d); +long DH_get_length(const DH *dh); +int DH_set_length(DH *dh, long length); + +DH_METHOD *DH_meth_new(const char *name, int flags); +void DH_meth_free(DH_METHOD *dhm); +DH_METHOD *DH_meth_dup(const DH_METHOD *dhm); +const char *DH_meth_get0_name(const DH_METHOD *dhm); +int DH_meth_set1_name(DH_METHOD *dhm, const char *name); +int DH_meth_get_flags(const DH_METHOD *dhm); +int DH_meth_set_flags(DH_METHOD *dhm, int flags); +void *DH_meth_get0_app_data(const DH_METHOD *dhm); +int DH_meth_set0_app_data(DH_METHOD *dhm, void *app_data); +int (*DH_meth_get_generate_key(const DH_METHOD *dhm)) (DH *); +int DH_meth_set_generate_key(DH_METHOD *dhm, int (*generate_key) (DH *)); +int (*DH_meth_get_compute_key(const DH_METHOD *dhm)) + (unsigned char *key, const BIGNUM *pub_key, DH *dh); +int DH_meth_set_compute_key(DH_METHOD *dhm, + int (*compute_key) (unsigned char *key, const BIGNUM *pub_key, DH *dh)); +int (*DH_meth_get_bn_mod_exp(const DH_METHOD *dhm)) + (const DH *, BIGNUM *, const BIGNUM *, const BIGNUM *, const BIGNUM *, + BN_CTX *, BN_MONT_CTX *); +int DH_meth_set_bn_mod_exp(DH_METHOD *dhm, + int (*bn_mod_exp) (const DH *, BIGNUM *, const BIGNUM *, const BIGNUM *, + const BIGNUM *, BN_CTX *, BN_MONT_CTX *)); +int (*DH_meth_get_init(const DH_METHOD *dhm))(DH *); +int DH_meth_set_init(DH_METHOD *dhm, int (*init)(DH *)); +int (*DH_meth_get_finish(const DH_METHOD *dhm)) (DH *); +int DH_meth_set_finish(DH_METHOD *dhm, int (*finish) (DH *)); +int (*DH_meth_get_generate_params(const DH_METHOD *dhm)) + (DH *, int, int, BN_GENCB *); +int DH_meth_set_generate_params(DH_METHOD *dhm, + int (*generate_params) (DH *, int, int, BN_GENCB *)); + + +# define EVP_PKEY_CTX_set_dh_paramgen_prime_len(ctx, len) \ + EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_PARAMGEN, \ + EVP_PKEY_CTRL_DH_PARAMGEN_PRIME_LEN, len, NULL) + +# define EVP_PKEY_CTX_set_dh_paramgen_subprime_len(ctx, len) \ + EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_PARAMGEN, \ + EVP_PKEY_CTRL_DH_PARAMGEN_SUBPRIME_LEN, len, NULL) + +# define EVP_PKEY_CTX_set_dh_paramgen_type(ctx, typ) \ + EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_PARAMGEN, \ + EVP_PKEY_CTRL_DH_PARAMGEN_TYPE, typ, NULL) + +# define EVP_PKEY_CTX_set_dh_paramgen_generator(ctx, gen) \ + EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_PARAMGEN, \ + EVP_PKEY_CTRL_DH_PARAMGEN_GENERATOR, gen, NULL) + +# define EVP_PKEY_CTX_set_dh_rfc5114(ctx, gen) \ + EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, EVP_PKEY_OP_PARAMGEN, \ + EVP_PKEY_CTRL_DH_RFC5114, gen, NULL) + +# define EVP_PKEY_CTX_set_dhx_rfc5114(ctx, gen) \ + EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, EVP_PKEY_OP_PARAMGEN, \ + EVP_PKEY_CTRL_DH_RFC5114, gen, NULL) + +# define EVP_PKEY_CTX_set_dh_nid(ctx, nid) \ + EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, \ + EVP_PKEY_OP_PARAMGEN | EVP_PKEY_OP_KEYGEN, \ + EVP_PKEY_CTRL_DH_NID, nid, NULL) + +# define EVP_PKEY_CTX_set_dh_pad(ctx, pad) \ + EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_DERIVE, \ + EVP_PKEY_CTRL_DH_PAD, pad, NULL) + +# define EVP_PKEY_CTX_set_dh_kdf_type(ctx, kdf) \ + EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \ + EVP_PKEY_OP_DERIVE, \ + EVP_PKEY_CTRL_DH_KDF_TYPE, kdf, NULL) + +# define EVP_PKEY_CTX_get_dh_kdf_type(ctx) \ + EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \ + EVP_PKEY_OP_DERIVE, \ + EVP_PKEY_CTRL_DH_KDF_TYPE, -2, NULL) + +# define EVP_PKEY_CTX_set0_dh_kdf_oid(ctx, oid) \ + EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \ + EVP_PKEY_OP_DERIVE, \ + EVP_PKEY_CTRL_DH_KDF_OID, 0, (void *)(oid)) + +# define EVP_PKEY_CTX_get0_dh_kdf_oid(ctx, poid) \ + EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \ + EVP_PKEY_OP_DERIVE, \ + EVP_PKEY_CTRL_GET_DH_KDF_OID, 0, (void *)(poid)) + +# define EVP_PKEY_CTX_set_dh_kdf_md(ctx, md) \ + EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \ + EVP_PKEY_OP_DERIVE, \ + EVP_PKEY_CTRL_DH_KDF_MD, 0, (void *)(md)) + +# define EVP_PKEY_CTX_get_dh_kdf_md(ctx, pmd) \ + EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \ + EVP_PKEY_OP_DERIVE, \ + EVP_PKEY_CTRL_GET_DH_KDF_MD, 0, (void *)(pmd)) + +# define EVP_PKEY_CTX_set_dh_kdf_outlen(ctx, len) \ + EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \ + EVP_PKEY_OP_DERIVE, \ + EVP_PKEY_CTRL_DH_KDF_OUTLEN, len, NULL) + +# define EVP_PKEY_CTX_get_dh_kdf_outlen(ctx, plen) \ + EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \ + EVP_PKEY_OP_DERIVE, \ + EVP_PKEY_CTRL_GET_DH_KDF_OUTLEN, 0, (void *)(plen)) + +# define EVP_PKEY_CTX_set0_dh_kdf_ukm(ctx, p, plen) \ + EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \ + EVP_PKEY_OP_DERIVE, \ + EVP_PKEY_CTRL_DH_KDF_UKM, plen, (void *)(p)) + +# define EVP_PKEY_CTX_get0_dh_kdf_ukm(ctx, p) \ + EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \ + EVP_PKEY_OP_DERIVE, \ + EVP_PKEY_CTRL_GET_DH_KDF_UKM, 0, (void *)(p)) + +# define EVP_PKEY_CTRL_DH_PARAMGEN_PRIME_LEN (EVP_PKEY_ALG_CTRL + 1) +# define EVP_PKEY_CTRL_DH_PARAMGEN_GENERATOR (EVP_PKEY_ALG_CTRL + 2) +# define EVP_PKEY_CTRL_DH_RFC5114 (EVP_PKEY_ALG_CTRL + 3) +# define EVP_PKEY_CTRL_DH_PARAMGEN_SUBPRIME_LEN (EVP_PKEY_ALG_CTRL + 4) +# define EVP_PKEY_CTRL_DH_PARAMGEN_TYPE (EVP_PKEY_ALG_CTRL + 5) +# define EVP_PKEY_CTRL_DH_KDF_TYPE (EVP_PKEY_ALG_CTRL + 6) +# define EVP_PKEY_CTRL_DH_KDF_MD (EVP_PKEY_ALG_CTRL + 7) +# define EVP_PKEY_CTRL_GET_DH_KDF_MD (EVP_PKEY_ALG_CTRL + 8) +# define EVP_PKEY_CTRL_DH_KDF_OUTLEN (EVP_PKEY_ALG_CTRL + 9) +# define EVP_PKEY_CTRL_GET_DH_KDF_OUTLEN (EVP_PKEY_ALG_CTRL + 10) +# define EVP_PKEY_CTRL_DH_KDF_UKM (EVP_PKEY_ALG_CTRL + 11) +# define EVP_PKEY_CTRL_GET_DH_KDF_UKM (EVP_PKEY_ALG_CTRL + 12) +# define EVP_PKEY_CTRL_DH_KDF_OID (EVP_PKEY_ALG_CTRL + 13) +# define EVP_PKEY_CTRL_GET_DH_KDF_OID (EVP_PKEY_ALG_CTRL + 14) +# define EVP_PKEY_CTRL_DH_NID (EVP_PKEY_ALG_CTRL + 15) +# define EVP_PKEY_CTRL_DH_PAD (EVP_PKEY_ALG_CTRL + 16) + +/* KDF types */ +# define EVP_PKEY_DH_KDF_NONE 1 +# ifndef OPENSSL_NO_CMS +# define EVP_PKEY_DH_KDF_X9_42 2 +# endif + + +# ifdef __cplusplus +} +# endif +# endif +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/dherr.h b/example/android/third_party/crypto/arm/include/openssl/dherr.h new file mode 100644 index 00000000..81e73f75 --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/dherr.h @@ -0,0 +1,84 @@ +/* + * Generated by util/mkerr.pl DO NOT EDIT + * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_DHERR_H +# define HEADER_DHERR_H + +# include + +# ifndef OPENSSL_NO_DH + +# ifdef __cplusplus +extern "C" +# endif +int ERR_load_DH_strings(void); + +/* + * DH function codes. + */ +# define DH_F_COMPUTE_KEY 102 +# define DH_F_DHPARAMS_PRINT_FP 101 +# define DH_F_DH_BUILTIN_GENPARAMS 106 +# define DH_F_DH_CHECK_EX 121 +# define DH_F_DH_CHECK_PARAMS_EX 122 +# define DH_F_DH_CHECK_PUB_KEY_EX 123 +# define DH_F_DH_CMS_DECRYPT 114 +# define DH_F_DH_CMS_SET_PEERKEY 115 +# define DH_F_DH_CMS_SET_SHARED_INFO 116 +# define DH_F_DH_METH_DUP 117 +# define DH_F_DH_METH_NEW 118 +# define DH_F_DH_METH_SET1_NAME 119 +# define DH_F_DH_NEW_BY_NID 104 +# define DH_F_DH_NEW_METHOD 105 +# define DH_F_DH_PARAM_DECODE 107 +# define DH_F_DH_PKEY_PUBLIC_CHECK 124 +# define DH_F_DH_PRIV_DECODE 110 +# define DH_F_DH_PRIV_ENCODE 111 +# define DH_F_DH_PUB_DECODE 108 +# define DH_F_DH_PUB_ENCODE 109 +# define DH_F_DO_DH_PRINT 100 +# define DH_F_GENERATE_KEY 103 +# define DH_F_PKEY_DH_CTRL_STR 120 +# define DH_F_PKEY_DH_DERIVE 112 +# define DH_F_PKEY_DH_INIT 125 +# define DH_F_PKEY_DH_KEYGEN 113 + +/* + * DH reason codes. + */ +# define DH_R_BAD_GENERATOR 101 +# define DH_R_BN_DECODE_ERROR 109 +# define DH_R_BN_ERROR 106 +# define DH_R_CHECK_INVALID_J_VALUE 115 +# define DH_R_CHECK_INVALID_Q_VALUE 116 +# define DH_R_CHECK_PUBKEY_INVALID 122 +# define DH_R_CHECK_PUBKEY_TOO_LARGE 123 +# define DH_R_CHECK_PUBKEY_TOO_SMALL 124 +# define DH_R_CHECK_P_NOT_PRIME 117 +# define DH_R_CHECK_P_NOT_SAFE_PRIME 118 +# define DH_R_CHECK_Q_NOT_PRIME 119 +# define DH_R_DECODE_ERROR 104 +# define DH_R_INVALID_PARAMETER_NAME 110 +# define DH_R_INVALID_PARAMETER_NID 114 +# define DH_R_INVALID_PUBKEY 102 +# define DH_R_KDF_PARAMETER_ERROR 112 +# define DH_R_KEYS_NOT_SET 108 +# define DH_R_MISSING_PUBKEY 125 +# define DH_R_MODULUS_TOO_LARGE 103 +# define DH_R_NOT_SUITABLE_GENERATOR 120 +# define DH_R_NO_PARAMETERS_SET 107 +# define DH_R_NO_PRIVATE_VALUE 100 +# define DH_R_PARAMETER_ENCODING_ERROR 105 +# define DH_R_PEER_KEY_ERROR 111 +# define DH_R_SHARED_INFO_ERROR 113 +# define DH_R_UNABLE_TO_CHECK_GENERATOR 121 + +# endif +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/dsa.h b/example/android/third_party/crypto/arm/include/openssl/dsa.h new file mode 100644 index 00000000..822eff34 --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/dsa.h @@ -0,0 +1,238 @@ +/* + * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_DSA_H +# define HEADER_DSA_H + +# include + +# ifndef OPENSSL_NO_DSA +# ifdef __cplusplus +extern "C" { +# endif +# include +# include +# include +# include +# include +# if OPENSSL_API_COMPAT < 0x10100000L +# include +# endif +# include + +# ifndef OPENSSL_DSA_MAX_MODULUS_BITS +# define OPENSSL_DSA_MAX_MODULUS_BITS 10000 +# endif + +# define OPENSSL_DSA_FIPS_MIN_MODULUS_BITS 1024 + +# define DSA_FLAG_CACHE_MONT_P 0x01 +# if OPENSSL_API_COMPAT < 0x10100000L +/* + * Does nothing. Previously this switched off constant time behaviour. + */ +# define DSA_FLAG_NO_EXP_CONSTTIME 0x00 +# endif + +/* + * If this flag is set the DSA method is FIPS compliant and can be used in + * FIPS mode. This is set in the validated module method. If an application + * sets this flag in its own methods it is its responsibility to ensure the + * result is compliant. + */ + +# define DSA_FLAG_FIPS_METHOD 0x0400 + +/* + * If this flag is set the operations normally disabled in FIPS mode are + * permitted it is then the applications responsibility to ensure that the + * usage is compliant. + */ + +# define DSA_FLAG_NON_FIPS_ALLOW 0x0400 +# define DSA_FLAG_FIPS_CHECKED 0x0800 + +/* Already defined in ossl_typ.h */ +/* typedef struct dsa_st DSA; */ +/* typedef struct dsa_method DSA_METHOD; */ + +typedef struct DSA_SIG_st DSA_SIG; + +# define d2i_DSAparams_fp(fp,x) (DSA *)ASN1_d2i_fp((char *(*)())DSA_new, \ + (char *(*)())d2i_DSAparams,(fp),(unsigned char **)(x)) +# define i2d_DSAparams_fp(fp,x) ASN1_i2d_fp(i2d_DSAparams,(fp), \ + (unsigned char *)(x)) +# define d2i_DSAparams_bio(bp,x) ASN1_d2i_bio_of(DSA,DSA_new,d2i_DSAparams,bp,x) +# define i2d_DSAparams_bio(bp,x) ASN1_i2d_bio_of_const(DSA,i2d_DSAparams,bp,x) + +DSA *DSAparams_dup(DSA *x); +DSA_SIG *DSA_SIG_new(void); +void DSA_SIG_free(DSA_SIG *a); +int i2d_DSA_SIG(const DSA_SIG *a, unsigned char **pp); +DSA_SIG *d2i_DSA_SIG(DSA_SIG **v, const unsigned char **pp, long length); +void DSA_SIG_get0(const DSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps); +int DSA_SIG_set0(DSA_SIG *sig, BIGNUM *r, BIGNUM *s); + +DSA_SIG *DSA_do_sign(const unsigned char *dgst, int dlen, DSA *dsa); +int DSA_do_verify(const unsigned char *dgst, int dgst_len, + DSA_SIG *sig, DSA *dsa); + +const DSA_METHOD *DSA_OpenSSL(void); + +void DSA_set_default_method(const DSA_METHOD *); +const DSA_METHOD *DSA_get_default_method(void); +int DSA_set_method(DSA *dsa, const DSA_METHOD *); +const DSA_METHOD *DSA_get_method(DSA *d); + +DSA *DSA_new(void); +DSA *DSA_new_method(ENGINE *engine); +void DSA_free(DSA *r); +/* "up" the DSA object's reference count */ +int DSA_up_ref(DSA *r); +int DSA_size(const DSA *); +int DSA_bits(const DSA *d); +int DSA_security_bits(const DSA *d); + /* next 4 return -1 on error */ +DEPRECATEDIN_1_2_0(int DSA_sign_setup(DSA *dsa, BN_CTX *ctx_in, BIGNUM **kinvp, BIGNUM **rp)) +int DSA_sign(int type, const unsigned char *dgst, int dlen, + unsigned char *sig, unsigned int *siglen, DSA *dsa); +int DSA_verify(int type, const unsigned char *dgst, int dgst_len, + const unsigned char *sigbuf, int siglen, DSA *dsa); +#define DSA_get_ex_new_index(l, p, newf, dupf, freef) \ + CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_DSA, l, p, newf, dupf, freef) +int DSA_set_ex_data(DSA *d, int idx, void *arg); +void *DSA_get_ex_data(DSA *d, int idx); + +DSA *d2i_DSAPublicKey(DSA **a, const unsigned char **pp, long length); +DSA *d2i_DSAPrivateKey(DSA **a, const unsigned char **pp, long length); +DSA *d2i_DSAparams(DSA **a, const unsigned char **pp, long length); + +/* Deprecated version */ +DEPRECATEDIN_0_9_8(DSA *DSA_generate_parameters(int bits, + unsigned char *seed, + int seed_len, + int *counter_ret, + unsigned long *h_ret, void + (*callback) (int, int, + void *), + void *cb_arg)) + +/* New version */ +int DSA_generate_parameters_ex(DSA *dsa, int bits, + const unsigned char *seed, int seed_len, + int *counter_ret, unsigned long *h_ret, + BN_GENCB *cb); + +int DSA_generate_key(DSA *a); +int i2d_DSAPublicKey(const DSA *a, unsigned char **pp); +int i2d_DSAPrivateKey(const DSA *a, unsigned char **pp); +int i2d_DSAparams(const DSA *a, unsigned char **pp); + +int DSAparams_print(BIO *bp, const DSA *x); +int DSA_print(BIO *bp, const DSA *x, int off); +# ifndef OPENSSL_NO_STDIO +int DSAparams_print_fp(FILE *fp, const DSA *x); +int DSA_print_fp(FILE *bp, const DSA *x, int off); +# endif + +# define DSS_prime_checks 64 +/* + * Primality test according to FIPS PUB 186-4, Appendix C.3. Since we only + * have one value here we set the number of checks to 64 which is the 128 bit + * security level that is the highest level and valid for creating a 3072 bit + * DSA key. + */ +# define DSA_is_prime(n, callback, cb_arg) \ + BN_is_prime(n, DSS_prime_checks, callback, NULL, cb_arg) + +# ifndef OPENSSL_NO_DH +/* + * Convert DSA structure (key or just parameters) into DH structure (be + * careful to avoid small subgroup attacks when using this!) + */ +DH *DSA_dup_DH(const DSA *r); +# endif + +# define EVP_PKEY_CTX_set_dsa_paramgen_bits(ctx, nbits) \ + EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DSA, EVP_PKEY_OP_PARAMGEN, \ + EVP_PKEY_CTRL_DSA_PARAMGEN_BITS, nbits, NULL) + +# define EVP_PKEY_CTRL_DSA_PARAMGEN_BITS (EVP_PKEY_ALG_CTRL + 1) +# define EVP_PKEY_CTRL_DSA_PARAMGEN_Q_BITS (EVP_PKEY_ALG_CTRL + 2) +# define EVP_PKEY_CTRL_DSA_PARAMGEN_MD (EVP_PKEY_ALG_CTRL + 3) + +void DSA_get0_pqg(const DSA *d, + const BIGNUM **p, const BIGNUM **q, const BIGNUM **g); +int DSA_set0_pqg(DSA *d, BIGNUM *p, BIGNUM *q, BIGNUM *g); +void DSA_get0_key(const DSA *d, + const BIGNUM **pub_key, const BIGNUM **priv_key); +int DSA_set0_key(DSA *d, BIGNUM *pub_key, BIGNUM *priv_key); +const BIGNUM *DSA_get0_p(const DSA *d); +const BIGNUM *DSA_get0_q(const DSA *d); +const BIGNUM *DSA_get0_g(const DSA *d); +const BIGNUM *DSA_get0_pub_key(const DSA *d); +const BIGNUM *DSA_get0_priv_key(const DSA *d); +void DSA_clear_flags(DSA *d, int flags); +int DSA_test_flags(const DSA *d, int flags); +void DSA_set_flags(DSA *d, int flags); +ENGINE *DSA_get0_engine(DSA *d); + +DSA_METHOD *DSA_meth_new(const char *name, int flags); +void DSA_meth_free(DSA_METHOD *dsam); +DSA_METHOD *DSA_meth_dup(const DSA_METHOD *dsam); +const char *DSA_meth_get0_name(const DSA_METHOD *dsam); +int DSA_meth_set1_name(DSA_METHOD *dsam, const char *name); +int DSA_meth_get_flags(const DSA_METHOD *dsam); +int DSA_meth_set_flags(DSA_METHOD *dsam, int flags); +void *DSA_meth_get0_app_data(const DSA_METHOD *dsam); +int DSA_meth_set0_app_data(DSA_METHOD *dsam, void *app_data); +DSA_SIG *(*DSA_meth_get_sign(const DSA_METHOD *dsam)) + (const unsigned char *, int, DSA *); +int DSA_meth_set_sign(DSA_METHOD *dsam, + DSA_SIG *(*sign) (const unsigned char *, int, DSA *)); +int (*DSA_meth_get_sign_setup(const DSA_METHOD *dsam)) + (DSA *, BN_CTX *, BIGNUM **, BIGNUM **); +int DSA_meth_set_sign_setup(DSA_METHOD *dsam, + int (*sign_setup) (DSA *, BN_CTX *, BIGNUM **, BIGNUM **)); +int (*DSA_meth_get_verify(const DSA_METHOD *dsam)) + (const unsigned char *, int, DSA_SIG *, DSA *); +int DSA_meth_set_verify(DSA_METHOD *dsam, + int (*verify) (const unsigned char *, int, DSA_SIG *, DSA *)); +int (*DSA_meth_get_mod_exp(const DSA_METHOD *dsam)) + (DSA *, BIGNUM *, const BIGNUM *, const BIGNUM *, const BIGNUM *, + const BIGNUM *, const BIGNUM *, BN_CTX *, BN_MONT_CTX *); +int DSA_meth_set_mod_exp(DSA_METHOD *dsam, + int (*mod_exp) (DSA *, BIGNUM *, const BIGNUM *, const BIGNUM *, + const BIGNUM *, const BIGNUM *, const BIGNUM *, BN_CTX *, + BN_MONT_CTX *)); +int (*DSA_meth_get_bn_mod_exp(const DSA_METHOD *dsam)) + (DSA *, BIGNUM *, const BIGNUM *, const BIGNUM *, const BIGNUM *, + BN_CTX *, BN_MONT_CTX *); +int DSA_meth_set_bn_mod_exp(DSA_METHOD *dsam, + int (*bn_mod_exp) (DSA *, BIGNUM *, const BIGNUM *, const BIGNUM *, + const BIGNUM *, BN_CTX *, BN_MONT_CTX *)); +int (*DSA_meth_get_init(const DSA_METHOD *dsam))(DSA *); +int DSA_meth_set_init(DSA_METHOD *dsam, int (*init)(DSA *)); +int (*DSA_meth_get_finish(const DSA_METHOD *dsam)) (DSA *); +int DSA_meth_set_finish(DSA_METHOD *dsam, int (*finish) (DSA *)); +int (*DSA_meth_get_paramgen(const DSA_METHOD *dsam)) + (DSA *, int, const unsigned char *, int, int *, unsigned long *, + BN_GENCB *); +int DSA_meth_set_paramgen(DSA_METHOD *dsam, + int (*paramgen) (DSA *, int, const unsigned char *, int, int *, + unsigned long *, BN_GENCB *)); +int (*DSA_meth_get_keygen(const DSA_METHOD *dsam)) (DSA *); +int DSA_meth_set_keygen(DSA_METHOD *dsam, int (*keygen) (DSA *)); + + +# ifdef __cplusplus +} +# endif +# endif +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/dsaerr.h b/example/android/third_party/crypto/arm/include/openssl/dsaerr.h new file mode 100644 index 00000000..d94f97bb --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/dsaerr.h @@ -0,0 +1,67 @@ +/* + * Generated by util/mkerr.pl DO NOT EDIT + * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_DSAERR_H +# define HEADER_DSAERR_H + +# include + +# ifndef OPENSSL_NO_DSA + +# ifdef __cplusplus +extern "C" +# endif +int ERR_load_DSA_strings(void); + +/* + * DSA function codes. + */ +# define DSA_F_DSAPARAMS_PRINT 100 +# define DSA_F_DSAPARAMS_PRINT_FP 101 +# define DSA_F_DSA_BUILTIN_PARAMGEN 125 +# define DSA_F_DSA_BUILTIN_PARAMGEN2 126 +# define DSA_F_DSA_DO_SIGN 112 +# define DSA_F_DSA_DO_VERIFY 113 +# define DSA_F_DSA_METH_DUP 127 +# define DSA_F_DSA_METH_NEW 128 +# define DSA_F_DSA_METH_SET1_NAME 129 +# define DSA_F_DSA_NEW_METHOD 103 +# define DSA_F_DSA_PARAM_DECODE 119 +# define DSA_F_DSA_PRINT_FP 105 +# define DSA_F_DSA_PRIV_DECODE 115 +# define DSA_F_DSA_PRIV_ENCODE 116 +# define DSA_F_DSA_PUB_DECODE 117 +# define DSA_F_DSA_PUB_ENCODE 118 +# define DSA_F_DSA_SIGN 106 +# define DSA_F_DSA_SIGN_SETUP 107 +# define DSA_F_DSA_SIG_NEW 102 +# define DSA_F_OLD_DSA_PRIV_DECODE 122 +# define DSA_F_PKEY_DSA_CTRL 120 +# define DSA_F_PKEY_DSA_CTRL_STR 104 +# define DSA_F_PKEY_DSA_KEYGEN 121 + +/* + * DSA reason codes. + */ +# define DSA_R_BAD_Q_VALUE 102 +# define DSA_R_BN_DECODE_ERROR 108 +# define DSA_R_BN_ERROR 109 +# define DSA_R_DECODE_ERROR 104 +# define DSA_R_INVALID_DIGEST_TYPE 106 +# define DSA_R_INVALID_PARAMETERS 112 +# define DSA_R_MISSING_PARAMETERS 101 +# define DSA_R_MODULUS_TOO_LARGE 103 +# define DSA_R_NO_PARAMETERS_SET 107 +# define DSA_R_PARAMETER_ENCODING_ERROR 105 +# define DSA_R_Q_NOT_PRIME 113 +# define DSA_R_SEED_LEN_SMALL 110 + +# endif +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/dtls1.h b/example/android/third_party/crypto/arm/include/openssl/dtls1.h new file mode 100644 index 00000000..a312e386 --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/dtls1.h @@ -0,0 +1,55 @@ +/* + * Copyright 2005-2018 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_DTLS1_H +# define HEADER_DTLS1_H + +#ifdef __cplusplus +extern "C" { +#endif + +# define DTLS1_VERSION 0xFEFF +# define DTLS1_2_VERSION 0xFEFD +# define DTLS_MIN_VERSION DTLS1_VERSION +# define DTLS_MAX_VERSION DTLS1_2_VERSION +# define DTLS1_VERSION_MAJOR 0xFE + +# define DTLS1_BAD_VER 0x0100 + +/* Special value for method supporting multiple versions */ +# define DTLS_ANY_VERSION 0x1FFFF + +/* lengths of messages */ +/* + * Actually the max cookie length in DTLS is 255. But we can't change this now + * due to compatibility concerns. + */ +# define DTLS1_COOKIE_LENGTH 256 + +# define DTLS1_RT_HEADER_LENGTH 13 + +# define DTLS1_HM_HEADER_LENGTH 12 + +# define DTLS1_HM_BAD_FRAGMENT -2 +# define DTLS1_HM_FRAGMENT_RETRY -3 + +# define DTLS1_CCS_HEADER_LENGTH 1 + +# define DTLS1_AL_HEADER_LENGTH 2 + +/* Timeout multipliers (timeout slice is defined in apps/timeouts.h */ +# define DTLS1_TMO_READ_COUNT 2 +# define DTLS1_TMO_WRITE_COUNT 2 + +# define DTLS1_TMO_ALERT_COUNT 12 + +#ifdef __cplusplus +} +#endif +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/e_os2.h b/example/android/third_party/crypto/arm/include/openssl/e_os2.h new file mode 100644 index 00000000..eeae2154 --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/e_os2.h @@ -0,0 +1,293 @@ +/* + * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_E_OS2_H +# define HEADER_E_OS2_H + +# include + +#ifdef __cplusplus +extern "C" { +#endif + +/****************************************************************************** + * Detect operating systems. This probably needs completing. + * The result is that at least one OPENSSL_SYS_os macro should be defined. + * However, if none is defined, Unix is assumed. + **/ + +# define OPENSSL_SYS_UNIX + +/* --------------------- Microsoft operating systems ---------------------- */ + +/* + * Note that MSDOS actually denotes 32-bit environments running on top of + * MS-DOS, such as DJGPP one. + */ +# if defined(OPENSSL_SYS_MSDOS) +# undef OPENSSL_SYS_UNIX +# endif + +/* + * For 32 bit environment, there seems to be the CygWin environment and then + * all the others that try to do the same thing Microsoft does... + */ +/* + * UEFI lives here because it might be built with a Microsoft toolchain and + * we need to avoid the false positive match on Windows. + */ +# if defined(OPENSSL_SYS_UEFI) +# undef OPENSSL_SYS_UNIX +# elif defined(OPENSSL_SYS_UWIN) +# undef OPENSSL_SYS_UNIX +# define OPENSSL_SYS_WIN32_UWIN +# else +# if defined(__CYGWIN__) || defined(OPENSSL_SYS_CYGWIN) +# define OPENSSL_SYS_WIN32_CYGWIN +# else +# if defined(_WIN32) || defined(OPENSSL_SYS_WIN32) +# undef OPENSSL_SYS_UNIX +# if !defined(OPENSSL_SYS_WIN32) +# define OPENSSL_SYS_WIN32 +# endif +# endif +# if defined(_WIN64) || defined(OPENSSL_SYS_WIN64) +# undef OPENSSL_SYS_UNIX +# if !defined(OPENSSL_SYS_WIN64) +# define OPENSSL_SYS_WIN64 +# endif +# endif +# if defined(OPENSSL_SYS_WINNT) +# undef OPENSSL_SYS_UNIX +# endif +# if defined(OPENSSL_SYS_WINCE) +# undef OPENSSL_SYS_UNIX +# endif +# endif +# endif + +/* Anything that tries to look like Microsoft is "Windows" */ +# if defined(OPENSSL_SYS_WIN32) || defined(OPENSSL_SYS_WIN64) || defined(OPENSSL_SYS_WINNT) || defined(OPENSSL_SYS_WINCE) +# undef OPENSSL_SYS_UNIX +# define OPENSSL_SYS_WINDOWS +# ifndef OPENSSL_SYS_MSDOS +# define OPENSSL_SYS_MSDOS +# endif +# endif + +/* + * DLL settings. This part is a bit tough, because it's up to the + * application implementor how he or she will link the application, so it + * requires some macro to be used. + */ +# ifdef OPENSSL_SYS_WINDOWS +# ifndef OPENSSL_OPT_WINDLL +# if defined(_WINDLL) /* This is used when building OpenSSL to + * indicate that DLL linkage should be used */ +# define OPENSSL_OPT_WINDLL +# endif +# endif +# endif + +/* ------------------------------- OpenVMS -------------------------------- */ +# if defined(__VMS) || defined(VMS) || defined(OPENSSL_SYS_VMS) +# if !defined(OPENSSL_SYS_VMS) +# undef OPENSSL_SYS_UNIX +# endif +# define OPENSSL_SYS_VMS +# if defined(__DECC) +# define OPENSSL_SYS_VMS_DECC +# elif defined(__DECCXX) +# define OPENSSL_SYS_VMS_DECC +# define OPENSSL_SYS_VMS_DECCXX +# else +# define OPENSSL_SYS_VMS_NODECC +# endif +# endif + +/* -------------------------------- Unix ---------------------------------- */ +# ifdef OPENSSL_SYS_UNIX +# if defined(linux) || defined(__linux__) && !defined(OPENSSL_SYS_LINUX) +# define OPENSSL_SYS_LINUX +# endif +# if defined(_AIX) && !defined(OPENSSL_SYS_AIX) +# define OPENSSL_SYS_AIX +# endif +# endif + +/* -------------------------------- VOS ----------------------------------- */ +# if defined(__VOS__) && !defined(OPENSSL_SYS_VOS) +# define OPENSSL_SYS_VOS +# ifdef __HPPA__ +# define OPENSSL_SYS_VOS_HPPA +# endif +# ifdef __IA32__ +# define OPENSSL_SYS_VOS_IA32 +# endif +# endif + +/** + * That's it for OS-specific stuff + *****************************************************************************/ + +/* Specials for I/O an exit */ +# ifdef OPENSSL_SYS_MSDOS +# define OPENSSL_UNISTD_IO +# define OPENSSL_DECLARE_EXIT extern void exit(int); +# else +# define OPENSSL_UNISTD_IO OPENSSL_UNISTD +# define OPENSSL_DECLARE_EXIT /* declared in unistd.h */ +# endif + +/*- + * OPENSSL_EXTERN is normally used to declare a symbol with possible extra + * attributes to handle its presence in a shared library. + * OPENSSL_EXPORT is used to define a symbol with extra possible attributes + * to make it visible in a shared library. + * Care needs to be taken when a header file is used both to declare and + * define symbols. Basically, for any library that exports some global + * variables, the following code must be present in the header file that + * declares them, before OPENSSL_EXTERN is used: + * + * #ifdef SOME_BUILD_FLAG_MACRO + * # undef OPENSSL_EXTERN + * # define OPENSSL_EXTERN OPENSSL_EXPORT + * #endif + * + * The default is to have OPENSSL_EXPORT and OPENSSL_EXTERN + * have some generally sensible values. + */ + +# if defined(OPENSSL_SYS_WINDOWS) && defined(OPENSSL_OPT_WINDLL) +# define OPENSSL_EXPORT extern __declspec(dllexport) +# define OPENSSL_EXTERN extern __declspec(dllimport) +# else +# define OPENSSL_EXPORT extern +# define OPENSSL_EXTERN extern +# endif + +/*- + * Macros to allow global variables to be reached through function calls when + * required (if a shared library version requires it, for example. + * The way it's done allows definitions like this: + * + * // in foobar.c + * OPENSSL_IMPLEMENT_GLOBAL(int,foobar,0) + * // in foobar.h + * OPENSSL_DECLARE_GLOBAL(int,foobar); + * #define foobar OPENSSL_GLOBAL_REF(foobar) + */ +# ifdef OPENSSL_EXPORT_VAR_AS_FUNCTION +# define OPENSSL_IMPLEMENT_GLOBAL(type,name,value) \ + type *_shadow_##name(void) \ + { static type _hide_##name=value; return &_hide_##name; } +# define OPENSSL_DECLARE_GLOBAL(type,name) type *_shadow_##name(void) +# define OPENSSL_GLOBAL_REF(name) (*(_shadow_##name())) +# else +# define OPENSSL_IMPLEMENT_GLOBAL(type,name,value) type _shadow_##name=value; +# define OPENSSL_DECLARE_GLOBAL(type,name) OPENSSL_EXPORT type _shadow_##name +# define OPENSSL_GLOBAL_REF(name) _shadow_##name +# endif + +# ifdef _WIN32 +# ifdef _WIN64 +# define ossl_ssize_t __int64 +# define OSSL_SSIZE_MAX _I64_MAX +# else +# define ossl_ssize_t int +# define OSSL_SSIZE_MAX INT_MAX +# endif +# endif + +# if defined(OPENSSL_SYS_UEFI) && !defined(ossl_ssize_t) +# define ossl_ssize_t INTN +# define OSSL_SSIZE_MAX MAX_INTN +# endif + +# ifndef ossl_ssize_t +# define ossl_ssize_t ssize_t +# if defined(SSIZE_MAX) +# define OSSL_SSIZE_MAX SSIZE_MAX +# elif defined(_POSIX_SSIZE_MAX) +# define OSSL_SSIZE_MAX _POSIX_SSIZE_MAX +# else +# define OSSL_SSIZE_MAX ((ssize_t)(SIZE_MAX>>1)) +# endif +# endif + +# ifdef DEBUG_UNUSED +# define __owur __attribute__((__warn_unused_result__)) +# else +# define __owur +# endif + +/* Standard integer types */ +# if defined(OPENSSL_SYS_UEFI) +typedef INT8 int8_t; +typedef UINT8 uint8_t; +typedef INT16 int16_t; +typedef UINT16 uint16_t; +typedef INT32 int32_t; +typedef UINT32 uint32_t; +typedef INT64 int64_t; +typedef UINT64 uint64_t; +# elif (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || \ + defined(__osf__) || defined(__sgi) || defined(__hpux) || \ + defined(OPENSSL_SYS_VMS) || defined (__OpenBSD__) +# include +# elif defined(_MSC_VER) && _MSC_VER<=1500 +/* + * minimally required typdefs for systems not supporting inttypes.h or + * stdint.h: currently just older VC++ + */ +typedef signed char int8_t; +typedef unsigned char uint8_t; +typedef short int16_t; +typedef unsigned short uint16_t; +typedef int int32_t; +typedef unsigned int uint32_t; +typedef __int64 int64_t; +typedef unsigned __int64 uint64_t; +# else +# include +# endif + +/* ossl_inline: portable inline definition usable in public headers */ +# if !defined(inline) && !defined(__cplusplus) +# if defined(__STDC_VERSION__) && __STDC_VERSION__>=199901L + /* just use inline */ +# define ossl_inline inline +# elif defined(__GNUC__) && __GNUC__>=2 +# define ossl_inline __inline__ +# elif defined(_MSC_VER) + /* + * Visual Studio: inline is available in C++ only, however + * __inline is available for C, see + * http://msdn.microsoft.com/en-us/library/z8y1yy88.aspx + */ +# define ossl_inline __inline +# else +# define ossl_inline +# endif +# else +# define ossl_inline inline +# endif + +# if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L +# define ossl_noreturn _Noreturn +# elif defined(__GNUC__) && __GNUC__ >= 2 +# define ossl_noreturn __attribute__((noreturn)) +# else +# define ossl_noreturn +# endif + +#ifdef __cplusplus +} +#endif +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/ebcdic.h b/example/android/third_party/crypto/arm/include/openssl/ebcdic.h new file mode 100644 index 00000000..aa012855 --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/ebcdic.h @@ -0,0 +1,33 @@ +/* + * Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_EBCDIC_H +# define HEADER_EBCDIC_H + +# include + +#ifdef __cplusplus +extern "C" { +#endif + +/* Avoid name clashes with other applications */ +# define os_toascii _openssl_os_toascii +# define os_toebcdic _openssl_os_toebcdic +# define ebcdic2ascii _openssl_ebcdic2ascii +# define ascii2ebcdic _openssl_ascii2ebcdic + +extern const unsigned char os_toascii[256]; +extern const unsigned char os_toebcdic[256]; +void *ebcdic2ascii(void *dest, const void *srce, size_t count); +void *ascii2ebcdic(void *dest, const void *srce, size_t count); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/ec.h b/example/android/third_party/crypto/arm/include/openssl/ec.h new file mode 100644 index 00000000..347cfb6d --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/ec.h @@ -0,0 +1,1478 @@ +/* + * Copyright 2002-2018 The OpenSSL Project Authors. All Rights Reserved. + * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_EC_H +# define HEADER_EC_H + +# include + +# ifndef OPENSSL_NO_EC +# include +# include +# if OPENSSL_API_COMPAT < 0x10100000L +# include +# endif +# include +# ifdef __cplusplus +extern "C" { +# endif + +# ifndef OPENSSL_ECC_MAX_FIELD_BITS +# define OPENSSL_ECC_MAX_FIELD_BITS 661 +# endif + +/** Enum for the point conversion form as defined in X9.62 (ECDSA) + * for the encoding of a elliptic curve point (x,y) */ +typedef enum { + /** the point is encoded as z||x, where the octet z specifies + * which solution of the quadratic equation y is */ + POINT_CONVERSION_COMPRESSED = 2, + /** the point is encoded as z||x||y, where z is the octet 0x04 */ + POINT_CONVERSION_UNCOMPRESSED = 4, + /** the point is encoded as z||x||y, where the octet z specifies + * which solution of the quadratic equation y is */ + POINT_CONVERSION_HYBRID = 6 +} point_conversion_form_t; + +typedef struct ec_method_st EC_METHOD; +typedef struct ec_group_st EC_GROUP; +typedef struct ec_point_st EC_POINT; +typedef struct ecpk_parameters_st ECPKPARAMETERS; +typedef struct ec_parameters_st ECPARAMETERS; + +/********************************************************************/ +/* EC_METHODs for curves over GF(p) */ +/********************************************************************/ + +/** Returns the basic GFp ec methods which provides the basis for the + * optimized methods. + * \return EC_METHOD object + */ +const EC_METHOD *EC_GFp_simple_method(void); + +/** Returns GFp methods using montgomery multiplication. + * \return EC_METHOD object + */ +const EC_METHOD *EC_GFp_mont_method(void); + +/** Returns GFp methods using optimized methods for NIST recommended curves + * \return EC_METHOD object + */ +const EC_METHOD *EC_GFp_nist_method(void); + +# ifndef OPENSSL_NO_EC_NISTP_64_GCC_128 +/** Returns 64-bit optimized methods for nistp224 + * \return EC_METHOD object + */ +const EC_METHOD *EC_GFp_nistp224_method(void); + +/** Returns 64-bit optimized methods for nistp256 + * \return EC_METHOD object + */ +const EC_METHOD *EC_GFp_nistp256_method(void); + +/** Returns 64-bit optimized methods for nistp521 + * \return EC_METHOD object + */ +const EC_METHOD *EC_GFp_nistp521_method(void); +# endif + +# ifndef OPENSSL_NO_EC2M +/********************************************************************/ +/* EC_METHOD for curves over GF(2^m) */ +/********************************************************************/ + +/** Returns the basic GF2m ec method + * \return EC_METHOD object + */ +const EC_METHOD *EC_GF2m_simple_method(void); + +# endif + +/********************************************************************/ +/* EC_GROUP functions */ +/********************************************************************/ + +/** Creates a new EC_GROUP object + * \param meth EC_METHOD to use + * \return newly created EC_GROUP object or NULL in case of an error. + */ +EC_GROUP *EC_GROUP_new(const EC_METHOD *meth); + +/** Frees a EC_GROUP object + * \param group EC_GROUP object to be freed. + */ +void EC_GROUP_free(EC_GROUP *group); + +/** Clears and frees a EC_GROUP object + * \param group EC_GROUP object to be cleared and freed. + */ +void EC_GROUP_clear_free(EC_GROUP *group); + +/** Copies EC_GROUP objects. Note: both EC_GROUPs must use the same EC_METHOD. + * \param dst destination EC_GROUP object + * \param src source EC_GROUP object + * \return 1 on success and 0 if an error occurred. + */ +int EC_GROUP_copy(EC_GROUP *dst, const EC_GROUP *src); + +/** Creates a new EC_GROUP object and copies the copies the content + * form src to the newly created EC_KEY object + * \param src source EC_GROUP object + * \return newly created EC_GROUP object or NULL in case of an error. + */ +EC_GROUP *EC_GROUP_dup(const EC_GROUP *src); + +/** Returns the EC_METHOD of the EC_GROUP object. + * \param group EC_GROUP object + * \return EC_METHOD used in this EC_GROUP object. + */ +const EC_METHOD *EC_GROUP_method_of(const EC_GROUP *group); + +/** Returns the field type of the EC_METHOD. + * \param meth EC_METHOD object + * \return NID of the underlying field type OID. + */ +int EC_METHOD_get_field_type(const EC_METHOD *meth); + +/** Sets the generator and it's order/cofactor of a EC_GROUP object. + * \param group EC_GROUP object + * \param generator EC_POINT object with the generator. + * \param order the order of the group generated by the generator. + * \param cofactor the index of the sub-group generated by the generator + * in the group of all points on the elliptic curve. + * \return 1 on success and 0 if an error occurred + */ +int EC_GROUP_set_generator(EC_GROUP *group, const EC_POINT *generator, + const BIGNUM *order, const BIGNUM *cofactor); + +/** Returns the generator of a EC_GROUP object. + * \param group EC_GROUP object + * \return the currently used generator (possibly NULL). + */ +const EC_POINT *EC_GROUP_get0_generator(const EC_GROUP *group); + +/** Returns the montgomery data for order(Generator) + * \param group EC_GROUP object + * \return the currently used montgomery data (possibly NULL). +*/ +BN_MONT_CTX *EC_GROUP_get_mont_data(const EC_GROUP *group); + +/** Gets the order of a EC_GROUP + * \param group EC_GROUP object + * \param order BIGNUM to which the order is copied + * \param ctx unused + * \return 1 on success and 0 if an error occurred + */ +int EC_GROUP_get_order(const EC_GROUP *group, BIGNUM *order, BN_CTX *ctx); + +/** Gets the order of an EC_GROUP + * \param group EC_GROUP object + * \return the group order + */ +const BIGNUM *EC_GROUP_get0_order(const EC_GROUP *group); + +/** Gets the number of bits of the order of an EC_GROUP + * \param group EC_GROUP object + * \return number of bits of group order. + */ +int EC_GROUP_order_bits(const EC_GROUP *group); + +/** Gets the cofactor of a EC_GROUP + * \param group EC_GROUP object + * \param cofactor BIGNUM to which the cofactor is copied + * \param ctx unused + * \return 1 on success and 0 if an error occurred + */ +int EC_GROUP_get_cofactor(const EC_GROUP *group, BIGNUM *cofactor, + BN_CTX *ctx); + +/** Gets the cofactor of an EC_GROUP + * \param group EC_GROUP object + * \return the group cofactor + */ +const BIGNUM *EC_GROUP_get0_cofactor(const EC_GROUP *group); + +/** Sets the name of a EC_GROUP object + * \param group EC_GROUP object + * \param nid NID of the curve name OID + */ +void EC_GROUP_set_curve_name(EC_GROUP *group, int nid); + +/** Returns the curve name of a EC_GROUP object + * \param group EC_GROUP object + * \return NID of the curve name OID or 0 if not set. + */ +int EC_GROUP_get_curve_name(const EC_GROUP *group); + +void EC_GROUP_set_asn1_flag(EC_GROUP *group, int flag); +int EC_GROUP_get_asn1_flag(const EC_GROUP *group); + +void EC_GROUP_set_point_conversion_form(EC_GROUP *group, + point_conversion_form_t form); +point_conversion_form_t EC_GROUP_get_point_conversion_form(const EC_GROUP *); + +unsigned char *EC_GROUP_get0_seed(const EC_GROUP *x); +size_t EC_GROUP_get_seed_len(const EC_GROUP *); +size_t EC_GROUP_set_seed(EC_GROUP *, const unsigned char *, size_t len); + +/** Sets the parameters of a ec curve defined by y^2 = x^3 + a*x + b (for GFp) + * or y^2 + x*y = x^3 + a*x^2 + b (for GF2m) + * \param group EC_GROUP object + * \param p BIGNUM with the prime number (GFp) or the polynomial + * defining the underlying field (GF2m) + * \param a BIGNUM with parameter a of the equation + * \param b BIGNUM with parameter b of the equation + * \param ctx BN_CTX object (optional) + * \return 1 on success and 0 if an error occurred + */ +int EC_GROUP_set_curve(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a, + const BIGNUM *b, BN_CTX *ctx); + +/** Gets the parameters of the ec curve defined by y^2 = x^3 + a*x + b (for GFp) + * or y^2 + x*y = x^3 + a*x^2 + b (for GF2m) + * \param group EC_GROUP object + * \param p BIGNUM with the prime number (GFp) or the polynomial + * defining the underlying field (GF2m) + * \param a BIGNUM for parameter a of the equation + * \param b BIGNUM for parameter b of the equation + * \param ctx BN_CTX object (optional) + * \return 1 on success and 0 if an error occurred + */ +int EC_GROUP_get_curve(const EC_GROUP *group, BIGNUM *p, BIGNUM *a, BIGNUM *b, + BN_CTX *ctx); + +/** Sets the parameters of an ec curve. Synonym for EC_GROUP_set_curve + * \param group EC_GROUP object + * \param p BIGNUM with the prime number (GFp) or the polynomial + * defining the underlying field (GF2m) + * \param a BIGNUM with parameter a of the equation + * \param b BIGNUM with parameter b of the equation + * \param ctx BN_CTX object (optional) + * \return 1 on success and 0 if an error occurred + */ +DEPRECATEDIN_1_2_0(int EC_GROUP_set_curve_GFp(EC_GROUP *group, const BIGNUM *p, + const BIGNUM *a, const BIGNUM *b, + BN_CTX *ctx)) + +/** Gets the parameters of an ec curve. Synonym for EC_GROUP_get_curve + * \param group EC_GROUP object + * \param p BIGNUM with the prime number (GFp) or the polynomial + * defining the underlying field (GF2m) + * \param a BIGNUM for parameter a of the equation + * \param b BIGNUM for parameter b of the equation + * \param ctx BN_CTX object (optional) + * \return 1 on success and 0 if an error occurred + */ +DEPRECATEDIN_1_2_0(int EC_GROUP_get_curve_GFp(const EC_GROUP *group, BIGNUM *p, + BIGNUM *a, BIGNUM *b, + BN_CTX *ctx)) + +# ifndef OPENSSL_NO_EC2M +/** Sets the parameter of an ec curve. Synonym for EC_GROUP_set_curve + * \param group EC_GROUP object + * \param p BIGNUM with the prime number (GFp) or the polynomial + * defining the underlying field (GF2m) + * \param a BIGNUM with parameter a of the equation + * \param b BIGNUM with parameter b of the equation + * \param ctx BN_CTX object (optional) + * \return 1 on success and 0 if an error occurred + */ +DEPRECATEDIN_1_2_0(int EC_GROUP_set_curve_GF2m(EC_GROUP *group, const BIGNUM *p, + const BIGNUM *a, const BIGNUM *b, + BN_CTX *ctx)) + +/** Gets the parameters of an ec curve. Synonym for EC_GROUP_get_curve + * \param group EC_GROUP object + * \param p BIGNUM with the prime number (GFp) or the polynomial + * defining the underlying field (GF2m) + * \param a BIGNUM for parameter a of the equation + * \param b BIGNUM for parameter b of the equation + * \param ctx BN_CTX object (optional) + * \return 1 on success and 0 if an error occurred + */ +DEPRECATEDIN_1_2_0(int EC_GROUP_get_curve_GF2m(const EC_GROUP *group, BIGNUM *p, + BIGNUM *a, BIGNUM *b, + BN_CTX *ctx)) +# endif +/** Returns the number of bits needed to represent a field element + * \param group EC_GROUP object + * \return number of bits needed to represent a field element + */ +int EC_GROUP_get_degree(const EC_GROUP *group); + +/** Checks whether the parameter in the EC_GROUP define a valid ec group + * \param group EC_GROUP object + * \param ctx BN_CTX object (optional) + * \return 1 if group is a valid ec group and 0 otherwise + */ +int EC_GROUP_check(const EC_GROUP *group, BN_CTX *ctx); + +/** Checks whether the discriminant of the elliptic curve is zero or not + * \param group EC_GROUP object + * \param ctx BN_CTX object (optional) + * \return 1 if the discriminant is not zero and 0 otherwise + */ +int EC_GROUP_check_discriminant(const EC_GROUP *group, BN_CTX *ctx); + +/** Compares two EC_GROUP objects + * \param a first EC_GROUP object + * \param b second EC_GROUP object + * \param ctx BN_CTX object (optional) + * \return 0 if the groups are equal, 1 if not, or -1 on error + */ +int EC_GROUP_cmp(const EC_GROUP *a, const EC_GROUP *b, BN_CTX *ctx); + +/* + * EC_GROUP_new_GF*() calls EC_GROUP_new() and EC_GROUP_set_GF*() after + * choosing an appropriate EC_METHOD + */ + +/** Creates a new EC_GROUP object with the specified parameters defined + * over GFp (defined by the equation y^2 = x^3 + a*x + b) + * \param p BIGNUM with the prime number + * \param a BIGNUM with the parameter a of the equation + * \param b BIGNUM with the parameter b of the equation + * \param ctx BN_CTX object (optional) + * \return newly created EC_GROUP object with the specified parameters + */ +EC_GROUP *EC_GROUP_new_curve_GFp(const BIGNUM *p, const BIGNUM *a, + const BIGNUM *b, BN_CTX *ctx); +# ifndef OPENSSL_NO_EC2M +/** Creates a new EC_GROUP object with the specified parameters defined + * over GF2m (defined by the equation y^2 + x*y = x^3 + a*x^2 + b) + * \param p BIGNUM with the polynomial defining the underlying field + * \param a BIGNUM with the parameter a of the equation + * \param b BIGNUM with the parameter b of the equation + * \param ctx BN_CTX object (optional) + * \return newly created EC_GROUP object with the specified parameters + */ +EC_GROUP *EC_GROUP_new_curve_GF2m(const BIGNUM *p, const BIGNUM *a, + const BIGNUM *b, BN_CTX *ctx); +# endif + +/** Creates a EC_GROUP object with a curve specified by a NID + * \param nid NID of the OID of the curve name + * \return newly created EC_GROUP object with specified curve or NULL + * if an error occurred + */ +EC_GROUP *EC_GROUP_new_by_curve_name(int nid); + +/** Creates a new EC_GROUP object from an ECPARAMETERS object + * \param params pointer to the ECPARAMETERS object + * \return newly created EC_GROUP object with specified curve or NULL + * if an error occurred + */ +EC_GROUP *EC_GROUP_new_from_ecparameters(const ECPARAMETERS *params); + +/** Creates an ECPARAMETERS object for the given EC_GROUP object. + * \param group pointer to the EC_GROUP object + * \param params pointer to an existing ECPARAMETERS object or NULL + * \return pointer to the new ECPARAMETERS object or NULL + * if an error occurred. + */ +ECPARAMETERS *EC_GROUP_get_ecparameters(const EC_GROUP *group, + ECPARAMETERS *params); + +/** Creates a new EC_GROUP object from an ECPKPARAMETERS object + * \param params pointer to an existing ECPKPARAMETERS object, or NULL + * \return newly created EC_GROUP object with specified curve, or NULL + * if an error occurred + */ +EC_GROUP *EC_GROUP_new_from_ecpkparameters(const ECPKPARAMETERS *params); + +/** Creates an ECPKPARAMETERS object for the given EC_GROUP object. + * \param group pointer to the EC_GROUP object + * \param params pointer to an existing ECPKPARAMETERS object or NULL + * \return pointer to the new ECPKPARAMETERS object or NULL + * if an error occurred. + */ +ECPKPARAMETERS *EC_GROUP_get_ecpkparameters(const EC_GROUP *group, + ECPKPARAMETERS *params); + +/********************************************************************/ +/* handling of internal curves */ +/********************************************************************/ + +typedef struct { + int nid; + const char *comment; +} EC_builtin_curve; + +/* + * EC_builtin_curves(EC_builtin_curve *r, size_t size) returns number of all + * available curves or zero if a error occurred. In case r is not zero, + * nitems EC_builtin_curve structures are filled with the data of the first + * nitems internal groups + */ +size_t EC_get_builtin_curves(EC_builtin_curve *r, size_t nitems); + +const char *EC_curve_nid2nist(int nid); +int EC_curve_nist2nid(const char *name); + +/********************************************************************/ +/* EC_POINT functions */ +/********************************************************************/ + +/** Creates a new EC_POINT object for the specified EC_GROUP + * \param group EC_GROUP the underlying EC_GROUP object + * \return newly created EC_POINT object or NULL if an error occurred + */ +EC_POINT *EC_POINT_new(const EC_GROUP *group); + +/** Frees a EC_POINT object + * \param point EC_POINT object to be freed + */ +void EC_POINT_free(EC_POINT *point); + +/** Clears and frees a EC_POINT object + * \param point EC_POINT object to be cleared and freed + */ +void EC_POINT_clear_free(EC_POINT *point); + +/** Copies EC_POINT object + * \param dst destination EC_POINT object + * \param src source EC_POINT object + * \return 1 on success and 0 if an error occurred + */ +int EC_POINT_copy(EC_POINT *dst, const EC_POINT *src); + +/** Creates a new EC_POINT object and copies the content of the supplied + * EC_POINT + * \param src source EC_POINT object + * \param group underlying the EC_GROUP object + * \return newly created EC_POINT object or NULL if an error occurred + */ +EC_POINT *EC_POINT_dup(const EC_POINT *src, const EC_GROUP *group); + +/** Returns the EC_METHOD used in EC_POINT object + * \param point EC_POINT object + * \return the EC_METHOD used + */ +const EC_METHOD *EC_POINT_method_of(const EC_POINT *point); + +/** Sets a point to infinity (neutral element) + * \param group underlying EC_GROUP object + * \param point EC_POINT to set to infinity + * \return 1 on success and 0 if an error occurred + */ +int EC_POINT_set_to_infinity(const EC_GROUP *group, EC_POINT *point); + +/** Sets the jacobian projective coordinates of a EC_POINT over GFp + * \param group underlying EC_GROUP object + * \param p EC_POINT object + * \param x BIGNUM with the x-coordinate + * \param y BIGNUM with the y-coordinate + * \param z BIGNUM with the z-coordinate + * \param ctx BN_CTX object (optional) + * \return 1 on success and 0 if an error occurred + */ +int EC_POINT_set_Jprojective_coordinates_GFp(const EC_GROUP *group, + EC_POINT *p, const BIGNUM *x, + const BIGNUM *y, const BIGNUM *z, + BN_CTX *ctx); + +/** Gets the jacobian projective coordinates of a EC_POINT over GFp + * \param group underlying EC_GROUP object + * \param p EC_POINT object + * \param x BIGNUM for the x-coordinate + * \param y BIGNUM for the y-coordinate + * \param z BIGNUM for the z-coordinate + * \param ctx BN_CTX object (optional) + * \return 1 on success and 0 if an error occurred + */ +int EC_POINT_get_Jprojective_coordinates_GFp(const EC_GROUP *group, + const EC_POINT *p, BIGNUM *x, + BIGNUM *y, BIGNUM *z, + BN_CTX *ctx); + +/** Sets the affine coordinates of an EC_POINT + * \param group underlying EC_GROUP object + * \param p EC_POINT object + * \param x BIGNUM with the x-coordinate + * \param y BIGNUM with the y-coordinate + * \param ctx BN_CTX object (optional) + * \return 1 on success and 0 if an error occurred + */ +int EC_POINT_set_affine_coordinates(const EC_GROUP *group, EC_POINT *p, + const BIGNUM *x, const BIGNUM *y, + BN_CTX *ctx); + +/** Gets the affine coordinates of an EC_POINT. + * \param group underlying EC_GROUP object + * \param p EC_POINT object + * \param x BIGNUM for the x-coordinate + * \param y BIGNUM for the y-coordinate + * \param ctx BN_CTX object (optional) + * \return 1 on success and 0 if an error occurred + */ +int EC_POINT_get_affine_coordinates(const EC_GROUP *group, const EC_POINT *p, + BIGNUM *x, BIGNUM *y, BN_CTX *ctx); + +/** Sets the affine coordinates of an EC_POINT. A synonym of + * EC_POINT_set_affine_coordinates + * \param group underlying EC_GROUP object + * \param p EC_POINT object + * \param x BIGNUM with the x-coordinate + * \param y BIGNUM with the y-coordinate + * \param ctx BN_CTX object (optional) + * \return 1 on success and 0 if an error occurred + */ +DEPRECATEDIN_1_2_0(int EC_POINT_set_affine_coordinates_GFp(const EC_GROUP *group, + EC_POINT *p, + const BIGNUM *x, + const BIGNUM *y, + BN_CTX *ctx)) + +/** Gets the affine coordinates of an EC_POINT. A synonym of + * EC_POINT_get_affine_coordinates + * \param group underlying EC_GROUP object + * \param p EC_POINT object + * \param x BIGNUM for the x-coordinate + * \param y BIGNUM for the y-coordinate + * \param ctx BN_CTX object (optional) + * \return 1 on success and 0 if an error occurred + */ +DEPRECATEDIN_1_2_0(int EC_POINT_get_affine_coordinates_GFp(const EC_GROUP *group, + const EC_POINT *p, + BIGNUM *x, + BIGNUM *y, + BN_CTX *ctx)) + +/** Sets the x9.62 compressed coordinates of a EC_POINT + * \param group underlying EC_GROUP object + * \param p EC_POINT object + * \param x BIGNUM with x-coordinate + * \param y_bit integer with the y-Bit (either 0 or 1) + * \param ctx BN_CTX object (optional) + * \return 1 on success and 0 if an error occurred + */ +int EC_POINT_set_compressed_coordinates(const EC_GROUP *group, EC_POINT *p, + const BIGNUM *x, int y_bit, + BN_CTX *ctx); + +/** Sets the x9.62 compressed coordinates of a EC_POINT. A synonym of + * EC_POINT_set_compressed_coordinates + * \param group underlying EC_GROUP object + * \param p EC_POINT object + * \param x BIGNUM with x-coordinate + * \param y_bit integer with the y-Bit (either 0 or 1) + * \param ctx BN_CTX object (optional) + * \return 1 on success and 0 if an error occurred + */ +DEPRECATEDIN_1_2_0(int EC_POINT_set_compressed_coordinates_GFp(const EC_GROUP *group, + EC_POINT *p, + const BIGNUM *x, + int y_bit, + BN_CTX *ctx)) +# ifndef OPENSSL_NO_EC2M +/** Sets the affine coordinates of an EC_POINT. A synonym of + * EC_POINT_set_affine_coordinates + * \param group underlying EC_GROUP object + * \param p EC_POINT object + * \param x BIGNUM with the x-coordinate + * \param y BIGNUM with the y-coordinate + * \param ctx BN_CTX object (optional) + * \return 1 on success and 0 if an error occurred + */ +DEPRECATEDIN_1_2_0(int EC_POINT_set_affine_coordinates_GF2m(const EC_GROUP *group, + EC_POINT *p, + const BIGNUM *x, + const BIGNUM *y, + BN_CTX *ctx)) + +/** Gets the affine coordinates of an EC_POINT. A synonym of + * EC_POINT_get_affine_coordinates + * \param group underlying EC_GROUP object + * \param p EC_POINT object + * \param x BIGNUM for the x-coordinate + * \param y BIGNUM for the y-coordinate + * \param ctx BN_CTX object (optional) + * \return 1 on success and 0 if an error occurred + */ +DEPRECATEDIN_1_2_0(int EC_POINT_get_affine_coordinates_GF2m(const EC_GROUP *group, + const EC_POINT *p, + BIGNUM *x, + BIGNUM *y, + BN_CTX *ctx)) + +/** Sets the x9.62 compressed coordinates of a EC_POINT. A synonym of + * EC_POINT_set_compressed_coordinates + * \param group underlying EC_GROUP object + * \param p EC_POINT object + * \param x BIGNUM with x-coordinate + * \param y_bit integer with the y-Bit (either 0 or 1) + * \param ctx BN_CTX object (optional) + * \return 1 on success and 0 if an error occurred + */ +DEPRECATEDIN_1_2_0(int EC_POINT_set_compressed_coordinates_GF2m(const EC_GROUP *group, + EC_POINT *p, + const BIGNUM *x, + int y_bit, + BN_CTX *ctx)) +# endif +/** Encodes a EC_POINT object to a octet string + * \param group underlying EC_GROUP object + * \param p EC_POINT object + * \param form point conversion form + * \param buf memory buffer for the result. If NULL the function returns + * required buffer size. + * \param len length of the memory buffer + * \param ctx BN_CTX object (optional) + * \return the length of the encoded octet string or 0 if an error occurred + */ +size_t EC_POINT_point2oct(const EC_GROUP *group, const EC_POINT *p, + point_conversion_form_t form, + unsigned char *buf, size_t len, BN_CTX *ctx); + +/** Decodes a EC_POINT from a octet string + * \param group underlying EC_GROUP object + * \param p EC_POINT object + * \param buf memory buffer with the encoded ec point + * \param len length of the encoded ec point + * \param ctx BN_CTX object (optional) + * \return 1 on success and 0 if an error occurred + */ +int EC_POINT_oct2point(const EC_GROUP *group, EC_POINT *p, + const unsigned char *buf, size_t len, BN_CTX *ctx); + +/** Encodes an EC_POINT object to an allocated octet string + * \param group underlying EC_GROUP object + * \param point EC_POINT object + * \param form point conversion form + * \param pbuf returns pointer to allocated buffer + * \param ctx BN_CTX object (optional) + * \return the length of the encoded octet string or 0 if an error occurred + */ +size_t EC_POINT_point2buf(const EC_GROUP *group, const EC_POINT *point, + point_conversion_form_t form, + unsigned char **pbuf, BN_CTX *ctx); + +/* other interfaces to point2oct/oct2point: */ +BIGNUM *EC_POINT_point2bn(const EC_GROUP *, const EC_POINT *, + point_conversion_form_t form, BIGNUM *, BN_CTX *); +EC_POINT *EC_POINT_bn2point(const EC_GROUP *, const BIGNUM *, + EC_POINT *, BN_CTX *); +char *EC_POINT_point2hex(const EC_GROUP *, const EC_POINT *, + point_conversion_form_t form, BN_CTX *); +EC_POINT *EC_POINT_hex2point(const EC_GROUP *, const char *, + EC_POINT *, BN_CTX *); + +/********************************************************************/ +/* functions for doing EC_POINT arithmetic */ +/********************************************************************/ + +/** Computes the sum of two EC_POINT + * \param group underlying EC_GROUP object + * \param r EC_POINT object for the result (r = a + b) + * \param a EC_POINT object with the first summand + * \param b EC_POINT object with the second summand + * \param ctx BN_CTX object (optional) + * \return 1 on success and 0 if an error occurred + */ +int EC_POINT_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a, + const EC_POINT *b, BN_CTX *ctx); + +/** Computes the double of a EC_POINT + * \param group underlying EC_GROUP object + * \param r EC_POINT object for the result (r = 2 * a) + * \param a EC_POINT object + * \param ctx BN_CTX object (optional) + * \return 1 on success and 0 if an error occurred + */ +int EC_POINT_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a, + BN_CTX *ctx); + +/** Computes the inverse of a EC_POINT + * \param group underlying EC_GROUP object + * \param a EC_POINT object to be inverted (it's used for the result as well) + * \param ctx BN_CTX object (optional) + * \return 1 on success and 0 if an error occurred + */ +int EC_POINT_invert(const EC_GROUP *group, EC_POINT *a, BN_CTX *ctx); + +/** Checks whether the point is the neutral element of the group + * \param group the underlying EC_GROUP object + * \param p EC_POINT object + * \return 1 if the point is the neutral element and 0 otherwise + */ +int EC_POINT_is_at_infinity(const EC_GROUP *group, const EC_POINT *p); + +/** Checks whether the point is on the curve + * \param group underlying EC_GROUP object + * \param point EC_POINT object to check + * \param ctx BN_CTX object (optional) + * \return 1 if the point is on the curve, 0 if not, or -1 on error + */ +int EC_POINT_is_on_curve(const EC_GROUP *group, const EC_POINT *point, + BN_CTX *ctx); + +/** Compares two EC_POINTs + * \param group underlying EC_GROUP object + * \param a first EC_POINT object + * \param b second EC_POINT object + * \param ctx BN_CTX object (optional) + * \return 1 if the points are not equal, 0 if they are, or -1 on error + */ +int EC_POINT_cmp(const EC_GROUP *group, const EC_POINT *a, const EC_POINT *b, + BN_CTX *ctx); + +int EC_POINT_make_affine(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx); +int EC_POINTs_make_affine(const EC_GROUP *group, size_t num, + EC_POINT *points[], BN_CTX *ctx); + +/** Computes r = generator * n + sum_{i=0}^{num-1} p[i] * m[i] + * \param group underlying EC_GROUP object + * \param r EC_POINT object for the result + * \param n BIGNUM with the multiplier for the group generator (optional) + * \param num number further summands + * \param p array of size num of EC_POINT objects + * \param m array of size num of BIGNUM objects + * \param ctx BN_CTX object (optional) + * \return 1 on success and 0 if an error occurred + */ +int EC_POINTs_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *n, + size_t num, const EC_POINT *p[], const BIGNUM *m[], + BN_CTX *ctx); + +/** Computes r = generator * n + q * m + * \param group underlying EC_GROUP object + * \param r EC_POINT object for the result + * \param n BIGNUM with the multiplier for the group generator (optional) + * \param q EC_POINT object with the first factor of the second summand + * \param m BIGNUM with the second factor of the second summand + * \param ctx BN_CTX object (optional) + * \return 1 on success and 0 if an error occurred + */ +int EC_POINT_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *n, + const EC_POINT *q, const BIGNUM *m, BN_CTX *ctx); + +/** Stores multiples of generator for faster point multiplication + * \param group EC_GROUP object + * \param ctx BN_CTX object (optional) + * \return 1 on success and 0 if an error occurred + */ +int EC_GROUP_precompute_mult(EC_GROUP *group, BN_CTX *ctx); + +/** Reports whether a precomputation has been done + * \param group EC_GROUP object + * \return 1 if a pre-computation has been done and 0 otherwise + */ +int EC_GROUP_have_precompute_mult(const EC_GROUP *group); + +/********************************************************************/ +/* ASN1 stuff */ +/********************************************************************/ + +DECLARE_ASN1_ITEM(ECPKPARAMETERS) +DECLARE_ASN1_ALLOC_FUNCTIONS(ECPKPARAMETERS) +DECLARE_ASN1_ITEM(ECPARAMETERS) +DECLARE_ASN1_ALLOC_FUNCTIONS(ECPARAMETERS) + +/* + * EC_GROUP_get_basis_type() returns the NID of the basis type used to + * represent the field elements + */ +int EC_GROUP_get_basis_type(const EC_GROUP *); +# ifndef OPENSSL_NO_EC2M +int EC_GROUP_get_trinomial_basis(const EC_GROUP *, unsigned int *k); +int EC_GROUP_get_pentanomial_basis(const EC_GROUP *, unsigned int *k1, + unsigned int *k2, unsigned int *k3); +# endif + +# define OPENSSL_EC_EXPLICIT_CURVE 0x000 +# define OPENSSL_EC_NAMED_CURVE 0x001 + +EC_GROUP *d2i_ECPKParameters(EC_GROUP **, const unsigned char **in, long len); +int i2d_ECPKParameters(const EC_GROUP *, unsigned char **out); + +# define d2i_ECPKParameters_bio(bp,x) ASN1_d2i_bio_of(EC_GROUP,NULL,d2i_ECPKParameters,bp,x) +# define i2d_ECPKParameters_bio(bp,x) ASN1_i2d_bio_of_const(EC_GROUP,i2d_ECPKParameters,bp,x) +# define d2i_ECPKParameters_fp(fp,x) (EC_GROUP *)ASN1_d2i_fp(NULL, \ + (char *(*)())d2i_ECPKParameters,(fp),(unsigned char **)(x)) +# define i2d_ECPKParameters_fp(fp,x) ASN1_i2d_fp(i2d_ECPKParameters,(fp), \ + (unsigned char *)(x)) + +int ECPKParameters_print(BIO *bp, const EC_GROUP *x, int off); +# ifndef OPENSSL_NO_STDIO +int ECPKParameters_print_fp(FILE *fp, const EC_GROUP *x, int off); +# endif + +/********************************************************************/ +/* EC_KEY functions */ +/********************************************************************/ + +/* some values for the encoding_flag */ +# define EC_PKEY_NO_PARAMETERS 0x001 +# define EC_PKEY_NO_PUBKEY 0x002 + +/* some values for the flags field */ +# define EC_FLAG_NON_FIPS_ALLOW 0x1 +# define EC_FLAG_FIPS_CHECKED 0x2 +# define EC_FLAG_COFACTOR_ECDH 0x1000 + +/** Creates a new EC_KEY object. + * \return EC_KEY object or NULL if an error occurred. + */ +EC_KEY *EC_KEY_new(void); + +int EC_KEY_get_flags(const EC_KEY *key); + +void EC_KEY_set_flags(EC_KEY *key, int flags); + +void EC_KEY_clear_flags(EC_KEY *key, int flags); + +/** Creates a new EC_KEY object using a named curve as underlying + * EC_GROUP object. + * \param nid NID of the named curve. + * \return EC_KEY object or NULL if an error occurred. + */ +EC_KEY *EC_KEY_new_by_curve_name(int nid); + +/** Frees a EC_KEY object. + * \param key EC_KEY object to be freed. + */ +void EC_KEY_free(EC_KEY *key); + +/** Copies a EC_KEY object. + * \param dst destination EC_KEY object + * \param src src EC_KEY object + * \return dst or NULL if an error occurred. + */ +EC_KEY *EC_KEY_copy(EC_KEY *dst, const EC_KEY *src); + +/** Creates a new EC_KEY object and copies the content from src to it. + * \param src the source EC_KEY object + * \return newly created EC_KEY object or NULL if an error occurred. + */ +EC_KEY *EC_KEY_dup(const EC_KEY *src); + +/** Increases the internal reference count of a EC_KEY object. + * \param key EC_KEY object + * \return 1 on success and 0 if an error occurred. + */ +int EC_KEY_up_ref(EC_KEY *key); + +/** Returns the ENGINE object of a EC_KEY object + * \param eckey EC_KEY object + * \return the ENGINE object (possibly NULL). + */ +ENGINE *EC_KEY_get0_engine(const EC_KEY *eckey); + +/** Returns the EC_GROUP object of a EC_KEY object + * \param key EC_KEY object + * \return the EC_GROUP object (possibly NULL). + */ +const EC_GROUP *EC_KEY_get0_group(const EC_KEY *key); + +/** Sets the EC_GROUP of a EC_KEY object. + * \param key EC_KEY object + * \param group EC_GROUP to use in the EC_KEY object (note: the EC_KEY + * object will use an own copy of the EC_GROUP). + * \return 1 on success and 0 if an error occurred. + */ +int EC_KEY_set_group(EC_KEY *key, const EC_GROUP *group); + +/** Returns the private key of a EC_KEY object. + * \param key EC_KEY object + * \return a BIGNUM with the private key (possibly NULL). + */ +const BIGNUM *EC_KEY_get0_private_key(const EC_KEY *key); + +/** Sets the private key of a EC_KEY object. + * \param key EC_KEY object + * \param prv BIGNUM with the private key (note: the EC_KEY object + * will use an own copy of the BIGNUM). + * \return 1 on success and 0 if an error occurred. + */ +int EC_KEY_set_private_key(EC_KEY *key, const BIGNUM *prv); + +/** Returns the public key of a EC_KEY object. + * \param key the EC_KEY object + * \return a EC_POINT object with the public key (possibly NULL) + */ +const EC_POINT *EC_KEY_get0_public_key(const EC_KEY *key); + +/** Sets the public key of a EC_KEY object. + * \param key EC_KEY object + * \param pub EC_POINT object with the public key (note: the EC_KEY object + * will use an own copy of the EC_POINT object). + * \return 1 on success and 0 if an error occurred. + */ +int EC_KEY_set_public_key(EC_KEY *key, const EC_POINT *pub); + +unsigned EC_KEY_get_enc_flags(const EC_KEY *key); +void EC_KEY_set_enc_flags(EC_KEY *eckey, unsigned int flags); +point_conversion_form_t EC_KEY_get_conv_form(const EC_KEY *key); +void EC_KEY_set_conv_form(EC_KEY *eckey, point_conversion_form_t cform); + +#define EC_KEY_get_ex_new_index(l, p, newf, dupf, freef) \ + CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_EC_KEY, l, p, newf, dupf, freef) +int EC_KEY_set_ex_data(EC_KEY *key, int idx, void *arg); +void *EC_KEY_get_ex_data(const EC_KEY *key, int idx); + +/* wrapper functions for the underlying EC_GROUP object */ +void EC_KEY_set_asn1_flag(EC_KEY *eckey, int asn1_flag); + +/** Creates a table of pre-computed multiples of the generator to + * accelerate further EC_KEY operations. + * \param key EC_KEY object + * \param ctx BN_CTX object (optional) + * \return 1 on success and 0 if an error occurred. + */ +int EC_KEY_precompute_mult(EC_KEY *key, BN_CTX *ctx); + +/** Creates a new ec private (and optional a new public) key. + * \param key EC_KEY object + * \return 1 on success and 0 if an error occurred. + */ +int EC_KEY_generate_key(EC_KEY *key); + +/** Verifies that a private and/or public key is valid. + * \param key the EC_KEY object + * \return 1 on success and 0 otherwise. + */ +int EC_KEY_check_key(const EC_KEY *key); + +/** Indicates if an EC_KEY can be used for signing. + * \param eckey the EC_KEY object + * \return 1 if can can sign and 0 otherwise. + */ +int EC_KEY_can_sign(const EC_KEY *eckey); + +/** Sets a public key from affine coordinates performing + * necessary NIST PKV tests. + * \param key the EC_KEY object + * \param x public key x coordinate + * \param y public key y coordinate + * \return 1 on success and 0 otherwise. + */ +int EC_KEY_set_public_key_affine_coordinates(EC_KEY *key, BIGNUM *x, + BIGNUM *y); + +/** Encodes an EC_KEY public key to an allocated octet string + * \param key key to encode + * \param form point conversion form + * \param pbuf returns pointer to allocated buffer + * \param ctx BN_CTX object (optional) + * \return the length of the encoded octet string or 0 if an error occurred + */ +size_t EC_KEY_key2buf(const EC_KEY *key, point_conversion_form_t form, + unsigned char **pbuf, BN_CTX *ctx); + +/** Decodes a EC_KEY public key from a octet string + * \param key key to decode + * \param buf memory buffer with the encoded ec point + * \param len length of the encoded ec point + * \param ctx BN_CTX object (optional) + * \return 1 on success and 0 if an error occurred + */ + +int EC_KEY_oct2key(EC_KEY *key, const unsigned char *buf, size_t len, + BN_CTX *ctx); + +/** Decodes an EC_KEY private key from an octet string + * \param key key to decode + * \param buf memory buffer with the encoded private key + * \param len length of the encoded key + * \return 1 on success and 0 if an error occurred + */ + +int EC_KEY_oct2priv(EC_KEY *key, const unsigned char *buf, size_t len); + +/** Encodes a EC_KEY private key to an octet string + * \param key key to encode + * \param buf memory buffer for the result. If NULL the function returns + * required buffer size. + * \param len length of the memory buffer + * \return the length of the encoded octet string or 0 if an error occurred + */ + +size_t EC_KEY_priv2oct(const EC_KEY *key, unsigned char *buf, size_t len); + +/** Encodes an EC_KEY private key to an allocated octet string + * \param eckey key to encode + * \param pbuf returns pointer to allocated buffer + * \return the length of the encoded octet string or 0 if an error occurred + */ +size_t EC_KEY_priv2buf(const EC_KEY *eckey, unsigned char **pbuf); + +/********************************************************************/ +/* de- and encoding functions for SEC1 ECPrivateKey */ +/********************************************************************/ + +/** Decodes a private key from a memory buffer. + * \param key a pointer to a EC_KEY object which should be used (or NULL) + * \param in pointer to memory with the DER encoded private key + * \param len length of the DER encoded private key + * \return the decoded private key or NULL if an error occurred. + */ +EC_KEY *d2i_ECPrivateKey(EC_KEY **key, const unsigned char **in, long len); + +/** Encodes a private key object and stores the result in a buffer. + * \param key the EC_KEY object to encode + * \param out the buffer for the result (if NULL the function returns number + * of bytes needed). + * \return 1 on success and 0 if an error occurred. + */ +int i2d_ECPrivateKey(EC_KEY *key, unsigned char **out); + +/********************************************************************/ +/* de- and encoding functions for EC parameters */ +/********************************************************************/ + +/** Decodes ec parameter from a memory buffer. + * \param key a pointer to a EC_KEY object which should be used (or NULL) + * \param in pointer to memory with the DER encoded ec parameters + * \param len length of the DER encoded ec parameters + * \return a EC_KEY object with the decoded parameters or NULL if an error + * occurred. + */ +EC_KEY *d2i_ECParameters(EC_KEY **key, const unsigned char **in, long len); + +/** Encodes ec parameter and stores the result in a buffer. + * \param key the EC_KEY object with ec parameters to encode + * \param out the buffer for the result (if NULL the function returns number + * of bytes needed). + * \return 1 on success and 0 if an error occurred. + */ +int i2d_ECParameters(EC_KEY *key, unsigned char **out); + +/********************************************************************/ +/* de- and encoding functions for EC public key */ +/* (octet string, not DER -- hence 'o2i' and 'i2o') */ +/********************************************************************/ + +/** Decodes a ec public key from a octet string. + * \param key a pointer to a EC_KEY object which should be used + * \param in memory buffer with the encoded public key + * \param len length of the encoded public key + * \return EC_KEY object with decoded public key or NULL if an error + * occurred. + */ +EC_KEY *o2i_ECPublicKey(EC_KEY **key, const unsigned char **in, long len); + +/** Encodes a ec public key in an octet string. + * \param key the EC_KEY object with the public key + * \param out the buffer for the result (if NULL the function returns number + * of bytes needed). + * \return 1 on success and 0 if an error occurred + */ +int i2o_ECPublicKey(const EC_KEY *key, unsigned char **out); + +/** Prints out the ec parameters on human readable form. + * \param bp BIO object to which the information is printed + * \param key EC_KEY object + * \return 1 on success and 0 if an error occurred + */ +int ECParameters_print(BIO *bp, const EC_KEY *key); + +/** Prints out the contents of a EC_KEY object + * \param bp BIO object to which the information is printed + * \param key EC_KEY object + * \param off line offset + * \return 1 on success and 0 if an error occurred + */ +int EC_KEY_print(BIO *bp, const EC_KEY *key, int off); + +# ifndef OPENSSL_NO_STDIO +/** Prints out the ec parameters on human readable form. + * \param fp file descriptor to which the information is printed + * \param key EC_KEY object + * \return 1 on success and 0 if an error occurred + */ +int ECParameters_print_fp(FILE *fp, const EC_KEY *key); + +/** Prints out the contents of a EC_KEY object + * \param fp file descriptor to which the information is printed + * \param key EC_KEY object + * \param off line offset + * \return 1 on success and 0 if an error occurred + */ +int EC_KEY_print_fp(FILE *fp, const EC_KEY *key, int off); + +# endif + +const EC_KEY_METHOD *EC_KEY_OpenSSL(void); +const EC_KEY_METHOD *EC_KEY_get_default_method(void); +void EC_KEY_set_default_method(const EC_KEY_METHOD *meth); +const EC_KEY_METHOD *EC_KEY_get_method(const EC_KEY *key); +int EC_KEY_set_method(EC_KEY *key, const EC_KEY_METHOD *meth); +EC_KEY *EC_KEY_new_method(ENGINE *engine); + +/** The old name for ecdh_KDF_X9_63 + * The ECDH KDF specification has been mistakingly attributed to ANSI X9.62, + * it is actually specified in ANSI X9.63. + * This identifier is retained for backwards compatibility + */ +int ECDH_KDF_X9_62(unsigned char *out, size_t outlen, + const unsigned char *Z, size_t Zlen, + const unsigned char *sinfo, size_t sinfolen, + const EVP_MD *md); + +int ECDH_compute_key(void *out, size_t outlen, const EC_POINT *pub_key, + const EC_KEY *ecdh, + void *(*KDF) (const void *in, size_t inlen, + void *out, size_t *outlen)); + +typedef struct ECDSA_SIG_st ECDSA_SIG; + +/** Allocates and initialize a ECDSA_SIG structure + * \return pointer to a ECDSA_SIG structure or NULL if an error occurred + */ +ECDSA_SIG *ECDSA_SIG_new(void); + +/** frees a ECDSA_SIG structure + * \param sig pointer to the ECDSA_SIG structure + */ +void ECDSA_SIG_free(ECDSA_SIG *sig); + +/** DER encode content of ECDSA_SIG object (note: this function modifies *pp + * (*pp += length of the DER encoded signature)). + * \param sig pointer to the ECDSA_SIG object + * \param pp pointer to a unsigned char pointer for the output or NULL + * \return the length of the DER encoded ECDSA_SIG object or 0 + */ +int i2d_ECDSA_SIG(const ECDSA_SIG *sig, unsigned char **pp); + +/** Decodes a DER encoded ECDSA signature (note: this function changes *pp + * (*pp += len)). + * \param sig pointer to ECDSA_SIG pointer (may be NULL) + * \param pp memory buffer with the DER encoded signature + * \param len length of the buffer + * \return pointer to the decoded ECDSA_SIG structure (or NULL) + */ +ECDSA_SIG *d2i_ECDSA_SIG(ECDSA_SIG **sig, const unsigned char **pp, long len); + +/** Accessor for r and s fields of ECDSA_SIG + * \param sig pointer to ECDSA_SIG structure + * \param pr pointer to BIGNUM pointer for r (may be NULL) + * \param ps pointer to BIGNUM pointer for s (may be NULL) + */ +void ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps); + +/** Accessor for r field of ECDSA_SIG + * \param sig pointer to ECDSA_SIG structure + */ +const BIGNUM *ECDSA_SIG_get0_r(const ECDSA_SIG *sig); + +/** Accessor for s field of ECDSA_SIG + * \param sig pointer to ECDSA_SIG structure + */ +const BIGNUM *ECDSA_SIG_get0_s(const ECDSA_SIG *sig); + +/** Setter for r and s fields of ECDSA_SIG + * \param sig pointer to ECDSA_SIG structure + * \param r pointer to BIGNUM for r (may be NULL) + * \param s pointer to BIGNUM for s (may be NULL) + */ +int ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s); + +/** Computes the ECDSA signature of the given hash value using + * the supplied private key and returns the created signature. + * \param dgst pointer to the hash value + * \param dgst_len length of the hash value + * \param eckey EC_KEY object containing a private EC key + * \return pointer to a ECDSA_SIG structure or NULL if an error occurred + */ +ECDSA_SIG *ECDSA_do_sign(const unsigned char *dgst, int dgst_len, + EC_KEY *eckey); + +/** Computes ECDSA signature of a given hash value using the supplied + * private key (note: sig must point to ECDSA_size(eckey) bytes of memory). + * \param dgst pointer to the hash value to sign + * \param dgstlen length of the hash value + * \param kinv BIGNUM with a pre-computed inverse k (optional) + * \param rp BIGNUM with a pre-computed rp value (optional), + * see ECDSA_sign_setup + * \param eckey EC_KEY object containing a private EC key + * \return pointer to a ECDSA_SIG structure or NULL if an error occurred + */ +ECDSA_SIG *ECDSA_do_sign_ex(const unsigned char *dgst, int dgstlen, + const BIGNUM *kinv, const BIGNUM *rp, + EC_KEY *eckey); + +/** Verifies that the supplied signature is a valid ECDSA + * signature of the supplied hash value using the supplied public key. + * \param dgst pointer to the hash value + * \param dgst_len length of the hash value + * \param sig ECDSA_SIG structure + * \param eckey EC_KEY object containing a public EC key + * \return 1 if the signature is valid, 0 if the signature is invalid + * and -1 on error + */ +int ECDSA_do_verify(const unsigned char *dgst, int dgst_len, + const ECDSA_SIG *sig, EC_KEY *eckey); + +/** Precompute parts of the signing operation + * \param eckey EC_KEY object containing a private EC key + * \param ctx BN_CTX object (optional) + * \param kinv BIGNUM pointer for the inverse of k + * \param rp BIGNUM pointer for x coordinate of k * generator + * \return 1 on success and 0 otherwise + */ +int ECDSA_sign_setup(EC_KEY *eckey, BN_CTX *ctx, BIGNUM **kinv, BIGNUM **rp); + +/** Computes ECDSA signature of a given hash value using the supplied + * private key (note: sig must point to ECDSA_size(eckey) bytes of memory). + * \param type this parameter is ignored + * \param dgst pointer to the hash value to sign + * \param dgstlen length of the hash value + * \param sig memory for the DER encoded created signature + * \param siglen pointer to the length of the returned signature + * \param eckey EC_KEY object containing a private EC key + * \return 1 on success and 0 otherwise + */ +int ECDSA_sign(int type, const unsigned char *dgst, int dgstlen, + unsigned char *sig, unsigned int *siglen, EC_KEY *eckey); + +/** Computes ECDSA signature of a given hash value using the supplied + * private key (note: sig must point to ECDSA_size(eckey) bytes of memory). + * \param type this parameter is ignored + * \param dgst pointer to the hash value to sign + * \param dgstlen length of the hash value + * \param sig buffer to hold the DER encoded signature + * \param siglen pointer to the length of the returned signature + * \param kinv BIGNUM with a pre-computed inverse k (optional) + * \param rp BIGNUM with a pre-computed rp value (optional), + * see ECDSA_sign_setup + * \param eckey EC_KEY object containing a private EC key + * \return 1 on success and 0 otherwise + */ +int ECDSA_sign_ex(int type, const unsigned char *dgst, int dgstlen, + unsigned char *sig, unsigned int *siglen, + const BIGNUM *kinv, const BIGNUM *rp, EC_KEY *eckey); + +/** Verifies that the given signature is valid ECDSA signature + * of the supplied hash value using the specified public key. + * \param type this parameter is ignored + * \param dgst pointer to the hash value + * \param dgstlen length of the hash value + * \param sig pointer to the DER encoded signature + * \param siglen length of the DER encoded signature + * \param eckey EC_KEY object containing a public EC key + * \return 1 if the signature is valid, 0 if the signature is invalid + * and -1 on error + */ +int ECDSA_verify(int type, const unsigned char *dgst, int dgstlen, + const unsigned char *sig, int siglen, EC_KEY *eckey); + +/** Returns the maximum length of the DER encoded signature + * \param eckey EC_KEY object + * \return numbers of bytes required for the DER encoded signature + */ +int ECDSA_size(const EC_KEY *eckey); + +/********************************************************************/ +/* EC_KEY_METHOD constructors, destructors, writers and accessors */ +/********************************************************************/ + +EC_KEY_METHOD *EC_KEY_METHOD_new(const EC_KEY_METHOD *meth); +void EC_KEY_METHOD_free(EC_KEY_METHOD *meth); +void EC_KEY_METHOD_set_init(EC_KEY_METHOD *meth, + int (*init)(EC_KEY *key), + void (*finish)(EC_KEY *key), + int (*copy)(EC_KEY *dest, const EC_KEY *src), + int (*set_group)(EC_KEY *key, const EC_GROUP *grp), + int (*set_private)(EC_KEY *key, + const BIGNUM *priv_key), + int (*set_public)(EC_KEY *key, + const EC_POINT *pub_key)); + +void EC_KEY_METHOD_set_keygen(EC_KEY_METHOD *meth, + int (*keygen)(EC_KEY *key)); + +void EC_KEY_METHOD_set_compute_key(EC_KEY_METHOD *meth, + int (*ckey)(unsigned char **psec, + size_t *pseclen, + const EC_POINT *pub_key, + const EC_KEY *ecdh)); + +void EC_KEY_METHOD_set_sign(EC_KEY_METHOD *meth, + int (*sign)(int type, const unsigned char *dgst, + int dlen, unsigned char *sig, + unsigned int *siglen, + const BIGNUM *kinv, const BIGNUM *r, + EC_KEY *eckey), + int (*sign_setup)(EC_KEY *eckey, BN_CTX *ctx_in, + BIGNUM **kinvp, BIGNUM **rp), + ECDSA_SIG *(*sign_sig)(const unsigned char *dgst, + int dgst_len, + const BIGNUM *in_kinv, + const BIGNUM *in_r, + EC_KEY *eckey)); + +void EC_KEY_METHOD_set_verify(EC_KEY_METHOD *meth, + int (*verify)(int type, const unsigned + char *dgst, int dgst_len, + const unsigned char *sigbuf, + int sig_len, EC_KEY *eckey), + int (*verify_sig)(const unsigned char *dgst, + int dgst_len, + const ECDSA_SIG *sig, + EC_KEY *eckey)); + +void EC_KEY_METHOD_get_init(const EC_KEY_METHOD *meth, + int (**pinit)(EC_KEY *key), + void (**pfinish)(EC_KEY *key), + int (**pcopy)(EC_KEY *dest, const EC_KEY *src), + int (**pset_group)(EC_KEY *key, + const EC_GROUP *grp), + int (**pset_private)(EC_KEY *key, + const BIGNUM *priv_key), + int (**pset_public)(EC_KEY *key, + const EC_POINT *pub_key)); + +void EC_KEY_METHOD_get_keygen(const EC_KEY_METHOD *meth, + int (**pkeygen)(EC_KEY *key)); + +void EC_KEY_METHOD_get_compute_key(const EC_KEY_METHOD *meth, + int (**pck)(unsigned char **psec, + size_t *pseclen, + const EC_POINT *pub_key, + const EC_KEY *ecdh)); + +void EC_KEY_METHOD_get_sign(const EC_KEY_METHOD *meth, + int (**psign)(int type, const unsigned char *dgst, + int dlen, unsigned char *sig, + unsigned int *siglen, + const BIGNUM *kinv, const BIGNUM *r, + EC_KEY *eckey), + int (**psign_setup)(EC_KEY *eckey, BN_CTX *ctx_in, + BIGNUM **kinvp, BIGNUM **rp), + ECDSA_SIG *(**psign_sig)(const unsigned char *dgst, + int dgst_len, + const BIGNUM *in_kinv, + const BIGNUM *in_r, + EC_KEY *eckey)); + +void EC_KEY_METHOD_get_verify(const EC_KEY_METHOD *meth, + int (**pverify)(int type, const unsigned + char *dgst, int dgst_len, + const unsigned char *sigbuf, + int sig_len, EC_KEY *eckey), + int (**pverify_sig)(const unsigned char *dgst, + int dgst_len, + const ECDSA_SIG *sig, + EC_KEY *eckey)); + +# define ECParameters_dup(x) ASN1_dup_of(EC_KEY,i2d_ECParameters,d2i_ECParameters,x) + +# ifndef __cplusplus +# if defined(__SUNPRO_C) +# if __SUNPRO_C >= 0x520 +# pragma error_messages (default,E_ARRAY_OF_INCOMPLETE_NONAME,E_ARRAY_OF_INCOMPLETE) +# endif +# endif +# endif + +# define EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx, nid) \ + EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \ + EVP_PKEY_OP_PARAMGEN|EVP_PKEY_OP_KEYGEN, \ + EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID, nid, NULL) + +# define EVP_PKEY_CTX_set_ec_param_enc(ctx, flag) \ + EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \ + EVP_PKEY_OP_PARAMGEN|EVP_PKEY_OP_KEYGEN, \ + EVP_PKEY_CTRL_EC_PARAM_ENC, flag, NULL) + +# define EVP_PKEY_CTX_set_ecdh_cofactor_mode(ctx, flag) \ + EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \ + EVP_PKEY_OP_DERIVE, \ + EVP_PKEY_CTRL_EC_ECDH_COFACTOR, flag, NULL) + +# define EVP_PKEY_CTX_get_ecdh_cofactor_mode(ctx) \ + EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \ + EVP_PKEY_OP_DERIVE, \ + EVP_PKEY_CTRL_EC_ECDH_COFACTOR, -2, NULL) + +# define EVP_PKEY_CTX_set_ecdh_kdf_type(ctx, kdf) \ + EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \ + EVP_PKEY_OP_DERIVE, \ + EVP_PKEY_CTRL_EC_KDF_TYPE, kdf, NULL) + +# define EVP_PKEY_CTX_get_ecdh_kdf_type(ctx) \ + EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \ + EVP_PKEY_OP_DERIVE, \ + EVP_PKEY_CTRL_EC_KDF_TYPE, -2, NULL) + +# define EVP_PKEY_CTX_set_ecdh_kdf_md(ctx, md) \ + EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \ + EVP_PKEY_OP_DERIVE, \ + EVP_PKEY_CTRL_EC_KDF_MD, 0, (void *)(md)) + +# define EVP_PKEY_CTX_get_ecdh_kdf_md(ctx, pmd) \ + EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \ + EVP_PKEY_OP_DERIVE, \ + EVP_PKEY_CTRL_GET_EC_KDF_MD, 0, (void *)(pmd)) + +# define EVP_PKEY_CTX_set_ecdh_kdf_outlen(ctx, len) \ + EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \ + EVP_PKEY_OP_DERIVE, \ + EVP_PKEY_CTRL_EC_KDF_OUTLEN, len, NULL) + +# define EVP_PKEY_CTX_get_ecdh_kdf_outlen(ctx, plen) \ + EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \ + EVP_PKEY_OP_DERIVE, \ + EVP_PKEY_CTRL_GET_EC_KDF_OUTLEN, 0, \ + (void *)(plen)) + +# define EVP_PKEY_CTX_set0_ecdh_kdf_ukm(ctx, p, plen) \ + EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \ + EVP_PKEY_OP_DERIVE, \ + EVP_PKEY_CTRL_EC_KDF_UKM, plen, (void *)(p)) + +# define EVP_PKEY_CTX_get0_ecdh_kdf_ukm(ctx, p) \ + EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \ + EVP_PKEY_OP_DERIVE, \ + EVP_PKEY_CTRL_GET_EC_KDF_UKM, 0, (void *)(p)) + +/* SM2 will skip the operation check so no need to pass operation here */ +# define EVP_PKEY_CTX_set1_id(ctx, id, id_len) \ + EVP_PKEY_CTX_ctrl(ctx, -1, -1, \ + EVP_PKEY_CTRL_SET1_ID, (int)id_len, (void*)(id)) + +# define EVP_PKEY_CTX_get1_id(ctx, id) \ + EVP_PKEY_CTX_ctrl(ctx, -1, -1, \ + EVP_PKEY_CTRL_GET1_ID, 0, (void*)(id)) + +# define EVP_PKEY_CTX_get1_id_len(ctx, id_len) \ + EVP_PKEY_CTX_ctrl(ctx, -1, -1, \ + EVP_PKEY_CTRL_GET1_ID_LEN, 0, (void*)(id_len)) + +# define EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID (EVP_PKEY_ALG_CTRL + 1) +# define EVP_PKEY_CTRL_EC_PARAM_ENC (EVP_PKEY_ALG_CTRL + 2) +# define EVP_PKEY_CTRL_EC_ECDH_COFACTOR (EVP_PKEY_ALG_CTRL + 3) +# define EVP_PKEY_CTRL_EC_KDF_TYPE (EVP_PKEY_ALG_CTRL + 4) +# define EVP_PKEY_CTRL_EC_KDF_MD (EVP_PKEY_ALG_CTRL + 5) +# define EVP_PKEY_CTRL_GET_EC_KDF_MD (EVP_PKEY_ALG_CTRL + 6) +# define EVP_PKEY_CTRL_EC_KDF_OUTLEN (EVP_PKEY_ALG_CTRL + 7) +# define EVP_PKEY_CTRL_GET_EC_KDF_OUTLEN (EVP_PKEY_ALG_CTRL + 8) +# define EVP_PKEY_CTRL_EC_KDF_UKM (EVP_PKEY_ALG_CTRL + 9) +# define EVP_PKEY_CTRL_GET_EC_KDF_UKM (EVP_PKEY_ALG_CTRL + 10) +# define EVP_PKEY_CTRL_SET1_ID (EVP_PKEY_ALG_CTRL + 11) +# define EVP_PKEY_CTRL_GET1_ID (EVP_PKEY_ALG_CTRL + 12) +# define EVP_PKEY_CTRL_GET1_ID_LEN (EVP_PKEY_ALG_CTRL + 13) +/* KDF types */ +# define EVP_PKEY_ECDH_KDF_NONE 1 +# define EVP_PKEY_ECDH_KDF_X9_63 2 +/** The old name for EVP_PKEY_ECDH_KDF_X9_63 + * The ECDH KDF specification has been mistakingly attributed to ANSI X9.62, + * it is actually specified in ANSI X9.63. + * This identifier is retained for backwards compatibility + */ +# define EVP_PKEY_ECDH_KDF_X9_62 EVP_PKEY_ECDH_KDF_X9_63 + + +# ifdef __cplusplus +} +# endif +# endif +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/ecdh.h b/example/android/third_party/crypto/arm/include/openssl/ecdh.h new file mode 100644 index 00000000..681f3d5e --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/ecdh.h @@ -0,0 +1,10 @@ +/* + * Copyright 2002-2016 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#include diff --git a/example/android/third_party/crypto/arm/include/openssl/ecdsa.h b/example/android/third_party/crypto/arm/include/openssl/ecdsa.h new file mode 100644 index 00000000..681f3d5e --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/ecdsa.h @@ -0,0 +1,10 @@ +/* + * Copyright 2002-2016 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#include diff --git a/example/android/third_party/crypto/arm/include/openssl/ecerr.h b/example/android/third_party/crypto/arm/include/openssl/ecerr.h new file mode 100644 index 00000000..8d429387 --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/ecerr.h @@ -0,0 +1,267 @@ +/* + * Generated by util/mkerr.pl DO NOT EDIT + * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_ECERR_H +# define HEADER_ECERR_H + +# include + +# ifndef OPENSSL_NO_EC + +# ifdef __cplusplus +extern "C" +# endif +int ERR_load_EC_strings(void); + +/* + * EC function codes. + */ +# define EC_F_BN_TO_FELEM 224 +# define EC_F_D2I_ECPARAMETERS 144 +# define EC_F_D2I_ECPKPARAMETERS 145 +# define EC_F_D2I_ECPRIVATEKEY 146 +# define EC_F_DO_EC_KEY_PRINT 221 +# define EC_F_ECDH_CMS_DECRYPT 238 +# define EC_F_ECDH_CMS_SET_SHARED_INFO 239 +# define EC_F_ECDH_COMPUTE_KEY 246 +# define EC_F_ECDH_SIMPLE_COMPUTE_KEY 257 +# define EC_F_ECDSA_DO_SIGN_EX 251 +# define EC_F_ECDSA_DO_VERIFY 252 +# define EC_F_ECDSA_SIGN_EX 254 +# define EC_F_ECDSA_SIGN_SETUP 248 +# define EC_F_ECDSA_SIG_NEW 265 +# define EC_F_ECDSA_VERIFY 253 +# define EC_F_ECD_ITEM_VERIFY 270 +# define EC_F_ECKEY_PARAM2TYPE 223 +# define EC_F_ECKEY_PARAM_DECODE 212 +# define EC_F_ECKEY_PRIV_DECODE 213 +# define EC_F_ECKEY_PRIV_ENCODE 214 +# define EC_F_ECKEY_PUB_DECODE 215 +# define EC_F_ECKEY_PUB_ENCODE 216 +# define EC_F_ECKEY_TYPE2PARAM 220 +# define EC_F_ECPARAMETERS_PRINT 147 +# define EC_F_ECPARAMETERS_PRINT_FP 148 +# define EC_F_ECPKPARAMETERS_PRINT 149 +# define EC_F_ECPKPARAMETERS_PRINT_FP 150 +# define EC_F_ECP_NISTZ256_GET_AFFINE 240 +# define EC_F_ECP_NISTZ256_INV_MOD_ORD 275 +# define EC_F_ECP_NISTZ256_MULT_PRECOMPUTE 243 +# define EC_F_ECP_NISTZ256_POINTS_MUL 241 +# define EC_F_ECP_NISTZ256_PRE_COMP_NEW 244 +# define EC_F_ECP_NISTZ256_WINDOWED_MUL 242 +# define EC_F_ECX_KEY_OP 266 +# define EC_F_ECX_PRIV_ENCODE 267 +# define EC_F_ECX_PUB_ENCODE 268 +# define EC_F_EC_ASN1_GROUP2CURVE 153 +# define EC_F_EC_ASN1_GROUP2FIELDID 154 +# define EC_F_EC_GF2M_MONTGOMERY_POINT_MULTIPLY 208 +# define EC_F_EC_GF2M_SIMPLE_GROUP_CHECK_DISCRIMINANT 159 +# define EC_F_EC_GF2M_SIMPLE_GROUP_SET_CURVE 195 +# define EC_F_EC_GF2M_SIMPLE_LADDER_POST 285 +# define EC_F_EC_GF2M_SIMPLE_LADDER_PRE 288 +# define EC_F_EC_GF2M_SIMPLE_OCT2POINT 160 +# define EC_F_EC_GF2M_SIMPLE_POINT2OCT 161 +# define EC_F_EC_GF2M_SIMPLE_POINTS_MUL 289 +# define EC_F_EC_GF2M_SIMPLE_POINT_GET_AFFINE_COORDINATES 162 +# define EC_F_EC_GF2M_SIMPLE_POINT_SET_AFFINE_COORDINATES 163 +# define EC_F_EC_GF2M_SIMPLE_SET_COMPRESSED_COORDINATES 164 +# define EC_F_EC_GFP_MONT_FIELD_DECODE 133 +# define EC_F_EC_GFP_MONT_FIELD_ENCODE 134 +# define EC_F_EC_GFP_MONT_FIELD_MUL 131 +# define EC_F_EC_GFP_MONT_FIELD_SET_TO_ONE 209 +# define EC_F_EC_GFP_MONT_FIELD_SQR 132 +# define EC_F_EC_GFP_MONT_GROUP_SET_CURVE 189 +# define EC_F_EC_GFP_NISTP224_GROUP_SET_CURVE 225 +# define EC_F_EC_GFP_NISTP224_POINTS_MUL 228 +# define EC_F_EC_GFP_NISTP224_POINT_GET_AFFINE_COORDINATES 226 +# define EC_F_EC_GFP_NISTP256_GROUP_SET_CURVE 230 +# define EC_F_EC_GFP_NISTP256_POINTS_MUL 231 +# define EC_F_EC_GFP_NISTP256_POINT_GET_AFFINE_COORDINATES 232 +# define EC_F_EC_GFP_NISTP521_GROUP_SET_CURVE 233 +# define EC_F_EC_GFP_NISTP521_POINTS_MUL 234 +# define EC_F_EC_GFP_NISTP521_POINT_GET_AFFINE_COORDINATES 235 +# define EC_F_EC_GFP_NIST_FIELD_MUL 200 +# define EC_F_EC_GFP_NIST_FIELD_SQR 201 +# define EC_F_EC_GFP_NIST_GROUP_SET_CURVE 202 +# define EC_F_EC_GFP_SIMPLE_BLIND_COORDINATES 287 +# define EC_F_EC_GFP_SIMPLE_GROUP_CHECK_DISCRIMINANT 165 +# define EC_F_EC_GFP_SIMPLE_GROUP_SET_CURVE 166 +# define EC_F_EC_GFP_SIMPLE_MAKE_AFFINE 102 +# define EC_F_EC_GFP_SIMPLE_OCT2POINT 103 +# define EC_F_EC_GFP_SIMPLE_POINT2OCT 104 +# define EC_F_EC_GFP_SIMPLE_POINTS_MAKE_AFFINE 137 +# define EC_F_EC_GFP_SIMPLE_POINT_GET_AFFINE_COORDINATES 167 +# define EC_F_EC_GFP_SIMPLE_POINT_SET_AFFINE_COORDINATES 168 +# define EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES 169 +# define EC_F_EC_GROUP_CHECK 170 +# define EC_F_EC_GROUP_CHECK_DISCRIMINANT 171 +# define EC_F_EC_GROUP_COPY 106 +# define EC_F_EC_GROUP_GET_CURVE 291 +# define EC_F_EC_GROUP_GET_CURVE_GF2M 172 +# define EC_F_EC_GROUP_GET_CURVE_GFP 130 +# define EC_F_EC_GROUP_GET_DEGREE 173 +# define EC_F_EC_GROUP_GET_ECPARAMETERS 261 +# define EC_F_EC_GROUP_GET_ECPKPARAMETERS 262 +# define EC_F_EC_GROUP_GET_PENTANOMIAL_BASIS 193 +# define EC_F_EC_GROUP_GET_TRINOMIAL_BASIS 194 +# define EC_F_EC_GROUP_NEW 108 +# define EC_F_EC_GROUP_NEW_BY_CURVE_NAME 174 +# define EC_F_EC_GROUP_NEW_FROM_DATA 175 +# define EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS 263 +# define EC_F_EC_GROUP_NEW_FROM_ECPKPARAMETERS 264 +# define EC_F_EC_GROUP_SET_CURVE 292 +# define EC_F_EC_GROUP_SET_CURVE_GF2M 176 +# define EC_F_EC_GROUP_SET_CURVE_GFP 109 +# define EC_F_EC_GROUP_SET_GENERATOR 111 +# define EC_F_EC_GROUP_SET_SEED 286 +# define EC_F_EC_KEY_CHECK_KEY 177 +# define EC_F_EC_KEY_COPY 178 +# define EC_F_EC_KEY_GENERATE_KEY 179 +# define EC_F_EC_KEY_NEW 182 +# define EC_F_EC_KEY_NEW_METHOD 245 +# define EC_F_EC_KEY_OCT2PRIV 255 +# define EC_F_EC_KEY_PRINT 180 +# define EC_F_EC_KEY_PRINT_FP 181 +# define EC_F_EC_KEY_PRIV2BUF 279 +# define EC_F_EC_KEY_PRIV2OCT 256 +# define EC_F_EC_KEY_SET_PUBLIC_KEY_AFFINE_COORDINATES 229 +# define EC_F_EC_KEY_SIMPLE_CHECK_KEY 258 +# define EC_F_EC_KEY_SIMPLE_OCT2PRIV 259 +# define EC_F_EC_KEY_SIMPLE_PRIV2OCT 260 +# define EC_F_EC_PKEY_CHECK 273 +# define EC_F_EC_PKEY_PARAM_CHECK 274 +# define EC_F_EC_POINTS_MAKE_AFFINE 136 +# define EC_F_EC_POINTS_MUL 290 +# define EC_F_EC_POINT_ADD 112 +# define EC_F_EC_POINT_BN2POINT 280 +# define EC_F_EC_POINT_CMP 113 +# define EC_F_EC_POINT_COPY 114 +# define EC_F_EC_POINT_DBL 115 +# define EC_F_EC_POINT_GET_AFFINE_COORDINATES 293 +# define EC_F_EC_POINT_GET_AFFINE_COORDINATES_GF2M 183 +# define EC_F_EC_POINT_GET_AFFINE_COORDINATES_GFP 116 +# define EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP 117 +# define EC_F_EC_POINT_INVERT 210 +# define EC_F_EC_POINT_IS_AT_INFINITY 118 +# define EC_F_EC_POINT_IS_ON_CURVE 119 +# define EC_F_EC_POINT_MAKE_AFFINE 120 +# define EC_F_EC_POINT_NEW 121 +# define EC_F_EC_POINT_OCT2POINT 122 +# define EC_F_EC_POINT_POINT2BUF 281 +# define EC_F_EC_POINT_POINT2OCT 123 +# define EC_F_EC_POINT_SET_AFFINE_COORDINATES 294 +# define EC_F_EC_POINT_SET_AFFINE_COORDINATES_GF2M 185 +# define EC_F_EC_POINT_SET_AFFINE_COORDINATES_GFP 124 +# define EC_F_EC_POINT_SET_COMPRESSED_COORDINATES 295 +# define EC_F_EC_POINT_SET_COMPRESSED_COORDINATES_GF2M 186 +# define EC_F_EC_POINT_SET_COMPRESSED_COORDINATES_GFP 125 +# define EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP 126 +# define EC_F_EC_POINT_SET_TO_INFINITY 127 +# define EC_F_EC_PRE_COMP_NEW 196 +# define EC_F_EC_SCALAR_MUL_LADDER 284 +# define EC_F_EC_WNAF_MUL 187 +# define EC_F_EC_WNAF_PRECOMPUTE_MULT 188 +# define EC_F_I2D_ECPARAMETERS 190 +# define EC_F_I2D_ECPKPARAMETERS 191 +# define EC_F_I2D_ECPRIVATEKEY 192 +# define EC_F_I2O_ECPUBLICKEY 151 +# define EC_F_NISTP224_PRE_COMP_NEW 227 +# define EC_F_NISTP256_PRE_COMP_NEW 236 +# define EC_F_NISTP521_PRE_COMP_NEW 237 +# define EC_F_O2I_ECPUBLICKEY 152 +# define EC_F_OLD_EC_PRIV_DECODE 222 +# define EC_F_OSSL_ECDH_COMPUTE_KEY 247 +# define EC_F_OSSL_ECDSA_SIGN_SIG 249 +# define EC_F_OSSL_ECDSA_VERIFY_SIG 250 +# define EC_F_PKEY_ECD_CTRL 271 +# define EC_F_PKEY_ECD_DIGESTSIGN 272 +# define EC_F_PKEY_ECD_DIGESTSIGN25519 276 +# define EC_F_PKEY_ECD_DIGESTSIGN448 277 +# define EC_F_PKEY_ECX_DERIVE 269 +# define EC_F_PKEY_EC_CTRL 197 +# define EC_F_PKEY_EC_CTRL_STR 198 +# define EC_F_PKEY_EC_DERIVE 217 +# define EC_F_PKEY_EC_INIT 282 +# define EC_F_PKEY_EC_KDF_DERIVE 283 +# define EC_F_PKEY_EC_KEYGEN 199 +# define EC_F_PKEY_EC_PARAMGEN 219 +# define EC_F_PKEY_EC_SIGN 218 +# define EC_F_VALIDATE_ECX_DERIVE 278 + +/* + * EC reason codes. + */ +# define EC_R_ASN1_ERROR 115 +# define EC_R_BAD_SIGNATURE 156 +# define EC_R_BIGNUM_OUT_OF_RANGE 144 +# define EC_R_BUFFER_TOO_SMALL 100 +# define EC_R_COORDINATES_OUT_OF_RANGE 146 +# define EC_R_CURVE_DOES_NOT_SUPPORT_ECDH 160 +# define EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING 159 +# define EC_R_D2I_ECPKPARAMETERS_FAILURE 117 +# define EC_R_DECODE_ERROR 142 +# define EC_R_DISCRIMINANT_IS_ZERO 118 +# define EC_R_EC_GROUP_NEW_BY_NAME_FAILURE 119 +# define EC_R_FIELD_TOO_LARGE 143 +# define EC_R_GF2M_NOT_SUPPORTED 147 +# define EC_R_GROUP2PKPARAMETERS_FAILURE 120 +# define EC_R_I2D_ECPKPARAMETERS_FAILURE 121 +# define EC_R_INCOMPATIBLE_OBJECTS 101 +# define EC_R_INVALID_ARGUMENT 112 +# define EC_R_INVALID_COMPRESSED_POINT 110 +# define EC_R_INVALID_COMPRESSION_BIT 109 +# define EC_R_INVALID_CURVE 141 +# define EC_R_INVALID_DIGEST 151 +# define EC_R_INVALID_DIGEST_TYPE 138 +# define EC_R_INVALID_ENCODING 102 +# define EC_R_INVALID_FIELD 103 +# define EC_R_INVALID_FORM 104 +# define EC_R_INVALID_GROUP_ORDER 122 +# define EC_R_INVALID_KEY 116 +# define EC_R_INVALID_OUTPUT_LENGTH 161 +# define EC_R_INVALID_PEER_KEY 133 +# define EC_R_INVALID_PENTANOMIAL_BASIS 132 +# define EC_R_INVALID_PRIVATE_KEY 123 +# define EC_R_INVALID_TRINOMIAL_BASIS 137 +# define EC_R_KDF_PARAMETER_ERROR 148 +# define EC_R_KEYS_NOT_SET 140 +# define EC_R_LADDER_POST_FAILURE 136 +# define EC_R_LADDER_PRE_FAILURE 153 +# define EC_R_LADDER_STEP_FAILURE 162 +# define EC_R_MISSING_PARAMETERS 124 +# define EC_R_MISSING_PRIVATE_KEY 125 +# define EC_R_NEED_NEW_SETUP_VALUES 157 +# define EC_R_NOT_A_NIST_PRIME 135 +# define EC_R_NOT_IMPLEMENTED 126 +# define EC_R_NOT_INITIALIZED 111 +# define EC_R_NO_PARAMETERS_SET 139 +# define EC_R_NO_PRIVATE_VALUE 154 +# define EC_R_OPERATION_NOT_SUPPORTED 152 +# define EC_R_PASSED_NULL_PARAMETER 134 +# define EC_R_PEER_KEY_ERROR 149 +# define EC_R_PKPARAMETERS2GROUP_FAILURE 127 +# define EC_R_POINT_ARITHMETIC_FAILURE 155 +# define EC_R_POINT_AT_INFINITY 106 +# define EC_R_POINT_COORDINATES_BLIND_FAILURE 163 +# define EC_R_POINT_IS_NOT_ON_CURVE 107 +# define EC_R_RANDOM_NUMBER_GENERATION_FAILED 158 +# define EC_R_SHARED_INFO_ERROR 150 +# define EC_R_SLOT_FULL 108 +# define EC_R_UNDEFINED_GENERATOR 113 +# define EC_R_UNDEFINED_ORDER 128 +# define EC_R_UNKNOWN_COFACTOR 164 +# define EC_R_UNKNOWN_GROUP 129 +# define EC_R_UNKNOWN_ORDER 114 +# define EC_R_UNSUPPORTED_FIELD 131 +# define EC_R_WRONG_CURVE_PARAMETERS 145 +# define EC_R_WRONG_ORDER 130 + +# endif +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/engine.h b/example/android/third_party/crypto/arm/include/openssl/engine.h new file mode 100644 index 00000000..0780f0fb --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/engine.h @@ -0,0 +1,751 @@ +/* + * Copyright 2000-2018 The OpenSSL Project Authors. All Rights Reserved. + * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_ENGINE_H +# define HEADER_ENGINE_H + +# include + +# ifndef OPENSSL_NO_ENGINE +# if OPENSSL_API_COMPAT < 0x10100000L +# include +# include +# include +# include +# include +# include +# include +# include +# endif +# include +# include +# include +# include +# ifdef __cplusplus +extern "C" { +# endif + +/* + * These flags are used to control combinations of algorithm (methods) by + * bitwise "OR"ing. + */ +# define ENGINE_METHOD_RSA (unsigned int)0x0001 +# define ENGINE_METHOD_DSA (unsigned int)0x0002 +# define ENGINE_METHOD_DH (unsigned int)0x0004 +# define ENGINE_METHOD_RAND (unsigned int)0x0008 +# define ENGINE_METHOD_CIPHERS (unsigned int)0x0040 +# define ENGINE_METHOD_DIGESTS (unsigned int)0x0080 +# define ENGINE_METHOD_PKEY_METHS (unsigned int)0x0200 +# define ENGINE_METHOD_PKEY_ASN1_METHS (unsigned int)0x0400 +# define ENGINE_METHOD_EC (unsigned int)0x0800 +/* Obvious all-or-nothing cases. */ +# define ENGINE_METHOD_ALL (unsigned int)0xFFFF +# define ENGINE_METHOD_NONE (unsigned int)0x0000 + +/* + * This(ese) flag(s) controls behaviour of the ENGINE_TABLE mechanism used + * internally to control registration of ENGINE implementations, and can be + * set by ENGINE_set_table_flags(). The "NOINIT" flag prevents attempts to + * initialise registered ENGINEs if they are not already initialised. + */ +# define ENGINE_TABLE_FLAG_NOINIT (unsigned int)0x0001 + +/* ENGINE flags that can be set by ENGINE_set_flags(). */ +/* Not used */ +/* #define ENGINE_FLAGS_MALLOCED 0x0001 */ + +/* + * This flag is for ENGINEs that wish to handle the various 'CMD'-related + * control commands on their own. Without this flag, ENGINE_ctrl() handles + * these control commands on behalf of the ENGINE using their "cmd_defns" + * data. + */ +# define ENGINE_FLAGS_MANUAL_CMD_CTRL (int)0x0002 + +/* + * This flag is for ENGINEs who return new duplicate structures when found + * via "ENGINE_by_id()". When an ENGINE must store state (eg. if + * ENGINE_ctrl() commands are called in sequence as part of some stateful + * process like key-generation setup and execution), it can set this flag - + * then each attempt to obtain the ENGINE will result in it being copied into + * a new structure. Normally, ENGINEs don't declare this flag so + * ENGINE_by_id() just increments the existing ENGINE's structural reference + * count. + */ +# define ENGINE_FLAGS_BY_ID_COPY (int)0x0004 + +/* + * This flag if for an ENGINE that does not want its methods registered as + * part of ENGINE_register_all_complete() for example if the methods are not + * usable as default methods. + */ + +# define ENGINE_FLAGS_NO_REGISTER_ALL (int)0x0008 + +/* + * ENGINEs can support their own command types, and these flags are used in + * ENGINE_CTRL_GET_CMD_FLAGS to indicate to the caller what kind of input + * each command expects. Currently only numeric and string input is + * supported. If a control command supports none of the _NUMERIC, _STRING, or + * _NO_INPUT options, then it is regarded as an "internal" control command - + * and not for use in config setting situations. As such, they're not + * available to the ENGINE_ctrl_cmd_string() function, only raw ENGINE_ctrl() + * access. Changes to this list of 'command types' should be reflected + * carefully in ENGINE_cmd_is_executable() and ENGINE_ctrl_cmd_string(). + */ + +/* accepts a 'long' input value (3rd parameter to ENGINE_ctrl) */ +# define ENGINE_CMD_FLAG_NUMERIC (unsigned int)0x0001 +/* + * accepts string input (cast from 'void*' to 'const char *', 4th parameter + * to ENGINE_ctrl) + */ +# define ENGINE_CMD_FLAG_STRING (unsigned int)0x0002 +/* + * Indicates that the control command takes *no* input. Ie. the control + * command is unparameterised. + */ +# define ENGINE_CMD_FLAG_NO_INPUT (unsigned int)0x0004 +/* + * Indicates that the control command is internal. This control command won't + * be shown in any output, and is only usable through the ENGINE_ctrl_cmd() + * function. + */ +# define ENGINE_CMD_FLAG_INTERNAL (unsigned int)0x0008 + +/* + * NB: These 3 control commands are deprecated and should not be used. + * ENGINEs relying on these commands should compile conditional support for + * compatibility (eg. if these symbols are defined) but should also migrate + * the same functionality to their own ENGINE-specific control functions that + * can be "discovered" by calling applications. The fact these control + * commands wouldn't be "executable" (ie. usable by text-based config) + * doesn't change the fact that application code can find and use them + * without requiring per-ENGINE hacking. + */ + +/* + * These flags are used to tell the ctrl function what should be done. All + * command numbers are shared between all engines, even if some don't make + * sense to some engines. In such a case, they do nothing but return the + * error ENGINE_R_CTRL_COMMAND_NOT_IMPLEMENTED. + */ +# define ENGINE_CTRL_SET_LOGSTREAM 1 +# define ENGINE_CTRL_SET_PASSWORD_CALLBACK 2 +# define ENGINE_CTRL_HUP 3/* Close and reinitialise + * any handles/connections + * etc. */ +# define ENGINE_CTRL_SET_USER_INTERFACE 4/* Alternative to callback */ +# define ENGINE_CTRL_SET_CALLBACK_DATA 5/* User-specific data, used + * when calling the password + * callback and the user + * interface */ +# define ENGINE_CTRL_LOAD_CONFIGURATION 6/* Load a configuration, + * given a string that + * represents a file name + * or so */ +# define ENGINE_CTRL_LOAD_SECTION 7/* Load data from a given + * section in the already + * loaded configuration */ + +/* + * These control commands allow an application to deal with an arbitrary + * engine in a dynamic way. Warn: Negative return values indicate errors FOR + * THESE COMMANDS because zero is used to indicate 'end-of-list'. Other + * commands, including ENGINE-specific command types, return zero for an + * error. An ENGINE can choose to implement these ctrl functions, and can + * internally manage things however it chooses - it does so by setting the + * ENGINE_FLAGS_MANUAL_CMD_CTRL flag (using ENGINE_set_flags()). Otherwise + * the ENGINE_ctrl() code handles this on the ENGINE's behalf using the + * cmd_defns data (set using ENGINE_set_cmd_defns()). This means an ENGINE's + * ctrl() handler need only implement its own commands - the above "meta" + * commands will be taken care of. + */ + +/* + * Returns non-zero if the supplied ENGINE has a ctrl() handler. If "not", + * then all the remaining control commands will return failure, so it is + * worth checking this first if the caller is trying to "discover" the + * engine's capabilities and doesn't want errors generated unnecessarily. + */ +# define ENGINE_CTRL_HAS_CTRL_FUNCTION 10 +/* + * Returns a positive command number for the first command supported by the + * engine. Returns zero if no ctrl commands are supported. + */ +# define ENGINE_CTRL_GET_FIRST_CMD_TYPE 11 +/* + * The 'long' argument specifies a command implemented by the engine, and the + * return value is the next command supported, or zero if there are no more. + */ +# define ENGINE_CTRL_GET_NEXT_CMD_TYPE 12 +/* + * The 'void*' argument is a command name (cast from 'const char *'), and the + * return value is the command that corresponds to it. + */ +# define ENGINE_CTRL_GET_CMD_FROM_NAME 13 +/* + * The next two allow a command to be converted into its corresponding string + * form. In each case, the 'long' argument supplies the command. In the + * NAME_LEN case, the return value is the length of the command name (not + * counting a trailing EOL). In the NAME case, the 'void*' argument must be a + * string buffer large enough, and it will be populated with the name of the + * command (WITH a trailing EOL). + */ +# define ENGINE_CTRL_GET_NAME_LEN_FROM_CMD 14 +# define ENGINE_CTRL_GET_NAME_FROM_CMD 15 +/* The next two are similar but give a "short description" of a command. */ +# define ENGINE_CTRL_GET_DESC_LEN_FROM_CMD 16 +# define ENGINE_CTRL_GET_DESC_FROM_CMD 17 +/* + * With this command, the return value is the OR'd combination of + * ENGINE_CMD_FLAG_*** values that indicate what kind of input a given + * engine-specific ctrl command expects. + */ +# define ENGINE_CTRL_GET_CMD_FLAGS 18 + +/* + * ENGINE implementations should start the numbering of their own control + * commands from this value. (ie. ENGINE_CMD_BASE, ENGINE_CMD_BASE + 1, etc). + */ +# define ENGINE_CMD_BASE 200 + +/* + * NB: These 2 nCipher "chil" control commands are deprecated, and their + * functionality is now available through ENGINE-specific control commands + * (exposed through the above-mentioned 'CMD'-handling). Code using these 2 + * commands should be migrated to the more general command handling before + * these are removed. + */ + +/* Flags specific to the nCipher "chil" engine */ +# define ENGINE_CTRL_CHIL_SET_FORKCHECK 100 + /* + * Depending on the value of the (long)i argument, this sets or + * unsets the SimpleForkCheck flag in the CHIL API to enable or + * disable checking and workarounds for applications that fork(). + */ +# define ENGINE_CTRL_CHIL_NO_LOCKING 101 + /* + * This prevents the initialisation function from providing mutex + * callbacks to the nCipher library. + */ + +/* + * If an ENGINE supports its own specific control commands and wishes the + * framework to handle the above 'ENGINE_CMD_***'-manipulation commands on + * its behalf, it should supply a null-terminated array of ENGINE_CMD_DEFN + * entries to ENGINE_set_cmd_defns(). It should also implement a ctrl() + * handler that supports the stated commands (ie. the "cmd_num" entries as + * described by the array). NB: The array must be ordered in increasing order + * of cmd_num. "null-terminated" means that the last ENGINE_CMD_DEFN element + * has cmd_num set to zero and/or cmd_name set to NULL. + */ +typedef struct ENGINE_CMD_DEFN_st { + unsigned int cmd_num; /* The command number */ + const char *cmd_name; /* The command name itself */ + const char *cmd_desc; /* A short description of the command */ + unsigned int cmd_flags; /* The input the command expects */ +} ENGINE_CMD_DEFN; + +/* Generic function pointer */ +typedef int (*ENGINE_GEN_FUNC_PTR) (void); +/* Generic function pointer taking no arguments */ +typedef int (*ENGINE_GEN_INT_FUNC_PTR) (ENGINE *); +/* Specific control function pointer */ +typedef int (*ENGINE_CTRL_FUNC_PTR) (ENGINE *, int, long, void *, + void (*f) (void)); +/* Generic load_key function pointer */ +typedef EVP_PKEY *(*ENGINE_LOAD_KEY_PTR)(ENGINE *, const char *, + UI_METHOD *ui_method, + void *callback_data); +typedef int (*ENGINE_SSL_CLIENT_CERT_PTR) (ENGINE *, SSL *ssl, + STACK_OF(X509_NAME) *ca_dn, + X509 **pcert, EVP_PKEY **pkey, + STACK_OF(X509) **pother, + UI_METHOD *ui_method, + void *callback_data); +/*- + * These callback types are for an ENGINE's handler for cipher and digest logic. + * These handlers have these prototypes; + * int foo(ENGINE *e, const EVP_CIPHER **cipher, const int **nids, int nid); + * int foo(ENGINE *e, const EVP_MD **digest, const int **nids, int nid); + * Looking at how to implement these handlers in the case of cipher support, if + * the framework wants the EVP_CIPHER for 'nid', it will call; + * foo(e, &p_evp_cipher, NULL, nid); (return zero for failure) + * If the framework wants a list of supported 'nid's, it will call; + * foo(e, NULL, &p_nids, 0); (returns number of 'nids' or -1 for error) + */ +/* + * Returns to a pointer to the array of supported cipher 'nid's. If the + * second parameter is non-NULL it is set to the size of the returned array. + */ +typedef int (*ENGINE_CIPHERS_PTR) (ENGINE *, const EVP_CIPHER **, + const int **, int); +typedef int (*ENGINE_DIGESTS_PTR) (ENGINE *, const EVP_MD **, const int **, + int); +typedef int (*ENGINE_PKEY_METHS_PTR) (ENGINE *, EVP_PKEY_METHOD **, + const int **, int); +typedef int (*ENGINE_PKEY_ASN1_METHS_PTR) (ENGINE *, EVP_PKEY_ASN1_METHOD **, + const int **, int); +/* + * STRUCTURE functions ... all of these functions deal with pointers to + * ENGINE structures where the pointers have a "structural reference". This + * means that their reference is to allowed access to the structure but it + * does not imply that the structure is functional. To simply increment or + * decrement the structural reference count, use ENGINE_by_id and + * ENGINE_free. NB: This is not required when iterating using ENGINE_get_next + * as it will automatically decrement the structural reference count of the + * "current" ENGINE and increment the structural reference count of the + * ENGINE it returns (unless it is NULL). + */ + +/* Get the first/last "ENGINE" type available. */ +ENGINE *ENGINE_get_first(void); +ENGINE *ENGINE_get_last(void); +/* Iterate to the next/previous "ENGINE" type (NULL = end of the list). */ +ENGINE *ENGINE_get_next(ENGINE *e); +ENGINE *ENGINE_get_prev(ENGINE *e); +/* Add another "ENGINE" type into the array. */ +int ENGINE_add(ENGINE *e); +/* Remove an existing "ENGINE" type from the array. */ +int ENGINE_remove(ENGINE *e); +/* Retrieve an engine from the list by its unique "id" value. */ +ENGINE *ENGINE_by_id(const char *id); + +#if OPENSSL_API_COMPAT < 0x10100000L +# define ENGINE_load_openssl() \ + OPENSSL_init_crypto(OPENSSL_INIT_ENGINE_OPENSSL, NULL) +# define ENGINE_load_dynamic() \ + OPENSSL_init_crypto(OPENSSL_INIT_ENGINE_DYNAMIC, NULL) +# ifndef OPENSSL_NO_STATIC_ENGINE +# define ENGINE_load_padlock() \ + OPENSSL_init_crypto(OPENSSL_INIT_ENGINE_PADLOCK, NULL) +# define ENGINE_load_capi() \ + OPENSSL_init_crypto(OPENSSL_INIT_ENGINE_CAPI, NULL) +# define ENGINE_load_afalg() \ + OPENSSL_init_crypto(OPENSSL_INIT_ENGINE_AFALG, NULL) +# endif +# define ENGINE_load_cryptodev() \ + OPENSSL_init_crypto(OPENSSL_INIT_ENGINE_CRYPTODEV, NULL) +# define ENGINE_load_rdrand() \ + OPENSSL_init_crypto(OPENSSL_INIT_ENGINE_RDRAND, NULL) +#endif +void ENGINE_load_builtin_engines(void); + +/* + * Get and set global flags (ENGINE_TABLE_FLAG_***) for the implementation + * "registry" handling. + */ +unsigned int ENGINE_get_table_flags(void); +void ENGINE_set_table_flags(unsigned int flags); + +/*- Manage registration of ENGINEs per "table". For each type, there are 3 + * functions; + * ENGINE_register_***(e) - registers the implementation from 'e' (if it has one) + * ENGINE_unregister_***(e) - unregister the implementation from 'e' + * ENGINE_register_all_***() - call ENGINE_register_***() for each 'e' in the list + * Cleanup is automatically registered from each table when required. + */ + +int ENGINE_register_RSA(ENGINE *e); +void ENGINE_unregister_RSA(ENGINE *e); +void ENGINE_register_all_RSA(void); + +int ENGINE_register_DSA(ENGINE *e); +void ENGINE_unregister_DSA(ENGINE *e); +void ENGINE_register_all_DSA(void); + +int ENGINE_register_EC(ENGINE *e); +void ENGINE_unregister_EC(ENGINE *e); +void ENGINE_register_all_EC(void); + +int ENGINE_register_DH(ENGINE *e); +void ENGINE_unregister_DH(ENGINE *e); +void ENGINE_register_all_DH(void); + +int ENGINE_register_RAND(ENGINE *e); +void ENGINE_unregister_RAND(ENGINE *e); +void ENGINE_register_all_RAND(void); + +int ENGINE_register_ciphers(ENGINE *e); +void ENGINE_unregister_ciphers(ENGINE *e); +void ENGINE_register_all_ciphers(void); + +int ENGINE_register_digests(ENGINE *e); +void ENGINE_unregister_digests(ENGINE *e); +void ENGINE_register_all_digests(void); + +int ENGINE_register_pkey_meths(ENGINE *e); +void ENGINE_unregister_pkey_meths(ENGINE *e); +void ENGINE_register_all_pkey_meths(void); + +int ENGINE_register_pkey_asn1_meths(ENGINE *e); +void ENGINE_unregister_pkey_asn1_meths(ENGINE *e); +void ENGINE_register_all_pkey_asn1_meths(void); + +/* + * These functions register all support from the above categories. Note, use + * of these functions can result in static linkage of code your application + * may not need. If you only need a subset of functionality, consider using + * more selective initialisation. + */ +int ENGINE_register_complete(ENGINE *e); +int ENGINE_register_all_complete(void); + +/* + * Send parameterised control commands to the engine. The possibilities to + * send down an integer, a pointer to data or a function pointer are + * provided. Any of the parameters may or may not be NULL, depending on the + * command number. In actuality, this function only requires a structural + * (rather than functional) reference to an engine, but many control commands + * may require the engine be functional. The caller should be aware of trying + * commands that require an operational ENGINE, and only use functional + * references in such situations. + */ +int ENGINE_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void)); + +/* + * This function tests if an ENGINE-specific command is usable as a + * "setting". Eg. in an application's config file that gets processed through + * ENGINE_ctrl_cmd_string(). If this returns zero, it is not available to + * ENGINE_ctrl_cmd_string(), only ENGINE_ctrl(). + */ +int ENGINE_cmd_is_executable(ENGINE *e, int cmd); + +/* + * This function works like ENGINE_ctrl() with the exception of taking a + * command name instead of a command number, and can handle optional + * commands. See the comment on ENGINE_ctrl_cmd_string() for an explanation + * on how to use the cmd_name and cmd_optional. + */ +int ENGINE_ctrl_cmd(ENGINE *e, const char *cmd_name, + long i, void *p, void (*f) (void), int cmd_optional); + +/* + * This function passes a command-name and argument to an ENGINE. The + * cmd_name is converted to a command number and the control command is + * called using 'arg' as an argument (unless the ENGINE doesn't support such + * a command, in which case no control command is called). The command is + * checked for input flags, and if necessary the argument will be converted + * to a numeric value. If cmd_optional is non-zero, then if the ENGINE + * doesn't support the given cmd_name the return value will be success + * anyway. This function is intended for applications to use so that users + * (or config files) can supply engine-specific config data to the ENGINE at + * run-time to control behaviour of specific engines. As such, it shouldn't + * be used for calling ENGINE_ctrl() functions that return data, deal with + * binary data, or that are otherwise supposed to be used directly through + * ENGINE_ctrl() in application code. Any "return" data from an ENGINE_ctrl() + * operation in this function will be lost - the return value is interpreted + * as failure if the return value is zero, success otherwise, and this + * function returns a boolean value as a result. In other words, vendors of + * 'ENGINE'-enabled devices should write ENGINE implementations with + * parameterisations that work in this scheme, so that compliant ENGINE-based + * applications can work consistently with the same configuration for the + * same ENGINE-enabled devices, across applications. + */ +int ENGINE_ctrl_cmd_string(ENGINE *e, const char *cmd_name, const char *arg, + int cmd_optional); + +/* + * These functions are useful for manufacturing new ENGINE structures. They + * don't address reference counting at all - one uses them to populate an + * ENGINE structure with personalised implementations of things prior to + * using it directly or adding it to the builtin ENGINE list in OpenSSL. + * These are also here so that the ENGINE structure doesn't have to be + * exposed and break binary compatibility! + */ +ENGINE *ENGINE_new(void); +int ENGINE_free(ENGINE *e); +int ENGINE_up_ref(ENGINE *e); +int ENGINE_set_id(ENGINE *e, const char *id); +int ENGINE_set_name(ENGINE *e, const char *name); +int ENGINE_set_RSA(ENGINE *e, const RSA_METHOD *rsa_meth); +int ENGINE_set_DSA(ENGINE *e, const DSA_METHOD *dsa_meth); +int ENGINE_set_EC(ENGINE *e, const EC_KEY_METHOD *ecdsa_meth); +int ENGINE_set_DH(ENGINE *e, const DH_METHOD *dh_meth); +int ENGINE_set_RAND(ENGINE *e, const RAND_METHOD *rand_meth); +int ENGINE_set_destroy_function(ENGINE *e, ENGINE_GEN_INT_FUNC_PTR destroy_f); +int ENGINE_set_init_function(ENGINE *e, ENGINE_GEN_INT_FUNC_PTR init_f); +int ENGINE_set_finish_function(ENGINE *e, ENGINE_GEN_INT_FUNC_PTR finish_f); +int ENGINE_set_ctrl_function(ENGINE *e, ENGINE_CTRL_FUNC_PTR ctrl_f); +int ENGINE_set_load_privkey_function(ENGINE *e, + ENGINE_LOAD_KEY_PTR loadpriv_f); +int ENGINE_set_load_pubkey_function(ENGINE *e, ENGINE_LOAD_KEY_PTR loadpub_f); +int ENGINE_set_load_ssl_client_cert_function(ENGINE *e, + ENGINE_SSL_CLIENT_CERT_PTR + loadssl_f); +int ENGINE_set_ciphers(ENGINE *e, ENGINE_CIPHERS_PTR f); +int ENGINE_set_digests(ENGINE *e, ENGINE_DIGESTS_PTR f); +int ENGINE_set_pkey_meths(ENGINE *e, ENGINE_PKEY_METHS_PTR f); +int ENGINE_set_pkey_asn1_meths(ENGINE *e, ENGINE_PKEY_ASN1_METHS_PTR f); +int ENGINE_set_flags(ENGINE *e, int flags); +int ENGINE_set_cmd_defns(ENGINE *e, const ENGINE_CMD_DEFN *defns); +/* These functions allow control over any per-structure ENGINE data. */ +#define ENGINE_get_ex_new_index(l, p, newf, dupf, freef) \ + CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_ENGINE, l, p, newf, dupf, freef) +int ENGINE_set_ex_data(ENGINE *e, int idx, void *arg); +void *ENGINE_get_ex_data(const ENGINE *e, int idx); + +#if OPENSSL_API_COMPAT < 0x10100000L +/* + * This function previously cleaned up anything that needs it. Auto-deinit will + * now take care of it so it is no longer required to call this function. + */ +# define ENGINE_cleanup() while(0) continue +#endif + +/* + * These return values from within the ENGINE structure. These can be useful + * with functional references as well as structural references - it depends + * which you obtained. Using the result for functional purposes if you only + * obtained a structural reference may be problematic! + */ +const char *ENGINE_get_id(const ENGINE *e); +const char *ENGINE_get_name(const ENGINE *e); +const RSA_METHOD *ENGINE_get_RSA(const ENGINE *e); +const DSA_METHOD *ENGINE_get_DSA(const ENGINE *e); +const EC_KEY_METHOD *ENGINE_get_EC(const ENGINE *e); +const DH_METHOD *ENGINE_get_DH(const ENGINE *e); +const RAND_METHOD *ENGINE_get_RAND(const ENGINE *e); +ENGINE_GEN_INT_FUNC_PTR ENGINE_get_destroy_function(const ENGINE *e); +ENGINE_GEN_INT_FUNC_PTR ENGINE_get_init_function(const ENGINE *e); +ENGINE_GEN_INT_FUNC_PTR ENGINE_get_finish_function(const ENGINE *e); +ENGINE_CTRL_FUNC_PTR ENGINE_get_ctrl_function(const ENGINE *e); +ENGINE_LOAD_KEY_PTR ENGINE_get_load_privkey_function(const ENGINE *e); +ENGINE_LOAD_KEY_PTR ENGINE_get_load_pubkey_function(const ENGINE *e); +ENGINE_SSL_CLIENT_CERT_PTR ENGINE_get_ssl_client_cert_function(const ENGINE + *e); +ENGINE_CIPHERS_PTR ENGINE_get_ciphers(const ENGINE *e); +ENGINE_DIGESTS_PTR ENGINE_get_digests(const ENGINE *e); +ENGINE_PKEY_METHS_PTR ENGINE_get_pkey_meths(const ENGINE *e); +ENGINE_PKEY_ASN1_METHS_PTR ENGINE_get_pkey_asn1_meths(const ENGINE *e); +const EVP_CIPHER *ENGINE_get_cipher(ENGINE *e, int nid); +const EVP_MD *ENGINE_get_digest(ENGINE *e, int nid); +const EVP_PKEY_METHOD *ENGINE_get_pkey_meth(ENGINE *e, int nid); +const EVP_PKEY_ASN1_METHOD *ENGINE_get_pkey_asn1_meth(ENGINE *e, int nid); +const EVP_PKEY_ASN1_METHOD *ENGINE_get_pkey_asn1_meth_str(ENGINE *e, + const char *str, + int len); +const EVP_PKEY_ASN1_METHOD *ENGINE_pkey_asn1_find_str(ENGINE **pe, + const char *str, + int len); +const ENGINE_CMD_DEFN *ENGINE_get_cmd_defns(const ENGINE *e); +int ENGINE_get_flags(const ENGINE *e); + +/* + * FUNCTIONAL functions. These functions deal with ENGINE structures that + * have (or will) be initialised for use. Broadly speaking, the structural + * functions are useful for iterating the list of available engine types, + * creating new engine types, and other "list" operations. These functions + * actually deal with ENGINEs that are to be used. As such these functions + * can fail (if applicable) when particular engines are unavailable - eg. if + * a hardware accelerator is not attached or not functioning correctly. Each + * ENGINE has 2 reference counts; structural and functional. Every time a + * functional reference is obtained or released, a corresponding structural + * reference is automatically obtained or released too. + */ + +/* + * Initialise a engine type for use (or up its reference count if it's + * already in use). This will fail if the engine is not currently operational + * and cannot initialise. + */ +int ENGINE_init(ENGINE *e); +/* + * Free a functional reference to a engine type. This does not require a + * corresponding call to ENGINE_free as it also releases a structural + * reference. + */ +int ENGINE_finish(ENGINE *e); + +/* + * The following functions handle keys that are stored in some secondary + * location, handled by the engine. The storage may be on a card or + * whatever. + */ +EVP_PKEY *ENGINE_load_private_key(ENGINE *e, const char *key_id, + UI_METHOD *ui_method, void *callback_data); +EVP_PKEY *ENGINE_load_public_key(ENGINE *e, const char *key_id, + UI_METHOD *ui_method, void *callback_data); +int ENGINE_load_ssl_client_cert(ENGINE *e, SSL *s, + STACK_OF(X509_NAME) *ca_dn, X509 **pcert, + EVP_PKEY **ppkey, STACK_OF(X509) **pother, + UI_METHOD *ui_method, void *callback_data); + +/* + * This returns a pointer for the current ENGINE structure that is (by + * default) performing any RSA operations. The value returned is an + * incremented reference, so it should be free'd (ENGINE_finish) before it is + * discarded. + */ +ENGINE *ENGINE_get_default_RSA(void); +/* Same for the other "methods" */ +ENGINE *ENGINE_get_default_DSA(void); +ENGINE *ENGINE_get_default_EC(void); +ENGINE *ENGINE_get_default_DH(void); +ENGINE *ENGINE_get_default_RAND(void); +/* + * These functions can be used to get a functional reference to perform + * ciphering or digesting corresponding to "nid". + */ +ENGINE *ENGINE_get_cipher_engine(int nid); +ENGINE *ENGINE_get_digest_engine(int nid); +ENGINE *ENGINE_get_pkey_meth_engine(int nid); +ENGINE *ENGINE_get_pkey_asn1_meth_engine(int nid); + +/* + * This sets a new default ENGINE structure for performing RSA operations. If + * the result is non-zero (success) then the ENGINE structure will have had + * its reference count up'd so the caller should still free their own + * reference 'e'. + */ +int ENGINE_set_default_RSA(ENGINE *e); +int ENGINE_set_default_string(ENGINE *e, const char *def_list); +/* Same for the other "methods" */ +int ENGINE_set_default_DSA(ENGINE *e); +int ENGINE_set_default_EC(ENGINE *e); +int ENGINE_set_default_DH(ENGINE *e); +int ENGINE_set_default_RAND(ENGINE *e); +int ENGINE_set_default_ciphers(ENGINE *e); +int ENGINE_set_default_digests(ENGINE *e); +int ENGINE_set_default_pkey_meths(ENGINE *e); +int ENGINE_set_default_pkey_asn1_meths(ENGINE *e); + +/* + * The combination "set" - the flags are bitwise "OR"d from the + * ENGINE_METHOD_*** defines above. As with the "ENGINE_register_complete()" + * function, this function can result in unnecessary static linkage. If your + * application requires only specific functionality, consider using more + * selective functions. + */ +int ENGINE_set_default(ENGINE *e, unsigned int flags); + +void ENGINE_add_conf_module(void); + +/* Deprecated functions ... */ +/* int ENGINE_clear_defaults(void); */ + +/**************************/ +/* DYNAMIC ENGINE SUPPORT */ +/**************************/ + +/* Binary/behaviour compatibility levels */ +# define OSSL_DYNAMIC_VERSION (unsigned long)0x00030000 +/* + * Binary versions older than this are too old for us (whether we're a loader + * or a loadee) + */ +# define OSSL_DYNAMIC_OLDEST (unsigned long)0x00030000 + +/* + * When compiling an ENGINE entirely as an external shared library, loadable + * by the "dynamic" ENGINE, these types are needed. The 'dynamic_fns' + * structure type provides the calling application's (or library's) error + * functionality and memory management function pointers to the loaded + * library. These should be used/set in the loaded library code so that the + * loading application's 'state' will be used/changed in all operations. The + * 'static_state' pointer allows the loaded library to know if it shares the + * same static data as the calling application (or library), and thus whether + * these callbacks need to be set or not. + */ +typedef void *(*dyn_MEM_malloc_fn) (size_t, const char *, int); +typedef void *(*dyn_MEM_realloc_fn) (void *, size_t, const char *, int); +typedef void (*dyn_MEM_free_fn) (void *, const char *, int); +typedef struct st_dynamic_MEM_fns { + dyn_MEM_malloc_fn malloc_fn; + dyn_MEM_realloc_fn realloc_fn; + dyn_MEM_free_fn free_fn; +} dynamic_MEM_fns; +/* + * FIXME: Perhaps the memory and locking code (crypto.h) should declare and + * use these types so we (and any other dependent code) can simplify a bit?? + */ +/* The top-level structure */ +typedef struct st_dynamic_fns { + void *static_state; + dynamic_MEM_fns mem_fns; +} dynamic_fns; + +/* + * The version checking function should be of this prototype. NB: The + * ossl_version value passed in is the OSSL_DYNAMIC_VERSION of the loading + * code. If this function returns zero, it indicates a (potential) version + * incompatibility and the loaded library doesn't believe it can proceed. + * Otherwise, the returned value is the (latest) version supported by the + * loading library. The loader may still decide that the loaded code's + * version is unsatisfactory and could veto the load. The function is + * expected to be implemented with the symbol name "v_check", and a default + * implementation can be fully instantiated with + * IMPLEMENT_DYNAMIC_CHECK_FN(). + */ +typedef unsigned long (*dynamic_v_check_fn) (unsigned long ossl_version); +# define IMPLEMENT_DYNAMIC_CHECK_FN() \ + OPENSSL_EXPORT unsigned long v_check(unsigned long v); \ + OPENSSL_EXPORT unsigned long v_check(unsigned long v) { \ + if (v >= OSSL_DYNAMIC_OLDEST) return OSSL_DYNAMIC_VERSION; \ + return 0; } + +/* + * This function is passed the ENGINE structure to initialise with its own + * function and command settings. It should not adjust the structural or + * functional reference counts. If this function returns zero, (a) the load + * will be aborted, (b) the previous ENGINE state will be memcpy'd back onto + * the structure, and (c) the shared library will be unloaded. So + * implementations should do their own internal cleanup in failure + * circumstances otherwise they could leak. The 'id' parameter, if non-NULL, + * represents the ENGINE id that the loader is looking for. If this is NULL, + * the shared library can choose to return failure or to initialise a + * 'default' ENGINE. If non-NULL, the shared library must initialise only an + * ENGINE matching the passed 'id'. The function is expected to be + * implemented with the symbol name "bind_engine". A standard implementation + * can be instantiated with IMPLEMENT_DYNAMIC_BIND_FN(fn) where the parameter + * 'fn' is a callback function that populates the ENGINE structure and + * returns an int value (zero for failure). 'fn' should have prototype; + * [static] int fn(ENGINE *e, const char *id); + */ +typedef int (*dynamic_bind_engine) (ENGINE *e, const char *id, + const dynamic_fns *fns); +# define IMPLEMENT_DYNAMIC_BIND_FN(fn) \ + OPENSSL_EXPORT \ + int bind_engine(ENGINE *e, const char *id, const dynamic_fns *fns); \ + OPENSSL_EXPORT \ + int bind_engine(ENGINE *e, const char *id, const dynamic_fns *fns) { \ + if (ENGINE_get_static_state() == fns->static_state) goto skip_cbs; \ + CRYPTO_set_mem_functions(fns->mem_fns.malloc_fn, \ + fns->mem_fns.realloc_fn, \ + fns->mem_fns.free_fn); \ + skip_cbs: \ + if (!fn(e, id)) return 0; \ + return 1; } + +/* + * If the loading application (or library) and the loaded ENGINE library + * share the same static data (eg. they're both dynamically linked to the + * same libcrypto.so) we need a way to avoid trying to set system callbacks - + * this would fail, and for the same reason that it's unnecessary to try. If + * the loaded ENGINE has (or gets from through the loader) its own copy of + * the libcrypto static data, we will need to set the callbacks. The easiest + * way to detect this is to have a function that returns a pointer to some + * static data and let the loading application and loaded ENGINE compare + * their respective values. + */ +void *ENGINE_get_static_state(void); + +# if defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__DragonFly__) +DEPRECATEDIN_1_1_0(void ENGINE_setup_bsd_cryptodev(void)) +# endif + + +# ifdef __cplusplus +} +# endif +# endif +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/engineerr.h b/example/android/third_party/crypto/arm/include/openssl/engineerr.h new file mode 100644 index 00000000..b4c036b2 --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/engineerr.h @@ -0,0 +1,107 @@ +/* + * Generated by util/mkerr.pl DO NOT EDIT + * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_ENGINEERR_H +# define HEADER_ENGINEERR_H + +# include + +# ifndef OPENSSL_NO_ENGINE + +# ifdef __cplusplus +extern "C" +# endif +int ERR_load_ENGINE_strings(void); + +/* + * ENGINE function codes. + */ +# define ENGINE_F_DIGEST_UPDATE 198 +# define ENGINE_F_DYNAMIC_CTRL 180 +# define ENGINE_F_DYNAMIC_GET_DATA_CTX 181 +# define ENGINE_F_DYNAMIC_LOAD 182 +# define ENGINE_F_DYNAMIC_SET_DATA_CTX 183 +# define ENGINE_F_ENGINE_ADD 105 +# define ENGINE_F_ENGINE_BY_ID 106 +# define ENGINE_F_ENGINE_CMD_IS_EXECUTABLE 170 +# define ENGINE_F_ENGINE_CTRL 142 +# define ENGINE_F_ENGINE_CTRL_CMD 178 +# define ENGINE_F_ENGINE_CTRL_CMD_STRING 171 +# define ENGINE_F_ENGINE_FINISH 107 +# define ENGINE_F_ENGINE_GET_CIPHER 185 +# define ENGINE_F_ENGINE_GET_DIGEST 186 +# define ENGINE_F_ENGINE_GET_FIRST 195 +# define ENGINE_F_ENGINE_GET_LAST 196 +# define ENGINE_F_ENGINE_GET_NEXT 115 +# define ENGINE_F_ENGINE_GET_PKEY_ASN1_METH 193 +# define ENGINE_F_ENGINE_GET_PKEY_METH 192 +# define ENGINE_F_ENGINE_GET_PREV 116 +# define ENGINE_F_ENGINE_INIT 119 +# define ENGINE_F_ENGINE_LIST_ADD 120 +# define ENGINE_F_ENGINE_LIST_REMOVE 121 +# define ENGINE_F_ENGINE_LOAD_PRIVATE_KEY 150 +# define ENGINE_F_ENGINE_LOAD_PUBLIC_KEY 151 +# define ENGINE_F_ENGINE_LOAD_SSL_CLIENT_CERT 194 +# define ENGINE_F_ENGINE_NEW 122 +# define ENGINE_F_ENGINE_PKEY_ASN1_FIND_STR 197 +# define ENGINE_F_ENGINE_REMOVE 123 +# define ENGINE_F_ENGINE_SET_DEFAULT_STRING 189 +# define ENGINE_F_ENGINE_SET_ID 129 +# define ENGINE_F_ENGINE_SET_NAME 130 +# define ENGINE_F_ENGINE_TABLE_REGISTER 184 +# define ENGINE_F_ENGINE_UNLOCKED_FINISH 191 +# define ENGINE_F_ENGINE_UP_REF 190 +# define ENGINE_F_INT_CLEANUP_ITEM 199 +# define ENGINE_F_INT_CTRL_HELPER 172 +# define ENGINE_F_INT_ENGINE_CONFIGURE 188 +# define ENGINE_F_INT_ENGINE_MODULE_INIT 187 +# define ENGINE_F_OSSL_HMAC_INIT 200 + +/* + * ENGINE reason codes. + */ +# define ENGINE_R_ALREADY_LOADED 100 +# define ENGINE_R_ARGUMENT_IS_NOT_A_NUMBER 133 +# define ENGINE_R_CMD_NOT_EXECUTABLE 134 +# define ENGINE_R_COMMAND_TAKES_INPUT 135 +# define ENGINE_R_COMMAND_TAKES_NO_INPUT 136 +# define ENGINE_R_CONFLICTING_ENGINE_ID 103 +# define ENGINE_R_CTRL_COMMAND_NOT_IMPLEMENTED 119 +# define ENGINE_R_DSO_FAILURE 104 +# define ENGINE_R_DSO_NOT_FOUND 132 +# define ENGINE_R_ENGINES_SECTION_ERROR 148 +# define ENGINE_R_ENGINE_CONFIGURATION_ERROR 102 +# define ENGINE_R_ENGINE_IS_NOT_IN_LIST 105 +# define ENGINE_R_ENGINE_SECTION_ERROR 149 +# define ENGINE_R_FAILED_LOADING_PRIVATE_KEY 128 +# define ENGINE_R_FAILED_LOADING_PUBLIC_KEY 129 +# define ENGINE_R_FINISH_FAILED 106 +# define ENGINE_R_ID_OR_NAME_MISSING 108 +# define ENGINE_R_INIT_FAILED 109 +# define ENGINE_R_INTERNAL_LIST_ERROR 110 +# define ENGINE_R_INVALID_ARGUMENT 143 +# define ENGINE_R_INVALID_CMD_NAME 137 +# define ENGINE_R_INVALID_CMD_NUMBER 138 +# define ENGINE_R_INVALID_INIT_VALUE 151 +# define ENGINE_R_INVALID_STRING 150 +# define ENGINE_R_NOT_INITIALISED 117 +# define ENGINE_R_NOT_LOADED 112 +# define ENGINE_R_NO_CONTROL_FUNCTION 120 +# define ENGINE_R_NO_INDEX 144 +# define ENGINE_R_NO_LOAD_FUNCTION 125 +# define ENGINE_R_NO_REFERENCE 130 +# define ENGINE_R_NO_SUCH_ENGINE 116 +# define ENGINE_R_UNIMPLEMENTED_CIPHER 146 +# define ENGINE_R_UNIMPLEMENTED_DIGEST 147 +# define ENGINE_R_UNIMPLEMENTED_PUBLIC_KEY_METHOD 101 +# define ENGINE_R_VERSION_INCOMPATIBILITY 145 + +# endif +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/err.h b/example/android/third_party/crypto/arm/include/openssl/err.h new file mode 100644 index 00000000..6cae1a36 --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/err.h @@ -0,0 +1,273 @@ +/* + * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_ERR_H +# define HEADER_ERR_H + +# include + +# ifndef OPENSSL_NO_STDIO +# include +# include +# endif + +# include +# include +# include + +#ifdef __cplusplus +extern "C" { +#endif + +# ifndef OPENSSL_NO_ERR +# define ERR_PUT_error(a,b,c,d,e) ERR_put_error(a,b,c,d,e) +# else +# define ERR_PUT_error(a,b,c,d,e) ERR_put_error(a,b,c,NULL,0) +# endif + +# include + +# define ERR_TXT_MALLOCED 0x01 +# define ERR_TXT_STRING 0x02 + +# define ERR_FLAG_MARK 0x01 + +# define ERR_NUM_ERRORS 16 +typedef struct err_state_st { + int err_flags[ERR_NUM_ERRORS]; + unsigned long err_buffer[ERR_NUM_ERRORS]; + char *err_data[ERR_NUM_ERRORS]; + int err_data_flags[ERR_NUM_ERRORS]; + const char *err_file[ERR_NUM_ERRORS]; + int err_line[ERR_NUM_ERRORS]; + int top, bottom; +} ERR_STATE; + +/* library */ +# define ERR_LIB_NONE 1 +# define ERR_LIB_SYS 2 +# define ERR_LIB_BN 3 +# define ERR_LIB_RSA 4 +# define ERR_LIB_DH 5 +# define ERR_LIB_EVP 6 +# define ERR_LIB_BUF 7 +# define ERR_LIB_OBJ 8 +# define ERR_LIB_PEM 9 +# define ERR_LIB_DSA 10 +# define ERR_LIB_X509 11 +/* #define ERR_LIB_METH 12 */ +# define ERR_LIB_ASN1 13 +# define ERR_LIB_CONF 14 +# define ERR_LIB_CRYPTO 15 +# define ERR_LIB_EC 16 +# define ERR_LIB_SSL 20 +/* #define ERR_LIB_SSL23 21 */ +/* #define ERR_LIB_SSL2 22 */ +/* #define ERR_LIB_SSL3 23 */ +/* #define ERR_LIB_RSAREF 30 */ +/* #define ERR_LIB_PROXY 31 */ +# define ERR_LIB_BIO 32 +# define ERR_LIB_PKCS7 33 +# define ERR_LIB_X509V3 34 +# define ERR_LIB_PKCS12 35 +# define ERR_LIB_RAND 36 +# define ERR_LIB_DSO 37 +# define ERR_LIB_ENGINE 38 +# define ERR_LIB_OCSP 39 +# define ERR_LIB_UI 40 +# define ERR_LIB_COMP 41 +# define ERR_LIB_ECDSA 42 +# define ERR_LIB_ECDH 43 +# define ERR_LIB_OSSL_STORE 44 +# define ERR_LIB_FIPS 45 +# define ERR_LIB_CMS 46 +# define ERR_LIB_TS 47 +# define ERR_LIB_HMAC 48 +/* # define ERR_LIB_JPAKE 49 */ +# define ERR_LIB_CT 50 +# define ERR_LIB_ASYNC 51 +# define ERR_LIB_KDF 52 +# define ERR_LIB_SM2 53 + +# define ERR_LIB_USER 128 + +# define SYSerr(f,r) ERR_PUT_error(ERR_LIB_SYS,(f),(r),OPENSSL_FILE,OPENSSL_LINE) +# define BNerr(f,r) ERR_PUT_error(ERR_LIB_BN,(f),(r),OPENSSL_FILE,OPENSSL_LINE) +# define RSAerr(f,r) ERR_PUT_error(ERR_LIB_RSA,(f),(r),OPENSSL_FILE,OPENSSL_LINE) +# define DHerr(f,r) ERR_PUT_error(ERR_LIB_DH,(f),(r),OPENSSL_FILE,OPENSSL_LINE) +# define EVPerr(f,r) ERR_PUT_error(ERR_LIB_EVP,(f),(r),OPENSSL_FILE,OPENSSL_LINE) +# define BUFerr(f,r) ERR_PUT_error(ERR_LIB_BUF,(f),(r),OPENSSL_FILE,OPENSSL_LINE) +# define OBJerr(f,r) ERR_PUT_error(ERR_LIB_OBJ,(f),(r),OPENSSL_FILE,OPENSSL_LINE) +# define PEMerr(f,r) ERR_PUT_error(ERR_LIB_PEM,(f),(r),OPENSSL_FILE,OPENSSL_LINE) +# define DSAerr(f,r) ERR_PUT_error(ERR_LIB_DSA,(f),(r),OPENSSL_FILE,OPENSSL_LINE) +# define X509err(f,r) ERR_PUT_error(ERR_LIB_X509,(f),(r),OPENSSL_FILE,OPENSSL_LINE) +# define ASN1err(f,r) ERR_PUT_error(ERR_LIB_ASN1,(f),(r),OPENSSL_FILE,OPENSSL_LINE) +# define CONFerr(f,r) ERR_PUT_error(ERR_LIB_CONF,(f),(r),OPENSSL_FILE,OPENSSL_LINE) +# define CRYPTOerr(f,r) ERR_PUT_error(ERR_LIB_CRYPTO,(f),(r),OPENSSL_FILE,OPENSSL_LINE) +# define ECerr(f,r) ERR_PUT_error(ERR_LIB_EC,(f),(r),OPENSSL_FILE,OPENSSL_LINE) +# define SSLerr(f,r) ERR_PUT_error(ERR_LIB_SSL,(f),(r),OPENSSL_FILE,OPENSSL_LINE) +# define BIOerr(f,r) ERR_PUT_error(ERR_LIB_BIO,(f),(r),OPENSSL_FILE,OPENSSL_LINE) +# define PKCS7err(f,r) ERR_PUT_error(ERR_LIB_PKCS7,(f),(r),OPENSSL_FILE,OPENSSL_LINE) +# define X509V3err(f,r) ERR_PUT_error(ERR_LIB_X509V3,(f),(r),OPENSSL_FILE,OPENSSL_LINE) +# define PKCS12err(f,r) ERR_PUT_error(ERR_LIB_PKCS12,(f),(r),OPENSSL_FILE,OPENSSL_LINE) +# define RANDerr(f,r) ERR_PUT_error(ERR_LIB_RAND,(f),(r),OPENSSL_FILE,OPENSSL_LINE) +# define DSOerr(f,r) ERR_PUT_error(ERR_LIB_DSO,(f),(r),OPENSSL_FILE,OPENSSL_LINE) +# define ENGINEerr(f,r) ERR_PUT_error(ERR_LIB_ENGINE,(f),(r),OPENSSL_FILE,OPENSSL_LINE) +# define OCSPerr(f,r) ERR_PUT_error(ERR_LIB_OCSP,(f),(r),OPENSSL_FILE,OPENSSL_LINE) +# define UIerr(f,r) ERR_PUT_error(ERR_LIB_UI,(f),(r),OPENSSL_FILE,OPENSSL_LINE) +# define COMPerr(f,r) ERR_PUT_error(ERR_LIB_COMP,(f),(r),OPENSSL_FILE,OPENSSL_LINE) +# define ECDSAerr(f,r) ERR_PUT_error(ERR_LIB_ECDSA,(f),(r),OPENSSL_FILE,OPENSSL_LINE) +# define ECDHerr(f,r) ERR_PUT_error(ERR_LIB_ECDH,(f),(r),OPENSSL_FILE,OPENSSL_LINE) +# define OSSL_STOREerr(f,r) ERR_PUT_error(ERR_LIB_OSSL_STORE,(f),(r),OPENSSL_FILE,OPENSSL_LINE) +# define FIPSerr(f,r) ERR_PUT_error(ERR_LIB_FIPS,(f),(r),OPENSSL_FILE,OPENSSL_LINE) +# define CMSerr(f,r) ERR_PUT_error(ERR_LIB_CMS,(f),(r),OPENSSL_FILE,OPENSSL_LINE) +# define TSerr(f,r) ERR_PUT_error(ERR_LIB_TS,(f),(r),OPENSSL_FILE,OPENSSL_LINE) +# define HMACerr(f,r) ERR_PUT_error(ERR_LIB_HMAC,(f),(r),OPENSSL_FILE,OPENSSL_LINE) +# define CTerr(f,r) ERR_PUT_error(ERR_LIB_CT,(f),(r),OPENSSL_FILE,OPENSSL_LINE) +# define ASYNCerr(f,r) ERR_PUT_error(ERR_LIB_ASYNC,(f),(r),OPENSSL_FILE,OPENSSL_LINE) +# define KDFerr(f,r) ERR_PUT_error(ERR_LIB_KDF,(f),(r),OPENSSL_FILE,OPENSSL_LINE) +# define SM2err(f,r) ERR_PUT_error(ERR_LIB_SM2,(f),(r),OPENSSL_FILE,OPENSSL_LINE) + +# define ERR_PACK(l,f,r) ( \ + (((unsigned int)(l) & 0x0FF) << 24L) | \ + (((unsigned int)(f) & 0xFFF) << 12L) | \ + (((unsigned int)(r) & 0xFFF) ) ) +# define ERR_GET_LIB(l) (int)(((l) >> 24L) & 0x0FFL) +# define ERR_GET_FUNC(l) (int)(((l) >> 12L) & 0xFFFL) +# define ERR_GET_REASON(l) (int)( (l) & 0xFFFL) +# define ERR_FATAL_ERROR(l) (int)( (l) & ERR_R_FATAL) + +/* OS functions */ +# define SYS_F_FOPEN 1 +# define SYS_F_CONNECT 2 +# define SYS_F_GETSERVBYNAME 3 +# define SYS_F_SOCKET 4 +# define SYS_F_IOCTLSOCKET 5 +# define SYS_F_BIND 6 +# define SYS_F_LISTEN 7 +# define SYS_F_ACCEPT 8 +# define SYS_F_WSASTARTUP 9/* Winsock stuff */ +# define SYS_F_OPENDIR 10 +# define SYS_F_FREAD 11 +# define SYS_F_GETADDRINFO 12 +# define SYS_F_GETNAMEINFO 13 +# define SYS_F_SETSOCKOPT 14 +# define SYS_F_GETSOCKOPT 15 +# define SYS_F_GETSOCKNAME 16 +# define SYS_F_GETHOSTBYNAME 17 +# define SYS_F_FFLUSH 18 +# define SYS_F_OPEN 19 +# define SYS_F_CLOSE 20 +# define SYS_F_IOCTL 21 +# define SYS_F_STAT 22 +# define SYS_F_FCNTL 23 +# define SYS_F_FSTAT 24 + +/* reasons */ +# define ERR_R_SYS_LIB ERR_LIB_SYS/* 2 */ +# define ERR_R_BN_LIB ERR_LIB_BN/* 3 */ +# define ERR_R_RSA_LIB ERR_LIB_RSA/* 4 */ +# define ERR_R_DH_LIB ERR_LIB_DH/* 5 */ +# define ERR_R_EVP_LIB ERR_LIB_EVP/* 6 */ +# define ERR_R_BUF_LIB ERR_LIB_BUF/* 7 */ +# define ERR_R_OBJ_LIB ERR_LIB_OBJ/* 8 */ +# define ERR_R_PEM_LIB ERR_LIB_PEM/* 9 */ +# define ERR_R_DSA_LIB ERR_LIB_DSA/* 10 */ +# define ERR_R_X509_LIB ERR_LIB_X509/* 11 */ +# define ERR_R_ASN1_LIB ERR_LIB_ASN1/* 13 */ +# define ERR_R_EC_LIB ERR_LIB_EC/* 16 */ +# define ERR_R_BIO_LIB ERR_LIB_BIO/* 32 */ +# define ERR_R_PKCS7_LIB ERR_LIB_PKCS7/* 33 */ +# define ERR_R_X509V3_LIB ERR_LIB_X509V3/* 34 */ +# define ERR_R_ENGINE_LIB ERR_LIB_ENGINE/* 38 */ +# define ERR_R_UI_LIB ERR_LIB_UI/* 40 */ +# define ERR_R_ECDSA_LIB ERR_LIB_ECDSA/* 42 */ +# define ERR_R_OSSL_STORE_LIB ERR_LIB_OSSL_STORE/* 44 */ + +# define ERR_R_NESTED_ASN1_ERROR 58 +# define ERR_R_MISSING_ASN1_EOS 63 + +/* fatal error */ +# define ERR_R_FATAL 64 +# define ERR_R_MALLOC_FAILURE (1|ERR_R_FATAL) +# define ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED (2|ERR_R_FATAL) +# define ERR_R_PASSED_NULL_PARAMETER (3|ERR_R_FATAL) +# define ERR_R_INTERNAL_ERROR (4|ERR_R_FATAL) +# define ERR_R_DISABLED (5|ERR_R_FATAL) +# define ERR_R_INIT_FAIL (6|ERR_R_FATAL) +# define ERR_R_PASSED_INVALID_ARGUMENT (7) +# define ERR_R_OPERATION_FAIL (8|ERR_R_FATAL) + +/* + * 99 is the maximum possible ERR_R_... code, higher values are reserved for + * the individual libraries + */ + +typedef struct ERR_string_data_st { + unsigned long error; + const char *string; +} ERR_STRING_DATA; + +DEFINE_LHASH_OF(ERR_STRING_DATA); + +void ERR_put_error(int lib, int func, int reason, const char *file, int line); +void ERR_set_error_data(char *data, int flags); + +unsigned long ERR_get_error(void); +unsigned long ERR_get_error_line(const char **file, int *line); +unsigned long ERR_get_error_line_data(const char **file, int *line, + const char **data, int *flags); +unsigned long ERR_peek_error(void); +unsigned long ERR_peek_error_line(const char **file, int *line); +unsigned long ERR_peek_error_line_data(const char **file, int *line, + const char **data, int *flags); +unsigned long ERR_peek_last_error(void); +unsigned long ERR_peek_last_error_line(const char **file, int *line); +unsigned long ERR_peek_last_error_line_data(const char **file, int *line, + const char **data, int *flags); +void ERR_clear_error(void); +char *ERR_error_string(unsigned long e, char *buf); +void ERR_error_string_n(unsigned long e, char *buf, size_t len); +const char *ERR_lib_error_string(unsigned long e); +const char *ERR_func_error_string(unsigned long e); +const char *ERR_reason_error_string(unsigned long e); +void ERR_print_errors_cb(int (*cb) (const char *str, size_t len, void *u), + void *u); +# ifndef OPENSSL_NO_STDIO +void ERR_print_errors_fp(FILE *fp); +# endif +void ERR_print_errors(BIO *bp); +void ERR_add_error_data(int num, ...); +void ERR_add_error_vdata(int num, va_list args); +int ERR_load_strings(int lib, ERR_STRING_DATA *str); +int ERR_load_strings_const(const ERR_STRING_DATA *str); +int ERR_unload_strings(int lib, ERR_STRING_DATA *str); +int ERR_load_ERR_strings(void); + +#if OPENSSL_API_COMPAT < 0x10100000L +# define ERR_load_crypto_strings() \ + OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL) +# define ERR_free_strings() while(0) continue +#endif + +DEPRECATEDIN_1_1_0(void ERR_remove_thread_state(void *)) +DEPRECATEDIN_1_0_0(void ERR_remove_state(unsigned long pid)) +ERR_STATE *ERR_get_state(void); + +int ERR_get_next_error_library(void); + +int ERR_set_mark(void); +int ERR_pop_to_mark(void); +int ERR_clear_last_mark(void); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/evp.h b/example/android/third_party/crypto/arm/include/openssl/evp.h new file mode 100644 index 00000000..8c805199 --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/evp.h @@ -0,0 +1,1638 @@ +/* + * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_ENVELOPE_H +# define HEADER_ENVELOPE_H + +# include +# include +# include +# include +# include + +# define EVP_MAX_MD_SIZE 64/* longest known is SHA512 */ +# define EVP_MAX_KEY_LENGTH 64 +# define EVP_MAX_IV_LENGTH 16 +# define EVP_MAX_BLOCK_LENGTH 32 + +# define PKCS5_SALT_LEN 8 +/* Default PKCS#5 iteration count */ +# define PKCS5_DEFAULT_ITER 2048 + +# include + +# define EVP_PK_RSA 0x0001 +# define EVP_PK_DSA 0x0002 +# define EVP_PK_DH 0x0004 +# define EVP_PK_EC 0x0008 +# define EVP_PKT_SIGN 0x0010 +# define EVP_PKT_ENC 0x0020 +# define EVP_PKT_EXCH 0x0040 +# define EVP_PKS_RSA 0x0100 +# define EVP_PKS_DSA 0x0200 +# define EVP_PKS_EC 0x0400 + +# define EVP_PKEY_NONE NID_undef +# define EVP_PKEY_RSA NID_rsaEncryption +# define EVP_PKEY_RSA2 NID_rsa +# define EVP_PKEY_RSA_PSS NID_rsassaPss +# define EVP_PKEY_DSA NID_dsa +# define EVP_PKEY_DSA1 NID_dsa_2 +# define EVP_PKEY_DSA2 NID_dsaWithSHA +# define EVP_PKEY_DSA3 NID_dsaWithSHA1 +# define EVP_PKEY_DSA4 NID_dsaWithSHA1_2 +# define EVP_PKEY_DH NID_dhKeyAgreement +# define EVP_PKEY_DHX NID_dhpublicnumber +# define EVP_PKEY_EC NID_X9_62_id_ecPublicKey +# define EVP_PKEY_SM2 NID_sm2 +# define EVP_PKEY_HMAC NID_hmac +# define EVP_PKEY_CMAC NID_cmac +# define EVP_PKEY_SCRYPT NID_id_scrypt +# define EVP_PKEY_TLS1_PRF NID_tls1_prf +# define EVP_PKEY_HKDF NID_hkdf +# define EVP_PKEY_POLY1305 NID_poly1305 +# define EVP_PKEY_SIPHASH NID_siphash +# define EVP_PKEY_X25519 NID_X25519 +# define EVP_PKEY_ED25519 NID_ED25519 +# define EVP_PKEY_X448 NID_X448 +# define EVP_PKEY_ED448 NID_ED448 + +#ifdef __cplusplus +extern "C" { +#endif + +# define EVP_PKEY_MO_SIGN 0x0001 +# define EVP_PKEY_MO_VERIFY 0x0002 +# define EVP_PKEY_MO_ENCRYPT 0x0004 +# define EVP_PKEY_MO_DECRYPT 0x0008 + +# ifndef EVP_MD +EVP_MD *EVP_MD_meth_new(int md_type, int pkey_type); +EVP_MD *EVP_MD_meth_dup(const EVP_MD *md); +void EVP_MD_meth_free(EVP_MD *md); + +int EVP_MD_meth_set_input_blocksize(EVP_MD *md, int blocksize); +int EVP_MD_meth_set_result_size(EVP_MD *md, int resultsize); +int EVP_MD_meth_set_app_datasize(EVP_MD *md, int datasize); +int EVP_MD_meth_set_flags(EVP_MD *md, unsigned long flags); +int EVP_MD_meth_set_init(EVP_MD *md, int (*init)(EVP_MD_CTX *ctx)); +int EVP_MD_meth_set_update(EVP_MD *md, int (*update)(EVP_MD_CTX *ctx, + const void *data, + size_t count)); +int EVP_MD_meth_set_final(EVP_MD *md, int (*final)(EVP_MD_CTX *ctx, + unsigned char *md)); +int EVP_MD_meth_set_copy(EVP_MD *md, int (*copy)(EVP_MD_CTX *to, + const EVP_MD_CTX *from)); +int EVP_MD_meth_set_cleanup(EVP_MD *md, int (*cleanup)(EVP_MD_CTX *ctx)); +int EVP_MD_meth_set_ctrl(EVP_MD *md, int (*ctrl)(EVP_MD_CTX *ctx, int cmd, + int p1, void *p2)); + +int EVP_MD_meth_get_input_blocksize(const EVP_MD *md); +int EVP_MD_meth_get_result_size(const EVP_MD *md); +int EVP_MD_meth_get_app_datasize(const EVP_MD *md); +unsigned long EVP_MD_meth_get_flags(const EVP_MD *md); +int (*EVP_MD_meth_get_init(const EVP_MD *md))(EVP_MD_CTX *ctx); +int (*EVP_MD_meth_get_update(const EVP_MD *md))(EVP_MD_CTX *ctx, + const void *data, + size_t count); +int (*EVP_MD_meth_get_final(const EVP_MD *md))(EVP_MD_CTX *ctx, + unsigned char *md); +int (*EVP_MD_meth_get_copy(const EVP_MD *md))(EVP_MD_CTX *to, + const EVP_MD_CTX *from); +int (*EVP_MD_meth_get_cleanup(const EVP_MD *md))(EVP_MD_CTX *ctx); +int (*EVP_MD_meth_get_ctrl(const EVP_MD *md))(EVP_MD_CTX *ctx, int cmd, + int p1, void *p2); + +/* digest can only handle a single block */ +# define EVP_MD_FLAG_ONESHOT 0x0001 + +/* digest is extensible-output function, XOF */ +# define EVP_MD_FLAG_XOF 0x0002 + +/* DigestAlgorithmIdentifier flags... */ + +# define EVP_MD_FLAG_DIGALGID_MASK 0x0018 + +/* NULL or absent parameter accepted. Use NULL */ + +# define EVP_MD_FLAG_DIGALGID_NULL 0x0000 + +/* NULL or absent parameter accepted. Use NULL for PKCS#1 otherwise absent */ + +# define EVP_MD_FLAG_DIGALGID_ABSENT 0x0008 + +/* Custom handling via ctrl */ + +# define EVP_MD_FLAG_DIGALGID_CUSTOM 0x0018 + +/* Note if suitable for use in FIPS mode */ +# define EVP_MD_FLAG_FIPS 0x0400 + +/* Digest ctrls */ + +# define EVP_MD_CTRL_DIGALGID 0x1 +# define EVP_MD_CTRL_MICALG 0x2 +# define EVP_MD_CTRL_XOF_LEN 0x3 + +/* Minimum Algorithm specific ctrl value */ + +# define EVP_MD_CTRL_ALG_CTRL 0x1000 + +# endif /* !EVP_MD */ + +/* values for EVP_MD_CTX flags */ + +# define EVP_MD_CTX_FLAG_ONESHOT 0x0001/* digest update will be + * called once only */ +# define EVP_MD_CTX_FLAG_CLEANED 0x0002/* context has already been + * cleaned */ +# define EVP_MD_CTX_FLAG_REUSE 0x0004/* Don't free up ctx->md_data + * in EVP_MD_CTX_reset */ +/* + * FIPS and pad options are ignored in 1.0.0, definitions are here so we + * don't accidentally reuse the values for other purposes. + */ + +# define EVP_MD_CTX_FLAG_NON_FIPS_ALLOW 0x0008/* Allow use of non FIPS + * digest in FIPS mode */ + +/* + * The following PAD options are also currently ignored in 1.0.0, digest + * parameters are handled through EVP_DigestSign*() and EVP_DigestVerify*() + * instead. + */ +# define EVP_MD_CTX_FLAG_PAD_MASK 0xF0/* RSA mode to use */ +# define EVP_MD_CTX_FLAG_PAD_PKCS1 0x00/* PKCS#1 v1.5 mode */ +# define EVP_MD_CTX_FLAG_PAD_X931 0x10/* X9.31 mode */ +# define EVP_MD_CTX_FLAG_PAD_PSS 0x20/* PSS mode */ + +# define EVP_MD_CTX_FLAG_NO_INIT 0x0100/* Don't initialize md_data */ +/* + * Some functions such as EVP_DigestSign only finalise copies of internal + * contexts so additional data can be included after the finalisation call. + * This is inefficient if this functionality is not required: it is disabled + * if the following flag is set. + */ +# define EVP_MD_CTX_FLAG_FINALISE 0x0200 +/* NOTE: 0x0400 is reserved for internal usage in evp_int.h */ + +EVP_CIPHER *EVP_CIPHER_meth_new(int cipher_type, int block_size, int key_len); +EVP_CIPHER *EVP_CIPHER_meth_dup(const EVP_CIPHER *cipher); +void EVP_CIPHER_meth_free(EVP_CIPHER *cipher); + +int EVP_CIPHER_meth_set_iv_length(EVP_CIPHER *cipher, int iv_len); +int EVP_CIPHER_meth_set_flags(EVP_CIPHER *cipher, unsigned long flags); +int EVP_CIPHER_meth_set_impl_ctx_size(EVP_CIPHER *cipher, int ctx_size); +int EVP_CIPHER_meth_set_init(EVP_CIPHER *cipher, + int (*init) (EVP_CIPHER_CTX *ctx, + const unsigned char *key, + const unsigned char *iv, + int enc)); +int EVP_CIPHER_meth_set_do_cipher(EVP_CIPHER *cipher, + int (*do_cipher) (EVP_CIPHER_CTX *ctx, + unsigned char *out, + const unsigned char *in, + size_t inl)); +int EVP_CIPHER_meth_set_cleanup(EVP_CIPHER *cipher, + int (*cleanup) (EVP_CIPHER_CTX *)); +int EVP_CIPHER_meth_set_set_asn1_params(EVP_CIPHER *cipher, + int (*set_asn1_parameters) (EVP_CIPHER_CTX *, + ASN1_TYPE *)); +int EVP_CIPHER_meth_set_get_asn1_params(EVP_CIPHER *cipher, + int (*get_asn1_parameters) (EVP_CIPHER_CTX *, + ASN1_TYPE *)); +int EVP_CIPHER_meth_set_ctrl(EVP_CIPHER *cipher, + int (*ctrl) (EVP_CIPHER_CTX *, int type, + int arg, void *ptr)); + +int (*EVP_CIPHER_meth_get_init(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *ctx, + const unsigned char *key, + const unsigned char *iv, + int enc); +int (*EVP_CIPHER_meth_get_do_cipher(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *ctx, + unsigned char *out, + const unsigned char *in, + size_t inl); +int (*EVP_CIPHER_meth_get_cleanup(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *); +int (*EVP_CIPHER_meth_get_set_asn1_params(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *, + ASN1_TYPE *); +int (*EVP_CIPHER_meth_get_get_asn1_params(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *, + ASN1_TYPE *); +int (*EVP_CIPHER_meth_get_ctrl(const EVP_CIPHER *cipher))(EVP_CIPHER_CTX *, + int type, int arg, + void *ptr); + +/* Values for cipher flags */ + +/* Modes for ciphers */ + +# define EVP_CIPH_STREAM_CIPHER 0x0 +# define EVP_CIPH_ECB_MODE 0x1 +# define EVP_CIPH_CBC_MODE 0x2 +# define EVP_CIPH_CFB_MODE 0x3 +# define EVP_CIPH_OFB_MODE 0x4 +# define EVP_CIPH_CTR_MODE 0x5 +# define EVP_CIPH_GCM_MODE 0x6 +# define EVP_CIPH_CCM_MODE 0x7 +# define EVP_CIPH_XTS_MODE 0x10001 +# define EVP_CIPH_WRAP_MODE 0x10002 +# define EVP_CIPH_OCB_MODE 0x10003 +# define EVP_CIPH_MODE 0xF0007 +/* Set if variable length cipher */ +# define EVP_CIPH_VARIABLE_LENGTH 0x8 +/* Set if the iv handling should be done by the cipher itself */ +# define EVP_CIPH_CUSTOM_IV 0x10 +/* Set if the cipher's init() function should be called if key is NULL */ +# define EVP_CIPH_ALWAYS_CALL_INIT 0x20 +/* Call ctrl() to init cipher parameters */ +# define EVP_CIPH_CTRL_INIT 0x40 +/* Don't use standard key length function */ +# define EVP_CIPH_CUSTOM_KEY_LENGTH 0x80 +/* Don't use standard block padding */ +# define EVP_CIPH_NO_PADDING 0x100 +/* cipher handles random key generation */ +# define EVP_CIPH_RAND_KEY 0x200 +/* cipher has its own additional copying logic */ +# define EVP_CIPH_CUSTOM_COPY 0x400 +/* Allow use default ASN1 get/set iv */ +# define EVP_CIPH_FLAG_DEFAULT_ASN1 0x1000 +/* Buffer length in bits not bytes: CFB1 mode only */ +# define EVP_CIPH_FLAG_LENGTH_BITS 0x2000 +/* Note if suitable for use in FIPS mode */ +# define EVP_CIPH_FLAG_FIPS 0x4000 +/* Allow non FIPS cipher in FIPS mode */ +# define EVP_CIPH_FLAG_NON_FIPS_ALLOW 0x8000 +/* + * Cipher handles any and all padding logic as well as finalisation. + */ +# define EVP_CIPH_FLAG_CUSTOM_CIPHER 0x100000 +# define EVP_CIPH_FLAG_AEAD_CIPHER 0x200000 +# define EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK 0x400000 +/* Cipher can handle pipeline operations */ +# define EVP_CIPH_FLAG_PIPELINE 0X800000 + +/* + * Cipher context flag to indicate we can handle wrap mode: if allowed in + * older applications it could overflow buffers. + */ + +# define EVP_CIPHER_CTX_FLAG_WRAP_ALLOW 0x1 + +/* ctrl() values */ + +# define EVP_CTRL_INIT 0x0 +# define EVP_CTRL_SET_KEY_LENGTH 0x1 +# define EVP_CTRL_GET_RC2_KEY_BITS 0x2 +# define EVP_CTRL_SET_RC2_KEY_BITS 0x3 +# define EVP_CTRL_GET_RC5_ROUNDS 0x4 +# define EVP_CTRL_SET_RC5_ROUNDS 0x5 +# define EVP_CTRL_RAND_KEY 0x6 +# define EVP_CTRL_PBE_PRF_NID 0x7 +# define EVP_CTRL_COPY 0x8 +# define EVP_CTRL_AEAD_SET_IVLEN 0x9 +# define EVP_CTRL_AEAD_GET_TAG 0x10 +# define EVP_CTRL_AEAD_SET_TAG 0x11 +# define EVP_CTRL_AEAD_SET_IV_FIXED 0x12 +# define EVP_CTRL_GCM_SET_IVLEN EVP_CTRL_AEAD_SET_IVLEN +# define EVP_CTRL_GCM_GET_TAG EVP_CTRL_AEAD_GET_TAG +# define EVP_CTRL_GCM_SET_TAG EVP_CTRL_AEAD_SET_TAG +# define EVP_CTRL_GCM_SET_IV_FIXED EVP_CTRL_AEAD_SET_IV_FIXED +# define EVP_CTRL_GCM_IV_GEN 0x13 +# define EVP_CTRL_CCM_SET_IVLEN EVP_CTRL_AEAD_SET_IVLEN +# define EVP_CTRL_CCM_GET_TAG EVP_CTRL_AEAD_GET_TAG +# define EVP_CTRL_CCM_SET_TAG EVP_CTRL_AEAD_SET_TAG +# define EVP_CTRL_CCM_SET_IV_FIXED EVP_CTRL_AEAD_SET_IV_FIXED +# define EVP_CTRL_CCM_SET_L 0x14 +# define EVP_CTRL_CCM_SET_MSGLEN 0x15 +/* + * AEAD cipher deduces payload length and returns number of bytes required to + * store MAC and eventual padding. Subsequent call to EVP_Cipher even + * appends/verifies MAC. + */ +# define EVP_CTRL_AEAD_TLS1_AAD 0x16 +/* Used by composite AEAD ciphers, no-op in GCM, CCM... */ +# define EVP_CTRL_AEAD_SET_MAC_KEY 0x17 +/* Set the GCM invocation field, decrypt only */ +# define EVP_CTRL_GCM_SET_IV_INV 0x18 + +# define EVP_CTRL_TLS1_1_MULTIBLOCK_AAD 0x19 +# define EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT 0x1a +# define EVP_CTRL_TLS1_1_MULTIBLOCK_DECRYPT 0x1b +# define EVP_CTRL_TLS1_1_MULTIBLOCK_MAX_BUFSIZE 0x1c + +# define EVP_CTRL_SSL3_MASTER_SECRET 0x1d + +/* EVP_CTRL_SET_SBOX takes the char * specifying S-boxes */ +# define EVP_CTRL_SET_SBOX 0x1e +/* + * EVP_CTRL_SBOX_USED takes a 'size_t' and 'char *', pointing at a + * pre-allocated buffer with specified size + */ +# define EVP_CTRL_SBOX_USED 0x1f +/* EVP_CTRL_KEY_MESH takes 'size_t' number of bytes to mesh the key after, + * 0 switches meshing off + */ +# define EVP_CTRL_KEY_MESH 0x20 +/* EVP_CTRL_BLOCK_PADDING_MODE takes the padding mode */ +# define EVP_CTRL_BLOCK_PADDING_MODE 0x21 + +/* Set the output buffers to use for a pipelined operation */ +# define EVP_CTRL_SET_PIPELINE_OUTPUT_BUFS 0x22 +/* Set the input buffers to use for a pipelined operation */ +# define EVP_CTRL_SET_PIPELINE_INPUT_BUFS 0x23 +/* Set the input buffer lengths to use for a pipelined operation */ +# define EVP_CTRL_SET_PIPELINE_INPUT_LENS 0x24 + +/* Padding modes */ +#define EVP_PADDING_PKCS7 1 +#define EVP_PADDING_ISO7816_4 2 +#define EVP_PADDING_ANSI923 3 +#define EVP_PADDING_ISO10126 4 +#define EVP_PADDING_ZERO 5 + +/* RFC 5246 defines additional data to be 13 bytes in length */ +# define EVP_AEAD_TLS1_AAD_LEN 13 + +typedef struct { + unsigned char *out; + const unsigned char *inp; + size_t len; + unsigned int interleave; +} EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM; + +/* GCM TLS constants */ +/* Length of fixed part of IV derived from PRF */ +# define EVP_GCM_TLS_FIXED_IV_LEN 4 +/* Length of explicit part of IV part of TLS records */ +# define EVP_GCM_TLS_EXPLICIT_IV_LEN 8 +/* Length of tag for TLS */ +# define EVP_GCM_TLS_TAG_LEN 16 + +/* CCM TLS constants */ +/* Length of fixed part of IV derived from PRF */ +# define EVP_CCM_TLS_FIXED_IV_LEN 4 +/* Length of explicit part of IV part of TLS records */ +# define EVP_CCM_TLS_EXPLICIT_IV_LEN 8 +/* Total length of CCM IV length for TLS */ +# define EVP_CCM_TLS_IV_LEN 12 +/* Length of tag for TLS */ +# define EVP_CCM_TLS_TAG_LEN 16 +/* Length of CCM8 tag for TLS */ +# define EVP_CCM8_TLS_TAG_LEN 8 + +/* Length of tag for TLS */ +# define EVP_CHACHAPOLY_TLS_TAG_LEN 16 + +typedef struct evp_cipher_info_st { + const EVP_CIPHER *cipher; + unsigned char iv[EVP_MAX_IV_LENGTH]; +} EVP_CIPHER_INFO; + + +/* Password based encryption function */ +typedef int (EVP_PBE_KEYGEN) (EVP_CIPHER_CTX *ctx, const char *pass, + int passlen, ASN1_TYPE *param, + const EVP_CIPHER *cipher, const EVP_MD *md, + int en_de); + +# ifndef OPENSSL_NO_RSA +# define EVP_PKEY_assign_RSA(pkey,rsa) EVP_PKEY_assign((pkey),EVP_PKEY_RSA,\ + (char *)(rsa)) +# endif + +# ifndef OPENSSL_NO_DSA +# define EVP_PKEY_assign_DSA(pkey,dsa) EVP_PKEY_assign((pkey),EVP_PKEY_DSA,\ + (char *)(dsa)) +# endif + +# ifndef OPENSSL_NO_DH +# define EVP_PKEY_assign_DH(pkey,dh) EVP_PKEY_assign((pkey),EVP_PKEY_DH,\ + (char *)(dh)) +# endif + +# ifndef OPENSSL_NO_EC +# define EVP_PKEY_assign_EC_KEY(pkey,eckey) EVP_PKEY_assign((pkey),EVP_PKEY_EC,\ + (char *)(eckey)) +# endif +# ifndef OPENSSL_NO_SIPHASH +# define EVP_PKEY_assign_SIPHASH(pkey,shkey) EVP_PKEY_assign((pkey),EVP_PKEY_SIPHASH,\ + (char *)(shkey)) +# endif + +# ifndef OPENSSL_NO_POLY1305 +# define EVP_PKEY_assign_POLY1305(pkey,polykey) EVP_PKEY_assign((pkey),EVP_PKEY_POLY1305,\ + (char *)(polykey)) +# endif + +/* Add some extra combinations */ +# define EVP_get_digestbynid(a) EVP_get_digestbyname(OBJ_nid2sn(a)) +# define EVP_get_digestbyobj(a) EVP_get_digestbynid(OBJ_obj2nid(a)) +# define EVP_get_cipherbynid(a) EVP_get_cipherbyname(OBJ_nid2sn(a)) +# define EVP_get_cipherbyobj(a) EVP_get_cipherbynid(OBJ_obj2nid(a)) + +int EVP_MD_type(const EVP_MD *md); +# define EVP_MD_nid(e) EVP_MD_type(e) +# define EVP_MD_name(e) OBJ_nid2sn(EVP_MD_nid(e)) +int EVP_MD_pkey_type(const EVP_MD *md); +int EVP_MD_size(const EVP_MD *md); +int EVP_MD_block_size(const EVP_MD *md); +unsigned long EVP_MD_flags(const EVP_MD *md); + +const EVP_MD *EVP_MD_CTX_md(const EVP_MD_CTX *ctx); +int (*EVP_MD_CTX_update_fn(EVP_MD_CTX *ctx))(EVP_MD_CTX *ctx, + const void *data, size_t count); +void EVP_MD_CTX_set_update_fn(EVP_MD_CTX *ctx, + int (*update) (EVP_MD_CTX *ctx, + const void *data, size_t count)); +# define EVP_MD_CTX_size(e) EVP_MD_size(EVP_MD_CTX_md(e)) +# define EVP_MD_CTX_block_size(e) EVP_MD_block_size(EVP_MD_CTX_md(e)) +# define EVP_MD_CTX_type(e) EVP_MD_type(EVP_MD_CTX_md(e)) +EVP_PKEY_CTX *EVP_MD_CTX_pkey_ctx(const EVP_MD_CTX *ctx); +void EVP_MD_CTX_set_pkey_ctx(EVP_MD_CTX *ctx, EVP_PKEY_CTX *pctx); +void *EVP_MD_CTX_md_data(const EVP_MD_CTX *ctx); + +int EVP_CIPHER_nid(const EVP_CIPHER *cipher); +# define EVP_CIPHER_name(e) OBJ_nid2sn(EVP_CIPHER_nid(e)) +int EVP_CIPHER_block_size(const EVP_CIPHER *cipher); +int EVP_CIPHER_impl_ctx_size(const EVP_CIPHER *cipher); +int EVP_CIPHER_key_length(const EVP_CIPHER *cipher); +int EVP_CIPHER_iv_length(const EVP_CIPHER *cipher); +unsigned long EVP_CIPHER_flags(const EVP_CIPHER *cipher); +# define EVP_CIPHER_mode(e) (EVP_CIPHER_flags(e) & EVP_CIPH_MODE) + +const EVP_CIPHER *EVP_CIPHER_CTX_cipher(const EVP_CIPHER_CTX *ctx); +int EVP_CIPHER_CTX_encrypting(const EVP_CIPHER_CTX *ctx); +int EVP_CIPHER_CTX_nid(const EVP_CIPHER_CTX *ctx); +int EVP_CIPHER_CTX_block_size(const EVP_CIPHER_CTX *ctx); +int EVP_CIPHER_CTX_key_length(const EVP_CIPHER_CTX *ctx); +int EVP_CIPHER_CTX_iv_length(const EVP_CIPHER_CTX *ctx); +const unsigned char *EVP_CIPHER_CTX_iv(const EVP_CIPHER_CTX *ctx); +const unsigned char *EVP_CIPHER_CTX_original_iv(const EVP_CIPHER_CTX *ctx); +unsigned char *EVP_CIPHER_CTX_iv_noconst(EVP_CIPHER_CTX *ctx); +unsigned char *EVP_CIPHER_CTX_buf_noconst(EVP_CIPHER_CTX *ctx); +int EVP_CIPHER_CTX_num(const EVP_CIPHER_CTX *ctx); +void EVP_CIPHER_CTX_set_num(EVP_CIPHER_CTX *ctx, int num); +int EVP_CIPHER_CTX_copy(EVP_CIPHER_CTX *out, const EVP_CIPHER_CTX *in); +void *EVP_CIPHER_CTX_get_app_data(const EVP_CIPHER_CTX *ctx); +void EVP_CIPHER_CTX_set_app_data(EVP_CIPHER_CTX *ctx, void *data); +void *EVP_CIPHER_CTX_get_cipher_data(const EVP_CIPHER_CTX *ctx); +void *EVP_CIPHER_CTX_set_cipher_data(EVP_CIPHER_CTX *ctx, void *cipher_data); +# define EVP_CIPHER_CTX_type(c) EVP_CIPHER_type(EVP_CIPHER_CTX_cipher(c)) +# if OPENSSL_API_COMPAT < 0x10100000L +# define EVP_CIPHER_CTX_flags(c) EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(c)) +# endif +# define EVP_CIPHER_CTX_mode(c) EVP_CIPHER_mode(EVP_CIPHER_CTX_cipher(c)) + +# define EVP_ENCODE_LENGTH(l) ((((l)+2)/3*4)+((l)/48+1)*2+80) +# define EVP_DECODE_LENGTH(l) (((l)+3)/4*3+80) + +# define EVP_SignInit_ex(a,b,c) EVP_DigestInit_ex(a,b,c) +# define EVP_SignInit(a,b) EVP_DigestInit(a,b) +# define EVP_SignUpdate(a,b,c) EVP_DigestUpdate(a,b,c) +# define EVP_VerifyInit_ex(a,b,c) EVP_DigestInit_ex(a,b,c) +# define EVP_VerifyInit(a,b) EVP_DigestInit(a,b) +# define EVP_VerifyUpdate(a,b,c) EVP_DigestUpdate(a,b,c) +# define EVP_OpenUpdate(a,b,c,d,e) EVP_DecryptUpdate(a,b,c,d,e) +# define EVP_SealUpdate(a,b,c,d,e) EVP_EncryptUpdate(a,b,c,d,e) +# define EVP_DigestSignUpdate(a,b,c) EVP_DigestUpdate(a,b,c) +# define EVP_DigestVerifyUpdate(a,b,c) EVP_DigestUpdate(a,b,c) + +# ifdef CONST_STRICT +void BIO_set_md(BIO *, const EVP_MD *md); +# else +# define BIO_set_md(b,md) BIO_ctrl(b,BIO_C_SET_MD,0,(char *)(md)) +# endif +# define BIO_get_md(b,mdp) BIO_ctrl(b,BIO_C_GET_MD,0,(char *)(mdp)) +# define BIO_get_md_ctx(b,mdcp) BIO_ctrl(b,BIO_C_GET_MD_CTX,0, \ + (char *)(mdcp)) +# define BIO_set_md_ctx(b,mdcp) BIO_ctrl(b,BIO_C_SET_MD_CTX,0, \ + (char *)(mdcp)) +# define BIO_get_cipher_status(b) BIO_ctrl(b,BIO_C_GET_CIPHER_STATUS,0,NULL) +# define BIO_get_cipher_ctx(b,c_pp) BIO_ctrl(b,BIO_C_GET_CIPHER_CTX,0, \ + (char *)(c_pp)) + +/*__owur*/ int EVP_Cipher(EVP_CIPHER_CTX *c, + unsigned char *out, + const unsigned char *in, unsigned int inl); + +# define EVP_add_cipher_alias(n,alias) \ + OBJ_NAME_add((alias),OBJ_NAME_TYPE_CIPHER_METH|OBJ_NAME_ALIAS,(n)) +# define EVP_add_digest_alias(n,alias) \ + OBJ_NAME_add((alias),OBJ_NAME_TYPE_MD_METH|OBJ_NAME_ALIAS,(n)) +# define EVP_delete_cipher_alias(alias) \ + OBJ_NAME_remove(alias,OBJ_NAME_TYPE_CIPHER_METH|OBJ_NAME_ALIAS); +# define EVP_delete_digest_alias(alias) \ + OBJ_NAME_remove(alias,OBJ_NAME_TYPE_MD_METH|OBJ_NAME_ALIAS); + +int EVP_MD_CTX_ctrl(EVP_MD_CTX *ctx, int cmd, int p1, void *p2); +EVP_MD_CTX *EVP_MD_CTX_new(void); +int EVP_MD_CTX_reset(EVP_MD_CTX *ctx); +void EVP_MD_CTX_free(EVP_MD_CTX *ctx); +# define EVP_MD_CTX_create() EVP_MD_CTX_new() +# define EVP_MD_CTX_init(ctx) EVP_MD_CTX_reset((ctx)) +# define EVP_MD_CTX_destroy(ctx) EVP_MD_CTX_free((ctx)) +__owur int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in); +void EVP_MD_CTX_set_flags(EVP_MD_CTX *ctx, int flags); +void EVP_MD_CTX_clear_flags(EVP_MD_CTX *ctx, int flags); +int EVP_MD_CTX_test_flags(const EVP_MD_CTX *ctx, int flags); +__owur int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, + ENGINE *impl); +__owur int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *d, + size_t cnt); +__owur int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *md, + unsigned int *s); +__owur int EVP_Digest(const void *data, size_t count, + unsigned char *md, unsigned int *size, + const EVP_MD *type, ENGINE *impl); + +__owur int EVP_MD_CTX_copy(EVP_MD_CTX *out, const EVP_MD_CTX *in); +__owur int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type); +__owur int EVP_DigestFinal(EVP_MD_CTX *ctx, unsigned char *md, + unsigned int *s); +__owur int EVP_DigestFinalXOF(EVP_MD_CTX *ctx, unsigned char *md, + size_t len); + +int EVP_read_pw_string(char *buf, int length, const char *prompt, int verify); +int EVP_read_pw_string_min(char *buf, int minlen, int maxlen, + const char *prompt, int verify); +void EVP_set_pw_prompt(const char *prompt); +char *EVP_get_pw_prompt(void); + +__owur int EVP_BytesToKey(const EVP_CIPHER *type, const EVP_MD *md, + const unsigned char *salt, + const unsigned char *data, int datal, int count, + unsigned char *key, unsigned char *iv); + +void EVP_CIPHER_CTX_set_flags(EVP_CIPHER_CTX *ctx, int flags); +void EVP_CIPHER_CTX_clear_flags(EVP_CIPHER_CTX *ctx, int flags); +int EVP_CIPHER_CTX_test_flags(const EVP_CIPHER_CTX *ctx, int flags); + +__owur int EVP_EncryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, + const unsigned char *key, const unsigned char *iv); +/*__owur*/ int EVP_EncryptInit_ex(EVP_CIPHER_CTX *ctx, + const EVP_CIPHER *cipher, ENGINE *impl, + const unsigned char *key, + const unsigned char *iv); +/*__owur*/ int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, + int *outl, const unsigned char *in, int inl); +/*__owur*/ int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, + int *outl); +/*__owur*/ int EVP_EncryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, + int *outl); + +__owur int EVP_DecryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, + const unsigned char *key, const unsigned char *iv); +/*__owur*/ int EVP_DecryptInit_ex(EVP_CIPHER_CTX *ctx, + const EVP_CIPHER *cipher, ENGINE *impl, + const unsigned char *key, + const unsigned char *iv); +/*__owur*/ int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, + int *outl, const unsigned char *in, int inl); +__owur int EVP_DecryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *outm, + int *outl); +/*__owur*/ int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *outm, + int *outl); + +__owur int EVP_CipherInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, + const unsigned char *key, const unsigned char *iv, + int enc); +/*__owur*/ int EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, + const EVP_CIPHER *cipher, ENGINE *impl, + const unsigned char *key, + const unsigned char *iv, int enc); +__owur int EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, + int *outl, const unsigned char *in, int inl); +__owur int EVP_CipherFinal(EVP_CIPHER_CTX *ctx, unsigned char *outm, + int *outl); +__owur int EVP_CipherFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *outm, + int *outl); + +__owur int EVP_SignFinal(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *s, + EVP_PKEY *pkey); + +__owur int EVP_DigestSign(EVP_MD_CTX *ctx, unsigned char *sigret, + size_t *siglen, const unsigned char *tbs, + size_t tbslen); + +__owur int EVP_VerifyFinal(EVP_MD_CTX *ctx, const unsigned char *sigbuf, + unsigned int siglen, EVP_PKEY *pkey); + +__owur int EVP_DigestVerify(EVP_MD_CTX *ctx, const unsigned char *sigret, + size_t siglen, const unsigned char *tbs, + size_t tbslen); + +/*__owur*/ int EVP_DigestSignInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx, + const EVP_MD *type, ENGINE *e, + EVP_PKEY *pkey); +__owur int EVP_DigestSignFinal(EVP_MD_CTX *ctx, unsigned char *sigret, + size_t *siglen); + +__owur int EVP_DigestVerifyInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx, + const EVP_MD *type, ENGINE *e, + EVP_PKEY *pkey); +__owur int EVP_DigestVerifyFinal(EVP_MD_CTX *ctx, const unsigned char *sig, + size_t siglen); + +# ifndef OPENSSL_NO_RSA +__owur int EVP_OpenInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type, + const unsigned char *ek, int ekl, + const unsigned char *iv, EVP_PKEY *priv); +__owur int EVP_OpenFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl); + +__owur int EVP_SealInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type, + unsigned char **ek, int *ekl, unsigned char *iv, + EVP_PKEY **pubk, int npubk); +__owur int EVP_SealFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl); +# endif + +EVP_ENCODE_CTX *EVP_ENCODE_CTX_new(void); +void EVP_ENCODE_CTX_free(EVP_ENCODE_CTX *ctx); +int EVP_ENCODE_CTX_copy(EVP_ENCODE_CTX *dctx, EVP_ENCODE_CTX *sctx); +int EVP_ENCODE_CTX_num(EVP_ENCODE_CTX *ctx); +void EVP_EncodeInit(EVP_ENCODE_CTX *ctx); +int EVP_EncodeUpdate(EVP_ENCODE_CTX *ctx, unsigned char *out, int *outl, + const unsigned char *in, int inl); +void EVP_EncodeFinal(EVP_ENCODE_CTX *ctx, unsigned char *out, int *outl); +int EVP_EncodeBlock(unsigned char *t, const unsigned char *f, int n); + +void EVP_DecodeInit(EVP_ENCODE_CTX *ctx); +int EVP_DecodeUpdate(EVP_ENCODE_CTX *ctx, unsigned char *out, int *outl, + const unsigned char *in, int inl); +int EVP_DecodeFinal(EVP_ENCODE_CTX *ctx, unsigned + char *out, int *outl); +int EVP_DecodeBlock(unsigned char *t, const unsigned char *f, int n); + +# if OPENSSL_API_COMPAT < 0x10100000L +# define EVP_CIPHER_CTX_init(c) EVP_CIPHER_CTX_reset(c) +# define EVP_CIPHER_CTX_cleanup(c) EVP_CIPHER_CTX_reset(c) +# endif +EVP_CIPHER_CTX *EVP_CIPHER_CTX_new(void); +int EVP_CIPHER_CTX_reset(EVP_CIPHER_CTX *c); +void EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *c); +int EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *x, int keylen); +int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *c, int pad); +int EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr); +int EVP_CIPHER_CTX_rand_key(EVP_CIPHER_CTX *ctx, unsigned char *key); + +const BIO_METHOD *BIO_f_md(void); +const BIO_METHOD *BIO_f_base64(void); +const BIO_METHOD *BIO_f_cipher(void); +const BIO_METHOD *BIO_f_reliable(void); +__owur int BIO_set_cipher(BIO *b, const EVP_CIPHER *c, const unsigned char *k, + const unsigned char *i, int enc); + +const EVP_MD *EVP_md_null(void); +# ifndef OPENSSL_NO_MD2 +const EVP_MD *EVP_md2(void); +# endif +# ifndef OPENSSL_NO_MD4 +const EVP_MD *EVP_md4(void); +# endif +# ifndef OPENSSL_NO_MD5 +const EVP_MD *EVP_md5(void); +const EVP_MD *EVP_md5_sha1(void); +# endif +# ifndef OPENSSL_NO_BLAKE2 +const EVP_MD *EVP_blake2b512(void); +const EVP_MD *EVP_blake2s256(void); +# endif +const EVP_MD *EVP_sha1(void); +const EVP_MD *EVP_sha224(void); +const EVP_MD *EVP_sha256(void); +const EVP_MD *EVP_sha384(void); +const EVP_MD *EVP_sha512(void); +const EVP_MD *EVP_sha512_224(void); +const EVP_MD *EVP_sha512_256(void); +const EVP_MD *EVP_sha3_224(void); +const EVP_MD *EVP_sha3_256(void); +const EVP_MD *EVP_sha3_384(void); +const EVP_MD *EVP_sha3_512(void); +const EVP_MD *EVP_shake128(void); +const EVP_MD *EVP_shake256(void); +# ifndef OPENSSL_NO_MDC2 +const EVP_MD *EVP_mdc2(void); +# endif +# ifndef OPENSSL_NO_RMD160 +const EVP_MD *EVP_ripemd160(void); +# endif +# ifndef OPENSSL_NO_WHIRLPOOL +const EVP_MD *EVP_whirlpool(void); +# endif +# ifndef OPENSSL_NO_SM3 +const EVP_MD *EVP_sm3(void); +# endif +const EVP_CIPHER *EVP_enc_null(void); /* does nothing :-) */ +# ifndef OPENSSL_NO_DES +const EVP_CIPHER *EVP_des_ecb(void); +const EVP_CIPHER *EVP_des_ede(void); +const EVP_CIPHER *EVP_des_ede3(void); +const EVP_CIPHER *EVP_des_ede_ecb(void); +const EVP_CIPHER *EVP_des_ede3_ecb(void); +const EVP_CIPHER *EVP_des_cfb64(void); +# define EVP_des_cfb EVP_des_cfb64 +const EVP_CIPHER *EVP_des_cfb1(void); +const EVP_CIPHER *EVP_des_cfb8(void); +const EVP_CIPHER *EVP_des_ede_cfb64(void); +# define EVP_des_ede_cfb EVP_des_ede_cfb64 +const EVP_CIPHER *EVP_des_ede3_cfb64(void); +# define EVP_des_ede3_cfb EVP_des_ede3_cfb64 +const EVP_CIPHER *EVP_des_ede3_cfb1(void); +const EVP_CIPHER *EVP_des_ede3_cfb8(void); +const EVP_CIPHER *EVP_des_ofb(void); +const EVP_CIPHER *EVP_des_ede_ofb(void); +const EVP_CIPHER *EVP_des_ede3_ofb(void); +const EVP_CIPHER *EVP_des_cbc(void); +const EVP_CIPHER *EVP_des_ede_cbc(void); +const EVP_CIPHER *EVP_des_ede3_cbc(void); +const EVP_CIPHER *EVP_desx_cbc(void); +const EVP_CIPHER *EVP_des_ede3_wrap(void); +/* + * This should now be supported through the dev_crypto ENGINE. But also, why + * are rc4 and md5 declarations made here inside a "NO_DES" precompiler + * branch? + */ +# endif +# ifndef OPENSSL_NO_RC4 +const EVP_CIPHER *EVP_rc4(void); +const EVP_CIPHER *EVP_rc4_40(void); +# ifndef OPENSSL_NO_MD5 +const EVP_CIPHER *EVP_rc4_hmac_md5(void); +# endif +# endif +# ifndef OPENSSL_NO_IDEA +const EVP_CIPHER *EVP_idea_ecb(void); +const EVP_CIPHER *EVP_idea_cfb64(void); +# define EVP_idea_cfb EVP_idea_cfb64 +const EVP_CIPHER *EVP_idea_ofb(void); +const EVP_CIPHER *EVP_idea_cbc(void); +# endif +# ifndef OPENSSL_NO_RC2 +const EVP_CIPHER *EVP_rc2_ecb(void); +const EVP_CIPHER *EVP_rc2_cbc(void); +const EVP_CIPHER *EVP_rc2_40_cbc(void); +const EVP_CIPHER *EVP_rc2_64_cbc(void); +const EVP_CIPHER *EVP_rc2_cfb64(void); +# define EVP_rc2_cfb EVP_rc2_cfb64 +const EVP_CIPHER *EVP_rc2_ofb(void); +# endif +# ifndef OPENSSL_NO_BF +const EVP_CIPHER *EVP_bf_ecb(void); +const EVP_CIPHER *EVP_bf_cbc(void); +const EVP_CIPHER *EVP_bf_cfb64(void); +# define EVP_bf_cfb EVP_bf_cfb64 +const EVP_CIPHER *EVP_bf_ofb(void); +# endif +# ifndef OPENSSL_NO_CAST +const EVP_CIPHER *EVP_cast5_ecb(void); +const EVP_CIPHER *EVP_cast5_cbc(void); +const EVP_CIPHER *EVP_cast5_cfb64(void); +# define EVP_cast5_cfb EVP_cast5_cfb64 +const EVP_CIPHER *EVP_cast5_ofb(void); +# endif +# ifndef OPENSSL_NO_RC5 +const EVP_CIPHER *EVP_rc5_32_12_16_cbc(void); +const EVP_CIPHER *EVP_rc5_32_12_16_ecb(void); +const EVP_CIPHER *EVP_rc5_32_12_16_cfb64(void); +# define EVP_rc5_32_12_16_cfb EVP_rc5_32_12_16_cfb64 +const EVP_CIPHER *EVP_rc5_32_12_16_ofb(void); +# endif +const EVP_CIPHER *EVP_aes_128_ecb(void); +const EVP_CIPHER *EVP_aes_128_cbc(void); +const EVP_CIPHER *EVP_aes_128_cfb1(void); +const EVP_CIPHER *EVP_aes_128_cfb8(void); +const EVP_CIPHER *EVP_aes_128_cfb128(void); +# define EVP_aes_128_cfb EVP_aes_128_cfb128 +const EVP_CIPHER *EVP_aes_128_ofb(void); +const EVP_CIPHER *EVP_aes_128_ctr(void); +const EVP_CIPHER *EVP_aes_128_ccm(void); +const EVP_CIPHER *EVP_aes_128_gcm(void); +const EVP_CIPHER *EVP_aes_128_xts(void); +const EVP_CIPHER *EVP_aes_128_wrap(void); +const EVP_CIPHER *EVP_aes_128_wrap_pad(void); +# ifndef OPENSSL_NO_OCB +const EVP_CIPHER *EVP_aes_128_ocb(void); +# endif +const EVP_CIPHER *EVP_aes_192_ecb(void); +const EVP_CIPHER *EVP_aes_192_cbc(void); +const EVP_CIPHER *EVP_aes_192_cfb1(void); +const EVP_CIPHER *EVP_aes_192_cfb8(void); +const EVP_CIPHER *EVP_aes_192_cfb128(void); +# define EVP_aes_192_cfb EVP_aes_192_cfb128 +const EVP_CIPHER *EVP_aes_192_ofb(void); +const EVP_CIPHER *EVP_aes_192_ctr(void); +const EVP_CIPHER *EVP_aes_192_ccm(void); +const EVP_CIPHER *EVP_aes_192_gcm(void); +const EVP_CIPHER *EVP_aes_192_wrap(void); +const EVP_CIPHER *EVP_aes_192_wrap_pad(void); +# ifndef OPENSSL_NO_OCB +const EVP_CIPHER *EVP_aes_192_ocb(void); +# endif +const EVP_CIPHER *EVP_aes_256_ecb(void); +const EVP_CIPHER *EVP_aes_256_cbc(void); +const EVP_CIPHER *EVP_aes_256_cfb1(void); +const EVP_CIPHER *EVP_aes_256_cfb8(void); +const EVP_CIPHER *EVP_aes_256_cfb128(void); +# define EVP_aes_256_cfb EVP_aes_256_cfb128 +const EVP_CIPHER *EVP_aes_256_ofb(void); +const EVP_CIPHER *EVP_aes_256_ctr(void); +const EVP_CIPHER *EVP_aes_256_ccm(void); +const EVP_CIPHER *EVP_aes_256_gcm(void); +const EVP_CIPHER *EVP_aes_256_xts(void); +const EVP_CIPHER *EVP_aes_256_wrap(void); +const EVP_CIPHER *EVP_aes_256_wrap_pad(void); +# ifndef OPENSSL_NO_OCB +const EVP_CIPHER *EVP_aes_256_ocb(void); +# endif +const EVP_CIPHER *EVP_aes_128_cbc_hmac_sha1(void); +const EVP_CIPHER *EVP_aes_256_cbc_hmac_sha1(void); +const EVP_CIPHER *EVP_aes_128_cbc_hmac_sha256(void); +const EVP_CIPHER *EVP_aes_256_cbc_hmac_sha256(void); +# ifndef OPENSSL_NO_ARIA +const EVP_CIPHER *EVP_aria_128_ecb(void); +const EVP_CIPHER *EVP_aria_128_cbc(void); +const EVP_CIPHER *EVP_aria_128_cfb1(void); +const EVP_CIPHER *EVP_aria_128_cfb8(void); +const EVP_CIPHER *EVP_aria_128_cfb128(void); +# define EVP_aria_128_cfb EVP_aria_128_cfb128 +const EVP_CIPHER *EVP_aria_128_ctr(void); +const EVP_CIPHER *EVP_aria_128_ofb(void); +const EVP_CIPHER *EVP_aria_128_gcm(void); +const EVP_CIPHER *EVP_aria_128_ccm(void); +const EVP_CIPHER *EVP_aria_192_ecb(void); +const EVP_CIPHER *EVP_aria_192_cbc(void); +const EVP_CIPHER *EVP_aria_192_cfb1(void); +const EVP_CIPHER *EVP_aria_192_cfb8(void); +const EVP_CIPHER *EVP_aria_192_cfb128(void); +# define EVP_aria_192_cfb EVP_aria_192_cfb128 +const EVP_CIPHER *EVP_aria_192_ctr(void); +const EVP_CIPHER *EVP_aria_192_ofb(void); +const EVP_CIPHER *EVP_aria_192_gcm(void); +const EVP_CIPHER *EVP_aria_192_ccm(void); +const EVP_CIPHER *EVP_aria_256_ecb(void); +const EVP_CIPHER *EVP_aria_256_cbc(void); +const EVP_CIPHER *EVP_aria_256_cfb1(void); +const EVP_CIPHER *EVP_aria_256_cfb8(void); +const EVP_CIPHER *EVP_aria_256_cfb128(void); +# define EVP_aria_256_cfb EVP_aria_256_cfb128 +const EVP_CIPHER *EVP_aria_256_ctr(void); +const EVP_CIPHER *EVP_aria_256_ofb(void); +const EVP_CIPHER *EVP_aria_256_gcm(void); +const EVP_CIPHER *EVP_aria_256_ccm(void); +# endif +# ifndef OPENSSL_NO_CAMELLIA +const EVP_CIPHER *EVP_camellia_128_ecb(void); +const EVP_CIPHER *EVP_camellia_128_cbc(void); +const EVP_CIPHER *EVP_camellia_128_cfb1(void); +const EVP_CIPHER *EVP_camellia_128_cfb8(void); +const EVP_CIPHER *EVP_camellia_128_cfb128(void); +# define EVP_camellia_128_cfb EVP_camellia_128_cfb128 +const EVP_CIPHER *EVP_camellia_128_ofb(void); +const EVP_CIPHER *EVP_camellia_128_ctr(void); +const EVP_CIPHER *EVP_camellia_192_ecb(void); +const EVP_CIPHER *EVP_camellia_192_cbc(void); +const EVP_CIPHER *EVP_camellia_192_cfb1(void); +const EVP_CIPHER *EVP_camellia_192_cfb8(void); +const EVP_CIPHER *EVP_camellia_192_cfb128(void); +# define EVP_camellia_192_cfb EVP_camellia_192_cfb128 +const EVP_CIPHER *EVP_camellia_192_ofb(void); +const EVP_CIPHER *EVP_camellia_192_ctr(void); +const EVP_CIPHER *EVP_camellia_256_ecb(void); +const EVP_CIPHER *EVP_camellia_256_cbc(void); +const EVP_CIPHER *EVP_camellia_256_cfb1(void); +const EVP_CIPHER *EVP_camellia_256_cfb8(void); +const EVP_CIPHER *EVP_camellia_256_cfb128(void); +# define EVP_camellia_256_cfb EVP_camellia_256_cfb128 +const EVP_CIPHER *EVP_camellia_256_ofb(void); +const EVP_CIPHER *EVP_camellia_256_ctr(void); +# endif +# ifndef OPENSSL_NO_CHACHA +const EVP_CIPHER *EVP_chacha20(void); +# ifndef OPENSSL_NO_POLY1305 +const EVP_CIPHER *EVP_chacha20_poly1305(void); +# endif +# endif + +# ifndef OPENSSL_NO_SEED +const EVP_CIPHER *EVP_seed_ecb(void); +const EVP_CIPHER *EVP_seed_cbc(void); +const EVP_CIPHER *EVP_seed_cfb128(void); +# define EVP_seed_cfb EVP_seed_cfb128 +const EVP_CIPHER *EVP_seed_ofb(void); +# endif + +# ifndef OPENSSL_NO_SM4 +const EVP_CIPHER *EVP_sm4_ecb(void); +const EVP_CIPHER *EVP_sm4_cbc(void); +const EVP_CIPHER *EVP_sm4_cfb128(void); +# define EVP_sm4_cfb EVP_sm4_cfb128 +const EVP_CIPHER *EVP_sm4_ofb(void); +const EVP_CIPHER *EVP_sm4_ctr(void); +# endif + +# if OPENSSL_API_COMPAT < 0x10100000L +# define OPENSSL_add_all_algorithms_conf() \ + OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS \ + | OPENSSL_INIT_ADD_ALL_DIGESTS \ + | OPENSSL_INIT_LOAD_CONFIG, NULL) +# define OPENSSL_add_all_algorithms_noconf() \ + OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS \ + | OPENSSL_INIT_ADD_ALL_DIGESTS, NULL) + +# ifdef OPENSSL_LOAD_CONF +# define OpenSSL_add_all_algorithms() \ + OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS \ + | OPENSSL_INIT_ADD_ALL_DIGESTS \ + | OPENSSL_INIT_LOAD_CONFIG, NULL) +# else +# define OpenSSL_add_all_algorithms() \ + OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS \ + | OPENSSL_INIT_ADD_ALL_DIGESTS, NULL) +# endif + +# define OpenSSL_add_all_ciphers() \ + OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS, NULL) +# define OpenSSL_add_all_digests() \ + OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_DIGESTS, NULL) + +# define EVP_cleanup() while(0) continue +# endif + +int EVP_add_cipher(const EVP_CIPHER *cipher); +int EVP_add_digest(const EVP_MD *digest); + +const EVP_CIPHER *EVP_get_cipherbyname(const char *name); +const EVP_MD *EVP_get_digestbyname(const char *name); + +void EVP_CIPHER_do_all(void (*fn) (const EVP_CIPHER *ciph, + const char *from, const char *to, void *x), + void *arg); +void EVP_CIPHER_do_all_sorted(void (*fn) + (const EVP_CIPHER *ciph, const char *from, + const char *to, void *x), void *arg); + +void EVP_MD_do_all(void (*fn) (const EVP_MD *ciph, + const char *from, const char *to, void *x), + void *arg); +void EVP_MD_do_all_sorted(void (*fn) + (const EVP_MD *ciph, const char *from, + const char *to, void *x), void *arg); + +int EVP_PKEY_decrypt_old(unsigned char *dec_key, + const unsigned char *enc_key, int enc_key_len, + EVP_PKEY *private_key); +int EVP_PKEY_encrypt_old(unsigned char *enc_key, + const unsigned char *key, int key_len, + EVP_PKEY *pub_key); +int EVP_PKEY_type(int type); +int EVP_PKEY_id(const EVP_PKEY *pkey); +int EVP_PKEY_base_id(const EVP_PKEY *pkey); +int EVP_PKEY_bits(const EVP_PKEY *pkey); +int EVP_PKEY_security_bits(const EVP_PKEY *pkey); +int EVP_PKEY_size(EVP_PKEY *pkey); +int EVP_PKEY_set_type(EVP_PKEY *pkey, int type); +int EVP_PKEY_set_type_str(EVP_PKEY *pkey, const char *str, int len); +int EVP_PKEY_set_alias_type(EVP_PKEY *pkey, int type); +# ifndef OPENSSL_NO_ENGINE +int EVP_PKEY_set1_engine(EVP_PKEY *pkey, ENGINE *e); +# endif +int EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key); +void *EVP_PKEY_get0(const EVP_PKEY *pkey); +const unsigned char *EVP_PKEY_get0_hmac(const EVP_PKEY *pkey, size_t *len); +# ifndef OPENSSL_NO_POLY1305 +const unsigned char *EVP_PKEY_get0_poly1305(const EVP_PKEY *pkey, size_t *len); +# endif +# ifndef OPENSSL_NO_SIPHASH +const unsigned char *EVP_PKEY_get0_siphash(const EVP_PKEY *pkey, size_t *len); +# endif + +# ifndef OPENSSL_NO_RSA +struct rsa_st; +int EVP_PKEY_set1_RSA(EVP_PKEY *pkey, struct rsa_st *key); +struct rsa_st *EVP_PKEY_get0_RSA(EVP_PKEY *pkey); +struct rsa_st *EVP_PKEY_get1_RSA(EVP_PKEY *pkey); +# endif +# ifndef OPENSSL_NO_DSA +struct dsa_st; +int EVP_PKEY_set1_DSA(EVP_PKEY *pkey, struct dsa_st *key); +struct dsa_st *EVP_PKEY_get0_DSA(EVP_PKEY *pkey); +struct dsa_st *EVP_PKEY_get1_DSA(EVP_PKEY *pkey); +# endif +# ifndef OPENSSL_NO_DH +struct dh_st; +int EVP_PKEY_set1_DH(EVP_PKEY *pkey, struct dh_st *key); +struct dh_st *EVP_PKEY_get0_DH(EVP_PKEY *pkey); +struct dh_st *EVP_PKEY_get1_DH(EVP_PKEY *pkey); +# endif +# ifndef OPENSSL_NO_EC +struct ec_key_st; +int EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, struct ec_key_st *key); +struct ec_key_st *EVP_PKEY_get0_EC_KEY(EVP_PKEY *pkey); +struct ec_key_st *EVP_PKEY_get1_EC_KEY(EVP_PKEY *pkey); +# endif + +EVP_PKEY *EVP_PKEY_new(void); +int EVP_PKEY_up_ref(EVP_PKEY *pkey); +void EVP_PKEY_free(EVP_PKEY *pkey); + +EVP_PKEY *d2i_PublicKey(int type, EVP_PKEY **a, const unsigned char **pp, + long length); +int i2d_PublicKey(EVP_PKEY *a, unsigned char **pp); + +EVP_PKEY *d2i_PrivateKey(int type, EVP_PKEY **a, const unsigned char **pp, + long length); +EVP_PKEY *d2i_AutoPrivateKey(EVP_PKEY **a, const unsigned char **pp, + long length); +int i2d_PrivateKey(EVP_PKEY *a, unsigned char **pp); + +int EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from); +int EVP_PKEY_missing_parameters(const EVP_PKEY *pkey); +int EVP_PKEY_save_parameters(EVP_PKEY *pkey, int mode); +int EVP_PKEY_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b); + +int EVP_PKEY_cmp(const EVP_PKEY *a, const EVP_PKEY *b); + +int EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey, + int indent, ASN1_PCTX *pctx); +int EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey, + int indent, ASN1_PCTX *pctx); +int EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey, + int indent, ASN1_PCTX *pctx); + +int EVP_PKEY_get_default_digest_nid(EVP_PKEY *pkey, int *pnid); + +int EVP_PKEY_set1_tls_encodedpoint(EVP_PKEY *pkey, + const unsigned char *pt, size_t ptlen); +size_t EVP_PKEY_get1_tls_encodedpoint(EVP_PKEY *pkey, unsigned char **ppt); + +int EVP_CIPHER_type(const EVP_CIPHER *ctx); + +/* calls methods */ +int EVP_CIPHER_param_to_asn1(EVP_CIPHER_CTX *c, ASN1_TYPE *type); +int EVP_CIPHER_asn1_to_param(EVP_CIPHER_CTX *c, ASN1_TYPE *type); + +/* These are used by EVP_CIPHER methods */ +int EVP_CIPHER_set_asn1_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type); +int EVP_CIPHER_get_asn1_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type); + +/* PKCS5 password based encryption */ +int PKCS5_PBE_keyivgen(EVP_CIPHER_CTX *ctx, const char *pass, int passlen, + ASN1_TYPE *param, const EVP_CIPHER *cipher, + const EVP_MD *md, int en_de); +int PKCS5_PBKDF2_HMAC_SHA1(const char *pass, int passlen, + const unsigned char *salt, int saltlen, int iter, + int keylen, unsigned char *out); +int PKCS5_PBKDF2_HMAC(const char *pass, int passlen, + const unsigned char *salt, int saltlen, int iter, + const EVP_MD *digest, int keylen, unsigned char *out); +int PKCS5_v2_PBE_keyivgen(EVP_CIPHER_CTX *ctx, const char *pass, int passlen, + ASN1_TYPE *param, const EVP_CIPHER *cipher, + const EVP_MD *md, int en_de); + +#ifndef OPENSSL_NO_SCRYPT +int EVP_PBE_scrypt(const char *pass, size_t passlen, + const unsigned char *salt, size_t saltlen, + uint64_t N, uint64_t r, uint64_t p, uint64_t maxmem, + unsigned char *key, size_t keylen); + +int PKCS5_v2_scrypt_keyivgen(EVP_CIPHER_CTX *ctx, const char *pass, + int passlen, ASN1_TYPE *param, + const EVP_CIPHER *c, const EVP_MD *md, int en_de); +#endif + +void PKCS5_PBE_add(void); + +int EVP_PBE_CipherInit(ASN1_OBJECT *pbe_obj, const char *pass, int passlen, + ASN1_TYPE *param, EVP_CIPHER_CTX *ctx, int en_de); + +/* PBE type */ + +/* Can appear as the outermost AlgorithmIdentifier */ +# define EVP_PBE_TYPE_OUTER 0x0 +/* Is an PRF type OID */ +# define EVP_PBE_TYPE_PRF 0x1 +/* Is a PKCS#5 v2.0 KDF */ +# define EVP_PBE_TYPE_KDF 0x2 + +int EVP_PBE_alg_add_type(int pbe_type, int pbe_nid, int cipher_nid, + int md_nid, EVP_PBE_KEYGEN *keygen); +int EVP_PBE_alg_add(int nid, const EVP_CIPHER *cipher, const EVP_MD *md, + EVP_PBE_KEYGEN *keygen); +int EVP_PBE_find(int type, int pbe_nid, int *pcnid, int *pmnid, + EVP_PBE_KEYGEN **pkeygen); +void EVP_PBE_cleanup(void); +int EVP_PBE_get(int *ptype, int *ppbe_nid, size_t num); + +# define ASN1_PKEY_ALIAS 0x1 +# define ASN1_PKEY_DYNAMIC 0x2 +# define ASN1_PKEY_SIGPARAM_NULL 0x4 + +# define ASN1_PKEY_CTRL_PKCS7_SIGN 0x1 +# define ASN1_PKEY_CTRL_PKCS7_ENCRYPT 0x2 +# define ASN1_PKEY_CTRL_DEFAULT_MD_NID 0x3 +# define ASN1_PKEY_CTRL_CMS_SIGN 0x5 +# define ASN1_PKEY_CTRL_CMS_ENVELOPE 0x7 +# define ASN1_PKEY_CTRL_CMS_RI_TYPE 0x8 + +# define ASN1_PKEY_CTRL_SET1_TLS_ENCPT 0x9 +# define ASN1_PKEY_CTRL_GET1_TLS_ENCPT 0xa + +int EVP_PKEY_asn1_get_count(void); +const EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_get0(int idx); +const EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_find(ENGINE **pe, int type); +const EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_find_str(ENGINE **pe, + const char *str, int len); +int EVP_PKEY_asn1_add0(const EVP_PKEY_ASN1_METHOD *ameth); +int EVP_PKEY_asn1_add_alias(int to, int from); +int EVP_PKEY_asn1_get0_info(int *ppkey_id, int *pkey_base_id, + int *ppkey_flags, const char **pinfo, + const char **ppem_str, + const EVP_PKEY_ASN1_METHOD *ameth); + +const EVP_PKEY_ASN1_METHOD *EVP_PKEY_get0_asn1(const EVP_PKEY *pkey); +EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_new(int id, int flags, + const char *pem_str, + const char *info); +void EVP_PKEY_asn1_copy(EVP_PKEY_ASN1_METHOD *dst, + const EVP_PKEY_ASN1_METHOD *src); +void EVP_PKEY_asn1_free(EVP_PKEY_ASN1_METHOD *ameth); +void EVP_PKEY_asn1_set_public(EVP_PKEY_ASN1_METHOD *ameth, + int (*pub_decode) (EVP_PKEY *pk, + X509_PUBKEY *pub), + int (*pub_encode) (X509_PUBKEY *pub, + const EVP_PKEY *pk), + int (*pub_cmp) (const EVP_PKEY *a, + const EVP_PKEY *b), + int (*pub_print) (BIO *out, + const EVP_PKEY *pkey, + int indent, ASN1_PCTX *pctx), + int (*pkey_size) (const EVP_PKEY *pk), + int (*pkey_bits) (const EVP_PKEY *pk)); +void EVP_PKEY_asn1_set_private(EVP_PKEY_ASN1_METHOD *ameth, + int (*priv_decode) (EVP_PKEY *pk, + const PKCS8_PRIV_KEY_INFO + *p8inf), + int (*priv_encode) (PKCS8_PRIV_KEY_INFO *p8, + const EVP_PKEY *pk), + int (*priv_print) (BIO *out, + const EVP_PKEY *pkey, + int indent, + ASN1_PCTX *pctx)); +void EVP_PKEY_asn1_set_param(EVP_PKEY_ASN1_METHOD *ameth, + int (*param_decode) (EVP_PKEY *pkey, + const unsigned char **pder, + int derlen), + int (*param_encode) (const EVP_PKEY *pkey, + unsigned char **pder), + int (*param_missing) (const EVP_PKEY *pk), + int (*param_copy) (EVP_PKEY *to, + const EVP_PKEY *from), + int (*param_cmp) (const EVP_PKEY *a, + const EVP_PKEY *b), + int (*param_print) (BIO *out, + const EVP_PKEY *pkey, + int indent, + ASN1_PCTX *pctx)); + +void EVP_PKEY_asn1_set_free(EVP_PKEY_ASN1_METHOD *ameth, + void (*pkey_free) (EVP_PKEY *pkey)); +void EVP_PKEY_asn1_set_ctrl(EVP_PKEY_ASN1_METHOD *ameth, + int (*pkey_ctrl) (EVP_PKEY *pkey, int op, + long arg1, void *arg2)); +void EVP_PKEY_asn1_set_item(EVP_PKEY_ASN1_METHOD *ameth, + int (*item_verify) (EVP_MD_CTX *ctx, + const ASN1_ITEM *it, + void *asn, + X509_ALGOR *a, + ASN1_BIT_STRING *sig, + EVP_PKEY *pkey), + int (*item_sign) (EVP_MD_CTX *ctx, + const ASN1_ITEM *it, + void *asn, + X509_ALGOR *alg1, + X509_ALGOR *alg2, + ASN1_BIT_STRING *sig)); + +void EVP_PKEY_asn1_set_siginf(EVP_PKEY_ASN1_METHOD *ameth, + int (*siginf_set) (X509_SIG_INFO *siginf, + const X509_ALGOR *alg, + const ASN1_STRING *sig)); + +void EVP_PKEY_asn1_set_check(EVP_PKEY_ASN1_METHOD *ameth, + int (*pkey_check) (const EVP_PKEY *pk)); + +void EVP_PKEY_asn1_set_public_check(EVP_PKEY_ASN1_METHOD *ameth, + int (*pkey_pub_check) (const EVP_PKEY *pk)); + +void EVP_PKEY_asn1_set_param_check(EVP_PKEY_ASN1_METHOD *ameth, + int (*pkey_param_check) (const EVP_PKEY *pk)); + +void EVP_PKEY_asn1_set_set_priv_key(EVP_PKEY_ASN1_METHOD *ameth, + int (*set_priv_key) (EVP_PKEY *pk, + const unsigned char + *priv, + size_t len)); +void EVP_PKEY_asn1_set_set_pub_key(EVP_PKEY_ASN1_METHOD *ameth, + int (*set_pub_key) (EVP_PKEY *pk, + const unsigned char *pub, + size_t len)); +void EVP_PKEY_asn1_set_get_priv_key(EVP_PKEY_ASN1_METHOD *ameth, + int (*get_priv_key) (const EVP_PKEY *pk, + unsigned char *priv, + size_t *len)); +void EVP_PKEY_asn1_set_get_pub_key(EVP_PKEY_ASN1_METHOD *ameth, + int (*get_pub_key) (const EVP_PKEY *pk, + unsigned char *pub, + size_t *len)); + +void EVP_PKEY_asn1_set_security_bits(EVP_PKEY_ASN1_METHOD *ameth, + int (*pkey_security_bits) (const EVP_PKEY + *pk)); + +# define EVP_PKEY_OP_UNDEFINED 0 +# define EVP_PKEY_OP_PARAMGEN (1<<1) +# define EVP_PKEY_OP_KEYGEN (1<<2) +# define EVP_PKEY_OP_SIGN (1<<3) +# define EVP_PKEY_OP_VERIFY (1<<4) +# define EVP_PKEY_OP_VERIFYRECOVER (1<<5) +# define EVP_PKEY_OP_SIGNCTX (1<<6) +# define EVP_PKEY_OP_VERIFYCTX (1<<7) +# define EVP_PKEY_OP_ENCRYPT (1<<8) +# define EVP_PKEY_OP_DECRYPT (1<<9) +# define EVP_PKEY_OP_DERIVE (1<<10) + +# define EVP_PKEY_OP_TYPE_SIG \ + (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_VERIFY | EVP_PKEY_OP_VERIFYRECOVER \ + | EVP_PKEY_OP_SIGNCTX | EVP_PKEY_OP_VERIFYCTX) + +# define EVP_PKEY_OP_TYPE_CRYPT \ + (EVP_PKEY_OP_ENCRYPT | EVP_PKEY_OP_DECRYPT) + +# define EVP_PKEY_OP_TYPE_NOGEN \ + (EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT | EVP_PKEY_OP_DERIVE) + +# define EVP_PKEY_OP_TYPE_GEN \ + (EVP_PKEY_OP_PARAMGEN | EVP_PKEY_OP_KEYGEN) + +# define EVP_PKEY_CTX_set_signature_md(ctx, md) \ + EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG, \ + EVP_PKEY_CTRL_MD, 0, (void *)(md)) + +# define EVP_PKEY_CTX_get_signature_md(ctx, pmd) \ + EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG, \ + EVP_PKEY_CTRL_GET_MD, 0, (void *)(pmd)) + +# define EVP_PKEY_CTX_set_mac_key(ctx, key, len) \ + EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_KEYGEN, \ + EVP_PKEY_CTRL_SET_MAC_KEY, len, (void *)(key)) + +# define EVP_PKEY_CTRL_MD 1 +# define EVP_PKEY_CTRL_PEER_KEY 2 + +# define EVP_PKEY_CTRL_PKCS7_ENCRYPT 3 +# define EVP_PKEY_CTRL_PKCS7_DECRYPT 4 + +# define EVP_PKEY_CTRL_PKCS7_SIGN 5 + +# define EVP_PKEY_CTRL_SET_MAC_KEY 6 + +# define EVP_PKEY_CTRL_DIGESTINIT 7 + +/* Used by GOST key encryption in TLS */ +# define EVP_PKEY_CTRL_SET_IV 8 + +# define EVP_PKEY_CTRL_CMS_ENCRYPT 9 +# define EVP_PKEY_CTRL_CMS_DECRYPT 10 +# define EVP_PKEY_CTRL_CMS_SIGN 11 + +# define EVP_PKEY_CTRL_CIPHER 12 + +# define EVP_PKEY_CTRL_GET_MD 13 + +# define EVP_PKEY_CTRL_SET_DIGEST_SIZE 14 + +# define EVP_PKEY_ALG_CTRL 0x1000 + +# define EVP_PKEY_FLAG_AUTOARGLEN 2 +/* + * Method handles all operations: don't assume any digest related defaults. + */ +# define EVP_PKEY_FLAG_SIGCTX_CUSTOM 4 + +const EVP_PKEY_METHOD *EVP_PKEY_meth_find(int type); +EVP_PKEY_METHOD *EVP_PKEY_meth_new(int id, int flags); +void EVP_PKEY_meth_get0_info(int *ppkey_id, int *pflags, + const EVP_PKEY_METHOD *meth); +void EVP_PKEY_meth_copy(EVP_PKEY_METHOD *dst, const EVP_PKEY_METHOD *src); +void EVP_PKEY_meth_free(EVP_PKEY_METHOD *pmeth); +int EVP_PKEY_meth_add0(const EVP_PKEY_METHOD *pmeth); +int EVP_PKEY_meth_remove(const EVP_PKEY_METHOD *pmeth); +size_t EVP_PKEY_meth_get_count(void); +const EVP_PKEY_METHOD *EVP_PKEY_meth_get0(size_t idx); + +EVP_PKEY_CTX *EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e); +EVP_PKEY_CTX *EVP_PKEY_CTX_new_id(int id, ENGINE *e); +EVP_PKEY_CTX *EVP_PKEY_CTX_dup(EVP_PKEY_CTX *ctx); +void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx); + +int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype, + int cmd, int p1, void *p2); +int EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX *ctx, const char *type, + const char *value); +int EVP_PKEY_CTX_ctrl_uint64(EVP_PKEY_CTX *ctx, int keytype, int optype, + int cmd, uint64_t value); + +int EVP_PKEY_CTX_str2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *str); +int EVP_PKEY_CTX_hex2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *hex); + +int EVP_PKEY_CTX_md(EVP_PKEY_CTX *ctx, int optype, int cmd, const char *md); + +int EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX *ctx); +void EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX *ctx, int *dat, int datlen); + +EVP_PKEY *EVP_PKEY_new_mac_key(int type, ENGINE *e, + const unsigned char *key, int keylen); +EVP_PKEY *EVP_PKEY_new_raw_private_key(int type, ENGINE *e, + const unsigned char *priv, + size_t len); +EVP_PKEY *EVP_PKEY_new_raw_public_key(int type, ENGINE *e, + const unsigned char *pub, + size_t len); +int EVP_PKEY_get_raw_private_key(const EVP_PKEY *pkey, unsigned char *priv, + size_t *len); +int EVP_PKEY_get_raw_public_key(const EVP_PKEY *pkey, unsigned char *pub, + size_t *len); + +EVP_PKEY *EVP_PKEY_new_CMAC_key(ENGINE *e, const unsigned char *priv, + size_t len, const EVP_CIPHER *cipher); + +void EVP_PKEY_CTX_set_data(EVP_PKEY_CTX *ctx, void *data); +void *EVP_PKEY_CTX_get_data(EVP_PKEY_CTX *ctx); +EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx); + +EVP_PKEY *EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX *ctx); + +void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data); +void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx); + +int EVP_PKEY_sign_init(EVP_PKEY_CTX *ctx); +int EVP_PKEY_sign(EVP_PKEY_CTX *ctx, + unsigned char *sig, size_t *siglen, + const unsigned char *tbs, size_t tbslen); +int EVP_PKEY_verify_init(EVP_PKEY_CTX *ctx); +int EVP_PKEY_verify(EVP_PKEY_CTX *ctx, + const unsigned char *sig, size_t siglen, + const unsigned char *tbs, size_t tbslen); +int EVP_PKEY_verify_recover_init(EVP_PKEY_CTX *ctx); +int EVP_PKEY_verify_recover(EVP_PKEY_CTX *ctx, + unsigned char *rout, size_t *routlen, + const unsigned char *sig, size_t siglen); +int EVP_PKEY_encrypt_init(EVP_PKEY_CTX *ctx); +int EVP_PKEY_encrypt(EVP_PKEY_CTX *ctx, + unsigned char *out, size_t *outlen, + const unsigned char *in, size_t inlen); +int EVP_PKEY_decrypt_init(EVP_PKEY_CTX *ctx); +int EVP_PKEY_decrypt(EVP_PKEY_CTX *ctx, + unsigned char *out, size_t *outlen, + const unsigned char *in, size_t inlen); + +int EVP_PKEY_derive_init(EVP_PKEY_CTX *ctx); +int EVP_PKEY_derive_set_peer(EVP_PKEY_CTX *ctx, EVP_PKEY *peer); +int EVP_PKEY_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen); + +typedef int EVP_PKEY_gen_cb(EVP_PKEY_CTX *ctx); + +int EVP_PKEY_paramgen_init(EVP_PKEY_CTX *ctx); +int EVP_PKEY_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey); +int EVP_PKEY_keygen_init(EVP_PKEY_CTX *ctx); +int EVP_PKEY_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey); +int EVP_PKEY_check(EVP_PKEY_CTX *ctx); +int EVP_PKEY_public_check(EVP_PKEY_CTX *ctx); +int EVP_PKEY_param_check(EVP_PKEY_CTX *ctx); + +void EVP_PKEY_CTX_set_cb(EVP_PKEY_CTX *ctx, EVP_PKEY_gen_cb *cb); +EVP_PKEY_gen_cb *EVP_PKEY_CTX_get_cb(EVP_PKEY_CTX *ctx); + +int EVP_PKEY_CTX_get_keygen_info(EVP_PKEY_CTX *ctx, int idx); + +void EVP_PKEY_meth_set_init(EVP_PKEY_METHOD *pmeth, + int (*init) (EVP_PKEY_CTX *ctx)); + +void EVP_PKEY_meth_set_copy(EVP_PKEY_METHOD *pmeth, + int (*copy) (EVP_PKEY_CTX *dst, + EVP_PKEY_CTX *src)); + +void EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD *pmeth, + void (*cleanup) (EVP_PKEY_CTX *ctx)); + +void EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD *pmeth, + int (*paramgen_init) (EVP_PKEY_CTX *ctx), + int (*paramgen) (EVP_PKEY_CTX *ctx, + EVP_PKEY *pkey)); + +void EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD *pmeth, + int (*keygen_init) (EVP_PKEY_CTX *ctx), + int (*keygen) (EVP_PKEY_CTX *ctx, + EVP_PKEY *pkey)); + +void EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD *pmeth, + int (*sign_init) (EVP_PKEY_CTX *ctx), + int (*sign) (EVP_PKEY_CTX *ctx, + unsigned char *sig, size_t *siglen, + const unsigned char *tbs, + size_t tbslen)); + +void EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD *pmeth, + int (*verify_init) (EVP_PKEY_CTX *ctx), + int (*verify) (EVP_PKEY_CTX *ctx, + const unsigned char *sig, + size_t siglen, + const unsigned char *tbs, + size_t tbslen)); + +void EVP_PKEY_meth_set_verify_recover(EVP_PKEY_METHOD *pmeth, + int (*verify_recover_init) (EVP_PKEY_CTX + *ctx), + int (*verify_recover) (EVP_PKEY_CTX + *ctx, + unsigned char + *sig, + size_t *siglen, + const unsigned + char *tbs, + size_t tbslen)); + +void EVP_PKEY_meth_set_signctx(EVP_PKEY_METHOD *pmeth, + int (*signctx_init) (EVP_PKEY_CTX *ctx, + EVP_MD_CTX *mctx), + int (*signctx) (EVP_PKEY_CTX *ctx, + unsigned char *sig, + size_t *siglen, + EVP_MD_CTX *mctx)); + +void EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD *pmeth, + int (*verifyctx_init) (EVP_PKEY_CTX *ctx, + EVP_MD_CTX *mctx), + int (*verifyctx) (EVP_PKEY_CTX *ctx, + const unsigned char *sig, + int siglen, + EVP_MD_CTX *mctx)); + +void EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD *pmeth, + int (*encrypt_init) (EVP_PKEY_CTX *ctx), + int (*encryptfn) (EVP_PKEY_CTX *ctx, + unsigned char *out, + size_t *outlen, + const unsigned char *in, + size_t inlen)); + +void EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD *pmeth, + int (*decrypt_init) (EVP_PKEY_CTX *ctx), + int (*decrypt) (EVP_PKEY_CTX *ctx, + unsigned char *out, + size_t *outlen, + const unsigned char *in, + size_t inlen)); + +void EVP_PKEY_meth_set_derive(EVP_PKEY_METHOD *pmeth, + int (*derive_init) (EVP_PKEY_CTX *ctx), + int (*derive) (EVP_PKEY_CTX *ctx, + unsigned char *key, + size_t *keylen)); + +void EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD *pmeth, + int (*ctrl) (EVP_PKEY_CTX *ctx, int type, int p1, + void *p2), + int (*ctrl_str) (EVP_PKEY_CTX *ctx, + const char *type, + const char *value)); + +void EVP_PKEY_meth_set_check(EVP_PKEY_METHOD *pmeth, + int (*check) (EVP_PKEY *pkey)); + +void EVP_PKEY_meth_set_public_check(EVP_PKEY_METHOD *pmeth, + int (*check) (EVP_PKEY *pkey)); + +void EVP_PKEY_meth_set_param_check(EVP_PKEY_METHOD *pmeth, + int (*check) (EVP_PKEY *pkey)); + +void EVP_PKEY_meth_set_digest_custom(EVP_PKEY_METHOD *pmeth, + int (*digest_custom) (EVP_PKEY_CTX *ctx, + EVP_MD_CTX *mctx)); + +void EVP_PKEY_meth_get_init(const EVP_PKEY_METHOD *pmeth, + int (**pinit) (EVP_PKEY_CTX *ctx)); + +void EVP_PKEY_meth_get_copy(const EVP_PKEY_METHOD *pmeth, + int (**pcopy) (EVP_PKEY_CTX *dst, + EVP_PKEY_CTX *src)); + +void EVP_PKEY_meth_get_cleanup(const EVP_PKEY_METHOD *pmeth, + void (**pcleanup) (EVP_PKEY_CTX *ctx)); + +void EVP_PKEY_meth_get_paramgen(const EVP_PKEY_METHOD *pmeth, + int (**pparamgen_init) (EVP_PKEY_CTX *ctx), + int (**pparamgen) (EVP_PKEY_CTX *ctx, + EVP_PKEY *pkey)); + +void EVP_PKEY_meth_get_keygen(const EVP_PKEY_METHOD *pmeth, + int (**pkeygen_init) (EVP_PKEY_CTX *ctx), + int (**pkeygen) (EVP_PKEY_CTX *ctx, + EVP_PKEY *pkey)); + +void EVP_PKEY_meth_get_sign(const EVP_PKEY_METHOD *pmeth, + int (**psign_init) (EVP_PKEY_CTX *ctx), + int (**psign) (EVP_PKEY_CTX *ctx, + unsigned char *sig, size_t *siglen, + const unsigned char *tbs, + size_t tbslen)); + +void EVP_PKEY_meth_get_verify(const EVP_PKEY_METHOD *pmeth, + int (**pverify_init) (EVP_PKEY_CTX *ctx), + int (**pverify) (EVP_PKEY_CTX *ctx, + const unsigned char *sig, + size_t siglen, + const unsigned char *tbs, + size_t tbslen)); + +void EVP_PKEY_meth_get_verify_recover(const EVP_PKEY_METHOD *pmeth, + int (**pverify_recover_init) (EVP_PKEY_CTX + *ctx), + int (**pverify_recover) (EVP_PKEY_CTX + *ctx, + unsigned char + *sig, + size_t *siglen, + const unsigned + char *tbs, + size_t tbslen)); + +void EVP_PKEY_meth_get_signctx(const EVP_PKEY_METHOD *pmeth, + int (**psignctx_init) (EVP_PKEY_CTX *ctx, + EVP_MD_CTX *mctx), + int (**psignctx) (EVP_PKEY_CTX *ctx, + unsigned char *sig, + size_t *siglen, + EVP_MD_CTX *mctx)); + +void EVP_PKEY_meth_get_verifyctx(const EVP_PKEY_METHOD *pmeth, + int (**pverifyctx_init) (EVP_PKEY_CTX *ctx, + EVP_MD_CTX *mctx), + int (**pverifyctx) (EVP_PKEY_CTX *ctx, + const unsigned char *sig, + int siglen, + EVP_MD_CTX *mctx)); + +void EVP_PKEY_meth_get_encrypt(const EVP_PKEY_METHOD *pmeth, + int (**pencrypt_init) (EVP_PKEY_CTX *ctx), + int (**pencryptfn) (EVP_PKEY_CTX *ctx, + unsigned char *out, + size_t *outlen, + const unsigned char *in, + size_t inlen)); + +void EVP_PKEY_meth_get_decrypt(const EVP_PKEY_METHOD *pmeth, + int (**pdecrypt_init) (EVP_PKEY_CTX *ctx), + int (**pdecrypt) (EVP_PKEY_CTX *ctx, + unsigned char *out, + size_t *outlen, + const unsigned char *in, + size_t inlen)); + +void EVP_PKEY_meth_get_derive(const EVP_PKEY_METHOD *pmeth, + int (**pderive_init) (EVP_PKEY_CTX *ctx), + int (**pderive) (EVP_PKEY_CTX *ctx, + unsigned char *key, + size_t *keylen)); + +void EVP_PKEY_meth_get_ctrl(const EVP_PKEY_METHOD *pmeth, + int (**pctrl) (EVP_PKEY_CTX *ctx, int type, int p1, + void *p2), + int (**pctrl_str) (EVP_PKEY_CTX *ctx, + const char *type, + const char *value)); + +void EVP_PKEY_meth_get_check(const EVP_PKEY_METHOD *pmeth, + int (**pcheck) (EVP_PKEY *pkey)); + +void EVP_PKEY_meth_get_public_check(const EVP_PKEY_METHOD *pmeth, + int (**pcheck) (EVP_PKEY *pkey)); + +void EVP_PKEY_meth_get_param_check(const EVP_PKEY_METHOD *pmeth, + int (**pcheck) (EVP_PKEY *pkey)); + +void EVP_PKEY_meth_get_digest_custom(EVP_PKEY_METHOD *pmeth, + int (**pdigest_custom) (EVP_PKEY_CTX *ctx, + EVP_MD_CTX *mctx)); +void EVP_add_alg_module(void); + + +# ifdef __cplusplus +} +# endif +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/evperr.h b/example/android/third_party/crypto/arm/include/openssl/evperr.h new file mode 100644 index 00000000..3484fa84 --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/evperr.h @@ -0,0 +1,193 @@ +/* + * Generated by util/mkerr.pl DO NOT EDIT + * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_EVPERR_H +# define HEADER_EVPERR_H + +# ifdef __cplusplus +extern "C" +# endif +int ERR_load_EVP_strings(void); + +/* + * EVP function codes. + */ +# define EVP_F_AESNI_INIT_KEY 165 +# define EVP_F_AES_GCM_CTRL 196 +# define EVP_F_AES_INIT_KEY 133 +# define EVP_F_AES_OCB_CIPHER 169 +# define EVP_F_AES_T4_INIT_KEY 178 +# define EVP_F_AES_WRAP_CIPHER 170 +# define EVP_F_ALG_MODULE_INIT 177 +# define EVP_F_ARIA_CCM_INIT_KEY 175 +# define EVP_F_ARIA_GCM_CTRL 197 +# define EVP_F_ARIA_GCM_INIT_KEY 176 +# define EVP_F_ARIA_INIT_KEY 185 +# define EVP_F_B64_NEW 198 +# define EVP_F_CAMELLIA_INIT_KEY 159 +# define EVP_F_CHACHA20_POLY1305_CTRL 182 +# define EVP_F_CMLL_T4_INIT_KEY 179 +# define EVP_F_DES_EDE3_WRAP_CIPHER 171 +# define EVP_F_DO_SIGVER_INIT 161 +# define EVP_F_ENC_NEW 199 +# define EVP_F_EVP_CIPHERINIT_EX 123 +# define EVP_F_EVP_CIPHER_ASN1_TO_PARAM 204 +# define EVP_F_EVP_CIPHER_CTX_COPY 163 +# define EVP_F_EVP_CIPHER_CTX_CTRL 124 +# define EVP_F_EVP_CIPHER_CTX_SET_KEY_LENGTH 122 +# define EVP_F_EVP_CIPHER_PARAM_TO_ASN1 205 +# define EVP_F_EVP_DECRYPTFINAL_EX 101 +# define EVP_F_EVP_DECRYPTUPDATE 166 +# define EVP_F_EVP_DIGESTFINALXOF 174 +# define EVP_F_EVP_DIGESTINIT_EX 128 +# define EVP_F_EVP_ENCRYPTFINAL_EX 127 +# define EVP_F_EVP_ENCRYPTUPDATE 167 +# define EVP_F_EVP_MD_CTX_COPY_EX 110 +# define EVP_F_EVP_MD_SIZE 162 +# define EVP_F_EVP_OPENINIT 102 +# define EVP_F_EVP_PBE_ALG_ADD 115 +# define EVP_F_EVP_PBE_ALG_ADD_TYPE 160 +# define EVP_F_EVP_PBE_CIPHERINIT 116 +# define EVP_F_EVP_PBE_SCRYPT 181 +# define EVP_F_EVP_PKCS82PKEY 111 +# define EVP_F_EVP_PKEY2PKCS8 113 +# define EVP_F_EVP_PKEY_ASN1_ADD0 188 +# define EVP_F_EVP_PKEY_CHECK 186 +# define EVP_F_EVP_PKEY_COPY_PARAMETERS 103 +# define EVP_F_EVP_PKEY_CTX_CTRL 137 +# define EVP_F_EVP_PKEY_CTX_CTRL_STR 150 +# define EVP_F_EVP_PKEY_CTX_DUP 156 +# define EVP_F_EVP_PKEY_CTX_MD 168 +# define EVP_F_EVP_PKEY_DECRYPT 104 +# define EVP_F_EVP_PKEY_DECRYPT_INIT 138 +# define EVP_F_EVP_PKEY_DECRYPT_OLD 151 +# define EVP_F_EVP_PKEY_DERIVE 153 +# define EVP_F_EVP_PKEY_DERIVE_INIT 154 +# define EVP_F_EVP_PKEY_DERIVE_SET_PEER 155 +# define EVP_F_EVP_PKEY_ENCRYPT 105 +# define EVP_F_EVP_PKEY_ENCRYPT_INIT 139 +# define EVP_F_EVP_PKEY_ENCRYPT_OLD 152 +# define EVP_F_EVP_PKEY_GET0_DH 119 +# define EVP_F_EVP_PKEY_GET0_DSA 120 +# define EVP_F_EVP_PKEY_GET0_EC_KEY 131 +# define EVP_F_EVP_PKEY_GET0_HMAC 183 +# define EVP_F_EVP_PKEY_GET0_POLY1305 184 +# define EVP_F_EVP_PKEY_GET0_RSA 121 +# define EVP_F_EVP_PKEY_GET0_SIPHASH 172 +# define EVP_F_EVP_PKEY_GET_RAW_PRIVATE_KEY 202 +# define EVP_F_EVP_PKEY_GET_RAW_PUBLIC_KEY 203 +# define EVP_F_EVP_PKEY_KEYGEN 146 +# define EVP_F_EVP_PKEY_KEYGEN_INIT 147 +# define EVP_F_EVP_PKEY_METH_ADD0 194 +# define EVP_F_EVP_PKEY_METH_NEW 195 +# define EVP_F_EVP_PKEY_NEW 106 +# define EVP_F_EVP_PKEY_NEW_CMAC_KEY 193 +# define EVP_F_EVP_PKEY_NEW_RAW_PRIVATE_KEY 191 +# define EVP_F_EVP_PKEY_NEW_RAW_PUBLIC_KEY 192 +# define EVP_F_EVP_PKEY_PARAMGEN 148 +# define EVP_F_EVP_PKEY_PARAMGEN_INIT 149 +# define EVP_F_EVP_PKEY_PARAM_CHECK 189 +# define EVP_F_EVP_PKEY_PUBLIC_CHECK 190 +# define EVP_F_EVP_PKEY_SET1_ENGINE 187 +# define EVP_F_EVP_PKEY_SET_ALIAS_TYPE 206 +# define EVP_F_EVP_PKEY_SIGN 140 +# define EVP_F_EVP_PKEY_SIGN_INIT 141 +# define EVP_F_EVP_PKEY_VERIFY 142 +# define EVP_F_EVP_PKEY_VERIFY_INIT 143 +# define EVP_F_EVP_PKEY_VERIFY_RECOVER 144 +# define EVP_F_EVP_PKEY_VERIFY_RECOVER_INIT 145 +# define EVP_F_EVP_SIGNFINAL 107 +# define EVP_F_EVP_VERIFYFINAL 108 +# define EVP_F_INT_CTX_NEW 157 +# define EVP_F_OK_NEW 200 +# define EVP_F_PKCS5_PBE_KEYIVGEN 117 +# define EVP_F_PKCS5_V2_PBE_KEYIVGEN 118 +# define EVP_F_PKCS5_V2_PBKDF2_KEYIVGEN 164 +# define EVP_F_PKCS5_V2_SCRYPT_KEYIVGEN 180 +# define EVP_F_PKEY_SET_TYPE 158 +# define EVP_F_RC2_MAGIC_TO_METH 109 +# define EVP_F_RC5_CTRL 125 +# define EVP_F_S390X_AES_GCM_CTRL 201 +# define EVP_F_UPDATE 173 + +/* + * EVP reason codes. + */ +# define EVP_R_AES_KEY_SETUP_FAILED 143 +# define EVP_R_ARIA_KEY_SETUP_FAILED 176 +# define EVP_R_BAD_DECRYPT 100 +# define EVP_R_BUFFER_TOO_SMALL 155 +# define EVP_R_CAMELLIA_KEY_SETUP_FAILED 157 +# define EVP_R_CIPHER_PARAMETER_ERROR 122 +# define EVP_R_COMMAND_NOT_SUPPORTED 147 +# define EVP_R_COPY_ERROR 173 +# define EVP_R_CTRL_NOT_IMPLEMENTED 132 +# define EVP_R_CTRL_OPERATION_NOT_IMPLEMENTED 133 +# define EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH 138 +# define EVP_R_DECODE_ERROR 114 +# define EVP_R_DIFFERENT_KEY_TYPES 101 +# define EVP_R_DIFFERENT_PARAMETERS 153 +# define EVP_R_ERROR_LOADING_SECTION 165 +# define EVP_R_ERROR_SETTING_FIPS_MODE 166 +# define EVP_R_EXPECTING_AN_HMAC_KEY 174 +# define EVP_R_EXPECTING_AN_RSA_KEY 127 +# define EVP_R_EXPECTING_A_DH_KEY 128 +# define EVP_R_EXPECTING_A_DSA_KEY 129 +# define EVP_R_EXPECTING_A_EC_KEY 142 +# define EVP_R_EXPECTING_A_POLY1305_KEY 164 +# define EVP_R_EXPECTING_A_SIPHASH_KEY 175 +# define EVP_R_FIPS_MODE_NOT_SUPPORTED 167 +# define EVP_R_GET_RAW_KEY_FAILED 182 +# define EVP_R_ILLEGAL_SCRYPT_PARAMETERS 171 +# define EVP_R_INITIALIZATION_ERROR 134 +# define EVP_R_INPUT_NOT_INITIALIZED 111 +# define EVP_R_INVALID_DIGEST 152 +# define EVP_R_INVALID_FIPS_MODE 168 +# define EVP_R_INVALID_KEY 163 +# define EVP_R_INVALID_KEY_LENGTH 130 +# define EVP_R_INVALID_OPERATION 148 +# define EVP_R_KEYGEN_FAILURE 120 +# define EVP_R_KEY_SETUP_FAILED 180 +# define EVP_R_MEMORY_LIMIT_EXCEEDED 172 +# define EVP_R_MESSAGE_DIGEST_IS_NULL 159 +# define EVP_R_METHOD_NOT_SUPPORTED 144 +# define EVP_R_MISSING_PARAMETERS 103 +# define EVP_R_NOT_XOF_OR_INVALID_LENGTH 178 +# define EVP_R_NO_CIPHER_SET 131 +# define EVP_R_NO_DEFAULT_DIGEST 158 +# define EVP_R_NO_DIGEST_SET 139 +# define EVP_R_NO_KEY_SET 154 +# define EVP_R_NO_OPERATION_SET 149 +# define EVP_R_ONLY_ONESHOT_SUPPORTED 177 +# define EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE 150 +# define EVP_R_OPERATON_NOT_INITIALIZED 151 +# define EVP_R_PARTIALLY_OVERLAPPING 162 +# define EVP_R_PBKDF2_ERROR 181 +# define EVP_R_PKEY_APPLICATION_ASN1_METHOD_ALREADY_REGISTERED 179 +# define EVP_R_PRIVATE_KEY_DECODE_ERROR 145 +# define EVP_R_PRIVATE_KEY_ENCODE_ERROR 146 +# define EVP_R_PUBLIC_KEY_NOT_RSA 106 +# define EVP_R_UNKNOWN_CIPHER 160 +# define EVP_R_UNKNOWN_DIGEST 161 +# define EVP_R_UNKNOWN_OPTION 169 +# define EVP_R_UNKNOWN_PBE_ALGORITHM 121 +# define EVP_R_UNSUPPORTED_ALGORITHM 156 +# define EVP_R_UNSUPPORTED_CIPHER 107 +# define EVP_R_UNSUPPORTED_KEYLENGTH 123 +# define EVP_R_UNSUPPORTED_KEY_DERIVATION_FUNCTION 124 +# define EVP_R_UNSUPPORTED_KEY_SIZE 108 +# define EVP_R_UNSUPPORTED_NUMBER_OF_ROUNDS 135 +# define EVP_R_UNSUPPORTED_PRF 125 +# define EVP_R_UNSUPPORTED_PRIVATE_KEY_ALGORITHM 118 +# define EVP_R_UNSUPPORTED_SALT_TYPE 126 +# define EVP_R_WRAP_MODE_NOT_ALLOWED 170 +# define EVP_R_WRONG_FINAL_BLOCK_LENGTH 109 + +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/hmac.h b/example/android/third_party/crypto/arm/include/openssl/hmac.h new file mode 100644 index 00000000..458efc1d --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/hmac.h @@ -0,0 +1,51 @@ +/* + * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_HMAC_H +# define HEADER_HMAC_H + +# include + +# include + +# if OPENSSL_API_COMPAT < 0x10200000L +# define HMAC_MAX_MD_CBLOCK 128 /* Deprecated */ +# endif + +#ifdef __cplusplus +extern "C" { +#endif + +size_t HMAC_size(const HMAC_CTX *e); +HMAC_CTX *HMAC_CTX_new(void); +int HMAC_CTX_reset(HMAC_CTX *ctx); +void HMAC_CTX_free(HMAC_CTX *ctx); + +DEPRECATEDIN_1_1_0(__owur int HMAC_Init(HMAC_CTX *ctx, const void *key, int len, + const EVP_MD *md)) + +/*__owur*/ int HMAC_Init_ex(HMAC_CTX *ctx, const void *key, int len, + const EVP_MD *md, ENGINE *impl); +/*__owur*/ int HMAC_Update(HMAC_CTX *ctx, const unsigned char *data, + size_t len); +/*__owur*/ int HMAC_Final(HMAC_CTX *ctx, unsigned char *md, + unsigned int *len); +unsigned char *HMAC(const EVP_MD *evp_md, const void *key, int key_len, + const unsigned char *d, size_t n, unsigned char *md, + unsigned int *md_len); +__owur int HMAC_CTX_copy(HMAC_CTX *dctx, HMAC_CTX *sctx); + +void HMAC_CTX_set_flags(HMAC_CTX *ctx, unsigned long flags); +const EVP_MD *HMAC_CTX_get_md(const HMAC_CTX *ctx); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/idea.h b/example/android/third_party/crypto/arm/include/openssl/idea.h new file mode 100644 index 00000000..4334f3ea --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/idea.h @@ -0,0 +1,64 @@ +/* + * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_IDEA_H +# define HEADER_IDEA_H + +# include + +# ifndef OPENSSL_NO_IDEA +# ifdef __cplusplus +extern "C" { +# endif + +typedef unsigned int IDEA_INT; + +# define IDEA_ENCRYPT 1 +# define IDEA_DECRYPT 0 + +# define IDEA_BLOCK 8 +# define IDEA_KEY_LENGTH 16 + +typedef struct idea_key_st { + IDEA_INT data[9][6]; +} IDEA_KEY_SCHEDULE; + +const char *IDEA_options(void); +void IDEA_ecb_encrypt(const unsigned char *in, unsigned char *out, + IDEA_KEY_SCHEDULE *ks); +void IDEA_set_encrypt_key(const unsigned char *key, IDEA_KEY_SCHEDULE *ks); +void IDEA_set_decrypt_key(IDEA_KEY_SCHEDULE *ek, IDEA_KEY_SCHEDULE *dk); +void IDEA_cbc_encrypt(const unsigned char *in, unsigned char *out, + long length, IDEA_KEY_SCHEDULE *ks, unsigned char *iv, + int enc); +void IDEA_cfb64_encrypt(const unsigned char *in, unsigned char *out, + long length, IDEA_KEY_SCHEDULE *ks, unsigned char *iv, + int *num, int enc); +void IDEA_ofb64_encrypt(const unsigned char *in, unsigned char *out, + long length, IDEA_KEY_SCHEDULE *ks, unsigned char *iv, + int *num); +void IDEA_encrypt(unsigned long *in, IDEA_KEY_SCHEDULE *ks); + +# if OPENSSL_API_COMPAT < 0x10100000L +# define idea_options IDEA_options +# define idea_ecb_encrypt IDEA_ecb_encrypt +# define idea_set_encrypt_key IDEA_set_encrypt_key +# define idea_set_decrypt_key IDEA_set_decrypt_key +# define idea_cbc_encrypt IDEA_cbc_encrypt +# define idea_cfb64_encrypt IDEA_cfb64_encrypt +# define idea_ofb64_encrypt IDEA_ofb64_encrypt +# define idea_encrypt IDEA_encrypt +# endif + +# ifdef __cplusplus +} +# endif +# endif + +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/kdf.h b/example/android/third_party/crypto/arm/include/openssl/kdf.h new file mode 100644 index 00000000..5abd4c37 --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/kdf.h @@ -0,0 +1,97 @@ +/* + * Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_KDF_H +# define HEADER_KDF_H + +# include +#ifdef __cplusplus +extern "C" { +#endif + +# define EVP_PKEY_CTRL_TLS_MD (EVP_PKEY_ALG_CTRL) +# define EVP_PKEY_CTRL_TLS_SECRET (EVP_PKEY_ALG_CTRL + 1) +# define EVP_PKEY_CTRL_TLS_SEED (EVP_PKEY_ALG_CTRL + 2) +# define EVP_PKEY_CTRL_HKDF_MD (EVP_PKEY_ALG_CTRL + 3) +# define EVP_PKEY_CTRL_HKDF_SALT (EVP_PKEY_ALG_CTRL + 4) +# define EVP_PKEY_CTRL_HKDF_KEY (EVP_PKEY_ALG_CTRL + 5) +# define EVP_PKEY_CTRL_HKDF_INFO (EVP_PKEY_ALG_CTRL + 6) +# define EVP_PKEY_CTRL_HKDF_MODE (EVP_PKEY_ALG_CTRL + 7) +# define EVP_PKEY_CTRL_PASS (EVP_PKEY_ALG_CTRL + 8) +# define EVP_PKEY_CTRL_SCRYPT_SALT (EVP_PKEY_ALG_CTRL + 9) +# define EVP_PKEY_CTRL_SCRYPT_N (EVP_PKEY_ALG_CTRL + 10) +# define EVP_PKEY_CTRL_SCRYPT_R (EVP_PKEY_ALG_CTRL + 11) +# define EVP_PKEY_CTRL_SCRYPT_P (EVP_PKEY_ALG_CTRL + 12) +# define EVP_PKEY_CTRL_SCRYPT_MAXMEM_BYTES (EVP_PKEY_ALG_CTRL + 13) + +# define EVP_PKEY_HKDEF_MODE_EXTRACT_AND_EXPAND 0 +# define EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY 1 +# define EVP_PKEY_HKDEF_MODE_EXPAND_ONLY 2 + +# define EVP_PKEY_CTX_set_tls1_prf_md(pctx, md) \ + EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_DERIVE, \ + EVP_PKEY_CTRL_TLS_MD, 0, (void *)(md)) + +# define EVP_PKEY_CTX_set1_tls1_prf_secret(pctx, sec, seclen) \ + EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_DERIVE, \ + EVP_PKEY_CTRL_TLS_SECRET, seclen, (void *)(sec)) + +# define EVP_PKEY_CTX_add1_tls1_prf_seed(pctx, seed, seedlen) \ + EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_DERIVE, \ + EVP_PKEY_CTRL_TLS_SEED, seedlen, (void *)(seed)) + +# define EVP_PKEY_CTX_set_hkdf_md(pctx, md) \ + EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_DERIVE, \ + EVP_PKEY_CTRL_HKDF_MD, 0, (void *)(md)) + +# define EVP_PKEY_CTX_set1_hkdf_salt(pctx, salt, saltlen) \ + EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_DERIVE, \ + EVP_PKEY_CTRL_HKDF_SALT, saltlen, (void *)(salt)) + +# define EVP_PKEY_CTX_set1_hkdf_key(pctx, key, keylen) \ + EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_DERIVE, \ + EVP_PKEY_CTRL_HKDF_KEY, keylen, (void *)(key)) + +# define EVP_PKEY_CTX_add1_hkdf_info(pctx, info, infolen) \ + EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_DERIVE, \ + EVP_PKEY_CTRL_HKDF_INFO, infolen, (void *)(info)) + +# define EVP_PKEY_CTX_hkdf_mode(pctx, mode) \ + EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_DERIVE, \ + EVP_PKEY_CTRL_HKDF_MODE, mode, NULL) + +# define EVP_PKEY_CTX_set1_pbe_pass(pctx, pass, passlen) \ + EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_DERIVE, \ + EVP_PKEY_CTRL_PASS, passlen, (void *)(pass)) + +# define EVP_PKEY_CTX_set1_scrypt_salt(pctx, salt, saltlen) \ + EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_DERIVE, \ + EVP_PKEY_CTRL_SCRYPT_SALT, saltlen, (void *)(salt)) + +# define EVP_PKEY_CTX_set_scrypt_N(pctx, n) \ + EVP_PKEY_CTX_ctrl_uint64(pctx, -1, EVP_PKEY_OP_DERIVE, \ + EVP_PKEY_CTRL_SCRYPT_N, n) + +# define EVP_PKEY_CTX_set_scrypt_r(pctx, r) \ + EVP_PKEY_CTX_ctrl_uint64(pctx, -1, EVP_PKEY_OP_DERIVE, \ + EVP_PKEY_CTRL_SCRYPT_R, r) + +# define EVP_PKEY_CTX_set_scrypt_p(pctx, p) \ + EVP_PKEY_CTX_ctrl_uint64(pctx, -1, EVP_PKEY_OP_DERIVE, \ + EVP_PKEY_CTRL_SCRYPT_P, p) + +# define EVP_PKEY_CTX_set_scrypt_maxmem_bytes(pctx, maxmem_bytes) \ + EVP_PKEY_CTX_ctrl_uint64(pctx, -1, EVP_PKEY_OP_DERIVE, \ + EVP_PKEY_CTRL_SCRYPT_MAXMEM_BYTES, maxmem_bytes) + + +# ifdef __cplusplus +} +# endif +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/kdferr.h b/example/android/third_party/crypto/arm/include/openssl/kdferr.h new file mode 100644 index 00000000..6437c271 --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/kdferr.h @@ -0,0 +1,51 @@ +/* + * Generated by util/mkerr.pl DO NOT EDIT + * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_KDFERR_H +# define HEADER_KDFERR_H + +# ifdef __cplusplus +extern "C" +# endif +int ERR_load_KDF_strings(void); + +/* + * KDF function codes. + */ +# define KDF_F_PKEY_HKDF_CTRL_STR 103 +# define KDF_F_PKEY_HKDF_DERIVE 102 +# define KDF_F_PKEY_HKDF_INIT 108 +# define KDF_F_PKEY_SCRYPT_CTRL_STR 104 +# define KDF_F_PKEY_SCRYPT_CTRL_UINT64 105 +# define KDF_F_PKEY_SCRYPT_DERIVE 109 +# define KDF_F_PKEY_SCRYPT_INIT 106 +# define KDF_F_PKEY_SCRYPT_SET_MEMBUF 107 +# define KDF_F_PKEY_TLS1_PRF_CTRL_STR 100 +# define KDF_F_PKEY_TLS1_PRF_DERIVE 101 +# define KDF_F_PKEY_TLS1_PRF_INIT 110 +# define KDF_F_TLS1_PRF_ALG 111 + +/* + * KDF reason codes. + */ +# define KDF_R_INVALID_DIGEST 100 +# define KDF_R_MISSING_ITERATION_COUNT 109 +# define KDF_R_MISSING_KEY 104 +# define KDF_R_MISSING_MESSAGE_DIGEST 105 +# define KDF_R_MISSING_PARAMETER 101 +# define KDF_R_MISSING_PASS 110 +# define KDF_R_MISSING_SALT 111 +# define KDF_R_MISSING_SECRET 107 +# define KDF_R_MISSING_SEED 106 +# define KDF_R_UNKNOWN_PARAMETER_TYPE 103 +# define KDF_R_VALUE_ERROR 108 +# define KDF_R_VALUE_MISSING 102 + +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/lhash.h b/example/android/third_party/crypto/arm/include/openssl/lhash.h new file mode 100644 index 00000000..88d7d977 --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/lhash.h @@ -0,0 +1,217 @@ +/* + * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +/* + * Header for dynamic hash table routines Author - Eric Young + */ + +#ifndef HEADER_LHASH_H +# define HEADER_LHASH_H + +# include +# include + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct lhash_node_st OPENSSL_LH_NODE; +typedef int (*OPENSSL_LH_COMPFUNC) (const void *, const void *); +typedef unsigned long (*OPENSSL_LH_HASHFUNC) (const void *); +typedef void (*OPENSSL_LH_DOALL_FUNC) (void *); +typedef void (*OPENSSL_LH_DOALL_FUNCARG) (void *, void *); +typedef struct lhash_st OPENSSL_LHASH; + +/* + * Macros for declaring and implementing type-safe wrappers for LHASH + * callbacks. This way, callbacks can be provided to LHASH structures without + * function pointer casting and the macro-defined callbacks provide + * per-variable casting before deferring to the underlying type-specific + * callbacks. NB: It is possible to place a "static" in front of both the + * DECLARE and IMPLEMENT macros if the functions are strictly internal. + */ + +/* First: "hash" functions */ +# define DECLARE_LHASH_HASH_FN(name, o_type) \ + unsigned long name##_LHASH_HASH(const void *); +# define IMPLEMENT_LHASH_HASH_FN(name, o_type) \ + unsigned long name##_LHASH_HASH(const void *arg) { \ + const o_type *a = arg; \ + return name##_hash(a); } +# define LHASH_HASH_FN(name) name##_LHASH_HASH + +/* Second: "compare" functions */ +# define DECLARE_LHASH_COMP_FN(name, o_type) \ + int name##_LHASH_COMP(const void *, const void *); +# define IMPLEMENT_LHASH_COMP_FN(name, o_type) \ + int name##_LHASH_COMP(const void *arg1, const void *arg2) { \ + const o_type *a = arg1; \ + const o_type *b = arg2; \ + return name##_cmp(a,b); } +# define LHASH_COMP_FN(name) name##_LHASH_COMP + +/* Fourth: "doall_arg" functions */ +# define DECLARE_LHASH_DOALL_ARG_FN(name, o_type, a_type) \ + void name##_LHASH_DOALL_ARG(void *, void *); +# define IMPLEMENT_LHASH_DOALL_ARG_FN(name, o_type, a_type) \ + void name##_LHASH_DOALL_ARG(void *arg1, void *arg2) { \ + o_type *a = arg1; \ + a_type *b = arg2; \ + name##_doall_arg(a, b); } +# define LHASH_DOALL_ARG_FN(name) name##_LHASH_DOALL_ARG + + +# define LH_LOAD_MULT 256 + +int OPENSSL_LH_error(OPENSSL_LHASH *lh); +OPENSSL_LHASH *OPENSSL_LH_new(OPENSSL_LH_HASHFUNC h, OPENSSL_LH_COMPFUNC c); +void OPENSSL_LH_free(OPENSSL_LHASH *lh); +void *OPENSSL_LH_insert(OPENSSL_LHASH *lh, void *data); +void *OPENSSL_LH_delete(OPENSSL_LHASH *lh, const void *data); +void *OPENSSL_LH_retrieve(OPENSSL_LHASH *lh, const void *data); +void OPENSSL_LH_doall(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNC func); +void OPENSSL_LH_doall_arg(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNCARG func, void *arg); +unsigned long OPENSSL_LH_strhash(const char *c); +unsigned long OPENSSL_LH_num_items(const OPENSSL_LHASH *lh); +unsigned long OPENSSL_LH_get_down_load(const OPENSSL_LHASH *lh); +void OPENSSL_LH_set_down_load(OPENSSL_LHASH *lh, unsigned long down_load); + +# ifndef OPENSSL_NO_STDIO +void OPENSSL_LH_stats(const OPENSSL_LHASH *lh, FILE *fp); +void OPENSSL_LH_node_stats(const OPENSSL_LHASH *lh, FILE *fp); +void OPENSSL_LH_node_usage_stats(const OPENSSL_LHASH *lh, FILE *fp); +# endif +void OPENSSL_LH_stats_bio(const OPENSSL_LHASH *lh, BIO *out); +void OPENSSL_LH_node_stats_bio(const OPENSSL_LHASH *lh, BIO *out); +void OPENSSL_LH_node_usage_stats_bio(const OPENSSL_LHASH *lh, BIO *out); + +# if OPENSSL_API_COMPAT < 0x10100000L +# define _LHASH OPENSSL_LHASH +# define LHASH_NODE OPENSSL_LH_NODE +# define lh_error OPENSSL_LH_error +# define lh_new OPENSSL_LH_new +# define lh_free OPENSSL_LH_free +# define lh_insert OPENSSL_LH_insert +# define lh_delete OPENSSL_LH_delete +# define lh_retrieve OPENSSL_LH_retrieve +# define lh_doall OPENSSL_LH_doall +# define lh_doall_arg OPENSSL_LH_doall_arg +# define lh_strhash OPENSSL_LH_strhash +# define lh_num_items OPENSSL_LH_num_items +# ifndef OPENSSL_NO_STDIO +# define lh_stats OPENSSL_LH_stats +# define lh_node_stats OPENSSL_LH_node_stats +# define lh_node_usage_stats OPENSSL_LH_node_usage_stats +# endif +# define lh_stats_bio OPENSSL_LH_stats_bio +# define lh_node_stats_bio OPENSSL_LH_node_stats_bio +# define lh_node_usage_stats_bio OPENSSL_LH_node_usage_stats_bio +# endif + +/* Type checking... */ + +# define LHASH_OF(type) struct lhash_st_##type + +# define DEFINE_LHASH_OF(type) \ + LHASH_OF(type) { union lh_##type##_dummy { void* d1; unsigned long d2; int d3; } dummy; }; \ + static ossl_inline LHASH_OF(type) * \ + lh_##type##_new(unsigned long (*hfn)(const type *), \ + int (*cfn)(const type *, const type *)) \ + { \ + return (LHASH_OF(type) *) \ + OPENSSL_LH_new((OPENSSL_LH_HASHFUNC)hfn, (OPENSSL_LH_COMPFUNC)cfn); \ + } \ + static ossl_inline void lh_##type##_free(LHASH_OF(type) *lh) \ + { \ + OPENSSL_LH_free((OPENSSL_LHASH *)lh); \ + } \ + static ossl_inline type *lh_##type##_insert(LHASH_OF(type) *lh, type *d) \ + { \ + return (type *)OPENSSL_LH_insert((OPENSSL_LHASH *)lh, d); \ + } \ + static ossl_inline type *lh_##type##_delete(LHASH_OF(type) *lh, const type *d) \ + { \ + return (type *)OPENSSL_LH_delete((OPENSSL_LHASH *)lh, d); \ + } \ + static ossl_inline type *lh_##type##_retrieve(LHASH_OF(type) *lh, const type *d) \ + { \ + return (type *)OPENSSL_LH_retrieve((OPENSSL_LHASH *)lh, d); \ + } \ + static ossl_inline int lh_##type##_error(LHASH_OF(type) *lh) \ + { \ + return OPENSSL_LH_error((OPENSSL_LHASH *)lh); \ + } \ + static ossl_inline unsigned long lh_##type##_num_items(LHASH_OF(type) *lh) \ + { \ + return OPENSSL_LH_num_items((OPENSSL_LHASH *)lh); \ + } \ + static ossl_inline void lh_##type##_node_stats_bio(const LHASH_OF(type) *lh, BIO *out) \ + { \ + OPENSSL_LH_node_stats_bio((const OPENSSL_LHASH *)lh, out); \ + } \ + static ossl_inline void lh_##type##_node_usage_stats_bio(const LHASH_OF(type) *lh, BIO *out) \ + { \ + OPENSSL_LH_node_usage_stats_bio((const OPENSSL_LHASH *)lh, out); \ + } \ + static ossl_inline void lh_##type##_stats_bio(const LHASH_OF(type) *lh, BIO *out) \ + { \ + OPENSSL_LH_stats_bio((const OPENSSL_LHASH *)lh, out); \ + } \ + static ossl_inline unsigned long lh_##type##_get_down_load(LHASH_OF(type) *lh) \ + { \ + return OPENSSL_LH_get_down_load((OPENSSL_LHASH *)lh); \ + } \ + static ossl_inline void lh_##type##_set_down_load(LHASH_OF(type) *lh, unsigned long dl) \ + { \ + OPENSSL_LH_set_down_load((OPENSSL_LHASH *)lh, dl); \ + } \ + static ossl_inline void lh_##type##_doall(LHASH_OF(type) *lh, \ + void (*doall)(type *)) \ + { \ + OPENSSL_LH_doall((OPENSSL_LHASH *)lh, (OPENSSL_LH_DOALL_FUNC)doall); \ + } \ + LHASH_OF(type) + +#define IMPLEMENT_LHASH_DOALL_ARG_CONST(type, argtype) \ + int_implement_lhash_doall(type, argtype, const type) + +#define IMPLEMENT_LHASH_DOALL_ARG(type, argtype) \ + int_implement_lhash_doall(type, argtype, type) + +#define int_implement_lhash_doall(type, argtype, cbargtype) \ + static ossl_inline void \ + lh_##type##_doall_##argtype(LHASH_OF(type) *lh, \ + void (*fn)(cbargtype *, argtype *), \ + argtype *arg) \ + { \ + OPENSSL_LH_doall_arg((OPENSSL_LHASH *)lh, (OPENSSL_LH_DOALL_FUNCARG)fn, (void *)arg); \ + } \ + LHASH_OF(type) + +DEFINE_LHASH_OF(OPENSSL_STRING); +# ifdef _MSC_VER +/* + * push and pop this warning: + * warning C4090: 'function': different 'const' qualifiers + */ +# pragma warning (push) +# pragma warning (disable: 4090) +# endif + +DEFINE_LHASH_OF(OPENSSL_CSTRING); + +# ifdef _MSC_VER +# pragma warning (pop) +# endif + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/md2.h b/example/android/third_party/crypto/arm/include/openssl/md2.h new file mode 100644 index 00000000..7faf8e3d --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/md2.h @@ -0,0 +1,44 @@ +/* + * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_MD2_H +# define HEADER_MD2_H + +# include + +# ifndef OPENSSL_NO_MD2 +# include +# ifdef __cplusplus +extern "C" { +# endif + +typedef unsigned char MD2_INT; + +# define MD2_DIGEST_LENGTH 16 +# define MD2_BLOCK 16 + +typedef struct MD2state_st { + unsigned int num; + unsigned char data[MD2_BLOCK]; + MD2_INT cksm[MD2_BLOCK]; + MD2_INT state[MD2_BLOCK]; +} MD2_CTX; + +const char *MD2_options(void); +int MD2_Init(MD2_CTX *c); +int MD2_Update(MD2_CTX *c, const unsigned char *data, size_t len); +int MD2_Final(unsigned char *md, MD2_CTX *c); +unsigned char *MD2(const unsigned char *d, size_t n, unsigned char *md); + +# ifdef __cplusplus +} +# endif +# endif + +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/md4.h b/example/android/third_party/crypto/arm/include/openssl/md4.h new file mode 100644 index 00000000..940e29db --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/md4.h @@ -0,0 +1,51 @@ +/* + * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_MD4_H +# define HEADER_MD4_H + +# include + +# ifndef OPENSSL_NO_MD4 +# include +# include +# ifdef __cplusplus +extern "C" { +# endif + +/*- + * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + * ! MD4_LONG has to be at least 32 bits wide. ! + * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + */ +# define MD4_LONG unsigned int + +# define MD4_CBLOCK 64 +# define MD4_LBLOCK (MD4_CBLOCK/4) +# define MD4_DIGEST_LENGTH 16 + +typedef struct MD4state_st { + MD4_LONG A, B, C, D; + MD4_LONG Nl, Nh; + MD4_LONG data[MD4_LBLOCK]; + unsigned int num; +} MD4_CTX; + +int MD4_Init(MD4_CTX *c); +int MD4_Update(MD4_CTX *c, const void *data, size_t len); +int MD4_Final(unsigned char *md, MD4_CTX *c); +unsigned char *MD4(const unsigned char *d, size_t n, unsigned char *md); +void MD4_Transform(MD4_CTX *c, const unsigned char *b); + +# ifdef __cplusplus +} +# endif +# endif + +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/md5.h b/example/android/third_party/crypto/arm/include/openssl/md5.h new file mode 100644 index 00000000..2deb7721 --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/md5.h @@ -0,0 +1,50 @@ +/* + * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_MD5_H +# define HEADER_MD5_H + +# include + +# ifndef OPENSSL_NO_MD5 +# include +# include +# ifdef __cplusplus +extern "C" { +# endif + +/* + * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + * ! MD5_LONG has to be at least 32 bits wide. ! + * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + */ +# define MD5_LONG unsigned int + +# define MD5_CBLOCK 64 +# define MD5_LBLOCK (MD5_CBLOCK/4) +# define MD5_DIGEST_LENGTH 16 + +typedef struct MD5state_st { + MD5_LONG A, B, C, D; + MD5_LONG Nl, Nh; + MD5_LONG data[MD5_LBLOCK]; + unsigned int num; +} MD5_CTX; + +int MD5_Init(MD5_CTX *c); +int MD5_Update(MD5_CTX *c, const void *data, size_t len); +int MD5_Final(unsigned char *md, MD5_CTX *c); +unsigned char *MD5(const unsigned char *d, size_t n, unsigned char *md); +void MD5_Transform(MD5_CTX *c, const unsigned char *b); +# ifdef __cplusplus +} +# endif +# endif + +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/mdc2.h b/example/android/third_party/crypto/arm/include/openssl/mdc2.h new file mode 100644 index 00000000..aabd2bfa --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/mdc2.h @@ -0,0 +1,42 @@ +/* + * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_MDC2_H +# define HEADER_MDC2_H + +# include + +#ifndef OPENSSL_NO_MDC2 +# include +# include +# ifdef __cplusplus +extern "C" { +# endif + +# define MDC2_BLOCK 8 +# define MDC2_DIGEST_LENGTH 16 + +typedef struct mdc2_ctx_st { + unsigned int num; + unsigned char data[MDC2_BLOCK]; + DES_cblock h, hh; + int pad_type; /* either 1 or 2, default 1 */ +} MDC2_CTX; + +int MDC2_Init(MDC2_CTX *c); +int MDC2_Update(MDC2_CTX *c, const unsigned char *data, size_t len); +int MDC2_Final(unsigned char *md, MDC2_CTX *c); +unsigned char *MDC2(const unsigned char *d, size_t n, unsigned char *md); + +# ifdef __cplusplus +} +# endif +# endif + +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/modes.h b/example/android/third_party/crypto/arm/include/openssl/modes.h new file mode 100644 index 00000000..d544f98d --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/modes.h @@ -0,0 +1,208 @@ +/* + * Copyright 2008-2016 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_MODES_H +# define HEADER_MODES_H + +# include + +# ifdef __cplusplus +extern "C" { +# endif +typedef void (*block128_f) (const unsigned char in[16], + unsigned char out[16], const void *key); + +typedef void (*cbc128_f) (const unsigned char *in, unsigned char *out, + size_t len, const void *key, + unsigned char ivec[16], int enc); + +typedef void (*ctr128_f) (const unsigned char *in, unsigned char *out, + size_t blocks, const void *key, + const unsigned char ivec[16]); + +typedef void (*ccm128_f) (const unsigned char *in, unsigned char *out, + size_t blocks, const void *key, + const unsigned char ivec[16], + unsigned char cmac[16]); + +void CRYPTO_cbc128_encrypt(const unsigned char *in, unsigned char *out, + size_t len, const void *key, + unsigned char ivec[16], block128_f block); +void CRYPTO_cbc128_decrypt(const unsigned char *in, unsigned char *out, + size_t len, const void *key, + unsigned char ivec[16], block128_f block); + +void CRYPTO_ctr128_encrypt(const unsigned char *in, unsigned char *out, + size_t len, const void *key, + unsigned char ivec[16], + unsigned char ecount_buf[16], unsigned int *num, + block128_f block); + +void CRYPTO_ctr128_encrypt_ctr32(const unsigned char *in, unsigned char *out, + size_t len, const void *key, + unsigned char ivec[16], + unsigned char ecount_buf[16], + unsigned int *num, ctr128_f ctr); + +void CRYPTO_ofb128_encrypt(const unsigned char *in, unsigned char *out, + size_t len, const void *key, + unsigned char ivec[16], int *num, + block128_f block); + +void CRYPTO_cfb128_encrypt(const unsigned char *in, unsigned char *out, + size_t len, const void *key, + unsigned char ivec[16], int *num, + int enc, block128_f block); +void CRYPTO_cfb128_8_encrypt(const unsigned char *in, unsigned char *out, + size_t length, const void *key, + unsigned char ivec[16], int *num, + int enc, block128_f block); +void CRYPTO_cfb128_1_encrypt(const unsigned char *in, unsigned char *out, + size_t bits, const void *key, + unsigned char ivec[16], int *num, + int enc, block128_f block); + +size_t CRYPTO_cts128_encrypt_block(const unsigned char *in, + unsigned char *out, size_t len, + const void *key, unsigned char ivec[16], + block128_f block); +size_t CRYPTO_cts128_encrypt(const unsigned char *in, unsigned char *out, + size_t len, const void *key, + unsigned char ivec[16], cbc128_f cbc); +size_t CRYPTO_cts128_decrypt_block(const unsigned char *in, + unsigned char *out, size_t len, + const void *key, unsigned char ivec[16], + block128_f block); +size_t CRYPTO_cts128_decrypt(const unsigned char *in, unsigned char *out, + size_t len, const void *key, + unsigned char ivec[16], cbc128_f cbc); + +size_t CRYPTO_nistcts128_encrypt_block(const unsigned char *in, + unsigned char *out, size_t len, + const void *key, + unsigned char ivec[16], + block128_f block); +size_t CRYPTO_nistcts128_encrypt(const unsigned char *in, unsigned char *out, + size_t len, const void *key, + unsigned char ivec[16], cbc128_f cbc); +size_t CRYPTO_nistcts128_decrypt_block(const unsigned char *in, + unsigned char *out, size_t len, + const void *key, + unsigned char ivec[16], + block128_f block); +size_t CRYPTO_nistcts128_decrypt(const unsigned char *in, unsigned char *out, + size_t len, const void *key, + unsigned char ivec[16], cbc128_f cbc); + +typedef struct gcm128_context GCM128_CONTEXT; + +GCM128_CONTEXT *CRYPTO_gcm128_new(void *key, block128_f block); +void CRYPTO_gcm128_init(GCM128_CONTEXT *ctx, void *key, block128_f block); +void CRYPTO_gcm128_setiv(GCM128_CONTEXT *ctx, const unsigned char *iv, + size_t len); +int CRYPTO_gcm128_aad(GCM128_CONTEXT *ctx, const unsigned char *aad, + size_t len); +int CRYPTO_gcm128_encrypt(GCM128_CONTEXT *ctx, + const unsigned char *in, unsigned char *out, + size_t len); +int CRYPTO_gcm128_decrypt(GCM128_CONTEXT *ctx, + const unsigned char *in, unsigned char *out, + size_t len); +int CRYPTO_gcm128_encrypt_ctr32(GCM128_CONTEXT *ctx, + const unsigned char *in, unsigned char *out, + size_t len, ctr128_f stream); +int CRYPTO_gcm128_decrypt_ctr32(GCM128_CONTEXT *ctx, + const unsigned char *in, unsigned char *out, + size_t len, ctr128_f stream); +int CRYPTO_gcm128_finish(GCM128_CONTEXT *ctx, const unsigned char *tag, + size_t len); +void CRYPTO_gcm128_tag(GCM128_CONTEXT *ctx, unsigned char *tag, size_t len); +void CRYPTO_gcm128_release(GCM128_CONTEXT *ctx); + +typedef struct ccm128_context CCM128_CONTEXT; + +void CRYPTO_ccm128_init(CCM128_CONTEXT *ctx, + unsigned int M, unsigned int L, void *key, + block128_f block); +int CRYPTO_ccm128_setiv(CCM128_CONTEXT *ctx, const unsigned char *nonce, + size_t nlen, size_t mlen); +void CRYPTO_ccm128_aad(CCM128_CONTEXT *ctx, const unsigned char *aad, + size_t alen); +int CRYPTO_ccm128_encrypt(CCM128_CONTEXT *ctx, const unsigned char *inp, + unsigned char *out, size_t len); +int CRYPTO_ccm128_decrypt(CCM128_CONTEXT *ctx, const unsigned char *inp, + unsigned char *out, size_t len); +int CRYPTO_ccm128_encrypt_ccm64(CCM128_CONTEXT *ctx, const unsigned char *inp, + unsigned char *out, size_t len, + ccm128_f stream); +int CRYPTO_ccm128_decrypt_ccm64(CCM128_CONTEXT *ctx, const unsigned char *inp, + unsigned char *out, size_t len, + ccm128_f stream); +size_t CRYPTO_ccm128_tag(CCM128_CONTEXT *ctx, unsigned char *tag, size_t len); + +typedef struct xts128_context XTS128_CONTEXT; + +int CRYPTO_xts128_encrypt(const XTS128_CONTEXT *ctx, + const unsigned char iv[16], + const unsigned char *inp, unsigned char *out, + size_t len, int enc); + +size_t CRYPTO_128_wrap(void *key, const unsigned char *iv, + unsigned char *out, + const unsigned char *in, size_t inlen, + block128_f block); + +size_t CRYPTO_128_unwrap(void *key, const unsigned char *iv, + unsigned char *out, + const unsigned char *in, size_t inlen, + block128_f block); +size_t CRYPTO_128_wrap_pad(void *key, const unsigned char *icv, + unsigned char *out, const unsigned char *in, + size_t inlen, block128_f block); +size_t CRYPTO_128_unwrap_pad(void *key, const unsigned char *icv, + unsigned char *out, const unsigned char *in, + size_t inlen, block128_f block); + +# ifndef OPENSSL_NO_OCB +typedef struct ocb128_context OCB128_CONTEXT; + +typedef void (*ocb128_f) (const unsigned char *in, unsigned char *out, + size_t blocks, const void *key, + size_t start_block_num, + unsigned char offset_i[16], + const unsigned char L_[][16], + unsigned char checksum[16]); + +OCB128_CONTEXT *CRYPTO_ocb128_new(void *keyenc, void *keydec, + block128_f encrypt, block128_f decrypt, + ocb128_f stream); +int CRYPTO_ocb128_init(OCB128_CONTEXT *ctx, void *keyenc, void *keydec, + block128_f encrypt, block128_f decrypt, + ocb128_f stream); +int CRYPTO_ocb128_copy_ctx(OCB128_CONTEXT *dest, OCB128_CONTEXT *src, + void *keyenc, void *keydec); +int CRYPTO_ocb128_setiv(OCB128_CONTEXT *ctx, const unsigned char *iv, + size_t len, size_t taglen); +int CRYPTO_ocb128_aad(OCB128_CONTEXT *ctx, const unsigned char *aad, + size_t len); +int CRYPTO_ocb128_encrypt(OCB128_CONTEXT *ctx, const unsigned char *in, + unsigned char *out, size_t len); +int CRYPTO_ocb128_decrypt(OCB128_CONTEXT *ctx, const unsigned char *in, + unsigned char *out, size_t len); +int CRYPTO_ocb128_finish(OCB128_CONTEXT *ctx, const unsigned char *tag, + size_t len); +int CRYPTO_ocb128_tag(OCB128_CONTEXT *ctx, unsigned char *tag, size_t len); +void CRYPTO_ocb128_cleanup(OCB128_CONTEXT *ctx); +# endif /* OPENSSL_NO_OCB */ + +# ifdef __cplusplus +} +# endif + +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/obj_mac.h b/example/android/third_party/crypto/arm/include/openssl/obj_mac.h new file mode 100644 index 00000000..80ff5a7c --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/obj_mac.h @@ -0,0 +1,5198 @@ +/* + * WARNING: do not edit! + * Generated by crypto/objects/objects.pl + * + * Copyright 2000-2018 The OpenSSL Project Authors. All Rights Reserved. + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#define SN_undef "UNDEF" +#define LN_undef "undefined" +#define NID_undef 0 +#define OBJ_undef 0L + +#define SN_itu_t "ITU-T" +#define LN_itu_t "itu-t" +#define NID_itu_t 645 +#define OBJ_itu_t 0L + +#define NID_ccitt 404 +#define OBJ_ccitt OBJ_itu_t + +#define SN_iso "ISO" +#define LN_iso "iso" +#define NID_iso 181 +#define OBJ_iso 1L + +#define SN_joint_iso_itu_t "JOINT-ISO-ITU-T" +#define LN_joint_iso_itu_t "joint-iso-itu-t" +#define NID_joint_iso_itu_t 646 +#define OBJ_joint_iso_itu_t 2L + +#define NID_joint_iso_ccitt 393 +#define OBJ_joint_iso_ccitt OBJ_joint_iso_itu_t + +#define SN_member_body "member-body" +#define LN_member_body "ISO Member Body" +#define NID_member_body 182 +#define OBJ_member_body OBJ_iso,2L + +#define SN_identified_organization "identified-organization" +#define NID_identified_organization 676 +#define OBJ_identified_organization OBJ_iso,3L + +#define SN_hmac_md5 "HMAC-MD5" +#define LN_hmac_md5 "hmac-md5" +#define NID_hmac_md5 780 +#define OBJ_hmac_md5 OBJ_identified_organization,6L,1L,5L,5L,8L,1L,1L + +#define SN_hmac_sha1 "HMAC-SHA1" +#define LN_hmac_sha1 "hmac-sha1" +#define NID_hmac_sha1 781 +#define OBJ_hmac_sha1 OBJ_identified_organization,6L,1L,5L,5L,8L,1L,2L + +#define SN_x509ExtAdmission "x509ExtAdmission" +#define LN_x509ExtAdmission "Professional Information or basis for Admission" +#define NID_x509ExtAdmission 1093 +#define OBJ_x509ExtAdmission OBJ_identified_organization,36L,8L,3L,3L + +#define SN_certicom_arc "certicom-arc" +#define NID_certicom_arc 677 +#define OBJ_certicom_arc OBJ_identified_organization,132L + +#define SN_ieee "ieee" +#define NID_ieee 1170 +#define OBJ_ieee OBJ_identified_organization,111L + +#define SN_ieee_siswg "ieee-siswg" +#define LN_ieee_siswg "IEEE Security in Storage Working Group" +#define NID_ieee_siswg 1171 +#define OBJ_ieee_siswg OBJ_ieee,2L,1619L + +#define SN_international_organizations "international-organizations" +#define LN_international_organizations "International Organizations" +#define NID_international_organizations 647 +#define OBJ_international_organizations OBJ_joint_iso_itu_t,23L + +#define SN_wap "wap" +#define NID_wap 678 +#define OBJ_wap OBJ_international_organizations,43L + +#define SN_wap_wsg "wap-wsg" +#define NID_wap_wsg 679 +#define OBJ_wap_wsg OBJ_wap,1L + +#define SN_selected_attribute_types "selected-attribute-types" +#define LN_selected_attribute_types "Selected Attribute Types" +#define NID_selected_attribute_types 394 +#define OBJ_selected_attribute_types OBJ_joint_iso_itu_t,5L,1L,5L + +#define SN_clearance "clearance" +#define NID_clearance 395 +#define OBJ_clearance OBJ_selected_attribute_types,55L + +#define SN_ISO_US "ISO-US" +#define LN_ISO_US "ISO US Member Body" +#define NID_ISO_US 183 +#define OBJ_ISO_US OBJ_member_body,840L + +#define SN_X9_57 "X9-57" +#define LN_X9_57 "X9.57" +#define NID_X9_57 184 +#define OBJ_X9_57 OBJ_ISO_US,10040L + +#define SN_X9cm "X9cm" +#define LN_X9cm "X9.57 CM ?" +#define NID_X9cm 185 +#define OBJ_X9cm OBJ_X9_57,4L + +#define SN_ISO_CN "ISO-CN" +#define LN_ISO_CN "ISO CN Member Body" +#define NID_ISO_CN 1140 +#define OBJ_ISO_CN OBJ_member_body,156L + +#define SN_oscca "oscca" +#define NID_oscca 1141 +#define OBJ_oscca OBJ_ISO_CN,10197L + +#define SN_sm_scheme "sm-scheme" +#define NID_sm_scheme 1142 +#define OBJ_sm_scheme OBJ_oscca,1L + +#define SN_dsa "DSA" +#define LN_dsa "dsaEncryption" +#define NID_dsa 116 +#define OBJ_dsa OBJ_X9cm,1L + +#define SN_dsaWithSHA1 "DSA-SHA1" +#define LN_dsaWithSHA1 "dsaWithSHA1" +#define NID_dsaWithSHA1 113 +#define OBJ_dsaWithSHA1 OBJ_X9cm,3L + +#define SN_ansi_X9_62 "ansi-X9-62" +#define LN_ansi_X9_62 "ANSI X9.62" +#define NID_ansi_X9_62 405 +#define OBJ_ansi_X9_62 OBJ_ISO_US,10045L + +#define OBJ_X9_62_id_fieldType OBJ_ansi_X9_62,1L + +#define SN_X9_62_prime_field "prime-field" +#define NID_X9_62_prime_field 406 +#define OBJ_X9_62_prime_field OBJ_X9_62_id_fieldType,1L + +#define SN_X9_62_characteristic_two_field "characteristic-two-field" +#define NID_X9_62_characteristic_two_field 407 +#define OBJ_X9_62_characteristic_two_field OBJ_X9_62_id_fieldType,2L + +#define SN_X9_62_id_characteristic_two_basis "id-characteristic-two-basis" +#define NID_X9_62_id_characteristic_two_basis 680 +#define OBJ_X9_62_id_characteristic_two_basis OBJ_X9_62_characteristic_two_field,3L + +#define SN_X9_62_onBasis "onBasis" +#define NID_X9_62_onBasis 681 +#define OBJ_X9_62_onBasis OBJ_X9_62_id_characteristic_two_basis,1L + +#define SN_X9_62_tpBasis "tpBasis" +#define NID_X9_62_tpBasis 682 +#define OBJ_X9_62_tpBasis OBJ_X9_62_id_characteristic_two_basis,2L + +#define SN_X9_62_ppBasis "ppBasis" +#define NID_X9_62_ppBasis 683 +#define OBJ_X9_62_ppBasis OBJ_X9_62_id_characteristic_two_basis,3L + +#define OBJ_X9_62_id_publicKeyType OBJ_ansi_X9_62,2L + +#define SN_X9_62_id_ecPublicKey "id-ecPublicKey" +#define NID_X9_62_id_ecPublicKey 408 +#define OBJ_X9_62_id_ecPublicKey OBJ_X9_62_id_publicKeyType,1L + +#define OBJ_X9_62_ellipticCurve OBJ_ansi_X9_62,3L + +#define OBJ_X9_62_c_TwoCurve OBJ_X9_62_ellipticCurve,0L + +#define SN_X9_62_c2pnb163v1 "c2pnb163v1" +#define NID_X9_62_c2pnb163v1 684 +#define OBJ_X9_62_c2pnb163v1 OBJ_X9_62_c_TwoCurve,1L + +#define SN_X9_62_c2pnb163v2 "c2pnb163v2" +#define NID_X9_62_c2pnb163v2 685 +#define OBJ_X9_62_c2pnb163v2 OBJ_X9_62_c_TwoCurve,2L + +#define SN_X9_62_c2pnb163v3 "c2pnb163v3" +#define NID_X9_62_c2pnb163v3 686 +#define OBJ_X9_62_c2pnb163v3 OBJ_X9_62_c_TwoCurve,3L + +#define SN_X9_62_c2pnb176v1 "c2pnb176v1" +#define NID_X9_62_c2pnb176v1 687 +#define OBJ_X9_62_c2pnb176v1 OBJ_X9_62_c_TwoCurve,4L + +#define SN_X9_62_c2tnb191v1 "c2tnb191v1" +#define NID_X9_62_c2tnb191v1 688 +#define OBJ_X9_62_c2tnb191v1 OBJ_X9_62_c_TwoCurve,5L + +#define SN_X9_62_c2tnb191v2 "c2tnb191v2" +#define NID_X9_62_c2tnb191v2 689 +#define OBJ_X9_62_c2tnb191v2 OBJ_X9_62_c_TwoCurve,6L + +#define SN_X9_62_c2tnb191v3 "c2tnb191v3" +#define NID_X9_62_c2tnb191v3 690 +#define OBJ_X9_62_c2tnb191v3 OBJ_X9_62_c_TwoCurve,7L + +#define SN_X9_62_c2onb191v4 "c2onb191v4" +#define NID_X9_62_c2onb191v4 691 +#define OBJ_X9_62_c2onb191v4 OBJ_X9_62_c_TwoCurve,8L + +#define SN_X9_62_c2onb191v5 "c2onb191v5" +#define NID_X9_62_c2onb191v5 692 +#define OBJ_X9_62_c2onb191v5 OBJ_X9_62_c_TwoCurve,9L + +#define SN_X9_62_c2pnb208w1 "c2pnb208w1" +#define NID_X9_62_c2pnb208w1 693 +#define OBJ_X9_62_c2pnb208w1 OBJ_X9_62_c_TwoCurve,10L + +#define SN_X9_62_c2tnb239v1 "c2tnb239v1" +#define NID_X9_62_c2tnb239v1 694 +#define OBJ_X9_62_c2tnb239v1 OBJ_X9_62_c_TwoCurve,11L + +#define SN_X9_62_c2tnb239v2 "c2tnb239v2" +#define NID_X9_62_c2tnb239v2 695 +#define OBJ_X9_62_c2tnb239v2 OBJ_X9_62_c_TwoCurve,12L + +#define SN_X9_62_c2tnb239v3 "c2tnb239v3" +#define NID_X9_62_c2tnb239v3 696 +#define OBJ_X9_62_c2tnb239v3 OBJ_X9_62_c_TwoCurve,13L + +#define SN_X9_62_c2onb239v4 "c2onb239v4" +#define NID_X9_62_c2onb239v4 697 +#define OBJ_X9_62_c2onb239v4 OBJ_X9_62_c_TwoCurve,14L + +#define SN_X9_62_c2onb239v5 "c2onb239v5" +#define NID_X9_62_c2onb239v5 698 +#define OBJ_X9_62_c2onb239v5 OBJ_X9_62_c_TwoCurve,15L + +#define SN_X9_62_c2pnb272w1 "c2pnb272w1" +#define NID_X9_62_c2pnb272w1 699 +#define OBJ_X9_62_c2pnb272w1 OBJ_X9_62_c_TwoCurve,16L + +#define SN_X9_62_c2pnb304w1 "c2pnb304w1" +#define NID_X9_62_c2pnb304w1 700 +#define OBJ_X9_62_c2pnb304w1 OBJ_X9_62_c_TwoCurve,17L + +#define SN_X9_62_c2tnb359v1 "c2tnb359v1" +#define NID_X9_62_c2tnb359v1 701 +#define OBJ_X9_62_c2tnb359v1 OBJ_X9_62_c_TwoCurve,18L + +#define SN_X9_62_c2pnb368w1 "c2pnb368w1" +#define NID_X9_62_c2pnb368w1 702 +#define OBJ_X9_62_c2pnb368w1 OBJ_X9_62_c_TwoCurve,19L + +#define SN_X9_62_c2tnb431r1 "c2tnb431r1" +#define NID_X9_62_c2tnb431r1 703 +#define OBJ_X9_62_c2tnb431r1 OBJ_X9_62_c_TwoCurve,20L + +#define OBJ_X9_62_primeCurve OBJ_X9_62_ellipticCurve,1L + +#define SN_X9_62_prime192v1 "prime192v1" +#define NID_X9_62_prime192v1 409 +#define OBJ_X9_62_prime192v1 OBJ_X9_62_primeCurve,1L + +#define SN_X9_62_prime192v2 "prime192v2" +#define NID_X9_62_prime192v2 410 +#define OBJ_X9_62_prime192v2 OBJ_X9_62_primeCurve,2L + +#define SN_X9_62_prime192v3 "prime192v3" +#define NID_X9_62_prime192v3 411 +#define OBJ_X9_62_prime192v3 OBJ_X9_62_primeCurve,3L + +#define SN_X9_62_prime239v1 "prime239v1" +#define NID_X9_62_prime239v1 412 +#define OBJ_X9_62_prime239v1 OBJ_X9_62_primeCurve,4L + +#define SN_X9_62_prime239v2 "prime239v2" +#define NID_X9_62_prime239v2 413 +#define OBJ_X9_62_prime239v2 OBJ_X9_62_primeCurve,5L + +#define SN_X9_62_prime239v3 "prime239v3" +#define NID_X9_62_prime239v3 414 +#define OBJ_X9_62_prime239v3 OBJ_X9_62_primeCurve,6L + +#define SN_X9_62_prime256v1 "prime256v1" +#define NID_X9_62_prime256v1 415 +#define OBJ_X9_62_prime256v1 OBJ_X9_62_primeCurve,7L + +#define OBJ_X9_62_id_ecSigType OBJ_ansi_X9_62,4L + +#define SN_ecdsa_with_SHA1 "ecdsa-with-SHA1" +#define NID_ecdsa_with_SHA1 416 +#define OBJ_ecdsa_with_SHA1 OBJ_X9_62_id_ecSigType,1L + +#define SN_ecdsa_with_Recommended "ecdsa-with-Recommended" +#define NID_ecdsa_with_Recommended 791 +#define OBJ_ecdsa_with_Recommended OBJ_X9_62_id_ecSigType,2L + +#define SN_ecdsa_with_Specified "ecdsa-with-Specified" +#define NID_ecdsa_with_Specified 792 +#define OBJ_ecdsa_with_Specified OBJ_X9_62_id_ecSigType,3L + +#define SN_ecdsa_with_SHA224 "ecdsa-with-SHA224" +#define NID_ecdsa_with_SHA224 793 +#define OBJ_ecdsa_with_SHA224 OBJ_ecdsa_with_Specified,1L + +#define SN_ecdsa_with_SHA256 "ecdsa-with-SHA256" +#define NID_ecdsa_with_SHA256 794 +#define OBJ_ecdsa_with_SHA256 OBJ_ecdsa_with_Specified,2L + +#define SN_ecdsa_with_SHA384 "ecdsa-with-SHA384" +#define NID_ecdsa_with_SHA384 795 +#define OBJ_ecdsa_with_SHA384 OBJ_ecdsa_with_Specified,3L + +#define SN_ecdsa_with_SHA512 "ecdsa-with-SHA512" +#define NID_ecdsa_with_SHA512 796 +#define OBJ_ecdsa_with_SHA512 OBJ_ecdsa_with_Specified,4L + +#define OBJ_secg_ellipticCurve OBJ_certicom_arc,0L + +#define SN_secp112r1 "secp112r1" +#define NID_secp112r1 704 +#define OBJ_secp112r1 OBJ_secg_ellipticCurve,6L + +#define SN_secp112r2 "secp112r2" +#define NID_secp112r2 705 +#define OBJ_secp112r2 OBJ_secg_ellipticCurve,7L + +#define SN_secp128r1 "secp128r1" +#define NID_secp128r1 706 +#define OBJ_secp128r1 OBJ_secg_ellipticCurve,28L + +#define SN_secp128r2 "secp128r2" +#define NID_secp128r2 707 +#define OBJ_secp128r2 OBJ_secg_ellipticCurve,29L + +#define SN_secp160k1 "secp160k1" +#define NID_secp160k1 708 +#define OBJ_secp160k1 OBJ_secg_ellipticCurve,9L + +#define SN_secp160r1 "secp160r1" +#define NID_secp160r1 709 +#define OBJ_secp160r1 OBJ_secg_ellipticCurve,8L + +#define SN_secp160r2 "secp160r2" +#define NID_secp160r2 710 +#define OBJ_secp160r2 OBJ_secg_ellipticCurve,30L + +#define SN_secp192k1 "secp192k1" +#define NID_secp192k1 711 +#define OBJ_secp192k1 OBJ_secg_ellipticCurve,31L + +#define SN_secp224k1 "secp224k1" +#define NID_secp224k1 712 +#define OBJ_secp224k1 OBJ_secg_ellipticCurve,32L + +#define SN_secp224r1 "secp224r1" +#define NID_secp224r1 713 +#define OBJ_secp224r1 OBJ_secg_ellipticCurve,33L + +#define SN_secp256k1 "secp256k1" +#define NID_secp256k1 714 +#define OBJ_secp256k1 OBJ_secg_ellipticCurve,10L + +#define SN_secp384r1 "secp384r1" +#define NID_secp384r1 715 +#define OBJ_secp384r1 OBJ_secg_ellipticCurve,34L + +#define SN_secp521r1 "secp521r1" +#define NID_secp521r1 716 +#define OBJ_secp521r1 OBJ_secg_ellipticCurve,35L + +#define SN_sect113r1 "sect113r1" +#define NID_sect113r1 717 +#define OBJ_sect113r1 OBJ_secg_ellipticCurve,4L + +#define SN_sect113r2 "sect113r2" +#define NID_sect113r2 718 +#define OBJ_sect113r2 OBJ_secg_ellipticCurve,5L + +#define SN_sect131r1 "sect131r1" +#define NID_sect131r1 719 +#define OBJ_sect131r1 OBJ_secg_ellipticCurve,22L + +#define SN_sect131r2 "sect131r2" +#define NID_sect131r2 720 +#define OBJ_sect131r2 OBJ_secg_ellipticCurve,23L + +#define SN_sect163k1 "sect163k1" +#define NID_sect163k1 721 +#define OBJ_sect163k1 OBJ_secg_ellipticCurve,1L + +#define SN_sect163r1 "sect163r1" +#define NID_sect163r1 722 +#define OBJ_sect163r1 OBJ_secg_ellipticCurve,2L + +#define SN_sect163r2 "sect163r2" +#define NID_sect163r2 723 +#define OBJ_sect163r2 OBJ_secg_ellipticCurve,15L + +#define SN_sect193r1 "sect193r1" +#define NID_sect193r1 724 +#define OBJ_sect193r1 OBJ_secg_ellipticCurve,24L + +#define SN_sect193r2 "sect193r2" +#define NID_sect193r2 725 +#define OBJ_sect193r2 OBJ_secg_ellipticCurve,25L + +#define SN_sect233k1 "sect233k1" +#define NID_sect233k1 726 +#define OBJ_sect233k1 OBJ_secg_ellipticCurve,26L + +#define SN_sect233r1 "sect233r1" +#define NID_sect233r1 727 +#define OBJ_sect233r1 OBJ_secg_ellipticCurve,27L + +#define SN_sect239k1 "sect239k1" +#define NID_sect239k1 728 +#define OBJ_sect239k1 OBJ_secg_ellipticCurve,3L + +#define SN_sect283k1 "sect283k1" +#define NID_sect283k1 729 +#define OBJ_sect283k1 OBJ_secg_ellipticCurve,16L + +#define SN_sect283r1 "sect283r1" +#define NID_sect283r1 730 +#define OBJ_sect283r1 OBJ_secg_ellipticCurve,17L + +#define SN_sect409k1 "sect409k1" +#define NID_sect409k1 731 +#define OBJ_sect409k1 OBJ_secg_ellipticCurve,36L + +#define SN_sect409r1 "sect409r1" +#define NID_sect409r1 732 +#define OBJ_sect409r1 OBJ_secg_ellipticCurve,37L + +#define SN_sect571k1 "sect571k1" +#define NID_sect571k1 733 +#define OBJ_sect571k1 OBJ_secg_ellipticCurve,38L + +#define SN_sect571r1 "sect571r1" +#define NID_sect571r1 734 +#define OBJ_sect571r1 OBJ_secg_ellipticCurve,39L + +#define OBJ_wap_wsg_idm_ecid OBJ_wap_wsg,4L + +#define SN_wap_wsg_idm_ecid_wtls1 "wap-wsg-idm-ecid-wtls1" +#define NID_wap_wsg_idm_ecid_wtls1 735 +#define OBJ_wap_wsg_idm_ecid_wtls1 OBJ_wap_wsg_idm_ecid,1L + +#define SN_wap_wsg_idm_ecid_wtls3 "wap-wsg-idm-ecid-wtls3" +#define NID_wap_wsg_idm_ecid_wtls3 736 +#define OBJ_wap_wsg_idm_ecid_wtls3 OBJ_wap_wsg_idm_ecid,3L + +#define SN_wap_wsg_idm_ecid_wtls4 "wap-wsg-idm-ecid-wtls4" +#define NID_wap_wsg_idm_ecid_wtls4 737 +#define OBJ_wap_wsg_idm_ecid_wtls4 OBJ_wap_wsg_idm_ecid,4L + +#define SN_wap_wsg_idm_ecid_wtls5 "wap-wsg-idm-ecid-wtls5" +#define NID_wap_wsg_idm_ecid_wtls5 738 +#define OBJ_wap_wsg_idm_ecid_wtls5 OBJ_wap_wsg_idm_ecid,5L + +#define SN_wap_wsg_idm_ecid_wtls6 "wap-wsg-idm-ecid-wtls6" +#define NID_wap_wsg_idm_ecid_wtls6 739 +#define OBJ_wap_wsg_idm_ecid_wtls6 OBJ_wap_wsg_idm_ecid,6L + +#define SN_wap_wsg_idm_ecid_wtls7 "wap-wsg-idm-ecid-wtls7" +#define NID_wap_wsg_idm_ecid_wtls7 740 +#define OBJ_wap_wsg_idm_ecid_wtls7 OBJ_wap_wsg_idm_ecid,7L + +#define SN_wap_wsg_idm_ecid_wtls8 "wap-wsg-idm-ecid-wtls8" +#define NID_wap_wsg_idm_ecid_wtls8 741 +#define OBJ_wap_wsg_idm_ecid_wtls8 OBJ_wap_wsg_idm_ecid,8L + +#define SN_wap_wsg_idm_ecid_wtls9 "wap-wsg-idm-ecid-wtls9" +#define NID_wap_wsg_idm_ecid_wtls9 742 +#define OBJ_wap_wsg_idm_ecid_wtls9 OBJ_wap_wsg_idm_ecid,9L + +#define SN_wap_wsg_idm_ecid_wtls10 "wap-wsg-idm-ecid-wtls10" +#define NID_wap_wsg_idm_ecid_wtls10 743 +#define OBJ_wap_wsg_idm_ecid_wtls10 OBJ_wap_wsg_idm_ecid,10L + +#define SN_wap_wsg_idm_ecid_wtls11 "wap-wsg-idm-ecid-wtls11" +#define NID_wap_wsg_idm_ecid_wtls11 744 +#define OBJ_wap_wsg_idm_ecid_wtls11 OBJ_wap_wsg_idm_ecid,11L + +#define SN_wap_wsg_idm_ecid_wtls12 "wap-wsg-idm-ecid-wtls12" +#define NID_wap_wsg_idm_ecid_wtls12 745 +#define OBJ_wap_wsg_idm_ecid_wtls12 OBJ_wap_wsg_idm_ecid,12L + +#define SN_cast5_cbc "CAST5-CBC" +#define LN_cast5_cbc "cast5-cbc" +#define NID_cast5_cbc 108 +#define OBJ_cast5_cbc OBJ_ISO_US,113533L,7L,66L,10L + +#define SN_cast5_ecb "CAST5-ECB" +#define LN_cast5_ecb "cast5-ecb" +#define NID_cast5_ecb 109 + +#define SN_cast5_cfb64 "CAST5-CFB" +#define LN_cast5_cfb64 "cast5-cfb" +#define NID_cast5_cfb64 110 + +#define SN_cast5_ofb64 "CAST5-OFB" +#define LN_cast5_ofb64 "cast5-ofb" +#define NID_cast5_ofb64 111 + +#define LN_pbeWithMD5AndCast5_CBC "pbeWithMD5AndCast5CBC" +#define NID_pbeWithMD5AndCast5_CBC 112 +#define OBJ_pbeWithMD5AndCast5_CBC OBJ_ISO_US,113533L,7L,66L,12L + +#define SN_id_PasswordBasedMAC "id-PasswordBasedMAC" +#define LN_id_PasswordBasedMAC "password based MAC" +#define NID_id_PasswordBasedMAC 782 +#define OBJ_id_PasswordBasedMAC OBJ_ISO_US,113533L,7L,66L,13L + +#define SN_id_DHBasedMac "id-DHBasedMac" +#define LN_id_DHBasedMac "Diffie-Hellman based MAC" +#define NID_id_DHBasedMac 783 +#define OBJ_id_DHBasedMac OBJ_ISO_US,113533L,7L,66L,30L + +#define SN_rsadsi "rsadsi" +#define LN_rsadsi "RSA Data Security, Inc." +#define NID_rsadsi 1 +#define OBJ_rsadsi OBJ_ISO_US,113549L + +#define SN_pkcs "pkcs" +#define LN_pkcs "RSA Data Security, Inc. PKCS" +#define NID_pkcs 2 +#define OBJ_pkcs OBJ_rsadsi,1L + +#define SN_pkcs1 "pkcs1" +#define NID_pkcs1 186 +#define OBJ_pkcs1 OBJ_pkcs,1L + +#define LN_rsaEncryption "rsaEncryption" +#define NID_rsaEncryption 6 +#define OBJ_rsaEncryption OBJ_pkcs1,1L + +#define SN_md2WithRSAEncryption "RSA-MD2" +#define LN_md2WithRSAEncryption "md2WithRSAEncryption" +#define NID_md2WithRSAEncryption 7 +#define OBJ_md2WithRSAEncryption OBJ_pkcs1,2L + +#define SN_md4WithRSAEncryption "RSA-MD4" +#define LN_md4WithRSAEncryption "md4WithRSAEncryption" +#define NID_md4WithRSAEncryption 396 +#define OBJ_md4WithRSAEncryption OBJ_pkcs1,3L + +#define SN_md5WithRSAEncryption "RSA-MD5" +#define LN_md5WithRSAEncryption "md5WithRSAEncryption" +#define NID_md5WithRSAEncryption 8 +#define OBJ_md5WithRSAEncryption OBJ_pkcs1,4L + +#define SN_sha1WithRSAEncryption "RSA-SHA1" +#define LN_sha1WithRSAEncryption "sha1WithRSAEncryption" +#define NID_sha1WithRSAEncryption 65 +#define OBJ_sha1WithRSAEncryption OBJ_pkcs1,5L + +#define SN_rsaesOaep "RSAES-OAEP" +#define LN_rsaesOaep "rsaesOaep" +#define NID_rsaesOaep 919 +#define OBJ_rsaesOaep OBJ_pkcs1,7L + +#define SN_mgf1 "MGF1" +#define LN_mgf1 "mgf1" +#define NID_mgf1 911 +#define OBJ_mgf1 OBJ_pkcs1,8L + +#define SN_pSpecified "PSPECIFIED" +#define LN_pSpecified "pSpecified" +#define NID_pSpecified 935 +#define OBJ_pSpecified OBJ_pkcs1,9L + +#define SN_rsassaPss "RSASSA-PSS" +#define LN_rsassaPss "rsassaPss" +#define NID_rsassaPss 912 +#define OBJ_rsassaPss OBJ_pkcs1,10L + +#define SN_sha256WithRSAEncryption "RSA-SHA256" +#define LN_sha256WithRSAEncryption "sha256WithRSAEncryption" +#define NID_sha256WithRSAEncryption 668 +#define OBJ_sha256WithRSAEncryption OBJ_pkcs1,11L + +#define SN_sha384WithRSAEncryption "RSA-SHA384" +#define LN_sha384WithRSAEncryption "sha384WithRSAEncryption" +#define NID_sha384WithRSAEncryption 669 +#define OBJ_sha384WithRSAEncryption OBJ_pkcs1,12L + +#define SN_sha512WithRSAEncryption "RSA-SHA512" +#define LN_sha512WithRSAEncryption "sha512WithRSAEncryption" +#define NID_sha512WithRSAEncryption 670 +#define OBJ_sha512WithRSAEncryption OBJ_pkcs1,13L + +#define SN_sha224WithRSAEncryption "RSA-SHA224" +#define LN_sha224WithRSAEncryption "sha224WithRSAEncryption" +#define NID_sha224WithRSAEncryption 671 +#define OBJ_sha224WithRSAEncryption OBJ_pkcs1,14L + +#define SN_sha512_224WithRSAEncryption "RSA-SHA512/224" +#define LN_sha512_224WithRSAEncryption "sha512-224WithRSAEncryption" +#define NID_sha512_224WithRSAEncryption 1145 +#define OBJ_sha512_224WithRSAEncryption OBJ_pkcs1,15L + +#define SN_sha512_256WithRSAEncryption "RSA-SHA512/256" +#define LN_sha512_256WithRSAEncryption "sha512-256WithRSAEncryption" +#define NID_sha512_256WithRSAEncryption 1146 +#define OBJ_sha512_256WithRSAEncryption OBJ_pkcs1,16L + +#define SN_pkcs3 "pkcs3" +#define NID_pkcs3 27 +#define OBJ_pkcs3 OBJ_pkcs,3L + +#define LN_dhKeyAgreement "dhKeyAgreement" +#define NID_dhKeyAgreement 28 +#define OBJ_dhKeyAgreement OBJ_pkcs3,1L + +#define SN_pkcs5 "pkcs5" +#define NID_pkcs5 187 +#define OBJ_pkcs5 OBJ_pkcs,5L + +#define SN_pbeWithMD2AndDES_CBC "PBE-MD2-DES" +#define LN_pbeWithMD2AndDES_CBC "pbeWithMD2AndDES-CBC" +#define NID_pbeWithMD2AndDES_CBC 9 +#define OBJ_pbeWithMD2AndDES_CBC OBJ_pkcs5,1L + +#define SN_pbeWithMD5AndDES_CBC "PBE-MD5-DES" +#define LN_pbeWithMD5AndDES_CBC "pbeWithMD5AndDES-CBC" +#define NID_pbeWithMD5AndDES_CBC 10 +#define OBJ_pbeWithMD5AndDES_CBC OBJ_pkcs5,3L + +#define SN_pbeWithMD2AndRC2_CBC "PBE-MD2-RC2-64" +#define LN_pbeWithMD2AndRC2_CBC "pbeWithMD2AndRC2-CBC" +#define NID_pbeWithMD2AndRC2_CBC 168 +#define OBJ_pbeWithMD2AndRC2_CBC OBJ_pkcs5,4L + +#define SN_pbeWithMD5AndRC2_CBC "PBE-MD5-RC2-64" +#define LN_pbeWithMD5AndRC2_CBC "pbeWithMD5AndRC2-CBC" +#define NID_pbeWithMD5AndRC2_CBC 169 +#define OBJ_pbeWithMD5AndRC2_CBC OBJ_pkcs5,6L + +#define SN_pbeWithSHA1AndDES_CBC "PBE-SHA1-DES" +#define LN_pbeWithSHA1AndDES_CBC "pbeWithSHA1AndDES-CBC" +#define NID_pbeWithSHA1AndDES_CBC 170 +#define OBJ_pbeWithSHA1AndDES_CBC OBJ_pkcs5,10L + +#define SN_pbeWithSHA1AndRC2_CBC "PBE-SHA1-RC2-64" +#define LN_pbeWithSHA1AndRC2_CBC "pbeWithSHA1AndRC2-CBC" +#define NID_pbeWithSHA1AndRC2_CBC 68 +#define OBJ_pbeWithSHA1AndRC2_CBC OBJ_pkcs5,11L + +#define LN_id_pbkdf2 "PBKDF2" +#define NID_id_pbkdf2 69 +#define OBJ_id_pbkdf2 OBJ_pkcs5,12L + +#define LN_pbes2 "PBES2" +#define NID_pbes2 161 +#define OBJ_pbes2 OBJ_pkcs5,13L + +#define LN_pbmac1 "PBMAC1" +#define NID_pbmac1 162 +#define OBJ_pbmac1 OBJ_pkcs5,14L + +#define SN_pkcs7 "pkcs7" +#define NID_pkcs7 20 +#define OBJ_pkcs7 OBJ_pkcs,7L + +#define LN_pkcs7_data "pkcs7-data" +#define NID_pkcs7_data 21 +#define OBJ_pkcs7_data OBJ_pkcs7,1L + +#define LN_pkcs7_signed "pkcs7-signedData" +#define NID_pkcs7_signed 22 +#define OBJ_pkcs7_signed OBJ_pkcs7,2L + +#define LN_pkcs7_enveloped "pkcs7-envelopedData" +#define NID_pkcs7_enveloped 23 +#define OBJ_pkcs7_enveloped OBJ_pkcs7,3L + +#define LN_pkcs7_signedAndEnveloped "pkcs7-signedAndEnvelopedData" +#define NID_pkcs7_signedAndEnveloped 24 +#define OBJ_pkcs7_signedAndEnveloped OBJ_pkcs7,4L + +#define LN_pkcs7_digest "pkcs7-digestData" +#define NID_pkcs7_digest 25 +#define OBJ_pkcs7_digest OBJ_pkcs7,5L + +#define LN_pkcs7_encrypted "pkcs7-encryptedData" +#define NID_pkcs7_encrypted 26 +#define OBJ_pkcs7_encrypted OBJ_pkcs7,6L + +#define SN_pkcs9 "pkcs9" +#define NID_pkcs9 47 +#define OBJ_pkcs9 OBJ_pkcs,9L + +#define LN_pkcs9_emailAddress "emailAddress" +#define NID_pkcs9_emailAddress 48 +#define OBJ_pkcs9_emailAddress OBJ_pkcs9,1L + +#define LN_pkcs9_unstructuredName "unstructuredName" +#define NID_pkcs9_unstructuredName 49 +#define OBJ_pkcs9_unstructuredName OBJ_pkcs9,2L + +#define LN_pkcs9_contentType "contentType" +#define NID_pkcs9_contentType 50 +#define OBJ_pkcs9_contentType OBJ_pkcs9,3L + +#define LN_pkcs9_messageDigest "messageDigest" +#define NID_pkcs9_messageDigest 51 +#define OBJ_pkcs9_messageDigest OBJ_pkcs9,4L + +#define LN_pkcs9_signingTime "signingTime" +#define NID_pkcs9_signingTime 52 +#define OBJ_pkcs9_signingTime OBJ_pkcs9,5L + +#define LN_pkcs9_countersignature "countersignature" +#define NID_pkcs9_countersignature 53 +#define OBJ_pkcs9_countersignature OBJ_pkcs9,6L + +#define LN_pkcs9_challengePassword "challengePassword" +#define NID_pkcs9_challengePassword 54 +#define OBJ_pkcs9_challengePassword OBJ_pkcs9,7L + +#define LN_pkcs9_unstructuredAddress "unstructuredAddress" +#define NID_pkcs9_unstructuredAddress 55 +#define OBJ_pkcs9_unstructuredAddress OBJ_pkcs9,8L + +#define LN_pkcs9_extCertAttributes "extendedCertificateAttributes" +#define NID_pkcs9_extCertAttributes 56 +#define OBJ_pkcs9_extCertAttributes OBJ_pkcs9,9L + +#define SN_ext_req "extReq" +#define LN_ext_req "Extension Request" +#define NID_ext_req 172 +#define OBJ_ext_req OBJ_pkcs9,14L + +#define SN_SMIMECapabilities "SMIME-CAPS" +#define LN_SMIMECapabilities "S/MIME Capabilities" +#define NID_SMIMECapabilities 167 +#define OBJ_SMIMECapabilities OBJ_pkcs9,15L + +#define SN_SMIME "SMIME" +#define LN_SMIME "S/MIME" +#define NID_SMIME 188 +#define OBJ_SMIME OBJ_pkcs9,16L + +#define SN_id_smime_mod "id-smime-mod" +#define NID_id_smime_mod 189 +#define OBJ_id_smime_mod OBJ_SMIME,0L + +#define SN_id_smime_ct "id-smime-ct" +#define NID_id_smime_ct 190 +#define OBJ_id_smime_ct OBJ_SMIME,1L + +#define SN_id_smime_aa "id-smime-aa" +#define NID_id_smime_aa 191 +#define OBJ_id_smime_aa OBJ_SMIME,2L + +#define SN_id_smime_alg "id-smime-alg" +#define NID_id_smime_alg 192 +#define OBJ_id_smime_alg OBJ_SMIME,3L + +#define SN_id_smime_cd "id-smime-cd" +#define NID_id_smime_cd 193 +#define OBJ_id_smime_cd OBJ_SMIME,4L + +#define SN_id_smime_spq "id-smime-spq" +#define NID_id_smime_spq 194 +#define OBJ_id_smime_spq OBJ_SMIME,5L + +#define SN_id_smime_cti "id-smime-cti" +#define NID_id_smime_cti 195 +#define OBJ_id_smime_cti OBJ_SMIME,6L + +#define SN_id_smime_mod_cms "id-smime-mod-cms" +#define NID_id_smime_mod_cms 196 +#define OBJ_id_smime_mod_cms OBJ_id_smime_mod,1L + +#define SN_id_smime_mod_ess "id-smime-mod-ess" +#define NID_id_smime_mod_ess 197 +#define OBJ_id_smime_mod_ess OBJ_id_smime_mod,2L + +#define SN_id_smime_mod_oid "id-smime-mod-oid" +#define NID_id_smime_mod_oid 198 +#define OBJ_id_smime_mod_oid OBJ_id_smime_mod,3L + +#define SN_id_smime_mod_msg_v3 "id-smime-mod-msg-v3" +#define NID_id_smime_mod_msg_v3 199 +#define OBJ_id_smime_mod_msg_v3 OBJ_id_smime_mod,4L + +#define SN_id_smime_mod_ets_eSignature_88 "id-smime-mod-ets-eSignature-88" +#define NID_id_smime_mod_ets_eSignature_88 200 +#define OBJ_id_smime_mod_ets_eSignature_88 OBJ_id_smime_mod,5L + +#define SN_id_smime_mod_ets_eSignature_97 "id-smime-mod-ets-eSignature-97" +#define NID_id_smime_mod_ets_eSignature_97 201 +#define OBJ_id_smime_mod_ets_eSignature_97 OBJ_id_smime_mod,6L + +#define SN_id_smime_mod_ets_eSigPolicy_88 "id-smime-mod-ets-eSigPolicy-88" +#define NID_id_smime_mod_ets_eSigPolicy_88 202 +#define OBJ_id_smime_mod_ets_eSigPolicy_88 OBJ_id_smime_mod,7L + +#define SN_id_smime_mod_ets_eSigPolicy_97 "id-smime-mod-ets-eSigPolicy-97" +#define NID_id_smime_mod_ets_eSigPolicy_97 203 +#define OBJ_id_smime_mod_ets_eSigPolicy_97 OBJ_id_smime_mod,8L + +#define SN_id_smime_ct_receipt "id-smime-ct-receipt" +#define NID_id_smime_ct_receipt 204 +#define OBJ_id_smime_ct_receipt OBJ_id_smime_ct,1L + +#define SN_id_smime_ct_authData "id-smime-ct-authData" +#define NID_id_smime_ct_authData 205 +#define OBJ_id_smime_ct_authData OBJ_id_smime_ct,2L + +#define SN_id_smime_ct_publishCert "id-smime-ct-publishCert" +#define NID_id_smime_ct_publishCert 206 +#define OBJ_id_smime_ct_publishCert OBJ_id_smime_ct,3L + +#define SN_id_smime_ct_TSTInfo "id-smime-ct-TSTInfo" +#define NID_id_smime_ct_TSTInfo 207 +#define OBJ_id_smime_ct_TSTInfo OBJ_id_smime_ct,4L + +#define SN_id_smime_ct_TDTInfo "id-smime-ct-TDTInfo" +#define NID_id_smime_ct_TDTInfo 208 +#define OBJ_id_smime_ct_TDTInfo OBJ_id_smime_ct,5L + +#define SN_id_smime_ct_contentInfo "id-smime-ct-contentInfo" +#define NID_id_smime_ct_contentInfo 209 +#define OBJ_id_smime_ct_contentInfo OBJ_id_smime_ct,6L + +#define SN_id_smime_ct_DVCSRequestData "id-smime-ct-DVCSRequestData" +#define NID_id_smime_ct_DVCSRequestData 210 +#define OBJ_id_smime_ct_DVCSRequestData OBJ_id_smime_ct,7L + +#define SN_id_smime_ct_DVCSResponseData "id-smime-ct-DVCSResponseData" +#define NID_id_smime_ct_DVCSResponseData 211 +#define OBJ_id_smime_ct_DVCSResponseData OBJ_id_smime_ct,8L + +#define SN_id_smime_ct_compressedData "id-smime-ct-compressedData" +#define NID_id_smime_ct_compressedData 786 +#define OBJ_id_smime_ct_compressedData OBJ_id_smime_ct,9L + +#define SN_id_smime_ct_contentCollection "id-smime-ct-contentCollection" +#define NID_id_smime_ct_contentCollection 1058 +#define OBJ_id_smime_ct_contentCollection OBJ_id_smime_ct,19L + +#define SN_id_smime_ct_authEnvelopedData "id-smime-ct-authEnvelopedData" +#define NID_id_smime_ct_authEnvelopedData 1059 +#define OBJ_id_smime_ct_authEnvelopedData OBJ_id_smime_ct,23L + +#define SN_id_ct_asciiTextWithCRLF "id-ct-asciiTextWithCRLF" +#define NID_id_ct_asciiTextWithCRLF 787 +#define OBJ_id_ct_asciiTextWithCRLF OBJ_id_smime_ct,27L + +#define SN_id_ct_xml "id-ct-xml" +#define NID_id_ct_xml 1060 +#define OBJ_id_ct_xml OBJ_id_smime_ct,28L + +#define SN_id_smime_aa_receiptRequest "id-smime-aa-receiptRequest" +#define NID_id_smime_aa_receiptRequest 212 +#define OBJ_id_smime_aa_receiptRequest OBJ_id_smime_aa,1L + +#define SN_id_smime_aa_securityLabel "id-smime-aa-securityLabel" +#define NID_id_smime_aa_securityLabel 213 +#define OBJ_id_smime_aa_securityLabel OBJ_id_smime_aa,2L + +#define SN_id_smime_aa_mlExpandHistory "id-smime-aa-mlExpandHistory" +#define NID_id_smime_aa_mlExpandHistory 214 +#define OBJ_id_smime_aa_mlExpandHistory OBJ_id_smime_aa,3L + +#define SN_id_smime_aa_contentHint "id-smime-aa-contentHint" +#define NID_id_smime_aa_contentHint 215 +#define OBJ_id_smime_aa_contentHint OBJ_id_smime_aa,4L + +#define SN_id_smime_aa_msgSigDigest "id-smime-aa-msgSigDigest" +#define NID_id_smime_aa_msgSigDigest 216 +#define OBJ_id_smime_aa_msgSigDigest OBJ_id_smime_aa,5L + +#define SN_id_smime_aa_encapContentType "id-smime-aa-encapContentType" +#define NID_id_smime_aa_encapContentType 217 +#define OBJ_id_smime_aa_encapContentType OBJ_id_smime_aa,6L + +#define SN_id_smime_aa_contentIdentifier "id-smime-aa-contentIdentifier" +#define NID_id_smime_aa_contentIdentifier 218 +#define OBJ_id_smime_aa_contentIdentifier OBJ_id_smime_aa,7L + +#define SN_id_smime_aa_macValue "id-smime-aa-macValue" +#define NID_id_smime_aa_macValue 219 +#define OBJ_id_smime_aa_macValue OBJ_id_smime_aa,8L + +#define SN_id_smime_aa_equivalentLabels "id-smime-aa-equivalentLabels" +#define NID_id_smime_aa_equivalentLabels 220 +#define OBJ_id_smime_aa_equivalentLabels OBJ_id_smime_aa,9L + +#define SN_id_smime_aa_contentReference "id-smime-aa-contentReference" +#define NID_id_smime_aa_contentReference 221 +#define OBJ_id_smime_aa_contentReference OBJ_id_smime_aa,10L + +#define SN_id_smime_aa_encrypKeyPref "id-smime-aa-encrypKeyPref" +#define NID_id_smime_aa_encrypKeyPref 222 +#define OBJ_id_smime_aa_encrypKeyPref OBJ_id_smime_aa,11L + +#define SN_id_smime_aa_signingCertificate "id-smime-aa-signingCertificate" +#define NID_id_smime_aa_signingCertificate 223 +#define OBJ_id_smime_aa_signingCertificate OBJ_id_smime_aa,12L + +#define SN_id_smime_aa_smimeEncryptCerts "id-smime-aa-smimeEncryptCerts" +#define NID_id_smime_aa_smimeEncryptCerts 224 +#define OBJ_id_smime_aa_smimeEncryptCerts OBJ_id_smime_aa,13L + +#define SN_id_smime_aa_timeStampToken "id-smime-aa-timeStampToken" +#define NID_id_smime_aa_timeStampToken 225 +#define OBJ_id_smime_aa_timeStampToken OBJ_id_smime_aa,14L + +#define SN_id_smime_aa_ets_sigPolicyId "id-smime-aa-ets-sigPolicyId" +#define NID_id_smime_aa_ets_sigPolicyId 226 +#define OBJ_id_smime_aa_ets_sigPolicyId OBJ_id_smime_aa,15L + +#define SN_id_smime_aa_ets_commitmentType "id-smime-aa-ets-commitmentType" +#define NID_id_smime_aa_ets_commitmentType 227 +#define OBJ_id_smime_aa_ets_commitmentType OBJ_id_smime_aa,16L + +#define SN_id_smime_aa_ets_signerLocation "id-smime-aa-ets-signerLocation" +#define NID_id_smime_aa_ets_signerLocation 228 +#define OBJ_id_smime_aa_ets_signerLocation OBJ_id_smime_aa,17L + +#define SN_id_smime_aa_ets_signerAttr "id-smime-aa-ets-signerAttr" +#define NID_id_smime_aa_ets_signerAttr 229 +#define OBJ_id_smime_aa_ets_signerAttr OBJ_id_smime_aa,18L + +#define SN_id_smime_aa_ets_otherSigCert "id-smime-aa-ets-otherSigCert" +#define NID_id_smime_aa_ets_otherSigCert 230 +#define OBJ_id_smime_aa_ets_otherSigCert OBJ_id_smime_aa,19L + +#define SN_id_smime_aa_ets_contentTimestamp "id-smime-aa-ets-contentTimestamp" +#define NID_id_smime_aa_ets_contentTimestamp 231 +#define OBJ_id_smime_aa_ets_contentTimestamp OBJ_id_smime_aa,20L + +#define SN_id_smime_aa_ets_CertificateRefs "id-smime-aa-ets-CertificateRefs" +#define NID_id_smime_aa_ets_CertificateRefs 232 +#define OBJ_id_smime_aa_ets_CertificateRefs OBJ_id_smime_aa,21L + +#define SN_id_smime_aa_ets_RevocationRefs "id-smime-aa-ets-RevocationRefs" +#define NID_id_smime_aa_ets_RevocationRefs 233 +#define OBJ_id_smime_aa_ets_RevocationRefs OBJ_id_smime_aa,22L + +#define SN_id_smime_aa_ets_certValues "id-smime-aa-ets-certValues" +#define NID_id_smime_aa_ets_certValues 234 +#define OBJ_id_smime_aa_ets_certValues OBJ_id_smime_aa,23L + +#define SN_id_smime_aa_ets_revocationValues "id-smime-aa-ets-revocationValues" +#define NID_id_smime_aa_ets_revocationValues 235 +#define OBJ_id_smime_aa_ets_revocationValues OBJ_id_smime_aa,24L + +#define SN_id_smime_aa_ets_escTimeStamp "id-smime-aa-ets-escTimeStamp" +#define NID_id_smime_aa_ets_escTimeStamp 236 +#define OBJ_id_smime_aa_ets_escTimeStamp OBJ_id_smime_aa,25L + +#define SN_id_smime_aa_ets_certCRLTimestamp "id-smime-aa-ets-certCRLTimestamp" +#define NID_id_smime_aa_ets_certCRLTimestamp 237 +#define OBJ_id_smime_aa_ets_certCRLTimestamp OBJ_id_smime_aa,26L + +#define SN_id_smime_aa_ets_archiveTimeStamp "id-smime-aa-ets-archiveTimeStamp" +#define NID_id_smime_aa_ets_archiveTimeStamp 238 +#define OBJ_id_smime_aa_ets_archiveTimeStamp OBJ_id_smime_aa,27L + +#define SN_id_smime_aa_signatureType "id-smime-aa-signatureType" +#define NID_id_smime_aa_signatureType 239 +#define OBJ_id_smime_aa_signatureType OBJ_id_smime_aa,28L + +#define SN_id_smime_aa_dvcs_dvc "id-smime-aa-dvcs-dvc" +#define NID_id_smime_aa_dvcs_dvc 240 +#define OBJ_id_smime_aa_dvcs_dvc OBJ_id_smime_aa,29L + +#define SN_id_smime_aa_signingCertificateV2 "id-smime-aa-signingCertificateV2" +#define NID_id_smime_aa_signingCertificateV2 1086 +#define OBJ_id_smime_aa_signingCertificateV2 OBJ_id_smime_aa,47L + +#define SN_id_smime_alg_ESDHwith3DES "id-smime-alg-ESDHwith3DES" +#define NID_id_smime_alg_ESDHwith3DES 241 +#define OBJ_id_smime_alg_ESDHwith3DES OBJ_id_smime_alg,1L + +#define SN_id_smime_alg_ESDHwithRC2 "id-smime-alg-ESDHwithRC2" +#define NID_id_smime_alg_ESDHwithRC2 242 +#define OBJ_id_smime_alg_ESDHwithRC2 OBJ_id_smime_alg,2L + +#define SN_id_smime_alg_3DESwrap "id-smime-alg-3DESwrap" +#define NID_id_smime_alg_3DESwrap 243 +#define OBJ_id_smime_alg_3DESwrap OBJ_id_smime_alg,3L + +#define SN_id_smime_alg_RC2wrap "id-smime-alg-RC2wrap" +#define NID_id_smime_alg_RC2wrap 244 +#define OBJ_id_smime_alg_RC2wrap OBJ_id_smime_alg,4L + +#define SN_id_smime_alg_ESDH "id-smime-alg-ESDH" +#define NID_id_smime_alg_ESDH 245 +#define OBJ_id_smime_alg_ESDH OBJ_id_smime_alg,5L + +#define SN_id_smime_alg_CMS3DESwrap "id-smime-alg-CMS3DESwrap" +#define NID_id_smime_alg_CMS3DESwrap 246 +#define OBJ_id_smime_alg_CMS3DESwrap OBJ_id_smime_alg,6L + +#define SN_id_smime_alg_CMSRC2wrap "id-smime-alg-CMSRC2wrap" +#define NID_id_smime_alg_CMSRC2wrap 247 +#define OBJ_id_smime_alg_CMSRC2wrap OBJ_id_smime_alg,7L + +#define SN_id_alg_PWRI_KEK "id-alg-PWRI-KEK" +#define NID_id_alg_PWRI_KEK 893 +#define OBJ_id_alg_PWRI_KEK OBJ_id_smime_alg,9L + +#define SN_id_smime_cd_ldap "id-smime-cd-ldap" +#define NID_id_smime_cd_ldap 248 +#define OBJ_id_smime_cd_ldap OBJ_id_smime_cd,1L + +#define SN_id_smime_spq_ets_sqt_uri "id-smime-spq-ets-sqt-uri" +#define NID_id_smime_spq_ets_sqt_uri 249 +#define OBJ_id_smime_spq_ets_sqt_uri OBJ_id_smime_spq,1L + +#define SN_id_smime_spq_ets_sqt_unotice "id-smime-spq-ets-sqt-unotice" +#define NID_id_smime_spq_ets_sqt_unotice 250 +#define OBJ_id_smime_spq_ets_sqt_unotice OBJ_id_smime_spq,2L + +#define SN_id_smime_cti_ets_proofOfOrigin "id-smime-cti-ets-proofOfOrigin" +#define NID_id_smime_cti_ets_proofOfOrigin 251 +#define OBJ_id_smime_cti_ets_proofOfOrigin OBJ_id_smime_cti,1L + +#define SN_id_smime_cti_ets_proofOfReceipt "id-smime-cti-ets-proofOfReceipt" +#define NID_id_smime_cti_ets_proofOfReceipt 252 +#define OBJ_id_smime_cti_ets_proofOfReceipt OBJ_id_smime_cti,2L + +#define SN_id_smime_cti_ets_proofOfDelivery "id-smime-cti-ets-proofOfDelivery" +#define NID_id_smime_cti_ets_proofOfDelivery 253 +#define OBJ_id_smime_cti_ets_proofOfDelivery OBJ_id_smime_cti,3L + +#define SN_id_smime_cti_ets_proofOfSender "id-smime-cti-ets-proofOfSender" +#define NID_id_smime_cti_ets_proofOfSender 254 +#define OBJ_id_smime_cti_ets_proofOfSender OBJ_id_smime_cti,4L + +#define SN_id_smime_cti_ets_proofOfApproval "id-smime-cti-ets-proofOfApproval" +#define NID_id_smime_cti_ets_proofOfApproval 255 +#define OBJ_id_smime_cti_ets_proofOfApproval OBJ_id_smime_cti,5L + +#define SN_id_smime_cti_ets_proofOfCreation "id-smime-cti-ets-proofOfCreation" +#define NID_id_smime_cti_ets_proofOfCreation 256 +#define OBJ_id_smime_cti_ets_proofOfCreation OBJ_id_smime_cti,6L + +#define LN_friendlyName "friendlyName" +#define NID_friendlyName 156 +#define OBJ_friendlyName OBJ_pkcs9,20L + +#define LN_localKeyID "localKeyID" +#define NID_localKeyID 157 +#define OBJ_localKeyID OBJ_pkcs9,21L + +#define SN_ms_csp_name "CSPName" +#define LN_ms_csp_name "Microsoft CSP Name" +#define NID_ms_csp_name 417 +#define OBJ_ms_csp_name 1L,3L,6L,1L,4L,1L,311L,17L,1L + +#define SN_LocalKeySet "LocalKeySet" +#define LN_LocalKeySet "Microsoft Local Key set" +#define NID_LocalKeySet 856 +#define OBJ_LocalKeySet 1L,3L,6L,1L,4L,1L,311L,17L,2L + +#define OBJ_certTypes OBJ_pkcs9,22L + +#define LN_x509Certificate "x509Certificate" +#define NID_x509Certificate 158 +#define OBJ_x509Certificate OBJ_certTypes,1L + +#define LN_sdsiCertificate "sdsiCertificate" +#define NID_sdsiCertificate 159 +#define OBJ_sdsiCertificate OBJ_certTypes,2L + +#define OBJ_crlTypes OBJ_pkcs9,23L + +#define LN_x509Crl "x509Crl" +#define NID_x509Crl 160 +#define OBJ_x509Crl OBJ_crlTypes,1L + +#define OBJ_pkcs12 OBJ_pkcs,12L + +#define OBJ_pkcs12_pbeids OBJ_pkcs12,1L + +#define SN_pbe_WithSHA1And128BitRC4 "PBE-SHA1-RC4-128" +#define LN_pbe_WithSHA1And128BitRC4 "pbeWithSHA1And128BitRC4" +#define NID_pbe_WithSHA1And128BitRC4 144 +#define OBJ_pbe_WithSHA1And128BitRC4 OBJ_pkcs12_pbeids,1L + +#define SN_pbe_WithSHA1And40BitRC4 "PBE-SHA1-RC4-40" +#define LN_pbe_WithSHA1And40BitRC4 "pbeWithSHA1And40BitRC4" +#define NID_pbe_WithSHA1And40BitRC4 145 +#define OBJ_pbe_WithSHA1And40BitRC4 OBJ_pkcs12_pbeids,2L + +#define SN_pbe_WithSHA1And3_Key_TripleDES_CBC "PBE-SHA1-3DES" +#define LN_pbe_WithSHA1And3_Key_TripleDES_CBC "pbeWithSHA1And3-KeyTripleDES-CBC" +#define NID_pbe_WithSHA1And3_Key_TripleDES_CBC 146 +#define OBJ_pbe_WithSHA1And3_Key_TripleDES_CBC OBJ_pkcs12_pbeids,3L + +#define SN_pbe_WithSHA1And2_Key_TripleDES_CBC "PBE-SHA1-2DES" +#define LN_pbe_WithSHA1And2_Key_TripleDES_CBC "pbeWithSHA1And2-KeyTripleDES-CBC" +#define NID_pbe_WithSHA1And2_Key_TripleDES_CBC 147 +#define OBJ_pbe_WithSHA1And2_Key_TripleDES_CBC OBJ_pkcs12_pbeids,4L + +#define SN_pbe_WithSHA1And128BitRC2_CBC "PBE-SHA1-RC2-128" +#define LN_pbe_WithSHA1And128BitRC2_CBC "pbeWithSHA1And128BitRC2-CBC" +#define NID_pbe_WithSHA1And128BitRC2_CBC 148 +#define OBJ_pbe_WithSHA1And128BitRC2_CBC OBJ_pkcs12_pbeids,5L + +#define SN_pbe_WithSHA1And40BitRC2_CBC "PBE-SHA1-RC2-40" +#define LN_pbe_WithSHA1And40BitRC2_CBC "pbeWithSHA1And40BitRC2-CBC" +#define NID_pbe_WithSHA1And40BitRC2_CBC 149 +#define OBJ_pbe_WithSHA1And40BitRC2_CBC OBJ_pkcs12_pbeids,6L + +#define OBJ_pkcs12_Version1 OBJ_pkcs12,10L + +#define OBJ_pkcs12_BagIds OBJ_pkcs12_Version1,1L + +#define LN_keyBag "keyBag" +#define NID_keyBag 150 +#define OBJ_keyBag OBJ_pkcs12_BagIds,1L + +#define LN_pkcs8ShroudedKeyBag "pkcs8ShroudedKeyBag" +#define NID_pkcs8ShroudedKeyBag 151 +#define OBJ_pkcs8ShroudedKeyBag OBJ_pkcs12_BagIds,2L + +#define LN_certBag "certBag" +#define NID_certBag 152 +#define OBJ_certBag OBJ_pkcs12_BagIds,3L + +#define LN_crlBag "crlBag" +#define NID_crlBag 153 +#define OBJ_crlBag OBJ_pkcs12_BagIds,4L + +#define LN_secretBag "secretBag" +#define NID_secretBag 154 +#define OBJ_secretBag OBJ_pkcs12_BagIds,5L + +#define LN_safeContentsBag "safeContentsBag" +#define NID_safeContentsBag 155 +#define OBJ_safeContentsBag OBJ_pkcs12_BagIds,6L + +#define SN_md2 "MD2" +#define LN_md2 "md2" +#define NID_md2 3 +#define OBJ_md2 OBJ_rsadsi,2L,2L + +#define SN_md4 "MD4" +#define LN_md4 "md4" +#define NID_md4 257 +#define OBJ_md4 OBJ_rsadsi,2L,4L + +#define SN_md5 "MD5" +#define LN_md5 "md5" +#define NID_md5 4 +#define OBJ_md5 OBJ_rsadsi,2L,5L + +#define SN_md5_sha1 "MD5-SHA1" +#define LN_md5_sha1 "md5-sha1" +#define NID_md5_sha1 114 + +#define LN_hmacWithMD5 "hmacWithMD5" +#define NID_hmacWithMD5 797 +#define OBJ_hmacWithMD5 OBJ_rsadsi,2L,6L + +#define LN_hmacWithSHA1 "hmacWithSHA1" +#define NID_hmacWithSHA1 163 +#define OBJ_hmacWithSHA1 OBJ_rsadsi,2L,7L + +#define SN_sm2 "SM2" +#define LN_sm2 "sm2" +#define NID_sm2 1172 +#define OBJ_sm2 OBJ_sm_scheme,301L + +#define SN_sm3 "SM3" +#define LN_sm3 "sm3" +#define NID_sm3 1143 +#define OBJ_sm3 OBJ_sm_scheme,401L + +#define SN_sm3WithRSAEncryption "RSA-SM3" +#define LN_sm3WithRSAEncryption "sm3WithRSAEncryption" +#define NID_sm3WithRSAEncryption 1144 +#define OBJ_sm3WithRSAEncryption OBJ_sm_scheme,504L + +#define LN_hmacWithSHA224 "hmacWithSHA224" +#define NID_hmacWithSHA224 798 +#define OBJ_hmacWithSHA224 OBJ_rsadsi,2L,8L + +#define LN_hmacWithSHA256 "hmacWithSHA256" +#define NID_hmacWithSHA256 799 +#define OBJ_hmacWithSHA256 OBJ_rsadsi,2L,9L + +#define LN_hmacWithSHA384 "hmacWithSHA384" +#define NID_hmacWithSHA384 800 +#define OBJ_hmacWithSHA384 OBJ_rsadsi,2L,10L + +#define LN_hmacWithSHA512 "hmacWithSHA512" +#define NID_hmacWithSHA512 801 +#define OBJ_hmacWithSHA512 OBJ_rsadsi,2L,11L + +#define LN_hmacWithSHA512_224 "hmacWithSHA512-224" +#define NID_hmacWithSHA512_224 1193 +#define OBJ_hmacWithSHA512_224 OBJ_rsadsi,2L,12L + +#define LN_hmacWithSHA512_256 "hmacWithSHA512-256" +#define NID_hmacWithSHA512_256 1194 +#define OBJ_hmacWithSHA512_256 OBJ_rsadsi,2L,13L + +#define SN_rc2_cbc "RC2-CBC" +#define LN_rc2_cbc "rc2-cbc" +#define NID_rc2_cbc 37 +#define OBJ_rc2_cbc OBJ_rsadsi,3L,2L + +#define SN_rc2_ecb "RC2-ECB" +#define LN_rc2_ecb "rc2-ecb" +#define NID_rc2_ecb 38 + +#define SN_rc2_cfb64 "RC2-CFB" +#define LN_rc2_cfb64 "rc2-cfb" +#define NID_rc2_cfb64 39 + +#define SN_rc2_ofb64 "RC2-OFB" +#define LN_rc2_ofb64 "rc2-ofb" +#define NID_rc2_ofb64 40 + +#define SN_rc2_40_cbc "RC2-40-CBC" +#define LN_rc2_40_cbc "rc2-40-cbc" +#define NID_rc2_40_cbc 98 + +#define SN_rc2_64_cbc "RC2-64-CBC" +#define LN_rc2_64_cbc "rc2-64-cbc" +#define NID_rc2_64_cbc 166 + +#define SN_rc4 "RC4" +#define LN_rc4 "rc4" +#define NID_rc4 5 +#define OBJ_rc4 OBJ_rsadsi,3L,4L + +#define SN_rc4_40 "RC4-40" +#define LN_rc4_40 "rc4-40" +#define NID_rc4_40 97 + +#define SN_des_ede3_cbc "DES-EDE3-CBC" +#define LN_des_ede3_cbc "des-ede3-cbc" +#define NID_des_ede3_cbc 44 +#define OBJ_des_ede3_cbc OBJ_rsadsi,3L,7L + +#define SN_rc5_cbc "RC5-CBC" +#define LN_rc5_cbc "rc5-cbc" +#define NID_rc5_cbc 120 +#define OBJ_rc5_cbc OBJ_rsadsi,3L,8L + +#define SN_rc5_ecb "RC5-ECB" +#define LN_rc5_ecb "rc5-ecb" +#define NID_rc5_ecb 121 + +#define SN_rc5_cfb64 "RC5-CFB" +#define LN_rc5_cfb64 "rc5-cfb" +#define NID_rc5_cfb64 122 + +#define SN_rc5_ofb64 "RC5-OFB" +#define LN_rc5_ofb64 "rc5-ofb" +#define NID_rc5_ofb64 123 + +#define SN_ms_ext_req "msExtReq" +#define LN_ms_ext_req "Microsoft Extension Request" +#define NID_ms_ext_req 171 +#define OBJ_ms_ext_req 1L,3L,6L,1L,4L,1L,311L,2L,1L,14L + +#define SN_ms_code_ind "msCodeInd" +#define LN_ms_code_ind "Microsoft Individual Code Signing" +#define NID_ms_code_ind 134 +#define OBJ_ms_code_ind 1L,3L,6L,1L,4L,1L,311L,2L,1L,21L + +#define SN_ms_code_com "msCodeCom" +#define LN_ms_code_com "Microsoft Commercial Code Signing" +#define NID_ms_code_com 135 +#define OBJ_ms_code_com 1L,3L,6L,1L,4L,1L,311L,2L,1L,22L + +#define SN_ms_ctl_sign "msCTLSign" +#define LN_ms_ctl_sign "Microsoft Trust List Signing" +#define NID_ms_ctl_sign 136 +#define OBJ_ms_ctl_sign 1L,3L,6L,1L,4L,1L,311L,10L,3L,1L + +#define SN_ms_sgc "msSGC" +#define LN_ms_sgc "Microsoft Server Gated Crypto" +#define NID_ms_sgc 137 +#define OBJ_ms_sgc 1L,3L,6L,1L,4L,1L,311L,10L,3L,3L + +#define SN_ms_efs "msEFS" +#define LN_ms_efs "Microsoft Encrypted File System" +#define NID_ms_efs 138 +#define OBJ_ms_efs 1L,3L,6L,1L,4L,1L,311L,10L,3L,4L + +#define SN_ms_smartcard_login "msSmartcardLogin" +#define LN_ms_smartcard_login "Microsoft Smartcardlogin" +#define NID_ms_smartcard_login 648 +#define OBJ_ms_smartcard_login 1L,3L,6L,1L,4L,1L,311L,20L,2L,2L + +#define SN_ms_upn "msUPN" +#define LN_ms_upn "Microsoft Universal Principal Name" +#define NID_ms_upn 649 +#define OBJ_ms_upn 1L,3L,6L,1L,4L,1L,311L,20L,2L,3L + +#define SN_idea_cbc "IDEA-CBC" +#define LN_idea_cbc "idea-cbc" +#define NID_idea_cbc 34 +#define OBJ_idea_cbc 1L,3L,6L,1L,4L,1L,188L,7L,1L,1L,2L + +#define SN_idea_ecb "IDEA-ECB" +#define LN_idea_ecb "idea-ecb" +#define NID_idea_ecb 36 + +#define SN_idea_cfb64 "IDEA-CFB" +#define LN_idea_cfb64 "idea-cfb" +#define NID_idea_cfb64 35 + +#define SN_idea_ofb64 "IDEA-OFB" +#define LN_idea_ofb64 "idea-ofb" +#define NID_idea_ofb64 46 + +#define SN_bf_cbc "BF-CBC" +#define LN_bf_cbc "bf-cbc" +#define NID_bf_cbc 91 +#define OBJ_bf_cbc 1L,3L,6L,1L,4L,1L,3029L,1L,2L + +#define SN_bf_ecb "BF-ECB" +#define LN_bf_ecb "bf-ecb" +#define NID_bf_ecb 92 + +#define SN_bf_cfb64 "BF-CFB" +#define LN_bf_cfb64 "bf-cfb" +#define NID_bf_cfb64 93 + +#define SN_bf_ofb64 "BF-OFB" +#define LN_bf_ofb64 "bf-ofb" +#define NID_bf_ofb64 94 + +#define SN_id_pkix "PKIX" +#define NID_id_pkix 127 +#define OBJ_id_pkix 1L,3L,6L,1L,5L,5L,7L + +#define SN_id_pkix_mod "id-pkix-mod" +#define NID_id_pkix_mod 258 +#define OBJ_id_pkix_mod OBJ_id_pkix,0L + +#define SN_id_pe "id-pe" +#define NID_id_pe 175 +#define OBJ_id_pe OBJ_id_pkix,1L + +#define SN_id_qt "id-qt" +#define NID_id_qt 259 +#define OBJ_id_qt OBJ_id_pkix,2L + +#define SN_id_kp "id-kp" +#define NID_id_kp 128 +#define OBJ_id_kp OBJ_id_pkix,3L + +#define SN_id_it "id-it" +#define NID_id_it 260 +#define OBJ_id_it OBJ_id_pkix,4L + +#define SN_id_pkip "id-pkip" +#define NID_id_pkip 261 +#define OBJ_id_pkip OBJ_id_pkix,5L + +#define SN_id_alg "id-alg" +#define NID_id_alg 262 +#define OBJ_id_alg OBJ_id_pkix,6L + +#define SN_id_cmc "id-cmc" +#define NID_id_cmc 263 +#define OBJ_id_cmc OBJ_id_pkix,7L + +#define SN_id_on "id-on" +#define NID_id_on 264 +#define OBJ_id_on OBJ_id_pkix,8L + +#define SN_id_pda "id-pda" +#define NID_id_pda 265 +#define OBJ_id_pda OBJ_id_pkix,9L + +#define SN_id_aca "id-aca" +#define NID_id_aca 266 +#define OBJ_id_aca OBJ_id_pkix,10L + +#define SN_id_qcs "id-qcs" +#define NID_id_qcs 267 +#define OBJ_id_qcs OBJ_id_pkix,11L + +#define SN_id_cct "id-cct" +#define NID_id_cct 268 +#define OBJ_id_cct OBJ_id_pkix,12L + +#define SN_id_ppl "id-ppl" +#define NID_id_ppl 662 +#define OBJ_id_ppl OBJ_id_pkix,21L + +#define SN_id_ad "id-ad" +#define NID_id_ad 176 +#define OBJ_id_ad OBJ_id_pkix,48L + +#define SN_id_pkix1_explicit_88 "id-pkix1-explicit-88" +#define NID_id_pkix1_explicit_88 269 +#define OBJ_id_pkix1_explicit_88 OBJ_id_pkix_mod,1L + +#define SN_id_pkix1_implicit_88 "id-pkix1-implicit-88" +#define NID_id_pkix1_implicit_88 270 +#define OBJ_id_pkix1_implicit_88 OBJ_id_pkix_mod,2L + +#define SN_id_pkix1_explicit_93 "id-pkix1-explicit-93" +#define NID_id_pkix1_explicit_93 271 +#define OBJ_id_pkix1_explicit_93 OBJ_id_pkix_mod,3L + +#define SN_id_pkix1_implicit_93 "id-pkix1-implicit-93" +#define NID_id_pkix1_implicit_93 272 +#define OBJ_id_pkix1_implicit_93 OBJ_id_pkix_mod,4L + +#define SN_id_mod_crmf "id-mod-crmf" +#define NID_id_mod_crmf 273 +#define OBJ_id_mod_crmf OBJ_id_pkix_mod,5L + +#define SN_id_mod_cmc "id-mod-cmc" +#define NID_id_mod_cmc 274 +#define OBJ_id_mod_cmc OBJ_id_pkix_mod,6L + +#define SN_id_mod_kea_profile_88 "id-mod-kea-profile-88" +#define NID_id_mod_kea_profile_88 275 +#define OBJ_id_mod_kea_profile_88 OBJ_id_pkix_mod,7L + +#define SN_id_mod_kea_profile_93 "id-mod-kea-profile-93" +#define NID_id_mod_kea_profile_93 276 +#define OBJ_id_mod_kea_profile_93 OBJ_id_pkix_mod,8L + +#define SN_id_mod_cmp "id-mod-cmp" +#define NID_id_mod_cmp 277 +#define OBJ_id_mod_cmp OBJ_id_pkix_mod,9L + +#define SN_id_mod_qualified_cert_88 "id-mod-qualified-cert-88" +#define NID_id_mod_qualified_cert_88 278 +#define OBJ_id_mod_qualified_cert_88 OBJ_id_pkix_mod,10L + +#define SN_id_mod_qualified_cert_93 "id-mod-qualified-cert-93" +#define NID_id_mod_qualified_cert_93 279 +#define OBJ_id_mod_qualified_cert_93 OBJ_id_pkix_mod,11L + +#define SN_id_mod_attribute_cert "id-mod-attribute-cert" +#define NID_id_mod_attribute_cert 280 +#define OBJ_id_mod_attribute_cert OBJ_id_pkix_mod,12L + +#define SN_id_mod_timestamp_protocol "id-mod-timestamp-protocol" +#define NID_id_mod_timestamp_protocol 281 +#define OBJ_id_mod_timestamp_protocol OBJ_id_pkix_mod,13L + +#define SN_id_mod_ocsp "id-mod-ocsp" +#define NID_id_mod_ocsp 282 +#define OBJ_id_mod_ocsp OBJ_id_pkix_mod,14L + +#define SN_id_mod_dvcs "id-mod-dvcs" +#define NID_id_mod_dvcs 283 +#define OBJ_id_mod_dvcs OBJ_id_pkix_mod,15L + +#define SN_id_mod_cmp2000 "id-mod-cmp2000" +#define NID_id_mod_cmp2000 284 +#define OBJ_id_mod_cmp2000 OBJ_id_pkix_mod,16L + +#define SN_info_access "authorityInfoAccess" +#define LN_info_access "Authority Information Access" +#define NID_info_access 177 +#define OBJ_info_access OBJ_id_pe,1L + +#define SN_biometricInfo "biometricInfo" +#define LN_biometricInfo "Biometric Info" +#define NID_biometricInfo 285 +#define OBJ_biometricInfo OBJ_id_pe,2L + +#define SN_qcStatements "qcStatements" +#define NID_qcStatements 286 +#define OBJ_qcStatements OBJ_id_pe,3L + +#define SN_ac_auditEntity "ac-auditEntity" +#define NID_ac_auditEntity 287 +#define OBJ_ac_auditEntity OBJ_id_pe,4L + +#define SN_ac_targeting "ac-targeting" +#define NID_ac_targeting 288 +#define OBJ_ac_targeting OBJ_id_pe,5L + +#define SN_aaControls "aaControls" +#define NID_aaControls 289 +#define OBJ_aaControls OBJ_id_pe,6L + +#define SN_sbgp_ipAddrBlock "sbgp-ipAddrBlock" +#define NID_sbgp_ipAddrBlock 290 +#define OBJ_sbgp_ipAddrBlock OBJ_id_pe,7L + +#define SN_sbgp_autonomousSysNum "sbgp-autonomousSysNum" +#define NID_sbgp_autonomousSysNum 291 +#define OBJ_sbgp_autonomousSysNum OBJ_id_pe,8L + +#define SN_sbgp_routerIdentifier "sbgp-routerIdentifier" +#define NID_sbgp_routerIdentifier 292 +#define OBJ_sbgp_routerIdentifier OBJ_id_pe,9L + +#define SN_ac_proxying "ac-proxying" +#define NID_ac_proxying 397 +#define OBJ_ac_proxying OBJ_id_pe,10L + +#define SN_sinfo_access "subjectInfoAccess" +#define LN_sinfo_access "Subject Information Access" +#define NID_sinfo_access 398 +#define OBJ_sinfo_access OBJ_id_pe,11L + +#define SN_proxyCertInfo "proxyCertInfo" +#define LN_proxyCertInfo "Proxy Certificate Information" +#define NID_proxyCertInfo 663 +#define OBJ_proxyCertInfo OBJ_id_pe,14L + +#define SN_tlsfeature "tlsfeature" +#define LN_tlsfeature "TLS Feature" +#define NID_tlsfeature 1020 +#define OBJ_tlsfeature OBJ_id_pe,24L + +#define SN_id_qt_cps "id-qt-cps" +#define LN_id_qt_cps "Policy Qualifier CPS" +#define NID_id_qt_cps 164 +#define OBJ_id_qt_cps OBJ_id_qt,1L + +#define SN_id_qt_unotice "id-qt-unotice" +#define LN_id_qt_unotice "Policy Qualifier User Notice" +#define NID_id_qt_unotice 165 +#define OBJ_id_qt_unotice OBJ_id_qt,2L + +#define SN_textNotice "textNotice" +#define NID_textNotice 293 +#define OBJ_textNotice OBJ_id_qt,3L + +#define SN_server_auth "serverAuth" +#define LN_server_auth "TLS Web Server Authentication" +#define NID_server_auth 129 +#define OBJ_server_auth OBJ_id_kp,1L + +#define SN_client_auth "clientAuth" +#define LN_client_auth "TLS Web Client Authentication" +#define NID_client_auth 130 +#define OBJ_client_auth OBJ_id_kp,2L + +#define SN_code_sign "codeSigning" +#define LN_code_sign "Code Signing" +#define NID_code_sign 131 +#define OBJ_code_sign OBJ_id_kp,3L + +#define SN_email_protect "emailProtection" +#define LN_email_protect "E-mail Protection" +#define NID_email_protect 132 +#define OBJ_email_protect OBJ_id_kp,4L + +#define SN_ipsecEndSystem "ipsecEndSystem" +#define LN_ipsecEndSystem "IPSec End System" +#define NID_ipsecEndSystem 294 +#define OBJ_ipsecEndSystem OBJ_id_kp,5L + +#define SN_ipsecTunnel "ipsecTunnel" +#define LN_ipsecTunnel "IPSec Tunnel" +#define NID_ipsecTunnel 295 +#define OBJ_ipsecTunnel OBJ_id_kp,6L + +#define SN_ipsecUser "ipsecUser" +#define LN_ipsecUser "IPSec User" +#define NID_ipsecUser 296 +#define OBJ_ipsecUser OBJ_id_kp,7L + +#define SN_time_stamp "timeStamping" +#define LN_time_stamp "Time Stamping" +#define NID_time_stamp 133 +#define OBJ_time_stamp OBJ_id_kp,8L + +#define SN_OCSP_sign "OCSPSigning" +#define LN_OCSP_sign "OCSP Signing" +#define NID_OCSP_sign 180 +#define OBJ_OCSP_sign OBJ_id_kp,9L + +#define SN_dvcs "DVCS" +#define LN_dvcs "dvcs" +#define NID_dvcs 297 +#define OBJ_dvcs OBJ_id_kp,10L + +#define SN_ipsec_IKE "ipsecIKE" +#define LN_ipsec_IKE "ipsec Internet Key Exchange" +#define NID_ipsec_IKE 1022 +#define OBJ_ipsec_IKE OBJ_id_kp,17L + +#define SN_capwapAC "capwapAC" +#define LN_capwapAC "Ctrl/provision WAP Access" +#define NID_capwapAC 1023 +#define OBJ_capwapAC OBJ_id_kp,18L + +#define SN_capwapWTP "capwapWTP" +#define LN_capwapWTP "Ctrl/Provision WAP Termination" +#define NID_capwapWTP 1024 +#define OBJ_capwapWTP OBJ_id_kp,19L + +#define SN_sshClient "secureShellClient" +#define LN_sshClient "SSH Client" +#define NID_sshClient 1025 +#define OBJ_sshClient OBJ_id_kp,21L + +#define SN_sshServer "secureShellServer" +#define LN_sshServer "SSH Server" +#define NID_sshServer 1026 +#define OBJ_sshServer OBJ_id_kp,22L + +#define SN_sendRouter "sendRouter" +#define LN_sendRouter "Send Router" +#define NID_sendRouter 1027 +#define OBJ_sendRouter OBJ_id_kp,23L + +#define SN_sendProxiedRouter "sendProxiedRouter" +#define LN_sendProxiedRouter "Send Proxied Router" +#define NID_sendProxiedRouter 1028 +#define OBJ_sendProxiedRouter OBJ_id_kp,24L + +#define SN_sendOwner "sendOwner" +#define LN_sendOwner "Send Owner" +#define NID_sendOwner 1029 +#define OBJ_sendOwner OBJ_id_kp,25L + +#define SN_sendProxiedOwner "sendProxiedOwner" +#define LN_sendProxiedOwner "Send Proxied Owner" +#define NID_sendProxiedOwner 1030 +#define OBJ_sendProxiedOwner OBJ_id_kp,26L + +#define SN_cmcCA "cmcCA" +#define LN_cmcCA "CMC Certificate Authority" +#define NID_cmcCA 1131 +#define OBJ_cmcCA OBJ_id_kp,27L + +#define SN_cmcRA "cmcRA" +#define LN_cmcRA "CMC Registration Authority" +#define NID_cmcRA 1132 +#define OBJ_cmcRA OBJ_id_kp,28L + +#define SN_id_it_caProtEncCert "id-it-caProtEncCert" +#define NID_id_it_caProtEncCert 298 +#define OBJ_id_it_caProtEncCert OBJ_id_it,1L + +#define SN_id_it_signKeyPairTypes "id-it-signKeyPairTypes" +#define NID_id_it_signKeyPairTypes 299 +#define OBJ_id_it_signKeyPairTypes OBJ_id_it,2L + +#define SN_id_it_encKeyPairTypes "id-it-encKeyPairTypes" +#define NID_id_it_encKeyPairTypes 300 +#define OBJ_id_it_encKeyPairTypes OBJ_id_it,3L + +#define SN_id_it_preferredSymmAlg "id-it-preferredSymmAlg" +#define NID_id_it_preferredSymmAlg 301 +#define OBJ_id_it_preferredSymmAlg OBJ_id_it,4L + +#define SN_id_it_caKeyUpdateInfo "id-it-caKeyUpdateInfo" +#define NID_id_it_caKeyUpdateInfo 302 +#define OBJ_id_it_caKeyUpdateInfo OBJ_id_it,5L + +#define SN_id_it_currentCRL "id-it-currentCRL" +#define NID_id_it_currentCRL 303 +#define OBJ_id_it_currentCRL OBJ_id_it,6L + +#define SN_id_it_unsupportedOIDs "id-it-unsupportedOIDs" +#define NID_id_it_unsupportedOIDs 304 +#define OBJ_id_it_unsupportedOIDs OBJ_id_it,7L + +#define SN_id_it_subscriptionRequest "id-it-subscriptionRequest" +#define NID_id_it_subscriptionRequest 305 +#define OBJ_id_it_subscriptionRequest OBJ_id_it,8L + +#define SN_id_it_subscriptionResponse "id-it-subscriptionResponse" +#define NID_id_it_subscriptionResponse 306 +#define OBJ_id_it_subscriptionResponse OBJ_id_it,9L + +#define SN_id_it_keyPairParamReq "id-it-keyPairParamReq" +#define NID_id_it_keyPairParamReq 307 +#define OBJ_id_it_keyPairParamReq OBJ_id_it,10L + +#define SN_id_it_keyPairParamRep "id-it-keyPairParamRep" +#define NID_id_it_keyPairParamRep 308 +#define OBJ_id_it_keyPairParamRep OBJ_id_it,11L + +#define SN_id_it_revPassphrase "id-it-revPassphrase" +#define NID_id_it_revPassphrase 309 +#define OBJ_id_it_revPassphrase OBJ_id_it,12L + +#define SN_id_it_implicitConfirm "id-it-implicitConfirm" +#define NID_id_it_implicitConfirm 310 +#define OBJ_id_it_implicitConfirm OBJ_id_it,13L + +#define SN_id_it_confirmWaitTime "id-it-confirmWaitTime" +#define NID_id_it_confirmWaitTime 311 +#define OBJ_id_it_confirmWaitTime OBJ_id_it,14L + +#define SN_id_it_origPKIMessage "id-it-origPKIMessage" +#define NID_id_it_origPKIMessage 312 +#define OBJ_id_it_origPKIMessage OBJ_id_it,15L + +#define SN_id_it_suppLangTags "id-it-suppLangTags" +#define NID_id_it_suppLangTags 784 +#define OBJ_id_it_suppLangTags OBJ_id_it,16L + +#define SN_id_regCtrl "id-regCtrl" +#define NID_id_regCtrl 313 +#define OBJ_id_regCtrl OBJ_id_pkip,1L + +#define SN_id_regInfo "id-regInfo" +#define NID_id_regInfo 314 +#define OBJ_id_regInfo OBJ_id_pkip,2L + +#define SN_id_regCtrl_regToken "id-regCtrl-regToken" +#define NID_id_regCtrl_regToken 315 +#define OBJ_id_regCtrl_regToken OBJ_id_regCtrl,1L + +#define SN_id_regCtrl_authenticator "id-regCtrl-authenticator" +#define NID_id_regCtrl_authenticator 316 +#define OBJ_id_regCtrl_authenticator OBJ_id_regCtrl,2L + +#define SN_id_regCtrl_pkiPublicationInfo "id-regCtrl-pkiPublicationInfo" +#define NID_id_regCtrl_pkiPublicationInfo 317 +#define OBJ_id_regCtrl_pkiPublicationInfo OBJ_id_regCtrl,3L + +#define SN_id_regCtrl_pkiArchiveOptions "id-regCtrl-pkiArchiveOptions" +#define NID_id_regCtrl_pkiArchiveOptions 318 +#define OBJ_id_regCtrl_pkiArchiveOptions OBJ_id_regCtrl,4L + +#define SN_id_regCtrl_oldCertID "id-regCtrl-oldCertID" +#define NID_id_regCtrl_oldCertID 319 +#define OBJ_id_regCtrl_oldCertID OBJ_id_regCtrl,5L + +#define SN_id_regCtrl_protocolEncrKey "id-regCtrl-protocolEncrKey" +#define NID_id_regCtrl_protocolEncrKey 320 +#define OBJ_id_regCtrl_protocolEncrKey OBJ_id_regCtrl,6L + +#define SN_id_regInfo_utf8Pairs "id-regInfo-utf8Pairs" +#define NID_id_regInfo_utf8Pairs 321 +#define OBJ_id_regInfo_utf8Pairs OBJ_id_regInfo,1L + +#define SN_id_regInfo_certReq "id-regInfo-certReq" +#define NID_id_regInfo_certReq 322 +#define OBJ_id_regInfo_certReq OBJ_id_regInfo,2L + +#define SN_id_alg_des40 "id-alg-des40" +#define NID_id_alg_des40 323 +#define OBJ_id_alg_des40 OBJ_id_alg,1L + +#define SN_id_alg_noSignature "id-alg-noSignature" +#define NID_id_alg_noSignature 324 +#define OBJ_id_alg_noSignature OBJ_id_alg,2L + +#define SN_id_alg_dh_sig_hmac_sha1 "id-alg-dh-sig-hmac-sha1" +#define NID_id_alg_dh_sig_hmac_sha1 325 +#define OBJ_id_alg_dh_sig_hmac_sha1 OBJ_id_alg,3L + +#define SN_id_alg_dh_pop "id-alg-dh-pop" +#define NID_id_alg_dh_pop 326 +#define OBJ_id_alg_dh_pop OBJ_id_alg,4L + +#define SN_id_cmc_statusInfo "id-cmc-statusInfo" +#define NID_id_cmc_statusInfo 327 +#define OBJ_id_cmc_statusInfo OBJ_id_cmc,1L + +#define SN_id_cmc_identification "id-cmc-identification" +#define NID_id_cmc_identification 328 +#define OBJ_id_cmc_identification OBJ_id_cmc,2L + +#define SN_id_cmc_identityProof "id-cmc-identityProof" +#define NID_id_cmc_identityProof 329 +#define OBJ_id_cmc_identityProof OBJ_id_cmc,3L + +#define SN_id_cmc_dataReturn "id-cmc-dataReturn" +#define NID_id_cmc_dataReturn 330 +#define OBJ_id_cmc_dataReturn OBJ_id_cmc,4L + +#define SN_id_cmc_transactionId "id-cmc-transactionId" +#define NID_id_cmc_transactionId 331 +#define OBJ_id_cmc_transactionId OBJ_id_cmc,5L + +#define SN_id_cmc_senderNonce "id-cmc-senderNonce" +#define NID_id_cmc_senderNonce 332 +#define OBJ_id_cmc_senderNonce OBJ_id_cmc,6L + +#define SN_id_cmc_recipientNonce "id-cmc-recipientNonce" +#define NID_id_cmc_recipientNonce 333 +#define OBJ_id_cmc_recipientNonce OBJ_id_cmc,7L + +#define SN_id_cmc_addExtensions "id-cmc-addExtensions" +#define NID_id_cmc_addExtensions 334 +#define OBJ_id_cmc_addExtensions OBJ_id_cmc,8L + +#define SN_id_cmc_encryptedPOP "id-cmc-encryptedPOP" +#define NID_id_cmc_encryptedPOP 335 +#define OBJ_id_cmc_encryptedPOP OBJ_id_cmc,9L + +#define SN_id_cmc_decryptedPOP "id-cmc-decryptedPOP" +#define NID_id_cmc_decryptedPOP 336 +#define OBJ_id_cmc_decryptedPOP OBJ_id_cmc,10L + +#define SN_id_cmc_lraPOPWitness "id-cmc-lraPOPWitness" +#define NID_id_cmc_lraPOPWitness 337 +#define OBJ_id_cmc_lraPOPWitness OBJ_id_cmc,11L + +#define SN_id_cmc_getCert "id-cmc-getCert" +#define NID_id_cmc_getCert 338 +#define OBJ_id_cmc_getCert OBJ_id_cmc,15L + +#define SN_id_cmc_getCRL "id-cmc-getCRL" +#define NID_id_cmc_getCRL 339 +#define OBJ_id_cmc_getCRL OBJ_id_cmc,16L + +#define SN_id_cmc_revokeRequest "id-cmc-revokeRequest" +#define NID_id_cmc_revokeRequest 340 +#define OBJ_id_cmc_revokeRequest OBJ_id_cmc,17L + +#define SN_id_cmc_regInfo "id-cmc-regInfo" +#define NID_id_cmc_regInfo 341 +#define OBJ_id_cmc_regInfo OBJ_id_cmc,18L + +#define SN_id_cmc_responseInfo "id-cmc-responseInfo" +#define NID_id_cmc_responseInfo 342 +#define OBJ_id_cmc_responseInfo OBJ_id_cmc,19L + +#define SN_id_cmc_queryPending "id-cmc-queryPending" +#define NID_id_cmc_queryPending 343 +#define OBJ_id_cmc_queryPending OBJ_id_cmc,21L + +#define SN_id_cmc_popLinkRandom "id-cmc-popLinkRandom" +#define NID_id_cmc_popLinkRandom 344 +#define OBJ_id_cmc_popLinkRandom OBJ_id_cmc,22L + +#define SN_id_cmc_popLinkWitness "id-cmc-popLinkWitness" +#define NID_id_cmc_popLinkWitness 345 +#define OBJ_id_cmc_popLinkWitness OBJ_id_cmc,23L + +#define SN_id_cmc_confirmCertAcceptance "id-cmc-confirmCertAcceptance" +#define NID_id_cmc_confirmCertAcceptance 346 +#define OBJ_id_cmc_confirmCertAcceptance OBJ_id_cmc,24L + +#define SN_id_on_personalData "id-on-personalData" +#define NID_id_on_personalData 347 +#define OBJ_id_on_personalData OBJ_id_on,1L + +#define SN_id_on_permanentIdentifier "id-on-permanentIdentifier" +#define LN_id_on_permanentIdentifier "Permanent Identifier" +#define NID_id_on_permanentIdentifier 858 +#define OBJ_id_on_permanentIdentifier OBJ_id_on,3L + +#define SN_id_pda_dateOfBirth "id-pda-dateOfBirth" +#define NID_id_pda_dateOfBirth 348 +#define OBJ_id_pda_dateOfBirth OBJ_id_pda,1L + +#define SN_id_pda_placeOfBirth "id-pda-placeOfBirth" +#define NID_id_pda_placeOfBirth 349 +#define OBJ_id_pda_placeOfBirth OBJ_id_pda,2L + +#define SN_id_pda_gender "id-pda-gender" +#define NID_id_pda_gender 351 +#define OBJ_id_pda_gender OBJ_id_pda,3L + +#define SN_id_pda_countryOfCitizenship "id-pda-countryOfCitizenship" +#define NID_id_pda_countryOfCitizenship 352 +#define OBJ_id_pda_countryOfCitizenship OBJ_id_pda,4L + +#define SN_id_pda_countryOfResidence "id-pda-countryOfResidence" +#define NID_id_pda_countryOfResidence 353 +#define OBJ_id_pda_countryOfResidence OBJ_id_pda,5L + +#define SN_id_aca_authenticationInfo "id-aca-authenticationInfo" +#define NID_id_aca_authenticationInfo 354 +#define OBJ_id_aca_authenticationInfo OBJ_id_aca,1L + +#define SN_id_aca_accessIdentity "id-aca-accessIdentity" +#define NID_id_aca_accessIdentity 355 +#define OBJ_id_aca_accessIdentity OBJ_id_aca,2L + +#define SN_id_aca_chargingIdentity "id-aca-chargingIdentity" +#define NID_id_aca_chargingIdentity 356 +#define OBJ_id_aca_chargingIdentity OBJ_id_aca,3L + +#define SN_id_aca_group "id-aca-group" +#define NID_id_aca_group 357 +#define OBJ_id_aca_group OBJ_id_aca,4L + +#define SN_id_aca_role "id-aca-role" +#define NID_id_aca_role 358 +#define OBJ_id_aca_role OBJ_id_aca,5L + +#define SN_id_aca_encAttrs "id-aca-encAttrs" +#define NID_id_aca_encAttrs 399 +#define OBJ_id_aca_encAttrs OBJ_id_aca,6L + +#define SN_id_qcs_pkixQCSyntax_v1 "id-qcs-pkixQCSyntax-v1" +#define NID_id_qcs_pkixQCSyntax_v1 359 +#define OBJ_id_qcs_pkixQCSyntax_v1 OBJ_id_qcs,1L + +#define SN_id_cct_crs "id-cct-crs" +#define NID_id_cct_crs 360 +#define OBJ_id_cct_crs OBJ_id_cct,1L + +#define SN_id_cct_PKIData "id-cct-PKIData" +#define NID_id_cct_PKIData 361 +#define OBJ_id_cct_PKIData OBJ_id_cct,2L + +#define SN_id_cct_PKIResponse "id-cct-PKIResponse" +#define NID_id_cct_PKIResponse 362 +#define OBJ_id_cct_PKIResponse OBJ_id_cct,3L + +#define SN_id_ppl_anyLanguage "id-ppl-anyLanguage" +#define LN_id_ppl_anyLanguage "Any language" +#define NID_id_ppl_anyLanguage 664 +#define OBJ_id_ppl_anyLanguage OBJ_id_ppl,0L + +#define SN_id_ppl_inheritAll "id-ppl-inheritAll" +#define LN_id_ppl_inheritAll "Inherit all" +#define NID_id_ppl_inheritAll 665 +#define OBJ_id_ppl_inheritAll OBJ_id_ppl,1L + +#define SN_Independent "id-ppl-independent" +#define LN_Independent "Independent" +#define NID_Independent 667 +#define OBJ_Independent OBJ_id_ppl,2L + +#define SN_ad_OCSP "OCSP" +#define LN_ad_OCSP "OCSP" +#define NID_ad_OCSP 178 +#define OBJ_ad_OCSP OBJ_id_ad,1L + +#define SN_ad_ca_issuers "caIssuers" +#define LN_ad_ca_issuers "CA Issuers" +#define NID_ad_ca_issuers 179 +#define OBJ_ad_ca_issuers OBJ_id_ad,2L + +#define SN_ad_timeStamping "ad_timestamping" +#define LN_ad_timeStamping "AD Time Stamping" +#define NID_ad_timeStamping 363 +#define OBJ_ad_timeStamping OBJ_id_ad,3L + +#define SN_ad_dvcs "AD_DVCS" +#define LN_ad_dvcs "ad dvcs" +#define NID_ad_dvcs 364 +#define OBJ_ad_dvcs OBJ_id_ad,4L + +#define SN_caRepository "caRepository" +#define LN_caRepository "CA Repository" +#define NID_caRepository 785 +#define OBJ_caRepository OBJ_id_ad,5L + +#define OBJ_id_pkix_OCSP OBJ_ad_OCSP + +#define SN_id_pkix_OCSP_basic "basicOCSPResponse" +#define LN_id_pkix_OCSP_basic "Basic OCSP Response" +#define NID_id_pkix_OCSP_basic 365 +#define OBJ_id_pkix_OCSP_basic OBJ_id_pkix_OCSP,1L + +#define SN_id_pkix_OCSP_Nonce "Nonce" +#define LN_id_pkix_OCSP_Nonce "OCSP Nonce" +#define NID_id_pkix_OCSP_Nonce 366 +#define OBJ_id_pkix_OCSP_Nonce OBJ_id_pkix_OCSP,2L + +#define SN_id_pkix_OCSP_CrlID "CrlID" +#define LN_id_pkix_OCSP_CrlID "OCSP CRL ID" +#define NID_id_pkix_OCSP_CrlID 367 +#define OBJ_id_pkix_OCSP_CrlID OBJ_id_pkix_OCSP,3L + +#define SN_id_pkix_OCSP_acceptableResponses "acceptableResponses" +#define LN_id_pkix_OCSP_acceptableResponses "Acceptable OCSP Responses" +#define NID_id_pkix_OCSP_acceptableResponses 368 +#define OBJ_id_pkix_OCSP_acceptableResponses OBJ_id_pkix_OCSP,4L + +#define SN_id_pkix_OCSP_noCheck "noCheck" +#define LN_id_pkix_OCSP_noCheck "OCSP No Check" +#define NID_id_pkix_OCSP_noCheck 369 +#define OBJ_id_pkix_OCSP_noCheck OBJ_id_pkix_OCSP,5L + +#define SN_id_pkix_OCSP_archiveCutoff "archiveCutoff" +#define LN_id_pkix_OCSP_archiveCutoff "OCSP Archive Cutoff" +#define NID_id_pkix_OCSP_archiveCutoff 370 +#define OBJ_id_pkix_OCSP_archiveCutoff OBJ_id_pkix_OCSP,6L + +#define SN_id_pkix_OCSP_serviceLocator "serviceLocator" +#define LN_id_pkix_OCSP_serviceLocator "OCSP Service Locator" +#define NID_id_pkix_OCSP_serviceLocator 371 +#define OBJ_id_pkix_OCSP_serviceLocator OBJ_id_pkix_OCSP,7L + +#define SN_id_pkix_OCSP_extendedStatus "extendedStatus" +#define LN_id_pkix_OCSP_extendedStatus "Extended OCSP Status" +#define NID_id_pkix_OCSP_extendedStatus 372 +#define OBJ_id_pkix_OCSP_extendedStatus OBJ_id_pkix_OCSP,8L + +#define SN_id_pkix_OCSP_valid "valid" +#define NID_id_pkix_OCSP_valid 373 +#define OBJ_id_pkix_OCSP_valid OBJ_id_pkix_OCSP,9L + +#define SN_id_pkix_OCSP_path "path" +#define NID_id_pkix_OCSP_path 374 +#define OBJ_id_pkix_OCSP_path OBJ_id_pkix_OCSP,10L + +#define SN_id_pkix_OCSP_trustRoot "trustRoot" +#define LN_id_pkix_OCSP_trustRoot "Trust Root" +#define NID_id_pkix_OCSP_trustRoot 375 +#define OBJ_id_pkix_OCSP_trustRoot OBJ_id_pkix_OCSP,11L + +#define SN_algorithm "algorithm" +#define LN_algorithm "algorithm" +#define NID_algorithm 376 +#define OBJ_algorithm 1L,3L,14L,3L,2L + +#define SN_md5WithRSA "RSA-NP-MD5" +#define LN_md5WithRSA "md5WithRSA" +#define NID_md5WithRSA 104 +#define OBJ_md5WithRSA OBJ_algorithm,3L + +#define SN_des_ecb "DES-ECB" +#define LN_des_ecb "des-ecb" +#define NID_des_ecb 29 +#define OBJ_des_ecb OBJ_algorithm,6L + +#define SN_des_cbc "DES-CBC" +#define LN_des_cbc "des-cbc" +#define NID_des_cbc 31 +#define OBJ_des_cbc OBJ_algorithm,7L + +#define SN_des_ofb64 "DES-OFB" +#define LN_des_ofb64 "des-ofb" +#define NID_des_ofb64 45 +#define OBJ_des_ofb64 OBJ_algorithm,8L + +#define SN_des_cfb64 "DES-CFB" +#define LN_des_cfb64 "des-cfb" +#define NID_des_cfb64 30 +#define OBJ_des_cfb64 OBJ_algorithm,9L + +#define SN_rsaSignature "rsaSignature" +#define NID_rsaSignature 377 +#define OBJ_rsaSignature OBJ_algorithm,11L + +#define SN_dsa_2 "DSA-old" +#define LN_dsa_2 "dsaEncryption-old" +#define NID_dsa_2 67 +#define OBJ_dsa_2 OBJ_algorithm,12L + +#define SN_dsaWithSHA "DSA-SHA" +#define LN_dsaWithSHA "dsaWithSHA" +#define NID_dsaWithSHA 66 +#define OBJ_dsaWithSHA OBJ_algorithm,13L + +#define SN_shaWithRSAEncryption "RSA-SHA" +#define LN_shaWithRSAEncryption "shaWithRSAEncryption" +#define NID_shaWithRSAEncryption 42 +#define OBJ_shaWithRSAEncryption OBJ_algorithm,15L + +#define SN_des_ede_ecb "DES-EDE" +#define LN_des_ede_ecb "des-ede" +#define NID_des_ede_ecb 32 +#define OBJ_des_ede_ecb OBJ_algorithm,17L + +#define SN_des_ede3_ecb "DES-EDE3" +#define LN_des_ede3_ecb "des-ede3" +#define NID_des_ede3_ecb 33 + +#define SN_des_ede_cbc "DES-EDE-CBC" +#define LN_des_ede_cbc "des-ede-cbc" +#define NID_des_ede_cbc 43 + +#define SN_des_ede_cfb64 "DES-EDE-CFB" +#define LN_des_ede_cfb64 "des-ede-cfb" +#define NID_des_ede_cfb64 60 + +#define SN_des_ede3_cfb64 "DES-EDE3-CFB" +#define LN_des_ede3_cfb64 "des-ede3-cfb" +#define NID_des_ede3_cfb64 61 + +#define SN_des_ede_ofb64 "DES-EDE-OFB" +#define LN_des_ede_ofb64 "des-ede-ofb" +#define NID_des_ede_ofb64 62 + +#define SN_des_ede3_ofb64 "DES-EDE3-OFB" +#define LN_des_ede3_ofb64 "des-ede3-ofb" +#define NID_des_ede3_ofb64 63 + +#define SN_desx_cbc "DESX-CBC" +#define LN_desx_cbc "desx-cbc" +#define NID_desx_cbc 80 + +#define SN_sha "SHA" +#define LN_sha "sha" +#define NID_sha 41 +#define OBJ_sha OBJ_algorithm,18L + +#define SN_sha1 "SHA1" +#define LN_sha1 "sha1" +#define NID_sha1 64 +#define OBJ_sha1 OBJ_algorithm,26L + +#define SN_dsaWithSHA1_2 "DSA-SHA1-old" +#define LN_dsaWithSHA1_2 "dsaWithSHA1-old" +#define NID_dsaWithSHA1_2 70 +#define OBJ_dsaWithSHA1_2 OBJ_algorithm,27L + +#define SN_sha1WithRSA "RSA-SHA1-2" +#define LN_sha1WithRSA "sha1WithRSA" +#define NID_sha1WithRSA 115 +#define OBJ_sha1WithRSA OBJ_algorithm,29L + +#define SN_ripemd160 "RIPEMD160" +#define LN_ripemd160 "ripemd160" +#define NID_ripemd160 117 +#define OBJ_ripemd160 1L,3L,36L,3L,2L,1L + +#define SN_ripemd160WithRSA "RSA-RIPEMD160" +#define LN_ripemd160WithRSA "ripemd160WithRSA" +#define NID_ripemd160WithRSA 119 +#define OBJ_ripemd160WithRSA 1L,3L,36L,3L,3L,1L,2L + +#define SN_blake2b512 "BLAKE2b512" +#define LN_blake2b512 "blake2b512" +#define NID_blake2b512 1056 +#define OBJ_blake2b512 1L,3L,6L,1L,4L,1L,1722L,12L,2L,1L,16L + +#define SN_blake2s256 "BLAKE2s256" +#define LN_blake2s256 "blake2s256" +#define NID_blake2s256 1057 +#define OBJ_blake2s256 1L,3L,6L,1L,4L,1L,1722L,12L,2L,2L,8L + +#define SN_sxnet "SXNetID" +#define LN_sxnet "Strong Extranet ID" +#define NID_sxnet 143 +#define OBJ_sxnet 1L,3L,101L,1L,4L,1L + +#define SN_X500 "X500" +#define LN_X500 "directory services (X.500)" +#define NID_X500 11 +#define OBJ_X500 2L,5L + +#define SN_X509 "X509" +#define NID_X509 12 +#define OBJ_X509 OBJ_X500,4L + +#define SN_commonName "CN" +#define LN_commonName "commonName" +#define NID_commonName 13 +#define OBJ_commonName OBJ_X509,3L + +#define SN_surname "SN" +#define LN_surname "surname" +#define NID_surname 100 +#define OBJ_surname OBJ_X509,4L + +#define LN_serialNumber "serialNumber" +#define NID_serialNumber 105 +#define OBJ_serialNumber OBJ_X509,5L + +#define SN_countryName "C" +#define LN_countryName "countryName" +#define NID_countryName 14 +#define OBJ_countryName OBJ_X509,6L + +#define SN_localityName "L" +#define LN_localityName "localityName" +#define NID_localityName 15 +#define OBJ_localityName OBJ_X509,7L + +#define SN_stateOrProvinceName "ST" +#define LN_stateOrProvinceName "stateOrProvinceName" +#define NID_stateOrProvinceName 16 +#define OBJ_stateOrProvinceName OBJ_X509,8L + +#define SN_streetAddress "street" +#define LN_streetAddress "streetAddress" +#define NID_streetAddress 660 +#define OBJ_streetAddress OBJ_X509,9L + +#define SN_organizationName "O" +#define LN_organizationName "organizationName" +#define NID_organizationName 17 +#define OBJ_organizationName OBJ_X509,10L + +#define SN_organizationalUnitName "OU" +#define LN_organizationalUnitName "organizationalUnitName" +#define NID_organizationalUnitName 18 +#define OBJ_organizationalUnitName OBJ_X509,11L + +#define SN_title "title" +#define LN_title "title" +#define NID_title 106 +#define OBJ_title OBJ_X509,12L + +#define LN_description "description" +#define NID_description 107 +#define OBJ_description OBJ_X509,13L + +#define LN_searchGuide "searchGuide" +#define NID_searchGuide 859 +#define OBJ_searchGuide OBJ_X509,14L + +#define LN_businessCategory "businessCategory" +#define NID_businessCategory 860 +#define OBJ_businessCategory OBJ_X509,15L + +#define LN_postalAddress "postalAddress" +#define NID_postalAddress 861 +#define OBJ_postalAddress OBJ_X509,16L + +#define LN_postalCode "postalCode" +#define NID_postalCode 661 +#define OBJ_postalCode OBJ_X509,17L + +#define LN_postOfficeBox "postOfficeBox" +#define NID_postOfficeBox 862 +#define OBJ_postOfficeBox OBJ_X509,18L + +#define LN_physicalDeliveryOfficeName "physicalDeliveryOfficeName" +#define NID_physicalDeliveryOfficeName 863 +#define OBJ_physicalDeliveryOfficeName OBJ_X509,19L + +#define LN_telephoneNumber "telephoneNumber" +#define NID_telephoneNumber 864 +#define OBJ_telephoneNumber OBJ_X509,20L + +#define LN_telexNumber "telexNumber" +#define NID_telexNumber 865 +#define OBJ_telexNumber OBJ_X509,21L + +#define LN_teletexTerminalIdentifier "teletexTerminalIdentifier" +#define NID_teletexTerminalIdentifier 866 +#define OBJ_teletexTerminalIdentifier OBJ_X509,22L + +#define LN_facsimileTelephoneNumber "facsimileTelephoneNumber" +#define NID_facsimileTelephoneNumber 867 +#define OBJ_facsimileTelephoneNumber OBJ_X509,23L + +#define LN_x121Address "x121Address" +#define NID_x121Address 868 +#define OBJ_x121Address OBJ_X509,24L + +#define LN_internationaliSDNNumber "internationaliSDNNumber" +#define NID_internationaliSDNNumber 869 +#define OBJ_internationaliSDNNumber OBJ_X509,25L + +#define LN_registeredAddress "registeredAddress" +#define NID_registeredAddress 870 +#define OBJ_registeredAddress OBJ_X509,26L + +#define LN_destinationIndicator "destinationIndicator" +#define NID_destinationIndicator 871 +#define OBJ_destinationIndicator OBJ_X509,27L + +#define LN_preferredDeliveryMethod "preferredDeliveryMethod" +#define NID_preferredDeliveryMethod 872 +#define OBJ_preferredDeliveryMethod OBJ_X509,28L + +#define LN_presentationAddress "presentationAddress" +#define NID_presentationAddress 873 +#define OBJ_presentationAddress OBJ_X509,29L + +#define LN_supportedApplicationContext "supportedApplicationContext" +#define NID_supportedApplicationContext 874 +#define OBJ_supportedApplicationContext OBJ_X509,30L + +#define SN_member "member" +#define NID_member 875 +#define OBJ_member OBJ_X509,31L + +#define SN_owner "owner" +#define NID_owner 876 +#define OBJ_owner OBJ_X509,32L + +#define LN_roleOccupant "roleOccupant" +#define NID_roleOccupant 877 +#define OBJ_roleOccupant OBJ_X509,33L + +#define SN_seeAlso "seeAlso" +#define NID_seeAlso 878 +#define OBJ_seeAlso OBJ_X509,34L + +#define LN_userPassword "userPassword" +#define NID_userPassword 879 +#define OBJ_userPassword OBJ_X509,35L + +#define LN_userCertificate "userCertificate" +#define NID_userCertificate 880 +#define OBJ_userCertificate OBJ_X509,36L + +#define LN_cACertificate "cACertificate" +#define NID_cACertificate 881 +#define OBJ_cACertificate OBJ_X509,37L + +#define LN_authorityRevocationList "authorityRevocationList" +#define NID_authorityRevocationList 882 +#define OBJ_authorityRevocationList OBJ_X509,38L + +#define LN_certificateRevocationList "certificateRevocationList" +#define NID_certificateRevocationList 883 +#define OBJ_certificateRevocationList OBJ_X509,39L + +#define LN_crossCertificatePair "crossCertificatePair" +#define NID_crossCertificatePair 884 +#define OBJ_crossCertificatePair OBJ_X509,40L + +#define SN_name "name" +#define LN_name "name" +#define NID_name 173 +#define OBJ_name OBJ_X509,41L + +#define SN_givenName "GN" +#define LN_givenName "givenName" +#define NID_givenName 99 +#define OBJ_givenName OBJ_X509,42L + +#define SN_initials "initials" +#define LN_initials "initials" +#define NID_initials 101 +#define OBJ_initials OBJ_X509,43L + +#define LN_generationQualifier "generationQualifier" +#define NID_generationQualifier 509 +#define OBJ_generationQualifier OBJ_X509,44L + +#define LN_x500UniqueIdentifier "x500UniqueIdentifier" +#define NID_x500UniqueIdentifier 503 +#define OBJ_x500UniqueIdentifier OBJ_X509,45L + +#define SN_dnQualifier "dnQualifier" +#define LN_dnQualifier "dnQualifier" +#define NID_dnQualifier 174 +#define OBJ_dnQualifier OBJ_X509,46L + +#define LN_enhancedSearchGuide "enhancedSearchGuide" +#define NID_enhancedSearchGuide 885 +#define OBJ_enhancedSearchGuide OBJ_X509,47L + +#define LN_protocolInformation "protocolInformation" +#define NID_protocolInformation 886 +#define OBJ_protocolInformation OBJ_X509,48L + +#define LN_distinguishedName "distinguishedName" +#define NID_distinguishedName 887 +#define OBJ_distinguishedName OBJ_X509,49L + +#define LN_uniqueMember "uniqueMember" +#define NID_uniqueMember 888 +#define OBJ_uniqueMember OBJ_X509,50L + +#define LN_houseIdentifier "houseIdentifier" +#define NID_houseIdentifier 889 +#define OBJ_houseIdentifier OBJ_X509,51L + +#define LN_supportedAlgorithms "supportedAlgorithms" +#define NID_supportedAlgorithms 890 +#define OBJ_supportedAlgorithms OBJ_X509,52L + +#define LN_deltaRevocationList "deltaRevocationList" +#define NID_deltaRevocationList 891 +#define OBJ_deltaRevocationList OBJ_X509,53L + +#define SN_dmdName "dmdName" +#define NID_dmdName 892 +#define OBJ_dmdName OBJ_X509,54L + +#define LN_pseudonym "pseudonym" +#define NID_pseudonym 510 +#define OBJ_pseudonym OBJ_X509,65L + +#define SN_role "role" +#define LN_role "role" +#define NID_role 400 +#define OBJ_role OBJ_X509,72L + +#define LN_organizationIdentifier "organizationIdentifier" +#define NID_organizationIdentifier 1089 +#define OBJ_organizationIdentifier OBJ_X509,97L + +#define SN_countryCode3c "c3" +#define LN_countryCode3c "countryCode3c" +#define NID_countryCode3c 1090 +#define OBJ_countryCode3c OBJ_X509,98L + +#define SN_countryCode3n "n3" +#define LN_countryCode3n "countryCode3n" +#define NID_countryCode3n 1091 +#define OBJ_countryCode3n OBJ_X509,99L + +#define LN_dnsName "dnsName" +#define NID_dnsName 1092 +#define OBJ_dnsName OBJ_X509,100L + +#define SN_X500algorithms "X500algorithms" +#define LN_X500algorithms "directory services - algorithms" +#define NID_X500algorithms 378 +#define OBJ_X500algorithms OBJ_X500,8L + +#define SN_rsa "RSA" +#define LN_rsa "rsa" +#define NID_rsa 19 +#define OBJ_rsa OBJ_X500algorithms,1L,1L + +#define SN_mdc2WithRSA "RSA-MDC2" +#define LN_mdc2WithRSA "mdc2WithRSA" +#define NID_mdc2WithRSA 96 +#define OBJ_mdc2WithRSA OBJ_X500algorithms,3L,100L + +#define SN_mdc2 "MDC2" +#define LN_mdc2 "mdc2" +#define NID_mdc2 95 +#define OBJ_mdc2 OBJ_X500algorithms,3L,101L + +#define SN_id_ce "id-ce" +#define NID_id_ce 81 +#define OBJ_id_ce OBJ_X500,29L + +#define SN_subject_directory_attributes "subjectDirectoryAttributes" +#define LN_subject_directory_attributes "X509v3 Subject Directory Attributes" +#define NID_subject_directory_attributes 769 +#define OBJ_subject_directory_attributes OBJ_id_ce,9L + +#define SN_subject_key_identifier "subjectKeyIdentifier" +#define LN_subject_key_identifier "X509v3 Subject Key Identifier" +#define NID_subject_key_identifier 82 +#define OBJ_subject_key_identifier OBJ_id_ce,14L + +#define SN_key_usage "keyUsage" +#define LN_key_usage "X509v3 Key Usage" +#define NID_key_usage 83 +#define OBJ_key_usage OBJ_id_ce,15L + +#define SN_private_key_usage_period "privateKeyUsagePeriod" +#define LN_private_key_usage_period "X509v3 Private Key Usage Period" +#define NID_private_key_usage_period 84 +#define OBJ_private_key_usage_period OBJ_id_ce,16L + +#define SN_subject_alt_name "subjectAltName" +#define LN_subject_alt_name "X509v3 Subject Alternative Name" +#define NID_subject_alt_name 85 +#define OBJ_subject_alt_name OBJ_id_ce,17L + +#define SN_issuer_alt_name "issuerAltName" +#define LN_issuer_alt_name "X509v3 Issuer Alternative Name" +#define NID_issuer_alt_name 86 +#define OBJ_issuer_alt_name OBJ_id_ce,18L + +#define SN_basic_constraints "basicConstraints" +#define LN_basic_constraints "X509v3 Basic Constraints" +#define NID_basic_constraints 87 +#define OBJ_basic_constraints OBJ_id_ce,19L + +#define SN_crl_number "crlNumber" +#define LN_crl_number "X509v3 CRL Number" +#define NID_crl_number 88 +#define OBJ_crl_number OBJ_id_ce,20L + +#define SN_crl_reason "CRLReason" +#define LN_crl_reason "X509v3 CRL Reason Code" +#define NID_crl_reason 141 +#define OBJ_crl_reason OBJ_id_ce,21L + +#define SN_invalidity_date "invalidityDate" +#define LN_invalidity_date "Invalidity Date" +#define NID_invalidity_date 142 +#define OBJ_invalidity_date OBJ_id_ce,24L + +#define SN_delta_crl "deltaCRL" +#define LN_delta_crl "X509v3 Delta CRL Indicator" +#define NID_delta_crl 140 +#define OBJ_delta_crl OBJ_id_ce,27L + +#define SN_issuing_distribution_point "issuingDistributionPoint" +#define LN_issuing_distribution_point "X509v3 Issuing Distribution Point" +#define NID_issuing_distribution_point 770 +#define OBJ_issuing_distribution_point OBJ_id_ce,28L + +#define SN_certificate_issuer "certificateIssuer" +#define LN_certificate_issuer "X509v3 Certificate Issuer" +#define NID_certificate_issuer 771 +#define OBJ_certificate_issuer OBJ_id_ce,29L + +#define SN_name_constraints "nameConstraints" +#define LN_name_constraints "X509v3 Name Constraints" +#define NID_name_constraints 666 +#define OBJ_name_constraints OBJ_id_ce,30L + +#define SN_crl_distribution_points "crlDistributionPoints" +#define LN_crl_distribution_points "X509v3 CRL Distribution Points" +#define NID_crl_distribution_points 103 +#define OBJ_crl_distribution_points OBJ_id_ce,31L + +#define SN_certificate_policies "certificatePolicies" +#define LN_certificate_policies "X509v3 Certificate Policies" +#define NID_certificate_policies 89 +#define OBJ_certificate_policies OBJ_id_ce,32L + +#define SN_any_policy "anyPolicy" +#define LN_any_policy "X509v3 Any Policy" +#define NID_any_policy 746 +#define OBJ_any_policy OBJ_certificate_policies,0L + +#define SN_policy_mappings "policyMappings" +#define LN_policy_mappings "X509v3 Policy Mappings" +#define NID_policy_mappings 747 +#define OBJ_policy_mappings OBJ_id_ce,33L + +#define SN_authority_key_identifier "authorityKeyIdentifier" +#define LN_authority_key_identifier "X509v3 Authority Key Identifier" +#define NID_authority_key_identifier 90 +#define OBJ_authority_key_identifier OBJ_id_ce,35L + +#define SN_policy_constraints "policyConstraints" +#define LN_policy_constraints "X509v3 Policy Constraints" +#define NID_policy_constraints 401 +#define OBJ_policy_constraints OBJ_id_ce,36L + +#define SN_ext_key_usage "extendedKeyUsage" +#define LN_ext_key_usage "X509v3 Extended Key Usage" +#define NID_ext_key_usage 126 +#define OBJ_ext_key_usage OBJ_id_ce,37L + +#define SN_freshest_crl "freshestCRL" +#define LN_freshest_crl "X509v3 Freshest CRL" +#define NID_freshest_crl 857 +#define OBJ_freshest_crl OBJ_id_ce,46L + +#define SN_inhibit_any_policy "inhibitAnyPolicy" +#define LN_inhibit_any_policy "X509v3 Inhibit Any Policy" +#define NID_inhibit_any_policy 748 +#define OBJ_inhibit_any_policy OBJ_id_ce,54L + +#define SN_target_information "targetInformation" +#define LN_target_information "X509v3 AC Targeting" +#define NID_target_information 402 +#define OBJ_target_information OBJ_id_ce,55L + +#define SN_no_rev_avail "noRevAvail" +#define LN_no_rev_avail "X509v3 No Revocation Available" +#define NID_no_rev_avail 403 +#define OBJ_no_rev_avail OBJ_id_ce,56L + +#define SN_anyExtendedKeyUsage "anyExtendedKeyUsage" +#define LN_anyExtendedKeyUsage "Any Extended Key Usage" +#define NID_anyExtendedKeyUsage 910 +#define OBJ_anyExtendedKeyUsage OBJ_ext_key_usage,0L + +#define SN_netscape "Netscape" +#define LN_netscape "Netscape Communications Corp." +#define NID_netscape 57 +#define OBJ_netscape 2L,16L,840L,1L,113730L + +#define SN_netscape_cert_extension "nsCertExt" +#define LN_netscape_cert_extension "Netscape Certificate Extension" +#define NID_netscape_cert_extension 58 +#define OBJ_netscape_cert_extension OBJ_netscape,1L + +#define SN_netscape_data_type "nsDataType" +#define LN_netscape_data_type "Netscape Data Type" +#define NID_netscape_data_type 59 +#define OBJ_netscape_data_type OBJ_netscape,2L + +#define SN_netscape_cert_type "nsCertType" +#define LN_netscape_cert_type "Netscape Cert Type" +#define NID_netscape_cert_type 71 +#define OBJ_netscape_cert_type OBJ_netscape_cert_extension,1L + +#define SN_netscape_base_url "nsBaseUrl" +#define LN_netscape_base_url "Netscape Base Url" +#define NID_netscape_base_url 72 +#define OBJ_netscape_base_url OBJ_netscape_cert_extension,2L + +#define SN_netscape_revocation_url "nsRevocationUrl" +#define LN_netscape_revocation_url "Netscape Revocation Url" +#define NID_netscape_revocation_url 73 +#define OBJ_netscape_revocation_url OBJ_netscape_cert_extension,3L + +#define SN_netscape_ca_revocation_url "nsCaRevocationUrl" +#define LN_netscape_ca_revocation_url "Netscape CA Revocation Url" +#define NID_netscape_ca_revocation_url 74 +#define OBJ_netscape_ca_revocation_url OBJ_netscape_cert_extension,4L + +#define SN_netscape_renewal_url "nsRenewalUrl" +#define LN_netscape_renewal_url "Netscape Renewal Url" +#define NID_netscape_renewal_url 75 +#define OBJ_netscape_renewal_url OBJ_netscape_cert_extension,7L + +#define SN_netscape_ca_policy_url "nsCaPolicyUrl" +#define LN_netscape_ca_policy_url "Netscape CA Policy Url" +#define NID_netscape_ca_policy_url 76 +#define OBJ_netscape_ca_policy_url OBJ_netscape_cert_extension,8L + +#define SN_netscape_ssl_server_name "nsSslServerName" +#define LN_netscape_ssl_server_name "Netscape SSL Server Name" +#define NID_netscape_ssl_server_name 77 +#define OBJ_netscape_ssl_server_name OBJ_netscape_cert_extension,12L + +#define SN_netscape_comment "nsComment" +#define LN_netscape_comment "Netscape Comment" +#define NID_netscape_comment 78 +#define OBJ_netscape_comment OBJ_netscape_cert_extension,13L + +#define SN_netscape_cert_sequence "nsCertSequence" +#define LN_netscape_cert_sequence "Netscape Certificate Sequence" +#define NID_netscape_cert_sequence 79 +#define OBJ_netscape_cert_sequence OBJ_netscape_data_type,5L + +#define SN_ns_sgc "nsSGC" +#define LN_ns_sgc "Netscape Server Gated Crypto" +#define NID_ns_sgc 139 +#define OBJ_ns_sgc OBJ_netscape,4L,1L + +#define SN_org "ORG" +#define LN_org "org" +#define NID_org 379 +#define OBJ_org OBJ_iso,3L + +#define SN_dod "DOD" +#define LN_dod "dod" +#define NID_dod 380 +#define OBJ_dod OBJ_org,6L + +#define SN_iana "IANA" +#define LN_iana "iana" +#define NID_iana 381 +#define OBJ_iana OBJ_dod,1L + +#define OBJ_internet OBJ_iana + +#define SN_Directory "directory" +#define LN_Directory "Directory" +#define NID_Directory 382 +#define OBJ_Directory OBJ_internet,1L + +#define SN_Management "mgmt" +#define LN_Management "Management" +#define NID_Management 383 +#define OBJ_Management OBJ_internet,2L + +#define SN_Experimental "experimental" +#define LN_Experimental "Experimental" +#define NID_Experimental 384 +#define OBJ_Experimental OBJ_internet,3L + +#define SN_Private "private" +#define LN_Private "Private" +#define NID_Private 385 +#define OBJ_Private OBJ_internet,4L + +#define SN_Security "security" +#define LN_Security "Security" +#define NID_Security 386 +#define OBJ_Security OBJ_internet,5L + +#define SN_SNMPv2 "snmpv2" +#define LN_SNMPv2 "SNMPv2" +#define NID_SNMPv2 387 +#define OBJ_SNMPv2 OBJ_internet,6L + +#define LN_Mail "Mail" +#define NID_Mail 388 +#define OBJ_Mail OBJ_internet,7L + +#define SN_Enterprises "enterprises" +#define LN_Enterprises "Enterprises" +#define NID_Enterprises 389 +#define OBJ_Enterprises OBJ_Private,1L + +#define SN_dcObject "dcobject" +#define LN_dcObject "dcObject" +#define NID_dcObject 390 +#define OBJ_dcObject OBJ_Enterprises,1466L,344L + +#define SN_mime_mhs "mime-mhs" +#define LN_mime_mhs "MIME MHS" +#define NID_mime_mhs 504 +#define OBJ_mime_mhs OBJ_Mail,1L + +#define SN_mime_mhs_headings "mime-mhs-headings" +#define LN_mime_mhs_headings "mime-mhs-headings" +#define NID_mime_mhs_headings 505 +#define OBJ_mime_mhs_headings OBJ_mime_mhs,1L + +#define SN_mime_mhs_bodies "mime-mhs-bodies" +#define LN_mime_mhs_bodies "mime-mhs-bodies" +#define NID_mime_mhs_bodies 506 +#define OBJ_mime_mhs_bodies OBJ_mime_mhs,2L + +#define SN_id_hex_partial_message "id-hex-partial-message" +#define LN_id_hex_partial_message "id-hex-partial-message" +#define NID_id_hex_partial_message 507 +#define OBJ_id_hex_partial_message OBJ_mime_mhs_headings,1L + +#define SN_id_hex_multipart_message "id-hex-multipart-message" +#define LN_id_hex_multipart_message "id-hex-multipart-message" +#define NID_id_hex_multipart_message 508 +#define OBJ_id_hex_multipart_message OBJ_mime_mhs_headings,2L + +#define SN_zlib_compression "ZLIB" +#define LN_zlib_compression "zlib compression" +#define NID_zlib_compression 125 +#define OBJ_zlib_compression OBJ_id_smime_alg,8L + +#define OBJ_csor 2L,16L,840L,1L,101L,3L + +#define OBJ_nistAlgorithms OBJ_csor,4L + +#define OBJ_aes OBJ_nistAlgorithms,1L + +#define SN_aes_128_ecb "AES-128-ECB" +#define LN_aes_128_ecb "aes-128-ecb" +#define NID_aes_128_ecb 418 +#define OBJ_aes_128_ecb OBJ_aes,1L + +#define SN_aes_128_cbc "AES-128-CBC" +#define LN_aes_128_cbc "aes-128-cbc" +#define NID_aes_128_cbc 419 +#define OBJ_aes_128_cbc OBJ_aes,2L + +#define SN_aes_128_ofb128 "AES-128-OFB" +#define LN_aes_128_ofb128 "aes-128-ofb" +#define NID_aes_128_ofb128 420 +#define OBJ_aes_128_ofb128 OBJ_aes,3L + +#define SN_aes_128_cfb128 "AES-128-CFB" +#define LN_aes_128_cfb128 "aes-128-cfb" +#define NID_aes_128_cfb128 421 +#define OBJ_aes_128_cfb128 OBJ_aes,4L + +#define SN_id_aes128_wrap "id-aes128-wrap" +#define NID_id_aes128_wrap 788 +#define OBJ_id_aes128_wrap OBJ_aes,5L + +#define SN_aes_128_gcm "id-aes128-GCM" +#define LN_aes_128_gcm "aes-128-gcm" +#define NID_aes_128_gcm 895 +#define OBJ_aes_128_gcm OBJ_aes,6L + +#define SN_aes_128_ccm "id-aes128-CCM" +#define LN_aes_128_ccm "aes-128-ccm" +#define NID_aes_128_ccm 896 +#define OBJ_aes_128_ccm OBJ_aes,7L + +#define SN_id_aes128_wrap_pad "id-aes128-wrap-pad" +#define NID_id_aes128_wrap_pad 897 +#define OBJ_id_aes128_wrap_pad OBJ_aes,8L + +#define SN_aes_192_ecb "AES-192-ECB" +#define LN_aes_192_ecb "aes-192-ecb" +#define NID_aes_192_ecb 422 +#define OBJ_aes_192_ecb OBJ_aes,21L + +#define SN_aes_192_cbc "AES-192-CBC" +#define LN_aes_192_cbc "aes-192-cbc" +#define NID_aes_192_cbc 423 +#define OBJ_aes_192_cbc OBJ_aes,22L + +#define SN_aes_192_ofb128 "AES-192-OFB" +#define LN_aes_192_ofb128 "aes-192-ofb" +#define NID_aes_192_ofb128 424 +#define OBJ_aes_192_ofb128 OBJ_aes,23L + +#define SN_aes_192_cfb128 "AES-192-CFB" +#define LN_aes_192_cfb128 "aes-192-cfb" +#define NID_aes_192_cfb128 425 +#define OBJ_aes_192_cfb128 OBJ_aes,24L + +#define SN_id_aes192_wrap "id-aes192-wrap" +#define NID_id_aes192_wrap 789 +#define OBJ_id_aes192_wrap OBJ_aes,25L + +#define SN_aes_192_gcm "id-aes192-GCM" +#define LN_aes_192_gcm "aes-192-gcm" +#define NID_aes_192_gcm 898 +#define OBJ_aes_192_gcm OBJ_aes,26L + +#define SN_aes_192_ccm "id-aes192-CCM" +#define LN_aes_192_ccm "aes-192-ccm" +#define NID_aes_192_ccm 899 +#define OBJ_aes_192_ccm OBJ_aes,27L + +#define SN_id_aes192_wrap_pad "id-aes192-wrap-pad" +#define NID_id_aes192_wrap_pad 900 +#define OBJ_id_aes192_wrap_pad OBJ_aes,28L + +#define SN_aes_256_ecb "AES-256-ECB" +#define LN_aes_256_ecb "aes-256-ecb" +#define NID_aes_256_ecb 426 +#define OBJ_aes_256_ecb OBJ_aes,41L + +#define SN_aes_256_cbc "AES-256-CBC" +#define LN_aes_256_cbc "aes-256-cbc" +#define NID_aes_256_cbc 427 +#define OBJ_aes_256_cbc OBJ_aes,42L + +#define SN_aes_256_ofb128 "AES-256-OFB" +#define LN_aes_256_ofb128 "aes-256-ofb" +#define NID_aes_256_ofb128 428 +#define OBJ_aes_256_ofb128 OBJ_aes,43L + +#define SN_aes_256_cfb128 "AES-256-CFB" +#define LN_aes_256_cfb128 "aes-256-cfb" +#define NID_aes_256_cfb128 429 +#define OBJ_aes_256_cfb128 OBJ_aes,44L + +#define SN_id_aes256_wrap "id-aes256-wrap" +#define NID_id_aes256_wrap 790 +#define OBJ_id_aes256_wrap OBJ_aes,45L + +#define SN_aes_256_gcm "id-aes256-GCM" +#define LN_aes_256_gcm "aes-256-gcm" +#define NID_aes_256_gcm 901 +#define OBJ_aes_256_gcm OBJ_aes,46L + +#define SN_aes_256_ccm "id-aes256-CCM" +#define LN_aes_256_ccm "aes-256-ccm" +#define NID_aes_256_ccm 902 +#define OBJ_aes_256_ccm OBJ_aes,47L + +#define SN_id_aes256_wrap_pad "id-aes256-wrap-pad" +#define NID_id_aes256_wrap_pad 903 +#define OBJ_id_aes256_wrap_pad OBJ_aes,48L + +#define SN_aes_128_xts "AES-128-XTS" +#define LN_aes_128_xts "aes-128-xts" +#define NID_aes_128_xts 913 +#define OBJ_aes_128_xts OBJ_ieee_siswg,0L,1L,1L + +#define SN_aes_256_xts "AES-256-XTS" +#define LN_aes_256_xts "aes-256-xts" +#define NID_aes_256_xts 914 +#define OBJ_aes_256_xts OBJ_ieee_siswg,0L,1L,2L + +#define SN_aes_128_cfb1 "AES-128-CFB1" +#define LN_aes_128_cfb1 "aes-128-cfb1" +#define NID_aes_128_cfb1 650 + +#define SN_aes_192_cfb1 "AES-192-CFB1" +#define LN_aes_192_cfb1 "aes-192-cfb1" +#define NID_aes_192_cfb1 651 + +#define SN_aes_256_cfb1 "AES-256-CFB1" +#define LN_aes_256_cfb1 "aes-256-cfb1" +#define NID_aes_256_cfb1 652 + +#define SN_aes_128_cfb8 "AES-128-CFB8" +#define LN_aes_128_cfb8 "aes-128-cfb8" +#define NID_aes_128_cfb8 653 + +#define SN_aes_192_cfb8 "AES-192-CFB8" +#define LN_aes_192_cfb8 "aes-192-cfb8" +#define NID_aes_192_cfb8 654 + +#define SN_aes_256_cfb8 "AES-256-CFB8" +#define LN_aes_256_cfb8 "aes-256-cfb8" +#define NID_aes_256_cfb8 655 + +#define SN_aes_128_ctr "AES-128-CTR" +#define LN_aes_128_ctr "aes-128-ctr" +#define NID_aes_128_ctr 904 + +#define SN_aes_192_ctr "AES-192-CTR" +#define LN_aes_192_ctr "aes-192-ctr" +#define NID_aes_192_ctr 905 + +#define SN_aes_256_ctr "AES-256-CTR" +#define LN_aes_256_ctr "aes-256-ctr" +#define NID_aes_256_ctr 906 + +#define SN_aes_128_ocb "AES-128-OCB" +#define LN_aes_128_ocb "aes-128-ocb" +#define NID_aes_128_ocb 958 + +#define SN_aes_192_ocb "AES-192-OCB" +#define LN_aes_192_ocb "aes-192-ocb" +#define NID_aes_192_ocb 959 + +#define SN_aes_256_ocb "AES-256-OCB" +#define LN_aes_256_ocb "aes-256-ocb" +#define NID_aes_256_ocb 960 + +#define SN_des_cfb1 "DES-CFB1" +#define LN_des_cfb1 "des-cfb1" +#define NID_des_cfb1 656 + +#define SN_des_cfb8 "DES-CFB8" +#define LN_des_cfb8 "des-cfb8" +#define NID_des_cfb8 657 + +#define SN_des_ede3_cfb1 "DES-EDE3-CFB1" +#define LN_des_ede3_cfb1 "des-ede3-cfb1" +#define NID_des_ede3_cfb1 658 + +#define SN_des_ede3_cfb8 "DES-EDE3-CFB8" +#define LN_des_ede3_cfb8 "des-ede3-cfb8" +#define NID_des_ede3_cfb8 659 + +#define OBJ_nist_hashalgs OBJ_nistAlgorithms,2L + +#define SN_sha256 "SHA256" +#define LN_sha256 "sha256" +#define NID_sha256 672 +#define OBJ_sha256 OBJ_nist_hashalgs,1L + +#define SN_sha384 "SHA384" +#define LN_sha384 "sha384" +#define NID_sha384 673 +#define OBJ_sha384 OBJ_nist_hashalgs,2L + +#define SN_sha512 "SHA512" +#define LN_sha512 "sha512" +#define NID_sha512 674 +#define OBJ_sha512 OBJ_nist_hashalgs,3L + +#define SN_sha224 "SHA224" +#define LN_sha224 "sha224" +#define NID_sha224 675 +#define OBJ_sha224 OBJ_nist_hashalgs,4L + +#define SN_sha512_224 "SHA512-224" +#define LN_sha512_224 "sha512-224" +#define NID_sha512_224 1094 +#define OBJ_sha512_224 OBJ_nist_hashalgs,5L + +#define SN_sha512_256 "SHA512-256" +#define LN_sha512_256 "sha512-256" +#define NID_sha512_256 1095 +#define OBJ_sha512_256 OBJ_nist_hashalgs,6L + +#define SN_sha3_224 "SHA3-224" +#define LN_sha3_224 "sha3-224" +#define NID_sha3_224 1096 +#define OBJ_sha3_224 OBJ_nist_hashalgs,7L + +#define SN_sha3_256 "SHA3-256" +#define LN_sha3_256 "sha3-256" +#define NID_sha3_256 1097 +#define OBJ_sha3_256 OBJ_nist_hashalgs,8L + +#define SN_sha3_384 "SHA3-384" +#define LN_sha3_384 "sha3-384" +#define NID_sha3_384 1098 +#define OBJ_sha3_384 OBJ_nist_hashalgs,9L + +#define SN_sha3_512 "SHA3-512" +#define LN_sha3_512 "sha3-512" +#define NID_sha3_512 1099 +#define OBJ_sha3_512 OBJ_nist_hashalgs,10L + +#define SN_shake128 "SHAKE128" +#define LN_shake128 "shake128" +#define NID_shake128 1100 +#define OBJ_shake128 OBJ_nist_hashalgs,11L + +#define SN_shake256 "SHAKE256" +#define LN_shake256 "shake256" +#define NID_shake256 1101 +#define OBJ_shake256 OBJ_nist_hashalgs,12L + +#define SN_hmac_sha3_224 "id-hmacWithSHA3-224" +#define LN_hmac_sha3_224 "hmac-sha3-224" +#define NID_hmac_sha3_224 1102 +#define OBJ_hmac_sha3_224 OBJ_nist_hashalgs,13L + +#define SN_hmac_sha3_256 "id-hmacWithSHA3-256" +#define LN_hmac_sha3_256 "hmac-sha3-256" +#define NID_hmac_sha3_256 1103 +#define OBJ_hmac_sha3_256 OBJ_nist_hashalgs,14L + +#define SN_hmac_sha3_384 "id-hmacWithSHA3-384" +#define LN_hmac_sha3_384 "hmac-sha3-384" +#define NID_hmac_sha3_384 1104 +#define OBJ_hmac_sha3_384 OBJ_nist_hashalgs,15L + +#define SN_hmac_sha3_512 "id-hmacWithSHA3-512" +#define LN_hmac_sha3_512 "hmac-sha3-512" +#define NID_hmac_sha3_512 1105 +#define OBJ_hmac_sha3_512 OBJ_nist_hashalgs,16L + +#define OBJ_dsa_with_sha2 OBJ_nistAlgorithms,3L + +#define SN_dsa_with_SHA224 "dsa_with_SHA224" +#define NID_dsa_with_SHA224 802 +#define OBJ_dsa_with_SHA224 OBJ_dsa_with_sha2,1L + +#define SN_dsa_with_SHA256 "dsa_with_SHA256" +#define NID_dsa_with_SHA256 803 +#define OBJ_dsa_with_SHA256 OBJ_dsa_with_sha2,2L + +#define OBJ_sigAlgs OBJ_nistAlgorithms,3L + +#define SN_dsa_with_SHA384 "id-dsa-with-sha384" +#define LN_dsa_with_SHA384 "dsa_with_SHA384" +#define NID_dsa_with_SHA384 1106 +#define OBJ_dsa_with_SHA384 OBJ_sigAlgs,3L + +#define SN_dsa_with_SHA512 "id-dsa-with-sha512" +#define LN_dsa_with_SHA512 "dsa_with_SHA512" +#define NID_dsa_with_SHA512 1107 +#define OBJ_dsa_with_SHA512 OBJ_sigAlgs,4L + +#define SN_dsa_with_SHA3_224 "id-dsa-with-sha3-224" +#define LN_dsa_with_SHA3_224 "dsa_with_SHA3-224" +#define NID_dsa_with_SHA3_224 1108 +#define OBJ_dsa_with_SHA3_224 OBJ_sigAlgs,5L + +#define SN_dsa_with_SHA3_256 "id-dsa-with-sha3-256" +#define LN_dsa_with_SHA3_256 "dsa_with_SHA3-256" +#define NID_dsa_with_SHA3_256 1109 +#define OBJ_dsa_with_SHA3_256 OBJ_sigAlgs,6L + +#define SN_dsa_with_SHA3_384 "id-dsa-with-sha3-384" +#define LN_dsa_with_SHA3_384 "dsa_with_SHA3-384" +#define NID_dsa_with_SHA3_384 1110 +#define OBJ_dsa_with_SHA3_384 OBJ_sigAlgs,7L + +#define SN_dsa_with_SHA3_512 "id-dsa-with-sha3-512" +#define LN_dsa_with_SHA3_512 "dsa_with_SHA3-512" +#define NID_dsa_with_SHA3_512 1111 +#define OBJ_dsa_with_SHA3_512 OBJ_sigAlgs,8L + +#define SN_ecdsa_with_SHA3_224 "id-ecdsa-with-sha3-224" +#define LN_ecdsa_with_SHA3_224 "ecdsa_with_SHA3-224" +#define NID_ecdsa_with_SHA3_224 1112 +#define OBJ_ecdsa_with_SHA3_224 OBJ_sigAlgs,9L + +#define SN_ecdsa_with_SHA3_256 "id-ecdsa-with-sha3-256" +#define LN_ecdsa_with_SHA3_256 "ecdsa_with_SHA3-256" +#define NID_ecdsa_with_SHA3_256 1113 +#define OBJ_ecdsa_with_SHA3_256 OBJ_sigAlgs,10L + +#define SN_ecdsa_with_SHA3_384 "id-ecdsa-with-sha3-384" +#define LN_ecdsa_with_SHA3_384 "ecdsa_with_SHA3-384" +#define NID_ecdsa_with_SHA3_384 1114 +#define OBJ_ecdsa_with_SHA3_384 OBJ_sigAlgs,11L + +#define SN_ecdsa_with_SHA3_512 "id-ecdsa-with-sha3-512" +#define LN_ecdsa_with_SHA3_512 "ecdsa_with_SHA3-512" +#define NID_ecdsa_with_SHA3_512 1115 +#define OBJ_ecdsa_with_SHA3_512 OBJ_sigAlgs,12L + +#define SN_RSA_SHA3_224 "id-rsassa-pkcs1-v1_5-with-sha3-224" +#define LN_RSA_SHA3_224 "RSA-SHA3-224" +#define NID_RSA_SHA3_224 1116 +#define OBJ_RSA_SHA3_224 OBJ_sigAlgs,13L + +#define SN_RSA_SHA3_256 "id-rsassa-pkcs1-v1_5-with-sha3-256" +#define LN_RSA_SHA3_256 "RSA-SHA3-256" +#define NID_RSA_SHA3_256 1117 +#define OBJ_RSA_SHA3_256 OBJ_sigAlgs,14L + +#define SN_RSA_SHA3_384 "id-rsassa-pkcs1-v1_5-with-sha3-384" +#define LN_RSA_SHA3_384 "RSA-SHA3-384" +#define NID_RSA_SHA3_384 1118 +#define OBJ_RSA_SHA3_384 OBJ_sigAlgs,15L + +#define SN_RSA_SHA3_512 "id-rsassa-pkcs1-v1_5-with-sha3-512" +#define LN_RSA_SHA3_512 "RSA-SHA3-512" +#define NID_RSA_SHA3_512 1119 +#define OBJ_RSA_SHA3_512 OBJ_sigAlgs,16L + +#define SN_hold_instruction_code "holdInstructionCode" +#define LN_hold_instruction_code "Hold Instruction Code" +#define NID_hold_instruction_code 430 +#define OBJ_hold_instruction_code OBJ_id_ce,23L + +#define OBJ_holdInstruction OBJ_X9_57,2L + +#define SN_hold_instruction_none "holdInstructionNone" +#define LN_hold_instruction_none "Hold Instruction None" +#define NID_hold_instruction_none 431 +#define OBJ_hold_instruction_none OBJ_holdInstruction,1L + +#define SN_hold_instruction_call_issuer "holdInstructionCallIssuer" +#define LN_hold_instruction_call_issuer "Hold Instruction Call Issuer" +#define NID_hold_instruction_call_issuer 432 +#define OBJ_hold_instruction_call_issuer OBJ_holdInstruction,2L + +#define SN_hold_instruction_reject "holdInstructionReject" +#define LN_hold_instruction_reject "Hold Instruction Reject" +#define NID_hold_instruction_reject 433 +#define OBJ_hold_instruction_reject OBJ_holdInstruction,3L + +#define SN_data "data" +#define NID_data 434 +#define OBJ_data OBJ_itu_t,9L + +#define SN_pss "pss" +#define NID_pss 435 +#define OBJ_pss OBJ_data,2342L + +#define SN_ucl "ucl" +#define NID_ucl 436 +#define OBJ_ucl OBJ_pss,19200300L + +#define SN_pilot "pilot" +#define NID_pilot 437 +#define OBJ_pilot OBJ_ucl,100L + +#define LN_pilotAttributeType "pilotAttributeType" +#define NID_pilotAttributeType 438 +#define OBJ_pilotAttributeType OBJ_pilot,1L + +#define LN_pilotAttributeSyntax "pilotAttributeSyntax" +#define NID_pilotAttributeSyntax 439 +#define OBJ_pilotAttributeSyntax OBJ_pilot,3L + +#define LN_pilotObjectClass "pilotObjectClass" +#define NID_pilotObjectClass 440 +#define OBJ_pilotObjectClass OBJ_pilot,4L + +#define LN_pilotGroups "pilotGroups" +#define NID_pilotGroups 441 +#define OBJ_pilotGroups OBJ_pilot,10L + +#define LN_iA5StringSyntax "iA5StringSyntax" +#define NID_iA5StringSyntax 442 +#define OBJ_iA5StringSyntax OBJ_pilotAttributeSyntax,4L + +#define LN_caseIgnoreIA5StringSyntax "caseIgnoreIA5StringSyntax" +#define NID_caseIgnoreIA5StringSyntax 443 +#define OBJ_caseIgnoreIA5StringSyntax OBJ_pilotAttributeSyntax,5L + +#define LN_pilotObject "pilotObject" +#define NID_pilotObject 444 +#define OBJ_pilotObject OBJ_pilotObjectClass,3L + +#define LN_pilotPerson "pilotPerson" +#define NID_pilotPerson 445 +#define OBJ_pilotPerson OBJ_pilotObjectClass,4L + +#define SN_account "account" +#define NID_account 446 +#define OBJ_account OBJ_pilotObjectClass,5L + +#define SN_document "document" +#define NID_document 447 +#define OBJ_document OBJ_pilotObjectClass,6L + +#define SN_room "room" +#define NID_room 448 +#define OBJ_room OBJ_pilotObjectClass,7L + +#define LN_documentSeries "documentSeries" +#define NID_documentSeries 449 +#define OBJ_documentSeries OBJ_pilotObjectClass,9L + +#define SN_Domain "domain" +#define LN_Domain "Domain" +#define NID_Domain 392 +#define OBJ_Domain OBJ_pilotObjectClass,13L + +#define LN_rFC822localPart "rFC822localPart" +#define NID_rFC822localPart 450 +#define OBJ_rFC822localPart OBJ_pilotObjectClass,14L + +#define LN_dNSDomain "dNSDomain" +#define NID_dNSDomain 451 +#define OBJ_dNSDomain OBJ_pilotObjectClass,15L + +#define LN_domainRelatedObject "domainRelatedObject" +#define NID_domainRelatedObject 452 +#define OBJ_domainRelatedObject OBJ_pilotObjectClass,17L + +#define LN_friendlyCountry "friendlyCountry" +#define NID_friendlyCountry 453 +#define OBJ_friendlyCountry OBJ_pilotObjectClass,18L + +#define LN_simpleSecurityObject "simpleSecurityObject" +#define NID_simpleSecurityObject 454 +#define OBJ_simpleSecurityObject OBJ_pilotObjectClass,19L + +#define LN_pilotOrganization "pilotOrganization" +#define NID_pilotOrganization 455 +#define OBJ_pilotOrganization OBJ_pilotObjectClass,20L + +#define LN_pilotDSA "pilotDSA" +#define NID_pilotDSA 456 +#define OBJ_pilotDSA OBJ_pilotObjectClass,21L + +#define LN_qualityLabelledData "qualityLabelledData" +#define NID_qualityLabelledData 457 +#define OBJ_qualityLabelledData OBJ_pilotObjectClass,22L + +#define SN_userId "UID" +#define LN_userId "userId" +#define NID_userId 458 +#define OBJ_userId OBJ_pilotAttributeType,1L + +#define LN_textEncodedORAddress "textEncodedORAddress" +#define NID_textEncodedORAddress 459 +#define OBJ_textEncodedORAddress OBJ_pilotAttributeType,2L + +#define SN_rfc822Mailbox "mail" +#define LN_rfc822Mailbox "rfc822Mailbox" +#define NID_rfc822Mailbox 460 +#define OBJ_rfc822Mailbox OBJ_pilotAttributeType,3L + +#define SN_info "info" +#define NID_info 461 +#define OBJ_info OBJ_pilotAttributeType,4L + +#define LN_favouriteDrink "favouriteDrink" +#define NID_favouriteDrink 462 +#define OBJ_favouriteDrink OBJ_pilotAttributeType,5L + +#define LN_roomNumber "roomNumber" +#define NID_roomNumber 463 +#define OBJ_roomNumber OBJ_pilotAttributeType,6L + +#define SN_photo "photo" +#define NID_photo 464 +#define OBJ_photo OBJ_pilotAttributeType,7L + +#define LN_userClass "userClass" +#define NID_userClass 465 +#define OBJ_userClass OBJ_pilotAttributeType,8L + +#define SN_host "host" +#define NID_host 466 +#define OBJ_host OBJ_pilotAttributeType,9L + +#define SN_manager "manager" +#define NID_manager 467 +#define OBJ_manager OBJ_pilotAttributeType,10L + +#define LN_documentIdentifier "documentIdentifier" +#define NID_documentIdentifier 468 +#define OBJ_documentIdentifier OBJ_pilotAttributeType,11L + +#define LN_documentTitle "documentTitle" +#define NID_documentTitle 469 +#define OBJ_documentTitle OBJ_pilotAttributeType,12L + +#define LN_documentVersion "documentVersion" +#define NID_documentVersion 470 +#define OBJ_documentVersion OBJ_pilotAttributeType,13L + +#define LN_documentAuthor "documentAuthor" +#define NID_documentAuthor 471 +#define OBJ_documentAuthor OBJ_pilotAttributeType,14L + +#define LN_documentLocation "documentLocation" +#define NID_documentLocation 472 +#define OBJ_documentLocation OBJ_pilotAttributeType,15L + +#define LN_homeTelephoneNumber "homeTelephoneNumber" +#define NID_homeTelephoneNumber 473 +#define OBJ_homeTelephoneNumber OBJ_pilotAttributeType,20L + +#define SN_secretary "secretary" +#define NID_secretary 474 +#define OBJ_secretary OBJ_pilotAttributeType,21L + +#define LN_otherMailbox "otherMailbox" +#define NID_otherMailbox 475 +#define OBJ_otherMailbox OBJ_pilotAttributeType,22L + +#define LN_lastModifiedTime "lastModifiedTime" +#define NID_lastModifiedTime 476 +#define OBJ_lastModifiedTime OBJ_pilotAttributeType,23L + +#define LN_lastModifiedBy "lastModifiedBy" +#define NID_lastModifiedBy 477 +#define OBJ_lastModifiedBy OBJ_pilotAttributeType,24L + +#define SN_domainComponent "DC" +#define LN_domainComponent "domainComponent" +#define NID_domainComponent 391 +#define OBJ_domainComponent OBJ_pilotAttributeType,25L + +#define LN_aRecord "aRecord" +#define NID_aRecord 478 +#define OBJ_aRecord OBJ_pilotAttributeType,26L + +#define LN_pilotAttributeType27 "pilotAttributeType27" +#define NID_pilotAttributeType27 479 +#define OBJ_pilotAttributeType27 OBJ_pilotAttributeType,27L + +#define LN_mXRecord "mXRecord" +#define NID_mXRecord 480 +#define OBJ_mXRecord OBJ_pilotAttributeType,28L + +#define LN_nSRecord "nSRecord" +#define NID_nSRecord 481 +#define OBJ_nSRecord OBJ_pilotAttributeType,29L + +#define LN_sOARecord "sOARecord" +#define NID_sOARecord 482 +#define OBJ_sOARecord OBJ_pilotAttributeType,30L + +#define LN_cNAMERecord "cNAMERecord" +#define NID_cNAMERecord 483 +#define OBJ_cNAMERecord OBJ_pilotAttributeType,31L + +#define LN_associatedDomain "associatedDomain" +#define NID_associatedDomain 484 +#define OBJ_associatedDomain OBJ_pilotAttributeType,37L + +#define LN_associatedName "associatedName" +#define NID_associatedName 485 +#define OBJ_associatedName OBJ_pilotAttributeType,38L + +#define LN_homePostalAddress "homePostalAddress" +#define NID_homePostalAddress 486 +#define OBJ_homePostalAddress OBJ_pilotAttributeType,39L + +#define LN_personalTitle "personalTitle" +#define NID_personalTitle 487 +#define OBJ_personalTitle OBJ_pilotAttributeType,40L + +#define LN_mobileTelephoneNumber "mobileTelephoneNumber" +#define NID_mobileTelephoneNumber 488 +#define OBJ_mobileTelephoneNumber OBJ_pilotAttributeType,41L + +#define LN_pagerTelephoneNumber "pagerTelephoneNumber" +#define NID_pagerTelephoneNumber 489 +#define OBJ_pagerTelephoneNumber OBJ_pilotAttributeType,42L + +#define LN_friendlyCountryName "friendlyCountryName" +#define NID_friendlyCountryName 490 +#define OBJ_friendlyCountryName OBJ_pilotAttributeType,43L + +#define SN_uniqueIdentifier "uid" +#define LN_uniqueIdentifier "uniqueIdentifier" +#define NID_uniqueIdentifier 102 +#define OBJ_uniqueIdentifier OBJ_pilotAttributeType,44L + +#define LN_organizationalStatus "organizationalStatus" +#define NID_organizationalStatus 491 +#define OBJ_organizationalStatus OBJ_pilotAttributeType,45L + +#define LN_janetMailbox "janetMailbox" +#define NID_janetMailbox 492 +#define OBJ_janetMailbox OBJ_pilotAttributeType,46L + +#define LN_mailPreferenceOption "mailPreferenceOption" +#define NID_mailPreferenceOption 493 +#define OBJ_mailPreferenceOption OBJ_pilotAttributeType,47L + +#define LN_buildingName "buildingName" +#define NID_buildingName 494 +#define OBJ_buildingName OBJ_pilotAttributeType,48L + +#define LN_dSAQuality "dSAQuality" +#define NID_dSAQuality 495 +#define OBJ_dSAQuality OBJ_pilotAttributeType,49L + +#define LN_singleLevelQuality "singleLevelQuality" +#define NID_singleLevelQuality 496 +#define OBJ_singleLevelQuality OBJ_pilotAttributeType,50L + +#define LN_subtreeMinimumQuality "subtreeMinimumQuality" +#define NID_subtreeMinimumQuality 497 +#define OBJ_subtreeMinimumQuality OBJ_pilotAttributeType,51L + +#define LN_subtreeMaximumQuality "subtreeMaximumQuality" +#define NID_subtreeMaximumQuality 498 +#define OBJ_subtreeMaximumQuality OBJ_pilotAttributeType,52L + +#define LN_personalSignature "personalSignature" +#define NID_personalSignature 499 +#define OBJ_personalSignature OBJ_pilotAttributeType,53L + +#define LN_dITRedirect "dITRedirect" +#define NID_dITRedirect 500 +#define OBJ_dITRedirect OBJ_pilotAttributeType,54L + +#define SN_audio "audio" +#define NID_audio 501 +#define OBJ_audio OBJ_pilotAttributeType,55L + +#define LN_documentPublisher "documentPublisher" +#define NID_documentPublisher 502 +#define OBJ_documentPublisher OBJ_pilotAttributeType,56L + +#define SN_id_set "id-set" +#define LN_id_set "Secure Electronic Transactions" +#define NID_id_set 512 +#define OBJ_id_set OBJ_international_organizations,42L + +#define SN_set_ctype "set-ctype" +#define LN_set_ctype "content types" +#define NID_set_ctype 513 +#define OBJ_set_ctype OBJ_id_set,0L + +#define SN_set_msgExt "set-msgExt" +#define LN_set_msgExt "message extensions" +#define NID_set_msgExt 514 +#define OBJ_set_msgExt OBJ_id_set,1L + +#define SN_set_attr "set-attr" +#define NID_set_attr 515 +#define OBJ_set_attr OBJ_id_set,3L + +#define SN_set_policy "set-policy" +#define NID_set_policy 516 +#define OBJ_set_policy OBJ_id_set,5L + +#define SN_set_certExt "set-certExt" +#define LN_set_certExt "certificate extensions" +#define NID_set_certExt 517 +#define OBJ_set_certExt OBJ_id_set,7L + +#define SN_set_brand "set-brand" +#define NID_set_brand 518 +#define OBJ_set_brand OBJ_id_set,8L + +#define SN_setct_PANData "setct-PANData" +#define NID_setct_PANData 519 +#define OBJ_setct_PANData OBJ_set_ctype,0L + +#define SN_setct_PANToken "setct-PANToken" +#define NID_setct_PANToken 520 +#define OBJ_setct_PANToken OBJ_set_ctype,1L + +#define SN_setct_PANOnly "setct-PANOnly" +#define NID_setct_PANOnly 521 +#define OBJ_setct_PANOnly OBJ_set_ctype,2L + +#define SN_setct_OIData "setct-OIData" +#define NID_setct_OIData 522 +#define OBJ_setct_OIData OBJ_set_ctype,3L + +#define SN_setct_PI "setct-PI" +#define NID_setct_PI 523 +#define OBJ_setct_PI OBJ_set_ctype,4L + +#define SN_setct_PIData "setct-PIData" +#define NID_setct_PIData 524 +#define OBJ_setct_PIData OBJ_set_ctype,5L + +#define SN_setct_PIDataUnsigned "setct-PIDataUnsigned" +#define NID_setct_PIDataUnsigned 525 +#define OBJ_setct_PIDataUnsigned OBJ_set_ctype,6L + +#define SN_setct_HODInput "setct-HODInput" +#define NID_setct_HODInput 526 +#define OBJ_setct_HODInput OBJ_set_ctype,7L + +#define SN_setct_AuthResBaggage "setct-AuthResBaggage" +#define NID_setct_AuthResBaggage 527 +#define OBJ_setct_AuthResBaggage OBJ_set_ctype,8L + +#define SN_setct_AuthRevReqBaggage "setct-AuthRevReqBaggage" +#define NID_setct_AuthRevReqBaggage 528 +#define OBJ_setct_AuthRevReqBaggage OBJ_set_ctype,9L + +#define SN_setct_AuthRevResBaggage "setct-AuthRevResBaggage" +#define NID_setct_AuthRevResBaggage 529 +#define OBJ_setct_AuthRevResBaggage OBJ_set_ctype,10L + +#define SN_setct_CapTokenSeq "setct-CapTokenSeq" +#define NID_setct_CapTokenSeq 530 +#define OBJ_setct_CapTokenSeq OBJ_set_ctype,11L + +#define SN_setct_PInitResData "setct-PInitResData" +#define NID_setct_PInitResData 531 +#define OBJ_setct_PInitResData OBJ_set_ctype,12L + +#define SN_setct_PI_TBS "setct-PI-TBS" +#define NID_setct_PI_TBS 532 +#define OBJ_setct_PI_TBS OBJ_set_ctype,13L + +#define SN_setct_PResData "setct-PResData" +#define NID_setct_PResData 533 +#define OBJ_setct_PResData OBJ_set_ctype,14L + +#define SN_setct_AuthReqTBS "setct-AuthReqTBS" +#define NID_setct_AuthReqTBS 534 +#define OBJ_setct_AuthReqTBS OBJ_set_ctype,16L + +#define SN_setct_AuthResTBS "setct-AuthResTBS" +#define NID_setct_AuthResTBS 535 +#define OBJ_setct_AuthResTBS OBJ_set_ctype,17L + +#define SN_setct_AuthResTBSX "setct-AuthResTBSX" +#define NID_setct_AuthResTBSX 536 +#define OBJ_setct_AuthResTBSX OBJ_set_ctype,18L + +#define SN_setct_AuthTokenTBS "setct-AuthTokenTBS" +#define NID_setct_AuthTokenTBS 537 +#define OBJ_setct_AuthTokenTBS OBJ_set_ctype,19L + +#define SN_setct_CapTokenData "setct-CapTokenData" +#define NID_setct_CapTokenData 538 +#define OBJ_setct_CapTokenData OBJ_set_ctype,20L + +#define SN_setct_CapTokenTBS "setct-CapTokenTBS" +#define NID_setct_CapTokenTBS 539 +#define OBJ_setct_CapTokenTBS OBJ_set_ctype,21L + +#define SN_setct_AcqCardCodeMsg "setct-AcqCardCodeMsg" +#define NID_setct_AcqCardCodeMsg 540 +#define OBJ_setct_AcqCardCodeMsg OBJ_set_ctype,22L + +#define SN_setct_AuthRevReqTBS "setct-AuthRevReqTBS" +#define NID_setct_AuthRevReqTBS 541 +#define OBJ_setct_AuthRevReqTBS OBJ_set_ctype,23L + +#define SN_setct_AuthRevResData "setct-AuthRevResData" +#define NID_setct_AuthRevResData 542 +#define OBJ_setct_AuthRevResData OBJ_set_ctype,24L + +#define SN_setct_AuthRevResTBS "setct-AuthRevResTBS" +#define NID_setct_AuthRevResTBS 543 +#define OBJ_setct_AuthRevResTBS OBJ_set_ctype,25L + +#define SN_setct_CapReqTBS "setct-CapReqTBS" +#define NID_setct_CapReqTBS 544 +#define OBJ_setct_CapReqTBS OBJ_set_ctype,26L + +#define SN_setct_CapReqTBSX "setct-CapReqTBSX" +#define NID_setct_CapReqTBSX 545 +#define OBJ_setct_CapReqTBSX OBJ_set_ctype,27L + +#define SN_setct_CapResData "setct-CapResData" +#define NID_setct_CapResData 546 +#define OBJ_setct_CapResData OBJ_set_ctype,28L + +#define SN_setct_CapRevReqTBS "setct-CapRevReqTBS" +#define NID_setct_CapRevReqTBS 547 +#define OBJ_setct_CapRevReqTBS OBJ_set_ctype,29L + +#define SN_setct_CapRevReqTBSX "setct-CapRevReqTBSX" +#define NID_setct_CapRevReqTBSX 548 +#define OBJ_setct_CapRevReqTBSX OBJ_set_ctype,30L + +#define SN_setct_CapRevResData "setct-CapRevResData" +#define NID_setct_CapRevResData 549 +#define OBJ_setct_CapRevResData OBJ_set_ctype,31L + +#define SN_setct_CredReqTBS "setct-CredReqTBS" +#define NID_setct_CredReqTBS 550 +#define OBJ_setct_CredReqTBS OBJ_set_ctype,32L + +#define SN_setct_CredReqTBSX "setct-CredReqTBSX" +#define NID_setct_CredReqTBSX 551 +#define OBJ_setct_CredReqTBSX OBJ_set_ctype,33L + +#define SN_setct_CredResData "setct-CredResData" +#define NID_setct_CredResData 552 +#define OBJ_setct_CredResData OBJ_set_ctype,34L + +#define SN_setct_CredRevReqTBS "setct-CredRevReqTBS" +#define NID_setct_CredRevReqTBS 553 +#define OBJ_setct_CredRevReqTBS OBJ_set_ctype,35L + +#define SN_setct_CredRevReqTBSX "setct-CredRevReqTBSX" +#define NID_setct_CredRevReqTBSX 554 +#define OBJ_setct_CredRevReqTBSX OBJ_set_ctype,36L + +#define SN_setct_CredRevResData "setct-CredRevResData" +#define NID_setct_CredRevResData 555 +#define OBJ_setct_CredRevResData OBJ_set_ctype,37L + +#define SN_setct_PCertReqData "setct-PCertReqData" +#define NID_setct_PCertReqData 556 +#define OBJ_setct_PCertReqData OBJ_set_ctype,38L + +#define SN_setct_PCertResTBS "setct-PCertResTBS" +#define NID_setct_PCertResTBS 557 +#define OBJ_setct_PCertResTBS OBJ_set_ctype,39L + +#define SN_setct_BatchAdminReqData "setct-BatchAdminReqData" +#define NID_setct_BatchAdminReqData 558 +#define OBJ_setct_BatchAdminReqData OBJ_set_ctype,40L + +#define SN_setct_BatchAdminResData "setct-BatchAdminResData" +#define NID_setct_BatchAdminResData 559 +#define OBJ_setct_BatchAdminResData OBJ_set_ctype,41L + +#define SN_setct_CardCInitResTBS "setct-CardCInitResTBS" +#define NID_setct_CardCInitResTBS 560 +#define OBJ_setct_CardCInitResTBS OBJ_set_ctype,42L + +#define SN_setct_MeAqCInitResTBS "setct-MeAqCInitResTBS" +#define NID_setct_MeAqCInitResTBS 561 +#define OBJ_setct_MeAqCInitResTBS OBJ_set_ctype,43L + +#define SN_setct_RegFormResTBS "setct-RegFormResTBS" +#define NID_setct_RegFormResTBS 562 +#define OBJ_setct_RegFormResTBS OBJ_set_ctype,44L + +#define SN_setct_CertReqData "setct-CertReqData" +#define NID_setct_CertReqData 563 +#define OBJ_setct_CertReqData OBJ_set_ctype,45L + +#define SN_setct_CertReqTBS "setct-CertReqTBS" +#define NID_setct_CertReqTBS 564 +#define OBJ_setct_CertReqTBS OBJ_set_ctype,46L + +#define SN_setct_CertResData "setct-CertResData" +#define NID_setct_CertResData 565 +#define OBJ_setct_CertResData OBJ_set_ctype,47L + +#define SN_setct_CertInqReqTBS "setct-CertInqReqTBS" +#define NID_setct_CertInqReqTBS 566 +#define OBJ_setct_CertInqReqTBS OBJ_set_ctype,48L + +#define SN_setct_ErrorTBS "setct-ErrorTBS" +#define NID_setct_ErrorTBS 567 +#define OBJ_setct_ErrorTBS OBJ_set_ctype,49L + +#define SN_setct_PIDualSignedTBE "setct-PIDualSignedTBE" +#define NID_setct_PIDualSignedTBE 568 +#define OBJ_setct_PIDualSignedTBE OBJ_set_ctype,50L + +#define SN_setct_PIUnsignedTBE "setct-PIUnsignedTBE" +#define NID_setct_PIUnsignedTBE 569 +#define OBJ_setct_PIUnsignedTBE OBJ_set_ctype,51L + +#define SN_setct_AuthReqTBE "setct-AuthReqTBE" +#define NID_setct_AuthReqTBE 570 +#define OBJ_setct_AuthReqTBE OBJ_set_ctype,52L + +#define SN_setct_AuthResTBE "setct-AuthResTBE" +#define NID_setct_AuthResTBE 571 +#define OBJ_setct_AuthResTBE OBJ_set_ctype,53L + +#define SN_setct_AuthResTBEX "setct-AuthResTBEX" +#define NID_setct_AuthResTBEX 572 +#define OBJ_setct_AuthResTBEX OBJ_set_ctype,54L + +#define SN_setct_AuthTokenTBE "setct-AuthTokenTBE" +#define NID_setct_AuthTokenTBE 573 +#define OBJ_setct_AuthTokenTBE OBJ_set_ctype,55L + +#define SN_setct_CapTokenTBE "setct-CapTokenTBE" +#define NID_setct_CapTokenTBE 574 +#define OBJ_setct_CapTokenTBE OBJ_set_ctype,56L + +#define SN_setct_CapTokenTBEX "setct-CapTokenTBEX" +#define NID_setct_CapTokenTBEX 575 +#define OBJ_setct_CapTokenTBEX OBJ_set_ctype,57L + +#define SN_setct_AcqCardCodeMsgTBE "setct-AcqCardCodeMsgTBE" +#define NID_setct_AcqCardCodeMsgTBE 576 +#define OBJ_setct_AcqCardCodeMsgTBE OBJ_set_ctype,58L + +#define SN_setct_AuthRevReqTBE "setct-AuthRevReqTBE" +#define NID_setct_AuthRevReqTBE 577 +#define OBJ_setct_AuthRevReqTBE OBJ_set_ctype,59L + +#define SN_setct_AuthRevResTBE "setct-AuthRevResTBE" +#define NID_setct_AuthRevResTBE 578 +#define OBJ_setct_AuthRevResTBE OBJ_set_ctype,60L + +#define SN_setct_AuthRevResTBEB "setct-AuthRevResTBEB" +#define NID_setct_AuthRevResTBEB 579 +#define OBJ_setct_AuthRevResTBEB OBJ_set_ctype,61L + +#define SN_setct_CapReqTBE "setct-CapReqTBE" +#define NID_setct_CapReqTBE 580 +#define OBJ_setct_CapReqTBE OBJ_set_ctype,62L + +#define SN_setct_CapReqTBEX "setct-CapReqTBEX" +#define NID_setct_CapReqTBEX 581 +#define OBJ_setct_CapReqTBEX OBJ_set_ctype,63L + +#define SN_setct_CapResTBE "setct-CapResTBE" +#define NID_setct_CapResTBE 582 +#define OBJ_setct_CapResTBE OBJ_set_ctype,64L + +#define SN_setct_CapRevReqTBE "setct-CapRevReqTBE" +#define NID_setct_CapRevReqTBE 583 +#define OBJ_setct_CapRevReqTBE OBJ_set_ctype,65L + +#define SN_setct_CapRevReqTBEX "setct-CapRevReqTBEX" +#define NID_setct_CapRevReqTBEX 584 +#define OBJ_setct_CapRevReqTBEX OBJ_set_ctype,66L + +#define SN_setct_CapRevResTBE "setct-CapRevResTBE" +#define NID_setct_CapRevResTBE 585 +#define OBJ_setct_CapRevResTBE OBJ_set_ctype,67L + +#define SN_setct_CredReqTBE "setct-CredReqTBE" +#define NID_setct_CredReqTBE 586 +#define OBJ_setct_CredReqTBE OBJ_set_ctype,68L + +#define SN_setct_CredReqTBEX "setct-CredReqTBEX" +#define NID_setct_CredReqTBEX 587 +#define OBJ_setct_CredReqTBEX OBJ_set_ctype,69L + +#define SN_setct_CredResTBE "setct-CredResTBE" +#define NID_setct_CredResTBE 588 +#define OBJ_setct_CredResTBE OBJ_set_ctype,70L + +#define SN_setct_CredRevReqTBE "setct-CredRevReqTBE" +#define NID_setct_CredRevReqTBE 589 +#define OBJ_setct_CredRevReqTBE OBJ_set_ctype,71L + +#define SN_setct_CredRevReqTBEX "setct-CredRevReqTBEX" +#define NID_setct_CredRevReqTBEX 590 +#define OBJ_setct_CredRevReqTBEX OBJ_set_ctype,72L + +#define SN_setct_CredRevResTBE "setct-CredRevResTBE" +#define NID_setct_CredRevResTBE 591 +#define OBJ_setct_CredRevResTBE OBJ_set_ctype,73L + +#define SN_setct_BatchAdminReqTBE "setct-BatchAdminReqTBE" +#define NID_setct_BatchAdminReqTBE 592 +#define OBJ_setct_BatchAdminReqTBE OBJ_set_ctype,74L + +#define SN_setct_BatchAdminResTBE "setct-BatchAdminResTBE" +#define NID_setct_BatchAdminResTBE 593 +#define OBJ_setct_BatchAdminResTBE OBJ_set_ctype,75L + +#define SN_setct_RegFormReqTBE "setct-RegFormReqTBE" +#define NID_setct_RegFormReqTBE 594 +#define OBJ_setct_RegFormReqTBE OBJ_set_ctype,76L + +#define SN_setct_CertReqTBE "setct-CertReqTBE" +#define NID_setct_CertReqTBE 595 +#define OBJ_setct_CertReqTBE OBJ_set_ctype,77L + +#define SN_setct_CertReqTBEX "setct-CertReqTBEX" +#define NID_setct_CertReqTBEX 596 +#define OBJ_setct_CertReqTBEX OBJ_set_ctype,78L + +#define SN_setct_CertResTBE "setct-CertResTBE" +#define NID_setct_CertResTBE 597 +#define OBJ_setct_CertResTBE OBJ_set_ctype,79L + +#define SN_setct_CRLNotificationTBS "setct-CRLNotificationTBS" +#define NID_setct_CRLNotificationTBS 598 +#define OBJ_setct_CRLNotificationTBS OBJ_set_ctype,80L + +#define SN_setct_CRLNotificationResTBS "setct-CRLNotificationResTBS" +#define NID_setct_CRLNotificationResTBS 599 +#define OBJ_setct_CRLNotificationResTBS OBJ_set_ctype,81L + +#define SN_setct_BCIDistributionTBS "setct-BCIDistributionTBS" +#define NID_setct_BCIDistributionTBS 600 +#define OBJ_setct_BCIDistributionTBS OBJ_set_ctype,82L + +#define SN_setext_genCrypt "setext-genCrypt" +#define LN_setext_genCrypt "generic cryptogram" +#define NID_setext_genCrypt 601 +#define OBJ_setext_genCrypt OBJ_set_msgExt,1L + +#define SN_setext_miAuth "setext-miAuth" +#define LN_setext_miAuth "merchant initiated auth" +#define NID_setext_miAuth 602 +#define OBJ_setext_miAuth OBJ_set_msgExt,3L + +#define SN_setext_pinSecure "setext-pinSecure" +#define NID_setext_pinSecure 603 +#define OBJ_setext_pinSecure OBJ_set_msgExt,4L + +#define SN_setext_pinAny "setext-pinAny" +#define NID_setext_pinAny 604 +#define OBJ_setext_pinAny OBJ_set_msgExt,5L + +#define SN_setext_track2 "setext-track2" +#define NID_setext_track2 605 +#define OBJ_setext_track2 OBJ_set_msgExt,7L + +#define SN_setext_cv "setext-cv" +#define LN_setext_cv "additional verification" +#define NID_setext_cv 606 +#define OBJ_setext_cv OBJ_set_msgExt,8L + +#define SN_set_policy_root "set-policy-root" +#define NID_set_policy_root 607 +#define OBJ_set_policy_root OBJ_set_policy,0L + +#define SN_setCext_hashedRoot "setCext-hashedRoot" +#define NID_setCext_hashedRoot 608 +#define OBJ_setCext_hashedRoot OBJ_set_certExt,0L + +#define SN_setCext_certType "setCext-certType" +#define NID_setCext_certType 609 +#define OBJ_setCext_certType OBJ_set_certExt,1L + +#define SN_setCext_merchData "setCext-merchData" +#define NID_setCext_merchData 610 +#define OBJ_setCext_merchData OBJ_set_certExt,2L + +#define SN_setCext_cCertRequired "setCext-cCertRequired" +#define NID_setCext_cCertRequired 611 +#define OBJ_setCext_cCertRequired OBJ_set_certExt,3L + +#define SN_setCext_tunneling "setCext-tunneling" +#define NID_setCext_tunneling 612 +#define OBJ_setCext_tunneling OBJ_set_certExt,4L + +#define SN_setCext_setExt "setCext-setExt" +#define NID_setCext_setExt 613 +#define OBJ_setCext_setExt OBJ_set_certExt,5L + +#define SN_setCext_setQualf "setCext-setQualf" +#define NID_setCext_setQualf 614 +#define OBJ_setCext_setQualf OBJ_set_certExt,6L + +#define SN_setCext_PGWYcapabilities "setCext-PGWYcapabilities" +#define NID_setCext_PGWYcapabilities 615 +#define OBJ_setCext_PGWYcapabilities OBJ_set_certExt,7L + +#define SN_setCext_TokenIdentifier "setCext-TokenIdentifier" +#define NID_setCext_TokenIdentifier 616 +#define OBJ_setCext_TokenIdentifier OBJ_set_certExt,8L + +#define SN_setCext_Track2Data "setCext-Track2Data" +#define NID_setCext_Track2Data 617 +#define OBJ_setCext_Track2Data OBJ_set_certExt,9L + +#define SN_setCext_TokenType "setCext-TokenType" +#define NID_setCext_TokenType 618 +#define OBJ_setCext_TokenType OBJ_set_certExt,10L + +#define SN_setCext_IssuerCapabilities "setCext-IssuerCapabilities" +#define NID_setCext_IssuerCapabilities 619 +#define OBJ_setCext_IssuerCapabilities OBJ_set_certExt,11L + +#define SN_setAttr_Cert "setAttr-Cert" +#define NID_setAttr_Cert 620 +#define OBJ_setAttr_Cert OBJ_set_attr,0L + +#define SN_setAttr_PGWYcap "setAttr-PGWYcap" +#define LN_setAttr_PGWYcap "payment gateway capabilities" +#define NID_setAttr_PGWYcap 621 +#define OBJ_setAttr_PGWYcap OBJ_set_attr,1L + +#define SN_setAttr_TokenType "setAttr-TokenType" +#define NID_setAttr_TokenType 622 +#define OBJ_setAttr_TokenType OBJ_set_attr,2L + +#define SN_setAttr_IssCap "setAttr-IssCap" +#define LN_setAttr_IssCap "issuer capabilities" +#define NID_setAttr_IssCap 623 +#define OBJ_setAttr_IssCap OBJ_set_attr,3L + +#define SN_set_rootKeyThumb "set-rootKeyThumb" +#define NID_set_rootKeyThumb 624 +#define OBJ_set_rootKeyThumb OBJ_setAttr_Cert,0L + +#define SN_set_addPolicy "set-addPolicy" +#define NID_set_addPolicy 625 +#define OBJ_set_addPolicy OBJ_setAttr_Cert,1L + +#define SN_setAttr_Token_EMV "setAttr-Token-EMV" +#define NID_setAttr_Token_EMV 626 +#define OBJ_setAttr_Token_EMV OBJ_setAttr_TokenType,1L + +#define SN_setAttr_Token_B0Prime "setAttr-Token-B0Prime" +#define NID_setAttr_Token_B0Prime 627 +#define OBJ_setAttr_Token_B0Prime OBJ_setAttr_TokenType,2L + +#define SN_setAttr_IssCap_CVM "setAttr-IssCap-CVM" +#define NID_setAttr_IssCap_CVM 628 +#define OBJ_setAttr_IssCap_CVM OBJ_setAttr_IssCap,3L + +#define SN_setAttr_IssCap_T2 "setAttr-IssCap-T2" +#define NID_setAttr_IssCap_T2 629 +#define OBJ_setAttr_IssCap_T2 OBJ_setAttr_IssCap,4L + +#define SN_setAttr_IssCap_Sig "setAttr-IssCap-Sig" +#define NID_setAttr_IssCap_Sig 630 +#define OBJ_setAttr_IssCap_Sig OBJ_setAttr_IssCap,5L + +#define SN_setAttr_GenCryptgrm "setAttr-GenCryptgrm" +#define LN_setAttr_GenCryptgrm "generate cryptogram" +#define NID_setAttr_GenCryptgrm 631 +#define OBJ_setAttr_GenCryptgrm OBJ_setAttr_IssCap_CVM,1L + +#define SN_setAttr_T2Enc "setAttr-T2Enc" +#define LN_setAttr_T2Enc "encrypted track 2" +#define NID_setAttr_T2Enc 632 +#define OBJ_setAttr_T2Enc OBJ_setAttr_IssCap_T2,1L + +#define SN_setAttr_T2cleartxt "setAttr-T2cleartxt" +#define LN_setAttr_T2cleartxt "cleartext track 2" +#define NID_setAttr_T2cleartxt 633 +#define OBJ_setAttr_T2cleartxt OBJ_setAttr_IssCap_T2,2L + +#define SN_setAttr_TokICCsig "setAttr-TokICCsig" +#define LN_setAttr_TokICCsig "ICC or token signature" +#define NID_setAttr_TokICCsig 634 +#define OBJ_setAttr_TokICCsig OBJ_setAttr_IssCap_Sig,1L + +#define SN_setAttr_SecDevSig "setAttr-SecDevSig" +#define LN_setAttr_SecDevSig "secure device signature" +#define NID_setAttr_SecDevSig 635 +#define OBJ_setAttr_SecDevSig OBJ_setAttr_IssCap_Sig,2L + +#define SN_set_brand_IATA_ATA "set-brand-IATA-ATA" +#define NID_set_brand_IATA_ATA 636 +#define OBJ_set_brand_IATA_ATA OBJ_set_brand,1L + +#define SN_set_brand_Diners "set-brand-Diners" +#define NID_set_brand_Diners 637 +#define OBJ_set_brand_Diners OBJ_set_brand,30L + +#define SN_set_brand_AmericanExpress "set-brand-AmericanExpress" +#define NID_set_brand_AmericanExpress 638 +#define OBJ_set_brand_AmericanExpress OBJ_set_brand,34L + +#define SN_set_brand_JCB "set-brand-JCB" +#define NID_set_brand_JCB 639 +#define OBJ_set_brand_JCB OBJ_set_brand,35L + +#define SN_set_brand_Visa "set-brand-Visa" +#define NID_set_brand_Visa 640 +#define OBJ_set_brand_Visa OBJ_set_brand,4L + +#define SN_set_brand_MasterCard "set-brand-MasterCard" +#define NID_set_brand_MasterCard 641 +#define OBJ_set_brand_MasterCard OBJ_set_brand,5L + +#define SN_set_brand_Novus "set-brand-Novus" +#define NID_set_brand_Novus 642 +#define OBJ_set_brand_Novus OBJ_set_brand,6011L + +#define SN_des_cdmf "DES-CDMF" +#define LN_des_cdmf "des-cdmf" +#define NID_des_cdmf 643 +#define OBJ_des_cdmf OBJ_rsadsi,3L,10L + +#define SN_rsaOAEPEncryptionSET "rsaOAEPEncryptionSET" +#define NID_rsaOAEPEncryptionSET 644 +#define OBJ_rsaOAEPEncryptionSET OBJ_rsadsi,1L,1L,6L + +#define SN_ipsec3 "Oakley-EC2N-3" +#define LN_ipsec3 "ipsec3" +#define NID_ipsec3 749 + +#define SN_ipsec4 "Oakley-EC2N-4" +#define LN_ipsec4 "ipsec4" +#define NID_ipsec4 750 + +#define SN_whirlpool "whirlpool" +#define NID_whirlpool 804 +#define OBJ_whirlpool OBJ_iso,0L,10118L,3L,0L,55L + +#define SN_cryptopro "cryptopro" +#define NID_cryptopro 805 +#define OBJ_cryptopro OBJ_member_body,643L,2L,2L + +#define SN_cryptocom "cryptocom" +#define NID_cryptocom 806 +#define OBJ_cryptocom OBJ_member_body,643L,2L,9L + +#define SN_id_tc26 "id-tc26" +#define NID_id_tc26 974 +#define OBJ_id_tc26 OBJ_member_body,643L,7L,1L + +#define SN_id_GostR3411_94_with_GostR3410_2001 "id-GostR3411-94-with-GostR3410-2001" +#define LN_id_GostR3411_94_with_GostR3410_2001 "GOST R 34.11-94 with GOST R 34.10-2001" +#define NID_id_GostR3411_94_with_GostR3410_2001 807 +#define OBJ_id_GostR3411_94_with_GostR3410_2001 OBJ_cryptopro,3L + +#define SN_id_GostR3411_94_with_GostR3410_94 "id-GostR3411-94-with-GostR3410-94" +#define LN_id_GostR3411_94_with_GostR3410_94 "GOST R 34.11-94 with GOST R 34.10-94" +#define NID_id_GostR3411_94_with_GostR3410_94 808 +#define OBJ_id_GostR3411_94_with_GostR3410_94 OBJ_cryptopro,4L + +#define SN_id_GostR3411_94 "md_gost94" +#define LN_id_GostR3411_94 "GOST R 34.11-94" +#define NID_id_GostR3411_94 809 +#define OBJ_id_GostR3411_94 OBJ_cryptopro,9L + +#define SN_id_HMACGostR3411_94 "id-HMACGostR3411-94" +#define LN_id_HMACGostR3411_94 "HMAC GOST 34.11-94" +#define NID_id_HMACGostR3411_94 810 +#define OBJ_id_HMACGostR3411_94 OBJ_cryptopro,10L + +#define SN_id_GostR3410_2001 "gost2001" +#define LN_id_GostR3410_2001 "GOST R 34.10-2001" +#define NID_id_GostR3410_2001 811 +#define OBJ_id_GostR3410_2001 OBJ_cryptopro,19L + +#define SN_id_GostR3410_94 "gost94" +#define LN_id_GostR3410_94 "GOST R 34.10-94" +#define NID_id_GostR3410_94 812 +#define OBJ_id_GostR3410_94 OBJ_cryptopro,20L + +#define SN_id_Gost28147_89 "gost89" +#define LN_id_Gost28147_89 "GOST 28147-89" +#define NID_id_Gost28147_89 813 +#define OBJ_id_Gost28147_89 OBJ_cryptopro,21L + +#define SN_gost89_cnt "gost89-cnt" +#define NID_gost89_cnt 814 + +#define SN_gost89_cnt_12 "gost89-cnt-12" +#define NID_gost89_cnt_12 975 + +#define SN_gost89_cbc "gost89-cbc" +#define NID_gost89_cbc 1009 + +#define SN_gost89_ecb "gost89-ecb" +#define NID_gost89_ecb 1010 + +#define SN_gost89_ctr "gost89-ctr" +#define NID_gost89_ctr 1011 + +#define SN_id_Gost28147_89_MAC "gost-mac" +#define LN_id_Gost28147_89_MAC "GOST 28147-89 MAC" +#define NID_id_Gost28147_89_MAC 815 +#define OBJ_id_Gost28147_89_MAC OBJ_cryptopro,22L + +#define SN_gost_mac_12 "gost-mac-12" +#define NID_gost_mac_12 976 + +#define SN_id_GostR3411_94_prf "prf-gostr3411-94" +#define LN_id_GostR3411_94_prf "GOST R 34.11-94 PRF" +#define NID_id_GostR3411_94_prf 816 +#define OBJ_id_GostR3411_94_prf OBJ_cryptopro,23L + +#define SN_id_GostR3410_2001DH "id-GostR3410-2001DH" +#define LN_id_GostR3410_2001DH "GOST R 34.10-2001 DH" +#define NID_id_GostR3410_2001DH 817 +#define OBJ_id_GostR3410_2001DH OBJ_cryptopro,98L + +#define SN_id_GostR3410_94DH "id-GostR3410-94DH" +#define LN_id_GostR3410_94DH "GOST R 34.10-94 DH" +#define NID_id_GostR3410_94DH 818 +#define OBJ_id_GostR3410_94DH OBJ_cryptopro,99L + +#define SN_id_Gost28147_89_CryptoPro_KeyMeshing "id-Gost28147-89-CryptoPro-KeyMeshing" +#define NID_id_Gost28147_89_CryptoPro_KeyMeshing 819 +#define OBJ_id_Gost28147_89_CryptoPro_KeyMeshing OBJ_cryptopro,14L,1L + +#define SN_id_Gost28147_89_None_KeyMeshing "id-Gost28147-89-None-KeyMeshing" +#define NID_id_Gost28147_89_None_KeyMeshing 820 +#define OBJ_id_Gost28147_89_None_KeyMeshing OBJ_cryptopro,14L,0L + +#define SN_id_GostR3411_94_TestParamSet "id-GostR3411-94-TestParamSet" +#define NID_id_GostR3411_94_TestParamSet 821 +#define OBJ_id_GostR3411_94_TestParamSet OBJ_cryptopro,30L,0L + +#define SN_id_GostR3411_94_CryptoProParamSet "id-GostR3411-94-CryptoProParamSet" +#define NID_id_GostR3411_94_CryptoProParamSet 822 +#define OBJ_id_GostR3411_94_CryptoProParamSet OBJ_cryptopro,30L,1L + +#define SN_id_Gost28147_89_TestParamSet "id-Gost28147-89-TestParamSet" +#define NID_id_Gost28147_89_TestParamSet 823 +#define OBJ_id_Gost28147_89_TestParamSet OBJ_cryptopro,31L,0L + +#define SN_id_Gost28147_89_CryptoPro_A_ParamSet "id-Gost28147-89-CryptoPro-A-ParamSet" +#define NID_id_Gost28147_89_CryptoPro_A_ParamSet 824 +#define OBJ_id_Gost28147_89_CryptoPro_A_ParamSet OBJ_cryptopro,31L,1L + +#define SN_id_Gost28147_89_CryptoPro_B_ParamSet "id-Gost28147-89-CryptoPro-B-ParamSet" +#define NID_id_Gost28147_89_CryptoPro_B_ParamSet 825 +#define OBJ_id_Gost28147_89_CryptoPro_B_ParamSet OBJ_cryptopro,31L,2L + +#define SN_id_Gost28147_89_CryptoPro_C_ParamSet "id-Gost28147-89-CryptoPro-C-ParamSet" +#define NID_id_Gost28147_89_CryptoPro_C_ParamSet 826 +#define OBJ_id_Gost28147_89_CryptoPro_C_ParamSet OBJ_cryptopro,31L,3L + +#define SN_id_Gost28147_89_CryptoPro_D_ParamSet "id-Gost28147-89-CryptoPro-D-ParamSet" +#define NID_id_Gost28147_89_CryptoPro_D_ParamSet 827 +#define OBJ_id_Gost28147_89_CryptoPro_D_ParamSet OBJ_cryptopro,31L,4L + +#define SN_id_Gost28147_89_CryptoPro_Oscar_1_1_ParamSet "id-Gost28147-89-CryptoPro-Oscar-1-1-ParamSet" +#define NID_id_Gost28147_89_CryptoPro_Oscar_1_1_ParamSet 828 +#define OBJ_id_Gost28147_89_CryptoPro_Oscar_1_1_ParamSet OBJ_cryptopro,31L,5L + +#define SN_id_Gost28147_89_CryptoPro_Oscar_1_0_ParamSet "id-Gost28147-89-CryptoPro-Oscar-1-0-ParamSet" +#define NID_id_Gost28147_89_CryptoPro_Oscar_1_0_ParamSet 829 +#define OBJ_id_Gost28147_89_CryptoPro_Oscar_1_0_ParamSet OBJ_cryptopro,31L,6L + +#define SN_id_Gost28147_89_CryptoPro_RIC_1_ParamSet "id-Gost28147-89-CryptoPro-RIC-1-ParamSet" +#define NID_id_Gost28147_89_CryptoPro_RIC_1_ParamSet 830 +#define OBJ_id_Gost28147_89_CryptoPro_RIC_1_ParamSet OBJ_cryptopro,31L,7L + +#define SN_id_GostR3410_94_TestParamSet "id-GostR3410-94-TestParamSet" +#define NID_id_GostR3410_94_TestParamSet 831 +#define OBJ_id_GostR3410_94_TestParamSet OBJ_cryptopro,32L,0L + +#define SN_id_GostR3410_94_CryptoPro_A_ParamSet "id-GostR3410-94-CryptoPro-A-ParamSet" +#define NID_id_GostR3410_94_CryptoPro_A_ParamSet 832 +#define OBJ_id_GostR3410_94_CryptoPro_A_ParamSet OBJ_cryptopro,32L,2L + +#define SN_id_GostR3410_94_CryptoPro_B_ParamSet "id-GostR3410-94-CryptoPro-B-ParamSet" +#define NID_id_GostR3410_94_CryptoPro_B_ParamSet 833 +#define OBJ_id_GostR3410_94_CryptoPro_B_ParamSet OBJ_cryptopro,32L,3L + +#define SN_id_GostR3410_94_CryptoPro_C_ParamSet "id-GostR3410-94-CryptoPro-C-ParamSet" +#define NID_id_GostR3410_94_CryptoPro_C_ParamSet 834 +#define OBJ_id_GostR3410_94_CryptoPro_C_ParamSet OBJ_cryptopro,32L,4L + +#define SN_id_GostR3410_94_CryptoPro_D_ParamSet "id-GostR3410-94-CryptoPro-D-ParamSet" +#define NID_id_GostR3410_94_CryptoPro_D_ParamSet 835 +#define OBJ_id_GostR3410_94_CryptoPro_D_ParamSet OBJ_cryptopro,32L,5L + +#define SN_id_GostR3410_94_CryptoPro_XchA_ParamSet "id-GostR3410-94-CryptoPro-XchA-ParamSet" +#define NID_id_GostR3410_94_CryptoPro_XchA_ParamSet 836 +#define OBJ_id_GostR3410_94_CryptoPro_XchA_ParamSet OBJ_cryptopro,33L,1L + +#define SN_id_GostR3410_94_CryptoPro_XchB_ParamSet "id-GostR3410-94-CryptoPro-XchB-ParamSet" +#define NID_id_GostR3410_94_CryptoPro_XchB_ParamSet 837 +#define OBJ_id_GostR3410_94_CryptoPro_XchB_ParamSet OBJ_cryptopro,33L,2L + +#define SN_id_GostR3410_94_CryptoPro_XchC_ParamSet "id-GostR3410-94-CryptoPro-XchC-ParamSet" +#define NID_id_GostR3410_94_CryptoPro_XchC_ParamSet 838 +#define OBJ_id_GostR3410_94_CryptoPro_XchC_ParamSet OBJ_cryptopro,33L,3L + +#define SN_id_GostR3410_2001_TestParamSet "id-GostR3410-2001-TestParamSet" +#define NID_id_GostR3410_2001_TestParamSet 839 +#define OBJ_id_GostR3410_2001_TestParamSet OBJ_cryptopro,35L,0L + +#define SN_id_GostR3410_2001_CryptoPro_A_ParamSet "id-GostR3410-2001-CryptoPro-A-ParamSet" +#define NID_id_GostR3410_2001_CryptoPro_A_ParamSet 840 +#define OBJ_id_GostR3410_2001_CryptoPro_A_ParamSet OBJ_cryptopro,35L,1L + +#define SN_id_GostR3410_2001_CryptoPro_B_ParamSet "id-GostR3410-2001-CryptoPro-B-ParamSet" +#define NID_id_GostR3410_2001_CryptoPro_B_ParamSet 841 +#define OBJ_id_GostR3410_2001_CryptoPro_B_ParamSet OBJ_cryptopro,35L,2L + +#define SN_id_GostR3410_2001_CryptoPro_C_ParamSet "id-GostR3410-2001-CryptoPro-C-ParamSet" +#define NID_id_GostR3410_2001_CryptoPro_C_ParamSet 842 +#define OBJ_id_GostR3410_2001_CryptoPro_C_ParamSet OBJ_cryptopro,35L,3L + +#define SN_id_GostR3410_2001_CryptoPro_XchA_ParamSet "id-GostR3410-2001-CryptoPro-XchA-ParamSet" +#define NID_id_GostR3410_2001_CryptoPro_XchA_ParamSet 843 +#define OBJ_id_GostR3410_2001_CryptoPro_XchA_ParamSet OBJ_cryptopro,36L,0L + +#define SN_id_GostR3410_2001_CryptoPro_XchB_ParamSet "id-GostR3410-2001-CryptoPro-XchB-ParamSet" +#define NID_id_GostR3410_2001_CryptoPro_XchB_ParamSet 844 +#define OBJ_id_GostR3410_2001_CryptoPro_XchB_ParamSet OBJ_cryptopro,36L,1L + +#define SN_id_GostR3410_94_a "id-GostR3410-94-a" +#define NID_id_GostR3410_94_a 845 +#define OBJ_id_GostR3410_94_a OBJ_id_GostR3410_94,1L + +#define SN_id_GostR3410_94_aBis "id-GostR3410-94-aBis" +#define NID_id_GostR3410_94_aBis 846 +#define OBJ_id_GostR3410_94_aBis OBJ_id_GostR3410_94,2L + +#define SN_id_GostR3410_94_b "id-GostR3410-94-b" +#define NID_id_GostR3410_94_b 847 +#define OBJ_id_GostR3410_94_b OBJ_id_GostR3410_94,3L + +#define SN_id_GostR3410_94_bBis "id-GostR3410-94-bBis" +#define NID_id_GostR3410_94_bBis 848 +#define OBJ_id_GostR3410_94_bBis OBJ_id_GostR3410_94,4L + +#define SN_id_Gost28147_89_cc "id-Gost28147-89-cc" +#define LN_id_Gost28147_89_cc "GOST 28147-89 Cryptocom ParamSet" +#define NID_id_Gost28147_89_cc 849 +#define OBJ_id_Gost28147_89_cc OBJ_cryptocom,1L,6L,1L + +#define SN_id_GostR3410_94_cc "gost94cc" +#define LN_id_GostR3410_94_cc "GOST 34.10-94 Cryptocom" +#define NID_id_GostR3410_94_cc 850 +#define OBJ_id_GostR3410_94_cc OBJ_cryptocom,1L,5L,3L + +#define SN_id_GostR3410_2001_cc "gost2001cc" +#define LN_id_GostR3410_2001_cc "GOST 34.10-2001 Cryptocom" +#define NID_id_GostR3410_2001_cc 851 +#define OBJ_id_GostR3410_2001_cc OBJ_cryptocom,1L,5L,4L + +#define SN_id_GostR3411_94_with_GostR3410_94_cc "id-GostR3411-94-with-GostR3410-94-cc" +#define LN_id_GostR3411_94_with_GostR3410_94_cc "GOST R 34.11-94 with GOST R 34.10-94 Cryptocom" +#define NID_id_GostR3411_94_with_GostR3410_94_cc 852 +#define OBJ_id_GostR3411_94_with_GostR3410_94_cc OBJ_cryptocom,1L,3L,3L + +#define SN_id_GostR3411_94_with_GostR3410_2001_cc "id-GostR3411-94-with-GostR3410-2001-cc" +#define LN_id_GostR3411_94_with_GostR3410_2001_cc "GOST R 34.11-94 with GOST R 34.10-2001 Cryptocom" +#define NID_id_GostR3411_94_with_GostR3410_2001_cc 853 +#define OBJ_id_GostR3411_94_with_GostR3410_2001_cc OBJ_cryptocom,1L,3L,4L + +#define SN_id_GostR3410_2001_ParamSet_cc "id-GostR3410-2001-ParamSet-cc" +#define LN_id_GostR3410_2001_ParamSet_cc "GOST R 3410-2001 Parameter Set Cryptocom" +#define NID_id_GostR3410_2001_ParamSet_cc 854 +#define OBJ_id_GostR3410_2001_ParamSet_cc OBJ_cryptocom,1L,8L,1L + +#define SN_id_tc26_algorithms "id-tc26-algorithms" +#define NID_id_tc26_algorithms 977 +#define OBJ_id_tc26_algorithms OBJ_id_tc26,1L + +#define SN_id_tc26_sign "id-tc26-sign" +#define NID_id_tc26_sign 978 +#define OBJ_id_tc26_sign OBJ_id_tc26_algorithms,1L + +#define SN_id_GostR3410_2012_256 "gost2012_256" +#define LN_id_GostR3410_2012_256 "GOST R 34.10-2012 with 256 bit modulus" +#define NID_id_GostR3410_2012_256 979 +#define OBJ_id_GostR3410_2012_256 OBJ_id_tc26_sign,1L + +#define SN_id_GostR3410_2012_512 "gost2012_512" +#define LN_id_GostR3410_2012_512 "GOST R 34.10-2012 with 512 bit modulus" +#define NID_id_GostR3410_2012_512 980 +#define OBJ_id_GostR3410_2012_512 OBJ_id_tc26_sign,2L + +#define SN_id_tc26_digest "id-tc26-digest" +#define NID_id_tc26_digest 981 +#define OBJ_id_tc26_digest OBJ_id_tc26_algorithms,2L + +#define SN_id_GostR3411_2012_256 "md_gost12_256" +#define LN_id_GostR3411_2012_256 "GOST R 34.11-2012 with 256 bit hash" +#define NID_id_GostR3411_2012_256 982 +#define OBJ_id_GostR3411_2012_256 OBJ_id_tc26_digest,2L + +#define SN_id_GostR3411_2012_512 "md_gost12_512" +#define LN_id_GostR3411_2012_512 "GOST R 34.11-2012 with 512 bit hash" +#define NID_id_GostR3411_2012_512 983 +#define OBJ_id_GostR3411_2012_512 OBJ_id_tc26_digest,3L + +#define SN_id_tc26_signwithdigest "id-tc26-signwithdigest" +#define NID_id_tc26_signwithdigest 984 +#define OBJ_id_tc26_signwithdigest OBJ_id_tc26_algorithms,3L + +#define SN_id_tc26_signwithdigest_gost3410_2012_256 "id-tc26-signwithdigest-gost3410-2012-256" +#define LN_id_tc26_signwithdigest_gost3410_2012_256 "GOST R 34.10-2012 with GOST R 34.11-2012 (256 bit)" +#define NID_id_tc26_signwithdigest_gost3410_2012_256 985 +#define OBJ_id_tc26_signwithdigest_gost3410_2012_256 OBJ_id_tc26_signwithdigest,2L + +#define SN_id_tc26_signwithdigest_gost3410_2012_512 "id-tc26-signwithdigest-gost3410-2012-512" +#define LN_id_tc26_signwithdigest_gost3410_2012_512 "GOST R 34.10-2012 with GOST R 34.11-2012 (512 bit)" +#define NID_id_tc26_signwithdigest_gost3410_2012_512 986 +#define OBJ_id_tc26_signwithdigest_gost3410_2012_512 OBJ_id_tc26_signwithdigest,3L + +#define SN_id_tc26_mac "id-tc26-mac" +#define NID_id_tc26_mac 987 +#define OBJ_id_tc26_mac OBJ_id_tc26_algorithms,4L + +#define SN_id_tc26_hmac_gost_3411_2012_256 "id-tc26-hmac-gost-3411-2012-256" +#define LN_id_tc26_hmac_gost_3411_2012_256 "HMAC GOST 34.11-2012 256 bit" +#define NID_id_tc26_hmac_gost_3411_2012_256 988 +#define OBJ_id_tc26_hmac_gost_3411_2012_256 OBJ_id_tc26_mac,1L + +#define SN_id_tc26_hmac_gost_3411_2012_512 "id-tc26-hmac-gost-3411-2012-512" +#define LN_id_tc26_hmac_gost_3411_2012_512 "HMAC GOST 34.11-2012 512 bit" +#define NID_id_tc26_hmac_gost_3411_2012_512 989 +#define OBJ_id_tc26_hmac_gost_3411_2012_512 OBJ_id_tc26_mac,2L + +#define SN_id_tc26_cipher "id-tc26-cipher" +#define NID_id_tc26_cipher 990 +#define OBJ_id_tc26_cipher OBJ_id_tc26_algorithms,5L + +#define SN_id_tc26_cipher_gostr3412_2015_magma "id-tc26-cipher-gostr3412-2015-magma" +#define NID_id_tc26_cipher_gostr3412_2015_magma 1173 +#define OBJ_id_tc26_cipher_gostr3412_2015_magma OBJ_id_tc26_cipher,1L + +#define SN_id_tc26_cipher_gostr3412_2015_magma_ctracpkm "id-tc26-cipher-gostr3412-2015-magma-ctracpkm" +#define NID_id_tc26_cipher_gostr3412_2015_magma_ctracpkm 1174 +#define OBJ_id_tc26_cipher_gostr3412_2015_magma_ctracpkm OBJ_id_tc26_cipher_gostr3412_2015_magma,1L + +#define SN_id_tc26_cipher_gostr3412_2015_magma_ctracpkm_omac "id-tc26-cipher-gostr3412-2015-magma-ctracpkm-omac" +#define NID_id_tc26_cipher_gostr3412_2015_magma_ctracpkm_omac 1175 +#define OBJ_id_tc26_cipher_gostr3412_2015_magma_ctracpkm_omac OBJ_id_tc26_cipher_gostr3412_2015_magma,2L + +#define SN_id_tc26_cipher_gostr3412_2015_kuznyechik "id-tc26-cipher-gostr3412-2015-kuznyechik" +#define NID_id_tc26_cipher_gostr3412_2015_kuznyechik 1176 +#define OBJ_id_tc26_cipher_gostr3412_2015_kuznyechik OBJ_id_tc26_cipher,2L + +#define SN_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm "id-tc26-cipher-gostr3412-2015-kuznyechik-ctracpkm" +#define NID_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm 1177 +#define OBJ_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm OBJ_id_tc26_cipher_gostr3412_2015_kuznyechik,1L + +#define SN_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm_omac "id-tc26-cipher-gostr3412-2015-kuznyechik-ctracpkm-omac" +#define NID_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm_omac 1178 +#define OBJ_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm_omac OBJ_id_tc26_cipher_gostr3412_2015_kuznyechik,2L + +#define SN_id_tc26_agreement "id-tc26-agreement" +#define NID_id_tc26_agreement 991 +#define OBJ_id_tc26_agreement OBJ_id_tc26_algorithms,6L + +#define SN_id_tc26_agreement_gost_3410_2012_256 "id-tc26-agreement-gost-3410-2012-256" +#define NID_id_tc26_agreement_gost_3410_2012_256 992 +#define OBJ_id_tc26_agreement_gost_3410_2012_256 OBJ_id_tc26_agreement,1L + +#define SN_id_tc26_agreement_gost_3410_2012_512 "id-tc26-agreement-gost-3410-2012-512" +#define NID_id_tc26_agreement_gost_3410_2012_512 993 +#define OBJ_id_tc26_agreement_gost_3410_2012_512 OBJ_id_tc26_agreement,2L + +#define SN_id_tc26_wrap "id-tc26-wrap" +#define NID_id_tc26_wrap 1179 +#define OBJ_id_tc26_wrap OBJ_id_tc26_algorithms,7L + +#define SN_id_tc26_wrap_gostr3412_2015_magma "id-tc26-wrap-gostr3412-2015-magma" +#define NID_id_tc26_wrap_gostr3412_2015_magma 1180 +#define OBJ_id_tc26_wrap_gostr3412_2015_magma OBJ_id_tc26_wrap,1L + +#define SN_id_tc26_wrap_gostr3412_2015_magma_kexp15 "id-tc26-wrap-gostr3412-2015-magma-kexp15" +#define NID_id_tc26_wrap_gostr3412_2015_magma_kexp15 1181 +#define OBJ_id_tc26_wrap_gostr3412_2015_magma_kexp15 OBJ_id_tc26_wrap_gostr3412_2015_magma,1L + +#define SN_id_tc26_wrap_gostr3412_2015_kuznyechik "id-tc26-wrap-gostr3412-2015-kuznyechik" +#define NID_id_tc26_wrap_gostr3412_2015_kuznyechik 1182 +#define OBJ_id_tc26_wrap_gostr3412_2015_kuznyechik OBJ_id_tc26_wrap,2L + +#define SN_id_tc26_wrap_gostr3412_2015_kuznyechik_kexp15 "id-tc26-wrap-gostr3412-2015-kuznyechik-kexp15" +#define NID_id_tc26_wrap_gostr3412_2015_kuznyechik_kexp15 1183 +#define OBJ_id_tc26_wrap_gostr3412_2015_kuznyechik_kexp15 OBJ_id_tc26_wrap_gostr3412_2015_magma,1L + +#define SN_id_tc26_constants "id-tc26-constants" +#define NID_id_tc26_constants 994 +#define OBJ_id_tc26_constants OBJ_id_tc26,2L + +#define SN_id_tc26_sign_constants "id-tc26-sign-constants" +#define NID_id_tc26_sign_constants 995 +#define OBJ_id_tc26_sign_constants OBJ_id_tc26_constants,1L + +#define SN_id_tc26_gost_3410_2012_256_constants "id-tc26-gost-3410-2012-256-constants" +#define NID_id_tc26_gost_3410_2012_256_constants 1147 +#define OBJ_id_tc26_gost_3410_2012_256_constants OBJ_id_tc26_sign_constants,1L + +#define SN_id_tc26_gost_3410_2012_256_paramSetA "id-tc26-gost-3410-2012-256-paramSetA" +#define LN_id_tc26_gost_3410_2012_256_paramSetA "GOST R 34.10-2012 (256 bit) ParamSet A" +#define NID_id_tc26_gost_3410_2012_256_paramSetA 1148 +#define OBJ_id_tc26_gost_3410_2012_256_paramSetA OBJ_id_tc26_gost_3410_2012_256_constants,1L + +#define SN_id_tc26_gost_3410_2012_256_paramSetB "id-tc26-gost-3410-2012-256-paramSetB" +#define LN_id_tc26_gost_3410_2012_256_paramSetB "GOST R 34.10-2012 (256 bit) ParamSet B" +#define NID_id_tc26_gost_3410_2012_256_paramSetB 1184 +#define OBJ_id_tc26_gost_3410_2012_256_paramSetB OBJ_id_tc26_gost_3410_2012_256_constants,2L + +#define SN_id_tc26_gost_3410_2012_256_paramSetC "id-tc26-gost-3410-2012-256-paramSetC" +#define LN_id_tc26_gost_3410_2012_256_paramSetC "GOST R 34.10-2012 (256 bit) ParamSet C" +#define NID_id_tc26_gost_3410_2012_256_paramSetC 1185 +#define OBJ_id_tc26_gost_3410_2012_256_paramSetC OBJ_id_tc26_gost_3410_2012_256_constants,3L + +#define SN_id_tc26_gost_3410_2012_256_paramSetD "id-tc26-gost-3410-2012-256-paramSetD" +#define LN_id_tc26_gost_3410_2012_256_paramSetD "GOST R 34.10-2012 (256 bit) ParamSet D" +#define NID_id_tc26_gost_3410_2012_256_paramSetD 1186 +#define OBJ_id_tc26_gost_3410_2012_256_paramSetD OBJ_id_tc26_gost_3410_2012_256_constants,4L + +#define SN_id_tc26_gost_3410_2012_512_constants "id-tc26-gost-3410-2012-512-constants" +#define NID_id_tc26_gost_3410_2012_512_constants 996 +#define OBJ_id_tc26_gost_3410_2012_512_constants OBJ_id_tc26_sign_constants,2L + +#define SN_id_tc26_gost_3410_2012_512_paramSetTest "id-tc26-gost-3410-2012-512-paramSetTest" +#define LN_id_tc26_gost_3410_2012_512_paramSetTest "GOST R 34.10-2012 (512 bit) testing parameter set" +#define NID_id_tc26_gost_3410_2012_512_paramSetTest 997 +#define OBJ_id_tc26_gost_3410_2012_512_paramSetTest OBJ_id_tc26_gost_3410_2012_512_constants,0L + +#define SN_id_tc26_gost_3410_2012_512_paramSetA "id-tc26-gost-3410-2012-512-paramSetA" +#define LN_id_tc26_gost_3410_2012_512_paramSetA "GOST R 34.10-2012 (512 bit) ParamSet A" +#define NID_id_tc26_gost_3410_2012_512_paramSetA 998 +#define OBJ_id_tc26_gost_3410_2012_512_paramSetA OBJ_id_tc26_gost_3410_2012_512_constants,1L + +#define SN_id_tc26_gost_3410_2012_512_paramSetB "id-tc26-gost-3410-2012-512-paramSetB" +#define LN_id_tc26_gost_3410_2012_512_paramSetB "GOST R 34.10-2012 (512 bit) ParamSet B" +#define NID_id_tc26_gost_3410_2012_512_paramSetB 999 +#define OBJ_id_tc26_gost_3410_2012_512_paramSetB OBJ_id_tc26_gost_3410_2012_512_constants,2L + +#define SN_id_tc26_gost_3410_2012_512_paramSetC "id-tc26-gost-3410-2012-512-paramSetC" +#define LN_id_tc26_gost_3410_2012_512_paramSetC "GOST R 34.10-2012 (512 bit) ParamSet C" +#define NID_id_tc26_gost_3410_2012_512_paramSetC 1149 +#define OBJ_id_tc26_gost_3410_2012_512_paramSetC OBJ_id_tc26_gost_3410_2012_512_constants,3L + +#define SN_id_tc26_digest_constants "id-tc26-digest-constants" +#define NID_id_tc26_digest_constants 1000 +#define OBJ_id_tc26_digest_constants OBJ_id_tc26_constants,2L + +#define SN_id_tc26_cipher_constants "id-tc26-cipher-constants" +#define NID_id_tc26_cipher_constants 1001 +#define OBJ_id_tc26_cipher_constants OBJ_id_tc26_constants,5L + +#define SN_id_tc26_gost_28147_constants "id-tc26-gost-28147-constants" +#define NID_id_tc26_gost_28147_constants 1002 +#define OBJ_id_tc26_gost_28147_constants OBJ_id_tc26_cipher_constants,1L + +#define SN_id_tc26_gost_28147_param_Z "id-tc26-gost-28147-param-Z" +#define LN_id_tc26_gost_28147_param_Z "GOST 28147-89 TC26 parameter set" +#define NID_id_tc26_gost_28147_param_Z 1003 +#define OBJ_id_tc26_gost_28147_param_Z OBJ_id_tc26_gost_28147_constants,1L + +#define SN_INN "INN" +#define LN_INN "INN" +#define NID_INN 1004 +#define OBJ_INN OBJ_member_body,643L,3L,131L,1L,1L + +#define SN_OGRN "OGRN" +#define LN_OGRN "OGRN" +#define NID_OGRN 1005 +#define OBJ_OGRN OBJ_member_body,643L,100L,1L + +#define SN_SNILS "SNILS" +#define LN_SNILS "SNILS" +#define NID_SNILS 1006 +#define OBJ_SNILS OBJ_member_body,643L,100L,3L + +#define SN_subjectSignTool "subjectSignTool" +#define LN_subjectSignTool "Signing Tool of Subject" +#define NID_subjectSignTool 1007 +#define OBJ_subjectSignTool OBJ_member_body,643L,100L,111L + +#define SN_issuerSignTool "issuerSignTool" +#define LN_issuerSignTool "Signing Tool of Issuer" +#define NID_issuerSignTool 1008 +#define OBJ_issuerSignTool OBJ_member_body,643L,100L,112L + +#define SN_grasshopper_ecb "grasshopper-ecb" +#define NID_grasshopper_ecb 1012 + +#define SN_grasshopper_ctr "grasshopper-ctr" +#define NID_grasshopper_ctr 1013 + +#define SN_grasshopper_ofb "grasshopper-ofb" +#define NID_grasshopper_ofb 1014 + +#define SN_grasshopper_cbc "grasshopper-cbc" +#define NID_grasshopper_cbc 1015 + +#define SN_grasshopper_cfb "grasshopper-cfb" +#define NID_grasshopper_cfb 1016 + +#define SN_grasshopper_mac "grasshopper-mac" +#define NID_grasshopper_mac 1017 + +#define SN_magma_ecb "magma-ecb" +#define NID_magma_ecb 1187 + +#define SN_magma_ctr "magma-ctr" +#define NID_magma_ctr 1188 + +#define SN_magma_ofb "magma-ofb" +#define NID_magma_ofb 1189 + +#define SN_magma_cbc "magma-cbc" +#define NID_magma_cbc 1190 + +#define SN_magma_cfb "magma-cfb" +#define NID_magma_cfb 1191 + +#define SN_magma_mac "magma-mac" +#define NID_magma_mac 1192 + +#define SN_camellia_128_cbc "CAMELLIA-128-CBC" +#define LN_camellia_128_cbc "camellia-128-cbc" +#define NID_camellia_128_cbc 751 +#define OBJ_camellia_128_cbc 1L,2L,392L,200011L,61L,1L,1L,1L,2L + +#define SN_camellia_192_cbc "CAMELLIA-192-CBC" +#define LN_camellia_192_cbc "camellia-192-cbc" +#define NID_camellia_192_cbc 752 +#define OBJ_camellia_192_cbc 1L,2L,392L,200011L,61L,1L,1L,1L,3L + +#define SN_camellia_256_cbc "CAMELLIA-256-CBC" +#define LN_camellia_256_cbc "camellia-256-cbc" +#define NID_camellia_256_cbc 753 +#define OBJ_camellia_256_cbc 1L,2L,392L,200011L,61L,1L,1L,1L,4L + +#define SN_id_camellia128_wrap "id-camellia128-wrap" +#define NID_id_camellia128_wrap 907 +#define OBJ_id_camellia128_wrap 1L,2L,392L,200011L,61L,1L,1L,3L,2L + +#define SN_id_camellia192_wrap "id-camellia192-wrap" +#define NID_id_camellia192_wrap 908 +#define OBJ_id_camellia192_wrap 1L,2L,392L,200011L,61L,1L,1L,3L,3L + +#define SN_id_camellia256_wrap "id-camellia256-wrap" +#define NID_id_camellia256_wrap 909 +#define OBJ_id_camellia256_wrap 1L,2L,392L,200011L,61L,1L,1L,3L,4L + +#define OBJ_ntt_ds 0L,3L,4401L,5L + +#define OBJ_camellia OBJ_ntt_ds,3L,1L,9L + +#define SN_camellia_128_ecb "CAMELLIA-128-ECB" +#define LN_camellia_128_ecb "camellia-128-ecb" +#define NID_camellia_128_ecb 754 +#define OBJ_camellia_128_ecb OBJ_camellia,1L + +#define SN_camellia_128_ofb128 "CAMELLIA-128-OFB" +#define LN_camellia_128_ofb128 "camellia-128-ofb" +#define NID_camellia_128_ofb128 766 +#define OBJ_camellia_128_ofb128 OBJ_camellia,3L + +#define SN_camellia_128_cfb128 "CAMELLIA-128-CFB" +#define LN_camellia_128_cfb128 "camellia-128-cfb" +#define NID_camellia_128_cfb128 757 +#define OBJ_camellia_128_cfb128 OBJ_camellia,4L + +#define SN_camellia_128_gcm "CAMELLIA-128-GCM" +#define LN_camellia_128_gcm "camellia-128-gcm" +#define NID_camellia_128_gcm 961 +#define OBJ_camellia_128_gcm OBJ_camellia,6L + +#define SN_camellia_128_ccm "CAMELLIA-128-CCM" +#define LN_camellia_128_ccm "camellia-128-ccm" +#define NID_camellia_128_ccm 962 +#define OBJ_camellia_128_ccm OBJ_camellia,7L + +#define SN_camellia_128_ctr "CAMELLIA-128-CTR" +#define LN_camellia_128_ctr "camellia-128-ctr" +#define NID_camellia_128_ctr 963 +#define OBJ_camellia_128_ctr OBJ_camellia,9L + +#define SN_camellia_128_cmac "CAMELLIA-128-CMAC" +#define LN_camellia_128_cmac "camellia-128-cmac" +#define NID_camellia_128_cmac 964 +#define OBJ_camellia_128_cmac OBJ_camellia,10L + +#define SN_camellia_192_ecb "CAMELLIA-192-ECB" +#define LN_camellia_192_ecb "camellia-192-ecb" +#define NID_camellia_192_ecb 755 +#define OBJ_camellia_192_ecb OBJ_camellia,21L + +#define SN_camellia_192_ofb128 "CAMELLIA-192-OFB" +#define LN_camellia_192_ofb128 "camellia-192-ofb" +#define NID_camellia_192_ofb128 767 +#define OBJ_camellia_192_ofb128 OBJ_camellia,23L + +#define SN_camellia_192_cfb128 "CAMELLIA-192-CFB" +#define LN_camellia_192_cfb128 "camellia-192-cfb" +#define NID_camellia_192_cfb128 758 +#define OBJ_camellia_192_cfb128 OBJ_camellia,24L + +#define SN_camellia_192_gcm "CAMELLIA-192-GCM" +#define LN_camellia_192_gcm "camellia-192-gcm" +#define NID_camellia_192_gcm 965 +#define OBJ_camellia_192_gcm OBJ_camellia,26L + +#define SN_camellia_192_ccm "CAMELLIA-192-CCM" +#define LN_camellia_192_ccm "camellia-192-ccm" +#define NID_camellia_192_ccm 966 +#define OBJ_camellia_192_ccm OBJ_camellia,27L + +#define SN_camellia_192_ctr "CAMELLIA-192-CTR" +#define LN_camellia_192_ctr "camellia-192-ctr" +#define NID_camellia_192_ctr 967 +#define OBJ_camellia_192_ctr OBJ_camellia,29L + +#define SN_camellia_192_cmac "CAMELLIA-192-CMAC" +#define LN_camellia_192_cmac "camellia-192-cmac" +#define NID_camellia_192_cmac 968 +#define OBJ_camellia_192_cmac OBJ_camellia,30L + +#define SN_camellia_256_ecb "CAMELLIA-256-ECB" +#define LN_camellia_256_ecb "camellia-256-ecb" +#define NID_camellia_256_ecb 756 +#define OBJ_camellia_256_ecb OBJ_camellia,41L + +#define SN_camellia_256_ofb128 "CAMELLIA-256-OFB" +#define LN_camellia_256_ofb128 "camellia-256-ofb" +#define NID_camellia_256_ofb128 768 +#define OBJ_camellia_256_ofb128 OBJ_camellia,43L + +#define SN_camellia_256_cfb128 "CAMELLIA-256-CFB" +#define LN_camellia_256_cfb128 "camellia-256-cfb" +#define NID_camellia_256_cfb128 759 +#define OBJ_camellia_256_cfb128 OBJ_camellia,44L + +#define SN_camellia_256_gcm "CAMELLIA-256-GCM" +#define LN_camellia_256_gcm "camellia-256-gcm" +#define NID_camellia_256_gcm 969 +#define OBJ_camellia_256_gcm OBJ_camellia,46L + +#define SN_camellia_256_ccm "CAMELLIA-256-CCM" +#define LN_camellia_256_ccm "camellia-256-ccm" +#define NID_camellia_256_ccm 970 +#define OBJ_camellia_256_ccm OBJ_camellia,47L + +#define SN_camellia_256_ctr "CAMELLIA-256-CTR" +#define LN_camellia_256_ctr "camellia-256-ctr" +#define NID_camellia_256_ctr 971 +#define OBJ_camellia_256_ctr OBJ_camellia,49L + +#define SN_camellia_256_cmac "CAMELLIA-256-CMAC" +#define LN_camellia_256_cmac "camellia-256-cmac" +#define NID_camellia_256_cmac 972 +#define OBJ_camellia_256_cmac OBJ_camellia,50L + +#define SN_camellia_128_cfb1 "CAMELLIA-128-CFB1" +#define LN_camellia_128_cfb1 "camellia-128-cfb1" +#define NID_camellia_128_cfb1 760 + +#define SN_camellia_192_cfb1 "CAMELLIA-192-CFB1" +#define LN_camellia_192_cfb1 "camellia-192-cfb1" +#define NID_camellia_192_cfb1 761 + +#define SN_camellia_256_cfb1 "CAMELLIA-256-CFB1" +#define LN_camellia_256_cfb1 "camellia-256-cfb1" +#define NID_camellia_256_cfb1 762 + +#define SN_camellia_128_cfb8 "CAMELLIA-128-CFB8" +#define LN_camellia_128_cfb8 "camellia-128-cfb8" +#define NID_camellia_128_cfb8 763 + +#define SN_camellia_192_cfb8 "CAMELLIA-192-CFB8" +#define LN_camellia_192_cfb8 "camellia-192-cfb8" +#define NID_camellia_192_cfb8 764 + +#define SN_camellia_256_cfb8 "CAMELLIA-256-CFB8" +#define LN_camellia_256_cfb8 "camellia-256-cfb8" +#define NID_camellia_256_cfb8 765 + +#define OBJ_aria 1L,2L,410L,200046L,1L,1L + +#define SN_aria_128_ecb "ARIA-128-ECB" +#define LN_aria_128_ecb "aria-128-ecb" +#define NID_aria_128_ecb 1065 +#define OBJ_aria_128_ecb OBJ_aria,1L + +#define SN_aria_128_cbc "ARIA-128-CBC" +#define LN_aria_128_cbc "aria-128-cbc" +#define NID_aria_128_cbc 1066 +#define OBJ_aria_128_cbc OBJ_aria,2L + +#define SN_aria_128_cfb128 "ARIA-128-CFB" +#define LN_aria_128_cfb128 "aria-128-cfb" +#define NID_aria_128_cfb128 1067 +#define OBJ_aria_128_cfb128 OBJ_aria,3L + +#define SN_aria_128_ofb128 "ARIA-128-OFB" +#define LN_aria_128_ofb128 "aria-128-ofb" +#define NID_aria_128_ofb128 1068 +#define OBJ_aria_128_ofb128 OBJ_aria,4L + +#define SN_aria_128_ctr "ARIA-128-CTR" +#define LN_aria_128_ctr "aria-128-ctr" +#define NID_aria_128_ctr 1069 +#define OBJ_aria_128_ctr OBJ_aria,5L + +#define SN_aria_192_ecb "ARIA-192-ECB" +#define LN_aria_192_ecb "aria-192-ecb" +#define NID_aria_192_ecb 1070 +#define OBJ_aria_192_ecb OBJ_aria,6L + +#define SN_aria_192_cbc "ARIA-192-CBC" +#define LN_aria_192_cbc "aria-192-cbc" +#define NID_aria_192_cbc 1071 +#define OBJ_aria_192_cbc OBJ_aria,7L + +#define SN_aria_192_cfb128 "ARIA-192-CFB" +#define LN_aria_192_cfb128 "aria-192-cfb" +#define NID_aria_192_cfb128 1072 +#define OBJ_aria_192_cfb128 OBJ_aria,8L + +#define SN_aria_192_ofb128 "ARIA-192-OFB" +#define LN_aria_192_ofb128 "aria-192-ofb" +#define NID_aria_192_ofb128 1073 +#define OBJ_aria_192_ofb128 OBJ_aria,9L + +#define SN_aria_192_ctr "ARIA-192-CTR" +#define LN_aria_192_ctr "aria-192-ctr" +#define NID_aria_192_ctr 1074 +#define OBJ_aria_192_ctr OBJ_aria,10L + +#define SN_aria_256_ecb "ARIA-256-ECB" +#define LN_aria_256_ecb "aria-256-ecb" +#define NID_aria_256_ecb 1075 +#define OBJ_aria_256_ecb OBJ_aria,11L + +#define SN_aria_256_cbc "ARIA-256-CBC" +#define LN_aria_256_cbc "aria-256-cbc" +#define NID_aria_256_cbc 1076 +#define OBJ_aria_256_cbc OBJ_aria,12L + +#define SN_aria_256_cfb128 "ARIA-256-CFB" +#define LN_aria_256_cfb128 "aria-256-cfb" +#define NID_aria_256_cfb128 1077 +#define OBJ_aria_256_cfb128 OBJ_aria,13L + +#define SN_aria_256_ofb128 "ARIA-256-OFB" +#define LN_aria_256_ofb128 "aria-256-ofb" +#define NID_aria_256_ofb128 1078 +#define OBJ_aria_256_ofb128 OBJ_aria,14L + +#define SN_aria_256_ctr "ARIA-256-CTR" +#define LN_aria_256_ctr "aria-256-ctr" +#define NID_aria_256_ctr 1079 +#define OBJ_aria_256_ctr OBJ_aria,15L + +#define SN_aria_128_cfb1 "ARIA-128-CFB1" +#define LN_aria_128_cfb1 "aria-128-cfb1" +#define NID_aria_128_cfb1 1080 + +#define SN_aria_192_cfb1 "ARIA-192-CFB1" +#define LN_aria_192_cfb1 "aria-192-cfb1" +#define NID_aria_192_cfb1 1081 + +#define SN_aria_256_cfb1 "ARIA-256-CFB1" +#define LN_aria_256_cfb1 "aria-256-cfb1" +#define NID_aria_256_cfb1 1082 + +#define SN_aria_128_cfb8 "ARIA-128-CFB8" +#define LN_aria_128_cfb8 "aria-128-cfb8" +#define NID_aria_128_cfb8 1083 + +#define SN_aria_192_cfb8 "ARIA-192-CFB8" +#define LN_aria_192_cfb8 "aria-192-cfb8" +#define NID_aria_192_cfb8 1084 + +#define SN_aria_256_cfb8 "ARIA-256-CFB8" +#define LN_aria_256_cfb8 "aria-256-cfb8" +#define NID_aria_256_cfb8 1085 + +#define SN_aria_128_ccm "ARIA-128-CCM" +#define LN_aria_128_ccm "aria-128-ccm" +#define NID_aria_128_ccm 1120 +#define OBJ_aria_128_ccm OBJ_aria,37L + +#define SN_aria_192_ccm "ARIA-192-CCM" +#define LN_aria_192_ccm "aria-192-ccm" +#define NID_aria_192_ccm 1121 +#define OBJ_aria_192_ccm OBJ_aria,38L + +#define SN_aria_256_ccm "ARIA-256-CCM" +#define LN_aria_256_ccm "aria-256-ccm" +#define NID_aria_256_ccm 1122 +#define OBJ_aria_256_ccm OBJ_aria,39L + +#define SN_aria_128_gcm "ARIA-128-GCM" +#define LN_aria_128_gcm "aria-128-gcm" +#define NID_aria_128_gcm 1123 +#define OBJ_aria_128_gcm OBJ_aria,34L + +#define SN_aria_192_gcm "ARIA-192-GCM" +#define LN_aria_192_gcm "aria-192-gcm" +#define NID_aria_192_gcm 1124 +#define OBJ_aria_192_gcm OBJ_aria,35L + +#define SN_aria_256_gcm "ARIA-256-GCM" +#define LN_aria_256_gcm "aria-256-gcm" +#define NID_aria_256_gcm 1125 +#define OBJ_aria_256_gcm OBJ_aria,36L + +#define SN_kisa "KISA" +#define LN_kisa "kisa" +#define NID_kisa 773 +#define OBJ_kisa OBJ_member_body,410L,200004L + +#define SN_seed_ecb "SEED-ECB" +#define LN_seed_ecb "seed-ecb" +#define NID_seed_ecb 776 +#define OBJ_seed_ecb OBJ_kisa,1L,3L + +#define SN_seed_cbc "SEED-CBC" +#define LN_seed_cbc "seed-cbc" +#define NID_seed_cbc 777 +#define OBJ_seed_cbc OBJ_kisa,1L,4L + +#define SN_seed_cfb128 "SEED-CFB" +#define LN_seed_cfb128 "seed-cfb" +#define NID_seed_cfb128 779 +#define OBJ_seed_cfb128 OBJ_kisa,1L,5L + +#define SN_seed_ofb128 "SEED-OFB" +#define LN_seed_ofb128 "seed-ofb" +#define NID_seed_ofb128 778 +#define OBJ_seed_ofb128 OBJ_kisa,1L,6L + +#define SN_sm4_ecb "SM4-ECB" +#define LN_sm4_ecb "sm4-ecb" +#define NID_sm4_ecb 1133 +#define OBJ_sm4_ecb OBJ_sm_scheme,104L,1L + +#define SN_sm4_cbc "SM4-CBC" +#define LN_sm4_cbc "sm4-cbc" +#define NID_sm4_cbc 1134 +#define OBJ_sm4_cbc OBJ_sm_scheme,104L,2L + +#define SN_sm4_ofb128 "SM4-OFB" +#define LN_sm4_ofb128 "sm4-ofb" +#define NID_sm4_ofb128 1135 +#define OBJ_sm4_ofb128 OBJ_sm_scheme,104L,3L + +#define SN_sm4_cfb128 "SM4-CFB" +#define LN_sm4_cfb128 "sm4-cfb" +#define NID_sm4_cfb128 1137 +#define OBJ_sm4_cfb128 OBJ_sm_scheme,104L,4L + +#define SN_sm4_cfb1 "SM4-CFB1" +#define LN_sm4_cfb1 "sm4-cfb1" +#define NID_sm4_cfb1 1136 +#define OBJ_sm4_cfb1 OBJ_sm_scheme,104L,5L + +#define SN_sm4_cfb8 "SM4-CFB8" +#define LN_sm4_cfb8 "sm4-cfb8" +#define NID_sm4_cfb8 1138 +#define OBJ_sm4_cfb8 OBJ_sm_scheme,104L,6L + +#define SN_sm4_ctr "SM4-CTR" +#define LN_sm4_ctr "sm4-ctr" +#define NID_sm4_ctr 1139 +#define OBJ_sm4_ctr OBJ_sm_scheme,104L,7L + +#define SN_hmac "HMAC" +#define LN_hmac "hmac" +#define NID_hmac 855 + +#define SN_cmac "CMAC" +#define LN_cmac "cmac" +#define NID_cmac 894 + +#define SN_rc4_hmac_md5 "RC4-HMAC-MD5" +#define LN_rc4_hmac_md5 "rc4-hmac-md5" +#define NID_rc4_hmac_md5 915 + +#define SN_aes_128_cbc_hmac_sha1 "AES-128-CBC-HMAC-SHA1" +#define LN_aes_128_cbc_hmac_sha1 "aes-128-cbc-hmac-sha1" +#define NID_aes_128_cbc_hmac_sha1 916 + +#define SN_aes_192_cbc_hmac_sha1 "AES-192-CBC-HMAC-SHA1" +#define LN_aes_192_cbc_hmac_sha1 "aes-192-cbc-hmac-sha1" +#define NID_aes_192_cbc_hmac_sha1 917 + +#define SN_aes_256_cbc_hmac_sha1 "AES-256-CBC-HMAC-SHA1" +#define LN_aes_256_cbc_hmac_sha1 "aes-256-cbc-hmac-sha1" +#define NID_aes_256_cbc_hmac_sha1 918 + +#define SN_aes_128_cbc_hmac_sha256 "AES-128-CBC-HMAC-SHA256" +#define LN_aes_128_cbc_hmac_sha256 "aes-128-cbc-hmac-sha256" +#define NID_aes_128_cbc_hmac_sha256 948 + +#define SN_aes_192_cbc_hmac_sha256 "AES-192-CBC-HMAC-SHA256" +#define LN_aes_192_cbc_hmac_sha256 "aes-192-cbc-hmac-sha256" +#define NID_aes_192_cbc_hmac_sha256 949 + +#define SN_aes_256_cbc_hmac_sha256 "AES-256-CBC-HMAC-SHA256" +#define LN_aes_256_cbc_hmac_sha256 "aes-256-cbc-hmac-sha256" +#define NID_aes_256_cbc_hmac_sha256 950 + +#define SN_chacha20_poly1305 "ChaCha20-Poly1305" +#define LN_chacha20_poly1305 "chacha20-poly1305" +#define NID_chacha20_poly1305 1018 + +#define SN_chacha20 "ChaCha20" +#define LN_chacha20 "chacha20" +#define NID_chacha20 1019 + +#define SN_dhpublicnumber "dhpublicnumber" +#define LN_dhpublicnumber "X9.42 DH" +#define NID_dhpublicnumber 920 +#define OBJ_dhpublicnumber OBJ_ISO_US,10046L,2L,1L + +#define SN_brainpoolP160r1 "brainpoolP160r1" +#define NID_brainpoolP160r1 921 +#define OBJ_brainpoolP160r1 1L,3L,36L,3L,3L,2L,8L,1L,1L,1L + +#define SN_brainpoolP160t1 "brainpoolP160t1" +#define NID_brainpoolP160t1 922 +#define OBJ_brainpoolP160t1 1L,3L,36L,3L,3L,2L,8L,1L,1L,2L + +#define SN_brainpoolP192r1 "brainpoolP192r1" +#define NID_brainpoolP192r1 923 +#define OBJ_brainpoolP192r1 1L,3L,36L,3L,3L,2L,8L,1L,1L,3L + +#define SN_brainpoolP192t1 "brainpoolP192t1" +#define NID_brainpoolP192t1 924 +#define OBJ_brainpoolP192t1 1L,3L,36L,3L,3L,2L,8L,1L,1L,4L + +#define SN_brainpoolP224r1 "brainpoolP224r1" +#define NID_brainpoolP224r1 925 +#define OBJ_brainpoolP224r1 1L,3L,36L,3L,3L,2L,8L,1L,1L,5L + +#define SN_brainpoolP224t1 "brainpoolP224t1" +#define NID_brainpoolP224t1 926 +#define OBJ_brainpoolP224t1 1L,3L,36L,3L,3L,2L,8L,1L,1L,6L + +#define SN_brainpoolP256r1 "brainpoolP256r1" +#define NID_brainpoolP256r1 927 +#define OBJ_brainpoolP256r1 1L,3L,36L,3L,3L,2L,8L,1L,1L,7L + +#define SN_brainpoolP256t1 "brainpoolP256t1" +#define NID_brainpoolP256t1 928 +#define OBJ_brainpoolP256t1 1L,3L,36L,3L,3L,2L,8L,1L,1L,8L + +#define SN_brainpoolP320r1 "brainpoolP320r1" +#define NID_brainpoolP320r1 929 +#define OBJ_brainpoolP320r1 1L,3L,36L,3L,3L,2L,8L,1L,1L,9L + +#define SN_brainpoolP320t1 "brainpoolP320t1" +#define NID_brainpoolP320t1 930 +#define OBJ_brainpoolP320t1 1L,3L,36L,3L,3L,2L,8L,1L,1L,10L + +#define SN_brainpoolP384r1 "brainpoolP384r1" +#define NID_brainpoolP384r1 931 +#define OBJ_brainpoolP384r1 1L,3L,36L,3L,3L,2L,8L,1L,1L,11L + +#define SN_brainpoolP384t1 "brainpoolP384t1" +#define NID_brainpoolP384t1 932 +#define OBJ_brainpoolP384t1 1L,3L,36L,3L,3L,2L,8L,1L,1L,12L + +#define SN_brainpoolP512r1 "brainpoolP512r1" +#define NID_brainpoolP512r1 933 +#define OBJ_brainpoolP512r1 1L,3L,36L,3L,3L,2L,8L,1L,1L,13L + +#define SN_brainpoolP512t1 "brainpoolP512t1" +#define NID_brainpoolP512t1 934 +#define OBJ_brainpoolP512t1 1L,3L,36L,3L,3L,2L,8L,1L,1L,14L + +#define OBJ_x9_63_scheme 1L,3L,133L,16L,840L,63L,0L + +#define OBJ_secg_scheme OBJ_certicom_arc,1L + +#define SN_dhSinglePass_stdDH_sha1kdf_scheme "dhSinglePass-stdDH-sha1kdf-scheme" +#define NID_dhSinglePass_stdDH_sha1kdf_scheme 936 +#define OBJ_dhSinglePass_stdDH_sha1kdf_scheme OBJ_x9_63_scheme,2L + +#define SN_dhSinglePass_stdDH_sha224kdf_scheme "dhSinglePass-stdDH-sha224kdf-scheme" +#define NID_dhSinglePass_stdDH_sha224kdf_scheme 937 +#define OBJ_dhSinglePass_stdDH_sha224kdf_scheme OBJ_secg_scheme,11L,0L + +#define SN_dhSinglePass_stdDH_sha256kdf_scheme "dhSinglePass-stdDH-sha256kdf-scheme" +#define NID_dhSinglePass_stdDH_sha256kdf_scheme 938 +#define OBJ_dhSinglePass_stdDH_sha256kdf_scheme OBJ_secg_scheme,11L,1L + +#define SN_dhSinglePass_stdDH_sha384kdf_scheme "dhSinglePass-stdDH-sha384kdf-scheme" +#define NID_dhSinglePass_stdDH_sha384kdf_scheme 939 +#define OBJ_dhSinglePass_stdDH_sha384kdf_scheme OBJ_secg_scheme,11L,2L + +#define SN_dhSinglePass_stdDH_sha512kdf_scheme "dhSinglePass-stdDH-sha512kdf-scheme" +#define NID_dhSinglePass_stdDH_sha512kdf_scheme 940 +#define OBJ_dhSinglePass_stdDH_sha512kdf_scheme OBJ_secg_scheme,11L,3L + +#define SN_dhSinglePass_cofactorDH_sha1kdf_scheme "dhSinglePass-cofactorDH-sha1kdf-scheme" +#define NID_dhSinglePass_cofactorDH_sha1kdf_scheme 941 +#define OBJ_dhSinglePass_cofactorDH_sha1kdf_scheme OBJ_x9_63_scheme,3L + +#define SN_dhSinglePass_cofactorDH_sha224kdf_scheme "dhSinglePass-cofactorDH-sha224kdf-scheme" +#define NID_dhSinglePass_cofactorDH_sha224kdf_scheme 942 +#define OBJ_dhSinglePass_cofactorDH_sha224kdf_scheme OBJ_secg_scheme,14L,0L + +#define SN_dhSinglePass_cofactorDH_sha256kdf_scheme "dhSinglePass-cofactorDH-sha256kdf-scheme" +#define NID_dhSinglePass_cofactorDH_sha256kdf_scheme 943 +#define OBJ_dhSinglePass_cofactorDH_sha256kdf_scheme OBJ_secg_scheme,14L,1L + +#define SN_dhSinglePass_cofactorDH_sha384kdf_scheme "dhSinglePass-cofactorDH-sha384kdf-scheme" +#define NID_dhSinglePass_cofactorDH_sha384kdf_scheme 944 +#define OBJ_dhSinglePass_cofactorDH_sha384kdf_scheme OBJ_secg_scheme,14L,2L + +#define SN_dhSinglePass_cofactorDH_sha512kdf_scheme "dhSinglePass-cofactorDH-sha512kdf-scheme" +#define NID_dhSinglePass_cofactorDH_sha512kdf_scheme 945 +#define OBJ_dhSinglePass_cofactorDH_sha512kdf_scheme OBJ_secg_scheme,14L,3L + +#define SN_dh_std_kdf "dh-std-kdf" +#define NID_dh_std_kdf 946 + +#define SN_dh_cofactor_kdf "dh-cofactor-kdf" +#define NID_dh_cofactor_kdf 947 + +#define SN_ct_precert_scts "ct_precert_scts" +#define LN_ct_precert_scts "CT Precertificate SCTs" +#define NID_ct_precert_scts 951 +#define OBJ_ct_precert_scts 1L,3L,6L,1L,4L,1L,11129L,2L,4L,2L + +#define SN_ct_precert_poison "ct_precert_poison" +#define LN_ct_precert_poison "CT Precertificate Poison" +#define NID_ct_precert_poison 952 +#define OBJ_ct_precert_poison 1L,3L,6L,1L,4L,1L,11129L,2L,4L,3L + +#define SN_ct_precert_signer "ct_precert_signer" +#define LN_ct_precert_signer "CT Precertificate Signer" +#define NID_ct_precert_signer 953 +#define OBJ_ct_precert_signer 1L,3L,6L,1L,4L,1L,11129L,2L,4L,4L + +#define SN_ct_cert_scts "ct_cert_scts" +#define LN_ct_cert_scts "CT Certificate SCTs" +#define NID_ct_cert_scts 954 +#define OBJ_ct_cert_scts 1L,3L,6L,1L,4L,1L,11129L,2L,4L,5L + +#define SN_jurisdictionLocalityName "jurisdictionL" +#define LN_jurisdictionLocalityName "jurisdictionLocalityName" +#define NID_jurisdictionLocalityName 955 +#define OBJ_jurisdictionLocalityName 1L,3L,6L,1L,4L,1L,311L,60L,2L,1L,1L + +#define SN_jurisdictionStateOrProvinceName "jurisdictionST" +#define LN_jurisdictionStateOrProvinceName "jurisdictionStateOrProvinceName" +#define NID_jurisdictionStateOrProvinceName 956 +#define OBJ_jurisdictionStateOrProvinceName 1L,3L,6L,1L,4L,1L,311L,60L,2L,1L,2L + +#define SN_jurisdictionCountryName "jurisdictionC" +#define LN_jurisdictionCountryName "jurisdictionCountryName" +#define NID_jurisdictionCountryName 957 +#define OBJ_jurisdictionCountryName 1L,3L,6L,1L,4L,1L,311L,60L,2L,1L,3L + +#define SN_id_scrypt "id-scrypt" +#define LN_id_scrypt "scrypt" +#define NID_id_scrypt 973 +#define OBJ_id_scrypt 1L,3L,6L,1L,4L,1L,11591L,4L,11L + +#define SN_tls1_prf "TLS1-PRF" +#define LN_tls1_prf "tls1-prf" +#define NID_tls1_prf 1021 + +#define SN_hkdf "HKDF" +#define LN_hkdf "hkdf" +#define NID_hkdf 1036 + +#define SN_id_pkinit "id-pkinit" +#define NID_id_pkinit 1031 +#define OBJ_id_pkinit 1L,3L,6L,1L,5L,2L,3L + +#define SN_pkInitClientAuth "pkInitClientAuth" +#define LN_pkInitClientAuth "PKINIT Client Auth" +#define NID_pkInitClientAuth 1032 +#define OBJ_pkInitClientAuth OBJ_id_pkinit,4L + +#define SN_pkInitKDC "pkInitKDC" +#define LN_pkInitKDC "Signing KDC Response" +#define NID_pkInitKDC 1033 +#define OBJ_pkInitKDC OBJ_id_pkinit,5L + +#define SN_X25519 "X25519" +#define NID_X25519 1034 +#define OBJ_X25519 1L,3L,101L,110L + +#define SN_X448 "X448" +#define NID_X448 1035 +#define OBJ_X448 1L,3L,101L,111L + +#define SN_ED25519 "ED25519" +#define NID_ED25519 1087 +#define OBJ_ED25519 1L,3L,101L,112L + +#define SN_ED448 "ED448" +#define NID_ED448 1088 +#define OBJ_ED448 1L,3L,101L,113L + +#define SN_kx_rsa "KxRSA" +#define LN_kx_rsa "kx-rsa" +#define NID_kx_rsa 1037 + +#define SN_kx_ecdhe "KxECDHE" +#define LN_kx_ecdhe "kx-ecdhe" +#define NID_kx_ecdhe 1038 + +#define SN_kx_dhe "KxDHE" +#define LN_kx_dhe "kx-dhe" +#define NID_kx_dhe 1039 + +#define SN_kx_ecdhe_psk "KxECDHE-PSK" +#define LN_kx_ecdhe_psk "kx-ecdhe-psk" +#define NID_kx_ecdhe_psk 1040 + +#define SN_kx_dhe_psk "KxDHE-PSK" +#define LN_kx_dhe_psk "kx-dhe-psk" +#define NID_kx_dhe_psk 1041 + +#define SN_kx_rsa_psk "KxRSA_PSK" +#define LN_kx_rsa_psk "kx-rsa-psk" +#define NID_kx_rsa_psk 1042 + +#define SN_kx_psk "KxPSK" +#define LN_kx_psk "kx-psk" +#define NID_kx_psk 1043 + +#define SN_kx_srp "KxSRP" +#define LN_kx_srp "kx-srp" +#define NID_kx_srp 1044 + +#define SN_kx_gost "KxGOST" +#define LN_kx_gost "kx-gost" +#define NID_kx_gost 1045 + +#define SN_kx_any "KxANY" +#define LN_kx_any "kx-any" +#define NID_kx_any 1063 + +#define SN_auth_rsa "AuthRSA" +#define LN_auth_rsa "auth-rsa" +#define NID_auth_rsa 1046 + +#define SN_auth_ecdsa "AuthECDSA" +#define LN_auth_ecdsa "auth-ecdsa" +#define NID_auth_ecdsa 1047 + +#define SN_auth_psk "AuthPSK" +#define LN_auth_psk "auth-psk" +#define NID_auth_psk 1048 + +#define SN_auth_dss "AuthDSS" +#define LN_auth_dss "auth-dss" +#define NID_auth_dss 1049 + +#define SN_auth_gost01 "AuthGOST01" +#define LN_auth_gost01 "auth-gost01" +#define NID_auth_gost01 1050 + +#define SN_auth_gost12 "AuthGOST12" +#define LN_auth_gost12 "auth-gost12" +#define NID_auth_gost12 1051 + +#define SN_auth_srp "AuthSRP" +#define LN_auth_srp "auth-srp" +#define NID_auth_srp 1052 + +#define SN_auth_null "AuthNULL" +#define LN_auth_null "auth-null" +#define NID_auth_null 1053 + +#define SN_auth_any "AuthANY" +#define LN_auth_any "auth-any" +#define NID_auth_any 1064 + +#define SN_poly1305 "Poly1305" +#define LN_poly1305 "poly1305" +#define NID_poly1305 1061 + +#define SN_siphash "SipHash" +#define LN_siphash "siphash" +#define NID_siphash 1062 + +#define SN_ffdhe2048 "ffdhe2048" +#define NID_ffdhe2048 1126 + +#define SN_ffdhe3072 "ffdhe3072" +#define NID_ffdhe3072 1127 + +#define SN_ffdhe4096 "ffdhe4096" +#define NID_ffdhe4096 1128 + +#define SN_ffdhe6144 "ffdhe6144" +#define NID_ffdhe6144 1129 + +#define SN_ffdhe8192 "ffdhe8192" +#define NID_ffdhe8192 1130 + +#define SN_ISO_UA "ISO-UA" +#define NID_ISO_UA 1150 +#define OBJ_ISO_UA OBJ_member_body,804L + +#define SN_ua_pki "ua-pki" +#define NID_ua_pki 1151 +#define OBJ_ua_pki OBJ_ISO_UA,2L,1L,1L,1L + +#define SN_dstu28147 "dstu28147" +#define LN_dstu28147 "DSTU Gost 28147-2009" +#define NID_dstu28147 1152 +#define OBJ_dstu28147 OBJ_ua_pki,1L,1L,1L + +#define SN_dstu28147_ofb "dstu28147-ofb" +#define LN_dstu28147_ofb "DSTU Gost 28147-2009 OFB mode" +#define NID_dstu28147_ofb 1153 +#define OBJ_dstu28147_ofb OBJ_dstu28147,2L + +#define SN_dstu28147_cfb "dstu28147-cfb" +#define LN_dstu28147_cfb "DSTU Gost 28147-2009 CFB mode" +#define NID_dstu28147_cfb 1154 +#define OBJ_dstu28147_cfb OBJ_dstu28147,3L + +#define SN_dstu28147_wrap "dstu28147-wrap" +#define LN_dstu28147_wrap "DSTU Gost 28147-2009 key wrap" +#define NID_dstu28147_wrap 1155 +#define OBJ_dstu28147_wrap OBJ_dstu28147,5L + +#define SN_hmacWithDstu34311 "hmacWithDstu34311" +#define LN_hmacWithDstu34311 "HMAC DSTU Gost 34311-95" +#define NID_hmacWithDstu34311 1156 +#define OBJ_hmacWithDstu34311 OBJ_ua_pki,1L,1L,2L + +#define SN_dstu34311 "dstu34311" +#define LN_dstu34311 "DSTU Gost 34311-95" +#define NID_dstu34311 1157 +#define OBJ_dstu34311 OBJ_ua_pki,1L,2L,1L + +#define SN_dstu4145le "dstu4145le" +#define LN_dstu4145le "DSTU 4145-2002 little endian" +#define NID_dstu4145le 1158 +#define OBJ_dstu4145le OBJ_ua_pki,1L,3L,1L,1L + +#define SN_dstu4145be "dstu4145be" +#define LN_dstu4145be "DSTU 4145-2002 big endian" +#define NID_dstu4145be 1159 +#define OBJ_dstu4145be OBJ_dstu4145le,1L,1L + +#define SN_uacurve0 "uacurve0" +#define LN_uacurve0 "DSTU curve 0" +#define NID_uacurve0 1160 +#define OBJ_uacurve0 OBJ_dstu4145le,2L,0L + +#define SN_uacurve1 "uacurve1" +#define LN_uacurve1 "DSTU curve 1" +#define NID_uacurve1 1161 +#define OBJ_uacurve1 OBJ_dstu4145le,2L,1L + +#define SN_uacurve2 "uacurve2" +#define LN_uacurve2 "DSTU curve 2" +#define NID_uacurve2 1162 +#define OBJ_uacurve2 OBJ_dstu4145le,2L,2L + +#define SN_uacurve3 "uacurve3" +#define LN_uacurve3 "DSTU curve 3" +#define NID_uacurve3 1163 +#define OBJ_uacurve3 OBJ_dstu4145le,2L,3L + +#define SN_uacurve4 "uacurve4" +#define LN_uacurve4 "DSTU curve 4" +#define NID_uacurve4 1164 +#define OBJ_uacurve4 OBJ_dstu4145le,2L,4L + +#define SN_uacurve5 "uacurve5" +#define LN_uacurve5 "DSTU curve 5" +#define NID_uacurve5 1165 +#define OBJ_uacurve5 OBJ_dstu4145le,2L,5L + +#define SN_uacurve6 "uacurve6" +#define LN_uacurve6 "DSTU curve 6" +#define NID_uacurve6 1166 +#define OBJ_uacurve6 OBJ_dstu4145le,2L,6L + +#define SN_uacurve7 "uacurve7" +#define LN_uacurve7 "DSTU curve 7" +#define NID_uacurve7 1167 +#define OBJ_uacurve7 OBJ_dstu4145le,2L,7L + +#define SN_uacurve8 "uacurve8" +#define LN_uacurve8 "DSTU curve 8" +#define NID_uacurve8 1168 +#define OBJ_uacurve8 OBJ_dstu4145le,2L,8L + +#define SN_uacurve9 "uacurve9" +#define LN_uacurve9 "DSTU curve 9" +#define NID_uacurve9 1169 +#define OBJ_uacurve9 OBJ_dstu4145le,2L,9L diff --git a/example/android/third_party/crypto/arm/include/openssl/objects.h b/example/android/third_party/crypto/arm/include/openssl/objects.h new file mode 100644 index 00000000..5e8b5762 --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/objects.h @@ -0,0 +1,175 @@ +/* + * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_OBJECTS_H +# define HEADER_OBJECTS_H + +# include +# include +# include +# include + +# define OBJ_NAME_TYPE_UNDEF 0x00 +# define OBJ_NAME_TYPE_MD_METH 0x01 +# define OBJ_NAME_TYPE_CIPHER_METH 0x02 +# define OBJ_NAME_TYPE_PKEY_METH 0x03 +# define OBJ_NAME_TYPE_COMP_METH 0x04 +# define OBJ_NAME_TYPE_NUM 0x05 + +# define OBJ_NAME_ALIAS 0x8000 + +# define OBJ_BSEARCH_VALUE_ON_NOMATCH 0x01 +# define OBJ_BSEARCH_FIRST_VALUE_ON_MATCH 0x02 + + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct obj_name_st { + int type; + int alias; + const char *name; + const char *data; +} OBJ_NAME; + +# define OBJ_create_and_add_object(a,b,c) OBJ_create(a,b,c) + +int OBJ_NAME_init(void); +int OBJ_NAME_new_index(unsigned long (*hash_func) (const char *), + int (*cmp_func) (const char *, const char *), + void (*free_func) (const char *, int, const char *)); +const char *OBJ_NAME_get(const char *name, int type); +int OBJ_NAME_add(const char *name, int type, const char *data); +int OBJ_NAME_remove(const char *name, int type); +void OBJ_NAME_cleanup(int type); /* -1 for everything */ +void OBJ_NAME_do_all(int type, void (*fn) (const OBJ_NAME *, void *arg), + void *arg); +void OBJ_NAME_do_all_sorted(int type, + void (*fn) (const OBJ_NAME *, void *arg), + void *arg); + +ASN1_OBJECT *OBJ_dup(const ASN1_OBJECT *o); +ASN1_OBJECT *OBJ_nid2obj(int n); +const char *OBJ_nid2ln(int n); +const char *OBJ_nid2sn(int n); +int OBJ_obj2nid(const ASN1_OBJECT *o); +ASN1_OBJECT *OBJ_txt2obj(const char *s, int no_name); +int OBJ_obj2txt(char *buf, int buf_len, const ASN1_OBJECT *a, int no_name); +int OBJ_txt2nid(const char *s); +int OBJ_ln2nid(const char *s); +int OBJ_sn2nid(const char *s); +int OBJ_cmp(const ASN1_OBJECT *a, const ASN1_OBJECT *b); +const void *OBJ_bsearch_(const void *key, const void *base, int num, int size, + int (*cmp) (const void *, const void *)); +const void *OBJ_bsearch_ex_(const void *key, const void *base, int num, + int size, + int (*cmp) (const void *, const void *), + int flags); + +# define _DECLARE_OBJ_BSEARCH_CMP_FN(scope, type1, type2, nm) \ + static int nm##_cmp_BSEARCH_CMP_FN(const void *, const void *); \ + static int nm##_cmp(type1 const *, type2 const *); \ + scope type2 * OBJ_bsearch_##nm(type1 *key, type2 const *base, int num) + +# define DECLARE_OBJ_BSEARCH_CMP_FN(type1, type2, cmp) \ + _DECLARE_OBJ_BSEARCH_CMP_FN(static, type1, type2, cmp) +# define DECLARE_OBJ_BSEARCH_GLOBAL_CMP_FN(type1, type2, nm) \ + type2 * OBJ_bsearch_##nm(type1 *key, type2 const *base, int num) + +/*- + * Unsolved problem: if a type is actually a pointer type, like + * nid_triple is, then its impossible to get a const where you need + * it. Consider: + * + * typedef int nid_triple[3]; + * const void *a_; + * const nid_triple const *a = a_; + * + * The assignment discards a const because what you really want is: + * + * const int const * const *a = a_; + * + * But if you do that, you lose the fact that a is an array of 3 ints, + * which breaks comparison functions. + * + * Thus we end up having to cast, sadly, or unpack the + * declarations. Or, as I finally did in this case, declare nid_triple + * to be a struct, which it should have been in the first place. + * + * Ben, August 2008. + * + * Also, strictly speaking not all types need be const, but handling + * the non-constness means a lot of complication, and in practice + * comparison routines do always not touch their arguments. + */ + +# define IMPLEMENT_OBJ_BSEARCH_CMP_FN(type1, type2, nm) \ + static int nm##_cmp_BSEARCH_CMP_FN(const void *a_, const void *b_) \ + { \ + type1 const *a = a_; \ + type2 const *b = b_; \ + return nm##_cmp(a,b); \ + } \ + static type2 *OBJ_bsearch_##nm(type1 *key, type2 const *base, int num) \ + { \ + return (type2 *)OBJ_bsearch_(key, base, num, sizeof(type2), \ + nm##_cmp_BSEARCH_CMP_FN); \ + } \ + extern void dummy_prototype(void) + +# define IMPLEMENT_OBJ_BSEARCH_GLOBAL_CMP_FN(type1, type2, nm) \ + static int nm##_cmp_BSEARCH_CMP_FN(const void *a_, const void *b_) \ + { \ + type1 const *a = a_; \ + type2 const *b = b_; \ + return nm##_cmp(a,b); \ + } \ + type2 *OBJ_bsearch_##nm(type1 *key, type2 const *base, int num) \ + { \ + return (type2 *)OBJ_bsearch_(key, base, num, sizeof(type2), \ + nm##_cmp_BSEARCH_CMP_FN); \ + } \ + extern void dummy_prototype(void) + +# define OBJ_bsearch(type1,key,type2,base,num,cmp) \ + ((type2 *)OBJ_bsearch_(CHECKED_PTR_OF(type1,key),CHECKED_PTR_OF(type2,base), \ + num,sizeof(type2), \ + ((void)CHECKED_PTR_OF(type1,cmp##_type_1), \ + (void)CHECKED_PTR_OF(type2,cmp##_type_2), \ + cmp##_BSEARCH_CMP_FN))) + +# define OBJ_bsearch_ex(type1,key,type2,base,num,cmp,flags) \ + ((type2 *)OBJ_bsearch_ex_(CHECKED_PTR_OF(type1,key),CHECKED_PTR_OF(type2,base), \ + num,sizeof(type2), \ + ((void)CHECKED_PTR_OF(type1,cmp##_type_1), \ + (void)type_2=CHECKED_PTR_OF(type2,cmp##_type_2), \ + cmp##_BSEARCH_CMP_FN)),flags) + +int OBJ_new_nid(int num); +int OBJ_add_object(const ASN1_OBJECT *obj); +int OBJ_create(const char *oid, const char *sn, const char *ln); +#if OPENSSL_API_COMPAT < 0x10100000L +# define OBJ_cleanup() while(0) continue +#endif +int OBJ_create_objects(BIO *in); + +size_t OBJ_length(const ASN1_OBJECT *obj); +const unsigned char *OBJ_get0_data(const ASN1_OBJECT *obj); + +int OBJ_find_sigid_algs(int signid, int *pdig_nid, int *ppkey_nid); +int OBJ_find_sigid_by_algs(int *psignid, int dig_nid, int pkey_nid); +int OBJ_add_sigid(int signid, int dig_id, int pkey_id); +void OBJ_sigid_free(void); + + +# ifdef __cplusplus +} +# endif +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/objectserr.h b/example/android/third_party/crypto/arm/include/openssl/objectserr.h new file mode 100644 index 00000000..02308dfa --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/objectserr.h @@ -0,0 +1,38 @@ +/* + * Generated by util/mkerr.pl DO NOT EDIT + * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_OBJERR_H +# define HEADER_OBJERR_H + +# ifdef __cplusplus +extern "C" +# endif +int ERR_load_OBJ_strings(void); + +/* + * OBJ function codes. + */ +# define OBJ_F_OBJ_ADD_OBJECT 105 +# define OBJ_F_OBJ_ADD_SIGID 107 +# define OBJ_F_OBJ_CREATE 100 +# define OBJ_F_OBJ_DUP 101 +# define OBJ_F_OBJ_NAME_NEW_INDEX 106 +# define OBJ_F_OBJ_NID2LN 102 +# define OBJ_F_OBJ_NID2OBJ 103 +# define OBJ_F_OBJ_NID2SN 104 +# define OBJ_F_OBJ_TXT2OBJ 108 + +/* + * OBJ reason codes. + */ +# define OBJ_R_OID_EXISTS 102 +# define OBJ_R_UNKNOWN_NID 101 + +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/ocsp.h b/example/android/third_party/crypto/arm/include/openssl/ocsp.h new file mode 100644 index 00000000..0a17166b --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/ocsp.h @@ -0,0 +1,352 @@ +/* + * Copyright 2000-2018 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_OCSP_H +# define HEADER_OCSP_H + +#include + +/* + * These definitions are outside the OPENSSL_NO_OCSP guard because although for + * historical reasons they have OCSP_* names, they can actually be used + * independently of OCSP. E.g. see RFC5280 + */ +/*- + * CRLReason ::= ENUMERATED { + * unspecified (0), + * keyCompromise (1), + * cACompromise (2), + * affiliationChanged (3), + * superseded (4), + * cessationOfOperation (5), + * certificateHold (6), + * removeFromCRL (8) } + */ +# define OCSP_REVOKED_STATUS_NOSTATUS -1 +# define OCSP_REVOKED_STATUS_UNSPECIFIED 0 +# define OCSP_REVOKED_STATUS_KEYCOMPROMISE 1 +# define OCSP_REVOKED_STATUS_CACOMPROMISE 2 +# define OCSP_REVOKED_STATUS_AFFILIATIONCHANGED 3 +# define OCSP_REVOKED_STATUS_SUPERSEDED 4 +# define OCSP_REVOKED_STATUS_CESSATIONOFOPERATION 5 +# define OCSP_REVOKED_STATUS_CERTIFICATEHOLD 6 +# define OCSP_REVOKED_STATUS_REMOVEFROMCRL 8 + + +# ifndef OPENSSL_NO_OCSP + +# include +# include +# include +# include +# include + +#ifdef __cplusplus +extern "C" { +#endif + +/* Various flags and values */ + +# define OCSP_DEFAULT_NONCE_LENGTH 16 + +# define OCSP_NOCERTS 0x1 +# define OCSP_NOINTERN 0x2 +# define OCSP_NOSIGS 0x4 +# define OCSP_NOCHAIN 0x8 +# define OCSP_NOVERIFY 0x10 +# define OCSP_NOEXPLICIT 0x20 +# define OCSP_NOCASIGN 0x40 +# define OCSP_NODELEGATED 0x80 +# define OCSP_NOCHECKS 0x100 +# define OCSP_TRUSTOTHER 0x200 +# define OCSP_RESPID_KEY 0x400 +# define OCSP_NOTIME 0x800 + +typedef struct ocsp_cert_id_st OCSP_CERTID; + +DEFINE_STACK_OF(OCSP_CERTID) + +typedef struct ocsp_one_request_st OCSP_ONEREQ; + +DEFINE_STACK_OF(OCSP_ONEREQ) + +typedef struct ocsp_req_info_st OCSP_REQINFO; +typedef struct ocsp_signature_st OCSP_SIGNATURE; +typedef struct ocsp_request_st OCSP_REQUEST; + +# define OCSP_RESPONSE_STATUS_SUCCESSFUL 0 +# define OCSP_RESPONSE_STATUS_MALFORMEDREQUEST 1 +# define OCSP_RESPONSE_STATUS_INTERNALERROR 2 +# define OCSP_RESPONSE_STATUS_TRYLATER 3 +# define OCSP_RESPONSE_STATUS_SIGREQUIRED 5 +# define OCSP_RESPONSE_STATUS_UNAUTHORIZED 6 + +typedef struct ocsp_resp_bytes_st OCSP_RESPBYTES; + +# define V_OCSP_RESPID_NAME 0 +# define V_OCSP_RESPID_KEY 1 + +DEFINE_STACK_OF(OCSP_RESPID) + +typedef struct ocsp_revoked_info_st OCSP_REVOKEDINFO; + +# define V_OCSP_CERTSTATUS_GOOD 0 +# define V_OCSP_CERTSTATUS_REVOKED 1 +# define V_OCSP_CERTSTATUS_UNKNOWN 2 + +typedef struct ocsp_cert_status_st OCSP_CERTSTATUS; +typedef struct ocsp_single_response_st OCSP_SINGLERESP; + +DEFINE_STACK_OF(OCSP_SINGLERESP) + +typedef struct ocsp_response_data_st OCSP_RESPDATA; + +typedef struct ocsp_basic_response_st OCSP_BASICRESP; + +typedef struct ocsp_crl_id_st OCSP_CRLID; +typedef struct ocsp_service_locator_st OCSP_SERVICELOC; + +# define PEM_STRING_OCSP_REQUEST "OCSP REQUEST" +# define PEM_STRING_OCSP_RESPONSE "OCSP RESPONSE" + +# define d2i_OCSP_REQUEST_bio(bp,p) ASN1_d2i_bio_of(OCSP_REQUEST,OCSP_REQUEST_new,d2i_OCSP_REQUEST,bp,p) + +# define d2i_OCSP_RESPONSE_bio(bp,p) ASN1_d2i_bio_of(OCSP_RESPONSE,OCSP_RESPONSE_new,d2i_OCSP_RESPONSE,bp,p) + +# define PEM_read_bio_OCSP_REQUEST(bp,x,cb) (OCSP_REQUEST *)PEM_ASN1_read_bio( \ + (char *(*)())d2i_OCSP_REQUEST,PEM_STRING_OCSP_REQUEST, \ + bp,(char **)(x),cb,NULL) + +# define PEM_read_bio_OCSP_RESPONSE(bp,x,cb)(OCSP_RESPONSE *)PEM_ASN1_read_bio(\ + (char *(*)())d2i_OCSP_RESPONSE,PEM_STRING_OCSP_RESPONSE, \ + bp,(char **)(x),cb,NULL) + +# define PEM_write_bio_OCSP_REQUEST(bp,o) \ + PEM_ASN1_write_bio((int (*)())i2d_OCSP_REQUEST,PEM_STRING_OCSP_REQUEST,\ + bp,(char *)(o), NULL,NULL,0,NULL,NULL) + +# define PEM_write_bio_OCSP_RESPONSE(bp,o) \ + PEM_ASN1_write_bio((int (*)())i2d_OCSP_RESPONSE,PEM_STRING_OCSP_RESPONSE,\ + bp,(char *)(o), NULL,NULL,0,NULL,NULL) + +# define i2d_OCSP_RESPONSE_bio(bp,o) ASN1_i2d_bio_of(OCSP_RESPONSE,i2d_OCSP_RESPONSE,bp,o) + +# define i2d_OCSP_REQUEST_bio(bp,o) ASN1_i2d_bio_of(OCSP_REQUEST,i2d_OCSP_REQUEST,bp,o) + +# define ASN1_BIT_STRING_digest(data,type,md,len) \ + ASN1_item_digest(ASN1_ITEM_rptr(ASN1_BIT_STRING),type,data,md,len) + +# define OCSP_CERTSTATUS_dup(cs)\ + (OCSP_CERTSTATUS*)ASN1_dup((int(*)())i2d_OCSP_CERTSTATUS,\ + (char *(*)())d2i_OCSP_CERTSTATUS,(char *)(cs)) + +OCSP_CERTID *OCSP_CERTID_dup(OCSP_CERTID *id); + +OCSP_RESPONSE *OCSP_sendreq_bio(BIO *b, const char *path, OCSP_REQUEST *req); +OCSP_REQ_CTX *OCSP_sendreq_new(BIO *io, const char *path, OCSP_REQUEST *req, + int maxline); +int OCSP_REQ_CTX_nbio(OCSP_REQ_CTX *rctx); +int OCSP_sendreq_nbio(OCSP_RESPONSE **presp, OCSP_REQ_CTX *rctx); +OCSP_REQ_CTX *OCSP_REQ_CTX_new(BIO *io, int maxline); +void OCSP_REQ_CTX_free(OCSP_REQ_CTX *rctx); +void OCSP_set_max_response_length(OCSP_REQ_CTX *rctx, unsigned long len); +int OCSP_REQ_CTX_i2d(OCSP_REQ_CTX *rctx, const ASN1_ITEM *it, + ASN1_VALUE *val); +int OCSP_REQ_CTX_nbio_d2i(OCSP_REQ_CTX *rctx, ASN1_VALUE **pval, + const ASN1_ITEM *it); +BIO *OCSP_REQ_CTX_get0_mem_bio(OCSP_REQ_CTX *rctx); +int OCSP_REQ_CTX_http(OCSP_REQ_CTX *rctx, const char *op, const char *path); +int OCSP_REQ_CTX_set1_req(OCSP_REQ_CTX *rctx, OCSP_REQUEST *req); +int OCSP_REQ_CTX_add1_header(OCSP_REQ_CTX *rctx, + const char *name, const char *value); + +OCSP_CERTID *OCSP_cert_to_id(const EVP_MD *dgst, const X509 *subject, + const X509 *issuer); + +OCSP_CERTID *OCSP_cert_id_new(const EVP_MD *dgst, + const X509_NAME *issuerName, + const ASN1_BIT_STRING *issuerKey, + const ASN1_INTEGER *serialNumber); + +OCSP_ONEREQ *OCSP_request_add0_id(OCSP_REQUEST *req, OCSP_CERTID *cid); + +int OCSP_request_add1_nonce(OCSP_REQUEST *req, unsigned char *val, int len); +int OCSP_basic_add1_nonce(OCSP_BASICRESP *resp, unsigned char *val, int len); +int OCSP_check_nonce(OCSP_REQUEST *req, OCSP_BASICRESP *bs); +int OCSP_copy_nonce(OCSP_BASICRESP *resp, OCSP_REQUEST *req); + +int OCSP_request_set1_name(OCSP_REQUEST *req, X509_NAME *nm); +int OCSP_request_add1_cert(OCSP_REQUEST *req, X509 *cert); + +int OCSP_request_sign(OCSP_REQUEST *req, + X509 *signer, + EVP_PKEY *key, + const EVP_MD *dgst, + STACK_OF(X509) *certs, unsigned long flags); + +int OCSP_response_status(OCSP_RESPONSE *resp); +OCSP_BASICRESP *OCSP_response_get1_basic(OCSP_RESPONSE *resp); + +const ASN1_OCTET_STRING *OCSP_resp_get0_signature(const OCSP_BASICRESP *bs); +const X509_ALGOR *OCSP_resp_get0_tbs_sigalg(const OCSP_BASICRESP *bs); +const OCSP_RESPDATA *OCSP_resp_get0_respdata(const OCSP_BASICRESP *bs); +int OCSP_resp_get0_signer(OCSP_BASICRESP *bs, X509 **signer, + STACK_OF(X509) *extra_certs); + +int OCSP_resp_count(OCSP_BASICRESP *bs); +OCSP_SINGLERESP *OCSP_resp_get0(OCSP_BASICRESP *bs, int idx); +const ASN1_GENERALIZEDTIME *OCSP_resp_get0_produced_at(const OCSP_BASICRESP* bs); +const STACK_OF(X509) *OCSP_resp_get0_certs(const OCSP_BASICRESP *bs); +int OCSP_resp_get0_id(const OCSP_BASICRESP *bs, + const ASN1_OCTET_STRING **pid, + const X509_NAME **pname); +int OCSP_resp_get1_id(const OCSP_BASICRESP *bs, + ASN1_OCTET_STRING **pid, + X509_NAME **pname); + +int OCSP_resp_find(OCSP_BASICRESP *bs, OCSP_CERTID *id, int last); +int OCSP_single_get0_status(OCSP_SINGLERESP *single, int *reason, + ASN1_GENERALIZEDTIME **revtime, + ASN1_GENERALIZEDTIME **thisupd, + ASN1_GENERALIZEDTIME **nextupd); +int OCSP_resp_find_status(OCSP_BASICRESP *bs, OCSP_CERTID *id, int *status, + int *reason, + ASN1_GENERALIZEDTIME **revtime, + ASN1_GENERALIZEDTIME **thisupd, + ASN1_GENERALIZEDTIME **nextupd); +int OCSP_check_validity(ASN1_GENERALIZEDTIME *thisupd, + ASN1_GENERALIZEDTIME *nextupd, long sec, long maxsec); + +int OCSP_request_verify(OCSP_REQUEST *req, STACK_OF(X509) *certs, + X509_STORE *store, unsigned long flags); + +int OCSP_parse_url(const char *url, char **phost, char **pport, char **ppath, + int *pssl); + +int OCSP_id_issuer_cmp(OCSP_CERTID *a, OCSP_CERTID *b); +int OCSP_id_cmp(OCSP_CERTID *a, OCSP_CERTID *b); + +int OCSP_request_onereq_count(OCSP_REQUEST *req); +OCSP_ONEREQ *OCSP_request_onereq_get0(OCSP_REQUEST *req, int i); +OCSP_CERTID *OCSP_onereq_get0_id(OCSP_ONEREQ *one); +int OCSP_id_get0_info(ASN1_OCTET_STRING **piNameHash, ASN1_OBJECT **pmd, + ASN1_OCTET_STRING **pikeyHash, + ASN1_INTEGER **pserial, OCSP_CERTID *cid); +int OCSP_request_is_signed(OCSP_REQUEST *req); +OCSP_RESPONSE *OCSP_response_create(int status, OCSP_BASICRESP *bs); +OCSP_SINGLERESP *OCSP_basic_add1_status(OCSP_BASICRESP *rsp, + OCSP_CERTID *cid, + int status, int reason, + ASN1_TIME *revtime, + ASN1_TIME *thisupd, + ASN1_TIME *nextupd); +int OCSP_basic_add1_cert(OCSP_BASICRESP *resp, X509 *cert); +int OCSP_basic_sign(OCSP_BASICRESP *brsp, + X509 *signer, EVP_PKEY *key, const EVP_MD *dgst, + STACK_OF(X509) *certs, unsigned long flags); +int OCSP_basic_sign_ctx(OCSP_BASICRESP *brsp, + X509 *signer, EVP_MD_CTX *ctx, + STACK_OF(X509) *certs, unsigned long flags); +int OCSP_RESPID_set_by_name(OCSP_RESPID *respid, X509 *cert); +int OCSP_RESPID_set_by_key(OCSP_RESPID *respid, X509 *cert); +int OCSP_RESPID_match(OCSP_RESPID *respid, X509 *cert); + +X509_EXTENSION *OCSP_crlID_new(const char *url, long *n, char *tim); + +X509_EXTENSION *OCSP_accept_responses_new(char **oids); + +X509_EXTENSION *OCSP_archive_cutoff_new(char *tim); + +X509_EXTENSION *OCSP_url_svcloc_new(X509_NAME *issuer, const char **urls); + +int OCSP_REQUEST_get_ext_count(OCSP_REQUEST *x); +int OCSP_REQUEST_get_ext_by_NID(OCSP_REQUEST *x, int nid, int lastpos); +int OCSP_REQUEST_get_ext_by_OBJ(OCSP_REQUEST *x, const ASN1_OBJECT *obj, + int lastpos); +int OCSP_REQUEST_get_ext_by_critical(OCSP_REQUEST *x, int crit, int lastpos); +X509_EXTENSION *OCSP_REQUEST_get_ext(OCSP_REQUEST *x, int loc); +X509_EXTENSION *OCSP_REQUEST_delete_ext(OCSP_REQUEST *x, int loc); +void *OCSP_REQUEST_get1_ext_d2i(OCSP_REQUEST *x, int nid, int *crit, + int *idx); +int OCSP_REQUEST_add1_ext_i2d(OCSP_REQUEST *x, int nid, void *value, int crit, + unsigned long flags); +int OCSP_REQUEST_add_ext(OCSP_REQUEST *x, X509_EXTENSION *ex, int loc); + +int OCSP_ONEREQ_get_ext_count(OCSP_ONEREQ *x); +int OCSP_ONEREQ_get_ext_by_NID(OCSP_ONEREQ *x, int nid, int lastpos); +int OCSP_ONEREQ_get_ext_by_OBJ(OCSP_ONEREQ *x, const ASN1_OBJECT *obj, int lastpos); +int OCSP_ONEREQ_get_ext_by_critical(OCSP_ONEREQ *x, int crit, int lastpos); +X509_EXTENSION *OCSP_ONEREQ_get_ext(OCSP_ONEREQ *x, int loc); +X509_EXTENSION *OCSP_ONEREQ_delete_ext(OCSP_ONEREQ *x, int loc); +void *OCSP_ONEREQ_get1_ext_d2i(OCSP_ONEREQ *x, int nid, int *crit, int *idx); +int OCSP_ONEREQ_add1_ext_i2d(OCSP_ONEREQ *x, int nid, void *value, int crit, + unsigned long flags); +int OCSP_ONEREQ_add_ext(OCSP_ONEREQ *x, X509_EXTENSION *ex, int loc); + +int OCSP_BASICRESP_get_ext_count(OCSP_BASICRESP *x); +int OCSP_BASICRESP_get_ext_by_NID(OCSP_BASICRESP *x, int nid, int lastpos); +int OCSP_BASICRESP_get_ext_by_OBJ(OCSP_BASICRESP *x, const ASN1_OBJECT *obj, + int lastpos); +int OCSP_BASICRESP_get_ext_by_critical(OCSP_BASICRESP *x, int crit, + int lastpos); +X509_EXTENSION *OCSP_BASICRESP_get_ext(OCSP_BASICRESP *x, int loc); +X509_EXTENSION *OCSP_BASICRESP_delete_ext(OCSP_BASICRESP *x, int loc); +void *OCSP_BASICRESP_get1_ext_d2i(OCSP_BASICRESP *x, int nid, int *crit, + int *idx); +int OCSP_BASICRESP_add1_ext_i2d(OCSP_BASICRESP *x, int nid, void *value, + int crit, unsigned long flags); +int OCSP_BASICRESP_add_ext(OCSP_BASICRESP *x, X509_EXTENSION *ex, int loc); + +int OCSP_SINGLERESP_get_ext_count(OCSP_SINGLERESP *x); +int OCSP_SINGLERESP_get_ext_by_NID(OCSP_SINGLERESP *x, int nid, int lastpos); +int OCSP_SINGLERESP_get_ext_by_OBJ(OCSP_SINGLERESP *x, const ASN1_OBJECT *obj, + int lastpos); +int OCSP_SINGLERESP_get_ext_by_critical(OCSP_SINGLERESP *x, int crit, + int lastpos); +X509_EXTENSION *OCSP_SINGLERESP_get_ext(OCSP_SINGLERESP *x, int loc); +X509_EXTENSION *OCSP_SINGLERESP_delete_ext(OCSP_SINGLERESP *x, int loc); +void *OCSP_SINGLERESP_get1_ext_d2i(OCSP_SINGLERESP *x, int nid, int *crit, + int *idx); +int OCSP_SINGLERESP_add1_ext_i2d(OCSP_SINGLERESP *x, int nid, void *value, + int crit, unsigned long flags); +int OCSP_SINGLERESP_add_ext(OCSP_SINGLERESP *x, X509_EXTENSION *ex, int loc); +const OCSP_CERTID *OCSP_SINGLERESP_get0_id(const OCSP_SINGLERESP *x); + +DECLARE_ASN1_FUNCTIONS(OCSP_SINGLERESP) +DECLARE_ASN1_FUNCTIONS(OCSP_CERTSTATUS) +DECLARE_ASN1_FUNCTIONS(OCSP_REVOKEDINFO) +DECLARE_ASN1_FUNCTIONS(OCSP_BASICRESP) +DECLARE_ASN1_FUNCTIONS(OCSP_RESPDATA) +DECLARE_ASN1_FUNCTIONS(OCSP_RESPID) +DECLARE_ASN1_FUNCTIONS(OCSP_RESPONSE) +DECLARE_ASN1_FUNCTIONS(OCSP_RESPBYTES) +DECLARE_ASN1_FUNCTIONS(OCSP_ONEREQ) +DECLARE_ASN1_FUNCTIONS(OCSP_CERTID) +DECLARE_ASN1_FUNCTIONS(OCSP_REQUEST) +DECLARE_ASN1_FUNCTIONS(OCSP_SIGNATURE) +DECLARE_ASN1_FUNCTIONS(OCSP_REQINFO) +DECLARE_ASN1_FUNCTIONS(OCSP_CRLID) +DECLARE_ASN1_FUNCTIONS(OCSP_SERVICELOC) + +const char *OCSP_response_status_str(long s); +const char *OCSP_cert_status_str(long s); +const char *OCSP_crl_reason_str(long s); + +int OCSP_REQUEST_print(BIO *bp, OCSP_REQUEST *a, unsigned long flags); +int OCSP_RESPONSE_print(BIO *bp, OCSP_RESPONSE *o, unsigned long flags); + +int OCSP_basic_verify(OCSP_BASICRESP *bs, STACK_OF(X509) *certs, + X509_STORE *st, unsigned long flags); + + +# ifdef __cplusplus +} +# endif +# endif +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/ocsperr.h b/example/android/third_party/crypto/arm/include/openssl/ocsperr.h new file mode 100644 index 00000000..7d93b12d --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/ocsperr.h @@ -0,0 +1,74 @@ +/* + * Generated by util/mkerr.pl DO NOT EDIT + * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_OCSPERR_H +# define HEADER_OCSPERR_H + +# include + +# ifndef OPENSSL_NO_OCSP + +# ifdef __cplusplus +extern "C" +# endif +int ERR_load_OCSP_strings(void); + +/* + * OCSP function codes. + */ +# define OCSP_F_D2I_OCSP_NONCE 102 +# define OCSP_F_OCSP_BASIC_ADD1_STATUS 103 +# define OCSP_F_OCSP_BASIC_SIGN 104 +# define OCSP_F_OCSP_BASIC_SIGN_CTX 119 +# define OCSP_F_OCSP_BASIC_VERIFY 105 +# define OCSP_F_OCSP_CERT_ID_NEW 101 +# define OCSP_F_OCSP_CHECK_DELEGATED 106 +# define OCSP_F_OCSP_CHECK_IDS 107 +# define OCSP_F_OCSP_CHECK_ISSUER 108 +# define OCSP_F_OCSP_CHECK_VALIDITY 115 +# define OCSP_F_OCSP_MATCH_ISSUERID 109 +# define OCSP_F_OCSP_PARSE_URL 114 +# define OCSP_F_OCSP_REQUEST_SIGN 110 +# define OCSP_F_OCSP_REQUEST_VERIFY 116 +# define OCSP_F_OCSP_RESPONSE_GET1_BASIC 111 +# define OCSP_F_PARSE_HTTP_LINE1 118 + +/* + * OCSP reason codes. + */ +# define OCSP_R_CERTIFICATE_VERIFY_ERROR 101 +# define OCSP_R_DIGEST_ERR 102 +# define OCSP_R_ERROR_IN_NEXTUPDATE_FIELD 122 +# define OCSP_R_ERROR_IN_THISUPDATE_FIELD 123 +# define OCSP_R_ERROR_PARSING_URL 121 +# define OCSP_R_MISSING_OCSPSIGNING_USAGE 103 +# define OCSP_R_NEXTUPDATE_BEFORE_THISUPDATE 124 +# define OCSP_R_NOT_BASIC_RESPONSE 104 +# define OCSP_R_NO_CERTIFICATES_IN_CHAIN 105 +# define OCSP_R_NO_RESPONSE_DATA 108 +# define OCSP_R_NO_REVOKED_TIME 109 +# define OCSP_R_NO_SIGNER_KEY 130 +# define OCSP_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE 110 +# define OCSP_R_REQUEST_NOT_SIGNED 128 +# define OCSP_R_RESPONSE_CONTAINS_NO_REVOCATION_DATA 111 +# define OCSP_R_ROOT_CA_NOT_TRUSTED 112 +# define OCSP_R_SERVER_RESPONSE_ERROR 114 +# define OCSP_R_SERVER_RESPONSE_PARSE_ERROR 115 +# define OCSP_R_SIGNATURE_FAILURE 117 +# define OCSP_R_SIGNER_CERTIFICATE_NOT_FOUND 118 +# define OCSP_R_STATUS_EXPIRED 125 +# define OCSP_R_STATUS_NOT_YET_VALID 126 +# define OCSP_R_STATUS_TOO_OLD 127 +# define OCSP_R_UNKNOWN_MESSAGE_DIGEST 119 +# define OCSP_R_UNKNOWN_NID 120 +# define OCSP_R_UNSUPPORTED_REQUESTORNAME_TYPE 129 + +# endif +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/opensslconf.h b/example/android/third_party/crypto/arm/include/openssl/opensslconf.h new file mode 100644 index 00000000..79016086 --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/opensslconf.h @@ -0,0 +1,195 @@ +/* + * WARNING: do not edit! + * Generated by Makefile from include/openssl/opensslconf.h.in + * + * Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef OPENSSL_ALGORITHM_DEFINES +# error OPENSSL_ALGORITHM_DEFINES no longer supported +#endif + +/* + * OpenSSL was configured with the following options: + */ + +#ifndef OPENSSL_NO_MD2 +# define OPENSSL_NO_MD2 +#endif +#ifndef OPENSSL_NO_RC5 +# define OPENSSL_NO_RC5 +#endif +#ifndef OPENSSL_THREADS +# define OPENSSL_THREADS +#endif +#ifndef OPENSSL_RAND_SEED_OS +# define OPENSSL_RAND_SEED_OS +#endif +#ifndef OPENSSL_NO_ASAN +# define OPENSSL_NO_ASAN +#endif +#ifndef OPENSSL_NO_CRYPTO_MDEBUG +# define OPENSSL_NO_CRYPTO_MDEBUG +#endif +#ifndef OPENSSL_NO_CRYPTO_MDEBUG_BACKTRACE +# define OPENSSL_NO_CRYPTO_MDEBUG_BACKTRACE +#endif +#ifndef OPENSSL_NO_DEVCRYPTOENG +# define OPENSSL_NO_DEVCRYPTOENG +#endif +#ifndef OPENSSL_NO_EC_NISTP_64_GCC_128 +# define OPENSSL_NO_EC_NISTP_64_GCC_128 +#endif +#ifndef OPENSSL_NO_EGD +# define OPENSSL_NO_EGD +#endif +#ifndef OPENSSL_NO_EXTERNAL_TESTS +# define OPENSSL_NO_EXTERNAL_TESTS +#endif +#ifndef OPENSSL_NO_FUZZ_AFL +# define OPENSSL_NO_FUZZ_AFL +#endif +#ifndef OPENSSL_NO_FUZZ_LIBFUZZER +# define OPENSSL_NO_FUZZ_LIBFUZZER +#endif +#ifndef OPENSSL_NO_HEARTBEATS +# define OPENSSL_NO_HEARTBEATS +#endif +#ifndef OPENSSL_NO_MSAN +# define OPENSSL_NO_MSAN +#endif +#ifndef OPENSSL_NO_SCTP +# define OPENSSL_NO_SCTP +#endif +#ifndef OPENSSL_NO_SSL_TRACE +# define OPENSSL_NO_SSL_TRACE +#endif +#ifndef OPENSSL_NO_SSL3 +# define OPENSSL_NO_SSL3 +#endif +#ifndef OPENSSL_NO_SSL3_METHOD +# define OPENSSL_NO_SSL3_METHOD +#endif +#ifndef OPENSSL_NO_UBSAN +# define OPENSSL_NO_UBSAN +#endif +#ifndef OPENSSL_NO_UNIT_TEST +# define OPENSSL_NO_UNIT_TEST +#endif +#ifndef OPENSSL_NO_WEAK_SSL_CIPHERS +# define OPENSSL_NO_WEAK_SSL_CIPHERS +#endif +#ifndef OPENSSL_NO_DYNAMIC_ENGINE +# define OPENSSL_NO_DYNAMIC_ENGINE +#endif +#ifndef OPENSSL_NO_AFALGENG +# define OPENSSL_NO_AFALGENG +#endif + + +/* + * Sometimes OPENSSSL_NO_xxx ends up with an empty file and some compilers + * don't like that. This will hopefully silence them. + */ +#define NON_EMPTY_TRANSLATION_UNIT static void *dummy = &dummy; + +/* + * Applications should use -DOPENSSL_API_COMPAT= to suppress the + * declarations of functions deprecated in or before . Otherwise, they + * still won't see them if the library has been built to disable deprecated + * functions. + */ +#ifndef DECLARE_DEPRECATED +# define DECLARE_DEPRECATED(f) f; +# ifdef __GNUC__ +# if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 0) +# undef DECLARE_DEPRECATED +# define DECLARE_DEPRECATED(f) f __attribute__ ((deprecated)); +# endif +# endif +#endif + +#ifndef OPENSSL_FILE +# ifdef OPENSSL_NO_FILENAMES +# define OPENSSL_FILE "" +# define OPENSSL_LINE 0 +# else +# define OPENSSL_FILE __FILE__ +# define OPENSSL_LINE __LINE__ +# endif +#endif + +#ifndef OPENSSL_MIN_API +# define OPENSSL_MIN_API 0 +#endif + +#if !defined(OPENSSL_API_COMPAT) || OPENSSL_API_COMPAT < OPENSSL_MIN_API +# undef OPENSSL_API_COMPAT +# define OPENSSL_API_COMPAT OPENSSL_MIN_API +#endif + +/* + * Do not deprecate things to be deprecated in version 1.2.0 before the + * OpenSSL version number matches. + */ +#if OPENSSL_VERSION_NUMBER < 0x10200000L +# define DEPRECATEDIN_1_2_0(f) f; +#elif OPENSSL_API_COMPAT < 0x10200000L +# define DEPRECATEDIN_1_2_0(f) DECLARE_DEPRECATED(f) +#else +# define DEPRECATEDIN_1_2_0(f) +#endif + +#if OPENSSL_API_COMPAT < 0x10100000L +# define DEPRECATEDIN_1_1_0(f) DECLARE_DEPRECATED(f) +#else +# define DEPRECATEDIN_1_1_0(f) +#endif + +#if OPENSSL_API_COMPAT < 0x10000000L +# define DEPRECATEDIN_1_0_0(f) DECLARE_DEPRECATED(f) +#else +# define DEPRECATEDIN_1_0_0(f) +#endif + +#if OPENSSL_API_COMPAT < 0x00908000L +# define DEPRECATEDIN_0_9_8(f) DECLARE_DEPRECATED(f) +#else +# define DEPRECATEDIN_0_9_8(f) +#endif + +/* Generate 80386 code? */ +#undef I386_ONLY + +#undef OPENSSL_UNISTD +#define OPENSSL_UNISTD + +#undef OPENSSL_EXPORT_VAR_AS_FUNCTION + +/* + * The following are cipher-specific, but are part of the public API. + */ +#if !defined(OPENSSL_SYS_UEFI) +# define BN_LLONG +/* Only one for the following should be defined */ +# undef SIXTY_FOUR_BIT_LONG +# undef SIXTY_FOUR_BIT +# define THIRTY_TWO_BIT +#endif + +#define RC4_INT unsigned char + +#ifdef __cplusplus +} +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/opensslconf.h.in b/example/android/third_party/crypto/arm/include/openssl/opensslconf.h.in new file mode 100644 index 00000000..bc98cad5 --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/opensslconf.h.in @@ -0,0 +1,155 @@ +/* + * {- join("\n * ", @autowarntext) -} + * + * Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef OPENSSL_ALGORITHM_DEFINES +# error OPENSSL_ALGORITHM_DEFINES no longer supported +#endif + +/* + * OpenSSL was configured with the following options: + */ + +{- if (@{$config{openssl_sys_defines}}) { + foreach (@{$config{openssl_sys_defines}}) { + $OUT .= "#ifndef $_\n"; + $OUT .= "# define $_ 1\n"; + $OUT .= "#endif\n"; + } + } + foreach (@{$config{openssl_api_defines}}) { + (my $macro, my $value) = $_ =~ /^(.*?)=(.*?)$/; + $OUT .= "#define $macro $value\n"; + } + if (@{$config{openssl_algorithm_defines}}) { + foreach (@{$config{openssl_algorithm_defines}}) { + $OUT .= "#ifndef $_\n"; + $OUT .= "# define $_\n"; + $OUT .= "#endif\n"; + } + } + if (@{$config{openssl_thread_defines}}) { + foreach (@{$config{openssl_thread_defines}}) { + $OUT .= "#ifndef $_\n"; + $OUT .= "# define $_\n"; + $OUT .= "#endif\n"; + } + } + if (@{$config{openssl_other_defines}}) { + foreach (@{$config{openssl_other_defines}}) { + $OUT .= "#ifndef $_\n"; + $OUT .= "# define $_\n"; + $OUT .= "#endif\n"; + } + } + ""; +-} + +/* + * Sometimes OPENSSSL_NO_xxx ends up with an empty file and some compilers + * don't like that. This will hopefully silence them. + */ +#define NON_EMPTY_TRANSLATION_UNIT static void *dummy = &dummy; + +/* + * Applications should use -DOPENSSL_API_COMPAT= to suppress the + * declarations of functions deprecated in or before . Otherwise, they + * still won't see them if the library has been built to disable deprecated + * functions. + */ +#ifndef DECLARE_DEPRECATED +# define DECLARE_DEPRECATED(f) f; +# ifdef __GNUC__ +# if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 0) +# undef DECLARE_DEPRECATED +# define DECLARE_DEPRECATED(f) f __attribute__ ((deprecated)); +# endif +# endif +#endif + +#ifndef OPENSSL_FILE +# ifdef OPENSSL_NO_FILENAMES +# define OPENSSL_FILE "" +# define OPENSSL_LINE 0 +# else +# define OPENSSL_FILE __FILE__ +# define OPENSSL_LINE __LINE__ +# endif +#endif + +#ifndef OPENSSL_MIN_API +# define OPENSSL_MIN_API 0 +#endif + +#if !defined(OPENSSL_API_COMPAT) || OPENSSL_API_COMPAT < OPENSSL_MIN_API +# undef OPENSSL_API_COMPAT +# define OPENSSL_API_COMPAT OPENSSL_MIN_API +#endif + +/* + * Do not deprecate things to be deprecated in version 1.2.0 before the + * OpenSSL version number matches. + */ +#if OPENSSL_VERSION_NUMBER < 0x10200000L +# define DEPRECATEDIN_1_2_0(f) f; +#elif OPENSSL_API_COMPAT < 0x10200000L +# define DEPRECATEDIN_1_2_0(f) DECLARE_DEPRECATED(f) +#else +# define DEPRECATEDIN_1_2_0(f) +#endif + +#if OPENSSL_API_COMPAT < 0x10100000L +# define DEPRECATEDIN_1_1_0(f) DECLARE_DEPRECATED(f) +#else +# define DEPRECATEDIN_1_1_0(f) +#endif + +#if OPENSSL_API_COMPAT < 0x10000000L +# define DEPRECATEDIN_1_0_0(f) DECLARE_DEPRECATED(f) +#else +# define DEPRECATEDIN_1_0_0(f) +#endif + +#if OPENSSL_API_COMPAT < 0x00908000L +# define DEPRECATEDIN_0_9_8(f) DECLARE_DEPRECATED(f) +#else +# define DEPRECATEDIN_0_9_8(f) +#endif + +/* Generate 80386 code? */ +{- $config{processor} eq "386" ? "#define" : "#undef" -} I386_ONLY + +#undef OPENSSL_UNISTD +#define OPENSSL_UNISTD {- $target{unistd} -} + +{- $config{export_var_as_fn} ? "#define" : "#undef" -} OPENSSL_EXPORT_VAR_AS_FUNCTION + +/* + * The following are cipher-specific, but are part of the public API. + */ +#if !defined(OPENSSL_SYS_UEFI) +{- $config{bn_ll} ? "# define" : "# undef" -} BN_LLONG +/* Only one for the following should be defined */ +{- $config{b64l} ? "# define" : "# undef" -} SIXTY_FOUR_BIT_LONG +{- $config{b64} ? "# define" : "# undef" -} SIXTY_FOUR_BIT +{- $config{b32} ? "# define" : "# undef" -} THIRTY_TWO_BIT +#endif + +#define RC4_INT {- $config{rc4_int} -} + +#ifdef __cplusplus +} +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/opensslv.h b/example/android/third_party/crypto/arm/include/openssl/opensslv.h new file mode 100644 index 00000000..af7a3b56 --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/opensslv.h @@ -0,0 +1,101 @@ +/* + * Copyright 1999-2018 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_OPENSSLV_H +# define HEADER_OPENSSLV_H + +#ifdef __cplusplus +extern "C" { +#endif + +/*- + * Numeric release version identifier: + * MNNFFPPS: major minor fix patch status + * The status nibble has one of the values 0 for development, 1 to e for betas + * 1 to 14, and f for release. The patch level is exactly that. + * For example: + * 0.9.3-dev 0x00903000 + * 0.9.3-beta1 0x00903001 + * 0.9.3-beta2-dev 0x00903002 + * 0.9.3-beta2 0x00903002 (same as ...beta2-dev) + * 0.9.3 0x0090300f + * 0.9.3a 0x0090301f + * 0.9.4 0x0090400f + * 1.2.3z 0x102031af + * + * For continuity reasons (because 0.9.5 is already out, and is coded + * 0x00905100), between 0.9.5 and 0.9.6 the coding of the patch level + * part is slightly different, by setting the highest bit. This means + * that 0.9.5a looks like this: 0x0090581f. At 0.9.6, we can start + * with 0x0090600S... + * + * (Prior to 0.9.3-dev a different scheme was used: 0.9.2b is 0x0922.) + * (Prior to 0.9.5a beta1, a different scheme was used: MMNNFFRBB for + * major minor fix final patch/beta) + */ +# define OPENSSL_VERSION_NUMBER 0x1010101fL +# define OPENSSL_VERSION_TEXT "OpenSSL 1.1.1a 20 Nov 2018" + +/*- + * The macros below are to be used for shared library (.so, .dll, ...) + * versioning. That kind of versioning works a bit differently between + * operating systems. The most usual scheme is to set a major and a minor + * number, and have the runtime loader check that the major number is equal + * to what it was at application link time, while the minor number has to + * be greater or equal to what it was at application link time. With this + * scheme, the version number is usually part of the file name, like this: + * + * libcrypto.so.0.9 + * + * Some unixen also make a softlink with the major version number only: + * + * libcrypto.so.0 + * + * On Tru64 and IRIX 6.x it works a little bit differently. There, the + * shared library version is stored in the file, and is actually a series + * of versions, separated by colons. The rightmost version present in the + * library when linking an application is stored in the application to be + * matched at run time. When the application is run, a check is done to + * see if the library version stored in the application matches any of the + * versions in the version string of the library itself. + * This version string can be constructed in any way, depending on what + * kind of matching is desired. However, to implement the same scheme as + * the one used in the other unixen, all compatible versions, from lowest + * to highest, should be part of the string. Consecutive builds would + * give the following versions strings: + * + * 3.0 + * 3.0:3.1 + * 3.0:3.1:3.2 + * 4.0 + * 4.0:4.1 + * + * Notice how version 4 is completely incompatible with version, and + * therefore give the breach you can see. + * + * There may be other schemes as well that I haven't yet discovered. + * + * So, here's the way it works here: first of all, the library version + * number doesn't need at all to match the overall OpenSSL version. + * However, it's nice and more understandable if it actually does. + * The current library version is stored in the macro SHLIB_VERSION_NUMBER, + * which is just a piece of text in the format "M.m.e" (Major, minor, edit). + * For the sake of Tru64, IRIX, and any other OS that behaves in similar ways, + * we need to keep a history of version numbers, which is done in the + * macro SHLIB_VERSION_HISTORY. The numbers are separated by colons and + * should only keep the versions that are binary compatible with the current. + */ +# define SHLIB_VERSION_HISTORY "" +# define SHLIB_VERSION_NUMBER "1.1" + + +#ifdef __cplusplus +} +#endif +#endif /* HEADER_OPENSSLV_H */ diff --git a/example/android/third_party/crypto/arm/include/openssl/ossl_typ.h b/example/android/third_party/crypto/arm/include/openssl/ossl_typ.h new file mode 100644 index 00000000..7993ca28 --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/ossl_typ.h @@ -0,0 +1,196 @@ +/* + * Copyright 2001-2018 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_OPENSSL_TYPES_H +# define HEADER_OPENSSL_TYPES_H + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +# include + +# ifdef NO_ASN1_TYPEDEFS +# define ASN1_INTEGER ASN1_STRING +# define ASN1_ENUMERATED ASN1_STRING +# define ASN1_BIT_STRING ASN1_STRING +# define ASN1_OCTET_STRING ASN1_STRING +# define ASN1_PRINTABLESTRING ASN1_STRING +# define ASN1_T61STRING ASN1_STRING +# define ASN1_IA5STRING ASN1_STRING +# define ASN1_UTCTIME ASN1_STRING +# define ASN1_GENERALIZEDTIME ASN1_STRING +# define ASN1_TIME ASN1_STRING +# define ASN1_GENERALSTRING ASN1_STRING +# define ASN1_UNIVERSALSTRING ASN1_STRING +# define ASN1_BMPSTRING ASN1_STRING +# define ASN1_VISIBLESTRING ASN1_STRING +# define ASN1_UTF8STRING ASN1_STRING +# define ASN1_BOOLEAN int +# define ASN1_NULL int +# else +typedef struct asn1_string_st ASN1_INTEGER; +typedef struct asn1_string_st ASN1_ENUMERATED; +typedef struct asn1_string_st ASN1_BIT_STRING; +typedef struct asn1_string_st ASN1_OCTET_STRING; +typedef struct asn1_string_st ASN1_PRINTABLESTRING; +typedef struct asn1_string_st ASN1_T61STRING; +typedef struct asn1_string_st ASN1_IA5STRING; +typedef struct asn1_string_st ASN1_GENERALSTRING; +typedef struct asn1_string_st ASN1_UNIVERSALSTRING; +typedef struct asn1_string_st ASN1_BMPSTRING; +typedef struct asn1_string_st ASN1_UTCTIME; +typedef struct asn1_string_st ASN1_TIME; +typedef struct asn1_string_st ASN1_GENERALIZEDTIME; +typedef struct asn1_string_st ASN1_VISIBLESTRING; +typedef struct asn1_string_st ASN1_UTF8STRING; +typedef struct asn1_string_st ASN1_STRING; +typedef int ASN1_BOOLEAN; +typedef int ASN1_NULL; +# endif + +typedef struct asn1_object_st ASN1_OBJECT; + +typedef struct ASN1_ITEM_st ASN1_ITEM; +typedef struct asn1_pctx_st ASN1_PCTX; +typedef struct asn1_sctx_st ASN1_SCTX; + +# ifdef _WIN32 +# undef X509_NAME +# undef X509_EXTENSIONS +# undef PKCS7_ISSUER_AND_SERIAL +# undef PKCS7_SIGNER_INFO +# undef OCSP_REQUEST +# undef OCSP_RESPONSE +# endif + +# ifdef BIGNUM +# undef BIGNUM +# endif +struct dane_st; +typedef struct bio_st BIO; +typedef struct bignum_st BIGNUM; +typedef struct bignum_ctx BN_CTX; +typedef struct bn_blinding_st BN_BLINDING; +typedef struct bn_mont_ctx_st BN_MONT_CTX; +typedef struct bn_recp_ctx_st BN_RECP_CTX; +typedef struct bn_gencb_st BN_GENCB; + +typedef struct buf_mem_st BUF_MEM; + +typedef struct evp_cipher_st EVP_CIPHER; +typedef struct evp_cipher_ctx_st EVP_CIPHER_CTX; +typedef struct evp_md_st EVP_MD; +typedef struct evp_md_ctx_st EVP_MD_CTX; +typedef struct evp_pkey_st EVP_PKEY; + +typedef struct evp_pkey_asn1_method_st EVP_PKEY_ASN1_METHOD; + +typedef struct evp_pkey_method_st EVP_PKEY_METHOD; +typedef struct evp_pkey_ctx_st EVP_PKEY_CTX; + +typedef struct evp_Encode_Ctx_st EVP_ENCODE_CTX; + +typedef struct hmac_ctx_st HMAC_CTX; + +typedef struct dh_st DH; +typedef struct dh_method DH_METHOD; + +typedef struct dsa_st DSA; +typedef struct dsa_method DSA_METHOD; + +typedef struct rsa_st RSA; +typedef struct rsa_meth_st RSA_METHOD; + +typedef struct ec_key_st EC_KEY; +typedef struct ec_key_method_st EC_KEY_METHOD; + +typedef struct rand_meth_st RAND_METHOD; +typedef struct rand_drbg_st RAND_DRBG; + +typedef struct ssl_dane_st SSL_DANE; +typedef struct x509_st X509; +typedef struct X509_algor_st X509_ALGOR; +typedef struct X509_crl_st X509_CRL; +typedef struct x509_crl_method_st X509_CRL_METHOD; +typedef struct x509_revoked_st X509_REVOKED; +typedef struct X509_name_st X509_NAME; +typedef struct X509_pubkey_st X509_PUBKEY; +typedef struct x509_store_st X509_STORE; +typedef struct x509_store_ctx_st X509_STORE_CTX; + +typedef struct x509_object_st X509_OBJECT; +typedef struct x509_lookup_st X509_LOOKUP; +typedef struct x509_lookup_method_st X509_LOOKUP_METHOD; +typedef struct X509_VERIFY_PARAM_st X509_VERIFY_PARAM; + +typedef struct x509_sig_info_st X509_SIG_INFO; + +typedef struct pkcs8_priv_key_info_st PKCS8_PRIV_KEY_INFO; + +typedef struct v3_ext_ctx X509V3_CTX; +typedef struct conf_st CONF; +typedef struct ossl_init_settings_st OPENSSL_INIT_SETTINGS; + +typedef struct ui_st UI; +typedef struct ui_method_st UI_METHOD; + +typedef struct engine_st ENGINE; +typedef struct ssl_st SSL; +typedef struct ssl_ctx_st SSL_CTX; + +typedef struct comp_ctx_st COMP_CTX; +typedef struct comp_method_st COMP_METHOD; + +typedef struct X509_POLICY_NODE_st X509_POLICY_NODE; +typedef struct X509_POLICY_LEVEL_st X509_POLICY_LEVEL; +typedef struct X509_POLICY_TREE_st X509_POLICY_TREE; +typedef struct X509_POLICY_CACHE_st X509_POLICY_CACHE; + +typedef struct AUTHORITY_KEYID_st AUTHORITY_KEYID; +typedef struct DIST_POINT_st DIST_POINT; +typedef struct ISSUING_DIST_POINT_st ISSUING_DIST_POINT; +typedef struct NAME_CONSTRAINTS_st NAME_CONSTRAINTS; + +typedef struct crypto_ex_data_st CRYPTO_EX_DATA; + +typedef struct ocsp_req_ctx_st OCSP_REQ_CTX; +typedef struct ocsp_response_st OCSP_RESPONSE; +typedef struct ocsp_responder_id_st OCSP_RESPID; + +typedef struct sct_st SCT; +typedef struct sct_ctx_st SCT_CTX; +typedef struct ctlog_st CTLOG; +typedef struct ctlog_store_st CTLOG_STORE; +typedef struct ct_policy_eval_ctx_st CT_POLICY_EVAL_CTX; + +typedef struct ossl_store_info_st OSSL_STORE_INFO; +typedef struct ossl_store_search_st OSSL_STORE_SEARCH; + +#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L && \ + defined(INTMAX_MAX) && defined(UINTMAX_MAX) +typedef intmax_t ossl_intmax_t; +typedef uintmax_t ossl_uintmax_t; +#else +/* + * Not long long, because the C-library can only be expected to provide + * strtoll(), strtoull() at the same time as intmax_t and strtoimax(), + * strtoumax(). Since we use these for parsing arguments, we need the + * conversion functions, not just the sizes. + */ +typedef long ossl_intmax_t; +typedef unsigned long ossl_uintmax_t; +#endif + +#ifdef __cplusplus +} +#endif +#endif /* def HEADER_OPENSSL_TYPES_H */ diff --git a/example/android/third_party/crypto/arm/include/openssl/pem.h b/example/android/third_party/crypto/arm/include/openssl/pem.h new file mode 100644 index 00000000..2ef5b5d0 --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/pem.h @@ -0,0 +1,378 @@ +/* + * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_PEM_H +# define HEADER_PEM_H + +# include +# include +# include +# include +# include +# include + +#ifdef __cplusplus +extern "C" { +#endif + +# define PEM_BUFSIZE 1024 + +# define PEM_STRING_X509_OLD "X509 CERTIFICATE" +# define PEM_STRING_X509 "CERTIFICATE" +# define PEM_STRING_X509_TRUSTED "TRUSTED CERTIFICATE" +# define PEM_STRING_X509_REQ_OLD "NEW CERTIFICATE REQUEST" +# define PEM_STRING_X509_REQ "CERTIFICATE REQUEST" +# define PEM_STRING_X509_CRL "X509 CRL" +# define PEM_STRING_EVP_PKEY "ANY PRIVATE KEY" +# define PEM_STRING_PUBLIC "PUBLIC KEY" +# define PEM_STRING_RSA "RSA PRIVATE KEY" +# define PEM_STRING_RSA_PUBLIC "RSA PUBLIC KEY" +# define PEM_STRING_DSA "DSA PRIVATE KEY" +# define PEM_STRING_DSA_PUBLIC "DSA PUBLIC KEY" +# define PEM_STRING_PKCS7 "PKCS7" +# define PEM_STRING_PKCS7_SIGNED "PKCS #7 SIGNED DATA" +# define PEM_STRING_PKCS8 "ENCRYPTED PRIVATE KEY" +# define PEM_STRING_PKCS8INF "PRIVATE KEY" +# define PEM_STRING_DHPARAMS "DH PARAMETERS" +# define PEM_STRING_DHXPARAMS "X9.42 DH PARAMETERS" +# define PEM_STRING_SSL_SESSION "SSL SESSION PARAMETERS" +# define PEM_STRING_DSAPARAMS "DSA PARAMETERS" +# define PEM_STRING_ECDSA_PUBLIC "ECDSA PUBLIC KEY" +# define PEM_STRING_ECPARAMETERS "EC PARAMETERS" +# define PEM_STRING_ECPRIVATEKEY "EC PRIVATE KEY" +# define PEM_STRING_PARAMETERS "PARAMETERS" +# define PEM_STRING_CMS "CMS" + +# define PEM_TYPE_ENCRYPTED 10 +# define PEM_TYPE_MIC_ONLY 20 +# define PEM_TYPE_MIC_CLEAR 30 +# define PEM_TYPE_CLEAR 40 + +/* + * These macros make the PEM_read/PEM_write functions easier to maintain and + * write. Now they are all implemented with either: IMPLEMENT_PEM_rw(...) or + * IMPLEMENT_PEM_rw_cb(...) + */ + +# ifdef OPENSSL_NO_STDIO + +# define IMPLEMENT_PEM_read_fp(name, type, str, asn1) /**/ +# define IMPLEMENT_PEM_write_fp(name, type, str, asn1) /**/ +# define IMPLEMENT_PEM_write_fp_const(name, type, str, asn1) /**/ +# define IMPLEMENT_PEM_write_cb_fp(name, type, str, asn1) /**/ +# define IMPLEMENT_PEM_write_cb_fp_const(name, type, str, asn1) /**/ +# else + +# define IMPLEMENT_PEM_read_fp(name, type, str, asn1) \ +type *PEM_read_##name(FILE *fp, type **x, pem_password_cb *cb, void *u)\ +{ \ +return PEM_ASN1_read((d2i_of_void *)d2i_##asn1, str,fp,(void **)x,cb,u); \ +} + +# define IMPLEMENT_PEM_write_fp(name, type, str, asn1) \ +int PEM_write_##name(FILE *fp, type *x) \ +{ \ +return PEM_ASN1_write((i2d_of_void *)i2d_##asn1,str,fp,x,NULL,NULL,0,NULL,NULL); \ +} + +# define IMPLEMENT_PEM_write_fp_const(name, type, str, asn1) \ +int PEM_write_##name(FILE *fp, const type *x) \ +{ \ +return PEM_ASN1_write((i2d_of_void *)i2d_##asn1,str,fp,(void *)x,NULL,NULL,0,NULL,NULL); \ +} + +# define IMPLEMENT_PEM_write_cb_fp(name, type, str, asn1) \ +int PEM_write_##name(FILE *fp, type *x, const EVP_CIPHER *enc, \ + unsigned char *kstr, int klen, pem_password_cb *cb, \ + void *u) \ + { \ + return PEM_ASN1_write((i2d_of_void *)i2d_##asn1,str,fp,x,enc,kstr,klen,cb,u); \ + } + +# define IMPLEMENT_PEM_write_cb_fp_const(name, type, str, asn1) \ +int PEM_write_##name(FILE *fp, type *x, const EVP_CIPHER *enc, \ + unsigned char *kstr, int klen, pem_password_cb *cb, \ + void *u) \ + { \ + return PEM_ASN1_write((i2d_of_void *)i2d_##asn1,str,fp,x,enc,kstr,klen,cb,u); \ + } + +# endif + +# define IMPLEMENT_PEM_read_bio(name, type, str, asn1) \ +type *PEM_read_bio_##name(BIO *bp, type **x, pem_password_cb *cb, void *u)\ +{ \ +return PEM_ASN1_read_bio((d2i_of_void *)d2i_##asn1, str,bp,(void **)x,cb,u); \ +} + +# define IMPLEMENT_PEM_write_bio(name, type, str, asn1) \ +int PEM_write_bio_##name(BIO *bp, type *x) \ +{ \ +return PEM_ASN1_write_bio((i2d_of_void *)i2d_##asn1,str,bp,x,NULL,NULL,0,NULL,NULL); \ +} + +# define IMPLEMENT_PEM_write_bio_const(name, type, str, asn1) \ +int PEM_write_bio_##name(BIO *bp, const type *x) \ +{ \ +return PEM_ASN1_write_bio((i2d_of_void *)i2d_##asn1,str,bp,(void *)x,NULL,NULL,0,NULL,NULL); \ +} + +# define IMPLEMENT_PEM_write_cb_bio(name, type, str, asn1) \ +int PEM_write_bio_##name(BIO *bp, type *x, const EVP_CIPHER *enc, \ + unsigned char *kstr, int klen, pem_password_cb *cb, void *u) \ + { \ + return PEM_ASN1_write_bio((i2d_of_void *)i2d_##asn1,str,bp,x,enc,kstr,klen,cb,u); \ + } + +# define IMPLEMENT_PEM_write_cb_bio_const(name, type, str, asn1) \ +int PEM_write_bio_##name(BIO *bp, type *x, const EVP_CIPHER *enc, \ + unsigned char *kstr, int klen, pem_password_cb *cb, void *u) \ + { \ + return PEM_ASN1_write_bio((i2d_of_void *)i2d_##asn1,str,bp,(void *)x,enc,kstr,klen,cb,u); \ + } + +# define IMPLEMENT_PEM_write(name, type, str, asn1) \ + IMPLEMENT_PEM_write_bio(name, type, str, asn1) \ + IMPLEMENT_PEM_write_fp(name, type, str, asn1) + +# define IMPLEMENT_PEM_write_const(name, type, str, asn1) \ + IMPLEMENT_PEM_write_bio_const(name, type, str, asn1) \ + IMPLEMENT_PEM_write_fp_const(name, type, str, asn1) + +# define IMPLEMENT_PEM_write_cb(name, type, str, asn1) \ + IMPLEMENT_PEM_write_cb_bio(name, type, str, asn1) \ + IMPLEMENT_PEM_write_cb_fp(name, type, str, asn1) + +# define IMPLEMENT_PEM_write_cb_const(name, type, str, asn1) \ + IMPLEMENT_PEM_write_cb_bio_const(name, type, str, asn1) \ + IMPLEMENT_PEM_write_cb_fp_const(name, type, str, asn1) + +# define IMPLEMENT_PEM_read(name, type, str, asn1) \ + IMPLEMENT_PEM_read_bio(name, type, str, asn1) \ + IMPLEMENT_PEM_read_fp(name, type, str, asn1) + +# define IMPLEMENT_PEM_rw(name, type, str, asn1) \ + IMPLEMENT_PEM_read(name, type, str, asn1) \ + IMPLEMENT_PEM_write(name, type, str, asn1) + +# define IMPLEMENT_PEM_rw_const(name, type, str, asn1) \ + IMPLEMENT_PEM_read(name, type, str, asn1) \ + IMPLEMENT_PEM_write_const(name, type, str, asn1) + +# define IMPLEMENT_PEM_rw_cb(name, type, str, asn1) \ + IMPLEMENT_PEM_read(name, type, str, asn1) \ + IMPLEMENT_PEM_write_cb(name, type, str, asn1) + +/* These are the same except they are for the declarations */ + +# if defined(OPENSSL_NO_STDIO) + +# define DECLARE_PEM_read_fp(name, type) /**/ +# define DECLARE_PEM_write_fp(name, type) /**/ +# define DECLARE_PEM_write_fp_const(name, type) /**/ +# define DECLARE_PEM_write_cb_fp(name, type) /**/ +# else + +# define DECLARE_PEM_read_fp(name, type) \ + type *PEM_read_##name(FILE *fp, type **x, pem_password_cb *cb, void *u); + +# define DECLARE_PEM_write_fp(name, type) \ + int PEM_write_##name(FILE *fp, type *x); + +# define DECLARE_PEM_write_fp_const(name, type) \ + int PEM_write_##name(FILE *fp, const type *x); + +# define DECLARE_PEM_write_cb_fp(name, type) \ + int PEM_write_##name(FILE *fp, type *x, const EVP_CIPHER *enc, \ + unsigned char *kstr, int klen, pem_password_cb *cb, void *u); + +# endif + +# define DECLARE_PEM_read_bio(name, type) \ + type *PEM_read_bio_##name(BIO *bp, type **x, pem_password_cb *cb, void *u); + +# define DECLARE_PEM_write_bio(name, type) \ + int PEM_write_bio_##name(BIO *bp, type *x); + +# define DECLARE_PEM_write_bio_const(name, type) \ + int PEM_write_bio_##name(BIO *bp, const type *x); + +# define DECLARE_PEM_write_cb_bio(name, type) \ + int PEM_write_bio_##name(BIO *bp, type *x, const EVP_CIPHER *enc, \ + unsigned char *kstr, int klen, pem_password_cb *cb, void *u); + +# define DECLARE_PEM_write(name, type) \ + DECLARE_PEM_write_bio(name, type) \ + DECLARE_PEM_write_fp(name, type) +# define DECLARE_PEM_write_const(name, type) \ + DECLARE_PEM_write_bio_const(name, type) \ + DECLARE_PEM_write_fp_const(name, type) +# define DECLARE_PEM_write_cb(name, type) \ + DECLARE_PEM_write_cb_bio(name, type) \ + DECLARE_PEM_write_cb_fp(name, type) +# define DECLARE_PEM_read(name, type) \ + DECLARE_PEM_read_bio(name, type) \ + DECLARE_PEM_read_fp(name, type) +# define DECLARE_PEM_rw(name, type) \ + DECLARE_PEM_read(name, type) \ + DECLARE_PEM_write(name, type) +# define DECLARE_PEM_rw_const(name, type) \ + DECLARE_PEM_read(name, type) \ + DECLARE_PEM_write_const(name, type) +# define DECLARE_PEM_rw_cb(name, type) \ + DECLARE_PEM_read(name, type) \ + DECLARE_PEM_write_cb(name, type) +typedef int pem_password_cb (char *buf, int size, int rwflag, void *userdata); + +int PEM_get_EVP_CIPHER_INFO(char *header, EVP_CIPHER_INFO *cipher); +int PEM_do_header(EVP_CIPHER_INFO *cipher, unsigned char *data, long *len, + pem_password_cb *callback, void *u); + +int PEM_read_bio(BIO *bp, char **name, char **header, + unsigned char **data, long *len); +# define PEM_FLAG_SECURE 0x1 +# define PEM_FLAG_EAY_COMPATIBLE 0x2 +# define PEM_FLAG_ONLY_B64 0x4 +int PEM_read_bio_ex(BIO *bp, char **name, char **header, + unsigned char **data, long *len, unsigned int flags); +int PEM_bytes_read_bio_secmem(unsigned char **pdata, long *plen, char **pnm, + const char *name, BIO *bp, pem_password_cb *cb, + void *u); +int PEM_write_bio(BIO *bp, const char *name, const char *hdr, + const unsigned char *data, long len); +int PEM_bytes_read_bio(unsigned char **pdata, long *plen, char **pnm, + const char *name, BIO *bp, pem_password_cb *cb, + void *u); +void *PEM_ASN1_read_bio(d2i_of_void *d2i, const char *name, BIO *bp, void **x, + pem_password_cb *cb, void *u); +int PEM_ASN1_write_bio(i2d_of_void *i2d, const char *name, BIO *bp, void *x, + const EVP_CIPHER *enc, unsigned char *kstr, int klen, + pem_password_cb *cb, void *u); + +STACK_OF(X509_INFO) *PEM_X509_INFO_read_bio(BIO *bp, STACK_OF(X509_INFO) *sk, + pem_password_cb *cb, void *u); +int PEM_X509_INFO_write_bio(BIO *bp, X509_INFO *xi, EVP_CIPHER *enc, + unsigned char *kstr, int klen, + pem_password_cb *cd, void *u); + +#ifndef OPENSSL_NO_STDIO +int PEM_read(FILE *fp, char **name, char **header, + unsigned char **data, long *len); +int PEM_write(FILE *fp, const char *name, const char *hdr, + const unsigned char *data, long len); +void *PEM_ASN1_read(d2i_of_void *d2i, const char *name, FILE *fp, void **x, + pem_password_cb *cb, void *u); +int PEM_ASN1_write(i2d_of_void *i2d, const char *name, FILE *fp, + void *x, const EVP_CIPHER *enc, unsigned char *kstr, + int klen, pem_password_cb *callback, void *u); +STACK_OF(X509_INFO) *PEM_X509_INFO_read(FILE *fp, STACK_OF(X509_INFO) *sk, + pem_password_cb *cb, void *u); +#endif + +int PEM_SignInit(EVP_MD_CTX *ctx, EVP_MD *type); +int PEM_SignUpdate(EVP_MD_CTX *ctx, unsigned char *d, unsigned int cnt); +int PEM_SignFinal(EVP_MD_CTX *ctx, unsigned char *sigret, + unsigned int *siglen, EVP_PKEY *pkey); + +/* The default pem_password_cb that's used internally */ +int PEM_def_callback(char *buf, int num, int rwflag, void *userdata); +void PEM_proc_type(char *buf, int type); +void PEM_dek_info(char *buf, const char *type, int len, char *str); + +# include + +DECLARE_PEM_rw(X509, X509) +DECLARE_PEM_rw(X509_AUX, X509) +DECLARE_PEM_rw(X509_REQ, X509_REQ) +DECLARE_PEM_write(X509_REQ_NEW, X509_REQ) +DECLARE_PEM_rw(X509_CRL, X509_CRL) +DECLARE_PEM_rw(PKCS7, PKCS7) +DECLARE_PEM_rw(NETSCAPE_CERT_SEQUENCE, NETSCAPE_CERT_SEQUENCE) +DECLARE_PEM_rw(PKCS8, X509_SIG) +DECLARE_PEM_rw(PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO) +# ifndef OPENSSL_NO_RSA +DECLARE_PEM_rw_cb(RSAPrivateKey, RSA) +DECLARE_PEM_rw_const(RSAPublicKey, RSA) +DECLARE_PEM_rw(RSA_PUBKEY, RSA) +# endif +# ifndef OPENSSL_NO_DSA +DECLARE_PEM_rw_cb(DSAPrivateKey, DSA) +DECLARE_PEM_rw(DSA_PUBKEY, DSA) +DECLARE_PEM_rw_const(DSAparams, DSA) +# endif +# ifndef OPENSSL_NO_EC +DECLARE_PEM_rw_const(ECPKParameters, EC_GROUP) +DECLARE_PEM_rw_cb(ECPrivateKey, EC_KEY) +DECLARE_PEM_rw(EC_PUBKEY, EC_KEY) +# endif +# ifndef OPENSSL_NO_DH +DECLARE_PEM_rw_const(DHparams, DH) +DECLARE_PEM_write_const(DHxparams, DH) +# endif +DECLARE_PEM_rw_cb(PrivateKey, EVP_PKEY) +DECLARE_PEM_rw(PUBKEY, EVP_PKEY) + +int PEM_write_bio_PrivateKey_traditional(BIO *bp, EVP_PKEY *x, + const EVP_CIPHER *enc, + unsigned char *kstr, int klen, + pem_password_cb *cb, void *u); + +int PEM_write_bio_PKCS8PrivateKey_nid(BIO *bp, EVP_PKEY *x, int nid, + char *kstr, int klen, + pem_password_cb *cb, void *u); +int PEM_write_bio_PKCS8PrivateKey(BIO *, EVP_PKEY *, const EVP_CIPHER *, + char *, int, pem_password_cb *, void *); +int i2d_PKCS8PrivateKey_bio(BIO *bp, EVP_PKEY *x, const EVP_CIPHER *enc, + char *kstr, int klen, + pem_password_cb *cb, void *u); +int i2d_PKCS8PrivateKey_nid_bio(BIO *bp, EVP_PKEY *x, int nid, + char *kstr, int klen, + pem_password_cb *cb, void *u); +EVP_PKEY *d2i_PKCS8PrivateKey_bio(BIO *bp, EVP_PKEY **x, pem_password_cb *cb, + void *u); + +# ifndef OPENSSL_NO_STDIO +int i2d_PKCS8PrivateKey_fp(FILE *fp, EVP_PKEY *x, const EVP_CIPHER *enc, + char *kstr, int klen, + pem_password_cb *cb, void *u); +int i2d_PKCS8PrivateKey_nid_fp(FILE *fp, EVP_PKEY *x, int nid, + char *kstr, int klen, + pem_password_cb *cb, void *u); +int PEM_write_PKCS8PrivateKey_nid(FILE *fp, EVP_PKEY *x, int nid, + char *kstr, int klen, + pem_password_cb *cb, void *u); + +EVP_PKEY *d2i_PKCS8PrivateKey_fp(FILE *fp, EVP_PKEY **x, pem_password_cb *cb, + void *u); + +int PEM_write_PKCS8PrivateKey(FILE *fp, EVP_PKEY *x, const EVP_CIPHER *enc, + char *kstr, int klen, pem_password_cb *cd, + void *u); +# endif +EVP_PKEY *PEM_read_bio_Parameters(BIO *bp, EVP_PKEY **x); +int PEM_write_bio_Parameters(BIO *bp, EVP_PKEY *x); + +# ifndef OPENSSL_NO_DSA +EVP_PKEY *b2i_PrivateKey(const unsigned char **in, long length); +EVP_PKEY *b2i_PublicKey(const unsigned char **in, long length); +EVP_PKEY *b2i_PrivateKey_bio(BIO *in); +EVP_PKEY *b2i_PublicKey_bio(BIO *in); +int i2b_PrivateKey_bio(BIO *out, EVP_PKEY *pk); +int i2b_PublicKey_bio(BIO *out, EVP_PKEY *pk); +# ifndef OPENSSL_NO_RC4 +EVP_PKEY *b2i_PVK_bio(BIO *in, pem_password_cb *cb, void *u); +int i2b_PVK_bio(BIO *out, EVP_PKEY *pk, int enclevel, + pem_password_cb *cb, void *u); +# endif +# endif + +# ifdef __cplusplus +} +# endif +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/pem2.h b/example/android/third_party/crypto/arm/include/openssl/pem2.h new file mode 100644 index 00000000..038fe790 --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/pem2.h @@ -0,0 +1,13 @@ +/* + * Copyright 1999-2018 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_PEM2_H +# define HEADER_PEM2_H +# include +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/pemerr.h b/example/android/third_party/crypto/arm/include/openssl/pemerr.h new file mode 100644 index 00000000..cd61b823 --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/pemerr.h @@ -0,0 +1,99 @@ +/* + * Generated by util/mkerr.pl DO NOT EDIT + * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_PEMERR_H +# define HEADER_PEMERR_H + +# ifdef __cplusplus +extern "C" +# endif +int ERR_load_PEM_strings(void); + +/* + * PEM function codes. + */ +# define PEM_F_B2I_DSS 127 +# define PEM_F_B2I_PVK_BIO 128 +# define PEM_F_B2I_RSA 129 +# define PEM_F_CHECK_BITLEN_DSA 130 +# define PEM_F_CHECK_BITLEN_RSA 131 +# define PEM_F_D2I_PKCS8PRIVATEKEY_BIO 120 +# define PEM_F_D2I_PKCS8PRIVATEKEY_FP 121 +# define PEM_F_DO_B2I 132 +# define PEM_F_DO_B2I_BIO 133 +# define PEM_F_DO_BLOB_HEADER 134 +# define PEM_F_DO_I2B 146 +# define PEM_F_DO_PK8PKEY 126 +# define PEM_F_DO_PK8PKEY_FP 125 +# define PEM_F_DO_PVK_BODY 135 +# define PEM_F_DO_PVK_HEADER 136 +# define PEM_F_GET_HEADER_AND_DATA 143 +# define PEM_F_GET_NAME 144 +# define PEM_F_I2B_PVK 137 +# define PEM_F_I2B_PVK_BIO 138 +# define PEM_F_LOAD_IV 101 +# define PEM_F_PEM_ASN1_READ 102 +# define PEM_F_PEM_ASN1_READ_BIO 103 +# define PEM_F_PEM_ASN1_WRITE 104 +# define PEM_F_PEM_ASN1_WRITE_BIO 105 +# define PEM_F_PEM_DEF_CALLBACK 100 +# define PEM_F_PEM_DO_HEADER 106 +# define PEM_F_PEM_GET_EVP_CIPHER_INFO 107 +# define PEM_F_PEM_READ 108 +# define PEM_F_PEM_READ_BIO 109 +# define PEM_F_PEM_READ_BIO_DHPARAMS 141 +# define PEM_F_PEM_READ_BIO_EX 145 +# define PEM_F_PEM_READ_BIO_PARAMETERS 140 +# define PEM_F_PEM_READ_BIO_PRIVATEKEY 123 +# define PEM_F_PEM_READ_DHPARAMS 142 +# define PEM_F_PEM_READ_PRIVATEKEY 124 +# define PEM_F_PEM_SIGNFINAL 112 +# define PEM_F_PEM_WRITE 113 +# define PEM_F_PEM_WRITE_BIO 114 +# define PEM_F_PEM_WRITE_PRIVATEKEY 139 +# define PEM_F_PEM_X509_INFO_READ 115 +# define PEM_F_PEM_X509_INFO_READ_BIO 116 +# define PEM_F_PEM_X509_INFO_WRITE_BIO 117 + +/* + * PEM reason codes. + */ +# define PEM_R_BAD_BASE64_DECODE 100 +# define PEM_R_BAD_DECRYPT 101 +# define PEM_R_BAD_END_LINE 102 +# define PEM_R_BAD_IV_CHARS 103 +# define PEM_R_BAD_MAGIC_NUMBER 116 +# define PEM_R_BAD_PASSWORD_READ 104 +# define PEM_R_BAD_VERSION_NUMBER 117 +# define PEM_R_BIO_WRITE_FAILURE 118 +# define PEM_R_CIPHER_IS_NULL 127 +# define PEM_R_ERROR_CONVERTING_PRIVATE_KEY 115 +# define PEM_R_EXPECTING_PRIVATE_KEY_BLOB 119 +# define PEM_R_EXPECTING_PUBLIC_KEY_BLOB 120 +# define PEM_R_HEADER_TOO_LONG 128 +# define PEM_R_INCONSISTENT_HEADER 121 +# define PEM_R_KEYBLOB_HEADER_PARSE_ERROR 122 +# define PEM_R_KEYBLOB_TOO_SHORT 123 +# define PEM_R_MISSING_DEK_IV 129 +# define PEM_R_NOT_DEK_INFO 105 +# define PEM_R_NOT_ENCRYPTED 106 +# define PEM_R_NOT_PROC_TYPE 107 +# define PEM_R_NO_START_LINE 108 +# define PEM_R_PROBLEMS_GETTING_PASSWORD 109 +# define PEM_R_PVK_DATA_TOO_SHORT 124 +# define PEM_R_PVK_TOO_SHORT 125 +# define PEM_R_READ_KEY 111 +# define PEM_R_SHORT_HEADER 112 +# define PEM_R_UNEXPECTED_DEK_IV 130 +# define PEM_R_UNSUPPORTED_CIPHER 113 +# define PEM_R_UNSUPPORTED_ENCRYPTION 114 +# define PEM_R_UNSUPPORTED_KEY_COMPONENTS 126 + +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/pkcs12.h b/example/android/third_party/crypto/arm/include/openssl/pkcs12.h new file mode 100644 index 00000000..3f43dad6 --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/pkcs12.h @@ -0,0 +1,223 @@ +/* + * Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_PKCS12_H +# define HEADER_PKCS12_H + +# include +# include +# include + +#ifdef __cplusplus +extern "C" { +#endif + +# define PKCS12_KEY_ID 1 +# define PKCS12_IV_ID 2 +# define PKCS12_MAC_ID 3 + +/* Default iteration count */ +# ifndef PKCS12_DEFAULT_ITER +# define PKCS12_DEFAULT_ITER PKCS5_DEFAULT_ITER +# endif + +# define PKCS12_MAC_KEY_LENGTH 20 + +# define PKCS12_SALT_LEN 8 + +/* It's not clear if these are actually needed... */ +# define PKCS12_key_gen PKCS12_key_gen_utf8 +# define PKCS12_add_friendlyname PKCS12_add_friendlyname_utf8 + +/* MS key usage constants */ + +# define KEY_EX 0x10 +# define KEY_SIG 0x80 + +typedef struct PKCS12_MAC_DATA_st PKCS12_MAC_DATA; + +typedef struct PKCS12_st PKCS12; + +typedef struct PKCS12_SAFEBAG_st PKCS12_SAFEBAG; + +DEFINE_STACK_OF(PKCS12_SAFEBAG) + +typedef struct pkcs12_bag_st PKCS12_BAGS; + +# define PKCS12_ERROR 0 +# define PKCS12_OK 1 + +/* Compatibility macros */ + +#if OPENSSL_API_COMPAT < 0x10100000L + +# define M_PKCS12_bag_type PKCS12_bag_type +# define M_PKCS12_cert_bag_type PKCS12_cert_bag_type +# define M_PKCS12_crl_bag_type PKCS12_cert_bag_type + +# define PKCS12_certbag2x509 PKCS12_SAFEBAG_get1_cert +# define PKCS12_certbag2scrl PKCS12_SAFEBAG_get1_crl +# define PKCS12_bag_type PKCS12_SAFEBAG_get_nid +# define PKCS12_cert_bag_type PKCS12_SAFEBAG_get_bag_nid +# define PKCS12_x5092certbag PKCS12_SAFEBAG_create_cert +# define PKCS12_x509crl2certbag PKCS12_SAFEBAG_create_crl +# define PKCS12_MAKE_KEYBAG PKCS12_SAFEBAG_create0_p8inf +# define PKCS12_MAKE_SHKEYBAG PKCS12_SAFEBAG_create_pkcs8_encrypt + +#endif + +DEPRECATEDIN_1_1_0(ASN1_TYPE *PKCS12_get_attr(const PKCS12_SAFEBAG *bag, int attr_nid)) + +ASN1_TYPE *PKCS8_get_attr(PKCS8_PRIV_KEY_INFO *p8, int attr_nid); +int PKCS12_mac_present(const PKCS12 *p12); +void PKCS12_get0_mac(const ASN1_OCTET_STRING **pmac, + const X509_ALGOR **pmacalg, + const ASN1_OCTET_STRING **psalt, + const ASN1_INTEGER **piter, + const PKCS12 *p12); + +const ASN1_TYPE *PKCS12_SAFEBAG_get0_attr(const PKCS12_SAFEBAG *bag, + int attr_nid); +const ASN1_OBJECT *PKCS12_SAFEBAG_get0_type(const PKCS12_SAFEBAG *bag); +int PKCS12_SAFEBAG_get_nid(const PKCS12_SAFEBAG *bag); +int PKCS12_SAFEBAG_get_bag_nid(const PKCS12_SAFEBAG *bag); + +X509 *PKCS12_SAFEBAG_get1_cert(const PKCS12_SAFEBAG *bag); +X509_CRL *PKCS12_SAFEBAG_get1_crl(const PKCS12_SAFEBAG *bag); +const STACK_OF(PKCS12_SAFEBAG) * +PKCS12_SAFEBAG_get0_safes(const PKCS12_SAFEBAG *bag); +const PKCS8_PRIV_KEY_INFO *PKCS12_SAFEBAG_get0_p8inf(const PKCS12_SAFEBAG *bag); +const X509_SIG *PKCS12_SAFEBAG_get0_pkcs8(const PKCS12_SAFEBAG *bag); + +PKCS12_SAFEBAG *PKCS12_SAFEBAG_create_cert(X509 *x509); +PKCS12_SAFEBAG *PKCS12_SAFEBAG_create_crl(X509_CRL *crl); +PKCS12_SAFEBAG *PKCS12_SAFEBAG_create0_p8inf(PKCS8_PRIV_KEY_INFO *p8); +PKCS12_SAFEBAG *PKCS12_SAFEBAG_create0_pkcs8(X509_SIG *p8); +PKCS12_SAFEBAG *PKCS12_SAFEBAG_create_pkcs8_encrypt(int pbe_nid, + const char *pass, + int passlen, + unsigned char *salt, + int saltlen, int iter, + PKCS8_PRIV_KEY_INFO *p8inf); + +PKCS12_SAFEBAG *PKCS12_item_pack_safebag(void *obj, const ASN1_ITEM *it, + int nid1, int nid2); +PKCS8_PRIV_KEY_INFO *PKCS8_decrypt(const X509_SIG *p8, const char *pass, + int passlen); +PKCS8_PRIV_KEY_INFO *PKCS12_decrypt_skey(const PKCS12_SAFEBAG *bag, + const char *pass, int passlen); +X509_SIG *PKCS8_encrypt(int pbe_nid, const EVP_CIPHER *cipher, + const char *pass, int passlen, unsigned char *salt, + int saltlen, int iter, PKCS8_PRIV_KEY_INFO *p8); +X509_SIG *PKCS8_set0_pbe(const char *pass, int passlen, + PKCS8_PRIV_KEY_INFO *p8inf, X509_ALGOR *pbe); +PKCS7 *PKCS12_pack_p7data(STACK_OF(PKCS12_SAFEBAG) *sk); +STACK_OF(PKCS12_SAFEBAG) *PKCS12_unpack_p7data(PKCS7 *p7); +PKCS7 *PKCS12_pack_p7encdata(int pbe_nid, const char *pass, int passlen, + unsigned char *salt, int saltlen, int iter, + STACK_OF(PKCS12_SAFEBAG) *bags); +STACK_OF(PKCS12_SAFEBAG) *PKCS12_unpack_p7encdata(PKCS7 *p7, const char *pass, + int passlen); + +int PKCS12_pack_authsafes(PKCS12 *p12, STACK_OF(PKCS7) *safes); +STACK_OF(PKCS7) *PKCS12_unpack_authsafes(const PKCS12 *p12); + +int PKCS12_add_localkeyid(PKCS12_SAFEBAG *bag, unsigned char *name, + int namelen); +int PKCS12_add_friendlyname_asc(PKCS12_SAFEBAG *bag, const char *name, + int namelen); +int PKCS12_add_friendlyname_utf8(PKCS12_SAFEBAG *bag, const char *name, + int namelen); +int PKCS12_add_CSPName_asc(PKCS12_SAFEBAG *bag, const char *name, + int namelen); +int PKCS12_add_friendlyname_uni(PKCS12_SAFEBAG *bag, + const unsigned char *name, int namelen); +int PKCS8_add_keyusage(PKCS8_PRIV_KEY_INFO *p8, int usage); +ASN1_TYPE *PKCS12_get_attr_gen(const STACK_OF(X509_ATTRIBUTE) *attrs, + int attr_nid); +char *PKCS12_get_friendlyname(PKCS12_SAFEBAG *bag); +const STACK_OF(X509_ATTRIBUTE) * +PKCS12_SAFEBAG_get0_attrs(const PKCS12_SAFEBAG *bag); +unsigned char *PKCS12_pbe_crypt(const X509_ALGOR *algor, + const char *pass, int passlen, + const unsigned char *in, int inlen, + unsigned char **data, int *datalen, + int en_de); +void *PKCS12_item_decrypt_d2i(const X509_ALGOR *algor, const ASN1_ITEM *it, + const char *pass, int passlen, + const ASN1_OCTET_STRING *oct, int zbuf); +ASN1_OCTET_STRING *PKCS12_item_i2d_encrypt(X509_ALGOR *algor, + const ASN1_ITEM *it, + const char *pass, int passlen, + void *obj, int zbuf); +PKCS12 *PKCS12_init(int mode); +int PKCS12_key_gen_asc(const char *pass, int passlen, unsigned char *salt, + int saltlen, int id, int iter, int n, + unsigned char *out, const EVP_MD *md_type); +int PKCS12_key_gen_uni(unsigned char *pass, int passlen, unsigned char *salt, + int saltlen, int id, int iter, int n, + unsigned char *out, const EVP_MD *md_type); +int PKCS12_key_gen_utf8(const char *pass, int passlen, unsigned char *salt, + int saltlen, int id, int iter, int n, + unsigned char *out, const EVP_MD *md_type); +int PKCS12_PBE_keyivgen(EVP_CIPHER_CTX *ctx, const char *pass, int passlen, + ASN1_TYPE *param, const EVP_CIPHER *cipher, + const EVP_MD *md_type, int en_de); +int PKCS12_gen_mac(PKCS12 *p12, const char *pass, int passlen, + unsigned char *mac, unsigned int *maclen); +int PKCS12_verify_mac(PKCS12 *p12, const char *pass, int passlen); +int PKCS12_set_mac(PKCS12 *p12, const char *pass, int passlen, + unsigned char *salt, int saltlen, int iter, + const EVP_MD *md_type); +int PKCS12_setup_mac(PKCS12 *p12, int iter, unsigned char *salt, + int saltlen, const EVP_MD *md_type); +unsigned char *OPENSSL_asc2uni(const char *asc, int asclen, + unsigned char **uni, int *unilen); +char *OPENSSL_uni2asc(const unsigned char *uni, int unilen); +unsigned char *OPENSSL_utf82uni(const char *asc, int asclen, + unsigned char **uni, int *unilen); +char *OPENSSL_uni2utf8(const unsigned char *uni, int unilen); + +DECLARE_ASN1_FUNCTIONS(PKCS12) +DECLARE_ASN1_FUNCTIONS(PKCS12_MAC_DATA) +DECLARE_ASN1_FUNCTIONS(PKCS12_SAFEBAG) +DECLARE_ASN1_FUNCTIONS(PKCS12_BAGS) + +DECLARE_ASN1_ITEM(PKCS12_SAFEBAGS) +DECLARE_ASN1_ITEM(PKCS12_AUTHSAFES) + +void PKCS12_PBE_add(void); +int PKCS12_parse(PKCS12 *p12, const char *pass, EVP_PKEY **pkey, X509 **cert, + STACK_OF(X509) **ca); +PKCS12 *PKCS12_create(const char *pass, const char *name, EVP_PKEY *pkey, + X509 *cert, STACK_OF(X509) *ca, int nid_key, int nid_cert, + int iter, int mac_iter, int keytype); + +PKCS12_SAFEBAG *PKCS12_add_cert(STACK_OF(PKCS12_SAFEBAG) **pbags, X509 *cert); +PKCS12_SAFEBAG *PKCS12_add_key(STACK_OF(PKCS12_SAFEBAG) **pbags, + EVP_PKEY *key, int key_usage, int iter, + int key_nid, const char *pass); +int PKCS12_add_safe(STACK_OF(PKCS7) **psafes, STACK_OF(PKCS12_SAFEBAG) *bags, + int safe_nid, int iter, const char *pass); +PKCS12 *PKCS12_add_safes(STACK_OF(PKCS7) *safes, int p7_nid); + +int i2d_PKCS12_bio(BIO *bp, PKCS12 *p12); +# ifndef OPENSSL_NO_STDIO +int i2d_PKCS12_fp(FILE *fp, PKCS12 *p12); +# endif +PKCS12 *d2i_PKCS12_bio(BIO *bp, PKCS12 **p12); +# ifndef OPENSSL_NO_STDIO +PKCS12 *d2i_PKCS12_fp(FILE *fp, PKCS12 **p12); +# endif +int PKCS12_newpass(PKCS12 *p12, const char *oldpass, const char *newpass); + +# ifdef __cplusplus +} +# endif +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/pkcs12err.h b/example/android/third_party/crypto/arm/include/openssl/pkcs12err.h new file mode 100644 index 00000000..c7184ffe --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/pkcs12err.h @@ -0,0 +1,77 @@ +/* + * Generated by util/mkerr.pl DO NOT EDIT + * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_PKCS12ERR_H +# define HEADER_PKCS12ERR_H + +# ifdef __cplusplus +extern "C" +# endif +int ERR_load_PKCS12_strings(void); + +/* + * PKCS12 function codes. + */ +# define PKCS12_F_OPENSSL_ASC2UNI 121 +# define PKCS12_F_OPENSSL_UNI2ASC 124 +# define PKCS12_F_OPENSSL_UNI2UTF8 127 +# define PKCS12_F_OPENSSL_UTF82UNI 129 +# define PKCS12_F_PKCS12_CREATE 105 +# define PKCS12_F_PKCS12_GEN_MAC 107 +# define PKCS12_F_PKCS12_INIT 109 +# define PKCS12_F_PKCS12_ITEM_DECRYPT_D2I 106 +# define PKCS12_F_PKCS12_ITEM_I2D_ENCRYPT 108 +# define PKCS12_F_PKCS12_ITEM_PACK_SAFEBAG 117 +# define PKCS12_F_PKCS12_KEY_GEN_ASC 110 +# define PKCS12_F_PKCS12_KEY_GEN_UNI 111 +# define PKCS12_F_PKCS12_KEY_GEN_UTF8 116 +# define PKCS12_F_PKCS12_NEWPASS 128 +# define PKCS12_F_PKCS12_PACK_P7DATA 114 +# define PKCS12_F_PKCS12_PACK_P7ENCDATA 115 +# define PKCS12_F_PKCS12_PARSE 118 +# define PKCS12_F_PKCS12_PBE_CRYPT 119 +# define PKCS12_F_PKCS12_PBE_KEYIVGEN 120 +# define PKCS12_F_PKCS12_SAFEBAG_CREATE0_P8INF 112 +# define PKCS12_F_PKCS12_SAFEBAG_CREATE0_PKCS8 113 +# define PKCS12_F_PKCS12_SAFEBAG_CREATE_PKCS8_ENCRYPT 133 +# define PKCS12_F_PKCS12_SETUP_MAC 122 +# define PKCS12_F_PKCS12_SET_MAC 123 +# define PKCS12_F_PKCS12_UNPACK_AUTHSAFES 130 +# define PKCS12_F_PKCS12_UNPACK_P7DATA 131 +# define PKCS12_F_PKCS12_VERIFY_MAC 126 +# define PKCS12_F_PKCS8_ENCRYPT 125 +# define PKCS12_F_PKCS8_SET0_PBE 132 + +/* + * PKCS12 reason codes. + */ +# define PKCS12_R_CANT_PACK_STRUCTURE 100 +# define PKCS12_R_CONTENT_TYPE_NOT_DATA 121 +# define PKCS12_R_DECODE_ERROR 101 +# define PKCS12_R_ENCODE_ERROR 102 +# define PKCS12_R_ENCRYPT_ERROR 103 +# define PKCS12_R_ERROR_SETTING_ENCRYPTED_DATA_TYPE 120 +# define PKCS12_R_INVALID_NULL_ARGUMENT 104 +# define PKCS12_R_INVALID_NULL_PKCS12_POINTER 105 +# define PKCS12_R_IV_GEN_ERROR 106 +# define PKCS12_R_KEY_GEN_ERROR 107 +# define PKCS12_R_MAC_ABSENT 108 +# define PKCS12_R_MAC_GENERATION_ERROR 109 +# define PKCS12_R_MAC_SETUP_ERROR 110 +# define PKCS12_R_MAC_STRING_SET_ERROR 111 +# define PKCS12_R_MAC_VERIFY_FAILURE 113 +# define PKCS12_R_PARSE_ERROR 114 +# define PKCS12_R_PKCS12_ALGOR_CIPHERINIT_ERROR 115 +# define PKCS12_R_PKCS12_CIPHERFINAL_ERROR 116 +# define PKCS12_R_PKCS12_PBE_CRYPT_ERROR 117 +# define PKCS12_R_UNKNOWN_DIGEST_ALGORITHM 118 +# define PKCS12_R_UNSUPPORTED_PKCS12_MODE 119 + +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/pkcs7.h b/example/android/third_party/crypto/arm/include/openssl/pkcs7.h new file mode 100644 index 00000000..9b66e002 --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/pkcs7.h @@ -0,0 +1,319 @@ +/* + * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_PKCS7_H +# define HEADER_PKCS7_H + +# include +# include +# include + +# include +# include +# include + +#ifdef __cplusplus +extern "C" { +#endif + +/*- +Encryption_ID DES-CBC +Digest_ID MD5 +Digest_Encryption_ID rsaEncryption +Key_Encryption_ID rsaEncryption +*/ + +typedef struct pkcs7_issuer_and_serial_st { + X509_NAME *issuer; + ASN1_INTEGER *serial; +} PKCS7_ISSUER_AND_SERIAL; + +typedef struct pkcs7_signer_info_st { + ASN1_INTEGER *version; /* version 1 */ + PKCS7_ISSUER_AND_SERIAL *issuer_and_serial; + X509_ALGOR *digest_alg; + STACK_OF(X509_ATTRIBUTE) *auth_attr; /* [ 0 ] */ + X509_ALGOR *digest_enc_alg; + ASN1_OCTET_STRING *enc_digest; + STACK_OF(X509_ATTRIBUTE) *unauth_attr; /* [ 1 ] */ + /* The private key to sign with */ + EVP_PKEY *pkey; +} PKCS7_SIGNER_INFO; + +DEFINE_STACK_OF(PKCS7_SIGNER_INFO) + +typedef struct pkcs7_recip_info_st { + ASN1_INTEGER *version; /* version 0 */ + PKCS7_ISSUER_AND_SERIAL *issuer_and_serial; + X509_ALGOR *key_enc_algor; + ASN1_OCTET_STRING *enc_key; + X509 *cert; /* get the pub-key from this */ +} PKCS7_RECIP_INFO; + +DEFINE_STACK_OF(PKCS7_RECIP_INFO) + +typedef struct pkcs7_signed_st { + ASN1_INTEGER *version; /* version 1 */ + STACK_OF(X509_ALGOR) *md_algs; /* md used */ + STACK_OF(X509) *cert; /* [ 0 ] */ + STACK_OF(X509_CRL) *crl; /* [ 1 ] */ + STACK_OF(PKCS7_SIGNER_INFO) *signer_info; + struct pkcs7_st *contents; +} PKCS7_SIGNED; +/* + * The above structure is very very similar to PKCS7_SIGN_ENVELOPE. How about + * merging the two + */ + +typedef struct pkcs7_enc_content_st { + ASN1_OBJECT *content_type; + X509_ALGOR *algorithm; + ASN1_OCTET_STRING *enc_data; /* [ 0 ] */ + const EVP_CIPHER *cipher; +} PKCS7_ENC_CONTENT; + +typedef struct pkcs7_enveloped_st { + ASN1_INTEGER *version; /* version 0 */ + STACK_OF(PKCS7_RECIP_INFO) *recipientinfo; + PKCS7_ENC_CONTENT *enc_data; +} PKCS7_ENVELOPE; + +typedef struct pkcs7_signedandenveloped_st { + ASN1_INTEGER *version; /* version 1 */ + STACK_OF(X509_ALGOR) *md_algs; /* md used */ + STACK_OF(X509) *cert; /* [ 0 ] */ + STACK_OF(X509_CRL) *crl; /* [ 1 ] */ + STACK_OF(PKCS7_SIGNER_INFO) *signer_info; + PKCS7_ENC_CONTENT *enc_data; + STACK_OF(PKCS7_RECIP_INFO) *recipientinfo; +} PKCS7_SIGN_ENVELOPE; + +typedef struct pkcs7_digest_st { + ASN1_INTEGER *version; /* version 0 */ + X509_ALGOR *md; /* md used */ + struct pkcs7_st *contents; + ASN1_OCTET_STRING *digest; +} PKCS7_DIGEST; + +typedef struct pkcs7_encrypted_st { + ASN1_INTEGER *version; /* version 0 */ + PKCS7_ENC_CONTENT *enc_data; +} PKCS7_ENCRYPT; + +typedef struct pkcs7_st { + /* + * The following is non NULL if it contains ASN1 encoding of this + * structure + */ + unsigned char *asn1; + long length; +# define PKCS7_S_HEADER 0 +# define PKCS7_S_BODY 1 +# define PKCS7_S_TAIL 2 + int state; /* used during processing */ + int detached; + ASN1_OBJECT *type; + /* content as defined by the type */ + /* + * all encryption/message digests are applied to the 'contents', leaving + * out the 'type' field. + */ + union { + char *ptr; + /* NID_pkcs7_data */ + ASN1_OCTET_STRING *data; + /* NID_pkcs7_signed */ + PKCS7_SIGNED *sign; + /* NID_pkcs7_enveloped */ + PKCS7_ENVELOPE *enveloped; + /* NID_pkcs7_signedAndEnveloped */ + PKCS7_SIGN_ENVELOPE *signed_and_enveloped; + /* NID_pkcs7_digest */ + PKCS7_DIGEST *digest; + /* NID_pkcs7_encrypted */ + PKCS7_ENCRYPT *encrypted; + /* Anything else */ + ASN1_TYPE *other; + } d; +} PKCS7; + +DEFINE_STACK_OF(PKCS7) + +# define PKCS7_OP_SET_DETACHED_SIGNATURE 1 +# define PKCS7_OP_GET_DETACHED_SIGNATURE 2 + +# define PKCS7_get_signed_attributes(si) ((si)->auth_attr) +# define PKCS7_get_attributes(si) ((si)->unauth_attr) + +# define PKCS7_type_is_signed(a) (OBJ_obj2nid((a)->type) == NID_pkcs7_signed) +# define PKCS7_type_is_encrypted(a) (OBJ_obj2nid((a)->type) == NID_pkcs7_encrypted) +# define PKCS7_type_is_enveloped(a) (OBJ_obj2nid((a)->type) == NID_pkcs7_enveloped) +# define PKCS7_type_is_signedAndEnveloped(a) \ + (OBJ_obj2nid((a)->type) == NID_pkcs7_signedAndEnveloped) +# define PKCS7_type_is_data(a) (OBJ_obj2nid((a)->type) == NID_pkcs7_data) +# define PKCS7_type_is_digest(a) (OBJ_obj2nid((a)->type) == NID_pkcs7_digest) + +# define PKCS7_set_detached(p,v) \ + PKCS7_ctrl(p,PKCS7_OP_SET_DETACHED_SIGNATURE,v,NULL) +# define PKCS7_get_detached(p) \ + PKCS7_ctrl(p,PKCS7_OP_GET_DETACHED_SIGNATURE,0,NULL) + +# define PKCS7_is_detached(p7) (PKCS7_type_is_signed(p7) && PKCS7_get_detached(p7)) + +/* S/MIME related flags */ + +# define PKCS7_TEXT 0x1 +# define PKCS7_NOCERTS 0x2 +# define PKCS7_NOSIGS 0x4 +# define PKCS7_NOCHAIN 0x8 +# define PKCS7_NOINTERN 0x10 +# define PKCS7_NOVERIFY 0x20 +# define PKCS7_DETACHED 0x40 +# define PKCS7_BINARY 0x80 +# define PKCS7_NOATTR 0x100 +# define PKCS7_NOSMIMECAP 0x200 +# define PKCS7_NOOLDMIMETYPE 0x400 +# define PKCS7_CRLFEOL 0x800 +# define PKCS7_STREAM 0x1000 +# define PKCS7_NOCRL 0x2000 +# define PKCS7_PARTIAL 0x4000 +# define PKCS7_REUSE_DIGEST 0x8000 +# define PKCS7_NO_DUAL_CONTENT 0x10000 + +/* Flags: for compatibility with older code */ + +# define SMIME_TEXT PKCS7_TEXT +# define SMIME_NOCERTS PKCS7_NOCERTS +# define SMIME_NOSIGS PKCS7_NOSIGS +# define SMIME_NOCHAIN PKCS7_NOCHAIN +# define SMIME_NOINTERN PKCS7_NOINTERN +# define SMIME_NOVERIFY PKCS7_NOVERIFY +# define SMIME_DETACHED PKCS7_DETACHED +# define SMIME_BINARY PKCS7_BINARY +# define SMIME_NOATTR PKCS7_NOATTR + +/* CRLF ASCII canonicalisation */ +# define SMIME_ASCIICRLF 0x80000 + +DECLARE_ASN1_FUNCTIONS(PKCS7_ISSUER_AND_SERIAL) + +int PKCS7_ISSUER_AND_SERIAL_digest(PKCS7_ISSUER_AND_SERIAL *data, + const EVP_MD *type, unsigned char *md, + unsigned int *len); +# ifndef OPENSSL_NO_STDIO +PKCS7 *d2i_PKCS7_fp(FILE *fp, PKCS7 **p7); +int i2d_PKCS7_fp(FILE *fp, PKCS7 *p7); +# endif +PKCS7 *PKCS7_dup(PKCS7 *p7); +PKCS7 *d2i_PKCS7_bio(BIO *bp, PKCS7 **p7); +int i2d_PKCS7_bio(BIO *bp, PKCS7 *p7); +int i2d_PKCS7_bio_stream(BIO *out, PKCS7 *p7, BIO *in, int flags); +int PEM_write_bio_PKCS7_stream(BIO *out, PKCS7 *p7, BIO *in, int flags); + +DECLARE_ASN1_FUNCTIONS(PKCS7_SIGNER_INFO) +DECLARE_ASN1_FUNCTIONS(PKCS7_RECIP_INFO) +DECLARE_ASN1_FUNCTIONS(PKCS7_SIGNED) +DECLARE_ASN1_FUNCTIONS(PKCS7_ENC_CONTENT) +DECLARE_ASN1_FUNCTIONS(PKCS7_ENVELOPE) +DECLARE_ASN1_FUNCTIONS(PKCS7_SIGN_ENVELOPE) +DECLARE_ASN1_FUNCTIONS(PKCS7_DIGEST) +DECLARE_ASN1_FUNCTIONS(PKCS7_ENCRYPT) +DECLARE_ASN1_FUNCTIONS(PKCS7) + +DECLARE_ASN1_ITEM(PKCS7_ATTR_SIGN) +DECLARE_ASN1_ITEM(PKCS7_ATTR_VERIFY) + +DECLARE_ASN1_NDEF_FUNCTION(PKCS7) +DECLARE_ASN1_PRINT_FUNCTION(PKCS7) + +long PKCS7_ctrl(PKCS7 *p7, int cmd, long larg, char *parg); + +int PKCS7_set_type(PKCS7 *p7, int type); +int PKCS7_set0_type_other(PKCS7 *p7, int type, ASN1_TYPE *other); +int PKCS7_set_content(PKCS7 *p7, PKCS7 *p7_data); +int PKCS7_SIGNER_INFO_set(PKCS7_SIGNER_INFO *p7i, X509 *x509, EVP_PKEY *pkey, + const EVP_MD *dgst); +int PKCS7_SIGNER_INFO_sign(PKCS7_SIGNER_INFO *si); +int PKCS7_add_signer(PKCS7 *p7, PKCS7_SIGNER_INFO *p7i); +int PKCS7_add_certificate(PKCS7 *p7, X509 *x509); +int PKCS7_add_crl(PKCS7 *p7, X509_CRL *x509); +int PKCS7_content_new(PKCS7 *p7, int nid); +int PKCS7_dataVerify(X509_STORE *cert_store, X509_STORE_CTX *ctx, + BIO *bio, PKCS7 *p7, PKCS7_SIGNER_INFO *si); +int PKCS7_signatureVerify(BIO *bio, PKCS7 *p7, PKCS7_SIGNER_INFO *si, + X509 *x509); + +BIO *PKCS7_dataInit(PKCS7 *p7, BIO *bio); +int PKCS7_dataFinal(PKCS7 *p7, BIO *bio); +BIO *PKCS7_dataDecode(PKCS7 *p7, EVP_PKEY *pkey, BIO *in_bio, X509 *pcert); + +PKCS7_SIGNER_INFO *PKCS7_add_signature(PKCS7 *p7, X509 *x509, + EVP_PKEY *pkey, const EVP_MD *dgst); +X509 *PKCS7_cert_from_signer_info(PKCS7 *p7, PKCS7_SIGNER_INFO *si); +int PKCS7_set_digest(PKCS7 *p7, const EVP_MD *md); +STACK_OF(PKCS7_SIGNER_INFO) *PKCS7_get_signer_info(PKCS7 *p7); + +PKCS7_RECIP_INFO *PKCS7_add_recipient(PKCS7 *p7, X509 *x509); +void PKCS7_SIGNER_INFO_get0_algs(PKCS7_SIGNER_INFO *si, EVP_PKEY **pk, + X509_ALGOR **pdig, X509_ALGOR **psig); +void PKCS7_RECIP_INFO_get0_alg(PKCS7_RECIP_INFO *ri, X509_ALGOR **penc); +int PKCS7_add_recipient_info(PKCS7 *p7, PKCS7_RECIP_INFO *ri); +int PKCS7_RECIP_INFO_set(PKCS7_RECIP_INFO *p7i, X509 *x509); +int PKCS7_set_cipher(PKCS7 *p7, const EVP_CIPHER *cipher); +int PKCS7_stream(unsigned char ***boundary, PKCS7 *p7); + +PKCS7_ISSUER_AND_SERIAL *PKCS7_get_issuer_and_serial(PKCS7 *p7, int idx); +ASN1_OCTET_STRING *PKCS7_digest_from_attributes(STACK_OF(X509_ATTRIBUTE) *sk); +int PKCS7_add_signed_attribute(PKCS7_SIGNER_INFO *p7si, int nid, int type, + void *data); +int PKCS7_add_attribute(PKCS7_SIGNER_INFO *p7si, int nid, int atrtype, + void *value); +ASN1_TYPE *PKCS7_get_attribute(PKCS7_SIGNER_INFO *si, int nid); +ASN1_TYPE *PKCS7_get_signed_attribute(PKCS7_SIGNER_INFO *si, int nid); +int PKCS7_set_signed_attributes(PKCS7_SIGNER_INFO *p7si, + STACK_OF(X509_ATTRIBUTE) *sk); +int PKCS7_set_attributes(PKCS7_SIGNER_INFO *p7si, + STACK_OF(X509_ATTRIBUTE) *sk); + +PKCS7 *PKCS7_sign(X509 *signcert, EVP_PKEY *pkey, STACK_OF(X509) *certs, + BIO *data, int flags); + +PKCS7_SIGNER_INFO *PKCS7_sign_add_signer(PKCS7 *p7, + X509 *signcert, EVP_PKEY *pkey, + const EVP_MD *md, int flags); + +int PKCS7_final(PKCS7 *p7, BIO *data, int flags); +int PKCS7_verify(PKCS7 *p7, STACK_OF(X509) *certs, X509_STORE *store, + BIO *indata, BIO *out, int flags); +STACK_OF(X509) *PKCS7_get0_signers(PKCS7 *p7, STACK_OF(X509) *certs, + int flags); +PKCS7 *PKCS7_encrypt(STACK_OF(X509) *certs, BIO *in, const EVP_CIPHER *cipher, + int flags); +int PKCS7_decrypt(PKCS7 *p7, EVP_PKEY *pkey, X509 *cert, BIO *data, + int flags); + +int PKCS7_add_attrib_smimecap(PKCS7_SIGNER_INFO *si, + STACK_OF(X509_ALGOR) *cap); +STACK_OF(X509_ALGOR) *PKCS7_get_smimecap(PKCS7_SIGNER_INFO *si); +int PKCS7_simple_smimecap(STACK_OF(X509_ALGOR) *sk, int nid, int arg); + +int PKCS7_add_attrib_content_type(PKCS7_SIGNER_INFO *si, ASN1_OBJECT *coid); +int PKCS7_add0_attrib_signing_time(PKCS7_SIGNER_INFO *si, ASN1_TIME *t); +int PKCS7_add1_attrib_digest(PKCS7_SIGNER_INFO *si, + const unsigned char *md, int mdlen); + +int SMIME_write_PKCS7(BIO *bio, PKCS7 *p7, BIO *data, int flags); +PKCS7 *SMIME_read_PKCS7(BIO *bio, BIO **bcont); + +BIO *BIO_new_PKCS7(BIO *out, PKCS7 *p7); + +# ifdef __cplusplus +} +# endif +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/pkcs7err.h b/example/android/third_party/crypto/arm/include/openssl/pkcs7err.h new file mode 100644 index 00000000..0ba418d7 --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/pkcs7err.h @@ -0,0 +1,99 @@ +/* + * Generated by util/mkerr.pl DO NOT EDIT + * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_PKCS7ERR_H +# define HEADER_PKCS7ERR_H + +# ifdef __cplusplus +extern "C" +# endif +int ERR_load_PKCS7_strings(void); + +/* + * PKCS7 function codes. + */ +# define PKCS7_F_DO_PKCS7_SIGNED_ATTRIB 136 +# define PKCS7_F_PKCS7_ADD0_ATTRIB_SIGNING_TIME 135 +# define PKCS7_F_PKCS7_ADD_ATTRIB_SMIMECAP 118 +# define PKCS7_F_PKCS7_ADD_CERTIFICATE 100 +# define PKCS7_F_PKCS7_ADD_CRL 101 +# define PKCS7_F_PKCS7_ADD_RECIPIENT_INFO 102 +# define PKCS7_F_PKCS7_ADD_SIGNATURE 131 +# define PKCS7_F_PKCS7_ADD_SIGNER 103 +# define PKCS7_F_PKCS7_BIO_ADD_DIGEST 125 +# define PKCS7_F_PKCS7_COPY_EXISTING_DIGEST 138 +# define PKCS7_F_PKCS7_CTRL 104 +# define PKCS7_F_PKCS7_DATADECODE 112 +# define PKCS7_F_PKCS7_DATAFINAL 128 +# define PKCS7_F_PKCS7_DATAINIT 105 +# define PKCS7_F_PKCS7_DATAVERIFY 107 +# define PKCS7_F_PKCS7_DECRYPT 114 +# define PKCS7_F_PKCS7_DECRYPT_RINFO 133 +# define PKCS7_F_PKCS7_ENCODE_RINFO 132 +# define PKCS7_F_PKCS7_ENCRYPT 115 +# define PKCS7_F_PKCS7_FINAL 134 +# define PKCS7_F_PKCS7_FIND_DIGEST 127 +# define PKCS7_F_PKCS7_GET0_SIGNERS 124 +# define PKCS7_F_PKCS7_RECIP_INFO_SET 130 +# define PKCS7_F_PKCS7_SET_CIPHER 108 +# define PKCS7_F_PKCS7_SET_CONTENT 109 +# define PKCS7_F_PKCS7_SET_DIGEST 126 +# define PKCS7_F_PKCS7_SET_TYPE 110 +# define PKCS7_F_PKCS7_SIGN 116 +# define PKCS7_F_PKCS7_SIGNATUREVERIFY 113 +# define PKCS7_F_PKCS7_SIGNER_INFO_SET 129 +# define PKCS7_F_PKCS7_SIGNER_INFO_SIGN 139 +# define PKCS7_F_PKCS7_SIGN_ADD_SIGNER 137 +# define PKCS7_F_PKCS7_SIMPLE_SMIMECAP 119 +# define PKCS7_F_PKCS7_VERIFY 117 + +/* + * PKCS7 reason codes. + */ +# define PKCS7_R_CERTIFICATE_VERIFY_ERROR 117 +# define PKCS7_R_CIPHER_HAS_NO_OBJECT_IDENTIFIER 144 +# define PKCS7_R_CIPHER_NOT_INITIALIZED 116 +# define PKCS7_R_CONTENT_AND_DATA_PRESENT 118 +# define PKCS7_R_CTRL_ERROR 152 +# define PKCS7_R_DECRYPT_ERROR 119 +# define PKCS7_R_DIGEST_FAILURE 101 +# define PKCS7_R_ENCRYPTION_CTRL_FAILURE 149 +# define PKCS7_R_ENCRYPTION_NOT_SUPPORTED_FOR_THIS_KEY_TYPE 150 +# define PKCS7_R_ERROR_ADDING_RECIPIENT 120 +# define PKCS7_R_ERROR_SETTING_CIPHER 121 +# define PKCS7_R_INVALID_NULL_POINTER 143 +# define PKCS7_R_INVALID_SIGNED_DATA_TYPE 155 +# define PKCS7_R_NO_CONTENT 122 +# define PKCS7_R_NO_DEFAULT_DIGEST 151 +# define PKCS7_R_NO_MATCHING_DIGEST_TYPE_FOUND 154 +# define PKCS7_R_NO_RECIPIENT_MATCHES_CERTIFICATE 115 +# define PKCS7_R_NO_SIGNATURES_ON_DATA 123 +# define PKCS7_R_NO_SIGNERS 142 +# define PKCS7_R_OPERATION_NOT_SUPPORTED_ON_THIS_TYPE 104 +# define PKCS7_R_PKCS7_ADD_SIGNATURE_ERROR 124 +# define PKCS7_R_PKCS7_ADD_SIGNER_ERROR 153 +# define PKCS7_R_PKCS7_DATASIGN 145 +# define PKCS7_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE 127 +# define PKCS7_R_SIGNATURE_FAILURE 105 +# define PKCS7_R_SIGNER_CERTIFICATE_NOT_FOUND 128 +# define PKCS7_R_SIGNING_CTRL_FAILURE 147 +# define PKCS7_R_SIGNING_NOT_SUPPORTED_FOR_THIS_KEY_TYPE 148 +# define PKCS7_R_SMIME_TEXT_ERROR 129 +# define PKCS7_R_UNABLE_TO_FIND_CERTIFICATE 106 +# define PKCS7_R_UNABLE_TO_FIND_MEM_BIO 107 +# define PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST 108 +# define PKCS7_R_UNKNOWN_DIGEST_TYPE 109 +# define PKCS7_R_UNKNOWN_OPERATION 110 +# define PKCS7_R_UNSUPPORTED_CIPHER_TYPE 111 +# define PKCS7_R_UNSUPPORTED_CONTENT_TYPE 112 +# define PKCS7_R_WRONG_CONTENT_TYPE 113 +# define PKCS7_R_WRONG_PKCS7_TYPE 114 + +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/rand.h b/example/android/third_party/crypto/arm/include/openssl/rand.h new file mode 100644 index 00000000..38a2a271 --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/rand.h @@ -0,0 +1,77 @@ +/* + * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_RAND_H +# define HEADER_RAND_H + +# include +# include +# include +# include + +#ifdef __cplusplus +extern "C" { +#endif + +struct rand_meth_st { + int (*seed) (const void *buf, int num); + int (*bytes) (unsigned char *buf, int num); + void (*cleanup) (void); + int (*add) (const void *buf, int num, double randomness); + int (*pseudorand) (unsigned char *buf, int num); + int (*status) (void); +}; + +int RAND_set_rand_method(const RAND_METHOD *meth); +const RAND_METHOD *RAND_get_rand_method(void); +# ifndef OPENSSL_NO_ENGINE +int RAND_set_rand_engine(ENGINE *engine); +# endif + +RAND_METHOD *RAND_OpenSSL(void); + +# if OPENSSL_API_COMPAT < 0x10100000L +# define RAND_cleanup() while(0) continue +# endif +int RAND_bytes(unsigned char *buf, int num); +int RAND_priv_bytes(unsigned char *buf, int num); +DEPRECATEDIN_1_1_0(int RAND_pseudo_bytes(unsigned char *buf, int num)) + +void RAND_seed(const void *buf, int num); +void RAND_keep_random_devices_open(int keep); + +# if defined(__ANDROID__) && defined(__NDK_FPABI__) +__NDK_FPABI__ /* __attribute__((pcs("aapcs"))) on ARM */ +# endif +void RAND_add(const void *buf, int num, double randomness); +int RAND_load_file(const char *file, long max_bytes); +int RAND_write_file(const char *file); +const char *RAND_file_name(char *file, size_t num); +int RAND_status(void); + +# ifndef OPENSSL_NO_EGD +int RAND_query_egd_bytes(const char *path, unsigned char *buf, int bytes); +int RAND_egd(const char *path); +int RAND_egd_bytes(const char *path, int bytes); +# endif + +int RAND_poll(void); + +# if defined(_WIN32) && (defined(BASETYPES) || defined(_WINDEF_H)) +/* application has to include in order to use these */ +DEPRECATEDIN_1_1_0(void RAND_screen(void)) +DEPRECATEDIN_1_1_0(int RAND_event(UINT, WPARAM, LPARAM)) +# endif + + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/rand_drbg.h b/example/android/third_party/crypto/arm/include/openssl/rand_drbg.h new file mode 100644 index 00000000..45b731b7 --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/rand_drbg.h @@ -0,0 +1,130 @@ +/* + * Copyright 2017-2018 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_DRBG_RAND_H +# define HEADER_DRBG_RAND_H + +# include +# include +# include + +/* + * RAND_DRBG flags + * + * Note: if new flags are added, the constant `rand_drbg_used_flags` + * in drbg_lib.c needs to be updated accordingly. + */ + +/* In CTR mode, disable derivation function ctr_df */ +# define RAND_DRBG_FLAG_CTR_NO_DF 0x1 + + +# if OPENSSL_API_COMPAT < 0x10200000L +/* This #define was replaced by an internal constant and should not be used. */ +# define RAND_DRBG_USED_FLAGS (RAND_DRBG_FLAG_CTR_NO_DF) +# endif + +/* + * Default security strength (in the sense of [NIST SP 800-90Ar1]) + * + * NIST SP 800-90Ar1 supports the strength of the DRBG being smaller than that + * of the cipher by collecting less entropy. The current DRBG implementation + * does not take RAND_DRBG_STRENGTH into account and sets the strength of the + * DRBG to that of the cipher. + * + * RAND_DRBG_STRENGTH is currently only used for the legacy RAND + * implementation. + * + * Currently supported ciphers are: NID_aes_128_ctr, NID_aes_192_ctr and + * NID_aes_256_ctr + */ +# define RAND_DRBG_STRENGTH 256 +/* Default drbg type */ +# define RAND_DRBG_TYPE NID_aes_256_ctr +/* Default drbg flags */ +# define RAND_DRBG_FLAGS 0 + + +# ifdef __cplusplus +extern "C" { +# endif + +/* + * Object lifetime functions. + */ +RAND_DRBG *RAND_DRBG_new(int type, unsigned int flags, RAND_DRBG *parent); +RAND_DRBG *RAND_DRBG_secure_new(int type, unsigned int flags, RAND_DRBG *parent); +int RAND_DRBG_set(RAND_DRBG *drbg, int type, unsigned int flags); +int RAND_DRBG_set_defaults(int type, unsigned int flags); +int RAND_DRBG_instantiate(RAND_DRBG *drbg, + const unsigned char *pers, size_t perslen); +int RAND_DRBG_uninstantiate(RAND_DRBG *drbg); +void RAND_DRBG_free(RAND_DRBG *drbg); + +/* + * Object "use" functions. + */ +int RAND_DRBG_reseed(RAND_DRBG *drbg, + const unsigned char *adin, size_t adinlen, + int prediction_resistance); +int RAND_DRBG_generate(RAND_DRBG *drbg, unsigned char *out, size_t outlen, + int prediction_resistance, + const unsigned char *adin, size_t adinlen); +int RAND_DRBG_bytes(RAND_DRBG *drbg, unsigned char *out, size_t outlen); + +int RAND_DRBG_set_reseed_interval(RAND_DRBG *drbg, unsigned int interval); +int RAND_DRBG_set_reseed_time_interval(RAND_DRBG *drbg, time_t interval); + +int RAND_DRBG_set_reseed_defaults( + unsigned int master_reseed_interval, + unsigned int slave_reseed_interval, + time_t master_reseed_time_interval, + time_t slave_reseed_time_interval + ); + +RAND_DRBG *RAND_DRBG_get0_master(void); +RAND_DRBG *RAND_DRBG_get0_public(void); +RAND_DRBG *RAND_DRBG_get0_private(void); + +/* + * EXDATA + */ +# define RAND_DRBG_get_ex_new_index(l, p, newf, dupf, freef) \ + CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_DRBG, l, p, newf, dupf, freef) +int RAND_DRBG_set_ex_data(RAND_DRBG *drbg, int idx, void *arg); +void *RAND_DRBG_get_ex_data(const RAND_DRBG *drbg, int idx); + +/* + * Callback function typedefs + */ +typedef size_t (*RAND_DRBG_get_entropy_fn)(RAND_DRBG *drbg, + unsigned char **pout, + int entropy, size_t min_len, + size_t max_len, + int prediction_resistance); +typedef void (*RAND_DRBG_cleanup_entropy_fn)(RAND_DRBG *ctx, + unsigned char *out, size_t outlen); +typedef size_t (*RAND_DRBG_get_nonce_fn)(RAND_DRBG *drbg, unsigned char **pout, + int entropy, size_t min_len, + size_t max_len); +typedef void (*RAND_DRBG_cleanup_nonce_fn)(RAND_DRBG *drbg, + unsigned char *out, size_t outlen); + +int RAND_DRBG_set_callbacks(RAND_DRBG *drbg, + RAND_DRBG_get_entropy_fn get_entropy, + RAND_DRBG_cleanup_entropy_fn cleanup_entropy, + RAND_DRBG_get_nonce_fn get_nonce, + RAND_DRBG_cleanup_nonce_fn cleanup_nonce); + + +# ifdef __cplusplus +} +# endif + +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/randerr.h b/example/android/third_party/crypto/arm/include/openssl/randerr.h new file mode 100644 index 00000000..599a2a18 --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/randerr.h @@ -0,0 +1,89 @@ +/* + * Generated by util/mkerr.pl DO NOT EDIT + * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_RANDERR_H +# define HEADER_RANDERR_H + +# ifdef __cplusplus +extern "C" +# endif +int ERR_load_RAND_strings(void); + +/* + * RAND function codes. + */ +# define RAND_F_DRBG_BYTES 101 +# define RAND_F_DRBG_GET_ENTROPY 105 +# define RAND_F_DRBG_SETUP 117 +# define RAND_F_GET_ENTROPY 106 +# define RAND_F_RAND_BYTES 100 +# define RAND_F_RAND_DRBG_ENABLE_LOCKING 119 +# define RAND_F_RAND_DRBG_GENERATE 107 +# define RAND_F_RAND_DRBG_GET_ENTROPY 120 +# define RAND_F_RAND_DRBG_GET_NONCE 123 +# define RAND_F_RAND_DRBG_INSTANTIATE 108 +# define RAND_F_RAND_DRBG_NEW 109 +# define RAND_F_RAND_DRBG_RESEED 110 +# define RAND_F_RAND_DRBG_RESTART 102 +# define RAND_F_RAND_DRBG_SET 104 +# define RAND_F_RAND_DRBG_SET_DEFAULTS 121 +# define RAND_F_RAND_DRBG_UNINSTANTIATE 118 +# define RAND_F_RAND_LOAD_FILE 111 +# define RAND_F_RAND_POOL_ACQUIRE_ENTROPY 122 +# define RAND_F_RAND_POOL_ADD 103 +# define RAND_F_RAND_POOL_ADD_BEGIN 113 +# define RAND_F_RAND_POOL_ADD_END 114 +# define RAND_F_RAND_POOL_ATTACH 124 +# define RAND_F_RAND_POOL_BYTES_NEEDED 115 +# define RAND_F_RAND_POOL_NEW 116 +# define RAND_F_RAND_WRITE_FILE 112 + +/* + * RAND reason codes. + */ +# define RAND_R_ADDITIONAL_INPUT_TOO_LONG 102 +# define RAND_R_ALREADY_INSTANTIATED 103 +# define RAND_R_ARGUMENT_OUT_OF_RANGE 105 +# define RAND_R_CANNOT_OPEN_FILE 121 +# define RAND_R_DRBG_ALREADY_INITIALIZED 129 +# define RAND_R_DRBG_NOT_INITIALISED 104 +# define RAND_R_ENTROPY_INPUT_TOO_LONG 106 +# define RAND_R_ENTROPY_OUT_OF_RANGE 124 +# define RAND_R_ERROR_ENTROPY_POOL_WAS_IGNORED 127 +# define RAND_R_ERROR_INITIALISING_DRBG 107 +# define RAND_R_ERROR_INSTANTIATING_DRBG 108 +# define RAND_R_ERROR_RETRIEVING_ADDITIONAL_INPUT 109 +# define RAND_R_ERROR_RETRIEVING_ENTROPY 110 +# define RAND_R_ERROR_RETRIEVING_NONCE 111 +# define RAND_R_FAILED_TO_CREATE_LOCK 126 +# define RAND_R_FUNC_NOT_IMPLEMENTED 101 +# define RAND_R_FWRITE_ERROR 123 +# define RAND_R_GENERATE_ERROR 112 +# define RAND_R_INTERNAL_ERROR 113 +# define RAND_R_IN_ERROR_STATE 114 +# define RAND_R_NOT_A_REGULAR_FILE 122 +# define RAND_R_NOT_INSTANTIATED 115 +# define RAND_R_NO_DRBG_IMPLEMENTATION_SELECTED 128 +# define RAND_R_PARENT_LOCKING_NOT_ENABLED 130 +# define RAND_R_PARENT_STRENGTH_TOO_WEAK 131 +# define RAND_R_PERSONALISATION_STRING_TOO_LONG 116 +# define RAND_R_PREDICTION_RESISTANCE_NOT_SUPPORTED 133 +# define RAND_R_PRNG_NOT_SEEDED 100 +# define RAND_R_RANDOM_POOL_OVERFLOW 125 +# define RAND_R_RANDOM_POOL_UNDERFLOW 134 +# define RAND_R_REQUEST_TOO_LARGE_FOR_DRBG 117 +# define RAND_R_RESEED_ERROR 118 +# define RAND_R_SELFTEST_FAILURE 119 +# define RAND_R_TOO_LITTLE_NONCE_REQUESTED 135 +# define RAND_R_TOO_MUCH_NONCE_REQUESTED 136 +# define RAND_R_UNSUPPORTED_DRBG_FLAGS 132 +# define RAND_R_UNSUPPORTED_DRBG_TYPE 120 + +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/rc2.h b/example/android/third_party/crypto/arm/include/openssl/rc2.h new file mode 100644 index 00000000..585f9e4c --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/rc2.h @@ -0,0 +1,51 @@ +/* + * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_RC2_H +# define HEADER_RC2_H + +# include + +# ifndef OPENSSL_NO_RC2 +# ifdef __cplusplus +extern "C" { +# endif + +typedef unsigned int RC2_INT; + +# define RC2_ENCRYPT 1 +# define RC2_DECRYPT 0 + +# define RC2_BLOCK 8 +# define RC2_KEY_LENGTH 16 + +typedef struct rc2_key_st { + RC2_INT data[64]; +} RC2_KEY; + +void RC2_set_key(RC2_KEY *key, int len, const unsigned char *data, int bits); +void RC2_ecb_encrypt(const unsigned char *in, unsigned char *out, + RC2_KEY *key, int enc); +void RC2_encrypt(unsigned long *data, RC2_KEY *key); +void RC2_decrypt(unsigned long *data, RC2_KEY *key); +void RC2_cbc_encrypt(const unsigned char *in, unsigned char *out, long length, + RC2_KEY *ks, unsigned char *iv, int enc); +void RC2_cfb64_encrypt(const unsigned char *in, unsigned char *out, + long length, RC2_KEY *schedule, unsigned char *ivec, + int *num, int enc); +void RC2_ofb64_encrypt(const unsigned char *in, unsigned char *out, + long length, RC2_KEY *schedule, unsigned char *ivec, + int *num); + +# ifdef __cplusplus +} +# endif +# endif + +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/rc4.h b/example/android/third_party/crypto/arm/include/openssl/rc4.h new file mode 100644 index 00000000..86803b37 --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/rc4.h @@ -0,0 +1,36 @@ +/* + * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_RC4_H +# define HEADER_RC4_H + +# include + +# ifndef OPENSSL_NO_RC4 +# include +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct rc4_key_st { + RC4_INT x, y; + RC4_INT data[256]; +} RC4_KEY; + +const char *RC4_options(void); +void RC4_set_key(RC4_KEY *key, int len, const unsigned char *data); +void RC4(RC4_KEY *key, size_t len, const unsigned char *indata, + unsigned char *outdata); + +# ifdef __cplusplus +} +# endif +# endif + +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/rc5.h b/example/android/third_party/crypto/arm/include/openssl/rc5.h new file mode 100644 index 00000000..793f88e4 --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/rc5.h @@ -0,0 +1,63 @@ +/* + * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_RC5_H +# define HEADER_RC5_H + +# include + +# ifndef OPENSSL_NO_RC5 +# ifdef __cplusplus +extern "C" { +# endif + +# define RC5_ENCRYPT 1 +# define RC5_DECRYPT 0 + +# define RC5_32_INT unsigned int + +# define RC5_32_BLOCK 8 +# define RC5_32_KEY_LENGTH 16/* This is a default, max is 255 */ + +/* + * This are the only values supported. Tweak the code if you want more The + * most supported modes will be RC5-32/12/16 RC5-32/16/8 + */ +# define RC5_8_ROUNDS 8 +# define RC5_12_ROUNDS 12 +# define RC5_16_ROUNDS 16 + +typedef struct rc5_key_st { + /* Number of rounds */ + int rounds; + RC5_32_INT data[2 * (RC5_16_ROUNDS + 1)]; +} RC5_32_KEY; + +void RC5_32_set_key(RC5_32_KEY *key, int len, const unsigned char *data, + int rounds); +void RC5_32_ecb_encrypt(const unsigned char *in, unsigned char *out, + RC5_32_KEY *key, int enc); +void RC5_32_encrypt(unsigned long *data, RC5_32_KEY *key); +void RC5_32_decrypt(unsigned long *data, RC5_32_KEY *key); +void RC5_32_cbc_encrypt(const unsigned char *in, unsigned char *out, + long length, RC5_32_KEY *ks, unsigned char *iv, + int enc); +void RC5_32_cfb64_encrypt(const unsigned char *in, unsigned char *out, + long length, RC5_32_KEY *schedule, + unsigned char *ivec, int *num, int enc); +void RC5_32_ofb64_encrypt(const unsigned char *in, unsigned char *out, + long length, RC5_32_KEY *schedule, + unsigned char *ivec, int *num); + +# ifdef __cplusplus +} +# endif +# endif + +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/ripemd.h b/example/android/third_party/crypto/arm/include/openssl/ripemd.h new file mode 100644 index 00000000..c42026aa --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/ripemd.h @@ -0,0 +1,47 @@ +/* + * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_RIPEMD_H +# define HEADER_RIPEMD_H + +# include + +#ifndef OPENSSL_NO_RMD160 +# include +# include +# ifdef __cplusplus +extern "C" { +# endif + +# define RIPEMD160_LONG unsigned int + +# define RIPEMD160_CBLOCK 64 +# define RIPEMD160_LBLOCK (RIPEMD160_CBLOCK/4) +# define RIPEMD160_DIGEST_LENGTH 20 + +typedef struct RIPEMD160state_st { + RIPEMD160_LONG A, B, C, D, E; + RIPEMD160_LONG Nl, Nh; + RIPEMD160_LONG data[RIPEMD160_LBLOCK]; + unsigned int num; +} RIPEMD160_CTX; + +int RIPEMD160_Init(RIPEMD160_CTX *c); +int RIPEMD160_Update(RIPEMD160_CTX *c, const void *data, size_t len); +int RIPEMD160_Final(unsigned char *md, RIPEMD160_CTX *c); +unsigned char *RIPEMD160(const unsigned char *d, size_t n, unsigned char *md); +void RIPEMD160_Transform(RIPEMD160_CTX *c, const unsigned char *b); + +# ifdef __cplusplus +} +# endif +# endif + + +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/rsa.h b/example/android/third_party/crypto/arm/include/openssl/rsa.h new file mode 100644 index 00000000..cdce1264 --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/rsa.h @@ -0,0 +1,512 @@ +/* + * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_RSA_H +# define HEADER_RSA_H + +# include + +# ifndef OPENSSL_NO_RSA +# include +# include +# include +# include +# if OPENSSL_API_COMPAT < 0x10100000L +# include +# endif +# include +# ifdef __cplusplus +extern "C" { +# endif + +/* The types RSA and RSA_METHOD are defined in ossl_typ.h */ + +# ifndef OPENSSL_RSA_MAX_MODULUS_BITS +# define OPENSSL_RSA_MAX_MODULUS_BITS 16384 +# endif + +# define OPENSSL_RSA_FIPS_MIN_MODULUS_BITS 1024 + +# ifndef OPENSSL_RSA_SMALL_MODULUS_BITS +# define OPENSSL_RSA_SMALL_MODULUS_BITS 3072 +# endif +# ifndef OPENSSL_RSA_MAX_PUBEXP_BITS + +/* exponent limit enforced for "large" modulus only */ +# define OPENSSL_RSA_MAX_PUBEXP_BITS 64 +# endif + +# define RSA_3 0x3L +# define RSA_F4 0x10001L + +/* based on RFC 8017 appendix A.1.2 */ +# define RSA_ASN1_VERSION_DEFAULT 0 +# define RSA_ASN1_VERSION_MULTI 1 + +# define RSA_DEFAULT_PRIME_NUM 2 + +# define RSA_METHOD_FLAG_NO_CHECK 0x0001/* don't check pub/private + * match */ + +# define RSA_FLAG_CACHE_PUBLIC 0x0002 +# define RSA_FLAG_CACHE_PRIVATE 0x0004 +# define RSA_FLAG_BLINDING 0x0008 +# define RSA_FLAG_THREAD_SAFE 0x0010 +/* + * This flag means the private key operations will be handled by rsa_mod_exp + * and that they do not depend on the private key components being present: + * for example a key stored in external hardware. Without this flag + * bn_mod_exp gets called when private key components are absent. + */ +# define RSA_FLAG_EXT_PKEY 0x0020 + +/* + * new with 0.9.6j and 0.9.7b; the built-in + * RSA implementation now uses blinding by + * default (ignoring RSA_FLAG_BLINDING), + * but other engines might not need it + */ +# define RSA_FLAG_NO_BLINDING 0x0080 +# if OPENSSL_API_COMPAT < 0x10100000L +/* + * Does nothing. Previously this switched off constant time behaviour. + */ +# define RSA_FLAG_NO_CONSTTIME 0x0000 +# endif +# if OPENSSL_API_COMPAT < 0x00908000L +/* deprecated name for the flag*/ +/* + * new with 0.9.7h; the built-in RSA + * implementation now uses constant time + * modular exponentiation for secret exponents + * by default. This flag causes the + * faster variable sliding window method to + * be used for all exponents. + */ +# define RSA_FLAG_NO_EXP_CONSTTIME RSA_FLAG_NO_CONSTTIME +# endif + +# define EVP_PKEY_CTX_set_rsa_padding(ctx, pad) \ + RSA_pkey_ctx_ctrl(ctx, -1, EVP_PKEY_CTRL_RSA_PADDING, pad, NULL) + +# define EVP_PKEY_CTX_get_rsa_padding(ctx, ppad) \ + RSA_pkey_ctx_ctrl(ctx, -1, EVP_PKEY_CTRL_GET_RSA_PADDING, 0, ppad) + +# define EVP_PKEY_CTX_set_rsa_pss_saltlen(ctx, len) \ + RSA_pkey_ctx_ctrl(ctx, (EVP_PKEY_OP_SIGN|EVP_PKEY_OP_VERIFY), \ + EVP_PKEY_CTRL_RSA_PSS_SALTLEN, len, NULL) +/* Salt length matches digest */ +# define RSA_PSS_SALTLEN_DIGEST -1 +/* Verify only: auto detect salt length */ +# define RSA_PSS_SALTLEN_AUTO -2 +/* Set salt length to maximum possible */ +# define RSA_PSS_SALTLEN_MAX -3 +/* Old compatible max salt length for sign only */ +# define RSA_PSS_SALTLEN_MAX_SIGN -2 + +# define EVP_PKEY_CTX_set_rsa_pss_keygen_saltlen(ctx, len) \ + EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA_PSS, EVP_PKEY_OP_KEYGEN, \ + EVP_PKEY_CTRL_RSA_PSS_SALTLEN, len, NULL) + +# define EVP_PKEY_CTX_get_rsa_pss_saltlen(ctx, plen) \ + RSA_pkey_ctx_ctrl(ctx, (EVP_PKEY_OP_SIGN|EVP_PKEY_OP_VERIFY), \ + EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN, 0, plen) + +# define EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, bits) \ + RSA_pkey_ctx_ctrl(ctx, EVP_PKEY_OP_KEYGEN, \ + EVP_PKEY_CTRL_RSA_KEYGEN_BITS, bits, NULL) + +# define EVP_PKEY_CTX_set_rsa_keygen_pubexp(ctx, pubexp) \ + RSA_pkey_ctx_ctrl(ctx, EVP_PKEY_OP_KEYGEN, \ + EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP, 0, pubexp) + +# define EVP_PKEY_CTX_set_rsa_keygen_primes(ctx, primes) \ + RSA_pkey_ctx_ctrl(ctx, EVP_PKEY_OP_KEYGEN, \ + EVP_PKEY_CTRL_RSA_KEYGEN_PRIMES, primes, NULL) + +# define EVP_PKEY_CTX_set_rsa_mgf1_md(ctx, md) \ + RSA_pkey_ctx_ctrl(ctx, EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT, \ + EVP_PKEY_CTRL_RSA_MGF1_MD, 0, (void *)(md)) + +# define EVP_PKEY_CTX_set_rsa_pss_keygen_mgf1_md(ctx, md) \ + EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA_PSS, EVP_PKEY_OP_KEYGEN, \ + EVP_PKEY_CTRL_RSA_MGF1_MD, 0, (void *)(md)) + +# define EVP_PKEY_CTX_set_rsa_oaep_md(ctx, md) \ + EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT, \ + EVP_PKEY_CTRL_RSA_OAEP_MD, 0, (void *)(md)) + +# define EVP_PKEY_CTX_get_rsa_mgf1_md(ctx, pmd) \ + RSA_pkey_ctx_ctrl(ctx, EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT, \ + EVP_PKEY_CTRL_GET_RSA_MGF1_MD, 0, (void *)(pmd)) + +# define EVP_PKEY_CTX_get_rsa_oaep_md(ctx, pmd) \ + EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT, \ + EVP_PKEY_CTRL_GET_RSA_OAEP_MD, 0, (void *)(pmd)) + +# define EVP_PKEY_CTX_set0_rsa_oaep_label(ctx, l, llen) \ + EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT, \ + EVP_PKEY_CTRL_RSA_OAEP_LABEL, llen, (void *)(l)) + +# define EVP_PKEY_CTX_get0_rsa_oaep_label(ctx, l) \ + EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT, \ + EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL, 0, (void *)(l)) + +# define EVP_PKEY_CTX_set_rsa_pss_keygen_md(ctx, md) \ + EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA_PSS, \ + EVP_PKEY_OP_KEYGEN, EVP_PKEY_CTRL_MD, \ + 0, (void *)(md)) + +# define EVP_PKEY_CTRL_RSA_PADDING (EVP_PKEY_ALG_CTRL + 1) +# define EVP_PKEY_CTRL_RSA_PSS_SALTLEN (EVP_PKEY_ALG_CTRL + 2) + +# define EVP_PKEY_CTRL_RSA_KEYGEN_BITS (EVP_PKEY_ALG_CTRL + 3) +# define EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP (EVP_PKEY_ALG_CTRL + 4) +# define EVP_PKEY_CTRL_RSA_MGF1_MD (EVP_PKEY_ALG_CTRL + 5) + +# define EVP_PKEY_CTRL_GET_RSA_PADDING (EVP_PKEY_ALG_CTRL + 6) +# define EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN (EVP_PKEY_ALG_CTRL + 7) +# define EVP_PKEY_CTRL_GET_RSA_MGF1_MD (EVP_PKEY_ALG_CTRL + 8) + +# define EVP_PKEY_CTRL_RSA_OAEP_MD (EVP_PKEY_ALG_CTRL + 9) +# define EVP_PKEY_CTRL_RSA_OAEP_LABEL (EVP_PKEY_ALG_CTRL + 10) + +# define EVP_PKEY_CTRL_GET_RSA_OAEP_MD (EVP_PKEY_ALG_CTRL + 11) +# define EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL (EVP_PKEY_ALG_CTRL + 12) + +# define EVP_PKEY_CTRL_RSA_KEYGEN_PRIMES (EVP_PKEY_ALG_CTRL + 13) + +# define RSA_PKCS1_PADDING 1 +# define RSA_SSLV23_PADDING 2 +# define RSA_NO_PADDING 3 +# define RSA_PKCS1_OAEP_PADDING 4 +# define RSA_X931_PADDING 5 +/* EVP_PKEY_ only */ +# define RSA_PKCS1_PSS_PADDING 6 + +# define RSA_PKCS1_PADDING_SIZE 11 + +# define RSA_set_app_data(s,arg) RSA_set_ex_data(s,0,arg) +# define RSA_get_app_data(s) RSA_get_ex_data(s,0) + +RSA *RSA_new(void); +RSA *RSA_new_method(ENGINE *engine); +int RSA_bits(const RSA *rsa); +int RSA_size(const RSA *rsa); +int RSA_security_bits(const RSA *rsa); + +int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d); +int RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q); +int RSA_set0_crt_params(RSA *r,BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp); +int RSA_set0_multi_prime_params(RSA *r, BIGNUM *primes[], BIGNUM *exps[], + BIGNUM *coeffs[], int pnum); +void RSA_get0_key(const RSA *r, + const BIGNUM **n, const BIGNUM **e, const BIGNUM **d); +void RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q); +int RSA_get_multi_prime_extra_count(const RSA *r); +int RSA_get0_multi_prime_factors(const RSA *r, const BIGNUM *primes[]); +void RSA_get0_crt_params(const RSA *r, + const BIGNUM **dmp1, const BIGNUM **dmq1, + const BIGNUM **iqmp); +int RSA_get0_multi_prime_crt_params(const RSA *r, const BIGNUM *exps[], + const BIGNUM *coeffs[]); +const BIGNUM *RSA_get0_n(const RSA *d); +const BIGNUM *RSA_get0_e(const RSA *d); +const BIGNUM *RSA_get0_d(const RSA *d); +const BIGNUM *RSA_get0_p(const RSA *d); +const BIGNUM *RSA_get0_q(const RSA *d); +const BIGNUM *RSA_get0_dmp1(const RSA *r); +const BIGNUM *RSA_get0_dmq1(const RSA *r); +const BIGNUM *RSA_get0_iqmp(const RSA *r); +void RSA_clear_flags(RSA *r, int flags); +int RSA_test_flags(const RSA *r, int flags); +void RSA_set_flags(RSA *r, int flags); +int RSA_get_version(RSA *r); +ENGINE *RSA_get0_engine(const RSA *r); + +/* Deprecated version */ +DEPRECATEDIN_0_9_8(RSA *RSA_generate_key(int bits, unsigned long e, void + (*callback) (int, int, void *), + void *cb_arg)) + +/* New version */ +int RSA_generate_key_ex(RSA *rsa, int bits, BIGNUM *e, BN_GENCB *cb); +/* Multi-prime version */ +int RSA_generate_multi_prime_key(RSA *rsa, int bits, int primes, + BIGNUM *e, BN_GENCB *cb); + +int RSA_X931_derive_ex(RSA *rsa, BIGNUM *p1, BIGNUM *p2, BIGNUM *q1, + BIGNUM *q2, const BIGNUM *Xp1, const BIGNUM *Xp2, + const BIGNUM *Xp, const BIGNUM *Xq1, const BIGNUM *Xq2, + const BIGNUM *Xq, const BIGNUM *e, BN_GENCB *cb); +int RSA_X931_generate_key_ex(RSA *rsa, int bits, const BIGNUM *e, + BN_GENCB *cb); + +int RSA_check_key(const RSA *); +int RSA_check_key_ex(const RSA *, BN_GENCB *cb); + /* next 4 return -1 on error */ +int RSA_public_encrypt(int flen, const unsigned char *from, + unsigned char *to, RSA *rsa, int padding); +int RSA_private_encrypt(int flen, const unsigned char *from, + unsigned char *to, RSA *rsa, int padding); +int RSA_public_decrypt(int flen, const unsigned char *from, + unsigned char *to, RSA *rsa, int padding); +int RSA_private_decrypt(int flen, const unsigned char *from, + unsigned char *to, RSA *rsa, int padding); +void RSA_free(RSA *r); +/* "up" the RSA object's reference count */ +int RSA_up_ref(RSA *r); + +int RSA_flags(const RSA *r); + +void RSA_set_default_method(const RSA_METHOD *meth); +const RSA_METHOD *RSA_get_default_method(void); +const RSA_METHOD *RSA_null_method(void); +const RSA_METHOD *RSA_get_method(const RSA *rsa); +int RSA_set_method(RSA *rsa, const RSA_METHOD *meth); + +/* these are the actual RSA functions */ +const RSA_METHOD *RSA_PKCS1_OpenSSL(void); + +int RSA_pkey_ctx_ctrl(EVP_PKEY_CTX *ctx, int optype, int cmd, int p1, void *p2); + +DECLARE_ASN1_ENCODE_FUNCTIONS_const(RSA, RSAPublicKey) +DECLARE_ASN1_ENCODE_FUNCTIONS_const(RSA, RSAPrivateKey) + +typedef struct rsa_pss_params_st { + X509_ALGOR *hashAlgorithm; + X509_ALGOR *maskGenAlgorithm; + ASN1_INTEGER *saltLength; + ASN1_INTEGER *trailerField; + /* Decoded hash algorithm from maskGenAlgorithm */ + X509_ALGOR *maskHash; +} RSA_PSS_PARAMS; + +DECLARE_ASN1_FUNCTIONS(RSA_PSS_PARAMS) + +typedef struct rsa_oaep_params_st { + X509_ALGOR *hashFunc; + X509_ALGOR *maskGenFunc; + X509_ALGOR *pSourceFunc; + /* Decoded hash algorithm from maskGenFunc */ + X509_ALGOR *maskHash; +} RSA_OAEP_PARAMS; + +DECLARE_ASN1_FUNCTIONS(RSA_OAEP_PARAMS) + +# ifndef OPENSSL_NO_STDIO +int RSA_print_fp(FILE *fp, const RSA *r, int offset); +# endif + +int RSA_print(BIO *bp, const RSA *r, int offset); + +/* + * The following 2 functions sign and verify a X509_SIG ASN1 object inside + * PKCS#1 padded RSA encryption + */ +int RSA_sign(int type, const unsigned char *m, unsigned int m_length, + unsigned char *sigret, unsigned int *siglen, RSA *rsa); +int RSA_verify(int type, const unsigned char *m, unsigned int m_length, + const unsigned char *sigbuf, unsigned int siglen, RSA *rsa); + +/* + * The following 2 function sign and verify a ASN1_OCTET_STRING object inside + * PKCS#1 padded RSA encryption + */ +int RSA_sign_ASN1_OCTET_STRING(int type, + const unsigned char *m, unsigned int m_length, + unsigned char *sigret, unsigned int *siglen, + RSA *rsa); +int RSA_verify_ASN1_OCTET_STRING(int type, const unsigned char *m, + unsigned int m_length, unsigned char *sigbuf, + unsigned int siglen, RSA *rsa); + +int RSA_blinding_on(RSA *rsa, BN_CTX *ctx); +void RSA_blinding_off(RSA *rsa); +BN_BLINDING *RSA_setup_blinding(RSA *rsa, BN_CTX *ctx); + +int RSA_padding_add_PKCS1_type_1(unsigned char *to, int tlen, + const unsigned char *f, int fl); +int RSA_padding_check_PKCS1_type_1(unsigned char *to, int tlen, + const unsigned char *f, int fl, + int rsa_len); +int RSA_padding_add_PKCS1_type_2(unsigned char *to, int tlen, + const unsigned char *f, int fl); +int RSA_padding_check_PKCS1_type_2(unsigned char *to, int tlen, + const unsigned char *f, int fl, + int rsa_len); +int PKCS1_MGF1(unsigned char *mask, long len, const unsigned char *seed, + long seedlen, const EVP_MD *dgst); +int RSA_padding_add_PKCS1_OAEP(unsigned char *to, int tlen, + const unsigned char *f, int fl, + const unsigned char *p, int pl); +int RSA_padding_check_PKCS1_OAEP(unsigned char *to, int tlen, + const unsigned char *f, int fl, int rsa_len, + const unsigned char *p, int pl); +int RSA_padding_add_PKCS1_OAEP_mgf1(unsigned char *to, int tlen, + const unsigned char *from, int flen, + const unsigned char *param, int plen, + const EVP_MD *md, const EVP_MD *mgf1md); +int RSA_padding_check_PKCS1_OAEP_mgf1(unsigned char *to, int tlen, + const unsigned char *from, int flen, + int num, const unsigned char *param, + int plen, const EVP_MD *md, + const EVP_MD *mgf1md); +int RSA_padding_add_SSLv23(unsigned char *to, int tlen, + const unsigned char *f, int fl); +int RSA_padding_check_SSLv23(unsigned char *to, int tlen, + const unsigned char *f, int fl, int rsa_len); +int RSA_padding_add_none(unsigned char *to, int tlen, const unsigned char *f, + int fl); +int RSA_padding_check_none(unsigned char *to, int tlen, + const unsigned char *f, int fl, int rsa_len); +int RSA_padding_add_X931(unsigned char *to, int tlen, const unsigned char *f, + int fl); +int RSA_padding_check_X931(unsigned char *to, int tlen, + const unsigned char *f, int fl, int rsa_len); +int RSA_X931_hash_id(int nid); + +int RSA_verify_PKCS1_PSS(RSA *rsa, const unsigned char *mHash, + const EVP_MD *Hash, const unsigned char *EM, + int sLen); +int RSA_padding_add_PKCS1_PSS(RSA *rsa, unsigned char *EM, + const unsigned char *mHash, const EVP_MD *Hash, + int sLen); + +int RSA_verify_PKCS1_PSS_mgf1(RSA *rsa, const unsigned char *mHash, + const EVP_MD *Hash, const EVP_MD *mgf1Hash, + const unsigned char *EM, int sLen); + +int RSA_padding_add_PKCS1_PSS_mgf1(RSA *rsa, unsigned char *EM, + const unsigned char *mHash, + const EVP_MD *Hash, const EVP_MD *mgf1Hash, + int sLen); + +#define RSA_get_ex_new_index(l, p, newf, dupf, freef) \ + CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_RSA, l, p, newf, dupf, freef) +int RSA_set_ex_data(RSA *r, int idx, void *arg); +void *RSA_get_ex_data(const RSA *r, int idx); + +RSA *RSAPublicKey_dup(RSA *rsa); +RSA *RSAPrivateKey_dup(RSA *rsa); + +/* + * If this flag is set the RSA method is FIPS compliant and can be used in + * FIPS mode. This is set in the validated module method. If an application + * sets this flag in its own methods it is its responsibility to ensure the + * result is compliant. + */ + +# define RSA_FLAG_FIPS_METHOD 0x0400 + +/* + * If this flag is set the operations normally disabled in FIPS mode are + * permitted it is then the applications responsibility to ensure that the + * usage is compliant. + */ + +# define RSA_FLAG_NON_FIPS_ALLOW 0x0400 +/* + * Application has decided PRNG is good enough to generate a key: don't + * check. + */ +# define RSA_FLAG_CHECKED 0x0800 + +RSA_METHOD *RSA_meth_new(const char *name, int flags); +void RSA_meth_free(RSA_METHOD *meth); +RSA_METHOD *RSA_meth_dup(const RSA_METHOD *meth); +const char *RSA_meth_get0_name(const RSA_METHOD *meth); +int RSA_meth_set1_name(RSA_METHOD *meth, const char *name); +int RSA_meth_get_flags(const RSA_METHOD *meth); +int RSA_meth_set_flags(RSA_METHOD *meth, int flags); +void *RSA_meth_get0_app_data(const RSA_METHOD *meth); +int RSA_meth_set0_app_data(RSA_METHOD *meth, void *app_data); +int (*RSA_meth_get_pub_enc(const RSA_METHOD *meth)) + (int flen, const unsigned char *from, + unsigned char *to, RSA *rsa, int padding); +int RSA_meth_set_pub_enc(RSA_METHOD *rsa, + int (*pub_enc) (int flen, const unsigned char *from, + unsigned char *to, RSA *rsa, + int padding)); +int (*RSA_meth_get_pub_dec(const RSA_METHOD *meth)) + (int flen, const unsigned char *from, + unsigned char *to, RSA *rsa, int padding); +int RSA_meth_set_pub_dec(RSA_METHOD *rsa, + int (*pub_dec) (int flen, const unsigned char *from, + unsigned char *to, RSA *rsa, + int padding)); +int (*RSA_meth_get_priv_enc(const RSA_METHOD *meth)) + (int flen, const unsigned char *from, + unsigned char *to, RSA *rsa, int padding); +int RSA_meth_set_priv_enc(RSA_METHOD *rsa, + int (*priv_enc) (int flen, const unsigned char *from, + unsigned char *to, RSA *rsa, + int padding)); +int (*RSA_meth_get_priv_dec(const RSA_METHOD *meth)) + (int flen, const unsigned char *from, + unsigned char *to, RSA *rsa, int padding); +int RSA_meth_set_priv_dec(RSA_METHOD *rsa, + int (*priv_dec) (int flen, const unsigned char *from, + unsigned char *to, RSA *rsa, + int padding)); +int (*RSA_meth_get_mod_exp(const RSA_METHOD *meth)) + (BIGNUM *r0, const BIGNUM *i, RSA *rsa, BN_CTX *ctx); +int RSA_meth_set_mod_exp(RSA_METHOD *rsa, + int (*mod_exp) (BIGNUM *r0, const BIGNUM *i, RSA *rsa, + BN_CTX *ctx)); +int (*RSA_meth_get_bn_mod_exp(const RSA_METHOD *meth)) + (BIGNUM *r, const BIGNUM *a, const BIGNUM *p, + const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx); +int RSA_meth_set_bn_mod_exp(RSA_METHOD *rsa, + int (*bn_mod_exp) (BIGNUM *r, + const BIGNUM *a, + const BIGNUM *p, + const BIGNUM *m, + BN_CTX *ctx, + BN_MONT_CTX *m_ctx)); +int (*RSA_meth_get_init(const RSA_METHOD *meth)) (RSA *rsa); +int RSA_meth_set_init(RSA_METHOD *rsa, int (*init) (RSA *rsa)); +int (*RSA_meth_get_finish(const RSA_METHOD *meth)) (RSA *rsa); +int RSA_meth_set_finish(RSA_METHOD *rsa, int (*finish) (RSA *rsa)); +int (*RSA_meth_get_sign(const RSA_METHOD *meth)) + (int type, + const unsigned char *m, unsigned int m_length, + unsigned char *sigret, unsigned int *siglen, + const RSA *rsa); +int RSA_meth_set_sign(RSA_METHOD *rsa, + int (*sign) (int type, const unsigned char *m, + unsigned int m_length, + unsigned char *sigret, unsigned int *siglen, + const RSA *rsa)); +int (*RSA_meth_get_verify(const RSA_METHOD *meth)) + (int dtype, const unsigned char *m, + unsigned int m_length, const unsigned char *sigbuf, + unsigned int siglen, const RSA *rsa); +int RSA_meth_set_verify(RSA_METHOD *rsa, + int (*verify) (int dtype, const unsigned char *m, + unsigned int m_length, + const unsigned char *sigbuf, + unsigned int siglen, const RSA *rsa)); +int (*RSA_meth_get_keygen(const RSA_METHOD *meth)) + (RSA *rsa, int bits, BIGNUM *e, BN_GENCB *cb); +int RSA_meth_set_keygen(RSA_METHOD *rsa, + int (*keygen) (RSA *rsa, int bits, BIGNUM *e, + BN_GENCB *cb)); +int (*RSA_meth_get_multi_prime_keygen(const RSA_METHOD *meth)) + (RSA *rsa, int bits, int primes, BIGNUM *e, BN_GENCB *cb); +int RSA_meth_set_multi_prime_keygen(RSA_METHOD *meth, + int (*keygen) (RSA *rsa, int bits, + int primes, BIGNUM *e, + BN_GENCB *cb)); + +# ifdef __cplusplus +} +# endif +# endif +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/rsaerr.h b/example/android/third_party/crypto/arm/include/openssl/rsaerr.h new file mode 100644 index 00000000..d5bc01c1 --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/rsaerr.h @@ -0,0 +1,162 @@ +/* + * Generated by util/mkerr.pl DO NOT EDIT + * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_RSAERR_H +# define HEADER_RSAERR_H + +# ifdef __cplusplus +extern "C" +# endif +int ERR_load_RSA_strings(void); + +/* + * RSA function codes. + */ +# define RSA_F_CHECK_PADDING_MD 140 +# define RSA_F_ENCODE_PKCS1 146 +# define RSA_F_INT_RSA_VERIFY 145 +# define RSA_F_OLD_RSA_PRIV_DECODE 147 +# define RSA_F_PKEY_PSS_INIT 165 +# define RSA_F_PKEY_RSA_CTRL 143 +# define RSA_F_PKEY_RSA_CTRL_STR 144 +# define RSA_F_PKEY_RSA_SIGN 142 +# define RSA_F_PKEY_RSA_VERIFY 149 +# define RSA_F_PKEY_RSA_VERIFYRECOVER 141 +# define RSA_F_RSA_ALGOR_TO_MD 156 +# define RSA_F_RSA_BUILTIN_KEYGEN 129 +# define RSA_F_RSA_CHECK_KEY 123 +# define RSA_F_RSA_CHECK_KEY_EX 160 +# define RSA_F_RSA_CMS_DECRYPT 159 +# define RSA_F_RSA_CMS_VERIFY 158 +# define RSA_F_RSA_ITEM_VERIFY 148 +# define RSA_F_RSA_METH_DUP 161 +# define RSA_F_RSA_METH_NEW 162 +# define RSA_F_RSA_METH_SET1_NAME 163 +# define RSA_F_RSA_MGF1_TO_MD 157 +# define RSA_F_RSA_MULTIP_INFO_NEW 166 +# define RSA_F_RSA_NEW_METHOD 106 +# define RSA_F_RSA_NULL 124 +# define RSA_F_RSA_NULL_PRIVATE_DECRYPT 132 +# define RSA_F_RSA_NULL_PRIVATE_ENCRYPT 133 +# define RSA_F_RSA_NULL_PUBLIC_DECRYPT 134 +# define RSA_F_RSA_NULL_PUBLIC_ENCRYPT 135 +# define RSA_F_RSA_OSSL_PRIVATE_DECRYPT 101 +# define RSA_F_RSA_OSSL_PRIVATE_ENCRYPT 102 +# define RSA_F_RSA_OSSL_PUBLIC_DECRYPT 103 +# define RSA_F_RSA_OSSL_PUBLIC_ENCRYPT 104 +# define RSA_F_RSA_PADDING_ADD_NONE 107 +# define RSA_F_RSA_PADDING_ADD_PKCS1_OAEP 121 +# define RSA_F_RSA_PADDING_ADD_PKCS1_OAEP_MGF1 154 +# define RSA_F_RSA_PADDING_ADD_PKCS1_PSS 125 +# define RSA_F_RSA_PADDING_ADD_PKCS1_PSS_MGF1 152 +# define RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_1 108 +# define RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_2 109 +# define RSA_F_RSA_PADDING_ADD_SSLV23 110 +# define RSA_F_RSA_PADDING_ADD_X931 127 +# define RSA_F_RSA_PADDING_CHECK_NONE 111 +# define RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP 122 +# define RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP_MGF1 153 +# define RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1 112 +# define RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2 113 +# define RSA_F_RSA_PADDING_CHECK_SSLV23 114 +# define RSA_F_RSA_PADDING_CHECK_X931 128 +# define RSA_F_RSA_PARAM_DECODE 164 +# define RSA_F_RSA_PRINT 115 +# define RSA_F_RSA_PRINT_FP 116 +# define RSA_F_RSA_PRIV_DECODE 150 +# define RSA_F_RSA_PRIV_ENCODE 138 +# define RSA_F_RSA_PSS_GET_PARAM 151 +# define RSA_F_RSA_PSS_TO_CTX 155 +# define RSA_F_RSA_PUB_DECODE 139 +# define RSA_F_RSA_SETUP_BLINDING 136 +# define RSA_F_RSA_SIGN 117 +# define RSA_F_RSA_SIGN_ASN1_OCTET_STRING 118 +# define RSA_F_RSA_VERIFY 119 +# define RSA_F_RSA_VERIFY_ASN1_OCTET_STRING 120 +# define RSA_F_RSA_VERIFY_PKCS1_PSS_MGF1 126 +# define RSA_F_SETUP_TBUF 167 + +/* + * RSA reason codes. + */ +# define RSA_R_ALGORITHM_MISMATCH 100 +# define RSA_R_BAD_E_VALUE 101 +# define RSA_R_BAD_FIXED_HEADER_DECRYPT 102 +# define RSA_R_BAD_PAD_BYTE_COUNT 103 +# define RSA_R_BAD_SIGNATURE 104 +# define RSA_R_BLOCK_TYPE_IS_NOT_01 106 +# define RSA_R_BLOCK_TYPE_IS_NOT_02 107 +# define RSA_R_DATA_GREATER_THAN_MOD_LEN 108 +# define RSA_R_DATA_TOO_LARGE 109 +# define RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE 110 +# define RSA_R_DATA_TOO_LARGE_FOR_MODULUS 132 +# define RSA_R_DATA_TOO_SMALL 111 +# define RSA_R_DATA_TOO_SMALL_FOR_KEY_SIZE 122 +# define RSA_R_DIGEST_DOES_NOT_MATCH 158 +# define RSA_R_DIGEST_NOT_ALLOWED 145 +# define RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY 112 +# define RSA_R_DMP1_NOT_CONGRUENT_TO_D 124 +# define RSA_R_DMQ1_NOT_CONGRUENT_TO_D 125 +# define RSA_R_D_E_NOT_CONGRUENT_TO_1 123 +# define RSA_R_FIRST_OCTET_INVALID 133 +# define RSA_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE 144 +# define RSA_R_INVALID_DIGEST 157 +# define RSA_R_INVALID_DIGEST_LENGTH 143 +# define RSA_R_INVALID_HEADER 137 +# define RSA_R_INVALID_LABEL 160 +# define RSA_R_INVALID_MESSAGE_LENGTH 131 +# define RSA_R_INVALID_MGF1_MD 156 +# define RSA_R_INVALID_MULTI_PRIME_KEY 167 +# define RSA_R_INVALID_OAEP_PARAMETERS 161 +# define RSA_R_INVALID_PADDING 138 +# define RSA_R_INVALID_PADDING_MODE 141 +# define RSA_R_INVALID_PSS_PARAMETERS 149 +# define RSA_R_INVALID_PSS_SALTLEN 146 +# define RSA_R_INVALID_SALT_LENGTH 150 +# define RSA_R_INVALID_TRAILER 139 +# define RSA_R_INVALID_X931_DIGEST 142 +# define RSA_R_IQMP_NOT_INVERSE_OF_Q 126 +# define RSA_R_KEY_PRIME_NUM_INVALID 165 +# define RSA_R_KEY_SIZE_TOO_SMALL 120 +# define RSA_R_LAST_OCTET_INVALID 134 +# define RSA_R_MGF1_DIGEST_NOT_ALLOWED 152 +# define RSA_R_MODULUS_TOO_LARGE 105 +# define RSA_R_MP_COEFFICIENT_NOT_INVERSE_OF_R 168 +# define RSA_R_MP_EXPONENT_NOT_CONGRUENT_TO_D 169 +# define RSA_R_MP_R_NOT_PRIME 170 +# define RSA_R_NO_PUBLIC_EXPONENT 140 +# define RSA_R_NULL_BEFORE_BLOCK_MISSING 113 +# define RSA_R_N_DOES_NOT_EQUAL_PRODUCT_OF_PRIMES 172 +# define RSA_R_N_DOES_NOT_EQUAL_P_Q 127 +# define RSA_R_OAEP_DECODING_ERROR 121 +# define RSA_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE 148 +# define RSA_R_PADDING_CHECK_FAILED 114 +# define RSA_R_PKCS_DECODING_ERROR 159 +# define RSA_R_PSS_SALTLEN_TOO_SMALL 164 +# define RSA_R_P_NOT_PRIME 128 +# define RSA_R_Q_NOT_PRIME 129 +# define RSA_R_RSA_OPERATIONS_NOT_SUPPORTED 130 +# define RSA_R_SLEN_CHECK_FAILED 136 +# define RSA_R_SLEN_RECOVERY_FAILED 135 +# define RSA_R_SSLV3_ROLLBACK_ATTACK 115 +# define RSA_R_THE_ASN1_OBJECT_IDENTIFIER_IS_NOT_KNOWN_FOR_THIS_MD 116 +# define RSA_R_UNKNOWN_ALGORITHM_TYPE 117 +# define RSA_R_UNKNOWN_DIGEST 166 +# define RSA_R_UNKNOWN_MASK_DIGEST 151 +# define RSA_R_UNKNOWN_PADDING_TYPE 118 +# define RSA_R_UNSUPPORTED_ENCRYPTION_TYPE 162 +# define RSA_R_UNSUPPORTED_LABEL_SOURCE 163 +# define RSA_R_UNSUPPORTED_MASK_ALGORITHM 153 +# define RSA_R_UNSUPPORTED_MASK_PARAMETER 154 +# define RSA_R_UNSUPPORTED_SIGNATURE_TYPE 155 +# define RSA_R_VALUE_MISSING 147 +# define RSA_R_WRONG_SIGNATURE_LENGTH 119 + +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/safestack.h b/example/android/third_party/crypto/arm/include/openssl/safestack.h new file mode 100644 index 00000000..7438b193 --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/safestack.h @@ -0,0 +1,172 @@ +/* + * Copyright 1999-2017 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_SAFESTACK_H +# define HEADER_SAFESTACK_H + +# include +# include + +#ifdef __cplusplus +extern "C" { +#endif + +# define STACK_OF(type) struct stack_st_##type + +# define SKM_DEFINE_STACK_OF(t1, t2, t3) \ + STACK_OF(t1); \ + typedef int (*sk_##t1##_compfunc)(const t3 * const *a, const t3 *const *b); \ + typedef void (*sk_##t1##_freefunc)(t3 *a); \ + typedef t3 * (*sk_##t1##_copyfunc)(const t3 *a); \ + static ossl_inline int sk_##t1##_num(const STACK_OF(t1) *sk) \ + { \ + return OPENSSL_sk_num((const OPENSSL_STACK *)sk); \ + } \ + static ossl_inline t2 *sk_##t1##_value(const STACK_OF(t1) *sk, int idx) \ + { \ + return (t2 *)OPENSSL_sk_value((const OPENSSL_STACK *)sk, idx); \ + } \ + static ossl_inline STACK_OF(t1) *sk_##t1##_new(sk_##t1##_compfunc compare) \ + { \ + return (STACK_OF(t1) *)OPENSSL_sk_new((OPENSSL_sk_compfunc)compare); \ + } \ + static ossl_inline STACK_OF(t1) *sk_##t1##_new_null(void) \ + { \ + return (STACK_OF(t1) *)OPENSSL_sk_new_null(); \ + } \ + static ossl_inline STACK_OF(t1) *sk_##t1##_new_reserve(sk_##t1##_compfunc compare, int n) \ + { \ + return (STACK_OF(t1) *)OPENSSL_sk_new_reserve((OPENSSL_sk_compfunc)compare, n); \ + } \ + static ossl_inline int sk_##t1##_reserve(STACK_OF(t1) *sk, int n) \ + { \ + return OPENSSL_sk_reserve((OPENSSL_STACK *)sk, n); \ + } \ + static ossl_inline void sk_##t1##_free(STACK_OF(t1) *sk) \ + { \ + OPENSSL_sk_free((OPENSSL_STACK *)sk); \ + } \ + static ossl_inline void sk_##t1##_zero(STACK_OF(t1) *sk) \ + { \ + OPENSSL_sk_zero((OPENSSL_STACK *)sk); \ + } \ + static ossl_inline t2 *sk_##t1##_delete(STACK_OF(t1) *sk, int i) \ + { \ + return (t2 *)OPENSSL_sk_delete((OPENSSL_STACK *)sk, i); \ + } \ + static ossl_inline t2 *sk_##t1##_delete_ptr(STACK_OF(t1) *sk, t2 *ptr) \ + { \ + return (t2 *)OPENSSL_sk_delete_ptr((OPENSSL_STACK *)sk, \ + (const void *)ptr); \ + } \ + static ossl_inline int sk_##t1##_push(STACK_OF(t1) *sk, t2 *ptr) \ + { \ + return OPENSSL_sk_push((OPENSSL_STACK *)sk, (const void *)ptr); \ + } \ + static ossl_inline int sk_##t1##_unshift(STACK_OF(t1) *sk, t2 *ptr) \ + { \ + return OPENSSL_sk_unshift((OPENSSL_STACK *)sk, (const void *)ptr); \ + } \ + static ossl_inline t2 *sk_##t1##_pop(STACK_OF(t1) *sk) \ + { \ + return (t2 *)OPENSSL_sk_pop((OPENSSL_STACK *)sk); \ + } \ + static ossl_inline t2 *sk_##t1##_shift(STACK_OF(t1) *sk) \ + { \ + return (t2 *)OPENSSL_sk_shift((OPENSSL_STACK *)sk); \ + } \ + static ossl_inline void sk_##t1##_pop_free(STACK_OF(t1) *sk, sk_##t1##_freefunc freefunc) \ + { \ + OPENSSL_sk_pop_free((OPENSSL_STACK *)sk, (OPENSSL_sk_freefunc)freefunc); \ + } \ + static ossl_inline int sk_##t1##_insert(STACK_OF(t1) *sk, t2 *ptr, int idx) \ + { \ + return OPENSSL_sk_insert((OPENSSL_STACK *)sk, (const void *)ptr, idx); \ + } \ + static ossl_inline t2 *sk_##t1##_set(STACK_OF(t1) *sk, int idx, t2 *ptr) \ + { \ + return (t2 *)OPENSSL_sk_set((OPENSSL_STACK *)sk, idx, (const void *)ptr); \ + } \ + static ossl_inline int sk_##t1##_find(STACK_OF(t1) *sk, t2 *ptr) \ + { \ + return OPENSSL_sk_find((OPENSSL_STACK *)sk, (const void *)ptr); \ + } \ + static ossl_inline int sk_##t1##_find_ex(STACK_OF(t1) *sk, t2 *ptr) \ + { \ + return OPENSSL_sk_find_ex((OPENSSL_STACK *)sk, (const void *)ptr); \ + } \ + static ossl_inline void sk_##t1##_sort(STACK_OF(t1) *sk) \ + { \ + OPENSSL_sk_sort((OPENSSL_STACK *)sk); \ + } \ + static ossl_inline int sk_##t1##_is_sorted(const STACK_OF(t1) *sk) \ + { \ + return OPENSSL_sk_is_sorted((const OPENSSL_STACK *)sk); \ + } \ + static ossl_inline STACK_OF(t1) * sk_##t1##_dup(const STACK_OF(t1) *sk) \ + { \ + return (STACK_OF(t1) *)OPENSSL_sk_dup((const OPENSSL_STACK *)sk); \ + } \ + static ossl_inline STACK_OF(t1) *sk_##t1##_deep_copy(const STACK_OF(t1) *sk, \ + sk_##t1##_copyfunc copyfunc, \ + sk_##t1##_freefunc freefunc) \ + { \ + return (STACK_OF(t1) *)OPENSSL_sk_deep_copy((const OPENSSL_STACK *)sk, \ + (OPENSSL_sk_copyfunc)copyfunc, \ + (OPENSSL_sk_freefunc)freefunc); \ + } \ + static ossl_inline sk_##t1##_compfunc sk_##t1##_set_cmp_func(STACK_OF(t1) *sk, sk_##t1##_compfunc compare) \ + { \ + return (sk_##t1##_compfunc)OPENSSL_sk_set_cmp_func((OPENSSL_STACK *)sk, (OPENSSL_sk_compfunc)compare); \ + } + +# define DEFINE_SPECIAL_STACK_OF(t1, t2) SKM_DEFINE_STACK_OF(t1, t2, t2) +# define DEFINE_STACK_OF(t) SKM_DEFINE_STACK_OF(t, t, t) +# define DEFINE_SPECIAL_STACK_OF_CONST(t1, t2) \ + SKM_DEFINE_STACK_OF(t1, const t2, t2) +# define DEFINE_STACK_OF_CONST(t) SKM_DEFINE_STACK_OF(t, const t, t) + +/*- + * Strings are special: normally an lhash entry will point to a single + * (somewhat) mutable object. In the case of strings: + * + * a) Instead of a single char, there is an array of chars, NUL-terminated. + * b) The string may have be immutable. + * + * So, they need their own declarations. Especially important for + * type-checking tools, such as Deputy. + * + * In practice, however, it appears to be hard to have a const + * string. For now, I'm settling for dealing with the fact it is a + * string at all. + */ +typedef char *OPENSSL_STRING; +typedef const char *OPENSSL_CSTRING; + +/*- + * Confusingly, LHASH_OF(STRING) deals with char ** throughout, but + * STACK_OF(STRING) is really more like STACK_OF(char), only, as mentioned + * above, instead of a single char each entry is a NUL-terminated array of + * chars. So, we have to implement STRING specially for STACK_OF. This is + * dealt with in the autogenerated macros below. + */ +DEFINE_SPECIAL_STACK_OF(OPENSSL_STRING, char) +DEFINE_SPECIAL_STACK_OF_CONST(OPENSSL_CSTRING, char) + +/* + * Similarly, we sometimes use a block of characters, NOT nul-terminated. + * These should also be distinguished from "normal" stacks. + */ +typedef void *OPENSSL_BLOCK; +DEFINE_SPECIAL_STACK_OF(OPENSSL_BLOCK, void) + +# ifdef __cplusplus +} +# endif +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/seed.h b/example/android/third_party/crypto/arm/include/openssl/seed.h new file mode 100644 index 00000000..de10b085 --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/seed.h @@ -0,0 +1,96 @@ +/* + * Copyright 2007-2016 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +/* + * Copyright (c) 2007 KISA(Korea Information Security Agency). All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Neither the name of author nor the names of its contributors may + * be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef HEADER_SEED_H +# define HEADER_SEED_H + +# include + +# ifndef OPENSSL_NO_SEED +# include +# include + +#ifdef __cplusplus +extern "C" { +#endif + +/* look whether we need 'long' to get 32 bits */ +# ifdef AES_LONG +# ifndef SEED_LONG +# define SEED_LONG 1 +# endif +# endif + +# include + +# define SEED_BLOCK_SIZE 16 +# define SEED_KEY_LENGTH 16 + +typedef struct seed_key_st { +# ifdef SEED_LONG + unsigned long data[32]; +# else + unsigned int data[32]; +# endif +} SEED_KEY_SCHEDULE; + +void SEED_set_key(const unsigned char rawkey[SEED_KEY_LENGTH], + SEED_KEY_SCHEDULE *ks); + +void SEED_encrypt(const unsigned char s[SEED_BLOCK_SIZE], + unsigned char d[SEED_BLOCK_SIZE], + const SEED_KEY_SCHEDULE *ks); +void SEED_decrypt(const unsigned char s[SEED_BLOCK_SIZE], + unsigned char d[SEED_BLOCK_SIZE], + const SEED_KEY_SCHEDULE *ks); + +void SEED_ecb_encrypt(const unsigned char *in, unsigned char *out, + const SEED_KEY_SCHEDULE *ks, int enc); +void SEED_cbc_encrypt(const unsigned char *in, unsigned char *out, size_t len, + const SEED_KEY_SCHEDULE *ks, + unsigned char ivec[SEED_BLOCK_SIZE], int enc); +void SEED_cfb128_encrypt(const unsigned char *in, unsigned char *out, + size_t len, const SEED_KEY_SCHEDULE *ks, + unsigned char ivec[SEED_BLOCK_SIZE], int *num, + int enc); +void SEED_ofb128_encrypt(const unsigned char *in, unsigned char *out, + size_t len, const SEED_KEY_SCHEDULE *ks, + unsigned char ivec[SEED_BLOCK_SIZE], int *num); + +# ifdef __cplusplus +} +# endif +# endif + +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/sha.h b/example/android/third_party/crypto/arm/include/openssl/sha.h new file mode 100644 index 00000000..6a1eb0de --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/sha.h @@ -0,0 +1,119 @@ +/* + * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_SHA_H +# define HEADER_SHA_H + +# include +# include + +#ifdef __cplusplus +extern "C" { +#endif + +/*- + * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + * ! SHA_LONG has to be at least 32 bits wide. ! + * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + */ +# define SHA_LONG unsigned int + +# define SHA_LBLOCK 16 +# define SHA_CBLOCK (SHA_LBLOCK*4)/* SHA treats input data as a + * contiguous array of 32 bit wide + * big-endian values. */ +# define SHA_LAST_BLOCK (SHA_CBLOCK-8) +# define SHA_DIGEST_LENGTH 20 + +typedef struct SHAstate_st { + SHA_LONG h0, h1, h2, h3, h4; + SHA_LONG Nl, Nh; + SHA_LONG data[SHA_LBLOCK]; + unsigned int num; +} SHA_CTX; + +int SHA1_Init(SHA_CTX *c); +int SHA1_Update(SHA_CTX *c, const void *data, size_t len); +int SHA1_Final(unsigned char *md, SHA_CTX *c); +unsigned char *SHA1(const unsigned char *d, size_t n, unsigned char *md); +void SHA1_Transform(SHA_CTX *c, const unsigned char *data); + +# define SHA256_CBLOCK (SHA_LBLOCK*4)/* SHA-256 treats input data as a + * contiguous array of 32 bit wide + * big-endian values. */ + +typedef struct SHA256state_st { + SHA_LONG h[8]; + SHA_LONG Nl, Nh; + SHA_LONG data[SHA_LBLOCK]; + unsigned int num, md_len; +} SHA256_CTX; + +int SHA224_Init(SHA256_CTX *c); +int SHA224_Update(SHA256_CTX *c, const void *data, size_t len); +int SHA224_Final(unsigned char *md, SHA256_CTX *c); +unsigned char *SHA224(const unsigned char *d, size_t n, unsigned char *md); +int SHA256_Init(SHA256_CTX *c); +int SHA256_Update(SHA256_CTX *c, const void *data, size_t len); +int SHA256_Final(unsigned char *md, SHA256_CTX *c); +unsigned char *SHA256(const unsigned char *d, size_t n, unsigned char *md); +void SHA256_Transform(SHA256_CTX *c, const unsigned char *data); + +# define SHA224_DIGEST_LENGTH 28 +# define SHA256_DIGEST_LENGTH 32 +# define SHA384_DIGEST_LENGTH 48 +# define SHA512_DIGEST_LENGTH 64 + +/* + * Unlike 32-bit digest algorithms, SHA-512 *relies* on SHA_LONG64 + * being exactly 64-bit wide. See Implementation Notes in sha512.c + * for further details. + */ +/* + * SHA-512 treats input data as a + * contiguous array of 64 bit + * wide big-endian values. + */ +# define SHA512_CBLOCK (SHA_LBLOCK*8) +# if (defined(_WIN32) || defined(_WIN64)) && !defined(__MINGW32__) +# define SHA_LONG64 unsigned __int64 +# define U64(C) C##UI64 +# elif defined(__arch64__) +# define SHA_LONG64 unsigned long +# define U64(C) C##UL +# else +# define SHA_LONG64 unsigned long long +# define U64(C) C##ULL +# endif + +typedef struct SHA512state_st { + SHA_LONG64 h[8]; + SHA_LONG64 Nl, Nh; + union { + SHA_LONG64 d[SHA_LBLOCK]; + unsigned char p[SHA512_CBLOCK]; + } u; + unsigned int num, md_len; +} SHA512_CTX; + +int SHA384_Init(SHA512_CTX *c); +int SHA384_Update(SHA512_CTX *c, const void *data, size_t len); +int SHA384_Final(unsigned char *md, SHA512_CTX *c); +unsigned char *SHA384(const unsigned char *d, size_t n, unsigned char *md); +int SHA512_Init(SHA512_CTX *c); +int SHA512_Update(SHA512_CTX *c, const void *data, size_t len); +int SHA512_Final(unsigned char *md, SHA512_CTX *c); +unsigned char *SHA512(const unsigned char *d, size_t n, unsigned char *md); +void SHA512_Transform(SHA512_CTX *c, const unsigned char *data); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/srp.h b/example/android/third_party/crypto/arm/include/openssl/srp.h new file mode 100644 index 00000000..aaf13558 --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/srp.h @@ -0,0 +1,135 @@ +/* + * Copyright 2004-2018 The OpenSSL Project Authors. All Rights Reserved. + * Copyright (c) 2004, EdelKey Project. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + * + * Originally written by Christophe Renou and Peter Sylvester, + * for the EdelKey project. + */ + +#ifndef HEADER_SRP_H +# define HEADER_SRP_H + +#include + +#ifndef OPENSSL_NO_SRP +# include +# include +# include +# include +# include + +# ifdef __cplusplus +extern "C" { +# endif + +typedef struct SRP_gN_cache_st { + char *b64_bn; + BIGNUM *bn; +} SRP_gN_cache; + + +DEFINE_STACK_OF(SRP_gN_cache) + +typedef struct SRP_user_pwd_st { + /* Owned by us. */ + char *id; + BIGNUM *s; + BIGNUM *v; + /* Not owned by us. */ + const BIGNUM *g; + const BIGNUM *N; + /* Owned by us. */ + char *info; +} SRP_user_pwd; + +void SRP_user_pwd_free(SRP_user_pwd *user_pwd); + +DEFINE_STACK_OF(SRP_user_pwd) + +typedef struct SRP_VBASE_st { + STACK_OF(SRP_user_pwd) *users_pwd; + STACK_OF(SRP_gN_cache) *gN_cache; +/* to simulate a user */ + char *seed_key; + const BIGNUM *default_g; + const BIGNUM *default_N; +} SRP_VBASE; + +/* + * Internal structure storing N and g pair + */ +typedef struct SRP_gN_st { + char *id; + const BIGNUM *g; + const BIGNUM *N; +} SRP_gN; + +DEFINE_STACK_OF(SRP_gN) + +SRP_VBASE *SRP_VBASE_new(char *seed_key); +void SRP_VBASE_free(SRP_VBASE *vb); +int SRP_VBASE_init(SRP_VBASE *vb, char *verifier_file); + +/* This method ignores the configured seed and fails for an unknown user. */ +DEPRECATEDIN_1_1_0(SRP_user_pwd *SRP_VBASE_get_by_user(SRP_VBASE *vb, char *username)) +/* NOTE: unlike in SRP_VBASE_get_by_user, caller owns the returned pointer.*/ +SRP_user_pwd *SRP_VBASE_get1_by_user(SRP_VBASE *vb, char *username); + +char *SRP_create_verifier(const char *user, const char *pass, char **salt, + char **verifier, const char *N, const char *g); +int SRP_create_verifier_BN(const char *user, const char *pass, BIGNUM **salt, + BIGNUM **verifier, const BIGNUM *N, + const BIGNUM *g); + +# define SRP_NO_ERROR 0 +# define SRP_ERR_VBASE_INCOMPLETE_FILE 1 +# define SRP_ERR_VBASE_BN_LIB 2 +# define SRP_ERR_OPEN_FILE 3 +# define SRP_ERR_MEMORY 4 + +# define DB_srptype 0 +# define DB_srpverifier 1 +# define DB_srpsalt 2 +# define DB_srpid 3 +# define DB_srpgN 4 +# define DB_srpinfo 5 +# undef DB_NUMBER +# define DB_NUMBER 6 + +# define DB_SRP_INDEX 'I' +# define DB_SRP_VALID 'V' +# define DB_SRP_REVOKED 'R' +# define DB_SRP_MODIF 'v' + +/* see srp.c */ +char *SRP_check_known_gN_param(const BIGNUM *g, const BIGNUM *N); +SRP_gN *SRP_get_default_gN(const char *id); + +/* server side .... */ +BIGNUM *SRP_Calc_server_key(const BIGNUM *A, const BIGNUM *v, const BIGNUM *u, + const BIGNUM *b, const BIGNUM *N); +BIGNUM *SRP_Calc_B(const BIGNUM *b, const BIGNUM *N, const BIGNUM *g, + const BIGNUM *v); +int SRP_Verify_A_mod_N(const BIGNUM *A, const BIGNUM *N); +BIGNUM *SRP_Calc_u(const BIGNUM *A, const BIGNUM *B, const BIGNUM *N); + +/* client side .... */ +BIGNUM *SRP_Calc_x(const BIGNUM *s, const char *user, const char *pass); +BIGNUM *SRP_Calc_A(const BIGNUM *a, const BIGNUM *N, const BIGNUM *g); +BIGNUM *SRP_Calc_client_key(const BIGNUM *N, const BIGNUM *B, const BIGNUM *g, + const BIGNUM *x, const BIGNUM *a, const BIGNUM *u); +int SRP_Verify_B_mod_N(const BIGNUM *B, const BIGNUM *N); + +# define SRP_MINIMAL_N 1024 + +# ifdef __cplusplus +} +# endif +# endif + +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/srtp.h b/example/android/third_party/crypto/arm/include/openssl/srtp.h new file mode 100644 index 00000000..0b57c235 --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/srtp.h @@ -0,0 +1,50 @@ +/* + * Copyright 2011-2016 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +/* + * DTLS code by Eric Rescorla + * + * Copyright (C) 2006, Network Resonance, Inc. Copyright (C) 2011, RTFM, Inc. + */ + +#ifndef HEADER_D1_SRTP_H +# define HEADER_D1_SRTP_H + +# include + +#ifdef __cplusplus +extern "C" { +#endif + +# define SRTP_AES128_CM_SHA1_80 0x0001 +# define SRTP_AES128_CM_SHA1_32 0x0002 +# define SRTP_AES128_F8_SHA1_80 0x0003 +# define SRTP_AES128_F8_SHA1_32 0x0004 +# define SRTP_NULL_SHA1_80 0x0005 +# define SRTP_NULL_SHA1_32 0x0006 + +/* AEAD SRTP protection profiles from RFC 7714 */ +# define SRTP_AEAD_AES_128_GCM 0x0007 +# define SRTP_AEAD_AES_256_GCM 0x0008 + +# ifndef OPENSSL_NO_SRTP + +__owur int SSL_CTX_set_tlsext_use_srtp(SSL_CTX *ctx, const char *profiles); +__owur int SSL_set_tlsext_use_srtp(SSL *ssl, const char *profiles); + +__owur STACK_OF(SRTP_PROTECTION_PROFILE) *SSL_get_srtp_profiles(SSL *ssl); +__owur SRTP_PROTECTION_PROFILE *SSL_get_selected_srtp_profile(SSL *s); + +# endif + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/ssl.h b/example/android/third_party/crypto/arm/include/openssl/ssl.h new file mode 100644 index 00000000..d6b1b4e6 --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/ssl.h @@ -0,0 +1,2425 @@ +/* + * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. + * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved + * Copyright 2005 Nokia. All rights reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_SSL_H +# define HEADER_SSL_H + +# include +# include +# include +# include +# if OPENSSL_API_COMPAT < 0x10100000L +# include +# include +# include +# endif +# include +# include +# include +# include + +# include +# include +# include +# include + +#ifdef __cplusplus +extern "C" { +#endif + +/* OpenSSL version number for ASN.1 encoding of the session information */ +/*- + * Version 0 - initial version + * Version 1 - added the optional peer certificate + */ +# define SSL_SESSION_ASN1_VERSION 0x0001 + +# define SSL_MAX_SSL_SESSION_ID_LENGTH 32 +# define SSL_MAX_SID_CTX_LENGTH 32 + +# define SSL_MIN_RSA_MODULUS_LENGTH_IN_BYTES (512/8) +# define SSL_MAX_KEY_ARG_LENGTH 8 +# define SSL_MAX_MASTER_KEY_LENGTH 48 + +/* The maximum number of encrypt/decrypt pipelines we can support */ +# define SSL_MAX_PIPELINES 32 + +/* text strings for the ciphers */ + +/* These are used to specify which ciphers to use and not to use */ + +# define SSL_TXT_LOW "LOW" +# define SSL_TXT_MEDIUM "MEDIUM" +# define SSL_TXT_HIGH "HIGH" +# define SSL_TXT_FIPS "FIPS" + +# define SSL_TXT_aNULL "aNULL" +# define SSL_TXT_eNULL "eNULL" +# define SSL_TXT_NULL "NULL" + +# define SSL_TXT_kRSA "kRSA" +# define SSL_TXT_kDHr "kDHr"/* this cipher class has been removed */ +# define SSL_TXT_kDHd "kDHd"/* this cipher class has been removed */ +# define SSL_TXT_kDH "kDH"/* this cipher class has been removed */ +# define SSL_TXT_kEDH "kEDH"/* alias for kDHE */ +# define SSL_TXT_kDHE "kDHE" +# define SSL_TXT_kECDHr "kECDHr"/* this cipher class has been removed */ +# define SSL_TXT_kECDHe "kECDHe"/* this cipher class has been removed */ +# define SSL_TXT_kECDH "kECDH"/* this cipher class has been removed */ +# define SSL_TXT_kEECDH "kEECDH"/* alias for kECDHE */ +# define SSL_TXT_kECDHE "kECDHE" +# define SSL_TXT_kPSK "kPSK" +# define SSL_TXT_kRSAPSK "kRSAPSK" +# define SSL_TXT_kECDHEPSK "kECDHEPSK" +# define SSL_TXT_kDHEPSK "kDHEPSK" +# define SSL_TXT_kGOST "kGOST" +# define SSL_TXT_kSRP "kSRP" + +# define SSL_TXT_aRSA "aRSA" +# define SSL_TXT_aDSS "aDSS" +# define SSL_TXT_aDH "aDH"/* this cipher class has been removed */ +# define SSL_TXT_aECDH "aECDH"/* this cipher class has been removed */ +# define SSL_TXT_aECDSA "aECDSA" +# define SSL_TXT_aPSK "aPSK" +# define SSL_TXT_aGOST94 "aGOST94" +# define SSL_TXT_aGOST01 "aGOST01" +# define SSL_TXT_aGOST12 "aGOST12" +# define SSL_TXT_aGOST "aGOST" +# define SSL_TXT_aSRP "aSRP" + +# define SSL_TXT_DSS "DSS" +# define SSL_TXT_DH "DH" +# define SSL_TXT_DHE "DHE"/* same as "kDHE:-ADH" */ +# define SSL_TXT_EDH "EDH"/* alias for DHE */ +# define SSL_TXT_ADH "ADH" +# define SSL_TXT_RSA "RSA" +# define SSL_TXT_ECDH "ECDH" +# define SSL_TXT_EECDH "EECDH"/* alias for ECDHE" */ +# define SSL_TXT_ECDHE "ECDHE"/* same as "kECDHE:-AECDH" */ +# define SSL_TXT_AECDH "AECDH" +# define SSL_TXT_ECDSA "ECDSA" +# define SSL_TXT_PSK "PSK" +# define SSL_TXT_SRP "SRP" + +# define SSL_TXT_DES "DES" +# define SSL_TXT_3DES "3DES" +# define SSL_TXT_RC4 "RC4" +# define SSL_TXT_RC2 "RC2" +# define SSL_TXT_IDEA "IDEA" +# define SSL_TXT_SEED "SEED" +# define SSL_TXT_AES128 "AES128" +# define SSL_TXT_AES256 "AES256" +# define SSL_TXT_AES "AES" +# define SSL_TXT_AES_GCM "AESGCM" +# define SSL_TXT_AES_CCM "AESCCM" +# define SSL_TXT_AES_CCM_8 "AESCCM8" +# define SSL_TXT_CAMELLIA128 "CAMELLIA128" +# define SSL_TXT_CAMELLIA256 "CAMELLIA256" +# define SSL_TXT_CAMELLIA "CAMELLIA" +# define SSL_TXT_CHACHA20 "CHACHA20" +# define SSL_TXT_GOST "GOST89" +# define SSL_TXT_ARIA "ARIA" +# define SSL_TXT_ARIA_GCM "ARIAGCM" +# define SSL_TXT_ARIA128 "ARIA128" +# define SSL_TXT_ARIA256 "ARIA256" + +# define SSL_TXT_MD5 "MD5" +# define SSL_TXT_SHA1 "SHA1" +# define SSL_TXT_SHA "SHA"/* same as "SHA1" */ +# define SSL_TXT_GOST94 "GOST94" +# define SSL_TXT_GOST89MAC "GOST89MAC" +# define SSL_TXT_GOST12 "GOST12" +# define SSL_TXT_GOST89MAC12 "GOST89MAC12" +# define SSL_TXT_SHA256 "SHA256" +# define SSL_TXT_SHA384 "SHA384" + +# define SSL_TXT_SSLV3 "SSLv3" +# define SSL_TXT_TLSV1 "TLSv1" +# define SSL_TXT_TLSV1_1 "TLSv1.1" +# define SSL_TXT_TLSV1_2 "TLSv1.2" + +# define SSL_TXT_ALL "ALL" + +/*- + * COMPLEMENTOF* definitions. These identifiers are used to (de-select) + * ciphers normally not being used. + * Example: "RC4" will activate all ciphers using RC4 including ciphers + * without authentication, which would normally disabled by DEFAULT (due + * the "!ADH" being part of default). Therefore "RC4:!COMPLEMENTOFDEFAULT" + * will make sure that it is also disabled in the specific selection. + * COMPLEMENTOF* identifiers are portable between version, as adjustments + * to the default cipher setup will also be included here. + * + * COMPLEMENTOFDEFAULT does not experience the same special treatment that + * DEFAULT gets, as only selection is being done and no sorting as needed + * for DEFAULT. + */ +# define SSL_TXT_CMPALL "COMPLEMENTOFALL" +# define SSL_TXT_CMPDEF "COMPLEMENTOFDEFAULT" + +/* + * The following cipher list is used by default. It also is substituted when + * an application-defined cipher list string starts with 'DEFAULT'. + * This applies to ciphersuites for TLSv1.2 and below. + */ +# define SSL_DEFAULT_CIPHER_LIST "ALL:!COMPLEMENTOFDEFAULT:!eNULL" +/* This is the default set of TLSv1.3 ciphersuites */ +# if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305) +# define TLS_DEFAULT_CIPHERSUITES "TLS_AES_256_GCM_SHA384:" \ + "TLS_CHACHA20_POLY1305_SHA256:" \ + "TLS_AES_128_GCM_SHA256" +# else +# define TLS_DEFAULT_CIPHERSUITES "TLS_AES_256_GCM_SHA384:" \ + "TLS_AES_128_GCM_SHA256" +#endif +/* + * As of OpenSSL 1.0.0, ssl_create_cipher_list() in ssl/ssl_ciph.c always + * starts with a reasonable order, and all we have to do for DEFAULT is + * throwing out anonymous and unencrypted ciphersuites! (The latter are not + * actually enabled by ALL, but "ALL:RSA" would enable some of them.) + */ + +/* Used in SSL_set_shutdown()/SSL_get_shutdown(); */ +# define SSL_SENT_SHUTDOWN 1 +# define SSL_RECEIVED_SHUTDOWN 2 + +#ifdef __cplusplus +} +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +# define SSL_FILETYPE_ASN1 X509_FILETYPE_ASN1 +# define SSL_FILETYPE_PEM X509_FILETYPE_PEM + +/* + * This is needed to stop compilers complaining about the 'struct ssl_st *' + * function parameters used to prototype callbacks in SSL_CTX. + */ +typedef struct ssl_st *ssl_crock_st; +typedef struct tls_session_ticket_ext_st TLS_SESSION_TICKET_EXT; +typedef struct ssl_method_st SSL_METHOD; +typedef struct ssl_cipher_st SSL_CIPHER; +typedef struct ssl_session_st SSL_SESSION; +typedef struct tls_sigalgs_st TLS_SIGALGS; +typedef struct ssl_conf_ctx_st SSL_CONF_CTX; +typedef struct ssl_comp_st SSL_COMP; + +STACK_OF(SSL_CIPHER); +STACK_OF(SSL_COMP); + +/* SRTP protection profiles for use with the use_srtp extension (RFC 5764)*/ +typedef struct srtp_protection_profile_st { + const char *name; + unsigned long id; +} SRTP_PROTECTION_PROFILE; + +DEFINE_STACK_OF(SRTP_PROTECTION_PROFILE) + +typedef int (*tls_session_ticket_ext_cb_fn)(SSL *s, const unsigned char *data, + int len, void *arg); +typedef int (*tls_session_secret_cb_fn)(SSL *s, void *secret, int *secret_len, + STACK_OF(SSL_CIPHER) *peer_ciphers, + const SSL_CIPHER **cipher, void *arg); + +/* Extension context codes */ +/* This extension is only allowed in TLS */ +#define SSL_EXT_TLS_ONLY 0x0001 +/* This extension is only allowed in DTLS */ +#define SSL_EXT_DTLS_ONLY 0x0002 +/* Some extensions may be allowed in DTLS but we don't implement them for it */ +#define SSL_EXT_TLS_IMPLEMENTATION_ONLY 0x0004 +/* Most extensions are not defined for SSLv3 but EXT_TYPE_renegotiate is */ +#define SSL_EXT_SSL3_ALLOWED 0x0008 +/* Extension is only defined for TLS1.2 and below */ +#define SSL_EXT_TLS1_2_AND_BELOW_ONLY 0x0010 +/* Extension is only defined for TLS1.3 and above */ +#define SSL_EXT_TLS1_3_ONLY 0x0020 +/* Ignore this extension during parsing if we are resuming */ +#define SSL_EXT_IGNORE_ON_RESUMPTION 0x0040 +#define SSL_EXT_CLIENT_HELLO 0x0080 +/* Really means TLS1.2 or below */ +#define SSL_EXT_TLS1_2_SERVER_HELLO 0x0100 +#define SSL_EXT_TLS1_3_SERVER_HELLO 0x0200 +#define SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS 0x0400 +#define SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST 0x0800 +#define SSL_EXT_TLS1_3_CERTIFICATE 0x1000 +#define SSL_EXT_TLS1_3_NEW_SESSION_TICKET 0x2000 +#define SSL_EXT_TLS1_3_CERTIFICATE_REQUEST 0x4000 + +/* Typedefs for handling custom extensions */ + +typedef int (*custom_ext_add_cb)(SSL *s, unsigned int ext_type, + const unsigned char **out, size_t *outlen, + int *al, void *add_arg); + +typedef void (*custom_ext_free_cb)(SSL *s, unsigned int ext_type, + const unsigned char *out, void *add_arg); + +typedef int (*custom_ext_parse_cb)(SSL *s, unsigned int ext_type, + const unsigned char *in, size_t inlen, + int *al, void *parse_arg); + + +typedef int (*SSL_custom_ext_add_cb_ex)(SSL *s, unsigned int ext_type, + unsigned int context, + const unsigned char **out, + size_t *outlen, X509 *x, + size_t chainidx, + int *al, void *add_arg); + +typedef void (*SSL_custom_ext_free_cb_ex)(SSL *s, unsigned int ext_type, + unsigned int context, + const unsigned char *out, + void *add_arg); + +typedef int (*SSL_custom_ext_parse_cb_ex)(SSL *s, unsigned int ext_type, + unsigned int context, + const unsigned char *in, + size_t inlen, X509 *x, + size_t chainidx, + int *al, void *parse_arg); + +/* Typedef for verification callback */ +typedef int (*SSL_verify_cb)(int preverify_ok, X509_STORE_CTX *x509_ctx); + +/* + * Some values are reserved until OpenSSL 1.2.0 because they were previously + * included in SSL_OP_ALL in a 1.1.x release. + * + * Reserved value (until OpenSSL 1.2.0) 0x00000001U + * Reserved value (until OpenSSL 1.2.0) 0x00000002U + */ +/* Allow initial connection to servers that don't support RI */ +# define SSL_OP_LEGACY_SERVER_CONNECT 0x00000004U + +/* Reserved value (until OpenSSL 1.2.0) 0x00000008U */ +# define SSL_OP_TLSEXT_PADDING 0x00000010U +/* Reserved value (until OpenSSL 1.2.0) 0x00000020U */ +# define SSL_OP_SAFARI_ECDHE_ECDSA_BUG 0x00000040U +/* + * Reserved value (until OpenSSL 1.2.0) 0x00000080U + * Reserved value (until OpenSSL 1.2.0) 0x00000100U + * Reserved value (until OpenSSL 1.2.0) 0x00000200U + */ + +/* In TLSv1.3 allow a non-(ec)dhe based kex_mode */ +# define SSL_OP_ALLOW_NO_DHE_KEX 0x00000400U + +/* + * Disable SSL 3.0/TLS 1.0 CBC vulnerability workaround that was added in + * OpenSSL 0.9.6d. Usually (depending on the application protocol) the + * workaround is not needed. Unfortunately some broken SSL/TLS + * implementations cannot handle it at all, which is why we include it in + * SSL_OP_ALL. Added in 0.9.6e + */ +# define SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS 0x00000800U + +/* DTLS options */ +# define SSL_OP_NO_QUERY_MTU 0x00001000U +/* Turn on Cookie Exchange (on relevant for servers) */ +# define SSL_OP_COOKIE_EXCHANGE 0x00002000U +/* Don't use RFC4507 ticket extension */ +# define SSL_OP_NO_TICKET 0x00004000U +# ifndef OPENSSL_NO_DTLS1_METHOD +/* Use Cisco's "speshul" version of DTLS_BAD_VER + * (only with deprecated DTLSv1_client_method()) */ +# define SSL_OP_CISCO_ANYCONNECT 0x00008000U +# endif + +/* As server, disallow session resumption on renegotiation */ +# define SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION 0x00010000U +/* Don't use compression even if supported */ +# define SSL_OP_NO_COMPRESSION 0x00020000U +/* Permit unsafe legacy renegotiation */ +# define SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION 0x00040000U +/* Disable encrypt-then-mac */ +# define SSL_OP_NO_ENCRYPT_THEN_MAC 0x00080000U + +/* + * Enable TLSv1.3 Compatibility mode. This is on by default. A future version + * of OpenSSL may have this disabled by default. + */ +# define SSL_OP_ENABLE_MIDDLEBOX_COMPAT 0x00100000U + +/* Prioritize Chacha20Poly1305 when client does. + * Modifies SSL_OP_CIPHER_SERVER_PREFERENCE */ +# define SSL_OP_PRIORITIZE_CHACHA 0x00200000U + +/* + * Set on servers to choose the cipher according to the server's preferences + */ +# define SSL_OP_CIPHER_SERVER_PREFERENCE 0x00400000U +/* + * If set, a server will allow a client to issue a SSLv3.0 version number as + * latest version supported in the premaster secret, even when TLSv1.0 + * (version 3.1) was announced in the client hello. Normally this is + * forbidden to prevent version rollback attacks. + */ +# define SSL_OP_TLS_ROLLBACK_BUG 0x00800000U + +/* + * Switches off automatic TLSv1.3 anti-replay protection for early data. This + * is a server-side option only (no effect on the client). + */ +# define SSL_OP_NO_ANTI_REPLAY 0x01000000U + +# define SSL_OP_NO_SSLv3 0x02000000U +# define SSL_OP_NO_TLSv1 0x04000000U +# define SSL_OP_NO_TLSv1_2 0x08000000U +# define SSL_OP_NO_TLSv1_1 0x10000000U +# define SSL_OP_NO_TLSv1_3 0x20000000U + +# define SSL_OP_NO_DTLSv1 0x04000000U +# define SSL_OP_NO_DTLSv1_2 0x08000000U + +# define SSL_OP_NO_SSL_MASK (SSL_OP_NO_SSLv3|\ + SSL_OP_NO_TLSv1|SSL_OP_NO_TLSv1_1|SSL_OP_NO_TLSv1_2|SSL_OP_NO_TLSv1_3) +# define SSL_OP_NO_DTLS_MASK (SSL_OP_NO_DTLSv1|SSL_OP_NO_DTLSv1_2) + +/* Disallow all renegotiation */ +# define SSL_OP_NO_RENEGOTIATION 0x40000000U + +/* + * Make server add server-hello extension from early version of cryptopro + * draft, when GOST ciphersuite is negotiated. Required for interoperability + * with CryptoPro CSP 3.x + */ +# define SSL_OP_CRYPTOPRO_TLSEXT_BUG 0x80000000U + +/* + * SSL_OP_ALL: various bug workarounds that should be rather harmless. + * This used to be 0x000FFFFFL before 0.9.7. + * This used to be 0x80000BFFU before 1.1.1. + */ +# define SSL_OP_ALL (SSL_OP_CRYPTOPRO_TLSEXT_BUG|\ + SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS|\ + SSL_OP_LEGACY_SERVER_CONNECT|\ + SSL_OP_TLSEXT_PADDING|\ + SSL_OP_SAFARI_ECDHE_ECDSA_BUG) + +/* OBSOLETE OPTIONS: retained for compatibility */ + +/* Removed from OpenSSL 1.1.0. Was 0x00000001L */ +/* Related to removed SSLv2. */ +# define SSL_OP_MICROSOFT_SESS_ID_BUG 0x0 +/* Removed from OpenSSL 1.1.0. Was 0x00000002L */ +/* Related to removed SSLv2. */ +# define SSL_OP_NETSCAPE_CHALLENGE_BUG 0x0 +/* Removed from OpenSSL 0.9.8q and 1.0.0c. Was 0x00000008L */ +/* Dead forever, see CVE-2010-4180 */ +# define SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG 0x0 +/* Removed from OpenSSL 1.0.1h and 1.0.2. Was 0x00000010L */ +/* Refers to ancient SSLREF and SSLv2. */ +# define SSL_OP_SSLREF2_REUSE_CERT_TYPE_BUG 0x0 +/* Removed from OpenSSL 1.1.0. Was 0x00000020 */ +# define SSL_OP_MICROSOFT_BIG_SSLV3_BUFFER 0x0 +/* Removed from OpenSSL 0.9.7h and 0.9.8b. Was 0x00000040L */ +# define SSL_OP_MSIE_SSLV2_RSA_PADDING 0x0 +/* Removed from OpenSSL 1.1.0. Was 0x00000080 */ +/* Ancient SSLeay version. */ +# define SSL_OP_SSLEAY_080_CLIENT_DH_BUG 0x0 +/* Removed from OpenSSL 1.1.0. Was 0x00000100L */ +# define SSL_OP_TLS_D5_BUG 0x0 +/* Removed from OpenSSL 1.1.0. Was 0x00000200L */ +# define SSL_OP_TLS_BLOCK_PADDING_BUG 0x0 +/* Removed from OpenSSL 1.1.0. Was 0x00080000L */ +# define SSL_OP_SINGLE_ECDH_USE 0x0 +/* Removed from OpenSSL 1.1.0. Was 0x00100000L */ +# define SSL_OP_SINGLE_DH_USE 0x0 +/* Removed from OpenSSL 1.0.1k and 1.0.2. Was 0x00200000L */ +# define SSL_OP_EPHEMERAL_RSA 0x0 +/* Removed from OpenSSL 1.1.0. Was 0x01000000L */ +# define SSL_OP_NO_SSLv2 0x0 +/* Removed from OpenSSL 1.0.1. Was 0x08000000L */ +# define SSL_OP_PKCS1_CHECK_1 0x0 +/* Removed from OpenSSL 1.0.1. Was 0x10000000L */ +# define SSL_OP_PKCS1_CHECK_2 0x0 +/* Removed from OpenSSL 1.1.0. Was 0x20000000L */ +# define SSL_OP_NETSCAPE_CA_DN_BUG 0x0 +/* Removed from OpenSSL 1.1.0. Was 0x40000000L */ +# define SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG 0x0 + +/* + * Allow SSL_write(..., n) to return r with 0 < r < n (i.e. report success + * when just a single record has been written): + */ +# define SSL_MODE_ENABLE_PARTIAL_WRITE 0x00000001U +/* + * Make it possible to retry SSL_write() with changed buffer location (buffer + * contents must stay the same!); this is not the default to avoid the + * misconception that non-blocking SSL_write() behaves like non-blocking + * write(): + */ +# define SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER 0x00000002U +/* + * Never bother the application with retries if the transport is blocking: + */ +# define SSL_MODE_AUTO_RETRY 0x00000004U +/* Don't attempt to automatically build certificate chain */ +# define SSL_MODE_NO_AUTO_CHAIN 0x00000008U +/* + * Save RAM by releasing read and write buffers when they're empty. (SSL3 and + * TLS only.) Released buffers are freed. + */ +# define SSL_MODE_RELEASE_BUFFERS 0x00000010U +/* + * Send the current time in the Random fields of the ClientHello and + * ServerHello records for compatibility with hypothetical implementations + * that require it. + */ +# define SSL_MODE_SEND_CLIENTHELLO_TIME 0x00000020U +# define SSL_MODE_SEND_SERVERHELLO_TIME 0x00000040U +/* + * Send TLS_FALLBACK_SCSV in the ClientHello. To be set only by applications + * that reconnect with a downgraded protocol version; see + * draft-ietf-tls-downgrade-scsv-00 for details. DO NOT ENABLE THIS if your + * application attempts a normal handshake. Only use this in explicit + * fallback retries, following the guidance in + * draft-ietf-tls-downgrade-scsv-00. + */ +# define SSL_MODE_SEND_FALLBACK_SCSV 0x00000080U +/* + * Support Asynchronous operation + */ +# define SSL_MODE_ASYNC 0x00000100U + +/* Cert related flags */ +/* + * Many implementations ignore some aspects of the TLS standards such as + * enforcing certificate chain algorithms. When this is set we enforce them. + */ +# define SSL_CERT_FLAG_TLS_STRICT 0x00000001U + +/* Suite B modes, takes same values as certificate verify flags */ +# define SSL_CERT_FLAG_SUITEB_128_LOS_ONLY 0x10000 +/* Suite B 192 bit only mode */ +# define SSL_CERT_FLAG_SUITEB_192_LOS 0x20000 +/* Suite B 128 bit mode allowing 192 bit algorithms */ +# define SSL_CERT_FLAG_SUITEB_128_LOS 0x30000 + +/* Perform all sorts of protocol violations for testing purposes */ +# define SSL_CERT_FLAG_BROKEN_PROTOCOL 0x10000000 + +/* Flags for building certificate chains */ +/* Treat any existing certificates as untrusted CAs */ +# define SSL_BUILD_CHAIN_FLAG_UNTRUSTED 0x1 +/* Don't include root CA in chain */ +# define SSL_BUILD_CHAIN_FLAG_NO_ROOT 0x2 +/* Just check certificates already there */ +# define SSL_BUILD_CHAIN_FLAG_CHECK 0x4 +/* Ignore verification errors */ +# define SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR 0x8 +/* Clear verification errors from queue */ +# define SSL_BUILD_CHAIN_FLAG_CLEAR_ERROR 0x10 + +/* Flags returned by SSL_check_chain */ +/* Certificate can be used with this session */ +# define CERT_PKEY_VALID 0x1 +/* Certificate can also be used for signing */ +# define CERT_PKEY_SIGN 0x2 +/* EE certificate signing algorithm OK */ +# define CERT_PKEY_EE_SIGNATURE 0x10 +/* CA signature algorithms OK */ +# define CERT_PKEY_CA_SIGNATURE 0x20 +/* EE certificate parameters OK */ +# define CERT_PKEY_EE_PARAM 0x40 +/* CA certificate parameters OK */ +# define CERT_PKEY_CA_PARAM 0x80 +/* Signing explicitly allowed as opposed to SHA1 fallback */ +# define CERT_PKEY_EXPLICIT_SIGN 0x100 +/* Client CA issuer names match (always set for server cert) */ +# define CERT_PKEY_ISSUER_NAME 0x200 +/* Cert type matches client types (always set for server cert) */ +# define CERT_PKEY_CERT_TYPE 0x400 +/* Cert chain suitable to Suite B */ +# define CERT_PKEY_SUITEB 0x800 + +# define SSL_CONF_FLAG_CMDLINE 0x1 +# define SSL_CONF_FLAG_FILE 0x2 +# define SSL_CONF_FLAG_CLIENT 0x4 +# define SSL_CONF_FLAG_SERVER 0x8 +# define SSL_CONF_FLAG_SHOW_ERRORS 0x10 +# define SSL_CONF_FLAG_CERTIFICATE 0x20 +# define SSL_CONF_FLAG_REQUIRE_PRIVATE 0x40 +/* Configuration value types */ +# define SSL_CONF_TYPE_UNKNOWN 0x0 +# define SSL_CONF_TYPE_STRING 0x1 +# define SSL_CONF_TYPE_FILE 0x2 +# define SSL_CONF_TYPE_DIR 0x3 +# define SSL_CONF_TYPE_NONE 0x4 + +/* Maximum length of the application-controlled segment of a a TLSv1.3 cookie */ +# define SSL_COOKIE_LENGTH 4096 + +/* + * Note: SSL[_CTX]_set_{options,mode} use |= op on the previous value, they + * cannot be used to clear bits. + */ + +unsigned long SSL_CTX_get_options(const SSL_CTX *ctx); +unsigned long SSL_get_options(const SSL *s); +unsigned long SSL_CTX_clear_options(SSL_CTX *ctx, unsigned long op); +unsigned long SSL_clear_options(SSL *s, unsigned long op); +unsigned long SSL_CTX_set_options(SSL_CTX *ctx, unsigned long op); +unsigned long SSL_set_options(SSL *s, unsigned long op); + +# define SSL_CTX_set_mode(ctx,op) \ + SSL_CTX_ctrl((ctx),SSL_CTRL_MODE,(op),NULL) +# define SSL_CTX_clear_mode(ctx,op) \ + SSL_CTX_ctrl((ctx),SSL_CTRL_CLEAR_MODE,(op),NULL) +# define SSL_CTX_get_mode(ctx) \ + SSL_CTX_ctrl((ctx),SSL_CTRL_MODE,0,NULL) +# define SSL_clear_mode(ssl,op) \ + SSL_ctrl((ssl),SSL_CTRL_CLEAR_MODE,(op),NULL) +# define SSL_set_mode(ssl,op) \ + SSL_ctrl((ssl),SSL_CTRL_MODE,(op),NULL) +# define SSL_get_mode(ssl) \ + SSL_ctrl((ssl),SSL_CTRL_MODE,0,NULL) +# define SSL_set_mtu(ssl, mtu) \ + SSL_ctrl((ssl),SSL_CTRL_SET_MTU,(mtu),NULL) +# define DTLS_set_link_mtu(ssl, mtu) \ + SSL_ctrl((ssl),DTLS_CTRL_SET_LINK_MTU,(mtu),NULL) +# define DTLS_get_link_min_mtu(ssl) \ + SSL_ctrl((ssl),DTLS_CTRL_GET_LINK_MIN_MTU,0,NULL) + +# define SSL_get_secure_renegotiation_support(ssl) \ + SSL_ctrl((ssl), SSL_CTRL_GET_RI_SUPPORT, 0, NULL) + +# ifndef OPENSSL_NO_HEARTBEATS +# define SSL_heartbeat(ssl) \ + SSL_ctrl((ssl),SSL_CTRL_DTLS_EXT_SEND_HEARTBEAT,0,NULL) +# endif + +# define SSL_CTX_set_cert_flags(ctx,op) \ + SSL_CTX_ctrl((ctx),SSL_CTRL_CERT_FLAGS,(op),NULL) +# define SSL_set_cert_flags(s,op) \ + SSL_ctrl((s),SSL_CTRL_CERT_FLAGS,(op),NULL) +# define SSL_CTX_clear_cert_flags(ctx,op) \ + SSL_CTX_ctrl((ctx),SSL_CTRL_CLEAR_CERT_FLAGS,(op),NULL) +# define SSL_clear_cert_flags(s,op) \ + SSL_ctrl((s),SSL_CTRL_CLEAR_CERT_FLAGS,(op),NULL) + +void SSL_CTX_set_msg_callback(SSL_CTX *ctx, + void (*cb) (int write_p, int version, + int content_type, const void *buf, + size_t len, SSL *ssl, void *arg)); +void SSL_set_msg_callback(SSL *ssl, + void (*cb) (int write_p, int version, + int content_type, const void *buf, + size_t len, SSL *ssl, void *arg)); +# define SSL_CTX_set_msg_callback_arg(ctx, arg) SSL_CTX_ctrl((ctx), SSL_CTRL_SET_MSG_CALLBACK_ARG, 0, (arg)) +# define SSL_set_msg_callback_arg(ssl, arg) SSL_ctrl((ssl), SSL_CTRL_SET_MSG_CALLBACK_ARG, 0, (arg)) + +# define SSL_get_extms_support(s) \ + SSL_ctrl((s),SSL_CTRL_GET_EXTMS_SUPPORT,0,NULL) + +# ifndef OPENSSL_NO_SRP + +/* see tls_srp.c */ +__owur int SSL_SRP_CTX_init(SSL *s); +__owur int SSL_CTX_SRP_CTX_init(SSL_CTX *ctx); +int SSL_SRP_CTX_free(SSL *ctx); +int SSL_CTX_SRP_CTX_free(SSL_CTX *ctx); +__owur int SSL_srp_server_param_with_username(SSL *s, int *ad); +__owur int SRP_Calc_A_param(SSL *s); + +# endif + +/* 100k max cert list */ +# define SSL_MAX_CERT_LIST_DEFAULT 1024*100 + +# define SSL_SESSION_CACHE_MAX_SIZE_DEFAULT (1024*20) + +/* + * This callback type is used inside SSL_CTX, SSL, and in the functions that + * set them. It is used to override the generation of SSL/TLS session IDs in + * a server. Return value should be zero on an error, non-zero to proceed. + * Also, callbacks should themselves check if the id they generate is unique + * otherwise the SSL handshake will fail with an error - callbacks can do + * this using the 'ssl' value they're passed by; + * SSL_has_matching_session_id(ssl, id, *id_len) The length value passed in + * is set at the maximum size the session ID can be. In SSLv3/TLSv1 it is 32 + * bytes. The callback can alter this length to be less if desired. It is + * also an error for the callback to set the size to zero. + */ +typedef int (*GEN_SESSION_CB) (SSL *ssl, unsigned char *id, + unsigned int *id_len); + +# define SSL_SESS_CACHE_OFF 0x0000 +# define SSL_SESS_CACHE_CLIENT 0x0001 +# define SSL_SESS_CACHE_SERVER 0x0002 +# define SSL_SESS_CACHE_BOTH (SSL_SESS_CACHE_CLIENT|SSL_SESS_CACHE_SERVER) +# define SSL_SESS_CACHE_NO_AUTO_CLEAR 0x0080 +/* enough comments already ... see SSL_CTX_set_session_cache_mode(3) */ +# define SSL_SESS_CACHE_NO_INTERNAL_LOOKUP 0x0100 +# define SSL_SESS_CACHE_NO_INTERNAL_STORE 0x0200 +# define SSL_SESS_CACHE_NO_INTERNAL \ + (SSL_SESS_CACHE_NO_INTERNAL_LOOKUP|SSL_SESS_CACHE_NO_INTERNAL_STORE) + +LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx); +# define SSL_CTX_sess_number(ctx) \ + SSL_CTX_ctrl(ctx,SSL_CTRL_SESS_NUMBER,0,NULL) +# define SSL_CTX_sess_connect(ctx) \ + SSL_CTX_ctrl(ctx,SSL_CTRL_SESS_CONNECT,0,NULL) +# define SSL_CTX_sess_connect_good(ctx) \ + SSL_CTX_ctrl(ctx,SSL_CTRL_SESS_CONNECT_GOOD,0,NULL) +# define SSL_CTX_sess_connect_renegotiate(ctx) \ + SSL_CTX_ctrl(ctx,SSL_CTRL_SESS_CONNECT_RENEGOTIATE,0,NULL) +# define SSL_CTX_sess_accept(ctx) \ + SSL_CTX_ctrl(ctx,SSL_CTRL_SESS_ACCEPT,0,NULL) +# define SSL_CTX_sess_accept_renegotiate(ctx) \ + SSL_CTX_ctrl(ctx,SSL_CTRL_SESS_ACCEPT_RENEGOTIATE,0,NULL) +# define SSL_CTX_sess_accept_good(ctx) \ + SSL_CTX_ctrl(ctx,SSL_CTRL_SESS_ACCEPT_GOOD,0,NULL) +# define SSL_CTX_sess_hits(ctx) \ + SSL_CTX_ctrl(ctx,SSL_CTRL_SESS_HIT,0,NULL) +# define SSL_CTX_sess_cb_hits(ctx) \ + SSL_CTX_ctrl(ctx,SSL_CTRL_SESS_CB_HIT,0,NULL) +# define SSL_CTX_sess_misses(ctx) \ + SSL_CTX_ctrl(ctx,SSL_CTRL_SESS_MISSES,0,NULL) +# define SSL_CTX_sess_timeouts(ctx) \ + SSL_CTX_ctrl(ctx,SSL_CTRL_SESS_TIMEOUTS,0,NULL) +# define SSL_CTX_sess_cache_full(ctx) \ + SSL_CTX_ctrl(ctx,SSL_CTRL_SESS_CACHE_FULL,0,NULL) + +void SSL_CTX_sess_set_new_cb(SSL_CTX *ctx, + int (*new_session_cb) (struct ssl_st *ssl, + SSL_SESSION *sess)); +int (*SSL_CTX_sess_get_new_cb(SSL_CTX *ctx)) (struct ssl_st *ssl, + SSL_SESSION *sess); +void SSL_CTX_sess_set_remove_cb(SSL_CTX *ctx, + void (*remove_session_cb) (struct ssl_ctx_st + *ctx, + SSL_SESSION *sess)); +void (*SSL_CTX_sess_get_remove_cb(SSL_CTX *ctx)) (struct ssl_ctx_st *ctx, + SSL_SESSION *sess); +void SSL_CTX_sess_set_get_cb(SSL_CTX *ctx, + SSL_SESSION *(*get_session_cb) (struct ssl_st + *ssl, + const unsigned char + *data, int len, + int *copy)); +SSL_SESSION *(*SSL_CTX_sess_get_get_cb(SSL_CTX *ctx)) (struct ssl_st *ssl, + const unsigned char *data, + int len, int *copy); +void SSL_CTX_set_info_callback(SSL_CTX *ctx, + void (*cb) (const SSL *ssl, int type, int val)); +void (*SSL_CTX_get_info_callback(SSL_CTX *ctx)) (const SSL *ssl, int type, + int val); +void SSL_CTX_set_client_cert_cb(SSL_CTX *ctx, + int (*client_cert_cb) (SSL *ssl, X509 **x509, + EVP_PKEY **pkey)); +int (*SSL_CTX_get_client_cert_cb(SSL_CTX *ctx)) (SSL *ssl, X509 **x509, + EVP_PKEY **pkey); +# ifndef OPENSSL_NO_ENGINE +__owur int SSL_CTX_set_client_cert_engine(SSL_CTX *ctx, ENGINE *e); +# endif +void SSL_CTX_set_cookie_generate_cb(SSL_CTX *ctx, + int (*app_gen_cookie_cb) (SSL *ssl, + unsigned char + *cookie, + unsigned int + *cookie_len)); +void SSL_CTX_set_cookie_verify_cb(SSL_CTX *ctx, + int (*app_verify_cookie_cb) (SSL *ssl, + const unsigned + char *cookie, + unsigned int + cookie_len)); + +void SSL_CTX_set_stateless_cookie_generate_cb( + SSL_CTX *ctx, + int (*gen_stateless_cookie_cb) (SSL *ssl, + unsigned char *cookie, + size_t *cookie_len)); +void SSL_CTX_set_stateless_cookie_verify_cb( + SSL_CTX *ctx, + int (*verify_stateless_cookie_cb) (SSL *ssl, + const unsigned char *cookie, + size_t cookie_len)); +# ifndef OPENSSL_NO_NEXTPROTONEG + +typedef int (*SSL_CTX_npn_advertised_cb_func)(SSL *ssl, + const unsigned char **out, + unsigned int *outlen, + void *arg); +void SSL_CTX_set_next_protos_advertised_cb(SSL_CTX *s, + SSL_CTX_npn_advertised_cb_func cb, + void *arg); +# define SSL_CTX_set_npn_advertised_cb SSL_CTX_set_next_protos_advertised_cb + +typedef int (*SSL_CTX_npn_select_cb_func)(SSL *s, + unsigned char **out, + unsigned char *outlen, + const unsigned char *in, + unsigned int inlen, + void *arg); +void SSL_CTX_set_next_proto_select_cb(SSL_CTX *s, + SSL_CTX_npn_select_cb_func cb, + void *arg); +# define SSL_CTX_set_npn_select_cb SSL_CTX_set_next_proto_select_cb + +void SSL_get0_next_proto_negotiated(const SSL *s, const unsigned char **data, + unsigned *len); +# define SSL_get0_npn_negotiated SSL_get0_next_proto_negotiated +# endif + +__owur int SSL_select_next_proto(unsigned char **out, unsigned char *outlen, + const unsigned char *in, unsigned int inlen, + const unsigned char *client, + unsigned int client_len); + +# define OPENSSL_NPN_UNSUPPORTED 0 +# define OPENSSL_NPN_NEGOTIATED 1 +# define OPENSSL_NPN_NO_OVERLAP 2 + +__owur int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const unsigned char *protos, + unsigned int protos_len); +__owur int SSL_set_alpn_protos(SSL *ssl, const unsigned char *protos, + unsigned int protos_len); +typedef int (*SSL_CTX_alpn_select_cb_func)(SSL *ssl, + const unsigned char **out, + unsigned char *outlen, + const unsigned char *in, + unsigned int inlen, + void *arg); +void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx, + SSL_CTX_alpn_select_cb_func cb, + void *arg); +void SSL_get0_alpn_selected(const SSL *ssl, const unsigned char **data, + unsigned int *len); + +# ifndef OPENSSL_NO_PSK +/* + * the maximum length of the buffer given to callbacks containing the + * resulting identity/psk + */ +# define PSK_MAX_IDENTITY_LEN 128 +# define PSK_MAX_PSK_LEN 256 +typedef unsigned int (*SSL_psk_client_cb_func)(SSL *ssl, + const char *hint, + char *identity, + unsigned int max_identity_len, + unsigned char *psk, + unsigned int max_psk_len); +void SSL_CTX_set_psk_client_callback(SSL_CTX *ctx, SSL_psk_client_cb_func cb); +void SSL_set_psk_client_callback(SSL *ssl, SSL_psk_client_cb_func cb); + +typedef unsigned int (*SSL_psk_server_cb_func)(SSL *ssl, + const char *identity, + unsigned char *psk, + unsigned int max_psk_len); +void SSL_CTX_set_psk_server_callback(SSL_CTX *ctx, SSL_psk_server_cb_func cb); +void SSL_set_psk_server_callback(SSL *ssl, SSL_psk_server_cb_func cb); + +__owur int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint); +__owur int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint); +const char *SSL_get_psk_identity_hint(const SSL *s); +const char *SSL_get_psk_identity(const SSL *s); +# endif + +typedef int (*SSL_psk_find_session_cb_func)(SSL *ssl, + const unsigned char *identity, + size_t identity_len, + SSL_SESSION **sess); +typedef int (*SSL_psk_use_session_cb_func)(SSL *ssl, const EVP_MD *md, + const unsigned char **id, + size_t *idlen, + SSL_SESSION **sess); + +void SSL_set_psk_find_session_callback(SSL *s, SSL_psk_find_session_cb_func cb); +void SSL_CTX_set_psk_find_session_callback(SSL_CTX *ctx, + SSL_psk_find_session_cb_func cb); +void SSL_set_psk_use_session_callback(SSL *s, SSL_psk_use_session_cb_func cb); +void SSL_CTX_set_psk_use_session_callback(SSL_CTX *ctx, + SSL_psk_use_session_cb_func cb); + +/* Register callbacks to handle custom TLS Extensions for client or server. */ + +__owur int SSL_CTX_has_client_custom_ext(const SSL_CTX *ctx, + unsigned int ext_type); + +__owur int SSL_CTX_add_client_custom_ext(SSL_CTX *ctx, + unsigned int ext_type, + custom_ext_add_cb add_cb, + custom_ext_free_cb free_cb, + void *add_arg, + custom_ext_parse_cb parse_cb, + void *parse_arg); + +__owur int SSL_CTX_add_server_custom_ext(SSL_CTX *ctx, + unsigned int ext_type, + custom_ext_add_cb add_cb, + custom_ext_free_cb free_cb, + void *add_arg, + custom_ext_parse_cb parse_cb, + void *parse_arg); + +__owur int SSL_CTX_add_custom_ext(SSL_CTX *ctx, unsigned int ext_type, + unsigned int context, + SSL_custom_ext_add_cb_ex add_cb, + SSL_custom_ext_free_cb_ex free_cb, + void *add_arg, + SSL_custom_ext_parse_cb_ex parse_cb, + void *parse_arg); + +__owur int SSL_extension_supported(unsigned int ext_type); + +# define SSL_NOTHING 1 +# define SSL_WRITING 2 +# define SSL_READING 3 +# define SSL_X509_LOOKUP 4 +# define SSL_ASYNC_PAUSED 5 +# define SSL_ASYNC_NO_JOBS 6 +# define SSL_CLIENT_HELLO_CB 7 + +/* These will only be used when doing non-blocking IO */ +# define SSL_want_nothing(s) (SSL_want(s) == SSL_NOTHING) +# define SSL_want_read(s) (SSL_want(s) == SSL_READING) +# define SSL_want_write(s) (SSL_want(s) == SSL_WRITING) +# define SSL_want_x509_lookup(s) (SSL_want(s) == SSL_X509_LOOKUP) +# define SSL_want_async(s) (SSL_want(s) == SSL_ASYNC_PAUSED) +# define SSL_want_async_job(s) (SSL_want(s) == SSL_ASYNC_NO_JOBS) +# define SSL_want_client_hello_cb(s) (SSL_want(s) == SSL_CLIENT_HELLO_CB) + +# define SSL_MAC_FLAG_READ_MAC_STREAM 1 +# define SSL_MAC_FLAG_WRITE_MAC_STREAM 2 + +/* + * A callback for logging out TLS key material. This callback should log out + * |line| followed by a newline. + */ +typedef void (*SSL_CTX_keylog_cb_func)(const SSL *ssl, const char *line); + +/* + * SSL_CTX_set_keylog_callback configures a callback to log key material. This + * is intended for debugging use with tools like Wireshark. The cb function + * should log line followed by a newline. + */ +void SSL_CTX_set_keylog_callback(SSL_CTX *ctx, SSL_CTX_keylog_cb_func cb); + +/* + * SSL_CTX_get_keylog_callback returns the callback configured by + * SSL_CTX_set_keylog_callback. + */ +SSL_CTX_keylog_cb_func SSL_CTX_get_keylog_callback(const SSL_CTX *ctx); + +int SSL_CTX_set_max_early_data(SSL_CTX *ctx, uint32_t max_early_data); +uint32_t SSL_CTX_get_max_early_data(const SSL_CTX *ctx); +int SSL_set_max_early_data(SSL *s, uint32_t max_early_data); +uint32_t SSL_get_max_early_data(const SSL *s); +int SSL_CTX_set_recv_max_early_data(SSL_CTX *ctx, uint32_t recv_max_early_data); +uint32_t SSL_CTX_get_recv_max_early_data(const SSL_CTX *ctx); +int SSL_set_recv_max_early_data(SSL *s, uint32_t recv_max_early_data); +uint32_t SSL_get_recv_max_early_data(const SSL *s); + +#ifdef __cplusplus +} +#endif + +# include +# include +# include /* This is mostly sslv3 with a few tweaks */ +# include /* Datagram TLS */ +# include /* Support for the use_srtp extension */ + +#ifdef __cplusplus +extern "C" { +#endif + +/* + * These need to be after the above set of includes due to a compiler bug + * in VisualStudio 2015 + */ +DEFINE_STACK_OF_CONST(SSL_CIPHER) +DEFINE_STACK_OF(SSL_COMP) + +/* compatibility */ +# define SSL_set_app_data(s,arg) (SSL_set_ex_data(s,0,(char *)(arg))) +# define SSL_get_app_data(s) (SSL_get_ex_data(s,0)) +# define SSL_SESSION_set_app_data(s,a) (SSL_SESSION_set_ex_data(s,0, \ + (char *)(a))) +# define SSL_SESSION_get_app_data(s) (SSL_SESSION_get_ex_data(s,0)) +# define SSL_CTX_get_app_data(ctx) (SSL_CTX_get_ex_data(ctx,0)) +# define SSL_CTX_set_app_data(ctx,arg) (SSL_CTX_set_ex_data(ctx,0, \ + (char *)(arg))) +DEPRECATEDIN_1_1_0(void SSL_set_debug(SSL *s, int debug)) + +/* TLSv1.3 KeyUpdate message types */ +/* -1 used so that this is an invalid value for the on-the-wire protocol */ +#define SSL_KEY_UPDATE_NONE -1 +/* Values as defined for the on-the-wire protocol */ +#define SSL_KEY_UPDATE_NOT_REQUESTED 0 +#define SSL_KEY_UPDATE_REQUESTED 1 + +/* + * The valid handshake states (one for each type message sent and one for each + * type of message received). There are also two "special" states: + * TLS = TLS or DTLS state + * DTLS = DTLS specific state + * CR/SR = Client Read/Server Read + * CW/SW = Client Write/Server Write + * + * The "special" states are: + * TLS_ST_BEFORE = No handshake has been initiated yet + * TLS_ST_OK = A handshake has been successfully completed + */ +typedef enum { + TLS_ST_BEFORE, + TLS_ST_OK, + DTLS_ST_CR_HELLO_VERIFY_REQUEST, + TLS_ST_CR_SRVR_HELLO, + TLS_ST_CR_CERT, + TLS_ST_CR_CERT_STATUS, + TLS_ST_CR_KEY_EXCH, + TLS_ST_CR_CERT_REQ, + TLS_ST_CR_SRVR_DONE, + TLS_ST_CR_SESSION_TICKET, + TLS_ST_CR_CHANGE, + TLS_ST_CR_FINISHED, + TLS_ST_CW_CLNT_HELLO, + TLS_ST_CW_CERT, + TLS_ST_CW_KEY_EXCH, + TLS_ST_CW_CERT_VRFY, + TLS_ST_CW_CHANGE, + TLS_ST_CW_NEXT_PROTO, + TLS_ST_CW_FINISHED, + TLS_ST_SW_HELLO_REQ, + TLS_ST_SR_CLNT_HELLO, + DTLS_ST_SW_HELLO_VERIFY_REQUEST, + TLS_ST_SW_SRVR_HELLO, + TLS_ST_SW_CERT, + TLS_ST_SW_KEY_EXCH, + TLS_ST_SW_CERT_REQ, + TLS_ST_SW_SRVR_DONE, + TLS_ST_SR_CERT, + TLS_ST_SR_KEY_EXCH, + TLS_ST_SR_CERT_VRFY, + TLS_ST_SR_NEXT_PROTO, + TLS_ST_SR_CHANGE, + TLS_ST_SR_FINISHED, + TLS_ST_SW_SESSION_TICKET, + TLS_ST_SW_CERT_STATUS, + TLS_ST_SW_CHANGE, + TLS_ST_SW_FINISHED, + TLS_ST_SW_ENCRYPTED_EXTENSIONS, + TLS_ST_CR_ENCRYPTED_EXTENSIONS, + TLS_ST_CR_CERT_VRFY, + TLS_ST_SW_CERT_VRFY, + TLS_ST_CR_HELLO_REQ, + TLS_ST_SW_KEY_UPDATE, + TLS_ST_CW_KEY_UPDATE, + TLS_ST_SR_KEY_UPDATE, + TLS_ST_CR_KEY_UPDATE, + TLS_ST_EARLY_DATA, + TLS_ST_PENDING_EARLY_DATA_END, + TLS_ST_CW_END_OF_EARLY_DATA, + TLS_ST_SR_END_OF_EARLY_DATA +} OSSL_HANDSHAKE_STATE; + +/* + * Most of the following state values are no longer used and are defined to be + * the closest equivalent value in the current state machine code. Not all + * defines have an equivalent and are set to a dummy value (-1). SSL_ST_CONNECT + * and SSL_ST_ACCEPT are still in use in the definition of SSL_CB_ACCEPT_LOOP, + * SSL_CB_ACCEPT_EXIT, SSL_CB_CONNECT_LOOP and SSL_CB_CONNECT_EXIT. + */ + +# define SSL_ST_CONNECT 0x1000 +# define SSL_ST_ACCEPT 0x2000 + +# define SSL_ST_MASK 0x0FFF + +# define SSL_CB_LOOP 0x01 +# define SSL_CB_EXIT 0x02 +# define SSL_CB_READ 0x04 +# define SSL_CB_WRITE 0x08 +# define SSL_CB_ALERT 0x4000/* used in callback */ +# define SSL_CB_READ_ALERT (SSL_CB_ALERT|SSL_CB_READ) +# define SSL_CB_WRITE_ALERT (SSL_CB_ALERT|SSL_CB_WRITE) +# define SSL_CB_ACCEPT_LOOP (SSL_ST_ACCEPT|SSL_CB_LOOP) +# define SSL_CB_ACCEPT_EXIT (SSL_ST_ACCEPT|SSL_CB_EXIT) +# define SSL_CB_CONNECT_LOOP (SSL_ST_CONNECT|SSL_CB_LOOP) +# define SSL_CB_CONNECT_EXIT (SSL_ST_CONNECT|SSL_CB_EXIT) +# define SSL_CB_HANDSHAKE_START 0x10 +# define SSL_CB_HANDSHAKE_DONE 0x20 + +/* Is the SSL_connection established? */ +# define SSL_in_connect_init(a) (SSL_in_init(a) && !SSL_is_server(a)) +# define SSL_in_accept_init(a) (SSL_in_init(a) && SSL_is_server(a)) +int SSL_in_init(const SSL *s); +int SSL_in_before(const SSL *s); +int SSL_is_init_finished(const SSL *s); + +/* + * The following 3 states are kept in ssl->rlayer.rstate when reads fail, you + * should not need these + */ +# define SSL_ST_READ_HEADER 0xF0 +# define SSL_ST_READ_BODY 0xF1 +# define SSL_ST_READ_DONE 0xF2 + +/*- + * Obtain latest Finished message + * -- that we sent (SSL_get_finished) + * -- that we expected from peer (SSL_get_peer_finished). + * Returns length (0 == no Finished so far), copies up to 'count' bytes. + */ +size_t SSL_get_finished(const SSL *s, void *buf, size_t count); +size_t SSL_get_peer_finished(const SSL *s, void *buf, size_t count); + +/* + * use either SSL_VERIFY_NONE or SSL_VERIFY_PEER, the last 3 options are + * 'ored' with SSL_VERIFY_PEER if they are desired + */ +# define SSL_VERIFY_NONE 0x00 +# define SSL_VERIFY_PEER 0x01 +# define SSL_VERIFY_FAIL_IF_NO_PEER_CERT 0x02 +# define SSL_VERIFY_CLIENT_ONCE 0x04 +# define SSL_VERIFY_POST_HANDSHAKE 0x08 + +# if OPENSSL_API_COMPAT < 0x10100000L +# define OpenSSL_add_ssl_algorithms() SSL_library_init() +# define SSLeay_add_ssl_algorithms() SSL_library_init() +# endif + +/* More backward compatibility */ +# define SSL_get_cipher(s) \ + SSL_CIPHER_get_name(SSL_get_current_cipher(s)) +# define SSL_get_cipher_bits(s,np) \ + SSL_CIPHER_get_bits(SSL_get_current_cipher(s),np) +# define SSL_get_cipher_version(s) \ + SSL_CIPHER_get_version(SSL_get_current_cipher(s)) +# define SSL_get_cipher_name(s) \ + SSL_CIPHER_get_name(SSL_get_current_cipher(s)) +# define SSL_get_time(a) SSL_SESSION_get_time(a) +# define SSL_set_time(a,b) SSL_SESSION_set_time((a),(b)) +# define SSL_get_timeout(a) SSL_SESSION_get_timeout(a) +# define SSL_set_timeout(a,b) SSL_SESSION_set_timeout((a),(b)) + +# define d2i_SSL_SESSION_bio(bp,s_id) ASN1_d2i_bio_of(SSL_SESSION,SSL_SESSION_new,d2i_SSL_SESSION,bp,s_id) +# define i2d_SSL_SESSION_bio(bp,s_id) ASN1_i2d_bio_of(SSL_SESSION,i2d_SSL_SESSION,bp,s_id) + +DECLARE_PEM_rw(SSL_SESSION, SSL_SESSION) +# define SSL_AD_REASON_OFFSET 1000/* offset to get SSL_R_... value + * from SSL_AD_... */ +/* These alert types are for SSLv3 and TLSv1 */ +# define SSL_AD_CLOSE_NOTIFY SSL3_AD_CLOSE_NOTIFY +/* fatal */ +# define SSL_AD_UNEXPECTED_MESSAGE SSL3_AD_UNEXPECTED_MESSAGE +/* fatal */ +# define SSL_AD_BAD_RECORD_MAC SSL3_AD_BAD_RECORD_MAC +# define SSL_AD_DECRYPTION_FAILED TLS1_AD_DECRYPTION_FAILED +# define SSL_AD_RECORD_OVERFLOW TLS1_AD_RECORD_OVERFLOW +/* fatal */ +# define SSL_AD_DECOMPRESSION_FAILURE SSL3_AD_DECOMPRESSION_FAILURE +/* fatal */ +# define SSL_AD_HANDSHAKE_FAILURE SSL3_AD_HANDSHAKE_FAILURE +/* Not for TLS */ +# define SSL_AD_NO_CERTIFICATE SSL3_AD_NO_CERTIFICATE +# define SSL_AD_BAD_CERTIFICATE SSL3_AD_BAD_CERTIFICATE +# define SSL_AD_UNSUPPORTED_CERTIFICATE SSL3_AD_UNSUPPORTED_CERTIFICATE +# define SSL_AD_CERTIFICATE_REVOKED SSL3_AD_CERTIFICATE_REVOKED +# define SSL_AD_CERTIFICATE_EXPIRED SSL3_AD_CERTIFICATE_EXPIRED +# define SSL_AD_CERTIFICATE_UNKNOWN SSL3_AD_CERTIFICATE_UNKNOWN +/* fatal */ +# define SSL_AD_ILLEGAL_PARAMETER SSL3_AD_ILLEGAL_PARAMETER +/* fatal */ +# define SSL_AD_UNKNOWN_CA TLS1_AD_UNKNOWN_CA +/* fatal */ +# define SSL_AD_ACCESS_DENIED TLS1_AD_ACCESS_DENIED +/* fatal */ +# define SSL_AD_DECODE_ERROR TLS1_AD_DECODE_ERROR +# define SSL_AD_DECRYPT_ERROR TLS1_AD_DECRYPT_ERROR +/* fatal */ +# define SSL_AD_EXPORT_RESTRICTION TLS1_AD_EXPORT_RESTRICTION +/* fatal */ +# define SSL_AD_PROTOCOL_VERSION TLS1_AD_PROTOCOL_VERSION +/* fatal */ +# define SSL_AD_INSUFFICIENT_SECURITY TLS1_AD_INSUFFICIENT_SECURITY +/* fatal */ +# define SSL_AD_INTERNAL_ERROR TLS1_AD_INTERNAL_ERROR +# define SSL_AD_USER_CANCELLED TLS1_AD_USER_CANCELLED +# define SSL_AD_NO_RENEGOTIATION TLS1_AD_NO_RENEGOTIATION +# define SSL_AD_MISSING_EXTENSION TLS13_AD_MISSING_EXTENSION +# define SSL_AD_CERTIFICATE_REQUIRED TLS13_AD_CERTIFICATE_REQUIRED +# define SSL_AD_UNSUPPORTED_EXTENSION TLS1_AD_UNSUPPORTED_EXTENSION +# define SSL_AD_CERTIFICATE_UNOBTAINABLE TLS1_AD_CERTIFICATE_UNOBTAINABLE +# define SSL_AD_UNRECOGNIZED_NAME TLS1_AD_UNRECOGNIZED_NAME +# define SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE TLS1_AD_BAD_CERTIFICATE_STATUS_RESPONSE +# define SSL_AD_BAD_CERTIFICATE_HASH_VALUE TLS1_AD_BAD_CERTIFICATE_HASH_VALUE +/* fatal */ +# define SSL_AD_UNKNOWN_PSK_IDENTITY TLS1_AD_UNKNOWN_PSK_IDENTITY +/* fatal */ +# define SSL_AD_INAPPROPRIATE_FALLBACK TLS1_AD_INAPPROPRIATE_FALLBACK +# define SSL_AD_NO_APPLICATION_PROTOCOL TLS1_AD_NO_APPLICATION_PROTOCOL +# define SSL_ERROR_NONE 0 +# define SSL_ERROR_SSL 1 +# define SSL_ERROR_WANT_READ 2 +# define SSL_ERROR_WANT_WRITE 3 +# define SSL_ERROR_WANT_X509_LOOKUP 4 +# define SSL_ERROR_SYSCALL 5/* look at error stack/return + * value/errno */ +# define SSL_ERROR_ZERO_RETURN 6 +# define SSL_ERROR_WANT_CONNECT 7 +# define SSL_ERROR_WANT_ACCEPT 8 +# define SSL_ERROR_WANT_ASYNC 9 +# define SSL_ERROR_WANT_ASYNC_JOB 10 +# define SSL_ERROR_WANT_CLIENT_HELLO_CB 11 +# define SSL_CTRL_SET_TMP_DH 3 +# define SSL_CTRL_SET_TMP_ECDH 4 +# define SSL_CTRL_SET_TMP_DH_CB 6 +# define SSL_CTRL_GET_CLIENT_CERT_REQUEST 9 +# define SSL_CTRL_GET_NUM_RENEGOTIATIONS 10 +# define SSL_CTRL_CLEAR_NUM_RENEGOTIATIONS 11 +# define SSL_CTRL_GET_TOTAL_RENEGOTIATIONS 12 +# define SSL_CTRL_GET_FLAGS 13 +# define SSL_CTRL_EXTRA_CHAIN_CERT 14 +# define SSL_CTRL_SET_MSG_CALLBACK 15 +# define SSL_CTRL_SET_MSG_CALLBACK_ARG 16 +/* only applies to datagram connections */ +# define SSL_CTRL_SET_MTU 17 +/* Stats */ +# define SSL_CTRL_SESS_NUMBER 20 +# define SSL_CTRL_SESS_CONNECT 21 +# define SSL_CTRL_SESS_CONNECT_GOOD 22 +# define SSL_CTRL_SESS_CONNECT_RENEGOTIATE 23 +# define SSL_CTRL_SESS_ACCEPT 24 +# define SSL_CTRL_SESS_ACCEPT_GOOD 25 +# define SSL_CTRL_SESS_ACCEPT_RENEGOTIATE 26 +# define SSL_CTRL_SESS_HIT 27 +# define SSL_CTRL_SESS_CB_HIT 28 +# define SSL_CTRL_SESS_MISSES 29 +# define SSL_CTRL_SESS_TIMEOUTS 30 +# define SSL_CTRL_SESS_CACHE_FULL 31 +# define SSL_CTRL_MODE 33 +# define SSL_CTRL_GET_READ_AHEAD 40 +# define SSL_CTRL_SET_READ_AHEAD 41 +# define SSL_CTRL_SET_SESS_CACHE_SIZE 42 +# define SSL_CTRL_GET_SESS_CACHE_SIZE 43 +# define SSL_CTRL_SET_SESS_CACHE_MODE 44 +# define SSL_CTRL_GET_SESS_CACHE_MODE 45 +# define SSL_CTRL_GET_MAX_CERT_LIST 50 +# define SSL_CTRL_SET_MAX_CERT_LIST 51 +# define SSL_CTRL_SET_MAX_SEND_FRAGMENT 52 +/* see tls1.h for macros based on these */ +# define SSL_CTRL_SET_TLSEXT_SERVERNAME_CB 53 +# define SSL_CTRL_SET_TLSEXT_SERVERNAME_ARG 54 +# define SSL_CTRL_SET_TLSEXT_HOSTNAME 55 +# define SSL_CTRL_SET_TLSEXT_DEBUG_CB 56 +# define SSL_CTRL_SET_TLSEXT_DEBUG_ARG 57 +# define SSL_CTRL_GET_TLSEXT_TICKET_KEYS 58 +# define SSL_CTRL_SET_TLSEXT_TICKET_KEYS 59 +/*# define SSL_CTRL_SET_TLSEXT_OPAQUE_PRF_INPUT 60 */ +/*# define SSL_CTRL_SET_TLSEXT_OPAQUE_PRF_INPUT_CB 61 */ +/*# define SSL_CTRL_SET_TLSEXT_OPAQUE_PRF_INPUT_CB_ARG 62 */ +# define SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB 63 +# define SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB_ARG 64 +# define SSL_CTRL_SET_TLSEXT_STATUS_REQ_TYPE 65 +# define SSL_CTRL_GET_TLSEXT_STATUS_REQ_EXTS 66 +# define SSL_CTRL_SET_TLSEXT_STATUS_REQ_EXTS 67 +# define SSL_CTRL_GET_TLSEXT_STATUS_REQ_IDS 68 +# define SSL_CTRL_SET_TLSEXT_STATUS_REQ_IDS 69 +# define SSL_CTRL_GET_TLSEXT_STATUS_REQ_OCSP_RESP 70 +# define SSL_CTRL_SET_TLSEXT_STATUS_REQ_OCSP_RESP 71 +# define SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB 72 +# define SSL_CTRL_SET_TLS_EXT_SRP_USERNAME_CB 75 +# define SSL_CTRL_SET_SRP_VERIFY_PARAM_CB 76 +# define SSL_CTRL_SET_SRP_GIVE_CLIENT_PWD_CB 77 +# define SSL_CTRL_SET_SRP_ARG 78 +# define SSL_CTRL_SET_TLS_EXT_SRP_USERNAME 79 +# define SSL_CTRL_SET_TLS_EXT_SRP_STRENGTH 80 +# define SSL_CTRL_SET_TLS_EXT_SRP_PASSWORD 81 +# ifndef OPENSSL_NO_HEARTBEATS +# define SSL_CTRL_DTLS_EXT_SEND_HEARTBEAT 85 +# define SSL_CTRL_GET_DTLS_EXT_HEARTBEAT_PENDING 86 +# define SSL_CTRL_SET_DTLS_EXT_HEARTBEAT_NO_REQUESTS 87 +# endif +# define DTLS_CTRL_GET_TIMEOUT 73 +# define DTLS_CTRL_HANDLE_TIMEOUT 74 +# define SSL_CTRL_GET_RI_SUPPORT 76 +# define SSL_CTRL_CLEAR_MODE 78 +# define SSL_CTRL_SET_NOT_RESUMABLE_SESS_CB 79 +# define SSL_CTRL_GET_EXTRA_CHAIN_CERTS 82 +# define SSL_CTRL_CLEAR_EXTRA_CHAIN_CERTS 83 +# define SSL_CTRL_CHAIN 88 +# define SSL_CTRL_CHAIN_CERT 89 +# define SSL_CTRL_GET_GROUPS 90 +# define SSL_CTRL_SET_GROUPS 91 +# define SSL_CTRL_SET_GROUPS_LIST 92 +# define SSL_CTRL_GET_SHARED_GROUP 93 +# define SSL_CTRL_SET_SIGALGS 97 +# define SSL_CTRL_SET_SIGALGS_LIST 98 +# define SSL_CTRL_CERT_FLAGS 99 +# define SSL_CTRL_CLEAR_CERT_FLAGS 100 +# define SSL_CTRL_SET_CLIENT_SIGALGS 101 +# define SSL_CTRL_SET_CLIENT_SIGALGS_LIST 102 +# define SSL_CTRL_GET_CLIENT_CERT_TYPES 103 +# define SSL_CTRL_SET_CLIENT_CERT_TYPES 104 +# define SSL_CTRL_BUILD_CERT_CHAIN 105 +# define SSL_CTRL_SET_VERIFY_CERT_STORE 106 +# define SSL_CTRL_SET_CHAIN_CERT_STORE 107 +# define SSL_CTRL_GET_PEER_SIGNATURE_NID 108 +# define SSL_CTRL_GET_PEER_TMP_KEY 109 +# define SSL_CTRL_GET_RAW_CIPHERLIST 110 +# define SSL_CTRL_GET_EC_POINT_FORMATS 111 +# define SSL_CTRL_GET_CHAIN_CERTS 115 +# define SSL_CTRL_SELECT_CURRENT_CERT 116 +# define SSL_CTRL_SET_CURRENT_CERT 117 +# define SSL_CTRL_SET_DH_AUTO 118 +# define DTLS_CTRL_SET_LINK_MTU 120 +# define DTLS_CTRL_GET_LINK_MIN_MTU 121 +# define SSL_CTRL_GET_EXTMS_SUPPORT 122 +# define SSL_CTRL_SET_MIN_PROTO_VERSION 123 +# define SSL_CTRL_SET_MAX_PROTO_VERSION 124 +# define SSL_CTRL_SET_SPLIT_SEND_FRAGMENT 125 +# define SSL_CTRL_SET_MAX_PIPELINES 126 +# define SSL_CTRL_GET_TLSEXT_STATUS_REQ_TYPE 127 +# define SSL_CTRL_GET_TLSEXT_STATUS_REQ_CB 128 +# define SSL_CTRL_GET_TLSEXT_STATUS_REQ_CB_ARG 129 +# define SSL_CTRL_GET_MIN_PROTO_VERSION 130 +# define SSL_CTRL_GET_MAX_PROTO_VERSION 131 +# define SSL_CTRL_GET_SIGNATURE_NID 132 +# define SSL_CTRL_GET_TMP_KEY 133 +# define SSL_CERT_SET_FIRST 1 +# define SSL_CERT_SET_NEXT 2 +# define SSL_CERT_SET_SERVER 3 +# define DTLSv1_get_timeout(ssl, arg) \ + SSL_ctrl(ssl,DTLS_CTRL_GET_TIMEOUT,0, (void *)(arg)) +# define DTLSv1_handle_timeout(ssl) \ + SSL_ctrl(ssl,DTLS_CTRL_HANDLE_TIMEOUT,0, NULL) +# define SSL_num_renegotiations(ssl) \ + SSL_ctrl((ssl),SSL_CTRL_GET_NUM_RENEGOTIATIONS,0,NULL) +# define SSL_clear_num_renegotiations(ssl) \ + SSL_ctrl((ssl),SSL_CTRL_CLEAR_NUM_RENEGOTIATIONS,0,NULL) +# define SSL_total_renegotiations(ssl) \ + SSL_ctrl((ssl),SSL_CTRL_GET_TOTAL_RENEGOTIATIONS,0,NULL) +# define SSL_CTX_set_tmp_dh(ctx,dh) \ + SSL_CTX_ctrl(ctx,SSL_CTRL_SET_TMP_DH,0,(char *)(dh)) +# define SSL_CTX_set_tmp_ecdh(ctx,ecdh) \ + SSL_CTX_ctrl(ctx,SSL_CTRL_SET_TMP_ECDH,0,(char *)(ecdh)) +# define SSL_CTX_set_dh_auto(ctx, onoff) \ + SSL_CTX_ctrl(ctx,SSL_CTRL_SET_DH_AUTO,onoff,NULL) +# define SSL_set_dh_auto(s, onoff) \ + SSL_ctrl(s,SSL_CTRL_SET_DH_AUTO,onoff,NULL) +# define SSL_set_tmp_dh(ssl,dh) \ + SSL_ctrl(ssl,SSL_CTRL_SET_TMP_DH,0,(char *)(dh)) +# define SSL_set_tmp_ecdh(ssl,ecdh) \ + SSL_ctrl(ssl,SSL_CTRL_SET_TMP_ECDH,0,(char *)(ecdh)) +# define SSL_CTX_add_extra_chain_cert(ctx,x509) \ + SSL_CTX_ctrl(ctx,SSL_CTRL_EXTRA_CHAIN_CERT,0,(char *)(x509)) +# define SSL_CTX_get_extra_chain_certs(ctx,px509) \ + SSL_CTX_ctrl(ctx,SSL_CTRL_GET_EXTRA_CHAIN_CERTS,0,px509) +# define SSL_CTX_get_extra_chain_certs_only(ctx,px509) \ + SSL_CTX_ctrl(ctx,SSL_CTRL_GET_EXTRA_CHAIN_CERTS,1,px509) +# define SSL_CTX_clear_extra_chain_certs(ctx) \ + SSL_CTX_ctrl(ctx,SSL_CTRL_CLEAR_EXTRA_CHAIN_CERTS,0,NULL) +# define SSL_CTX_set0_chain(ctx,sk) \ + SSL_CTX_ctrl(ctx,SSL_CTRL_CHAIN,0,(char *)(sk)) +# define SSL_CTX_set1_chain(ctx,sk) \ + SSL_CTX_ctrl(ctx,SSL_CTRL_CHAIN,1,(char *)(sk)) +# define SSL_CTX_add0_chain_cert(ctx,x509) \ + SSL_CTX_ctrl(ctx,SSL_CTRL_CHAIN_CERT,0,(char *)(x509)) +# define SSL_CTX_add1_chain_cert(ctx,x509) \ + SSL_CTX_ctrl(ctx,SSL_CTRL_CHAIN_CERT,1,(char *)(x509)) +# define SSL_CTX_get0_chain_certs(ctx,px509) \ + SSL_CTX_ctrl(ctx,SSL_CTRL_GET_CHAIN_CERTS,0,px509) +# define SSL_CTX_clear_chain_certs(ctx) \ + SSL_CTX_set0_chain(ctx,NULL) +# define SSL_CTX_build_cert_chain(ctx, flags) \ + SSL_CTX_ctrl(ctx,SSL_CTRL_BUILD_CERT_CHAIN, flags, NULL) +# define SSL_CTX_select_current_cert(ctx,x509) \ + SSL_CTX_ctrl(ctx,SSL_CTRL_SELECT_CURRENT_CERT,0,(char *)(x509)) +# define SSL_CTX_set_current_cert(ctx, op) \ + SSL_CTX_ctrl(ctx,SSL_CTRL_SET_CURRENT_CERT, op, NULL) +# define SSL_CTX_set0_verify_cert_store(ctx,st) \ + SSL_CTX_ctrl(ctx,SSL_CTRL_SET_VERIFY_CERT_STORE,0,(char *)(st)) +# define SSL_CTX_set1_verify_cert_store(ctx,st) \ + SSL_CTX_ctrl(ctx,SSL_CTRL_SET_VERIFY_CERT_STORE,1,(char *)(st)) +# define SSL_CTX_set0_chain_cert_store(ctx,st) \ + SSL_CTX_ctrl(ctx,SSL_CTRL_SET_CHAIN_CERT_STORE,0,(char *)(st)) +# define SSL_CTX_set1_chain_cert_store(ctx,st) \ + SSL_CTX_ctrl(ctx,SSL_CTRL_SET_CHAIN_CERT_STORE,1,(char *)(st)) +# define SSL_set0_chain(ctx,sk) \ + SSL_ctrl(ctx,SSL_CTRL_CHAIN,0,(char *)(sk)) +# define SSL_set1_chain(ctx,sk) \ + SSL_ctrl(ctx,SSL_CTRL_CHAIN,1,(char *)(sk)) +# define SSL_add0_chain_cert(ctx,x509) \ + SSL_ctrl(ctx,SSL_CTRL_CHAIN_CERT,0,(char *)(x509)) +# define SSL_add1_chain_cert(ctx,x509) \ + SSL_ctrl(ctx,SSL_CTRL_CHAIN_CERT,1,(char *)(x509)) +# define SSL_get0_chain_certs(ctx,px509) \ + SSL_ctrl(ctx,SSL_CTRL_GET_CHAIN_CERTS,0,px509) +# define SSL_clear_chain_certs(ctx) \ + SSL_set0_chain(ctx,NULL) +# define SSL_build_cert_chain(s, flags) \ + SSL_ctrl(s,SSL_CTRL_BUILD_CERT_CHAIN, flags, NULL) +# define SSL_select_current_cert(ctx,x509) \ + SSL_ctrl(ctx,SSL_CTRL_SELECT_CURRENT_CERT,0,(char *)(x509)) +# define SSL_set_current_cert(ctx,op) \ + SSL_ctrl(ctx,SSL_CTRL_SET_CURRENT_CERT, op, NULL) +# define SSL_set0_verify_cert_store(s,st) \ + SSL_ctrl(s,SSL_CTRL_SET_VERIFY_CERT_STORE,0,(char *)(st)) +# define SSL_set1_verify_cert_store(s,st) \ + SSL_ctrl(s,SSL_CTRL_SET_VERIFY_CERT_STORE,1,(char *)(st)) +# define SSL_set0_chain_cert_store(s,st) \ + SSL_ctrl(s,SSL_CTRL_SET_CHAIN_CERT_STORE,0,(char *)(st)) +# define SSL_set1_chain_cert_store(s,st) \ + SSL_ctrl(s,SSL_CTRL_SET_CHAIN_CERT_STORE,1,(char *)(st)) +# define SSL_get1_groups(ctx, s) \ + SSL_ctrl(ctx,SSL_CTRL_GET_GROUPS,0,(char *)(s)) +# define SSL_CTX_set1_groups(ctx, glist, glistlen) \ + SSL_CTX_ctrl(ctx,SSL_CTRL_SET_GROUPS,glistlen,(char *)(glist)) +# define SSL_CTX_set1_groups_list(ctx, s) \ + SSL_CTX_ctrl(ctx,SSL_CTRL_SET_GROUPS_LIST,0,(char *)(s)) +# define SSL_set1_groups(ctx, glist, glistlen) \ + SSL_ctrl(ctx,SSL_CTRL_SET_GROUPS,glistlen,(char *)(glist)) +# define SSL_set1_groups_list(ctx, s) \ + SSL_ctrl(ctx,SSL_CTRL_SET_GROUPS_LIST,0,(char *)(s)) +# define SSL_get_shared_group(s, n) \ + SSL_ctrl(s,SSL_CTRL_GET_SHARED_GROUP,n,NULL) +# define SSL_CTX_set1_sigalgs(ctx, slist, slistlen) \ + SSL_CTX_ctrl(ctx,SSL_CTRL_SET_SIGALGS,slistlen,(int *)(slist)) +# define SSL_CTX_set1_sigalgs_list(ctx, s) \ + SSL_CTX_ctrl(ctx,SSL_CTRL_SET_SIGALGS_LIST,0,(char *)(s)) +# define SSL_set1_sigalgs(ctx, slist, slistlen) \ + SSL_ctrl(ctx,SSL_CTRL_SET_SIGALGS,slistlen,(int *)(slist)) +# define SSL_set1_sigalgs_list(ctx, s) \ + SSL_ctrl(ctx,SSL_CTRL_SET_SIGALGS_LIST,0,(char *)(s)) +# define SSL_CTX_set1_client_sigalgs(ctx, slist, slistlen) \ + SSL_CTX_ctrl(ctx,SSL_CTRL_SET_CLIENT_SIGALGS,slistlen,(int *)(slist)) +# define SSL_CTX_set1_client_sigalgs_list(ctx, s) \ + SSL_CTX_ctrl(ctx,SSL_CTRL_SET_CLIENT_SIGALGS_LIST,0,(char *)(s)) +# define SSL_set1_client_sigalgs(ctx, slist, slistlen) \ + SSL_ctrl(ctx,SSL_CTRL_SET_CLIENT_SIGALGS,clistlen,(int *)(slist)) +# define SSL_set1_client_sigalgs_list(ctx, s) \ + SSL_ctrl(ctx,SSL_CTRL_SET_CLIENT_SIGALGS_LIST,0,(char *)(s)) +# define SSL_get0_certificate_types(s, clist) \ + SSL_ctrl(s, SSL_CTRL_GET_CLIENT_CERT_TYPES, 0, (char *)(clist)) +# define SSL_CTX_set1_client_certificate_types(ctx, clist, clistlen) \ + SSL_CTX_ctrl(ctx,SSL_CTRL_SET_CLIENT_CERT_TYPES,clistlen, \ + (char *)(clist)) +# define SSL_set1_client_certificate_types(s, clist, clistlen) \ + SSL_ctrl(s,SSL_CTRL_SET_CLIENT_CERT_TYPES,clistlen,(char *)(clist)) +# define SSL_get_signature_nid(s, pn) \ + SSL_ctrl(s,SSL_CTRL_GET_SIGNATURE_NID,0,pn) +# define SSL_get_peer_signature_nid(s, pn) \ + SSL_ctrl(s,SSL_CTRL_GET_PEER_SIGNATURE_NID,0,pn) +# define SSL_get_peer_tmp_key(s, pk) \ + SSL_ctrl(s,SSL_CTRL_GET_PEER_TMP_KEY,0,pk) +# define SSL_get_tmp_key(s, pk) \ + SSL_ctrl(s,SSL_CTRL_GET_TMP_KEY,0,pk) +# define SSL_get0_raw_cipherlist(s, plst) \ + SSL_ctrl(s,SSL_CTRL_GET_RAW_CIPHERLIST,0,plst) +# define SSL_get0_ec_point_formats(s, plst) \ + SSL_ctrl(s,SSL_CTRL_GET_EC_POINT_FORMATS,0,plst) +# define SSL_CTX_set_min_proto_version(ctx, version) \ + SSL_CTX_ctrl(ctx, SSL_CTRL_SET_MIN_PROTO_VERSION, version, NULL) +# define SSL_CTX_set_max_proto_version(ctx, version) \ + SSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION, version, NULL) +# define SSL_CTX_get_min_proto_version(ctx) \ + SSL_CTX_ctrl(ctx, SSL_CTRL_GET_MIN_PROTO_VERSION, 0, NULL) +# define SSL_CTX_get_max_proto_version(ctx) \ + SSL_CTX_ctrl(ctx, SSL_CTRL_GET_MAX_PROTO_VERSION, 0, NULL) +# define SSL_set_min_proto_version(s, version) \ + SSL_ctrl(s, SSL_CTRL_SET_MIN_PROTO_VERSION, version, NULL) +# define SSL_set_max_proto_version(s, version) \ + SSL_ctrl(s, SSL_CTRL_SET_MAX_PROTO_VERSION, version, NULL) +# define SSL_get_min_proto_version(s) \ + SSL_ctrl(s, SSL_CTRL_GET_MIN_PROTO_VERSION, 0, NULL) +# define SSL_get_max_proto_version(s) \ + SSL_ctrl(s, SSL_CTRL_GET_MAX_PROTO_VERSION, 0, NULL) + +/* Backwards compatibility, original 1.1.0 names */ +# define SSL_CTRL_GET_SERVER_TMP_KEY \ + SSL_CTRL_GET_PEER_TMP_KEY +# define SSL_get_server_tmp_key(s, pk) \ + SSL_get_peer_tmp_key(s, pk) + +/* + * The following symbol names are old and obsolete. They are kept + * for compatibility reasons only and should not be used anymore. + */ +# define SSL_CTRL_GET_CURVES SSL_CTRL_GET_GROUPS +# define SSL_CTRL_SET_CURVES SSL_CTRL_SET_GROUPS +# define SSL_CTRL_SET_CURVES_LIST SSL_CTRL_SET_GROUPS_LIST +# define SSL_CTRL_GET_SHARED_CURVE SSL_CTRL_GET_SHARED_GROUP + +# define SSL_get1_curves SSL_get1_groups +# define SSL_CTX_set1_curves SSL_CTX_set1_groups +# define SSL_CTX_set1_curves_list SSL_CTX_set1_groups_list +# define SSL_set1_curves SSL_set1_groups +# define SSL_set1_curves_list SSL_set1_groups_list +# define SSL_get_shared_curve SSL_get_shared_group + + +# if OPENSSL_API_COMPAT < 0x10100000L +/* Provide some compatibility macros for removed functionality. */ +# define SSL_CTX_need_tmp_RSA(ctx) 0 +# define SSL_CTX_set_tmp_rsa(ctx,rsa) 1 +# define SSL_need_tmp_RSA(ssl) 0 +# define SSL_set_tmp_rsa(ssl,rsa) 1 +# define SSL_CTX_set_ecdh_auto(dummy, onoff) ((onoff) != 0) +# define SSL_set_ecdh_auto(dummy, onoff) ((onoff) != 0) +/* + * We "pretend" to call the callback to avoid warnings about unused static + * functions. + */ +# define SSL_CTX_set_tmp_rsa_callback(ctx, cb) while(0) (cb)(NULL, 0, 0) +# define SSL_set_tmp_rsa_callback(ssl, cb) while(0) (cb)(NULL, 0, 0) +# endif +__owur const BIO_METHOD *BIO_f_ssl(void); +__owur BIO *BIO_new_ssl(SSL_CTX *ctx, int client); +__owur BIO *BIO_new_ssl_connect(SSL_CTX *ctx); +__owur BIO *BIO_new_buffer_ssl_connect(SSL_CTX *ctx); +__owur int BIO_ssl_copy_session_id(BIO *to, BIO *from); +void BIO_ssl_shutdown(BIO *ssl_bio); + +__owur int SSL_CTX_set_cipher_list(SSL_CTX *, const char *str); +__owur SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth); +int SSL_CTX_up_ref(SSL_CTX *ctx); +void SSL_CTX_free(SSL_CTX *); +__owur long SSL_CTX_set_timeout(SSL_CTX *ctx, long t); +__owur long SSL_CTX_get_timeout(const SSL_CTX *ctx); +__owur X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *); +void SSL_CTX_set_cert_store(SSL_CTX *, X509_STORE *); +void SSL_CTX_set1_cert_store(SSL_CTX *, X509_STORE *); +__owur int SSL_want(const SSL *s); +__owur int SSL_clear(SSL *s); + +void SSL_CTX_flush_sessions(SSL_CTX *ctx, long tm); + +__owur const SSL_CIPHER *SSL_get_current_cipher(const SSL *s); +__owur const SSL_CIPHER *SSL_get_pending_cipher(const SSL *s); +__owur int SSL_CIPHER_get_bits(const SSL_CIPHER *c, int *alg_bits); +__owur const char *SSL_CIPHER_get_version(const SSL_CIPHER *c); +__owur const char *SSL_CIPHER_get_name(const SSL_CIPHER *c); +__owur const char *SSL_CIPHER_standard_name(const SSL_CIPHER *c); +__owur const char *OPENSSL_cipher_name(const char *rfc_name); +__owur uint32_t SSL_CIPHER_get_id(const SSL_CIPHER *c); +__owur uint16_t SSL_CIPHER_get_protocol_id(const SSL_CIPHER *c); +__owur int SSL_CIPHER_get_kx_nid(const SSL_CIPHER *c); +__owur int SSL_CIPHER_get_auth_nid(const SSL_CIPHER *c); +__owur const EVP_MD *SSL_CIPHER_get_handshake_digest(const SSL_CIPHER *c); +__owur int SSL_CIPHER_is_aead(const SSL_CIPHER *c); + +__owur int SSL_get_fd(const SSL *s); +__owur int SSL_get_rfd(const SSL *s); +__owur int SSL_get_wfd(const SSL *s); +__owur const char *SSL_get_cipher_list(const SSL *s, int n); +__owur char *SSL_get_shared_ciphers(const SSL *s, char *buf, int size); +__owur int SSL_get_read_ahead(const SSL *s); +__owur int SSL_pending(const SSL *s); +__owur int SSL_has_pending(const SSL *s); +# ifndef OPENSSL_NO_SOCK +__owur int SSL_set_fd(SSL *s, int fd); +__owur int SSL_set_rfd(SSL *s, int fd); +__owur int SSL_set_wfd(SSL *s, int fd); +# endif +void SSL_set0_rbio(SSL *s, BIO *rbio); +void SSL_set0_wbio(SSL *s, BIO *wbio); +void SSL_set_bio(SSL *s, BIO *rbio, BIO *wbio); +__owur BIO *SSL_get_rbio(const SSL *s); +__owur BIO *SSL_get_wbio(const SSL *s); +__owur int SSL_set_cipher_list(SSL *s, const char *str); +__owur int SSL_CTX_set_ciphersuites(SSL_CTX *ctx, const char *str); +__owur int SSL_set_ciphersuites(SSL *s, const char *str); +void SSL_set_read_ahead(SSL *s, int yes); +__owur int SSL_get_verify_mode(const SSL *s); +__owur int SSL_get_verify_depth(const SSL *s); +__owur SSL_verify_cb SSL_get_verify_callback(const SSL *s); +void SSL_set_verify(SSL *s, int mode, SSL_verify_cb callback); +void SSL_set_verify_depth(SSL *s, int depth); +void SSL_set_cert_cb(SSL *s, int (*cb) (SSL *ssl, void *arg), void *arg); +# ifndef OPENSSL_NO_RSA +__owur int SSL_use_RSAPrivateKey(SSL *ssl, RSA *rsa); +__owur int SSL_use_RSAPrivateKey_ASN1(SSL *ssl, const unsigned char *d, + long len); +# endif +__owur int SSL_use_PrivateKey(SSL *ssl, EVP_PKEY *pkey); +__owur int SSL_use_PrivateKey_ASN1(int pk, SSL *ssl, const unsigned char *d, + long len); +__owur int SSL_use_certificate(SSL *ssl, X509 *x); +__owur int SSL_use_certificate_ASN1(SSL *ssl, const unsigned char *d, int len); +__owur int SSL_use_cert_and_key(SSL *ssl, X509 *x509, EVP_PKEY *privatekey, + STACK_OF(X509) *chain, int override); + + +/* serverinfo file format versions */ +# define SSL_SERVERINFOV1 1 +# define SSL_SERVERINFOV2 2 + +/* Set serverinfo data for the current active cert. */ +__owur int SSL_CTX_use_serverinfo(SSL_CTX *ctx, const unsigned char *serverinfo, + size_t serverinfo_length); +__owur int SSL_CTX_use_serverinfo_ex(SSL_CTX *ctx, unsigned int version, + const unsigned char *serverinfo, + size_t serverinfo_length); +__owur int SSL_CTX_use_serverinfo_file(SSL_CTX *ctx, const char *file); + +#ifndef OPENSSL_NO_RSA +__owur int SSL_use_RSAPrivateKey_file(SSL *ssl, const char *file, int type); +#endif + +__owur int SSL_use_PrivateKey_file(SSL *ssl, const char *file, int type); +__owur int SSL_use_certificate_file(SSL *ssl, const char *file, int type); + +#ifndef OPENSSL_NO_RSA +__owur int SSL_CTX_use_RSAPrivateKey_file(SSL_CTX *ctx, const char *file, + int type); +#endif +__owur int SSL_CTX_use_PrivateKey_file(SSL_CTX *ctx, const char *file, + int type); +__owur int SSL_CTX_use_certificate_file(SSL_CTX *ctx, const char *file, + int type); +/* PEM type */ +__owur int SSL_CTX_use_certificate_chain_file(SSL_CTX *ctx, const char *file); +__owur int SSL_use_certificate_chain_file(SSL *ssl, const char *file); +__owur STACK_OF(X509_NAME) *SSL_load_client_CA_file(const char *file); +__owur int SSL_add_file_cert_subjects_to_stack(STACK_OF(X509_NAME) *stackCAs, + const char *file); +int SSL_add_dir_cert_subjects_to_stack(STACK_OF(X509_NAME) *stackCAs, + const char *dir); + +# if OPENSSL_API_COMPAT < 0x10100000L +# define SSL_load_error_strings() \ + OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS \ + | OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL) +# endif + +__owur const char *SSL_state_string(const SSL *s); +__owur const char *SSL_rstate_string(const SSL *s); +__owur const char *SSL_state_string_long(const SSL *s); +__owur const char *SSL_rstate_string_long(const SSL *s); +__owur long SSL_SESSION_get_time(const SSL_SESSION *s); +__owur long SSL_SESSION_set_time(SSL_SESSION *s, long t); +__owur long SSL_SESSION_get_timeout(const SSL_SESSION *s); +__owur long SSL_SESSION_set_timeout(SSL_SESSION *s, long t); +__owur int SSL_SESSION_get_protocol_version(const SSL_SESSION *s); +__owur int SSL_SESSION_set_protocol_version(SSL_SESSION *s, int version); + +__owur const char *SSL_SESSION_get0_hostname(const SSL_SESSION *s); +__owur int SSL_SESSION_set1_hostname(SSL_SESSION *s, const char *hostname); +void SSL_SESSION_get0_alpn_selected(const SSL_SESSION *s, + const unsigned char **alpn, + size_t *len); +__owur int SSL_SESSION_set1_alpn_selected(SSL_SESSION *s, + const unsigned char *alpn, + size_t len); +__owur const SSL_CIPHER *SSL_SESSION_get0_cipher(const SSL_SESSION *s); +__owur int SSL_SESSION_set_cipher(SSL_SESSION *s, const SSL_CIPHER *cipher); +__owur int SSL_SESSION_has_ticket(const SSL_SESSION *s); +__owur unsigned long SSL_SESSION_get_ticket_lifetime_hint(const SSL_SESSION *s); +void SSL_SESSION_get0_ticket(const SSL_SESSION *s, const unsigned char **tick, + size_t *len); +__owur uint32_t SSL_SESSION_get_max_early_data(const SSL_SESSION *s); +__owur int SSL_SESSION_set_max_early_data(SSL_SESSION *s, + uint32_t max_early_data); +__owur int SSL_copy_session_id(SSL *to, const SSL *from); +__owur X509 *SSL_SESSION_get0_peer(SSL_SESSION *s); +__owur int SSL_SESSION_set1_id_context(SSL_SESSION *s, + const unsigned char *sid_ctx, + unsigned int sid_ctx_len); +__owur int SSL_SESSION_set1_id(SSL_SESSION *s, const unsigned char *sid, + unsigned int sid_len); +__owur int SSL_SESSION_is_resumable(const SSL_SESSION *s); + +__owur SSL_SESSION *SSL_SESSION_new(void); +__owur SSL_SESSION *SSL_SESSION_dup(SSL_SESSION *src); +const unsigned char *SSL_SESSION_get_id(const SSL_SESSION *s, + unsigned int *len); +const unsigned char *SSL_SESSION_get0_id_context(const SSL_SESSION *s, + unsigned int *len); +__owur unsigned int SSL_SESSION_get_compress_id(const SSL_SESSION *s); +# ifndef OPENSSL_NO_STDIO +int SSL_SESSION_print_fp(FILE *fp, const SSL_SESSION *ses); +# endif +int SSL_SESSION_print(BIO *fp, const SSL_SESSION *ses); +int SSL_SESSION_print_keylog(BIO *bp, const SSL_SESSION *x); +int SSL_SESSION_up_ref(SSL_SESSION *ses); +void SSL_SESSION_free(SSL_SESSION *ses); +__owur int i2d_SSL_SESSION(SSL_SESSION *in, unsigned char **pp); +__owur int SSL_set_session(SSL *to, SSL_SESSION *session); +int SSL_CTX_add_session(SSL_CTX *ctx, SSL_SESSION *session); +int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *session); +__owur int SSL_CTX_set_generate_session_id(SSL_CTX *ctx, GEN_SESSION_CB cb); +__owur int SSL_set_generate_session_id(SSL *s, GEN_SESSION_CB cb); +__owur int SSL_has_matching_session_id(const SSL *s, + const unsigned char *id, + unsigned int id_len); +SSL_SESSION *d2i_SSL_SESSION(SSL_SESSION **a, const unsigned char **pp, + long length); + +# ifdef HEADER_X509_H +__owur X509 *SSL_get_peer_certificate(const SSL *s); +# endif + +__owur STACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *s); + +__owur int SSL_CTX_get_verify_mode(const SSL_CTX *ctx); +__owur int SSL_CTX_get_verify_depth(const SSL_CTX *ctx); +__owur SSL_verify_cb SSL_CTX_get_verify_callback(const SSL_CTX *ctx); +void SSL_CTX_set_verify(SSL_CTX *ctx, int mode, SSL_verify_cb callback); +void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth); +void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx, + int (*cb) (X509_STORE_CTX *, void *), + void *arg); +void SSL_CTX_set_cert_cb(SSL_CTX *c, int (*cb) (SSL *ssl, void *arg), + void *arg); +# ifndef OPENSSL_NO_RSA +__owur int SSL_CTX_use_RSAPrivateKey(SSL_CTX *ctx, RSA *rsa); +__owur int SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX *ctx, const unsigned char *d, + long len); +# endif +__owur int SSL_CTX_use_PrivateKey(SSL_CTX *ctx, EVP_PKEY *pkey); +__owur int SSL_CTX_use_PrivateKey_ASN1(int pk, SSL_CTX *ctx, + const unsigned char *d, long len); +__owur int SSL_CTX_use_certificate(SSL_CTX *ctx, X509 *x); +__owur int SSL_CTX_use_certificate_ASN1(SSL_CTX *ctx, int len, + const unsigned char *d); +__owur int SSL_CTX_use_cert_and_key(SSL_CTX *ctx, X509 *x509, EVP_PKEY *privatekey, + STACK_OF(X509) *chain, int override); + +void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb); +void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx, void *u); +pem_password_cb *SSL_CTX_get_default_passwd_cb(SSL_CTX *ctx); +void *SSL_CTX_get_default_passwd_cb_userdata(SSL_CTX *ctx); +void SSL_set_default_passwd_cb(SSL *s, pem_password_cb *cb); +void SSL_set_default_passwd_cb_userdata(SSL *s, void *u); +pem_password_cb *SSL_get_default_passwd_cb(SSL *s); +void *SSL_get_default_passwd_cb_userdata(SSL *s); + +__owur int SSL_CTX_check_private_key(const SSL_CTX *ctx); +__owur int SSL_check_private_key(const SSL *ctx); + +__owur int SSL_CTX_set_session_id_context(SSL_CTX *ctx, + const unsigned char *sid_ctx, + unsigned int sid_ctx_len); + +SSL *SSL_new(SSL_CTX *ctx); +int SSL_up_ref(SSL *s); +int SSL_is_dtls(const SSL *s); +__owur int SSL_set_session_id_context(SSL *ssl, const unsigned char *sid_ctx, + unsigned int sid_ctx_len); + +__owur int SSL_CTX_set_purpose(SSL_CTX *ctx, int purpose); +__owur int SSL_set_purpose(SSL *ssl, int purpose); +__owur int SSL_CTX_set_trust(SSL_CTX *ctx, int trust); +__owur int SSL_set_trust(SSL *ssl, int trust); + +__owur int SSL_set1_host(SSL *s, const char *hostname); +__owur int SSL_add1_host(SSL *s, const char *hostname); +__owur const char *SSL_get0_peername(SSL *s); +void SSL_set_hostflags(SSL *s, unsigned int flags); + +__owur int SSL_CTX_dane_enable(SSL_CTX *ctx); +__owur int SSL_CTX_dane_mtype_set(SSL_CTX *ctx, const EVP_MD *md, + uint8_t mtype, uint8_t ord); +__owur int SSL_dane_enable(SSL *s, const char *basedomain); +__owur int SSL_dane_tlsa_add(SSL *s, uint8_t usage, uint8_t selector, + uint8_t mtype, unsigned const char *data, size_t dlen); +__owur int SSL_get0_dane_authority(SSL *s, X509 **mcert, EVP_PKEY **mspki); +__owur int SSL_get0_dane_tlsa(SSL *s, uint8_t *usage, uint8_t *selector, + uint8_t *mtype, unsigned const char **data, + size_t *dlen); +/* + * Bridge opacity barrier between libcrypt and libssl, also needed to support + * offline testing in test/danetest.c + */ +SSL_DANE *SSL_get0_dane(SSL *ssl); +/* + * DANE flags + */ +unsigned long SSL_CTX_dane_set_flags(SSL_CTX *ctx, unsigned long flags); +unsigned long SSL_CTX_dane_clear_flags(SSL_CTX *ctx, unsigned long flags); +unsigned long SSL_dane_set_flags(SSL *ssl, unsigned long flags); +unsigned long SSL_dane_clear_flags(SSL *ssl, unsigned long flags); + +__owur int SSL_CTX_set1_param(SSL_CTX *ctx, X509_VERIFY_PARAM *vpm); +__owur int SSL_set1_param(SSL *ssl, X509_VERIFY_PARAM *vpm); + +__owur X509_VERIFY_PARAM *SSL_CTX_get0_param(SSL_CTX *ctx); +__owur X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl); + +# ifndef OPENSSL_NO_SRP +int SSL_CTX_set_srp_username(SSL_CTX *ctx, char *name); +int SSL_CTX_set_srp_password(SSL_CTX *ctx, char *password); +int SSL_CTX_set_srp_strength(SSL_CTX *ctx, int strength); +int SSL_CTX_set_srp_client_pwd_callback(SSL_CTX *ctx, + char *(*cb) (SSL *, void *)); +int SSL_CTX_set_srp_verify_param_callback(SSL_CTX *ctx, + int (*cb) (SSL *, void *)); +int SSL_CTX_set_srp_username_callback(SSL_CTX *ctx, + int (*cb) (SSL *, int *, void *)); +int SSL_CTX_set_srp_cb_arg(SSL_CTX *ctx, void *arg); + +int SSL_set_srp_server_param(SSL *s, const BIGNUM *N, const BIGNUM *g, + BIGNUM *sa, BIGNUM *v, char *info); +int SSL_set_srp_server_param_pw(SSL *s, const char *user, const char *pass, + const char *grp); + +__owur BIGNUM *SSL_get_srp_g(SSL *s); +__owur BIGNUM *SSL_get_srp_N(SSL *s); + +__owur char *SSL_get_srp_username(SSL *s); +__owur char *SSL_get_srp_userinfo(SSL *s); +# endif + +/* + * ClientHello callback and helpers. + */ + +# define SSL_CLIENT_HELLO_SUCCESS 1 +# define SSL_CLIENT_HELLO_ERROR 0 +# define SSL_CLIENT_HELLO_RETRY (-1) + +typedef int (*SSL_client_hello_cb_fn) (SSL *s, int *al, void *arg); +void SSL_CTX_set_client_hello_cb(SSL_CTX *c, SSL_client_hello_cb_fn cb, + void *arg); +int SSL_client_hello_isv2(SSL *s); +unsigned int SSL_client_hello_get0_legacy_version(SSL *s); +size_t SSL_client_hello_get0_random(SSL *s, const unsigned char **out); +size_t SSL_client_hello_get0_session_id(SSL *s, const unsigned char **out); +size_t SSL_client_hello_get0_ciphers(SSL *s, const unsigned char **out); +size_t SSL_client_hello_get0_compression_methods(SSL *s, + const unsigned char **out); +int SSL_client_hello_get1_extensions_present(SSL *s, int **out, size_t *outlen); +int SSL_client_hello_get0_ext(SSL *s, unsigned int type, + const unsigned char **out, size_t *outlen); + +void SSL_certs_clear(SSL *s); +void SSL_free(SSL *ssl); +# ifdef OSSL_ASYNC_FD +/* + * Windows application developer has to include windows.h to use these. + */ +__owur int SSL_waiting_for_async(SSL *s); +__owur int SSL_get_all_async_fds(SSL *s, OSSL_ASYNC_FD *fds, size_t *numfds); +__owur int SSL_get_changed_async_fds(SSL *s, OSSL_ASYNC_FD *addfd, + size_t *numaddfds, OSSL_ASYNC_FD *delfd, + size_t *numdelfds); +# endif +__owur int SSL_accept(SSL *ssl); +__owur int SSL_stateless(SSL *s); +__owur int SSL_connect(SSL *ssl); +__owur int SSL_read(SSL *ssl, void *buf, int num); +__owur int SSL_read_ex(SSL *ssl, void *buf, size_t num, size_t *readbytes); + +# define SSL_READ_EARLY_DATA_ERROR 0 +# define SSL_READ_EARLY_DATA_SUCCESS 1 +# define SSL_READ_EARLY_DATA_FINISH 2 + +__owur int SSL_read_early_data(SSL *s, void *buf, size_t num, + size_t *readbytes); +__owur int SSL_peek(SSL *ssl, void *buf, int num); +__owur int SSL_peek_ex(SSL *ssl, void *buf, size_t num, size_t *readbytes); +__owur int SSL_write(SSL *ssl, const void *buf, int num); +__owur int SSL_write_ex(SSL *s, const void *buf, size_t num, size_t *written); +__owur int SSL_write_early_data(SSL *s, const void *buf, size_t num, + size_t *written); +long SSL_ctrl(SSL *ssl, int cmd, long larg, void *parg); +long SSL_callback_ctrl(SSL *, int, void (*)(void)); +long SSL_CTX_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg); +long SSL_CTX_callback_ctrl(SSL_CTX *, int, void (*)(void)); + +# define SSL_EARLY_DATA_NOT_SENT 0 +# define SSL_EARLY_DATA_REJECTED 1 +# define SSL_EARLY_DATA_ACCEPTED 2 + +__owur int SSL_get_early_data_status(const SSL *s); + +__owur int SSL_get_error(const SSL *s, int ret_code); +__owur const char *SSL_get_version(const SSL *s); + +/* This sets the 'default' SSL version that SSL_new() will create */ +__owur int SSL_CTX_set_ssl_version(SSL_CTX *ctx, const SSL_METHOD *meth); + +# ifndef OPENSSL_NO_SSL3_METHOD +DEPRECATEDIN_1_1_0(__owur const SSL_METHOD *SSLv3_method(void)) /* SSLv3 */ +DEPRECATEDIN_1_1_0(__owur const SSL_METHOD *SSLv3_server_method(void)) +DEPRECATEDIN_1_1_0(__owur const SSL_METHOD *SSLv3_client_method(void)) +# endif + +#define SSLv23_method TLS_method +#define SSLv23_server_method TLS_server_method +#define SSLv23_client_method TLS_client_method + +/* Negotiate highest available SSL/TLS version */ +__owur const SSL_METHOD *TLS_method(void); +__owur const SSL_METHOD *TLS_server_method(void); +__owur const SSL_METHOD *TLS_client_method(void); + +# ifndef OPENSSL_NO_TLS1_METHOD +DEPRECATEDIN_1_1_0(__owur const SSL_METHOD *TLSv1_method(void)) /* TLSv1.0 */ +DEPRECATEDIN_1_1_0(__owur const SSL_METHOD *TLSv1_server_method(void)) +DEPRECATEDIN_1_1_0(__owur const SSL_METHOD *TLSv1_client_method(void)) +# endif + +# ifndef OPENSSL_NO_TLS1_1_METHOD +DEPRECATEDIN_1_1_0(__owur const SSL_METHOD *TLSv1_1_method(void)) /* TLSv1.1 */ +DEPRECATEDIN_1_1_0(__owur const SSL_METHOD *TLSv1_1_server_method(void)) +DEPRECATEDIN_1_1_0(__owur const SSL_METHOD *TLSv1_1_client_method(void)) +# endif + +# ifndef OPENSSL_NO_TLS1_2_METHOD +DEPRECATEDIN_1_1_0(__owur const SSL_METHOD *TLSv1_2_method(void)) /* TLSv1.2 */ +DEPRECATEDIN_1_1_0(__owur const SSL_METHOD *TLSv1_2_server_method(void)) +DEPRECATEDIN_1_1_0(__owur const SSL_METHOD *TLSv1_2_client_method(void)) +# endif + +# ifndef OPENSSL_NO_DTLS1_METHOD +DEPRECATEDIN_1_1_0(__owur const SSL_METHOD *DTLSv1_method(void)) /* DTLSv1.0 */ +DEPRECATEDIN_1_1_0(__owur const SSL_METHOD *DTLSv1_server_method(void)) +DEPRECATEDIN_1_1_0(__owur const SSL_METHOD *DTLSv1_client_method(void)) +# endif + +# ifndef OPENSSL_NO_DTLS1_2_METHOD +/* DTLSv1.2 */ +DEPRECATEDIN_1_1_0(__owur const SSL_METHOD *DTLSv1_2_method(void)) +DEPRECATEDIN_1_1_0(__owur const SSL_METHOD *DTLSv1_2_server_method(void)) +DEPRECATEDIN_1_1_0(__owur const SSL_METHOD *DTLSv1_2_client_method(void)) +# endif + +__owur const SSL_METHOD *DTLS_method(void); /* DTLS 1.0 and 1.2 */ +__owur const SSL_METHOD *DTLS_server_method(void); /* DTLS 1.0 and 1.2 */ +__owur const SSL_METHOD *DTLS_client_method(void); /* DTLS 1.0 and 1.2 */ + +__owur size_t DTLS_get_data_mtu(const SSL *s); + +__owur STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *s); +__owur STACK_OF(SSL_CIPHER) *SSL_CTX_get_ciphers(const SSL_CTX *ctx); +__owur STACK_OF(SSL_CIPHER) *SSL_get_client_ciphers(const SSL *s); +__owur STACK_OF(SSL_CIPHER) *SSL_get1_supported_ciphers(SSL *s); + +__owur int SSL_do_handshake(SSL *s); +int SSL_key_update(SSL *s, int updatetype); +int SSL_get_key_update_type(SSL *s); +int SSL_renegotiate(SSL *s); +int SSL_renegotiate_abbreviated(SSL *s); +__owur int SSL_renegotiate_pending(SSL *s); +int SSL_shutdown(SSL *s); +__owur int SSL_verify_client_post_handshake(SSL *s); +void SSL_CTX_set_post_handshake_auth(SSL_CTX *ctx, int val); +void SSL_set_post_handshake_auth(SSL *s, int val); + +__owur const SSL_METHOD *SSL_CTX_get_ssl_method(SSL_CTX *ctx); +__owur const SSL_METHOD *SSL_get_ssl_method(SSL *s); +__owur int SSL_set_ssl_method(SSL *s, const SSL_METHOD *method); +__owur const char *SSL_alert_type_string_long(int value); +__owur const char *SSL_alert_type_string(int value); +__owur const char *SSL_alert_desc_string_long(int value); +__owur const char *SSL_alert_desc_string(int value); + +void SSL_set0_CA_list(SSL *s, STACK_OF(X509_NAME) *name_list); +void SSL_CTX_set0_CA_list(SSL_CTX *ctx, STACK_OF(X509_NAME) *name_list); +__owur const STACK_OF(X509_NAME) *SSL_get0_CA_list(const SSL *s); +__owur const STACK_OF(X509_NAME) *SSL_CTX_get0_CA_list(const SSL_CTX *ctx); +__owur int SSL_add1_to_CA_list(SSL *ssl, const X509 *x); +__owur int SSL_CTX_add1_to_CA_list(SSL_CTX *ctx, const X509 *x); +__owur const STACK_OF(X509_NAME) *SSL_get0_peer_CA_list(const SSL *s); + +void SSL_set_client_CA_list(SSL *s, STACK_OF(X509_NAME) *name_list); +void SSL_CTX_set_client_CA_list(SSL_CTX *ctx, STACK_OF(X509_NAME) *name_list); +__owur STACK_OF(X509_NAME) *SSL_get_client_CA_list(const SSL *s); +__owur STACK_OF(X509_NAME) *SSL_CTX_get_client_CA_list(const SSL_CTX *s); +__owur int SSL_add_client_CA(SSL *ssl, X509 *x); +__owur int SSL_CTX_add_client_CA(SSL_CTX *ctx, X509 *x); + +void SSL_set_connect_state(SSL *s); +void SSL_set_accept_state(SSL *s); + +__owur long SSL_get_default_timeout(const SSL *s); + +# if OPENSSL_API_COMPAT < 0x10100000L +# define SSL_library_init() OPENSSL_init_ssl(0, NULL) +# endif + +__owur char *SSL_CIPHER_description(const SSL_CIPHER *, char *buf, int size); +__owur STACK_OF(X509_NAME) *SSL_dup_CA_list(const STACK_OF(X509_NAME) *sk); + +__owur SSL *SSL_dup(SSL *ssl); + +__owur X509 *SSL_get_certificate(const SSL *ssl); +/* + * EVP_PKEY + */ +struct evp_pkey_st *SSL_get_privatekey(const SSL *ssl); + +__owur X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx); +__owur EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx); + +void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode); +__owur int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx); +void SSL_set_quiet_shutdown(SSL *ssl, int mode); +__owur int SSL_get_quiet_shutdown(const SSL *ssl); +void SSL_set_shutdown(SSL *ssl, int mode); +__owur int SSL_get_shutdown(const SSL *ssl); +__owur int SSL_version(const SSL *ssl); +__owur int SSL_client_version(const SSL *s); +__owur int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx); +__owur int SSL_CTX_set_default_verify_dir(SSL_CTX *ctx); +__owur int SSL_CTX_set_default_verify_file(SSL_CTX *ctx); +__owur int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile, + const char *CApath); +# define SSL_get0_session SSL_get_session/* just peek at pointer */ +__owur SSL_SESSION *SSL_get_session(const SSL *ssl); +__owur SSL_SESSION *SSL_get1_session(SSL *ssl); /* obtain a reference count */ +__owur SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl); +SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx); +void SSL_set_info_callback(SSL *ssl, + void (*cb) (const SSL *ssl, int type, int val)); +void (*SSL_get_info_callback(const SSL *ssl)) (const SSL *ssl, int type, + int val); +__owur OSSL_HANDSHAKE_STATE SSL_get_state(const SSL *ssl); + +void SSL_set_verify_result(SSL *ssl, long v); +__owur long SSL_get_verify_result(const SSL *ssl); +__owur STACK_OF(X509) *SSL_get0_verified_chain(const SSL *s); + +__owur size_t SSL_get_client_random(const SSL *ssl, unsigned char *out, + size_t outlen); +__owur size_t SSL_get_server_random(const SSL *ssl, unsigned char *out, + size_t outlen); +__owur size_t SSL_SESSION_get_master_key(const SSL_SESSION *sess, + unsigned char *out, size_t outlen); +__owur int SSL_SESSION_set1_master_key(SSL_SESSION *sess, + const unsigned char *in, size_t len); +uint8_t SSL_SESSION_get_max_fragment_length(const SSL_SESSION *sess); + +#define SSL_get_ex_new_index(l, p, newf, dupf, freef) \ + CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL, l, p, newf, dupf, freef) +__owur int SSL_set_ex_data(SSL *ssl, int idx, void *data); +void *SSL_get_ex_data(const SSL *ssl, int idx); +#define SSL_SESSION_get_ex_new_index(l, p, newf, dupf, freef) \ + CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_SESSION, l, p, newf, dupf, freef) +__owur int SSL_SESSION_set_ex_data(SSL_SESSION *ss, int idx, void *data); +void *SSL_SESSION_get_ex_data(const SSL_SESSION *ss, int idx); +#define SSL_CTX_get_ex_new_index(l, p, newf, dupf, freef) \ + CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_CTX, l, p, newf, dupf, freef) +__owur int SSL_CTX_set_ex_data(SSL_CTX *ssl, int idx, void *data); +void *SSL_CTX_get_ex_data(const SSL_CTX *ssl, int idx); + +__owur int SSL_get_ex_data_X509_STORE_CTX_idx(void); + +# define SSL_CTX_sess_set_cache_size(ctx,t) \ + SSL_CTX_ctrl(ctx,SSL_CTRL_SET_SESS_CACHE_SIZE,t,NULL) +# define SSL_CTX_sess_get_cache_size(ctx) \ + SSL_CTX_ctrl(ctx,SSL_CTRL_GET_SESS_CACHE_SIZE,0,NULL) +# define SSL_CTX_set_session_cache_mode(ctx,m) \ + SSL_CTX_ctrl(ctx,SSL_CTRL_SET_SESS_CACHE_MODE,m,NULL) +# define SSL_CTX_get_session_cache_mode(ctx) \ + SSL_CTX_ctrl(ctx,SSL_CTRL_GET_SESS_CACHE_MODE,0,NULL) + +# define SSL_CTX_get_default_read_ahead(ctx) SSL_CTX_get_read_ahead(ctx) +# define SSL_CTX_set_default_read_ahead(ctx,m) SSL_CTX_set_read_ahead(ctx,m) +# define SSL_CTX_get_read_ahead(ctx) \ + SSL_CTX_ctrl(ctx,SSL_CTRL_GET_READ_AHEAD,0,NULL) +# define SSL_CTX_set_read_ahead(ctx,m) \ + SSL_CTX_ctrl(ctx,SSL_CTRL_SET_READ_AHEAD,m,NULL) +# define SSL_CTX_get_max_cert_list(ctx) \ + SSL_CTX_ctrl(ctx,SSL_CTRL_GET_MAX_CERT_LIST,0,NULL) +# define SSL_CTX_set_max_cert_list(ctx,m) \ + SSL_CTX_ctrl(ctx,SSL_CTRL_SET_MAX_CERT_LIST,m,NULL) +# define SSL_get_max_cert_list(ssl) \ + SSL_ctrl(ssl,SSL_CTRL_GET_MAX_CERT_LIST,0,NULL) +# define SSL_set_max_cert_list(ssl,m) \ + SSL_ctrl(ssl,SSL_CTRL_SET_MAX_CERT_LIST,m,NULL) + +# define SSL_CTX_set_max_send_fragment(ctx,m) \ + SSL_CTX_ctrl(ctx,SSL_CTRL_SET_MAX_SEND_FRAGMENT,m,NULL) +# define SSL_set_max_send_fragment(ssl,m) \ + SSL_ctrl(ssl,SSL_CTRL_SET_MAX_SEND_FRAGMENT,m,NULL) +# define SSL_CTX_set_split_send_fragment(ctx,m) \ + SSL_CTX_ctrl(ctx,SSL_CTRL_SET_SPLIT_SEND_FRAGMENT,m,NULL) +# define SSL_set_split_send_fragment(ssl,m) \ + SSL_ctrl(ssl,SSL_CTRL_SET_SPLIT_SEND_FRAGMENT,m,NULL) +# define SSL_CTX_set_max_pipelines(ctx,m) \ + SSL_CTX_ctrl(ctx,SSL_CTRL_SET_MAX_PIPELINES,m,NULL) +# define SSL_set_max_pipelines(ssl,m) \ + SSL_ctrl(ssl,SSL_CTRL_SET_MAX_PIPELINES,m,NULL) + +void SSL_CTX_set_default_read_buffer_len(SSL_CTX *ctx, size_t len); +void SSL_set_default_read_buffer_len(SSL *s, size_t len); + +# ifndef OPENSSL_NO_DH +/* NB: the |keylength| is only applicable when is_export is true */ +void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx, + DH *(*dh) (SSL *ssl, int is_export, + int keylength)); +void SSL_set_tmp_dh_callback(SSL *ssl, + DH *(*dh) (SSL *ssl, int is_export, + int keylength)); +# endif + +__owur const COMP_METHOD *SSL_get_current_compression(SSL *s); +__owur const COMP_METHOD *SSL_get_current_expansion(SSL *s); +__owur const char *SSL_COMP_get_name(const COMP_METHOD *comp); +__owur const char *SSL_COMP_get0_name(const SSL_COMP *comp); +__owur int SSL_COMP_get_id(const SSL_COMP *comp); +STACK_OF(SSL_COMP) *SSL_COMP_get_compression_methods(void); +__owur STACK_OF(SSL_COMP) *SSL_COMP_set0_compression_methods(STACK_OF(SSL_COMP) + *meths); +# if OPENSSL_API_COMPAT < 0x10100000L +# define SSL_COMP_free_compression_methods() while(0) continue +# endif +__owur int SSL_COMP_add_compression_method(int id, COMP_METHOD *cm); + +const SSL_CIPHER *SSL_CIPHER_find(SSL *ssl, const unsigned char *ptr); +int SSL_CIPHER_get_cipher_nid(const SSL_CIPHER *c); +int SSL_CIPHER_get_digest_nid(const SSL_CIPHER *c); +int SSL_bytes_to_cipher_list(SSL *s, const unsigned char *bytes, size_t len, + int isv2format, STACK_OF(SSL_CIPHER) **sk, + STACK_OF(SSL_CIPHER) **scsvs); + +/* TLS extensions functions */ +__owur int SSL_set_session_ticket_ext(SSL *s, void *ext_data, int ext_len); + +__owur int SSL_set_session_ticket_ext_cb(SSL *s, + tls_session_ticket_ext_cb_fn cb, + void *arg); + +/* Pre-shared secret session resumption functions */ +__owur int SSL_set_session_secret_cb(SSL *s, + tls_session_secret_cb_fn session_secret_cb, + void *arg); + +void SSL_CTX_set_not_resumable_session_callback(SSL_CTX *ctx, + int (*cb) (SSL *ssl, + int + is_forward_secure)); + +void SSL_set_not_resumable_session_callback(SSL *ssl, + int (*cb) (SSL *ssl, + int is_forward_secure)); + +void SSL_CTX_set_record_padding_callback(SSL_CTX *ctx, + size_t (*cb) (SSL *ssl, int type, + size_t len, void *arg)); +void SSL_CTX_set_record_padding_callback_arg(SSL_CTX *ctx, void *arg); +void *SSL_CTX_get_record_padding_callback_arg(SSL_CTX *ctx); +int SSL_CTX_set_block_padding(SSL_CTX *ctx, size_t block_size); + +void SSL_set_record_padding_callback(SSL *ssl, + size_t (*cb) (SSL *ssl, int type, + size_t len, void *arg)); +void SSL_set_record_padding_callback_arg(SSL *ssl, void *arg); +void *SSL_get_record_padding_callback_arg(SSL *ssl); +int SSL_set_block_padding(SSL *ssl, size_t block_size); + +int SSL_set_num_tickets(SSL *s, size_t num_tickets); +size_t SSL_get_num_tickets(SSL *s); +int SSL_CTX_set_num_tickets(SSL_CTX *ctx, size_t num_tickets); +size_t SSL_CTX_get_num_tickets(SSL_CTX *ctx); + +# if OPENSSL_API_COMPAT < 0x10100000L +# define SSL_cache_hit(s) SSL_session_reused(s) +# endif + +__owur int SSL_session_reused(SSL *s); +__owur int SSL_is_server(const SSL *s); + +__owur __owur SSL_CONF_CTX *SSL_CONF_CTX_new(void); +int SSL_CONF_CTX_finish(SSL_CONF_CTX *cctx); +void SSL_CONF_CTX_free(SSL_CONF_CTX *cctx); +unsigned int SSL_CONF_CTX_set_flags(SSL_CONF_CTX *cctx, unsigned int flags); +__owur unsigned int SSL_CONF_CTX_clear_flags(SSL_CONF_CTX *cctx, + unsigned int flags); +__owur int SSL_CONF_CTX_set1_prefix(SSL_CONF_CTX *cctx, const char *pre); + +void SSL_CONF_CTX_set_ssl(SSL_CONF_CTX *cctx, SSL *ssl); +void SSL_CONF_CTX_set_ssl_ctx(SSL_CONF_CTX *cctx, SSL_CTX *ctx); + +__owur int SSL_CONF_cmd(SSL_CONF_CTX *cctx, const char *cmd, const char *value); +__owur int SSL_CONF_cmd_argv(SSL_CONF_CTX *cctx, int *pargc, char ***pargv); +__owur int SSL_CONF_cmd_value_type(SSL_CONF_CTX *cctx, const char *cmd); + +void SSL_add_ssl_module(void); +int SSL_config(SSL *s, const char *name); +int SSL_CTX_config(SSL_CTX *ctx, const char *name); + +# ifndef OPENSSL_NO_SSL_TRACE +void SSL_trace(int write_p, int version, int content_type, + const void *buf, size_t len, SSL *ssl, void *arg); +# endif + +# ifndef OPENSSL_NO_SOCK +int DTLSv1_listen(SSL *s, BIO_ADDR *client); +# endif + +# ifndef OPENSSL_NO_CT + +/* + * A callback for verifying that the received SCTs are sufficient. + * Expected to return 1 if they are sufficient, otherwise 0. + * May return a negative integer if an error occurs. + * A connection should be aborted if the SCTs are deemed insufficient. + */ +typedef int (*ssl_ct_validation_cb)(const CT_POLICY_EVAL_CTX *ctx, + const STACK_OF(SCT) *scts, void *arg); + +/* + * Sets a |callback| that is invoked upon receipt of ServerHelloDone to validate + * the received SCTs. + * If the callback returns a non-positive result, the connection is terminated. + * Call this function before beginning a handshake. + * If a NULL |callback| is provided, SCT validation is disabled. + * |arg| is arbitrary userdata that will be passed to the callback whenever it + * is invoked. Ownership of |arg| remains with the caller. + * + * NOTE: A side-effect of setting a CT callback is that an OCSP stapled response + * will be requested. + */ +int SSL_set_ct_validation_callback(SSL *s, ssl_ct_validation_cb callback, + void *arg); +int SSL_CTX_set_ct_validation_callback(SSL_CTX *ctx, + ssl_ct_validation_cb callback, + void *arg); +#define SSL_disable_ct(s) \ + ((void) SSL_set_validation_callback((s), NULL, NULL)) +#define SSL_CTX_disable_ct(ctx) \ + ((void) SSL_CTX_set_validation_callback((ctx), NULL, NULL)) + +/* + * The validation type enumerates the available behaviours of the built-in SSL + * CT validation callback selected via SSL_enable_ct() and SSL_CTX_enable_ct(). + * The underlying callback is a static function in libssl. + */ +enum { + SSL_CT_VALIDATION_PERMISSIVE = 0, + SSL_CT_VALIDATION_STRICT +}; + +/* + * Enable CT by setting up a callback that implements one of the built-in + * validation variants. The SSL_CT_VALIDATION_PERMISSIVE variant always + * continues the handshake, the application can make appropriate decisions at + * handshake completion. The SSL_CT_VALIDATION_STRICT variant requires at + * least one valid SCT, or else handshake termination will be requested. The + * handshake may continue anyway if SSL_VERIFY_NONE is in effect. + */ +int SSL_enable_ct(SSL *s, int validation_mode); +int SSL_CTX_enable_ct(SSL_CTX *ctx, int validation_mode); + +/* + * Report whether a non-NULL callback is enabled. + */ +int SSL_ct_is_enabled(const SSL *s); +int SSL_CTX_ct_is_enabled(const SSL_CTX *ctx); + +/* Gets the SCTs received from a connection */ +const STACK_OF(SCT) *SSL_get0_peer_scts(SSL *s); + +/* + * Loads the CT log list from the default location. + * If a CTLOG_STORE has previously been set using SSL_CTX_set_ctlog_store, + * the log information loaded from this file will be appended to the + * CTLOG_STORE. + * Returns 1 on success, 0 otherwise. + */ +int SSL_CTX_set_default_ctlog_list_file(SSL_CTX *ctx); + +/* + * Loads the CT log list from the specified file path. + * If a CTLOG_STORE has previously been set using SSL_CTX_set_ctlog_store, + * the log information loaded from this file will be appended to the + * CTLOG_STORE. + * Returns 1 on success, 0 otherwise. + */ +int SSL_CTX_set_ctlog_list_file(SSL_CTX *ctx, const char *path); + +/* + * Sets the CT log list used by all SSL connections created from this SSL_CTX. + * Ownership of the CTLOG_STORE is transferred to the SSL_CTX. + */ +void SSL_CTX_set0_ctlog_store(SSL_CTX *ctx, CTLOG_STORE *logs); + +/* + * Gets the CT log list used by all SSL connections created from this SSL_CTX. + * This will be NULL unless one of the following functions has been called: + * - SSL_CTX_set_default_ctlog_list_file + * - SSL_CTX_set_ctlog_list_file + * - SSL_CTX_set_ctlog_store + */ +const CTLOG_STORE *SSL_CTX_get0_ctlog_store(const SSL_CTX *ctx); + +# endif /* OPENSSL_NO_CT */ + +/* What the "other" parameter contains in security callback */ +/* Mask for type */ +# define SSL_SECOP_OTHER_TYPE 0xffff0000 +# define SSL_SECOP_OTHER_NONE 0 +# define SSL_SECOP_OTHER_CIPHER (1 << 16) +# define SSL_SECOP_OTHER_CURVE (2 << 16) +# define SSL_SECOP_OTHER_DH (3 << 16) +# define SSL_SECOP_OTHER_PKEY (4 << 16) +# define SSL_SECOP_OTHER_SIGALG (5 << 16) +# define SSL_SECOP_OTHER_CERT (6 << 16) + +/* Indicated operation refers to peer key or certificate */ +# define SSL_SECOP_PEER 0x1000 + +/* Values for "op" parameter in security callback */ + +/* Called to filter ciphers */ +/* Ciphers client supports */ +# define SSL_SECOP_CIPHER_SUPPORTED (1 | SSL_SECOP_OTHER_CIPHER) +/* Cipher shared by client/server */ +# define SSL_SECOP_CIPHER_SHARED (2 | SSL_SECOP_OTHER_CIPHER) +/* Sanity check of cipher server selects */ +# define SSL_SECOP_CIPHER_CHECK (3 | SSL_SECOP_OTHER_CIPHER) +/* Curves supported by client */ +# define SSL_SECOP_CURVE_SUPPORTED (4 | SSL_SECOP_OTHER_CURVE) +/* Curves shared by client/server */ +# define SSL_SECOP_CURVE_SHARED (5 | SSL_SECOP_OTHER_CURVE) +/* Sanity check of curve server selects */ +# define SSL_SECOP_CURVE_CHECK (6 | SSL_SECOP_OTHER_CURVE) +/* Temporary DH key */ +# define SSL_SECOP_TMP_DH (7 | SSL_SECOP_OTHER_PKEY) +/* SSL/TLS version */ +# define SSL_SECOP_VERSION (9 | SSL_SECOP_OTHER_NONE) +/* Session tickets */ +# define SSL_SECOP_TICKET (10 | SSL_SECOP_OTHER_NONE) +/* Supported signature algorithms sent to peer */ +# define SSL_SECOP_SIGALG_SUPPORTED (11 | SSL_SECOP_OTHER_SIGALG) +/* Shared signature algorithm */ +# define SSL_SECOP_SIGALG_SHARED (12 | SSL_SECOP_OTHER_SIGALG) +/* Sanity check signature algorithm allowed */ +# define SSL_SECOP_SIGALG_CHECK (13 | SSL_SECOP_OTHER_SIGALG) +/* Used to get mask of supported public key signature algorithms */ +# define SSL_SECOP_SIGALG_MASK (14 | SSL_SECOP_OTHER_SIGALG) +/* Use to see if compression is allowed */ +# define SSL_SECOP_COMPRESSION (15 | SSL_SECOP_OTHER_NONE) +/* EE key in certificate */ +# define SSL_SECOP_EE_KEY (16 | SSL_SECOP_OTHER_CERT) +/* CA key in certificate */ +# define SSL_SECOP_CA_KEY (17 | SSL_SECOP_OTHER_CERT) +/* CA digest algorithm in certificate */ +# define SSL_SECOP_CA_MD (18 | SSL_SECOP_OTHER_CERT) +/* Peer EE key in certificate */ +# define SSL_SECOP_PEER_EE_KEY (SSL_SECOP_EE_KEY | SSL_SECOP_PEER) +/* Peer CA key in certificate */ +# define SSL_SECOP_PEER_CA_KEY (SSL_SECOP_CA_KEY | SSL_SECOP_PEER) +/* Peer CA digest algorithm in certificate */ +# define SSL_SECOP_PEER_CA_MD (SSL_SECOP_CA_MD | SSL_SECOP_PEER) + +void SSL_set_security_level(SSL *s, int level); +__owur int SSL_get_security_level(const SSL *s); +void SSL_set_security_callback(SSL *s, + int (*cb) (const SSL *s, const SSL_CTX *ctx, + int op, int bits, int nid, + void *other, void *ex)); +int (*SSL_get_security_callback(const SSL *s)) (const SSL *s, + const SSL_CTX *ctx, int op, + int bits, int nid, void *other, + void *ex); +void SSL_set0_security_ex_data(SSL *s, void *ex); +__owur void *SSL_get0_security_ex_data(const SSL *s); + +void SSL_CTX_set_security_level(SSL_CTX *ctx, int level); +__owur int SSL_CTX_get_security_level(const SSL_CTX *ctx); +void SSL_CTX_set_security_callback(SSL_CTX *ctx, + int (*cb) (const SSL *s, const SSL_CTX *ctx, + int op, int bits, int nid, + void *other, void *ex)); +int (*SSL_CTX_get_security_callback(const SSL_CTX *ctx)) (const SSL *s, + const SSL_CTX *ctx, + int op, int bits, + int nid, + void *other, + void *ex); +void SSL_CTX_set0_security_ex_data(SSL_CTX *ctx, void *ex); +__owur void *SSL_CTX_get0_security_ex_data(const SSL_CTX *ctx); + +/* OPENSSL_INIT flag 0x010000 reserved for internal use */ +# define OPENSSL_INIT_NO_LOAD_SSL_STRINGS 0x00100000L +# define OPENSSL_INIT_LOAD_SSL_STRINGS 0x00200000L + +# define OPENSSL_INIT_SSL_DEFAULT \ + (OPENSSL_INIT_LOAD_SSL_STRINGS | OPENSSL_INIT_LOAD_CRYPTO_STRINGS) + +int OPENSSL_init_ssl(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings); + +# ifndef OPENSSL_NO_UNIT_TEST +__owur const struct openssl_ssl_test_functions *SSL_test_functions(void); +# endif + +__owur int SSL_free_buffers(SSL *ssl); +__owur int SSL_alloc_buffers(SSL *ssl); + +/* Status codes passed to the decrypt session ticket callback. Some of these + * are for internal use only and are never passed to the callback. */ +typedef int SSL_TICKET_STATUS; + +/* Support for ticket appdata */ +/* fatal error, malloc failure */ +# define SSL_TICKET_FATAL_ERR_MALLOC 0 +/* fatal error, either from parsing or decrypting the ticket */ +# define SSL_TICKET_FATAL_ERR_OTHER 1 +/* No ticket present */ +# define SSL_TICKET_NONE 2 +/* Empty ticket present */ +# define SSL_TICKET_EMPTY 3 +/* the ticket couldn't be decrypted */ +# define SSL_TICKET_NO_DECRYPT 4 +/* a ticket was successfully decrypted */ +# define SSL_TICKET_SUCCESS 5 +/* same as above but the ticket needs to be renewed */ +# define SSL_TICKET_SUCCESS_RENEW 6 + +/* Return codes for the decrypt session ticket callback */ +typedef int SSL_TICKET_RETURN; + +/* An error occurred */ +#define SSL_TICKET_RETURN_ABORT 0 +/* Do not use the ticket, do not send a renewed ticket to the client */ +#define SSL_TICKET_RETURN_IGNORE 1 +/* Do not use the ticket, send a renewed ticket to the client */ +#define SSL_TICKET_RETURN_IGNORE_RENEW 2 +/* Use the ticket, do not send a renewed ticket to the client */ +#define SSL_TICKET_RETURN_USE 3 +/* Use the ticket, send a renewed ticket to the client */ +#define SSL_TICKET_RETURN_USE_RENEW 4 + +typedef int (*SSL_CTX_generate_session_ticket_fn)(SSL *s, void *arg); +typedef SSL_TICKET_RETURN (*SSL_CTX_decrypt_session_ticket_fn)(SSL *s, SSL_SESSION *ss, + const unsigned char *keyname, + size_t keyname_length, + SSL_TICKET_STATUS status, + void *arg); +int SSL_CTX_set_session_ticket_cb(SSL_CTX *ctx, + SSL_CTX_generate_session_ticket_fn gen_cb, + SSL_CTX_decrypt_session_ticket_fn dec_cb, + void *arg); +int SSL_SESSION_set1_ticket_appdata(SSL_SESSION *ss, const void *data, size_t len); +int SSL_SESSION_get0_ticket_appdata(SSL_SESSION *ss, void **data, size_t *len); + +extern const char SSL_version_str[]; + +typedef unsigned int (*DTLS_timer_cb)(SSL *s, unsigned int timer_us); + +void DTLS_set_timer_cb(SSL *s, DTLS_timer_cb cb); + + +typedef int (*SSL_allow_early_data_cb_fn)(SSL *s, void *arg); +void SSL_CTX_set_allow_early_data_cb(SSL_CTX *ctx, + SSL_allow_early_data_cb_fn cb, + void *arg); +void SSL_set_allow_early_data_cb(SSL *s, + SSL_allow_early_data_cb_fn cb, + void *arg); + +# ifdef __cplusplus +} +# endif +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/ssl2.h b/example/android/third_party/crypto/arm/include/openssl/ssl2.h new file mode 100644 index 00000000..5321bd27 --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/ssl2.h @@ -0,0 +1,24 @@ +/* + * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_SSL2_H +# define HEADER_SSL2_H + +#ifdef __cplusplus +extern "C" { +#endif + +# define SSL2_VERSION 0x0002 + +# define SSL2_MT_CLIENT_HELLO 1 + +#ifdef __cplusplus +} +#endif +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/ssl3.h b/example/android/third_party/crypto/arm/include/openssl/ssl3.h new file mode 100644 index 00000000..8d01fcc4 --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/ssl3.h @@ -0,0 +1,339 @@ +/* + * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. + * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_SSL3_H +# define HEADER_SSL3_H + +# include +# include +# include +# include + +#ifdef __cplusplus +extern "C" { +#endif + +/* + * Signalling cipher suite value from RFC 5746 + * (TLS_EMPTY_RENEGOTIATION_INFO_SCSV) + */ +# define SSL3_CK_SCSV 0x030000FF + +/* + * Signalling cipher suite value from draft-ietf-tls-downgrade-scsv-00 + * (TLS_FALLBACK_SCSV) + */ +# define SSL3_CK_FALLBACK_SCSV 0x03005600 + +# define SSL3_CK_RSA_NULL_MD5 0x03000001 +# define SSL3_CK_RSA_NULL_SHA 0x03000002 +# define SSL3_CK_RSA_RC4_40_MD5 0x03000003 +# define SSL3_CK_RSA_RC4_128_MD5 0x03000004 +# define SSL3_CK_RSA_RC4_128_SHA 0x03000005 +# define SSL3_CK_RSA_RC2_40_MD5 0x03000006 +# define SSL3_CK_RSA_IDEA_128_SHA 0x03000007 +# define SSL3_CK_RSA_DES_40_CBC_SHA 0x03000008 +# define SSL3_CK_RSA_DES_64_CBC_SHA 0x03000009 +# define SSL3_CK_RSA_DES_192_CBC3_SHA 0x0300000A + +# define SSL3_CK_DH_DSS_DES_40_CBC_SHA 0x0300000B +# define SSL3_CK_DH_DSS_DES_64_CBC_SHA 0x0300000C +# define SSL3_CK_DH_DSS_DES_192_CBC3_SHA 0x0300000D +# define SSL3_CK_DH_RSA_DES_40_CBC_SHA 0x0300000E +# define SSL3_CK_DH_RSA_DES_64_CBC_SHA 0x0300000F +# define SSL3_CK_DH_RSA_DES_192_CBC3_SHA 0x03000010 + +# define SSL3_CK_DHE_DSS_DES_40_CBC_SHA 0x03000011 +# define SSL3_CK_EDH_DSS_DES_40_CBC_SHA SSL3_CK_DHE_DSS_DES_40_CBC_SHA +# define SSL3_CK_DHE_DSS_DES_64_CBC_SHA 0x03000012 +# define SSL3_CK_EDH_DSS_DES_64_CBC_SHA SSL3_CK_DHE_DSS_DES_64_CBC_SHA +# define SSL3_CK_DHE_DSS_DES_192_CBC3_SHA 0x03000013 +# define SSL3_CK_EDH_DSS_DES_192_CBC3_SHA SSL3_CK_DHE_DSS_DES_192_CBC3_SHA +# define SSL3_CK_DHE_RSA_DES_40_CBC_SHA 0x03000014 +# define SSL3_CK_EDH_RSA_DES_40_CBC_SHA SSL3_CK_DHE_RSA_DES_40_CBC_SHA +# define SSL3_CK_DHE_RSA_DES_64_CBC_SHA 0x03000015 +# define SSL3_CK_EDH_RSA_DES_64_CBC_SHA SSL3_CK_DHE_RSA_DES_64_CBC_SHA +# define SSL3_CK_DHE_RSA_DES_192_CBC3_SHA 0x03000016 +# define SSL3_CK_EDH_RSA_DES_192_CBC3_SHA SSL3_CK_DHE_RSA_DES_192_CBC3_SHA + +# define SSL3_CK_ADH_RC4_40_MD5 0x03000017 +# define SSL3_CK_ADH_RC4_128_MD5 0x03000018 +# define SSL3_CK_ADH_DES_40_CBC_SHA 0x03000019 +# define SSL3_CK_ADH_DES_64_CBC_SHA 0x0300001A +# define SSL3_CK_ADH_DES_192_CBC_SHA 0x0300001B + +/* a bundle of RFC standard cipher names, generated from ssl3_ciphers[] */ +# define SSL3_RFC_RSA_NULL_MD5 "TLS_RSA_WITH_NULL_MD5" +# define SSL3_RFC_RSA_NULL_SHA "TLS_RSA_WITH_NULL_SHA" +# define SSL3_RFC_RSA_DES_192_CBC3_SHA "TLS_RSA_WITH_3DES_EDE_CBC_SHA" +# define SSL3_RFC_DHE_DSS_DES_192_CBC3_SHA "TLS_DHE_DSS_WITH_3DES_EDE_CBC_SHA" +# define SSL3_RFC_DHE_RSA_DES_192_CBC3_SHA "TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA" +# define SSL3_RFC_ADH_DES_192_CBC_SHA "TLS_DH_anon_WITH_3DES_EDE_CBC_SHA" +# define SSL3_RFC_RSA_IDEA_128_SHA "TLS_RSA_WITH_IDEA_CBC_SHA" +# define SSL3_RFC_RSA_RC4_128_MD5 "TLS_RSA_WITH_RC4_128_MD5" +# define SSL3_RFC_RSA_RC4_128_SHA "TLS_RSA_WITH_RC4_128_SHA" +# define SSL3_RFC_ADH_RC4_128_MD5 "TLS_DH_anon_WITH_RC4_128_MD5" + +# define SSL3_TXT_RSA_NULL_MD5 "NULL-MD5" +# define SSL3_TXT_RSA_NULL_SHA "NULL-SHA" +# define SSL3_TXT_RSA_RC4_40_MD5 "EXP-RC4-MD5" +# define SSL3_TXT_RSA_RC4_128_MD5 "RC4-MD5" +# define SSL3_TXT_RSA_RC4_128_SHA "RC4-SHA" +# define SSL3_TXT_RSA_RC2_40_MD5 "EXP-RC2-CBC-MD5" +# define SSL3_TXT_RSA_IDEA_128_SHA "IDEA-CBC-SHA" +# define SSL3_TXT_RSA_DES_40_CBC_SHA "EXP-DES-CBC-SHA" +# define SSL3_TXT_RSA_DES_64_CBC_SHA "DES-CBC-SHA" +# define SSL3_TXT_RSA_DES_192_CBC3_SHA "DES-CBC3-SHA" + +# define SSL3_TXT_DH_DSS_DES_40_CBC_SHA "EXP-DH-DSS-DES-CBC-SHA" +# define SSL3_TXT_DH_DSS_DES_64_CBC_SHA "DH-DSS-DES-CBC-SHA" +# define SSL3_TXT_DH_DSS_DES_192_CBC3_SHA "DH-DSS-DES-CBC3-SHA" +# define SSL3_TXT_DH_RSA_DES_40_CBC_SHA "EXP-DH-RSA-DES-CBC-SHA" +# define SSL3_TXT_DH_RSA_DES_64_CBC_SHA "DH-RSA-DES-CBC-SHA" +# define SSL3_TXT_DH_RSA_DES_192_CBC3_SHA "DH-RSA-DES-CBC3-SHA" + +# define SSL3_TXT_DHE_DSS_DES_40_CBC_SHA "EXP-DHE-DSS-DES-CBC-SHA" +# define SSL3_TXT_DHE_DSS_DES_64_CBC_SHA "DHE-DSS-DES-CBC-SHA" +# define SSL3_TXT_DHE_DSS_DES_192_CBC3_SHA "DHE-DSS-DES-CBC3-SHA" +# define SSL3_TXT_DHE_RSA_DES_40_CBC_SHA "EXP-DHE-RSA-DES-CBC-SHA" +# define SSL3_TXT_DHE_RSA_DES_64_CBC_SHA "DHE-RSA-DES-CBC-SHA" +# define SSL3_TXT_DHE_RSA_DES_192_CBC3_SHA "DHE-RSA-DES-CBC3-SHA" + +/* + * This next block of six "EDH" labels is for backward compatibility with + * older versions of OpenSSL. New code should use the six "DHE" labels above + * instead: + */ +# define SSL3_TXT_EDH_DSS_DES_40_CBC_SHA "EXP-EDH-DSS-DES-CBC-SHA" +# define SSL3_TXT_EDH_DSS_DES_64_CBC_SHA "EDH-DSS-DES-CBC-SHA" +# define SSL3_TXT_EDH_DSS_DES_192_CBC3_SHA "EDH-DSS-DES-CBC3-SHA" +# define SSL3_TXT_EDH_RSA_DES_40_CBC_SHA "EXP-EDH-RSA-DES-CBC-SHA" +# define SSL3_TXT_EDH_RSA_DES_64_CBC_SHA "EDH-RSA-DES-CBC-SHA" +# define SSL3_TXT_EDH_RSA_DES_192_CBC3_SHA "EDH-RSA-DES-CBC3-SHA" + +# define SSL3_TXT_ADH_RC4_40_MD5 "EXP-ADH-RC4-MD5" +# define SSL3_TXT_ADH_RC4_128_MD5 "ADH-RC4-MD5" +# define SSL3_TXT_ADH_DES_40_CBC_SHA "EXP-ADH-DES-CBC-SHA" +# define SSL3_TXT_ADH_DES_64_CBC_SHA "ADH-DES-CBC-SHA" +# define SSL3_TXT_ADH_DES_192_CBC_SHA "ADH-DES-CBC3-SHA" + +# define SSL3_SSL_SESSION_ID_LENGTH 32 +# define SSL3_MAX_SSL_SESSION_ID_LENGTH 32 + +# define SSL3_MASTER_SECRET_SIZE 48 +# define SSL3_RANDOM_SIZE 32 +# define SSL3_SESSION_ID_SIZE 32 +# define SSL3_RT_HEADER_LENGTH 5 + +# define SSL3_HM_HEADER_LENGTH 4 + +# ifndef SSL3_ALIGN_PAYLOAD + /* + * Some will argue that this increases memory footprint, but it's not + * actually true. Point is that malloc has to return at least 64-bit aligned + * pointers, meaning that allocating 5 bytes wastes 3 bytes in either case. + * Suggested pre-gaping simply moves these wasted bytes from the end of + * allocated region to its front, but makes data payload aligned, which + * improves performance:-) + */ +# define SSL3_ALIGN_PAYLOAD 8 +# else +# if (SSL3_ALIGN_PAYLOAD&(SSL3_ALIGN_PAYLOAD-1))!=0 +# error "insane SSL3_ALIGN_PAYLOAD" +# undef SSL3_ALIGN_PAYLOAD +# endif +# endif + +/* + * This is the maximum MAC (digest) size used by the SSL library. Currently + * maximum of 20 is used by SHA1, but we reserve for future extension for + * 512-bit hashes. + */ + +# define SSL3_RT_MAX_MD_SIZE 64 + +/* + * Maximum block size used in all ciphersuites. Currently 16 for AES. + */ + +# define SSL_RT_MAX_CIPHER_BLOCK_SIZE 16 + +# define SSL3_RT_MAX_EXTRA (16384) + +/* Maximum plaintext length: defined by SSL/TLS standards */ +# define SSL3_RT_MAX_PLAIN_LENGTH 16384 +/* Maximum compression overhead: defined by SSL/TLS standards */ +# define SSL3_RT_MAX_COMPRESSED_OVERHEAD 1024 + +/* + * The standards give a maximum encryption overhead of 1024 bytes. In + * practice the value is lower than this. The overhead is the maximum number + * of padding bytes (256) plus the mac size. + */ +# define SSL3_RT_MAX_ENCRYPTED_OVERHEAD (256 + SSL3_RT_MAX_MD_SIZE) +# define SSL3_RT_MAX_TLS13_ENCRYPTED_OVERHEAD 256 + +/* + * OpenSSL currently only uses a padding length of at most one block so the + * send overhead is smaller. + */ + +# define SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD \ + (SSL_RT_MAX_CIPHER_BLOCK_SIZE + SSL3_RT_MAX_MD_SIZE) + +/* If compression isn't used don't include the compression overhead */ + +# ifdef OPENSSL_NO_COMP +# define SSL3_RT_MAX_COMPRESSED_LENGTH SSL3_RT_MAX_PLAIN_LENGTH +# else +# define SSL3_RT_MAX_COMPRESSED_LENGTH \ + (SSL3_RT_MAX_PLAIN_LENGTH+SSL3_RT_MAX_COMPRESSED_OVERHEAD) +# endif +# define SSL3_RT_MAX_ENCRYPTED_LENGTH \ + (SSL3_RT_MAX_ENCRYPTED_OVERHEAD+SSL3_RT_MAX_COMPRESSED_LENGTH) +# define SSL3_RT_MAX_TLS13_ENCRYPTED_LENGTH \ + (SSL3_RT_MAX_PLAIN_LENGTH + SSL3_RT_MAX_TLS13_ENCRYPTED_OVERHEAD) +# define SSL3_RT_MAX_PACKET_SIZE \ + (SSL3_RT_MAX_ENCRYPTED_LENGTH+SSL3_RT_HEADER_LENGTH) + +# define SSL3_MD_CLIENT_FINISHED_CONST "\x43\x4C\x4E\x54" +# define SSL3_MD_SERVER_FINISHED_CONST "\x53\x52\x56\x52" + +# define SSL3_VERSION 0x0300 +# define SSL3_VERSION_MAJOR 0x03 +# define SSL3_VERSION_MINOR 0x00 + +# define SSL3_RT_CHANGE_CIPHER_SPEC 20 +# define SSL3_RT_ALERT 21 +# define SSL3_RT_HANDSHAKE 22 +# define SSL3_RT_APPLICATION_DATA 23 +# define DTLS1_RT_HEARTBEAT 24 + +/* Pseudo content types to indicate additional parameters */ +# define TLS1_RT_CRYPTO 0x1000 +# define TLS1_RT_CRYPTO_PREMASTER (TLS1_RT_CRYPTO | 0x1) +# define TLS1_RT_CRYPTO_CLIENT_RANDOM (TLS1_RT_CRYPTO | 0x2) +# define TLS1_RT_CRYPTO_SERVER_RANDOM (TLS1_RT_CRYPTO | 0x3) +# define TLS1_RT_CRYPTO_MASTER (TLS1_RT_CRYPTO | 0x4) + +# define TLS1_RT_CRYPTO_READ 0x0000 +# define TLS1_RT_CRYPTO_WRITE 0x0100 +# define TLS1_RT_CRYPTO_MAC (TLS1_RT_CRYPTO | 0x5) +# define TLS1_RT_CRYPTO_KEY (TLS1_RT_CRYPTO | 0x6) +# define TLS1_RT_CRYPTO_IV (TLS1_RT_CRYPTO | 0x7) +# define TLS1_RT_CRYPTO_FIXED_IV (TLS1_RT_CRYPTO | 0x8) + +/* Pseudo content types for SSL/TLS header info */ +# define SSL3_RT_HEADER 0x100 +# define SSL3_RT_INNER_CONTENT_TYPE 0x101 + +# define SSL3_AL_WARNING 1 +# define SSL3_AL_FATAL 2 + +# define SSL3_AD_CLOSE_NOTIFY 0 +# define SSL3_AD_UNEXPECTED_MESSAGE 10/* fatal */ +# define SSL3_AD_BAD_RECORD_MAC 20/* fatal */ +# define SSL3_AD_DECOMPRESSION_FAILURE 30/* fatal */ +# define SSL3_AD_HANDSHAKE_FAILURE 40/* fatal */ +# define SSL3_AD_NO_CERTIFICATE 41 +# define SSL3_AD_BAD_CERTIFICATE 42 +# define SSL3_AD_UNSUPPORTED_CERTIFICATE 43 +# define SSL3_AD_CERTIFICATE_REVOKED 44 +# define SSL3_AD_CERTIFICATE_EXPIRED 45 +# define SSL3_AD_CERTIFICATE_UNKNOWN 46 +# define SSL3_AD_ILLEGAL_PARAMETER 47/* fatal */ + +# define TLS1_HB_REQUEST 1 +# define TLS1_HB_RESPONSE 2 + + +# define SSL3_CT_RSA_SIGN 1 +# define SSL3_CT_DSS_SIGN 2 +# define SSL3_CT_RSA_FIXED_DH 3 +# define SSL3_CT_DSS_FIXED_DH 4 +# define SSL3_CT_RSA_EPHEMERAL_DH 5 +# define SSL3_CT_DSS_EPHEMERAL_DH 6 +# define SSL3_CT_FORTEZZA_DMS 20 +/* + * SSL3_CT_NUMBER is used to size arrays and it must be large enough to + * contain all of the cert types defined for *either* SSLv3 and TLSv1. + */ +# define SSL3_CT_NUMBER 10 + +# if defined(TLS_CT_NUMBER) +# if TLS_CT_NUMBER != SSL3_CT_NUMBER +# error "SSL/TLS CT_NUMBER values do not match" +# endif +# endif + +/* No longer used as of OpenSSL 1.1.1 */ +# define SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS 0x0001 + +/* Removed from OpenSSL 1.1.0 */ +# define TLS1_FLAGS_TLS_PADDING_BUG 0x0 + +# define TLS1_FLAGS_SKIP_CERT_VERIFY 0x0010 + +/* Set if we encrypt then mac instead of usual mac then encrypt */ +# define TLS1_FLAGS_ENCRYPT_THEN_MAC_READ 0x0100 +# define TLS1_FLAGS_ENCRYPT_THEN_MAC TLS1_FLAGS_ENCRYPT_THEN_MAC_READ + +/* Set if extended master secret extension received from peer */ +# define TLS1_FLAGS_RECEIVED_EXTMS 0x0200 + +# define TLS1_FLAGS_ENCRYPT_THEN_MAC_WRITE 0x0400 + +# define TLS1_FLAGS_STATELESS 0x0800 + +# define SSL3_MT_HELLO_REQUEST 0 +# define SSL3_MT_CLIENT_HELLO 1 +# define SSL3_MT_SERVER_HELLO 2 +# define SSL3_MT_NEWSESSION_TICKET 4 +# define SSL3_MT_END_OF_EARLY_DATA 5 +# define SSL3_MT_ENCRYPTED_EXTENSIONS 8 +# define SSL3_MT_CERTIFICATE 11 +# define SSL3_MT_SERVER_KEY_EXCHANGE 12 +# define SSL3_MT_CERTIFICATE_REQUEST 13 +# define SSL3_MT_SERVER_DONE 14 +# define SSL3_MT_CERTIFICATE_VERIFY 15 +# define SSL3_MT_CLIENT_KEY_EXCHANGE 16 +# define SSL3_MT_FINISHED 20 +# define SSL3_MT_CERTIFICATE_URL 21 +# define SSL3_MT_CERTIFICATE_STATUS 22 +# define SSL3_MT_SUPPLEMENTAL_DATA 23 +# define SSL3_MT_KEY_UPDATE 24 +# ifndef OPENSSL_NO_NEXTPROTONEG +# define SSL3_MT_NEXT_PROTO 67 +# endif +# define SSL3_MT_MESSAGE_HASH 254 +# define DTLS1_MT_HELLO_VERIFY_REQUEST 3 + +/* Dummy message type for handling CCS like a normal handshake message */ +# define SSL3_MT_CHANGE_CIPHER_SPEC 0x0101 + +# define SSL3_MT_CCS 1 + +/* These are used when changing over to a new cipher */ +# define SSL3_CC_READ 0x001 +# define SSL3_CC_WRITE 0x002 +# define SSL3_CC_CLIENT 0x010 +# define SSL3_CC_SERVER 0x020 +# define SSL3_CC_EARLY 0x040 +# define SSL3_CC_HANDSHAKE 0x080 +# define SSL3_CC_APPLICATION 0x100 +# define SSL3_CHANGE_CIPHER_CLIENT_WRITE (SSL3_CC_CLIENT|SSL3_CC_WRITE) +# define SSL3_CHANGE_CIPHER_SERVER_READ (SSL3_CC_SERVER|SSL3_CC_READ) +# define SSL3_CHANGE_CIPHER_CLIENT_READ (SSL3_CC_CLIENT|SSL3_CC_READ) +# define SSL3_CHANGE_CIPHER_SERVER_WRITE (SSL3_CC_SERVER|SSL3_CC_WRITE) + +#ifdef __cplusplus +} +#endif +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/sslerr.h b/example/android/third_party/crypto/arm/include/openssl/sslerr.h new file mode 100644 index 00000000..87b295c9 --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/sslerr.h @@ -0,0 +1,767 @@ +/* + * Generated by util/mkerr.pl DO NOT EDIT + * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_SSLERR_H +# define HEADER_SSLERR_H + +# ifdef __cplusplus +extern "C" +# endif +int ERR_load_SSL_strings(void); + +/* + * SSL function codes. + */ +# define SSL_F_ADD_CLIENT_KEY_SHARE_EXT 438 +# define SSL_F_ADD_KEY_SHARE 512 +# define SSL_F_BYTES_TO_CIPHER_LIST 519 +# define SSL_F_CHECK_SUITEB_CIPHER_LIST 331 +# define SSL_F_CIPHERSUITE_CB 622 +# define SSL_F_CONSTRUCT_CA_NAMES 552 +# define SSL_F_CONSTRUCT_KEY_EXCHANGE_TBS 553 +# define SSL_F_CONSTRUCT_STATEFUL_TICKET 636 +# define SSL_F_CONSTRUCT_STATELESS_TICKET 637 +# define SSL_F_CREATE_SYNTHETIC_MESSAGE_HASH 539 +# define SSL_F_CREATE_TICKET_PREQUEL 638 +# define SSL_F_CT_MOVE_SCTS 345 +# define SSL_F_CT_STRICT 349 +# define SSL_F_CUSTOM_EXT_ADD 554 +# define SSL_F_CUSTOM_EXT_PARSE 555 +# define SSL_F_D2I_SSL_SESSION 103 +# define SSL_F_DANE_CTX_ENABLE 347 +# define SSL_F_DANE_MTYPE_SET 393 +# define SSL_F_DANE_TLSA_ADD 394 +# define SSL_F_DERIVE_SECRET_KEY_AND_IV 514 +# define SSL_F_DO_DTLS1_WRITE 245 +# define SSL_F_DO_SSL3_WRITE 104 +# define SSL_F_DTLS1_BUFFER_RECORD 247 +# define SSL_F_DTLS1_CHECK_TIMEOUT_NUM 318 +# define SSL_F_DTLS1_HEARTBEAT 305 +# define SSL_F_DTLS1_HM_FRAGMENT_NEW 623 +# define SSL_F_DTLS1_PREPROCESS_FRAGMENT 288 +# define SSL_F_DTLS1_PROCESS_BUFFERED_RECORDS 424 +# define SSL_F_DTLS1_PROCESS_RECORD 257 +# define SSL_F_DTLS1_READ_BYTES 258 +# define SSL_F_DTLS1_READ_FAILED 339 +# define SSL_F_DTLS1_RETRANSMIT_MESSAGE 390 +# define SSL_F_DTLS1_WRITE_APP_DATA_BYTES 268 +# define SSL_F_DTLS1_WRITE_BYTES 545 +# define SSL_F_DTLSV1_LISTEN 350 +# define SSL_F_DTLS_CONSTRUCT_CHANGE_CIPHER_SPEC 371 +# define SSL_F_DTLS_CONSTRUCT_HELLO_VERIFY_REQUEST 385 +# define SSL_F_DTLS_GET_REASSEMBLED_MESSAGE 370 +# define SSL_F_DTLS_PROCESS_HELLO_VERIFY 386 +# define SSL_F_DTLS_RECORD_LAYER_NEW 635 +# define SSL_F_DTLS_WAIT_FOR_DRY 592 +# define SSL_F_EARLY_DATA_COUNT_OK 532 +# define SSL_F_FINAL_EARLY_DATA 556 +# define SSL_F_FINAL_EC_PT_FORMATS 485 +# define SSL_F_FINAL_EMS 486 +# define SSL_F_FINAL_KEY_SHARE 503 +# define SSL_F_FINAL_MAXFRAGMENTLEN 557 +# define SSL_F_FINAL_RENEGOTIATE 483 +# define SSL_F_FINAL_SERVER_NAME 558 +# define SSL_F_FINAL_SIG_ALGS 497 +# define SSL_F_GET_CERT_VERIFY_TBS_DATA 588 +# define SSL_F_NSS_KEYLOG_INT 500 +# define SSL_F_OPENSSL_INIT_SSL 342 +# define SSL_F_OSSL_STATEM_CLIENT13_READ_TRANSITION 436 +# define SSL_F_OSSL_STATEM_CLIENT13_WRITE_TRANSITION 598 +# define SSL_F_OSSL_STATEM_CLIENT_CONSTRUCT_MESSAGE 430 +# define SSL_F_OSSL_STATEM_CLIENT_POST_PROCESS_MESSAGE 593 +# define SSL_F_OSSL_STATEM_CLIENT_PROCESS_MESSAGE 594 +# define SSL_F_OSSL_STATEM_CLIENT_READ_TRANSITION 417 +# define SSL_F_OSSL_STATEM_CLIENT_WRITE_TRANSITION 599 +# define SSL_F_OSSL_STATEM_SERVER13_READ_TRANSITION 437 +# define SSL_F_OSSL_STATEM_SERVER13_WRITE_TRANSITION 600 +# define SSL_F_OSSL_STATEM_SERVER_CONSTRUCT_MESSAGE 431 +# define SSL_F_OSSL_STATEM_SERVER_POST_PROCESS_MESSAGE 601 +# define SSL_F_OSSL_STATEM_SERVER_POST_WORK 602 +# define SSL_F_OSSL_STATEM_SERVER_PROCESS_MESSAGE 603 +# define SSL_F_OSSL_STATEM_SERVER_READ_TRANSITION 418 +# define SSL_F_OSSL_STATEM_SERVER_WRITE_TRANSITION 604 +# define SSL_F_PARSE_CA_NAMES 541 +# define SSL_F_PITEM_NEW 624 +# define SSL_F_PQUEUE_NEW 625 +# define SSL_F_PROCESS_KEY_SHARE_EXT 439 +# define SSL_F_READ_STATE_MACHINE 352 +# define SSL_F_SET_CLIENT_CIPHERSUITE 540 +# define SSL_F_SRP_GENERATE_CLIENT_MASTER_SECRET 595 +# define SSL_F_SRP_GENERATE_SERVER_MASTER_SECRET 589 +# define SSL_F_SRP_VERIFY_SERVER_PARAM 596 +# define SSL_F_SSL3_CHANGE_CIPHER_STATE 129 +# define SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM 130 +# define SSL_F_SSL3_CTRL 213 +# define SSL_F_SSL3_CTX_CTRL 133 +# define SSL_F_SSL3_DIGEST_CACHED_RECORDS 293 +# define SSL_F_SSL3_DO_CHANGE_CIPHER_SPEC 292 +# define SSL_F_SSL3_ENC 608 +# define SSL_F_SSL3_FINAL_FINISH_MAC 285 +# define SSL_F_SSL3_FINISH_MAC 587 +# define SSL_F_SSL3_GENERATE_KEY_BLOCK 238 +# define SSL_F_SSL3_GENERATE_MASTER_SECRET 388 +# define SSL_F_SSL3_GET_RECORD 143 +# define SSL_F_SSL3_INIT_FINISHED_MAC 397 +# define SSL_F_SSL3_OUTPUT_CERT_CHAIN 147 +# define SSL_F_SSL3_READ_BYTES 148 +# define SSL_F_SSL3_READ_N 149 +# define SSL_F_SSL3_SETUP_KEY_BLOCK 157 +# define SSL_F_SSL3_SETUP_READ_BUFFER 156 +# define SSL_F_SSL3_SETUP_WRITE_BUFFER 291 +# define SSL_F_SSL3_WRITE_BYTES 158 +# define SSL_F_SSL3_WRITE_PENDING 159 +# define SSL_F_SSL_ADD_CERT_CHAIN 316 +# define SSL_F_SSL_ADD_CERT_TO_BUF 319 +# define SSL_F_SSL_ADD_CERT_TO_WPACKET 493 +# define SSL_F_SSL_ADD_CLIENTHELLO_RENEGOTIATE_EXT 298 +# define SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT 277 +# define SSL_F_SSL_ADD_CLIENTHELLO_USE_SRTP_EXT 307 +# define SSL_F_SSL_ADD_DIR_CERT_SUBJECTS_TO_STACK 215 +# define SSL_F_SSL_ADD_FILE_CERT_SUBJECTS_TO_STACK 216 +# define SSL_F_SSL_ADD_SERVERHELLO_RENEGOTIATE_EXT 299 +# define SSL_F_SSL_ADD_SERVERHELLO_TLSEXT 278 +# define SSL_F_SSL_ADD_SERVERHELLO_USE_SRTP_EXT 308 +# define SSL_F_SSL_BAD_METHOD 160 +# define SSL_F_SSL_BUILD_CERT_CHAIN 332 +# define SSL_F_SSL_BYTES_TO_CIPHER_LIST 161 +# define SSL_F_SSL_CACHE_CIPHERLIST 520 +# define SSL_F_SSL_CERT_ADD0_CHAIN_CERT 346 +# define SSL_F_SSL_CERT_DUP 221 +# define SSL_F_SSL_CERT_NEW 162 +# define SSL_F_SSL_CERT_SET0_CHAIN 340 +# define SSL_F_SSL_CHECK_PRIVATE_KEY 163 +# define SSL_F_SSL_CHECK_SERVERHELLO_TLSEXT 280 +# define SSL_F_SSL_CHECK_SRP_EXT_CLIENTHELLO 606 +# define SSL_F_SSL_CHECK_SRVR_ECC_CERT_AND_ALG 279 +# define SSL_F_SSL_CHOOSE_CLIENT_VERSION 607 +# define SSL_F_SSL_CIPHER_DESCRIPTION 626 +# define SSL_F_SSL_CIPHER_LIST_TO_BYTES 425 +# define SSL_F_SSL_CIPHER_PROCESS_RULESTR 230 +# define SSL_F_SSL_CIPHER_STRENGTH_SORT 231 +# define SSL_F_SSL_CLEAR 164 +# define SSL_F_SSL_CLIENT_HELLO_GET1_EXTENSIONS_PRESENT 627 +# define SSL_F_SSL_COMP_ADD_COMPRESSION_METHOD 165 +# define SSL_F_SSL_CONF_CMD 334 +# define SSL_F_SSL_CREATE_CIPHER_LIST 166 +# define SSL_F_SSL_CTRL 232 +# define SSL_F_SSL_CTX_CHECK_PRIVATE_KEY 168 +# define SSL_F_SSL_CTX_ENABLE_CT 398 +# define SSL_F_SSL_CTX_MAKE_PROFILES 309 +# define SSL_F_SSL_CTX_NEW 169 +# define SSL_F_SSL_CTX_SET_ALPN_PROTOS 343 +# define SSL_F_SSL_CTX_SET_CIPHER_LIST 269 +# define SSL_F_SSL_CTX_SET_CLIENT_CERT_ENGINE 290 +# define SSL_F_SSL_CTX_SET_CT_VALIDATION_CALLBACK 396 +# define SSL_F_SSL_CTX_SET_SESSION_ID_CONTEXT 219 +# define SSL_F_SSL_CTX_SET_SSL_VERSION 170 +# define SSL_F_SSL_CTX_SET_TLSEXT_MAX_FRAGMENT_LENGTH 551 +# define SSL_F_SSL_CTX_USE_CERTIFICATE 171 +# define SSL_F_SSL_CTX_USE_CERTIFICATE_ASN1 172 +# define SSL_F_SSL_CTX_USE_CERTIFICATE_FILE 173 +# define SSL_F_SSL_CTX_USE_PRIVATEKEY 174 +# define SSL_F_SSL_CTX_USE_PRIVATEKEY_ASN1 175 +# define SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE 176 +# define SSL_F_SSL_CTX_USE_PSK_IDENTITY_HINT 272 +# define SSL_F_SSL_CTX_USE_RSAPRIVATEKEY 177 +# define SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_ASN1 178 +# define SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE 179 +# define SSL_F_SSL_CTX_USE_SERVERINFO 336 +# define SSL_F_SSL_CTX_USE_SERVERINFO_EX 543 +# define SSL_F_SSL_CTX_USE_SERVERINFO_FILE 337 +# define SSL_F_SSL_DANE_DUP 403 +# define SSL_F_SSL_DANE_ENABLE 395 +# define SSL_F_SSL_DERIVE 590 +# define SSL_F_SSL_DO_CONFIG 391 +# define SSL_F_SSL_DO_HANDSHAKE 180 +# define SSL_F_SSL_DUP_CA_LIST 408 +# define SSL_F_SSL_ENABLE_CT 402 +# define SSL_F_SSL_GENERATE_PKEY_GROUP 559 +# define SSL_F_SSL_GENERATE_SESSION_ID 547 +# define SSL_F_SSL_GET_NEW_SESSION 181 +# define SSL_F_SSL_GET_PREV_SESSION 217 +# define SSL_F_SSL_GET_SERVER_CERT_INDEX 322 +# define SSL_F_SSL_GET_SIGN_PKEY 183 +# define SSL_F_SSL_HANDSHAKE_HASH 560 +# define SSL_F_SSL_INIT_WBIO_BUFFER 184 +# define SSL_F_SSL_KEY_UPDATE 515 +# define SSL_F_SSL_LOAD_CLIENT_CA_FILE 185 +# define SSL_F_SSL_LOG_MASTER_SECRET 498 +# define SSL_F_SSL_LOG_RSA_CLIENT_KEY_EXCHANGE 499 +# define SSL_F_SSL_MODULE_INIT 392 +# define SSL_F_SSL_NEW 186 +# define SSL_F_SSL_NEXT_PROTO_VALIDATE 565 +# define SSL_F_SSL_PARSE_CLIENTHELLO_RENEGOTIATE_EXT 300 +# define SSL_F_SSL_PARSE_CLIENTHELLO_TLSEXT 302 +# define SSL_F_SSL_PARSE_CLIENTHELLO_USE_SRTP_EXT 310 +# define SSL_F_SSL_PARSE_SERVERHELLO_RENEGOTIATE_EXT 301 +# define SSL_F_SSL_PARSE_SERVERHELLO_TLSEXT 303 +# define SSL_F_SSL_PARSE_SERVERHELLO_USE_SRTP_EXT 311 +# define SSL_F_SSL_PEEK 270 +# define SSL_F_SSL_PEEK_EX 432 +# define SSL_F_SSL_PEEK_INTERNAL 522 +# define SSL_F_SSL_READ 223 +# define SSL_F_SSL_READ_EARLY_DATA 529 +# define SSL_F_SSL_READ_EX 434 +# define SSL_F_SSL_READ_INTERNAL 523 +# define SSL_F_SSL_RENEGOTIATE 516 +# define SSL_F_SSL_RENEGOTIATE_ABBREVIATED 546 +# define SSL_F_SSL_SCAN_CLIENTHELLO_TLSEXT 320 +# define SSL_F_SSL_SCAN_SERVERHELLO_TLSEXT 321 +# define SSL_F_SSL_SESSION_DUP 348 +# define SSL_F_SSL_SESSION_NEW 189 +# define SSL_F_SSL_SESSION_PRINT_FP 190 +# define SSL_F_SSL_SESSION_SET1_ID 423 +# define SSL_F_SSL_SESSION_SET1_ID_CONTEXT 312 +# define SSL_F_SSL_SET_ALPN_PROTOS 344 +# define SSL_F_SSL_SET_CERT 191 +# define SSL_F_SSL_SET_CERT_AND_KEY 621 +# define SSL_F_SSL_SET_CIPHER_LIST 271 +# define SSL_F_SSL_SET_CT_VALIDATION_CALLBACK 399 +# define SSL_F_SSL_SET_FD 192 +# define SSL_F_SSL_SET_PKEY 193 +# define SSL_F_SSL_SET_RFD 194 +# define SSL_F_SSL_SET_SESSION 195 +# define SSL_F_SSL_SET_SESSION_ID_CONTEXT 218 +# define SSL_F_SSL_SET_SESSION_TICKET_EXT 294 +# define SSL_F_SSL_SET_TLSEXT_MAX_FRAGMENT_LENGTH 550 +# define SSL_F_SSL_SET_WFD 196 +# define SSL_F_SSL_SHUTDOWN 224 +# define SSL_F_SSL_SRP_CTX_INIT 313 +# define SSL_F_SSL_START_ASYNC_JOB 389 +# define SSL_F_SSL_UNDEFINED_FUNCTION 197 +# define SSL_F_SSL_UNDEFINED_VOID_FUNCTION 244 +# define SSL_F_SSL_USE_CERTIFICATE 198 +# define SSL_F_SSL_USE_CERTIFICATE_ASN1 199 +# define SSL_F_SSL_USE_CERTIFICATE_FILE 200 +# define SSL_F_SSL_USE_PRIVATEKEY 201 +# define SSL_F_SSL_USE_PRIVATEKEY_ASN1 202 +# define SSL_F_SSL_USE_PRIVATEKEY_FILE 203 +# define SSL_F_SSL_USE_PSK_IDENTITY_HINT 273 +# define SSL_F_SSL_USE_RSAPRIVATEKEY 204 +# define SSL_F_SSL_USE_RSAPRIVATEKEY_ASN1 205 +# define SSL_F_SSL_USE_RSAPRIVATEKEY_FILE 206 +# define SSL_F_SSL_VALIDATE_CT 400 +# define SSL_F_SSL_VERIFY_CERT_CHAIN 207 +# define SSL_F_SSL_VERIFY_CLIENT_POST_HANDSHAKE 616 +# define SSL_F_SSL_WRITE 208 +# define SSL_F_SSL_WRITE_EARLY_DATA 526 +# define SSL_F_SSL_WRITE_EARLY_FINISH 527 +# define SSL_F_SSL_WRITE_EX 433 +# define SSL_F_SSL_WRITE_INTERNAL 524 +# define SSL_F_STATE_MACHINE 353 +# define SSL_F_TLS12_CHECK_PEER_SIGALG 333 +# define SSL_F_TLS12_COPY_SIGALGS 533 +# define SSL_F_TLS13_CHANGE_CIPHER_STATE 440 +# define SSL_F_TLS13_ENC 609 +# define SSL_F_TLS13_FINAL_FINISH_MAC 605 +# define SSL_F_TLS13_GENERATE_SECRET 591 +# define SSL_F_TLS13_HKDF_EXPAND 561 +# define SSL_F_TLS13_RESTORE_HANDSHAKE_DIGEST_FOR_PHA 617 +# define SSL_F_TLS13_SAVE_HANDSHAKE_DIGEST_FOR_PHA 618 +# define SSL_F_TLS13_SETUP_KEY_BLOCK 441 +# define SSL_F_TLS1_CHANGE_CIPHER_STATE 209 +# define SSL_F_TLS1_CHECK_DUPLICATE_EXTENSIONS 341 +# define SSL_F_TLS1_ENC 401 +# define SSL_F_TLS1_EXPORT_KEYING_MATERIAL 314 +# define SSL_F_TLS1_GET_CURVELIST 338 +# define SSL_F_TLS1_PRF 284 +# define SSL_F_TLS1_SAVE_U16 628 +# define SSL_F_TLS1_SETUP_KEY_BLOCK 211 +# define SSL_F_TLS1_SET_GROUPS 629 +# define SSL_F_TLS1_SET_RAW_SIGALGS 630 +# define SSL_F_TLS1_SET_SERVER_SIGALGS 335 +# define SSL_F_TLS1_SET_SHARED_SIGALGS 631 +# define SSL_F_TLS1_SET_SIGALGS 632 +# define SSL_F_TLS_CHOOSE_SIGALG 513 +# define SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK 354 +# define SSL_F_TLS_COLLECT_EXTENSIONS 435 +# define SSL_F_TLS_CONSTRUCT_CERTIFICATE_AUTHORITIES 542 +# define SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST 372 +# define SSL_F_TLS_CONSTRUCT_CERT_STATUS 429 +# define SSL_F_TLS_CONSTRUCT_CERT_STATUS_BODY 494 +# define SSL_F_TLS_CONSTRUCT_CERT_VERIFY 496 +# define SSL_F_TLS_CONSTRUCT_CHANGE_CIPHER_SPEC 427 +# define SSL_F_TLS_CONSTRUCT_CKE_DHE 404 +# define SSL_F_TLS_CONSTRUCT_CKE_ECDHE 405 +# define SSL_F_TLS_CONSTRUCT_CKE_GOST 406 +# define SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE 407 +# define SSL_F_TLS_CONSTRUCT_CKE_RSA 409 +# define SSL_F_TLS_CONSTRUCT_CKE_SRP 410 +# define SSL_F_TLS_CONSTRUCT_CLIENT_CERTIFICATE 484 +# define SSL_F_TLS_CONSTRUCT_CLIENT_HELLO 487 +# define SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE 488 +# define SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY 489 +# define SSL_F_TLS_CONSTRUCT_CTOS_ALPN 466 +# define SSL_F_TLS_CONSTRUCT_CTOS_CERTIFICATE 355 +# define SSL_F_TLS_CONSTRUCT_CTOS_COOKIE 535 +# define SSL_F_TLS_CONSTRUCT_CTOS_EARLY_DATA 530 +# define SSL_F_TLS_CONSTRUCT_CTOS_EC_PT_FORMATS 467 +# define SSL_F_TLS_CONSTRUCT_CTOS_EMS 468 +# define SSL_F_TLS_CONSTRUCT_CTOS_ETM 469 +# define SSL_F_TLS_CONSTRUCT_CTOS_HELLO 356 +# define SSL_F_TLS_CONSTRUCT_CTOS_KEY_EXCHANGE 357 +# define SSL_F_TLS_CONSTRUCT_CTOS_KEY_SHARE 470 +# define SSL_F_TLS_CONSTRUCT_CTOS_MAXFRAGMENTLEN 549 +# define SSL_F_TLS_CONSTRUCT_CTOS_NPN 471 +# define SSL_F_TLS_CONSTRUCT_CTOS_PADDING 472 +# define SSL_F_TLS_CONSTRUCT_CTOS_POST_HANDSHAKE_AUTH 619 +# define SSL_F_TLS_CONSTRUCT_CTOS_PSK 501 +# define SSL_F_TLS_CONSTRUCT_CTOS_PSK_KEX_MODES 509 +# define SSL_F_TLS_CONSTRUCT_CTOS_RENEGOTIATE 473 +# define SSL_F_TLS_CONSTRUCT_CTOS_SCT 474 +# define SSL_F_TLS_CONSTRUCT_CTOS_SERVER_NAME 475 +# define SSL_F_TLS_CONSTRUCT_CTOS_SESSION_TICKET 476 +# define SSL_F_TLS_CONSTRUCT_CTOS_SIG_ALGS 477 +# define SSL_F_TLS_CONSTRUCT_CTOS_SRP 478 +# define SSL_F_TLS_CONSTRUCT_CTOS_STATUS_REQUEST 479 +# define SSL_F_TLS_CONSTRUCT_CTOS_SUPPORTED_GROUPS 480 +# define SSL_F_TLS_CONSTRUCT_CTOS_SUPPORTED_VERSIONS 481 +# define SSL_F_TLS_CONSTRUCT_CTOS_USE_SRTP 482 +# define SSL_F_TLS_CONSTRUCT_CTOS_VERIFY 358 +# define SSL_F_TLS_CONSTRUCT_ENCRYPTED_EXTENSIONS 443 +# define SSL_F_TLS_CONSTRUCT_END_OF_EARLY_DATA 536 +# define SSL_F_TLS_CONSTRUCT_EXTENSIONS 447 +# define SSL_F_TLS_CONSTRUCT_FINISHED 359 +# define SSL_F_TLS_CONSTRUCT_HELLO_REQUEST 373 +# define SSL_F_TLS_CONSTRUCT_HELLO_RETRY_REQUEST 510 +# define SSL_F_TLS_CONSTRUCT_KEY_UPDATE 517 +# define SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET 428 +# define SSL_F_TLS_CONSTRUCT_NEXT_PROTO 426 +# define SSL_F_TLS_CONSTRUCT_SERVER_CERTIFICATE 490 +# define SSL_F_TLS_CONSTRUCT_SERVER_HELLO 491 +# define SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE 492 +# define SSL_F_TLS_CONSTRUCT_STOC_ALPN 451 +# define SSL_F_TLS_CONSTRUCT_STOC_CERTIFICATE 374 +# define SSL_F_TLS_CONSTRUCT_STOC_COOKIE 613 +# define SSL_F_TLS_CONSTRUCT_STOC_CRYPTOPRO_BUG 452 +# define SSL_F_TLS_CONSTRUCT_STOC_DONE 375 +# define SSL_F_TLS_CONSTRUCT_STOC_EARLY_DATA 531 +# define SSL_F_TLS_CONSTRUCT_STOC_EARLY_DATA_INFO 525 +# define SSL_F_TLS_CONSTRUCT_STOC_EC_PT_FORMATS 453 +# define SSL_F_TLS_CONSTRUCT_STOC_EMS 454 +# define SSL_F_TLS_CONSTRUCT_STOC_ETM 455 +# define SSL_F_TLS_CONSTRUCT_STOC_HELLO 376 +# define SSL_F_TLS_CONSTRUCT_STOC_KEY_EXCHANGE 377 +# define SSL_F_TLS_CONSTRUCT_STOC_KEY_SHARE 456 +# define SSL_F_TLS_CONSTRUCT_STOC_MAXFRAGMENTLEN 548 +# define SSL_F_TLS_CONSTRUCT_STOC_NEXT_PROTO_NEG 457 +# define SSL_F_TLS_CONSTRUCT_STOC_PSK 504 +# define SSL_F_TLS_CONSTRUCT_STOC_RENEGOTIATE 458 +# define SSL_F_TLS_CONSTRUCT_STOC_SERVER_NAME 459 +# define SSL_F_TLS_CONSTRUCT_STOC_SESSION_TICKET 460 +# define SSL_F_TLS_CONSTRUCT_STOC_STATUS_REQUEST 461 +# define SSL_F_TLS_CONSTRUCT_STOC_SUPPORTED_GROUPS 544 +# define SSL_F_TLS_CONSTRUCT_STOC_SUPPORTED_VERSIONS 611 +# define SSL_F_TLS_CONSTRUCT_STOC_USE_SRTP 462 +# define SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO 521 +# define SSL_F_TLS_FINISH_HANDSHAKE 597 +# define SSL_F_TLS_GET_MESSAGE_BODY 351 +# define SSL_F_TLS_GET_MESSAGE_HEADER 387 +# define SSL_F_TLS_HANDLE_ALPN 562 +# define SSL_F_TLS_HANDLE_STATUS_REQUEST 563 +# define SSL_F_TLS_PARSE_CERTIFICATE_AUTHORITIES 566 +# define SSL_F_TLS_PARSE_CLIENTHELLO_TLSEXT 449 +# define SSL_F_TLS_PARSE_CTOS_ALPN 567 +# define SSL_F_TLS_PARSE_CTOS_COOKIE 614 +# define SSL_F_TLS_PARSE_CTOS_EARLY_DATA 568 +# define SSL_F_TLS_PARSE_CTOS_EC_PT_FORMATS 569 +# define SSL_F_TLS_PARSE_CTOS_EMS 570 +# define SSL_F_TLS_PARSE_CTOS_KEY_SHARE 463 +# define SSL_F_TLS_PARSE_CTOS_MAXFRAGMENTLEN 571 +# define SSL_F_TLS_PARSE_CTOS_POST_HANDSHAKE_AUTH 620 +# define SSL_F_TLS_PARSE_CTOS_PSK 505 +# define SSL_F_TLS_PARSE_CTOS_PSK_KEX_MODES 572 +# define SSL_F_TLS_PARSE_CTOS_RENEGOTIATE 464 +# define SSL_F_TLS_PARSE_CTOS_SERVER_NAME 573 +# define SSL_F_TLS_PARSE_CTOS_SESSION_TICKET 574 +# define SSL_F_TLS_PARSE_CTOS_SIG_ALGS 575 +# define SSL_F_TLS_PARSE_CTOS_SIG_ALGS_CERT 615 +# define SSL_F_TLS_PARSE_CTOS_SRP 576 +# define SSL_F_TLS_PARSE_CTOS_STATUS_REQUEST 577 +# define SSL_F_TLS_PARSE_CTOS_SUPPORTED_GROUPS 578 +# define SSL_F_TLS_PARSE_CTOS_USE_SRTP 465 +# define SSL_F_TLS_PARSE_STOC_ALPN 579 +# define SSL_F_TLS_PARSE_STOC_COOKIE 534 +# define SSL_F_TLS_PARSE_STOC_EARLY_DATA 538 +# define SSL_F_TLS_PARSE_STOC_EARLY_DATA_INFO 528 +# define SSL_F_TLS_PARSE_STOC_EC_PT_FORMATS 580 +# define SSL_F_TLS_PARSE_STOC_KEY_SHARE 445 +# define SSL_F_TLS_PARSE_STOC_MAXFRAGMENTLEN 581 +# define SSL_F_TLS_PARSE_STOC_NPN 582 +# define SSL_F_TLS_PARSE_STOC_PSK 502 +# define SSL_F_TLS_PARSE_STOC_RENEGOTIATE 448 +# define SSL_F_TLS_PARSE_STOC_SCT 564 +# define SSL_F_TLS_PARSE_STOC_SERVER_NAME 583 +# define SSL_F_TLS_PARSE_STOC_SESSION_TICKET 584 +# define SSL_F_TLS_PARSE_STOC_STATUS_REQUEST 585 +# define SSL_F_TLS_PARSE_STOC_SUPPORTED_VERSIONS 612 +# define SSL_F_TLS_PARSE_STOC_USE_SRTP 446 +# define SSL_F_TLS_POST_PROCESS_CLIENT_HELLO 378 +# define SSL_F_TLS_POST_PROCESS_CLIENT_KEY_EXCHANGE 384 +# define SSL_F_TLS_PREPARE_CLIENT_CERTIFICATE 360 +# define SSL_F_TLS_PROCESS_AS_HELLO_RETRY_REQUEST 610 +# define SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST 361 +# define SSL_F_TLS_PROCESS_CERT_STATUS 362 +# define SSL_F_TLS_PROCESS_CERT_STATUS_BODY 495 +# define SSL_F_TLS_PROCESS_CERT_VERIFY 379 +# define SSL_F_TLS_PROCESS_CHANGE_CIPHER_SPEC 363 +# define SSL_F_TLS_PROCESS_CKE_DHE 411 +# define SSL_F_TLS_PROCESS_CKE_ECDHE 412 +# define SSL_F_TLS_PROCESS_CKE_GOST 413 +# define SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE 414 +# define SSL_F_TLS_PROCESS_CKE_RSA 415 +# define SSL_F_TLS_PROCESS_CKE_SRP 416 +# define SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE 380 +# define SSL_F_TLS_PROCESS_CLIENT_HELLO 381 +# define SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE 382 +# define SSL_F_TLS_PROCESS_ENCRYPTED_EXTENSIONS 444 +# define SSL_F_TLS_PROCESS_END_OF_EARLY_DATA 537 +# define SSL_F_TLS_PROCESS_FINISHED 364 +# define SSL_F_TLS_PROCESS_HELLO_REQ 507 +# define SSL_F_TLS_PROCESS_HELLO_RETRY_REQUEST 511 +# define SSL_F_TLS_PROCESS_INITIAL_SERVER_FLIGHT 442 +# define SSL_F_TLS_PROCESS_KEY_EXCHANGE 365 +# define SSL_F_TLS_PROCESS_KEY_UPDATE 518 +# define SSL_F_TLS_PROCESS_NEW_SESSION_TICKET 366 +# define SSL_F_TLS_PROCESS_NEXT_PROTO 383 +# define SSL_F_TLS_PROCESS_SERVER_CERTIFICATE 367 +# define SSL_F_TLS_PROCESS_SERVER_DONE 368 +# define SSL_F_TLS_PROCESS_SERVER_HELLO 369 +# define SSL_F_TLS_PROCESS_SKE_DHE 419 +# define SSL_F_TLS_PROCESS_SKE_ECDHE 420 +# define SSL_F_TLS_PROCESS_SKE_PSK_PREAMBLE 421 +# define SSL_F_TLS_PROCESS_SKE_SRP 422 +# define SSL_F_TLS_PSK_DO_BINDER 506 +# define SSL_F_TLS_SCAN_CLIENTHELLO_TLSEXT 450 +# define SSL_F_TLS_SETUP_HANDSHAKE 508 +# define SSL_F_USE_CERTIFICATE_CHAIN_FILE 220 +# define SSL_F_WPACKET_INTERN_INIT_LEN 633 +# define SSL_F_WPACKET_START_SUB_PACKET_LEN__ 634 +# define SSL_F_WRITE_STATE_MACHINE 586 + +/* + * SSL reason codes. + */ +# define SSL_R_APPLICATION_DATA_AFTER_CLOSE_NOTIFY 291 +# define SSL_R_APP_DATA_IN_HANDSHAKE 100 +# define SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT 272 +# define SSL_R_AT_LEAST_TLS_1_0_NEEDED_IN_FIPS_MODE 143 +# define SSL_R_AT_LEAST_TLS_1_2_NEEDED_IN_SUITEB_MODE 158 +# define SSL_R_BAD_CHANGE_CIPHER_SPEC 103 +# define SSL_R_BAD_CIPHER 186 +# define SSL_R_BAD_DATA 390 +# define SSL_R_BAD_DATA_RETURNED_BY_CALLBACK 106 +# define SSL_R_BAD_DECOMPRESSION 107 +# define SSL_R_BAD_DH_VALUE 102 +# define SSL_R_BAD_DIGEST_LENGTH 111 +# define SSL_R_BAD_EARLY_DATA 233 +# define SSL_R_BAD_ECC_CERT 304 +# define SSL_R_BAD_ECPOINT 306 +# define SSL_R_BAD_EXTENSION 110 +# define SSL_R_BAD_HANDSHAKE_LENGTH 332 +# define SSL_R_BAD_HANDSHAKE_STATE 236 +# define SSL_R_BAD_HELLO_REQUEST 105 +# define SSL_R_BAD_HRR_VERSION 263 +# define SSL_R_BAD_KEY_SHARE 108 +# define SSL_R_BAD_KEY_UPDATE 122 +# define SSL_R_BAD_LEGACY_VERSION 292 +# define SSL_R_BAD_LENGTH 271 +# define SSL_R_BAD_PACKET 240 +# define SSL_R_BAD_PACKET_LENGTH 115 +# define SSL_R_BAD_PROTOCOL_VERSION_NUMBER 116 +# define SSL_R_BAD_PSK 219 +# define SSL_R_BAD_PSK_IDENTITY 114 +# define SSL_R_BAD_RECORD_TYPE 443 +# define SSL_R_BAD_RSA_ENCRYPT 119 +# define SSL_R_BAD_SIGNATURE 123 +# define SSL_R_BAD_SRP_A_LENGTH 347 +# define SSL_R_BAD_SRP_PARAMETERS 371 +# define SSL_R_BAD_SRTP_MKI_VALUE 352 +# define SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST 353 +# define SSL_R_BAD_SSL_FILETYPE 124 +# define SSL_R_BAD_VALUE 384 +# define SSL_R_BAD_WRITE_RETRY 127 +# define SSL_R_BINDER_DOES_NOT_VERIFY 253 +# define SSL_R_BIO_NOT_SET 128 +# define SSL_R_BLOCK_CIPHER_PAD_IS_WRONG 129 +# define SSL_R_BN_LIB 130 +# define SSL_R_CALLBACK_FAILED 234 +# define SSL_R_CANNOT_CHANGE_CIPHER 109 +# define SSL_R_CA_DN_LENGTH_MISMATCH 131 +# define SSL_R_CA_KEY_TOO_SMALL 397 +# define SSL_R_CA_MD_TOO_WEAK 398 +# define SSL_R_CCS_RECEIVED_EARLY 133 +# define SSL_R_CERTIFICATE_VERIFY_FAILED 134 +# define SSL_R_CERT_CB_ERROR 377 +# define SSL_R_CERT_LENGTH_MISMATCH 135 +# define SSL_R_CIPHERSUITE_DIGEST_HAS_CHANGED 218 +# define SSL_R_CIPHER_CODE_WRONG_LENGTH 137 +# define SSL_R_CIPHER_OR_HASH_UNAVAILABLE 138 +# define SSL_R_CLIENTHELLO_TLSEXT 226 +# define SSL_R_COMPRESSED_LENGTH_TOO_LONG 140 +# define SSL_R_COMPRESSION_DISABLED 343 +# define SSL_R_COMPRESSION_FAILURE 141 +# define SSL_R_COMPRESSION_ID_NOT_WITHIN_PRIVATE_RANGE 307 +# define SSL_R_COMPRESSION_LIBRARY_ERROR 142 +# define SSL_R_CONNECTION_TYPE_NOT_SET 144 +# define SSL_R_CONTEXT_NOT_DANE_ENABLED 167 +# define SSL_R_COOKIE_GEN_CALLBACK_FAILURE 400 +# define SSL_R_COOKIE_MISMATCH 308 +# define SSL_R_CUSTOM_EXT_HANDLER_ALREADY_INSTALLED 206 +# define SSL_R_DANE_ALREADY_ENABLED 172 +# define SSL_R_DANE_CANNOT_OVERRIDE_MTYPE_FULL 173 +# define SSL_R_DANE_NOT_ENABLED 175 +# define SSL_R_DANE_TLSA_BAD_CERTIFICATE 180 +# define SSL_R_DANE_TLSA_BAD_CERTIFICATE_USAGE 184 +# define SSL_R_DANE_TLSA_BAD_DATA_LENGTH 189 +# define SSL_R_DANE_TLSA_BAD_DIGEST_LENGTH 192 +# define SSL_R_DANE_TLSA_BAD_MATCHING_TYPE 200 +# define SSL_R_DANE_TLSA_BAD_PUBLIC_KEY 201 +# define SSL_R_DANE_TLSA_BAD_SELECTOR 202 +# define SSL_R_DANE_TLSA_NULL_DATA 203 +# define SSL_R_DATA_BETWEEN_CCS_AND_FINISHED 145 +# define SSL_R_DATA_LENGTH_TOO_LONG 146 +# define SSL_R_DECRYPTION_FAILED 147 +# define SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC 281 +# define SSL_R_DH_KEY_TOO_SMALL 394 +# define SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG 148 +# define SSL_R_DIGEST_CHECK_FAILED 149 +# define SSL_R_DTLS_MESSAGE_TOO_BIG 334 +# define SSL_R_DUPLICATE_COMPRESSION_ID 309 +# define SSL_R_ECC_CERT_NOT_FOR_SIGNING 318 +# define SSL_R_ECDH_REQUIRED_FOR_SUITEB_MODE 374 +# define SSL_R_EE_KEY_TOO_SMALL 399 +# define SSL_R_EMPTY_SRTP_PROTECTION_PROFILE_LIST 354 +# define SSL_R_ENCRYPTED_LENGTH_TOO_LONG 150 +# define SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST 151 +# define SSL_R_ERROR_SETTING_TLSA_BASE_DOMAIN 204 +# define SSL_R_EXCEEDS_MAX_FRAGMENT_SIZE 194 +# define SSL_R_EXCESSIVE_MESSAGE_SIZE 152 +# define SSL_R_EXTENSION_NOT_RECEIVED 279 +# define SSL_R_EXTRA_DATA_IN_MESSAGE 153 +# define SSL_R_EXT_LENGTH_MISMATCH 163 +# define SSL_R_FAILED_TO_INIT_ASYNC 405 +# define SSL_R_FRAGMENTED_CLIENT_HELLO 401 +# define SSL_R_GOT_A_FIN_BEFORE_A_CCS 154 +# define SSL_R_HTTPS_PROXY_REQUEST 155 +# define SSL_R_HTTP_REQUEST 156 +# define SSL_R_ILLEGAL_POINT_COMPRESSION 162 +# define SSL_R_ILLEGAL_SUITEB_DIGEST 380 +# define SSL_R_INAPPROPRIATE_FALLBACK 373 +# define SSL_R_INCONSISTENT_COMPRESSION 340 +# define SSL_R_INCONSISTENT_EARLY_DATA_ALPN 222 +# define SSL_R_INCONSISTENT_EARLY_DATA_SNI 231 +# define SSL_R_INCONSISTENT_EXTMS 104 +# define SSL_R_INSUFFICIENT_SECURITY 241 +# define SSL_R_INVALID_ALERT 205 +# define SSL_R_INVALID_CCS_MESSAGE 260 +# define SSL_R_INVALID_CERTIFICATE_OR_ALG 238 +# define SSL_R_INVALID_COMMAND 280 +# define SSL_R_INVALID_COMPRESSION_ALGORITHM 341 +# define SSL_R_INVALID_CONFIG 283 +# define SSL_R_INVALID_CONFIGURATION_NAME 113 +# define SSL_R_INVALID_CONTEXT 282 +# define SSL_R_INVALID_CT_VALIDATION_TYPE 212 +# define SSL_R_INVALID_KEY_UPDATE_TYPE 120 +# define SSL_R_INVALID_MAX_EARLY_DATA 174 +# define SSL_R_INVALID_NULL_CMD_NAME 385 +# define SSL_R_INVALID_SEQUENCE_NUMBER 402 +# define SSL_R_INVALID_SERVERINFO_DATA 388 +# define SSL_R_INVALID_SESSION_ID 999 +# define SSL_R_INVALID_SRP_USERNAME 357 +# define SSL_R_INVALID_STATUS_RESPONSE 328 +# define SSL_R_INVALID_TICKET_KEYS_LENGTH 325 +# define SSL_R_LENGTH_MISMATCH 159 +# define SSL_R_LENGTH_TOO_LONG 404 +# define SSL_R_LENGTH_TOO_SHORT 160 +# define SSL_R_LIBRARY_BUG 274 +# define SSL_R_LIBRARY_HAS_NO_CIPHERS 161 +# define SSL_R_MISSING_DSA_SIGNING_CERT 165 +# define SSL_R_MISSING_ECDSA_SIGNING_CERT 381 +# define SSL_R_MISSING_FATAL 256 +# define SSL_R_MISSING_PARAMETERS 290 +# define SSL_R_MISSING_RSA_CERTIFICATE 168 +# define SSL_R_MISSING_RSA_ENCRYPTING_CERT 169 +# define SSL_R_MISSING_RSA_SIGNING_CERT 170 +# define SSL_R_MISSING_SIGALGS_EXTENSION 112 +# define SSL_R_MISSING_SIGNING_CERT 221 +# define SSL_R_MISSING_SRP_PARAM 358 +# define SSL_R_MISSING_SUPPORTED_GROUPS_EXTENSION 209 +# define SSL_R_MISSING_TMP_DH_KEY 171 +# define SSL_R_MISSING_TMP_ECDH_KEY 311 +# define SSL_R_NOT_ON_RECORD_BOUNDARY 182 +# define SSL_R_NOT_REPLACING_CERTIFICATE 289 +# define SSL_R_NOT_SERVER 284 +# define SSL_R_NO_APPLICATION_PROTOCOL 235 +# define SSL_R_NO_CERTIFICATES_RETURNED 176 +# define SSL_R_NO_CERTIFICATE_ASSIGNED 177 +# define SSL_R_NO_CERTIFICATE_SET 179 +# define SSL_R_NO_CHANGE_FOLLOWING_HRR 214 +# define SSL_R_NO_CIPHERS_AVAILABLE 181 +# define SSL_R_NO_CIPHERS_SPECIFIED 183 +# define SSL_R_NO_CIPHER_MATCH 185 +# define SSL_R_NO_CLIENT_CERT_METHOD 331 +# define SSL_R_NO_COMPRESSION_SPECIFIED 187 +# define SSL_R_NO_COOKIE_CALLBACK_SET 287 +# define SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER 330 +# define SSL_R_NO_METHOD_SPECIFIED 188 +# define SSL_R_NO_PEM_EXTENSIONS 389 +# define SSL_R_NO_PRIVATE_KEY_ASSIGNED 190 +# define SSL_R_NO_PROTOCOLS_AVAILABLE 191 +# define SSL_R_NO_RENEGOTIATION 339 +# define SSL_R_NO_REQUIRED_DIGEST 324 +# define SSL_R_NO_SHARED_CIPHER 193 +# define SSL_R_NO_SHARED_GROUPS 410 +# define SSL_R_NO_SHARED_SIGNATURE_ALGORITHMS 376 +# define SSL_R_NO_SRTP_PROFILES 359 +# define SSL_R_NO_SUITABLE_KEY_SHARE 101 +# define SSL_R_NO_SUITABLE_SIGNATURE_ALGORITHM 118 +# define SSL_R_NO_VALID_SCTS 216 +# define SSL_R_NO_VERIFY_COOKIE_CALLBACK 403 +# define SSL_R_NULL_SSL_CTX 195 +# define SSL_R_NULL_SSL_METHOD_PASSED 196 +# define SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED 197 +# define SSL_R_OLD_SESSION_COMPRESSION_ALGORITHM_NOT_RETURNED 344 +# define SSL_R_OVERFLOW_ERROR 237 +# define SSL_R_PACKET_LENGTH_TOO_LONG 198 +# define SSL_R_PARSE_TLSEXT 227 +# define SSL_R_PATH_TOO_LONG 270 +# define SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE 199 +# define SSL_R_PEM_NAME_BAD_PREFIX 391 +# define SSL_R_PEM_NAME_TOO_SHORT 392 +# define SSL_R_PIPELINE_FAILURE 406 +# define SSL_R_POST_HANDSHAKE_AUTH_ENCODING_ERR 278 +# define SSL_R_PRIVATE_KEY_MISMATCH 288 +# define SSL_R_PROTOCOL_IS_SHUTDOWN 207 +# define SSL_R_PSK_IDENTITY_NOT_FOUND 223 +# define SSL_R_PSK_NO_CLIENT_CB 224 +# define SSL_R_PSK_NO_SERVER_CB 225 +# define SSL_R_READ_BIO_NOT_SET 211 +# define SSL_R_READ_TIMEOUT_EXPIRED 312 +# define SSL_R_RECORD_LENGTH_MISMATCH 213 +# define SSL_R_RECORD_TOO_SMALL 298 +# define SSL_R_RENEGOTIATE_EXT_TOO_LONG 335 +# define SSL_R_RENEGOTIATION_ENCODING_ERR 336 +# define SSL_R_RENEGOTIATION_MISMATCH 337 +# define SSL_R_REQUEST_PENDING 285 +# define SSL_R_REQUEST_SENT 286 +# define SSL_R_REQUIRED_CIPHER_MISSING 215 +# define SSL_R_REQUIRED_COMPRESSION_ALGORITHM_MISSING 342 +# define SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING 345 +# define SSL_R_SCT_VERIFICATION_FAILED 208 +# define SSL_R_SERVERHELLO_TLSEXT 275 +# define SSL_R_SESSION_ID_CONTEXT_UNINITIALIZED 277 +# define SSL_R_SHUTDOWN_WHILE_IN_INIT 407 +# define SSL_R_SIGNATURE_ALGORITHMS_ERROR 360 +# define SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE 220 +# define SSL_R_SRP_A_CALC 361 +# define SSL_R_SRTP_COULD_NOT_ALLOCATE_PROFILES 362 +# define SSL_R_SRTP_PROTECTION_PROFILE_LIST_TOO_LONG 363 +# define SSL_R_SRTP_UNKNOWN_PROTECTION_PROFILE 364 +# define SSL_R_SSL3_EXT_INVALID_MAX_FRAGMENT_LENGTH 232 +# define SSL_R_SSL3_EXT_INVALID_SERVERNAME 319 +# define SSL_R_SSL3_EXT_INVALID_SERVERNAME_TYPE 320 +# define SSL_R_SSL3_SESSION_ID_TOO_LONG 300 +# define SSL_R_SSLV3_ALERT_BAD_CERTIFICATE 1042 +# define SSL_R_SSLV3_ALERT_BAD_RECORD_MAC 1020 +# define SSL_R_SSLV3_ALERT_CERTIFICATE_EXPIRED 1045 +# define SSL_R_SSLV3_ALERT_CERTIFICATE_REVOKED 1044 +# define SSL_R_SSLV3_ALERT_CERTIFICATE_UNKNOWN 1046 +# define SSL_R_SSLV3_ALERT_DECOMPRESSION_FAILURE 1030 +# define SSL_R_SSLV3_ALERT_HANDSHAKE_FAILURE 1040 +# define SSL_R_SSLV3_ALERT_ILLEGAL_PARAMETER 1047 +# define SSL_R_SSLV3_ALERT_NO_CERTIFICATE 1041 +# define SSL_R_SSLV3_ALERT_UNEXPECTED_MESSAGE 1010 +# define SSL_R_SSLV3_ALERT_UNSUPPORTED_CERTIFICATE 1043 +# define SSL_R_SSL_COMMAND_SECTION_EMPTY 117 +# define SSL_R_SSL_COMMAND_SECTION_NOT_FOUND 125 +# define SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION 228 +# define SSL_R_SSL_HANDSHAKE_FAILURE 229 +# define SSL_R_SSL_LIBRARY_HAS_NO_CIPHERS 230 +# define SSL_R_SSL_NEGATIVE_LENGTH 372 +# define SSL_R_SSL_SECTION_EMPTY 126 +# define SSL_R_SSL_SECTION_NOT_FOUND 136 +# define SSL_R_SSL_SESSION_ID_CALLBACK_FAILED 301 +# define SSL_R_SSL_SESSION_ID_CONFLICT 302 +# define SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG 273 +# define SSL_R_SSL_SESSION_ID_HAS_BAD_LENGTH 303 +# define SSL_R_SSL_SESSION_ID_TOO_LONG 408 +# define SSL_R_SSL_SESSION_VERSION_MISMATCH 210 +# define SSL_R_STILL_IN_INIT 121 +# define SSL_R_TLSV13_ALERT_CERTIFICATE_REQUIRED 1116 +# define SSL_R_TLSV13_ALERT_MISSING_EXTENSION 1109 +# define SSL_R_TLSV1_ALERT_ACCESS_DENIED 1049 +# define SSL_R_TLSV1_ALERT_DECODE_ERROR 1050 +# define SSL_R_TLSV1_ALERT_DECRYPTION_FAILED 1021 +# define SSL_R_TLSV1_ALERT_DECRYPT_ERROR 1051 +# define SSL_R_TLSV1_ALERT_EXPORT_RESTRICTION 1060 +# define SSL_R_TLSV1_ALERT_INAPPROPRIATE_FALLBACK 1086 +# define SSL_R_TLSV1_ALERT_INSUFFICIENT_SECURITY 1071 +# define SSL_R_TLSV1_ALERT_INTERNAL_ERROR 1080 +# define SSL_R_TLSV1_ALERT_NO_RENEGOTIATION 1100 +# define SSL_R_TLSV1_ALERT_PROTOCOL_VERSION 1070 +# define SSL_R_TLSV1_ALERT_RECORD_OVERFLOW 1022 +# define SSL_R_TLSV1_ALERT_UNKNOWN_CA 1048 +# define SSL_R_TLSV1_ALERT_USER_CANCELLED 1090 +# define SSL_R_TLSV1_BAD_CERTIFICATE_HASH_VALUE 1114 +# define SSL_R_TLSV1_BAD_CERTIFICATE_STATUS_RESPONSE 1113 +# define SSL_R_TLSV1_CERTIFICATE_UNOBTAINABLE 1111 +# define SSL_R_TLSV1_UNRECOGNIZED_NAME 1112 +# define SSL_R_TLSV1_UNSUPPORTED_EXTENSION 1110 +# define SSL_R_TLS_HEARTBEAT_PEER_DOESNT_ACCEPT 365 +# define SSL_R_TLS_HEARTBEAT_PENDING 366 +# define SSL_R_TLS_ILLEGAL_EXPORTER_LABEL 367 +# define SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST 157 +# define SSL_R_TOO_MANY_KEY_UPDATES 132 +# define SSL_R_TOO_MANY_WARN_ALERTS 409 +# define SSL_R_TOO_MUCH_EARLY_DATA 164 +# define SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS 314 +# define SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS 239 +# define SSL_R_UNABLE_TO_LOAD_SSL3_MD5_ROUTINES 242 +# define SSL_R_UNABLE_TO_LOAD_SSL3_SHA1_ROUTINES 243 +# define SSL_R_UNEXPECTED_CCS_MESSAGE 262 +# define SSL_R_UNEXPECTED_END_OF_EARLY_DATA 178 +# define SSL_R_UNEXPECTED_MESSAGE 244 +# define SSL_R_UNEXPECTED_RECORD 245 +# define SSL_R_UNINITIALIZED 276 +# define SSL_R_UNKNOWN_ALERT_TYPE 246 +# define SSL_R_UNKNOWN_CERTIFICATE_TYPE 247 +# define SSL_R_UNKNOWN_CIPHER_RETURNED 248 +# define SSL_R_UNKNOWN_CIPHER_TYPE 249 +# define SSL_R_UNKNOWN_CMD_NAME 386 +# define SSL_R_UNKNOWN_COMMAND 139 +# define SSL_R_UNKNOWN_DIGEST 368 +# define SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE 250 +# define SSL_R_UNKNOWN_PKEY_TYPE 251 +# define SSL_R_UNKNOWN_PROTOCOL 252 +# define SSL_R_UNKNOWN_SSL_VERSION 254 +# define SSL_R_UNKNOWN_STATE 255 +# define SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED 338 +# define SSL_R_UNSOLICITED_EXTENSION 217 +# define SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM 257 +# define SSL_R_UNSUPPORTED_ELLIPTIC_CURVE 315 +# define SSL_R_UNSUPPORTED_PROTOCOL 258 +# define SSL_R_UNSUPPORTED_SSL_VERSION 259 +# define SSL_R_UNSUPPORTED_STATUS_TYPE 329 +# define SSL_R_USE_SRTP_NOT_NEGOTIATED 369 +# define SSL_R_VERSION_TOO_HIGH 166 +# define SSL_R_VERSION_TOO_LOW 396 +# define SSL_R_WRONG_CERTIFICATE_TYPE 383 +# define SSL_R_WRONG_CIPHER_RETURNED 261 +# define SSL_R_WRONG_CURVE 378 +# define SSL_R_WRONG_SIGNATURE_LENGTH 264 +# define SSL_R_WRONG_SIGNATURE_SIZE 265 +# define SSL_R_WRONG_SIGNATURE_TYPE 370 +# define SSL_R_WRONG_SSL_VERSION 266 +# define SSL_R_WRONG_VERSION_NUMBER 267 +# define SSL_R_X509_LIB 268 +# define SSL_R_X509_VERIFICATION_SETUP_PROBLEMS 269 + +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/stack.h b/example/android/third_party/crypto/arm/include/openssl/stack.h new file mode 100644 index 00000000..cfc07505 --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/stack.h @@ -0,0 +1,83 @@ +/* + * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_STACK_H +# define HEADER_STACK_H + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct stack_st OPENSSL_STACK; /* Use STACK_OF(...) instead */ + +typedef int (*OPENSSL_sk_compfunc)(const void *, const void *); +typedef void (*OPENSSL_sk_freefunc)(void *); +typedef void *(*OPENSSL_sk_copyfunc)(const void *); + +int OPENSSL_sk_num(const OPENSSL_STACK *); +void *OPENSSL_sk_value(const OPENSSL_STACK *, int); + +void *OPENSSL_sk_set(OPENSSL_STACK *st, int i, const void *data); + +OPENSSL_STACK *OPENSSL_sk_new(OPENSSL_sk_compfunc cmp); +OPENSSL_STACK *OPENSSL_sk_new_null(void); +OPENSSL_STACK *OPENSSL_sk_new_reserve(OPENSSL_sk_compfunc c, int n); +int OPENSSL_sk_reserve(OPENSSL_STACK *st, int n); +void OPENSSL_sk_free(OPENSSL_STACK *); +void OPENSSL_sk_pop_free(OPENSSL_STACK *st, void (*func) (void *)); +OPENSSL_STACK *OPENSSL_sk_deep_copy(const OPENSSL_STACK *, + OPENSSL_sk_copyfunc c, + OPENSSL_sk_freefunc f); +int OPENSSL_sk_insert(OPENSSL_STACK *sk, const void *data, int where); +void *OPENSSL_sk_delete(OPENSSL_STACK *st, int loc); +void *OPENSSL_sk_delete_ptr(OPENSSL_STACK *st, const void *p); +int OPENSSL_sk_find(OPENSSL_STACK *st, const void *data); +int OPENSSL_sk_find_ex(OPENSSL_STACK *st, const void *data); +int OPENSSL_sk_push(OPENSSL_STACK *st, const void *data); +int OPENSSL_sk_unshift(OPENSSL_STACK *st, const void *data); +void *OPENSSL_sk_shift(OPENSSL_STACK *st); +void *OPENSSL_sk_pop(OPENSSL_STACK *st); +void OPENSSL_sk_zero(OPENSSL_STACK *st); +OPENSSL_sk_compfunc OPENSSL_sk_set_cmp_func(OPENSSL_STACK *sk, + OPENSSL_sk_compfunc cmp); +OPENSSL_STACK *OPENSSL_sk_dup(const OPENSSL_STACK *st); +void OPENSSL_sk_sort(OPENSSL_STACK *st); +int OPENSSL_sk_is_sorted(const OPENSSL_STACK *st); + +# if OPENSSL_API_COMPAT < 0x10100000L +# define _STACK OPENSSL_STACK +# define sk_num OPENSSL_sk_num +# define sk_value OPENSSL_sk_value +# define sk_set OPENSSL_sk_set +# define sk_new OPENSSL_sk_new +# define sk_new_null OPENSSL_sk_new_null +# define sk_free OPENSSL_sk_free +# define sk_pop_free OPENSSL_sk_pop_free +# define sk_deep_copy OPENSSL_sk_deep_copy +# define sk_insert OPENSSL_sk_insert +# define sk_delete OPENSSL_sk_delete +# define sk_delete_ptr OPENSSL_sk_delete_ptr +# define sk_find OPENSSL_sk_find +# define sk_find_ex OPENSSL_sk_find_ex +# define sk_push OPENSSL_sk_push +# define sk_unshift OPENSSL_sk_unshift +# define sk_shift OPENSSL_sk_shift +# define sk_pop OPENSSL_sk_pop +# define sk_zero OPENSSL_sk_zero +# define sk_set_cmp_func OPENSSL_sk_set_cmp_func +# define sk_dup OPENSSL_sk_dup +# define sk_sort OPENSSL_sk_sort +# define sk_is_sorted OPENSSL_sk_is_sorted +# endif + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/store.h b/example/android/third_party/crypto/arm/include/openssl/store.h new file mode 100644 index 00000000..7b43e8bd --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/store.h @@ -0,0 +1,266 @@ +/* + * Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_OSSL_STORE_H +# define HEADER_OSSL_STORE_H + +# include +# include +# include +# include + +# ifdef __cplusplus +extern "C" { +# endif + +/*- + * The main OSSL_STORE functions. + * ------------------------------ + * + * These allow applications to open a channel to a resource with supported + * data (keys, certs, crls, ...), read the data a piece at a time and decide + * what to do with it, and finally close. + */ + +typedef struct ossl_store_ctx_st OSSL_STORE_CTX; + +/* + * Typedef for the OSSL_STORE_INFO post processing callback. This can be used + * to massage the given OSSL_STORE_INFO, or to drop it entirely (by returning + * NULL). + */ +typedef OSSL_STORE_INFO *(*OSSL_STORE_post_process_info_fn)(OSSL_STORE_INFO *, + void *); + +/* + * Open a channel given a URI. The given UI method will be used any time the + * loader needs extra input, for example when a password or pin is needed, and + * will be passed the same user data every time it's needed in this context. + * + * Returns a context reference which represents the channel to communicate + * through. + */ +OSSL_STORE_CTX *OSSL_STORE_open(const char *uri, const UI_METHOD *ui_method, + void *ui_data, + OSSL_STORE_post_process_info_fn post_process, + void *post_process_data); + +/* + * Control / fine tune the OSSL_STORE channel. |cmd| determines what is to be + * done, and depends on the underlying loader (use OSSL_STORE_get0_scheme to + * determine which loader is used), except for common commands (see below). + * Each command takes different arguments. + */ +int OSSL_STORE_ctrl(OSSL_STORE_CTX *ctx, int cmd, ... /* args */); +int OSSL_STORE_vctrl(OSSL_STORE_CTX *ctx, int cmd, va_list args); + +/* + * Common ctrl commands that different loaders may choose to support. + */ +/* int on = 0 or 1; STORE_ctrl(ctx, STORE_C_USE_SECMEM, &on); */ +# define OSSL_STORE_C_USE_SECMEM 1 +/* Where custom commands start */ +# define OSSL_STORE_C_CUSTOM_START 100 + +/* + * Read one data item (a key, a cert, a CRL) that is supported by the OSSL_STORE + * functionality, given a context. + * Returns a OSSL_STORE_INFO pointer, from which OpenSSL typed data can be + * extracted with OSSL_STORE_INFO_get0_PKEY(), OSSL_STORE_INFO_get0_CERT(), ... + * NULL is returned on error, which may include that the data found at the URI + * can't be figured out for certain or is ambiguous. + */ +OSSL_STORE_INFO *OSSL_STORE_load(OSSL_STORE_CTX *ctx); + +/* + * Check if end of data (end of file) is reached + * Returns 1 on end, 0 otherwise. + */ +int OSSL_STORE_eof(OSSL_STORE_CTX *ctx); + +/* + * Check if an error occured + * Returns 1 if it did, 0 otherwise. + */ +int OSSL_STORE_error(OSSL_STORE_CTX *ctx); + +/* + * Close the channel + * Returns 1 on success, 0 on error. + */ +int OSSL_STORE_close(OSSL_STORE_CTX *ctx); + + +/*- + * Extracting OpenSSL types from and creating new OSSL_STORE_INFOs + * --------------------------------------------------------------- + */ + +/* + * Types of data that can be ossl_stored in a OSSL_STORE_INFO. + * OSSL_STORE_INFO_NAME is typically found when getting a listing of + * available "files" / "tokens" / what have you. + */ +# define OSSL_STORE_INFO_NAME 1 /* char * */ +# define OSSL_STORE_INFO_PARAMS 2 /* EVP_PKEY * */ +# define OSSL_STORE_INFO_PKEY 3 /* EVP_PKEY * */ +# define OSSL_STORE_INFO_CERT 4 /* X509 * */ +# define OSSL_STORE_INFO_CRL 5 /* X509_CRL * */ + +/* + * Functions to generate OSSL_STORE_INFOs, one function for each type we + * support having in them, as well as a generic constructor. + * + * In all cases, ownership of the object is transfered to the OSSL_STORE_INFO + * and will therefore be freed when the OSSL_STORE_INFO is freed. + */ +OSSL_STORE_INFO *OSSL_STORE_INFO_new_NAME(char *name); +int OSSL_STORE_INFO_set0_NAME_description(OSSL_STORE_INFO *info, char *desc); +OSSL_STORE_INFO *OSSL_STORE_INFO_new_PARAMS(EVP_PKEY *params); +OSSL_STORE_INFO *OSSL_STORE_INFO_new_PKEY(EVP_PKEY *pkey); +OSSL_STORE_INFO *OSSL_STORE_INFO_new_CERT(X509 *x509); +OSSL_STORE_INFO *OSSL_STORE_INFO_new_CRL(X509_CRL *crl); + +/* + * Functions to try to extract data from a OSSL_STORE_INFO. + */ +int OSSL_STORE_INFO_get_type(const OSSL_STORE_INFO *info); +const char *OSSL_STORE_INFO_get0_NAME(const OSSL_STORE_INFO *info); +char *OSSL_STORE_INFO_get1_NAME(const OSSL_STORE_INFO *info); +const char *OSSL_STORE_INFO_get0_NAME_description(const OSSL_STORE_INFO *info); +char *OSSL_STORE_INFO_get1_NAME_description(const OSSL_STORE_INFO *info); +EVP_PKEY *OSSL_STORE_INFO_get0_PARAMS(const OSSL_STORE_INFO *info); +EVP_PKEY *OSSL_STORE_INFO_get1_PARAMS(const OSSL_STORE_INFO *info); +EVP_PKEY *OSSL_STORE_INFO_get0_PKEY(const OSSL_STORE_INFO *info); +EVP_PKEY *OSSL_STORE_INFO_get1_PKEY(const OSSL_STORE_INFO *info); +X509 *OSSL_STORE_INFO_get0_CERT(const OSSL_STORE_INFO *info); +X509 *OSSL_STORE_INFO_get1_CERT(const OSSL_STORE_INFO *info); +X509_CRL *OSSL_STORE_INFO_get0_CRL(const OSSL_STORE_INFO *info); +X509_CRL *OSSL_STORE_INFO_get1_CRL(const OSSL_STORE_INFO *info); + +const char *OSSL_STORE_INFO_type_string(int type); + +/* + * Free the OSSL_STORE_INFO + */ +void OSSL_STORE_INFO_free(OSSL_STORE_INFO *info); + + +/*- + * Functions to construct a search URI from a base URI and search criteria + * ----------------------------------------------------------------------- + */ + +/* OSSL_STORE search types */ +# define OSSL_STORE_SEARCH_BY_NAME 1 /* subject in certs, issuer in CRLs */ +# define OSSL_STORE_SEARCH_BY_ISSUER_SERIAL 2 +# define OSSL_STORE_SEARCH_BY_KEY_FINGERPRINT 3 +# define OSSL_STORE_SEARCH_BY_ALIAS 4 + +/* To check what search types the scheme handler supports */ +int OSSL_STORE_supports_search(OSSL_STORE_CTX *ctx, int search_type); + +/* Search term constructors */ +/* + * The input is considered to be owned by the caller, and must therefore + * remain present throughout the lifetime of the returned OSSL_STORE_SEARCH + */ +OSSL_STORE_SEARCH *OSSL_STORE_SEARCH_by_name(X509_NAME *name); +OSSL_STORE_SEARCH *OSSL_STORE_SEARCH_by_issuer_serial(X509_NAME *name, + const ASN1_INTEGER + *serial); +OSSL_STORE_SEARCH *OSSL_STORE_SEARCH_by_key_fingerprint(const EVP_MD *digest, + const unsigned char + *bytes, size_t len); +OSSL_STORE_SEARCH *OSSL_STORE_SEARCH_by_alias(const char *alias); + +/* Search term destructor */ +void OSSL_STORE_SEARCH_free(OSSL_STORE_SEARCH *search); + +/* Search term accessors */ +int OSSL_STORE_SEARCH_get_type(const OSSL_STORE_SEARCH *criterion); +X509_NAME *OSSL_STORE_SEARCH_get0_name(OSSL_STORE_SEARCH *criterion); +const ASN1_INTEGER *OSSL_STORE_SEARCH_get0_serial(const OSSL_STORE_SEARCH + *criterion); +const unsigned char *OSSL_STORE_SEARCH_get0_bytes(const OSSL_STORE_SEARCH + *criterion, size_t *length); +const char *OSSL_STORE_SEARCH_get0_string(const OSSL_STORE_SEARCH *criterion); +const EVP_MD *OSSL_STORE_SEARCH_get0_digest(const OSSL_STORE_SEARCH *criterion); + +/* + * Add search criterion and expected return type (which can be unspecified) + * to the loading channel. This MUST happen before the first OSSL_STORE_load(). + */ +int OSSL_STORE_expect(OSSL_STORE_CTX *ctx, int expected_type); +int OSSL_STORE_find(OSSL_STORE_CTX *ctx, OSSL_STORE_SEARCH *search); + + +/*- + * Function to register a loader for the given URI scheme. + * ------------------------------------------------------- + * + * The loader receives all the main components of an URI except for the + * scheme. + */ + +typedef struct ossl_store_loader_st OSSL_STORE_LOADER; +OSSL_STORE_LOADER *OSSL_STORE_LOADER_new(ENGINE *e, const char *scheme); +const ENGINE *OSSL_STORE_LOADER_get0_engine(const OSSL_STORE_LOADER *loader); +const char *OSSL_STORE_LOADER_get0_scheme(const OSSL_STORE_LOADER *loader); +/* struct ossl_store_loader_ctx_st is defined differently by each loader */ +typedef struct ossl_store_loader_ctx_st OSSL_STORE_LOADER_CTX; +typedef OSSL_STORE_LOADER_CTX *(*OSSL_STORE_open_fn)(const OSSL_STORE_LOADER + *loader, + const char *uri, + const UI_METHOD *ui_method, + void *ui_data); +int OSSL_STORE_LOADER_set_open(OSSL_STORE_LOADER *loader, + OSSL_STORE_open_fn open_function); +typedef int (*OSSL_STORE_ctrl_fn)(OSSL_STORE_LOADER_CTX *ctx, int cmd, + va_list args); +int OSSL_STORE_LOADER_set_ctrl(OSSL_STORE_LOADER *loader, + OSSL_STORE_ctrl_fn ctrl_function); +typedef int (*OSSL_STORE_expect_fn)(OSSL_STORE_LOADER_CTX *ctx, int expected); +int OSSL_STORE_LOADER_set_expect(OSSL_STORE_LOADER *loader, + OSSL_STORE_expect_fn expect_function); +typedef int (*OSSL_STORE_find_fn)(OSSL_STORE_LOADER_CTX *ctx, + OSSL_STORE_SEARCH *criteria); +int OSSL_STORE_LOADER_set_find(OSSL_STORE_LOADER *loader, + OSSL_STORE_find_fn find_function); +typedef OSSL_STORE_INFO *(*OSSL_STORE_load_fn)(OSSL_STORE_LOADER_CTX *ctx, + const UI_METHOD *ui_method, + void *ui_data); +int OSSL_STORE_LOADER_set_load(OSSL_STORE_LOADER *loader, + OSSL_STORE_load_fn load_function); +typedef int (*OSSL_STORE_eof_fn)(OSSL_STORE_LOADER_CTX *ctx); +int OSSL_STORE_LOADER_set_eof(OSSL_STORE_LOADER *loader, + OSSL_STORE_eof_fn eof_function); +typedef int (*OSSL_STORE_error_fn)(OSSL_STORE_LOADER_CTX *ctx); +int OSSL_STORE_LOADER_set_error(OSSL_STORE_LOADER *loader, + OSSL_STORE_error_fn error_function); +typedef int (*OSSL_STORE_close_fn)(OSSL_STORE_LOADER_CTX *ctx); +int OSSL_STORE_LOADER_set_close(OSSL_STORE_LOADER *loader, + OSSL_STORE_close_fn close_function); +void OSSL_STORE_LOADER_free(OSSL_STORE_LOADER *loader); + +int OSSL_STORE_register_loader(OSSL_STORE_LOADER *loader); +OSSL_STORE_LOADER *OSSL_STORE_unregister_loader(const char *scheme); + +/*- + * Functions to list STORE loaders + * ------------------------------- + */ +int OSSL_STORE_do_all_loaders(void (*do_function) (const OSSL_STORE_LOADER + *loader, void *do_arg), + void *do_arg); + +# ifdef __cplusplus +} +# endif +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/storeerr.h b/example/android/third_party/crypto/arm/include/openssl/storeerr.h new file mode 100644 index 00000000..33d0ab79 --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/storeerr.h @@ -0,0 +1,87 @@ +/* + * Generated by util/mkerr.pl DO NOT EDIT + * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_OSSL_STOREERR_H +# define HEADER_OSSL_STOREERR_H + +# ifdef __cplusplus +extern "C" +# endif +int ERR_load_OSSL_STORE_strings(void); + +/* + * OSSL_STORE function codes. + */ +# define OSSL_STORE_F_FILE_CTRL 129 +# define OSSL_STORE_F_FILE_FIND 138 +# define OSSL_STORE_F_FILE_GET_PASS 118 +# define OSSL_STORE_F_FILE_LOAD 119 +# define OSSL_STORE_F_FILE_LOAD_TRY_DECODE 124 +# define OSSL_STORE_F_FILE_NAME_TO_URI 126 +# define OSSL_STORE_F_FILE_OPEN 120 +# define OSSL_STORE_F_OSSL_STORE_ATTACH_PEM_BIO 127 +# define OSSL_STORE_F_OSSL_STORE_EXPECT 130 +# define OSSL_STORE_F_OSSL_STORE_FILE_ATTACH_PEM_BIO_INT 128 +# define OSSL_STORE_F_OSSL_STORE_FIND 131 +# define OSSL_STORE_F_OSSL_STORE_GET0_LOADER_INT 100 +# define OSSL_STORE_F_OSSL_STORE_INFO_GET1_CERT 101 +# define OSSL_STORE_F_OSSL_STORE_INFO_GET1_CRL 102 +# define OSSL_STORE_F_OSSL_STORE_INFO_GET1_NAME 103 +# define OSSL_STORE_F_OSSL_STORE_INFO_GET1_NAME_DESCRIPTION 135 +# define OSSL_STORE_F_OSSL_STORE_INFO_GET1_PARAMS 104 +# define OSSL_STORE_F_OSSL_STORE_INFO_GET1_PKEY 105 +# define OSSL_STORE_F_OSSL_STORE_INFO_NEW_CERT 106 +# define OSSL_STORE_F_OSSL_STORE_INFO_NEW_CRL 107 +# define OSSL_STORE_F_OSSL_STORE_INFO_NEW_EMBEDDED 123 +# define OSSL_STORE_F_OSSL_STORE_INFO_NEW_NAME 109 +# define OSSL_STORE_F_OSSL_STORE_INFO_NEW_PARAMS 110 +# define OSSL_STORE_F_OSSL_STORE_INFO_NEW_PKEY 111 +# define OSSL_STORE_F_OSSL_STORE_INFO_SET0_NAME_DESCRIPTION 134 +# define OSSL_STORE_F_OSSL_STORE_INIT_ONCE 112 +# define OSSL_STORE_F_OSSL_STORE_LOADER_NEW 113 +# define OSSL_STORE_F_OSSL_STORE_OPEN 114 +# define OSSL_STORE_F_OSSL_STORE_OPEN_INT 115 +# define OSSL_STORE_F_OSSL_STORE_REGISTER_LOADER_INT 117 +# define OSSL_STORE_F_OSSL_STORE_SEARCH_BY_ALIAS 132 +# define OSSL_STORE_F_OSSL_STORE_SEARCH_BY_ISSUER_SERIAL 133 +# define OSSL_STORE_F_OSSL_STORE_SEARCH_BY_KEY_FINGERPRINT 136 +# define OSSL_STORE_F_OSSL_STORE_SEARCH_BY_NAME 137 +# define OSSL_STORE_F_OSSL_STORE_UNREGISTER_LOADER_INT 116 +# define OSSL_STORE_F_TRY_DECODE_PARAMS 121 +# define OSSL_STORE_F_TRY_DECODE_PKCS12 122 +# define OSSL_STORE_F_TRY_DECODE_PKCS8ENCRYPTED 125 + +/* + * OSSL_STORE reason codes. + */ +# define OSSL_STORE_R_AMBIGUOUS_CONTENT_TYPE 107 +# define OSSL_STORE_R_BAD_PASSWORD_READ 115 +# define OSSL_STORE_R_ERROR_VERIFYING_PKCS12_MAC 113 +# define OSSL_STORE_R_FINGERPRINT_SIZE_DOES_NOT_MATCH_DIGEST 121 +# define OSSL_STORE_R_INVALID_SCHEME 106 +# define OSSL_STORE_R_IS_NOT_A 112 +# define OSSL_STORE_R_LOADER_INCOMPLETE 116 +# define OSSL_STORE_R_LOADING_STARTED 117 +# define OSSL_STORE_R_NOT_A_CERTIFICATE 100 +# define OSSL_STORE_R_NOT_A_CRL 101 +# define OSSL_STORE_R_NOT_A_KEY 102 +# define OSSL_STORE_R_NOT_A_NAME 103 +# define OSSL_STORE_R_NOT_PARAMETERS 104 +# define OSSL_STORE_R_PASSPHRASE_CALLBACK_ERROR 114 +# define OSSL_STORE_R_PATH_MUST_BE_ABSOLUTE 108 +# define OSSL_STORE_R_SEARCH_ONLY_SUPPORTED_FOR_DIRECTORIES 119 +# define OSSL_STORE_R_UI_PROCESS_INTERRUPTED_OR_CANCELLED 109 +# define OSSL_STORE_R_UNREGISTERED_SCHEME 105 +# define OSSL_STORE_R_UNSUPPORTED_CONTENT_TYPE 110 +# define OSSL_STORE_R_UNSUPPORTED_OPERATION 118 +# define OSSL_STORE_R_UNSUPPORTED_SEARCH_TYPE 120 +# define OSSL_STORE_R_URI_AUTHORITY_UNSUPPORTED 111 + +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/symhacks.h b/example/android/third_party/crypto/arm/include/openssl/symhacks.h new file mode 100644 index 00000000..156ea6e4 --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/symhacks.h @@ -0,0 +1,37 @@ +/* + * Copyright 1999-2018 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_SYMHACKS_H +# define HEADER_SYMHACKS_H + +# include + +/* Case insensitive linking causes problems.... */ +# if defined(OPENSSL_SYS_VMS) +# undef ERR_load_CRYPTO_strings +# define ERR_load_CRYPTO_strings ERR_load_CRYPTOlib_strings +# undef OCSP_crlID_new +# define OCSP_crlID_new OCSP_crlID2_new + +# undef d2i_ECPARAMETERS +# define d2i_ECPARAMETERS d2i_UC_ECPARAMETERS +# undef i2d_ECPARAMETERS +# define i2d_ECPARAMETERS i2d_UC_ECPARAMETERS +# undef d2i_ECPKPARAMETERS +# define d2i_ECPKPARAMETERS d2i_UC_ECPKPARAMETERS +# undef i2d_ECPKPARAMETERS +# define i2d_ECPKPARAMETERS i2d_UC_ECPKPARAMETERS + +/* This one clashes with CMS_data_create */ +# undef cms_Data_create +# define cms_Data_create priv_cms_Data_create + +# endif + +#endif /* ! defined HEADER_VMS_IDHACKS_H */ diff --git a/example/android/third_party/crypto/arm/include/openssl/tls1.h b/example/android/third_party/crypto/arm/include/openssl/tls1.h new file mode 100644 index 00000000..e13b5dd4 --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/tls1.h @@ -0,0 +1,1237 @@ +/* + * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. + * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved + * Copyright 2005 Nokia. All rights reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_TLS1_H +# define HEADER_TLS1_H + +# include +# include + +#ifdef __cplusplus +extern "C" { +#endif + +/* Default security level if not overridden at config time */ +# ifndef OPENSSL_TLS_SECURITY_LEVEL +# define OPENSSL_TLS_SECURITY_LEVEL 1 +# endif + +# define TLS1_VERSION 0x0301 +# define TLS1_1_VERSION 0x0302 +# define TLS1_2_VERSION 0x0303 +# define TLS1_3_VERSION 0x0304 +# define TLS_MAX_VERSION TLS1_3_VERSION + +/* Special value for method supporting multiple versions */ +# define TLS_ANY_VERSION 0x10000 + +# define TLS1_VERSION_MAJOR 0x03 +# define TLS1_VERSION_MINOR 0x01 + +# define TLS1_1_VERSION_MAJOR 0x03 +# define TLS1_1_VERSION_MINOR 0x02 + +# define TLS1_2_VERSION_MAJOR 0x03 +# define TLS1_2_VERSION_MINOR 0x03 + +# define TLS1_get_version(s) \ + ((SSL_version(s) >> 8) == TLS1_VERSION_MAJOR ? SSL_version(s) : 0) + +# define TLS1_get_client_version(s) \ + ((SSL_client_version(s) >> 8) == TLS1_VERSION_MAJOR ? SSL_client_version(s) : 0) + +# define TLS1_AD_DECRYPTION_FAILED 21 +# define TLS1_AD_RECORD_OVERFLOW 22 +# define TLS1_AD_UNKNOWN_CA 48/* fatal */ +# define TLS1_AD_ACCESS_DENIED 49/* fatal */ +# define TLS1_AD_DECODE_ERROR 50/* fatal */ +# define TLS1_AD_DECRYPT_ERROR 51 +# define TLS1_AD_EXPORT_RESTRICTION 60/* fatal */ +# define TLS1_AD_PROTOCOL_VERSION 70/* fatal */ +# define TLS1_AD_INSUFFICIENT_SECURITY 71/* fatal */ +# define TLS1_AD_INTERNAL_ERROR 80/* fatal */ +# define TLS1_AD_INAPPROPRIATE_FALLBACK 86/* fatal */ +# define TLS1_AD_USER_CANCELLED 90 +# define TLS1_AD_NO_RENEGOTIATION 100 +/* TLSv1.3 alerts */ +# define TLS13_AD_MISSING_EXTENSION 109 /* fatal */ +# define TLS13_AD_CERTIFICATE_REQUIRED 116 /* fatal */ +/* codes 110-114 are from RFC3546 */ +# define TLS1_AD_UNSUPPORTED_EXTENSION 110 +# define TLS1_AD_CERTIFICATE_UNOBTAINABLE 111 +# define TLS1_AD_UNRECOGNIZED_NAME 112 +# define TLS1_AD_BAD_CERTIFICATE_STATUS_RESPONSE 113 +# define TLS1_AD_BAD_CERTIFICATE_HASH_VALUE 114 +# define TLS1_AD_UNKNOWN_PSK_IDENTITY 115/* fatal */ +# define TLS1_AD_NO_APPLICATION_PROTOCOL 120 /* fatal */ + +/* ExtensionType values from RFC3546 / RFC4366 / RFC6066 */ +# define TLSEXT_TYPE_server_name 0 +# define TLSEXT_TYPE_max_fragment_length 1 +# define TLSEXT_TYPE_client_certificate_url 2 +# define TLSEXT_TYPE_trusted_ca_keys 3 +# define TLSEXT_TYPE_truncated_hmac 4 +# define TLSEXT_TYPE_status_request 5 +/* ExtensionType values from RFC4681 */ +# define TLSEXT_TYPE_user_mapping 6 +/* ExtensionType values from RFC5878 */ +# define TLSEXT_TYPE_client_authz 7 +# define TLSEXT_TYPE_server_authz 8 +/* ExtensionType values from RFC6091 */ +# define TLSEXT_TYPE_cert_type 9 + +/* ExtensionType values from RFC4492 */ +/* + * Prior to TLSv1.3 the supported_groups extension was known as + * elliptic_curves + */ +# define TLSEXT_TYPE_supported_groups 10 +# define TLSEXT_TYPE_elliptic_curves TLSEXT_TYPE_supported_groups +# define TLSEXT_TYPE_ec_point_formats 11 + + +/* ExtensionType value from RFC5054 */ +# define TLSEXT_TYPE_srp 12 + +/* ExtensionType values from RFC5246 */ +# define TLSEXT_TYPE_signature_algorithms 13 + +/* ExtensionType value from RFC5764 */ +# define TLSEXT_TYPE_use_srtp 14 + +/* ExtensionType value from RFC5620 */ +# define TLSEXT_TYPE_heartbeat 15 + +/* ExtensionType value from RFC7301 */ +# define TLSEXT_TYPE_application_layer_protocol_negotiation 16 + +/* + * Extension type for Certificate Transparency + * https://tools.ietf.org/html/rfc6962#section-3.3.1 + */ +# define TLSEXT_TYPE_signed_certificate_timestamp 18 + +/* + * ExtensionType value for TLS padding extension. + * http://tools.ietf.org/html/draft-agl-tls-padding + */ +# define TLSEXT_TYPE_padding 21 + +/* ExtensionType value from RFC7366 */ +# define TLSEXT_TYPE_encrypt_then_mac 22 + +/* ExtensionType value from RFC7627 */ +# define TLSEXT_TYPE_extended_master_secret 23 + +/* ExtensionType value from RFC4507 */ +# define TLSEXT_TYPE_session_ticket 35 + +/* As defined for TLS1.3 */ +# define TLSEXT_TYPE_psk 41 +# define TLSEXT_TYPE_early_data 42 +# define TLSEXT_TYPE_supported_versions 43 +# define TLSEXT_TYPE_cookie 44 +# define TLSEXT_TYPE_psk_kex_modes 45 +# define TLSEXT_TYPE_certificate_authorities 47 +# define TLSEXT_TYPE_post_handshake_auth 49 +# define TLSEXT_TYPE_signature_algorithms_cert 50 +# define TLSEXT_TYPE_key_share 51 + +/* Temporary extension type */ +# define TLSEXT_TYPE_renegotiate 0xff01 + +# ifndef OPENSSL_NO_NEXTPROTONEG +/* This is not an IANA defined extension number */ +# define TLSEXT_TYPE_next_proto_neg 13172 +# endif + +/* NameType value from RFC3546 */ +# define TLSEXT_NAMETYPE_host_name 0 +/* status request value from RFC3546 */ +# define TLSEXT_STATUSTYPE_ocsp 1 + +/* ECPointFormat values from RFC4492 */ +# define TLSEXT_ECPOINTFORMAT_first 0 +# define TLSEXT_ECPOINTFORMAT_uncompressed 0 +# define TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime 1 +# define TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2 2 +# define TLSEXT_ECPOINTFORMAT_last 2 + +/* Signature and hash algorithms from RFC5246 */ +# define TLSEXT_signature_anonymous 0 +# define TLSEXT_signature_rsa 1 +# define TLSEXT_signature_dsa 2 +# define TLSEXT_signature_ecdsa 3 +# define TLSEXT_signature_gostr34102001 237 +# define TLSEXT_signature_gostr34102012_256 238 +# define TLSEXT_signature_gostr34102012_512 239 + +/* Total number of different signature algorithms */ +# define TLSEXT_signature_num 7 + +# define TLSEXT_hash_none 0 +# define TLSEXT_hash_md5 1 +# define TLSEXT_hash_sha1 2 +# define TLSEXT_hash_sha224 3 +# define TLSEXT_hash_sha256 4 +# define TLSEXT_hash_sha384 5 +# define TLSEXT_hash_sha512 6 +# define TLSEXT_hash_gostr3411 237 +# define TLSEXT_hash_gostr34112012_256 238 +# define TLSEXT_hash_gostr34112012_512 239 + +/* Total number of different digest algorithms */ + +# define TLSEXT_hash_num 10 + +/* Flag set for unrecognised algorithms */ +# define TLSEXT_nid_unknown 0x1000000 + +/* ECC curves */ + +# define TLSEXT_curve_P_256 23 +# define TLSEXT_curve_P_384 24 + +/* OpenSSL value to disable maximum fragment length extension */ +# define TLSEXT_max_fragment_length_DISABLED 0 +/* Allowed values for max fragment length extension */ +# define TLSEXT_max_fragment_length_512 1 +# define TLSEXT_max_fragment_length_1024 2 +# define TLSEXT_max_fragment_length_2048 3 +# define TLSEXT_max_fragment_length_4096 4 + +int SSL_CTX_set_tlsext_max_fragment_length(SSL_CTX *ctx, uint8_t mode); +int SSL_set_tlsext_max_fragment_length(SSL *ssl, uint8_t mode); + +# define TLSEXT_MAXLEN_host_name 255 + +__owur const char *SSL_get_servername(const SSL *s, const int type); +__owur int SSL_get_servername_type(const SSL *s); +/* + * SSL_export_keying_material exports a value derived from the master secret, + * as specified in RFC 5705. It writes |olen| bytes to |out| given a label and + * optional context. (Since a zero length context is allowed, the |use_context| + * flag controls whether a context is included.) It returns 1 on success and + * 0 or -1 otherwise. + */ +__owur int SSL_export_keying_material(SSL *s, unsigned char *out, size_t olen, + const char *label, size_t llen, + const unsigned char *context, + size_t contextlen, int use_context); + +/* + * SSL_export_keying_material_early exports a value derived from the + * early exporter master secret, as specified in + * https://tools.ietf.org/html/draft-ietf-tls-tls13-23. It writes + * |olen| bytes to |out| given a label and optional context. It + * returns 1 on success and 0 otherwise. + */ +__owur int SSL_export_keying_material_early(SSL *s, unsigned char *out, + size_t olen, const char *label, + size_t llen, + const unsigned char *context, + size_t contextlen); + +int SSL_get_peer_signature_type_nid(const SSL *s, int *pnid); +int SSL_get_signature_type_nid(const SSL *s, int *pnid); + +int SSL_get_sigalgs(SSL *s, int idx, + int *psign, int *phash, int *psignandhash, + unsigned char *rsig, unsigned char *rhash); + +int SSL_get_shared_sigalgs(SSL *s, int idx, + int *psign, int *phash, int *psignandhash, + unsigned char *rsig, unsigned char *rhash); + +__owur int SSL_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain); + +# define SSL_set_tlsext_host_name(s,name) \ + SSL_ctrl(s,SSL_CTRL_SET_TLSEXT_HOSTNAME,TLSEXT_NAMETYPE_host_name,\ + (void *)name) + +# define SSL_set_tlsext_debug_callback(ssl, cb) \ + SSL_callback_ctrl(ssl,SSL_CTRL_SET_TLSEXT_DEBUG_CB,\ + (void (*)(void))cb) + +# define SSL_set_tlsext_debug_arg(ssl, arg) \ + SSL_ctrl(ssl,SSL_CTRL_SET_TLSEXT_DEBUG_ARG,0,arg) + +# define SSL_get_tlsext_status_type(ssl) \ + SSL_ctrl(ssl,SSL_CTRL_GET_TLSEXT_STATUS_REQ_TYPE,0,NULL) + +# define SSL_set_tlsext_status_type(ssl, type) \ + SSL_ctrl(ssl,SSL_CTRL_SET_TLSEXT_STATUS_REQ_TYPE,type,NULL) + +# define SSL_get_tlsext_status_exts(ssl, arg) \ + SSL_ctrl(ssl,SSL_CTRL_GET_TLSEXT_STATUS_REQ_EXTS,0,arg) + +# define SSL_set_tlsext_status_exts(ssl, arg) \ + SSL_ctrl(ssl,SSL_CTRL_SET_TLSEXT_STATUS_REQ_EXTS,0,arg) + +# define SSL_get_tlsext_status_ids(ssl, arg) \ + SSL_ctrl(ssl,SSL_CTRL_GET_TLSEXT_STATUS_REQ_IDS,0,arg) + +# define SSL_set_tlsext_status_ids(ssl, arg) \ + SSL_ctrl(ssl,SSL_CTRL_SET_TLSEXT_STATUS_REQ_IDS,0,arg) + +# define SSL_get_tlsext_status_ocsp_resp(ssl, arg) \ + SSL_ctrl(ssl,SSL_CTRL_GET_TLSEXT_STATUS_REQ_OCSP_RESP,0,arg) + +# define SSL_set_tlsext_status_ocsp_resp(ssl, arg, arglen) \ + SSL_ctrl(ssl,SSL_CTRL_SET_TLSEXT_STATUS_REQ_OCSP_RESP,arglen,arg) + +# define SSL_CTX_set_tlsext_servername_callback(ctx, cb) \ + SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TLSEXT_SERVERNAME_CB,\ + (void (*)(void))cb) + +# define SSL_TLSEXT_ERR_OK 0 +# define SSL_TLSEXT_ERR_ALERT_WARNING 1 +# define SSL_TLSEXT_ERR_ALERT_FATAL 2 +# define SSL_TLSEXT_ERR_NOACK 3 + +# define SSL_CTX_set_tlsext_servername_arg(ctx, arg) \ + SSL_CTX_ctrl(ctx,SSL_CTRL_SET_TLSEXT_SERVERNAME_ARG,0,arg) + +# define SSL_CTX_get_tlsext_ticket_keys(ctx, keys, keylen) \ + SSL_CTX_ctrl(ctx,SSL_CTRL_GET_TLSEXT_TICKET_KEYS,keylen,keys) +# define SSL_CTX_set_tlsext_ticket_keys(ctx, keys, keylen) \ + SSL_CTX_ctrl(ctx,SSL_CTRL_SET_TLSEXT_TICKET_KEYS,keylen,keys) + +# define SSL_CTX_get_tlsext_status_cb(ssl, cb) \ + SSL_CTX_ctrl(ssl,SSL_CTRL_GET_TLSEXT_STATUS_REQ_CB,0,(void *)cb) +# define SSL_CTX_set_tlsext_status_cb(ssl, cb) \ + SSL_CTX_callback_ctrl(ssl,SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB,\ + (void (*)(void))cb) + +# define SSL_CTX_get_tlsext_status_arg(ssl, arg) \ + SSL_CTX_ctrl(ssl,SSL_CTRL_GET_TLSEXT_STATUS_REQ_CB_ARG,0,arg) +# define SSL_CTX_set_tlsext_status_arg(ssl, arg) \ + SSL_CTX_ctrl(ssl,SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB_ARG,0,arg) + +# define SSL_CTX_set_tlsext_status_type(ssl, type) \ + SSL_CTX_ctrl(ssl,SSL_CTRL_SET_TLSEXT_STATUS_REQ_TYPE,type,NULL) + +# define SSL_CTX_get_tlsext_status_type(ssl) \ + SSL_CTX_ctrl(ssl,SSL_CTRL_GET_TLSEXT_STATUS_REQ_TYPE,0,NULL) + +# define SSL_CTX_set_tlsext_ticket_key_cb(ssl, cb) \ + SSL_CTX_callback_ctrl(ssl,SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB,\ + (void (*)(void))cb) + +# ifndef OPENSSL_NO_HEARTBEATS +# define SSL_DTLSEXT_HB_ENABLED 0x01 +# define SSL_DTLSEXT_HB_DONT_SEND_REQUESTS 0x02 +# define SSL_DTLSEXT_HB_DONT_RECV_REQUESTS 0x04 +# define SSL_get_dtlsext_heartbeat_pending(ssl) \ + SSL_ctrl(ssl,SSL_CTRL_GET_DTLS_EXT_HEARTBEAT_PENDING,0,NULL) +# define SSL_set_dtlsext_heartbeat_no_requests(ssl, arg) \ + SSL_ctrl(ssl,SSL_CTRL_SET_DTLS_EXT_HEARTBEAT_NO_REQUESTS,arg,NULL) + +# if OPENSSL_API_COMPAT < 0x10100000L +# define SSL_CTRL_TLS_EXT_SEND_HEARTBEAT \ + SSL_CTRL_DTLS_EXT_SEND_HEARTBEAT +# define SSL_CTRL_GET_TLS_EXT_HEARTBEAT_PENDING \ + SSL_CTRL_GET_DTLS_EXT_HEARTBEAT_PENDING +# define SSL_CTRL_SET_TLS_EXT_HEARTBEAT_NO_REQUESTS \ + SSL_CTRL_SET_DTLS_EXT_HEARTBEAT_NO_REQUESTS +# define SSL_TLSEXT_HB_ENABLED \ + SSL_DTLSEXT_HB_ENABLED +# define SSL_TLSEXT_HB_DONT_SEND_REQUESTS \ + SSL_DTLSEXT_HB_DONT_SEND_REQUESTS +# define SSL_TLSEXT_HB_DONT_RECV_REQUESTS \ + SSL_DTLSEXT_HB_DONT_RECV_REQUESTS +# define SSL_get_tlsext_heartbeat_pending(ssl) \ + SSL_get_dtlsext_heartbeat_pending(ssl) +# define SSL_set_tlsext_heartbeat_no_requests(ssl, arg) \ + SSL_set_dtlsext_heartbeat_no_requests(ssl,arg) +# endif +# endif + +/* PSK ciphersuites from 4279 */ +# define TLS1_CK_PSK_WITH_RC4_128_SHA 0x0300008A +# define TLS1_CK_PSK_WITH_3DES_EDE_CBC_SHA 0x0300008B +# define TLS1_CK_PSK_WITH_AES_128_CBC_SHA 0x0300008C +# define TLS1_CK_PSK_WITH_AES_256_CBC_SHA 0x0300008D +# define TLS1_CK_DHE_PSK_WITH_RC4_128_SHA 0x0300008E +# define TLS1_CK_DHE_PSK_WITH_3DES_EDE_CBC_SHA 0x0300008F +# define TLS1_CK_DHE_PSK_WITH_AES_128_CBC_SHA 0x03000090 +# define TLS1_CK_DHE_PSK_WITH_AES_256_CBC_SHA 0x03000091 +# define TLS1_CK_RSA_PSK_WITH_RC4_128_SHA 0x03000092 +# define TLS1_CK_RSA_PSK_WITH_3DES_EDE_CBC_SHA 0x03000093 +# define TLS1_CK_RSA_PSK_WITH_AES_128_CBC_SHA 0x03000094 +# define TLS1_CK_RSA_PSK_WITH_AES_256_CBC_SHA 0x03000095 + +/* PSK ciphersuites from 5487 */ +# define TLS1_CK_PSK_WITH_AES_128_GCM_SHA256 0x030000A8 +# define TLS1_CK_PSK_WITH_AES_256_GCM_SHA384 0x030000A9 +# define TLS1_CK_DHE_PSK_WITH_AES_128_GCM_SHA256 0x030000AA +# define TLS1_CK_DHE_PSK_WITH_AES_256_GCM_SHA384 0x030000AB +# define TLS1_CK_RSA_PSK_WITH_AES_128_GCM_SHA256 0x030000AC +# define TLS1_CK_RSA_PSK_WITH_AES_256_GCM_SHA384 0x030000AD +# define TLS1_CK_PSK_WITH_AES_128_CBC_SHA256 0x030000AE +# define TLS1_CK_PSK_WITH_AES_256_CBC_SHA384 0x030000AF +# define TLS1_CK_PSK_WITH_NULL_SHA256 0x030000B0 +# define TLS1_CK_PSK_WITH_NULL_SHA384 0x030000B1 +# define TLS1_CK_DHE_PSK_WITH_AES_128_CBC_SHA256 0x030000B2 +# define TLS1_CK_DHE_PSK_WITH_AES_256_CBC_SHA384 0x030000B3 +# define TLS1_CK_DHE_PSK_WITH_NULL_SHA256 0x030000B4 +# define TLS1_CK_DHE_PSK_WITH_NULL_SHA384 0x030000B5 +# define TLS1_CK_RSA_PSK_WITH_AES_128_CBC_SHA256 0x030000B6 +# define TLS1_CK_RSA_PSK_WITH_AES_256_CBC_SHA384 0x030000B7 +# define TLS1_CK_RSA_PSK_WITH_NULL_SHA256 0x030000B8 +# define TLS1_CK_RSA_PSK_WITH_NULL_SHA384 0x030000B9 + +/* NULL PSK ciphersuites from RFC4785 */ +# define TLS1_CK_PSK_WITH_NULL_SHA 0x0300002C +# define TLS1_CK_DHE_PSK_WITH_NULL_SHA 0x0300002D +# define TLS1_CK_RSA_PSK_WITH_NULL_SHA 0x0300002E + +/* AES ciphersuites from RFC3268 */ +# define TLS1_CK_RSA_WITH_AES_128_SHA 0x0300002F +# define TLS1_CK_DH_DSS_WITH_AES_128_SHA 0x03000030 +# define TLS1_CK_DH_RSA_WITH_AES_128_SHA 0x03000031 +# define TLS1_CK_DHE_DSS_WITH_AES_128_SHA 0x03000032 +# define TLS1_CK_DHE_RSA_WITH_AES_128_SHA 0x03000033 +# define TLS1_CK_ADH_WITH_AES_128_SHA 0x03000034 +# define TLS1_CK_RSA_WITH_AES_256_SHA 0x03000035 +# define TLS1_CK_DH_DSS_WITH_AES_256_SHA 0x03000036 +# define TLS1_CK_DH_RSA_WITH_AES_256_SHA 0x03000037 +# define TLS1_CK_DHE_DSS_WITH_AES_256_SHA 0x03000038 +# define TLS1_CK_DHE_RSA_WITH_AES_256_SHA 0x03000039 +# define TLS1_CK_ADH_WITH_AES_256_SHA 0x0300003A + +/* TLS v1.2 ciphersuites */ +# define TLS1_CK_RSA_WITH_NULL_SHA256 0x0300003B +# define TLS1_CK_RSA_WITH_AES_128_SHA256 0x0300003C +# define TLS1_CK_RSA_WITH_AES_256_SHA256 0x0300003D +# define TLS1_CK_DH_DSS_WITH_AES_128_SHA256 0x0300003E +# define TLS1_CK_DH_RSA_WITH_AES_128_SHA256 0x0300003F +# define TLS1_CK_DHE_DSS_WITH_AES_128_SHA256 0x03000040 + +/* Camellia ciphersuites from RFC4132 */ +# define TLS1_CK_RSA_WITH_CAMELLIA_128_CBC_SHA 0x03000041 +# define TLS1_CK_DH_DSS_WITH_CAMELLIA_128_CBC_SHA 0x03000042 +# define TLS1_CK_DH_RSA_WITH_CAMELLIA_128_CBC_SHA 0x03000043 +# define TLS1_CK_DHE_DSS_WITH_CAMELLIA_128_CBC_SHA 0x03000044 +# define TLS1_CK_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA 0x03000045 +# define TLS1_CK_ADH_WITH_CAMELLIA_128_CBC_SHA 0x03000046 + +/* TLS v1.2 ciphersuites */ +# define TLS1_CK_DHE_RSA_WITH_AES_128_SHA256 0x03000067 +# define TLS1_CK_DH_DSS_WITH_AES_256_SHA256 0x03000068 +# define TLS1_CK_DH_RSA_WITH_AES_256_SHA256 0x03000069 +# define TLS1_CK_DHE_DSS_WITH_AES_256_SHA256 0x0300006A +# define TLS1_CK_DHE_RSA_WITH_AES_256_SHA256 0x0300006B +# define TLS1_CK_ADH_WITH_AES_128_SHA256 0x0300006C +# define TLS1_CK_ADH_WITH_AES_256_SHA256 0x0300006D + +/* Camellia ciphersuites from RFC4132 */ +# define TLS1_CK_RSA_WITH_CAMELLIA_256_CBC_SHA 0x03000084 +# define TLS1_CK_DH_DSS_WITH_CAMELLIA_256_CBC_SHA 0x03000085 +# define TLS1_CK_DH_RSA_WITH_CAMELLIA_256_CBC_SHA 0x03000086 +# define TLS1_CK_DHE_DSS_WITH_CAMELLIA_256_CBC_SHA 0x03000087 +# define TLS1_CK_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA 0x03000088 +# define TLS1_CK_ADH_WITH_CAMELLIA_256_CBC_SHA 0x03000089 + +/* SEED ciphersuites from RFC4162 */ +# define TLS1_CK_RSA_WITH_SEED_SHA 0x03000096 +# define TLS1_CK_DH_DSS_WITH_SEED_SHA 0x03000097 +# define TLS1_CK_DH_RSA_WITH_SEED_SHA 0x03000098 +# define TLS1_CK_DHE_DSS_WITH_SEED_SHA 0x03000099 +# define TLS1_CK_DHE_RSA_WITH_SEED_SHA 0x0300009A +# define TLS1_CK_ADH_WITH_SEED_SHA 0x0300009B + +/* TLS v1.2 GCM ciphersuites from RFC5288 */ +# define TLS1_CK_RSA_WITH_AES_128_GCM_SHA256 0x0300009C +# define TLS1_CK_RSA_WITH_AES_256_GCM_SHA384 0x0300009D +# define TLS1_CK_DHE_RSA_WITH_AES_128_GCM_SHA256 0x0300009E +# define TLS1_CK_DHE_RSA_WITH_AES_256_GCM_SHA384 0x0300009F +# define TLS1_CK_DH_RSA_WITH_AES_128_GCM_SHA256 0x030000A0 +# define TLS1_CK_DH_RSA_WITH_AES_256_GCM_SHA384 0x030000A1 +# define TLS1_CK_DHE_DSS_WITH_AES_128_GCM_SHA256 0x030000A2 +# define TLS1_CK_DHE_DSS_WITH_AES_256_GCM_SHA384 0x030000A3 +# define TLS1_CK_DH_DSS_WITH_AES_128_GCM_SHA256 0x030000A4 +# define TLS1_CK_DH_DSS_WITH_AES_256_GCM_SHA384 0x030000A5 +# define TLS1_CK_ADH_WITH_AES_128_GCM_SHA256 0x030000A6 +# define TLS1_CK_ADH_WITH_AES_256_GCM_SHA384 0x030000A7 + +/* CCM ciphersuites from RFC6655 */ +# define TLS1_CK_RSA_WITH_AES_128_CCM 0x0300C09C +# define TLS1_CK_RSA_WITH_AES_256_CCM 0x0300C09D +# define TLS1_CK_DHE_RSA_WITH_AES_128_CCM 0x0300C09E +# define TLS1_CK_DHE_RSA_WITH_AES_256_CCM 0x0300C09F +# define TLS1_CK_RSA_WITH_AES_128_CCM_8 0x0300C0A0 +# define TLS1_CK_RSA_WITH_AES_256_CCM_8 0x0300C0A1 +# define TLS1_CK_DHE_RSA_WITH_AES_128_CCM_8 0x0300C0A2 +# define TLS1_CK_DHE_RSA_WITH_AES_256_CCM_8 0x0300C0A3 +# define TLS1_CK_PSK_WITH_AES_128_CCM 0x0300C0A4 +# define TLS1_CK_PSK_WITH_AES_256_CCM 0x0300C0A5 +# define TLS1_CK_DHE_PSK_WITH_AES_128_CCM 0x0300C0A6 +# define TLS1_CK_DHE_PSK_WITH_AES_256_CCM 0x0300C0A7 +# define TLS1_CK_PSK_WITH_AES_128_CCM_8 0x0300C0A8 +# define TLS1_CK_PSK_WITH_AES_256_CCM_8 0x0300C0A9 +# define TLS1_CK_DHE_PSK_WITH_AES_128_CCM_8 0x0300C0AA +# define TLS1_CK_DHE_PSK_WITH_AES_256_CCM_8 0x0300C0AB + +/* CCM ciphersuites from RFC7251 */ +# define TLS1_CK_ECDHE_ECDSA_WITH_AES_128_CCM 0x0300C0AC +# define TLS1_CK_ECDHE_ECDSA_WITH_AES_256_CCM 0x0300C0AD +# define TLS1_CK_ECDHE_ECDSA_WITH_AES_128_CCM_8 0x0300C0AE +# define TLS1_CK_ECDHE_ECDSA_WITH_AES_256_CCM_8 0x0300C0AF + +/* TLS 1.2 Camellia SHA-256 ciphersuites from RFC5932 */ +# define TLS1_CK_RSA_WITH_CAMELLIA_128_CBC_SHA256 0x030000BA +# define TLS1_CK_DH_DSS_WITH_CAMELLIA_128_CBC_SHA256 0x030000BB +# define TLS1_CK_DH_RSA_WITH_CAMELLIA_128_CBC_SHA256 0x030000BC +# define TLS1_CK_DHE_DSS_WITH_CAMELLIA_128_CBC_SHA256 0x030000BD +# define TLS1_CK_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256 0x030000BE +# define TLS1_CK_ADH_WITH_CAMELLIA_128_CBC_SHA256 0x030000BF + +# define TLS1_CK_RSA_WITH_CAMELLIA_256_CBC_SHA256 0x030000C0 +# define TLS1_CK_DH_DSS_WITH_CAMELLIA_256_CBC_SHA256 0x030000C1 +# define TLS1_CK_DH_RSA_WITH_CAMELLIA_256_CBC_SHA256 0x030000C2 +# define TLS1_CK_DHE_DSS_WITH_CAMELLIA_256_CBC_SHA256 0x030000C3 +# define TLS1_CK_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256 0x030000C4 +# define TLS1_CK_ADH_WITH_CAMELLIA_256_CBC_SHA256 0x030000C5 + +/* ECC ciphersuites from RFC4492 */ +# define TLS1_CK_ECDH_ECDSA_WITH_NULL_SHA 0x0300C001 +# define TLS1_CK_ECDH_ECDSA_WITH_RC4_128_SHA 0x0300C002 +# define TLS1_CK_ECDH_ECDSA_WITH_DES_192_CBC3_SHA 0x0300C003 +# define TLS1_CK_ECDH_ECDSA_WITH_AES_128_CBC_SHA 0x0300C004 +# define TLS1_CK_ECDH_ECDSA_WITH_AES_256_CBC_SHA 0x0300C005 + +# define TLS1_CK_ECDHE_ECDSA_WITH_NULL_SHA 0x0300C006 +# define TLS1_CK_ECDHE_ECDSA_WITH_RC4_128_SHA 0x0300C007 +# define TLS1_CK_ECDHE_ECDSA_WITH_DES_192_CBC3_SHA 0x0300C008 +# define TLS1_CK_ECDHE_ECDSA_WITH_AES_128_CBC_SHA 0x0300C009 +# define TLS1_CK_ECDHE_ECDSA_WITH_AES_256_CBC_SHA 0x0300C00A + +# define TLS1_CK_ECDH_RSA_WITH_NULL_SHA 0x0300C00B +# define TLS1_CK_ECDH_RSA_WITH_RC4_128_SHA 0x0300C00C +# define TLS1_CK_ECDH_RSA_WITH_DES_192_CBC3_SHA 0x0300C00D +# define TLS1_CK_ECDH_RSA_WITH_AES_128_CBC_SHA 0x0300C00E +# define TLS1_CK_ECDH_RSA_WITH_AES_256_CBC_SHA 0x0300C00F + +# define TLS1_CK_ECDHE_RSA_WITH_NULL_SHA 0x0300C010 +# define TLS1_CK_ECDHE_RSA_WITH_RC4_128_SHA 0x0300C011 +# define TLS1_CK_ECDHE_RSA_WITH_DES_192_CBC3_SHA 0x0300C012 +# define TLS1_CK_ECDHE_RSA_WITH_AES_128_CBC_SHA 0x0300C013 +# define TLS1_CK_ECDHE_RSA_WITH_AES_256_CBC_SHA 0x0300C014 + +# define TLS1_CK_ECDH_anon_WITH_NULL_SHA 0x0300C015 +# define TLS1_CK_ECDH_anon_WITH_RC4_128_SHA 0x0300C016 +# define TLS1_CK_ECDH_anon_WITH_DES_192_CBC3_SHA 0x0300C017 +# define TLS1_CK_ECDH_anon_WITH_AES_128_CBC_SHA 0x0300C018 +# define TLS1_CK_ECDH_anon_WITH_AES_256_CBC_SHA 0x0300C019 + +/* SRP ciphersuites from RFC 5054 */ +# define TLS1_CK_SRP_SHA_WITH_3DES_EDE_CBC_SHA 0x0300C01A +# define TLS1_CK_SRP_SHA_RSA_WITH_3DES_EDE_CBC_SHA 0x0300C01B +# define TLS1_CK_SRP_SHA_DSS_WITH_3DES_EDE_CBC_SHA 0x0300C01C +# define TLS1_CK_SRP_SHA_WITH_AES_128_CBC_SHA 0x0300C01D +# define TLS1_CK_SRP_SHA_RSA_WITH_AES_128_CBC_SHA 0x0300C01E +# define TLS1_CK_SRP_SHA_DSS_WITH_AES_128_CBC_SHA 0x0300C01F +# define TLS1_CK_SRP_SHA_WITH_AES_256_CBC_SHA 0x0300C020 +# define TLS1_CK_SRP_SHA_RSA_WITH_AES_256_CBC_SHA 0x0300C021 +# define TLS1_CK_SRP_SHA_DSS_WITH_AES_256_CBC_SHA 0x0300C022 + +/* ECDH HMAC based ciphersuites from RFC5289 */ +# define TLS1_CK_ECDHE_ECDSA_WITH_AES_128_SHA256 0x0300C023 +# define TLS1_CK_ECDHE_ECDSA_WITH_AES_256_SHA384 0x0300C024 +# define TLS1_CK_ECDH_ECDSA_WITH_AES_128_SHA256 0x0300C025 +# define TLS1_CK_ECDH_ECDSA_WITH_AES_256_SHA384 0x0300C026 +# define TLS1_CK_ECDHE_RSA_WITH_AES_128_SHA256 0x0300C027 +# define TLS1_CK_ECDHE_RSA_WITH_AES_256_SHA384 0x0300C028 +# define TLS1_CK_ECDH_RSA_WITH_AES_128_SHA256 0x0300C029 +# define TLS1_CK_ECDH_RSA_WITH_AES_256_SHA384 0x0300C02A + +/* ECDH GCM based ciphersuites from RFC5289 */ +# define TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 0x0300C02B +# define TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 0x0300C02C +# define TLS1_CK_ECDH_ECDSA_WITH_AES_128_GCM_SHA256 0x0300C02D +# define TLS1_CK_ECDH_ECDSA_WITH_AES_256_GCM_SHA384 0x0300C02E +# define TLS1_CK_ECDHE_RSA_WITH_AES_128_GCM_SHA256 0x0300C02F +# define TLS1_CK_ECDHE_RSA_WITH_AES_256_GCM_SHA384 0x0300C030 +# define TLS1_CK_ECDH_RSA_WITH_AES_128_GCM_SHA256 0x0300C031 +# define TLS1_CK_ECDH_RSA_WITH_AES_256_GCM_SHA384 0x0300C032 + +/* ECDHE PSK ciphersuites from RFC5489 */ +# define TLS1_CK_ECDHE_PSK_WITH_RC4_128_SHA 0x0300C033 +# define TLS1_CK_ECDHE_PSK_WITH_3DES_EDE_CBC_SHA 0x0300C034 +# define TLS1_CK_ECDHE_PSK_WITH_AES_128_CBC_SHA 0x0300C035 +# define TLS1_CK_ECDHE_PSK_WITH_AES_256_CBC_SHA 0x0300C036 + +# define TLS1_CK_ECDHE_PSK_WITH_AES_128_CBC_SHA256 0x0300C037 +# define TLS1_CK_ECDHE_PSK_WITH_AES_256_CBC_SHA384 0x0300C038 + +/* NULL PSK ciphersuites from RFC4785 */ +# define TLS1_CK_ECDHE_PSK_WITH_NULL_SHA 0x0300C039 +# define TLS1_CK_ECDHE_PSK_WITH_NULL_SHA256 0x0300C03A +# define TLS1_CK_ECDHE_PSK_WITH_NULL_SHA384 0x0300C03B + +/* Camellia-CBC ciphersuites from RFC6367 */ +# define TLS1_CK_ECDHE_ECDSA_WITH_CAMELLIA_128_CBC_SHA256 0x0300C072 +# define TLS1_CK_ECDHE_ECDSA_WITH_CAMELLIA_256_CBC_SHA384 0x0300C073 +# define TLS1_CK_ECDH_ECDSA_WITH_CAMELLIA_128_CBC_SHA256 0x0300C074 +# define TLS1_CK_ECDH_ECDSA_WITH_CAMELLIA_256_CBC_SHA384 0x0300C075 +# define TLS1_CK_ECDHE_RSA_WITH_CAMELLIA_128_CBC_SHA256 0x0300C076 +# define TLS1_CK_ECDHE_RSA_WITH_CAMELLIA_256_CBC_SHA384 0x0300C077 +# define TLS1_CK_ECDH_RSA_WITH_CAMELLIA_128_CBC_SHA256 0x0300C078 +# define TLS1_CK_ECDH_RSA_WITH_CAMELLIA_256_CBC_SHA384 0x0300C079 + +# define TLS1_CK_PSK_WITH_CAMELLIA_128_CBC_SHA256 0x0300C094 +# define TLS1_CK_PSK_WITH_CAMELLIA_256_CBC_SHA384 0x0300C095 +# define TLS1_CK_DHE_PSK_WITH_CAMELLIA_128_CBC_SHA256 0x0300C096 +# define TLS1_CK_DHE_PSK_WITH_CAMELLIA_256_CBC_SHA384 0x0300C097 +# define TLS1_CK_RSA_PSK_WITH_CAMELLIA_128_CBC_SHA256 0x0300C098 +# define TLS1_CK_RSA_PSK_WITH_CAMELLIA_256_CBC_SHA384 0x0300C099 +# define TLS1_CK_ECDHE_PSK_WITH_CAMELLIA_128_CBC_SHA256 0x0300C09A +# define TLS1_CK_ECDHE_PSK_WITH_CAMELLIA_256_CBC_SHA384 0x0300C09B + +/* draft-ietf-tls-chacha20-poly1305-03 */ +# define TLS1_CK_ECDHE_RSA_WITH_CHACHA20_POLY1305 0x0300CCA8 +# define TLS1_CK_ECDHE_ECDSA_WITH_CHACHA20_POLY1305 0x0300CCA9 +# define TLS1_CK_DHE_RSA_WITH_CHACHA20_POLY1305 0x0300CCAA +# define TLS1_CK_PSK_WITH_CHACHA20_POLY1305 0x0300CCAB +# define TLS1_CK_ECDHE_PSK_WITH_CHACHA20_POLY1305 0x0300CCAC +# define TLS1_CK_DHE_PSK_WITH_CHACHA20_POLY1305 0x0300CCAD +# define TLS1_CK_RSA_PSK_WITH_CHACHA20_POLY1305 0x0300CCAE + +/* TLS v1.3 ciphersuites */ +# define TLS1_3_CK_AES_128_GCM_SHA256 0x03001301 +# define TLS1_3_CK_AES_256_GCM_SHA384 0x03001302 +# define TLS1_3_CK_CHACHA20_POLY1305_SHA256 0x03001303 +# define TLS1_3_CK_AES_128_CCM_SHA256 0x03001304 +# define TLS1_3_CK_AES_128_CCM_8_SHA256 0x03001305 + +/* Aria ciphersuites from RFC6209 */ +# define TLS1_CK_RSA_WITH_ARIA_128_GCM_SHA256 0x0300C050 +# define TLS1_CK_RSA_WITH_ARIA_256_GCM_SHA384 0x0300C051 +# define TLS1_CK_DHE_RSA_WITH_ARIA_128_GCM_SHA256 0x0300C052 +# define TLS1_CK_DHE_RSA_WITH_ARIA_256_GCM_SHA384 0x0300C053 +# define TLS1_CK_DH_RSA_WITH_ARIA_128_GCM_SHA256 0x0300C054 +# define TLS1_CK_DH_RSA_WITH_ARIA_256_GCM_SHA384 0x0300C055 +# define TLS1_CK_DHE_DSS_WITH_ARIA_128_GCM_SHA256 0x0300C056 +# define TLS1_CK_DHE_DSS_WITH_ARIA_256_GCM_SHA384 0x0300C057 +# define TLS1_CK_DH_DSS_WITH_ARIA_128_GCM_SHA256 0x0300C058 +# define TLS1_CK_DH_DSS_WITH_ARIA_256_GCM_SHA384 0x0300C059 +# define TLS1_CK_DH_anon_WITH_ARIA_128_GCM_SHA256 0x0300C05A +# define TLS1_CK_DH_anon_WITH_ARIA_256_GCM_SHA384 0x0300C05B +# define TLS1_CK_ECDHE_ECDSA_WITH_ARIA_128_GCM_SHA256 0x0300C05C +# define TLS1_CK_ECDHE_ECDSA_WITH_ARIA_256_GCM_SHA384 0x0300C05D +# define TLS1_CK_ECDH_ECDSA_WITH_ARIA_128_GCM_SHA256 0x0300C05E +# define TLS1_CK_ECDH_ECDSA_WITH_ARIA_256_GCM_SHA384 0x0300C05F +# define TLS1_CK_ECDHE_RSA_WITH_ARIA_128_GCM_SHA256 0x0300C060 +# define TLS1_CK_ECDHE_RSA_WITH_ARIA_256_GCM_SHA384 0x0300C061 +# define TLS1_CK_ECDH_RSA_WITH_ARIA_128_GCM_SHA256 0x0300C062 +# define TLS1_CK_ECDH_RSA_WITH_ARIA_256_GCM_SHA384 0x0300C063 +# define TLS1_CK_PSK_WITH_ARIA_128_GCM_SHA256 0x0300C06A +# define TLS1_CK_PSK_WITH_ARIA_256_GCM_SHA384 0x0300C06B +# define TLS1_CK_DHE_PSK_WITH_ARIA_128_GCM_SHA256 0x0300C06C +# define TLS1_CK_DHE_PSK_WITH_ARIA_256_GCM_SHA384 0x0300C06D +# define TLS1_CK_RSA_PSK_WITH_ARIA_128_GCM_SHA256 0x0300C06E +# define TLS1_CK_RSA_PSK_WITH_ARIA_256_GCM_SHA384 0x0300C06F + +/* a bundle of RFC standard cipher names, generated from ssl3_ciphers[] */ +# define TLS1_RFC_RSA_WITH_AES_128_SHA "TLS_RSA_WITH_AES_128_CBC_SHA" +# define TLS1_RFC_DHE_DSS_WITH_AES_128_SHA "TLS_DHE_DSS_WITH_AES_128_CBC_SHA" +# define TLS1_RFC_DHE_RSA_WITH_AES_128_SHA "TLS_DHE_RSA_WITH_AES_128_CBC_SHA" +# define TLS1_RFC_ADH_WITH_AES_128_SHA "TLS_DH_anon_WITH_AES_128_CBC_SHA" +# define TLS1_RFC_RSA_WITH_AES_256_SHA "TLS_RSA_WITH_AES_256_CBC_SHA" +# define TLS1_RFC_DHE_DSS_WITH_AES_256_SHA "TLS_DHE_DSS_WITH_AES_256_CBC_SHA" +# define TLS1_RFC_DHE_RSA_WITH_AES_256_SHA "TLS_DHE_RSA_WITH_AES_256_CBC_SHA" +# define TLS1_RFC_ADH_WITH_AES_256_SHA "TLS_DH_anon_WITH_AES_256_CBC_SHA" +# define TLS1_RFC_RSA_WITH_NULL_SHA256 "TLS_RSA_WITH_NULL_SHA256" +# define TLS1_RFC_RSA_WITH_AES_128_SHA256 "TLS_RSA_WITH_AES_128_CBC_SHA256" +# define TLS1_RFC_RSA_WITH_AES_256_SHA256 "TLS_RSA_WITH_AES_256_CBC_SHA256" +# define TLS1_RFC_DHE_DSS_WITH_AES_128_SHA256 "TLS_DHE_DSS_WITH_AES_128_CBC_SHA256" +# define TLS1_RFC_DHE_RSA_WITH_AES_128_SHA256 "TLS_DHE_RSA_WITH_AES_128_CBC_SHA256" +# define TLS1_RFC_DHE_DSS_WITH_AES_256_SHA256 "TLS_DHE_DSS_WITH_AES_256_CBC_SHA256" +# define TLS1_RFC_DHE_RSA_WITH_AES_256_SHA256 "TLS_DHE_RSA_WITH_AES_256_CBC_SHA256" +# define TLS1_RFC_ADH_WITH_AES_128_SHA256 "TLS_DH_anon_WITH_AES_128_CBC_SHA256" +# define TLS1_RFC_ADH_WITH_AES_256_SHA256 "TLS_DH_anon_WITH_AES_256_CBC_SHA256" +# define TLS1_RFC_RSA_WITH_AES_128_GCM_SHA256 "TLS_RSA_WITH_AES_128_GCM_SHA256" +# define TLS1_RFC_RSA_WITH_AES_256_GCM_SHA384 "TLS_RSA_WITH_AES_256_GCM_SHA384" +# define TLS1_RFC_DHE_RSA_WITH_AES_128_GCM_SHA256 "TLS_DHE_RSA_WITH_AES_128_GCM_SHA256" +# define TLS1_RFC_DHE_RSA_WITH_AES_256_GCM_SHA384 "TLS_DHE_RSA_WITH_AES_256_GCM_SHA384" +# define TLS1_RFC_DHE_DSS_WITH_AES_128_GCM_SHA256 "TLS_DHE_DSS_WITH_AES_128_GCM_SHA256" +# define TLS1_RFC_DHE_DSS_WITH_AES_256_GCM_SHA384 "TLS_DHE_DSS_WITH_AES_256_GCM_SHA384" +# define TLS1_RFC_ADH_WITH_AES_128_GCM_SHA256 "TLS_DH_anon_WITH_AES_128_GCM_SHA256" +# define TLS1_RFC_ADH_WITH_AES_256_GCM_SHA384 "TLS_DH_anon_WITH_AES_256_GCM_SHA384" +# define TLS1_RFC_RSA_WITH_AES_128_CCM "TLS_RSA_WITH_AES_128_CCM" +# define TLS1_RFC_RSA_WITH_AES_256_CCM "TLS_RSA_WITH_AES_256_CCM" +# define TLS1_RFC_DHE_RSA_WITH_AES_128_CCM "TLS_DHE_RSA_WITH_AES_128_CCM" +# define TLS1_RFC_DHE_RSA_WITH_AES_256_CCM "TLS_DHE_RSA_WITH_AES_256_CCM" +# define TLS1_RFC_RSA_WITH_AES_128_CCM_8 "TLS_RSA_WITH_AES_128_CCM_8" +# define TLS1_RFC_RSA_WITH_AES_256_CCM_8 "TLS_RSA_WITH_AES_256_CCM_8" +# define TLS1_RFC_DHE_RSA_WITH_AES_128_CCM_8 "TLS_DHE_RSA_WITH_AES_128_CCM_8" +# define TLS1_RFC_DHE_RSA_WITH_AES_256_CCM_8 "TLS_DHE_RSA_WITH_AES_256_CCM_8" +# define TLS1_RFC_PSK_WITH_AES_128_CCM "TLS_PSK_WITH_AES_128_CCM" +# define TLS1_RFC_PSK_WITH_AES_256_CCM "TLS_PSK_WITH_AES_256_CCM" +# define TLS1_RFC_DHE_PSK_WITH_AES_128_CCM "TLS_DHE_PSK_WITH_AES_128_CCM" +# define TLS1_RFC_DHE_PSK_WITH_AES_256_CCM "TLS_DHE_PSK_WITH_AES_256_CCM" +# define TLS1_RFC_PSK_WITH_AES_128_CCM_8 "TLS_PSK_WITH_AES_128_CCM_8" +# define TLS1_RFC_PSK_WITH_AES_256_CCM_8 "TLS_PSK_WITH_AES_256_CCM_8" +# define TLS1_RFC_DHE_PSK_WITH_AES_128_CCM_8 "TLS_PSK_DHE_WITH_AES_128_CCM_8" +# define TLS1_RFC_DHE_PSK_WITH_AES_256_CCM_8 "TLS_PSK_DHE_WITH_AES_256_CCM_8" +# define TLS1_RFC_ECDHE_ECDSA_WITH_AES_128_CCM "TLS_ECDHE_ECDSA_WITH_AES_128_CCM" +# define TLS1_RFC_ECDHE_ECDSA_WITH_AES_256_CCM "TLS_ECDHE_ECDSA_WITH_AES_256_CCM" +# define TLS1_RFC_ECDHE_ECDSA_WITH_AES_128_CCM_8 "TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8" +# define TLS1_RFC_ECDHE_ECDSA_WITH_AES_256_CCM_8 "TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8" +# define TLS1_3_RFC_AES_128_GCM_SHA256 "TLS_AES_128_GCM_SHA256" +# define TLS1_3_RFC_AES_256_GCM_SHA384 "TLS_AES_256_GCM_SHA384" +# define TLS1_3_RFC_CHACHA20_POLY1305_SHA256 "TLS_CHACHA20_POLY1305_SHA256" +# define TLS1_3_RFC_AES_128_CCM_SHA256 "TLS_AES_128_CCM_SHA256" +# define TLS1_3_RFC_AES_128_CCM_8_SHA256 "TLS_AES_128_CCM_8_SHA256" +# define TLS1_RFC_ECDHE_ECDSA_WITH_NULL_SHA "TLS_ECDHE_ECDSA_WITH_NULL_SHA" +# define TLS1_RFC_ECDHE_ECDSA_WITH_DES_192_CBC3_SHA "TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA" +# define TLS1_RFC_ECDHE_ECDSA_WITH_AES_128_CBC_SHA "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA" +# define TLS1_RFC_ECDHE_ECDSA_WITH_AES_256_CBC_SHA "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA" +# define TLS1_RFC_ECDHE_RSA_WITH_NULL_SHA "TLS_ECDHE_RSA_WITH_NULL_SHA" +# define TLS1_RFC_ECDHE_RSA_WITH_DES_192_CBC3_SHA "TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA" +# define TLS1_RFC_ECDHE_RSA_WITH_AES_128_CBC_SHA "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA" +# define TLS1_RFC_ECDHE_RSA_WITH_AES_256_CBC_SHA "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA" +# define TLS1_RFC_ECDH_anon_WITH_NULL_SHA "TLS_ECDH_anon_WITH_NULL_SHA" +# define TLS1_RFC_ECDH_anon_WITH_DES_192_CBC3_SHA "TLS_ECDH_anon_WITH_3DES_EDE_CBC_SHA" +# define TLS1_RFC_ECDH_anon_WITH_AES_128_CBC_SHA "TLS_ECDH_anon_WITH_AES_128_CBC_SHA" +# define TLS1_RFC_ECDH_anon_WITH_AES_256_CBC_SHA "TLS_ECDH_anon_WITH_AES_256_CBC_SHA" +# define TLS1_RFC_ECDHE_ECDSA_WITH_AES_128_SHA256 "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256" +# define TLS1_RFC_ECDHE_ECDSA_WITH_AES_256_SHA384 "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384" +# define TLS1_RFC_ECDHE_RSA_WITH_AES_128_SHA256 "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256" +# define TLS1_RFC_ECDHE_RSA_WITH_AES_256_SHA384 "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384" +# define TLS1_RFC_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256" +# define TLS1_RFC_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384" +# define TLS1_RFC_ECDHE_RSA_WITH_AES_128_GCM_SHA256 "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256" +# define TLS1_RFC_ECDHE_RSA_WITH_AES_256_GCM_SHA384 "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384" +# define TLS1_RFC_PSK_WITH_NULL_SHA "TLS_PSK_WITH_NULL_SHA" +# define TLS1_RFC_DHE_PSK_WITH_NULL_SHA "TLS_DHE_PSK_WITH_NULL_SHA" +# define TLS1_RFC_RSA_PSK_WITH_NULL_SHA "TLS_RSA_PSK_WITH_NULL_SHA" +# define TLS1_RFC_PSK_WITH_3DES_EDE_CBC_SHA "TLS_PSK_WITH_3DES_EDE_CBC_SHA" +# define TLS1_RFC_PSK_WITH_AES_128_CBC_SHA "TLS_PSK_WITH_AES_128_CBC_SHA" +# define TLS1_RFC_PSK_WITH_AES_256_CBC_SHA "TLS_PSK_WITH_AES_256_CBC_SHA" +# define TLS1_RFC_DHE_PSK_WITH_3DES_EDE_CBC_SHA "TLS_DHE_PSK_WITH_3DES_EDE_CBC_SHA" +# define TLS1_RFC_DHE_PSK_WITH_AES_128_CBC_SHA "TLS_DHE_PSK_WITH_AES_128_CBC_SHA" +# define TLS1_RFC_DHE_PSK_WITH_AES_256_CBC_SHA "TLS_DHE_PSK_WITH_AES_256_CBC_SHA" +# define TLS1_RFC_RSA_PSK_WITH_3DES_EDE_CBC_SHA "TLS_RSA_PSK_WITH_3DES_EDE_CBC_SHA" +# define TLS1_RFC_RSA_PSK_WITH_AES_128_CBC_SHA "TLS_RSA_PSK_WITH_AES_128_CBC_SHA" +# define TLS1_RFC_RSA_PSK_WITH_AES_256_CBC_SHA "TLS_RSA_PSK_WITH_AES_256_CBC_SHA" +# define TLS1_RFC_PSK_WITH_AES_128_GCM_SHA256 "TLS_PSK_WITH_AES_128_GCM_SHA256" +# define TLS1_RFC_PSK_WITH_AES_256_GCM_SHA384 "TLS_PSK_WITH_AES_256_GCM_SHA384" +# define TLS1_RFC_DHE_PSK_WITH_AES_128_GCM_SHA256 "TLS_DHE_PSK_WITH_AES_128_GCM_SHA256" +# define TLS1_RFC_DHE_PSK_WITH_AES_256_GCM_SHA384 "TLS_DHE_PSK_WITH_AES_256_GCM_SHA384" +# define TLS1_RFC_RSA_PSK_WITH_AES_128_GCM_SHA256 "TLS_RSA_PSK_WITH_AES_128_GCM_SHA256" +# define TLS1_RFC_RSA_PSK_WITH_AES_256_GCM_SHA384 "TLS_RSA_PSK_WITH_AES_256_GCM_SHA384" +# define TLS1_RFC_PSK_WITH_AES_128_CBC_SHA256 "TLS_PSK_WITH_AES_128_CBC_SHA256" +# define TLS1_RFC_PSK_WITH_AES_256_CBC_SHA384 "TLS_PSK_WITH_AES_256_CBC_SHA384" +# define TLS1_RFC_PSK_WITH_NULL_SHA256 "TLS_PSK_WITH_NULL_SHA256" +# define TLS1_RFC_PSK_WITH_NULL_SHA384 "TLS_PSK_WITH_NULL_SHA384" +# define TLS1_RFC_DHE_PSK_WITH_AES_128_CBC_SHA256 "TLS_DHE_PSK_WITH_AES_128_CBC_SHA256" +# define TLS1_RFC_DHE_PSK_WITH_AES_256_CBC_SHA384 "TLS_DHE_PSK_WITH_AES_256_CBC_SHA384" +# define TLS1_RFC_DHE_PSK_WITH_NULL_SHA256 "TLS_DHE_PSK_WITH_NULL_SHA256" +# define TLS1_RFC_DHE_PSK_WITH_NULL_SHA384 "TLS_DHE_PSK_WITH_NULL_SHA384" +# define TLS1_RFC_RSA_PSK_WITH_AES_128_CBC_SHA256 "TLS_RSA_PSK_WITH_AES_128_CBC_SHA256" +# define TLS1_RFC_RSA_PSK_WITH_AES_256_CBC_SHA384 "TLS_RSA_PSK_WITH_AES_256_CBC_SHA384" +# define TLS1_RFC_RSA_PSK_WITH_NULL_SHA256 "TLS_RSA_PSK_WITH_NULL_SHA256" +# define TLS1_RFC_RSA_PSK_WITH_NULL_SHA384 "TLS_RSA_PSK_WITH_NULL_SHA384" +# define TLS1_RFC_ECDHE_PSK_WITH_3DES_EDE_CBC_SHA "TLS_ECDHE_PSK_WITH_3DES_EDE_CBC_SHA" +# define TLS1_RFC_ECDHE_PSK_WITH_AES_128_CBC_SHA "TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA" +# define TLS1_RFC_ECDHE_PSK_WITH_AES_256_CBC_SHA "TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA" +# define TLS1_RFC_ECDHE_PSK_WITH_AES_128_CBC_SHA256 "TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256" +# define TLS1_RFC_ECDHE_PSK_WITH_AES_256_CBC_SHA384 "TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA384" +# define TLS1_RFC_ECDHE_PSK_WITH_NULL_SHA "TLS_ECDHE_PSK_WITH_NULL_SHA" +# define TLS1_RFC_ECDHE_PSK_WITH_NULL_SHA256 "TLS_ECDHE_PSK_WITH_NULL_SHA256" +# define TLS1_RFC_ECDHE_PSK_WITH_NULL_SHA384 "TLS_ECDHE_PSK_WITH_NULL_SHA384" +# define TLS1_RFC_SRP_SHA_WITH_3DES_EDE_CBC_SHA "TLS_SRP_SHA_WITH_3DES_EDE_CBC_SHA" +# define TLS1_RFC_SRP_SHA_RSA_WITH_3DES_EDE_CBC_SHA "TLS_SRP_SHA_RSA_WITH_3DES_EDE_CBC_SHA" +# define TLS1_RFC_SRP_SHA_DSS_WITH_3DES_EDE_CBC_SHA "TLS_SRP_SHA_DSS_WITH_3DES_EDE_CBC_SHA" +# define TLS1_RFC_SRP_SHA_WITH_AES_128_CBC_SHA "TLS_SRP_SHA_WITH_AES_128_CBC_SHA" +# define TLS1_RFC_SRP_SHA_RSA_WITH_AES_128_CBC_SHA "TLS_SRP_SHA_RSA_WITH_AES_128_CBC_SHA" +# define TLS1_RFC_SRP_SHA_DSS_WITH_AES_128_CBC_SHA "TLS_SRP_SHA_DSS_WITH_AES_128_CBC_SHA" +# define TLS1_RFC_SRP_SHA_WITH_AES_256_CBC_SHA "TLS_SRP_SHA_WITH_AES_256_CBC_SHA" +# define TLS1_RFC_SRP_SHA_RSA_WITH_AES_256_CBC_SHA "TLS_SRP_SHA_RSA_WITH_AES_256_CBC_SHA" +# define TLS1_RFC_SRP_SHA_DSS_WITH_AES_256_CBC_SHA "TLS_SRP_SHA_DSS_WITH_AES_256_CBC_SHA" +# define TLS1_RFC_DHE_RSA_WITH_CHACHA20_POLY1305 "TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256" +# define TLS1_RFC_ECDHE_RSA_WITH_CHACHA20_POLY1305 "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256" +# define TLS1_RFC_ECDHE_ECDSA_WITH_CHACHA20_POLY1305 "TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256" +# define TLS1_RFC_PSK_WITH_CHACHA20_POLY1305 "TLS_PSK_WITH_CHACHA20_POLY1305_SHA256" +# define TLS1_RFC_ECDHE_PSK_WITH_CHACHA20_POLY1305 "TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256" +# define TLS1_RFC_DHE_PSK_WITH_CHACHA20_POLY1305 "TLS_DHE_PSK_WITH_CHACHA20_POLY1305_SHA256" +# define TLS1_RFC_RSA_PSK_WITH_CHACHA20_POLY1305 "TLS_RSA_PSK_WITH_CHACHA20_POLY1305_SHA256" +# define TLS1_RFC_RSA_WITH_CAMELLIA_128_CBC_SHA256 "TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256" +# define TLS1_RFC_DHE_DSS_WITH_CAMELLIA_128_CBC_SHA256 "TLS_DHE_DSS_WITH_CAMELLIA_128_CBC_SHA256" +# define TLS1_RFC_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256 "TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256" +# define TLS1_RFC_ADH_WITH_CAMELLIA_128_CBC_SHA256 "TLS_DH_anon_WITH_CAMELLIA_128_CBC_SHA256" +# define TLS1_RFC_RSA_WITH_CAMELLIA_256_CBC_SHA256 "TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256" +# define TLS1_RFC_DHE_DSS_WITH_CAMELLIA_256_CBC_SHA256 "TLS_DHE_DSS_WITH_CAMELLIA_256_CBC_SHA256" +# define TLS1_RFC_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256 "TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256" +# define TLS1_RFC_ADH_WITH_CAMELLIA_256_CBC_SHA256 "TLS_DH_anon_WITH_CAMELLIA_256_CBC_SHA256" +# define TLS1_RFC_RSA_WITH_CAMELLIA_256_CBC_SHA "TLS_RSA_WITH_CAMELLIA_256_CBC_SHA" +# define TLS1_RFC_DHE_DSS_WITH_CAMELLIA_256_CBC_SHA "TLS_DHE_DSS_WITH_CAMELLIA_256_CBC_SHA" +# define TLS1_RFC_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA "TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA" +# define TLS1_RFC_ADH_WITH_CAMELLIA_256_CBC_SHA "TLS_DH_anon_WITH_CAMELLIA_256_CBC_SHA" +# define TLS1_RFC_RSA_WITH_CAMELLIA_128_CBC_SHA "TLS_RSA_WITH_CAMELLIA_128_CBC_SHA" +# define TLS1_RFC_DHE_DSS_WITH_CAMELLIA_128_CBC_SHA "TLS_DHE_DSS_WITH_CAMELLIA_128_CBC_SHA" +# define TLS1_RFC_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA "TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA" +# define TLS1_RFC_ADH_WITH_CAMELLIA_128_CBC_SHA "TLS_DH_anon_WITH_CAMELLIA_128_CBC_SHA" +# define TLS1_RFC_ECDHE_ECDSA_WITH_CAMELLIA_128_CBC_SHA256 "TLS_ECDHE_ECDSA_WITH_CAMELLIA_128_CBC_SHA256" +# define TLS1_RFC_ECDHE_ECDSA_WITH_CAMELLIA_256_CBC_SHA384 "TLS_ECDHE_ECDSA_WITH_CAMELLIA_256_CBC_SHA384" +# define TLS1_RFC_ECDHE_RSA_WITH_CAMELLIA_128_CBC_SHA256 "TLS_ECDHE_RSA_WITH_CAMELLIA_128_CBC_SHA256" +# define TLS1_RFC_ECDHE_RSA_WITH_CAMELLIA_256_CBC_SHA384 "TLS_ECDHE_RSA_WITH_CAMELLIA_256_CBC_SHA384" +# define TLS1_RFC_PSK_WITH_CAMELLIA_128_CBC_SHA256 "TLS_PSK_WITH_CAMELLIA_128_CBC_SHA256" +# define TLS1_RFC_PSK_WITH_CAMELLIA_256_CBC_SHA384 "TLS_PSK_WITH_CAMELLIA_256_CBC_SHA384" +# define TLS1_RFC_DHE_PSK_WITH_CAMELLIA_128_CBC_SHA256 "TLS_DHE_PSK_WITH_CAMELLIA_128_CBC_SHA256" +# define TLS1_RFC_DHE_PSK_WITH_CAMELLIA_256_CBC_SHA384 "TLS_DHE_PSK_WITH_CAMELLIA_256_CBC_SHA384" +# define TLS1_RFC_RSA_PSK_WITH_CAMELLIA_128_CBC_SHA256 "TLS_RSA_PSK_WITH_CAMELLIA_128_CBC_SHA256" +# define TLS1_RFC_RSA_PSK_WITH_CAMELLIA_256_CBC_SHA384 "TLS_RSA_PSK_WITH_CAMELLIA_256_CBC_SHA384" +# define TLS1_RFC_ECDHE_PSK_WITH_CAMELLIA_128_CBC_SHA256 "TLS_ECDHE_PSK_WITH_CAMELLIA_128_CBC_SHA256" +# define TLS1_RFC_ECDHE_PSK_WITH_CAMELLIA_256_CBC_SHA384 "TLS_ECDHE_PSK_WITH_CAMELLIA_256_CBC_SHA384" +# define TLS1_RFC_RSA_WITH_SEED_SHA "TLS_RSA_WITH_SEED_CBC_SHA" +# define TLS1_RFC_DHE_DSS_WITH_SEED_SHA "TLS_DHE_DSS_WITH_SEED_CBC_SHA" +# define TLS1_RFC_DHE_RSA_WITH_SEED_SHA "TLS_DHE_RSA_WITH_SEED_CBC_SHA" +# define TLS1_RFC_ADH_WITH_SEED_SHA "TLS_DH_anon_WITH_SEED_CBC_SHA" +# define TLS1_RFC_ECDHE_PSK_WITH_RC4_128_SHA "TLS_ECDHE_PSK_WITH_RC4_128_SHA" +# define TLS1_RFC_ECDH_anon_WITH_RC4_128_SHA "TLS_ECDH_anon_WITH_RC4_128_SHA" +# define TLS1_RFC_ECDHE_ECDSA_WITH_RC4_128_SHA "TLS_ECDHE_ECDSA_WITH_RC4_128_SHA" +# define TLS1_RFC_ECDHE_RSA_WITH_RC4_128_SHA "TLS_ECDHE_RSA_WITH_RC4_128_SHA" +# define TLS1_RFC_PSK_WITH_RC4_128_SHA "TLS_PSK_WITH_RC4_128_SHA" +# define TLS1_RFC_RSA_PSK_WITH_RC4_128_SHA "TLS_RSA_PSK_WITH_RC4_128_SHA" +# define TLS1_RFC_DHE_PSK_WITH_RC4_128_SHA "TLS_DHE_PSK_WITH_RC4_128_SHA" +# define TLS1_RFC_RSA_WITH_ARIA_128_GCM_SHA256 "TLS_RSA_WITH_ARIA_128_GCM_SHA256" +# define TLS1_RFC_RSA_WITH_ARIA_256_GCM_SHA384 "TLS_RSA_WITH_ARIA_256_GCM_SHA384" +# define TLS1_RFC_DHE_RSA_WITH_ARIA_128_GCM_SHA256 "TLS_DHE_RSA_WITH_ARIA_128_GCM_SHA256" +# define TLS1_RFC_DHE_RSA_WITH_ARIA_256_GCM_SHA384 "TLS_DHE_RSA_WITH_ARIA_256_GCM_SHA384" +# define TLS1_RFC_DH_RSA_WITH_ARIA_128_GCM_SHA256 "TLS_DH_RSA_WITH_ARIA_128_GCM_SHA256" +# define TLS1_RFC_DH_RSA_WITH_ARIA_256_GCM_SHA384 "TLS_DH_RSA_WITH_ARIA_256_GCM_SHA384" +# define TLS1_RFC_DHE_DSS_WITH_ARIA_128_GCM_SHA256 "TLS_DHE_DSS_WITH_ARIA_128_GCM_SHA256" +# define TLS1_RFC_DHE_DSS_WITH_ARIA_256_GCM_SHA384 "TLS_DHE_DSS_WITH_ARIA_256_GCM_SHA384" +# define TLS1_RFC_DH_DSS_WITH_ARIA_128_GCM_SHA256 "TLS_DH_DSS_WITH_ARIA_128_GCM_SHA256" +# define TLS1_RFC_DH_DSS_WITH_ARIA_256_GCM_SHA384 "TLS_DH_DSS_WITH_ARIA_256_GCM_SHA384" +# define TLS1_RFC_DH_anon_WITH_ARIA_128_GCM_SHA256 "TLS_DH_anon_WITH_ARIA_128_GCM_SHA256" +# define TLS1_RFC_DH_anon_WITH_ARIA_256_GCM_SHA384 "TLS_DH_anon_WITH_ARIA_256_GCM_SHA384" +# define TLS1_RFC_ECDHE_ECDSA_WITH_ARIA_128_GCM_SHA256 "TLS_ECDHE_ECDSA_WITH_ARIA_128_GCM_SHA256" +# define TLS1_RFC_ECDHE_ECDSA_WITH_ARIA_256_GCM_SHA384 "TLS_ECDHE_ECDSA_WITH_ARIA_256_GCM_SHA384" +# define TLS1_RFC_ECDH_ECDSA_WITH_ARIA_128_GCM_SHA256 "TLS_ECDH_ECDSA_WITH_ARIA_128_GCM_SHA256" +# define TLS1_RFC_ECDH_ECDSA_WITH_ARIA_256_GCM_SHA384 "TLS_ECDH_ECDSA_WITH_ARIA_256_GCM_SHA384" +# define TLS1_RFC_ECDHE_RSA_WITH_ARIA_128_GCM_SHA256 "TLS_ECDHE_RSA_WITH_ARIA_128_GCM_SHA256" +# define TLS1_RFC_ECDHE_RSA_WITH_ARIA_256_GCM_SHA384 "TLS_ECDHE_RSA_WITH_ARIA_256_GCM_SHA384" +# define TLS1_RFC_ECDH_RSA_WITH_ARIA_128_GCM_SHA256 "TLS_ECDH_RSA_WITH_ARIA_128_GCM_SHA256" +# define TLS1_RFC_ECDH_RSA_WITH_ARIA_256_GCM_SHA384 "TLS_ECDH_RSA_WITH_ARIA_256_GCM_SHA384" +# define TLS1_RFC_PSK_WITH_ARIA_128_GCM_SHA256 "TLS_PSK_WITH_ARIA_128_GCM_SHA256" +# define TLS1_RFC_PSK_WITH_ARIA_256_GCM_SHA384 "TLS_PSK_WITH_ARIA_256_GCM_SHA384" +# define TLS1_RFC_DHE_PSK_WITH_ARIA_128_GCM_SHA256 "TLS_DHE_PSK_WITH_ARIA_128_GCM_SHA256" +# define TLS1_RFC_DHE_PSK_WITH_ARIA_256_GCM_SHA384 "TLS_DHE_PSK_WITH_ARIA_256_GCM_SHA384" +# define TLS1_RFC_RSA_PSK_WITH_ARIA_128_GCM_SHA256 "TLS_RSA_PSK_WITH_ARIA_128_GCM_SHA256" +# define TLS1_RFC_RSA_PSK_WITH_ARIA_256_GCM_SHA384 "TLS_RSA_PSK_WITH_ARIA_256_GCM_SHA384" + + +/* + * XXX Backward compatibility alert: Older versions of OpenSSL gave some DHE + * ciphers names with "EDH" instead of "DHE". Going forward, we should be + * using DHE everywhere, though we may indefinitely maintain aliases for + * users or configurations that used "EDH" + */ +# define TLS1_TXT_DHE_DSS_WITH_RC4_128_SHA "DHE-DSS-RC4-SHA" + +# define TLS1_TXT_PSK_WITH_NULL_SHA "PSK-NULL-SHA" +# define TLS1_TXT_DHE_PSK_WITH_NULL_SHA "DHE-PSK-NULL-SHA" +# define TLS1_TXT_RSA_PSK_WITH_NULL_SHA "RSA-PSK-NULL-SHA" + +/* AES ciphersuites from RFC3268 */ +# define TLS1_TXT_RSA_WITH_AES_128_SHA "AES128-SHA" +# define TLS1_TXT_DH_DSS_WITH_AES_128_SHA "DH-DSS-AES128-SHA" +# define TLS1_TXT_DH_RSA_WITH_AES_128_SHA "DH-RSA-AES128-SHA" +# define TLS1_TXT_DHE_DSS_WITH_AES_128_SHA "DHE-DSS-AES128-SHA" +# define TLS1_TXT_DHE_RSA_WITH_AES_128_SHA "DHE-RSA-AES128-SHA" +# define TLS1_TXT_ADH_WITH_AES_128_SHA "ADH-AES128-SHA" + +# define TLS1_TXT_RSA_WITH_AES_256_SHA "AES256-SHA" +# define TLS1_TXT_DH_DSS_WITH_AES_256_SHA "DH-DSS-AES256-SHA" +# define TLS1_TXT_DH_RSA_WITH_AES_256_SHA "DH-RSA-AES256-SHA" +# define TLS1_TXT_DHE_DSS_WITH_AES_256_SHA "DHE-DSS-AES256-SHA" +# define TLS1_TXT_DHE_RSA_WITH_AES_256_SHA "DHE-RSA-AES256-SHA" +# define TLS1_TXT_ADH_WITH_AES_256_SHA "ADH-AES256-SHA" + +/* ECC ciphersuites from RFC4492 */ +# define TLS1_TXT_ECDH_ECDSA_WITH_NULL_SHA "ECDH-ECDSA-NULL-SHA" +# define TLS1_TXT_ECDH_ECDSA_WITH_RC4_128_SHA "ECDH-ECDSA-RC4-SHA" +# define TLS1_TXT_ECDH_ECDSA_WITH_DES_192_CBC3_SHA "ECDH-ECDSA-DES-CBC3-SHA" +# define TLS1_TXT_ECDH_ECDSA_WITH_AES_128_CBC_SHA "ECDH-ECDSA-AES128-SHA" +# define TLS1_TXT_ECDH_ECDSA_WITH_AES_256_CBC_SHA "ECDH-ECDSA-AES256-SHA" + +# define TLS1_TXT_ECDHE_ECDSA_WITH_NULL_SHA "ECDHE-ECDSA-NULL-SHA" +# define TLS1_TXT_ECDHE_ECDSA_WITH_RC4_128_SHA "ECDHE-ECDSA-RC4-SHA" +# define TLS1_TXT_ECDHE_ECDSA_WITH_DES_192_CBC3_SHA "ECDHE-ECDSA-DES-CBC3-SHA" +# define TLS1_TXT_ECDHE_ECDSA_WITH_AES_128_CBC_SHA "ECDHE-ECDSA-AES128-SHA" +# define TLS1_TXT_ECDHE_ECDSA_WITH_AES_256_CBC_SHA "ECDHE-ECDSA-AES256-SHA" + +# define TLS1_TXT_ECDH_RSA_WITH_NULL_SHA "ECDH-RSA-NULL-SHA" +# define TLS1_TXT_ECDH_RSA_WITH_RC4_128_SHA "ECDH-RSA-RC4-SHA" +# define TLS1_TXT_ECDH_RSA_WITH_DES_192_CBC3_SHA "ECDH-RSA-DES-CBC3-SHA" +# define TLS1_TXT_ECDH_RSA_WITH_AES_128_CBC_SHA "ECDH-RSA-AES128-SHA" +# define TLS1_TXT_ECDH_RSA_WITH_AES_256_CBC_SHA "ECDH-RSA-AES256-SHA" + +# define TLS1_TXT_ECDHE_RSA_WITH_NULL_SHA "ECDHE-RSA-NULL-SHA" +# define TLS1_TXT_ECDHE_RSA_WITH_RC4_128_SHA "ECDHE-RSA-RC4-SHA" +# define TLS1_TXT_ECDHE_RSA_WITH_DES_192_CBC3_SHA "ECDHE-RSA-DES-CBC3-SHA" +# define TLS1_TXT_ECDHE_RSA_WITH_AES_128_CBC_SHA "ECDHE-RSA-AES128-SHA" +# define TLS1_TXT_ECDHE_RSA_WITH_AES_256_CBC_SHA "ECDHE-RSA-AES256-SHA" + +# define TLS1_TXT_ECDH_anon_WITH_NULL_SHA "AECDH-NULL-SHA" +# define TLS1_TXT_ECDH_anon_WITH_RC4_128_SHA "AECDH-RC4-SHA" +# define TLS1_TXT_ECDH_anon_WITH_DES_192_CBC3_SHA "AECDH-DES-CBC3-SHA" +# define TLS1_TXT_ECDH_anon_WITH_AES_128_CBC_SHA "AECDH-AES128-SHA" +# define TLS1_TXT_ECDH_anon_WITH_AES_256_CBC_SHA "AECDH-AES256-SHA" + +/* PSK ciphersuites from RFC 4279 */ +# define TLS1_TXT_PSK_WITH_RC4_128_SHA "PSK-RC4-SHA" +# define TLS1_TXT_PSK_WITH_3DES_EDE_CBC_SHA "PSK-3DES-EDE-CBC-SHA" +# define TLS1_TXT_PSK_WITH_AES_128_CBC_SHA "PSK-AES128-CBC-SHA" +# define TLS1_TXT_PSK_WITH_AES_256_CBC_SHA "PSK-AES256-CBC-SHA" + +# define TLS1_TXT_DHE_PSK_WITH_RC4_128_SHA "DHE-PSK-RC4-SHA" +# define TLS1_TXT_DHE_PSK_WITH_3DES_EDE_CBC_SHA "DHE-PSK-3DES-EDE-CBC-SHA" +# define TLS1_TXT_DHE_PSK_WITH_AES_128_CBC_SHA "DHE-PSK-AES128-CBC-SHA" +# define TLS1_TXT_DHE_PSK_WITH_AES_256_CBC_SHA "DHE-PSK-AES256-CBC-SHA" +# define TLS1_TXT_RSA_PSK_WITH_RC4_128_SHA "RSA-PSK-RC4-SHA" +# define TLS1_TXT_RSA_PSK_WITH_3DES_EDE_CBC_SHA "RSA-PSK-3DES-EDE-CBC-SHA" +# define TLS1_TXT_RSA_PSK_WITH_AES_128_CBC_SHA "RSA-PSK-AES128-CBC-SHA" +# define TLS1_TXT_RSA_PSK_WITH_AES_256_CBC_SHA "RSA-PSK-AES256-CBC-SHA" + +/* PSK ciphersuites from RFC 5487 */ +# define TLS1_TXT_PSK_WITH_AES_128_GCM_SHA256 "PSK-AES128-GCM-SHA256" +# define TLS1_TXT_PSK_WITH_AES_256_GCM_SHA384 "PSK-AES256-GCM-SHA384" +# define TLS1_TXT_DHE_PSK_WITH_AES_128_GCM_SHA256 "DHE-PSK-AES128-GCM-SHA256" +# define TLS1_TXT_DHE_PSK_WITH_AES_256_GCM_SHA384 "DHE-PSK-AES256-GCM-SHA384" +# define TLS1_TXT_RSA_PSK_WITH_AES_128_GCM_SHA256 "RSA-PSK-AES128-GCM-SHA256" +# define TLS1_TXT_RSA_PSK_WITH_AES_256_GCM_SHA384 "RSA-PSK-AES256-GCM-SHA384" + +# define TLS1_TXT_PSK_WITH_AES_128_CBC_SHA256 "PSK-AES128-CBC-SHA256" +# define TLS1_TXT_PSK_WITH_AES_256_CBC_SHA384 "PSK-AES256-CBC-SHA384" +# define TLS1_TXT_PSK_WITH_NULL_SHA256 "PSK-NULL-SHA256" +# define TLS1_TXT_PSK_WITH_NULL_SHA384 "PSK-NULL-SHA384" + +# define TLS1_TXT_DHE_PSK_WITH_AES_128_CBC_SHA256 "DHE-PSK-AES128-CBC-SHA256" +# define TLS1_TXT_DHE_PSK_WITH_AES_256_CBC_SHA384 "DHE-PSK-AES256-CBC-SHA384" +# define TLS1_TXT_DHE_PSK_WITH_NULL_SHA256 "DHE-PSK-NULL-SHA256" +# define TLS1_TXT_DHE_PSK_WITH_NULL_SHA384 "DHE-PSK-NULL-SHA384" + +# define TLS1_TXT_RSA_PSK_WITH_AES_128_CBC_SHA256 "RSA-PSK-AES128-CBC-SHA256" +# define TLS1_TXT_RSA_PSK_WITH_AES_256_CBC_SHA384 "RSA-PSK-AES256-CBC-SHA384" +# define TLS1_TXT_RSA_PSK_WITH_NULL_SHA256 "RSA-PSK-NULL-SHA256" +# define TLS1_TXT_RSA_PSK_WITH_NULL_SHA384 "RSA-PSK-NULL-SHA384" + +/* SRP ciphersuite from RFC 5054 */ +# define TLS1_TXT_SRP_SHA_WITH_3DES_EDE_CBC_SHA "SRP-3DES-EDE-CBC-SHA" +# define TLS1_TXT_SRP_SHA_RSA_WITH_3DES_EDE_CBC_SHA "SRP-RSA-3DES-EDE-CBC-SHA" +# define TLS1_TXT_SRP_SHA_DSS_WITH_3DES_EDE_CBC_SHA "SRP-DSS-3DES-EDE-CBC-SHA" +# define TLS1_TXT_SRP_SHA_WITH_AES_128_CBC_SHA "SRP-AES-128-CBC-SHA" +# define TLS1_TXT_SRP_SHA_RSA_WITH_AES_128_CBC_SHA "SRP-RSA-AES-128-CBC-SHA" +# define TLS1_TXT_SRP_SHA_DSS_WITH_AES_128_CBC_SHA "SRP-DSS-AES-128-CBC-SHA" +# define TLS1_TXT_SRP_SHA_WITH_AES_256_CBC_SHA "SRP-AES-256-CBC-SHA" +# define TLS1_TXT_SRP_SHA_RSA_WITH_AES_256_CBC_SHA "SRP-RSA-AES-256-CBC-SHA" +# define TLS1_TXT_SRP_SHA_DSS_WITH_AES_256_CBC_SHA "SRP-DSS-AES-256-CBC-SHA" + +/* Camellia ciphersuites from RFC4132 */ +# define TLS1_TXT_RSA_WITH_CAMELLIA_128_CBC_SHA "CAMELLIA128-SHA" +# define TLS1_TXT_DH_DSS_WITH_CAMELLIA_128_CBC_SHA "DH-DSS-CAMELLIA128-SHA" +# define TLS1_TXT_DH_RSA_WITH_CAMELLIA_128_CBC_SHA "DH-RSA-CAMELLIA128-SHA" +# define TLS1_TXT_DHE_DSS_WITH_CAMELLIA_128_CBC_SHA "DHE-DSS-CAMELLIA128-SHA" +# define TLS1_TXT_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA "DHE-RSA-CAMELLIA128-SHA" +# define TLS1_TXT_ADH_WITH_CAMELLIA_128_CBC_SHA "ADH-CAMELLIA128-SHA" + +# define TLS1_TXT_RSA_WITH_CAMELLIA_256_CBC_SHA "CAMELLIA256-SHA" +# define TLS1_TXT_DH_DSS_WITH_CAMELLIA_256_CBC_SHA "DH-DSS-CAMELLIA256-SHA" +# define TLS1_TXT_DH_RSA_WITH_CAMELLIA_256_CBC_SHA "DH-RSA-CAMELLIA256-SHA" +# define TLS1_TXT_DHE_DSS_WITH_CAMELLIA_256_CBC_SHA "DHE-DSS-CAMELLIA256-SHA" +# define TLS1_TXT_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA "DHE-RSA-CAMELLIA256-SHA" +# define TLS1_TXT_ADH_WITH_CAMELLIA_256_CBC_SHA "ADH-CAMELLIA256-SHA" + +/* TLS 1.2 Camellia SHA-256 ciphersuites from RFC5932 */ +# define TLS1_TXT_RSA_WITH_CAMELLIA_128_CBC_SHA256 "CAMELLIA128-SHA256" +# define TLS1_TXT_DH_DSS_WITH_CAMELLIA_128_CBC_SHA256 "DH-DSS-CAMELLIA128-SHA256" +# define TLS1_TXT_DH_RSA_WITH_CAMELLIA_128_CBC_SHA256 "DH-RSA-CAMELLIA128-SHA256" +# define TLS1_TXT_DHE_DSS_WITH_CAMELLIA_128_CBC_SHA256 "DHE-DSS-CAMELLIA128-SHA256" +# define TLS1_TXT_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256 "DHE-RSA-CAMELLIA128-SHA256" +# define TLS1_TXT_ADH_WITH_CAMELLIA_128_CBC_SHA256 "ADH-CAMELLIA128-SHA256" + +# define TLS1_TXT_RSA_WITH_CAMELLIA_256_CBC_SHA256 "CAMELLIA256-SHA256" +# define TLS1_TXT_DH_DSS_WITH_CAMELLIA_256_CBC_SHA256 "DH-DSS-CAMELLIA256-SHA256" +# define TLS1_TXT_DH_RSA_WITH_CAMELLIA_256_CBC_SHA256 "DH-RSA-CAMELLIA256-SHA256" +# define TLS1_TXT_DHE_DSS_WITH_CAMELLIA_256_CBC_SHA256 "DHE-DSS-CAMELLIA256-SHA256" +# define TLS1_TXT_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256 "DHE-RSA-CAMELLIA256-SHA256" +# define TLS1_TXT_ADH_WITH_CAMELLIA_256_CBC_SHA256 "ADH-CAMELLIA256-SHA256" + +# define TLS1_TXT_PSK_WITH_CAMELLIA_128_CBC_SHA256 "PSK-CAMELLIA128-SHA256" +# define TLS1_TXT_PSK_WITH_CAMELLIA_256_CBC_SHA384 "PSK-CAMELLIA256-SHA384" +# define TLS1_TXT_DHE_PSK_WITH_CAMELLIA_128_CBC_SHA256 "DHE-PSK-CAMELLIA128-SHA256" +# define TLS1_TXT_DHE_PSK_WITH_CAMELLIA_256_CBC_SHA384 "DHE-PSK-CAMELLIA256-SHA384" +# define TLS1_TXT_RSA_PSK_WITH_CAMELLIA_128_CBC_SHA256 "RSA-PSK-CAMELLIA128-SHA256" +# define TLS1_TXT_RSA_PSK_WITH_CAMELLIA_256_CBC_SHA384 "RSA-PSK-CAMELLIA256-SHA384" +# define TLS1_TXT_ECDHE_PSK_WITH_CAMELLIA_128_CBC_SHA256 "ECDHE-PSK-CAMELLIA128-SHA256" +# define TLS1_TXT_ECDHE_PSK_WITH_CAMELLIA_256_CBC_SHA384 "ECDHE-PSK-CAMELLIA256-SHA384" + +/* SEED ciphersuites from RFC4162 */ +# define TLS1_TXT_RSA_WITH_SEED_SHA "SEED-SHA" +# define TLS1_TXT_DH_DSS_WITH_SEED_SHA "DH-DSS-SEED-SHA" +# define TLS1_TXT_DH_RSA_WITH_SEED_SHA "DH-RSA-SEED-SHA" +# define TLS1_TXT_DHE_DSS_WITH_SEED_SHA "DHE-DSS-SEED-SHA" +# define TLS1_TXT_DHE_RSA_WITH_SEED_SHA "DHE-RSA-SEED-SHA" +# define TLS1_TXT_ADH_WITH_SEED_SHA "ADH-SEED-SHA" + +/* TLS v1.2 ciphersuites */ +# define TLS1_TXT_RSA_WITH_NULL_SHA256 "NULL-SHA256" +# define TLS1_TXT_RSA_WITH_AES_128_SHA256 "AES128-SHA256" +# define TLS1_TXT_RSA_WITH_AES_256_SHA256 "AES256-SHA256" +# define TLS1_TXT_DH_DSS_WITH_AES_128_SHA256 "DH-DSS-AES128-SHA256" +# define TLS1_TXT_DH_RSA_WITH_AES_128_SHA256 "DH-RSA-AES128-SHA256" +# define TLS1_TXT_DHE_DSS_WITH_AES_128_SHA256 "DHE-DSS-AES128-SHA256" +# define TLS1_TXT_DHE_RSA_WITH_AES_128_SHA256 "DHE-RSA-AES128-SHA256" +# define TLS1_TXT_DH_DSS_WITH_AES_256_SHA256 "DH-DSS-AES256-SHA256" +# define TLS1_TXT_DH_RSA_WITH_AES_256_SHA256 "DH-RSA-AES256-SHA256" +# define TLS1_TXT_DHE_DSS_WITH_AES_256_SHA256 "DHE-DSS-AES256-SHA256" +# define TLS1_TXT_DHE_RSA_WITH_AES_256_SHA256 "DHE-RSA-AES256-SHA256" +# define TLS1_TXT_ADH_WITH_AES_128_SHA256 "ADH-AES128-SHA256" +# define TLS1_TXT_ADH_WITH_AES_256_SHA256 "ADH-AES256-SHA256" + +/* TLS v1.2 GCM ciphersuites from RFC5288 */ +# define TLS1_TXT_RSA_WITH_AES_128_GCM_SHA256 "AES128-GCM-SHA256" +# define TLS1_TXT_RSA_WITH_AES_256_GCM_SHA384 "AES256-GCM-SHA384" +# define TLS1_TXT_DHE_RSA_WITH_AES_128_GCM_SHA256 "DHE-RSA-AES128-GCM-SHA256" +# define TLS1_TXT_DHE_RSA_WITH_AES_256_GCM_SHA384 "DHE-RSA-AES256-GCM-SHA384" +# define TLS1_TXT_DH_RSA_WITH_AES_128_GCM_SHA256 "DH-RSA-AES128-GCM-SHA256" +# define TLS1_TXT_DH_RSA_WITH_AES_256_GCM_SHA384 "DH-RSA-AES256-GCM-SHA384" +# define TLS1_TXT_DHE_DSS_WITH_AES_128_GCM_SHA256 "DHE-DSS-AES128-GCM-SHA256" +# define TLS1_TXT_DHE_DSS_WITH_AES_256_GCM_SHA384 "DHE-DSS-AES256-GCM-SHA384" +# define TLS1_TXT_DH_DSS_WITH_AES_128_GCM_SHA256 "DH-DSS-AES128-GCM-SHA256" +# define TLS1_TXT_DH_DSS_WITH_AES_256_GCM_SHA384 "DH-DSS-AES256-GCM-SHA384" +# define TLS1_TXT_ADH_WITH_AES_128_GCM_SHA256 "ADH-AES128-GCM-SHA256" +# define TLS1_TXT_ADH_WITH_AES_256_GCM_SHA384 "ADH-AES256-GCM-SHA384" + +/* CCM ciphersuites from RFC6655 */ +# define TLS1_TXT_RSA_WITH_AES_128_CCM "AES128-CCM" +# define TLS1_TXT_RSA_WITH_AES_256_CCM "AES256-CCM" +# define TLS1_TXT_DHE_RSA_WITH_AES_128_CCM "DHE-RSA-AES128-CCM" +# define TLS1_TXT_DHE_RSA_WITH_AES_256_CCM "DHE-RSA-AES256-CCM" + +# define TLS1_TXT_RSA_WITH_AES_128_CCM_8 "AES128-CCM8" +# define TLS1_TXT_RSA_WITH_AES_256_CCM_8 "AES256-CCM8" +# define TLS1_TXT_DHE_RSA_WITH_AES_128_CCM_8 "DHE-RSA-AES128-CCM8" +# define TLS1_TXT_DHE_RSA_WITH_AES_256_CCM_8 "DHE-RSA-AES256-CCM8" + +# define TLS1_TXT_PSK_WITH_AES_128_CCM "PSK-AES128-CCM" +# define TLS1_TXT_PSK_WITH_AES_256_CCM "PSK-AES256-CCM" +# define TLS1_TXT_DHE_PSK_WITH_AES_128_CCM "DHE-PSK-AES128-CCM" +# define TLS1_TXT_DHE_PSK_WITH_AES_256_CCM "DHE-PSK-AES256-CCM" + +# define TLS1_TXT_PSK_WITH_AES_128_CCM_8 "PSK-AES128-CCM8" +# define TLS1_TXT_PSK_WITH_AES_256_CCM_8 "PSK-AES256-CCM8" +# define TLS1_TXT_DHE_PSK_WITH_AES_128_CCM_8 "DHE-PSK-AES128-CCM8" +# define TLS1_TXT_DHE_PSK_WITH_AES_256_CCM_8 "DHE-PSK-AES256-CCM8" + +/* CCM ciphersuites from RFC7251 */ +# define TLS1_TXT_ECDHE_ECDSA_WITH_AES_128_CCM "ECDHE-ECDSA-AES128-CCM" +# define TLS1_TXT_ECDHE_ECDSA_WITH_AES_256_CCM "ECDHE-ECDSA-AES256-CCM" +# define TLS1_TXT_ECDHE_ECDSA_WITH_AES_128_CCM_8 "ECDHE-ECDSA-AES128-CCM8" +# define TLS1_TXT_ECDHE_ECDSA_WITH_AES_256_CCM_8 "ECDHE-ECDSA-AES256-CCM8" + +/* ECDH HMAC based ciphersuites from RFC5289 */ +# define TLS1_TXT_ECDHE_ECDSA_WITH_AES_128_SHA256 "ECDHE-ECDSA-AES128-SHA256" +# define TLS1_TXT_ECDHE_ECDSA_WITH_AES_256_SHA384 "ECDHE-ECDSA-AES256-SHA384" +# define TLS1_TXT_ECDH_ECDSA_WITH_AES_128_SHA256 "ECDH-ECDSA-AES128-SHA256" +# define TLS1_TXT_ECDH_ECDSA_WITH_AES_256_SHA384 "ECDH-ECDSA-AES256-SHA384" +# define TLS1_TXT_ECDHE_RSA_WITH_AES_128_SHA256 "ECDHE-RSA-AES128-SHA256" +# define TLS1_TXT_ECDHE_RSA_WITH_AES_256_SHA384 "ECDHE-RSA-AES256-SHA384" +# define TLS1_TXT_ECDH_RSA_WITH_AES_128_SHA256 "ECDH-RSA-AES128-SHA256" +# define TLS1_TXT_ECDH_RSA_WITH_AES_256_SHA384 "ECDH-RSA-AES256-SHA384" + +/* ECDH GCM based ciphersuites from RFC5289 */ +# define TLS1_TXT_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 "ECDHE-ECDSA-AES128-GCM-SHA256" +# define TLS1_TXT_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 "ECDHE-ECDSA-AES256-GCM-SHA384" +# define TLS1_TXT_ECDH_ECDSA_WITH_AES_128_GCM_SHA256 "ECDH-ECDSA-AES128-GCM-SHA256" +# define TLS1_TXT_ECDH_ECDSA_WITH_AES_256_GCM_SHA384 "ECDH-ECDSA-AES256-GCM-SHA384" +# define TLS1_TXT_ECDHE_RSA_WITH_AES_128_GCM_SHA256 "ECDHE-RSA-AES128-GCM-SHA256" +# define TLS1_TXT_ECDHE_RSA_WITH_AES_256_GCM_SHA384 "ECDHE-RSA-AES256-GCM-SHA384" +# define TLS1_TXT_ECDH_RSA_WITH_AES_128_GCM_SHA256 "ECDH-RSA-AES128-GCM-SHA256" +# define TLS1_TXT_ECDH_RSA_WITH_AES_256_GCM_SHA384 "ECDH-RSA-AES256-GCM-SHA384" + +/* TLS v1.2 PSK GCM ciphersuites from RFC5487 */ +# define TLS1_TXT_PSK_WITH_AES_128_GCM_SHA256 "PSK-AES128-GCM-SHA256" +# define TLS1_TXT_PSK_WITH_AES_256_GCM_SHA384 "PSK-AES256-GCM-SHA384" + +/* ECDHE PSK ciphersuites from RFC 5489 */ +# define TLS1_TXT_ECDHE_PSK_WITH_RC4_128_SHA "ECDHE-PSK-RC4-SHA" +# define TLS1_TXT_ECDHE_PSK_WITH_3DES_EDE_CBC_SHA "ECDHE-PSK-3DES-EDE-CBC-SHA" +# define TLS1_TXT_ECDHE_PSK_WITH_AES_128_CBC_SHA "ECDHE-PSK-AES128-CBC-SHA" +# define TLS1_TXT_ECDHE_PSK_WITH_AES_256_CBC_SHA "ECDHE-PSK-AES256-CBC-SHA" + +# define TLS1_TXT_ECDHE_PSK_WITH_AES_128_CBC_SHA256 "ECDHE-PSK-AES128-CBC-SHA256" +# define TLS1_TXT_ECDHE_PSK_WITH_AES_256_CBC_SHA384 "ECDHE-PSK-AES256-CBC-SHA384" + +# define TLS1_TXT_ECDHE_PSK_WITH_NULL_SHA "ECDHE-PSK-NULL-SHA" +# define TLS1_TXT_ECDHE_PSK_WITH_NULL_SHA256 "ECDHE-PSK-NULL-SHA256" +# define TLS1_TXT_ECDHE_PSK_WITH_NULL_SHA384 "ECDHE-PSK-NULL-SHA384" + +/* Camellia-CBC ciphersuites from RFC6367 */ +# define TLS1_TXT_ECDHE_ECDSA_WITH_CAMELLIA_128_CBC_SHA256 "ECDHE-ECDSA-CAMELLIA128-SHA256" +# define TLS1_TXT_ECDHE_ECDSA_WITH_CAMELLIA_256_CBC_SHA384 "ECDHE-ECDSA-CAMELLIA256-SHA384" +# define TLS1_TXT_ECDH_ECDSA_WITH_CAMELLIA_128_CBC_SHA256 "ECDH-ECDSA-CAMELLIA128-SHA256" +# define TLS1_TXT_ECDH_ECDSA_WITH_CAMELLIA_256_CBC_SHA384 "ECDH-ECDSA-CAMELLIA256-SHA384" +# define TLS1_TXT_ECDHE_RSA_WITH_CAMELLIA_128_CBC_SHA256 "ECDHE-RSA-CAMELLIA128-SHA256" +# define TLS1_TXT_ECDHE_RSA_WITH_CAMELLIA_256_CBC_SHA384 "ECDHE-RSA-CAMELLIA256-SHA384" +# define TLS1_TXT_ECDH_RSA_WITH_CAMELLIA_128_CBC_SHA256 "ECDH-RSA-CAMELLIA128-SHA256" +# define TLS1_TXT_ECDH_RSA_WITH_CAMELLIA_256_CBC_SHA384 "ECDH-RSA-CAMELLIA256-SHA384" + +/* draft-ietf-tls-chacha20-poly1305-03 */ +# define TLS1_TXT_ECDHE_RSA_WITH_CHACHA20_POLY1305 "ECDHE-RSA-CHACHA20-POLY1305" +# define TLS1_TXT_ECDHE_ECDSA_WITH_CHACHA20_POLY1305 "ECDHE-ECDSA-CHACHA20-POLY1305" +# define TLS1_TXT_DHE_RSA_WITH_CHACHA20_POLY1305 "DHE-RSA-CHACHA20-POLY1305" +# define TLS1_TXT_PSK_WITH_CHACHA20_POLY1305 "PSK-CHACHA20-POLY1305" +# define TLS1_TXT_ECDHE_PSK_WITH_CHACHA20_POLY1305 "ECDHE-PSK-CHACHA20-POLY1305" +# define TLS1_TXT_DHE_PSK_WITH_CHACHA20_POLY1305 "DHE-PSK-CHACHA20-POLY1305" +# define TLS1_TXT_RSA_PSK_WITH_CHACHA20_POLY1305 "RSA-PSK-CHACHA20-POLY1305" + +/* Aria ciphersuites from RFC6209 */ +# define TLS1_TXT_RSA_WITH_ARIA_128_GCM_SHA256 "ARIA128-GCM-SHA256" +# define TLS1_TXT_RSA_WITH_ARIA_256_GCM_SHA384 "ARIA256-GCM-SHA384" +# define TLS1_TXT_DHE_RSA_WITH_ARIA_128_GCM_SHA256 "DHE-RSA-ARIA128-GCM-SHA256" +# define TLS1_TXT_DHE_RSA_WITH_ARIA_256_GCM_SHA384 "DHE-RSA-ARIA256-GCM-SHA384" +# define TLS1_TXT_DH_RSA_WITH_ARIA_128_GCM_SHA256 "DH-RSA-ARIA128-GCM-SHA256" +# define TLS1_TXT_DH_RSA_WITH_ARIA_256_GCM_SHA384 "DH-RSA-ARIA256-GCM-SHA384" +# define TLS1_TXT_DHE_DSS_WITH_ARIA_128_GCM_SHA256 "DHE-DSS-ARIA128-GCM-SHA256" +# define TLS1_TXT_DHE_DSS_WITH_ARIA_256_GCM_SHA384 "DHE-DSS-ARIA256-GCM-SHA384" +# define TLS1_TXT_DH_DSS_WITH_ARIA_128_GCM_SHA256 "DH-DSS-ARIA128-GCM-SHA256" +# define TLS1_TXT_DH_DSS_WITH_ARIA_256_GCM_SHA384 "DH-DSS-ARIA256-GCM-SHA384" +# define TLS1_TXT_DH_anon_WITH_ARIA_128_GCM_SHA256 "ADH-ARIA128-GCM-SHA256" +# define TLS1_TXT_DH_anon_WITH_ARIA_256_GCM_SHA384 "ADH-ARIA256-GCM-SHA384" +# define TLS1_TXT_ECDHE_ECDSA_WITH_ARIA_128_GCM_SHA256 "ECDHE-ECDSA-ARIA128-GCM-SHA256" +# define TLS1_TXT_ECDHE_ECDSA_WITH_ARIA_256_GCM_SHA384 "ECDHE-ECDSA-ARIA256-GCM-SHA384" +# define TLS1_TXT_ECDH_ECDSA_WITH_ARIA_128_GCM_SHA256 "ECDH-ECDSA-ARIA128-GCM-SHA256" +# define TLS1_TXT_ECDH_ECDSA_WITH_ARIA_256_GCM_SHA384 "ECDH-ECDSA-ARIA256-GCM-SHA384" +# define TLS1_TXT_ECDHE_RSA_WITH_ARIA_128_GCM_SHA256 "ECDHE-ARIA128-GCM-SHA256" +# define TLS1_TXT_ECDHE_RSA_WITH_ARIA_256_GCM_SHA384 "ECDHE-ARIA256-GCM-SHA384" +# define TLS1_TXT_ECDH_RSA_WITH_ARIA_128_GCM_SHA256 "ECDH-ARIA128-GCM-SHA256" +# define TLS1_TXT_ECDH_RSA_WITH_ARIA_256_GCM_SHA384 "ECDH-ARIA256-GCM-SHA384" +# define TLS1_TXT_PSK_WITH_ARIA_128_GCM_SHA256 "PSK-ARIA128-GCM-SHA256" +# define TLS1_TXT_PSK_WITH_ARIA_256_GCM_SHA384 "PSK-ARIA256-GCM-SHA384" +# define TLS1_TXT_DHE_PSK_WITH_ARIA_128_GCM_SHA256 "DHE-PSK-ARIA128-GCM-SHA256" +# define TLS1_TXT_DHE_PSK_WITH_ARIA_256_GCM_SHA384 "DHE-PSK-ARIA256-GCM-SHA384" +# define TLS1_TXT_RSA_PSK_WITH_ARIA_128_GCM_SHA256 "RSA-PSK-ARIA128-GCM-SHA256" +# define TLS1_TXT_RSA_PSK_WITH_ARIA_256_GCM_SHA384 "RSA-PSK-ARIA256-GCM-SHA384" + +# define TLS_CT_RSA_SIGN 1 +# define TLS_CT_DSS_SIGN 2 +# define TLS_CT_RSA_FIXED_DH 3 +# define TLS_CT_DSS_FIXED_DH 4 +# define TLS_CT_ECDSA_SIGN 64 +# define TLS_CT_RSA_FIXED_ECDH 65 +# define TLS_CT_ECDSA_FIXED_ECDH 66 +# define TLS_CT_GOST01_SIGN 22 +# define TLS_CT_GOST12_SIGN 238 +# define TLS_CT_GOST12_512_SIGN 239 + +/* + * when correcting this number, correct also SSL3_CT_NUMBER in ssl3.h (see + * comment there) + */ +# define TLS_CT_NUMBER 10 + +# if defined(SSL3_CT_NUMBER) +# if TLS_CT_NUMBER != SSL3_CT_NUMBER +# error "SSL/TLS CT_NUMBER values do not match" +# endif +# endif + +# define TLS1_FINISH_MAC_LENGTH 12 + +# define TLS_MD_MAX_CONST_SIZE 22 +# define TLS_MD_CLIENT_FINISH_CONST "client finished" +# define TLS_MD_CLIENT_FINISH_CONST_SIZE 15 +# define TLS_MD_SERVER_FINISH_CONST "server finished" +# define TLS_MD_SERVER_FINISH_CONST_SIZE 15 +# define TLS_MD_KEY_EXPANSION_CONST "key expansion" +# define TLS_MD_KEY_EXPANSION_CONST_SIZE 13 +# define TLS_MD_CLIENT_WRITE_KEY_CONST "client write key" +# define TLS_MD_CLIENT_WRITE_KEY_CONST_SIZE 16 +# define TLS_MD_SERVER_WRITE_KEY_CONST "server write key" +# define TLS_MD_SERVER_WRITE_KEY_CONST_SIZE 16 +# define TLS_MD_IV_BLOCK_CONST "IV block" +# define TLS_MD_IV_BLOCK_CONST_SIZE 8 +# define TLS_MD_MASTER_SECRET_CONST "master secret" +# define TLS_MD_MASTER_SECRET_CONST_SIZE 13 +# define TLS_MD_EXTENDED_MASTER_SECRET_CONST "extended master secret" +# define TLS_MD_EXTENDED_MASTER_SECRET_CONST_SIZE 22 + +# ifdef CHARSET_EBCDIC +# undef TLS_MD_CLIENT_FINISH_CONST +/* + * client finished + */ +# define TLS_MD_CLIENT_FINISH_CONST "\x63\x6c\x69\x65\x6e\x74\x20\x66\x69\x6e\x69\x73\x68\x65\x64" + +# undef TLS_MD_SERVER_FINISH_CONST +/* + * server finished + */ +# define TLS_MD_SERVER_FINISH_CONST "\x73\x65\x72\x76\x65\x72\x20\x66\x69\x6e\x69\x73\x68\x65\x64" + +# undef TLS_MD_SERVER_WRITE_KEY_CONST +/* + * server write key + */ +# define TLS_MD_SERVER_WRITE_KEY_CONST "\x73\x65\x72\x76\x65\x72\x20\x77\x72\x69\x74\x65\x20\x6b\x65\x79" + +# undef TLS_MD_KEY_EXPANSION_CONST +/* + * key expansion + */ +# define TLS_MD_KEY_EXPANSION_CONST "\x6b\x65\x79\x20\x65\x78\x70\x61\x6e\x73\x69\x6f\x6e" + +# undef TLS_MD_CLIENT_WRITE_KEY_CONST +/* + * client write key + */ +# define TLS_MD_CLIENT_WRITE_KEY_CONST "\x63\x6c\x69\x65\x6e\x74\x20\x77\x72\x69\x74\x65\x20\x6b\x65\x79" + +# undef TLS_MD_SERVER_WRITE_KEY_CONST +/* + * server write key + */ +# define TLS_MD_SERVER_WRITE_KEY_CONST "\x73\x65\x72\x76\x65\x72\x20\x77\x72\x69\x74\x65\x20\x6b\x65\x79" + +# undef TLS_MD_IV_BLOCK_CONST +/* + * IV block + */ +# define TLS_MD_IV_BLOCK_CONST "\x49\x56\x20\x62\x6c\x6f\x63\x6b" + +# undef TLS_MD_MASTER_SECRET_CONST +/* + * master secret + */ +# define TLS_MD_MASTER_SECRET_CONST "\x6d\x61\x73\x74\x65\x72\x20\x73\x65\x63\x72\x65\x74" +# undef TLS_MD_EXTENDED_MASTER_SECRET_CONST +/* + * extended master secret + */ +# define TLS_MD_EXTENDED_MASTER_SECRET_CONST "\x65\x78\x74\x65\x63\x64\x65\x64\x20\x6d\x61\x73\x74\x65\x72\x20\x73\x65\x63\x72\x65\x74" +# endif + +/* TLS Session Ticket extension struct */ +struct tls_session_ticket_ext_st { + unsigned short length; + void *data; +}; + +#ifdef __cplusplus +} +#endif +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/ts.h b/example/android/third_party/crypto/arm/include/openssl/ts.h new file mode 100644 index 00000000..3b58aa52 --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/ts.h @@ -0,0 +1,559 @@ +/* + * Copyright 2006-2018 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_TS_H +# define HEADER_TS_H + +# include + +# ifndef OPENSSL_NO_TS +# include +# include +# include +# include +# include +# include +# include +# include +# include +# include +# ifdef __cplusplus +extern "C" { +# endif + +# include +# include + +typedef struct TS_msg_imprint_st TS_MSG_IMPRINT; +typedef struct TS_req_st TS_REQ; +typedef struct TS_accuracy_st TS_ACCURACY; +typedef struct TS_tst_info_st TS_TST_INFO; + +/* Possible values for status. */ +# define TS_STATUS_GRANTED 0 +# define TS_STATUS_GRANTED_WITH_MODS 1 +# define TS_STATUS_REJECTION 2 +# define TS_STATUS_WAITING 3 +# define TS_STATUS_REVOCATION_WARNING 4 +# define TS_STATUS_REVOCATION_NOTIFICATION 5 + +/* Possible values for failure_info. */ +# define TS_INFO_BAD_ALG 0 +# define TS_INFO_BAD_REQUEST 2 +# define TS_INFO_BAD_DATA_FORMAT 5 +# define TS_INFO_TIME_NOT_AVAILABLE 14 +# define TS_INFO_UNACCEPTED_POLICY 15 +# define TS_INFO_UNACCEPTED_EXTENSION 16 +# define TS_INFO_ADD_INFO_NOT_AVAILABLE 17 +# define TS_INFO_SYSTEM_FAILURE 25 + + +typedef struct TS_status_info_st TS_STATUS_INFO; +typedef struct ESS_issuer_serial ESS_ISSUER_SERIAL; +typedef struct ESS_cert_id ESS_CERT_ID; +typedef struct ESS_signing_cert ESS_SIGNING_CERT; + +DEFINE_STACK_OF(ESS_CERT_ID) + +typedef struct ESS_cert_id_v2_st ESS_CERT_ID_V2; +typedef struct ESS_signing_cert_v2_st ESS_SIGNING_CERT_V2; + +DEFINE_STACK_OF(ESS_CERT_ID_V2) + +typedef struct TS_resp_st TS_RESP; + +TS_REQ *TS_REQ_new(void); +void TS_REQ_free(TS_REQ *a); +int i2d_TS_REQ(const TS_REQ *a, unsigned char **pp); +TS_REQ *d2i_TS_REQ(TS_REQ **a, const unsigned char **pp, long length); + +TS_REQ *TS_REQ_dup(TS_REQ *a); + +#ifndef OPENSSL_NO_STDIO +TS_REQ *d2i_TS_REQ_fp(FILE *fp, TS_REQ **a); +int i2d_TS_REQ_fp(FILE *fp, TS_REQ *a); +#endif +TS_REQ *d2i_TS_REQ_bio(BIO *fp, TS_REQ **a); +int i2d_TS_REQ_bio(BIO *fp, TS_REQ *a); + +TS_MSG_IMPRINT *TS_MSG_IMPRINT_new(void); +void TS_MSG_IMPRINT_free(TS_MSG_IMPRINT *a); +int i2d_TS_MSG_IMPRINT(const TS_MSG_IMPRINT *a, unsigned char **pp); +TS_MSG_IMPRINT *d2i_TS_MSG_IMPRINT(TS_MSG_IMPRINT **a, + const unsigned char **pp, long length); + +TS_MSG_IMPRINT *TS_MSG_IMPRINT_dup(TS_MSG_IMPRINT *a); + +#ifndef OPENSSL_NO_STDIO +TS_MSG_IMPRINT *d2i_TS_MSG_IMPRINT_fp(FILE *fp, TS_MSG_IMPRINT **a); +int i2d_TS_MSG_IMPRINT_fp(FILE *fp, TS_MSG_IMPRINT *a); +#endif +TS_MSG_IMPRINT *d2i_TS_MSG_IMPRINT_bio(BIO *bio, TS_MSG_IMPRINT **a); +int i2d_TS_MSG_IMPRINT_bio(BIO *bio, TS_MSG_IMPRINT *a); + +TS_RESP *TS_RESP_new(void); +void TS_RESP_free(TS_RESP *a); +int i2d_TS_RESP(const TS_RESP *a, unsigned char **pp); +TS_RESP *d2i_TS_RESP(TS_RESP **a, const unsigned char **pp, long length); +TS_TST_INFO *PKCS7_to_TS_TST_INFO(PKCS7 *token); +TS_RESP *TS_RESP_dup(TS_RESP *a); + +#ifndef OPENSSL_NO_STDIO +TS_RESP *d2i_TS_RESP_fp(FILE *fp, TS_RESP **a); +int i2d_TS_RESP_fp(FILE *fp, TS_RESP *a); +#endif +TS_RESP *d2i_TS_RESP_bio(BIO *bio, TS_RESP **a); +int i2d_TS_RESP_bio(BIO *bio, TS_RESP *a); + +TS_STATUS_INFO *TS_STATUS_INFO_new(void); +void TS_STATUS_INFO_free(TS_STATUS_INFO *a); +int i2d_TS_STATUS_INFO(const TS_STATUS_INFO *a, unsigned char **pp); +TS_STATUS_INFO *d2i_TS_STATUS_INFO(TS_STATUS_INFO **a, + const unsigned char **pp, long length); +TS_STATUS_INFO *TS_STATUS_INFO_dup(TS_STATUS_INFO *a); + +TS_TST_INFO *TS_TST_INFO_new(void); +void TS_TST_INFO_free(TS_TST_INFO *a); +int i2d_TS_TST_INFO(const TS_TST_INFO *a, unsigned char **pp); +TS_TST_INFO *d2i_TS_TST_INFO(TS_TST_INFO **a, const unsigned char **pp, + long length); +TS_TST_INFO *TS_TST_INFO_dup(TS_TST_INFO *a); + +#ifndef OPENSSL_NO_STDIO +TS_TST_INFO *d2i_TS_TST_INFO_fp(FILE *fp, TS_TST_INFO **a); +int i2d_TS_TST_INFO_fp(FILE *fp, TS_TST_INFO *a); +#endif +TS_TST_INFO *d2i_TS_TST_INFO_bio(BIO *bio, TS_TST_INFO **a); +int i2d_TS_TST_INFO_bio(BIO *bio, TS_TST_INFO *a); + +TS_ACCURACY *TS_ACCURACY_new(void); +void TS_ACCURACY_free(TS_ACCURACY *a); +int i2d_TS_ACCURACY(const TS_ACCURACY *a, unsigned char **pp); +TS_ACCURACY *d2i_TS_ACCURACY(TS_ACCURACY **a, const unsigned char **pp, + long length); +TS_ACCURACY *TS_ACCURACY_dup(TS_ACCURACY *a); + +ESS_ISSUER_SERIAL *ESS_ISSUER_SERIAL_new(void); +void ESS_ISSUER_SERIAL_free(ESS_ISSUER_SERIAL *a); +int i2d_ESS_ISSUER_SERIAL(const ESS_ISSUER_SERIAL *a, unsigned char **pp); +ESS_ISSUER_SERIAL *d2i_ESS_ISSUER_SERIAL(ESS_ISSUER_SERIAL **a, + const unsigned char **pp, + long length); +ESS_ISSUER_SERIAL *ESS_ISSUER_SERIAL_dup(ESS_ISSUER_SERIAL *a); + +ESS_CERT_ID *ESS_CERT_ID_new(void); +void ESS_CERT_ID_free(ESS_CERT_ID *a); +int i2d_ESS_CERT_ID(const ESS_CERT_ID *a, unsigned char **pp); +ESS_CERT_ID *d2i_ESS_CERT_ID(ESS_CERT_ID **a, const unsigned char **pp, + long length); +ESS_CERT_ID *ESS_CERT_ID_dup(ESS_CERT_ID *a); + +ESS_SIGNING_CERT *ESS_SIGNING_CERT_new(void); +void ESS_SIGNING_CERT_free(ESS_SIGNING_CERT *a); +int i2d_ESS_SIGNING_CERT(const ESS_SIGNING_CERT *a, unsigned char **pp); +ESS_SIGNING_CERT *d2i_ESS_SIGNING_CERT(ESS_SIGNING_CERT **a, + const unsigned char **pp, long length); +ESS_SIGNING_CERT *ESS_SIGNING_CERT_dup(ESS_SIGNING_CERT *a); + +ESS_CERT_ID_V2 *ESS_CERT_ID_V2_new(void); +void ESS_CERT_ID_V2_free(ESS_CERT_ID_V2 *a); +int i2d_ESS_CERT_ID_V2(const ESS_CERT_ID_V2 *a, unsigned char **pp); +ESS_CERT_ID_V2 *d2i_ESS_CERT_ID_V2(ESS_CERT_ID_V2 **a, + const unsigned char **pp, long length); +ESS_CERT_ID_V2 *ESS_CERT_ID_V2_dup(ESS_CERT_ID_V2 *a); + +ESS_SIGNING_CERT_V2 *ESS_SIGNING_CERT_V2_new(void); +void ESS_SIGNING_CERT_V2_free(ESS_SIGNING_CERT_V2 *a); +int i2d_ESS_SIGNING_CERT_V2(const ESS_SIGNING_CERT_V2 *a, unsigned char **pp); +ESS_SIGNING_CERT_V2 *d2i_ESS_SIGNING_CERT_V2(ESS_SIGNING_CERT_V2 **a, + const unsigned char **pp, + long length); +ESS_SIGNING_CERT_V2 *ESS_SIGNING_CERT_V2_dup(ESS_SIGNING_CERT_V2 *a); + +int TS_REQ_set_version(TS_REQ *a, long version); +long TS_REQ_get_version(const TS_REQ *a); + +int TS_STATUS_INFO_set_status(TS_STATUS_INFO *a, int i); +const ASN1_INTEGER *TS_STATUS_INFO_get0_status(const TS_STATUS_INFO *a); + +const STACK_OF(ASN1_UTF8STRING) * +TS_STATUS_INFO_get0_text(const TS_STATUS_INFO *a); + +const ASN1_BIT_STRING * +TS_STATUS_INFO_get0_failure_info(const TS_STATUS_INFO *a); + +int TS_REQ_set_msg_imprint(TS_REQ *a, TS_MSG_IMPRINT *msg_imprint); +TS_MSG_IMPRINT *TS_REQ_get_msg_imprint(TS_REQ *a); + +int TS_MSG_IMPRINT_set_algo(TS_MSG_IMPRINT *a, X509_ALGOR *alg); +X509_ALGOR *TS_MSG_IMPRINT_get_algo(TS_MSG_IMPRINT *a); + +int TS_MSG_IMPRINT_set_msg(TS_MSG_IMPRINT *a, unsigned char *d, int len); +ASN1_OCTET_STRING *TS_MSG_IMPRINT_get_msg(TS_MSG_IMPRINT *a); + +int TS_REQ_set_policy_id(TS_REQ *a, const ASN1_OBJECT *policy); +ASN1_OBJECT *TS_REQ_get_policy_id(TS_REQ *a); + +int TS_REQ_set_nonce(TS_REQ *a, const ASN1_INTEGER *nonce); +const ASN1_INTEGER *TS_REQ_get_nonce(const TS_REQ *a); + +int TS_REQ_set_cert_req(TS_REQ *a, int cert_req); +int TS_REQ_get_cert_req(const TS_REQ *a); + +STACK_OF(X509_EXTENSION) *TS_REQ_get_exts(TS_REQ *a); +void TS_REQ_ext_free(TS_REQ *a); +int TS_REQ_get_ext_count(TS_REQ *a); +int TS_REQ_get_ext_by_NID(TS_REQ *a, int nid, int lastpos); +int TS_REQ_get_ext_by_OBJ(TS_REQ *a, const ASN1_OBJECT *obj, int lastpos); +int TS_REQ_get_ext_by_critical(TS_REQ *a, int crit, int lastpos); +X509_EXTENSION *TS_REQ_get_ext(TS_REQ *a, int loc); +X509_EXTENSION *TS_REQ_delete_ext(TS_REQ *a, int loc); +int TS_REQ_add_ext(TS_REQ *a, X509_EXTENSION *ex, int loc); +void *TS_REQ_get_ext_d2i(TS_REQ *a, int nid, int *crit, int *idx); + +/* Function declarations for TS_REQ defined in ts/ts_req_print.c */ + +int TS_REQ_print_bio(BIO *bio, TS_REQ *a); + +/* Function declarations for TS_RESP defined in ts/ts_resp_utils.c */ + +int TS_RESP_set_status_info(TS_RESP *a, TS_STATUS_INFO *info); +TS_STATUS_INFO *TS_RESP_get_status_info(TS_RESP *a); + +/* Caller loses ownership of PKCS7 and TS_TST_INFO objects. */ +void TS_RESP_set_tst_info(TS_RESP *a, PKCS7 *p7, TS_TST_INFO *tst_info); +PKCS7 *TS_RESP_get_token(TS_RESP *a); +TS_TST_INFO *TS_RESP_get_tst_info(TS_RESP *a); + +int TS_TST_INFO_set_version(TS_TST_INFO *a, long version); +long TS_TST_INFO_get_version(const TS_TST_INFO *a); + +int TS_TST_INFO_set_policy_id(TS_TST_INFO *a, ASN1_OBJECT *policy_id); +ASN1_OBJECT *TS_TST_INFO_get_policy_id(TS_TST_INFO *a); + +int TS_TST_INFO_set_msg_imprint(TS_TST_INFO *a, TS_MSG_IMPRINT *msg_imprint); +TS_MSG_IMPRINT *TS_TST_INFO_get_msg_imprint(TS_TST_INFO *a); + +int TS_TST_INFO_set_serial(TS_TST_INFO *a, const ASN1_INTEGER *serial); +const ASN1_INTEGER *TS_TST_INFO_get_serial(const TS_TST_INFO *a); + +int TS_TST_INFO_set_time(TS_TST_INFO *a, const ASN1_GENERALIZEDTIME *gtime); +const ASN1_GENERALIZEDTIME *TS_TST_INFO_get_time(const TS_TST_INFO *a); + +int TS_TST_INFO_set_accuracy(TS_TST_INFO *a, TS_ACCURACY *accuracy); +TS_ACCURACY *TS_TST_INFO_get_accuracy(TS_TST_INFO *a); + +int TS_ACCURACY_set_seconds(TS_ACCURACY *a, const ASN1_INTEGER *seconds); +const ASN1_INTEGER *TS_ACCURACY_get_seconds(const TS_ACCURACY *a); + +int TS_ACCURACY_set_millis(TS_ACCURACY *a, const ASN1_INTEGER *millis); +const ASN1_INTEGER *TS_ACCURACY_get_millis(const TS_ACCURACY *a); + +int TS_ACCURACY_set_micros(TS_ACCURACY *a, const ASN1_INTEGER *micros); +const ASN1_INTEGER *TS_ACCURACY_get_micros(const TS_ACCURACY *a); + +int TS_TST_INFO_set_ordering(TS_TST_INFO *a, int ordering); +int TS_TST_INFO_get_ordering(const TS_TST_INFO *a); + +int TS_TST_INFO_set_nonce(TS_TST_INFO *a, const ASN1_INTEGER *nonce); +const ASN1_INTEGER *TS_TST_INFO_get_nonce(const TS_TST_INFO *a); + +int TS_TST_INFO_set_tsa(TS_TST_INFO *a, GENERAL_NAME *tsa); +GENERAL_NAME *TS_TST_INFO_get_tsa(TS_TST_INFO *a); + +STACK_OF(X509_EXTENSION) *TS_TST_INFO_get_exts(TS_TST_INFO *a); +void TS_TST_INFO_ext_free(TS_TST_INFO *a); +int TS_TST_INFO_get_ext_count(TS_TST_INFO *a); +int TS_TST_INFO_get_ext_by_NID(TS_TST_INFO *a, int nid, int lastpos); +int TS_TST_INFO_get_ext_by_OBJ(TS_TST_INFO *a, const ASN1_OBJECT *obj, + int lastpos); +int TS_TST_INFO_get_ext_by_critical(TS_TST_INFO *a, int crit, int lastpos); +X509_EXTENSION *TS_TST_INFO_get_ext(TS_TST_INFO *a, int loc); +X509_EXTENSION *TS_TST_INFO_delete_ext(TS_TST_INFO *a, int loc); +int TS_TST_INFO_add_ext(TS_TST_INFO *a, X509_EXTENSION *ex, int loc); +void *TS_TST_INFO_get_ext_d2i(TS_TST_INFO *a, int nid, int *crit, int *idx); + +/* + * Declarations related to response generation, defined in ts/ts_resp_sign.c. + */ + +/* Optional flags for response generation. */ + +/* Don't include the TSA name in response. */ +# define TS_TSA_NAME 0x01 + +/* Set ordering to true in response. */ +# define TS_ORDERING 0x02 + +/* + * Include the signer certificate and the other specified certificates in + * the ESS signing certificate attribute beside the PKCS7 signed data. + * Only the signer certificates is included by default. + */ +# define TS_ESS_CERT_ID_CHAIN 0x04 + +/* Forward declaration. */ +struct TS_resp_ctx; + +/* This must return a unique number less than 160 bits long. */ +typedef ASN1_INTEGER *(*TS_serial_cb) (struct TS_resp_ctx *, void *); + +/* + * This must return the seconds and microseconds since Jan 1, 1970 in the sec + * and usec variables allocated by the caller. Return non-zero for success + * and zero for failure. + */ +typedef int (*TS_time_cb) (struct TS_resp_ctx *, void *, long *sec, + long *usec); + +/* + * This must process the given extension. It can modify the TS_TST_INFO + * object of the context. Return values: !0 (processed), 0 (error, it must + * set the status info/failure info of the response). + */ +typedef int (*TS_extension_cb) (struct TS_resp_ctx *, X509_EXTENSION *, + void *); + +typedef struct TS_resp_ctx TS_RESP_CTX; + +DEFINE_STACK_OF_CONST(EVP_MD) + +/* Creates a response context that can be used for generating responses. */ +TS_RESP_CTX *TS_RESP_CTX_new(void); +void TS_RESP_CTX_free(TS_RESP_CTX *ctx); + +/* This parameter must be set. */ +int TS_RESP_CTX_set_signer_cert(TS_RESP_CTX *ctx, X509 *signer); + +/* This parameter must be set. */ +int TS_RESP_CTX_set_signer_key(TS_RESP_CTX *ctx, EVP_PKEY *key); + +int TS_RESP_CTX_set_signer_digest(TS_RESP_CTX *ctx, + const EVP_MD *signer_digest); +int TS_RESP_CTX_set_ess_cert_id_digest(TS_RESP_CTX *ctx, const EVP_MD *md); + +/* This parameter must be set. */ +int TS_RESP_CTX_set_def_policy(TS_RESP_CTX *ctx, const ASN1_OBJECT *def_policy); + +/* No additional certs are included in the response by default. */ +int TS_RESP_CTX_set_certs(TS_RESP_CTX *ctx, STACK_OF(X509) *certs); + +/* + * Adds a new acceptable policy, only the default policy is accepted by + * default. + */ +int TS_RESP_CTX_add_policy(TS_RESP_CTX *ctx, const ASN1_OBJECT *policy); + +/* + * Adds a new acceptable message digest. Note that no message digests are + * accepted by default. The md argument is shared with the caller. + */ +int TS_RESP_CTX_add_md(TS_RESP_CTX *ctx, const EVP_MD *md); + +/* Accuracy is not included by default. */ +int TS_RESP_CTX_set_accuracy(TS_RESP_CTX *ctx, + int secs, int millis, int micros); + +/* + * Clock precision digits, i.e. the number of decimal digits: '0' means sec, + * '3' msec, '6' usec, and so on. Default is 0. + */ +int TS_RESP_CTX_set_clock_precision_digits(TS_RESP_CTX *ctx, + unsigned clock_precision_digits); +/* At most we accept usec precision. */ +# define TS_MAX_CLOCK_PRECISION_DIGITS 6 + +/* Maximum status message length */ +# define TS_MAX_STATUS_LENGTH (1024 * 1024) + +/* No flags are set by default. */ +void TS_RESP_CTX_add_flags(TS_RESP_CTX *ctx, int flags); + +/* Default callback always returns a constant. */ +void TS_RESP_CTX_set_serial_cb(TS_RESP_CTX *ctx, TS_serial_cb cb, void *data); + +/* Default callback uses the gettimeofday() and gmtime() system calls. */ +void TS_RESP_CTX_set_time_cb(TS_RESP_CTX *ctx, TS_time_cb cb, void *data); + +/* + * Default callback rejects all extensions. The extension callback is called + * when the TS_TST_INFO object is already set up and not signed yet. + */ +/* FIXME: extension handling is not tested yet. */ +void TS_RESP_CTX_set_extension_cb(TS_RESP_CTX *ctx, + TS_extension_cb cb, void *data); + +/* The following methods can be used in the callbacks. */ +int TS_RESP_CTX_set_status_info(TS_RESP_CTX *ctx, + int status, const char *text); + +/* Sets the status info only if it is still TS_STATUS_GRANTED. */ +int TS_RESP_CTX_set_status_info_cond(TS_RESP_CTX *ctx, + int status, const char *text); + +int TS_RESP_CTX_add_failure_info(TS_RESP_CTX *ctx, int failure); + +/* The get methods below can be used in the extension callback. */ +TS_REQ *TS_RESP_CTX_get_request(TS_RESP_CTX *ctx); + +TS_TST_INFO *TS_RESP_CTX_get_tst_info(TS_RESP_CTX *ctx); + +/* + * Creates the signed TS_TST_INFO and puts it in TS_RESP. + * In case of errors it sets the status info properly. + * Returns NULL only in case of memory allocation/fatal error. + */ +TS_RESP *TS_RESP_create_response(TS_RESP_CTX *ctx, BIO *req_bio); + +/* + * Declarations related to response verification, + * they are defined in ts/ts_resp_verify.c. + */ + +int TS_RESP_verify_signature(PKCS7 *token, STACK_OF(X509) *certs, + X509_STORE *store, X509 **signer_out); + +/* Context structure for the generic verify method. */ + +/* Verify the signer's certificate and the signature of the response. */ +# define TS_VFY_SIGNATURE (1u << 0) +/* Verify the version number of the response. */ +# define TS_VFY_VERSION (1u << 1) +/* Verify if the policy supplied by the user matches the policy of the TSA. */ +# define TS_VFY_POLICY (1u << 2) +/* + * Verify the message imprint provided by the user. This flag should not be + * specified with TS_VFY_DATA. + */ +# define TS_VFY_IMPRINT (1u << 3) +/* + * Verify the message imprint computed by the verify method from the user + * provided data and the MD algorithm of the response. This flag should not + * be specified with TS_VFY_IMPRINT. + */ +# define TS_VFY_DATA (1u << 4) +/* Verify the nonce value. */ +# define TS_VFY_NONCE (1u << 5) +/* Verify if the TSA name field matches the signer certificate. */ +# define TS_VFY_SIGNER (1u << 6) +/* Verify if the TSA name field equals to the user provided name. */ +# define TS_VFY_TSA_NAME (1u << 7) + +/* You can use the following convenience constants. */ +# define TS_VFY_ALL_IMPRINT (TS_VFY_SIGNATURE \ + | TS_VFY_VERSION \ + | TS_VFY_POLICY \ + | TS_VFY_IMPRINT \ + | TS_VFY_NONCE \ + | TS_VFY_SIGNER \ + | TS_VFY_TSA_NAME) +# define TS_VFY_ALL_DATA (TS_VFY_SIGNATURE \ + | TS_VFY_VERSION \ + | TS_VFY_POLICY \ + | TS_VFY_DATA \ + | TS_VFY_NONCE \ + | TS_VFY_SIGNER \ + | TS_VFY_TSA_NAME) + +typedef struct TS_verify_ctx TS_VERIFY_CTX; + +int TS_RESP_verify_response(TS_VERIFY_CTX *ctx, TS_RESP *response); +int TS_RESP_verify_token(TS_VERIFY_CTX *ctx, PKCS7 *token); + +/* + * Declarations related to response verification context, + */ +TS_VERIFY_CTX *TS_VERIFY_CTX_new(void); +void TS_VERIFY_CTX_init(TS_VERIFY_CTX *ctx); +void TS_VERIFY_CTX_free(TS_VERIFY_CTX *ctx); +void TS_VERIFY_CTX_cleanup(TS_VERIFY_CTX *ctx); +int TS_VERIFY_CTX_set_flags(TS_VERIFY_CTX *ctx, int f); +int TS_VERIFY_CTX_add_flags(TS_VERIFY_CTX *ctx, int f); +BIO *TS_VERIFY_CTX_set_data(TS_VERIFY_CTX *ctx, BIO *b); +unsigned char *TS_VERIFY_CTX_set_imprint(TS_VERIFY_CTX *ctx, + unsigned char *hexstr, long len); +X509_STORE *TS_VERIFY_CTX_set_store(TS_VERIFY_CTX *ctx, X509_STORE *s); +STACK_OF(X509) *TS_VERIFY_CTS_set_certs(TS_VERIFY_CTX *ctx, STACK_OF(X509) *certs); + +/*- + * If ctx is NULL, it allocates and returns a new object, otherwise + * it returns ctx. It initialises all the members as follows: + * flags = TS_VFY_ALL_IMPRINT & ~(TS_VFY_TSA_NAME | TS_VFY_SIGNATURE) + * certs = NULL + * store = NULL + * policy = policy from the request or NULL if absent (in this case + * TS_VFY_POLICY is cleared from flags as well) + * md_alg = MD algorithm from request + * imprint, imprint_len = imprint from request + * data = NULL + * nonce, nonce_len = nonce from the request or NULL if absent (in this case + * TS_VFY_NONCE is cleared from flags as well) + * tsa_name = NULL + * Important: after calling this method TS_VFY_SIGNATURE should be added! + */ +TS_VERIFY_CTX *TS_REQ_to_TS_VERIFY_CTX(TS_REQ *req, TS_VERIFY_CTX *ctx); + +/* Function declarations for TS_RESP defined in ts/ts_resp_print.c */ + +int TS_RESP_print_bio(BIO *bio, TS_RESP *a); +int TS_STATUS_INFO_print_bio(BIO *bio, TS_STATUS_INFO *a); +int TS_TST_INFO_print_bio(BIO *bio, TS_TST_INFO *a); + +/* Common utility functions defined in ts/ts_lib.c */ + +int TS_ASN1_INTEGER_print_bio(BIO *bio, const ASN1_INTEGER *num); +int TS_OBJ_print_bio(BIO *bio, const ASN1_OBJECT *obj); +int TS_ext_print_bio(BIO *bio, const STACK_OF(X509_EXTENSION) *extensions); +int TS_X509_ALGOR_print_bio(BIO *bio, const X509_ALGOR *alg); +int TS_MSG_IMPRINT_print_bio(BIO *bio, TS_MSG_IMPRINT *msg); + +/* + * Function declarations for handling configuration options, defined in + * ts/ts_conf.c + */ + +X509 *TS_CONF_load_cert(const char *file); +STACK_OF(X509) *TS_CONF_load_certs(const char *file); +EVP_PKEY *TS_CONF_load_key(const char *file, const char *pass); +const char *TS_CONF_get_tsa_section(CONF *conf, const char *section); +int TS_CONF_set_serial(CONF *conf, const char *section, TS_serial_cb cb, + TS_RESP_CTX *ctx); +#ifndef OPENSSL_NO_ENGINE +int TS_CONF_set_crypto_device(CONF *conf, const char *section, + const char *device); +int TS_CONF_set_default_engine(const char *name); +#endif +int TS_CONF_set_signer_cert(CONF *conf, const char *section, + const char *cert, TS_RESP_CTX *ctx); +int TS_CONF_set_certs(CONF *conf, const char *section, const char *certs, + TS_RESP_CTX *ctx); +int TS_CONF_set_signer_key(CONF *conf, const char *section, + const char *key, const char *pass, + TS_RESP_CTX *ctx); +int TS_CONF_set_signer_digest(CONF *conf, const char *section, + const char *md, TS_RESP_CTX *ctx); +int TS_CONF_set_def_policy(CONF *conf, const char *section, + const char *policy, TS_RESP_CTX *ctx); +int TS_CONF_set_policies(CONF *conf, const char *section, TS_RESP_CTX *ctx); +int TS_CONF_set_digests(CONF *conf, const char *section, TS_RESP_CTX *ctx); +int TS_CONF_set_accuracy(CONF *conf, const char *section, TS_RESP_CTX *ctx); +int TS_CONF_set_clock_precision_digits(CONF *conf, const char *section, + TS_RESP_CTX *ctx); +int TS_CONF_set_ordering(CONF *conf, const char *section, TS_RESP_CTX *ctx); +int TS_CONF_set_tsa_name(CONF *conf, const char *section, TS_RESP_CTX *ctx); +int TS_CONF_set_ess_cert_id_chain(CONF *conf, const char *section, + TS_RESP_CTX *ctx); +int TS_CONF_set_ess_cert_id_digest(CONF *conf, const char *section, + TS_RESP_CTX *ctx); + +# ifdef __cplusplus +} +# endif +# endif +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/tserr.h b/example/android/third_party/crypto/arm/include/openssl/tserr.h new file mode 100644 index 00000000..3e049256 --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/tserr.h @@ -0,0 +1,128 @@ +/* + * Generated by util/mkerr.pl DO NOT EDIT + * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_TSERR_H +# define HEADER_TSERR_H + +# include + +# ifndef OPENSSL_NO_TS + +# ifdef __cplusplus +extern "C" +# endif +int ERR_load_TS_strings(void); + +/* + * TS function codes. + */ +# define TS_F_DEF_SERIAL_CB 110 +# define TS_F_DEF_TIME_CB 111 +# define TS_F_ESS_ADD_SIGNING_CERT 112 +# define TS_F_ESS_ADD_SIGNING_CERT_V2 147 +# define TS_F_ESS_CERT_ID_NEW_INIT 113 +# define TS_F_ESS_CERT_ID_V2_NEW_INIT 156 +# define TS_F_ESS_SIGNING_CERT_NEW_INIT 114 +# define TS_F_ESS_SIGNING_CERT_V2_NEW_INIT 157 +# define TS_F_INT_TS_RESP_VERIFY_TOKEN 149 +# define TS_F_PKCS7_TO_TS_TST_INFO 148 +# define TS_F_TS_ACCURACY_SET_MICROS 115 +# define TS_F_TS_ACCURACY_SET_MILLIS 116 +# define TS_F_TS_ACCURACY_SET_SECONDS 117 +# define TS_F_TS_CHECK_IMPRINTS 100 +# define TS_F_TS_CHECK_NONCES 101 +# define TS_F_TS_CHECK_POLICY 102 +# define TS_F_TS_CHECK_SIGNING_CERTS 103 +# define TS_F_TS_CHECK_STATUS_INFO 104 +# define TS_F_TS_COMPUTE_IMPRINT 145 +# define TS_F_TS_CONF_INVALID 151 +# define TS_F_TS_CONF_LOAD_CERT 153 +# define TS_F_TS_CONF_LOAD_CERTS 154 +# define TS_F_TS_CONF_LOAD_KEY 155 +# define TS_F_TS_CONF_LOOKUP_FAIL 152 +# define TS_F_TS_CONF_SET_DEFAULT_ENGINE 146 +# define TS_F_TS_GET_STATUS_TEXT 105 +# define TS_F_TS_MSG_IMPRINT_SET_ALGO 118 +# define TS_F_TS_REQ_SET_MSG_IMPRINT 119 +# define TS_F_TS_REQ_SET_NONCE 120 +# define TS_F_TS_REQ_SET_POLICY_ID 121 +# define TS_F_TS_RESP_CREATE_RESPONSE 122 +# define TS_F_TS_RESP_CREATE_TST_INFO 123 +# define TS_F_TS_RESP_CTX_ADD_FAILURE_INFO 124 +# define TS_F_TS_RESP_CTX_ADD_MD 125 +# define TS_F_TS_RESP_CTX_ADD_POLICY 126 +# define TS_F_TS_RESP_CTX_NEW 127 +# define TS_F_TS_RESP_CTX_SET_ACCURACY 128 +# define TS_F_TS_RESP_CTX_SET_CERTS 129 +# define TS_F_TS_RESP_CTX_SET_DEF_POLICY 130 +# define TS_F_TS_RESP_CTX_SET_SIGNER_CERT 131 +# define TS_F_TS_RESP_CTX_SET_STATUS_INFO 132 +# define TS_F_TS_RESP_GET_POLICY 133 +# define TS_F_TS_RESP_SET_GENTIME_WITH_PRECISION 134 +# define TS_F_TS_RESP_SET_STATUS_INFO 135 +# define TS_F_TS_RESP_SET_TST_INFO 150 +# define TS_F_TS_RESP_SIGN 136 +# define TS_F_TS_RESP_VERIFY_SIGNATURE 106 +# define TS_F_TS_TST_INFO_SET_ACCURACY 137 +# define TS_F_TS_TST_INFO_SET_MSG_IMPRINT 138 +# define TS_F_TS_TST_INFO_SET_NONCE 139 +# define TS_F_TS_TST_INFO_SET_POLICY_ID 140 +# define TS_F_TS_TST_INFO_SET_SERIAL 141 +# define TS_F_TS_TST_INFO_SET_TIME 142 +# define TS_F_TS_TST_INFO_SET_TSA 143 +# define TS_F_TS_VERIFY 108 +# define TS_F_TS_VERIFY_CERT 109 +# define TS_F_TS_VERIFY_CTX_NEW 144 + +/* + * TS reason codes. + */ +# define TS_R_BAD_PKCS7_TYPE 132 +# define TS_R_BAD_TYPE 133 +# define TS_R_CANNOT_LOAD_CERT 137 +# define TS_R_CANNOT_LOAD_KEY 138 +# define TS_R_CERTIFICATE_VERIFY_ERROR 100 +# define TS_R_COULD_NOT_SET_ENGINE 127 +# define TS_R_COULD_NOT_SET_TIME 115 +# define TS_R_DETACHED_CONTENT 134 +# define TS_R_ESS_ADD_SIGNING_CERT_ERROR 116 +# define TS_R_ESS_ADD_SIGNING_CERT_V2_ERROR 139 +# define TS_R_ESS_SIGNING_CERTIFICATE_ERROR 101 +# define TS_R_INVALID_NULL_POINTER 102 +# define TS_R_INVALID_SIGNER_CERTIFICATE_PURPOSE 117 +# define TS_R_MESSAGE_IMPRINT_MISMATCH 103 +# define TS_R_NONCE_MISMATCH 104 +# define TS_R_NONCE_NOT_RETURNED 105 +# define TS_R_NO_CONTENT 106 +# define TS_R_NO_TIME_STAMP_TOKEN 107 +# define TS_R_PKCS7_ADD_SIGNATURE_ERROR 118 +# define TS_R_PKCS7_ADD_SIGNED_ATTR_ERROR 119 +# define TS_R_PKCS7_TO_TS_TST_INFO_FAILED 129 +# define TS_R_POLICY_MISMATCH 108 +# define TS_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE 120 +# define TS_R_RESPONSE_SETUP_ERROR 121 +# define TS_R_SIGNATURE_FAILURE 109 +# define TS_R_THERE_MUST_BE_ONE_SIGNER 110 +# define TS_R_TIME_SYSCALL_ERROR 122 +# define TS_R_TOKEN_NOT_PRESENT 130 +# define TS_R_TOKEN_PRESENT 131 +# define TS_R_TSA_NAME_MISMATCH 111 +# define TS_R_TSA_UNTRUSTED 112 +# define TS_R_TST_INFO_SETUP_ERROR 123 +# define TS_R_TS_DATASIGN 124 +# define TS_R_UNACCEPTABLE_POLICY 125 +# define TS_R_UNSUPPORTED_MD_ALGORITHM 126 +# define TS_R_UNSUPPORTED_VERSION 113 +# define TS_R_VAR_BAD_VALUE 135 +# define TS_R_VAR_LOOKUP_FAILURE 136 +# define TS_R_WRONG_CONTENT_TYPE 114 + +# endif +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/txt_db.h b/example/android/third_party/crypto/arm/include/openssl/txt_db.h new file mode 100644 index 00000000..ec981a43 --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/txt_db.h @@ -0,0 +1,57 @@ +/* + * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_TXT_DB_H +# define HEADER_TXT_DB_H + +# include +# include +# include +# include + +# define DB_ERROR_OK 0 +# define DB_ERROR_MALLOC 1 +# define DB_ERROR_INDEX_CLASH 2 +# define DB_ERROR_INDEX_OUT_OF_RANGE 3 +# define DB_ERROR_NO_INDEX 4 +# define DB_ERROR_INSERT_INDEX_CLASH 5 +# define DB_ERROR_WRONG_NUM_FIELDS 6 + +#ifdef __cplusplus +extern "C" { +#endif + +typedef OPENSSL_STRING *OPENSSL_PSTRING; +DEFINE_SPECIAL_STACK_OF(OPENSSL_PSTRING, OPENSSL_STRING) + +typedef struct txt_db_st { + int num_fields; + STACK_OF(OPENSSL_PSTRING) *data; + LHASH_OF(OPENSSL_STRING) **index; + int (**qual) (OPENSSL_STRING *); + long error; + long arg1; + long arg2; + OPENSSL_STRING *arg_row; +} TXT_DB; + +TXT_DB *TXT_DB_read(BIO *in, int num); +long TXT_DB_write(BIO *out, TXT_DB *db); +int TXT_DB_create_index(TXT_DB *db, int field, int (*qual) (OPENSSL_STRING *), + OPENSSL_LH_HASHFUNC hash, OPENSSL_LH_COMPFUNC cmp); +void TXT_DB_free(TXT_DB *db); +OPENSSL_STRING *TXT_DB_get_by_index(TXT_DB *db, int idx, + OPENSSL_STRING *value); +int TXT_DB_insert(TXT_DB *db, OPENSSL_STRING *value); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/ui.h b/example/android/third_party/crypto/arm/include/openssl/ui.h new file mode 100644 index 00000000..7c721ec8 --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/ui.h @@ -0,0 +1,368 @@ +/* + * Copyright 2001-2018 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_UI_H +# define HEADER_UI_H + +# include + +# if OPENSSL_API_COMPAT < 0x10100000L +# include +# endif +# include +# include +# include +# include + +/* For compatibility reasons, the macro OPENSSL_NO_UI is currently retained */ +# if OPENSSL_API_COMPAT < 0x10200000L +# ifdef OPENSSL_NO_UI_CONSOLE +# define OPENSSL_NO_UI +# endif +# endif + +# ifdef __cplusplus +extern "C" { +# endif + +/* + * All the following functions return -1 or NULL on error and in some cases + * (UI_process()) -2 if interrupted or in some other way cancelled. When + * everything is fine, they return 0, a positive value or a non-NULL pointer, + * all depending on their purpose. + */ + +/* Creators and destructor. */ +UI *UI_new(void); +UI *UI_new_method(const UI_METHOD *method); +void UI_free(UI *ui); + +/*- + The following functions are used to add strings to be printed and prompt + strings to prompt for data. The names are UI_{add,dup}__string + and UI_{add,dup}_input_boolean. + + UI_{add,dup}__string have the following meanings: + add add a text or prompt string. The pointers given to these + functions are used verbatim, no copying is done. + dup make a copy of the text or prompt string, then add the copy + to the collection of strings in the user interface. + + The function is a name for the functionality that the given + string shall be used for. It can be one of: + input use the string as data prompt. + verify use the string as verification prompt. This + is used to verify a previous input. + info use the string for informational output. + error use the string for error output. + Honestly, there's currently no difference between info and error for the + moment. + + UI_{add,dup}_input_boolean have the same semantics for "add" and "dup", + and are typically used when one wants to prompt for a yes/no response. + + All of the functions in this group take a UI and a prompt string. + The string input and verify addition functions also take a flag argument, + a buffer for the result to end up with, a minimum input size and a maximum + input size (the result buffer MUST be large enough to be able to contain + the maximum number of characters). Additionally, the verify addition + functions takes another buffer to compare the result against. + The boolean input functions take an action description string (which should + be safe to ignore if the expected user action is obvious, for example with + a dialog box with an OK button and a Cancel button), a string of acceptable + characters to mean OK and to mean Cancel. The two last strings are checked + to make sure they don't have common characters. Additionally, the same + flag argument as for the string input is taken, as well as a result buffer. + The result buffer is required to be at least one byte long. Depending on + the answer, the first character from the OK or the Cancel character strings + will be stored in the first byte of the result buffer. No NUL will be + added, so the result is *not* a string. + + On success, the all return an index of the added information. That index + is useful when retrieving results with UI_get0_result(). */ +int UI_add_input_string(UI *ui, const char *prompt, int flags, + char *result_buf, int minsize, int maxsize); +int UI_dup_input_string(UI *ui, const char *prompt, int flags, + char *result_buf, int minsize, int maxsize); +int UI_add_verify_string(UI *ui, const char *prompt, int flags, + char *result_buf, int minsize, int maxsize, + const char *test_buf); +int UI_dup_verify_string(UI *ui, const char *prompt, int flags, + char *result_buf, int minsize, int maxsize, + const char *test_buf); +int UI_add_input_boolean(UI *ui, const char *prompt, const char *action_desc, + const char *ok_chars, const char *cancel_chars, + int flags, char *result_buf); +int UI_dup_input_boolean(UI *ui, const char *prompt, const char *action_desc, + const char *ok_chars, const char *cancel_chars, + int flags, char *result_buf); +int UI_add_info_string(UI *ui, const char *text); +int UI_dup_info_string(UI *ui, const char *text); +int UI_add_error_string(UI *ui, const char *text); +int UI_dup_error_string(UI *ui, const char *text); + +/* These are the possible flags. They can be or'ed together. */ +/* Use to have echoing of input */ +# define UI_INPUT_FLAG_ECHO 0x01 +/* + * Use a default password. Where that password is found is completely up to + * the application, it might for example be in the user data set with + * UI_add_user_data(). It is not recommended to have more than one input in + * each UI being marked with this flag, or the application might get + * confused. + */ +# define UI_INPUT_FLAG_DEFAULT_PWD 0x02 + +/*- + * The user of these routines may want to define flags of their own. The core + * UI won't look at those, but will pass them on to the method routines. They + * must use higher bits so they don't get confused with the UI bits above. + * UI_INPUT_FLAG_USER_BASE tells which is the lowest bit to use. A good + * example of use is this: + * + * #define MY_UI_FLAG1 (0x01 << UI_INPUT_FLAG_USER_BASE) + * +*/ +# define UI_INPUT_FLAG_USER_BASE 16 + +/*- + * The following function helps construct a prompt. object_desc is a + * textual short description of the object, for example "pass phrase", + * and object_name is the name of the object (might be a card name or + * a file name. + * The returned string shall always be allocated on the heap with + * OPENSSL_malloc(), and need to be free'd with OPENSSL_free(). + * + * If the ui_method doesn't contain a pointer to a user-defined prompt + * constructor, a default string is built, looking like this: + * + * "Enter {object_desc} for {object_name}:" + * + * So, if object_desc has the value "pass phrase" and object_name has + * the value "foo.key", the resulting string is: + * + * "Enter pass phrase for foo.key:" +*/ +char *UI_construct_prompt(UI *ui_method, + const char *object_desc, const char *object_name); + +/* + * The following function is used to store a pointer to user-specific data. + * Any previous such pointer will be returned and replaced. + * + * For callback purposes, this function makes a lot more sense than using + * ex_data, since the latter requires that different parts of OpenSSL or + * applications share the same ex_data index. + * + * Note that the UI_OpenSSL() method completely ignores the user data. Other + * methods may not, however. + */ +void *UI_add_user_data(UI *ui, void *user_data); +/* + * Alternatively, this function is used to duplicate the user data. + * This uses the duplicator method function. The destroy function will + * be used to free the user data in this case. + */ +int UI_dup_user_data(UI *ui, void *user_data); +/* We need a user data retrieving function as well. */ +void *UI_get0_user_data(UI *ui); + +/* Return the result associated with a prompt given with the index i. */ +const char *UI_get0_result(UI *ui, int i); +int UI_get_result_length(UI *ui, int i); + +/* When all strings have been added, process the whole thing. */ +int UI_process(UI *ui); + +/* + * Give a user interface parameterised control commands. This can be used to + * send down an integer, a data pointer or a function pointer, as well as be + * used to get information from a UI. + */ +int UI_ctrl(UI *ui, int cmd, long i, void *p, void (*f) (void)); + +/* The commands */ +/* + * Use UI_CONTROL_PRINT_ERRORS with the value 1 to have UI_process print the + * OpenSSL error stack before printing any info or added error messages and + * before any prompting. + */ +# define UI_CTRL_PRINT_ERRORS 1 +/* + * Check if a UI_process() is possible to do again with the same instance of + * a user interface. This makes UI_ctrl() return 1 if it is redoable, and 0 + * if not. + */ +# define UI_CTRL_IS_REDOABLE 2 + +/* Some methods may use extra data */ +# define UI_set_app_data(s,arg) UI_set_ex_data(s,0,arg) +# define UI_get_app_data(s) UI_get_ex_data(s,0) + +# define UI_get_ex_new_index(l, p, newf, dupf, freef) \ + CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_UI, l, p, newf, dupf, freef) +int UI_set_ex_data(UI *r, int idx, void *arg); +void *UI_get_ex_data(UI *r, int idx); + +/* Use specific methods instead of the built-in one */ +void UI_set_default_method(const UI_METHOD *meth); +const UI_METHOD *UI_get_default_method(void); +const UI_METHOD *UI_get_method(UI *ui); +const UI_METHOD *UI_set_method(UI *ui, const UI_METHOD *meth); + +# ifndef OPENSSL_NO_UI_CONSOLE + +/* The method with all the built-in thingies */ +UI_METHOD *UI_OpenSSL(void); + +# endif + +/* + * NULL method. Literally does nothing, but may serve as a placeholder + * to avoid internal default. + */ +const UI_METHOD *UI_null(void); + +/* ---------- For method writers ---------- */ +/*- + A method contains a number of functions that implement the low level + of the User Interface. The functions are: + + an opener This function starts a session, maybe by opening + a channel to a tty, or by opening a window. + a writer This function is called to write a given string, + maybe to the tty, maybe as a field label in a + window. + a flusher This function is called to flush everything that + has been output so far. It can be used to actually + display a dialog box after it has been built. + a reader This function is called to read a given prompt, + maybe from the tty, maybe from a field in a + window. Note that it's called with all string + structures, not only the prompt ones, so it must + check such things itself. + a closer This function closes the session, maybe by closing + the channel to the tty, or closing the window. + + All these functions are expected to return: + + 0 on error. + 1 on success. + -1 on out-of-band events, for example if some prompting has + been canceled (by pressing Ctrl-C, for example). This is + only checked when returned by the flusher or the reader. + + The way this is used, the opener is first called, then the writer for all + strings, then the flusher, then the reader for all strings and finally the + closer. Note that if you want to prompt from a terminal or other command + line interface, the best is to have the reader also write the prompts + instead of having the writer do it. If you want to prompt from a dialog + box, the writer can be used to build up the contents of the box, and the + flusher to actually display the box and run the event loop until all data + has been given, after which the reader only grabs the given data and puts + them back into the UI strings. + + All method functions take a UI as argument. Additionally, the writer and + the reader take a UI_STRING. +*/ + +/* + * The UI_STRING type is the data structure that contains all the needed info + * about a string or a prompt, including test data for a verification prompt. + */ +typedef struct ui_string_st UI_STRING; +DEFINE_STACK_OF(UI_STRING) + +/* + * The different types of strings that are currently supported. This is only + * needed by method authors. + */ +enum UI_string_types { + UIT_NONE = 0, + UIT_PROMPT, /* Prompt for a string */ + UIT_VERIFY, /* Prompt for a string and verify */ + UIT_BOOLEAN, /* Prompt for a yes/no response */ + UIT_INFO, /* Send info to the user */ + UIT_ERROR /* Send an error message to the user */ +}; + +/* Create and manipulate methods */ +UI_METHOD *UI_create_method(const char *name); +void UI_destroy_method(UI_METHOD *ui_method); +int UI_method_set_opener(UI_METHOD *method, int (*opener) (UI *ui)); +int UI_method_set_writer(UI_METHOD *method, + int (*writer) (UI *ui, UI_STRING *uis)); +int UI_method_set_flusher(UI_METHOD *method, int (*flusher) (UI *ui)); +int UI_method_set_reader(UI_METHOD *method, + int (*reader) (UI *ui, UI_STRING *uis)); +int UI_method_set_closer(UI_METHOD *method, int (*closer) (UI *ui)); +int UI_method_set_data_duplicator(UI_METHOD *method, + void *(*duplicator) (UI *ui, void *ui_data), + void (*destructor)(UI *ui, void *ui_data)); +int UI_method_set_prompt_constructor(UI_METHOD *method, + char *(*prompt_constructor) (UI *ui, + const char + *object_desc, + const char + *object_name)); +int UI_method_set_ex_data(UI_METHOD *method, int idx, void *data); +int (*UI_method_get_opener(const UI_METHOD *method)) (UI *); +int (*UI_method_get_writer(const UI_METHOD *method)) (UI *, UI_STRING *); +int (*UI_method_get_flusher(const UI_METHOD *method)) (UI *); +int (*UI_method_get_reader(const UI_METHOD *method)) (UI *, UI_STRING *); +int (*UI_method_get_closer(const UI_METHOD *method)) (UI *); +char *(*UI_method_get_prompt_constructor(const UI_METHOD *method)) + (UI *, const char *, const char *); +void *(*UI_method_get_data_duplicator(const UI_METHOD *method)) (UI *, void *); +void (*UI_method_get_data_destructor(const UI_METHOD *method)) (UI *, void *); +const void *UI_method_get_ex_data(const UI_METHOD *method, int idx); + +/* + * The following functions are helpers for method writers to access relevant + * data from a UI_STRING. + */ + +/* Return type of the UI_STRING */ +enum UI_string_types UI_get_string_type(UI_STRING *uis); +/* Return input flags of the UI_STRING */ +int UI_get_input_flags(UI_STRING *uis); +/* Return the actual string to output (the prompt, info or error) */ +const char *UI_get0_output_string(UI_STRING *uis); +/* + * Return the optional action string to output (the boolean prompt + * instruction) + */ +const char *UI_get0_action_string(UI_STRING *uis); +/* Return the result of a prompt */ +const char *UI_get0_result_string(UI_STRING *uis); +int UI_get_result_string_length(UI_STRING *uis); +/* + * Return the string to test the result against. Only useful with verifies. + */ +const char *UI_get0_test_string(UI_STRING *uis); +/* Return the required minimum size of the result */ +int UI_get_result_minsize(UI_STRING *uis); +/* Return the required maximum size of the result */ +int UI_get_result_maxsize(UI_STRING *uis); +/* Set the result of a UI_STRING. */ +int UI_set_result(UI *ui, UI_STRING *uis, const char *result); +int UI_set_result_ex(UI *ui, UI_STRING *uis, const char *result, int len); + +/* A couple of popular utility functions */ +int UI_UTIL_read_pw_string(char *buf, int length, const char *prompt, + int verify); +int UI_UTIL_read_pw(char *buf, char *buff, int size, const char *prompt, + int verify); +UI_METHOD *UI_UTIL_wrap_read_pem_callback(pem_password_cb *cb, int rwflag); + + +# ifdef __cplusplus +} +# endif +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/uierr.h b/example/android/third_party/crypto/arm/include/openssl/uierr.h new file mode 100644 index 00000000..72fd9a9d --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/uierr.h @@ -0,0 +1,61 @@ +/* + * Generated by util/mkerr.pl DO NOT EDIT + * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_UIERR_H +# define HEADER_UIERR_H + +# ifdef __cplusplus +extern "C" +# endif +int ERR_load_UI_strings(void); + +/* + * UI function codes. + */ +# define UI_F_CLOSE_CONSOLE 115 +# define UI_F_ECHO_CONSOLE 116 +# define UI_F_GENERAL_ALLOCATE_BOOLEAN 108 +# define UI_F_GENERAL_ALLOCATE_PROMPT 109 +# define UI_F_NOECHO_CONSOLE 117 +# define UI_F_OPEN_CONSOLE 114 +# define UI_F_UI_CONSTRUCT_PROMPT 121 +# define UI_F_UI_CREATE_METHOD 112 +# define UI_F_UI_CTRL 111 +# define UI_F_UI_DUP_ERROR_STRING 101 +# define UI_F_UI_DUP_INFO_STRING 102 +# define UI_F_UI_DUP_INPUT_BOOLEAN 110 +# define UI_F_UI_DUP_INPUT_STRING 103 +# define UI_F_UI_DUP_USER_DATA 118 +# define UI_F_UI_DUP_VERIFY_STRING 106 +# define UI_F_UI_GET0_RESULT 107 +# define UI_F_UI_GET_RESULT_LENGTH 119 +# define UI_F_UI_NEW_METHOD 104 +# define UI_F_UI_PROCESS 113 +# define UI_F_UI_SET_RESULT 105 +# define UI_F_UI_SET_RESULT_EX 120 + +/* + * UI reason codes. + */ +# define UI_R_COMMON_OK_AND_CANCEL_CHARACTERS 104 +# define UI_R_INDEX_TOO_LARGE 102 +# define UI_R_INDEX_TOO_SMALL 103 +# define UI_R_NO_RESULT_BUFFER 105 +# define UI_R_PROCESSING_ERROR 107 +# define UI_R_RESULT_TOO_LARGE 100 +# define UI_R_RESULT_TOO_SMALL 101 +# define UI_R_SYSASSIGN_ERROR 109 +# define UI_R_SYSDASSGN_ERROR 110 +# define UI_R_SYSQIOW_ERROR 111 +# define UI_R_UNKNOWN_CONTROL_COMMAND 106 +# define UI_R_UNKNOWN_TTYGET_ERRNO_VALUE 108 +# define UI_R_USER_DATA_DUPLICATION_UNSUPPORTED 112 + +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/whrlpool.h b/example/android/third_party/crypto/arm/include/openssl/whrlpool.h new file mode 100644 index 00000000..20ea3503 --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/whrlpool.h @@ -0,0 +1,48 @@ +/* + * Copyright 2005-2016 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_WHRLPOOL_H +# define HEADER_WHRLPOOL_H + +#include + +# ifndef OPENSSL_NO_WHIRLPOOL +# include +# include +# ifdef __cplusplus +extern "C" { +# endif + +# define WHIRLPOOL_DIGEST_LENGTH (512/8) +# define WHIRLPOOL_BBLOCK 512 +# define WHIRLPOOL_COUNTER (256/8) + +typedef struct { + union { + unsigned char c[WHIRLPOOL_DIGEST_LENGTH]; + /* double q is here to ensure 64-bit alignment */ + double q[WHIRLPOOL_DIGEST_LENGTH / sizeof(double)]; + } H; + unsigned char data[WHIRLPOOL_BBLOCK / 8]; + unsigned int bitoff; + size_t bitlen[WHIRLPOOL_COUNTER / sizeof(size_t)]; +} WHIRLPOOL_CTX; + +int WHIRLPOOL_Init(WHIRLPOOL_CTX *c); +int WHIRLPOOL_Update(WHIRLPOOL_CTX *c, const void *inp, size_t bytes); +void WHIRLPOOL_BitUpdate(WHIRLPOOL_CTX *c, const void *inp, size_t bits); +int WHIRLPOOL_Final(unsigned char *md, WHIRLPOOL_CTX *c); +unsigned char *WHIRLPOOL(const void *inp, size_t bytes, unsigned char *md); + +# ifdef __cplusplus +} +# endif +# endif + +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/x509.h b/example/android/third_party/crypto/arm/include/openssl/x509.h new file mode 100644 index 00000000..39ca0ba5 --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/x509.h @@ -0,0 +1,1047 @@ +/* + * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. + * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_X509_H +# define HEADER_X509_H + +# include +# include +# include +# include +# include +# include +# include +# include +# include + +# if OPENSSL_API_COMPAT < 0x10100000L +# include +# include +# include +# endif + +# include +# include + +#ifdef __cplusplus +extern "C" { +#endif + + +/* Flags for X509_get_signature_info() */ +/* Signature info is valid */ +# define X509_SIG_INFO_VALID 0x1 +/* Signature is suitable for TLS use */ +# define X509_SIG_INFO_TLS 0x2 + +# define X509_FILETYPE_PEM 1 +# define X509_FILETYPE_ASN1 2 +# define X509_FILETYPE_DEFAULT 3 + +# define X509v3_KU_DIGITAL_SIGNATURE 0x0080 +# define X509v3_KU_NON_REPUDIATION 0x0040 +# define X509v3_KU_KEY_ENCIPHERMENT 0x0020 +# define X509v3_KU_DATA_ENCIPHERMENT 0x0010 +# define X509v3_KU_KEY_AGREEMENT 0x0008 +# define X509v3_KU_KEY_CERT_SIGN 0x0004 +# define X509v3_KU_CRL_SIGN 0x0002 +# define X509v3_KU_ENCIPHER_ONLY 0x0001 +# define X509v3_KU_DECIPHER_ONLY 0x8000 +# define X509v3_KU_UNDEF 0xffff + +struct X509_algor_st { + ASN1_OBJECT *algorithm; + ASN1_TYPE *parameter; +} /* X509_ALGOR */ ; + +typedef STACK_OF(X509_ALGOR) X509_ALGORS; + +typedef struct X509_val_st { + ASN1_TIME *notBefore; + ASN1_TIME *notAfter; +} X509_VAL; + +typedef struct X509_sig_st X509_SIG; + +typedef struct X509_name_entry_st X509_NAME_ENTRY; + +DEFINE_STACK_OF(X509_NAME_ENTRY) + +DEFINE_STACK_OF(X509_NAME) + +# define X509_EX_V_NETSCAPE_HACK 0x8000 +# define X509_EX_V_INIT 0x0001 +typedef struct X509_extension_st X509_EXTENSION; + +typedef STACK_OF(X509_EXTENSION) X509_EXTENSIONS; + +DEFINE_STACK_OF(X509_EXTENSION) + +typedef struct x509_attributes_st X509_ATTRIBUTE; + +DEFINE_STACK_OF(X509_ATTRIBUTE) + +typedef struct X509_req_info_st X509_REQ_INFO; + +typedef struct X509_req_st X509_REQ; + +typedef struct x509_cert_aux_st X509_CERT_AUX; + +typedef struct x509_cinf_st X509_CINF; + +DEFINE_STACK_OF(X509) + +/* This is used for a table of trust checking functions */ + +typedef struct x509_trust_st { + int trust; + int flags; + int (*check_trust) (struct x509_trust_st *, X509 *, int); + char *name; + int arg1; + void *arg2; +} X509_TRUST; + +DEFINE_STACK_OF(X509_TRUST) + +/* standard trust ids */ + +# define X509_TRUST_DEFAULT 0 /* Only valid in purpose settings */ + +# define X509_TRUST_COMPAT 1 +# define X509_TRUST_SSL_CLIENT 2 +# define X509_TRUST_SSL_SERVER 3 +# define X509_TRUST_EMAIL 4 +# define X509_TRUST_OBJECT_SIGN 5 +# define X509_TRUST_OCSP_SIGN 6 +# define X509_TRUST_OCSP_REQUEST 7 +# define X509_TRUST_TSA 8 + +/* Keep these up to date! */ +# define X509_TRUST_MIN 1 +# define X509_TRUST_MAX 8 + +/* trust_flags values */ +# define X509_TRUST_DYNAMIC (1U << 0) +# define X509_TRUST_DYNAMIC_NAME (1U << 1) +/* No compat trust if self-signed, preempts "DO_SS" */ +# define X509_TRUST_NO_SS_COMPAT (1U << 2) +/* Compat trust if no explicit accepted trust EKUs */ +# define X509_TRUST_DO_SS_COMPAT (1U << 3) +/* Accept "anyEKU" as a wildcard trust OID */ +# define X509_TRUST_OK_ANY_EKU (1U << 4) + +/* check_trust return codes */ + +# define X509_TRUST_TRUSTED 1 +# define X509_TRUST_REJECTED 2 +# define X509_TRUST_UNTRUSTED 3 + +/* Flags for X509_print_ex() */ + +# define X509_FLAG_COMPAT 0 +# define X509_FLAG_NO_HEADER 1L +# define X509_FLAG_NO_VERSION (1L << 1) +# define X509_FLAG_NO_SERIAL (1L << 2) +# define X509_FLAG_NO_SIGNAME (1L << 3) +# define X509_FLAG_NO_ISSUER (1L << 4) +# define X509_FLAG_NO_VALIDITY (1L << 5) +# define X509_FLAG_NO_SUBJECT (1L << 6) +# define X509_FLAG_NO_PUBKEY (1L << 7) +# define X509_FLAG_NO_EXTENSIONS (1L << 8) +# define X509_FLAG_NO_SIGDUMP (1L << 9) +# define X509_FLAG_NO_AUX (1L << 10) +# define X509_FLAG_NO_ATTRIBUTES (1L << 11) +# define X509_FLAG_NO_IDS (1L << 12) + +/* Flags specific to X509_NAME_print_ex() */ + +/* The field separator information */ + +# define XN_FLAG_SEP_MASK (0xf << 16) + +# define XN_FLAG_COMPAT 0/* Traditional; use old X509_NAME_print */ +# define XN_FLAG_SEP_COMMA_PLUS (1 << 16)/* RFC2253 ,+ */ +# define XN_FLAG_SEP_CPLUS_SPC (2 << 16)/* ,+ spaced: more readable */ +# define XN_FLAG_SEP_SPLUS_SPC (3 << 16)/* ;+ spaced */ +# define XN_FLAG_SEP_MULTILINE (4 << 16)/* One line per field */ + +# define XN_FLAG_DN_REV (1 << 20)/* Reverse DN order */ + +/* How the field name is shown */ + +# define XN_FLAG_FN_MASK (0x3 << 21) + +# define XN_FLAG_FN_SN 0/* Object short name */ +# define XN_FLAG_FN_LN (1 << 21)/* Object long name */ +# define XN_FLAG_FN_OID (2 << 21)/* Always use OIDs */ +# define XN_FLAG_FN_NONE (3 << 21)/* No field names */ + +# define XN_FLAG_SPC_EQ (1 << 23)/* Put spaces round '=' */ + +/* + * This determines if we dump fields we don't recognise: RFC2253 requires + * this. + */ + +# define XN_FLAG_DUMP_UNKNOWN_FIELDS (1 << 24) + +# define XN_FLAG_FN_ALIGN (1 << 25)/* Align field names to 20 + * characters */ + +/* Complete set of RFC2253 flags */ + +# define XN_FLAG_RFC2253 (ASN1_STRFLGS_RFC2253 | \ + XN_FLAG_SEP_COMMA_PLUS | \ + XN_FLAG_DN_REV | \ + XN_FLAG_FN_SN | \ + XN_FLAG_DUMP_UNKNOWN_FIELDS) + +/* readable oneline form */ + +# define XN_FLAG_ONELINE (ASN1_STRFLGS_RFC2253 | \ + ASN1_STRFLGS_ESC_QUOTE | \ + XN_FLAG_SEP_CPLUS_SPC | \ + XN_FLAG_SPC_EQ | \ + XN_FLAG_FN_SN) + +/* readable multiline form */ + +# define XN_FLAG_MULTILINE (ASN1_STRFLGS_ESC_CTRL | \ + ASN1_STRFLGS_ESC_MSB | \ + XN_FLAG_SEP_MULTILINE | \ + XN_FLAG_SPC_EQ | \ + XN_FLAG_FN_LN | \ + XN_FLAG_FN_ALIGN) + +DEFINE_STACK_OF(X509_REVOKED) + +typedef struct X509_crl_info_st X509_CRL_INFO; + +DEFINE_STACK_OF(X509_CRL) + +typedef struct private_key_st { + int version; + /* The PKCS#8 data types */ + X509_ALGOR *enc_algor; + ASN1_OCTET_STRING *enc_pkey; /* encrypted pub key */ + /* When decrypted, the following will not be NULL */ + EVP_PKEY *dec_pkey; + /* used to encrypt and decrypt */ + int key_length; + char *key_data; + int key_free; /* true if we should auto free key_data */ + /* expanded version of 'enc_algor' */ + EVP_CIPHER_INFO cipher; +} X509_PKEY; + +typedef struct X509_info_st { + X509 *x509; + X509_CRL *crl; + X509_PKEY *x_pkey; + EVP_CIPHER_INFO enc_cipher; + int enc_len; + char *enc_data; +} X509_INFO; + +DEFINE_STACK_OF(X509_INFO) + +/* + * The next 2 structures and their 8 routines are used to manipulate Netscape's + * spki structures - useful if you are writing a CA web page + */ +typedef struct Netscape_spkac_st { + X509_PUBKEY *pubkey; + ASN1_IA5STRING *challenge; /* challenge sent in atlas >= PR2 */ +} NETSCAPE_SPKAC; + +typedef struct Netscape_spki_st { + NETSCAPE_SPKAC *spkac; /* signed public key and challenge */ + X509_ALGOR sig_algor; + ASN1_BIT_STRING *signature; +} NETSCAPE_SPKI; + +/* Netscape certificate sequence structure */ +typedef struct Netscape_certificate_sequence { + ASN1_OBJECT *type; + STACK_OF(X509) *certs; +} NETSCAPE_CERT_SEQUENCE; + +/*- Unused (and iv length is wrong) +typedef struct CBCParameter_st + { + unsigned char iv[8]; + } CBC_PARAM; +*/ + +/* Password based encryption structure */ + +typedef struct PBEPARAM_st { + ASN1_OCTET_STRING *salt; + ASN1_INTEGER *iter; +} PBEPARAM; + +/* Password based encryption V2 structures */ + +typedef struct PBE2PARAM_st { + X509_ALGOR *keyfunc; + X509_ALGOR *encryption; +} PBE2PARAM; + +typedef struct PBKDF2PARAM_st { +/* Usually OCTET STRING but could be anything */ + ASN1_TYPE *salt; + ASN1_INTEGER *iter; + ASN1_INTEGER *keylength; + X509_ALGOR *prf; +} PBKDF2PARAM; + +#ifndef OPENSSL_NO_SCRYPT +typedef struct SCRYPT_PARAMS_st { + ASN1_OCTET_STRING *salt; + ASN1_INTEGER *costParameter; + ASN1_INTEGER *blockSize; + ASN1_INTEGER *parallelizationParameter; + ASN1_INTEGER *keyLength; +} SCRYPT_PARAMS; +#endif + +#ifdef __cplusplus +} +#endif + +# include +# include + +#ifdef __cplusplus +extern "C" { +#endif + +# define X509_EXT_PACK_UNKNOWN 1 +# define X509_EXT_PACK_STRING 2 + +# define X509_extract_key(x) X509_get_pubkey(x)/*****/ +# define X509_REQ_extract_key(a) X509_REQ_get_pubkey(a) +# define X509_name_cmp(a,b) X509_NAME_cmp((a),(b)) + +void X509_CRL_set_default_method(const X509_CRL_METHOD *meth); +X509_CRL_METHOD *X509_CRL_METHOD_new(int (*crl_init) (X509_CRL *crl), + int (*crl_free) (X509_CRL *crl), + int (*crl_lookup) (X509_CRL *crl, + X509_REVOKED **ret, + ASN1_INTEGER *ser, + X509_NAME *issuer), + int (*crl_verify) (X509_CRL *crl, + EVP_PKEY *pk)); +void X509_CRL_METHOD_free(X509_CRL_METHOD *m); + +void X509_CRL_set_meth_data(X509_CRL *crl, void *dat); +void *X509_CRL_get_meth_data(X509_CRL *crl); + +const char *X509_verify_cert_error_string(long n); + +int X509_verify(X509 *a, EVP_PKEY *r); + +int X509_REQ_verify(X509_REQ *a, EVP_PKEY *r); +int X509_CRL_verify(X509_CRL *a, EVP_PKEY *r); +int NETSCAPE_SPKI_verify(NETSCAPE_SPKI *a, EVP_PKEY *r); + +NETSCAPE_SPKI *NETSCAPE_SPKI_b64_decode(const char *str, int len); +char *NETSCAPE_SPKI_b64_encode(NETSCAPE_SPKI *x); +EVP_PKEY *NETSCAPE_SPKI_get_pubkey(NETSCAPE_SPKI *x); +int NETSCAPE_SPKI_set_pubkey(NETSCAPE_SPKI *x, EVP_PKEY *pkey); + +int NETSCAPE_SPKI_print(BIO *out, NETSCAPE_SPKI *spki); + +int X509_signature_dump(BIO *bp, const ASN1_STRING *sig, int indent); +int X509_signature_print(BIO *bp, const X509_ALGOR *alg, + const ASN1_STRING *sig); + +int X509_sign(X509 *x, EVP_PKEY *pkey, const EVP_MD *md); +int X509_sign_ctx(X509 *x, EVP_MD_CTX *ctx); +# ifndef OPENSSL_NO_OCSP +int X509_http_nbio(OCSP_REQ_CTX *rctx, X509 **pcert); +# endif +int X509_REQ_sign(X509_REQ *x, EVP_PKEY *pkey, const EVP_MD *md); +int X509_REQ_sign_ctx(X509_REQ *x, EVP_MD_CTX *ctx); +int X509_CRL_sign(X509_CRL *x, EVP_PKEY *pkey, const EVP_MD *md); +int X509_CRL_sign_ctx(X509_CRL *x, EVP_MD_CTX *ctx); +# ifndef OPENSSL_NO_OCSP +int X509_CRL_http_nbio(OCSP_REQ_CTX *rctx, X509_CRL **pcrl); +# endif +int NETSCAPE_SPKI_sign(NETSCAPE_SPKI *x, EVP_PKEY *pkey, const EVP_MD *md); + +int X509_pubkey_digest(const X509 *data, const EVP_MD *type, + unsigned char *md, unsigned int *len); +int X509_digest(const X509 *data, const EVP_MD *type, + unsigned char *md, unsigned int *len); +int X509_CRL_digest(const X509_CRL *data, const EVP_MD *type, + unsigned char *md, unsigned int *len); +int X509_REQ_digest(const X509_REQ *data, const EVP_MD *type, + unsigned char *md, unsigned int *len); +int X509_NAME_digest(const X509_NAME *data, const EVP_MD *type, + unsigned char *md, unsigned int *len); + +# ifndef OPENSSL_NO_STDIO +X509 *d2i_X509_fp(FILE *fp, X509 **x509); +int i2d_X509_fp(FILE *fp, X509 *x509); +X509_CRL *d2i_X509_CRL_fp(FILE *fp, X509_CRL **crl); +int i2d_X509_CRL_fp(FILE *fp, X509_CRL *crl); +X509_REQ *d2i_X509_REQ_fp(FILE *fp, X509_REQ **req); +int i2d_X509_REQ_fp(FILE *fp, X509_REQ *req); +# ifndef OPENSSL_NO_RSA +RSA *d2i_RSAPrivateKey_fp(FILE *fp, RSA **rsa); +int i2d_RSAPrivateKey_fp(FILE *fp, RSA *rsa); +RSA *d2i_RSAPublicKey_fp(FILE *fp, RSA **rsa); +int i2d_RSAPublicKey_fp(FILE *fp, RSA *rsa); +RSA *d2i_RSA_PUBKEY_fp(FILE *fp, RSA **rsa); +int i2d_RSA_PUBKEY_fp(FILE *fp, RSA *rsa); +# endif +# ifndef OPENSSL_NO_DSA +DSA *d2i_DSA_PUBKEY_fp(FILE *fp, DSA **dsa); +int i2d_DSA_PUBKEY_fp(FILE *fp, DSA *dsa); +DSA *d2i_DSAPrivateKey_fp(FILE *fp, DSA **dsa); +int i2d_DSAPrivateKey_fp(FILE *fp, DSA *dsa); +# endif +# ifndef OPENSSL_NO_EC +EC_KEY *d2i_EC_PUBKEY_fp(FILE *fp, EC_KEY **eckey); +int i2d_EC_PUBKEY_fp(FILE *fp, EC_KEY *eckey); +EC_KEY *d2i_ECPrivateKey_fp(FILE *fp, EC_KEY **eckey); +int i2d_ECPrivateKey_fp(FILE *fp, EC_KEY *eckey); +# endif +X509_SIG *d2i_PKCS8_fp(FILE *fp, X509_SIG **p8); +int i2d_PKCS8_fp(FILE *fp, X509_SIG *p8); +PKCS8_PRIV_KEY_INFO *d2i_PKCS8_PRIV_KEY_INFO_fp(FILE *fp, + PKCS8_PRIV_KEY_INFO **p8inf); +int i2d_PKCS8_PRIV_KEY_INFO_fp(FILE *fp, PKCS8_PRIV_KEY_INFO *p8inf); +int i2d_PKCS8PrivateKeyInfo_fp(FILE *fp, EVP_PKEY *key); +int i2d_PrivateKey_fp(FILE *fp, EVP_PKEY *pkey); +EVP_PKEY *d2i_PrivateKey_fp(FILE *fp, EVP_PKEY **a); +int i2d_PUBKEY_fp(FILE *fp, EVP_PKEY *pkey); +EVP_PKEY *d2i_PUBKEY_fp(FILE *fp, EVP_PKEY **a); +# endif + +X509 *d2i_X509_bio(BIO *bp, X509 **x509); +int i2d_X509_bio(BIO *bp, X509 *x509); +X509_CRL *d2i_X509_CRL_bio(BIO *bp, X509_CRL **crl); +int i2d_X509_CRL_bio(BIO *bp, X509_CRL *crl); +X509_REQ *d2i_X509_REQ_bio(BIO *bp, X509_REQ **req); +int i2d_X509_REQ_bio(BIO *bp, X509_REQ *req); +# ifndef OPENSSL_NO_RSA +RSA *d2i_RSAPrivateKey_bio(BIO *bp, RSA **rsa); +int i2d_RSAPrivateKey_bio(BIO *bp, RSA *rsa); +RSA *d2i_RSAPublicKey_bio(BIO *bp, RSA **rsa); +int i2d_RSAPublicKey_bio(BIO *bp, RSA *rsa); +RSA *d2i_RSA_PUBKEY_bio(BIO *bp, RSA **rsa); +int i2d_RSA_PUBKEY_bio(BIO *bp, RSA *rsa); +# endif +# ifndef OPENSSL_NO_DSA +DSA *d2i_DSA_PUBKEY_bio(BIO *bp, DSA **dsa); +int i2d_DSA_PUBKEY_bio(BIO *bp, DSA *dsa); +DSA *d2i_DSAPrivateKey_bio(BIO *bp, DSA **dsa); +int i2d_DSAPrivateKey_bio(BIO *bp, DSA *dsa); +# endif +# ifndef OPENSSL_NO_EC +EC_KEY *d2i_EC_PUBKEY_bio(BIO *bp, EC_KEY **eckey); +int i2d_EC_PUBKEY_bio(BIO *bp, EC_KEY *eckey); +EC_KEY *d2i_ECPrivateKey_bio(BIO *bp, EC_KEY **eckey); +int i2d_ECPrivateKey_bio(BIO *bp, EC_KEY *eckey); +# endif +X509_SIG *d2i_PKCS8_bio(BIO *bp, X509_SIG **p8); +int i2d_PKCS8_bio(BIO *bp, X509_SIG *p8); +PKCS8_PRIV_KEY_INFO *d2i_PKCS8_PRIV_KEY_INFO_bio(BIO *bp, + PKCS8_PRIV_KEY_INFO **p8inf); +int i2d_PKCS8_PRIV_KEY_INFO_bio(BIO *bp, PKCS8_PRIV_KEY_INFO *p8inf); +int i2d_PKCS8PrivateKeyInfo_bio(BIO *bp, EVP_PKEY *key); +int i2d_PrivateKey_bio(BIO *bp, EVP_PKEY *pkey); +EVP_PKEY *d2i_PrivateKey_bio(BIO *bp, EVP_PKEY **a); +int i2d_PUBKEY_bio(BIO *bp, EVP_PKEY *pkey); +EVP_PKEY *d2i_PUBKEY_bio(BIO *bp, EVP_PKEY **a); + +X509 *X509_dup(X509 *x509); +X509_ATTRIBUTE *X509_ATTRIBUTE_dup(X509_ATTRIBUTE *xa); +X509_EXTENSION *X509_EXTENSION_dup(X509_EXTENSION *ex); +X509_CRL *X509_CRL_dup(X509_CRL *crl); +X509_REVOKED *X509_REVOKED_dup(X509_REVOKED *rev); +X509_REQ *X509_REQ_dup(X509_REQ *req); +X509_ALGOR *X509_ALGOR_dup(X509_ALGOR *xn); +int X509_ALGOR_set0(X509_ALGOR *alg, ASN1_OBJECT *aobj, int ptype, + void *pval); +void X509_ALGOR_get0(const ASN1_OBJECT **paobj, int *pptype, + const void **ppval, const X509_ALGOR *algor); +void X509_ALGOR_set_md(X509_ALGOR *alg, const EVP_MD *md); +int X509_ALGOR_cmp(const X509_ALGOR *a, const X509_ALGOR *b); + +X509_NAME *X509_NAME_dup(X509_NAME *xn); +X509_NAME_ENTRY *X509_NAME_ENTRY_dup(X509_NAME_ENTRY *ne); + +int X509_cmp_time(const ASN1_TIME *s, time_t *t); +int X509_cmp_current_time(const ASN1_TIME *s); +ASN1_TIME *X509_time_adj(ASN1_TIME *s, long adj, time_t *t); +ASN1_TIME *X509_time_adj_ex(ASN1_TIME *s, + int offset_day, long offset_sec, time_t *t); +ASN1_TIME *X509_gmtime_adj(ASN1_TIME *s, long adj); + +const char *X509_get_default_cert_area(void); +const char *X509_get_default_cert_dir(void); +const char *X509_get_default_cert_file(void); +const char *X509_get_default_cert_dir_env(void); +const char *X509_get_default_cert_file_env(void); +const char *X509_get_default_private_dir(void); + +X509_REQ *X509_to_X509_REQ(X509 *x, EVP_PKEY *pkey, const EVP_MD *md); +X509 *X509_REQ_to_X509(X509_REQ *r, int days, EVP_PKEY *pkey); + +DECLARE_ASN1_FUNCTIONS(X509_ALGOR) +DECLARE_ASN1_ENCODE_FUNCTIONS(X509_ALGORS, X509_ALGORS, X509_ALGORS) +DECLARE_ASN1_FUNCTIONS(X509_VAL) + +DECLARE_ASN1_FUNCTIONS(X509_PUBKEY) + +int X509_PUBKEY_set(X509_PUBKEY **x, EVP_PKEY *pkey); +EVP_PKEY *X509_PUBKEY_get0(X509_PUBKEY *key); +EVP_PKEY *X509_PUBKEY_get(X509_PUBKEY *key); +int X509_get_pubkey_parameters(EVP_PKEY *pkey, STACK_OF(X509) *chain); +long X509_get_pathlen(X509 *x); +int i2d_PUBKEY(EVP_PKEY *a, unsigned char **pp); +EVP_PKEY *d2i_PUBKEY(EVP_PKEY **a, const unsigned char **pp, long length); +# ifndef OPENSSL_NO_RSA +int i2d_RSA_PUBKEY(RSA *a, unsigned char **pp); +RSA *d2i_RSA_PUBKEY(RSA **a, const unsigned char **pp, long length); +# endif +# ifndef OPENSSL_NO_DSA +int i2d_DSA_PUBKEY(DSA *a, unsigned char **pp); +DSA *d2i_DSA_PUBKEY(DSA **a, const unsigned char **pp, long length); +# endif +# ifndef OPENSSL_NO_EC +int i2d_EC_PUBKEY(EC_KEY *a, unsigned char **pp); +EC_KEY *d2i_EC_PUBKEY(EC_KEY **a, const unsigned char **pp, long length); +# endif + +DECLARE_ASN1_FUNCTIONS(X509_SIG) +void X509_SIG_get0(const X509_SIG *sig, const X509_ALGOR **palg, + const ASN1_OCTET_STRING **pdigest); +void X509_SIG_getm(X509_SIG *sig, X509_ALGOR **palg, + ASN1_OCTET_STRING **pdigest); + +DECLARE_ASN1_FUNCTIONS(X509_REQ_INFO) +DECLARE_ASN1_FUNCTIONS(X509_REQ) + +DECLARE_ASN1_FUNCTIONS(X509_ATTRIBUTE) +X509_ATTRIBUTE *X509_ATTRIBUTE_create(int nid, int atrtype, void *value); + +DECLARE_ASN1_FUNCTIONS(X509_EXTENSION) +DECLARE_ASN1_ENCODE_FUNCTIONS(X509_EXTENSIONS, X509_EXTENSIONS, X509_EXTENSIONS) + +DECLARE_ASN1_FUNCTIONS(X509_NAME_ENTRY) + +DECLARE_ASN1_FUNCTIONS(X509_NAME) + +int X509_NAME_set(X509_NAME **xn, X509_NAME *name); + +DECLARE_ASN1_FUNCTIONS(X509_CINF) + +DECLARE_ASN1_FUNCTIONS(X509) +DECLARE_ASN1_FUNCTIONS(X509_CERT_AUX) + +#define X509_get_ex_new_index(l, p, newf, dupf, freef) \ + CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_X509, l, p, newf, dupf, freef) +int X509_set_ex_data(X509 *r, int idx, void *arg); +void *X509_get_ex_data(X509 *r, int idx); +int i2d_X509_AUX(X509 *a, unsigned char **pp); +X509 *d2i_X509_AUX(X509 **a, const unsigned char **pp, long length); + +int i2d_re_X509_tbs(X509 *x, unsigned char **pp); + +int X509_SIG_INFO_get(const X509_SIG_INFO *siginf, int *mdnid, int *pknid, + int *secbits, uint32_t *flags); +void X509_SIG_INFO_set(X509_SIG_INFO *siginf, int mdnid, int pknid, + int secbits, uint32_t flags); + +int X509_get_signature_info(X509 *x, int *mdnid, int *pknid, int *secbits, + uint32_t *flags); + +void X509_get0_signature(const ASN1_BIT_STRING **psig, + const X509_ALGOR **palg, const X509 *x); +int X509_get_signature_nid(const X509 *x); + +int X509_trusted(const X509 *x); +int X509_alias_set1(X509 *x, const unsigned char *name, int len); +int X509_keyid_set1(X509 *x, const unsigned char *id, int len); +unsigned char *X509_alias_get0(X509 *x, int *len); +unsigned char *X509_keyid_get0(X509 *x, int *len); +int (*X509_TRUST_set_default(int (*trust) (int, X509 *, int))) (int, X509 *, + int); +int X509_TRUST_set(int *t, int trust); +int X509_add1_trust_object(X509 *x, const ASN1_OBJECT *obj); +int X509_add1_reject_object(X509 *x, const ASN1_OBJECT *obj); +void X509_trust_clear(X509 *x); +void X509_reject_clear(X509 *x); + +STACK_OF(ASN1_OBJECT) *X509_get0_trust_objects(X509 *x); +STACK_OF(ASN1_OBJECT) *X509_get0_reject_objects(X509 *x); + +DECLARE_ASN1_FUNCTIONS(X509_REVOKED) +DECLARE_ASN1_FUNCTIONS(X509_CRL_INFO) +DECLARE_ASN1_FUNCTIONS(X509_CRL) + +int X509_CRL_add0_revoked(X509_CRL *crl, X509_REVOKED *rev); +int X509_CRL_get0_by_serial(X509_CRL *crl, + X509_REVOKED **ret, ASN1_INTEGER *serial); +int X509_CRL_get0_by_cert(X509_CRL *crl, X509_REVOKED **ret, X509 *x); + +X509_PKEY *X509_PKEY_new(void); +void X509_PKEY_free(X509_PKEY *a); + +DECLARE_ASN1_FUNCTIONS(NETSCAPE_SPKI) +DECLARE_ASN1_FUNCTIONS(NETSCAPE_SPKAC) +DECLARE_ASN1_FUNCTIONS(NETSCAPE_CERT_SEQUENCE) + +X509_INFO *X509_INFO_new(void); +void X509_INFO_free(X509_INFO *a); +char *X509_NAME_oneline(const X509_NAME *a, char *buf, int size); + +int ASN1_verify(i2d_of_void *i2d, X509_ALGOR *algor1, + ASN1_BIT_STRING *signature, char *data, EVP_PKEY *pkey); + +int ASN1_digest(i2d_of_void *i2d, const EVP_MD *type, char *data, + unsigned char *md, unsigned int *len); + +int ASN1_sign(i2d_of_void *i2d, X509_ALGOR *algor1, + X509_ALGOR *algor2, ASN1_BIT_STRING *signature, + char *data, EVP_PKEY *pkey, const EVP_MD *type); + +int ASN1_item_digest(const ASN1_ITEM *it, const EVP_MD *type, void *data, + unsigned char *md, unsigned int *len); + +int ASN1_item_verify(const ASN1_ITEM *it, X509_ALGOR *algor1, + ASN1_BIT_STRING *signature, void *data, EVP_PKEY *pkey); + +int ASN1_item_sign(const ASN1_ITEM *it, X509_ALGOR *algor1, + X509_ALGOR *algor2, ASN1_BIT_STRING *signature, void *data, + EVP_PKEY *pkey, const EVP_MD *type); +int ASN1_item_sign_ctx(const ASN1_ITEM *it, X509_ALGOR *algor1, + X509_ALGOR *algor2, ASN1_BIT_STRING *signature, + void *asn, EVP_MD_CTX *ctx); + +long X509_get_version(const X509 *x); +int X509_set_version(X509 *x, long version); +int X509_set_serialNumber(X509 *x, ASN1_INTEGER *serial); +ASN1_INTEGER *X509_get_serialNumber(X509 *x); +const ASN1_INTEGER *X509_get0_serialNumber(const X509 *x); +int X509_set_issuer_name(X509 *x, X509_NAME *name); +X509_NAME *X509_get_issuer_name(const X509 *a); +int X509_set_subject_name(X509 *x, X509_NAME *name); +X509_NAME *X509_get_subject_name(const X509 *a); +const ASN1_TIME * X509_get0_notBefore(const X509 *x); +ASN1_TIME *X509_getm_notBefore(const X509 *x); +int X509_set1_notBefore(X509 *x, const ASN1_TIME *tm); +const ASN1_TIME *X509_get0_notAfter(const X509 *x); +ASN1_TIME *X509_getm_notAfter(const X509 *x); +int X509_set1_notAfter(X509 *x, const ASN1_TIME *tm); +int X509_set_pubkey(X509 *x, EVP_PKEY *pkey); +int X509_up_ref(X509 *x); +int X509_get_signature_type(const X509 *x); + +# if OPENSSL_API_COMPAT < 0x10100000L +# define X509_get_notBefore X509_getm_notBefore +# define X509_get_notAfter X509_getm_notAfter +# define X509_set_notBefore X509_set1_notBefore +# define X509_set_notAfter X509_set1_notAfter +#endif + + +/* + * This one is only used so that a binary form can output, as in + * i2d_X509_PUBKEY(X509_get_X509_PUBKEY(x), &buf) + */ +X509_PUBKEY *X509_get_X509_PUBKEY(const X509 *x); +const STACK_OF(X509_EXTENSION) *X509_get0_extensions(const X509 *x); +void X509_get0_uids(const X509 *x, const ASN1_BIT_STRING **piuid, + const ASN1_BIT_STRING **psuid); +const X509_ALGOR *X509_get0_tbs_sigalg(const X509 *x); + +EVP_PKEY *X509_get0_pubkey(const X509 *x); +EVP_PKEY *X509_get_pubkey(X509 *x); +ASN1_BIT_STRING *X509_get0_pubkey_bitstr(const X509 *x); +int X509_certificate_type(const X509 *x, const EVP_PKEY *pubkey); + +long X509_REQ_get_version(const X509_REQ *req); +int X509_REQ_set_version(X509_REQ *x, long version); +X509_NAME *X509_REQ_get_subject_name(const X509_REQ *req); +int X509_REQ_set_subject_name(X509_REQ *req, X509_NAME *name); +void X509_REQ_get0_signature(const X509_REQ *req, const ASN1_BIT_STRING **psig, + const X509_ALGOR **palg); +int X509_REQ_get_signature_nid(const X509_REQ *req); +int i2d_re_X509_REQ_tbs(X509_REQ *req, unsigned char **pp); +int X509_REQ_set_pubkey(X509_REQ *x, EVP_PKEY *pkey); +EVP_PKEY *X509_REQ_get_pubkey(X509_REQ *req); +EVP_PKEY *X509_REQ_get0_pubkey(X509_REQ *req); +X509_PUBKEY *X509_REQ_get_X509_PUBKEY(X509_REQ *req); +int X509_REQ_extension_nid(int nid); +int *X509_REQ_get_extension_nids(void); +void X509_REQ_set_extension_nids(int *nids); +STACK_OF(X509_EXTENSION) *X509_REQ_get_extensions(X509_REQ *req); +int X509_REQ_add_extensions_nid(X509_REQ *req, STACK_OF(X509_EXTENSION) *exts, + int nid); +int X509_REQ_add_extensions(X509_REQ *req, STACK_OF(X509_EXTENSION) *exts); +int X509_REQ_get_attr_count(const X509_REQ *req); +int X509_REQ_get_attr_by_NID(const X509_REQ *req, int nid, int lastpos); +int X509_REQ_get_attr_by_OBJ(const X509_REQ *req, const ASN1_OBJECT *obj, + int lastpos); +X509_ATTRIBUTE *X509_REQ_get_attr(const X509_REQ *req, int loc); +X509_ATTRIBUTE *X509_REQ_delete_attr(X509_REQ *req, int loc); +int X509_REQ_add1_attr(X509_REQ *req, X509_ATTRIBUTE *attr); +int X509_REQ_add1_attr_by_OBJ(X509_REQ *req, + const ASN1_OBJECT *obj, int type, + const unsigned char *bytes, int len); +int X509_REQ_add1_attr_by_NID(X509_REQ *req, + int nid, int type, + const unsigned char *bytes, int len); +int X509_REQ_add1_attr_by_txt(X509_REQ *req, + const char *attrname, int type, + const unsigned char *bytes, int len); + +int X509_CRL_set_version(X509_CRL *x, long version); +int X509_CRL_set_issuer_name(X509_CRL *x, X509_NAME *name); +int X509_CRL_set1_lastUpdate(X509_CRL *x, const ASN1_TIME *tm); +int X509_CRL_set1_nextUpdate(X509_CRL *x, const ASN1_TIME *tm); +int X509_CRL_sort(X509_CRL *crl); +int X509_CRL_up_ref(X509_CRL *crl); + +# if OPENSSL_API_COMPAT < 0x10100000L +# define X509_CRL_set_lastUpdate X509_CRL_set1_lastUpdate +# define X509_CRL_set_nextUpdate X509_CRL_set1_nextUpdate +#endif + +long X509_CRL_get_version(const X509_CRL *crl); +const ASN1_TIME *X509_CRL_get0_lastUpdate(const X509_CRL *crl); +const ASN1_TIME *X509_CRL_get0_nextUpdate(const X509_CRL *crl); +DEPRECATEDIN_1_1_0(ASN1_TIME *X509_CRL_get_lastUpdate(X509_CRL *crl)) +DEPRECATEDIN_1_1_0(ASN1_TIME *X509_CRL_get_nextUpdate(X509_CRL *crl)) +X509_NAME *X509_CRL_get_issuer(const X509_CRL *crl); +const STACK_OF(X509_EXTENSION) *X509_CRL_get0_extensions(const X509_CRL *crl); +STACK_OF(X509_REVOKED) *X509_CRL_get_REVOKED(X509_CRL *crl); +void X509_CRL_get0_signature(const X509_CRL *crl, const ASN1_BIT_STRING **psig, + const X509_ALGOR **palg); +int X509_CRL_get_signature_nid(const X509_CRL *crl); +int i2d_re_X509_CRL_tbs(X509_CRL *req, unsigned char **pp); + +const ASN1_INTEGER *X509_REVOKED_get0_serialNumber(const X509_REVOKED *x); +int X509_REVOKED_set_serialNumber(X509_REVOKED *x, ASN1_INTEGER *serial); +const ASN1_TIME *X509_REVOKED_get0_revocationDate(const X509_REVOKED *x); +int X509_REVOKED_set_revocationDate(X509_REVOKED *r, ASN1_TIME *tm); +const STACK_OF(X509_EXTENSION) * +X509_REVOKED_get0_extensions(const X509_REVOKED *r); + +X509_CRL *X509_CRL_diff(X509_CRL *base, X509_CRL *newer, + EVP_PKEY *skey, const EVP_MD *md, unsigned int flags); + +int X509_REQ_check_private_key(X509_REQ *x509, EVP_PKEY *pkey); + +int X509_check_private_key(const X509 *x509, const EVP_PKEY *pkey); +int X509_chain_check_suiteb(int *perror_depth, + X509 *x, STACK_OF(X509) *chain, + unsigned long flags); +int X509_CRL_check_suiteb(X509_CRL *crl, EVP_PKEY *pk, unsigned long flags); +STACK_OF(X509) *X509_chain_up_ref(STACK_OF(X509) *chain); + +int X509_issuer_and_serial_cmp(const X509 *a, const X509 *b); +unsigned long X509_issuer_and_serial_hash(X509 *a); + +int X509_issuer_name_cmp(const X509 *a, const X509 *b); +unsigned long X509_issuer_name_hash(X509 *a); + +int X509_subject_name_cmp(const X509 *a, const X509 *b); +unsigned long X509_subject_name_hash(X509 *x); + +# ifndef OPENSSL_NO_MD5 +unsigned long X509_issuer_name_hash_old(X509 *a); +unsigned long X509_subject_name_hash_old(X509 *x); +# endif + +int X509_cmp(const X509 *a, const X509 *b); +int X509_NAME_cmp(const X509_NAME *a, const X509_NAME *b); +unsigned long X509_NAME_hash(X509_NAME *x); +unsigned long X509_NAME_hash_old(X509_NAME *x); + +int X509_CRL_cmp(const X509_CRL *a, const X509_CRL *b); +int X509_CRL_match(const X509_CRL *a, const X509_CRL *b); +int X509_aux_print(BIO *out, X509 *x, int indent); +# ifndef OPENSSL_NO_STDIO +int X509_print_ex_fp(FILE *bp, X509 *x, unsigned long nmflag, + unsigned long cflag); +int X509_print_fp(FILE *bp, X509 *x); +int X509_CRL_print_fp(FILE *bp, X509_CRL *x); +int X509_REQ_print_fp(FILE *bp, X509_REQ *req); +int X509_NAME_print_ex_fp(FILE *fp, const X509_NAME *nm, int indent, + unsigned long flags); +# endif + +int X509_NAME_print(BIO *bp, const X509_NAME *name, int obase); +int X509_NAME_print_ex(BIO *out, const X509_NAME *nm, int indent, + unsigned long flags); +int X509_print_ex(BIO *bp, X509 *x, unsigned long nmflag, + unsigned long cflag); +int X509_print(BIO *bp, X509 *x); +int X509_ocspid_print(BIO *bp, X509 *x); +int X509_CRL_print_ex(BIO *out, X509_CRL *x, unsigned long nmflag); +int X509_CRL_print(BIO *bp, X509_CRL *x); +int X509_REQ_print_ex(BIO *bp, X509_REQ *x, unsigned long nmflag, + unsigned long cflag); +int X509_REQ_print(BIO *bp, X509_REQ *req); + +int X509_NAME_entry_count(const X509_NAME *name); +int X509_NAME_get_text_by_NID(X509_NAME *name, int nid, char *buf, int len); +int X509_NAME_get_text_by_OBJ(X509_NAME *name, const ASN1_OBJECT *obj, + char *buf, int len); + +/* + * NOTE: you should be passing -1, not 0 as lastpos. The functions that use + * lastpos, search after that position on. + */ +int X509_NAME_get_index_by_NID(X509_NAME *name, int nid, int lastpos); +int X509_NAME_get_index_by_OBJ(X509_NAME *name, const ASN1_OBJECT *obj, + int lastpos); +X509_NAME_ENTRY *X509_NAME_get_entry(const X509_NAME *name, int loc); +X509_NAME_ENTRY *X509_NAME_delete_entry(X509_NAME *name, int loc); +int X509_NAME_add_entry(X509_NAME *name, const X509_NAME_ENTRY *ne, + int loc, int set); +int X509_NAME_add_entry_by_OBJ(X509_NAME *name, const ASN1_OBJECT *obj, int type, + const unsigned char *bytes, int len, int loc, + int set); +int X509_NAME_add_entry_by_NID(X509_NAME *name, int nid, int type, + const unsigned char *bytes, int len, int loc, + int set); +X509_NAME_ENTRY *X509_NAME_ENTRY_create_by_txt(X509_NAME_ENTRY **ne, + const char *field, int type, + const unsigned char *bytes, + int len); +X509_NAME_ENTRY *X509_NAME_ENTRY_create_by_NID(X509_NAME_ENTRY **ne, int nid, + int type, + const unsigned char *bytes, + int len); +int X509_NAME_add_entry_by_txt(X509_NAME *name, const char *field, int type, + const unsigned char *bytes, int len, int loc, + int set); +X509_NAME_ENTRY *X509_NAME_ENTRY_create_by_OBJ(X509_NAME_ENTRY **ne, + const ASN1_OBJECT *obj, int type, + const unsigned char *bytes, + int len); +int X509_NAME_ENTRY_set_object(X509_NAME_ENTRY *ne, const ASN1_OBJECT *obj); +int X509_NAME_ENTRY_set_data(X509_NAME_ENTRY *ne, int type, + const unsigned char *bytes, int len); +ASN1_OBJECT *X509_NAME_ENTRY_get_object(const X509_NAME_ENTRY *ne); +ASN1_STRING * X509_NAME_ENTRY_get_data(const X509_NAME_ENTRY *ne); +int X509_NAME_ENTRY_set(const X509_NAME_ENTRY *ne); + +int X509_NAME_get0_der(X509_NAME *nm, const unsigned char **pder, + size_t *pderlen); + +int X509v3_get_ext_count(const STACK_OF(X509_EXTENSION) *x); +int X509v3_get_ext_by_NID(const STACK_OF(X509_EXTENSION) *x, + int nid, int lastpos); +int X509v3_get_ext_by_OBJ(const STACK_OF(X509_EXTENSION) *x, + const ASN1_OBJECT *obj, int lastpos); +int X509v3_get_ext_by_critical(const STACK_OF(X509_EXTENSION) *x, + int crit, int lastpos); +X509_EXTENSION *X509v3_get_ext(const STACK_OF(X509_EXTENSION) *x, int loc); +X509_EXTENSION *X509v3_delete_ext(STACK_OF(X509_EXTENSION) *x, int loc); +STACK_OF(X509_EXTENSION) *X509v3_add_ext(STACK_OF(X509_EXTENSION) **x, + X509_EXTENSION *ex, int loc); + +int X509_get_ext_count(const X509 *x); +int X509_get_ext_by_NID(const X509 *x, int nid, int lastpos); +int X509_get_ext_by_OBJ(const X509 *x, const ASN1_OBJECT *obj, int lastpos); +int X509_get_ext_by_critical(const X509 *x, int crit, int lastpos); +X509_EXTENSION *X509_get_ext(const X509 *x, int loc); +X509_EXTENSION *X509_delete_ext(X509 *x, int loc); +int X509_add_ext(X509 *x, X509_EXTENSION *ex, int loc); +void *X509_get_ext_d2i(const X509 *x, int nid, int *crit, int *idx); +int X509_add1_ext_i2d(X509 *x, int nid, void *value, int crit, + unsigned long flags); + +int X509_CRL_get_ext_count(const X509_CRL *x); +int X509_CRL_get_ext_by_NID(const X509_CRL *x, int nid, int lastpos); +int X509_CRL_get_ext_by_OBJ(const X509_CRL *x, const ASN1_OBJECT *obj, + int lastpos); +int X509_CRL_get_ext_by_critical(const X509_CRL *x, int crit, int lastpos); +X509_EXTENSION *X509_CRL_get_ext(const X509_CRL *x, int loc); +X509_EXTENSION *X509_CRL_delete_ext(X509_CRL *x, int loc); +int X509_CRL_add_ext(X509_CRL *x, X509_EXTENSION *ex, int loc); +void *X509_CRL_get_ext_d2i(const X509_CRL *x, int nid, int *crit, int *idx); +int X509_CRL_add1_ext_i2d(X509_CRL *x, int nid, void *value, int crit, + unsigned long flags); + +int X509_REVOKED_get_ext_count(const X509_REVOKED *x); +int X509_REVOKED_get_ext_by_NID(const X509_REVOKED *x, int nid, int lastpos); +int X509_REVOKED_get_ext_by_OBJ(const X509_REVOKED *x, const ASN1_OBJECT *obj, + int lastpos); +int X509_REVOKED_get_ext_by_critical(const X509_REVOKED *x, int crit, + int lastpos); +X509_EXTENSION *X509_REVOKED_get_ext(const X509_REVOKED *x, int loc); +X509_EXTENSION *X509_REVOKED_delete_ext(X509_REVOKED *x, int loc); +int X509_REVOKED_add_ext(X509_REVOKED *x, X509_EXTENSION *ex, int loc); +void *X509_REVOKED_get_ext_d2i(const X509_REVOKED *x, int nid, int *crit, + int *idx); +int X509_REVOKED_add1_ext_i2d(X509_REVOKED *x, int nid, void *value, int crit, + unsigned long flags); + +X509_EXTENSION *X509_EXTENSION_create_by_NID(X509_EXTENSION **ex, + int nid, int crit, + ASN1_OCTET_STRING *data); +X509_EXTENSION *X509_EXTENSION_create_by_OBJ(X509_EXTENSION **ex, + const ASN1_OBJECT *obj, int crit, + ASN1_OCTET_STRING *data); +int X509_EXTENSION_set_object(X509_EXTENSION *ex, const ASN1_OBJECT *obj); +int X509_EXTENSION_set_critical(X509_EXTENSION *ex, int crit); +int X509_EXTENSION_set_data(X509_EXTENSION *ex, ASN1_OCTET_STRING *data); +ASN1_OBJECT *X509_EXTENSION_get_object(X509_EXTENSION *ex); +ASN1_OCTET_STRING *X509_EXTENSION_get_data(X509_EXTENSION *ne); +int X509_EXTENSION_get_critical(const X509_EXTENSION *ex); + +int X509at_get_attr_count(const STACK_OF(X509_ATTRIBUTE) *x); +int X509at_get_attr_by_NID(const STACK_OF(X509_ATTRIBUTE) *x, int nid, + int lastpos); +int X509at_get_attr_by_OBJ(const STACK_OF(X509_ATTRIBUTE) *sk, + const ASN1_OBJECT *obj, int lastpos); +X509_ATTRIBUTE *X509at_get_attr(const STACK_OF(X509_ATTRIBUTE) *x, int loc); +X509_ATTRIBUTE *X509at_delete_attr(STACK_OF(X509_ATTRIBUTE) *x, int loc); +STACK_OF(X509_ATTRIBUTE) *X509at_add1_attr(STACK_OF(X509_ATTRIBUTE) **x, + X509_ATTRIBUTE *attr); +STACK_OF(X509_ATTRIBUTE) *X509at_add1_attr_by_OBJ(STACK_OF(X509_ATTRIBUTE) + **x, const ASN1_OBJECT *obj, + int type, + const unsigned char *bytes, + int len); +STACK_OF(X509_ATTRIBUTE) *X509at_add1_attr_by_NID(STACK_OF(X509_ATTRIBUTE) + **x, int nid, int type, + const unsigned char *bytes, + int len); +STACK_OF(X509_ATTRIBUTE) *X509at_add1_attr_by_txt(STACK_OF(X509_ATTRIBUTE) + **x, const char *attrname, + int type, + const unsigned char *bytes, + int len); +void *X509at_get0_data_by_OBJ(STACK_OF(X509_ATTRIBUTE) *x, + const ASN1_OBJECT *obj, int lastpos, int type); +X509_ATTRIBUTE *X509_ATTRIBUTE_create_by_NID(X509_ATTRIBUTE **attr, int nid, + int atrtype, const void *data, + int len); +X509_ATTRIBUTE *X509_ATTRIBUTE_create_by_OBJ(X509_ATTRIBUTE **attr, + const ASN1_OBJECT *obj, + int atrtype, const void *data, + int len); +X509_ATTRIBUTE *X509_ATTRIBUTE_create_by_txt(X509_ATTRIBUTE **attr, + const char *atrname, int type, + const unsigned char *bytes, + int len); +int X509_ATTRIBUTE_set1_object(X509_ATTRIBUTE *attr, const ASN1_OBJECT *obj); +int X509_ATTRIBUTE_set1_data(X509_ATTRIBUTE *attr, int attrtype, + const void *data, int len); +void *X509_ATTRIBUTE_get0_data(X509_ATTRIBUTE *attr, int idx, int atrtype, + void *data); +int X509_ATTRIBUTE_count(const X509_ATTRIBUTE *attr); +ASN1_OBJECT *X509_ATTRIBUTE_get0_object(X509_ATTRIBUTE *attr); +ASN1_TYPE *X509_ATTRIBUTE_get0_type(X509_ATTRIBUTE *attr, int idx); + +int EVP_PKEY_get_attr_count(const EVP_PKEY *key); +int EVP_PKEY_get_attr_by_NID(const EVP_PKEY *key, int nid, int lastpos); +int EVP_PKEY_get_attr_by_OBJ(const EVP_PKEY *key, const ASN1_OBJECT *obj, + int lastpos); +X509_ATTRIBUTE *EVP_PKEY_get_attr(const EVP_PKEY *key, int loc); +X509_ATTRIBUTE *EVP_PKEY_delete_attr(EVP_PKEY *key, int loc); +int EVP_PKEY_add1_attr(EVP_PKEY *key, X509_ATTRIBUTE *attr); +int EVP_PKEY_add1_attr_by_OBJ(EVP_PKEY *key, + const ASN1_OBJECT *obj, int type, + const unsigned char *bytes, int len); +int EVP_PKEY_add1_attr_by_NID(EVP_PKEY *key, + int nid, int type, + const unsigned char *bytes, int len); +int EVP_PKEY_add1_attr_by_txt(EVP_PKEY *key, + const char *attrname, int type, + const unsigned char *bytes, int len); + +int X509_verify_cert(X509_STORE_CTX *ctx); + +/* lookup a cert from a X509 STACK */ +X509 *X509_find_by_issuer_and_serial(STACK_OF(X509) *sk, X509_NAME *name, + ASN1_INTEGER *serial); +X509 *X509_find_by_subject(STACK_OF(X509) *sk, X509_NAME *name); + +DECLARE_ASN1_FUNCTIONS(PBEPARAM) +DECLARE_ASN1_FUNCTIONS(PBE2PARAM) +DECLARE_ASN1_FUNCTIONS(PBKDF2PARAM) +#ifndef OPENSSL_NO_SCRYPT +DECLARE_ASN1_FUNCTIONS(SCRYPT_PARAMS) +#endif + +int PKCS5_pbe_set0_algor(X509_ALGOR *algor, int alg, int iter, + const unsigned char *salt, int saltlen); + +X509_ALGOR *PKCS5_pbe_set(int alg, int iter, + const unsigned char *salt, int saltlen); +X509_ALGOR *PKCS5_pbe2_set(const EVP_CIPHER *cipher, int iter, + unsigned char *salt, int saltlen); +X509_ALGOR *PKCS5_pbe2_set_iv(const EVP_CIPHER *cipher, int iter, + unsigned char *salt, int saltlen, + unsigned char *aiv, int prf_nid); + +#ifndef OPENSSL_NO_SCRYPT +X509_ALGOR *PKCS5_pbe2_set_scrypt(const EVP_CIPHER *cipher, + const unsigned char *salt, int saltlen, + unsigned char *aiv, uint64_t N, uint64_t r, + uint64_t p); +#endif + +X509_ALGOR *PKCS5_pbkdf2_set(int iter, unsigned char *salt, int saltlen, + int prf_nid, int keylen); + +/* PKCS#8 utilities */ + +DECLARE_ASN1_FUNCTIONS(PKCS8_PRIV_KEY_INFO) + +EVP_PKEY *EVP_PKCS82PKEY(const PKCS8_PRIV_KEY_INFO *p8); +PKCS8_PRIV_KEY_INFO *EVP_PKEY2PKCS8(EVP_PKEY *pkey); + +int PKCS8_pkey_set0(PKCS8_PRIV_KEY_INFO *priv, ASN1_OBJECT *aobj, + int version, int ptype, void *pval, + unsigned char *penc, int penclen); +int PKCS8_pkey_get0(const ASN1_OBJECT **ppkalg, + const unsigned char **pk, int *ppklen, + const X509_ALGOR **pa, const PKCS8_PRIV_KEY_INFO *p8); + +const STACK_OF(X509_ATTRIBUTE) * +PKCS8_pkey_get0_attrs(const PKCS8_PRIV_KEY_INFO *p8); +int PKCS8_pkey_add1_attr_by_NID(PKCS8_PRIV_KEY_INFO *p8, int nid, int type, + const unsigned char *bytes, int len); + +int X509_PUBKEY_set0_param(X509_PUBKEY *pub, ASN1_OBJECT *aobj, + int ptype, void *pval, + unsigned char *penc, int penclen); +int X509_PUBKEY_get0_param(ASN1_OBJECT **ppkalg, + const unsigned char **pk, int *ppklen, + X509_ALGOR **pa, X509_PUBKEY *pub); + +int X509_check_trust(X509 *x, int id, int flags); +int X509_TRUST_get_count(void); +X509_TRUST *X509_TRUST_get0(int idx); +int X509_TRUST_get_by_id(int id); +int X509_TRUST_add(int id, int flags, int (*ck) (X509_TRUST *, X509 *, int), + const char *name, int arg1, void *arg2); +void X509_TRUST_cleanup(void); +int X509_TRUST_get_flags(const X509_TRUST *xp); +char *X509_TRUST_get0_name(const X509_TRUST *xp); +int X509_TRUST_get_trust(const X509_TRUST *xp); + +# ifdef __cplusplus +} +# endif +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/x509_vfy.h b/example/android/third_party/crypto/arm/include/openssl/x509_vfy.h new file mode 100644 index 00000000..2adb1559 --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/x509_vfy.h @@ -0,0 +1,624 @@ +/* + * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_X509_VFY_H +# define HEADER_X509_VFY_H + +/* + * Protect against recursion, x509.h and x509_vfy.h each include the other. + */ +# ifndef HEADER_X509_H +# include +# endif + +# include +# include +# include +# include +# include + +#ifdef __cplusplus +extern "C" { +#endif + +/*- +SSL_CTX -> X509_STORE + -> X509_LOOKUP + ->X509_LOOKUP_METHOD + -> X509_LOOKUP + ->X509_LOOKUP_METHOD + +SSL -> X509_STORE_CTX + ->X509_STORE + +The X509_STORE holds the tables etc for verification stuff. +A X509_STORE_CTX is used while validating a single certificate. +The X509_STORE has X509_LOOKUPs for looking up certs. +The X509_STORE then calls a function to actually verify the +certificate chain. +*/ + +typedef enum { + X509_LU_NONE = 0, + X509_LU_X509, X509_LU_CRL +} X509_LOOKUP_TYPE; + +#if OPENSSL_API_COMPAT < 0x10100000L +#define X509_LU_RETRY -1 +#define X509_LU_FAIL 0 +#endif + +DEFINE_STACK_OF(X509_LOOKUP) +DEFINE_STACK_OF(X509_OBJECT) +DEFINE_STACK_OF(X509_VERIFY_PARAM) + +int X509_STORE_set_depth(X509_STORE *store, int depth); + +typedef int (*X509_STORE_CTX_verify_cb)(int, X509_STORE_CTX *); +typedef int (*X509_STORE_CTX_verify_fn)(X509_STORE_CTX *); +typedef int (*X509_STORE_CTX_get_issuer_fn)(X509 **issuer, + X509_STORE_CTX *ctx, X509 *x); +typedef int (*X509_STORE_CTX_check_issued_fn)(X509_STORE_CTX *ctx, + X509 *x, X509 *issuer); +typedef int (*X509_STORE_CTX_check_revocation_fn)(X509_STORE_CTX *ctx); +typedef int (*X509_STORE_CTX_get_crl_fn)(X509_STORE_CTX *ctx, + X509_CRL **crl, X509 *x); +typedef int (*X509_STORE_CTX_check_crl_fn)(X509_STORE_CTX *ctx, X509_CRL *crl); +typedef int (*X509_STORE_CTX_cert_crl_fn)(X509_STORE_CTX *ctx, + X509_CRL *crl, X509 *x); +typedef int (*X509_STORE_CTX_check_policy_fn)(X509_STORE_CTX *ctx); +typedef STACK_OF(X509) *(*X509_STORE_CTX_lookup_certs_fn)(X509_STORE_CTX *ctx, + X509_NAME *nm); +typedef STACK_OF(X509_CRL) *(*X509_STORE_CTX_lookup_crls_fn)(X509_STORE_CTX *ctx, + X509_NAME *nm); +typedef int (*X509_STORE_CTX_cleanup_fn)(X509_STORE_CTX *ctx); + + +void X509_STORE_CTX_set_depth(X509_STORE_CTX *ctx, int depth); + +# define X509_STORE_CTX_set_app_data(ctx,data) \ + X509_STORE_CTX_set_ex_data(ctx,0,data) +# define X509_STORE_CTX_get_app_data(ctx) \ + X509_STORE_CTX_get_ex_data(ctx,0) + +# define X509_L_FILE_LOAD 1 +# define X509_L_ADD_DIR 2 + +# define X509_LOOKUP_load_file(x,name,type) \ + X509_LOOKUP_ctrl((x),X509_L_FILE_LOAD,(name),(long)(type),NULL) + +# define X509_LOOKUP_add_dir(x,name,type) \ + X509_LOOKUP_ctrl((x),X509_L_ADD_DIR,(name),(long)(type),NULL) + +# define X509_V_OK 0 +# define X509_V_ERR_UNSPECIFIED 1 +# define X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT 2 +# define X509_V_ERR_UNABLE_TO_GET_CRL 3 +# define X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE 4 +# define X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE 5 +# define X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY 6 +# define X509_V_ERR_CERT_SIGNATURE_FAILURE 7 +# define X509_V_ERR_CRL_SIGNATURE_FAILURE 8 +# define X509_V_ERR_CERT_NOT_YET_VALID 9 +# define X509_V_ERR_CERT_HAS_EXPIRED 10 +# define X509_V_ERR_CRL_NOT_YET_VALID 11 +# define X509_V_ERR_CRL_HAS_EXPIRED 12 +# define X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD 13 +# define X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD 14 +# define X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD 15 +# define X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD 16 +# define X509_V_ERR_OUT_OF_MEM 17 +# define X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT 18 +# define X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN 19 +# define X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY 20 +# define X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE 21 +# define X509_V_ERR_CERT_CHAIN_TOO_LONG 22 +# define X509_V_ERR_CERT_REVOKED 23 +# define X509_V_ERR_INVALID_CA 24 +# define X509_V_ERR_PATH_LENGTH_EXCEEDED 25 +# define X509_V_ERR_INVALID_PURPOSE 26 +# define X509_V_ERR_CERT_UNTRUSTED 27 +# define X509_V_ERR_CERT_REJECTED 28 +/* These are 'informational' when looking for issuer cert */ +# define X509_V_ERR_SUBJECT_ISSUER_MISMATCH 29 +# define X509_V_ERR_AKID_SKID_MISMATCH 30 +# define X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH 31 +# define X509_V_ERR_KEYUSAGE_NO_CERTSIGN 32 +# define X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER 33 +# define X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION 34 +# define X509_V_ERR_KEYUSAGE_NO_CRL_SIGN 35 +# define X509_V_ERR_UNHANDLED_CRITICAL_CRL_EXTENSION 36 +# define X509_V_ERR_INVALID_NON_CA 37 +# define X509_V_ERR_PROXY_PATH_LENGTH_EXCEEDED 38 +# define X509_V_ERR_KEYUSAGE_NO_DIGITAL_SIGNATURE 39 +# define X509_V_ERR_PROXY_CERTIFICATES_NOT_ALLOWED 40 +# define X509_V_ERR_INVALID_EXTENSION 41 +# define X509_V_ERR_INVALID_POLICY_EXTENSION 42 +# define X509_V_ERR_NO_EXPLICIT_POLICY 43 +# define X509_V_ERR_DIFFERENT_CRL_SCOPE 44 +# define X509_V_ERR_UNSUPPORTED_EXTENSION_FEATURE 45 +# define X509_V_ERR_UNNESTED_RESOURCE 46 +# define X509_V_ERR_PERMITTED_VIOLATION 47 +# define X509_V_ERR_EXCLUDED_VIOLATION 48 +# define X509_V_ERR_SUBTREE_MINMAX 49 +/* The application is not happy */ +# define X509_V_ERR_APPLICATION_VERIFICATION 50 +# define X509_V_ERR_UNSUPPORTED_CONSTRAINT_TYPE 51 +# define X509_V_ERR_UNSUPPORTED_CONSTRAINT_SYNTAX 52 +# define X509_V_ERR_UNSUPPORTED_NAME_SYNTAX 53 +# define X509_V_ERR_CRL_PATH_VALIDATION_ERROR 54 +/* Another issuer check debug option */ +# define X509_V_ERR_PATH_LOOP 55 +/* Suite B mode algorithm violation */ +# define X509_V_ERR_SUITE_B_INVALID_VERSION 56 +# define X509_V_ERR_SUITE_B_INVALID_ALGORITHM 57 +# define X509_V_ERR_SUITE_B_INVALID_CURVE 58 +# define X509_V_ERR_SUITE_B_INVALID_SIGNATURE_ALGORITHM 59 +# define X509_V_ERR_SUITE_B_LOS_NOT_ALLOWED 60 +# define X509_V_ERR_SUITE_B_CANNOT_SIGN_P_384_WITH_P_256 61 +/* Host, email and IP check errors */ +# define X509_V_ERR_HOSTNAME_MISMATCH 62 +# define X509_V_ERR_EMAIL_MISMATCH 63 +# define X509_V_ERR_IP_ADDRESS_MISMATCH 64 +/* DANE TLSA errors */ +# define X509_V_ERR_DANE_NO_MATCH 65 +/* security level errors */ +# define X509_V_ERR_EE_KEY_TOO_SMALL 66 +# define X509_V_ERR_CA_KEY_TOO_SMALL 67 +# define X509_V_ERR_CA_MD_TOO_WEAK 68 +/* Caller error */ +# define X509_V_ERR_INVALID_CALL 69 +/* Issuer lookup error */ +# define X509_V_ERR_STORE_LOOKUP 70 +/* Certificate transparency */ +# define X509_V_ERR_NO_VALID_SCTS 71 + +# define X509_V_ERR_PROXY_SUBJECT_NAME_VIOLATION 72 +/* OCSP status errors */ +# define X509_V_ERR_OCSP_VERIFY_NEEDED 73 /* Need OCSP verification */ +# define X509_V_ERR_OCSP_VERIFY_FAILED 74 /* Couldn't verify cert through OCSP */ +# define X509_V_ERR_OCSP_CERT_UNKNOWN 75 /* Certificate wasn't recognized by the OCSP responder */ + +/* Certificate verify flags */ + +# if OPENSSL_API_COMPAT < 0x10100000L +# define X509_V_FLAG_CB_ISSUER_CHECK 0x0 /* Deprecated */ +# endif +/* Use check time instead of current time */ +# define X509_V_FLAG_USE_CHECK_TIME 0x2 +/* Lookup CRLs */ +# define X509_V_FLAG_CRL_CHECK 0x4 +/* Lookup CRLs for whole chain */ +# define X509_V_FLAG_CRL_CHECK_ALL 0x8 +/* Ignore unhandled critical extensions */ +# define X509_V_FLAG_IGNORE_CRITICAL 0x10 +/* Disable workarounds for broken certificates */ +# define X509_V_FLAG_X509_STRICT 0x20 +/* Enable proxy certificate validation */ +# define X509_V_FLAG_ALLOW_PROXY_CERTS 0x40 +/* Enable policy checking */ +# define X509_V_FLAG_POLICY_CHECK 0x80 +/* Policy variable require-explicit-policy */ +# define X509_V_FLAG_EXPLICIT_POLICY 0x100 +/* Policy variable inhibit-any-policy */ +# define X509_V_FLAG_INHIBIT_ANY 0x200 +/* Policy variable inhibit-policy-mapping */ +# define X509_V_FLAG_INHIBIT_MAP 0x400 +/* Notify callback that policy is OK */ +# define X509_V_FLAG_NOTIFY_POLICY 0x800 +/* Extended CRL features such as indirect CRLs, alternate CRL signing keys */ +# define X509_V_FLAG_EXTENDED_CRL_SUPPORT 0x1000 +/* Delta CRL support */ +# define X509_V_FLAG_USE_DELTAS 0x2000 +/* Check self-signed CA signature */ +# define X509_V_FLAG_CHECK_SS_SIGNATURE 0x4000 +/* Use trusted store first */ +# define X509_V_FLAG_TRUSTED_FIRST 0x8000 +/* Suite B 128 bit only mode: not normally used */ +# define X509_V_FLAG_SUITEB_128_LOS_ONLY 0x10000 +/* Suite B 192 bit only mode */ +# define X509_V_FLAG_SUITEB_192_LOS 0x20000 +/* Suite B 128 bit mode allowing 192 bit algorithms */ +# define X509_V_FLAG_SUITEB_128_LOS 0x30000 +/* Allow partial chains if at least one certificate is in trusted store */ +# define X509_V_FLAG_PARTIAL_CHAIN 0x80000 +/* + * If the initial chain is not trusted, do not attempt to build an alternative + * chain. Alternate chain checking was introduced in 1.1.0. Setting this flag + * will force the behaviour to match that of previous versions. + */ +# define X509_V_FLAG_NO_ALT_CHAINS 0x100000 +/* Do not check certificate/CRL validity against current time */ +# define X509_V_FLAG_NO_CHECK_TIME 0x200000 + +# define X509_VP_FLAG_DEFAULT 0x1 +# define X509_VP_FLAG_OVERWRITE 0x2 +# define X509_VP_FLAG_RESET_FLAGS 0x4 +# define X509_VP_FLAG_LOCKED 0x8 +# define X509_VP_FLAG_ONCE 0x10 + +/* Internal use: mask of policy related options */ +# define X509_V_FLAG_POLICY_MASK (X509_V_FLAG_POLICY_CHECK \ + | X509_V_FLAG_EXPLICIT_POLICY \ + | X509_V_FLAG_INHIBIT_ANY \ + | X509_V_FLAG_INHIBIT_MAP) + +int X509_OBJECT_idx_by_subject(STACK_OF(X509_OBJECT) *h, X509_LOOKUP_TYPE type, + X509_NAME *name); +X509_OBJECT *X509_OBJECT_retrieve_by_subject(STACK_OF(X509_OBJECT) *h, + X509_LOOKUP_TYPE type, + X509_NAME *name); +X509_OBJECT *X509_OBJECT_retrieve_match(STACK_OF(X509_OBJECT) *h, + X509_OBJECT *x); +int X509_OBJECT_up_ref_count(X509_OBJECT *a); +X509_OBJECT *X509_OBJECT_new(void); +void X509_OBJECT_free(X509_OBJECT *a); +X509_LOOKUP_TYPE X509_OBJECT_get_type(const X509_OBJECT *a); +X509 *X509_OBJECT_get0_X509(const X509_OBJECT *a); +int X509_OBJECT_set1_X509(X509_OBJECT *a, X509 *obj); +X509_CRL *X509_OBJECT_get0_X509_CRL(X509_OBJECT *a); +int X509_OBJECT_set1_X509_CRL(X509_OBJECT *a, X509_CRL *obj); +X509_STORE *X509_STORE_new(void); +void X509_STORE_free(X509_STORE *v); +int X509_STORE_lock(X509_STORE *ctx); +int X509_STORE_unlock(X509_STORE *ctx); +int X509_STORE_up_ref(X509_STORE *v); +STACK_OF(X509_OBJECT) *X509_STORE_get0_objects(X509_STORE *v); + +STACK_OF(X509) *X509_STORE_CTX_get1_certs(X509_STORE_CTX *st, X509_NAME *nm); +STACK_OF(X509_CRL) *X509_STORE_CTX_get1_crls(X509_STORE_CTX *st, X509_NAME *nm); +int X509_STORE_set_flags(X509_STORE *ctx, unsigned long flags); +int X509_STORE_set_purpose(X509_STORE *ctx, int purpose); +int X509_STORE_set_trust(X509_STORE *ctx, int trust); +int X509_STORE_set1_param(X509_STORE *ctx, X509_VERIFY_PARAM *pm); +X509_VERIFY_PARAM *X509_STORE_get0_param(X509_STORE *ctx); + +void X509_STORE_set_verify(X509_STORE *ctx, X509_STORE_CTX_verify_fn verify); +#define X509_STORE_set_verify_func(ctx, func) \ + X509_STORE_set_verify((ctx),(func)) +void X509_STORE_CTX_set_verify(X509_STORE_CTX *ctx, + X509_STORE_CTX_verify_fn verify); +X509_STORE_CTX_verify_fn X509_STORE_get_verify(X509_STORE *ctx); +void X509_STORE_set_verify_cb(X509_STORE *ctx, + X509_STORE_CTX_verify_cb verify_cb); +# define X509_STORE_set_verify_cb_func(ctx,func) \ + X509_STORE_set_verify_cb((ctx),(func)) +X509_STORE_CTX_verify_cb X509_STORE_get_verify_cb(X509_STORE *ctx); +void X509_STORE_set_get_issuer(X509_STORE *ctx, + X509_STORE_CTX_get_issuer_fn get_issuer); +X509_STORE_CTX_get_issuer_fn X509_STORE_get_get_issuer(X509_STORE *ctx); +void X509_STORE_set_check_issued(X509_STORE *ctx, + X509_STORE_CTX_check_issued_fn check_issued); +X509_STORE_CTX_check_issued_fn X509_STORE_get_check_issued(X509_STORE *ctx); +void X509_STORE_set_check_revocation(X509_STORE *ctx, + X509_STORE_CTX_check_revocation_fn check_revocation); +X509_STORE_CTX_check_revocation_fn X509_STORE_get_check_revocation(X509_STORE *ctx); +void X509_STORE_set_get_crl(X509_STORE *ctx, + X509_STORE_CTX_get_crl_fn get_crl); +X509_STORE_CTX_get_crl_fn X509_STORE_get_get_crl(X509_STORE *ctx); +void X509_STORE_set_check_crl(X509_STORE *ctx, + X509_STORE_CTX_check_crl_fn check_crl); +X509_STORE_CTX_check_crl_fn X509_STORE_get_check_crl(X509_STORE *ctx); +void X509_STORE_set_cert_crl(X509_STORE *ctx, + X509_STORE_CTX_cert_crl_fn cert_crl); +X509_STORE_CTX_cert_crl_fn X509_STORE_get_cert_crl(X509_STORE *ctx); +void X509_STORE_set_check_policy(X509_STORE *ctx, + X509_STORE_CTX_check_policy_fn check_policy); +X509_STORE_CTX_check_policy_fn X509_STORE_get_check_policy(X509_STORE *ctx); +void X509_STORE_set_lookup_certs(X509_STORE *ctx, + X509_STORE_CTX_lookup_certs_fn lookup_certs); +X509_STORE_CTX_lookup_certs_fn X509_STORE_get_lookup_certs(X509_STORE *ctx); +void X509_STORE_set_lookup_crls(X509_STORE *ctx, + X509_STORE_CTX_lookup_crls_fn lookup_crls); +#define X509_STORE_set_lookup_crls_cb(ctx, func) \ + X509_STORE_set_lookup_crls((ctx), (func)) +X509_STORE_CTX_lookup_crls_fn X509_STORE_get_lookup_crls(X509_STORE *ctx); +void X509_STORE_set_cleanup(X509_STORE *ctx, + X509_STORE_CTX_cleanup_fn cleanup); +X509_STORE_CTX_cleanup_fn X509_STORE_get_cleanup(X509_STORE *ctx); + +#define X509_STORE_get_ex_new_index(l, p, newf, dupf, freef) \ + CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_X509_STORE, l, p, newf, dupf, freef) +int X509_STORE_set_ex_data(X509_STORE *ctx, int idx, void *data); +void *X509_STORE_get_ex_data(X509_STORE *ctx, int idx); + +X509_STORE_CTX *X509_STORE_CTX_new(void); + +int X509_STORE_CTX_get1_issuer(X509 **issuer, X509_STORE_CTX *ctx, X509 *x); + +void X509_STORE_CTX_free(X509_STORE_CTX *ctx); +int X509_STORE_CTX_init(X509_STORE_CTX *ctx, X509_STORE *store, + X509 *x509, STACK_OF(X509) *chain); +void X509_STORE_CTX_set0_trusted_stack(X509_STORE_CTX *ctx, STACK_OF(X509) *sk); +void X509_STORE_CTX_cleanup(X509_STORE_CTX *ctx); + +X509_STORE *X509_STORE_CTX_get0_store(X509_STORE_CTX *ctx); +X509 *X509_STORE_CTX_get0_cert(X509_STORE_CTX *ctx); +STACK_OF(X509)* X509_STORE_CTX_get0_untrusted(X509_STORE_CTX *ctx); +void X509_STORE_CTX_set0_untrusted(X509_STORE_CTX *ctx, STACK_OF(X509) *sk); +void X509_STORE_CTX_set_verify_cb(X509_STORE_CTX *ctx, + X509_STORE_CTX_verify_cb verify); +X509_STORE_CTX_verify_cb X509_STORE_CTX_get_verify_cb(X509_STORE_CTX *ctx); +X509_STORE_CTX_verify_fn X509_STORE_CTX_get_verify(X509_STORE_CTX *ctx); +X509_STORE_CTX_get_issuer_fn X509_STORE_CTX_get_get_issuer(X509_STORE_CTX *ctx); +X509_STORE_CTX_check_issued_fn X509_STORE_CTX_get_check_issued(X509_STORE_CTX *ctx); +X509_STORE_CTX_check_revocation_fn X509_STORE_CTX_get_check_revocation(X509_STORE_CTX *ctx); +X509_STORE_CTX_get_crl_fn X509_STORE_CTX_get_get_crl(X509_STORE_CTX *ctx); +X509_STORE_CTX_check_crl_fn X509_STORE_CTX_get_check_crl(X509_STORE_CTX *ctx); +X509_STORE_CTX_cert_crl_fn X509_STORE_CTX_get_cert_crl(X509_STORE_CTX *ctx); +X509_STORE_CTX_check_policy_fn X509_STORE_CTX_get_check_policy(X509_STORE_CTX *ctx); +X509_STORE_CTX_lookup_certs_fn X509_STORE_CTX_get_lookup_certs(X509_STORE_CTX *ctx); +X509_STORE_CTX_lookup_crls_fn X509_STORE_CTX_get_lookup_crls(X509_STORE_CTX *ctx); +X509_STORE_CTX_cleanup_fn X509_STORE_CTX_get_cleanup(X509_STORE_CTX *ctx); + +#if OPENSSL_API_COMPAT < 0x10100000L +# define X509_STORE_CTX_get_chain X509_STORE_CTX_get0_chain +# define X509_STORE_CTX_set_chain X509_STORE_CTX_set0_untrusted +# define X509_STORE_CTX_trusted_stack X509_STORE_CTX_set0_trusted_stack +# define X509_STORE_get_by_subject X509_STORE_CTX_get_by_subject +# define X509_STORE_get1_cert X509_STORE_CTX_get1_certs +# define X509_STORE_get1_crl X509_STORE_CTX_get1_crls +#endif + +X509_LOOKUP *X509_STORE_add_lookup(X509_STORE *v, X509_LOOKUP_METHOD *m); +X509_LOOKUP_METHOD *X509_LOOKUP_hash_dir(void); +X509_LOOKUP_METHOD *X509_LOOKUP_file(void); + +typedef int (*X509_LOOKUP_ctrl_fn)(X509_LOOKUP *ctx, int cmd, const char *argc, + long argl, char **ret); +typedef int (*X509_LOOKUP_get_by_subject_fn)(X509_LOOKUP *ctx, + X509_LOOKUP_TYPE type, + X509_NAME *name, + X509_OBJECT *ret); +typedef int (*X509_LOOKUP_get_by_issuer_serial_fn)(X509_LOOKUP *ctx, + X509_LOOKUP_TYPE type, + X509_NAME *name, + ASN1_INTEGER *serial, + X509_OBJECT *ret); +typedef int (*X509_LOOKUP_get_by_fingerprint_fn)(X509_LOOKUP *ctx, + X509_LOOKUP_TYPE type, + const unsigned char* bytes, + int len, + X509_OBJECT *ret); +typedef int (*X509_LOOKUP_get_by_alias_fn)(X509_LOOKUP *ctx, + X509_LOOKUP_TYPE type, + const char *str, + int len, + X509_OBJECT *ret); + +X509_LOOKUP_METHOD *X509_LOOKUP_meth_new(const char *name); +void X509_LOOKUP_meth_free(X509_LOOKUP_METHOD *method); + +int X509_LOOKUP_meth_set_new_item(X509_LOOKUP_METHOD *method, + int (*new_item) (X509_LOOKUP *ctx)); +int (*X509_LOOKUP_meth_get_new_item(const X509_LOOKUP_METHOD* method)) + (X509_LOOKUP *ctx); + +int X509_LOOKUP_meth_set_free(X509_LOOKUP_METHOD *method, + void (*free_fn) (X509_LOOKUP *ctx)); +void (*X509_LOOKUP_meth_get_free(const X509_LOOKUP_METHOD* method)) + (X509_LOOKUP *ctx); + +int X509_LOOKUP_meth_set_init(X509_LOOKUP_METHOD *method, + int (*init) (X509_LOOKUP *ctx)); +int (*X509_LOOKUP_meth_get_init(const X509_LOOKUP_METHOD* method)) + (X509_LOOKUP *ctx); + +int X509_LOOKUP_meth_set_shutdown(X509_LOOKUP_METHOD *method, + int (*shutdown) (X509_LOOKUP *ctx)); +int (*X509_LOOKUP_meth_get_shutdown(const X509_LOOKUP_METHOD* method)) + (X509_LOOKUP *ctx); + +int X509_LOOKUP_meth_set_ctrl(X509_LOOKUP_METHOD *method, + X509_LOOKUP_ctrl_fn ctrl_fn); +X509_LOOKUP_ctrl_fn X509_LOOKUP_meth_get_ctrl(const X509_LOOKUP_METHOD *method); + +int X509_LOOKUP_meth_set_get_by_subject(X509_LOOKUP_METHOD *method, + X509_LOOKUP_get_by_subject_fn fn); +X509_LOOKUP_get_by_subject_fn X509_LOOKUP_meth_get_get_by_subject( + const X509_LOOKUP_METHOD *method); + +int X509_LOOKUP_meth_set_get_by_issuer_serial(X509_LOOKUP_METHOD *method, + X509_LOOKUP_get_by_issuer_serial_fn fn); +X509_LOOKUP_get_by_issuer_serial_fn X509_LOOKUP_meth_get_get_by_issuer_serial( + const X509_LOOKUP_METHOD *method); + +int X509_LOOKUP_meth_set_get_by_fingerprint(X509_LOOKUP_METHOD *method, + X509_LOOKUP_get_by_fingerprint_fn fn); +X509_LOOKUP_get_by_fingerprint_fn X509_LOOKUP_meth_get_get_by_fingerprint( + const X509_LOOKUP_METHOD *method); + +int X509_LOOKUP_meth_set_get_by_alias(X509_LOOKUP_METHOD *method, + X509_LOOKUP_get_by_alias_fn fn); +X509_LOOKUP_get_by_alias_fn X509_LOOKUP_meth_get_get_by_alias( + const X509_LOOKUP_METHOD *method); + + +int X509_STORE_add_cert(X509_STORE *ctx, X509 *x); +int X509_STORE_add_crl(X509_STORE *ctx, X509_CRL *x); + +int X509_STORE_CTX_get_by_subject(X509_STORE_CTX *vs, X509_LOOKUP_TYPE type, + X509_NAME *name, X509_OBJECT *ret); +X509_OBJECT *X509_STORE_CTX_get_obj_by_subject(X509_STORE_CTX *vs, + X509_LOOKUP_TYPE type, + X509_NAME *name); + +int X509_LOOKUP_ctrl(X509_LOOKUP *ctx, int cmd, const char *argc, + long argl, char **ret); + +int X509_load_cert_file(X509_LOOKUP *ctx, const char *file, int type); +int X509_load_crl_file(X509_LOOKUP *ctx, const char *file, int type); +int X509_load_cert_crl_file(X509_LOOKUP *ctx, const char *file, int type); + +X509_LOOKUP *X509_LOOKUP_new(X509_LOOKUP_METHOD *method); +void X509_LOOKUP_free(X509_LOOKUP *ctx); +int X509_LOOKUP_init(X509_LOOKUP *ctx); +int X509_LOOKUP_by_subject(X509_LOOKUP *ctx, X509_LOOKUP_TYPE type, + X509_NAME *name, X509_OBJECT *ret); +int X509_LOOKUP_by_issuer_serial(X509_LOOKUP *ctx, X509_LOOKUP_TYPE type, + X509_NAME *name, ASN1_INTEGER *serial, + X509_OBJECT *ret); +int X509_LOOKUP_by_fingerprint(X509_LOOKUP *ctx, X509_LOOKUP_TYPE type, + const unsigned char *bytes, int len, + X509_OBJECT *ret); +int X509_LOOKUP_by_alias(X509_LOOKUP *ctx, X509_LOOKUP_TYPE type, + const char *str, int len, X509_OBJECT *ret); +int X509_LOOKUP_set_method_data(X509_LOOKUP *ctx, void *data); +void *X509_LOOKUP_get_method_data(const X509_LOOKUP *ctx); +X509_STORE *X509_LOOKUP_get_store(const X509_LOOKUP *ctx); +int X509_LOOKUP_shutdown(X509_LOOKUP *ctx); + +int X509_STORE_load_locations(X509_STORE *ctx, + const char *file, const char *dir); +int X509_STORE_set_default_paths(X509_STORE *ctx); + +#define X509_STORE_CTX_get_ex_new_index(l, p, newf, dupf, freef) \ + CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_X509_STORE_CTX, l, p, newf, dupf, freef) +int X509_STORE_CTX_set_ex_data(X509_STORE_CTX *ctx, int idx, void *data); +void *X509_STORE_CTX_get_ex_data(X509_STORE_CTX *ctx, int idx); +int X509_STORE_CTX_get_error(X509_STORE_CTX *ctx); +void X509_STORE_CTX_set_error(X509_STORE_CTX *ctx, int s); +int X509_STORE_CTX_get_error_depth(X509_STORE_CTX *ctx); +void X509_STORE_CTX_set_error_depth(X509_STORE_CTX *ctx, int depth); +X509 *X509_STORE_CTX_get_current_cert(X509_STORE_CTX *ctx); +void X509_STORE_CTX_set_current_cert(X509_STORE_CTX *ctx, X509 *x); +X509 *X509_STORE_CTX_get0_current_issuer(X509_STORE_CTX *ctx); +X509_CRL *X509_STORE_CTX_get0_current_crl(X509_STORE_CTX *ctx); +X509_STORE_CTX *X509_STORE_CTX_get0_parent_ctx(X509_STORE_CTX *ctx); +STACK_OF(X509) *X509_STORE_CTX_get0_chain(X509_STORE_CTX *ctx); +STACK_OF(X509) *X509_STORE_CTX_get1_chain(X509_STORE_CTX *ctx); +void X509_STORE_CTX_set_cert(X509_STORE_CTX *c, X509 *x); +void X509_STORE_CTX_set0_verified_chain(X509_STORE_CTX *c, STACK_OF(X509) *sk); +void X509_STORE_CTX_set0_crls(X509_STORE_CTX *c, STACK_OF(X509_CRL) *sk); +int X509_STORE_CTX_set_purpose(X509_STORE_CTX *ctx, int purpose); +int X509_STORE_CTX_set_trust(X509_STORE_CTX *ctx, int trust); +int X509_STORE_CTX_purpose_inherit(X509_STORE_CTX *ctx, int def_purpose, + int purpose, int trust); +void X509_STORE_CTX_set_flags(X509_STORE_CTX *ctx, unsigned long flags); +void X509_STORE_CTX_set_time(X509_STORE_CTX *ctx, unsigned long flags, + time_t t); + +X509_POLICY_TREE *X509_STORE_CTX_get0_policy_tree(X509_STORE_CTX *ctx); +int X509_STORE_CTX_get_explicit_policy(X509_STORE_CTX *ctx); +int X509_STORE_CTX_get_num_untrusted(X509_STORE_CTX *ctx); + +X509_VERIFY_PARAM *X509_STORE_CTX_get0_param(X509_STORE_CTX *ctx); +void X509_STORE_CTX_set0_param(X509_STORE_CTX *ctx, X509_VERIFY_PARAM *param); +int X509_STORE_CTX_set_default(X509_STORE_CTX *ctx, const char *name); + +/* + * Bridge opacity barrier between libcrypt and libssl, also needed to support + * offline testing in test/danetest.c + */ +void X509_STORE_CTX_set0_dane(X509_STORE_CTX *ctx, SSL_DANE *dane); +#define DANE_FLAG_NO_DANE_EE_NAMECHECKS (1L << 0) + +/* X509_VERIFY_PARAM functions */ + +X509_VERIFY_PARAM *X509_VERIFY_PARAM_new(void); +void X509_VERIFY_PARAM_free(X509_VERIFY_PARAM *param); +int X509_VERIFY_PARAM_inherit(X509_VERIFY_PARAM *to, + const X509_VERIFY_PARAM *from); +int X509_VERIFY_PARAM_set1(X509_VERIFY_PARAM *to, + const X509_VERIFY_PARAM *from); +int X509_VERIFY_PARAM_set1_name(X509_VERIFY_PARAM *param, const char *name); +int X509_VERIFY_PARAM_set_flags(X509_VERIFY_PARAM *param, + unsigned long flags); +int X509_VERIFY_PARAM_clear_flags(X509_VERIFY_PARAM *param, + unsigned long flags); +unsigned long X509_VERIFY_PARAM_get_flags(X509_VERIFY_PARAM *param); +int X509_VERIFY_PARAM_set_purpose(X509_VERIFY_PARAM *param, int purpose); +int X509_VERIFY_PARAM_set_trust(X509_VERIFY_PARAM *param, int trust); +void X509_VERIFY_PARAM_set_depth(X509_VERIFY_PARAM *param, int depth); +void X509_VERIFY_PARAM_set_auth_level(X509_VERIFY_PARAM *param, int auth_level); +time_t X509_VERIFY_PARAM_get_time(const X509_VERIFY_PARAM *param); +void X509_VERIFY_PARAM_set_time(X509_VERIFY_PARAM *param, time_t t); +int X509_VERIFY_PARAM_add0_policy(X509_VERIFY_PARAM *param, + ASN1_OBJECT *policy); +int X509_VERIFY_PARAM_set1_policies(X509_VERIFY_PARAM *param, + STACK_OF(ASN1_OBJECT) *policies); + +int X509_VERIFY_PARAM_set_inh_flags(X509_VERIFY_PARAM *param, + uint32_t flags); +uint32_t X509_VERIFY_PARAM_get_inh_flags(const X509_VERIFY_PARAM *param); + +int X509_VERIFY_PARAM_set1_host(X509_VERIFY_PARAM *param, + const char *name, size_t namelen); +int X509_VERIFY_PARAM_add1_host(X509_VERIFY_PARAM *param, + const char *name, size_t namelen); +void X509_VERIFY_PARAM_set_hostflags(X509_VERIFY_PARAM *param, + unsigned int flags); +unsigned int X509_VERIFY_PARAM_get_hostflags(const X509_VERIFY_PARAM *param); +char *X509_VERIFY_PARAM_get0_peername(X509_VERIFY_PARAM *); +void X509_VERIFY_PARAM_move_peername(X509_VERIFY_PARAM *, X509_VERIFY_PARAM *); +int X509_VERIFY_PARAM_set1_email(X509_VERIFY_PARAM *param, + const char *email, size_t emaillen); +int X509_VERIFY_PARAM_set1_ip(X509_VERIFY_PARAM *param, + const unsigned char *ip, size_t iplen); +int X509_VERIFY_PARAM_set1_ip_asc(X509_VERIFY_PARAM *param, + const char *ipasc); + +int X509_VERIFY_PARAM_get_depth(const X509_VERIFY_PARAM *param); +int X509_VERIFY_PARAM_get_auth_level(const X509_VERIFY_PARAM *param); +const char *X509_VERIFY_PARAM_get0_name(const X509_VERIFY_PARAM *param); + +int X509_VERIFY_PARAM_add0_table(X509_VERIFY_PARAM *param); +int X509_VERIFY_PARAM_get_count(void); +const X509_VERIFY_PARAM *X509_VERIFY_PARAM_get0(int id); +const X509_VERIFY_PARAM *X509_VERIFY_PARAM_lookup(const char *name); +void X509_VERIFY_PARAM_table_cleanup(void); + +/* Non positive return values are errors */ +#define X509_PCY_TREE_FAILURE -2 /* Failure to satisfy explicit policy */ +#define X509_PCY_TREE_INVALID -1 /* Inconsistent or invalid extensions */ +#define X509_PCY_TREE_INTERNAL 0 /* Internal error, most likely malloc */ + +/* + * Positive return values form a bit mask, all but the first are internal to + * the library and don't appear in results from X509_policy_check(). + */ +#define X509_PCY_TREE_VALID 1 /* The policy tree is valid */ +#define X509_PCY_TREE_EMPTY 2 /* The policy tree is empty */ +#define X509_PCY_TREE_EXPLICIT 4 /* Explicit policy required */ + +int X509_policy_check(X509_POLICY_TREE **ptree, int *pexplicit_policy, + STACK_OF(X509) *certs, + STACK_OF(ASN1_OBJECT) *policy_oids, unsigned int flags); + +void X509_policy_tree_free(X509_POLICY_TREE *tree); + +int X509_policy_tree_level_count(const X509_POLICY_TREE *tree); +X509_POLICY_LEVEL *X509_policy_tree_get0_level(const X509_POLICY_TREE *tree, + int i); + +STACK_OF(X509_POLICY_NODE) *X509_policy_tree_get0_policies(const + X509_POLICY_TREE + *tree); + +STACK_OF(X509_POLICY_NODE) *X509_policy_tree_get0_user_policies(const + X509_POLICY_TREE + *tree); + +int X509_policy_level_node_count(X509_POLICY_LEVEL *level); + +X509_POLICY_NODE *X509_policy_level_get0_node(X509_POLICY_LEVEL *level, + int i); + +const ASN1_OBJECT *X509_policy_node_get0_policy(const X509_POLICY_NODE *node); + +STACK_OF(POLICYQUALINFO) *X509_policy_node_get0_qualifiers(const + X509_POLICY_NODE + *node); +const X509_POLICY_NODE *X509_policy_node_get0_parent(const X509_POLICY_NODE + *node); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/x509err.h b/example/android/third_party/crypto/arm/include/openssl/x509err.h new file mode 100644 index 00000000..b1d6a870 --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/x509err.h @@ -0,0 +1,125 @@ +/* + * Generated by util/mkerr.pl DO NOT EDIT + * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_X509ERR_H +# define HEADER_X509ERR_H + +# ifdef __cplusplus +extern "C" +# endif +int ERR_load_X509_strings(void); + +/* + * X509 function codes. + */ +# define X509_F_ADD_CERT_DIR 100 +# define X509_F_BUILD_CHAIN 106 +# define X509_F_BY_FILE_CTRL 101 +# define X509_F_CHECK_NAME_CONSTRAINTS 149 +# define X509_F_CHECK_POLICY 145 +# define X509_F_DANE_I2D 107 +# define X509_F_DIR_CTRL 102 +# define X509_F_GET_CERT_BY_SUBJECT 103 +# define X509_F_I2D_X509_AUX 151 +# define X509_F_LOOKUP_CERTS_SK 152 +# define X509_F_NETSCAPE_SPKI_B64_DECODE 129 +# define X509_F_NETSCAPE_SPKI_B64_ENCODE 130 +# define X509_F_NEW_DIR 153 +# define X509_F_X509AT_ADD1_ATTR 135 +# define X509_F_X509V3_ADD_EXT 104 +# define X509_F_X509_ATTRIBUTE_CREATE_BY_NID 136 +# define X509_F_X509_ATTRIBUTE_CREATE_BY_OBJ 137 +# define X509_F_X509_ATTRIBUTE_CREATE_BY_TXT 140 +# define X509_F_X509_ATTRIBUTE_GET0_DATA 139 +# define X509_F_X509_ATTRIBUTE_SET1_DATA 138 +# define X509_F_X509_CHECK_PRIVATE_KEY 128 +# define X509_F_X509_CRL_DIFF 105 +# define X509_F_X509_CRL_METHOD_NEW 154 +# define X509_F_X509_CRL_PRINT_FP 147 +# define X509_F_X509_EXTENSION_CREATE_BY_NID 108 +# define X509_F_X509_EXTENSION_CREATE_BY_OBJ 109 +# define X509_F_X509_GET_PUBKEY_PARAMETERS 110 +# define X509_F_X509_LOAD_CERT_CRL_FILE 132 +# define X509_F_X509_LOAD_CERT_FILE 111 +# define X509_F_X509_LOAD_CRL_FILE 112 +# define X509_F_X509_LOOKUP_METH_NEW 160 +# define X509_F_X509_LOOKUP_NEW 155 +# define X509_F_X509_NAME_ADD_ENTRY 113 +# define X509_F_X509_NAME_CANON 156 +# define X509_F_X509_NAME_ENTRY_CREATE_BY_NID 114 +# define X509_F_X509_NAME_ENTRY_CREATE_BY_TXT 131 +# define X509_F_X509_NAME_ENTRY_SET_OBJECT 115 +# define X509_F_X509_NAME_ONELINE 116 +# define X509_F_X509_NAME_PRINT 117 +# define X509_F_X509_OBJECT_NEW 150 +# define X509_F_X509_PRINT_EX_FP 118 +# define X509_F_X509_PUBKEY_DECODE 148 +# define X509_F_X509_PUBKEY_GET0 119 +# define X509_F_X509_PUBKEY_SET 120 +# define X509_F_X509_REQ_CHECK_PRIVATE_KEY 144 +# define X509_F_X509_REQ_PRINT_EX 121 +# define X509_F_X509_REQ_PRINT_FP 122 +# define X509_F_X509_REQ_TO_X509 123 +# define X509_F_X509_STORE_ADD_CERT 124 +# define X509_F_X509_STORE_ADD_CRL 125 +# define X509_F_X509_STORE_ADD_LOOKUP 157 +# define X509_F_X509_STORE_CTX_GET1_ISSUER 146 +# define X509_F_X509_STORE_CTX_INIT 143 +# define X509_F_X509_STORE_CTX_NEW 142 +# define X509_F_X509_STORE_CTX_PURPOSE_INHERIT 134 +# define X509_F_X509_STORE_NEW 158 +# define X509_F_X509_TO_X509_REQ 126 +# define X509_F_X509_TRUST_ADD 133 +# define X509_F_X509_TRUST_SET 141 +# define X509_F_X509_VERIFY_CERT 127 +# define X509_F_X509_VERIFY_PARAM_NEW 159 + +/* + * X509 reason codes. + */ +# define X509_R_AKID_MISMATCH 110 +# define X509_R_BAD_SELECTOR 133 +# define X509_R_BAD_X509_FILETYPE 100 +# define X509_R_BASE64_DECODE_ERROR 118 +# define X509_R_CANT_CHECK_DH_KEY 114 +# define X509_R_CERT_ALREADY_IN_HASH_TABLE 101 +# define X509_R_CRL_ALREADY_DELTA 127 +# define X509_R_CRL_VERIFY_FAILURE 131 +# define X509_R_IDP_MISMATCH 128 +# define X509_R_INVALID_DIRECTORY 113 +# define X509_R_INVALID_FIELD_NAME 119 +# define X509_R_INVALID_TRUST 123 +# define X509_R_ISSUER_MISMATCH 129 +# define X509_R_KEY_TYPE_MISMATCH 115 +# define X509_R_KEY_VALUES_MISMATCH 116 +# define X509_R_LOADING_CERT_DIR 103 +# define X509_R_LOADING_DEFAULTS 104 +# define X509_R_METHOD_NOT_SUPPORTED 124 +# define X509_R_NAME_TOO_LONG 134 +# define X509_R_NEWER_CRL_NOT_NEWER 132 +# define X509_R_NO_CERTIFICATE_FOUND 135 +# define X509_R_NO_CERTIFICATE_OR_CRL_FOUND 136 +# define X509_R_NO_CERT_SET_FOR_US_TO_VERIFY 105 +# define X509_R_NO_CRL_FOUND 137 +# define X509_R_NO_CRL_NUMBER 130 +# define X509_R_PUBLIC_KEY_DECODE_ERROR 125 +# define X509_R_PUBLIC_KEY_ENCODE_ERROR 126 +# define X509_R_SHOULD_RETRY 106 +# define X509_R_UNABLE_TO_FIND_PARAMETERS_IN_CHAIN 107 +# define X509_R_UNABLE_TO_GET_CERTS_PUBLIC_KEY 108 +# define X509_R_UNKNOWN_KEY_TYPE 117 +# define X509_R_UNKNOWN_NID 109 +# define X509_R_UNKNOWN_PURPOSE_ID 121 +# define X509_R_UNKNOWN_TRUST_ID 120 +# define X509_R_UNSUPPORTED_ALGORITHM 111 +# define X509_R_WRONG_LOOKUP_TYPE 112 +# define X509_R_WRONG_TYPE 122 + +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/x509v3.h b/example/android/third_party/crypto/arm/include/openssl/x509v3.h new file mode 100644 index 00000000..fe1791c6 --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/x509v3.h @@ -0,0 +1,935 @@ +/* + * Copyright 1999-2018 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_X509V3_H +# define HEADER_X509V3_H + +# include +# include +# include +# include + +#ifdef __cplusplus +extern "C" { +#endif + +/* Forward reference */ +struct v3_ext_method; +struct v3_ext_ctx; + +/* Useful typedefs */ + +typedef void *(*X509V3_EXT_NEW)(void); +typedef void (*X509V3_EXT_FREE) (void *); +typedef void *(*X509V3_EXT_D2I)(void *, const unsigned char **, long); +typedef int (*X509V3_EXT_I2D) (void *, unsigned char **); +typedef STACK_OF(CONF_VALUE) * + (*X509V3_EXT_I2V) (const struct v3_ext_method *method, void *ext, + STACK_OF(CONF_VALUE) *extlist); +typedef void *(*X509V3_EXT_V2I)(const struct v3_ext_method *method, + struct v3_ext_ctx *ctx, + STACK_OF(CONF_VALUE) *values); +typedef char *(*X509V3_EXT_I2S)(const struct v3_ext_method *method, + void *ext); +typedef void *(*X509V3_EXT_S2I)(const struct v3_ext_method *method, + struct v3_ext_ctx *ctx, const char *str); +typedef int (*X509V3_EXT_I2R) (const struct v3_ext_method *method, void *ext, + BIO *out, int indent); +typedef void *(*X509V3_EXT_R2I)(const struct v3_ext_method *method, + struct v3_ext_ctx *ctx, const char *str); + +/* V3 extension structure */ + +struct v3_ext_method { + int ext_nid; + int ext_flags; +/* If this is set the following four fields are ignored */ + ASN1_ITEM_EXP *it; +/* Old style ASN1 calls */ + X509V3_EXT_NEW ext_new; + X509V3_EXT_FREE ext_free; + X509V3_EXT_D2I d2i; + X509V3_EXT_I2D i2d; +/* The following pair is used for string extensions */ + X509V3_EXT_I2S i2s; + X509V3_EXT_S2I s2i; +/* The following pair is used for multi-valued extensions */ + X509V3_EXT_I2V i2v; + X509V3_EXT_V2I v2i; +/* The following are used for raw extensions */ + X509V3_EXT_I2R i2r; + X509V3_EXT_R2I r2i; + void *usr_data; /* Any extension specific data */ +}; + +typedef struct X509V3_CONF_METHOD_st { + char *(*get_string) (void *db, const char *section, const char *value); + STACK_OF(CONF_VALUE) *(*get_section) (void *db, const char *section); + void (*free_string) (void *db, char *string); + void (*free_section) (void *db, STACK_OF(CONF_VALUE) *section); +} X509V3_CONF_METHOD; + +/* Context specific info */ +struct v3_ext_ctx { +# define CTX_TEST 0x1 +# define X509V3_CTX_REPLACE 0x2 + int flags; + X509 *issuer_cert; + X509 *subject_cert; + X509_REQ *subject_req; + X509_CRL *crl; + X509V3_CONF_METHOD *db_meth; + void *db; +/* Maybe more here */ +}; + +typedef struct v3_ext_method X509V3_EXT_METHOD; + +DEFINE_STACK_OF(X509V3_EXT_METHOD) + +/* ext_flags values */ +# define X509V3_EXT_DYNAMIC 0x1 +# define X509V3_EXT_CTX_DEP 0x2 +# define X509V3_EXT_MULTILINE 0x4 + +typedef BIT_STRING_BITNAME ENUMERATED_NAMES; + +typedef struct BASIC_CONSTRAINTS_st { + int ca; + ASN1_INTEGER *pathlen; +} BASIC_CONSTRAINTS; + +typedef struct PKEY_USAGE_PERIOD_st { + ASN1_GENERALIZEDTIME *notBefore; + ASN1_GENERALIZEDTIME *notAfter; +} PKEY_USAGE_PERIOD; + +typedef struct otherName_st { + ASN1_OBJECT *type_id; + ASN1_TYPE *value; +} OTHERNAME; + +typedef struct EDIPartyName_st { + ASN1_STRING *nameAssigner; + ASN1_STRING *partyName; +} EDIPARTYNAME; + +typedef struct GENERAL_NAME_st { +# define GEN_OTHERNAME 0 +# define GEN_EMAIL 1 +# define GEN_DNS 2 +# define GEN_X400 3 +# define GEN_DIRNAME 4 +# define GEN_EDIPARTY 5 +# define GEN_URI 6 +# define GEN_IPADD 7 +# define GEN_RID 8 + int type; + union { + char *ptr; + OTHERNAME *otherName; /* otherName */ + ASN1_IA5STRING *rfc822Name; + ASN1_IA5STRING *dNSName; + ASN1_TYPE *x400Address; + X509_NAME *directoryName; + EDIPARTYNAME *ediPartyName; + ASN1_IA5STRING *uniformResourceIdentifier; + ASN1_OCTET_STRING *iPAddress; + ASN1_OBJECT *registeredID; + /* Old names */ + ASN1_OCTET_STRING *ip; /* iPAddress */ + X509_NAME *dirn; /* dirn */ + ASN1_IA5STRING *ia5; /* rfc822Name, dNSName, + * uniformResourceIdentifier */ + ASN1_OBJECT *rid; /* registeredID */ + ASN1_TYPE *other; /* x400Address */ + } d; +} GENERAL_NAME; + +typedef struct ACCESS_DESCRIPTION_st { + ASN1_OBJECT *method; + GENERAL_NAME *location; +} ACCESS_DESCRIPTION; + +typedef STACK_OF(ACCESS_DESCRIPTION) AUTHORITY_INFO_ACCESS; + +typedef STACK_OF(ASN1_OBJECT) EXTENDED_KEY_USAGE; + +typedef STACK_OF(ASN1_INTEGER) TLS_FEATURE; + +DEFINE_STACK_OF(GENERAL_NAME) +typedef STACK_OF(GENERAL_NAME) GENERAL_NAMES; +DEFINE_STACK_OF(GENERAL_NAMES) + +DEFINE_STACK_OF(ACCESS_DESCRIPTION) + +typedef struct DIST_POINT_NAME_st { + int type; + union { + GENERAL_NAMES *fullname; + STACK_OF(X509_NAME_ENTRY) *relativename; + } name; +/* If relativename then this contains the full distribution point name */ + X509_NAME *dpname; +} DIST_POINT_NAME; +/* All existing reasons */ +# define CRLDP_ALL_REASONS 0x807f + +# define CRL_REASON_NONE -1 +# define CRL_REASON_UNSPECIFIED 0 +# define CRL_REASON_KEY_COMPROMISE 1 +# define CRL_REASON_CA_COMPROMISE 2 +# define CRL_REASON_AFFILIATION_CHANGED 3 +# define CRL_REASON_SUPERSEDED 4 +# define CRL_REASON_CESSATION_OF_OPERATION 5 +# define CRL_REASON_CERTIFICATE_HOLD 6 +# define CRL_REASON_REMOVE_FROM_CRL 8 +# define CRL_REASON_PRIVILEGE_WITHDRAWN 9 +# define CRL_REASON_AA_COMPROMISE 10 + +struct DIST_POINT_st { + DIST_POINT_NAME *distpoint; + ASN1_BIT_STRING *reasons; + GENERAL_NAMES *CRLissuer; + int dp_reasons; +}; + +typedef STACK_OF(DIST_POINT) CRL_DIST_POINTS; + +DEFINE_STACK_OF(DIST_POINT) + +struct AUTHORITY_KEYID_st { + ASN1_OCTET_STRING *keyid; + GENERAL_NAMES *issuer; + ASN1_INTEGER *serial; +}; + +/* Strong extranet structures */ + +typedef struct SXNET_ID_st { + ASN1_INTEGER *zone; + ASN1_OCTET_STRING *user; +} SXNETID; + +DEFINE_STACK_OF(SXNETID) + +typedef struct SXNET_st { + ASN1_INTEGER *version; + STACK_OF(SXNETID) *ids; +} SXNET; + +typedef struct NOTICEREF_st { + ASN1_STRING *organization; + STACK_OF(ASN1_INTEGER) *noticenos; +} NOTICEREF; + +typedef struct USERNOTICE_st { + NOTICEREF *noticeref; + ASN1_STRING *exptext; +} USERNOTICE; + +typedef struct POLICYQUALINFO_st { + ASN1_OBJECT *pqualid; + union { + ASN1_IA5STRING *cpsuri; + USERNOTICE *usernotice; + ASN1_TYPE *other; + } d; +} POLICYQUALINFO; + +DEFINE_STACK_OF(POLICYQUALINFO) + +typedef struct POLICYINFO_st { + ASN1_OBJECT *policyid; + STACK_OF(POLICYQUALINFO) *qualifiers; +} POLICYINFO; + +typedef STACK_OF(POLICYINFO) CERTIFICATEPOLICIES; + +DEFINE_STACK_OF(POLICYINFO) + +typedef struct POLICY_MAPPING_st { + ASN1_OBJECT *issuerDomainPolicy; + ASN1_OBJECT *subjectDomainPolicy; +} POLICY_MAPPING; + +DEFINE_STACK_OF(POLICY_MAPPING) + +typedef STACK_OF(POLICY_MAPPING) POLICY_MAPPINGS; + +typedef struct GENERAL_SUBTREE_st { + GENERAL_NAME *base; + ASN1_INTEGER *minimum; + ASN1_INTEGER *maximum; +} GENERAL_SUBTREE; + +DEFINE_STACK_OF(GENERAL_SUBTREE) + +struct NAME_CONSTRAINTS_st { + STACK_OF(GENERAL_SUBTREE) *permittedSubtrees; + STACK_OF(GENERAL_SUBTREE) *excludedSubtrees; +}; + +typedef struct POLICY_CONSTRAINTS_st { + ASN1_INTEGER *requireExplicitPolicy; + ASN1_INTEGER *inhibitPolicyMapping; +} POLICY_CONSTRAINTS; + +/* Proxy certificate structures, see RFC 3820 */ +typedef struct PROXY_POLICY_st { + ASN1_OBJECT *policyLanguage; + ASN1_OCTET_STRING *policy; +} PROXY_POLICY; + +typedef struct PROXY_CERT_INFO_EXTENSION_st { + ASN1_INTEGER *pcPathLengthConstraint; + PROXY_POLICY *proxyPolicy; +} PROXY_CERT_INFO_EXTENSION; + +DECLARE_ASN1_FUNCTIONS(PROXY_POLICY) +DECLARE_ASN1_FUNCTIONS(PROXY_CERT_INFO_EXTENSION) + +struct ISSUING_DIST_POINT_st { + DIST_POINT_NAME *distpoint; + int onlyuser; + int onlyCA; + ASN1_BIT_STRING *onlysomereasons; + int indirectCRL; + int onlyattr; +}; + +/* Values in idp_flags field */ +/* IDP present */ +# define IDP_PRESENT 0x1 +/* IDP values inconsistent */ +# define IDP_INVALID 0x2 +/* onlyuser true */ +# define IDP_ONLYUSER 0x4 +/* onlyCA true */ +# define IDP_ONLYCA 0x8 +/* onlyattr true */ +# define IDP_ONLYATTR 0x10 +/* indirectCRL true */ +# define IDP_INDIRECT 0x20 +/* onlysomereasons present */ +# define IDP_REASONS 0x40 + +# define X509V3_conf_err(val) ERR_add_error_data(6, \ + "section:", (val)->section, \ + ",name:", (val)->name, ",value:", (val)->value) + +# define X509V3_set_ctx_test(ctx) \ + X509V3_set_ctx(ctx, NULL, NULL, NULL, NULL, CTX_TEST) +# define X509V3_set_ctx_nodb(ctx) (ctx)->db = NULL; + +# define EXT_BITSTRING(nid, table) { nid, 0, ASN1_ITEM_ref(ASN1_BIT_STRING), \ + 0,0,0,0, \ + 0,0, \ + (X509V3_EXT_I2V)i2v_ASN1_BIT_STRING, \ + (X509V3_EXT_V2I)v2i_ASN1_BIT_STRING, \ + NULL, NULL, \ + table} + +# define EXT_IA5STRING(nid) { nid, 0, ASN1_ITEM_ref(ASN1_IA5STRING), \ + 0,0,0,0, \ + (X509V3_EXT_I2S)i2s_ASN1_IA5STRING, \ + (X509V3_EXT_S2I)s2i_ASN1_IA5STRING, \ + 0,0,0,0, \ + NULL} + +# define EXT_END { -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} + +/* X509_PURPOSE stuff */ + +# define EXFLAG_BCONS 0x1 +# define EXFLAG_KUSAGE 0x2 +# define EXFLAG_XKUSAGE 0x4 +# define EXFLAG_NSCERT 0x8 + +# define EXFLAG_CA 0x10 +/* Really self issued not necessarily self signed */ +# define EXFLAG_SI 0x20 +# define EXFLAG_V1 0x40 +# define EXFLAG_INVALID 0x80 +/* EXFLAG_SET is set to indicate that some values have been precomputed */ +# define EXFLAG_SET 0x100 +# define EXFLAG_CRITICAL 0x200 +# define EXFLAG_PROXY 0x400 + +# define EXFLAG_INVALID_POLICY 0x800 +# define EXFLAG_FRESHEST 0x1000 +/* Self signed */ +# define EXFLAG_SS 0x2000 + +# define KU_DIGITAL_SIGNATURE 0x0080 +# define KU_NON_REPUDIATION 0x0040 +# define KU_KEY_ENCIPHERMENT 0x0020 +# define KU_DATA_ENCIPHERMENT 0x0010 +# define KU_KEY_AGREEMENT 0x0008 +# define KU_KEY_CERT_SIGN 0x0004 +# define KU_CRL_SIGN 0x0002 +# define KU_ENCIPHER_ONLY 0x0001 +# define KU_DECIPHER_ONLY 0x8000 + +# define NS_SSL_CLIENT 0x80 +# define NS_SSL_SERVER 0x40 +# define NS_SMIME 0x20 +# define NS_OBJSIGN 0x10 +# define NS_SSL_CA 0x04 +# define NS_SMIME_CA 0x02 +# define NS_OBJSIGN_CA 0x01 +# define NS_ANY_CA (NS_SSL_CA|NS_SMIME_CA|NS_OBJSIGN_CA) + +# define XKU_SSL_SERVER 0x1 +# define XKU_SSL_CLIENT 0x2 +# define XKU_SMIME 0x4 +# define XKU_CODE_SIGN 0x8 +# define XKU_SGC 0x10 +# define XKU_OCSP_SIGN 0x20 +# define XKU_TIMESTAMP 0x40 +# define XKU_DVCS 0x80 +# define XKU_ANYEKU 0x100 + +# define X509_PURPOSE_DYNAMIC 0x1 +# define X509_PURPOSE_DYNAMIC_NAME 0x2 + +typedef struct x509_purpose_st { + int purpose; + int trust; /* Default trust ID */ + int flags; + int (*check_purpose) (const struct x509_purpose_st *, const X509 *, int); + char *name; + char *sname; + void *usr_data; +} X509_PURPOSE; + +# define X509_PURPOSE_SSL_CLIENT 1 +# define X509_PURPOSE_SSL_SERVER 2 +# define X509_PURPOSE_NS_SSL_SERVER 3 +# define X509_PURPOSE_SMIME_SIGN 4 +# define X509_PURPOSE_SMIME_ENCRYPT 5 +# define X509_PURPOSE_CRL_SIGN 6 +# define X509_PURPOSE_ANY 7 +# define X509_PURPOSE_OCSP_HELPER 8 +# define X509_PURPOSE_TIMESTAMP_SIGN 9 + +# define X509_PURPOSE_MIN 1 +# define X509_PURPOSE_MAX 9 + +/* Flags for X509V3_EXT_print() */ + +# define X509V3_EXT_UNKNOWN_MASK (0xfL << 16) +/* Return error for unknown extensions */ +# define X509V3_EXT_DEFAULT 0 +/* Print error for unknown extensions */ +# define X509V3_EXT_ERROR_UNKNOWN (1L << 16) +/* ASN1 parse unknown extensions */ +# define X509V3_EXT_PARSE_UNKNOWN (2L << 16) +/* BIO_dump unknown extensions */ +# define X509V3_EXT_DUMP_UNKNOWN (3L << 16) + +/* Flags for X509V3_add1_i2d */ + +# define X509V3_ADD_OP_MASK 0xfL +# define X509V3_ADD_DEFAULT 0L +# define X509V3_ADD_APPEND 1L +# define X509V3_ADD_REPLACE 2L +# define X509V3_ADD_REPLACE_EXISTING 3L +# define X509V3_ADD_KEEP_EXISTING 4L +# define X509V3_ADD_DELETE 5L +# define X509V3_ADD_SILENT 0x10 + +DEFINE_STACK_OF(X509_PURPOSE) + +DECLARE_ASN1_FUNCTIONS(BASIC_CONSTRAINTS) + +DECLARE_ASN1_FUNCTIONS(SXNET) +DECLARE_ASN1_FUNCTIONS(SXNETID) + +int SXNET_add_id_asc(SXNET **psx, const char *zone, const char *user, int userlen); +int SXNET_add_id_ulong(SXNET **psx, unsigned long lzone, const char *user, + int userlen); +int SXNET_add_id_INTEGER(SXNET **psx, ASN1_INTEGER *izone, const char *user, + int userlen); + +ASN1_OCTET_STRING *SXNET_get_id_asc(SXNET *sx, const char *zone); +ASN1_OCTET_STRING *SXNET_get_id_ulong(SXNET *sx, unsigned long lzone); +ASN1_OCTET_STRING *SXNET_get_id_INTEGER(SXNET *sx, ASN1_INTEGER *zone); + +DECLARE_ASN1_FUNCTIONS(AUTHORITY_KEYID) + +DECLARE_ASN1_FUNCTIONS(PKEY_USAGE_PERIOD) + +DECLARE_ASN1_FUNCTIONS(GENERAL_NAME) +GENERAL_NAME *GENERAL_NAME_dup(GENERAL_NAME *a); +int GENERAL_NAME_cmp(GENERAL_NAME *a, GENERAL_NAME *b); + +ASN1_BIT_STRING *v2i_ASN1_BIT_STRING(X509V3_EXT_METHOD *method, + X509V3_CTX *ctx, + STACK_OF(CONF_VALUE) *nval); +STACK_OF(CONF_VALUE) *i2v_ASN1_BIT_STRING(X509V3_EXT_METHOD *method, + ASN1_BIT_STRING *bits, + STACK_OF(CONF_VALUE) *extlist); +char *i2s_ASN1_IA5STRING(X509V3_EXT_METHOD *method, ASN1_IA5STRING *ia5); +ASN1_IA5STRING *s2i_ASN1_IA5STRING(X509V3_EXT_METHOD *method, + X509V3_CTX *ctx, const char *str); + +STACK_OF(CONF_VALUE) *i2v_GENERAL_NAME(X509V3_EXT_METHOD *method, + GENERAL_NAME *gen, + STACK_OF(CONF_VALUE) *ret); +int GENERAL_NAME_print(BIO *out, GENERAL_NAME *gen); + +DECLARE_ASN1_FUNCTIONS(GENERAL_NAMES) + +STACK_OF(CONF_VALUE) *i2v_GENERAL_NAMES(X509V3_EXT_METHOD *method, + GENERAL_NAMES *gen, + STACK_OF(CONF_VALUE) *extlist); +GENERAL_NAMES *v2i_GENERAL_NAMES(const X509V3_EXT_METHOD *method, + X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval); + +DECLARE_ASN1_FUNCTIONS(OTHERNAME) +DECLARE_ASN1_FUNCTIONS(EDIPARTYNAME) +int OTHERNAME_cmp(OTHERNAME *a, OTHERNAME *b); +void GENERAL_NAME_set0_value(GENERAL_NAME *a, int type, void *value); +void *GENERAL_NAME_get0_value(GENERAL_NAME *a, int *ptype); +int GENERAL_NAME_set0_othername(GENERAL_NAME *gen, + ASN1_OBJECT *oid, ASN1_TYPE *value); +int GENERAL_NAME_get0_otherName(GENERAL_NAME *gen, + ASN1_OBJECT **poid, ASN1_TYPE **pvalue); + +char *i2s_ASN1_OCTET_STRING(X509V3_EXT_METHOD *method, + const ASN1_OCTET_STRING *ia5); +ASN1_OCTET_STRING *s2i_ASN1_OCTET_STRING(X509V3_EXT_METHOD *method, + X509V3_CTX *ctx, const char *str); + +DECLARE_ASN1_FUNCTIONS(EXTENDED_KEY_USAGE) +int i2a_ACCESS_DESCRIPTION(BIO *bp, const ACCESS_DESCRIPTION *a); + +DECLARE_ASN1_ALLOC_FUNCTIONS(TLS_FEATURE) + +DECLARE_ASN1_FUNCTIONS(CERTIFICATEPOLICIES) +DECLARE_ASN1_FUNCTIONS(POLICYINFO) +DECLARE_ASN1_FUNCTIONS(POLICYQUALINFO) +DECLARE_ASN1_FUNCTIONS(USERNOTICE) +DECLARE_ASN1_FUNCTIONS(NOTICEREF) + +DECLARE_ASN1_FUNCTIONS(CRL_DIST_POINTS) +DECLARE_ASN1_FUNCTIONS(DIST_POINT) +DECLARE_ASN1_FUNCTIONS(DIST_POINT_NAME) +DECLARE_ASN1_FUNCTIONS(ISSUING_DIST_POINT) + +int DIST_POINT_set_dpname(DIST_POINT_NAME *dpn, X509_NAME *iname); + +int NAME_CONSTRAINTS_check(X509 *x, NAME_CONSTRAINTS *nc); +int NAME_CONSTRAINTS_check_CN(X509 *x, NAME_CONSTRAINTS *nc); + +DECLARE_ASN1_FUNCTIONS(ACCESS_DESCRIPTION) +DECLARE_ASN1_FUNCTIONS(AUTHORITY_INFO_ACCESS) + +DECLARE_ASN1_ITEM(POLICY_MAPPING) +DECLARE_ASN1_ALLOC_FUNCTIONS(POLICY_MAPPING) +DECLARE_ASN1_ITEM(POLICY_MAPPINGS) + +DECLARE_ASN1_ITEM(GENERAL_SUBTREE) +DECLARE_ASN1_ALLOC_FUNCTIONS(GENERAL_SUBTREE) + +DECLARE_ASN1_ITEM(NAME_CONSTRAINTS) +DECLARE_ASN1_ALLOC_FUNCTIONS(NAME_CONSTRAINTS) + +DECLARE_ASN1_ALLOC_FUNCTIONS(POLICY_CONSTRAINTS) +DECLARE_ASN1_ITEM(POLICY_CONSTRAINTS) + +GENERAL_NAME *a2i_GENERAL_NAME(GENERAL_NAME *out, + const X509V3_EXT_METHOD *method, + X509V3_CTX *ctx, int gen_type, + const char *value, int is_nc); + +# ifdef HEADER_CONF_H +GENERAL_NAME *v2i_GENERAL_NAME(const X509V3_EXT_METHOD *method, + X509V3_CTX *ctx, CONF_VALUE *cnf); +GENERAL_NAME *v2i_GENERAL_NAME_ex(GENERAL_NAME *out, + const X509V3_EXT_METHOD *method, + X509V3_CTX *ctx, CONF_VALUE *cnf, + int is_nc); +void X509V3_conf_free(CONF_VALUE *val); + +X509_EXTENSION *X509V3_EXT_nconf_nid(CONF *conf, X509V3_CTX *ctx, int ext_nid, + const char *value); +X509_EXTENSION *X509V3_EXT_nconf(CONF *conf, X509V3_CTX *ctx, const char *name, + const char *value); +int X509V3_EXT_add_nconf_sk(CONF *conf, X509V3_CTX *ctx, const char *section, + STACK_OF(X509_EXTENSION) **sk); +int X509V3_EXT_add_nconf(CONF *conf, X509V3_CTX *ctx, const char *section, + X509 *cert); +int X509V3_EXT_REQ_add_nconf(CONF *conf, X509V3_CTX *ctx, const char *section, + X509_REQ *req); +int X509V3_EXT_CRL_add_nconf(CONF *conf, X509V3_CTX *ctx, const char *section, + X509_CRL *crl); + +X509_EXTENSION *X509V3_EXT_conf_nid(LHASH_OF(CONF_VALUE) *conf, + X509V3_CTX *ctx, int ext_nid, + const char *value); +X509_EXTENSION *X509V3_EXT_conf(LHASH_OF(CONF_VALUE) *conf, X509V3_CTX *ctx, + const char *name, const char *value); +int X509V3_EXT_add_conf(LHASH_OF(CONF_VALUE) *conf, X509V3_CTX *ctx, + const char *section, X509 *cert); +int X509V3_EXT_REQ_add_conf(LHASH_OF(CONF_VALUE) *conf, X509V3_CTX *ctx, + const char *section, X509_REQ *req); +int X509V3_EXT_CRL_add_conf(LHASH_OF(CONF_VALUE) *conf, X509V3_CTX *ctx, + const char *section, X509_CRL *crl); + +int X509V3_add_value_bool_nf(const char *name, int asn1_bool, + STACK_OF(CONF_VALUE) **extlist); +int X509V3_get_value_bool(const CONF_VALUE *value, int *asn1_bool); +int X509V3_get_value_int(const CONF_VALUE *value, ASN1_INTEGER **aint); +void X509V3_set_nconf(X509V3_CTX *ctx, CONF *conf); +void X509V3_set_conf_lhash(X509V3_CTX *ctx, LHASH_OF(CONF_VALUE) *lhash); +# endif + +char *X509V3_get_string(X509V3_CTX *ctx, const char *name, const char *section); +STACK_OF(CONF_VALUE) *X509V3_get_section(X509V3_CTX *ctx, const char *section); +void X509V3_string_free(X509V3_CTX *ctx, char *str); +void X509V3_section_free(X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *section); +void X509V3_set_ctx(X509V3_CTX *ctx, X509 *issuer, X509 *subject, + X509_REQ *req, X509_CRL *crl, int flags); + +int X509V3_add_value(const char *name, const char *value, + STACK_OF(CONF_VALUE) **extlist); +int X509V3_add_value_uchar(const char *name, const unsigned char *value, + STACK_OF(CONF_VALUE) **extlist); +int X509V3_add_value_bool(const char *name, int asn1_bool, + STACK_OF(CONF_VALUE) **extlist); +int X509V3_add_value_int(const char *name, const ASN1_INTEGER *aint, + STACK_OF(CONF_VALUE) **extlist); +char *i2s_ASN1_INTEGER(X509V3_EXT_METHOD *meth, const ASN1_INTEGER *aint); +ASN1_INTEGER *s2i_ASN1_INTEGER(X509V3_EXT_METHOD *meth, const char *value); +char *i2s_ASN1_ENUMERATED(X509V3_EXT_METHOD *meth, const ASN1_ENUMERATED *aint); +char *i2s_ASN1_ENUMERATED_TABLE(X509V3_EXT_METHOD *meth, + const ASN1_ENUMERATED *aint); +int X509V3_EXT_add(X509V3_EXT_METHOD *ext); +int X509V3_EXT_add_list(X509V3_EXT_METHOD *extlist); +int X509V3_EXT_add_alias(int nid_to, int nid_from); +void X509V3_EXT_cleanup(void); + +const X509V3_EXT_METHOD *X509V3_EXT_get(X509_EXTENSION *ext); +const X509V3_EXT_METHOD *X509V3_EXT_get_nid(int nid); +int X509V3_add_standard_extensions(void); +STACK_OF(CONF_VALUE) *X509V3_parse_list(const char *line); +void *X509V3_EXT_d2i(X509_EXTENSION *ext); +void *X509V3_get_d2i(const STACK_OF(X509_EXTENSION) *x, int nid, int *crit, + int *idx); + +X509_EXTENSION *X509V3_EXT_i2d(int ext_nid, int crit, void *ext_struc); +int X509V3_add1_i2d(STACK_OF(X509_EXTENSION) **x, int nid, void *value, + int crit, unsigned long flags); + +#if OPENSSL_API_COMPAT < 0x10100000L +/* The new declarations are in crypto.h, but the old ones were here. */ +# define hex_to_string OPENSSL_buf2hexstr +# define string_to_hex OPENSSL_hexstr2buf +#endif + +void X509V3_EXT_val_prn(BIO *out, STACK_OF(CONF_VALUE) *val, int indent, + int ml); +int X509V3_EXT_print(BIO *out, X509_EXTENSION *ext, unsigned long flag, + int indent); +#ifndef OPENSSL_NO_STDIO +int X509V3_EXT_print_fp(FILE *out, X509_EXTENSION *ext, int flag, int indent); +#endif +int X509V3_extensions_print(BIO *out, const char *title, + const STACK_OF(X509_EXTENSION) *exts, + unsigned long flag, int indent); + +int X509_check_ca(X509 *x); +int X509_check_purpose(X509 *x, int id, int ca); +int X509_supported_extension(X509_EXTENSION *ex); +int X509_PURPOSE_set(int *p, int purpose); +int X509_check_issued(X509 *issuer, X509 *subject); +int X509_check_akid(X509 *issuer, AUTHORITY_KEYID *akid); +void X509_set_proxy_flag(X509 *x); +void X509_set_proxy_pathlen(X509 *x, long l); +long X509_get_proxy_pathlen(X509 *x); + +uint32_t X509_get_extension_flags(X509 *x); +uint32_t X509_get_key_usage(X509 *x); +uint32_t X509_get_extended_key_usage(X509 *x); +const ASN1_OCTET_STRING *X509_get0_subject_key_id(X509 *x); +const ASN1_OCTET_STRING *X509_get0_authority_key_id(X509 *x); + +int X509_PURPOSE_get_count(void); +X509_PURPOSE *X509_PURPOSE_get0(int idx); +int X509_PURPOSE_get_by_sname(const char *sname); +int X509_PURPOSE_get_by_id(int id); +int X509_PURPOSE_add(int id, int trust, int flags, + int (*ck) (const X509_PURPOSE *, const X509 *, int), + const char *name, const char *sname, void *arg); +char *X509_PURPOSE_get0_name(const X509_PURPOSE *xp); +char *X509_PURPOSE_get0_sname(const X509_PURPOSE *xp); +int X509_PURPOSE_get_trust(const X509_PURPOSE *xp); +void X509_PURPOSE_cleanup(void); +int X509_PURPOSE_get_id(const X509_PURPOSE *); + +STACK_OF(OPENSSL_STRING) *X509_get1_email(X509 *x); +STACK_OF(OPENSSL_STRING) *X509_REQ_get1_email(X509_REQ *x); +void X509_email_free(STACK_OF(OPENSSL_STRING) *sk); +STACK_OF(OPENSSL_STRING) *X509_get1_ocsp(X509 *x); +/* Flags for X509_check_* functions */ + +/* + * Always check subject name for host match even if subject alt names present + */ +# define X509_CHECK_FLAG_ALWAYS_CHECK_SUBJECT 0x1 +/* Disable wildcard matching for dnsName fields and common name. */ +# define X509_CHECK_FLAG_NO_WILDCARDS 0x2 +/* Wildcards must not match a partial label. */ +# define X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS 0x4 +/* Allow (non-partial) wildcards to match multiple labels. */ +# define X509_CHECK_FLAG_MULTI_LABEL_WILDCARDS 0x8 +/* Constraint verifier subdomain patterns to match a single labels. */ +# define X509_CHECK_FLAG_SINGLE_LABEL_SUBDOMAINS 0x10 +/* Never check the subject CN */ +# define X509_CHECK_FLAG_NEVER_CHECK_SUBJECT 0x20 +/* + * Match reference identifiers starting with "." to any sub-domain. + * This is a non-public flag, turned on implicitly when the subject + * reference identity is a DNS name. + */ +# define _X509_CHECK_FLAG_DOT_SUBDOMAINS 0x8000 + +int X509_check_host(X509 *x, const char *chk, size_t chklen, + unsigned int flags, char **peername); +int X509_check_email(X509 *x, const char *chk, size_t chklen, + unsigned int flags); +int X509_check_ip(X509 *x, const unsigned char *chk, size_t chklen, + unsigned int flags); +int X509_check_ip_asc(X509 *x, const char *ipasc, unsigned int flags); + +ASN1_OCTET_STRING *a2i_IPADDRESS(const char *ipasc); +ASN1_OCTET_STRING *a2i_IPADDRESS_NC(const char *ipasc); +int X509V3_NAME_from_section(X509_NAME *nm, STACK_OF(CONF_VALUE) *dn_sk, + unsigned long chtype); + +void X509_POLICY_NODE_print(BIO *out, X509_POLICY_NODE *node, int indent); +DEFINE_STACK_OF(X509_POLICY_NODE) + +#ifndef OPENSSL_NO_RFC3779 +typedef struct ASRange_st { + ASN1_INTEGER *min, *max; +} ASRange; + +# define ASIdOrRange_id 0 +# define ASIdOrRange_range 1 + +typedef struct ASIdOrRange_st { + int type; + union { + ASN1_INTEGER *id; + ASRange *range; + } u; +} ASIdOrRange; + +typedef STACK_OF(ASIdOrRange) ASIdOrRanges; +DEFINE_STACK_OF(ASIdOrRange) + +# define ASIdentifierChoice_inherit 0 +# define ASIdentifierChoice_asIdsOrRanges 1 + +typedef struct ASIdentifierChoice_st { + int type; + union { + ASN1_NULL *inherit; + ASIdOrRanges *asIdsOrRanges; + } u; +} ASIdentifierChoice; + +typedef struct ASIdentifiers_st { + ASIdentifierChoice *asnum, *rdi; +} ASIdentifiers; + +DECLARE_ASN1_FUNCTIONS(ASRange) +DECLARE_ASN1_FUNCTIONS(ASIdOrRange) +DECLARE_ASN1_FUNCTIONS(ASIdentifierChoice) +DECLARE_ASN1_FUNCTIONS(ASIdentifiers) + +typedef struct IPAddressRange_st { + ASN1_BIT_STRING *min, *max; +} IPAddressRange; + +# define IPAddressOrRange_addressPrefix 0 +# define IPAddressOrRange_addressRange 1 + +typedef struct IPAddressOrRange_st { + int type; + union { + ASN1_BIT_STRING *addressPrefix; + IPAddressRange *addressRange; + } u; +} IPAddressOrRange; + +typedef STACK_OF(IPAddressOrRange) IPAddressOrRanges; +DEFINE_STACK_OF(IPAddressOrRange) + +# define IPAddressChoice_inherit 0 +# define IPAddressChoice_addressesOrRanges 1 + +typedef struct IPAddressChoice_st { + int type; + union { + ASN1_NULL *inherit; + IPAddressOrRanges *addressesOrRanges; + } u; +} IPAddressChoice; + +typedef struct IPAddressFamily_st { + ASN1_OCTET_STRING *addressFamily; + IPAddressChoice *ipAddressChoice; +} IPAddressFamily; + +typedef STACK_OF(IPAddressFamily) IPAddrBlocks; +DEFINE_STACK_OF(IPAddressFamily) + +DECLARE_ASN1_FUNCTIONS(IPAddressRange) +DECLARE_ASN1_FUNCTIONS(IPAddressOrRange) +DECLARE_ASN1_FUNCTIONS(IPAddressChoice) +DECLARE_ASN1_FUNCTIONS(IPAddressFamily) + +/* + * API tag for elements of the ASIdentifer SEQUENCE. + */ +# define V3_ASID_ASNUM 0 +# define V3_ASID_RDI 1 + +/* + * AFI values, assigned by IANA. It'd be nice to make the AFI + * handling code totally generic, but there are too many little things + * that would need to be defined for other address families for it to + * be worth the trouble. + */ +# define IANA_AFI_IPV4 1 +# define IANA_AFI_IPV6 2 + +/* + * Utilities to construct and extract values from RFC3779 extensions, + * since some of the encodings (particularly for IP address prefixes + * and ranges) are a bit tedious to work with directly. + */ +int X509v3_asid_add_inherit(ASIdentifiers *asid, int which); +int X509v3_asid_add_id_or_range(ASIdentifiers *asid, int which, + ASN1_INTEGER *min, ASN1_INTEGER *max); +int X509v3_addr_add_inherit(IPAddrBlocks *addr, + const unsigned afi, const unsigned *safi); +int X509v3_addr_add_prefix(IPAddrBlocks *addr, + const unsigned afi, const unsigned *safi, + unsigned char *a, const int prefixlen); +int X509v3_addr_add_range(IPAddrBlocks *addr, + const unsigned afi, const unsigned *safi, + unsigned char *min, unsigned char *max); +unsigned X509v3_addr_get_afi(const IPAddressFamily *f); +int X509v3_addr_get_range(IPAddressOrRange *aor, const unsigned afi, + unsigned char *min, unsigned char *max, + const int length); + +/* + * Canonical forms. + */ +int X509v3_asid_is_canonical(ASIdentifiers *asid); +int X509v3_addr_is_canonical(IPAddrBlocks *addr); +int X509v3_asid_canonize(ASIdentifiers *asid); +int X509v3_addr_canonize(IPAddrBlocks *addr); + +/* + * Tests for inheritance and containment. + */ +int X509v3_asid_inherits(ASIdentifiers *asid); +int X509v3_addr_inherits(IPAddrBlocks *addr); +int X509v3_asid_subset(ASIdentifiers *a, ASIdentifiers *b); +int X509v3_addr_subset(IPAddrBlocks *a, IPAddrBlocks *b); + +/* + * Check whether RFC 3779 extensions nest properly in chains. + */ +int X509v3_asid_validate_path(X509_STORE_CTX *); +int X509v3_addr_validate_path(X509_STORE_CTX *); +int X509v3_asid_validate_resource_set(STACK_OF(X509) *chain, + ASIdentifiers *ext, + int allow_inheritance); +int X509v3_addr_validate_resource_set(STACK_OF(X509) *chain, + IPAddrBlocks *ext, int allow_inheritance); + +#endif /* OPENSSL_NO_RFC3779 */ + +DEFINE_STACK_OF(ASN1_STRING) + +/* + * Admission Syntax + */ +typedef struct NamingAuthority_st NAMING_AUTHORITY; +typedef struct ProfessionInfo_st PROFESSION_INFO; +typedef struct Admissions_st ADMISSIONS; +typedef struct AdmissionSyntax_st ADMISSION_SYNTAX; +DECLARE_ASN1_FUNCTIONS(NAMING_AUTHORITY) +DECLARE_ASN1_FUNCTIONS(PROFESSION_INFO) +DECLARE_ASN1_FUNCTIONS(ADMISSIONS) +DECLARE_ASN1_FUNCTIONS(ADMISSION_SYNTAX) +DEFINE_STACK_OF(ADMISSIONS) +DEFINE_STACK_OF(PROFESSION_INFO) +typedef STACK_OF(PROFESSION_INFO) PROFESSION_INFOS; + +const ASN1_OBJECT *NAMING_AUTHORITY_get0_authorityId( + const NAMING_AUTHORITY *n); +const ASN1_IA5STRING *NAMING_AUTHORITY_get0_authorityURL( + const NAMING_AUTHORITY *n); +const ASN1_STRING *NAMING_AUTHORITY_get0_authorityText( + const NAMING_AUTHORITY *n); +void NAMING_AUTHORITY_set0_authorityId(NAMING_AUTHORITY *n, + ASN1_OBJECT* namingAuthorityId); +void NAMING_AUTHORITY_set0_authorityURL(NAMING_AUTHORITY *n, + ASN1_IA5STRING* namingAuthorityUrl); +void NAMING_AUTHORITY_set0_authorityText(NAMING_AUTHORITY *n, + ASN1_STRING* namingAuthorityText); + +const GENERAL_NAME *ADMISSION_SYNTAX_get0_admissionAuthority( + const ADMISSION_SYNTAX *as); +void ADMISSION_SYNTAX_set0_admissionAuthority( + ADMISSION_SYNTAX *as, GENERAL_NAME *aa); +const STACK_OF(ADMISSIONS) *ADMISSION_SYNTAX_get0_contentsOfAdmissions( + const ADMISSION_SYNTAX *as); +void ADMISSION_SYNTAX_set0_contentsOfAdmissions( + ADMISSION_SYNTAX *as, STACK_OF(ADMISSIONS) *a); +const GENERAL_NAME *ADMISSIONS_get0_admissionAuthority(const ADMISSIONS *a); +void ADMISSIONS_set0_admissionAuthority(ADMISSIONS *a, GENERAL_NAME *aa); +const NAMING_AUTHORITY *ADMISSIONS_get0_namingAuthority(const ADMISSIONS *a); +void ADMISSIONS_set0_namingAuthority(ADMISSIONS *a, NAMING_AUTHORITY *na); +const PROFESSION_INFOS *ADMISSIONS_get0_professionInfos(const ADMISSIONS *a); +void ADMISSIONS_set0_professionInfos(ADMISSIONS *a, PROFESSION_INFOS *pi); +const ASN1_OCTET_STRING *PROFESSION_INFO_get0_addProfessionInfo( + const PROFESSION_INFO *pi); +void PROFESSION_INFO_set0_addProfessionInfo( + PROFESSION_INFO *pi, ASN1_OCTET_STRING *aos); +const NAMING_AUTHORITY *PROFESSION_INFO_get0_namingAuthority( + const PROFESSION_INFO *pi); +void PROFESSION_INFO_set0_namingAuthority( + PROFESSION_INFO *pi, NAMING_AUTHORITY *na); +const STACK_OF(ASN1_STRING) *PROFESSION_INFO_get0_professionItems( + const PROFESSION_INFO *pi); +void PROFESSION_INFO_set0_professionItems( + PROFESSION_INFO *pi, STACK_OF(ASN1_STRING) *as); +const STACK_OF(ASN1_OBJECT) *PROFESSION_INFO_get0_professionOIDs( + const PROFESSION_INFO *pi); +void PROFESSION_INFO_set0_professionOIDs( + PROFESSION_INFO *pi, STACK_OF(ASN1_OBJECT) *po); +const ASN1_PRINTABLESTRING *PROFESSION_INFO_get0_registrationNumber( + const PROFESSION_INFO *pi); +void PROFESSION_INFO_set0_registrationNumber( + PROFESSION_INFO *pi, ASN1_PRINTABLESTRING *rn); + +# ifdef __cplusplus +} +# endif +#endif diff --git a/example/android/third_party/crypto/arm/include/openssl/x509v3err.h b/example/android/third_party/crypto/arm/include/openssl/x509v3err.h new file mode 100644 index 00000000..6b3df12b --- /dev/null +++ b/example/android/third_party/crypto/arm/include/openssl/x509v3err.h @@ -0,0 +1,158 @@ +/* + * Generated by util/mkerr.pl DO NOT EDIT + * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef HEADER_X509V3ERR_H +# define HEADER_X509V3ERR_H + +# ifdef __cplusplus +extern "C" +# endif +int ERR_load_X509V3_strings(void); + +/* + * X509V3 function codes. + */ +# define X509V3_F_A2I_GENERAL_NAME 164 +# define X509V3_F_ADDR_VALIDATE_PATH_INTERNAL 166 +# define X509V3_F_ASIDENTIFIERCHOICE_CANONIZE 161 +# define X509V3_F_ASIDENTIFIERCHOICE_IS_CANONICAL 162 +# define X509V3_F_BIGNUM_TO_STRING 167 +# define X509V3_F_COPY_EMAIL 122 +# define X509V3_F_COPY_ISSUER 123 +# define X509V3_F_DO_DIRNAME 144 +# define X509V3_F_DO_EXT_I2D 135 +# define X509V3_F_DO_EXT_NCONF 151 +# define X509V3_F_GNAMES_FROM_SECTNAME 156 +# define X509V3_F_I2S_ASN1_ENUMERATED 121 +# define X509V3_F_I2S_ASN1_IA5STRING 149 +# define X509V3_F_I2S_ASN1_INTEGER 120 +# define X509V3_F_I2V_AUTHORITY_INFO_ACCESS 138 +# define X509V3_F_LEVEL_ADD_NODE 168 +# define X509V3_F_NOTICE_SECTION 132 +# define X509V3_F_NREF_NOS 133 +# define X509V3_F_POLICY_CACHE_CREATE 169 +# define X509V3_F_POLICY_CACHE_NEW 170 +# define X509V3_F_POLICY_DATA_NEW 171 +# define X509V3_F_POLICY_SECTION 131 +# define X509V3_F_PROCESS_PCI_VALUE 150 +# define X509V3_F_R2I_CERTPOL 130 +# define X509V3_F_R2I_PCI 155 +# define X509V3_F_S2I_ASN1_IA5STRING 100 +# define X509V3_F_S2I_ASN1_INTEGER 108 +# define X509V3_F_S2I_ASN1_OCTET_STRING 112 +# define X509V3_F_S2I_SKEY_ID 115 +# define X509V3_F_SET_DIST_POINT_NAME 158 +# define X509V3_F_SXNET_ADD_ID_ASC 125 +# define X509V3_F_SXNET_ADD_ID_INTEGER 126 +# define X509V3_F_SXNET_ADD_ID_ULONG 127 +# define X509V3_F_SXNET_GET_ID_ASC 128 +# define X509V3_F_SXNET_GET_ID_ULONG 129 +# define X509V3_F_TREE_INIT 172 +# define X509V3_F_V2I_ASIDENTIFIERS 163 +# define X509V3_F_V2I_ASN1_BIT_STRING 101 +# define X509V3_F_V2I_AUTHORITY_INFO_ACCESS 139 +# define X509V3_F_V2I_AUTHORITY_KEYID 119 +# define X509V3_F_V2I_BASIC_CONSTRAINTS 102 +# define X509V3_F_V2I_CRLD 134 +# define X509V3_F_V2I_EXTENDED_KEY_USAGE 103 +# define X509V3_F_V2I_GENERAL_NAMES 118 +# define X509V3_F_V2I_GENERAL_NAME_EX 117 +# define X509V3_F_V2I_IDP 157 +# define X509V3_F_V2I_IPADDRBLOCKS 159 +# define X509V3_F_V2I_ISSUER_ALT 153 +# define X509V3_F_V2I_NAME_CONSTRAINTS 147 +# define X509V3_F_V2I_POLICY_CONSTRAINTS 146 +# define X509V3_F_V2I_POLICY_MAPPINGS 145 +# define X509V3_F_V2I_SUBJECT_ALT 154 +# define X509V3_F_V2I_TLS_FEATURE 165 +# define X509V3_F_V3_GENERIC_EXTENSION 116 +# define X509V3_F_X509V3_ADD1_I2D 140 +# define X509V3_F_X509V3_ADD_VALUE 105 +# define X509V3_F_X509V3_EXT_ADD 104 +# define X509V3_F_X509V3_EXT_ADD_ALIAS 106 +# define X509V3_F_X509V3_EXT_I2D 136 +# define X509V3_F_X509V3_EXT_NCONF 152 +# define X509V3_F_X509V3_GET_SECTION 142 +# define X509V3_F_X509V3_GET_STRING 143 +# define X509V3_F_X509V3_GET_VALUE_BOOL 110 +# define X509V3_F_X509V3_PARSE_LIST 109 +# define X509V3_F_X509_PURPOSE_ADD 137 +# define X509V3_F_X509_PURPOSE_SET 141 + +/* + * X509V3 reason codes. + */ +# define X509V3_R_BAD_IP_ADDRESS 118 +# define X509V3_R_BAD_OBJECT 119 +# define X509V3_R_BN_DEC2BN_ERROR 100 +# define X509V3_R_BN_TO_ASN1_INTEGER_ERROR 101 +# define X509V3_R_DIRNAME_ERROR 149 +# define X509V3_R_DISTPOINT_ALREADY_SET 160 +# define X509V3_R_DUPLICATE_ZONE_ID 133 +# define X509V3_R_ERROR_CONVERTING_ZONE 131 +# define X509V3_R_ERROR_CREATING_EXTENSION 144 +# define X509V3_R_ERROR_IN_EXTENSION 128 +# define X509V3_R_EXPECTED_A_SECTION_NAME 137 +# define X509V3_R_EXTENSION_EXISTS 145 +# define X509V3_R_EXTENSION_NAME_ERROR 115 +# define X509V3_R_EXTENSION_NOT_FOUND 102 +# define X509V3_R_EXTENSION_SETTING_NOT_SUPPORTED 103 +# define X509V3_R_EXTENSION_VALUE_ERROR 116 +# define X509V3_R_ILLEGAL_EMPTY_EXTENSION 151 +# define X509V3_R_INCORRECT_POLICY_SYNTAX_TAG 152 +# define X509V3_R_INVALID_ASNUMBER 162 +# define X509V3_R_INVALID_ASRANGE 163 +# define X509V3_R_INVALID_BOOLEAN_STRING 104 +# define X509V3_R_INVALID_EXTENSION_STRING 105 +# define X509V3_R_INVALID_INHERITANCE 165 +# define X509V3_R_INVALID_IPADDRESS 166 +# define X509V3_R_INVALID_MULTIPLE_RDNS 161 +# define X509V3_R_INVALID_NAME 106 +# define X509V3_R_INVALID_NULL_ARGUMENT 107 +# define X509V3_R_INVALID_NULL_NAME 108 +# define X509V3_R_INVALID_NULL_VALUE 109 +# define X509V3_R_INVALID_NUMBER 140 +# define X509V3_R_INVALID_NUMBERS 141 +# define X509V3_R_INVALID_OBJECT_IDENTIFIER 110 +# define X509V3_R_INVALID_OPTION 138 +# define X509V3_R_INVALID_POLICY_IDENTIFIER 134 +# define X509V3_R_INVALID_PROXY_POLICY_SETTING 153 +# define X509V3_R_INVALID_PURPOSE 146 +# define X509V3_R_INVALID_SAFI 164 +# define X509V3_R_INVALID_SECTION 135 +# define X509V3_R_INVALID_SYNTAX 143 +# define X509V3_R_ISSUER_DECODE_ERROR 126 +# define X509V3_R_MISSING_VALUE 124 +# define X509V3_R_NEED_ORGANIZATION_AND_NUMBERS 142 +# define X509V3_R_NO_CONFIG_DATABASE 136 +# define X509V3_R_NO_ISSUER_CERTIFICATE 121 +# define X509V3_R_NO_ISSUER_DETAILS 127 +# define X509V3_R_NO_POLICY_IDENTIFIER 139 +# define X509V3_R_NO_PROXY_CERT_POLICY_LANGUAGE_DEFINED 154 +# define X509V3_R_NO_PUBLIC_KEY 114 +# define X509V3_R_NO_SUBJECT_DETAILS 125 +# define X509V3_R_OPERATION_NOT_DEFINED 148 +# define X509V3_R_OTHERNAME_ERROR 147 +# define X509V3_R_POLICY_LANGUAGE_ALREADY_DEFINED 155 +# define X509V3_R_POLICY_PATH_LENGTH 156 +# define X509V3_R_POLICY_PATH_LENGTH_ALREADY_DEFINED 157 +# define X509V3_R_POLICY_WHEN_PROXY_LANGUAGE_REQUIRES_NO_POLICY 159 +# define X509V3_R_SECTION_NOT_FOUND 150 +# define X509V3_R_UNABLE_TO_GET_ISSUER_DETAILS 122 +# define X509V3_R_UNABLE_TO_GET_ISSUER_KEYID 123 +# define X509V3_R_UNKNOWN_BIT_STRING_ARGUMENT 111 +# define X509V3_R_UNKNOWN_EXTENSION 129 +# define X509V3_R_UNKNOWN_EXTENSION_NAME 130 +# define X509V3_R_UNKNOWN_OPTION 120 +# define X509V3_R_UNSUPPORTED_OPTION 117 +# define X509V3_R_UNSUPPORTED_TYPE 167 +# define X509V3_R_USER_TOO_LONG 132 + +#endif diff --git a/example/android/third_party/crypto/arm/lib/libcrypto.a b/example/android/third_party/crypto/arm/lib/libcrypto.a new file mode 100644 index 0000000000000000000000000000000000000000..e807c4e7c5561d58807f44aabaee5102cc887375 GIT binary patch literal 5341188 zcmeFa516G_c^`Hxgb+x807(eEw&fy_0D)#^ceN`JNV7AuyTi`TjAmxVisF8{GjnI> zdS~X2?!B|SQWb|fPD&c(d1#QI$gb*=RCd(_2U0tam4wFFm5bU(k3&=ZShlGRl=zWr zoXQTa)mBaV`@Qe^^Pcm4cXkCU8Eo|E?A-Hv-}9dLyyySl?}u-1^;XtD_?la;&;J`S zUj1$Vz5e>qvC;iw`^a?fYn*%2AGq)P@=V?R$rs)CJ%2E|OWY&wl2^{C`@v`3C2xDm zU2@Zt>TWsdE@^T{x7{VyZC>dv3GSyS-6g?2ewDlA#k!w=&Rx>v{>4M?lG1(g)9wt=p3UyL_rPQB^4;8{H@M5`{=qlY{lYWu z@?q{*z;_|;OP_U@m+tqz?k=bM!_^nuK1HZ(Or4fgYL>bGr1eP(p?$cBTMee;Qsl*UHRm5?#f@^Q1`!n#a&7Fzu%j? zKi=oAya4ywH{ayGC3o9%?zOwQdp6u_n_Txx?zO?4`?P!Qx9a|jhuv%c+w0wHfBA;t z?*BdKUU!9YH{R@Ccd^{&qwaOPxxf0fd)?o>=`W7^+6(S=FVemK(udpyyPF<$ucsS* zaCf)#h**O%^B54zXy=Ds>`uiwqRaJ_r|5O>uZMqK88=rMPdb??2`T{V94 z!rXrdzS4Cba#scS!1L~^|Kz!5_sM77Ri*pY8F$qeo=WbE@c#z%`R$vM`|6k7Rlkq0 z7|&kyCpSd*hRbhoZ+P9F^Sg% z8&A47y;Sb=2i==4g1h>P&pUB%yv|+y*45qJO$Xi8!`#Hp?rORp-*#7ja_Oaa|MLs( z>MstyefRG++|}M)^QtA6x$nQ;U323T$=&h^cMaVKA9dHvZCse!c-URDoBLZIch_8q zd-NuEO>m!i!Cmumx!---T{F!6={MXp|Nb#|?Q5S;?wT*TYw3RYpu6_QhuyU!cdEPN zId|>UH{G?1A0O`i?x)ckS;Tb=Q6! zv_JUluI|mRdeDixYSO*=t(WF*<0<#%VeYSe&b_(G{q)oB&7bLGX0-P=CzuA{>`;&ro^CU^47?mD{FRd?N|F8|iuQ#ZKletnO- z?#mBH_m)@Q>)vwZxtHGUzuvv&<#Km_)4e6Q`zPI79)2))e-FCQ{oKv&EkFNp_m(dl zbZ_~s!MEhzdgZ(;-S=-j>!Q2=3HSZqiu?bZ`+m4RcOSiQclJ)V=X~yGmfW5TabNhf z+Y{XHe$nlDncNTTea>a>KYP;sz<03w!Pnj3e(>7obN91H-4D`z`U&@g|K{`Gaqfo> z-sCQt`^YEU4}H(MANrkp8{PGp$VHKT^08&$#R9{-cBHPJh!~U%LPN7v1%x z`!64N*V8@rXzqUbQ||f;bzi&EU0=8#zI@(Y4EMIb`KXKTOXu9%g8QRA?rndL?uPcN z+tvN@r`-)tKH_fpt$TC#Mc6D$6w0p-exBu(z9lN=s1NV+$?&Rm(JBB%| z?Z4xE?yo%J-Z9Mm?dRM(hPj_P=-%-i;%)8BMA(fzAE>V9*^-SjfKcdjqF%zf-S z_s)y$e(h=ZPV4@+FS~cbz02Ly>~6Thz3aug*0b(i);;i9_pTqGyfF872JT%=?w7XR zyXb!91^2EmJ({~ee9FD6aPPkSdH3!&edEQuTi4yY?>zU?x{uxK-u;<E8W4 z7j1C!)epHwH~NIT`9j^y=iJSAU*~SF2JYs~N8HV4UT`=6*fZ|tkAHNy`^7!(<|g;_ zyt}!{{p%at&Hv`H^Sbw3db1PvhL5}VSa;*6+mu&wh8hhbG;7=zj9+>VD=? z_n!aepnK0}@b}53@$ z{k)g%4?p4F`^S%}d;WI!-ha1G+%2!V$?fWnJ>hOy2hZSkcgsU3-7Syb+w5@u@s=0s ze&?LKrF4JvxVz=wBA&vHy!vrB^7@l*~!V@LwZ5-E-^Tmb>kBSGir?jqC2V+n#r~eej9D zxbA1?-EB?omwv+C_AR*gz3!yDsBUuL-e=wHH{AP%xgP`V@7|ufpMBiD&pMm~c;CPH z3HQEdE_LsJ-6Jor>+Esw|FJK9=eVan<=)@qe*aWdyARTR=5xvYQ}EDzVI#UbFs8cWvPpNx6*KOR zt1fkSTvNEwhus}?GuOF0?)qZxeq@ikBe);G(%o@B_fubTcLeuOKI-ncQ1_)zxjUNN z@1JvbeDgVX=T-CPceg+0?wp2O|9ahh0y2Jjrtbdb!|u-D{sdt!z>UB4O1G;!_KX{U zDc${FcH_Z4uJwZX&>?{OE^ zEq}sIeC)}(`{bQ&;?a?p)_wj3HxbYZ--rEvfLaW{RD+=t%%po?zrRqjL9;a!Oj9lrN_?%T1!uifcBG|c_R zb?!qigFA*hb?0^Oe8?U9R$Y6KJ9e?$Cm(UgF4XCe~w=9O;dyVMd*+;*5AF{yb@MN=yX&qe++Als=kEHCzdSy* zSgp2KdiQS+9DldB((itI5NOru_WeR92A(m5DKW~3*vj%sCTnGFd35Yx4Jv`0qrI}6 zF)Vkg&Z%|=mHzJbpwr#zi>}>zoI6`*d#&xLW%RV#{p!rY%4!{;L2vI^h_YJV=&qdZ zJ5LLS^WI=P?=8(|V9zOfEcZ#UF?*ybVkMm$^h3tDgv983omNoTwdkp75TRyTMDs2HE89DrRm#}ej#zJX&|h&A3-`<~%~hN2&6UkWs|jcgx|^Mqs)gIK zQRYUwwbgG&e`l*d=Y1bR>8d7>8db#*0%L`XVBiPGHOJ?(&}%G3bog6tr`KS zJbka3vKwF(xh-)jCHRg^%}yr8?OtcgM_ZDjr5Xqo zib!Ycl&0oXd#l}RZFC-JuMRq!?P}1i2Af{tF_Xp)JeiRQL=#HdEX&mFiQ`D$($r*y z%v55nGS`4a{?l!t4 z*0cz1C2a1{ho&Z$suh&@L0j28wAO3qz*hULYoSz#EM-c{dw*2c`#WQ;jGtx5hVP6G zGQZL$mZnk*67qY_qA}AmH0Inq8lR={Lo-v=;Qno&3g04`N_Ap(`tGTP#qk+WLVo$G zBWg&Y?{`jZsac}8S{a;EC=spjs_n@I80%Mx?!?mJgYM-1k=v@-aWoR58-v%OGO(tO zU4`iCMJl?&P9ro#86{Q?kFp_NZLhU@MT4!04wH`5rzU(w6{4CEw6MjlE}4Qcav>#LJ%DOScMoFx;S59#HVSwXMQR* zgG=x6X>DdIt@DE`mikM_2*X{tft` zx6!~Zb)?P>UD)QFk@58q^3L)QDvjR|6)J0MC_YqgYQYYuuEh4v@@FsX&3U)w4>oi7AB=6Q(4&D)WSlw(Z%Rds;YJ;m}Rm1)uv(4uF(A= zaE_hXs{lfbrIvVlY3g{Y+~V8lc25^=JKOtSmD)kye#eng@W+9G2Uc5ymc|-@LU2u_ zq_Y-mqrG)%fKy6Lq?e>nYH>Uq+hgyCm9t>*k7Pit`zC;CjBnPZ8n8BXwon`~ne*Kt z4BYpXq=Cv}-(CLE_KJ^Oe^-4Bk~MO)Fjc$j8yR{$w=^f3r&W7%xs4%n93-LKW1c29 zke}7+P8hiiTBpX?T+Aag&!SAWIk1ieq>bvbc${;DmEOjh4%gH#vk*%3^g;XF!2eV+ zLW?~Fg(4ct6E)E8Rb9+TH@mAl8(uYH{XuJaqn*Lhkh1;PkmA)2!5x~O(`Hl~i}qUQ zT#og3)-p^hR?bJv@U4kN5reF(W+$f(Yqz&E=+0|Pa184x>Bh`j$g<-fE%||Q^#QkvBFvTHoV2Yu%5}=FsfueuaCY$*?ACo;h zwX`@fj{bUTR#p`-0h69i3gR##>r13HunR;cT0Ao5G2}Fckg>@yZfYHA!X14RQwtc~ zPu+C_5lm?f8bZTZZV08OY$i0)gYJ0gYZCK_T z!O-DUx2MI^OOqIb07@FmBk^91#d{>0L&qi$=e!9J?&QtX8q4((Z`C;y^*+72rYus4 zf<9j7C^M+78hBr##$>DbzAkwxdIp&IIXvi`LFeWcr9@Ve@h7*Mtec)aJm>STfk3l0z`=Z2)`G?y6=G;O81%v%N5cq>-JF*Z{leR~C|ZaS zHE2CHHDv=X&6Zgp+iGD^2IG*h&|^tEn`!!FaJEcwTD8peycl|UXU%(Ky;+<;Hf>p? zZAYOnEq(i36#@2DacNF?K}BUY?743X)4hTLAyU66GQ=yrr;1E{r+`SoW@pej)7Ef; zVRX5@fgxcj3c{I_Vn-1cee)AbCk?lF*=en`dxL6iqjk!U!y=&x+uCeoWk1@)j_#(0 z=L#{m8Z{uH6iKW>1Hw%j5HBWgBG)ilGyD|egauGHPMOKPbdq*f_j)7yU zg^8NwQH;;tQ=w*-8IcrlZf<63d^RePx@c2q3>ZWi zD9So~s1Q~z!zSHIN)TJ(QMM=45}D(L7|$JrOpouc2}oE7oRAdIFSDiu_+d-5Tkyvq zvnOU|j0wdXVh~T{^ec;jn62G$5E$dgC`pwJZj=(EiYtIMGnAAdr^M@`Od=z5R5>9r zU>xJvvLC?^?;_SZEyL2Sqcxoq7D6Q?Mz>TJN*Ns|>xnmVmy4f?ucU-1OFW74t3*bG zM`BR#H$e^`tfi8xJ045{BNW5lJ-s-MrQKTa91jw5ltLz_7qH|#w{VY@P=W|vLK=2) zerEihrKyukS{f#&6Ch!dm;4-k6-}POLL-%+)Yv4fXenw0O-6!atu~O>>IAS}CZDZW z35_MX0V?B+r5YtUvHPN(fLLk`gvH0?E6SK5XbG}FC>aUM@><{`4?Pz1NQP-;{&4D| zSj?XYnf!@ZnLj1L+bY5-e-h;LCjyc`(U;{;^STn9?D9=2_}*gJ^i ze(Q|)$-r>E+gafRZv#s~n9vEEjDkAtjn!%!OEv;=Ftbwd$_~x=%#pc;MRoznD=YKH z%bhCYl{V7&5#5NT=d##@7-lGvF;lV4RVmbotMD#L<{2_goV<%zg*k%FTqa^7{S<~Y z%}QQsgb@IadLRIP+`5lHxHYK|e<}8ni1C-l65~e_=gb^V3P8$7!Vifqty3g#w8!G~ z5zTp_(U%PAeVb}I`XD>}Ft`DN< z-;e5r%GTPb(vBjTXP4c574>19b3+v)XqSmj5%%LYB$>Yzr+E~-R| zN}C52*(MN{EHQ^mLUy*S4yx&D+gE<7y^1=GxufdC6;JFfD4S@7sMTkJXdG)hTi9$! zi}=(}*AzK>r0>JyINrkfl3#WY$&I)l24%dAwS}`ZYpXReqR@J4>lBxho5^dnIGC4( zI&2KYu;A9j1v6J@2ib&icd73V9jI5)E8iJ{b4r8mN_Qhi`{)`N zO*5)1{FxXAkUfTgYOcocRiU5O?8v;{?GI!H$1gia!e;AqI|*TKMbdU_uB02CPmH2whAJS{7L10ykrZ>J$o20`GtsM{}hcv>Cf1eC4^eXs*f?TtZ%GI`2LphoUy+% zN^51MjgypK9BX112R&C)Fp!{3~-zjtu8ym~q zD~ipe$d@vxwL5U2jmLI0slKh0ML6+eFA^QnxdGayK~D{sm=UVl#%3N*u?aVUvlhXq z3PZA1N7E)7^06An=7T@j>WU0$W`pJwY5^RN)R;2R7kgr|R2D*^FlK(~L;_LLgCmf0 zF;+#;G4{KvE(P}2cLuB7vk_3knTo-ksc4Z+7Qodi7C-NgNe&H6Y$vyNu*BMfanT!7 zGw2n3VL0w9T9|N&6$W`hq^#wrbQl`dl5{j z&!Ldbf`+ms9vZn`yA!EMmau2ZB2r5h!8}<+@?;UolNFFmTY^QhB1A@M+IWUQW|7Nc z7W9bd&9nf>Z)OW~*z_8G>tTLMHcIdwPg?oLGE!99sU4h#*&5(n5Jd`6$6~dBjE=f( zw>sJ`Z^gvQuMv@eQ~6kpH$N5b%Sl~EF)=qg%k!MEfCnJtBI7|CV-JDxzVWS|!r>i^ z5wx-*6oszi=s^`MhkylZHh8o~hXuXlGal}?R3!_3g5CXC z0Sp1{BfqqTCNmDwvkUW5;Pbz5<|lb#uf-6if9z)-cC<~pOZS~?KQn%8YHYbW+}UcO zOYy#GE;t0wiEWq#$q-sU^pLJY_r0#{A%o6I_7T~NZagk$S}(hH~;i^FbvF0#PWr(y}NB3FPj0OisZxd;pO7q|r^pAU${x{MSU~g7($jLrE1m^i<>aLpc zwgZa=6G;TQSNn~|t&d?4nryV2r!X{P8;G&OSoE*1)1=ktts~W`CUCn6yw89%Vl@U4 zHh2VW@Cep}<5o>QIIr5&A_AC#udS`Fw?%AX6pZ<20t?2RD>3F-i7~f;!J=p>%*Y?D zOBndiqUHz(4xAM~V~gZT;#aHloG-;@6t)Z&?6p^{WK867IKT!Qi!q#9i6NyvV~vVK zX*OF>D?o@)T2kL6>hE4}Y`e?F2v0-+`y1n0bj^t-mMHu+9&ZwDw$&~*9w?Q`g}=sw zHaFvuCc4n4-`&6wiu=&cXRHaXNkx;Kv!aPEQwbiag)s>PGAnIM1-`BJDcBENAL{MuumA)Y&SNI57#^IG8lPq!Nv2SV%bSl4XN3Ik zXs*1G&JSZlHtp<9!k;lw^jK}_Pl=>2C>l(KOA{RKoa%12xsHHgj<@QW*2Yddr0{Hq zGc;X(n3bZ%Zl2{8OH+tI<;qBi2qRHX7wgw6SnJV(irU=TS{-9RnVI76S;ZJL8yMf9 z_@AhZykF^Q8~ydpn&b(|>Ko08qfH@l%(ny-;j6Fw)G?m_nL=!B0iwR7a7>4@x>#uT z=?Ux!gsR8qX8i&;4~$zNl*7oO({mHV7mdh(9Lppy2RXFC2kanNuE6BEM~&FF85GJ# z#zKA1n4`W#7!XmRc5ZHW#Ms-NP^i(S(lU^e4B&x{?pe$WcwZ>65J6ek>GfHYJX_8p z1*yi90T_GR@&?UNk4LI4EYyXOnGTCe7=sCFUxP7EXn^+~Y~3R)Z`u~5U` zf&GvT)IRs6w#c+Yz1TqMFDWc+WKi1iC$A+0Qt_uR6?8-=O$&z(2jU9`xP6w=T=SX(-ntgTG@zJ3)mh+rx%G+*uX=244@l+ zg^VeK)@0b-!zj3T;v&Q2uA)WLV;2b>^z1Vk5w{ze5e%D_5eQqC5n#8({mKXwPhw1r zFD@0krUGTr36I$>6^5K zM~3!k9orN<3J?h_(&EvDHu))QXY$K0oXKx2rQ#_JFfUf)ILO~w#>AsvEn-T~a}Nc4 zw_FnqgbA!Nb=Ep7SSy&2lRX&gYmf;{V7N`)UY*1eXF}0IYz?MyZ!l|n_9z&OXyYljl{Vg zZ!F@0kzTw?6w)y_Ko(GPFkv9fxB7jydW`}BwmaK{1zr0NK|E@_nuur@#8V?$qsF+v zIL%FBWD*L9x64BN40A_