This repository has been archived by the owner on Mar 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47 from apache/bls-sss
BLS SSS
- Loading branch information
Showing
15 changed files
with
1,535 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
1.0.1 | ||
1.1.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,242 @@ | ||
/* | ||
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. | ||
*/ | ||
|
||
/* | ||
BLS Shamir Secret Sharing example code. | ||
|
||
Build executable after installation: | ||
|
||
1. Change headers; | ||
|
||
#include "bls_ZZZ.h" -> #include <amcl/bls_ZZZ.h> | ||
|
||
2. Build code | ||
|
||
gcc -O0 -g ./example_bls_sss_ZZZ.c $(pkg-config --libs --cflags amcl) -o example_bls_sss_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[]="test message"; | ||
|
||
int bls_sss(csprng *RNG) | ||
{ | ||
int rc; | ||
int n=4; | ||
int k=3; | ||
|
||
char ski[BGS_ZZZ]; | ||
octet SKI = {0,sizeof(ski),ski}; | ||
char pki[G2LEN]; | ||
octet PKI = {0,sizeof(pki),pki}; | ||
|
||
char sko[BGS_ZZZ]; | ||
octet SKO = {sizeof(sko),sizeof(sko),sko}; | ||
char skr[BGS_ZZZ]; | ||
octet SKR = {sizeof(skr),sizeof(skr),skr}; | ||
|
||
char sigi[BFS_ZZZ+1]; | ||
octet SIGI = {0,sizeof(sigi),sigi}; | ||
char sigr[BFS_ZZZ+1]; | ||
octet SIGR = {0,sizeof(sigr),sigr}; | ||
|
||
// Secret shares | ||
char x[n][BGS_ZZZ]; | ||
octet X[n]; | ||
char y[n][BGS_ZZZ]; | ||
octet Y[n]; | ||
|
||
for(int i=0; i<n; i++) | ||
{ | ||
memset(&y[i], 0, sizeof(y[i])); | ||
Y[i].max = BGS_ZZZ; | ||
Y[i].len = BGS_ZZZ; | ||
Y[i].val = y[i]; | ||
memset(&x[i], 0, sizeof(x[i])); | ||
X[i].max = BGS_ZZZ; | ||
X[i].len = BGS_ZZZ; | ||
X[i].val = x[i]; | ||
} | ||
|
||
// Generate key pair | ||
BLS_ZZZ_KEY_PAIR_GENERATE(RNG,&SKI,&PKI); | ||
|
||
printf("Private key SKI: "); | ||
OCT_output(&SKI); | ||
printf("Public key PKI: "); | ||
OCT_output(&PKI); | ||
printf("\n"); | ||
|
||
// Sign the message | ||
BLS_ZZZ_SIGN(&SIGI,message,&SKI); | ||
|
||
printf("SIGI: "); | ||
OCT_output(&SIGI); | ||
|
||
// Verify signature | ||
rc=BLS_ZZZ_VERIFY(&SIGI,message,&PKI); | ||
if (rc!=BLS_OK) | ||
{ | ||
printf("Error: Invalid Signature\n"); | ||
return 1; | ||
} | ||
printf("Success: Signature is valid\n"); | ||
|
||
// Make shares of BLS secret key | ||
rc = BLS_ZZZ_MAKE_SHARES(k, n, RNG, X, Y, &SKI, &SKO); | ||
if (rc!=BLS_OK) | ||
{ | ||
printf("Error: BLS_ZZZ_MAKE_SHARES\n"); | ||
return 1; | ||
} | ||
|
||
for(int i=0; i<n; i++) | ||
{ | ||
printf("X[%d] ", i); | ||
OCT_output(&X[i]); | ||
printf("Y[%d] ", i); | ||
OCT_output(&Y[i]); | ||
} | ||
|
||
printf("SKO: "); | ||
OCT_output(&SKO); | ||
|
||
if (!OCT_comp(&SKI,&SKO)) | ||
{ | ||
printf("FAILURE SKI != SKO"); | ||
return 1; | ||
} | ||
|
||
// Recover BLS secret key | ||
rc = BLS_ZZZ_RECOVER_SECRET(k, X, Y, &SKR); | ||
if (rc!=BLS_OK) | ||
{ | ||
printf("Error: BLS_ZZZ_RECOVER_SECRET\n"); | ||
return 1; | ||
} | ||
printf("SKR: "); | ||
OCT_output(&SKR); | ||
|
||
if (!OCT_comp(&SKR,&SKO)) | ||
{ | ||
printf("FAILURE SKR != SKO"); | ||
return 1; | ||
} | ||
|
||
// Generate public keys and signatures using shares | ||
char pks[n][G2LEN]; | ||
octet PKS[n]; | ||
char sigs[n][BFS_ZZZ+1]; | ||
octet SIGS[n]; | ||
for(int i=0; i<n; i++) | ||
{ | ||
memset(&sigs[i], 0, sizeof(sigs[i])); | ||
SIGS[i].max = BFS_ZZZ+1; | ||
SIGS[i].len = BFS_ZZZ+1; | ||
SIGS[i].val = sigs[i]; | ||
memset(&PKS[i], 0, sizeof(PKS[i])); | ||
PKS[i].max = G2LEN; | ||
PKS[i].len = G2LEN; | ||
PKS[i].val = pks[i]; | ||
BLS_ZZZ_KEY_PAIR_GENERATE(NULL,&Y[i],&PKS[i]); | ||
BLS_ZZZ_SIGN(&SIGS[i],message,&Y[i]); | ||
} | ||
|
||
for(int i=0; i<n; i++) | ||
{ | ||
printf("PKS[%d] ", i); | ||
OCT_output(&PKS[i]); | ||
} | ||
|
||
for(int i=0; i<n; i++) | ||
{ | ||
printf("SIGS[%d] ", i); | ||
OCT_output(&SIGS[i]); | ||
} | ||
printf("\n\n"); | ||
|
||
// Recover BLS signature | ||
rc = BLS_ZZZ_RECOVER_SIGNATURE(k, X, SIGS, &SIGR); | ||
if (rc!=BLS_OK) | ||
{ | ||
printf("Error: BLS_ZZZ_RECOVER_SIGNATURE\n"); | ||
return 1; | ||
} | ||
printf("SIGR: "); | ||
OCT_output(&SIGR); | ||
|
||
if (!OCT_comp(&SIGR,&SIGI)) | ||
{ | ||
printf("FAILURE SIGR != SIGI"); | ||
return 1; | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
|
||
int main() | ||
{ | ||
#ifdef DEBUG | ||
printf("%d bit build\n",CHUNK); | ||
#endif | ||
|
||
char* seedHex = "78d0fb6705ce77dee47d03eb5b9c5d30"; | ||
char seed[16] = {0}; | ||
octet SEED = {sizeof(seed),sizeof(seed),seed}; | ||
|
||
// CSPRNG | ||
csprng RNG; | ||
|
||
// fake random source | ||
OCT_fromHex(&SEED,seedHex); | ||
printf("SEED: "); | ||
OCT_output(&SEED); | ||
|
||
// initialise strong RNG | ||
CREATE_CSPRNG(&RNG,&SEED); | ||
|
||
printf("\nBLS SSS example for curve ZZZ\n"); | ||
bls_sss(&RNG); | ||
|
||
KILL_CSPRNG(&RNG); | ||
} | ||
|
||
|
Oops, something went wrong.