Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
colinxu2020 committed Jul 16, 2024
1 parent ba70b01 commit 51b71d9
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 8 deletions.
1 change: 0 additions & 1 deletion .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions build_mypyc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from os import system
from pathlib import Path


sources = []
for pth in Path('slhdsa').glob('**/*.py'):
if pth.name!='__init__.py':
sources.append(pth.as_posix().replace('/', '\\'))
system(' '.join(['mypyc']+sources))
7 changes: 3 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[project]
name = "SLH-DSA"
version = "0.1.3"
description = "The pure python impl of the slh-das algorithm(based on fips205)."
description = "The pure python implement of the slh-dsa algorithm(based on fips205)."
authors = [
{name = "Colinxu2020", email = "[email protected]"},
]
Expand Down Expand Up @@ -37,9 +37,8 @@ keywords = ['fips205', 'slhdsa', 'sphincs', 'sphincsplus', 'crypto', 'cryptograp
"Bug Tracker" = "https://github.com/colinxu2020/slhdsa/issues"

[build-system]
requires = ["pdm-backend"]
build-backend = "pdm.backend"

requires = ["setuptools>=61", "wheel", "mypy>=1.10.1", "tomli>=1.1.0; python_version<'3.11'"]
build-backend = "setuptools.build_meta"

[tool.pdm]
distribution = true
Expand Down
58 changes: 58 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/usr/bin/env python

import glob
import os
from pathlib import Path
import sys

if sys.version_info < (3, 9, 0):
sys.stderr.write("ERROR: You need Python 3.9 or later to use slh-dsa yet.\n")
exit(1)

from setuptools import Extension, find_packages, setup
from setuptools.command.build_py import build_py

try:
import tomllib
except ModuleNotFoundError:
import tomli as tomllib


if os.getenv('SLHDSA_BUILD_OPTIMIZED', '0') == '1':
mypyc_targets = []
print('Building Optimized Library')

for pth in Path('slhdsa').glob('**/*.py'):
if pth.name != '__init__.py':
mypyc_targets.append(pth.as_posix().replace('/', os.sep))

from mypyc.build import mypycify

ext_modules = mypycify(
mypyc_targets,
opt_level = "3",
debug_level = "1"
)
else:
ext_modules = []


metadata = tomllib.load(open('pyproject.toml', 'rb'))['project']
setup(
name = "slhdsa",
version = metadata['version'],
description = metadata['description'],
long_description = open('README.md', encoding='utf8').read(),
author = metadata["authors"][0]["name"],
author_email = metadata["authors"][0]["email"],
url = metadata["urls"]["Homepage"],
license = metadata["license"],
py_modules = [],
ext_modules = ext_modules,
packages = find_packages(),
classifiers = metadata["classifiers"],
install_requires = metadata["dependencies"],
python_requires = metadata["requires-python"],
include_package_data = True,
project_urls = metadata["urls"],
)
1 change: 1 addition & 0 deletions slhdsa/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
for size in ["128", "192", "256"]:
for suffix in ["s", "f"]:
__all__.append(f"{algo}_{size}{suffix}")
__version__ = '0.1.3'
4 changes: 2 additions & 2 deletions slhdsa/lowlevel/addresses.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
class Address:
layer: int
tree: int
typ: int
typ: int = 0

def to_bytes(self) -> bytes:
return self.layer.to_bytes(4, "big") + self.tree.to_bytes(12, "big") + self.typ.to_bytes(4, "big")

def with_type(self, typ: type[T]) -> T:
return typ(self.layer, self.tree, typ.typ)
return typ(self.layer, self.tree)


@dataclass
Expand Down
3 changes: 2 additions & 1 deletion slhdsa/lowlevel/slhdsa.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ def keygen(par: Parameter) -> tuple[tuple[bytes, bytes, bytes, bytes], tuple[byt
return (sk_seed, sk_prf, pk_seed, pk_root), (pk_seed, pk_root)

def validate_secretkey(secret_key: tuple[bytes, bytes, bytes, bytes], par: Parameter) -> bool:
sk_seed, pk_prf, pk_seed, pk_root = secret_key
sk_seed, sk_prf, pk_seed, pk_root = secret_key
# len(sk_prf) # magic patch for mypyc, otherwise mypyc will crash
address = Address(par.d - 1, 0, 0)
pk_root_new = XMSS(par).node(sk_seed, 0, par.h_m, pk_seed, address)
return pk_root == pk_root_new
Expand Down
6 changes: 6 additions & 0 deletions slhdsa/slhdsa.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ def from_digest(cls, digest: bytes, par: Parameter) -> "PublicKey":
raise exc.SLHDSAKeyException('Wrong digest length')
return cls((digest[:par.n], digest[par.n:]), par)

def __str__(self):
return f'<SLHDSA Public Key: {self.digest().hex()}>'


@dataclass
class SecretKey:
Expand Down Expand Up @@ -52,6 +55,9 @@ def from_digest(cls, digest: bytes, par: Parameter) -> "SecretKey":
raise exc.SLHDSAKeyException("Wrong digest length")
return cls((digest[:par.n], digest[par.n:par.n*2], digest[par.n*2:par.n*3], digest[par.n*3:]), par)

def __str__(self):
return f'<SLHDSA Secret Key: {self.digest().hex()}>'


@dataclass
class KeyPair:
Expand Down

0 comments on commit 51b71d9

Please sign in to comment.