Class: Secp256k1::Signature
- Inherits:
-
Object
- Object
- Secp256k1::Signature
- Defined in:
- ext/rbsecp256k1/rbsecp256k1.c
Class Method Summary collapse
-
.from_compact(in_compact_signature) ⇒ Secp256k1::Signature
Deserializes a Signature from 64-byte compact signature data.
-
.from_der_encoded(in_der_encoded_signature) ⇒ Secp256k1::Signature
Converts a DER encoded binary signature into a signature object.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Compares two signatures.
-
#compact ⇒ String
Returns the 64 byte compact representation of this signature.
-
#der_encoded ⇒ String
Return Distinguished Encoding Rules (DER) encoded signature data.
-
#normalized ⇒ Array
Returns the normalized lower-S form of this signature.
Class Method Details
.from_compact(in_compact_signature) ⇒ Secp256k1::Signature
Deserializes a Signature from 64-byte compact signature data.
702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 |
# File 'ext/rbsecp256k1/rbsecp256k1.c', line 702
static VALUE
Signature_from_compact(VALUE klass, VALUE in_compact_signature)
{
Signature *signature;
VALUE signature_result;
unsigned char *signature_data;
Check_Type(in_compact_signature, T_STRING);
if (RSTRING_LEN(in_compact_signature) != 64)
{
rb_raise(Secp256k1_Error_class, "compact signature must be 64 bytes");
}
signature_data = (unsigned char*)StringValuePtr(in_compact_signature);
signature_result = Signature_alloc(Secp256k1_Signature_class);
TypedData_Get_Struct(signature_result, Signature, &Signature_DataType, signature);
if (secp256k1_ecdsa_signature_parse_compact(secp256k1_context_no_precomp,
&(signature->sig),
signature_data) != 1)
{
rb_raise(Secp256k1_DeserializationError_class, "invalid compact signature");
}
return signature_result;
}
|
.from_der_encoded(in_der_encoded_signature) ⇒ Secp256k1::Signature
Converts a DER encoded binary signature into a signature object.
740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 |
# File 'ext/rbsecp256k1/rbsecp256k1.c', line 740
static VALUE
Signature_from_der_encoded(VALUE klass, VALUE in_der_encoded_signature)
{
Signature *signature;
VALUE signature_result;
unsigned char *signature_data;
Check_Type(in_der_encoded_signature, T_STRING);
signature_data = (unsigned char*)StringValuePtr(in_der_encoded_signature);
signature_result = Signature_alloc(Secp256k1_Signature_class);
TypedData_Get_Struct(signature_result, Signature, &Signature_DataType, signature);
if (secp256k1_ecdsa_signature_parse_der(secp256k1_context_no_precomp,
&(signature->sig),
signature_data,
RSTRING_LEN(in_der_encoded_signature)) != 1)
{
rb_raise(Secp256k1_DeserializationError_class, "invalid DER encoded signature");
}
return signature_result;
}
|
Instance Method Details
#==(other) ⇒ Boolean
Compares two signatures.
Two signatures are equal if their compact encodings are identical.
872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 |
# File 'ext/rbsecp256k1/rbsecp256k1.c', line 872
static VALUE
Signature_equals(VALUE self, VALUE other)
{
Signature *lhs;
Signature *rhs;
unsigned char lhs_compact[64];
unsigned char rhs_compact[64];
TypedData_Get_Struct(self, Signature, &Signature_DataType, lhs);
TypedData_Get_Struct(other, Signature, &Signature_DataType, rhs);
secp256k1_ecdsa_signature_serialize_compact(
secp256k1_context_no_precomp, lhs_compact, &(lhs->sig)
);
secp256k1_ecdsa_signature_serialize_compact(
secp256k1_context_no_precomp, rhs_compact, &(rhs->sig)
);
if (memcmp(lhs_compact, rhs_compact, 64) == 0)
{
return Qtrue;
}
return Qfalse;
}
|
#compact ⇒ String
Returns the 64 byte compact representation of this signature.
800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 |
# File 'ext/rbsecp256k1/rbsecp256k1.c', line 800
static VALUE
Signature_compact(VALUE self)
{
// TODO: Cache value after first computation
Signature *signature;
unsigned char compact_signature[COMPACT_SIG_SIZE_BYTES];
TypedData_Get_Struct(self, Signature, &Signature_DataType, signature);
if (secp256k1_ecdsa_signature_serialize_compact(secp256k1_context_no_precomp,
compact_signature,
&(signature->sig)) != 1)
{
rb_raise(
Secp256k1_SerializationError_class,
"unable to compute compact signature"
);
}
return rb_str_new((char*)compact_signature, COMPACT_SIG_SIZE_BYTES);
}
|
#der_encoded ⇒ String
Return Distinguished Encoding Rules (DER) encoded signature data.
770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 |
# File 'ext/rbsecp256k1/rbsecp256k1.c', line 770
static VALUE
Signature_der_encoded(VALUE self)
{
// TODO: Cache value after first computation
Signature *signature;
unsigned long der_signature_len;
unsigned char der_signature[72];
TypedData_Get_Struct(self, Signature, &Signature_DataType, signature);
der_signature_len = 72;
if (secp256k1_ecdsa_signature_serialize_der(secp256k1_context_no_precomp,
der_signature,
&der_signature_len,
&(signature->sig)) != 1)
{
rb_raise(
Secp256k1_SerializationError_class,
"could not compute DER encoded signature"
);
}
return rb_str_new((char*)der_signature, der_signature_len);
}
|
#normalized ⇒ Array
Returns the normalized lower-S form of this signature.
This can be useful when importing signatures generated by other applications that may not be normalized. Non-normalized signatures are potentially forgeable.
833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 |
# File 'ext/rbsecp256k1/rbsecp256k1.c', line 833
static VALUE
Signature_normalized(VALUE self)
{
VALUE result_sig;
VALUE was_normalized;
VALUE result;
Signature *signature;
Signature *normalized_signature;
TypedData_Get_Struct(self, Signature, &Signature_DataType, signature);
result_sig = Signature_alloc(Secp256k1_Signature_class);
TypedData_Get_Struct(
result_sig, Signature, &Signature_DataType, normalized_signature
);
was_normalized = Qfalse;
if (secp256k1_ecdsa_signature_normalize(
secp256k1_context_no_precomp,
&(normalized_signature->sig),
&(signature->sig)) == 1)
{
was_normalized = Qtrue;
}
result = rb_ary_new2(2);
rb_ary_push(result, was_normalized);
rb_ary_push(result, result_sig);
return result;
}
|