void calc_light(t_param *param, t_info *info, t_list *spot) { t_info *light; t_spot *o_spot; t_lum lum; int *s_color; if (info->distance < 0) return ; s_color = init_color(); while (spot) { o_spot = (t_spot *)spot->content; light = init_light(info, o_spot); calc_intersection(param, light); if (point_cmp(info->r_pos, light->r_pos) == 1) { lum.fading = ft_abs(calc_fading(light->r_line.vec, info->vec_n)); lum.shining = ft_abs(calc_shining(info->vec_n, light->r_line.vec)); info->light += o_spot->value * lum.fading; info->light += o_spot->value * lum.shining * lum.fading; calc_color(&s_color, o_spot->color, o_spot->value, lum.fading); } spot = spot->next; } info->color = retrieve_col(s_color, damer(param, info, info->s_pos), get_shine(info)); }
//============================================ // MAP to POINT テスト //============================================ void test_map_to_point(const EC_GROUP ec) { int i; unsigned long long int t1, t2; EC_POINT P, Q; point_init(P, ec); point_init(Q, ec); point_map_to_point(P, MAP_STR, sizeof(MAP_STR), t); assert(point_is_on_curve(P)); point_mul(Q, ec->order, P); assert(point_is_infinity(Q)); point_map_to_point(Q, MAP_STR, sizeof(MAP_STR), t); assert(point_cmp(Q, P) == 0); t1 = rdtsc(); for (i = 0; i < M; i++) { point_map_to_point(P, MAP_STR, sizeof(MAP_STR), t); } t2 = rdtsc(); printf("point map to point in 128 security: %.2lf [clock]\n", (double)(t2 - t1) / M); point_clear(P); point_clear(Q); }
int point_list_query(point_t *p, point_list_t *list) { int i; dlink_t *x; assert(p); assert(list); i = 0; for (x = list->tail->next; x != list->head; x = x->next) { if (point_cmp(p, (point_t *)x->object) == 0) return i; i++; } return -1; }
static int curve_cmp(element_ptr a, element_ptr b) { if (a == b) { return 0; } else { // If we're working with a quotient group we must account for different // representatives of the same coset. curve_data_ptr cdp = (curve_data_ptr)a->field->data; if (cdp->quotient_cmp) { element_t e; element_init_same_as(e, a); element_div(e, a, b); element_pow_mpz(e, e, cdp->quotient_cmp); int result = !element_is1(e); element_clear(e); return result; } return point_cmp((point_ptr)a->data, (point_ptr)b->data); } }
//============================================ // 楕円曲線の演算テスト //============================================ void test_arithmetic_operation(const EC_GROUP ec) { int i; unsigned long long int t1, t2; EC_POINT a, b, c, d; Element dx, dy; mpz_t scalar; //------------------- // init //------------------- point_init(a, ec); point_init(b, ec); point_init(c, ec); point_init(d, ec); //------------------- // random //------------------- point_random(a); assert(point_is_on_curve(a)); t1 = rdtsc(); for (i = 0; i < M; i++) { point_random(a); } t2 = rdtsc(); printf("point random: %.2lf [clock]\n", (double)(t2 - t1) / M); //------------------- // add/dob //------------------- point_add(b, b, a); point_add(c, b, c); assert(point_cmp(b, a) == 0); assert(point_cmp(c, b) == 0); point_set_infinity(d); point_dob(d, d); assert(point_is_infinity(d)); point_set_str(a, "[6D2E4115FA177379A504A0EE4EF53767DE51C6364AAB69D4064529EC1FD047A 635B2C858AA4F4A3DB8AA17A588B037CAFFD36678F76E3F3369DFC90C6878C7,193E877C82EFCA81EC2815906630B837BBC6976CC8A7958E6A40D1B190FF2E5F E8A77E88AFCEE9F806DC15BF50EADD138320F1A5A87E78DDE86FA7A867300D]"); point_set_str(b, "[1A8F5DAB09EE4290F95FE4C824C153E355D55B6CF94B998C6203FEC3D81377CF 15A19F2704C4BDBAAE39A5E26772A3E4E7EC7A9E205651F8822298766DE044FF,1C566EB3917F06B05E0A786BD8030CAFCCDB62864DD0E2A22A9B6817B310FD53 6A0927BB33EB263F45CAB921A20E67A1BD8A791D6EB0415AC92C9B1F74D16D1]"); point_set_str(d, "[143D414F99AA18C844B331064C9DD66363EBA3D852250CBCF8C9D4B33E0C4C1C 225865D85EC7A34647CA55E026BD1FA201E0C4E8C66F7A43E69AF708F410A0FF,FACA1388C034CF614A72E06EE60DEDC4880CDBD368E5BEC2795130B266FFB9E 1681217E50705AB9A21FEB62E0BF9A5657EB27C3AED3323FE9C57058358735A9]"); point_add(c, a, b); assert(point_cmp(c, d) == 0); t1 = rdtsc(); for (i = 0; i < N; i++) { point_add(c, a, b); } t2 = rdtsc(); printf("point add: %.2lf [clock]\n", (double)(t2 - t1) / N); element_init(dx, ec->field); element_init(dy, ec->field); element_set_str(dx, "33F550F9A63EF53C786BF7BDFDAB1538CD76A3FCED3C9DBC3307DD4F354775A C814AE99C91C71845F0B51E4349520908E48C70181313D70C05F6ED24EC1F36"); element_set_str(dy, "3766ED0DD7C988DB76770081A298DAA924D0E3279726F9B5504129AFA3E57B9 520CE8A563F88AF882AB99086BFDBBDCEF9DE65879AB234DFF5AAFD5BEE7E4F"); point_set_xy(d, dx, dy); point_dob(c, a); assert(point_cmp(c, d) == 0); t1 = rdtsc(); for (i = 0; i < N; i++) { point_dob(c, a); } t2 = rdtsc(); printf("point dob: %.2lf [clock]\n", (double)(t2 - t1) / N); element_clear(dx); element_clear(dy); //------------------ // neg/sub //------------------ point_neg(c, a); point_add(d, a, c); point_sub(b, a, a); assert(point_is_infinity(d)); assert(point_is_infinity(b)); //------------------- // mul //------------------- mpz_init(scalar); mpz_set(scalar, ec->order); for (i = 0; i < 100; i++) { point_random(a); point_mul(b, scalar, a); ec_bn254_fp2_mul_end(c, scalar, a); assert(point_is_infinity(b)); assert(point_cmp(c, b) == 0); } t1 = rdtsc(); for (i = 0; i < M; i++) { point_mul(b, scalar, a); } t2 = rdtsc(); printf("point mul with endomorphism: %.2lf [clock]\n", (double)(t2 - t1) / M); t1 = rdtsc(); for (i = 0; i < M; i++) { ec_bn254_fp2_mul(b, scalar, a); } t2 = rdtsc(); printf("point mul with binary method: %.2lf [clock]\n", (double)(t2 - t1) / M); mpz_clear(scalar); //------------------- // clear //------------------- point_clear(a); point_clear(b); point_clear(c); point_clear(d); }
//============================================ // 楕円曲線の入出力テスト //============================================ void test_io(const EC_GROUP ec) { int i; unsigned long long int t1, t2; EC_POINT P, Q, R; size_t osize; unsigned char os[130]; char str[262]; point_init(P, ec); point_init(Q, ec); point_init(R, ec); //--------------------- // octet string //--------------------- point_set_infinity(R); point_to_oct(os, &osize, R); point_from_oct(Q, os, osize); assert(point_is_infinity(Q)); for (i = 0; i < 100; i++) { point_random(P); point_to_oct(os, &osize, P); point_from_oct(Q, os, osize); assert(point_cmp(P, Q) == 0); } t1 = rdtsc(); for (i = 0; i < N; i++) { point_to_oct(os, &osize, P); } t2 = rdtsc(); printf("point to octet string: %.2lf [clock]\n", (double)(t2 - t1) / N); t1 = rdtsc(); for (i = 0; i < N; i++) { point_from_oct(Q, os, osize); } t2 = rdtsc(); printf("point from octet string: %.2lf [clock]\n", (double)(t2 - t1) / N); //--------------------- // string //--------------------- point_set_infinity(R); point_get_str(str, R); point_set_str(Q, str); assert(point_is_infinity(Q)); for (i = 0; i < 100; i++) { point_get_str(str, P); point_set_str(Q, str); assert(point_cmp(P, Q) == 0); } t1 = rdtsc(); for (i = 0; i < N; i++) { point_get_str(str, P); } t2 = rdtsc(); printf("point get string: %.2lf [clock]\n", (double)(t2 - t1) / N); t1 = rdtsc(); for (i = 0; i < N; i++) { point_set_str(Q, str); } t2 = rdtsc(); printf("point set string: %.2lf [clock]\n", (double)(t2 - t1) / N); point_clear(P); point_clear(Q); point_clear(R); }