Skip to content

Commit d49c2c0

Browse files
committed
Wip
Signed-off-by: lovesh <lovesh.bond@gmail.com>
1 parent 784e0a9 commit d49c2c0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+4947
-2052
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,7 +1049,7 @@ check if the value is between -200 and 50, the verifier should ask the prover to
10491049
numbers, convert them to integers by multiplying with a number to make it integer, like if a decimal value can have maximum of 3 decimal places, they should be
10501050
multiplied by 1000. The [test](./tests/composite-proofs/bound-check.spec.ts) mentioned above shows these scenarios.
10511051
The conversions defined in the above tests are abstracted in this [Encoders](./src/bbs-plus/encoder.ts) class and you can see the usage
1052-
in [these tests](./tests/composite-proofs/sign-verify-js-obj.spec.ts).
1052+
in [these tests](tests/composite-proofs/msg-js-obj/bound-check.spec.ts).
10531053

10541054

10551055
For this, the verifier needs to first create the setup parameters which he then shares with the prover. Note that the
@@ -1228,4 +1228,4 @@ For complete example, see [these tests](./tests/composite-proofs/bound-check.spe
12281228
### Working with messages as JS objects
12291229

12301230
The above interfaces have been found to be a bit difficult to work with when signing messages that are represented as JS objects.
1231-
[Here](./src/sign-verify-js-objs.ts) are some [utilities](./src/bbs-plus/encoder.ts) to make this task a bit easier. [These tests](./tests/composite-proofs/sign-verify-js-obj.spec.ts) contain plenty of examples.
1231+
[Here](./src/sign-verify-js-objs.ts) are some [utilities](./src/bbs-plus/encoder.ts) to make this task a bit easier. [These tests](tests/composite-proofs/msg-js-obj) contain plenty of examples.

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@docknetwork/crypto-wasm-ts",
3-
"version": "0.20.0",
3+
"version": "0.21.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",
@@ -21,7 +21,7 @@
2121
"lib": "lib"
2222
},
2323
"dependencies": {
24-
"@docknetwork/crypto-wasm": "0.12.0"
24+
"@docknetwork/crypto-wasm": "file:../crypto-wasm"
2525
},
2626
"devDependencies": {
2727
"@types/flat": "^5.0.2",
@@ -37,6 +37,7 @@
3737
"jest": "^27.3.0",
3838
"prettier": "2.7.1",
3939
"pretty-quick": "3.1.3",
40+
"r1csfile": "^0.0.41",
4041
"ts-jest": "^27.0.7",
4142
"ts-node": "^10.4.0",
4243
"typescript": "^4.8.2"

src/Pseudonym.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export class PseudonymBases {
7979
* Each verifier should have a unique scope
8080
*/
8181
static generateBasesForAttributes(attributeCount: number, scope?: Uint8Array): Uint8Array[] {
82-
const b = [];
82+
const b: Uint8Array[] = [];
8383
let s: number[];
8484
if (scope !== undefined) {
8585
s = Array.from(scope);

src/accumulator/accumulator.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ export class UniversalAccumulator extends Accumulator {
619619
products.push(universalAccumulatorComputeInitialFv(fixed, secretKey.value));
620620

621621
// store a batch of generated elements and take the product once the batch is full
622-
let currentBatch = [];
622+
let currentBatch: Uint8Array[] = [];
623623
// Accumulate 1 more than the maximum number of allowed members as specified in the paper
624624
for (let i = 0; i <= maxSize; i++) {
625625
const e = generateRandomFieldElement();
@@ -796,7 +796,7 @@ export class UniversalAccumulator extends Accumulator {
796796
const sk = this.getSecretKey(secretKey);
797797
const params_ = this.getParams(params);
798798
const members = await state.elements();
799-
let currentBatch = [];
799+
let currentBatch: Uint8Array[] = [];
800800
const ds: Uint8Array[] = [];
801801
for (const member of members) {
802802
currentBatch.push(member);
@@ -862,7 +862,7 @@ export class UniversalAccumulator extends Accumulator {
862862
const sk = this.getSecretKey(secretKey);
863863
const params_ = this.getParams(params);
864864
const members = await state.elements();
865-
let currentBatch = [];
865+
let currentBatch: Uint8Array[] = [];
866866
// store multiple `d`s for each non-member
867867
const dsForAll: Uint8Array[][] = new Array(nonMembers.length);
868868
for (const member of members) {

src/bbs-plus/encoder.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,19 @@ export class Encoder {
6666
return [names, encoded];
6767
}
6868

69+
encodeDefault(value: unknown, strict = false): Uint8Array {
70+
if (this.defaultEncoder !== undefined) {
71+
return this.defaultEncoder(value);
72+
} else {
73+
if (!strict && value instanceof Uint8Array) {
74+
return SignatureG1.encodeMessageForSigning(value);
75+
} else {
76+
throw new Error(
77+
`Cannot encode value ${value} as neither was default encoder present nor it was an Uint8Array. Its type was ${typeof value}`
78+
);
79+
}
80+
}
81+
}
6982
/**
7083
* Returns an encoding function to be used on a message that is a positive integer.
7184
*/

src/bbs-plus/params.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export abstract class SignatureParams {
5252
* @param indices
5353
*/
5454
getParamsForIndices(indices: number[]): Uint8Array[] {
55-
const p = [];
55+
const p: Uint8Array[] = [];
5656
p.push(this.value.h_0);
5757
for (const i of indices) {
5858
if (!this.isValidIndex(i)) {

src/bbs-plus/signature.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export abstract class Signature extends BytearrayWrapper {
6868
}
6969
const decoder = new TextDecoder();
7070
const decoded = decoder.decode(message);
71-
const chars = [];
71+
const chars: string[] = [];
7272
for (let i = 0; i < maxLength; i++) {
7373
// If a null character found then stop looking further
7474
if (decoded.charCodeAt(i) == 0) {

src/composite-proof/proof-spec.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,8 @@ export class QuasiProofSpecG1 {
6868
setContext(context: Uint8Array) {
6969
this.context = context;
7070
}
71+
72+
toProofSpec(): ProofSpecG1 {
73+
return new ProofSpecG1(this.statements, this.metaStatements, this.setupParams, this.context);
74+
}
7175
}

src/composite-proof/setup-param.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ import {
1212
generateSetupParamForSaverProvingKey,
1313
generateSetupParamForSaverVerifyingKey,
1414
generateSetupParamForLegoProvingKey,
15-
generateSetupParamForLegoVerifyingKey
15+
generateSetupParamForLegoVerifyingKey, generateSetupParamForR1CS,
16+
R1CS,
17+
generateSetupParamForBytes,
18+
generateSetupParamForFieldElemVec
1619
} from '@docknetwork/crypto-wasm';
1720
import { BBSPlusPublicKeyG2, SignatureParamsG1 } from '../bbs-plus';
1821
import {
@@ -35,6 +38,7 @@ import {
3538
} from '../legosnark';
3639
import { AccumulatorParams, AccumulatorPublicKey, MembershipProvingKey, NonMembershipProvingKey } from '../accumulator';
3740
import { BytearrayWrapper } from '../bytearray-wrapper';
41+
import { getR1CS, ParsedR1CSFile } from '../r1cs';
3842

3943
/**
4044
* Represents (public) setup parameters of different protocols. Different setup parameters can be wrapped in this and
@@ -125,4 +129,17 @@ export class SetupParam extends BytearrayWrapper {
125129
static legosnarkVerifyingKeyUncompressed(key: LegoVerifyingKeyUncompressed): SetupParam {
126130
return new SetupParam(generateSetupParamForLegoVerifyingKey(key.value, true));
127131
}
132+
133+
static r1cs(r1cs: R1CS | ParsedR1CSFile): SetupParam {
134+
let processedR1cs = getR1CS(r1cs);
135+
return new SetupParam(generateSetupParamForR1CS(processedR1cs.curveName, processedR1cs.numPublic, processedR1cs.numPrivate, processedR1cs.constraints));
136+
}
137+
138+
static bytes(b: Uint8Array): SetupParam {
139+
return new SetupParam(generateSetupParamForBytes(b));
140+
}
141+
142+
static fieldElementVec(arr: Uint8Array[]): SetupParam {
143+
return new SetupParam(generateSetupParamForFieldElemVec(arr));
144+
}
128145
}

src/composite-proof/statement.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ import {
1515
generateBoundCheckLegoProverStatement,
1616
generateBoundCheckLegoProverStatementFromParamRefs,
1717
generateBoundCheckLegoVerifierStatement,
18-
generateBoundCheckLegoVerifierStatementFromParamRefs
18+
generateBoundCheckLegoVerifierStatementFromParamRefs,
19+
generateR1CSCircomProverStatement,
20+
generateR1CSCircomProverStatementFromParamRefs,
21+
generateR1CSCircomVerifierStatement,
22+
generateR1CSCircomVerifierStatementFromParamRefs, R1CS
1923
} from '@docknetwork/crypto-wasm';
2024
import { BBSPlusPublicKeyG2, SignatureParamsG1 } from '../bbs-plus';
2125
import {
@@ -40,6 +44,7 @@ import {
4044
import { AccumulatorParams, AccumulatorPublicKey, MembershipProvingKey, NonMembershipProvingKey } from '../accumulator';
4145
import { AttributeBoundPseudonym, Pseudonym } from '../Pseudonym';
4246
import { isPositiveInteger } from '../util';
47+
import { getR1CS, ParsedR1CSFile } from '../r1cs';
4348

4449
/**
4550
* Relation which needs to be proven. Contains any public data that needs to be known to both prover and verifier
@@ -400,6 +405,32 @@ export class Statement {
400405
}
401406
return Statement.pedersenCommitmentG1(b, pseudonym.value);
402407
}
408+
409+
static r1csCircomProver(r1cs: R1CS | ParsedR1CSFile, wasmBytes: Uint8Array, snarkPk: LegoProvingKeyUncompressed): Uint8Array {
410+
let processedR1cs = getR1CS(r1cs);
411+
return generateR1CSCircomProverStatement(processedR1cs.curveName, processedR1cs.numPublic, processedR1cs.numPrivate, processedR1cs.constraints, wasmBytes, snarkPk.value, true);
412+
}
413+
414+
static r1csCircomProverFromCompressedParams(r1cs: R1CS | ParsedR1CSFile, wasmBytes: Uint8Array, snarkPk: LegoProvingKey): Uint8Array {
415+
let processedR1cs = getR1CS(r1cs);
416+
return generateR1CSCircomProverStatement(processedR1cs.curveName, processedR1cs.numPublic, processedR1cs.numPrivate, processedR1cs.constraints, wasmBytes, snarkPk.value, false);
417+
}
418+
419+
static r1csCircomProverFromSetupParamRefs(processedR1cs: number, wasmBytes: number, snarkPkRef: number): Uint8Array {
420+
return generateR1CSCircomProverStatementFromParamRefs(processedR1cs, wasmBytes, snarkPkRef);
421+
}
422+
423+
static r1csCircomVerifier(publicInputs: Uint8Array[], snarkVk: LegoVerifyingKeyUncompressed): Uint8Array {
424+
return generateR1CSCircomVerifierStatement(publicInputs, snarkVk.value, true);
425+
}
426+
427+
static r1csCircomVerifierFromCompressedParams(publicInputs: Uint8Array[], snarkVk: LegoVerifyingKey): Uint8Array {
428+
return generateR1CSCircomVerifierStatement(publicInputs, snarkVk.value, false);
429+
}
430+
431+
static r1csCircomVerifierFromSetupParamRefs(publicInputsRef: number, snarkVkRef: number): Uint8Array {
432+
return generateR1CSCircomVerifierStatementFromParamRefs(publicInputsRef, snarkVkRef);
433+
}
403434
}
404435

405436
/**

0 commit comments

Comments
 (0)