📄 randomly + static.c
字号:
if (curv->form)
SUMLOOP (i)
p3->x.e[i] = theta.e[i] ^ theta2.e[i] ^ x1.e[i] ^ curv->a2.e[i];
else
SUMLOOP (i)
p3->x.e[i] = theta.e[i] ^ theta2.e[i] ^ x1.e[i];
/* next find y_3 */
SUMLOOP (i) x1.e[i] = p1->x.e[i] ^ p3->x.e[i];
poly_mul( &x1, &theta, &theta2);
SUMLOOP (i) p3->y.e[i] = theta2.e[i] ^ p3->x.e[i] ^ p1->y.e[i];
}
void poly_edbl (p1, p3, curv)
POINT *p1, *p3;
CURVE *curv;
{
FIELD2N x1, y1, theta, theta2, t1;
INDEX i;
ELEMENT check;
check = 0;
SUMLOOP (i) check |= p1->x.e[i];
if (!check)
{
printf("doubling point at infinity\n");
null(&p3->x);
null(&p3->y);
return;
}
/* first compute theta = x + y/x */
poly_inv( &p1->x, &x1);
poly_mul( &x1, &p1->y, &y1);
SUMLOOP (i) theta.e[i] = p1->x.e[i] ^ y1.e[i];
/* next compute x_3 */
poly_mul( &theta, &theta, &theta2);
if(curv->form)
SUMLOOP (i) p3->x.e[i] = theta.e[i] ^ theta2.e[i] ^ curv->a2.e[i];
else
SUMLOOP (i) p3->x.e[i] = theta.e[i] ^ theta2.e[i];
/* and lastly y_3 */
theta.e[NUMWORD] ^= 1; /* theta + 1 */
poly_mul( &theta, &p3->x, &t1);
poly_mul( &p1->x, &p1->x, &x1);
SUMLOOP (i) p3->y.e[i] = x1.e[i] ^ t1.e[i];
}
/* subtract two points on a curve. just negates p2 and does a sum.
Returns p3 = p1 - p2 over curv.
*/
void poly_esub (p1, p2, p3, curv)
POINT *p1, *p2, *p3;
CURVE *curv;
{
POINT negp;
INDEX i;
copy ( &p2->x, &negp.x);
null (&negp.y);
SUMLOOP(i) negp.y.e[i] = p2->x.e[i] ^ p2->y.e[i];
poly_esum (p1, &negp, p3, curv);
}
/* need to move points around, not just values. Optimize later. */
void poly_elptic_mul(k, p, r, curv)
FIELD2N *k;
POINT *p, *r;
CURVE *curv;
{
char blncd[NUMBITS+1];
INDEX bit_count, i;
ELEMENT notzero;
FIELD2N number;
POINT temp;
/* make sure input multiplier k is not zero.
Return point at infinity if it is.
*/
copy( k, &number);
notzero = 0;
SUMLOOP (i) notzero |= number.e[i];
if (!notzero)
{
null (&r->x);
null (&r->y);
return;
}
bit_count = 0;
while (notzero)
{
if ( number.e[NUMWORD] & 1 )
{
blncd[bit_count] = 2 - (number.e[NUMWORD] & 3);
if ( blncd[bit_count] < 0 )
{
for (i=NUMWORD; i>=0; i--)
{
number.e[i]++;
if (number.e[i]) break;
}
}
}
else
blncd[bit_count] = 0;
number.e[NUMWORD] &= ~0 << 1;
rot_right( &number);
bit_count++;
notzero = 0;
SUMLOOP (i) notzero |= number.e[i];
}
bit_count--;
copy_point(p,r); /* first bit always set */
while (bit_count > 0)
{
poly_edbl(r, &temp, curv);
bit_count--;
switch (blncd[bit_count])
{
case 1: poly_esum (p, &temp, r, curv);
break;
case -1: poly_esub (&temp, p, r, curv);
break;
case 0: copy_point (&temp, r);
}
}
}
void print_field( string, x)
char *string;
FIELD2N *x;
{
INDEX i;
printf("%s\n",string);
SUMLOOP(i) printf("%8x ",x->e[i]);
printf("\n");
}
void print_point( string, point)
char *string;
POINT *point;
{
INDEX i;
printf("%s\n",string);
printf("x: ");
SUMLOOP(i) printf("%8x ",point->x.e[i]);
printf("\n");
printf("y: ");
SUMLOOP(i) printf("%8x ",point->y.e[i]);
printf("\n");
}
void print_curve( string, curv)
char *string;
CURVE *curv;
{
INDEX i;
printf("%s\n",string);
printf("form: %d\n",curv->form);
if (curv->form)
{
printf("a2: ");
SUMLOOP(i) printf("%lx ",curv->a2.e[i]);
printf("\n");
}
printf("a6: ");
SUMLOOP(i) printf("%lx ",curv->a6.e[i]);
printf("\n\n");
}
void poly_gf8( prime, g)
FIELD2N *prime, g[3];
{
FIELD2N gamma_matrix[NUMBITS], solve[NUMBITS];
FIELD2N power_table[4*NUMBITS], temp;
INDEX row, column, i, j, found, vector;
ELEMENT tobit, frombit, bit, null_check;
null( &power_table[0]);
power_table[0].e[NUMWORD] = 1L;
for (row = 1; row < 4*NUMBITS; row++)
mul_x_mod( &power_table[row-1], prime, &power_table[row]);
for( row=0; row < NUMBITS; row++)
{
copy( &power_table[row], &gamma_matrix[row]);
SUMLOOP (i) gamma_matrix[row].e[i] ^=
power_table[row<<1].e[i] ^ power_table[row<<2].e[i];
}
for ( row=0; row < NUMBITS; row++) null( &solve[row]);
for ( row=0; row < NUMBITS; row++)
{
bit = 1L << (row % WORDSIZE);
i = NUMWORD - (row/WORDSIZE);
frombit = 1;
j = NUMWORD;
for ( column = 0; column < NUMBITS; column++)
{
if (gamma_matrix[row].e[j] & frombit)
solve[column].e[i] |= bit;
frombit <<= 1;
if ( !frombit)
{
frombit = 1;
j--;
}
}
}
vector = 0; /* set up solution space */
null( &g[0]);
null( &g[1]);
for (column = NUMBITS - 1; column > 0; column--)
{
bit = 1L << (column % WORDSIZE);
i = NUMWORD - column/WORDSIZE;
if ( !(bit & solve[column].e[i]) )
{
/* go look for a bit set in this column */
found = 0;
for (row = column - 1; row >= 0; row--)
{
if ( bit & solve[row].e[i])
{
copy ( &solve[row], &temp);
copy ( &solve[column], &solve[row]);
copy ( &temp, &solve[column]);
found = 1;
break;
}
}
}
else found = 1;
/* and eliminate any set bits below it */
if (found)
{
for ( row = column-1; row >=0; row--)
{
if (solve[row].e[i] & bit)
SUMLOOP (j) solve[row].e[j] ^= solve[column].e[j];
}
}
else
{
null_check = 0;
SUMLOOP (j) null_check |= solve[column].e[j];
if ( null_check)
{
/* search for a null row to put here */
for ( row = column-1; row >=0; row--)
{
null_check = 0;
SUMLOOP (j) null_check |= solve[row].e[j];
if ( !null_check)
{
copy ( &solve[row], &temp);
copy ( &solve[column], &solve[row]);
copy ( &temp, &solve[column]);
break;
}
}
}
g[vector].e[i] |= bit;
vector++;
}
}
if (vector < 2) g[1].e[NUMWORD] = 1;
for ( row=1; row<NUMBITS; row++)
{
tobit = 1L << (row % WORDSIZE);
j = NUMWORD - (row/WORDSIZE);
if (solve[row].e[j] & tobit)
{
for (column = row-1; column >= 0; column--)
{
frombit = 1L << (column % WORDSIZE);
i = NUMWORD - column/WORDSIZE;
if ( solve[row].e[i] & frombit)
{
if (g[0].e[i] & frombit) g[0].e[j] ^= tobit;
if (g[1].e[i] & frombit) g[1].e[j] ^= tobit;
}
}
}
}
SUMLOOP (i) g[2].e[i] = g[0].e[i] ^ g[1].e[i];
}
/* random seed is accessable to everyone, not best way, but functional. */
unsigned long random_seed;
extern FIELD2N poly_prime;
/* below is from Mother code, till end of mother. Above is all my fault. */
#include <string.h>
static short mother1[10];
static short mother2[10];
static short mStart=1;
#define m16Long 65536L /* 2^16 */
#define m16Mask 0xFFFF /* mask for lower 16 bits */
#define m15Mask 0x7FFF /* mask for lower 15 bits */
#define m31Mask 0x7FFFFFFF /* mask for 31 bits */
#define m32Double 4294967295.0 /* 2^32-1 */
void Mother(unsigned long *pSeed)
{
unsigned long number,
number1,
number2;
short n,
*p;
unsigned short sNumber;
/* Initialize motheri with 9 random values the first time */
if (mStart) {
sNumber= *pSeed&m16Mask; /* The low 16 bits */
number= *pSeed&m31Mask; /* Only want 31 bits */
p=mother1;
for (n=18;n--;) {
number=30903*sNumber+(number>>16);
/* One line multiply-with-cary */
*p++=sNumber=number&m16Mask;
if (n==9)
p=mother2;
}
/* make cary 15 bits */
mother1[0]&=m15Mask;
mother2[0]&=m15Mask;
mStart=0;
}
/* Move elements 1 to 8 to 2 to 9 */
memmove(mother1+2,mother1+1,8*sizeof(short));
memmove(mother2+2,mother2+1,8*sizeof(short));
/* Put the carry values in numberi */
number1=mother1[0];
number2=mother2[0];
/* Form the linear combinations */
number1+=1941*mother1[2]+1860*mother1[3]+1812*mother1[4]+1776*mother1[5]+
1492*mother1[6]+1215*mother1[7]+1066*mother1[8]+12013*mother1[9];
number2+=1111*mother2[2]+2222*mother2[3]+3333*mother2[4]+4444*mother2[5]+
5555*mother2[6]+6666*mother2[7]+7777*mother2[8]+9272*mother2[9];
/* Save the high bits of numberi as the new carry */
mother1[0]=number1/m16Long;
mother2[0]=number2/m16Long;
/* Put the low bits of numberi into motheri[1] */
mother1[1]=m16Mask&number1;
mother2[1]=m16Mask&number2;
/* Combine the two 16 bit random numbers into one 32 bit */
*pSeed=(((long)mother1[1])<<16)+(long)mother2[1];
/* Return a double value between 0 and 1
return ((double)*pSeed)/m32Double; */
}
void random_field( value)
FIELD2N *value;
{
INDEX i;
SUMLOOP(i)
{
Mother( &random_seed);
value->e[i] = random_seed;
}
value->e[0] &= UPRMASK;
}
void static_field(value, select)
FIELD2N *value;
int select;
{
unsigned long fld1[1][6] = {{0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000011}};
unsigned long fld2[1][6] = {{0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x0000000D}};
unsigned long fld3[1][6] = {{0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x000000CA}};
if(select == 1)
copy(fld1[0], value->e);
else if(select == 2)
copy(fld2[0], value->e);
else if(select == 3)
copy(fld3[0], value->e);
}
void create_field(value, type, select)
FIELD2N *value;
{
if(type == 1)
random_field(value);
else if(type == 2)
static_field(value, select);
}
void rand_curve ( curv)
CURVE *curv;
{
curv->form = 1;
random_field( &curv->a6);
null( &curv->a2);
}
void static_curve ( curv)
CURVE *curv;
{
unsigned long crv2[2][6] = {{0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000001},
{0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000001}};
curv->form = 1;
copy(crv2[0], &curv->a2);
copy(crv2[1], &curv->a6);
}
void create_curve(curv, type)
CURVE *curv;
int type;
{
if(type == 1)
rand_curve(curv);
else if(type == 2)
static_curve(curv);
}
void rand_point( point, curve)
POINT *point;
CURVE *curve;
{
FIELD2N rf;
random_field( &rf);
poly_embed( &rf, curve, NUMWORD, rf.e[NUMWORD]&1, point);
}
void static_point(point, curve)
POINT *point;
CURVE *curve;
{
//FIELD2N rf;
//FIELD2N *d1, *d2;
unsigned long pnt2[2][6] = {{0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x000000DF},
{0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x000000E8}};
copy(pnt2[0], &point->x);
copy(pnt2[1], &point->y);
}
void create_point( point, curve, type)
POINT *point;
CURVE *curve;
int type;
{
if(type == 1)
rand_point( point, curve);
else if(type == 2)
static_point(point, curve);
}
void send_elgamal(
Base_point, Base_curve,
Their_public, raw_data,
Hidden_data, Random_point, type)
FIELD2N *raw_data;
POINT *Base_point, *Their_public, *Hidden_data, *Random_point;
CURVE *Base_curve;
int type;
{
FIELD2N random_value;
POINT hidden_point, raw_point;
create_field(random_value, type, 1);
poly_elptic_mul (&random_value, Base_point, Random_point, Base_curve);
poly_embed( raw_data, Base_curve, 0, 0, &raw_point);
poly_elptic_mul( &random_value, Their_public, &hidden_point, Base_curve);
poly_esum( &hidden_point, &raw_point, Hidden_data, Base_curve);
}
void receive_elgamal(
Base_point, Base_curve,
my_private, Hidden_data, Random_point,
raw_data)
FIELD2N *my_private, *raw_data;
POINT *Base_point, *Hidden_data, *Random_point;
CURVE *Base_curve;
{
POINT hidden_point, raw_point;
/* compute hidden point using my private key and the random point */
poly_elptic_mul( my_private, Random_point, &hidden_point, Base_curve);
poly_esub( Hidden_data, &hidden_point, &raw_point, Base_curve);
copy(&raw_point.x, raw_data);
}
CURVE Base_curve;
void print_dbl( string, field)
char *string;
DBLFIELD *field;
{
INDEX i;
printf("%s : ", string);
DBLLOOP(i) printf("%8x ", field->e[i]);
printf("\n");
}
extern FIELD2N poly_prime;
extern unsigned long random_seed;
CURVE ncrv = {1,
{0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000001},
{0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000001},
};
int main()
{
FIELD2N private1, private2, send_data, get_data;
CURVE Public_Curve;
POINT Base_Point, Their_public, Hidden_data, Random_point;
INDEX error;
int choice = 0, type = 0;
// FIELD2N poly_prime = {0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x000001C3};
while(1){
printf("ENTER YOUR CHOICE (1: Random, 2:Static)\n");
scanf("%d",&choice);
switch(choice){
case 1:
printf("RANDOM\n");
type = 1;
break;
case 2:
printf("STATIC\n");
type = 2;
break;
case 3:
return 0;
default:
break;
}
if (!irreducible(&poly_prime)) return(0);
print_field("poly_prime = ", &poly_prime);
if (error = init_poly_math())
{
printf("Can't initialize S matrix, row = %d\n", error);
return(-1);
}
print_curve("Curve = ", &ncrv);
random_seed = 0xDEADBEEF;
printf("create Base curve and point\n\n");
create_curve(&Public_Curve, type);
print_curve("Public curve", &Public_Curve);
create_point( &Base_Point, &Public_Curve, type);
print_point("Base point", &Base_Point);
printf("\ncreate side 2's private key \n\n");
create_field(&private2, type, 2);
print_field("Side 2 secret: ", &private2);
print_point("Base point", &Base_Point);
printf("\nGenerate side 2's public key\n\n");
poly_elptic_mul( &private2, &Base_Point, &Their_public, &Public_Curve);
print_point("Side 2 public key", &Their_public);
printf("\nCreate message data\n\n");
create_field(&send_data, type, 3);
printf("\nHide data on curve and send from side 1 to side 2\n\n");
send_elgamal( &Base_Point, &Public_Curve, &Their_public, &send_data, &Hidden_data, &Random_point, type);
print_point("Hidden data", &Hidden_data);
print_point("Random point", &Random_point);
printf("\nRecover transmitted message\n\n");
receive_elgamal( &Base_Point, &Public_Curve, &private2, &Hidden_data, &Random_point, &get_data);
print_field("sent data ", &send_data);
print_field("received data", &get_data);
getch();
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -