def superset_basis(C, T): ''' Input: - C: linearly independent set of Vecs - T: set of Vecs such that every Vec in C is in Span(T) Output: Linearly independent set S consisting of all Vecs in C and some in T such that the span of S is the span of T (i.e. S is a basis for the span of T). Example: >>> from vec import Vec >>> from independence import is_independent >>> a0 = Vec({'a','b','c','d'}, {'a':1}) >>> a1 = Vec({'a','b','c','d'}, {'b':1}) >>> a2 = Vec({'a','b','c','d'}, {'c':1}) >>> a3 = Vec({'a','b','c','d'}, {'a':1,'c':3}) >>> sb = superset_basis({a0, a3}, {a0, a1, a2}) >>> a0 in sb and a3 in sb True >>> is_independent(sb) True >>> all(x in [a0,a1,a2,a3] for x in sb) True ''' S = set() for v in C: if is_independent(S | {v}): S = S | {v} for v in T: if is_independent(S | {v}): S = S | {v} return S
def growSec(): from independence import is_independent from itertools import combinations proven = [(a0, b0)] found = False while not found: t = [(list2vec([randGF2() for i in range(6)]), list2vec([randGF2() for i in range(6)])) for j in range(2)] vecs = proven + t if all( is_independent(list(sum(x, ()))) for x in combinations(vecs, 3)): found = True proven += t found1 = False while not found1: t1 = [(list2vec([randGF2() for i in range(6)]), list2vec([randGF2() for i in range(6)])) for j in range(2)] vecs1 = proven + t1 if all( is_independent(list(sum(x, ()))) for x in combinations(vecs1, 3)): found1 = True proven += t1 return proven
def growSec(): from independence import is_independent from itertools import combinations proven = [(a0,b0)] found = False while not found: t = [(list2vec([randGF2() for i in range(6)]),list2vec([randGF2() for i in range(6)])) for j in range(2)] vecs = proven + t if all(is_independent(list(sum(x,()))) for x in combinations(vecs,3)): found = True proven += t found1 = False while not found1: t1 = [(list2vec([randGF2() for i in range(6)]),list2vec([randGF2() for i in range(6)])) for j in range(2)] vecs1 = proven + t1 if all(is_independent(list(sum(x,()))) for x in combinations(vecs1,3)): found1 = True proven += t1 return proven
def is_token_pair_suitable(tokens, candidate): if len(tokens) == 2: return is_independent(tokens + candidate) pairs_cnt = len(tokens) // 2 for i in range(pairs_cnt): for j in range(i + 1, pairs_cnt): if not is_independent([tokens[2*i], tokens[2*i + 1], tokens[2*j], tokens[2*j + 1]] + candidate): return False return True
def findVectors(listInput, numbers, bits, keys): if len(listInput) == numbers + 1: return listInput if len(listInput) < keys: while len(listInput) < keys: g = (GF2vecGen(bits), GF2vecGen(bits)) listInput.append(g) if not all(is_independent(list(sum(x,()))) for x in combinations(listInput,len(listInput))): listInput.remove(g) return findVectors(listInput, numbers, bits, keys) c = (GF2vecGen(bits), GF2vecGen(bits)) listInput.append(c) if not all(is_independent(list(sum(x,()))) for x in combinations(listInput,keys)): listInput.remove(c) return findVectors(listInput, numbers, bits, keys)
def superset_basis(C, T): ''' Input: - C: linearly independent set of Vecs - T: set of Vecs such that every Vec in C is in Span(T) Output: Linearly independent set S consisting of all Vecs in C and some in T such that the span of S is the span of T (i.e. S is a basis for the span of T). Example: >>> from vec import Vec >>> from independence import is_independent >>> a0 = Vec({'a','b','c','d'}, {'a':1}) >>> a1 = Vec({'a','b','c','d'}, {'b':1}) >>> a2 = Vec({'a','b','c','d'}, {'c':1}) >>> a3 = Vec({'a','b','c','d'}, {'a':1,'c':3}) >>> sb = superset_basis({a0, a3}, {a0, a1, a2}) >>> a0 in sb and a3 in sb True >>> is_independent(sb) True >>> all(x in [a0,a1,a2,a3] for x in sb) True ''' S = C for v in T.difference(C): S0 = S.union({v}) if is_independent(S0): S.add(v) return S
def my_rank(L): rank = len(L) rank -= 1 while rank > 0: temp = L.copy() for x in range(0, len(L)): temp.remove(temp[x]) if (is_independent(temp)): return rank temp = L.copy() rank -= 1 if (is_independent(L)): return rank return rank
def generate_vectors(): K = {0, 1, 2, 3, 4, 5} vecs = [ (Vec(K,{x:randGF2() for x in K}),Vec(K,{x:randGF2() for x in K})) for i in range(5) ] while (not (all(is_independent(list(sum(x,()))) for x in combinations(vecs,3)))): vecs = [ (Vec(K,{x:randGF2() for x in K}),Vec(K,{x:randGF2() for x in K})) for i in range(5) ] return vecs
def select_independent_vectors(): def select_random_vector_pairs(): return [(a0,b0)] + [(getRandomVector(), getRandomVector()) for i in range(4)] while True: pairs = select_random_vector_pairs() if all([is_independent(list(sum(x,()))) for x in combinations(pairs, 3)]): return pairs
def helper(): L0 = [] while True: a1 = list2vec([randGF2(),randGF2(),randGF2(),randGF2(),randGF2(),randGF2()]) b1 = list2vec([randGF2(),randGF2(),randGF2(),randGF2(),randGF2(),randGF2()]) a2 = list2vec([randGF2(),randGF2(),randGF2(),randGF2(),randGF2(),randGF2()]) b2 = list2vec([randGF2(),randGF2(),randGF2(),randGF2(),randGF2(),randGF2()]) if is_independent ([a0, b0, a1, b1, a2, b2]) == True: break L0 = [a0, b0, a1, b1, a2, b2] while True: a3 = list2vec([randGF2(),randGF2(),randGF2(),randGF2(),randGF2(),randGF2()]) b3 = list2vec([randGF2(),randGF2(),randGF2(),randGF2(),randGF2(),randGF2()]) L1 = [a0, b0, a1, b1, a3, b3] L2 = [a0, b0, a2, b2, a3, b3] L3 = [a1, b1, a2, b2, a3, b3] if is_independent (L1) == True and is_independent (L2) == True and is_independent (L3) == True: break while True: a4 = list2vec([randGF2(),randGF2(),randGF2(),randGF2(),randGF2(),randGF2()]) b4 = list2vec([randGF2(),randGF2(),randGF2(),randGF2(),randGF2(),randGF2()]) L4 = [a0, b0, a1, b1, a4, b4] L5 = [a0, b0, a2, b2, a4, b4] L6 = [a1, b1, a2, b2, a4, b4] L7 = [a0, b0, a3, b3, a4, b4] L8 = [a1, b1, a3, b3, a4, b4] L9 = [a2, b2, a3, b3, a4, b4] if is_independent (L4) == True and is_independent (L5) == True and is_independent (L6) == True and is_independent (L7) == True and is_independent (L8) == True and is_independent (L9) == True: break return a0, b0, a1, b1, a2, b2, a3, b3, a4, b4
def is_invertible(M): ''' input: A matrix, M outpit: A boolean indicating if M is invertible. >>> M = Mat(({0, 1, 2, 3}, {0, 1, 2, 3}), {(0, 1): 0, (1, 2): 1, (3, 2): 0, (0, 0): 1, (3, 3): 4, (3, 0): 0, (3, 1): 0, (1, 1): 2, (2, 1): 0, (0, 2): 1, (2, 0): 0, (1, 3): 0, (2, 3): 1, (2, 2): 3, (1, 0): 0, (0, 3): 0}) >>> is_invertible(M) True ''' return len(M.D[0])==len(M.D[1]) and is_independent(list(mat2coldict(M).values()))
def select_independent_vectors(): def select_random_vector_pairs(): return [(a0, b0)] + [(getRandomVector(), getRandomVector()) for i in range(4)] while True: pairs = select_random_vector_pairs() if all( [is_independent(list(sum(x, ()))) for x in combinations(pairs, 3)]): return pairs
def find_base_vecs(): combs = {(i, j, k) for i in range(8) for j in range(8) for k in range(8)} while True: vecs = [rand_vec() for i in range(8)] for comb in combs: if not is_independent( {vecs[comb[0]], vecs[comb[1]], vecs[comb[2]]}): continue return vecs
def task2(): row = [(a0, b0)] + [ (list2vec([randGF2() for i in range(6)]), list2vec([randGF2() for i in range(6)])) for j in range(4) ] while not all(is_independent(list(sum(x, ()))) for x in combinations(row, 3)): row = [(a0, b0)] + [ (list2vec([randGF2() for i in range(6)]), list2vec([randGF2() for i in range(6)])) for j in range(4) ] return row
def gen(): while True: vecs = [(a0, b0)] while len(vecs) < 5: vecs.append((Vec(a0.D, {i: randGF2() for i in range(6)}), Vec(a0.D, {i: randGF2() for i in range(6)}))) if all( is_independent(list(sum(x, ()))) for x in combinations(vecs, 3)): return vecs
def is_invertible(M): ''' input: A matrix, M outpit: A boolean indicating if M is invertible. >>> M = Mat(({0, 1, 2, 3}, {0, 1, 2, 3}), {(0, 1): 0, (1, 2): 1, (3, 2): 0, (0, 0): 1, (3, 3): 4, (3, 0): 0, (3, 1): 0, (1, 1): 2, (2, 1): 0, (0, 2): 1, (2, 0): 0, (1, 3): 0, (2, 3): 1, (2, 2): 3, (1, 0): 0, (0, 3): 0}) >>> is_invertible(M) True ''' if len(M.D[0]) != len(M.D[1]): return False cols = [v for v in mat2coldict(M).values()] return is_independent(cols)
def choose_secret(): while True: secret_a1 = randVector() secret_b1 = randVector() secret_a2 = randVector() secret_b2 = randVector() secret_a3 = randVector() secret_b3 = randVector() secret_a4 = randVector() secret_b4 = randVector() vecs = [(secret_a0, secret_b0),(secret_a1,secret_b1),(secret_a2,secret_b2),(secret_a3,secret_b3),(secret_a4,secret_b4)] if all(is_independent(list(sum(x,()))) for x in combinations(vecs,3)): return vecs
def problem2(): cont = 0 while True: a1 = randomvector() b1 = randomvector() a2 = randomvector() b2 = randomvector() a3 = randomvector() b3 = randomvector() a4 = randomvector() b4 = randomvector() #lista = [a0,b0,a1,b1,a2,b2,a3,b3,a4,b4] #lista = [a0,b0,a1,b1,a2,b2] cond1 = is_independent([a1,b1,a2,b2,a3,b3]) cond2 = is_independent([a1,b1,a2,b2,a4,b4]) cond3 = is_independent([a1,b1,a3,b3,a4,b4]) cond4 = is_independent([a2,b2,a3,b3,a4,b4]) cond10 = is_independent([a0,b0,a1,b1,a2,b2]) cond20 = is_independent([a0,b0,a1,b1,a3,b3]) cond30 = is_independent([a0,b0,a1,b1,a4,b4]) cond40 = is_independent([a0,b0,a2,b2,a3,b3]) cond50 = is_independent([a0,b0,a2,b2,a4,b4]) cond60 = is_independent([a0,b0,a3,b3,a4,b4]) cont += 1 if cont % 50000 == 0: print(cont) if cond1 and cond2 and cond3 and cond4 and cond10 and cond20 and cond30 and cond40 and cond50 and cond60: break print('secret_a0 = Vec({}, {})'.format(a0.D, a0.f)) print('secret_b0 = Vec({}, {})'.format(b0.D, b0.f)) print('secret_a1 = Vec({}, {})'.format(a1.D, a1.f)) print('secret_b1 = Vec({}, {})'.format(b1.D, b1.f)) print('secret_a2 = Vec({}, {})'.format(a2.D, a2.f)) print('secret_b2 = Vec({}, {})'.format(b2.D, b2.f)) print('secret_a3 = Vec({}, {})'.format(a3.D, a3.f)) print('secret_b3 = Vec({}, {})'.format(b3.D, b3.f)) print('secret_a4 = Vec({}, {})'.format(a4.D, a4.f)) print('secret_b4 = Vec({}, {})'.format(b4.D, b4.f))
def pick_known_vectors(a, b): all = [ (a,b),None,None,None,None ] while True: for i in range(1,5): all[i] = (list2vec([ randGF2() for i in range(6) ]), list2vec([ randGF2() for i in range(6) ])) ok = True for x,y,z in combinations(range(5), 3): L = [ all[x][0], all[x][1], all[y][0], all[y][1], all[z][0], all[z][1] ] if not is_independent(L): ok = False break if ok: return all
def task2(): row = [(a0,b0)] + [ ( list2vec([randGF2() for i in range(6) ]), list2vec([randGF2() for i in range(6) ]) ) for j in range(4) ] while not all(is_independent(list(sum(x,()))) for x in combinations(row,3)): row = [(a0,b0)] + [ ( list2vec([randGF2() for i in range(6) ]), list2vec([randGF2() for i in range(6) ]) ) for j in range(4) ] return row
def is_invertible(M): ''' input: A matrix, M outpit: A boolean indicating if M is invertible. >>> M = Mat(({0, 1, 2, 3}, {0, 1, 2, 3}), {(0, 1): 0, (1, 2): 1, (3, 2): 0, (0, 0): 1, (3, 3): 4, (3, 0): 0, (3, 1): 0, (1, 1): 2, (2, 1): 0, (0, 2): 1, (2, 0): 0, (1, 3): 0, (2, 3): 1, (2, 2): 3, (1, 0): 0, (0, 3): 0}) >>> is_invertible(M) True ''' from matutil import mat2coldict from independence import is_independent, rank vecDict = mat2coldict(M) vecList = [v for i,v in vecDict.items()] if (len(M.D[0]) == len(M.D[1])) and is_independent(vecList): return True return False
def is_invertible(M): ''' input: A matrix, M outpit: A boolean indicating if M is invertible. >>> M = Mat(({0, 1, 2, 3}, {0, 1, 2, 3}), {(0, 1): 0, (1, 2): 1, (3, 2): 0, (0, 0): 1, (3, 3): 4, (3, 0): 0, (3, 1): 0, (1, 1): 2, (2, 1): 0, (0, 2): 1, (2, 0): 0, (1, 3): 0, (2, 3): 1, (2, 2): 3, (1, 0): 0, (0, 3): 0}) >>> is_invertible(M) True >>> M1 = Mat(({0,1,2},{0,1,2}),{(0,0):1,(0,2):2,(1,2):3,(2,2):4}) >>> is_invertible(M1) False ''' matcols = mat2coldict(M) cols_as_set = {matcols[key] for key in matcols} return ( (len(M.D[0]) == len(M.D[1])) and is_independent(cols_as_set) )
def is_invertible(M): ''' input: A matrix, M outpit: A boolean indicating if M is invertible. >>> M = Mat(({0, 1, 2, 3}, {0, 1, 2, 3}), {(0, 1): 0, (1, 2): 1, (3, 2): 0, (0, 0): 1, (3, 3): 4, (3, 0): 0, (3, 1): 0, (1, 1): 2, (2, 1): 0, (0, 2): 1, (2, 0): 0, (1, 3): 0, (2, 3): 1, (2, 2): 3, (1, 0): 0, (0, 3): 0}) >>> is_invertible(M) True ''' from matutil import mat2coldict from independence import is_independent, rank vecDict = mat2coldict(M) vecList = [v for i, v in vecDict.items()] if (len(M.D[0]) == len(M.D[1])) and is_independent(vecList): return True return False
def subset_basis(T): ''' Input: - T: a set of Vecs Output: - set S containing Vecs from T that is a basis for Span T. Examples: The following tests use the procedure is_independent, provided in module independence >>> from vec import Vec >>> from independence import is_independent >>> a0 = Vec({'a','b','c','d'}, {'a':1}) >>> a1 = Vec({'a','b','c','d'}, {'b':1}) >>> a2 = Vec({'a','b','c','d'}, {'c':1}) >>> a3 = Vec({'a','b','c','d'}, {'a':1,'c':3}) >>> sb = subset_basis({a0, a1, a2, a3}) >>> len(sb) 3 >>> all(v in [a0, a1, a2, a3] for v in sb) True >>> is_independent(sb) True >>> b0 = Vec({0,1,2,3},{0:2,1:2,3:4}) >>> b1 = Vec({0,1,2,3},{0:1,1:1}) >>> b2 = Vec({0,1,2,3},{2:3,3:4}) >>> b3 = Vec({0,1,2,3},{3:3}) >>> sb = subset_basis({b0, b1, b2, b3}) >>> len(sb) 3 >>> all(v in [b0, b1, b2, b3] for v in sb) True >>> is_independent(sb) True >>> D = {'a','b','c','d'} >>> c0, c1, c2, c3, c4 = Vec(D,{'d': one, 'c': one}), Vec(D,{'d': one, 'a': one, 'c': one, 'b': one}), Vec(D,{'a': one}), Vec(D,{}), Vec(D,{'d': one, 'a': one, 'b': one}) >>> subset_basis({c0,c1,c2,c3,c4}) == {c0,c1,c2,c4} True ''' S = set() for v in T: S0 = S.union({v}) if is_independent(S0): S.add(v) return S
def pick_known_vectors(a, b): all = [(a, b), None, None, None, None] while True: for i in range(1, 5): all[i] = (list2vec([randGF2() for i in range(6)]), list2vec([randGF2() for i in range(6)])) ok = True for x, y, z in combinations(range(5), 3): L = [ all[x][0], all[x][1], all[y][0], all[y][1], all[z][0], all[z][1] ] if not is_independent(L): ok = False break if ok: return all
def getVecs(): while(True): a1 = list2vec([randGF2(), randGF2(), randGF2(), randGF2(), randGF2(), randGF2()]) b1 = list2vec([randGF2(), randGF2(), randGF2(), randGF2(), randGF2(), randGF2()]) a2 = list2vec([randGF2(), randGF2(), randGF2(), randGF2(), randGF2(), randGF2()]) b2 = list2vec([randGF2(), randGF2(), randGF2(), randGF2(), randGF2(), randGF2()]) a3 = list2vec([randGF2(), randGF2(), randGF2(), randGF2(), randGF2(), randGF2()]) b3 = list2vec([randGF2(), randGF2(), randGF2(), randGF2(), randGF2(), randGF2()]) a4 = list2vec([randGF2(), randGF2(), randGF2(), randGF2(), randGF2(), randGF2()]) b4 = list2vec([randGF2(), randGF2(), randGF2(), randGF2(), randGF2(), randGF2()]) vecs = [a0,b0,a1,b1,a2,b2,a3,b3,a4,b4] result = [is_independent([vecs[x],vecs[x+1],vecs[y],vecs[y+1],vecs[z],vecs[z+1]]) for x in range(0,10,2) for y in range(x+2,10,2) for z in range(y+2,10,2)] if all(result): return vecs
def subset_basis(T): ''' input: A list, T, of Vecs output: A list, S, containing Vecs from T, that is a basis for the space spanned by T. >>> a0 = Vec({'a','b','c','d'}, {'a':1}) >>> a1 = Vec({'a','b','c','d'}, {'b':1}) >>> a2 = Vec({'a','b','c','d'}, {'c':1}) >>> a3 = Vec({'a','b','c','d'}, {'a':1,'c':3}) >>> subset_basis([a0,a1,a2,a3]) == [Vec({'c', 'b', 'a', 'd'},{'a': 1}), Vec({'c', 'b', 'a', 'd'},{'b': 1}), Vec({'c', 'b', 'a', 'd'},{'c': 1})] True ''' result = [] for v in T: if is_independent(result+[v]): result.append(v) return result
def main(m): cont = 0 while True: cont += 1 print('... VUELTA {} ...'.format(cont)) vecs = list(range(64)) ab_list = [a0, b0] while True: if len(vecs) > 0: rnd = random.randint(0, len(vecs)-1) new_v = bin2vect(vecs.pop(rnd)) if is_independent(ab_list + [new_v]): ab_list.append(new_v) if len(ab_list) > m: return ab_list print('rnd:{}, len:{}, vecs:{}'.format(rnd, len(ab_list), len(vecs))) else: break
def subset_basis(T): """ input: A list, T, of Vecs output: A list, S, containing Vecs from T, that is a basis for the space spanned by T. >>> a0 = Vec({'a','b','c','d'}, {'a':1}) >>> a1 = Vec({'a','b','c','d'}, {'b':1}) >>> a2 = Vec({'a','b','c','d'}, {'c':1}) >>> a3 = Vec({'a','b','c','d'}, {'a':1,'c':3}) >>> subset_basis([a0,a1,a2,a3]) == [Vec({'c', 'b', 'a', 'd'},{'a': 1}), Vec({'c', 'b', 'a', 'd'},{'b': 1}), Vec({'c', 'b', 'a', 'd'},{'c': 1})] True """ ret = [] for i in T: ret.append(i) if not is_independent(ret): ret.pop() return ret
def subset_basis(T): ''' input: A list, T, of Vecs output: A list, S, containing Vecs from T, that is a basis for the space spanned by T. >>> a0 = Vec({'a','b','c','d'}, {'a':1}) >>> a1 = Vec({'a','b','c','d'}, {'b':1}) >>> a2 = Vec({'a','b','c','d'}, {'c':1}) >>> a3 = Vec({'a','b','c','d'}, {'a':1,'c':3}) >>> subset_basis([a0,a1,a2,a3]) == [Vec({'c', 'b', 'a', 'd'},{'a': 1}), Vec({'c', 'b', 'a', 'd'},{'b': 1}), Vec({'c', 'b', 'a', 'd'},{'c': 1})] True ''' from independence import is_independent S = [] for vector in T: if vector not in S: S.append(vector) if not is_independent(S): S.pop() return S
def my_rank(L): ''' Input: - L: a list of Vecs Output: - the rank of the list of Vecs Example: >>> L = [list2vec(v) for v in [[1,2,3],[4,5,6],[1.1,1.1,1.1]]] >>> my_rank(L) 2 >>> L == [list2vec(v) for v in [[1,2,3],[4,5,6],[1.1,1.1,1.1]]] True >>> my_rank([list2vec(v) for v in [[1,1,1],[2,2,2],[3,3,3],[4,4,4],[123,432,123]]]) 2 ''' basis = [] for v in L: if not is_independent(basis + [v]): continue basis.append(v) return len(basis)
uFound = True return u ## Problem 2 # Give each vector as a Vec instance secret_a0 = a0 secret_b0 = b0 secret_a1 = Vec(a0.D, {}) secret_b1 = Vec(a0.D, {}) secret_a2 = Vec(a0.D, {}) secret_b2 = Vec(a0.D, {}) secret_a3 = Vec(a0.D, {}) secret_b3 = Vec(a0.D, {}) secret_a4 = Vec(a0.D, {}) secret_b4 = Vec(a0.D, {}) vecList = [(secret_a1, secret_b1), (secret_a2, secret_b2), (secret_a3, secret_b3), (secret_a4, secret_b4)] imVecs = [(secret_a0, secret_b0)] notFound = True while notFound: for vecPair in vecList: for vec in vecPair: for i in range(6): vec[i] = randGF2() if all( is_independent(list(sum(x, ()))) for x in combinations(vecList + imVecs, 3)): notFound = False
while not uFound: u = list2vec([randGF2() for i in range(6)]) if a0*u == s and b0*u == t: uFound = True return u ## Problem 2 # Give each vector as a Vec instance secret_a0 = a0 secret_b0 = b0 secret_a1 = Vec(a0.D, {}) secret_b1 = Vec(a0.D, {}) secret_a2 = Vec(a0.D, {}) secret_b2 = Vec(a0.D, {}) secret_a3 = Vec(a0.D, {}) secret_b3 = Vec(a0.D, {}) secret_a4 = Vec(a0.D, {}) secret_b4 = Vec(a0.D, {}) vecList = [(secret_a1, secret_b1), (secret_a2, secret_b2), (secret_a3, secret_b3), (secret_a4, secret_b4)] imVecs = [(secret_a0, secret_b0)] notFound = True while notFound: for vecPair in vecList: for vec in vecPair: for i in range(6): vec[i] = randGF2() if all(is_independent(list(sum(x,()))) for x in combinations(vecList +imVecs,3)): notFound = False
def is_ok(U): vecs = [(U[i], U[i + 1]) for i in range(0, len(U) - 1, 2)] return all( is_independent(list(sum(x, ()))) for x in combinations(vecs, min(len(vecs), 3)))
def is_ok(U): vecs = [(U[i], U[i+1]) for i in range(0, len(U)-1, 2)] return all(is_independent(list(sum(x,()))) for x in combinations(vecs,min(len(vecs), 3)))
def independent_vecs(): while True: L = [randVec() for _ in range(8)] T = [(a0, b0), (L[0], L[1]), (L[2], L[3]), (L[4], L[5]), (L[6], L[7])] if all(is_independent(list(sum(x,()))) for x in combinations(T,3)): return L
def independent_vectors(vecs): return all(is_independent(x) for x in vecs)
def test_vecs( lst ): return all(is_independent(list(sum(v,()))) for v in combinations(lst, 3)) def gen_vec(): return list2vec( [ randGF2() for _ in range(6) ] )
def test(L): return all(is_independent(list(sum(x,()))) for x in combinations(L,3))
def allInd(vecs): return all(is_independent(list(sum(x, ()))) for x in combinations(vecs, 3))
# just initialize the values secret_a1 = a0 secret_b1 = a0 secret_a2 = a0 secret_b2 = a0 secret_a3 = a0 secret_b3 = a0 secret_a4 = a0 secret_b4 = a0 while True: secret_a1 = list2vec([randGF2(),randGF2(),randGF2(),randGF2(),randGF2(),randGF2()]) secret_b1 = list2vec([randGF2(),randGF2(),randGF2(),randGF2(),randGF2(),randGF2()]) secret_a2 = list2vec([randGF2(),randGF2(),randGF2(),randGF2(),randGF2(),randGF2()]) secret_b2 = list2vec([randGF2(),randGF2(),randGF2(),randGF2(),randGF2(),randGF2()]) vecs_3 = [(secret_a0, secret_b0),(secret_a1,secret_b1),(secret_a2,secret_b2)] if all(is_independent(list(sum(x,()))) for x in combinations(vecs_3,3)) == True: #print(vecs_3) break while True: secret_a3 = list2vec([randGF2(),randGF2(),randGF2(),randGF2(),randGF2(),randGF2()]) secret_b3 = list2vec([randGF2(),randGF2(),randGF2(),randGF2(),randGF2(),randGF2()]) vecs_4 = [(secret_a0, secret_b0),(secret_a1,secret_b1),(secret_a2,secret_b2),(secret_a3,secret_b3)] if all(is_independent(list(sum(x,()))) for x in combinations(vecs_4,3)) == True: #print(vecs_4) break while True: secret_a4 = list2vec([randGF2(),randGF2(),randGF2(),randGF2(),randGF2(),randGF2()]) secret_b4 = list2vec([randGF2(),randGF2(),randGF2(),randGF2(),randGF2(),randGF2()]) vecs_5 = [(secret_a0, secret_b0),(secret_a1,secret_b1),(secret_a2,secret_b2),(secret_a3,secret_b3),(secret_a4,secret_b4)] if all(is_independent(list(sum(x,()))) for x in combinations(vecs_5,3)) == True: #print(vecs_5)
2 >>> L == [list2vec(v) for v in [[1,2,3],[4,5,6],[1.1,1.1,1.1]]] True >>> my_rank([list2vec(v) for v in [[1,1,1],[2,2,2],[3,3,3],[4,4,4],[123,432,123]]]) 2 ''' pass L = [ list2vec(v) for v in [[1, 1, 1], [2, 2, 2], [3, 3, 3], [4, 4, 4], [123, 432, 123]] ] r = 0 for i, j in enumerate(L): print(i, L[0:i + 1], is_independent(L[0:i + 1])) if is_independent(L[0:i + 1]) == True: #print (L[0:i+1]) r += 1 #print (r) ## 9: (Problem 6.7.11) Direct Sum Unique Representation def direct_sum_decompose(U_basis, V_basis, w): ''' Input: - U_basis: a list of Vecs forming a basis for a vector space U - V_basis: a list of Vecs forming a basis for a vector space V - w: a Vec in the direct sum of U and V Output: - a pair (u, v) such that u + v = w, u is in U, v is in V
def get_vec(): return list2vec([randGF2() for x in range(6)]) def test(L): return all(is_independent(list(sum(x,()))) for x in combinations(L,3)) vlist = [(a0,b0)] while len(vlist) < 5: u = (get_vec(), get_vec()) if test(vlist + [u]): vlist.append(u) print(vlist) ## 2: (Task 7.7.2) Finding Secret Sharing Vectors # Give each vector as a Vec instance secret_a0 = list2vec([one, one, 0, one, 0, one]) secret_b0 = list2vec([one, one, 0, 0, 0, one]) secret_a1 = list2vec([one,one,one,0,0,one]) secret_b1 = list2vec([one,0,one,0,0,one]) secret_a2 = list2vec([one,0,one,0,one,one]) secret_b2 = list2vec([one,0,one,0,0,0]) secret_a3 = list2vec([one,one,one,one,0,0]) secret_b3 = list2vec([0,0,one,one,one,one]) secret_a4 = list2vec([0,one,0,one,one,0]) secret_b4 = list2vec([one,0,0,0,one,0]) vecs = [(secret_a0, secret_b0),(secret_a1,secret_b1),(secret_a2,secret_b2),(secret_a3,secret_b3),(secret_a4,secret_b4)] print((all(is_independent(list(sum(x,()))) for x in combinations(vecs,3))))
u = randGF2_6() while dot(a0, u) != s or dot(b0, u) != t: u = randGF2_6() return u print(choose_secret_vector(0, one)) ## 2: (Task 7.7.2) Finding Secret Sharing Vectors # Give each vector as a Vec instance secret_a0 = list2vec([one, one, 0, one, 0, one]) secret_b0 = list2vec([one, one, 0, 0, 0, one]) for i in range(8): a1, b1, a2, b2 = [randGF2_6(), randGF2_6(), randGF2_6(), randGF2_6()] while (not is_independent([secret_a0, secret_b0, a1, b1, a2, b2])): a1, b1, a2, b2 = [randGF2_6(), randGF2_6(), randGF2_6(), randGF2_6()] a3, b3, a4, b4 = [randGF2_6(), randGF2_6(), randGF2_6(), randGF2_6()] while (not is_independent([secret_a0, secret_b0, a1, b1, a2, b2])) or \ (not is_independent([secret_a0, secret_b0, a1, b1, a3, b3])) or \ (not is_independent([secret_a0, secret_b0, a1, b1, a4, b4])) or \ (not is_independent([secret_a0, secret_b0, a2, b2, a3, b3])) or \ (not is_independent([secret_a0, secret_b0, a2, b2, a4, b4])) or \ (not is_independent([secret_a0, secret_b0, a3, b3, a4, b4])) or \ (not is_independent([a1, b1, a2, b2, a3, b3])) or \ (not is_independent([a1, b1, a2, b2, a4, b4])) or \ (not is_independent([a1, b1, a3, b3, a4, b4])) or \ (not is_independent([a2, b2, a3, b3, a4, b4])): a3, b3, a4, b4 = [randGF2_6(), randGF2_6(), randGF2_6(), randGF2_6()]
def allInd(vecs): return all(is_independent(list(sum(x,()))) for x in combinations(vecs,3))