herumi / mcl

a portable and fast pairing-based cryptography library
BSD 3-Clause "New" or "Revised" License
450 stars 151 forks source link

serialize G1 32 bytes failed for secp256k1 #173

Closed levyfan closed 1 year ago

levyfan commented 1 year ago

Hi, I get mclBn_getG1ByteSize=32 for secp256k1 curve, but the actual serialized length is 33. Serialize 32 bytes will fail with return size=0.

#include <mcl/bn256.hpp>
#include <mcl/bn_c256.h>
#include <iostream>

int main() {
    bool pb;
    mcl::bn::initG1only(&pb, mcl::ecparam::secp256k1);
    std::cout << "init pb=" << pb << std::endl;

    mcl::bn::G1 g;
    int bytes = mclBn_getG1ByteSize();
    std::cout << "getG1ByteSize=" << bytes << std::endl;

    char buf[128];
    size_t n = g.serialize(buf, 32);
    std::cout << "serialize 32 n=" << n << std::endl;

    n = g.serialize(buf, 33);
    std::cout << "serialize 33 n=" << n << std::endl;
    return 0;
}

The code will output:

init pb=1
getG1ByteSize=32
serialize 32 n=0
serialize 33 n=33

What is the expected byte size of G1 for secp256k1?

herumi commented 1 year ago

secp256k1 is a 256-bit curve, requiring 32 bytes for x and one byte for a signed bit of y. bn.h is mainly developed for BN254 and BLS12-381, so I don't consider other curves. I'll fix the return size later.

levyfan commented 1 year ago

Hi @herumi , how is it going on?

herumi commented 1 year ago

Could you use the magic number 33 until mclBn_getG1ByteSize() returns 33 for secp256k1?

levyfan commented 1 year ago

Could you use the magic number 33 until mclBn_getG1ByteSize() returns 33 for secp256k1?

I also use go api, there would be some error when mclBn_getG1ByteSize()=32 such as here https://github.com/herumi/mcl/blob/611d1cc7eedd890ed1fc023aef18b58c238ae444/ffi/go/mcl/mcl.go#L743-L751

herumi commented 1 year ago

I've fixed getG1ByteSize(). https://github.com/herumi/mcl/commit/fe28741fdba01893303efa1908bd043a7ff197f6 mcl.go does not support G1only, but I modified it.

levyfan commented 1 year ago

@herumi another question, does MCL_MAP_TO_MODE_HASH_TO_CURVE_07 support NIST P256 here https://www.ietf.org/archive/id/draft-irtf-cfrg-hash-to-curve-16.html#name-nist-p-256 and https://www.ietf.org/archive/id/draft-irtf-cfrg-hash-to-curve-16.html#name-suites-for-nist-p-256

herumi commented 1 year ago

The current mcl does not support it.