Skip to content

Commit

Permalink
Merge pull request #261 from jdhollis/add-EC_POINT_add-support
Browse files Browse the repository at this point in the history
Add EC_POINT_add support
  • Loading branch information
ioquatix authored Jul 4, 2019
2 parents 96efa29 + aba14fe commit 3b2126c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
29 changes: 29 additions & 0 deletions ext/openssl/ossl_pkey_ec.c
Original file line number Diff line number Diff line change
Expand Up @@ -1562,6 +1562,34 @@ ossl_ec_point_to_octet_string(VALUE self, VALUE conversion_form)
return str;
}

/*
* call-seq:
* point.add(point) => point
*
* Performs elliptic curve point addition.
*/
static VALUE ossl_ec_point_add(VALUE self, VALUE other)
{
EC_POINT *point_self, *point_other, *point_result;
const EC_GROUP *group;
VALUE group_v = rb_attr_get(self, id_i_group);
VALUE result;

GetECPoint(self, point_self);
GetECPoint(other, point_other);
GetECGroup(group_v, group);

result = rb_obj_alloc(cEC_POINT);
ossl_ec_point_initialize(1, &group_v, result);
GetECPoint(result, point_result);

if (EC_POINT_add(group, point_result, point_self, point_other, ossl_bn_ctx) != 1) {
ossl_raise(eEC_POINT, "EC_POINT_add");
}

return result;
}

/*
* call-seq:
* point.mul(bn1 [, bn2]) => point
Expand Down Expand Up @@ -1786,6 +1814,7 @@ void Init_ossl_ec(void)
/* all the other methods */

rb_define_method(cEC_POINT, "to_octet_string", ossl_ec_point_to_octet_string, 1);
rb_define_method(cEC_POINT, "add", ossl_ec_point_add, 1);
rb_define_method(cEC_POINT, "mul", ossl_ec_point_mul, -1);

id_i_group = rb_intern("@group");
Expand Down
16 changes: 16 additions & 0 deletions test/test_pkey_ec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,22 @@ def test_ec_point
assert_equal true, point.on_curve?
end

def test_ec_point_add
group = OpenSSL::PKey::EC::Group.new(:GFp, 17, 2, 2)
group.point_conversion_form = :uncompressed
gen = OpenSSL::PKey::EC::Point.new(group, B(%w{ 04 05 01 }))
group.set_generator(gen, 19, 1)

point_a = OpenSSL::PKey::EC::Point.new(group, B(%w{ 04 06 03 }))
point_b = OpenSSL::PKey::EC::Point.new(group, B(%w{ 04 10 0D }))

result = point_a.add(point_b)
assert_equal B(%w{ 04 0D 07 }), result.to_octet_string(:uncompressed)

assert_raise(TypeError) { point_a.add(nil) }
assert_raise(ArgumentError) { point_a.add }
end

def test_ec_point_mul
begin
# y^2 = x^3 + 2x + 2 over F_17
Expand Down

0 comments on commit 3b2126c

Please sign in to comment.