-
Notifications
You must be signed in to change notification settings - Fork 229
/
Copy pathprogram.ts
94 lines (87 loc) · 2.42 KB
/
program.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/* eslint-disable @typescript-eslint/no-explicit-any */
import { Backend, CompiledCircuit, ProofData } from '@noir-lang/types';
import { generateWitness } from './witness_generation.js';
import initAbi, { abiDecode, InputMap, InputValue } from '@noir-lang/noirc_abi';
import initACVM, { compressWitness, ForeignCallHandler } from '@noir-lang/acvm_js';
export class Noir {
constructor(
private circuit: CompiledCircuit,
private backend?: Backend,
) {}
/** @ignore */
async init(): Promise<void> {
// If these are available, then we are in the
// web environment. For the node environment, this
// is a no-op.
if (typeof initAbi === 'function') {
await Promise.all([initAbi(), initACVM()]);
}
}
/**
*
* @description
* Destroys the underlying backend instance.
*
* @example
* ```typescript
* await noir.destroy();
* ```
*
*/
async destroy(): Promise<void> {
await this.backend?.destroy();
}
private getBackend(): Backend {
if (this.backend === undefined) throw new Error('Operation requires a backend but none was provided');
return this.backend;
}
// Initial inputs to your program
/**
* @description
* Allows to execute a circuit to get its witness and return value.
*
* @example
* ```typescript
* async execute(inputs)
* ```
*/
async execute(
inputs: InputMap,
foreignCallHandler?: ForeignCallHandler,
): Promise<{ witness: Uint8Array; returnValue: InputValue }> {
await this.init();
const witness = await generateWitness(this.circuit, inputs, foreignCallHandler);
const { return_value: returnValue } = abiDecode(this.circuit.abi, witness);
return { witness: compressWitness(witness), returnValue };
}
/**
*
* @description
* Generates a witness and a proof given an object as input.
*
* @example
* ```typescript
* async generateProof(input)
* ```
*
*/
async generateProof(inputs: InputMap, foreignCallHandler?: ForeignCallHandler): Promise<ProofData> {
const { witness } = await this.execute(inputs, foreignCallHandler);
return this.getBackend().generateProof(witness);
}
/**
*
* @description
* Instantiates the verification key and verifies a proof.
*
*
* @example
* ```typescript
* async verifyProof(proof)
* ```
*
*/
async verifyProof(proofData: ProofData): Promise<boolean> {
return this.getBackend().verifyProof(proofData);
}
}