Skip to content

Commit b53484b

Browse files
Merge pull request #28 from ICME-Lab/feat/frontend-improvements
Feat/frontend improvements
2 parents 2c309b8 + 2b16f89 commit b53484b

File tree

26 files changed

+2183
-1409
lines changed

26 files changed

+2183
-1409
lines changed

src/tests.rs

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,8 @@ fn test_bulk_ops() -> Result<(), ZKWASMError> {
140140
.file_path(PathBuf::from("wasm/misc/bulk-ops.wat"))?
141141
.func_args(vec!["200".to_string()])
142142
.build();
143-
144143
let wasm_ctx = WASMCtx::new(wasm_args);
145-
146144
test_wasm_snark_with(wasm_ctx, step_size)?;
147-
148145
Ok(())
149146
}
150147

@@ -157,9 +154,7 @@ fn test_kth_factor() -> Result<(), ZKWASMError> {
157154
.func_args(vec!["250".to_string(), "15".to_string()])
158155
.invoke("kth_factor")
159156
.build();
160-
161157
let wasm_ctx = WASMCtx::new(wasm_args);
162-
163158
test_wasm_snark_with(wasm_ctx, step_size)?;
164159
Ok(())
165160
}
@@ -174,9 +169,7 @@ fn test_integer_hash() {
174169
.func_args(vec!["100".to_string()])
175170
.invoke("integer_hash")
176171
.build();
177-
178172
let wasm_ctx = WASMCtx::new(wasm_args);
179-
180173
test_wasm_snark_with(wasm_ctx, step_size).unwrap();
181174
}
182175

@@ -189,7 +182,6 @@ fn test_gradient_boosting() {
189182
.unwrap()
190183
.invoke("_start")
191184
.build();
192-
193185
let wasm_ctx = WasiWASMCtx::new(wasm_args);
194186
test_wasm_snark_with(wasm_ctx, step_size).unwrap();
195187
}
@@ -198,7 +190,6 @@ fn test_gradient_boosting() {
198190
fn test_uni_poly_eval() {
199191
let x = "1";
200192
let size = "10";
201-
202193
let step_size = StepSize::new(2_500).set_memory_step_size(50_000);
203194
init_logger();
204195
let wasm_args = WASMArgsBuilder::default()
@@ -207,7 +198,6 @@ fn test_uni_poly_eval() {
207198
.invoke("eval")
208199
.func_args(vec![String::from(x), String::from(size)])
209200
.build();
210-
211201
let wasm_ctx = WasiWASMCtx::new(wasm_args);
212202
test_wasm_snark_with(wasm_ctx, step_size).unwrap();
213203
}
@@ -237,10 +227,8 @@ fn test_fib_large() -> Result<(), ZKWASMError> {
237227
.invoke("fib")
238228
.func_args(vec![String::from("1000")])
239229
.build();
240-
241230
let wasm_ctx = WASMCtx::new(wasm_args);
242231
test_wasm_snark_with(wasm_ctx, step_size)?;
243-
244232
Ok(())
245233
}
246234

@@ -253,10 +241,8 @@ fn test_fib_small() -> Result<(), ZKWASMError> {
253241
.invoke("fib")
254242
.func_args(vec![String::from("16")])
255243
.build();
256-
257244
let wasm_ctx = WASMCtx::new(wasm_args);
258245
test_wasm_snark_with(wasm_ctx, step_size)?;
259-
260246
Ok(())
261247
}
262248

@@ -283,9 +269,7 @@ fn test_defi_transaction() {
283269
])
284270
.invoke("main")
285271
.build();
286-
287272
let wasm_ctx = WASMCtx::new(wasm_args);
288-
289273
test_wasm_snark_with(wasm_ctx, step_size).unwrap();
290274
}
291275

@@ -311,9 +295,7 @@ fn test_energy_consumption() {
311295
])
312296
.invoke("main")
313297
.build();
314-
315298
let wasm_ctx = WASMCtx::new(wasm_args);
316-
317299
test_wasm_snark_with(wasm_ctx, step_size).unwrap();
318300
}
319301

@@ -342,7 +324,6 @@ fn test_toy_rsa() {
342324
.build();
343325

344326
let wasm_ctx = WASMCtx::new(wasm_args);
345-
346327
test_wasm_snark_with(wasm_ctx, step_size).unwrap();
347328
}
348329

@@ -367,9 +348,7 @@ fn test_financial_protocol() {
367348
])
368349
.invoke("main")
369350
.build();
370-
371351
let wasm_ctx = WASMCtx::new(wasm_args);
372-
373352
test_wasm_snark_with(wasm_ctx, step_size).unwrap();
374353
}
375354

@@ -395,9 +374,7 @@ fn test_game_logic() {
395374
])
396375
.invoke("main")
397376
.build();
398-
399377
let wasm_ctx = WASMCtx::new(wasm_args);
400-
401378
test_wasm_snark_with(wasm_ctx, step_size).unwrap();
402379
}
403380

@@ -425,7 +402,6 @@ fn test_data_provenance() {
425402
.build();
426403

427404
let wasm_ctx = WASMCtx::new(wasm_args);
428-
429405
test_wasm_snark_with(wasm_ctx, step_size).unwrap();
430406
}
431407

@@ -453,7 +429,6 @@ fn test_regulatory_compliance() {
453429
.build();
454430

455431
let wasm_ctx = WASMCtx::new(wasm_args);
456-
457432
test_wasm_snark_with(wasm_ctx, step_size).unwrap();
458433
}
459434

@@ -480,6 +455,5 @@ fn test_smart_contract_audit() {
480455
.build();
481456

482457
let wasm_ctx = WASMCtx::new(wasm_args);
483-
484458
test_wasm_snark_with(wasm_ctx, step_size).unwrap();
485459
}

src/utils/tracing.rs

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ mod test {
3939
/// Count how many time an opcode gets used. Uses the J index of the opcode
4040
fn count_opcodes(vms: &[WitnessVM]) -> HashMap<u64, usize> {
4141
let capacity = wasmi::Instruction::MAX_J + 1;
42-
4342
let mut opcodes_count = HashMap::with_capacity(capacity as usize);
4443

4544
for c in 0..capacity {
@@ -103,6 +102,19 @@ mod test {
103102
wasmi::Instruction::MemoryGrow,
104103
wasmi::Instruction::MemoryFill,
105104
wasmi::Instruction::MemoryCopy,
105+
// locals
106+
wasmi::Instruction::LocalGet(0.into()),
107+
wasmi::Instruction::LocalSet(0.into()),
108+
wasmi::Instruction::LocalTee(0.into()),
109+
// Br's
110+
wasmi::Instruction::Br(0.into()),
111+
wasmi::Instruction::BrIfEqz(0.into()),
112+
wasmi::Instruction::BrIfNez(0.into()),
113+
wasmi::Instruction::BrTable(0.try_into().unwrap()),
114+
wasmi::Instruction::BrAdjust(0.into()),
115+
// store/load
116+
wasmi::Instruction::I64Store(0.into()),
117+
wasmi::Instruction::I64Load(0.into()),
106118
];
107119

108120
for instr_to_count in instrs_to_count.iter() {
@@ -235,9 +247,7 @@ mod test {
235247
])
236248
.invoke("main")
237249
.build();
238-
239250
let wasm_ctx = WASMCtx::new(wasm_args);
240-
241251
test_count_with(&wasm_ctx);
242252
}
243253

@@ -260,9 +270,7 @@ mod test {
260270
])
261271
.invoke("main")
262272
.build();
263-
264273
let wasm_ctx = WASMCtx::new(wasm_args);
265-
266274
test_count_with(&wasm_ctx);
267275
}
268276

@@ -285,9 +293,7 @@ mod test {
285293
])
286294
.invoke("main")
287295
.build();
288-
289296
let wasm_ctx = WASMCtx::new(wasm_args);
290-
291297
test_count_with(&wasm_ctx);
292298
}
293299

@@ -309,9 +315,7 @@ mod test {
309315
])
310316
.invoke("main")
311317
.build();
312-
313318
let wasm_ctx = WASMCtx::new(wasm_args);
314-
315319
test_count_with(&wasm_ctx);
316320
}
317321

@@ -323,9 +327,7 @@ mod test {
323327
.func_args(vec!["100".to_string()])
324328
.invoke("integer_hash")
325329
.build();
326-
327330
let wasm_ctx = WASMCtx::new(wasm_args);
328-
329331
test_count_with(&wasm_ctx);
330332
}
331333

@@ -336,9 +338,31 @@ mod test {
336338
.unwrap()
337339
.invoke("_start")
338340
.build();
341+
let wasm_ctx = WasiWASMCtx::new(wasm_args);
342+
test_count_with(&wasm_ctx);
343+
}
339344

345+
#[test]
346+
fn test_count_eq_func() {
347+
let wasm_args = WASMArgsBuilder::default()
348+
.file_path(PathBuf::from("wasm/nebula/eq_func.wat"))
349+
.unwrap()
350+
.invoke("eq_func")
351+
.func_args(vec!["255".to_string(), "255".to_string()])
352+
.build();
340353
let wasm_ctx = WasiWASMCtx::new(wasm_args);
354+
test_count_with(&wasm_ctx);
355+
}
341356

357+
#[test]
358+
fn test_count_kth_factor() {
359+
let wasm_args = WASMArgsBuilder::default()
360+
.file_path(PathBuf::from("wasm/nebula/kth_factor.wat"))
361+
.unwrap()
362+
.func_args(vec!["250".to_string(), "15".to_string()])
363+
.invoke("kth_factor")
364+
.build();
365+
let wasm_ctx = WASMCtx::new(wasm_args);
342366
test_count_with(&wasm_ctx);
343367
}
344368
}

src/wasm_snark/gadgets/int.rs

Lines changed: 1 addition & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
use bellpepper::gadgets::Assignment;
2-
use bellpepper_core::{
3-
boolean::{AllocatedBit, Boolean},
4-
num::AllocatedNum,
5-
ConstraintSystem, SynthesisError,
6-
};
2+
use bellpepper_core::{num::AllocatedNum, ConstraintSystem, SynthesisError};
73
use ff::PrimeField;
84

95
pub fn add<F: PrimeField, CS: ConstraintSystem<F>>(
@@ -181,53 +177,3 @@ pub fn alloc_negate<F: PrimeField, CS: ConstraintSystem<F>>(
181177

182178
Ok(b)
183179
}
184-
185-
/// Checks if AllocatedNum is zero
186-
pub fn eqz_bit<F, CS>(mut cs: CS, a: &AllocatedNum<F>) -> Result<Boolean, SynthesisError>
187-
where
188-
F: PrimeField,
189-
CS: ConstraintSystem<F>,
190-
{
191-
let a_val = a.get_value();
192-
let is_zero = a_val.map(|val| val == F::ZERO);
193-
194-
// result = (a == 0)
195-
let result = AllocatedBit::alloc(cs.namespace(|| "a == 0"), is_zero)?;
196-
197-
// result * a = 0
198-
// This means that at least one of result or a is zero.
199-
cs.enforce(
200-
|| "result or a is 0",
201-
|lc| lc + result.get_variable(),
202-
|lc| lc + a.get_variable(),
203-
|lc| lc,
204-
);
205-
206-
// Inverse of `a`, if it exists, otherwise one.
207-
let a_fe = a_val.unwrap_or(F::ZERO);
208-
let q = cs.alloc(
209-
|| "q",
210-
|| {
211-
let tmp = a_fe.invert();
212-
if tmp.is_some().into() {
213-
Ok(tmp.unwrap())
214-
} else {
215-
Ok(F::ONE)
216-
}
217-
},
218-
)?;
219-
220-
// (a + result) * q = 1.
221-
// This enforces that a and result are not both 0.
222-
cs.enforce(
223-
|| "(a + result) * q = 1",
224-
|lc| lc + a.get_variable() + result.get_variable(),
225-
|lc| lc + q,
226-
|lc| lc + CS::one(),
227-
);
228-
229-
// Taken together, these constraints enforce that exactly one of `a` and `result` is 0.
230-
// Since result is constrained to be boolean, that means `result` is true iff `a` is 0.
231-
232-
Ok(Boolean::Is(result))
233-
}

0 commit comments

Comments
 (0)