Skip to content

Commit 3df6935

Browse files
Merge pull request #3217 from o1-labs/rust-native-with-neon
Rust native with `neon-rs`
2 parents 9f484ee + 0fb5b5d commit 3df6935

31 files changed

+556
-637
lines changed

Cargo.lock

Lines changed: 104 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
members = [
33
"arrabbiata",
44
"book",
5+
"crates/*",
56
"curves",
67
"folding",
78
"groupmap",
@@ -13,6 +14,7 @@ members = [
1314
"msm",
1415
"mvpoly",
1516
"o1vm",
17+
"plonk-neon",
1618
"plonk-wasm",
1719
"poly-commitment",
1820
"poseidon",
@@ -87,6 +89,7 @@ tinytemplate = "1.1"
8789
wasm-bindgen = "=0.2.89"
8890
wasm-bindgen-test = ">=0.3.0"
8991

92+
arkworks = { path = "crates/arkworks" }
9093
arrabbiata = { path = "./arrabbiata", version = "0.1.0" }
9194
folding = { path = "./folding", version = "0.1.0" }
9295
groupmap = { path = "./groupmap", version = "0.1.0" }
@@ -108,6 +111,7 @@ saffron = { path = "./poly-commitment", version = "0.1.0" }
108111
signer = { path = "./signer", version = "0.1.0" }
109112
turshi = { path = "./turshi", version = "0.1.0" }
110113
utils = { path = "./utils", version = "0.1.0" }
114+
wasm-types = { path = "./crates/wasm-types" }
111115

112116
[profile.release]
113117
lto = true

crates/arkworks/Cargo.toml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
[package]
2+
name = "arkworks"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[features]
7+
default = ["wasm"]
8+
wasm = []
9+
std = []
10+
11+
[dependencies]
12+
# arkworks
13+
ark-ec.workspace = true
14+
ark-ff.workspace = true
15+
ark-poly.workspace = true
16+
ark-serialize.workspace = true
17+
18+
# proof-systems
19+
mina-curves.workspace = true
20+
num-bigint.workspace = true
21+
rand.workspace = true
22+
wasm-bindgen.workspace = true
23+
wasm-types.workspace = true

plonk-wasm/src/arkworks/bigint_256.rs renamed to crates/arkworks/src/bigint_256.rs

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
extern crate alloc;
2+
3+
use alloc::{string::String, vec::Vec};
14
use ark_ff::{BigInt, BigInteger as ark_BigInteger, BigInteger256};
2-
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize, Write};
5+
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
6+
37
use core::{
48
cmp::Ordering::{Equal, Greater, Less},
59
convert::TryInto,
@@ -25,7 +29,7 @@ pub struct WasmBigInteger256(pub BigInteger256);
2529

2630
impl wasm_bindgen::describe::WasmDescribe for WasmBigInteger256 {
2731
fn describe() {
28-
<Vec<u8> as wasm_bindgen::describe::WasmDescribe>::describe()
32+
<Vec<u8> as wasm_bindgen::describe::WasmDescribe>::describe();
2933
}
3034
}
3135

@@ -40,15 +44,14 @@ impl FromWasmAbi for WasmBigInteger256 {
4044

4145
impl IntoWasmAbi for WasmBigInteger256 {
4246
type Abi = <Vec<u8> as FromWasmAbi>::Abi;
47+
4348
fn into_abi(self) -> Self::Abi {
44-
let mut bytes: Vec<u8> = vec![];
45-
bytes.write_all(self.0.to_bytes_le().as_slice()).unwrap();
46-
bytes.into_abi()
49+
self.0.to_bytes_le().into_abi()
4750
}
4851
}
4952

5053
pub fn to_biguint(x: &BigInteger256) -> BigUint {
51-
let x_ = x.0.as_ptr() as *const u8;
54+
let x_ = x.0.as_ptr().cast::<u8>();
5255
let x_ = unsafe { core::slice::from_raw_parts(x_, BIGINT256_NUM_BYTES) };
5356
num_bigint::BigUint::from_bytes_le(x_)
5457
}
@@ -57,7 +60,7 @@ pub fn of_biguint(x: &BigUint) -> BigInteger256 {
5760
let mut bytes = x.to_bytes_le();
5861
bytes.resize(BIGINT256_NUM_BYTES, 0);
5962
let limbs = bytes.as_ptr();
60-
let limbs = limbs as *const [u64; BIGINT256_NUM_LIMBS as usize];
63+
let limbs = limbs.cast::<[u64; BIGINT256_NUM_LIMBS as usize]>();
6164
let limbs = unsafe { &(*limbs) };
6265
BigInt(*limbs)
6366
}
@@ -103,12 +106,16 @@ pub fn caml_bigint_256_compare(x: WasmBigInteger256, y: WasmBigInteger256) -> i8
103106
}
104107
}
105108

106-
#[wasm_bindgen]
109+
// I don't think this is used anywhere
110+
#[cfg_attr(feature = "std", wasm_bindgen)]
111+
#[cfg(feature = "std")]
107112
pub fn caml_bigint_256_print(x: WasmBigInteger256) {
108113
println!("{}", to_biguint(&x.0));
109114
}
110115

111-
#[wasm_bindgen]
116+
// I don't think this is used anywhere
117+
#[cfg_attr(feature = "std", wasm_bindgen)]
118+
#[cfg(feature = "std")]
112119
pub fn caml_bigint_256_to_string(x: WasmBigInteger256) -> String {
113120
to_biguint(&x.0).to_string()
114121
}
@@ -123,7 +130,9 @@ pub fn caml_bigint_256_test_bit(x: WasmBigInteger256, i: i32) -> bool {
123130

124131
#[wasm_bindgen]
125132
pub fn caml_bigint_256_to_bytes(x: WasmBigInteger256) -> Vec<u8> {
126-
let mut serialized_bytes = vec![];
133+
// This is the size of the implementation of BigInteger256 at the time of writing
134+
// but even if it changes, we will be no worse off than the original code `vec![]`
135+
let mut serialized_bytes = Vec::with_capacity(core::mem::size_of::<WasmBigInteger256>());
127136
x.0.serialize_compressed(&mut serialized_bytes)
128137
.expect("serialize failed");
129138
serialized_bytes
@@ -132,9 +141,7 @@ pub fn caml_bigint_256_to_bytes(x: WasmBigInteger256) -> Vec<u8> {
132141
#[wasm_bindgen]
133142
pub fn caml_bigint_256_of_bytes(x: &[u8]) -> WasmBigInteger256 {
134143
let len = core::mem::size_of::<WasmBigInteger256>();
135-
if x.len() != len {
136-
panic!("caml_bigint_256_of_bytes");
137-
};
144+
assert!(x.len() == len, "caml_bigint_256_of_bytes");
138145
// TODO this used FromBytes before arkworks 0.4.2, check serialization is consistent after update
139146
WasmBigInteger256(
140147
BigInteger256::deserialize_compressed(&mut &x[..]).expect("deserialization error"),

plonk-wasm/src/arkworks/group_affine.rs renamed to crates/arkworks/src/group_affine.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::arkworks::{pasta_fp::WasmPastaFp, pasta_fq::WasmPastaFq};
1+
use crate::{pasta_fp::WasmPastaFp, pasta_fq::WasmPastaFq};
22
use mina_curves::pasta::{
33
curves::{
44
pallas::{G_GENERATOR_X as GeneratorPallasX, G_GENERATOR_Y as GeneratorPallasY},

crates/arkworks/src/lib.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#![cfg_attr(not(feature = "std"), no_std)]
2+
3+
mod bigint_256;
4+
mod group_affine;
5+
mod group_projective;
6+
mod pasta_fp;
7+
mod pasta_fq;
8+
9+
pub use bigint_256::WasmBigInteger256;
10+
pub use group_affine::{WasmGPallas, WasmGVesta};
11+
pub use group_projective::{WasmPallasGProjective, WasmVestaGProjective};
12+
pub use pasta_fp::WasmPastaFp;
13+
pub use pasta_fq::WasmPastaFq;

0 commit comments

Comments
 (0)