You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

user_cryption.cpp 4.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /**
  2. * \file example/cpp_example/user_cryption.cpp
  3. * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  4. *
  5. * Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
  6. *
  7. * Unless required by applicable law or agreed to in writing,
  8. * software distributed under the License is distributed on an
  9. * "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. */
  11. #include "example.h"
  12. #if LITE_BUILD_WITH_MGE
  13. using namespace lite;
  14. using namespace example;
  15. namespace {
  16. std::vector<uint8_t> decrypt_model(
  17. const void* model_mem, size_t size, const std::vector<uint8_t>& key) {
  18. if (key.size() == 1) {
  19. std::vector<uint8_t> ret(size, 0);
  20. const uint8_t* ptr = static_cast<const uint8_t*>(model_mem);
  21. uint8_t key_data = key[0];
  22. for (size_t i = 0; i < size; i++) {
  23. ret[i] = ptr[i] ^ key_data ^ key_data;
  24. }
  25. return ret;
  26. } else {
  27. printf("the user define decrypt method key length is wrong.\n");
  28. return {};
  29. }
  30. }
  31. bool register_cryption_method(const Args& args) {
  32. std::string network_path = args.model_path;
  33. std::string input_path = args.input_path;
  34. //! register the decryption method
  35. register_decryption_and_key("just_for_test", decrypt_model, {15});
  36. lite::Config config;
  37. config.bare_model_cryption_name = "just_for_test";
  38. //! create and load the network
  39. std::shared_ptr<Network> network = std::make_shared<Network>(config);
  40. network->load_model(network_path);
  41. //! set input data to input tensor
  42. std::shared_ptr<Tensor> input_tensor = network->get_input_tensor(0);
  43. auto layout = input_tensor->get_layout();
  44. auto src_tensor = parse_npy(input_path);
  45. void* src = src_tensor->get_memory_ptr();
  46. input_tensor->reset(src, layout);
  47. //! forward
  48. network->forward();
  49. network->wait();
  50. //! get the output data or read tensor set in network_in
  51. std::shared_ptr<Tensor> output_tensor = network->get_output_tensor(0);
  52. void* out_data = output_tensor->get_memory_ptr();
  53. size_t out_length = output_tensor->get_tensor_total_size_in_byte() /
  54. output_tensor->get_layout().get_elem_size();
  55. float max = -1.0f;
  56. float sum = 0.0f;
  57. for (size_t i = 0; i < out_length; i++) {
  58. float data = static_cast<float*>(out_data)[i];
  59. sum += data;
  60. if (max < data)
  61. max = data;
  62. }
  63. printf("max=%e, sum=%e\n", max, sum);
  64. return true;
  65. }
  66. bool update_cryption_key(const Args& args) {
  67. std::string network_path = args.model_path;
  68. std::string input_path = args.input_path;
  69. //! update the decryption method key
  70. std::vector<uint8_t> key(32, 0);
  71. for (size_t i = 0; i < 32; i++) {
  72. key[i] = 31 - i;
  73. }
  74. update_decryption_or_key("AES_default", nullptr, key);
  75. lite::Config config;
  76. config.bare_model_cryption_name = "AES_default";
  77. //! create and load the network
  78. std::shared_ptr<Network> network = std::make_shared<Network>(config);
  79. network->load_model(network_path);
  80. //! set input data to input tensor
  81. std::shared_ptr<Tensor> input_tensor = network->get_input_tensor(0);
  82. auto layout = input_tensor->get_layout();
  83. auto src_tensor = parse_npy(input_path);
  84. void* src = src_tensor->get_memory_ptr();
  85. input_tensor->reset(src, layout);
  86. //! forward
  87. network->forward();
  88. network->wait();
  89. //! get the output data or read tensor set in network_in
  90. std::shared_ptr<Tensor> output_tensor = network->get_output_tensor(0);
  91. void* out_data = output_tensor->get_memory_ptr();
  92. size_t out_length = output_tensor->get_tensor_total_size_in_byte() /
  93. output_tensor->get_layout().get_elem_size();
  94. float max = -1.0f;
  95. float sum = 0.0f;
  96. for (size_t i = 0; i < out_length; i++) {
  97. float data = static_cast<float*>(out_data)[i];
  98. sum += data;
  99. if (max < data)
  100. max = data;
  101. }
  102. printf("max=%e, sum=%e\n", max, sum);
  103. return true;
  104. }
  105. } // namespace
  106. REGIST_EXAMPLE("register_cryption_method", register_cryption_method);
  107. REGIST_EXAMPLE("update_cryption_key", update_cryption_key);
  108. #endif
  109. // vim: syntax=cpp.doxygen foldmethod=marker foldmarker=f{{{,f}}}