From bc8beb99461abb26cd7f07b5209b6e2ab6c12823 Mon Sep 17 00:00:00 2001 From: MITSUNARI Shigeo Date: Wed, 10 Aug 2016 19:32:28 +0900 Subject: add bls_smpl --- Makefile | 6 +++++- bls_smpl.py | 38 ++++++++++++++++++++++++++++++++++++++ readme.md | 4 ++++ src/bls.cpp | 9 +++++---- 4 files changed, 52 insertions(+), 5 deletions(-) create mode 100644 bls_smpl.py diff --git a/Makefile b/Makefile index 3e023e0..3642cd7 100644 --- a/Makefile +++ b/Makefile @@ -5,9 +5,13 @@ EXE_DIR=bin SRC_SRC=bls.cpp TEST_SRC=bls_test.cpp -#SAMPLE_SRC= +SAMPLE_SRC=bls_smpl.cpp CFLAGS+=-I../mcl/include + +sample_test: $(EXE_DIR)/bls_smpl.exe + python bls_smpl.py + ################################################################## BLS_LIB=$(LIB_DIR)/libbls.a all: $(BLS_LIB) diff --git a/bls_smpl.py b/bls_smpl.py new file mode 100644 index 0000000..f3bfec0 --- /dev/null +++ b/bls_smpl.py @@ -0,0 +1,38 @@ +import os, sys, subprocess + +EXE='bin/bls_smpl.exe' + +def init(): + subprocess.check_call([EXE, "init"]) + +def sign(m, i=0): + subprocess.check_call([EXE, "sign", "-m", m, "-id", str(i)]) + +def verify(m, i=0): + subprocess.check_call([EXE, "verify", "-m", m, "-id", str(i)]) + +def share(n, k): + subprocess.check_call([EXE, "share", "-n", str(n), "-k", str(k)]) + +def recover(ids): + cmd = [EXE, "recover", "-ids"] + for i in ids: + cmd.append(str(i)) + subprocess.check_call(cmd) + +def main(): + m = "abc" + n = 10 + ids = [1, 5, 3, 7] + k = len(ids) + init() + sign(m) + verify(m) + share(n, k) + for i in ids: + sign(m, i) + recover(ids) + verify(m) + +if __name__ == '__main__': + main() diff --git a/readme.md b/readme.md index 59ec541..4fb6e3a 100644 --- a/readme.md +++ b/readme.md @@ -20,6 +20,10 @@ To make lib/libbls.a and test, run cd bls make test ``` +To make sample programs, run +``` +make sample_test +``` # License diff --git a/src/bls.cpp b/src/bls.cpp index c85ce61..d017930 100644 --- a/src/bls.cpp +++ b/src/bls.cpp @@ -65,7 +65,7 @@ struct Polynomial { FrVec c; // f[x] = sum_{i=0}^{k-1} c[i] x^i void init(const Fr& s, int k) { - if (k < 2) throw cybozu::Exception("Polynomial:init:bad k") << k; + if (k < 2) throw cybozu::Exception("bls:Polynomial:init:bad k") << k; c.resize(k); c[0] = s; for (size_t i = 1; i < c.size(); i++) { @@ -75,8 +75,8 @@ struct Polynomial { // y = f(id) void eval(Fr& y, int id) const { - if (id == 0) throw cybozu::Exception("Polynomial:calc_f:i is zero"); - if (c.size() < 2) throw cybozu::Exception("Polynomial:calc_f:bad size") << c.size(); + if (id == 0) throw cybozu::Exception("bls:Polynomial:eval:id is zero"); + if (c.size() < 2) throw cybozu::Exception("bls:Polynomial:eval:bad size") << c.size(); const Fr x(id); y = c[c.size() - 1]; for (int i = (int)c.size() - 2; i >= 0; i--) { @@ -93,7 +93,7 @@ struct Polynomial { static void calcDelta(FrVec& delta, const IntVec& S) { const size_t k = S.size(); - if (k < 2) throw cybozu::Exception("BLS:calcDelta:bad size") << k; + if (k < 2) throw cybozu::Exception("bls:calcDelta:bad size") << k; delta.resize(k); Fr a = S[0]; for (size_t i = 1; i < k; i++) { @@ -364,6 +364,7 @@ void PrivateKey::sign(Sign& sign, const std::string& m) const void PrivateKey::share(std::vector& prvVec, int n, int k) { if (id_ != 0) throw cybozu::Exception("bls:PrivateKey:share:already shared") << id_; + if (n <= 0 || k <= 0 || k > n) throw cybozu::Exception("bls:PrivateKey:share:bad n, k") << n << k; Polynomial poly; poly.init(self_->s, k); prvVec.resize(n); -- cgit v1.2.3