@@ -192,17 +192,35 @@ def shuffle(sectype, x):
192
192
"""Shuffle list x secretly in-place, and return None.
193
193
194
194
Given list x may contain public or secret elements.
195
+ Elements of x are all numbers or all lists (of the same length) of numbers.
195
196
"""
196
197
n = len (x )
197
- if not isinstance (x [0 ], sectype ): # assume same type for all elts of x
198
- for i in range (len (x )):
199
- x [i ] = sectype (x [i ])
198
+ # assume same type for all elts of x
199
+ x_i_is_list = isinstance (x [0 ], list )
200
+ if not x_i_is_list :
201
+ # elements of x are numbers
202
+ if not isinstance (x [0 ], sectype ):
203
+ for i in range (n ):
204
+ x [i ] = sectype (x [i ])
205
+ for i in range (n - 1 ):
206
+ u = random_unit_vector (sectype , n - i )
207
+ x_u = runtime .in_prod (x [i :], u )
208
+ d = runtime .scalar_mul (x [i ] - x_u , u )
209
+ x [i ] = x_u
210
+ x [i :] = runtime .vector_add (x [i :], d )
211
+ return
212
+
213
+ # elements of x are lists of numbers
214
+ for j in range (len (x [0 ])):
215
+ if not isinstance (x [0 ][j ], sectype ):
216
+ for i in range (n ):
217
+ x [i ][j ] = sectype (x [i ][j ])
200
218
for i in range (n - 1 ):
201
219
u = random_unit_vector (sectype , n - i )
202
- x_u = runtime .in_prod ( x [i :], u )
203
- d = runtime .scalar_mul ( x [ i ] - x_u , u )
220
+ x_u = runtime .matrix_prod ([ u ], x [i :])[ 0 ]
221
+ d = runtime .matrix_prod ([[ a ] for a in u ], [ runtime . vector_sub ( x [ i ], x_u )] )
204
222
x [i ] = x_u
205
- x [i :] = runtime .vector_add (x [i :], d )
223
+ x [i :] = runtime .matrix_add (x [i :], d )
206
224
207
225
208
226
def random_permutation (sectype , x ):
0 commit comments