📄 setupserverm.nc
字号:
/* tab:4
*
*
* Copyright (C) 2004 by North Carolina State University.
* All rights reserved.
*
* Permission to use, copy, modify, and distribute this software and its
* documentation for any purpose, without fee, and without written agreement is
* hereby granted, provided that the above copyright notice, the following
* two paragraphs and the author appear in all copies of this software.
*
* IN NO EVENT SHALL THE NORTH CAROLINA STATE UNIVERSITY BE LIABLE TO ANY PARTY
* FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING
* OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE NORTH
* CAROLINA STATE UNIVERSITY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* THE NORTH CAROLINA STATE UNIVERSITY SPECIFICALLY DISCLAIMS ANY WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
* ON AN "AS IS" BASIS, AND THE NORTH CAROLINA STATE UNIVERSITY HAS NO OBLIGATION
* TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
*
* Authors: Rongfang Li
* Date: 4/25/04
*/
includes PolyOne;
module SetupServerM {
provides {
interface SetupServer;
}
uses {
interface Primitive;
interface Random as ceftRandom;
interface Random as pidRandom;
// interface Channel;
}
}
implementation {
uint16_t seed0[4];
uint8_t msg[29];
uint16_t node;
// the following function is to evaluate a bivariate polynomial, the parameter *b is a pointer that point
// the polynomial, the parameter x is the x of f(x,y), sm is result
result_t Af_poly16(uint16_t *b, uint16_t x, uint32_t *sm){ // before uint16_t *sm
uint16_t b2;
uint32_t s;
uint8_t i,j;
uint16_t md0,mdv;
sm[0]=0;sm[1]=0; // sm[0]:low, sm[1]:high
for(i=T+1;i>0;i--){
j=i-1;
// a(t)*x
if(sm[1]) s=(uint32_t )x<<16;
else s=sm[0]*x; // before s= x*sm[0]
md0=s&0xffff;
mdv=s>>16;
if(md0>=mdv) s=md0-mdv;
else s=65536uL+1+md0-mdv;
// a(t-1)+a(t)*x
if(b[j]!=0){ /* b[j] is between 1 and 65535 */
s = s+b[j];
}
else{ /* b[j] is 0 or 65536 */
b2=j>>4; /* j div 16 is the right shift j by 4 bits */
j=j&15; /* j mod 16, is the least 4 bits of j */
b2=b[T+1+b2]>>j; /* get the 9th bit of b[j] */
b2 = b2&1;
if(b2){ /* b[j] is 65536 */
s = s+65536uL;
}
/* else b[i-1]=0, nothing need to do. */
}
md0=s&0xffff;
mdv=s>>16;
if(md0>=mdv) s=md0-mdv;
else s=65536uL+1+md0-mdv;
sm[0]=s&0xffff;
sm[1]=s>>16;
}
return SUCCESS;
}
/* the following function is to generate a set of random numbers, which are the
coefficients of a bivariate t-degree polynomials f(x,y) */
void generate_cefts(BivariatePoly *bp, uint8_t *seed) {
uint16_t i,j;
uint16_t cefts[4];
uint16_t highs[4];
memset((uint8_t *)bp,0x00,sizeof(BivariatePoly));
for(i=0;i<=T;i++){
for(j=0;j<=i;j++){
call Primitive.PRF(seed, i*T+j, (uint8_t *)cefts);
call Primitive.PRF((uint8_t *)cefts, i*T+j,(uint8_t *)highs);
if(highs[0]==0) {highs[0]=1;cefts[0]=0;}
else highs[0]=0;
if(highs[1]==0) {highs[1]=1;cefts[1]=0;}
else highs[1]=0;
if(highs[2]==0) {highs[2]=1;cefts[2]=0;}
else highs[2]=0;
if(highs[3]==0) {highs[3]=1;cefts[3]=0;}
else highs[3]=0;
bp->poly_ceft1[i*(T+1+tt)+j] = cefts[0];
bp->poly_ceft2[i*(T+1+tt)+j] = cefts[1];
bp->poly_ceft3[i*(T+1+tt)+j] = cefts[2];
bp->poly_ceft4[i*(T+1+tt)+j] = cefts[3];
bp->poly_ceft1[i*(T+1+tt)+T+(j>>4)+1] += (highs[0]<<(j&15));
bp->poly_ceft2[i*(T+1+tt)+T+(j>>4)+1] += (highs[1]<<(j&15));
bp->poly_ceft3[i*(T+1+tt)+T+(j>>4)+1] += (highs[2]<<(j&15));
bp->poly_ceft4[i*(T+1+tt)+T+(j>>4)+1] += (highs[3]<<(j&15));
bp->poly_ceft1[j*(T+1+tt)+i] = bp->poly_ceft1[i*(T+1+tt)+j];
bp->poly_ceft2[j*(T+1+tt)+i] = bp->poly_ceft2[i*(T+1+tt)+j];
bp->poly_ceft3[j*(T+1+tt)+i] = bp->poly_ceft3[i*(T+1+tt)+j];
bp->poly_ceft4[j*(T+1+tt)+i] = bp->poly_ceft4[i*(T+1+tt)+j];
bp->poly_ceft1[j*(T+1+tt)+T+(i>>4)+1] += (highs[0]<<(i&15));
bp->poly_ceft2[j*(T+1+tt)+T+(i>>4)+1] += (highs[1]<<(i&15));
bp->poly_ceft3[j*(T+1+tt)+T+(i>>4)+1] += (highs[2]<<(i&15));
bp->poly_ceft4[j*(T+1+tt)+T+(i>>4)+1] += (highs[3]<<(i&15));
}
}
/* for(i=0;i<=T;i++){
for(j=0;j<=T+tt;j++){
dbg(DBG_USR1,"%X%X%X%X",bp->poly_ceft1[i*(T+1+tt)+j],bp->poly_ceft2[i*(T+1+tt)+j],bp->poly_ceft3[i*(T+1+tt)+j],bp->poly_ceft4[i*(T+1+tt)+j]);
}
dbg(DBG_USR1,"\n");
}
*/
// for(i=0;i<=T;i++){
// for(j=0;j<=T+tt;j++){
// dbg(DBG_USR1,"%d ",bp->poly_ceft4[i*(T+1+tt)+j]);
// }
// dbg(DBG_USR1,"\n\n\n");
// }
return ;
}
// the following function is to assign a polynomial share to a sensor, the parameter id is sensor id,
// bp_id is polynomial id, a is the address of the result
void ceft_assign(uint16_t id, uint8_t bp_id, PolyShare *a){
uint16_t i,t2,t3;
uint32_t mod[2];
BivariatePoly bp;
uint16_t cefts_seed[8];
// dbg(DBG_USR1,"seed=%X%X%X%X%X%X%X%X ",seed0[0],seed0[1],seed0[2],seed0[3],seed0[4],seed0[5],seed0[6],seed0[7]);
call Primitive.PRF((uint8_t *)seed0, bp_id, (uint8_t *)cefts_seed);
// dbg(DBG_USR1,"cefts_seed=%X%X%X%X%X%X%X%X\n",cefts_seed[0],cefts_seed[1],cefts_seed[2],cefts_seed[3],cefts_seed[4],cefts_seed[5],cefts_seed[6],cefts_seed[7]);
generate_cefts(&bp,(uint8_t *)cefts_seed);
bp.poly_id=bp_id;
a->poly_id=bp.poly_id;
for(i=0;i<=T;i++){
t2 = i&15;
t3 = (i>>4)+1;
Af_poly16(&(bp.poly_ceft1[i*(T+1+tt)]), id, mod);
a->poly_ceft1[i] = mod[0];
a->poly_ceft1[T+t3] += (mod[1]<<t2);
Af_poly16(&(bp.poly_ceft2[i*(T+1+tt)]), id, mod);
a->poly_ceft2[i] = mod[0];
a->poly_ceft2[T+t3] += (mod[1]<<t2);
Af_poly16(&(bp.poly_ceft3[i*(T+1+tt)]), id, mod);
a->poly_ceft3[i] = mod[0];
a->poly_ceft3[T+t3] += (mod[1]<<t2);
Af_poly16(&(bp.poly_ceft4[i*(T+1+tt)]), id, mod);
a->poly_ceft4[i] = mod[0];
a->poly_ceft4[T+t3] += (mod[1]<<t2);
}
return;
}
command result_t SetupServer.init() {
// uint16_t sm[2];
// uint32_t x[2];
call ceftRandom.init();
call pidRandom.init();
seed0[0]=call ceftRandom.rand();
seed0[1]=call ceftRandom.rand();
seed0[2]=call ceftRandom.rand();
seed0[3]=call ceftRandom.rand();
// dbg(DBG_USR1,"seed=%d %d %d %d\n",seed0[0],seed0[1],seed0[2],seed0[3]);
// sm[0]=45667;sm[1]=32185;
// x[0]=sm[0]+sm[1];
// x[1]=sm[0]*sm[1];
return SUCCESS;
}
// event result_t Channel.sendDone(uint8_t *msgP,result_t success){
// return SUCCESS;
// }
// event uint8_t* Channel.receive(uint8_t *msg){
// return msg;
// }
command result_t SetupServer.SecretAssign(uint16_t ID, uint8_t *secret) {
PolyShare *subset;
uint8_t i,n,j[ss];
bool same,same_one;
subset = (PolyShare *)secret;
for(i=0;i<ss;i++){
/* randomly generate j; */
same=1;
while(same==1){
j[i]= (call pidRandom.rand())%SF;
if(i>0) { n=0; same_one=0; /* j[i]?=j[1],j[2],...j[n] */
while(n<i&&!same_one){
if(j[i]==j[n]) same_one=1;
else same_one=0;
n++;
}
same=same_one;
}
else same=0;
}
// j[i] is the BivariatePoly id
ceft_assign(ID, j[i], &subset[i]);
dbg(DBG_USR1,"=%d ",subset[i].poly_id);
// dbg(DBG_USR1,"ceft4=%d %d %d %d",subset[0].poly_ceft4[0],subset[0].poly_ceft4[1],subset[0].poly_ceft4[2],subset[0].poly_ceft4[3]);
// memcpy(msg+2, (uint8_t *)&ID, 2);
// memcpy(msg+6, (uint8_t *)subset[0].poly_ceft4, 8);
// node=TOS_UART_ADDR;
// call Channel.send(node, msg);
}
dbg(DBG_USR1,"\n");
return SUCCESS;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -