【Python】Coding the Matrix:Week 6 Secret Sharing Lab

这个Problem 2 有点困惑的,我通过程序获得了一组3对线性独立的向量,但是任然不过,过会再继续,但是代码的原理应该是可以的。

choose_rand_vector()返回一个符合要求3个tuple的list

choose_rand_vector(randNum=4,vecLength=6):
choose_pair()验证所有的vector组合是否满足6个向量相互线性独立的要求

def choose_pair():

# version code 988
# Please fill out this stencil and submit using the provided submission script.

import random
from GF2 import one
from vecutil import list2vec
from independence import *
from vec import *

from itertools import *



## Problem 1
def randGF2(): return random.randint(0,1)*one

a0 = list2vec([one, one,   0, one,   0, one])
b0 = list2vec([one, one,   0,   0,   0, one])

def choose_secret_vector(s,t):
    while True:
        u=list2vec([randGF2()  for i in range(len(a0.D))])
        if a0*u==s and b0*u==t:
            return u
            



## Problem 2
# 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([0, 0, 0, 0, one, 0])
secret_b1 = list2vec([one, one, 0, one, 0, 0])
secret_a2 = list2vec([0, one, 0, one, 0, one])
secret_b2 = list2vec([0, 0, one, 0, one, 0])
secret_a3 = list2vec([one, 0, 0, 0, one, 0])
secret_b3 = list2vec([0, one, one, 0, one, one])
secret_a4 = list2vec([one, one, one, one, 0, 0])
secret_b4 = list2vec([one, 0, one, one, 0, one])

def choose_rand_vector(randNum=4,vecLength=6):
    r=[]
    r.append(secret_a0)
    r.append(secret_b0)
    while randNum>0:
        u=list2vec([randGF2() for i in range(vecLength)])
        r.append(u)
        if is_independent(r):randNum-=1
        else:r.pop()
    return [(r[0],r[1]),(r[2],r[3]),(r[4],r[5])]
    
def choose_pair():
    r=[]
    while True:
        r1=choose_rand_vector(randNum=4,vecLength=6)
        r2=choose_rand_vector(randNum=4,vecLength=6)
        r3=r1+r2
        r3.remove(r1[0])
        r3.remove(r2[0])   
        r4=list(permutations(r3,2))
        r5=[list(r1[0])+list(x[0])+list(x[1]) for x in r4]
        for i in r5:
            if not is_independent(i):break
        return r3
 
'''    
k=choose_pair()
z=[]
k=[list(x) for x in k]
#print(k)
[print(x[0].f.values(),x[1].f.values()) for x in k]
#[print(list(a.f.values())) for a in r]
'''


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值