Merge remote-tracking branch 'origin/main'
This commit is contained in:
commit
7f9473097f
45 changed files with 1507 additions and 12 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
|
@ -1 +1,3 @@
|
|||
__pycache__/
|
||||
nimble.develop
|
||||
nimble.paths
|
||||
|
|
|
|||
24
README
24
README
|
|
@ -2,3 +2,27 @@ The "imbaud python library" (imp lib), or just imp for short!
|
|||
|
||||
TODO:
|
||||
- define a getPrime function like PyCryptodome's
|
||||
- rewrite nim-lang/bigints to implement features like Karatsuba multiplication, or even Toom-3 multiplication
|
||||
|
||||
|
||||
PyCryptodome defines getPrime as follows:
|
||||
```py
|
||||
def getPrime(N, randfunc=None):
|
||||
"""Return a random N-bit prime number.
|
||||
|
||||
N must be an integer larger than 1.
|
||||
If randfunc is omitted, then :meth:`Random.get_random_bytes` is used.
|
||||
"""
|
||||
if randfunc is None:
|
||||
randfunc = Random.get_random_bytes
|
||||
|
||||
if N < 2:
|
||||
raise ValueError("N must be larger than 1")
|
||||
|
||||
while True:
|
||||
number = getRandomNBitInteger(N, randfunc) | 1
|
||||
if isPrime(number, randfunc=randfunc):
|
||||
break
|
||||
return number
|
||||
```
|
||||
in essence infinite random generation until a prime is found
|
||||
|
|
|
|||
10
celeste.nimble
Normal file
10
celeste.nimble
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
# Package
|
||||
version = "0.1.0"
|
||||
author = "Emile Clark-Boman"
|
||||
description = "Self contained framework for computational mathematics"
|
||||
license = "MIT"
|
||||
srcDir = "src"
|
||||
|
||||
|
||||
# Dependencies
|
||||
requires "nim >= 2.2.0"
|
||||
|
|
@ -1,7 +1,7 @@
|
|||
from math import inf, ceil
|
||||
from typing import Callable
|
||||
|
||||
from imp.math.util import clamp_max
|
||||
from celeste.math.util import clamp_max
|
||||
|
||||
SPACER = b'\xff' # arbitrary spacing character
|
||||
|
||||
|
|
@ -12,7 +12,7 @@ Terminology:
|
|||
substitutions to the entire message (ie vigenere)
|
||||
'''
|
||||
|
||||
from imp.constants import ALPHA_LOWER, ALPHA_UPPER
|
||||
from celeste.constants import ALPHA_LOWER, ALPHA_UPPER
|
||||
|
||||
'''
|
||||
Constant Declarations
|
||||
|
|
@ -6,7 +6,7 @@ Terminology:
|
|||
the "prime proper divisors of n".
|
||||
'''
|
||||
|
||||
from imp.extern.primefac import primefac
|
||||
from celeste.extern.primefac import primefac
|
||||
|
||||
def factors(n: int) -> int:
|
||||
pfactors: list[tuple[int, int]] = []
|
||||
|
|
@ -1,7 +1,7 @@
|
|||
from math import gcd, inf
|
||||
|
||||
from imp.math.numbers import bigomega, factors
|
||||
from imp.extern.primefac import (
|
||||
from celeste.math.numbers import bigomega, factors
|
||||
from celeste.extern.primefac import (
|
||||
isprime,
|
||||
primegen as Primes,
|
||||
)
|
||||
4
config.nims
Normal file
4
config.nims
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
# begin Nimble config (version 2)
|
||||
when withDir(thisDir(), system.fileExists("nimble.paths")):
|
||||
include "nimble.paths"
|
||||
# end Nimble config
|
||||
|
|
@ -8,7 +8,7 @@ from math import ceil
|
|||
|
||||
from Crypto.Cipher import AES
|
||||
|
||||
from imp.math.util import xor_bytes
|
||||
from celeste.math.util import xor_bytes
|
||||
|
||||
class StepUpCounter(object):
|
||||
def __init__(self, step_up=False):
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ Solution to https://cryptohack.org/courses/symmetric/symmetry/
|
|||
import os
|
||||
import requests
|
||||
|
||||
from imp.math.util import xor_bytes, xor_str
|
||||
from celeste.math.util import xor_bytes, xor_str
|
||||
|
||||
URL = 'https://aes.cryptohack.org/symmetry'
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
import requests
|
||||
|
||||
from imp.constants import PRINTABLE
|
||||
from imp.attacks import paddingoracle
|
||||
from celeste.constants import PRINTABLE
|
||||
from celeste.attacks import paddingoracle
|
||||
|
||||
from Crypto.Util.Padding import pad
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import string
|
||||
|
||||
from imp.attacks import paddingoracle
|
||||
from celeste.attacks import paddingoracle
|
||||
|
||||
from Crypto.Cipher import AES
|
||||
from Crypto.Util.Padding import pad
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
[tool.poetry]
|
||||
name = "imp"
|
||||
name = "Celeste"
|
||||
version = "0.1.0"
|
||||
description = ""
|
||||
authors = ["Emile Clark-Boman <eclarkboman@gmail.com>"]
|
||||
|
|
@ -7,7 +7,6 @@ readme = "README.md"
|
|||
|
||||
[tool.poetry.dependencies]
|
||||
python = "^3.12"
|
||||
pycryptodome = "^3.23.0"
|
||||
requests = "^2.32.4"
|
||||
|
||||
|
||||
|
|
|
|||
12
research/aparith/README
Normal file
12
research/aparith/README
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
Comparing the speeds of various Nim libraries for
|
||||
arbitrary precision integers.
|
||||
|
||||
Test Targets:
|
||||
https://github.com/status-im/nim-stint
|
||||
https://github.com/nim-lang/bigints
|
||||
https://github.com/michaeljclark/bignum
|
||||
https://github.com/FedeOmoto/bignum
|
||||
https://github.com/fsh/integers
|
||||
|
||||
Test algorithms:
|
||||
https://en.wikipedia.org/wiki/Pollard%27s_rho_algorithm#Algorithm
|
||||
BIN
research/aparith/aparith
Executable file
BIN
research/aparith/aparith
Executable file
Binary file not shown.
14
research/aparith/aparith.nimble
Normal file
14
research/aparith/aparith.nimble
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
# Package
|
||||
|
||||
version = "1.0.0"
|
||||
author = "Emile Clark-Boman"
|
||||
description = "Arbitrary Precision Integer Test - BigInts"
|
||||
license = "MIT"
|
||||
srcDir = "src"
|
||||
bin = @["aparith", "speedtest_bigint"]
|
||||
|
||||
|
||||
# Dependencies
|
||||
|
||||
requires "nim >= 2.2.0"
|
||||
requires "bigints >= 1.0.0"
|
||||
5
research/aparith/src/aparith.nim
Normal file
5
research/aparith/src/aparith.nim
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
# This is just an example to get you started. A typical binary package
|
||||
# uses this file as the main entry point of the application.
|
||||
|
||||
when isMainModule:
|
||||
echo("Hello, World!")
|
||||
1311
research/aparith/src/bigints.git.nim
Normal file
1311
research/aparith/src/bigints.git.nim
Normal file
File diff suppressed because it is too large
Load diff
17
research/aparith/src/bigints.git/private/literals.nim
Normal file
17
research/aparith/src/bigints.git/private/literals.nim
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
# This is an include file, do not import it directly.
|
||||
# It is needed as a workaround for Nim's parser for versions <= 1.4.
|
||||
|
||||
proc `'bi`*(s: string): BigInt =
|
||||
## Create a `BigInt` from a literal, using the suffix `'bi`.
|
||||
runnableExamples:
|
||||
let
|
||||
a = 123'bi
|
||||
b = 0xFF'bi
|
||||
c = 0b1011'bi
|
||||
assert $a == "123"
|
||||
assert $b == "255"
|
||||
assert $c == "11"
|
||||
case s[0..min(s.high, 1)]
|
||||
of "0x", "0X": initBigInt(s[2..s.high], base = 16)
|
||||
of "0b", "0B": initBigInt(s[2..s.high], base = 2)
|
||||
else: initBigInt(s)
|
||||
43
research/aparith/src/bigints.git/random.nim
Normal file
43
research/aparith/src/bigints.git/random.nim
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
import ../bigints
|
||||
import std/sequtils
|
||||
import std/options
|
||||
import std/random
|
||||
|
||||
func rand*(r: var Rand, x: Slice[BigInt]): BigInt =
|
||||
## Return a random `BigInt`, within the given range, using the given state.
|
||||
assert(x.a <= x.b, "invalid range")
|
||||
let
|
||||
spread = x.b - x.a
|
||||
# number of bits *not* including leading bit
|
||||
nbits = spread.fastLog2
|
||||
# number of limbs to generate completely randomly
|
||||
nFullLimbs = max(nbits div 32 - 1, 0)
|
||||
# highest possible value of the top two limbs.
|
||||
hi64Max = (spread shr (nFullLimbs*32)).toInt[:uint64].get()
|
||||
while true:
|
||||
# these limbs can be generated completely arbitrarily
|
||||
var limbs = newSeqWith(nFullLimbs, r.rand(uint32.low..uint32.high))
|
||||
# generate the top two limbs more carefully. This all but guarantees
|
||||
# that the entire number is in the correct range
|
||||
let hi64 = r.rand(uint64.low..hi64Max)
|
||||
limbs.add(cast[uint32](hi64))
|
||||
limbs.add(cast[uint32](hi64 shr 32))
|
||||
result = initBigInt(limbs)
|
||||
if result <= spread:
|
||||
break
|
||||
result += x.a
|
||||
|
||||
func rand*(r: var Rand, max: BigInt): BigInt =
|
||||
## Return a random non-negative `BigInt`, up to `max`, using the given state.
|
||||
rand(r, 0.initBigInt..max)
|
||||
|
||||
# backwards compatibility with 1.4
|
||||
when not defined(randState):
|
||||
var state = initRand(777)
|
||||
proc randState(): var Rand = state
|
||||
|
||||
proc rand*(x: Slice[BigInt]): BigInt = rand(randState(), x)
|
||||
## Return a random `BigInt`, within the given range.
|
||||
|
||||
proc rand*(max: BigInt): BigInt = rand(randState(), max)
|
||||
## Return a random `BigInt`, up to `max`.
|
||||
BIN
research/aparith/src/speedtest_bigint
Executable file
BIN
research/aparith/src/speedtest_bigint
Executable file
Binary file not shown.
35
research/aparith/src/speedtest_bigint.nim
Normal file
35
research/aparith/src/speedtest_bigint.nim
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
import bigints
|
||||
import std/[options, times]
|
||||
|
||||
func g(x: BigInt, n: BigInt): BigInt {.inline.} =
|
||||
result = (x*x + 1.initBigInt) mod n
|
||||
|
||||
func pollardRho(n: BigInt): Option[BigInt] =
|
||||
var
|
||||
x: BigInt = 2.initBigInt
|
||||
y: BigInt = n
|
||||
d: BigInt = 1.initBigInt
|
||||
|
||||
while d == 1.initBigInt:
|
||||
x = g(x, n)
|
||||
y = g(g(y, n), n)
|
||||
d = gcd(abs(x - y), n)
|
||||
|
||||
if d == n:
|
||||
return none(BigInt)
|
||||
result = some(d)
|
||||
|
||||
|
||||
when isMainModule:
|
||||
let
|
||||
# num = 535006138814359.initBigInt
|
||||
# num = 12.initBigInt
|
||||
num = 976043389537.initBigInt * 270351207761773.initBigInt
|
||||
time = cpuTime()
|
||||
divisor = pollardRho(num)
|
||||
elapsed = cpuTime() - time
|
||||
echo "Time taken: ", elapsed
|
||||
if divisor.isSome:
|
||||
echo "Result: ", divisor.get()
|
||||
else:
|
||||
echo "Result: None(BigInt)"
|
||||
7
src/imp.nim
Normal file
7
src/imp.nim
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
# This is just an example to get you started. A typical library package
|
||||
# exports the main API in this file. Note that you cannot rename this file
|
||||
# but you can remove it if you wish.
|
||||
|
||||
proc add*(x, y: int): int =
|
||||
## Adds two numbers together.
|
||||
return x + y
|
||||
12
src/imp/submodule.nim
Normal file
12
src/imp/submodule.nim
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
# This is just an example to get you started. Users of your library will
|
||||
# import this file by writing ``import celeste/submodule``. Feel free to rename or
|
||||
# remove this file altogether. You may create additional modules alongside
|
||||
# this file as required.
|
||||
|
||||
type
|
||||
Submodule* = object
|
||||
name*: string
|
||||
|
||||
proc initSubmodule*(): Submodule =
|
||||
## Initialises a new ``Submodule`` object.
|
||||
Submodule(name: "Anonymous")
|
||||
Loading…
Add table
Add a link
Reference in a new issue