This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from cryptography.hazmat.primitives.asymmetric import ec | |
| from cryptography.hazmat.primitives.kdf.hkdf import HKDF | |
| from cryptography.hazmat.primitives import hashes | |
| from cryptography.hazmat.primitives.serialization import Encoding, PublicFormat | |
| # ---------------------------- | |
| # 1. 客户端生成 ECDH ephemeral key | |
| # ---------------------------- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import numpy as np | |
| M = None | |
| def xorshift32(x): | |
| x ^= (x << 13) & 0xFFFFFFFF | |
| x ^= x >> 17 | |
| x ^= (x << 5) & 0xFFFFFFFF | |
| return x |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def invmod(n, p): | |
| if n >=p : | |
| q, r = divmod(n, p) | |
| if r == 0: | |
| raise ValueError("xxx") | |
| else: | |
| n = r | |
| if n == 1: return 1 | |
| q, r = divmod(p, n) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def extend_gcd(a, b): | |
| if a < b: | |
| a, b = b, a | |
| r = np.array([[1, 0], [0, 1]]) | |
| while 1: | |
| quotient, remainder = divmod(a, b) | |
| # r = a - quotient * b | |
| # np.array([a,b]) @ np.array([0, 1]) = b; 相当于a = b | |
| # |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def base64_unit(input_bytes): | |
| base64_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" | |
| padding = {1: 2, 2: 1, 0: 0}[len(input_bytes) % 3] | |
| while len(input_bytes) < 3: | |
| input_bytes += b'\x00' | |
| num = int.from_bytes(input_bytes, byteorder='big') | |
| o = [] | |
| count = 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import socket | |
| import select | |
| import os | |
| import fcntl | |
| import threading | |
| buffer_size = 1024 | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import socket | |
| import os | |
| import array | |
| def send_fd(sock, fd): | |
| """Send a file descriptor to another process via a Unix domain socket.""" | |
| fds = array.array("i", [fd]) | |
| sock.sendmsg([b"x"], [(socket.SOL_SOCKET, socket.SCM_RIGHTS, fds)]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import socket | |
| import select | |
| def test(): | |
| c = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
| c.connect(("", 8877)) | |
| c.send(b"hello world") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ### SIDH | |
| ### https://nbviewer.org/url/defeo.lu/docet/assets/jupyter/2018-03-22-PostScryptum.ipynb | |
| p = 431 | |
| F.<i> = GF(p^2, modulus=x^2+1) | |
| E = EllipticCurve(F, [1, 0]) | |
| E.order() == (p+1)^2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import random | |
| import hashlib | |
| p = 2**256-2**32-977 | |
| g = 2 | |
| sk = random.randint(1, p-1) | |
| pk = pow(g, sk, p) | |
NewerOlder