aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--go/blscgo/bls.go24
-rw-r--r--go/main.go10
-rw-r--r--include/bls.hpp16
-rw-r--r--include/bls_if.h19
-rw-r--r--src/bls.cpp37
-rw-r--r--src/bls_if.cpp4
-rw-r--r--test/bls_if_test.cpp4
7 files changed, 75 insertions, 39 deletions
diff --git a/go/blscgo/bls.go b/go/blscgo/bls.go
index bd0cd2d..f75945b 100644
--- a/go/blscgo/bls.go
+++ b/go/blscgo/bls.go
@@ -9,14 +9,18 @@ import "C"
import "fmt"
import "unsafe"
+const CurveFp254BNb = 0
+const CurveFp382_1 = 1
+const CurveFp382_2 = 2
+
// Init --
-func Init() {
- C.blsInit()
+func Init(curve int) {
+ C.blsInit(C.int(curve))
}
// ID --
type ID struct {
- v [4]C.uint64_t
+ v [6]C.uint64_t
}
// getPointer --
@@ -49,8 +53,8 @@ func (id *ID) SetStr(s string) error {
// Set --
func (id *ID) Set(v []uint64) error {
- if len(v) != 4 {
- return fmt.Errorf("bad size (%d), expected size 4", len(v))
+ if len(v) != 6 {
+ return fmt.Errorf("bad size (%d), expected size 6", len(v))
}
// #nosec
C.blsIdSet(id.getPointer(), (*C.uint64_t)(unsafe.Pointer(&v[0])))
@@ -59,7 +63,7 @@ func (id *ID) Set(v []uint64) error {
// SecretKey --
type SecretKey struct {
- v [4]C.uint64_t
+ v [6]C.uint64_t
}
// getPointer --
@@ -92,8 +96,8 @@ func (sec *SecretKey) SetStr(s string) error {
// SetArray --
func (sec *SecretKey) SetArray(v []uint64) error {
- if len(v) != 4 {
- return fmt.Errorf("bad size (%d), expected size 4", len(v))
+ if len(v) != 6 {
+ return fmt.Errorf("bad size (%d), expected size 6", len(v))
}
// #nosec
C.blsSecretKeySetArray(sec.getPointer(), (*C.uint64_t)(unsafe.Pointer(&v[0])))
@@ -149,7 +153,7 @@ func (sec *SecretKey) GetPop() (sign *Sign) {
// PublicKey --
type PublicKey struct {
- v [4 * 2 * 3]C.uint64_t
+ v [6 * 2 * 3]C.uint64_t
}
// getPointer --
@@ -197,7 +201,7 @@ func (pub *PublicKey) Recover(pubVec []PublicKey, idVec []ID) {
// Sign --
type Sign struct {
- v [4 * 3]C.uint64_t
+ v [6 * 3]C.uint64_t
}
// getPointer --
diff --git a/go/main.go b/go/main.go
index 87934ad..c3da950 100644
--- a/go/main.go
+++ b/go/main.go
@@ -24,7 +24,7 @@ func testRecoverSecretKey() {
secVec := make([]blscgo.SecretKey, n)
idVec := make([]blscgo.ID, n)
for i := 0; i < n; i++ {
- idVec[i].Set([]uint64{1, 2, 3, uint64(i)})
+ idVec[i].Set([]uint64{1, 2, 3, uint64(i), 4, 5})
secVec[i].Set(msk, &idVec[i])
}
// recover sec2 from secVec and idVec
@@ -56,7 +56,7 @@ func testSign() {
idVec := make([]blscgo.ID, n)
for i := 0; i < n; i++ {
- idVec[i].Set([]uint64{idTbl[i], 0, 0, 0})
+ idVec[i].Set([]uint64{idTbl[i], 0, 0, 0, 0, 0})
fmt.Printf("idVec[%d]=%s\n", i, idVec[i].String())
secVec[i].Set(msk, &idVec[i])
@@ -112,10 +112,10 @@ func testPop() {
}
func main() {
fmt.Println("init")
- blscgo.Init()
+ blscgo.Init(blscgo.CurveFp254BNb)
{
var id blscgo.ID
- id.Set([]uint64{4, 3, 2, 1})
+ id.Set([]uint64{6, 5, 4, 3, 2, 1})
fmt.Println("id :", id)
var id2 blscgo.ID
id2.SetStr(id.String())
@@ -123,7 +123,7 @@ func main() {
}
{
var sec blscgo.SecretKey
- sec.SetArray([]uint64{1, 2, 3, 4})
+ sec.SetArray([]uint64{1, 2, 3, 4, 5, 6})
fmt.Println("sec=", sec)
}
diff --git a/include/bls.hpp b/include/bls.hpp
index 8122e09..194f669 100644
--- a/include/bls.hpp
+++ b/include/bls.hpp
@@ -17,6 +17,12 @@
namespace bls {
+enum {
+ CurveFp254BNb = 0,
+ CurveFp382_1 = 1,
+ CurveFp382_2 = 2
+};
+
namespace impl {
struct SecretKey;
@@ -41,7 +47,7 @@ struct Id;
initialize this library
call this once before using the other method
*/
-void init();
+void init(int curve = CurveFp254BNb);
class SecretKey;
class PublicKey;
@@ -61,7 +67,7 @@ typedef std::vector<Sign> SignVec;
typedef std::vector<Id> IdVec;
class Id {
- uint64_t self_[4]; // 256-bit
+ uint64_t self_[6]; // 384-bit
friend class PublicKey;
friend class SecretKey;
template<class T, class G> friend struct WrapArray;
@@ -86,7 +92,7 @@ public:
s ; secret key
*/
class SecretKey {
- uint64_t self_[4]; // 256-bit
+ uint64_t self_[6]; // 384-bit
template<class T, class G> friend struct WrapArray;
impl::SecretKey& getInner() { return *reinterpret_cast<impl::SecretKey*>(self_); }
const impl::SecretKey& getInner() const { return *reinterpret_cast<const impl::SecretKey*>(self_); }
@@ -144,7 +150,7 @@ public:
sQ ; public key
*/
class PublicKey {
- uint64_t self_[4 * 2 * 3]; // 256-bit x 2 x 3
+ uint64_t self_[6 * 2 * 3]; // 384-bit x 2 x 3
friend class SecretKey;
friend class Sign;
template<class T, class G> friend struct WrapArray;
@@ -181,7 +187,7 @@ public:
s H(m) ; sign
*/
class Sign {
- uint64_t self_[4 * 3]; // 256-bit x 3
+ uint64_t self_[6 * 3]; // 384-bit x 3
friend class SecretKey;
template<class T, class G> friend struct WrapArray;
impl::Sign& getInner() { return *reinterpret_cast<impl::Sign*>(self_); }
diff --git a/include/bls_if.h b/include/bls_if.h
index d0c2948..d02d325 100644
--- a/include/bls_if.h
+++ b/include/bls_if.h
@@ -18,23 +18,29 @@
extern "C" {
#endif
+enum {
+ BlsCurveFp254BNb = 0,
+ BlsCurveFp382_1 = 1,
+ BlsCurveFp382_2 = 2
+};
+
typedef struct {
- uint64_t buf[4];
+ uint64_t buf[6];
} blsId;
typedef struct {
- uint64_t buf[4];
+ uint64_t buf[6];
} blsSecretKey;
typedef struct {
- uint64_t buf[4 * 2 * 3];
+ uint64_t buf[6 * 2 * 3];
} blsPublicKey;
typedef struct {
- uint64_t buf[4 * 3];
+ uint64_t buf[6 * 3];
} blsSign;
-void blsInit(void);
+void blsInit(int curve);
blsId *blsIdCreate(void);
void blsIdDestroy(blsId *id);
@@ -50,7 +56,8 @@ int blsIdSetStr(blsId *id, const char *buf, size_t bufSize);
*/
size_t blsIdGetStr(const blsId *id, char *buf, size_t maxBufSize);
/*
- access p[0], p[1], p[2], p[3]
+ access p[0], ..., p[3] if 256-bit curve
+ access p[0], ..., p[5] if 384-bit curve
*/
void blsIdSet(blsId *id, const uint64_t *p);
diff --git a/src/bls.cpp b/src/bls.cpp
index e065911..ff747bf 100644
--- a/src/bls.cpp
+++ b/src/bls.cpp
@@ -5,13 +5,13 @@
http://opensource.org/licenses/BSD-3-Clause
*/
#include <bls.hpp>
-#include <mcl/bn256.hpp>
+#include <mcl/bn384.hpp>
#include <cybozu/crypto.hpp>
#include <cybozu/random_generator.hpp>
#include <vector>
#include <string>
-using namespace mcl::bn256;
+using namespace mcl::bn384;
typedef std::vector<Fr> FrVec;
#define PUT(x) std::cout << #x << "=" << x << std::endl;
@@ -156,9 +156,23 @@ std::ostream& writeAsHex(std::ostream& os, const T& t)
return os << str;
}
-void init()
-{
- BN::init(mcl::bn::CurveFp254BNb);
+void init(int curve)
+{
+ mcl::bn::CurveParam cp;
+ switch (curve) {
+ case bls::CurveFp254BNb:
+ cp = mcl::bn::CurveFp254BNb;
+ break;
+ case bls::CurveFp382_1:
+ cp = mcl::bn::CurveFp382_1;
+ break;
+ case bls::CurveFp382_2:
+ cp = mcl::bn::CurveFp382_2;
+ break;
+ default:
+ throw cybozu::Exception("bls:init:bad curve") << curve;
+ }
+ BN::init(cp);
G1::setCompressedExpression();
G2::setCompressedExpression();
Fr::init(BN::param.r);
@@ -167,10 +181,15 @@ void init()
assert(sizeof(SecretKey) == sizeof(impl::SecretKey));
assert(sizeof(PublicKey) == sizeof(impl::PublicKey));
assert(sizeof(Sign) == sizeof(impl::Sign));
- static const G2 Q(
- Fp2("12723517038133731887338407189719511622662176727675373276651903807414909099441", "4168783608814932154536427934509895782246573715297911553964171371032945126671"),
- Fp2("13891744915211034074451795021214165905772212241412891944830863846330766296736", "7937318970632701341203597196594272556916396164729705624521405069090520231616")
- );
+ static G2 Q;
+ if (curve == bls::CurveFp254BNb) {
+ Q.set(
+ Fp2("12723517038133731887338407189719511622662176727675373276651903807414909099441", "4168783608814932154536427934509895782246573715297911553964171371032945126671"),
+ Fp2("13891744915211034074451795021214165905772212241412891944830863846330766296736", "7937318970632701341203597196594272556916396164729705624521405069090520231616")
+ );
+ } else {
+ BN::mapToG2(Q, 1);
+ }
static std::vector<Fp6> Qcoeff;
BN::precomputeG2(Qcoeff, Q);
diff --git a/src/bls_if.cpp b/src/bls_if.cpp
index 0bea838..091bace 100644
--- a/src/bls_if.cpp
+++ b/src/bls_if.cpp
@@ -43,9 +43,9 @@ size_t getStrT(const Outer *p, char *buf, size_t maxBufSize)
return 0;
}
-void blsInit()
+void blsInit(int curve)
{
- bls::init();
+ bls::init(curve);
}
blsId *blsIdCreate()
diff --git a/test/bls_if_test.cpp b/test/bls_if_test.cpp
index c2dc0a9..84a2961 100644
--- a/test/bls_if_test.cpp
+++ b/test/bls_if_test.cpp
@@ -10,7 +10,7 @@ CYBOZU_TEST_AUTO(bls_if)
const char *msg = "this is a pen";
const size_t msgSize = strlen(msg);
- blsInit();
+ blsInit(BlsCurveFp254BNb);
sec = blsSecretKeyCreate();
blsSecretKeyInit(sec);
blsSecretKeyPut(sec);
@@ -38,7 +38,7 @@ CYBOZU_TEST_AUTO(bls_if_use_stack)
const char *msg = "this is a pen";
const size_t msgSize = strlen(msg);
- blsInit();
+ blsInit(BlsCurveFp254BNb);
blsSecretKeyInit(&sec);
blsSecretKeyPut(&sec);