/* * Completely rewritten in CryptKit-18, 13 Jan 1997, for new IEEE-style * curveParameters. */ int which_curve(giant x, curveParams *par) /* Returns (+-1) depending on whether x is on curve (+-)y^2 = x^3 + c x^2 + a x + b. */ { giant t1; giant t2; giant t3; int result; PROF_START; t1 = borrowGiant(par->maxDigits); t2 = borrowGiant(par->maxDigits); t3 = borrowGiant(par->maxDigits); /* First, set t2:= x^3 + c x^2 + a x + b. */ gtog(x, t2); addg(par->c, t2); mulg(x, t2); addg(par->a, t2); /* t2 := x^2 + c x + a. */ feemod(par, t2); mulg(x, t2); addg(par->b, t2); feemod(par, t2); /* Next, test whether t2 is a square. */ gtog(t2, t1); make_base(par, t3); iaddg(1, t3); gshiftright(1, t3); /* t3 = (p+1)/2. */ feepowermodg(par, t1, t3); /* t1 := t2^((p+1)/2) (mod p). */ if(gcompg(t1, t2) == 0) result = CURVE_PLUS; else result = CURVE_MINUS; returnGiant(t1); returnGiant(t2); returnGiant(t3); PROF_END(whichCurveTime); return result; }
bh_view Expander::make_temp(bh_type type, bh_index nelem) { bh_view view; view.base = make_base(type, nelem); view.start = 0; view.ndim = 1; view.shape[0] = nelem; view.stride[0] = 1; return view; }
/* Create a basic air critter */ static struct Critter *make_base_ac(SDL_Surface **gfx,float x,float y) { struct Critter *c = make_base(gfx); init_flyer(&c->flyer,AIRBORNE); c->type = AIRCRITTER; c->physics.x = x; c->physics.y = y; c->timer = 0; c->timerfunc = ac_searchtarget; return c; }
int my_putnbr_adress(unsigned long int nbr, int *count) { char *base; unsigned long int length; base = make_base("0123456789abcdef"); length = 16; if (nbr >= length) my_putnbr_adress(nbr / length, count); my_dischar(base[nbr % length], count); return (0); }
/* Create a basic water critter */ static struct Critter *make_base_wc(SDL_Surface **gfx,float x,float y) { struct Critter *c = make_base(gfx); init_flyer(&c->flyer,UNDERWATER); c->flyer.speed = 0.5; c->type = WATERCRITTER; c->physics.x = x; c->physics.y = y; c->timer = 0; c->timerfunc = ac_searchtarget; return c; }
/* Create a basic ground critter */ static struct Critter *make_base_gc(SDL_Surface **gfx,float x,float y) { struct Critter *c = make_base(gfx); c->type = GROUNDCRITTER; init_walker(&c->walker); c->physics.x = x; c->physics.y = y; c->physics.radius = 6; c->physics.mass = 12; c->walker.walking = rand()%2?-1:1; c->walker.walkspeed = 1; c->walker.slope = 5; c->ship = 0; c->timer = 1; c->timerfunc = gc_dosomething; c->die = gc_die; return c; }
/** * Expand matmul at the given pc in the bhir instruction list. * * Returns the number of additional instructions used. */ int Expander::expand_matmul(bh_ir& bhir, int pc) { int start_pc = pc; bh_instruction& composite = bhir.instr_list[pc]; // Lazy choice... no re-use just NOP it. composite.opcode = BH_NONE; // Grab operands bh_view out = composite.operand[0]; bh_view a = composite.operand[1]; bh_view b = composite.operand[2]; // Grab the shape int n = a.shape[0]; int m = a.shape[1]; int k = b.shape[1]; // Construct intermediary operands // Needs broadcast bh_view a_3d = a; // Needs transposition + broadcast bh_view b_3d = b; a_3d.ndim = 3; b_3d.ndim = 3; // Set the shape a_3d.shape[0] = b_3d.shape[0] = n; a_3d.shape[1] = b_3d.shape[1] = k; a_3d.shape[2] = b_3d.shape[2] = m; // Construct broadcast a_3d.stride[0] = a.stride[0]; a_3d.stride[1] = 0; a_3d.stride[2] = a.stride[1]; // Construct transpose + broadcast b_3d.stride[0] = 0; b_3d.stride[1] = b.stride[1]; b_3d.stride[2] = b.stride[0]; // Construct temp for mul-result bh_view c_3d = b_3d; // Count number of elements bh_intp nelements = 1; // Set contiguous stride for(bh_intp dim=c_3d.ndim-1; dim >= 0; --dim) { c_3d.stride[dim] = nelements; nelements *= c_3d.shape[dim]; } c_3d.start = 0; c_3d.base = make_base(b_3d.base->type, nelements); // Expand sequence inject(bhir, ++pc, BH_MULTIPLY, c_3d, a_3d, b_3d); inject(bhir, ++pc, BH_ADD_REDUCE, out, c_3d, (int64_t)2, BH_INT64); inject(bhir, ++pc, BH_FREE, c_3d); verbose_print("[Matmul] Expanding BH_MATMUL"); return pc - start_pc; }
bh_view Expander::make_temp(bh_view& meta, bh_type type, bh_index nelem) { bh_view view = meta; view.base = make_base(type, nelem); return view; }
static Uri make_base(const std::string& document) { return make_base(Uri{ document }); }
/* * Completely rewritten in CryptKit-18, 13 Jan 1997, for new IEEE-style * curveParameters. */ void elliptic_add(giant x1, giant x2, giant x3, curveParams *par, int s) { /* Addition algorithm for x3 = x1 + x2 on the curve, with sign ambiguity s. From theory, we know that if {x1,1} and {x2,1} are on a curve, then their elliptic sum (x1,1} + {x2,1} = {x3,1} must have x3 as one of two values: x3 = U/2 + s*Sqrt[U^2/4 - V] where sign s = +-1, and U,V are functions of x1,x2. Tho present function is called a maximum of twice, to settle which of +- is s. When a call is made, it is guaranteed already that x1, x2 both lie on the same curve (+- curve); i.e., which curve (+-) is not connected at all with sign s of the x3 relation. */ giant cur_n; giant t1; giant t2; giant t3; giant t4; giant t5; PROF_START; cur_n = borrowGiant(par->maxDigits); t1 = borrowGiant(par->maxDigits); t2 = borrowGiant(par->maxDigits); t3 = borrowGiant(par->maxDigits); t4 = borrowGiant(par->maxDigits); t5 = borrowGiant(par->maxDigits); if(gcompg(x1, x2)==0) { int_to_giant(1, t1); numer_double(x1, t1, x3, par); denom_double(x1, t1, t2, par); binvg_cp(par, t2); mulg(t2, x3); feemod(par, x3); goto out; } numer_plus(x1, x2, t1, par); int_to_giant(1, t3); numer_times(x1, t3, x2, t3, t2, par); int_to_giant(1, t4); int_to_giant(1, t5); denom_times(x1, t4, x2, t5, t3, par); binvg_cp(par, t3); mulg(t3, t1); feemod(par, t1); /* t1 := U/2. */ mulg(t3, t2); feemod(par, t2); /* t2 := V. */ /* Now x3 will be t1 +- Sqrt[t1^2 - t2]. */ gtog(t1, t4); gsquare(t4); feemod(par, t4); subg(t2, t4); make_base(par, cur_n); iaddg(1, cur_n); gshiftright(2, cur_n); /* cur_n := (p+1)/4. */ feepowermodg(par, t4, cur_n); /* t4 := t2^((p+1)/4) (mod p). */ gtog(t1, x3); if(s != SIGN_PLUS) negg(t4); addg(t4, x3); feemod(par, x3); out: returnGiant(cur_n); returnGiant(t1); returnGiant(t2); returnGiant(t3); returnGiant(t4); returnGiant(t5); PROF_END(ellAddTime); }