Exemplo n.º 1
0
void newhope_sharedb(unsigned char *sharedkey, unsigned char *send, const unsigned char *received)
{
  poly sp, ep, v, a, pka, c, epp, bp;
  unsigned char seed[NEWHOPE_SEEDBYTES];
  unsigned char noiseseed[32];
  
  randombytes(noiseseed, 32);

  decode_a(&pka, seed, received);
  gen_a(&a, seed);

  poly_getnoise(&sp,noiseseed,0);
  poly_ntt(&sp);
  poly_getnoise(&ep,noiseseed,1);
  poly_ntt(&ep);

  poly_pointwise(&bp, &a, &sp);
  poly_add(&bp, &bp, &ep);
  
  poly_pointwise(&v, &pka, &sp);
  poly_bitrev(&v);
  poly_invntt(&v);

  poly_getnoise(&epp,noiseseed,2);
  poly_add(&v, &v, &epp);

  helprec(&c, &v, noiseseed, 3);

  encode_b(send, &bp, &c);
  
  rec(sharedkey, &v, &c);

  sha3256(sharedkey, sharedkey, 32);
}
Exemplo n.º 2
0
void newhope_keygen(unsigned char *send, poly *sk)
{
  poly a, e, r, pk;
  unsigned char seed[NEWHOPE_SEEDBYTES];
  unsigned char noiseseed[32];

  randombytes(seed, NEWHOPE_SEEDBYTES);
  randombytes(noiseseed, 32);

  gen_a(&a, seed); //unsigned

  poly_getnoise(sk,noiseseed,0);
  poly_ntt(sk); //unsigned
  
  poly_getnoise(&e,noiseseed,1);
  poly_ntt(&e); //unsigned

  poly_pointwise(&r,sk,&a); //unsigned
  poly_add(&pk,&e,&r); //unsigned
  encode_a(send, &pk, seed);
}
Exemplo n.º 3
0
void Compiler::handle_ik_a(const JInst& jinst) {

    if (jinst.opcode == OPCODE_IINC) {
        gen_iinc(jinst.op0, jinst.op1);
        return;
    }

    switch(jinst.opcode) {
    case OPCODE_LCMP:
        gen_x_cmp(jinst.opcode, i64);
        return;
    case OPCODE_FCMPL:
    case OPCODE_FCMPG:
        gen_x_cmp(jinst.opcode, flt32);
        return;
    case OPCODE_DCMPL:
    case OPCODE_DCMPG:
        gen_x_cmp(jinst.opcode, dbl64);
        return;
    default:    break;
    }

    jtype opnd = jvoid;
    JavaByteCodes inst = jinst.opcode;

    switch(jinst.opcode) {
    case OPCODE_IADD: opnd = i32; inst = OPCODE_IADD; break;
    case OPCODE_LADD: opnd = i64; inst = OPCODE_IADD; break;
    case OPCODE_FADD: opnd = flt32; inst = OPCODE_IADD; break;
    case OPCODE_DADD: opnd = dbl64; inst = OPCODE_IADD; break;

    case OPCODE_ISUB: opnd = i32; inst = OPCODE_ISUB; break;
    case OPCODE_LSUB: opnd = i64; inst = OPCODE_ISUB; break;
    case OPCODE_FSUB: opnd = flt32; inst = OPCODE_ISUB; break;
    case OPCODE_DSUB: opnd = dbl64; inst = OPCODE_ISUB; break;

    case OPCODE_IMUL: opnd = i32; inst = OPCODE_IMUL; break;
    case OPCODE_LMUL: opnd = i64; inst = OPCODE_IMUL; break;
    case OPCODE_FMUL: opnd = flt32; inst = OPCODE_IMUL; break;
    case OPCODE_DMUL: opnd = dbl64; inst = OPCODE_IMUL; break;

    case OPCODE_IDIV: opnd = i32; inst = OPCODE_IDIV; break;
    case OPCODE_LDIV: opnd = i64; inst = OPCODE_IDIV; break;
    case OPCODE_FDIV: opnd = flt32; inst = OPCODE_IDIV; break;
    case OPCODE_DDIV: opnd = dbl64; inst = OPCODE_IDIV; break;

    case OPCODE_IREM: opnd = i32; inst = OPCODE_IREM; break;
    case OPCODE_LREM: opnd = i64; inst = OPCODE_IREM; break;
    case OPCODE_FREM: opnd = flt32; inst = OPCODE_IREM; break;
    case OPCODE_DREM: opnd = dbl64; inst = OPCODE_IREM; break;

    case OPCODE_INEG: opnd = i32; inst = OPCODE_INEG; break;
    case OPCODE_LNEG: opnd = i64; inst = OPCODE_INEG; break;
    case OPCODE_FNEG: opnd = flt32; inst = OPCODE_INEG; break;
    case OPCODE_DNEG: opnd = dbl64; inst = OPCODE_INEG; break;

    case OPCODE_ISHL: opnd = i32; inst = OPCODE_ISHL; break;
    case OPCODE_LSHL: opnd = i64; inst = OPCODE_ISHL; break;

    case OPCODE_ISHR: opnd = i32; inst = OPCODE_ISHR; break;
    case OPCODE_LSHR: opnd = i64; inst = OPCODE_ISHR; break;

    case OPCODE_IUSHR: opnd = i32; inst = OPCODE_IUSHR; break;
    case OPCODE_LUSHR: opnd = i64; inst = OPCODE_IUSHR; break;

    case OPCODE_IAND: opnd = i32; inst = OPCODE_IAND; break;
    case OPCODE_LAND: opnd = i64; inst = OPCODE_IAND; break;

    case OPCODE_IOR: opnd = i32; inst = OPCODE_IOR; break;
    case OPCODE_LOR: opnd = i64; inst = OPCODE_IOR; break;

    case OPCODE_IXOR: opnd = i32; inst = OPCODE_IXOR; break;
    case OPCODE_LXOR: opnd = i64; inst = OPCODE_IXOR; break;
    default: assert(false); break;
    }

    if ((inst == OPCODE_IDIV || inst == OPCODE_IREM ) && 
        (opnd == i32 || opnd == i64)) {
        gen_check_div_by_zero(opnd, 0);
    }
    gen_a(inst, opnd);
}
Exemplo n.º 4
0
int main()
{
	const MKL_INT m = 1000;
	const MKL_INT k = 100000;
	const MKL_INT n = 1000;
	const MKL_INT nb = 100;
	const MKL_INT nprow = 2;
	const MKL_INT npcol = 2;

    MKL_INT iam, nprocs, ictxt, myrow, mycol;
    MDESC   descA, descB, descC, descA_local, descB_local, descC_local;
	MKL_INT info;
	MKL_INT a_m_local, a_n_local, b_m_local, b_n_local, c_m_local, c_n_local;
	MKL_INT a_lld, b_lld, c_lld;

    blacs_pinfo_( &iam, &nprocs );
    blacs_get_( &i_negone, &i_zero, &ictxt );
    blacs_gridinit_( &ictxt, "R", &nprow, &npcol );
    blacs_gridinfo_( &ictxt, &nprow, &npcol, &myrow, &mycol );

    double *a = 0;
    double *b = 0;
	double *c = 0;

    if (iam==0)
    {
		a = gen_a(m, k);
		b = gen_b(k, n);
		c = (double*)calloc(m*n, sizeof(double));

		puts("a=");
		print(a, m, k);

		puts("b=");
		print(b, k, n);
    }

    a_m_local = numroc_( &m, &nb, &myrow, &i_zero, &nprow );
    a_n_local = numroc_( &k, &nb, &mycol, &i_zero, &npcol );

	b_m_local = numroc_( &k, &nb, &myrow, &i_zero, &nprow );
	b_n_local = numroc_( &n, &nb, &mycol, &i_zero, &npcol );

    c_m_local = numroc_( &m, &nb, &myrow, &i_zero, &nprow );
	c_n_local = numroc_( &n, &nb, &mycol, &i_zero, &npcol );

    double *A = (double*) calloc( a_m_local * a_n_local, sizeof( double ) );
    double *B = (double*) calloc( b_m_local * b_n_local, sizeof( double ) );
    double *C = (double*) calloc( c_m_local * c_n_local, sizeof( double ) );

    a_lld = MAX( a_m_local, 1 );
	b_lld = MAX( b_m_local, 1 );
	c_lld = MAX( c_m_local, 1 );

	if (iam==0)
	{
			printf("a_m_local = %d\ta_n_local = %d\tb_m_local = %d\tb_n_local = %d\tc_m_local = %d\tc_n_local = %d\n", a_m_local, a_n_local, b_m_local, b_n_local,
							c_m_local, c_n_local);
			printf("a_lld = %d\tb_lld = %d\tc_lld = %d\n", a_lld, b_lld, c_lld);
	}

    descinit_( descA_local, &m, &k, &m, &k, &i_zero, &i_zero, &ictxt, &m, &info );
    descinit_( descB_local, &k, &n, &k, &n, &i_zero, &i_zero, &ictxt, &k, &info );
    descinit_( descC_local, &m, &n, &m, &n, &i_zero, &i_zero, &ictxt, &m, &info );

    descinit_( descA, &m, &k, &nb, &nb, &i_zero, &i_zero, &ictxt, &a_lld, &info );
    descinit_( descB, &k, &n, &nb, &nb, &i_zero, &i_zero, &ictxt, &b_lld, &info );
    descinit_( descC, &m, &n, &nb, &nb, &i_zero, &i_zero, &ictxt, &c_lld, &info );

	printf("Rank %d: start distribute data\n", iam);
    pdgeadd_( &trans, &m, &k, &one, a, &i_one, &i_one, descA_local, &zero, A, &i_one, &i_one, descA );
    pdgeadd_( &trans, &k, &n, &one, b, &i_one, &i_one, descB_local, &zero, B, &i_one, &i_one, descB );
	printf("Rank %d: finished distribute data\n", iam);

	if (iam==0)
	{
			puts("a");
			print(A, a_m_local, a_n_local);
			puts("b");
			print(B, b_m_local, b_n_local);
	}

    pdgemm_( "N", "N", &m, &n, &k, &one, A, &i_one, &i_one, descA, B, &i_one, &i_one, descB,
             &zero, C, &i_one, &i_one, descC );
	printf("Rank %d: finished dgemm\n", iam);
	if (iam == 0)
	{
			puts("c");
			print(C, c_m_local, c_n_local);
	}

	pdgeadd_( &trans, &m, &n, &one, C, &i_one, &i_one, descC, &zero, c, &i_one, &i_one, descC_local);

	if (iam==0)
	{
			puts("global c");
			print(c, m, n);
	}

	free(A);
	free(B);
	free(C);
	if (iam==0)
	{
			free(a);
			free(b);
			free(c);
	}

    blacs_gridexit_( &ictxt );
    blacs_exit_( &i_zero );
}