@@ -223,6 +223,82 @@ impl<N: Network> Stack<N> {
223223 }
224224}
225225
226+ impl < N : Network > StackKeys < N > for Stack < N > {
227+ /// Returns `true` if the proving key for the given function name exists.
228+ #[ inline]
229+ fn contains_proving_key ( & self , function_name : & Identifier < N > ) -> bool {
230+ self . proving_keys . read ( ) . contains_key ( function_name)
231+ }
232+
233+ /// Returns the proving key for the given function name.
234+ #[ inline]
235+ fn get_proving_key ( & self , function_name : & Identifier < N > ) -> Result < ProvingKey < N > > {
236+ // If the program is 'credits.aleo', try to load the proving key, if it does not exist.
237+ self . try_insert_credits_function_proving_key ( function_name) ?;
238+ // Return the proving key, if it exists.
239+ match self . proving_keys . read ( ) . get ( function_name) {
240+ Some ( pk) => Ok ( pk. clone ( ) ) ,
241+ None => bail ! ( "Proving key not found for: {}/{}" , self . program. id( ) , function_name) ,
242+ }
243+ }
244+
245+ /// Inserts the given proving key for the given function name.
246+ #[ inline]
247+ fn insert_proving_key ( & self , function_name : & Identifier < N > , proving_key : ProvingKey < N > ) -> Result < ( ) > {
248+ // Ensure the function name exists in the program.
249+ ensure ! (
250+ self . program. contains_function( function_name) ,
251+ "Function '{function_name}' does not exist in program '{}'." ,
252+ self . program. id( )
253+ ) ;
254+ // Insert the proving key.
255+ self . proving_keys . write ( ) . insert ( * function_name, proving_key) ;
256+ Ok ( ( ) )
257+ }
258+
259+ /// Removes the proving key for the given function name.
260+ #[ inline]
261+ fn remove_proving_key ( & self , function_name : & Identifier < N > ) {
262+ self . proving_keys . write ( ) . shift_remove ( function_name) ;
263+ }
264+
265+ /// Returns `true` if the verifying key for the given function name exists.
266+ #[ inline]
267+ fn contains_verifying_key ( & self , function_name : & Identifier < N > ) -> bool {
268+ self . verifying_keys . read ( ) . contains_key ( function_name)
269+ }
270+
271+ /// Returns the verifying key for the given function name.
272+ #[ inline]
273+ fn get_verifying_key ( & self , function_name : & Identifier < N > ) -> Result < VerifyingKey < N > > {
274+ // Return the verifying key, if it exists.
275+ match self . verifying_keys . read ( ) . get ( function_name) {
276+ Some ( vk) => Ok ( vk. clone ( ) ) ,
277+ None => bail ! ( "Verifying key not found for: {}/{}" , self . program. id( ) , function_name) ,
278+ }
279+ }
280+
281+ /// Inserts the given verifying key for the given function name.
282+ #[ inline]
283+ fn insert_verifying_key ( & self , function_name : & Identifier < N > , verifying_key : VerifyingKey < N > ) -> Result < ( ) > {
284+ // Ensure the function name exists in the program.
285+ ensure ! (
286+ self . program. contains_function( function_name) ,
287+ "Function '{function_name}' does not exist in program '{}'." ,
288+ self . program. id( )
289+ ) ;
290+ // Insert the verifying key.
291+ self . verifying_keys . write ( ) . insert ( * function_name, verifying_key) ;
292+ Ok ( ( ) )
293+ }
294+
295+ /// Removes the verifying key for the given function name.
296+ #[ inline]
297+ fn remove_verifying_key ( & self , function_name : & Identifier < N > ) {
298+ self . verifying_keys . write ( ) . shift_remove ( function_name) ;
299+ }
300+ }
301+
226302impl < N : Network > StackProgram < N > for Stack < N > {
227303 /// Returns the program.
228304 #[ inline]
@@ -355,6 +431,23 @@ impl<N: Network> StackProgram<N> for Stack<N> {
355431 // Return the record.
356432 Ok ( record)
357433 }
434+
435+ /// Returns a record for the given record name, deriving the nonce from tvk and index.
436+ fn sample_record_using_tvk < R : Rng + CryptoRng > (
437+ & self ,
438+ burner_address : & Address < N > ,
439+ record_name : & Identifier < N > ,
440+ tvk : Field < N > ,
441+ index : Field < N > ,
442+ rng : & mut R ,
443+ ) -> Result < Record < N , Plaintext < N > > > {
444+ // Compute the randomizer.
445+ let randomizer = N :: hash_to_scalar_psd2 ( & [ tvk, index] ) ?;
446+ // Construct the record nonce from that randomizer.
447+ let record_nonce = N :: g_scalar_multiply ( & randomizer) ;
448+ // Sample the record with that nonce.
449+ self . sample_record ( burner_address, record_name, record_nonce, rng)
450+ }
358451}
359452
360453impl < N : Network > StackProgramTypes < N > for Stack < N > {
@@ -373,82 +466,6 @@ impl<N: Network> StackProgramTypes<N> for Stack<N> {
373466 }
374467}
375468
376- impl < N : Network > Stack < N > {
377- /// Returns `true` if the proving key for the given function name exists.
378- #[ inline]
379- pub fn contains_proving_key ( & self , function_name : & Identifier < N > ) -> bool {
380- self . proving_keys . read ( ) . contains_key ( function_name)
381- }
382-
383- /// Returns `true` if the verifying key for the given function name exists.
384- #[ inline]
385- pub fn contains_verifying_key ( & self , function_name : & Identifier < N > ) -> bool {
386- self . verifying_keys . read ( ) . contains_key ( function_name)
387- }
388-
389- /// Returns the proving key for the given function name.
390- #[ inline]
391- pub fn get_proving_key ( & self , function_name : & Identifier < N > ) -> Result < ProvingKey < N > > {
392- // If the program is 'credits.aleo', try to load the proving key, if it does not exist.
393- self . try_insert_credits_function_proving_key ( function_name) ?;
394- // Return the proving key, if it exists.
395- match self . proving_keys . read ( ) . get ( function_name) {
396- Some ( proving_key) => Ok ( proving_key. clone ( ) ) ,
397- None => bail ! ( "Proving key not found for: {}/{function_name}" , self . program. id( ) ) ,
398- }
399- }
400-
401- /// Returns the verifying key for the given function name.
402- #[ inline]
403- pub fn get_verifying_key ( & self , function_name : & Identifier < N > ) -> Result < VerifyingKey < N > > {
404- // Return the verifying key, if it exists.
405- match self . verifying_keys . read ( ) . get ( function_name) {
406- Some ( verifying_key) => Ok ( verifying_key. clone ( ) ) ,
407- None => bail ! ( "Verifying key not found for: {}/{function_name}" , self . program. id( ) ) ,
408- }
409- }
410-
411- /// Inserts the given proving key for the given function name.
412- #[ inline]
413- pub fn insert_proving_key ( & self , function_name : & Identifier < N > , proving_key : ProvingKey < N > ) -> Result < ( ) > {
414- // Ensure the function name exists in the program.
415- ensure ! (
416- self . program. contains_function( function_name) ,
417- "Function '{function_name}' does not exist in program '{}'." ,
418- self . program. id( )
419- ) ;
420- // Insert the proving key.
421- self . proving_keys . write ( ) . insert ( * function_name, proving_key) ;
422- Ok ( ( ) )
423- }
424-
425- /// Inserts the given verifying key for the given function name.
426- #[ inline]
427- pub fn insert_verifying_key ( & self , function_name : & Identifier < N > , verifying_key : VerifyingKey < N > ) -> Result < ( ) > {
428- // Ensure the function name exists in the program.
429- ensure ! (
430- self . program. contains_function( function_name) ,
431- "Function '{function_name}' does not exist in program '{}'." ,
432- self . program. id( )
433- ) ;
434- // Insert the verifying key.
435- self . verifying_keys . write ( ) . insert ( * function_name, verifying_key) ;
436- Ok ( ( ) )
437- }
438-
439- /// Removes the proving key for the given function name.
440- #[ inline]
441- pub fn remove_proving_key ( & self , function_name : & Identifier < N > ) {
442- self . proving_keys . write ( ) . shift_remove ( function_name) ;
443- }
444-
445- /// Removes the verifying key for the given function name.
446- #[ inline]
447- pub fn remove_verifying_key ( & self , function_name : & Identifier < N > ) {
448- self . verifying_keys . write ( ) . shift_remove ( function_name) ;
449- }
450- }
451-
452469impl < N : Network > Stack < N > {
453470 /// Inserts the proving key if the program ID is 'credits.aleo'.
454471 fn try_insert_credits_function_proving_key ( & self , function_name : & Identifier < N > ) -> Result < ( ) > {
0 commit comments