1
- import { Account , initThreadPool , ProgramManager , AleoKeyProvider , OfflineKeyProvider , CREDITS_PROGRAM_KEYS } from "@provablehq/sdk" ;
1
+ import { Account , initThreadPool , ProgramManager , AleoKeyProvider , AleoKeyProviderParams } from "@provablehq/sdk" ;
2
2
3
3
await initThreadPool ( ) ;
4
4
5
+ const programName = "hello_hello.aleo"
5
6
7
+ const hello_hello_program = `
8
+ program ${ programName } ;
6
9
7
- async function localProgramExecution ( ) {
10
+ function hello:
11
+ input r0 as u32.public;
12
+ input r1 as u32.private;
13
+ add r0 r1 into r2;
14
+ output r2 as u32.private;`
15
+
16
+ async function localProgramExecution ( program , programName , aleoFunction , inputs ) {
8
17
const programManager = new ProgramManager ( ) ;
9
18
10
19
// Create a temporary account for the execution of the program
@@ -13,19 +22,38 @@ async function localProgramExecution() {
13
22
14
23
// Create a key provider in order to re-use the same key for each execution
15
24
const keyProvider = new AleoKeyProvider ( ) ;
16
- const offlineKeyProvider = new OfflineKeyProvider ( ) ;
17
25
keyProvider . useCache ( true ) ;
18
26
programManager . setKeyProvider ( keyProvider ) ;
19
27
20
- const [ transferPublicProver , transferPublicVerifier ] = await keyProvider . fetchCreditsKeys ( CREDITS_PROGRAM_KEYS . transfer_public ) ;
21
- console . log ( transferPublicVerifier . toString ( ) ) ;
28
+ // Pre-synthesize the program keys and then cache them in memory using key provider
29
+ const keyPair = await programManager . synthesizeKeys ( hello_hello_program , aleoFunction , inputs ) ;
30
+ programManager . keyProvider . cacheKeys ( `${ programName } :${ aleoFunction } ` , keyPair ) ;
22
31
23
32
// Specify parameters for the key provider to use search for program keys. In particular specify the cache key
24
33
// that was used to cache the keys in the previous step.
25
-
34
+ const keyProviderParams = new AleoKeyProviderParams ( { cacheKey : `${ programName } :${ aleoFunction } ` } ) ;
35
+
36
+ // Execute once using the key provider params defined above. This will use the cached proving keys and make
37
+ // execution significantly faster.
38
+ let executionResponse = await programManager . run (
39
+ program ,
40
+ aleoFunction ,
41
+ inputs ,
42
+ true ,
43
+ undefined ,
44
+ keyProviderParams ,
45
+ ) ;
46
+ console . log ( "hello_hello/hello executed - result:" , executionResponse . getOutputs ( ) ) ;
47
+
48
+ // Verify the execution using the verifying key that was generated earlier.
49
+ if ( programManager . verifyExecution ( executionResponse , 9_000_000 ) ) {
50
+ console . log ( "hello_hello/hello execution verified!" ) ;
51
+ } else {
52
+ throw ( "Execution failed verification!" ) ;
53
+ }
26
54
}
27
55
28
56
const start = Date . now ( ) ;
29
57
console . log ( "Starting execute!" ) ;
30
- await localProgramExecution ( ) ;
58
+ await localProgramExecution ( hello_hello_program , programName , "hello" , [ "5u32" , "5u32" ] ) ;
31
59
console . log ( "Execute finished!" , Date . now ( ) - start ) ;
0 commit comments