-
Notifications
You must be signed in to change notification settings - Fork 9
/
proof-spec.ts
75 lines (65 loc) · 2.35 KB
/
proof-spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import { MetaStatements, Statements } from './statement';
import { SetupParam } from './setup-param';
import { generateProofSpecG1, isProofSpecG1Valid } from 'crypto-wasm-new';
/**
* The specification used to construct the proof. This contains all the statements and the meta statements.
* If you have a lot of `Statements` or `SetupParam`s or they have a large size like for SNARKs, use `QuasiProofSpec`
*/
export class ProofSpec {
value: Uint8Array;
constructor(
statements: Statements,
metaStatements: MetaStatements,
setupParams?: SetupParam[],
context?: Uint8Array
) {
const params = (setupParams ?? new Array<SetupParam>()).map((s) => s.value);
this.value = generateProofSpecG1(statements.values, metaStatements.values, params, context);
}
/**
* Check if the proof spec is valid.
* @returns
*/
isValid(): boolean {
return isProofSpecG1Valid(this.value);
}
}
/**
* The specification used to construct the proof. This contains all the statements and the meta statements.
* The difference between this and `ProofSpec` that this does not call WASM to generate a `ProofSpec` object that
* corresponds to the `ProofSpec` struct in Rust. This WASM call be expensive due to the serialization overhead and thus
* it's advised to use this when there are a lot of `Statements` or `SetupParam`s.
*/
export class QuasiProofSpec {
statements: Statements;
metaStatements: MetaStatements;
setupParams: SetupParam[];
context?: Uint8Array;
constructor(
statements?: Statements,
metaStatements?: MetaStatements,
setupParams?: SetupParam[],
context?: Uint8Array
) {
this.statements = statements || new Statements();
this.metaStatements = metaStatements || new MetaStatements();
this.setupParams = setupParams || new Array<SetupParam>();
this.context = context;
}
addStatement(statement: Uint8Array): number {
return this.statements.add(statement);
}
addMetaStatement(metaStatement: Uint8Array): number {
return this.metaStatements.add(metaStatement);
}
addSetupParam(setupParam: SetupParam): number {
this.setupParams.push(setupParam);
return this.setupParams.length - 1;
}
setContext(context: Uint8Array) {
this.context = context;
}
toProofSpec(): ProofSpec {
return new ProofSpec(this.statements, this.metaStatements, this.setupParams, this.context);
}
}