herumi / mcl

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

G2Mul fails on golang:alpine #76

Closed decoqt closed 4 years ago

decoqt commented 4 years ago

I compile on golang:alpine, but G2Mul got error.

environmet is:

FROM golang:1.13.4-alpine

RUN apk update \
&& apk add --no-cache gmp-dev g++ make git linux-headers bash upx\
&& cd /root \
&& git clone https://github.com/herumi/mcl.git --depth=1\
&& cd /root/mcl \
&& make -j 4 \
&& make install \
&& cp lib/libmclbn384.so /usr/local/lib \
&& ldconfig /

I compile my apps using cgo:

#cgo CFLAGS:-DMCLBN_FP_UNIT_SIZE=6
#cgo LDFLAGS:-lmclbn384 -lmcl
#include <mcl/bn.h>

error is:

fatal: morestack on g0
SIGTRAP: trace trap
PC=0x4636f2 m=4 sigcode=128

goroutine 0 [idle]:
runtime.abort()
        /usr/local/go/src/runtime/asm_amd64.s:859 +0x2
runtime.morestack()
        /usr/local/go/src/runtime/asm_amd64.s:416 +0x25

goroutine 21 [syscall]:
runtime.cgocall(0x5fb540, 0xc00013ac68, 0xfe73f09b73abc401)
        /usr/local/go/src/runtime/cgocall.go:128 +0x5b fp=0xc00013ac38 sp=0xc00013ac00 pc=0x40ac5b
crypto/bls12._Cfunc_mclBnG2_mul(0xc0017a2000, 0xc000c5b2a0, 0xc00008c090)
        _cgo_gotypes.go:681 +0x45 fp=0xc00013ac68 sp=0xc00013ac38 pc=0x5f6e45

This seems only appear on alpine image.

herumi commented 4 years ago

Though I've not used golang:alpine, the message fatal: morestack on g0 shows that the assigned stack size is too small. Do you know how to increase the stack size?

decoqt commented 4 years ago

Though I've not used golang:alpine, the message fatal: morestack on g0 shows that the assigned stack size is too small. Do you know how to increase the stack size?

Maybe, this error happens when I create many G2s, such as make([]G2,10240). One strange thing is that G1 and Fr computation is ok in this case.

I donot how to increase the stack size, I'm searching it.

herumi commented 4 years ago

You can decrease the stack size of mcl by decreasing maxMulVecN and maxMulVecNGLV. https://github.com/herumi/mcl/blob/master/include/mcl/op.hpp#L120-L121

Could you try it?

decoqt commented 4 years ago

You can decrease the stack size of mcl by decreasing maxMulVecN and maxMulVecNGLV. https://github.com/herumi/mcl/blob/master/include/mcl/op.hpp#L120-L121

Could you try it?

diff --git a/include/mcl/op.hpp b/include/mcl/op.hpp
index 958d438..3402be3 100644
--- a/include/mcl/op.hpp
+++ b/include/mcl/op.hpp
@@ -117,8 +117,8 @@ const size_t UnitBitSize = sizeof(Unit) * 8;
 const size_t maxUnitSize = (MCL_MAX_BIT_SIZE + UnitBitSize - 1) / UnitBitSize;
 #define MCL_MAX_UNIT_SIZE ((MCL_MAX_BIT_SIZE + MCL_UNIT_BIT_SIZE - 1) / MCL_UNIT_BIT_SIZE)

-const size_t maxMulVecN = 32; // inner loop of mulVec
-const size_t maxMulVecNGLV = 16; // inner loop of mulVec with GLV
+const size_t maxMulVecN = 16; // inner loop of mulVec
+const size_t maxMulVecNGLV = 8; // inner loop of mulVec with GLV

 struct FpGenerator;
 struct Op;

Works fine now! Need test more. Thanks very much!