1
1
use anyhow:: Result ;
2
- use num_bigint:: BigInt ;
3
- use serde_json;
4
2
use std:: collections:: HashMap ;
3
+ use witnesscalc_adapter:: * ;
5
4
6
5
witnesscalc_adapter:: witness!( multiplier2) ;
7
6
witnesscalc_adapter:: witness!( keccak256_256_test) ;
8
7
witnesscalc_adapter:: witness!( rsa_main) ;
9
8
10
- pub fn convert_json ( inputs : HashMap < String , Vec < BigInt > > ) -> String {
11
- //Convert the inputs into a JSON string
12
- let json_map: serde_json:: Map < String , serde_json:: Value > = inputs
13
- . into_iter ( )
14
- . map ( |( key, values) | {
15
- let values_as_strings: Vec < String > = values. iter ( ) . map ( |num| num. to_string ( ) ) . collect ( ) ;
16
- ( key, serde_json:: Value :: from ( values_as_strings) )
17
- } )
18
- . collect ( ) ;
19
- let json = serde_json:: Value :: Object ( json_map) ;
20
- serde_json:: to_string ( & json) . unwrap ( )
9
+ pub fn create_witness ( inputs : HashMap < String , Vec < String > > ) -> Result < Vec < u8 > > {
10
+ multiplier2_witness ( & convert_inputs_to_json ( inputs) )
21
11
}
22
12
23
- pub fn create_witness ( inputs : HashMap < String , Vec < BigInt > > ) -> Result < Vec < u8 > > {
24
- multiplier2_witness ( & convert_json ( inputs) )
13
+ pub fn create_keccak256_256_test_witness ( inputs : HashMap < String , Vec < String > > ) -> Result < Vec < u8 > > {
14
+ keccak256_256_test_witness ( & convert_inputs_to_json ( inputs) )
25
15
}
26
16
27
- pub fn create_keccak256_256_test_witness ( inputs : HashMap < String , Vec < BigInt > > ) -> Result < Vec < u8 > > {
28
- keccak256_256_test_witness ( & convert_json ( inputs) )
29
- }
30
-
31
- pub fn create_rsa_main_witness ( inputs : HashMap < String , Vec < BigInt > > ) -> Result < Vec < u8 > > {
32
- rsa_main_witness ( & convert_json ( inputs) )
17
+ pub fn create_rsa_main_witness ( json_input : String ) -> Result < Vec < u8 > > {
18
+ rsa_main_witness ( & json_input)
33
19
}
34
20
35
21
#[ cfg( test) ]
36
22
mod test {
37
23
38
24
use std:: collections:: HashMap ;
39
- use std:: str:: FromStr ;
40
25
41
26
use num_bigint:: BigInt ;
42
27
use witnesscalc_adapter:: parse_witness_to_bigints;
@@ -48,8 +33,8 @@ mod test {
48
33
fn test_witnesscalc ( ) {
49
34
let mut inputs = HashMap :: new ( ) ;
50
35
51
- let a = BigInt :: from ( 2u8 ) ;
52
- let b = BigInt :: from ( 3u8 ) ;
36
+ let a = "2" . to_string ( ) ;
37
+ let b = "3" . to_string ( ) ;
53
38
54
39
inputs. insert ( "a" . to_string ( ) , vec ! [ a] ) ;
55
40
inputs. insert ( "b" . to_string ( ) , vec ! [ b] ) ;
@@ -61,12 +46,7 @@ mod test {
61
46
62
47
assert_eq ! ( witness. len( ) , 4 ) ;
63
48
assert_eq ! ( witness[ 0 ] , BigInt :: from( 1u8 ) ) ;
64
- assert_eq ! (
65
- witness[ 1 ] ,
66
- BigInt :: from(
67
- 6u8 // 2 x 3
68
- )
69
- ) ;
49
+ assert_eq ! ( witness[ 1 ] , BigInt :: from( 6u8 ) ) ;
70
50
assert_eq ! ( witness[ 2 ] , BigInt :: from( 2u8 ) ) ;
71
51
assert_eq ! ( witness[ 3 ] , BigInt :: from( 3u8 ) ) ;
72
52
}
@@ -75,7 +55,7 @@ mod test {
75
55
fn test_keccak256_256_test_witnesscalc ( ) {
76
56
let mut inputs = HashMap :: new ( ) ;
77
57
78
- inputs. insert ( "in" . to_string ( ) , vec ! [ BigInt :: from ( 0u8 ) ; 256 ] ) ;
58
+ inputs. insert ( "in" . to_string ( ) , vec ! [ 0u8 . to_string ( ) ; 256 ] ) ;
79
59
80
60
use std:: time:: Instant ;
81
61
let start = Instant :: now ( ) ;
@@ -88,138 +68,13 @@ mod test {
88
68
}
89
69
90
70
#[ test]
91
- #[ ignore]
92
71
fn test_rsa_main_witnesscalc ( ) {
93
- let mut inputs = HashMap :: new ( ) ;
94
-
95
- inputs. insert (
96
- "signature" . to_string ( ) ,
97
- vec ! [
98
- "3582320600048169363" ,
99
- "7163546589759624213" ,
100
- "18262551396327275695" ,
101
- "4479772254206047016" ,
102
- "1970274621151677644" ,
103
- "6547632513799968987" ,
104
- "921117808165172908" ,
105
- "7155116889028933260" ,
106
- "16769940396381196125" ,
107
- "17141182191056257954" ,
108
- "4376997046052607007" ,
109
- "17471823348423771450" ,
110
- "16282311012391954891" ,
111
- "70286524413490741" ,
112
- "1588836847166444745" ,
113
- "15693430141227594668" ,
114
- "13832254169115286697" ,
115
- "15936550641925323613" ,
116
- "323842208142565220" ,
117
- "6558662646882345749" ,
118
- "15268061661646212265" ,
119
- "14962976685717212593" ,
120
- "15773505053543368901" ,
121
- "9586594741348111792" ,
122
- "1455720481014374292" ,
123
- "13945813312010515080" ,
124
- "6352059456732816887" ,
125
- "17556873002865047035" ,
126
- "2412591065060484384" ,
127
- "11512123092407778330" ,
128
- "8499281165724578877" ,
129
- "12768005853882726493" ,
130
- ] ,
131
- ) ;
132
-
133
- inputs. insert (
134
- "modulus" . to_string ( ) ,
135
- vec ! [
136
- "13792647154200341559" ,
137
- "12773492180790982043" ,
138
- "13046321649363433702" ,
139
- "10174370803876824128" ,
140
- "7282572246071034406" ,
141
- "1524365412687682781" ,
142
- "4900829043004737418" ,
143
- "6195884386932410966" ,
144
- "13554217876979843574" ,
145
- "17902692039595931737" ,
146
- "12433028734895890975" ,
147
- "15971442058448435996" ,
148
- "4591894758077129763" ,
149
- "11258250015882429548" ,
150
- "16399550288873254981" ,
151
- "8246389845141771315" ,
152
- "14040203746442788850" ,
153
- "7283856864330834987" ,
154
- "12297563098718697441" ,
155
- "13560928146585163504" ,
156
- "7380926829734048483" ,
157
- "14591299561622291080" ,
158
- "8439722381984777599" ,
159
- "17375431987296514829" ,
160
- "16727607878674407272" ,
161
- "3233954801381564296" ,
162
- "17255435698225160983" ,
163
- "15093748890170255670" ,
164
- "15810389980847260072" ,
165
- "11120056430439037392" ,
166
- "5866130971823719482" ,
167
- "13327552690270163501" ,
168
- ] ,
169
- ) ;
170
-
171
- inputs. insert (
172
- "base_message" . to_string ( ) ,
173
- vec ! [
174
- "18114495772705111902" ,
175
- "2254271930739856077" ,
176
- "2068851770" ,
177
- "0" ,
178
- "0" ,
179
- "0" ,
180
- "0" ,
181
- "0" ,
182
- "0" ,
183
- "0" ,
184
- "0" ,
185
- "0" ,
186
- "0" ,
187
- "0" ,
188
- "0" ,
189
- "0" ,
190
- "0" ,
191
- "0" ,
192
- "0" ,
193
- "0" ,
194
- "0" ,
195
- "0" ,
196
- "0" ,
197
- "0" ,
198
- "0" ,
199
- "0" ,
200
- "0" ,
201
- "0" ,
202
- "0" ,
203
- "0" ,
204
- "0" ,
205
- "0" ,
206
- ] ,
207
- ) ;
208
- let inputs_bigint = inputs
209
- . iter ( )
210
- . map ( |( k, v) | {
211
- (
212
- k. clone ( ) ,
213
- v. iter ( )
214
- . map ( |s| BigInt :: from_str ( s) . unwrap ( ) )
215
- . collect :: < Vec < BigInt > > ( ) ,
216
- )
217
- } )
218
- . collect ( ) ;
72
+ let json_input =
73
+ std:: fs:: read_to_string ( "testdata/rsa_main.json" ) . expect ( "Unable to read file" ) ;
219
74
220
75
use std:: time:: Instant ;
221
76
let start = Instant :: now ( ) ;
222
- let _ = create_rsa_main_witness ( inputs_bigint ) ;
77
+ let _ = create_rsa_main_witness ( json_input ) ;
223
78
let end = Instant :: now ( ) ;
224
79
println ! ( "Time taken for rsa_main: {:?}" , end. duration_since( start) ) ;
225
80
}
0 commit comments