11//! Defines the allocator and wires to be used for computing the key-derivation steps.
22
3- use alloc:: { rc:: Rc , vec:: Vec } ;
4- use core:: { cell:: RefCell , usize} ;
3+ use alloc:: { sync:: Arc , vec:: Vec } ;
54
5+ use spin:: RwLock ;
66use spongefish:: Unit ;
77
88/// A symbolic wire over which we perform out computation.
99/// Wraps over a [`usize`]
10- #[ derive( Clone , Copy , Default , PartialEq , Eq , Unit ) ]
10+ #[ derive( Clone , Copy , Default , Hash , PartialEq , Eq , Unit ) ]
1111pub struct FieldVar ( pub usize ) ;
1212
1313impl core:: fmt:: Debug for FieldVar {
@@ -16,52 +16,56 @@ impl core::fmt::Debug for FieldVar {
1616 }
1717}
1818
19- // /// A witness-instance pair that builds at the same time the instance and the relation.
20- // ///
21- // /// # Semantics
22- // ///
23- // /// When we deref this object, we are talking about its value.
24- // /// When we get the ref of this object, we are talking about its symbolic value.
25- // #[derive(Clone, Debug, Unit)]
26- // pub struct WitnessInstancePair<T: Unit>(FieldVar, T);
27-
2819/// Allocator for field variables.
2920///
3021/// Creates a new wire identifier when requested,
3122/// and keeps tracks of the wires that have been declared as public.
3223#[ derive( Clone ) ]
3324pub struct VarAllocator < T > {
34- state : Rc < RefCell < AllocatorState < T > > > ,
25+ state : Arc < RwLock < AllocatorState < T > > > ,
3526}
3627
3728struct AllocatorState < T > {
3829 vars_count : usize ,
3930 public_values : Vec < ( FieldVar , T ) > ,
4031}
4132
42- impl < T : Clone > VarAllocator < T > {
33+ impl < T : Clone + Unit > Default for VarAllocator < T > {
34+ fn default ( ) -> Self {
35+ Self :: new ( )
36+ }
37+ }
38+
39+ impl < T : Clone + Unit > VarAllocator < T > {
40+ #[ must_use]
4341 pub fn new ( ) -> Self {
42+ let zero_var = FieldVar :: ZERO ;
4443 Self {
45- state : Rc :: new ( RefCell :: new ( AllocatorState {
46- vars_count : 0 ,
47- public_values : Vec :: new ( ) ,
44+ state : Arc :: new ( RwLock :: new ( AllocatorState {
45+ vars_count : 1 ,
46+ public_values : Vec :: from ( [ ( zero_var , T :: ZERO ) ] ) ,
4847 } ) ) ,
4948 }
5049 }
5150
51+ #[ must_use]
5252 pub fn new_field_var ( & self ) -> FieldVar {
53- let mut state = self . state . borrow_mut ( ) ;
53+ let mut state = self . state . write ( ) ;
5454 let var = FieldVar ( state. vars_count ) ;
5555 state. vars_count += 1 ;
5656 var
5757 }
5858
59+ #[ must_use]
5960 pub fn allocate_vars < const N : usize > ( & self ) -> [ FieldVar ; N ] {
6061 let mut buf = [ FieldVar :: default ( ) ; N ] ;
61- buf. iter_mut ( ) . for_each ( |x| * x = self . new_field_var ( ) ) ;
62+ for x in & mut buf {
63+ * x = self . new_field_var ( ) ;
64+ }
6265 buf
6366 }
6467
68+ #[ must_use]
6569 pub fn allocate_vars_vec ( & self , count : usize ) -> Vec < FieldVar > {
6670 ( 0 ..count) . map ( |_| self . new_field_var ( ) ) . collect ( )
6771 }
@@ -78,12 +82,13 @@ impl<T: Clone> VarAllocator<T> {
7882 vars
7983 }
8084
85+ #[ must_use]
8186 pub fn vars_count ( & self ) -> usize {
82- self . state . borrow ( ) . vars_count
87+ self . state . read ( ) . vars_count
8388 }
8489
8590 pub fn set_public_var ( & self , val : FieldVar , var : T ) {
86- self . state . borrow_mut ( ) . public_values . push ( ( val, var) ) ;
91+ self . state . write ( ) . public_values . push ( ( val, var) ) ;
8792 }
8893
8994 pub fn set_public_vars < Val , Var > (
@@ -94,20 +99,15 @@ impl<T: Clone> VarAllocator<T> {
9499 Var : core:: borrow:: Borrow < FieldVar > ,
95100 Val : core:: borrow:: Borrow < T > ,
96101 {
97- self . state . borrow_mut ( ) . public_values . extend (
102+ self . state . write ( ) . public_values . extend (
98103 vars. into_iter ( )
99104 . zip ( vals)
100105 . map ( |( var, val) | ( * var. borrow ( ) , val. borrow ( ) . clone ( ) ) ) ,
101- )
106+ ) ;
102107 }
103108
109+ #[ must_use]
104110 pub fn public_vars ( & self ) -> Vec < ( FieldVar , T ) > {
105- self . state
106- . borrow ( )
107- . public_values
108- . iter ( )
109- . cloned ( )
110- . map ( |( var, val) | ( var, val. clone ( ) ) )
111- . collect ( )
111+ self . state . read ( ) . public_values . clone ( )
112112 }
113113}
0 commit comments