def __init__(self, a: RingElement, b: RingElement, ring: Ring = None, base_tuple: tuple = None, cardinality: int = None, check_singularity: bool = True): """ Parameters: a (RingElement): `a` coefficient. b (RingElement): `b` constant. ring (Ring): Underlying ring. base_tuple (tuple): Tuple representing the base point 'G'. cardinality (int): Number of points on the curve. check_singularity (bool): Check if the curve is singular (no cusps or self-intersections). """ from samson.math.symbols import Symbol self.a = a self.b = b self.ring = ring or self.a.ring if check_singularity: if (4 * a**3 - 27 * b**2) == self.ring.zero(): raise ValueError("Elliptic curve can't be singular") if base_tuple: base_tuple = WeierstrassPoint(*base_tuple, self) self.G_cache = base_tuple self.PAF_cache = None self.dpoly_cache = {} self.cardinality_cache = cardinality self.curve_poly_ring = self[Symbol('x'), Symbol('y')]
def parse_atkin(data: str): x = Symbol('x') y = Symbol('y') P = ZZ[x, y] p = P.zero for line in data.strip().split('\n'): xp, yp, c = [int(c) for c in line.strip().split()] p += x**xp * y**yp * c return p
def __init__(self, ring: Ring, symbol: Symbol=None): """ Parameters: ring (Ring): Underlying ring. """ self.ring = ring self.symbol = symbol or Symbol('x') self.symbol.build(self)
def test_vec0(self): seed = 0xACE1 x = Symbol('x') _ = (ZZ / ZZ(2))[x] poly = x**16 + x**14 + x**13 + x**11 + 1 expected_output = [ 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1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1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 1 ] self._run_test(seed, poly, expected_output)
def __init__(self, coeffs: list, coeff_ring: Ring = None, symbol: object = None, ring: Ring = None): """ Parameters: coeffs (list or Expr): Coefficients of the polynomial as a list of increasing degree or an expression. coeff_ring (Ring): Ring the coefficients are in. symbol (Symbol): Symbol to use as the indeterminate. ring (Ring): Parent PolynomialRing. """ from samson.math.symbols import Symbol self.coeff_ring = coeff_ring or coeffs[0].ring if type(coeffs) is list or type(coeffs) is tuple or type( coeffs) is dict: if type(coeffs) is dict or (len(coeffs) > 0 and type(coeffs[0]) is tuple): vec = coeffs else: vec = [self.coeff_ring.coerce(coeff) for coeff in coeffs] self.coeffs = SparseVector(vec, self.coeff_ring.zero()) elif type(coeffs) is SparseVector: self.coeffs = coeffs else: raise Exception( f"'coeffs' is not of an accepted type. Received {type(coeffs)}" ) self.symbol = symbol or Symbol('x') self.ring = ring or self.coeff_ring[self.symbol] if len(self.coeffs.values) == 0: self.coeffs = SparseVector([self.coeff_ring.zero()], self.coeff_ring.zero())
def __init__(self, kc: list, addr: list, master_clk: list): """ Parameters: kc (list): Session key derived from master key. addr (list): Hardware address. master_clk (list): Master clock values. """ Primitive.__init__(self) x = Symbol('x') _ = (ZZ / ZZ(2))[x] self.lfsrs = [ FLFSR(0, x**25 + x**20 + x**12 + x**8 + 1), FLFSR(0, x**31 + x**24 + x**16 + x**12 + 1), FLFSR(0, x**33 + x**28 + x**24 + x**4 + 1), FLFSR(0, x**39 + x**36 + x**28 + x**4 + 1) ] self.kc = kc self.addr = addr self.master_clk = master_clk self.state = 0 self.key = 0 self.key_schedule()
from samson.math.algebra.all import FF, ZZ, QQ, P256 from samson.math.symbols import Symbol, oo from samson.math.general import random_int, find_prime, factor from samson.utilities.exceptions import NotInvertibleException from functools import reduce import unittest x = Symbol('x') y = Symbol('y') F = FF(2, 8) F23 = F / F[23] FX2 = F[x] / (x**2) Z_star = ZZ.mul_group() Zp_star = (ZZ / ZZ(find_prime(128))).mul_group() Zn_star = (ZZ / ZZ(random_int(2**32))).mul_group() Z_2 = ZZ / ZZ(2) P = Z_2[x] P_q = P / (x**8 + x**4 + x**3 + x + 1) R = ZZ[x] / (x**167 - 1) P256C = P256[x, y] ALGEBRAS = [ZZ, QQ, F, F23, P, Z_star, Z_2, P_q, P256, P256C] class AlgebraTestCase(unittest.TestCase): def test_random(self): for algebra in ALGEBRAS: try: max_elem = algebra[11] except NotImplementedError:
from samson.math.algebra.rings.integer_ring import ZZ from samson.math.symbols import Symbol from samson.public_key.ntru import NTRU from samson.utilities.bytes import Bytes import unittest x = Symbol('x') _ = ZZ[x] # https://en.wikipedia.org/wiki/NTRUEncrypt PARAM_SETS = [(167, 128, 3), (251, 128, 3), (347, 128, 3), (503, 128, 3)] class NTRUTestCase(unittest.TestCase): def test_gauntlet(self): for N, q, p in PARAM_SETS: print(N, q, p) for _ in range(3): ntru = NTRU(N=N, p=p, q=q) plaintext = Bytes.random(8) ciphertext = ntru.encrypt(plaintext) self.assertEqual(ntru.decrypt(ciphertext).zfill(8), plaintext) # Tests generated manually using https://github.com/jkrauze/ntru # Commands generally of the form: # ./ntru.py gen N p q test_priv test_pub # echo -ne "<plaintext>" > test_msg # ./ntru.py -o enc test_pub.npz test_msg # ./ntru.py enc test_pub.npz test_msg > test_enc # ./ntru.py dec test_priv.npz test_enc