Skip to content

Commit f377630

Browse files
committed
feat: update API
1 parent 5f3e552 commit f377630

File tree

5 files changed

+197
-162
lines changed

5 files changed

+197
-162
lines changed

Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/src/lib.rs

+14-159
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,27 @@
11
use anyhow::Result;
2-
use num_bigint::BigInt;
3-
use serde_json;
42
use std::collections::HashMap;
3+
use witnesscalc_adapter::*;
54

65
witnesscalc_adapter::witness!(multiplier2);
76
witnesscalc_adapter::witness!(keccak256_256_test);
87
witnesscalc_adapter::witness!(rsa_main);
98

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))
2111
}
2212

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))
2515
}
2616

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)
3319
}
3420

3521
#[cfg(test)]
3622
mod test {
3723

3824
use std::collections::HashMap;
39-
use std::str::FromStr;
4025

4126
use num_bigint::BigInt;
4227
use witnesscalc_adapter::parse_witness_to_bigints;
@@ -48,8 +33,8 @@ mod test {
4833
fn test_witnesscalc() {
4934
let mut inputs = HashMap::new();
5035

51-
let a = BigInt::from(2u8);
52-
let b = BigInt::from(3u8);
36+
let a = "2".to_string();
37+
let b = "3".to_string();
5338

5439
inputs.insert("a".to_string(), vec![a]);
5540
inputs.insert("b".to_string(), vec![b]);
@@ -61,12 +46,7 @@ mod test {
6146

6247
assert_eq!(witness.len(), 4);
6348
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));
7050
assert_eq!(witness[2], BigInt::from(2u8));
7151
assert_eq!(witness[3], BigInt::from(3u8));
7252
}
@@ -75,7 +55,7 @@ mod test {
7555
fn test_keccak256_256_test_witnesscalc() {
7656
let mut inputs = HashMap::new();
7757

78-
inputs.insert("in".to_string(), vec![BigInt::from(0u8); 256]);
58+
inputs.insert("in".to_string(), vec![0u8.to_string(); 256]);
7959

8060
use std::time::Instant;
8161
let start = Instant::now();
@@ -88,138 +68,13 @@ mod test {
8868
}
8969

9070
#[test]
91-
#[ignore]
9271
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");
21974

22075
use std::time::Instant;
22176
let start = Instant::now();
222-
let _ = create_rsa_main_witness(inputs_bigint);
77+
let _ = create_rsa_main_witness(json_input);
22378
let end = Instant::now();
22479
println!("Time taken for rsa_main: {:?}", end.duration_since(start));
22580
}

tests/testdata/rsa_main.json

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
{
2+
"signature": [
3+
"3582320600048169363",
4+
"7163546589759624213",
5+
"18262551396327275695",
6+
"4479772254206047016",
7+
"1970274621151677644",
8+
"6547632513799968987",
9+
"921117808165172908",
10+
"7155116889028933260",
11+
"16769940396381196125",
12+
"17141182191056257954",
13+
"4376997046052607007",
14+
"17471823348423771450",
15+
"16282311012391954891",
16+
"70286524413490741",
17+
"1588836847166444745",
18+
"15693430141227594668",
19+
"13832254169115286697",
20+
"15936550641925323613",
21+
"323842208142565220",
22+
"6558662646882345749",
23+
"15268061661646212265",
24+
"14962976685717212593",
25+
"15773505053543368901",
26+
"9586594741348111792",
27+
"1455720481014374292",
28+
"13945813312010515080",
29+
"6352059456732816887",
30+
"17556873002865047035",
31+
"2412591065060484384",
32+
"11512123092407778330",
33+
"8499281165724578877",
34+
"12768005853882726493"
35+
],
36+
"modulus": [
37+
"13792647154200341559",
38+
"12773492180790982043",
39+
"13046321649363433702",
40+
"10174370803876824128",
41+
"7282572246071034406",
42+
"1524365412687682781",
43+
"4900829043004737418",
44+
"6195884386932410966",
45+
"13554217876979843574",
46+
"17902692039595931737",
47+
"12433028734895890975",
48+
"15971442058448435996",
49+
"4591894758077129763",
50+
"11258250015882429548",
51+
"16399550288873254981",
52+
"8246389845141771315",
53+
"14040203746442788850",
54+
"7283856864330834987",
55+
"12297563098718697441",
56+
"13560928146585163504",
57+
"7380926829734048483",
58+
"14591299561622291080",
59+
"8439722381984777599",
60+
"17375431987296514829",
61+
"16727607878674407272",
62+
"3233954801381564296",
63+
"17255435698225160983",
64+
"15093748890170255670",
65+
"15810389980847260072",
66+
"11120056430439037392",
67+
"5866130971823719482",
68+
"13327552690270163501"
69+
],
70+
"base_message": [
71+
"18114495772705111902",
72+
"2254271930739856077",
73+
"2068851770",
74+
"0",
75+
"0",
76+
"0",
77+
"0",
78+
"0",
79+
"0",
80+
"0",
81+
"0",
82+
"0",
83+
"0",
84+
"0",
85+
"0",
86+
"0",
87+
"0",
88+
"0",
89+
"0",
90+
"0",
91+
"0",
92+
"0",
93+
"0",
94+
"0",
95+
"0",
96+
"0",
97+
"0",
98+
"0",
99+
"0",
100+
"0",
101+
"0",
102+
"0"
103+
]
104+
}

0 commit comments

Comments
 (0)