mirror of
https://github.com/aljazceru/nutshell.git
synced 2025-12-21 11:04:19 +01:00
initial commit
This commit is contained in:
0
core/__init__.py
Normal file
0
core/__init__.py
Normal file
91
core/b_dhke.py
Normal file
91
core/b_dhke.py
Normal file
@@ -0,0 +1,91 @@
|
||||
"""
|
||||
Implementation of https://gist.github.com/RubenSomsen/be7a4760dd4596d06963d67baf140406
|
||||
|
||||
Alice:
|
||||
A = a*G
|
||||
return A
|
||||
|
||||
Bob:
|
||||
Y = hash_to_curve(secret_message)
|
||||
r = random blinding factor
|
||||
B'= Y + r*G
|
||||
return B'
|
||||
|
||||
Alice:
|
||||
C' = a*B'
|
||||
(= a*Y + a*r*G)
|
||||
return C'
|
||||
|
||||
Bob:
|
||||
C = C' - r*A
|
||||
(= C' - a*r*G)
|
||||
(= a*Y)
|
||||
return C, secret_message
|
||||
|
||||
Alice:
|
||||
Y = hash_to_curve(secret_message)
|
||||
C == a*Y
|
||||
|
||||
If true, C must have originated from Alice
|
||||
"""
|
||||
|
||||
import hashlib
|
||||
from ecc.curve import secp256k1, Point
|
||||
from ecc.key import gen_keypair
|
||||
|
||||
|
||||
G = secp256k1.G
|
||||
|
||||
|
||||
def hash_to_curve(secret_msg):
|
||||
"""Generates x coordinate from the message hash and checks if the point lies on the curve.
|
||||
If it does not, it tries computing again a new x coordinate from the hash of the coordinate."""
|
||||
point = None
|
||||
msg = secret_msg
|
||||
while point is None:
|
||||
x_coord = int(hashlib.sha256(msg).hexdigest().encode("utf-8"), 16)
|
||||
y_coord = secp256k1.compute_y(x_coord)
|
||||
try:
|
||||
# Fails if the point is not on the curve
|
||||
point = Point(x_coord, y_coord, secp256k1)
|
||||
except:
|
||||
msg = str(x_coord).encode("utf-8")
|
||||
|
||||
return point
|
||||
|
||||
|
||||
def step1_bob(secret_msg):
|
||||
secret_msg = secret_msg.encode("utf-8")
|
||||
Y = hash_to_curve(secret_msg)
|
||||
r, _ = gen_keypair(secp256k1)
|
||||
B_ = Y + r * G
|
||||
return B_, r
|
||||
|
||||
|
||||
def step2_alice(B_, a):
|
||||
C_ = a * B_
|
||||
return C_
|
||||
|
||||
|
||||
def step3_bob(C_, r, A):
|
||||
C = C_ - r * A
|
||||
return C
|
||||
|
||||
|
||||
def verify(a, C, secret_msg):
|
||||
Y = hash_to_curve(secret_msg.encode("utf-8"))
|
||||
return C == a * Y
|
||||
|
||||
|
||||
### Below is a test of a simple positive and negative case
|
||||
|
||||
# # Alice private key
|
||||
# a, A = gen_keypair(secp256k1)
|
||||
# secret_msg = "test"
|
||||
# B_, r = step1_bob(secret_msg)
|
||||
# C_ = step2_alice(B_, a)
|
||||
# C = step3_bob(C_, r, A)
|
||||
# print("C:{}, secret_msg:{}".format(C, secret_msg))
|
||||
|
||||
# assert verify(a, C, secret_msg)
|
||||
# assert verify(a, C + 1*G, secret_msg) == False # adding 1*G shouldn't pass
|
||||
192
core/db.py
Normal file
192
core/db.py
Normal file
@@ -0,0 +1,192 @@
|
||||
import asyncio
|
||||
import datetime
|
||||
import os
|
||||
import time
|
||||
from contextlib import asynccontextmanager
|
||||
from typing import Optional
|
||||
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy_aio.base import AsyncConnection
|
||||
from sqlalchemy_aio.strategy import ASYNCIO_STRATEGY # type: ignore
|
||||
|
||||
|
||||
POSTGRES = "POSTGRES"
|
||||
COCKROACH = "COCKROACH"
|
||||
SQLITE = "SQLITE"
|
||||
|
||||
|
||||
class Compat:
|
||||
type: Optional[str] = "<inherited>"
|
||||
schema: Optional[str] = "<inherited>"
|
||||
|
||||
def interval_seconds(self, seconds: int) -> str:
|
||||
if self.type in {POSTGRES, COCKROACH}:
|
||||
return f"interval '{seconds} seconds'"
|
||||
elif self.type == SQLITE:
|
||||
return f"{seconds}"
|
||||
return "<nothing>"
|
||||
|
||||
@property
|
||||
def timestamp_now(self) -> str:
|
||||
if self.type in {POSTGRES, COCKROACH}:
|
||||
return "now()"
|
||||
elif self.type == SQLITE:
|
||||
return "(strftime('%s', 'now'))"
|
||||
return "<nothing>"
|
||||
|
||||
@property
|
||||
def serial_primary_key(self) -> str:
|
||||
if self.type in {POSTGRES, COCKROACH}:
|
||||
return "SERIAL PRIMARY KEY"
|
||||
elif self.type == SQLITE:
|
||||
return "INTEGER PRIMARY KEY AUTOINCREMENT"
|
||||
return "<nothing>"
|
||||
|
||||
@property
|
||||
def references_schema(self) -> str:
|
||||
if self.type in {POSTGRES, COCKROACH}:
|
||||
return f"{self.schema}."
|
||||
elif self.type == SQLITE:
|
||||
return ""
|
||||
return "<nothing>"
|
||||
|
||||
|
||||
class Connection(Compat):
|
||||
def __init__(self, conn: AsyncConnection, txn, typ, name, schema):
|
||||
self.conn = conn
|
||||
self.txn = txn
|
||||
self.type = typ
|
||||
self.name = name
|
||||
self.schema = schema
|
||||
|
||||
def rewrite_query(self, query) -> str:
|
||||
if self.type in {POSTGRES, COCKROACH}:
|
||||
query = query.replace("%", "%%")
|
||||
query = query.replace("?", "%s")
|
||||
return query
|
||||
|
||||
async def fetchall(self, query: str, values: tuple = ()) -> list:
|
||||
result = await self.conn.execute(self.rewrite_query(query), values)
|
||||
return await result.fetchall()
|
||||
|
||||
async def fetchone(self, query: str, values: tuple = ()):
|
||||
result = await self.conn.execute(self.rewrite_query(query), values)
|
||||
row = await result.fetchone()
|
||||
await result.close()
|
||||
return row
|
||||
|
||||
async def execute(self, query: str, values: tuple = ()):
|
||||
return await self.conn.execute(self.rewrite_query(query), values)
|
||||
|
||||
|
||||
class Database(Compat):
|
||||
def __init__(self, db_name: str, db_location: str):
|
||||
self.name = db_name
|
||||
self.db_location = db_location
|
||||
self.db_location_is_url = "://" in self.db_location
|
||||
|
||||
if self.db_location_is_url:
|
||||
database_uri = self.db_location
|
||||
|
||||
if database_uri.startswith("cockroachdb://"):
|
||||
self.type = COCKROACH
|
||||
else:
|
||||
self.type = POSTGRES
|
||||
|
||||
import psycopg2 # type: ignore
|
||||
|
||||
def _parse_timestamp(value, _):
|
||||
f = "%Y-%m-%d %H:%M:%S.%f"
|
||||
if not "." in value:
|
||||
f = "%Y-%m-%d %H:%M:%S"
|
||||
return time.mktime(datetime.datetime.strptime(value, f).timetuple())
|
||||
|
||||
psycopg2.extensions.register_type(
|
||||
psycopg2.extensions.new_type(
|
||||
psycopg2.extensions.DECIMAL.values,
|
||||
"DEC2FLOAT",
|
||||
lambda value, curs: float(value) if value is not None else None,
|
||||
)
|
||||
)
|
||||
psycopg2.extensions.register_type(
|
||||
psycopg2.extensions.new_type(
|
||||
(1082, 1083, 1266),
|
||||
"DATE2INT",
|
||||
lambda value, curs: time.mktime(value.timetuple())
|
||||
if value is not None
|
||||
else None,
|
||||
)
|
||||
)
|
||||
|
||||
psycopg2.extensions.register_type(
|
||||
psycopg2.extensions.new_type(
|
||||
(1184, 1114),
|
||||
"TIMESTAMP2INT",
|
||||
_parse_timestamp
|
||||
# lambda value, curs: time.mktime(
|
||||
# datetime.datetime.strptime(
|
||||
# value, "%Y-%m-%d %H:%M:%S.%f"
|
||||
# ).timetuple()
|
||||
# ),
|
||||
)
|
||||
)
|
||||
else:
|
||||
if os.path.isdir(self.db_location):
|
||||
self.path = os.path.join(self.db_location, f"{self.name}.sqlite3")
|
||||
database_uri = f"sqlite:///{self.path}"
|
||||
self.type = SQLITE
|
||||
else:
|
||||
raise NotADirectoryError(
|
||||
f"db_location named {self.db_location} was not created"
|
||||
f" - please 'mkdir {self.db_location}' and try again"
|
||||
)
|
||||
self.schema = self.name
|
||||
if self.name.startswith("ext_"):
|
||||
self.schema = self.name[4:]
|
||||
else:
|
||||
self.schema = None
|
||||
|
||||
self.engine = create_engine(database_uri, strategy=ASYNCIO_STRATEGY)
|
||||
self.lock = asyncio.Lock()
|
||||
|
||||
@asynccontextmanager
|
||||
async def connect(self):
|
||||
await self.lock.acquire()
|
||||
try:
|
||||
async with self.engine.connect() as conn:
|
||||
async with conn.begin() as txn:
|
||||
wconn = Connection(conn, txn, self.type, self.name, self.schema)
|
||||
|
||||
if self.schema:
|
||||
if self.type in {POSTGRES, COCKROACH}:
|
||||
await wconn.execute(
|
||||
f"CREATE SCHEMA IF NOT EXISTS {self.schema}"
|
||||
)
|
||||
elif self.type == SQLITE:
|
||||
await wconn.execute(
|
||||
f"ATTACH '{self.path}' AS {self.schema}"
|
||||
)
|
||||
|
||||
yield wconn
|
||||
finally:
|
||||
self.lock.release()
|
||||
|
||||
async def fetchall(self, query: str, values: tuple = ()) -> list:
|
||||
async with self.connect() as conn:
|
||||
result = await conn.execute(query, values)
|
||||
return await result.fetchall()
|
||||
|
||||
async def fetchone(self, query: str, values: tuple = ()):
|
||||
async with self.connect() as conn:
|
||||
result = await conn.execute(query, values)
|
||||
row = await result.fetchone()
|
||||
await result.close()
|
||||
return row
|
||||
|
||||
async def execute(self, query: str, values: tuple = ()):
|
||||
async with self.connect() as conn:
|
||||
return await conn.execute(query, values)
|
||||
|
||||
@asynccontextmanager
|
||||
async def reuse_conn(self, conn: Connection):
|
||||
yield conn
|
||||
26
core/helpers.py
Normal file
26
core/helpers.py
Normal file
@@ -0,0 +1,26 @@
|
||||
import asyncio
|
||||
from functools import partial, wraps
|
||||
|
||||
|
||||
def async_wrap(func):
|
||||
@wraps(func)
|
||||
async def run(*args, loop=None, executor=None, **kwargs):
|
||||
if loop is None:
|
||||
loop = asyncio.get_event_loop()
|
||||
partial_func = partial(func, *args, **kwargs)
|
||||
return await loop.run_in_executor(executor, partial_func)
|
||||
|
||||
return run
|
||||
|
||||
|
||||
def async_unwrap(to_await):
|
||||
async_response = []
|
||||
|
||||
async def run_and_capture_result():
|
||||
r = await to_await
|
||||
async_response.append(r)
|
||||
|
||||
loop = asyncio.get_event_loop()
|
||||
coroutine = run_and_capture_result()
|
||||
loop.run_until_complete(coroutine)
|
||||
return async_response[0]
|
||||
1
core/settings.py
Normal file
1
core/settings.py
Normal file
@@ -0,0 +1 @@
|
||||
MAX_ORDER = 60
|
||||
8
core/split.py
Normal file
8
core/split.py
Normal file
@@ -0,0 +1,8 @@
|
||||
def amount_split(amount):
|
||||
"""Given an amount returns a list of amounts returned e.g. 13 is [1, 4, 8]."""
|
||||
bits_amt = bin(amount)[::-1][:-2]
|
||||
rv = []
|
||||
for (pos, bit) in enumerate(bits_amt):
|
||||
if bit == "1":
|
||||
rv.append(2**pos)
|
||||
return rv
|
||||
Reference in New Issue
Block a user