|
15 | 15 | import re |
16 | 16 |
|
17 | 17 | # Import simulation functions from plugins |
18 | | -from plugins.authentication.auth import generate_quantum_fingerprint_cirq, verify_fingerprint_cirq |
| 18 | +from plugins.authentication.auth import generate_quantum_fingerprint_cirq |
19 | 19 | from plugins.encryption_bb84.bb84 import bb84_protocol_cirq |
20 | 20 | from plugins.error_correction.shor_code import run_shor_code |
21 | 21 | from plugins.grover.grover import run_grover |
@@ -380,20 +380,19 @@ def validate_parameters(plugin, params): |
380 | 380 | # --- Plugin Registry --- |
381 | 381 | # Define all available quantum simulation plugins |
382 | 382 | PLUGINS = { |
383 | | - "auth": { |
384 | | - "name": "Quantum Authentication", |
385 | | - "description": "Simulate quantum fingerprint authentication using Cirq.", |
386 | | - "icon": "fa-fingerprint", |
387 | | - "category": "security", |
388 | | - "parameters": [ |
389 | | - {"name": "data", "type": "str", "default": "example_user", "description": "Data to authenticate", |
390 | | - "max_length": 64}, |
391 | | - {"name": "num_qubits", "type": "int", "default": 8, "description": "Number of qubits to use", |
392 | | - "min": 1, "max": 10} |
| 383 | + 'auth': { |
| 384 | + 'name': 'Post-Quantum Authentication', |
| 385 | + 'description': 'Simulate a lattice-based authentication system that remains secure against quantum computer attacks, based on the Ring-LWE problem.', |
| 386 | + 'icon': 'fa-lock', |
| 387 | + 'category': 'security', |
| 388 | + 'parameters': [ |
| 389 | + {'name': 'username', 'type': 'str', 'default': 'Bob', 'description': 'Username for authentication'}, |
| 390 | + {'name': 'noise', 'type': 'float', 'default': 0.0, 'description': 'Noise level (0.0 - 0.2)',"min": 0, "max": 0.2}, |
| 391 | + {'name': 'dimension', 'type': 'int', 'default': 4, 'description': 'Lattice dimension parameter',"min": 1, "max": 32} |
393 | 392 | ], |
394 | | - "run": lambda p: run_plugin(generate_quantum_fingerprint_cirq, _plugin_key="auth", data=p["data"], num_qubits=p["num_qubits"]) |
| 393 | + 'function': generate_quantum_fingerprint_cirq |
395 | 394 | }, |
396 | | - |
| 395 | + |
397 | 396 | "bb84": { |
398 | 397 | "name": "BB84 Protocol Simulation", |
399 | 398 | "description": "Simulate the BB84 quantum key distribution protocol with realistic physical effects.", |
@@ -947,38 +946,89 @@ def plugin_view(plugin_key): |
947 | 946 | educational_content = get_educational_content(plugin_key) |
948 | 947 |
|
949 | 948 | # Process form submission... |
950 | | - if request.method == "POST": |
951 | | - # Extract parameters from the form |
952 | | - raw_params = {} |
953 | | - for param in plugin["parameters"]: |
954 | | - raw_params[param["name"]] = request.form.get(param["name"], param.get("default", "")) |
955 | | - |
| 949 | + result = None |
| 950 | + if request.method == 'POST': |
956 | 951 | try: |
957 | | - # Validate parameters |
958 | | - params = validate_parameters(plugin, raw_params) |
| 952 | + # Get plugin function |
| 953 | + plugin_function = plugin.get('function') |
| 954 | + if not plugin_function: |
| 955 | + raise ValueError(f"Plugin function not found for {plugin_key}") |
959 | 956 |
|
960 | | - # Add plugin key for better error reporting |
961 | | - params['_plugin_key'] = plugin_key |
962 | | - |
963 | | - # Execute the plugin with the validated parameters |
964 | | - result = plugin["run"](params) |
| 957 | + # Special handling for auth plugin |
| 958 | + if plugin_key == 'auth': |
| 959 | + username = request.form.get('username', 'user123') |
| 960 | + noise = float(request.form.get('noise', 0.0)) |
| 961 | + dimension = int(request.form.get('dimension', 4)) |
| 962 | + |
| 963 | + # Call the function with updated parameters for the lattice-based implementation |
| 964 | + output = plugin_function(username, dimension) |
| 965 | + |
| 966 | + # Process log for display |
| 967 | + log = output.get('log', '') |
| 968 | + |
| 969 | + result = { |
| 970 | + 'output': output, |
| 971 | + 'log': log |
| 972 | + } |
| 973 | + else: |
| 974 | + # Process parameters from form |
| 975 | + params = {} |
| 976 | + for param in plugin.get('parameters', []): |
| 977 | + param_name = param['name'] |
| 978 | + param_type = param['type'] |
| 979 | + |
| 980 | + # Get form value |
| 981 | + form_value = request.form.get(param_name) |
| 982 | + if form_value is None: |
| 983 | + continue |
| 984 | + |
| 985 | + # Convert value to appropriate type |
| 986 | + if param_type == 'int': |
| 987 | + params[param_name] = int(form_value) |
| 988 | + elif param_type == 'float': |
| 989 | + params[param_name] = float(form_value) |
| 990 | + else: |
| 991 | + params[param_name] = form_value |
| 992 | + |
| 993 | + # Call the plugin function with parameters |
| 994 | + output = plugin_function(**params) |
| 995 | + |
| 996 | + # Process log for display |
| 997 | + if isinstance(output, dict) and 'log' in output: |
| 998 | + log = output.get('log', '') |
| 999 | + else: |
| 1000 | + log = f"Execution complete. No detailed log available." |
| 1001 | + |
| 1002 | + result = { |
| 1003 | + 'output': output, |
| 1004 | + 'log': log |
| 1005 | + } |
965 | 1006 |
|
966 | | - # If this is an AJAX request, return JSON |
967 | | - if request.headers.get("X-Requested-With") == "XMLHttpRequest": |
| 1007 | + # Return JSON response for AJAX requests |
| 1008 | + if request.headers.get('X-Requested-With') == 'XMLHttpRequest': |
968 | 1009 | return jsonify(result) |
969 | 1010 |
|
970 | | - except SimulationError as e: |
971 | | - # Handle validation errors |
972 | | - error_msg = str(e) |
973 | | - if hasattr(e, 'suggestion') and e.suggestion: |
974 | | - error_msg += f"\n\nSuggestion: {e.suggestion}" |
975 | | - |
976 | | - result = {"output": None, "log": None, "error": error_msg} |
| 1011 | + except Exception as e: |
| 1012 | + error_message = str(e) |
| 1013 | + stack_trace = traceback.format_exc() |
| 1014 | + |
| 1015 | + # Add suggestion for common errors |
| 1016 | + suggestion = "" |
| 1017 | + if "target" in error_message.lower() and "bits" in error_message.lower(): |
| 1018 | + suggestion = "Suggestion: Make sure your target state binary string length matches the number of qubits." |
| 1019 | + |
| 1020 | + error_details = f"{error_message}\n\n{stack_trace}\n\n{suggestion}" |
| 1021 | + |
| 1022 | + result = { |
| 1023 | + 'error': error_details |
| 1024 | + } |
977 | 1025 |
|
978 | | - if request.headers.get("X-Requested-With") == "XMLHttpRequest": |
| 1026 | + # Return JSON response for AJAX requests |
| 1027 | + if request.headers.get('X-Requested-With') == 'XMLHttpRequest': |
979 | 1028 | return jsonify(result) |
980 | 1029 |
|
981 | | - return render_template("plugin.html", plugin=plugin, result=result, educational_content=educational_content, mini_explanation=mini_explanation) |
| 1030 | + # Render template |
| 1031 | + return render_template('plugin.html', plugin=plugin, result=result, educational_content=educational_content, mini_explanation=mini_explanation) |
982 | 1032 |
|
983 | 1033 | @app.route("/api/plugins", methods=["GET"]) |
984 | 1034 | def api_plugins(): |
|
0 commit comments