def init_random(self, lmbd: int = 254):
     limit = (1<<lmbd)
     for i in range(self.size):
         self.M[i][0] = GE(randint(0, limit))
         self.M[i][1] = GE(randint(0, limit))
     assert self.all_validate(), "TDFMatrix contains invalid points"
     return self
    def test_dot(self):
        self.assertRaises(TDFError, VECTOR.dot,
                          bitarray(1) * (VECTOR_SIZE + 1))

        hashed = VECTOR.dot(DOT_TEST).to_affine()
        self.assertEqual(hashed, GE(DOT_TEST_R).to_affine())

        short_dot = VECTOR.dot([0]).to_affine()
        self.assertEqual(short_dot, GE(0).to_affine())
Пример #3
0
    def test_dot(self):
        self.assertRaises(TDFError, MATRIX.dot,
                          bitarray(1) * (MATRIX_SIZE + 1))

        hashed = MATRIX.dot(DOT_TEST).to_affine()
        self.assertEqual(hashed, GE(DOT_TEST_R).to_affine())

        short_dot = MATRIX.dot([0]).to_affine()
        self.assertEqual(short_dot, GE(1).to_affine())
Пример #4
0
def E1(PP, i, b, rho) -> list:
    n = len(PP)
    ct = [None for i in range(n)]
    for j in range(n):
        if i == j:
            if b:
                ct[j] = (GE(0), GE.copy(PP[j][1]).scale(rho))
            else:
                ct[j] = (GE.copy(PP[j][0]).scale(rho), GE(0))
        else:
            ct[j] = (GE.copy(PP[j][0]).scale(rho),
                     GE.copy(PP[j][1]).scale(rho))

    return ct
 def test_scale(self):
     n_vector = VECTOR.copy()
     n_vector.scale(42)
     scaled_hash = n_vector.dot(DOT_TEST).to_affine()
     self.assertEqual(scaled_hash, GE(DOT_TEST_R * 42).to_affine())
     self.assertTrue(VECTOR.all_validate())
     self.assertTrue(n_vector.all_validate())
Пример #6
0
def KG_ls(length: int, lmbd: int = 254) -> DDH_TDF_IndexKey:
    limit = (1 << lmbd)
    base_M = TDFVector(length).init_random(lmbd)
    r = [randint(1, limit) for i in range(length)]
    M = [base_M.copy().scale(r[i]) for i in range(length)]
    for i in range(length):
        M[i].set(i, GE(randint(1, limit)))
    return DDH_TDF_IndexKey(base_M, M)
Пример #7
0
 def dot(self, x: Sequence[Union[int, str, bool]]) -> GE:
     # x: bitarray, binary list, binary string, etc.
     #assert len(x) <= self.size, "dot: x is too long, len(x) = " + str(len(x)) + " > " + str(self.size)
     if len(x) > self.length:
         raise TDFError('dot: x is too long, len(x) = {} > {}'.format(
             len(x), self.length))
     a = GE(0)
     for i in range(1, len(x)):
         if x[i]:
             a.add(self.M[i])
     return a
Пример #8
0
    def KG(self, m: int, lmbd: int = 254):
        print("LTDF: Generating Keys")
        limit = (1 << lmbd)
        base_M = TDFMatrix(m).init_random(lmbd)
        r = [randint(0, limit) for i in range(m)]
        b = [randint(0, 1) for i in range(m)]
        g = [GE(randint(0, limit)) for i in range(m)]

        M = [
            base_M.copy().scale(r[i]).set(i, b[i], g[i].copy())
            for i in range(m)
        ]

        print("LTDF: Finished generating {} keys".format(m))
        return IndexKey(base_M, M), TrapdoorKey(base_M, r, b, g)
Пример #9
0
 def test_add(self):
     self.assertEqual(ge42, GE(20).add(GE(22)).to_affine())
Пример #10
0
def f(PP, x) -> GE:
    y: GE = GE(0)
    bin_x = str_to_bin_list(x)
    for i in range(len(bin_x)):
        y.add(PP[i][bin_x[i]])
    return y
Пример #11
0
import pickle
from bitarray import bitarray

from floodberry.floodberry_ed25519 import GE25519 as GE
from tdf_strucs import TDFMatrix, TDFError
from utils import serialize, deserialize

MATRIX_TEST_SERIALIZE_FILE = "tests/tdf_test_matrix.p"
ONE = [1, 0, 0, 0, 0]

MATRIX_SIZE = 3
MATRIX = TDFMatrix(MATRIX_SIZE)
#[ 1, 3, 5, ...]
#[ 2, 4, 6, ...]
for i in range(MATRIX_SIZE):
    MATRIX.set(i, 0, GE(2 * i + 1))
    MATRIX.set(i, 1, GE(2 * i + 2))

#[ 0, 1, 0, 1 ...]
DOT_TEST = [0, 1] * (MATRIX_SIZE // 2) + [0] * (MATRIX_SIZE % 2)
DOT_TEST_R = sum([i for i in range(1, 2 * MATRIX_SIZE, 4)]) + \
             sum([i for i in range(4, 2 * MATRIX_SIZE, 4)])


class TestTDFMatrix(unittest.TestCase):
    def test_copy(self):
        n_matrix = MATRIX.copy()
        self.assertEqual(n_matrix, MATRIX)

        n_matrix.scale(2)
        self.assertNotEqual(n_matrix, MATRIX)
from floodberry.floodberry_ed25519 import GE25519 as GE
from utils import serialize

serialize(GE(1), "temp/1.p")
serialize([GE(1)], "temp/1_list.p")
serialize([GE(1), GE(2)], "temp/2.p")
serialize([GE(i) for i in range(10)], "temp/10.p")
serialize([GE(i) for i in range(100)], "temp/100.p")
Пример #13
0
def K(n: int, lmbd: int = 254) -> list:
    limit = 1 << lmbd
    return [(GE(randint(0, limit)), GE(randint(0, limit))) for i in range(n)]
Пример #14
0
 def test_packing(self):
     self.assertEqual(GE(42).pack(), packed42)
     self.assertEqual(GE().unpack(packed42), ge42)
Пример #15
0
 def test_serialization_list(self):
     x = [GE(3).to_affine(), GE(4).to_affine(), GE(5).to_affine()]
     with open(SERIALIZE_LIST_TEST_FILE, "wb") as f:
         pickle.dump(x, f)
     with open(SERIALIZE_LIST_TEST_FILE, "rb") as f:
         self.assertEqual(pickle.load(f), x)
Пример #16
0
    def test_validate(self):
        self.assertTrue(ge42.validate())
        bad_ge = b'\xce\x1a2XXXXXXX\xf39\t\xf4Cs\xae,\xf9M\xde\xf0\xfd\x92 5\xc4\x83g\x067\xc2'

        self.assertFalse(GE().unpack(bad_ge).validate())
Пример #17
0
    def test_copy(self):
        ge_old = GE(42)
        ge_new = ge_old.copy().to_affine()

        self.assertNotEqual(ge_old, ge_new)
        self.assertEqual(ge_new, ge42)
Пример #18
0
 def test_scale(self):
     n_matrix = MATRIX.copy()
     n_matrix.scale(42)
     scaled_hash = n_matrix.dot(DOT_TEST).to_affine()
     self.assertEqual(scaled_hash, GE(DOT_TEST_R * 42).to_affine())
from floodberry.floodberry_ed25519 import GE25519 as GE
from tdf_strucs import TDFError, TDFMatrix, IndexKey, TrapdoorKey, TDFCipherText, TDFCodec
from utils import str_to_bitarr, int_lst_to_bitarr

from random import randint
from bitarray import bitarray
from array import array
import pickle

G1 = GE(1).to_affine()


class CTDFCipherText(TDFCipherText):
    def __init__(self, gc: GE, hc_list: bitarray):
        assert gc.validate(), "Ciphertext hash invalid."
        self._gc = gc
        self._b_prime = hc_list

    def __eq__(self, other):
        return all([self.gc == other.gc, self.b_prime == other.b_prime])


class CTDFCodec(TDFCodec):
    def __init__(self, max_len: int, lmbd: int = 254):
        self._ik, self._tk = self.KG(max_len, lmbd)

    #F()
    def encode(self, msg, c: int = 8):
        # (msg) will be converted to bitarray
        # c = character size in bits
Пример #20
0
 def test_scale(self):
     self.assertEqual(ge42, GE(6).scale(7).to_affine())
Пример #21
0
import unittest
import floodberry
from floodberry.floodberry_ed25519 import GE25519 as GE

import pickle

SERIALIZE_TEST_FILE = "tests/test_ge.p"
SERIALIZE_LIST_TEST_FILE = "tests/test_ge_list.p"

packed42 = b'\xce\x1a2\x99N\x83\\\x19>+\xf39\t\xf4Cs\xae,\xf9M\xde\xf0\xfd\x92 5\xc4\x83g\x067\xc2'
ge42 = GE(42).to_affine()

ge42x = [
    638129450090601, 1092992485173610, 659239496376681, 1398765529464492,
    1649159199051745
]
ge42y = [
    1270273507728078, 324623919465259, 2221781109510096, 721595290644262,
    1164865569307715
]
ge42z = [1, 0, 0, 0, 0]
ge42t = [
    621704001636999, 755815915372815, 259964580607406, 1557714783488804,
    1367295035199813
]


class TestGE(unittest.TestCase):
    def test_accessors(self):
        self.assertEqual(ge42.x, ge42x)
        self.assertEqual(ge42.y, ge42y)
Пример #22
0
 def init_random(self, lmbd: int = 254) -> TDFVector:
     limit = (1 << lmbd)
     for i in range(self.length):
         self.M[i] = GE(randint(1, limit))
     assert self.all_validate(), "TDFVector contains invalid points"
     return self
Пример #23
0
 def test_double(self):
     self.assertEqual(ge42, GE(21).double().to_affine())
import unittest
import pickle
from bitarray import bitarray

from floodberry.floodberry_ed25519 import GE25519 as GE
from ddh_tdf import TDFVector, TDFError
from utils import serialize, deserialize

VECTOR_TEST_SERIALIZE_FILE = "tests/tdf_test_vector.p"
ONE = [1, 0, 0, 0, 0]

VECTOR_SIZE = 3
VECTOR = TDFVector(VECTOR_SIZE)
#[ 1, 3, 5, ...]
for i in range(VECTOR_SIZE):
    VECTOR.set(i, GE(2 * i + 1))

#[ 0, 1, 0, 1 ...]
DOT_TEST = [0, 1] * (VECTOR_SIZE // 2) + [0] * (VECTOR_SIZE % 2)
DOT_TEST_R = sum([i for i in range(3, 2 * VECTOR_SIZE, 4)])


class TestTDFVector(unittest.TestCase):
    def test_copy(self):
        n_vector = VECTOR.copy()
        self.assertEqual(n_vector, VECTOR)

        n_vector.scale(2)
        self.assertNotEqual(n_vector, VECTOR)

    def test_all_to_affine(self):