Skip to content

Commit 829d17e

Browse files
committed
Fix docs and add few helpers
Signed-off-by: lovesh <lovesh.bond@gmail.com>
1 parent 213c930 commit 829d17e

21 files changed

+182
-62
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<component name="ProjectRunConfigurationManager">
2+
<configuration default="false" name="prefilled-positive-accumulator.spec.ts" type="JavaScriptTestRunnerJest" nameIsGenerated="true">
3+
<node-interpreter value="$USER_HOME$/.nvm/versions/node/v14.18.1/bin/node" />
4+
<node-options value="" />
5+
<jest-package value="$PROJECT_DIR$/node_modules/jest" />
6+
<working-dir value="$PROJECT_DIR$" />
7+
<envs />
8+
<scope-kind value="TEST_FILE" />
9+
<test-file value="$PROJECT_DIR$/tests/prefilled-positive-accumulator.spec.ts" />
10+
<method v="2" />
11+
</configuration>
12+
</component>

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -829,7 +829,7 @@ For a more involved demo with multiple BBS+ signatures being used with accumulat
829829
before requesting blind signatures, see [here](./tests/demo.spec.ts). This test paints a picture where before getting any credential,
830830
a user has to prove possession of a credential and membership in an accumulator (except the 1st credential).
831831

832-
##### Verifiable encryption using SAVER
832+
### Verifiable encryption using SAVER
833833

834834
Note: This section assumes you have read some of the previous examples on composite proof.
835835

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@docknetwork/crypto-wasm-ts",
3-
"version": "0.12.0",
3+
"version": "0.13.0",
44
"description": "Typescript abstractions over Dock's Rust crypto library's WASM wrapper",
55
"homepage": "https://github.com/docknetwork/crypto-wasm-ts",
66
"main": "lib/crypto-wasm-ts/src/index.js",

src/ICompressed.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export interface ICompressed<UncompressedType> {
1414
readonly value: Uint8Array;
1515

1616
/**
17-
* Convert the uncompressed
17+
* Convert to uncompressed form
1818
*/
1919
decompress(): UncompressedType;
2020
}

src/accumulator/accumulator.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
import {
2-
accumulatorDeriveMembershipProvingKeyFromNonMembershipKey,
3-
generateAccumulatorKeyPair,
42
generateFieldElementFromBytes,
53
generateFieldElementFromNumber,
6-
generateMembershipProvingKey,
7-
generateNonMembershipProvingKey,
84
generateRandomFieldElement,
9-
IKeypair,
105
positiveAccumulatorAdd,
116
positiveAccumulatorAddBatch,
127
positiveAccumulatorBatchUpdates,
@@ -36,7 +31,7 @@ import {
3631
universalAccumulatorVerifyNonMembership
3732
} from '@docknetwork/crypto-wasm';
3833
import { MembershipWitness, NonMembershipWitness } from './accumulatorWitness';
39-
import { getUint8ArraysFromObject } from '../util';
34+
import { ensurePositiveIntegerOfSize, getUint8ArraysFromObject } from '../util';
4035
import { IAccumulatorState, IUniversalAccumulatorState } from './IAccumulatorState';
4136
import { IInitialElementsStore } from './IInitialElementsStore';
4237
import {
@@ -98,6 +93,7 @@ export abstract class Accumulator {
9893
* @param num - should be a positive integer
9994
*/
10095
static encodePositiveNumberAsAccumulatorMember(num: number): Uint8Array {
96+
ensurePositiveIntegerOfSize(num, 32);
10197
return generateFieldElementFromNumber(num);
10298
}
10399

@@ -614,8 +610,8 @@ export class UniversalAccumulator extends Accumulator {
614610

615611
// store the products of each batch
616612
const products: Uint8Array[] = [];
617-
// The first batch of products is the elements fixed for each curve, in this case it's for BLS12-381
618-
const fixed = universalAccumulatorFixedInitialElements();
613+
// The first batch of products is the elements fixed for each curve
614+
const fixed = UniversalAccumulator.fixedInitialElements();
619615
if (storePresent) {
620616
for (const i of fixed) {
621617
await initialElementsStore.add(i);
@@ -942,6 +938,14 @@ export class UniversalAccumulator extends Accumulator {
942938
return universalAccumulatorVerifyNonMembership(this.value.V, nonMember, witness.value, pk.value, params_.value);
943939
}
944940

941+
/**
942+
* The first few members of a universal accumulator are fixed for each curve. These should be added to the curve
943+
* before creating any witness and must never be removed.
944+
*/
945+
static fixedInitialElements(): Uint8Array[] {
946+
return universalAccumulatorFixedInitialElements();
947+
}
948+
945949
/**
946950
* Takes product of the form `initial_element_i + secret_key`.
947951
* @param initialElements

src/bbs-plus/signature.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import {
88
bbsVerifyG1,
99
generateRandomFieldElement
1010
} from '@docknetwork/crypto-wasm';
11-
import { isNumberBiggerThanNBits } from '../util';
1211
import { BBSPlusPublicKeyG2, BBSPlusSecretKey } from './keys';
12+
import { ensurePositiveIntegerOfSize } from '../util';
1313

1414
export abstract class Signature {
1515
value: Uint8Array;
@@ -32,6 +32,7 @@ export abstract class Signature {
3232
* @param num
3333
*/
3434
static encodePositiveNumberForSigning(num: number): Uint8Array {
35+
ensurePositiveIntegerOfSize(num, 32);
3536
return generateFieldElementFromNumber(num);
3637
}
3738

src/bytearray-wrapper.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,18 @@ export class BytearrayWrapper {
88
constructor(value: Uint8Array) {
99
this.value = value;
1010
}
11+
12+
/**
13+
* Return the wrapped bytearray
14+
*/
15+
get bytes(): Uint8Array {
16+
return this.value;
17+
}
18+
19+
/**
20+
* Return the length of the wrapped bytearray
21+
*/
22+
get length(): number {
23+
return this.value.length;
24+
}
1125
}

src/composite-proof/proof.ts

Lines changed: 46 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,23 @@ export class CompositeProofG1 {
2222
this.value = proof;
2323
}
2424

25+
/**
26+
* Generate the composite proof using a `ProofSpec`
27+
* @param proofSpec
28+
* @param witnesses
29+
* @param nonce
30+
*/
2531
static generate(proofSpec: ProofSpecG1, witnesses: Witnesses, nonce?: Uint8Array): CompositeProofG1 {
2632
const proof = generateCompositeProofG1(proofSpec.value, witnesses.values, nonce);
2733
return new CompositeProofG1(proof);
2834
}
2935

36+
/**
37+
* Generate the composite proof using a `QuasiProofSpecG1`
38+
* @param proofSpec
39+
* @param witnesses
40+
* @param nonce
41+
*/
3042
static generateUsingQuasiProofSpec(
3143
proofSpec: QuasiProofSpecG1,
3244
witnesses: Witnesses,
@@ -42,6 +54,40 @@ export class CompositeProofG1 {
4254
);
4355
}
4456

57+
/**
58+
* Verify this composite proof using a `ProofSpec`
59+
* @param proofSpec
60+
* @param nonce
61+
*/
62+
verify(proofSpec: ProofSpecG1, nonce?: Uint8Array): VerifyResult {
63+
return verifyCompositeProofG1(this.value, proofSpec.value, nonce);
64+
}
65+
66+
/**
67+
* Verify this composite proof using a `QuasiProofSpecG1`
68+
* @param proofSpec
69+
* @param nonce
70+
*/
71+
verifyUsingQuasiProofSpec(proofSpec: QuasiProofSpecG1, nonce?: Uint8Array): VerifyResult {
72+
return this.verifyWithDeconstructedProofSpec(
73+
proofSpec.statements,
74+
proofSpec.metaStatements,
75+
proofSpec.setupParams,
76+
proofSpec.context,
77+
nonce
78+
);
79+
}
80+
81+
/**
82+
* Get the ciphertext for the SAVER statement at index `statementIndex`. The proof involving any SAVER statement also
83+
* contains the ciphertext corresponding to that statement. Will throw an error if it could not find the ciphertext or
84+
* statement at that index
85+
* @param statementIndex
86+
*/
87+
getSaverCiphertext(statementIndex: number): SaverCiphertext {
88+
return new SaverCiphertext(saverGetCiphertextFromProof(this.value, statementIndex));
89+
}
90+
4591
static generateWithDeconstructedProofSpec(
4692
statements: Statements,
4793
metaStatements: MetaStatements,
@@ -62,20 +108,6 @@ export class CompositeProofG1 {
62108
return new CompositeProofG1(proof);
63109
}
64110

65-
verify(proofSpec: ProofSpecG1, nonce?: Uint8Array): VerifyResult {
66-
return verifyCompositeProofG1(this.value, proofSpec.value, nonce);
67-
}
68-
69-
verifyUsingQuasiProofSpec(proofSpec: QuasiProofSpecG1, nonce?: Uint8Array): VerifyResult {
70-
return this.verifyWithDeconstructedProofSpec(
71-
proofSpec.statements,
72-
proofSpec.metaStatements,
73-
proofSpec.setupParams,
74-
proofSpec.context,
75-
nonce
76-
);
77-
}
78-
79111
verifyWithDeconstructedProofSpec(
80112
statements: Statements,
81113
metaStatements: MetaStatements,
@@ -93,13 +125,4 @@ export class CompositeProofG1 {
93125
nonce
94126
);
95127
}
96-
97-
/**
98-
* Get the ciphertext for the SAVER statement at index `statementIndex`. The proof involving any SAVER statement also
99-
* contains the ciphertext corresponding to that statement. Will throw an error if it could not find the ciphertext
100-
* @param statementIndex
101-
*/
102-
getSaverCiphertext(statementIndex: number): SaverCiphertext {
103-
return new SaverCiphertext(saverGetCiphertextFromProof(this.value, statementIndex));
104-
}
105128
}

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export { initializeWasm } from '@docknetwork/crypto-wasm';
12
export * from './util';
23
export * from './bbs-plus';
34
export * from './accumulator';

src/legosnark/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ export class LegoProvingKey extends BytearrayWrapper implements ICompressed<Lego
1616
}
1717

1818
/**
19-
* Get compressed verifying key from proving key
19+
* Get the compressed verifying key from this proving key
2020
*/
2121
getVerifyingKey(): LegoVerifyingKey {
2222
return new LegoVerifyingKey(legosnarkVkFromPk(this.value, false));
2323
}
2424

2525
/**
26-
* Get uncompressed verifying key from proving key
26+
* Get the uncompressed verifying key from this proving key
2727
*/
2828
getVerifyingKeyUncompressed(): LegoVerifyingKeyUncompressed {
2929
return new LegoVerifyingKeyUncompressed(legosnarkVkFromPk(this.value, true));

0 commit comments

Comments
 (0)