pythonのpow

pow(self.nom, exponent, self.prime)で計算できる

prime = 7
for i in range(1, prime):
    a = pow(i, prime-1, 6)
    print(a)
for prime in (7, 11, 17 , 31):
    for i in range(1, prime):
        a = pow(i, prime-1, 6)
        print(a)
for prime in (7, 11, 17 , 31):
    print([pow(i, prime-1, prime) for i in range(1, prime)])

$ python3 test.py
[1, 1, 1, 1, 1, 1]
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]

3/24 のF31は

prime = 31
a = 3 * pow(24, prime-2)
print(a % prime)

print(pow(17, -3, prime))

print(pow(4, -4, prime)*11 % prime)

Pythonのtype()と__class__

type() は、オブジェクトのタイプまたはクラスを見つけるために使用できる事前定義された関数
__name__ は、基本的にそれが使用されている現在のモジュールの名前を与える特別な組み込み変数

class Num:
    def __init__(self, num):
        self.num = num

x = Num(1)
print(type(x).__name__)

$ python3 test.py
Num

__class__ プロパティを使用して、オブジェクトのクラスまたはタイプを検索することもできる。これは基本的に、オブジェクトが作成されたクラスを指す。

class Num:
    def __init__(self, num):
        self.num = num

x = Num(1)
print(x.__class__)
print(x.__class__.__name__)

$ python3 test.py

Num

有限体 加算

    def __add__(self, other):
        if self.prime != other.prime:
            raise TypeError('Cannot add two numbers in different Fields')
        num = (self.num + other.num) % self.prime
        return self.__class__(num, self.prime)

Pythonのraise ValueError

関数などで例外を発生させたい場合にraiseを使用する。

raise 例外クラス(message):

def process_list(numbers):
    for num in numbers:
        try:
            if num < 0:
                raise ValueError("Negative numbers are not allowed.")
            result = 100 / num
            print(f"Result of division: {result}")
        except ZeroDivisionError:
            print("Error: Division by zero is not allowed.")
        except ValueError as ve:
            print(f"Error: {ve}")
        except Exception as e:
            print(f"Error occurred: {e}")

Pythonの__eq__メソッド

__eq__メソッド は等価の条件を定める

class Person:

    def __init__(self, firstname, lastname, email):
        self.firstname = firstname
        self.lastname = lastname
        self.email = email

    def __eq__(self, other):
        if other is None or not isinstance(other, Person): return False

        return self.firstname == other.firstname and \
            self.lastname == other.lastname and \
            self.email == other.email

    def __ne__(self, other):
        return not self.__eq__(other)
    

mike = Person('Mike', 'Peter', 'mike@gmail.com')
peter = Person('Mike', 'Peter', 'mike@gmail.com')
print(mike)
print(peter)
print(mike == peter)

eqの他にも、lt, ne, le, gt, geなどがある。

class Item(object):

    def __init__(self, price):
        self.price = price

    def __eq__(self, other):
        if not isinstance(other, Item):
            return NotImplemented
        return self.price == other.price

    def __lt__(self, other):
        if not isinstance(other, Item):
            return NotImplemented
        return self.price < other.price
    
    def __ne__(self, other):
        return not self.__eq__(other)

    def __le__(self, other):
        return self.__lt__(other) or self.__eq__(other)
    
    def __gt__(self, other):
        return not self.__le__(other)
    
    def __ge__(self, other):
        return not self.__lt__(other)

Pythonの__rpr__メソッド

__str__, __repr__は特殊メソッドと呼ぶ

initのみの場合

class Person:

    def __init__(self, name: str, age: int) -> None:
        self.name = name
        self.age = age

mike = Person('Mike', 20)
print(mike)
class Person:

    def __init__(self, name: str, age: int) -> None:
        self.name = name
        self.age = age

mike = Person('Mike', 20)
print(f'name: {mike.name}, age: {mike.age}')

classの中に書く

class Person:

    def __init__(self, name: str, age: int) -> None:
        self.name = name
        self.age = age

    def __str__(self) -> str:
        return f'name: {mike.name}, age: {mike.age}'

mike = Person('Mike', 20)
print(mike)

__repr__ は同じ値のオブジェクトを再生成できる文字列を定義。

class Person:

    def __init__(self, name: str, age: int) -> None:
        self.name = name
        self.age = age

    def __repr__(self) -> str:
        return f'name: {mike.name}, age: {mike.age}'

mike = Person('Mike', 20)
print(repr(mike))
class Person:

    def __init__(self, name: str, age: int) -> None:
        self.name = name
        self.age = age

    def __repr__(self) -> str:
        return f'name: {mike.name}, age: {mike.age}'

mike = Person('Mike', 20)
print(repr(mike))

有限体とは

位数が有限である体
四則演算が定義され閉じている有限集

整数の集合は無限の要素だが、有限体は要素が有限で、四則演算(足し算・引き算・掛け算・割り算)が閉じている
=> 閉じているとは、演算結果が、有理数でなくなるということがない
=> 演算を施した結果がふたたびもとの集合に属する
素数 P で割った余り

これ、初手でいきなりつまづくな…

pythonとhmac-sha512

HMACは暗号ハッシュ関数を使用してメッセージ認証を行う仕組み

import hmac
import hashlib

key=b"secret2"
text=b"foo bar"
signature=hmac.new(key,text,hashlib.md5).hexdigest()
print(signature)

b””としないと、エラーになる。
self._hmac = _hashopenssl.hmac_new(key, msg, digestmod=digestmod)
TypeError: Strings must be encoded before hashing

16進数と整数の変換

0xはその文字列が16進数であることを示す

print(format(0xabcd, 'x'))
print(hex(0xabcd))
print('%02x' % 0xabcd)

binasciiはバイト列に変換する

import binascii
print(binascii.unhexlify('abcd'))
print(str(binascii.hexlify(b'\xab\xcd'), 'utf-8'))