如何计算 charm-crypto 中组元素的大小(以位为单位)?

How can I compute the size in bits of group elements in charm-crypto?

我想使用方案“Hess - Efficient identity based signature schemes based on pairings.” of Charm-Crypto

我需要计算组元素的位长(例如签名中的 S2)。

据我从 , serializing the elements gives me a Base64 encoded string. From that and this other question about Base64 中了解到,我得出结论,我需要计算以下内容:

signature = {'S1' : S1, 'S2' : S2}
S2_serial = group.serialize(signature['S2'])

sigLenInBase64 = len(S2_serial)
sigLenInByte = (sigLenInBase64 *3)/4
sigLenInBit = sigLenInByte * 8

由于 S2 是 G1 的组元素,我希望其大小与基础曲线的大小相同(因此 'SS512' 为 512Bit,'MNT224' 为 224Bit,等等)。但是,'SS512' 和 'MNT224' 的大小相差 28 位(分别为 540 位和 252 位),'MNT159' 的大小相差 21 位。这是为什么?我如何预测给定曲线的偏移量?

我目前的猜测是,我没有考虑一些额外的信息(比如符号的字节)。

更新: 使用接受的答案,我现在可以像这样正确计算大小:

def compute_bitsize(base64_input):
    b_padded = base64_input.split(str.encode(":"))[1]
    pad_size = b_padded.count(str.encode("="))
    b_len_without_pad = len(b_padded)-4
    byte_len = (b_len_without_pad *3)/4 +(3-pad_size)-1
    bit_len = byte_len * 8
    return bit_len

中获取 'SS512' 的示例序列化。序列化字符串为:

1:Nri028S0SySkDoN0JH1Lu6HQo/Jkhq7DCZHI1MUrHOuIgCONonN14GzmhwopYQOxnjOysclhYNOnsvCLjVDLggE=

查看行:

PyObject *result = PyBytes_FromFormat("%d:%s", element->element_type, (const char *) base64_data_buf);

the source code for charm我们看到你感兴趣的字符串部分是

Nri028S0SySkDoN0JH1Lu6HQo/Jkhq7DCZHI1MUrHOuIgCONonN14GzmhwopYQOxnjOysclhYNOnsvCLjVDLggE=

按照 维基百科 'Decoding Base64 with padding' 下的描述对其进行解码,得到 65 个字节,其中一个字节是 "byte for the sign of the element",根据