Python-ELMO is a Python library which offers an encapsulation of the binary tool ELMO, in order to manipulate it easily in Python and SageMath script.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import os, shutil
  2. from subprocess import check_call
  3. ### Install ELMO
  4. from elmo.config import ELMO_TOOL_REPOSITORY
  5. ELMO_SOURCE = 'https://github.com/sca-research/ELMO.git'
  6. elmo_complete_path = os.path.join('elmo', ELMO_TOOL_REPOSITORY)
  7. if not os.path.isdir(elmo_complete_path):
  8. from setup import install_elmo_tool
  9. install_elmo_tool(elmo_complete_path)
  10. def print_success(text):
  11. OKGREEN = '\033[92m'
  12. ENDC = '\033[0m'
  13. print(OKGREEN+text+ENDC)
  14. #########################################################
  15. # TEST 1 : MANAGE A FRESH SIMULATION #
  16. #########################################################
  17. foldername = 'test-project'
  18. classname = 'TestSimu'
  19. ### Create Simulation
  20. from elmo.manage import create_simulation
  21. shutil.rmtree('test-project', ignore_errors=True) # Clean before
  22. create_simulation(foldername, classname)
  23. assert os.path.isdir(foldername), 'Folder not created'
  24. ### List Available Simulations
  25. from elmo.manage import search_simulations
  26. simulations = search_simulations(foldername)
  27. assert classname in simulations, 'Test Simulation not found'
  28. ### Use a Simulation
  29. from elmo.manage import get_simulation
  30. simu = get_simulation(classname, foldername)
  31. shutil.rmtree(foldername)
  32. print_success(' - Test 1 "Manage A Fresh Simulation": Success!')
  33. #########################################################
  34. # TEST 2 : USE ELMO ENGINE #
  35. #########################################################
  36. ### Use the ELMO Engine
  37. from elmo.engine import ELMOEngine, Instr
  38. engine = ELMOEngine()
  39. for i in range(0, 256):
  40. engine.add_point(
  41. (Instr.LDR, Instr.MUL, Instr.OTHER), # Types of the previous, current and next instructions
  42. (0x0000, i), # Operands of the previous instructions
  43. (0x2BAC, i) # Operands of the current instructions
  44. )
  45. engine.run() # Compute the power consumption of all these points
  46. power = engine.power # Numpy 1D array with an entry for each previous point
  47. engine.reset_points() # Reset the engine to study other points
  48. assert power.shape == (256, )
  49. print_success(' - Test 2 "Use ELMO Engine": Success!')
  50. #########################################################
  51. # TEST 3 : USE A REAL SIMULATION #
  52. #########################################################
  53. from elmo import get_simulation
  54. KyberNTTSimulation = get_simulation('KyberNTTSimulation')
  55. simulation = KyberNTTSimulation()
  56. simulation.set_challenges(simulation.get_random_challenges(10))
  57. res = simulation.run()
  58. assert not res['error']
  59. assert res['nb_traces'] == 10
  60. assert res['nb_instructions']
  61. # Test the methods for analysis
  62. multiplication_indexes = simulation.get_indexes_of(lambda instr: 'mul' in instr)
  63. asmtrace = simulation.get_asmtrace()
  64. for index, instr in enumerate(asmtrace):
  65. assert ('mul' in instr) == (index in multiplication_indexes)
  66. traces = simulation.get_traces()
  67. traces = simulation.get_traces(multiplication_indexes)
  68. assert traces.shape == (10, len(multiplication_indexes))
  69. printed_data = simulation.get_printed_data()
  70. print_success(' - Test 3 "Use A Real Simulation": Success!')
  71. #########################################################
  72. # TEST 4 : USE ELMO BY RUNNING ONLINE #
  73. #########################################################
  74. def test_online_server():
  75. from elmo import get_simulation
  76. KyberNTTSimulation = get_simulation('KyberNTTSimulation')
  77. simulation = KyberNTTSimulation()
  78. simulation.set_challenges(simulation.get_random_challenges(10))
  79. return simulation.run_online()
  80. from elmo.executor import launch_executor
  81. # Launch the server and realize the test
  82. res = launch_executor(waiting_function=test_online_server)
  83. assert not res['error']
  84. assert res['nb_traces'] == 10
  85. assert res['nb_instructions']
  86. print_success(' - Test 4 "Use ELMO By Running Online": Success!')
  87. #########################################################
  88. print_success('All seems fine!')