├── .github ├── pull_request_template.md └── workflows │ └── test.yaml ├── .gitignore ├── CHANGELOG.md ├── Cargo.lock ├── Cargo.toml ├── LICENSE ├── README.md ├── SECURITY.md ├── bench ├── bbs-plus.ts ├── bbs.ts ├── bound-check-snark.ts ├── helper.ts ├── index.ts └── ps.ts ├── docs ├── CONTRIBUTING.md └── RELEASE.md ├── jest.config.js ├── package.json ├── rustfmt.toml ├── sample ├── browser │ ├── .gitignore │ ├── README.md │ ├── index.web-sample.js │ ├── package.json │ ├── template.html │ ├── webpack.config.js │ └── yarn.lock └── ts-node │ ├── .gitignore │ ├── README.md │ ├── package.json │ ├── src │ └── index.ts │ ├── tsconfig.json │ └── yarn.lock ├── scripts ├── build-package.sh ├── install-dependencies.sh ├── pack-wasm-base64.js ├── publish-unstable.sh ├── publish.sh ├── remove-install-script.js └── test-browser.sh ├── src ├── accumulator │ ├── common.rs │ ├── kb_accumulator.rs │ ├── mod.rs │ └── vb_accumulator.rs ├── bbs.rs ├── bbs_plus.rs ├── bddt16_kvac.rs ├── bound_check.rs ├── common.rs ├── composite_proof_system │ ├── mod.rs │ ├── setup_params.rs │ └── statements │ │ ├── accumulator.rs │ │ ├── bound_check.rs │ │ ├── mod.rs │ │ ├── pok_sig.rs │ │ ├── r1cs.rs │ │ └── saver.rs ├── frost_dkg.rs ├── js │ ├── accumulator_wasm.js │ ├── bbs_plus_wasm.js │ ├── bbs_wasm.js │ ├── bdd16_kvac_wasm.js │ ├── bound_check_wasm.js │ ├── common.js │ ├── composite_proof_system_wasm.js │ ├── frost_dkg_wasm.js │ ├── index.d.ts │ ├── index.js │ ├── index.web.js │ ├── init_wasm.js │ ├── kb_accumulator_wasm.js │ ├── keyed_proof_wasm.js │ ├── legosnark_wasm.js │ ├── ps_wasm.js │ ├── r1cs_wasm.js │ ├── saver_wasm.js │ ├── setup_params_wasm.js │ ├── threshold_sig_wasm.js │ ├── type_declarations │ │ ├── accumulator.d.ts │ │ ├── bbs.d.ts │ │ ├── bbs_plus.d.ts │ │ ├── bdd16_kvac.d.ts │ │ ├── bound_check.d.ts │ │ ├── composite_proof_system.d.ts │ │ ├── frost_dkg.d.ts │ │ ├── index.d.ts │ │ ├── kb_accumulator.d.ts │ │ ├── keyed_proof.d.ts │ │ ├── legosnark.d.ts │ │ ├── ps.d.ts │ │ ├── r1cs.d.ts │ │ ├── saver.d.ts │ │ ├── setup_params.d.ts │ │ ├── threshold_sig.d.ts │ │ └── util.d.ts │ ├── types │ │ ├── BbsPlusPoKSigProof.ts │ │ ├── BbsPlusPoKSigProtocol.ts │ │ ├── BbsPlusSig.ts │ │ ├── BbsPlusSigParams.ts │ │ ├── BbsPoKSigProof.ts │ │ ├── BbsPoKSigProtocol.ts │ │ ├── BbsSig.ts │ │ ├── BbsSigParams.ts │ │ ├── Bddt16MacParams.ts │ │ ├── IKBUniversalAccumulator.ts │ │ ├── IKeypair.ts │ │ ├── INonMembershipWitness.ts │ │ ├── IUniversalAccumulator.ts │ │ ├── PSCommitMessage.ts │ │ ├── PSCommitmentOrMessage.ts │ │ ├── PSPoKSigProof.ts │ │ ├── PSPoKSigProtocol.ts │ │ ├── PSSig.ts │ │ ├── PSSigParams.ts │ │ ├── R1CS.ts │ │ ├── VerifyResult.ts │ │ └── index.ts │ ├── util_wasm.js │ └── wasm_module.js ├── keyed_proof.rs ├── legosnark.rs ├── lib.rs ├── ps.rs ├── r1cs.rs ├── saver.rs ├── threshold_sig │ ├── base_ot.rs │ ├── mod.rs │ └── signing.rs └── utils.rs ├── tests ├── accumulator.rs ├── bbs.rs ├── bbs_plus.rs ├── bound_check.rs ├── circom │ ├── less_than_32.r1cs │ ├── less_than_32.wasm │ ├── less_than_public_64.r1cs │ ├── less_than_public_64.wasm │ ├── multiply2.r1cs │ ├── multiply2.wasm │ ├── test1.r1cs │ ├── test1.wasm │ ├── test2.r1cs │ ├── test2.wasm │ ├── test3.r1cs │ ├── test3.wasm │ ├── test4.r1cs │ └── test4.wasm ├── common │ └── mod.rs ├── composite_proof_system.rs ├── js │ ├── accumulator.spec.ts │ ├── bbs.spec.ts │ ├── bbsPlus.spec.ts │ ├── bddt16Kvac.spec.ts │ ├── boundCheck.spec.ts │ ├── frostDkg.spec.ts │ ├── general.spec.ts │ ├── kb-universal-accumulator.spec.ts │ ├── proofSystem.spec.ts │ ├── ps.spec.ts │ ├── r1cs.spec.ts │ ├── saver.spec.ts │ ├── thresholdBbsPlusAndBbs.spec.ts │ └── util.ts ├── ps.rs └── saver.rs ├── tsconfig.json └── yarn.lock /.github/pull_request_template.md: -------------------------------------------------------------------------------- 1 | 4 | 5 | ## Description 6 | 7 | 8 | 9 | - [ ] Tests for the changes have been added (for bug fixes / features) 10 | - [ ] The commit message(s) follow [conventional commits](https://www.conventionalcommits.org/en/v1.0.0/) 11 | - [ ] Documentation has been added / updated (for bug fixes / features) 12 | - [ ] Changes follow the **[contributing](../docs/CONTRIBUTING.md)** document. 13 | 14 | ## Motivation and Context 15 | 16 | 17 | 18 | ## Does this PR introduce a breaking change? 19 | 20 | - [ ] Yes 21 | - [ ] No 22 | 23 | 24 | 25 | ## Which merge strategy will you use? 26 | 27 | 28 | 29 | - [ ] Squash 30 | - [ ] Rebase (REVIEW COMMITS) -------------------------------------------------------------------------------- /.github/workflows/test.yaml: -------------------------------------------------------------------------------- 1 | name: test 2 | 3 | on: 4 | push: 5 | branches: 6 | - master 7 | pull_request: {} 8 | 9 | jobs: 10 | js_test: 11 | name: Run JS tests in NodeJS environment 12 | runs-on: ${{matrix.os}} 13 | strategy: 14 | matrix: 15 | node-version: [18.x, 20.x] 16 | os: [ubuntu-latest, macos-latest] 17 | steps: 18 | - uses: actions/checkout@v1 19 | - uses: jetli/wasm-pack-action@v0.4.0 20 | with: 21 | # Optional version of wasm-pack to install(eg. 'v0.9.1', 'latest') 22 | version: 'latest' 23 | - uses: actions/setup-node@v1 24 | with: 25 | node-version: ${{ matrix.node-version }} 26 | - run: yarn install --frozen-lockfile 27 | - run: yarn build:release 28 | - run: yarn test:wasm 29 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | **/*.rs.bk 3 | bin/ 4 | pkg/ 5 | wasm-pack.log 6 | .idea/ 7 | /node_modules 8 | /binaryen 9 | .DS_Store 10 | /dist 11 | /lib -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # [0.6.0](https://github.com/mattrglobal/bbs-signatures/compare/v0.5.0...v0.6.0) (2021-05-25) 2 | 3 | BREAKING CHANGES: Support for asm.js has been removed due to its poor performance and it creating bloat for all 4 | package consumers. Support for NodeJS 10 which is now EOL has also been removed due in-compatibility with TextEncoder/TextDecoder that is used in the translated ES module generated by wasm-pack. 5 | 6 | ### Bug Fixes 7 | 8 | - handle when no. of message out of bound ([c3375a4](https://github.com/mattrglobal/bbs-signatures/commit/c3375a40f634ee317e4d5910649c4d8cb372daf4)) 9 | - verify proof bug ([#85](https://github.com/mattrglobal/bbs-signatures/issues/85)) ([ec016ef](https://github.com/mattrglobal/bbs-signatures/commit/ec016efdd5d412c4a9dea5470c4295297394f2b2)) 10 | 11 | ### Features 12 | 13 | - update approach to generating wasm ([#84](https://github.com/mattrglobal/bbs-signatures/issues/84)) ([f1afe8b](https://github.com/mattrglobal/bbs-signatures/commit/f1afe8be1fa69831f983d65908d132ef407d244b)) 14 | 15 | # [0.5.0](https://github.com/mattrglobal/bbs-signatures/compare/v0.3.0...v0.5.0) (2021-02-23) 16 | 17 | BREAKING CHANGES: The API exposed by this library is now promise based see [#61](https://github.com/mattrglobal/bbs-signatures/issues/61) for more details. 18 | 19 | ### Bug Fixes 20 | 21 | - set private key as defined on generate returntype ([#47](https://github.com/mattrglobal/bbs-signatures/issues/47)) ([5b29ec8](https://github.com/mattrglobal/bbs-signatures/commit/5b29ec86754e085f6db4dac4409f409da0990e1e)) 22 | - **sample:** update references to generateBls12381G2KeyPair ([#48](https://github.com/mattrglobal/bbs-signatures/issues/48)) ([691fe74](https://github.com/mattrglobal/bbs-signatures/commit/691fe7491664e5a1196eab08f2066fbd8996cd16)) 23 | - **sample:** use byte arrays instead of strings ([#51](https://github.com/mattrglobal/bbs-signatures/issues/51)) ([fcb06c9](https://github.com/mattrglobal/bbs-signatures/commit/fcb06c927d696f43453ec93aad11444d06d32cd3)), closes [#49](https://github.com/mattrglobal/bbs-signatures/issues/49) 24 | 25 | ### Features 26 | 27 | - change to use byte arrays for messages and support generating g1 keys ([#40](https://github.com/mattrglobal/bbs-signatures/issues/40)) ([92238cf](https://github.com/mattrglobal/bbs-signatures/commit/92238cf0895cde7a88c2ce6a830bf07bf2a7d28d)) 28 | - **sample:** update to the latest released version v0.4.0 ([f8fa318](https://github.com/mattrglobal/bbs-signatures/commit/f8fa3185787e2edb1484d942bd83a86d42f9a05f)), closes [#50](https://github.com/mattrglobal/bbs-signatures/issues/50) [#50](https://github.com/mattrglobal/bbs-signatures/issues/50) [#49](https://github.com/mattrglobal/bbs-signatures/issues/49) 29 | - migrate to async based api ([#61](https://github.com/mattrglobal/bbs-signatures/issues/61)) ([5dbd924](https://github.com/mattrglobal/bbs-signatures/commit/5dbd924fe601b8d085d80b68b0b69b1e7b892022)) 30 | 31 | # [0.4.0](https://github.com/mattrglobal/bbs-signatures/compare/v0.3.0...v0.4.0) (2020-08-27) 32 | 33 | ### Bug Fixes 34 | 35 | - uint8array return type casting ([#32](https://github.com/mattrglobal/bbs-signatures/issues/32)) ([28ed2fa](https://github.com/mattrglobal/bbs-signatures/commit/28ed2fa998562b253b1e793ff35d773602a88027)) 36 | 37 | ### Features 38 | 39 | - add asm.js roll back support ([#30](https://github.com/mattrglobal/bbs-signatures/issues/30)) ([4d28ad3](https://github.com/mattrglobal/bbs-signatures/commit/4d28ad3bce39e207a04ef660d478983212abde6c)) 40 | - add node bbs roll back support ([#33](https://github.com/mattrglobal/bbs-signatures/issues/33)) ([49aee81](https://github.com/mattrglobal/bbs-signatures/commit/49aee811ca73854456e9404b384a4935063f8e0a)) 41 | - change to use byte arrays for messages and support generating g1 keys ([#40](https://github.com/mattrglobal/bbs-signatures/issues/40)) ([92238cf](https://github.com/mattrglobal/bbs-signatures/commit/92238cf0895cde7a88c2ce6a830bf07bf2a7d28d)) 42 | 43 | ### BREAKING CHANGES 44 | 45 | - generateBls12381KeyPair has been changed to generateBls12381G2KeyPair 46 | - All operations involving messages and nonces are now in terms of Uint8Array's rather than strings 47 | 48 | # [0.3.0](https://github.com/mattrglobal/bbs-signatures/compare/v0.2.0...v0.3.0) (2020-07-20) 49 | 50 | ### Bug Fixes 51 | 52 | - uint8array return type casting ([#32](https://github.com/mattrglobal/bbs-signatures/issues/32)) ([28ed2fa](https://github.com/mattrglobal/bbs-signatures/commit/28ed2fa998562b253b1e793ff35d773602a88027)) 53 | 54 | ### Features 55 | 56 | - add asm.js roll back support ([#30](https://github.com/mattrglobal/bbs-signatures/issues/30)) ([4d28ad3](https://github.com/mattrglobal/bbs-signatures/commit/4d28ad3bce39e207a04ef660d478983212abde6c)) 57 | - add node bbs roll back support ([#33](https://github.com/mattrglobal/bbs-signatures/issues/33)) ([49aee81](https://github.com/mattrglobal/bbs-signatures/commit/49aee811ca73854456e9404b384a4935063f8e0a)) 58 | 59 | # [0.2.0](https://github.com/mattrglobal/bbs-signatures/compare/v0.1.0...v0.2.0) (2020-06-04) 60 | 61 | ### Features 62 | 63 | - add browser sample ([#27](https://github.com/mattrglobal/bbs-signatures/issues/27)) ([fdec4fc](https://github.com/mattrglobal/bbs-signatures/commit/fdec4fcf6645b7b94a704fc5fab1fa5d74c19d01)) 64 | - add node.js sample ([#25](https://github.com/mattrglobal/bbs-signatures/issues/25)) ([04042c2](https://github.com/mattrglobal/bbs-signatures/commit/04042c247689ebf5ba78ebd970c2c666fda34fa6)) 65 | 66 | # 0.1.0 (2020-06-04) 67 | 68 | Initial release 69 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | authors = ["Dock.io", "Tobias Looker ", "Mike Lodder "] 3 | description = "WASM binding to Dock's crypto lib" 4 | edition = "2021" 5 | license = "Apache-2.0" 6 | name = "dock_crypto_wasm" 7 | repository = "https://github.com/docknetwork/crypto-wasm" 8 | version = "0.21.0" 9 | 10 | [lib] 11 | crate-type = ["cdylib", "rlib"] 12 | 13 | [features] 14 | console = ["console_error_panic_hook"] 15 | default = ["dlmalloc"] 16 | 17 | [dependencies] 18 | console_error_panic_hook = { version = "0.1.7", optional = true } 19 | js-sys = "0.3" 20 | serde = { version = "1.0", features = ["derive"] } 21 | serde_json = { version = "1.0"} 22 | serde-wasm-bindgen = "0.6.5" 23 | wasm-bindgen = "= 0.2.86" 24 | dlmalloc = { version = "0.2.6", features = ["global"], optional = true } 25 | serde_with = { version = "1.10.0", default-features = false, features = ["macros"] } 26 | 27 | bbs_plus = { version = "0.22.0", default-features = false } 28 | vb_accumulator = { version = "0.26.0", default-features = false } 29 | schnorr_pok = { version = "0.20.0", default-features = false } 30 | proof_system = { version = "0.31.0", default-features = false } 31 | coconut-crypto = { version = "0.11.0", default-features = false } 32 | dock_crypto_utils = { version = "0.20.0", default-features = false } 33 | saver = { version = "0.18.0", default-features = false } 34 | legogroth16 = { version = "0.15.0", default-features = false, features = ["circom", "wasmer-js"] } 35 | secret_sharing_and_dkg = { version = "0.13.0", default-features = false } 36 | oblivious_transfer_protocols = { version = "0.9.0", default-features = false} 37 | bulletproofs_plus_plus = { version = "0.6.0", default-features = false} 38 | smc_range_proof = { version = "0.6.0", default-features = false} 39 | kvac = { version = "0.5.0", default-features = false} 40 | 41 | proof_system_old = { package = "proof_system", version = "0.30.0", default-features = false } 42 | 43 | ark-ec = { version = "^0.4.0", default-features = false } 44 | ark-ff = { version = "^0.4.0", default-features = false } 45 | ark-relations = { version = "^0.4.0", default-features = false } 46 | ark-bls12-381 = { version = "^0.4.0", default-features = false, features = [ "curve" ] } 47 | ark-serialize = { version = "^0.4.0", default-features = false, features = [ "derive" ] } 48 | blake2 = { version = "0.10.6", default-features = false } 49 | getrandom = { version = "0.2.12", features = ["js"] } 50 | ark-std = { version = "^0.4.0", default-features = false } 51 | zeroize = { version = "1.7.0", features = ["derive"] } 52 | 53 | [dev-dependencies] 54 | wasm-bindgen-test = "0.3.33" 55 | web-sys = { version = "0.3", features = ["console"] } 56 | 57 | [profile.dev] 58 | opt-level = 1 59 | 60 | [profile.release] 61 | lto = true 62 | opt-level = 3 63 | 64 | [package.metadata.wasm-pack.profile.dev] 65 | # Should `wasm-opt` be used to further optimize the wasm binary generated after 66 | # the Rust compiler has finished? Using `wasm-opt` can often further decrease 67 | # binary size or do clever tricks that haven't made their way into LLVM yet. 68 | # 69 | # Configuration is set to `false` by default for the dev profile, but it can 70 | # be set to an array of strings which are explicit arguments to pass to 71 | # `wasm-opt`. For example `['-Os']` would optimize for size while `['-O4']` 72 | # would execute very expensive optimizations passes 73 | wasm-opt = false 74 | 75 | [package.metadata.wasm-pack.profile.dev.wasm-bindgen] 76 | # Should we enable wasm-bindgen's debug assertions in its generated JS glue? 77 | debug-js-glue = true 78 | # Should wasm-bindgen demangle the symbols in the "name" custom section? 79 | demangle-name-section = true 80 | 81 | [profile.wasm-profiling] 82 | inherits = "release" 83 | 84 | [package.metadata.wasm-pack.profile.profiling] 85 | wasm-opt = ['-O4'] 86 | 87 | [package.metadata.wasm-pack.profile.profiling.wasm-bindgen] 88 | debug-js-glue = false 89 | demangle-name-section = false 90 | dwarf-debug-info = false 91 | 92 | [profile.wasm-release] 93 | inherits = "release" 94 | 95 | [package.metadata.wasm-pack.profile.release] 96 | wasm-opt = ['-O'] 97 | 98 | [package.metadata.wasm-pack.profile.release.wasm-bindgen] 99 | debug-js-glue = false 100 | demangle-name-section = true 101 | dwarf-debug-info = false -------------------------------------------------------------------------------- /SECURITY.md: -------------------------------------------------------------------------------- 1 | # Mattr Security Policy 2 | 3 | ## Reporting a Security Bug 4 | 5 | If you think you have discovered a security issue in any of the Mattr projects, we'd love to hear from you. We will take 6 | all security bugs seriously and if confirmed upon investigation we will patch it within a reasonable amount of time and 7 | release a public security bulletin discussing the impact and credit the discoverer. 8 | 9 | The best way to report a security bug is to email a description of the flaw and any related information (e.g. 10 | reproduction steps, version) to [security at mattr dot global](mailto:security@mattr.global). 11 | 12 | From there we'll work with you to assess the bug and decide the appropriate path forward. 13 | -------------------------------------------------------------------------------- /bench/bbs-plus.ts: -------------------------------------------------------------------------------- 1 | // main benchmark routine 2 | import { 3 | bbsPlusChallengeContributionFromProof, 4 | bbsPlusChallengeContributionFromProtocol, 5 | bbsPlusGenProofOfKnowledgeOfSignature, 6 | bbsPlusInitializeProofOfKnowledgeOfSignature, 7 | bbsPlusSignG1, 8 | bbsPlusVerifyG1, 9 | bbsPlusVerifyProofOfKnowledgeOfSignature, 10 | bbsPlusGenerateKeyPairG2, 11 | generateChallengeFromBytes, 12 | bbsPlusGenerateSignatureParamsG1, 13 | initializeWasm, 14 | encodeMessageForSigning, 15 | encodeMessagesForSigning 16 | } from "../lib"; 17 | import {benchmark, report} from "@stablelib/benchmark"; 18 | import {generateMessages} from "./helper"; 19 | 20 | export const benchmarkBBSPlus = async ( 21 | numberOfMessages: number, 22 | messageSizeInBytes: number, 23 | numberRevealed: number 24 | ): Promise => { 25 | await initializeWasm(); 26 | 27 | // Generate params 28 | report( 29 | `BBB+ Params generation for ${numberOfMessages} messages`, 30 | benchmark(() => bbsPlusGenerateSignatureParamsG1(numberOfMessages)) 31 | ); 32 | const sigParams = bbsPlusGenerateSignatureParamsG1(numberOfMessages); 33 | 34 | // Generate a new key pair 35 | report( 36 | "BBB+ Key Generation", 37 | benchmark(() => bbsPlusGenerateKeyPairG2(sigParams)) 38 | ); 39 | const keypair = bbsPlusGenerateKeyPairG2(sigParams); 40 | const sk = keypair.secret_key; 41 | const pk = keypair.public_key; 42 | 43 | const messages = generateMessages(numberOfMessages, messageSizeInBytes); 44 | report( 45 | `BBS+ encode ${numberOfMessages}, ${messageSizeInBytes} byte message(s)`, 46 | benchmark(() => { 47 | encodeMessagesForSigning(messages, Object.keys(messages).map(idx => +idx)) 48 | }) 49 | ); 50 | 51 | report( 52 | `BBS+ Sign ${numberOfMessages}, ${messageSizeInBytes} byte message(s)`, 53 | benchmark(() => bbsPlusSignG1(messages, sk, sigParams, true)) 54 | ); 55 | const signature = bbsPlusSignG1(messages, sk, sigParams, true); 56 | 57 | report( 58 | `BBS+ Verify ${numberOfMessages}, ${messageSizeInBytes} byte message(s)`, 59 | benchmark(() => bbsPlusVerifyG1(messages, signature, pk, sigParams, true)) 60 | ); 61 | 62 | const revealed: Set = new Set([...Array(numberRevealed).keys()]); 63 | const revealedMsgs = new Map(); 64 | revealed.forEach((i) => { 65 | revealedMsgs.set(i, messages[i]); 66 | }) 67 | 68 | function createProof() { 69 | const protocol = bbsPlusInitializeProofOfKnowledgeOfSignature(signature, sigParams, messages, new Map(), revealed, true); 70 | const pBytes = bbsPlusChallengeContributionFromProtocol(protocol, revealedMsgs, sigParams, true); 71 | const proverChallenge = generateChallengeFromBytes(pBytes); 72 | return bbsPlusGenProofOfKnowledgeOfSignature(protocol, proverChallenge); 73 | } 74 | 75 | report( 76 | `BBS+ Create Proof ${numberOfMessages}, ${messageSizeInBytes} byte message(s), revealing ${numberRevealed} message(s).`, 77 | benchmark(() => createProof()) 78 | ); 79 | const proof = createProof(); 80 | 81 | 82 | function verifyProof() { 83 | const vBytes = bbsPlusChallengeContributionFromProof(proof, revealedMsgs, sigParams, true); 84 | const verifierChallenge = generateChallengeFromBytes(vBytes); 85 | bbsPlusVerifyProofOfKnowledgeOfSignature(proof, revealedMsgs, verifierChallenge, pk, sigParams, true); 86 | } 87 | 88 | report( 89 | `BBS+ Verify Proof ${numberOfMessages}, ${messageSizeInBytes} byte message(s), revealing ${numberRevealed} message(s).`, 90 | benchmark(() => verifyProof()) 91 | ); 92 | }; 93 | -------------------------------------------------------------------------------- /bench/bbs.ts: -------------------------------------------------------------------------------- 1 | // main benchmark routine 2 | import { 3 | bbsChallengeContributionFromProof, 4 | bbsChallengeContributionFromProtocol, 5 | bbsGenProofOfKnowledgeOfSignature, 6 | bbsInitializeProofOfKnowledgeOfSignature, 7 | bbsSign, 8 | bbsVerify, 9 | bbsVerifyProofOfKnowledgeOfSignature, 10 | bbsGenerateKeyPair, 11 | generateChallengeFromBytes, 12 | bbsGenerateSignatureParams, 13 | initializeWasm, 14 | encodeMessagesForSigning 15 | } from "../lib"; 16 | import {benchmark, report} from "@stablelib/benchmark"; 17 | import {generateMessages} from "./helper"; 18 | 19 | export const benchmarkBBS = async ( 20 | numberOfMessages: number, 21 | messageSizeInBytes: number, 22 | numberRevealed: number 23 | ): Promise => { 24 | await initializeWasm(); 25 | 26 | // Generate params 27 | report( 28 | `BBB Params generation for ${numberOfMessages} messages`, 29 | benchmark(() => bbsGenerateSignatureParams(numberOfMessages)) 30 | ); 31 | const sigParams = bbsGenerateSignatureParams(numberOfMessages); 32 | 33 | // Generate a new key pair 34 | report( 35 | "BBB Key Generation", 36 | benchmark(() => bbsGenerateKeyPair(sigParams)) 37 | ); 38 | const keypair = bbsGenerateKeyPair(sigParams); 39 | const sk = keypair.secret_key; 40 | const pk = keypair.public_key; 41 | 42 | const messages = generateMessages(numberOfMessages, messageSizeInBytes); 43 | report( 44 | `BBS encode ${numberOfMessages}, ${messageSizeInBytes} byte message(s)`, 45 | benchmark(() => { 46 | encodeMessagesForSigning(messages, Object.keys(messages).map(idx => +idx)) 47 | }) 48 | ); 49 | 50 | report( 51 | `BBS Sign ${numberOfMessages}, ${messageSizeInBytes} byte message(s)`, 52 | benchmark(() => bbsSign(messages, sk, sigParams, true)) 53 | ); 54 | const signature = bbsSign(messages, sk, sigParams, true); 55 | 56 | report( 57 | `BBS Verify ${numberOfMessages}, ${messageSizeInBytes} byte message(s)`, 58 | benchmark(() => bbsVerify(messages, signature, pk, sigParams, true)) 59 | ); 60 | 61 | const revealed: Set = new Set([...Array(numberRevealed).keys()]); 62 | const revealedMsgs = new Map(); 63 | revealed.forEach((i) => { 64 | revealedMsgs.set(i, messages[i]); 65 | }) 66 | 67 | function createProof() { 68 | const protocol = bbsInitializeProofOfKnowledgeOfSignature(signature, sigParams, messages, new Map(), revealed, true); 69 | const pBytes = bbsChallengeContributionFromProtocol(protocol, revealedMsgs, sigParams, true); 70 | const proverChallenge = generateChallengeFromBytes(pBytes); 71 | return bbsGenProofOfKnowledgeOfSignature(protocol, proverChallenge); 72 | } 73 | 74 | report( 75 | `BBS Create Proof ${numberOfMessages}, ${messageSizeInBytes} byte message(s), revealing ${numberRevealed} message(s).`, 76 | benchmark(() => createProof()) 77 | ); 78 | const proof = createProof(); 79 | 80 | 81 | function verifyProof() { 82 | const vBytes = bbsChallengeContributionFromProof(proof, revealedMsgs, sigParams, true); 83 | const verifierChallenge = generateChallengeFromBytes(vBytes); 84 | bbsVerifyProofOfKnowledgeOfSignature(proof, revealedMsgs, verifierChallenge, pk, sigParams, true); 85 | } 86 | 87 | report( 88 | `BBS Verify Proof ${numberOfMessages}, ${messageSizeInBytes} byte message(s), revealing ${numberRevealed} message(s).`, 89 | benchmark(() => verifyProof()) 90 | ); 91 | }; 92 | -------------------------------------------------------------------------------- /bench/bound-check-snark.ts: -------------------------------------------------------------------------------- 1 | import {benchmark, report} from "@stablelib/benchmark"; 2 | import { 3 | boundCheckSnarkSetup, bbsPlusGeneratePublicKeyG2, bbsPlusGenerateSigningKey, 4 | bbsPlusGenerateSignatureParamsG1, 5 | initializeWasm, 6 | legosnarkDecompressPk, 7 | legosnarkVkFromPk 8 | } from "../lib"; 9 | 10 | export const benchmarkBoundCheckSnark = async ( 11 | ): Promise => { 12 | await initializeWasm(); 13 | 14 | report( 15 | 'Bound check snark setup', 16 | benchmark(() => boundCheckSnarkSetup(false)) 17 | ); 18 | const snarkPk = boundCheckSnarkSetup(false); 19 | 20 | report( 21 | 'Decompress legosnark proving key', 22 | benchmark(() => legosnarkDecompressPk(snarkPk)) 23 | ); 24 | const snarkPkDecom = legosnarkDecompressPk(snarkPk); 25 | 26 | report( 27 | 'Get uncompressed legosnark verifying key', 28 | benchmark(() => legosnarkVkFromPk(snarkPk, true)) 29 | ); 30 | report( 31 | 'Get compressed legosnark verifying key', 32 | benchmark(() => legosnarkVkFromPk(snarkPk, false)) 33 | ); 34 | const snarkVkDecom = legosnarkVkFromPk(snarkPk, true); 35 | 36 | const sigParams = bbsPlusGenerateSignatureParamsG1(1); 37 | const sigSk = bbsPlusGenerateSigningKey(); 38 | const sigPk = bbsPlusGeneratePublicKeyG2(sigSk, sigParams); 39 | 40 | // TODO: 41 | }; 42 | -------------------------------------------------------------------------------- /bench/helper.ts: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 - MATTR Limited 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | import { randomBytes } from "crypto"; 15 | 16 | export const generateMessages = ( 17 | numberOfMessages: number, 18 | messageSizeInBytes: number 19 | ): Uint8Array[] => { 20 | const messages: Uint8Array[] = []; 21 | for (let i = 0; i < numberOfMessages; i++) { 22 | messages[i] = randomBytes(messageSizeInBytes); 23 | } 24 | return messages; 25 | }; 26 | -------------------------------------------------------------------------------- /bench/index.ts: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 - MATTR Limited 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * http://www.apache.org/licenses/LICENSE-2.0 7 | * Unless required by applicable law or agreed to in writing, software 8 | * distributed under the License is distributed on an "AS IS" BASIS, 9 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | * See the License for the specific language governing permissions and 11 | * limitations under the License. 12 | */ 13 | 14 | /* eslint-disable @typescript-eslint/camelcase */ 15 | import { benchmarkBBSPlus } from "./bbs-plus"; 16 | import { benchmarkPS } from "./ps"; 17 | import { benchmarkBBS } from "./bbs"; 18 | import { benchmarkBoundCheckSnark } from "./bound-check-snark"; 19 | 20 | // ------------------------------ Sign/Verify/CreateProof/VerifyProof 1, 100 byte message ------------------------------ 21 | benchmarkBBS(1, 100, 0); 22 | // --------------------------------------------------------------------------------------------------------------------- 23 | 24 | // ------------------------------ Sign/Verify/CreateProof/VerifyProof 1, 1000 byte message ------------------------------ 25 | benchmarkBBS(1, 1000, 0); 26 | // ---------------------------------------------------------------------------------------------------------------------- 27 | 28 | // ------------------------------ Sign/Verify/CreateProof/VerifyProof 10, 100 byte messages ------------------------------ 29 | benchmarkBBS(10, 100, 1); 30 | // ----------------------------------------------------------------------------------------------------------------------- 31 | 32 | // ------------------------------ Sign/Verify/CreateProof/VerifyProof 10, 1000 byte messages ------------------------------ 33 | benchmarkBBS(10, 1000, 1); 34 | // ------------------------------------------------------------------------------------------------------------------------ 35 | 36 | // ------------------------------ Sign/Verify/CreateProof/VerifyProof 100, 100 byte messages ------------------------------ 37 | benchmarkBBS(100, 100, 1); 38 | // ------------------------------------------------------------------------------------------------------------------------- 39 | 40 | // ------------------------------ Sign/Verify/CreateProof/VerifyProof 100, 1000 byte messages ------------------------------ 41 | benchmarkBBS(100, 1000, 1); 42 | // ------------------------------------------------------------------------------------------------------------------------- 43 | 44 | // ------------------------------ Sign/Verify/CreateProof/VerifyProof 100, 100 byte messages ------------------------------ 45 | benchmarkBBS(100, 100, 50); 46 | // ------------------------------------------------------------------------------------------------------------------------- 47 | 48 | // ------------------------------ Sign/Verify/CreateProof/VerifyProof 100, 1000 byte messages ------------------------------ 49 | benchmarkBBS(100, 1000, 60); 50 | // ------------------------------------------------------------------------------------------------------------------------- 51 | 52 | // ------------------------------ Sign/Verify/CreateProof/VerifyProof 1, 100 byte message ------------------------------ 53 | benchmarkBBSPlus(1, 100, 1); 54 | // --------------------------------------------------------------------------------------------------------------------- 55 | 56 | // ------------------------------ Sign/Verify/CreateProof/VerifyProof 1, 1000 byte message ------------------------------ 57 | benchmarkBBSPlus(1, 1000, 1); 58 | // ---------------------------------------------------------------------------------------------------------------------- 59 | 60 | // ------------------------------ Sign/Verify/CreateProof/VerifyProof 10, 100 byte messages ------------------------------ 61 | benchmarkBBSPlus(10, 100, 1); 62 | // ----------------------------------------------------------------------------------------------------------------------- 63 | 64 | // ------------------------------ Sign/Verify/CreateProof/VerifyProof 10, 1000 byte messages ------------------------------ 65 | benchmarkBBSPlus(10, 1000, 1); 66 | // ------------------------------------------------------------------------------------------------------------------------ 67 | 68 | // ------------------------------ Sign/Verify/CreateProof/VerifyProof 100, 100 byte messages ------------------------------ 69 | benchmarkBBSPlus(100, 100, 1); 70 | // ------------------------------------------------------------------------------------------------------------------------- 71 | 72 | // ------------------------------ Sign/Verify/CreateProof/VerifyProof 100, 1000 byte messages ------------------------------ 73 | benchmarkBBSPlus(100, 1000, 1); 74 | // ------------------------------------------------------------------------------------------------------------------------- 75 | 76 | // ------------------------------ Sign/Verify/CreateProof/VerifyProof 100, 100 byte messages ------------------------------ 77 | benchmarkBBSPlus(100, 100, 50); 78 | // ------------------------------------------------------------------------------------------------------------------------- 79 | 80 | // ------------------------------ Sign/Verify/CreateProof/VerifyProof 100, 1000 byte messages ------------------------------ 81 | benchmarkBBSPlus(100, 1000, 60); 82 | // ------------------------------------------------------------------------------------------------------------------------- 83 | 84 | // ------------------------------ Sign/Verify/CreateProof/VerifyProof 1, 100 byte message ------------------------------ 85 | benchmarkPS(1, 100, 0); 86 | // --------------------------------------------------------------------------------------------------------------------- 87 | 88 | // ------------------------------ Sign/Verify/CreateProof/VerifyProof 1, 1000 byte message ------------------------------ 89 | benchmarkPS(1, 1000, 0); 90 | // ---------------------------------------------------------------------------------------------------------------------- 91 | 92 | // ------------------------------ Sign/Verify/CreateProof/VerifyProof 10, 100 byte messages ------------------------------ 93 | benchmarkPS(10, 100, 1); 94 | // ----------------------------------------------------------------------------------------------------------------------- 95 | 96 | // ------------------------------ Sign/Verify/CreateProof/VerifyProof 10, 1000 byte messages ------------------------------ 97 | benchmarkPS(10, 1000, 1); 98 | // ------------------------------------------------------------------------------------------------------------------------ 99 | 100 | // ------------------------------ Sign/Verify/CreateProof/VerifyProof 100, 100 byte messages ------------------------------ 101 | benchmarkPS(100, 100, 1); 102 | // ------------------------------------------------------------------------------------------------------------------------- 103 | 104 | // ------------------------------ Sign/Verify/CreateProof/VerifyProof 100, 1000 byte messages ------------------------------ 105 | benchmarkPS(100, 1000, 1); 106 | // ------------------------------------------------------------------------------------------------------------------------- 107 | 108 | // ------------------------------ Sign/Verify/CreateProof/VerifyProof 100, 100 byte messages ------------------------------ 109 | benchmarkPS(100, 100, 50); 110 | // ------------------------------------------------------------------------------------------------------------------------- 111 | 112 | // ------------------------------ Sign/Verify/CreateProof/VerifyProof 100, 1000 byte messages ------------------------------ 113 | benchmarkPS(100, 1000, 60); 114 | // ------------------------------------------------------------------------------------------------------------------------- 115 | 116 | benchmarkBoundCheckSnark(); 117 | -------------------------------------------------------------------------------- /bench/ps.ts: -------------------------------------------------------------------------------- 1 | // main benchmark routine 2 | import { 3 | psChallengeSignaturePoKContributionFromProof, 4 | psChallengeSignaturePoKContributionFromProtocol, 5 | psGenSignaturePoK, 6 | psInitializeSignaturePoK, 7 | psSign, 8 | psVerify, 9 | psVerifySignaturePoK, 10 | generateChallengeFromBytes, 11 | psGenerateSignatureParams, 12 | initializeWasm, 13 | psGenerateSigningKey, 14 | psGeneratePublicKey, 15 | encodeMessagesForSigning, 16 | } from "../lib"; 17 | import { benchmark, report } from "@stablelib/benchmark"; 18 | import { generateMessages } from "./helper"; 19 | 20 | export const benchmarkPS = async ( 21 | numberOfMessages: number, 22 | messageSizeInBytes: number, 23 | numberRevealed: number 24 | ): Promise => { 25 | await initializeWasm(); 26 | 27 | // Generate params 28 | report( 29 | `PS Params generation for ${numberOfMessages} messages`, 30 | benchmark(() => psGenerateSignatureParams(numberOfMessages)) 31 | ); 32 | const sigParams = psGenerateSignatureParams(numberOfMessages); 33 | 34 | // Generate a new key pair 35 | report( 36 | "PS Key Generation", 37 | benchmark(() => { 38 | const sk = psGenerateSigningKey(numberOfMessages); 39 | psGeneratePublicKey(sk, sigParams); 40 | }) 41 | ); 42 | const sk = psGenerateSigningKey(numberOfMessages); 43 | const pk = psGeneratePublicKey(sk, sigParams); 44 | 45 | const messages = generateMessages(numberOfMessages, messageSizeInBytes); 46 | report( 47 | `PS encode ${numberOfMessages}, ${messageSizeInBytes} byte message(s)`, 48 | benchmark(() => { 49 | encodeMessagesForSigning(messages, Object.keys(messages).map(idx => +idx)); 50 | }) 51 | ); 52 | 53 | const encodedMessages = encodeMessagesForSigning(messages, Object.keys(messages).map(idx => +idx)); 54 | 55 | report( 56 | `PS Sign ${numberOfMessages}, ${messageSizeInBytes} byte message(s)`, 57 | benchmark(() => { 58 | psSign(encodedMessages, sk, sigParams); 59 | }) 60 | ); 61 | const signature = psSign(encodedMessages, sk, sigParams); 62 | 63 | report( 64 | `PS Verify ${numberOfMessages}, ${messageSizeInBytes} byte message(s)`, 65 | benchmark(() => psVerify(encodedMessages, signature, pk, sigParams)) 66 | ); 67 | 68 | const revealed: Set = new Set([...Array(numberRevealed).keys()]); 69 | const revealedMsgs = new Map(); 70 | revealed.forEach((i) => { 71 | revealedMsgs.set(i, encodedMessages[i]); 72 | }); 73 | 74 | function createProof() { 75 | const protocol = psInitializeSignaturePoK( 76 | signature, 77 | sigParams, 78 | pk, 79 | encodedMessages.map((message, idx) => 80 | !revealedMsgs.has(idx) 81 | ? { BlindMessageRandomly: message } 82 | : "RevealMessage" 83 | ) 84 | ); 85 | const pBytes = psChallengeSignaturePoKContributionFromProtocol(protocol, pk, sigParams); 86 | const proverChallenge = generateChallengeFromBytes(pBytes); 87 | return psGenSignaturePoK(protocol, proverChallenge); 88 | } 89 | 90 | report( 91 | `PS Create Proof ${numberOfMessages}, ${messageSizeInBytes} byte message(s), revealing ${numberRevealed} message(s).`, 92 | benchmark(() => createProof()) 93 | ); 94 | const proof = createProof(); 95 | 96 | function verifyProof() { 97 | const vBytes = psChallengeSignaturePoKContributionFromProof(proof, pk, sigParams); 98 | const verifierChallenge = generateChallengeFromBytes(vBytes); 99 | psVerifySignaturePoK( 100 | proof, 101 | revealedMsgs, 102 | verifierChallenge, 103 | pk, 104 | sigParams 105 | ); 106 | } 107 | 108 | report( 109 | `PS Verify Proof ${numberOfMessages}, ${messageSizeInBytes} byte message(s), revealing ${numberRevealed} message(s).`, 110 | benchmark(() => verifyProof()) 111 | ); 112 | }; 113 | -------------------------------------------------------------------------------- /docs/CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | We use several pieces of technology in this repository to streamline the release process whilst maintaining high code 4 | quality. 5 | 6 | ## Debugging WASM 7 | 8 | By default if an error is un-handled (e.g a `panic!` in rust) when executing the WASM based module it will result in an 9 | un-helpful general error of `RuntimeError: unreachable`, which can be difficult to debug. To improve this experience, by 10 | default running `yarn build` in this library will compile the WASM from the rust with `console` feature enabled which provides a more insightful stacktrace for the error. 11 | 12 | ## Pre pull request checklist 13 | 14 | Below is a brief checklist prior to submitting or updating a pull request 15 | 16 | 1. Commit messages conform to the below conventions. 17 | 2. The pull request description fills in the relevant fields provided by the template. 18 | 19 | ## Commit messages 20 | 21 | A well formed commit message communicates context about a change. A diff will tell you what changed. A well cared for 22 | commit log is a beautiful and useful thing. 23 | 24 | What may be a hassle at first soon becomes habit, and eventually a source of pride and productivity for all 25 | involved. From reviews to maintenance it's a powerful tool. Understanding why something happened months or years ago 26 | becomes not only possible but efficient. 27 | 28 | We rely on consistent commit messages as we use 29 | [conventional-changelog](https://github.com/conventional-changelog/conventional-changelog) which automatically generates 30 | the changelog diff based on the commit messages 31 | 32 | We enforce well formed commit messages with pre-commit hooks using [husky](https://github.com/typicode/husky). 33 | 34 | The following guidelines are based on the angular 35 | team's [contribution guide](https://github.com/angular/angular/blob/22b96b9/CONTRIBUTING.md#-commit-message-guidelines). 36 | Checkout [commitizen](https://www.npmjs.com/package/commitizen) and [commitlint.io](https://commitlint.io/) for 37 | assistance. 38 | 39 | ``` 40 | (): 41 | 42 | 43 | 44 |