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.
949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 |
# File 'ext/rbsecp256k1/rbsecp256k1.c', line 949
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_static,
&(signature->sig),
signature_data) != 1)
{
rb_raise(Secp256k1_DeserializationError_class, "invalid compact signature");
return Qnil;
}
return signature_result;
}
|
.from_der_encoded(in_der_encoded_signature) ⇒ Secp256k1::Signature
Converts a DER encoded binary signature into a signature object.
988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 |
# File 'ext/rbsecp256k1/rbsecp256k1.c', line 988
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_static,
&(signature->sig),
signature_data,
RSTRING_LEN(in_der_encoded_signature)) != 1)
{
rb_raise(Secp256k1_DeserializationError_class, "invalid DER encoded signature");
return Qnil;
}
return signature_result;
}
|
Instance Method Details
#==(other) ⇒ Boolean
Compares two signatures.
Two signatures are equal if their compact encodings are identical.
1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 |
# File 'ext/rbsecp256k1/rbsecp256k1.c', line 1123
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_static, lhs_compact, &(lhs->sig)
);
secp256k1_ecdsa_signature_serialize_compact(
secp256k1_context_static, 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.
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 |
# File 'ext/rbsecp256k1/rbsecp256k1.c', line 1050
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_static,
compact_signature,
&(signature->sig)) != 1)
{
rb_raise(
Secp256k1_SerializationError_class,
"unable to compute compact signature"
);
return Qnil;
}
return rb_str_new((char*)compact_signature, COMPACT_SIG_SIZE_BYTES);
}
|
#der_encoded ⇒ String
Return Distinguished Encoding Rules (DER) encoded signature data.
1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 |
# File 'ext/rbsecp256k1/rbsecp256k1.c', line 1019
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_static,
der_signature,
&der_signature_len,
&(signature->sig)) != 1)
{
rb_raise(
Secp256k1_SerializationError_class,
"could not compute DER encoded signature"
);
return Qnil;
}
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.
1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 |
# File 'ext/rbsecp256k1/rbsecp256k1.c', line 1084
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_static,
&(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;
}
|