Skip to content

Commit

Permalink
Merge pull request #44 from iosis-tech/better_NPM
Browse files Browse the repository at this point in the history
Better npm
  • Loading branch information
Okm165 authored Sep 29, 2024
2 parents 576052b + e3826e8 commit 3589066
Show file tree
Hide file tree
Showing 8 changed files with 857 additions and 30 deletions.
16 changes: 8 additions & 8 deletions cli/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 40 additions & 1 deletion wasm_bindings/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,40 @@
pkg/
# Node modules
node_modules/

# Output directories
pkg/
dist/

# Generated files from scripts
generated_imports.ts
generated_verifier_map.ts

# Logs
*.log

# TypeScript compiled output
*.js
*.js.map

# IDE settings
.vscode/
.idea/
*.sublime-project
*.sublime-workspace

# Environment variables
.env
.env.local
.env.*.local

# NPM/Yarn specific files
package-lock.json
yarn.lock

# Operating System files
.DS_Store
Thumbs.db

# Other files to ignore
*.tgz
*.zip
18 changes: 9 additions & 9 deletions wasm_bindings/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 7 additions & 12 deletions wasm_bindings/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,13 @@ Swiftness is a Rust implementation of the Cairo-VM STARK verifier with layouts,
## Example Usage

```js
import init, { verify_proof } from "swiftness-{layout}-{commitment hash}";

async function run(proof_json) {
await init(); // Initialize the Wasm module
try {
const [programHash, programOutput] = JSON.parse(
await verify_proof(proof_json),
);
} catch (err) {
console.error(`Verification failed: ${err}`);
}
}
import { loadSwiftnessModule } from "swiftness";

// Load the module and verify the proof
let [parse_proof, verify_proof] = await loadSwiftnessModule(layout, commitment, stone);
const output = JSON.parse(verify_proof(parse_proof(proof)));

console.log({ programHash: output['program_hash'], outputHash: output['output_hash'] });
```

## Contributing
Expand Down
88 changes: 88 additions & 0 deletions wasm_bindings/generate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#!/bin/bash

# Define arrays for the different layouts, hashers, and stones
LAYOUTS=("dex" "recursive" "recursive_with_poseidon" "small" "starknet" "starknet_with_keccak" "dynamic")
HASHERS=("keccak_160_lsb" "keccak_248_lsb" "blake2s_160_lsb" "blake2s_248_lsb")
STONES=("stone5" "stone6")

# Function to build WASM packages
build_wasm_packages() {
for layout in "${LAYOUTS[@]}"; do
for hasher in "${HASHERS[@]}"; do
for stone in "${STONES[@]}"; do
# Define output directory and features
output_dir="src/pkg/swiftness_${layout}_${hasher}_${stone}"
features="${layout},${hasher},${stone}"

# Print message
echo "Building WASM package with layout=$layout, hasher=$hasher, stone=$stone"

# Run wasm-pack build with the specified features and output directory
wasm-pack build --out-dir "$output_dir" --target web --features "$features" --no-default-features
done
done
done
}

# Function to generate import statements
generate_imports() {
local output_file="generated_imports.ts"
> "$output_file" # Clear the file if it exists

for layout in "${LAYOUTS[@]}"; do
for hasher in "${HASHERS[@]}"; do
for stone in "${STONES[@]}"; do
# Construct package and function names
pkg_name="swiftness_${layout}_${hasher}_${stone}"
parse_fn="parse_proof_${pkg_name}"
verify_fn="verify_proof_${pkg_name}"

# Generate import statement and append to output file
echo "import init_${pkg_name}, {" >> "$output_file"
echo " parse_proof as ${parse_fn}," >> "$output_file"
echo " verify_proof as ${verify_fn}," >> "$output_file"
echo "} from \"./pkg/${pkg_name}\";" >> "$output_file"
echo "" >> "$output_file"
done
done
done

echo "Import statements have been generated and saved to $output_file."
}

# Function to generate the VerifierMap
generate_verifier_map() {
local output_file="generated_verifier_map.ts"
> "$output_file" # Clear the file if it exists

# Write the initial part of the VerifierMap object
echo "const verifier_map: VerifierMap = {" >> "$output_file"

for layout in "${LAYOUTS[@]}"; do
for hasher in "${HASHERS[@]}"; do
for stone in "${STONES[@]}"; do
# Construct the unique identifiers
map_key="\${Layout.${layout^^}}_\${Commitment.${hasher^^}}_\${Stone.${stone^^}}"
pkg_name="swiftness_${layout}_${hasher}_${stone}"
parse_fn="parse_proof_${pkg_name}"
verify_fn="verify_proof_${pkg_name}"

# Generate TypeScript entry for each combination
echo " [\`${map_key}\`]: [" >> "$output_file"
echo " init_${pkg_name}," >> "$output_file"
echo " ${parse_fn}," >> "$output_file"
echo " ${verify_fn}," >> "$output_file"
echo " ]," >> "$output_file"
done
done
done

# Close the VerifierMap object
echo "};" >> "$output_file"
echo "Verifier map has been generated and saved to $output_file."
}

# Main execution
build_wasm_packages
# generate_imports
# generate_verifier_map
27 changes: 27 additions & 0 deletions wasm_bindings/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "swiftness",
"type": "module",
"description": "Swiftness CairoVM Verifier",
"version": "0.1.2",
"license": "SEE LICENSE IN LICENSE",
"repository": {
"type": "git",
"url": "https://github.com/iosis-tech/swiftness"
},
"files": [
"dist/*",
"swiftness.ts"
],
"main": "dist/swiftness.js",
"types": "dist/swiftness.d.ts",
"sideEffects": [
"./snippets/*"
],
"devDependencies": {
"typescript": "^4.x.x"
},
"scripts": {
"build": "tsc && cp -r src/pkg dist",
"prepublishOnly": "npm run build"
}
}
Loading

0 comments on commit 3589066

Please sign in to comment.