Skip to content

Commit 4d7a44f

Browse files
committed
feat(maci-crypto): support ElGamal alg
1 parent 7d9e149 commit 4d7a44f

File tree

22 files changed

+1992
-102
lines changed

22 files changed

+1992
-102
lines changed

Makefile

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
.PHONY: wasm wasm-amaci wasm-registry wasm-api-maci wasm-api-saas wasm-test
2+
.PHONY: schema schema-amaci schema-registry schema-api-maci schema-api-saas
3+
.PHONY: test unit-test clean optimize help
4+
5+
# Compile all contracts to wasm
6+
wasm:
7+
@echo "Building all contracts..."
8+
@cd contracts/amaci && cargo wasm
9+
@cd contracts/registry && cargo wasm
10+
@cd contracts/api-maci && cargo wasm
11+
@cd contracts/api-saas && cargo wasm
12+
@cd contracts/test && cargo wasm
13+
@echo "✅ All contracts built successfully"
14+
15+
# Compile individual contracts
16+
wasm-amaci:
17+
@cd contracts/amaci && cargo wasm
18+
19+
wasm-registry:
20+
@cd contracts/registry && cargo wasm
21+
22+
wasm-api-maci:
23+
@cd contracts/api-maci && cargo wasm
24+
25+
wasm-api-saas:
26+
@cd contracts/api-saas && cargo wasm
27+
28+
wasm-test:
29+
@cd contracts/test && cargo wasm
30+
31+
# Generate schemas for all contracts
32+
schema:
33+
@echo "Generating schemas..."
34+
@cd contracts/amaci && cargo schema
35+
@cd contracts/registry && cargo schema
36+
@cd contracts/api-maci && cargo schema
37+
@cd contracts/api-saas && cargo schema
38+
@echo "✅ All schemas generated"
39+
40+
# Generate schema for individual contracts
41+
schema-amaci:
42+
@cd contracts/amaci && cargo schema
43+
44+
schema-registry:
45+
@cd contracts/registry && cargo schema
46+
47+
schema-api-maci:
48+
@cd contracts/api-maci && cargo schema
49+
50+
schema-api-saas:
51+
@cd contracts/api-saas && cargo schema
52+
53+
# Run all tests (contracts + crates)
54+
test:
55+
@cargo test
56+
57+
# Run unit tests for all contracts
58+
unit-test:
59+
@echo "Running contract unit tests..."
60+
@cd contracts/amaci && cargo unit-test
61+
@cd contracts/registry && cargo unit-test
62+
@cd contracts/api-maci && cargo unit-test
63+
@cd contracts/api-saas && cargo unit-test
64+
@cd contracts/test && cargo unit-test
65+
@echo "✅ All contract tests passed"
66+
67+
# Run tests for crates only
68+
test-crates:
69+
@echo "Running crates tests..."
70+
@cd crates/baby-jubjub && cargo test
71+
@cd crates/maci-utils && cargo test
72+
@cd crates/maci-crypto && cargo test
73+
@cd crates/eddsa-poseidon && cargo test
74+
@cd crates/crypto-test-gen && cargo test
75+
@echo "✅ All crate tests passed"
76+
77+
# Optimize wasm files (requires wasm-opt)
78+
optimize:
79+
@echo "Optimizing wasm files..."
80+
@for file in target/wasm32-unknown-unknown/release/*.wasm; do \
81+
if [ -f "$$file" ]; then \
82+
echo "Optimizing $$file..."; \
83+
wasm-opt -Os "$$file" -o "$${file%.wasm}_optimized.wasm"; \
84+
fi \
85+
done
86+
@echo "✅ Optimization complete"
87+
88+
# Clean build artifacts
89+
clean:
90+
@cargo clean
91+
@echo "✅ Clean complete"
92+
93+
# Show help
94+
help:
95+
@echo "MACI Workspace Commands:"
96+
@echo ""
97+
@echo " make wasm - Build all contracts to wasm"
98+
@echo " make wasm-amaci - Build amaci contract"
99+
@echo " make wasm-registry - Build registry contract"
100+
@echo " make wasm-api-maci - Build api-maci contract"
101+
@echo " make wasm-api-saas - Build api-saas contract"
102+
@echo " make wasm-test - Build test contract"
103+
@echo ""
104+
@echo " make schema - Generate schemas for all contracts"
105+
@echo " make schema-amaci - Generate schema for amaci"
106+
@echo ""
107+
@echo " make test - Run all tests (contracts + crates)"
108+
@echo " make unit-test - Run unit tests for all contracts"
109+
@echo " make test-crates - Run tests for all crates"
110+
@echo ""
111+
@echo " make optimize - Optimize wasm files (requires wasm-opt)"
112+
@echo " make clean - Clean build artifacts"
113+
@echo " make help - Show this help message"
114+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[alias]
2+
wasm = "build --release --lib --target wasm32-unknown-unknown"
3+
unit-test = "test --lib"
4+
schema = "run --bin schema"
5+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[alias]
2+
wasm = "build --release --lib --target wasm32-unknown-unknown"
3+
unit-test = "test --lib"
4+
schema = "run --bin schema"
5+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[alias]
2+
wasm = "build --release --lib --target wasm32-unknown-unknown"
3+
unit-test = "test --lib"
4+
schema = "run --bin schema"
5+

contracts/test/.cargo/config.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[alias]
2+
wasm = "build --release --lib --target wasm32-unknown-unknown"
3+
unit-test = "test --lib"
4+
schema = "run --bin schema"
5+

crates/baby-jubjub/examples/basic_operations.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ fn main() {
3838
);
3939

4040
// Verify unpacked point matches original
41-
if unpacked_point == public_key {
41+
// Compare in projective coordinates to handle different representations
42+
use baby_jubjub::EdwardsProjective;
43+
if EdwardsProjective::from(unpacked_point) == EdwardsProjective::from(public_key) {
4244
println!("✓ Unpacked point matches original point");
4345
} else {
4446
println!("✗ Unpacked point does not match original point");
@@ -52,4 +54,3 @@ fn main() {
5254
// Check if point is on curve
5355
println!("inCurve: {}", in_curve(&public_key));
5456
}
55-

crates/baby-jubjub/src/lib.rs

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,11 @@ pub fn gen_random_babyjub_value() -> BigUint {
122122
}
123123

124124
// Compute the private key modulo 2^253 (as per the TS implementation)
125-
let modulo = BigUint::from(2u32).pow(253);
125+
// Precomputed: 2^253 = 14474011154664524427946373126085988481658748083205070504932198000989141204992
126+
const MODULO_2_253: &str =
127+
"14474011154664524427946373126085988481658748083205070504932198000989141204992";
128+
let modulo =
129+
BigUint::parse_bytes(MODULO_2_253.as_bytes(), 10).expect("Failed to parse modulo constant");
126130
&rand_val % &modulo
127131
}
128132

@@ -346,17 +350,21 @@ mod tests {
346350

347351
let expected_base_point = EdwardsAffine::new_unchecked(BASE_X, BASE_Y);
348352
let cofactor = EdFr::from_be_bytes_mod_order(&[BabyJubjubConfig::COFACTOR[0] as u8]);
349-
let calculated_base_point = (g * cofactor).into_affine();
353+
let calculated_base_point = g * cofactor;
350354

351-
assert_eq!(calculated_base_point, expected_base_point);
355+
assert_eq!(
356+
calculated_base_point,
357+
EdwardsProjective::from(expected_base_point)
358+
);
352359
}
353360

354361
#[test]
355362
fn test_base_point_order() {
356363
let base_point = EdwardsAffine::new_unchecked(GENERATOR_X, GENERATOR_Y);
357364

358-
let result = (base_point * SUBGROUP_ORDER).into_affine();
359-
let identity = EdwardsAffine::new_unchecked(Fq::zero(), Fq::ONE);
365+
let result = base_point * SUBGROUP_ORDER;
366+
// Identity in projective coordinates is (0, 1, 0, 1) for twisted Edwards
367+
let identity = EdwardsProjective::new(Fq::zero(), Fq::ONE, Fq::zero(), Fq::ONE);
360368

361369
assert_eq!(result, identity);
362370
}
@@ -365,7 +373,10 @@ mod tests {
365373
fn test_base8() {
366374
let base8_point = base8();
367375
let expected = EdwardsAffine::new_unchecked(BASE_X, BASE_Y);
368-
assert_eq!(base8_point, expected);
376+
assert_eq!(
377+
EdwardsProjective::from(base8_point),
378+
EdwardsProjective::from(expected)
379+
);
369380
assert!(base8_point.is_on_curve());
370381
}
371382

@@ -387,9 +398,9 @@ mod tests {
387398
);
388399

389400
let result = add_point(&p1, &p2);
390-
let expected = (p1 + p2).into_affine();
401+
let expected = p1 + p2;
391402

392-
assert_eq!(result, expected);
403+
assert_eq!(EdwardsProjective::from(result), expected);
393404
assert!(result.is_on_curve());
394405
}
395406

@@ -399,9 +410,9 @@ mod tests {
399410
let scalar = EdFr::from(324u64);
400411

401412
let result = mul_point_escalar(&base8_point, scalar);
402-
let expected = (EdwardsProjective::from(base8_point) * scalar).into_affine();
413+
let expected = EdwardsProjective::from(base8_point) * scalar;
403414

404-
assert_eq!(result, expected);
415+
assert_eq!(EdwardsProjective::from(result), expected);
405416
assert!(result.is_on_curve());
406417
}
407418

@@ -421,7 +432,10 @@ mod tests {
421432
let packed = pack_point(&point);
422433
let unpacked = unpack_point(&packed).expect("Failed to unpack point");
423434

424-
assert_eq!(point, unpacked);
435+
assert_eq!(
436+
EdwardsProjective::from(point),
437+
EdwardsProjective::from(unpacked)
438+
);
425439
assert!(unpacked.is_on_curve());
426440
}
427441

@@ -435,7 +449,10 @@ mod tests {
435449
let packed = pack_point(&public_key);
436450
let unpacked = unpack_point(&packed).expect("Failed to unpack point");
437451

438-
assert_eq!(public_key, unpacked);
452+
assert_eq!(
453+
EdwardsProjective::from(public_key),
454+
EdwardsProjective::from(unpacked)
455+
);
439456
assert!(unpacked.is_on_curve());
440457
}
441458

@@ -452,7 +469,12 @@ mod tests {
452469
let packed = pack_point(&point);
453470
let unpacked = unpack_point(&packed).expect("Failed to unpack point");
454471

455-
assert_eq!(point, unpacked, "Failed for scalar {}", scalar_val);
472+
assert_eq!(
473+
EdwardsProjective::from(point),
474+
EdwardsProjective::from(unpacked),
475+
"Failed for scalar {}",
476+
scalar_val
477+
);
456478
assert!(unpacked.is_on_curve());
457479
}
458480
}
@@ -508,7 +530,8 @@ mod tests {
508530
// Unpack the point and verify it matches
509531
let unpacked_point = unpack_point(&packed_point).expect("Failed to unpack point");
510532
assert_eq!(
511-
public_key, unpacked_point,
533+
EdwardsProjective::from(public_key),
534+
EdwardsProjective::from(unpacked_point),
512535
"Unpacked point should match original"
513536
);
514537

0 commit comments

Comments
 (0)