Skip to content
This repository has been archived by the owner on Mar 25, 2024. It is now read-only.

Commit

Permalink
Merge pull request #12 from apache/add-bls
Browse files Browse the repository at this point in the history
add bls to library
  • Loading branch information
spector-in-london committed Jun 17, 2019
2 parents a91990c + 71c2bed commit aa31e19
Show file tree
Hide file tree
Showing 24 changed files with 961 additions and 119 deletions.
60 changes: 58 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ set(AMCL_SOVERSION ${AMCL_VERSION_MAJOR})
if(CMAKE_COMPILER_IS_GNUCC)
execute_process(COMMAND ${CMAKE_C_COMPILER} -dumpversion
OUTPUT_VARIABLE GCC_VERSION)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99 -static-libgcc -Wall -Wextra -Wno-strict-prototypes -Wunused-value -Wcast-align -Wunused-variable -Wundef -Wformat-security")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -static-libgcc -Wall -Wextra -Wno-strict-prototypes -Wunused-value -Wcast-align -Wunused-variable -Wundef -Wformat-security")

if (GCC_VERSION VERSION_GREATER 4.8 OR GCC_VERSION VERSION_EQUAL 4.8)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wshadow")
Expand Down Expand Up @@ -117,9 +117,11 @@ log(BUILD_TESTING) # added by 'include(CTest)'
option(BUILD_MPIN "Build MPIN" ON)
option(BUILD_WCC "Build WCC" ON)
option(BUILD_X509 "BUild X509" ON)
option(BUILD_BLS "Build BLS" ON)
log(BUILD_MPIN)
log(BUILD_WCC)
log(BUILD_X509)
log(BUILD_BLS)

option(DEBUG_REDUCE "Print debug message for field reduction" OFF)
option(DEBUG_NORM "Detect digit overflow" OFF)
Expand Down Expand Up @@ -223,7 +225,7 @@ install(FILES
DESTINATION ${INSTALL_INCLUDESUBDIR}
)

if (BUILD_MPIN OR BUILD_WCC)
if (BUILD_MPIN OR BUILD_WCC OR BUILD_BLS)
amcl_configure_file_core(include/config_test.h.in include/config_test.h amcl_core_pbc_GEN_HDRS)

target_sources(amcl_core PRIVATE src/pbc_support.c)
Expand Down Expand Up @@ -596,6 +598,60 @@ if(BUILD_WCC)
endforeach()
endif()

##################################################
# AMCL_BLS_*** Libraries
##################################################
if(BUILD_BLS)
foreach(curve ${AMCL_CURVE})
amcl_curve_field(TC "${curve}")
amcl_curve_field(CS "${curve}")

if(TARGET amcl_pairing_${TC})
if(CS STREQUAL "128")
amcl_configure_file_curve(include/bls.h.in include/bls_${TC}.h "${curve}" amcl_bls_${TC}_GEN_HDRS)
amcl_configure_file_curve(src/bls.c.in src/bls_${TC}.c "${curve}" amcl_bls_${TC}_GEN_SRCS)
else(CS STREQUAL "128")
amcl_configure_file_curve(include/bls${CS}.h.in include/bls${CS}_${TC}.h "${curve}" amcl_bls_${TC}_GEN_HDRS)
amcl_configure_file_curve(src/bls${CS}.c.in src/bls${CS}_${TC}.c "${curve}" amcl_bls_${TC}_GEN_SRCS)
endif(CS STREQUAL "128")

message(STATUS "Build libamcl_bls_${TC}")
add_library(amcl_bls_${TC}
${amcl_bls_${TC}_GEN_SRCS}
)
list(APPEND AMCL_LIBRARIES amcl_bls_${TC})

set_target_properties(amcl_bls_${TC} PROPERTIES
EXPORT_NAME bls_${TC}
VERSION ${AMCL_VERSION}
SOVERSION ${AMCL_SOVERSION}
)

target_include_directories(amcl_bls_${TC} PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)

target_link_libraries(amcl_bls_${TC} PUBLIC
amcl_pairing_${TC}
)

install(TARGETS amcl_bls_${TC}
EXPORT AMCLTargets
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
)

install(FILES
${amcl_bls_${TC}_GEN_HDRS}
DESTINATION ${INSTALL_INCLUDESUBDIR}
)
endif()
endforeach()
endif()

##################################################
# pkgconfig
##################################################
Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ ifeq ($(CMAKE_BUILD_TYPE),Coverage)
-DAMCL_RSA=$(AMCL_RSA) \
-DBUILD_MPIN=$(AMCL_BUILD_MPIN) \
-DBUILD_WCC=$(AMCL_BUILD_WCC) \
-DBUILD_BLS=$(AMCL_BUILD_BLS) \
-DBUILD_DOCS=$(AMCL_BUILD_DOCS) \
-DAMCL_MAXPIN=$(AMCL_MAXPIN) \
-DAMCL_PBLEN=$(AMCL_PBLEN) \
Expand Down
5 changes: 4 additions & 1 deletion config.mk
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# size of chunk in bits which is wordlength of computer = 16, 32 or 64. (see arch.h)
WORD_SIZE:=64

# Current choice of Elliptic Curve NIST256 C25519 ED25519 BRAINPOOL ANSSI NUMS256E NUMS256W NUMS384E NUMS384W NUMS512E NUMS512W HIFIVE GOLDILOCKS NIST384 C41417 NIST521 BN254 BN254CX BLS383 FP256BN FP512BN BLS461
# Current choice of Elliptic Curve ANSSI C25519 NIST521 BLS24 C41417 NUMS256E BLS381 ED25519 NUMS256W BLS383 FP256BN NUMS384E BLS461 FP512BN NUMS384W BLS48 GOLDILOCKS NUMS512E BN254 HIFIVE NUMS512W BN254CX NIST256 SECP256K1 BRAINPOOL NIST384
AMCL_CURVE:=ED25519,NIST256,GOLDILOCKS,BLS383

# RSA security level: 2048 3072 4096
Expand All @@ -30,6 +30,9 @@ AMCL_BUILD_MPIN:=ON
# Build WCC ON/OFF
AMCL_BUILD_WCC:=ON

# Build BLS ON/OFF
AMCL_BUILD_BLS:=ON

# Build Doxygen ON/OFF
AMCL_BUILD_DOCS:=ON

Expand Down
6 changes: 6 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ foreach(curve ${AMCL_CURVE})
target_link_libraries(testwcc_dta_${TC} PRIVATE amcl_wcc_${TC})
endif()

if(TARGET amcl_bls_${TC})
amcl_configure_file_curve(testbls_ZZZ.c.in testbls_${TC}.c "${curve}" testbls_${TC}_GEN_SRCS)
add_executable(testbls_${TC} ${testbls_${TC}_GEN_SRCS})
target_link_libraries(testbls_${TC} PRIVATE amcl_bls_${TC})
endif()

endforeach()

foreach(level ${AMCL_RSA})
Expand Down
140 changes: 140 additions & 0 deletions examples/testbls_ZZZ.c.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/

/* test driver and function exerciser for BLS Signature API Functions */

/* Build executable after installation:

gcc -O0 -g ./testbls_ZZZ.c $(pkg-config --libs --cflags amcl) -o testbls_ZZZ

*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "config_curve_ZZZ.h"
#include "randapi.h"

#if CURVE_SECURITY_ZZZ == 128
#include "bls_ZZZ.h"
#elif CURVE_SECURITY_ZZZ == 192
#include "bls192_ZZZ.h"
#elif CURVE_SECURITY_ZZZ == 256
#include "bls256_ZZZ.h"
#endif

// Support multiple security levels
#if CURVE_SECURITY_ZZZ == 128
#define G2LEN 4*BFS_ZZZ
#elif CURVE_SECURITY_ZZZ == 192
#define G2LEN 8*BFS_ZZZ
#elif CURVE_SECURITY_ZZZ == 256
#define G2LEN 16*BFS_ZZZ
#endif

static char message[]="This is a test message";

int bls(csprng *RNG)
{
int rc;
char s[BGS_ZZZ];
char w[G2LEN];
char sig[BFS_ZZZ+1];
octet S= {0,sizeof(s),s};
octet W= {0,sizeof(w),w};
octet SIG= {0,sizeof(sig),sig};

BLS_ZZZ_KEY_PAIR_GENERATE(RNG,&S,&W);

printf("Private key: 0x");
OCT_output(&S);
printf("Public key: 0x");
OCT_output(&W);

BLS_ZZZ_SIGN(&SIG,message,&S);
printf("Signature: 0x");
OCT_output(&SIG);

rc=BLS_ZZZ_VERIFY(&SIG,message,&W);
if (rc==BLS_OK)
{
printf("Success: Signature is valid\n");
}
else
{
printf("Error: Invalid Signature\n");
}

// change the message
message[0]='Z';
printf("message %s\n", message);
rc=BLS_ZZZ_VERIFY(&SIG,message,&W);
if (rc==BLS_OK)
{
printf("Success: Signature is valid\n");
}
else
{
printf("Error: Invalid Signature\n");
}

// Change the signature
message[0]='T';
SIG.val[0]=5;
printf("message %s\n", message);
printf("Signature: 0x");
OCT_output(&SIG);
rc=BLS_ZZZ_VERIFY(&SIG,message,&W);
if (rc==BLS_OK)
{
printf("Success: Signature is valid\n");
}
else
{
printf("Error: Invalid Signature\n");
}

return rc;
}


int main()
{
#ifdef DEBUG
printf("%d bit build\n",CHUNK);
#endif

// non random seed value
char seed[32] = {0};
octet SEED = {sizeof(seed),sizeof(seed),seed};
for (int i=0; i<32; i++) SEED.val[i]=i+1;
printf("SEED: ");
OCT_output(&SEED);
printf("\n");

// initialise random number generator
csprng RNG;
CREATE_CSPRNG(&RNG,&SEED);

printf("\nTesting BLS signature for curve ZZZ\n");
bls(&RNG);

KILL_CSPRNG(&RNG);
}


2 changes: 1 addition & 1 deletion examples/testmpin_ZZZ.c.in
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ under the License.

/* Build executible after installation:

gcc -std=c99 -g ./testmpin.c -I/opt/amcl/include -L/opt/amcl/lib -lamcl_mpin_ZZZ -lamcl_pairing_ZZZ -lamcl_curve_ZZZ -lamcl_core -o testmpin_ZZZ
gcc -O0 -g ./testmpin_ZZZ.c $(pkg-config --libs --cflags amcl) -o testmpin_ZZZ

*/

Expand Down
5 changes: 4 additions & 1 deletion examples/testwcc_ZZZ.c.in
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ under the License.
/* Demonstrate WCC with one TA */

/* Build executable after installation:
gcc -std=c99 -g testwcc_ZZZ.c -I/opt/amcl/include -L/opt/amcl/lib -lamcl_wcc_ZZZ -lamcl_pairing_ZZZ -lamcl_curve_ZZZ -lamcl_core_ZZZ -o testwcc_ZZZ */

gcc -O0 -g ./testwcc_ZZZ.c $(pkg-config --libs --cflags amcl) -o testwcc_ZZZ

*/

#include <stdio.h>
#include <stdlib.h>
Expand Down
75 changes: 75 additions & 0 deletions include/bls.h.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/

/**
* @file bls_ZZZ.h
* @author Mike Scott
* @date 28th Novemebr 2018
* @brief BLS Header file
*
* Allows some user configuration
* defines structures
* declares functions
*
*/

#ifndef BLS_ZZZ_H
#define BLS_ZZZ_H

#include "pair_ZZZ.h"

/* Field size is assumed to be greater than or equal to group size */

#define BGS_ZZZ MODBYTES_XXX /**< BLS Group Size */
#define BFS_ZZZ MODBYTES_XXX /**< BLS Field Size */

#define BLS_OK 0 /**< Function completed without error */
#define BLS_FAIL -1 /**< Point is NOT on the curve */

/* BLS API functions */

/** @brief Generate Key Pair
*
@param RNG Pointer to a cryptographically secure random number generator
@param S Private key
@param W Public Key. W = S*G, where G is fixed generator
@return Zero for success or else an error code
*/
int BLS_ZZZ_KEY_PAIR_GENERATE(csprng *RNG,octet* S,octet *W);

/** @brief Calculate a signature
*
@param SIG signature
@param m message to be signed
@param S Private key
@return Zero for success or else an error code
*/
int BLS_ZZZ_SIGN(octet *SIG,char *m,octet *S);

/** @brief Verify a signature
*
@param SIG signature
@param m message whose signature is to be verified.
@param W Public key
@return Zero for success or else an error code
*/
int BLS_ZZZ_VERIFY(octet *SIG,char *m,octet *W);

#endif

0 comments on commit aa31e19

Please sign in to comment.