This file contains 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 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): | |
a_coeff = [1, 0] | |
b_coeff = [0, 1] | |
while 1: | |
quotient, remainder = divmod(a, b) | |
# r = a - quotient * b | |
# np.array([a,b]) @ np.array([0, 1]) = b; 相当于a = b | |
# | |
# np.array([a,b]) @ np.array([1, -quotient]) = r; 相当于 b = r |
This file contains 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 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 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 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 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 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) |
This file contains 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 sage.stats.distributions.discrete_gaussian_lattice import DiscreteGaussianDistributionLatticeSampler | |
def test_lwe(n, p, sigma, size=100): | |
F = GF(p) | |
a = random_vector(F, n) | |
HALF_P = int(p/2) | |
s = random_vector(F,n) | |
p41 = int(p/4) | |
p43 = int(p*3/4) | |
D = DiscreteGaussianDistributionLatticeSampler(F^1, sigma) |
This file contains 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 | |
P = next_prime(2^256) | |
N = 64 | |
k = 16 | |
BOUND = 32 | |
HALF_P = int(P/2+0.5) | |
R.<x> = PolynomialRing(GF(P), 'x') |
NewerOlder