def Evaluate(pk,cl,ck,s={}): #S is for debugging (pk0,x,y)=pk cl0=[] for (i,j) in cl: cl0.append(i) c0 = SomeWhatEvaluate((pk0,x), cl0, ck) z=[None]*(Params.bigo+1) for i in range(1,Params.bigo+1): k=bigfloat.mul(c0,y[i],bigfloat.precision((Params.kappa+Params.gamma))) z[i]=float(bigfloat.mod(k,2,bigfloat.precision((Params.prec)))) if debug: su=0 yo=0 for i in range(1,Params.bigo+1): if s[i]==1: yo=bigfloat.add(yo,y[i],bigfloat.precision((Params.kappa+Params.gamma))) su=bigfloat.add(su,z[i],bigfloat.precision(Params.kappa+Params.gamma)) print "Enc_sum%2=",bigfloat.mod(su,8,bigfloat.precision((Params.prec+Params.gamma))) q=bigfloat.div(c0,globsk,bigfloat.precision(Params.kappa+Params.gamma)) print "(c0/sk)=",q q=bigfloat.mul(c0,yo,bigfloat.precision((Params.kappa+Params.gamma))) print "(c0*yo)=",q q=bigfloat.div(1,globsk,bigfloat.precision(Params.kappa+Params.gamma)) print "(1/sk)=",q print "(yo)=",yo print "(c0*1/sk)=",bigfloat.mul(q,c0,bigfloat.precision((Params.prec+Params.gamma))) q=bigfloat.div(c0,globsk,bigfloat.precision((Params.prec+Params.gamma))) print "(c0/sk)=",q c = (c0,z) return c
def appendZVector(self, c0): y = self.yvector z=[None]*(Params.bigo+1) for i in range(1,Params.bigo+1): k=bigfloat.mul(c0,y[i],bigfloat.precision(Params.gamma+Params.kappa)) z[i]=float(bigfloat.mod(k,2,bigfloat.precision(Params.prec))) # Sometimes mod goes wrong if z[i]>=2.0: z[i]=0 c = (c0,z) return c
def calcPhase(particle, detector, sim, waveLen): """Calculate the phase the particle has at the detector (current phase assumed to be 0)""" detCellIdx = getBinnedDetCellIdx(particle, detector, sim) #print("Target angles:", (detCellIdx - detector.size / 2) * detector.anglePerCell) dx = detector.distance + (sim.size[0] - particle.pos[0]) * sim.cellSize[0] dzdy = (detCellIdx - detector.size / 2) * detector.cellSize dydz = np.flipud(dzdy) + (sim.size[1:] / 2 - particle.pos[1:]) * sim.cellSize[1:] distance = bf.sqrt(dx**2 + dydz[0]**2 + dydz[1]**2) phase = 2 * bf.const_pi() / BigFloat(waveLen) * distance phase = bf.mod(phase, 2 * bf.const_pi()) return phase
def calcPhaseFarField(particle, sim, waveLen): """Calculate the phase the particle has at the detector in the far field""" # Only scatter in 1 direction assert (particle.directionAngles[0] == 0 or particle.directionAngles[1] == 0) # One of the sin is 0 -> Ok to use addition instead of a full projection pathDiff = particle.pos[1] * sim.cellSize[1] * bf.sin(particle.directionAngles[0]) + \ particle.pos[2] * sim.cellSize[2] * bf.sin(particle.directionAngles[1]) # Negate as increasing position decreases phase pathDiff = -pathDiff phase = 2 * bf.const_pi() / BigFloat(waveLen) * pathDiff phase = bf.mod(phase, 2 * bf.const_pi()) return phase
def Encrypt(pk,m,calcZ=True,s=None): #s is the secret key to be used for debugging purposes (pk0,x,y)=pk c0 = SomeWhatEncrypt((pk0,x), m) if calcZ: z=[None]*(Params.bigo+1) for i in range(1,Params.bigo+1): k=bigfloat.mul(c0,y[i],bigfloat.precision((Params.kappa+Params.gamma))) z[i]=float(bigfloat.mod(k,2.0,bigfloat.precision(Params.prec))) if z[i]>=2.0: z[i]=0 c = (c0,z) else: c = c0 if debug: su=0 for i in range(1,Params.bigo+1): if s and s[i]==1: su=bigfloat.add(su,z[i],bigfloat.precision(Params.kappa+Params.gamma)) print "Enc_sum%2=",bigfloat.mod(su,8,bigfloat.precision((Params.prec+Params.gamma))) q=bigfloat.div(c0,globsk,bigfloat.precision(Params.kappa+Params.gamma)) print "(Enc_c/sk)%2=",bigfloat.mod(q,8,bigfloat.precision((Params.prec+Params.gamma))) print "c0=",c0 return c
def Decrypt(sk,c,calcZ=True,sk1=1): #sk1 is for debugging purpose su=0 if calcZ: (c0,z) = c for i in range(1,Params.bigo+1): if sk[i]!=0: su=bigfloat.add(su,z[i],bigfloat.precision(Params.prec)) else: c0 = c if Keys.PK==None: print "Error: Z vector must be provided when public key not available" exit() y = Keys.PK[2] for i in range(1,Params.bigo+1): if sk[i]!=0: z = bigfloat.mul(c0,y[i],bigfloat.precision(Params.kappa)) z = float(bigfloat.mod(z,2,bigfloat.precision(Params.prec))) su=bigfloat.add(su,z,bigfloat.precision(Params.prec)) su=int(bigfloat.round(su)) m = (c0-su) % 2 return m
def EvaluateVector(pk,cl,ck,calcZ=True): (pk0,x0,y)=pk r=[] if calcZ: cl0=[] for (i,j) in cl: cl0.append(i) else: cl0=cl ck.setReductionVector(x0) #This is added to Trees constructor r0 = ck.eval(cl0) if calcZ: for c0 in r0: z=[None]*(Params.bigo+1) for i in range(1,Params.bigo+1): k=bigfloat.mul(c0,y[i],bigfloat.precision(Params.kappa)) z[i]=float(bigfloat.mod(k,2,bigfloat.precision(Params.prec))) c = (c0,z) r.append(c) else: r=r0 return r