Skip to content

Commit 689726c

Browse files
add arithmetic operators
1 parent ce7ad35 commit 689726c

File tree

3 files changed

+87
-3
lines changed

3 files changed

+87
-3
lines changed

wasm/src/types/field.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use wasm_bindgen::prelude::*;
3131
/// Field element.
3232
#[wasm_bindgen]
3333
#[derive(Clone, Debug, Eq, PartialEq)]
34-
pub struct Field(FieldNative);
34+
pub struct Field(pub FieldNative);
3535

3636
#[wasm_bindgen]
3737
impl Field {

wasm/src/types/integer.rs

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ use crate::{
2020
types::native::*,
2121
};
2222
use js_sys::{Array, Uint8Array};
23-
use once_cell::sync::OnceCell;
2423
use std::{ops::Deref, str::FromStr};
2524
use wasm_bindgen::prelude::*;
2625

@@ -73,6 +72,91 @@ macro_rules! impl_integer {
7372
to_bits_array_le!(self)
7473
}
7574

75+
/// Checked absolute value.
76+
#[wasm_bindgen(js_name = "absChecked")]
77+
pub fn abs_checked(&self) -> $name {
78+
Self(self.0.abs_checked())
79+
}
80+
81+
/// Wrapped absolute value.
82+
#[wasm_bindgen(js_name = "absWrapped")]
83+
pub fn abs_wrapped(&self) -> $name {
84+
Self(self.0.abs_wrapped())
85+
}
86+
87+
/// Wrapped addition.
88+
#[wasm_bindgen(js_name = "addWrapped")]
89+
pub fn add_wrapped(&self, other: &$name) -> $name {
90+
Self(self.0.add_wrapped(&other.0))
91+
}
92+
93+
/// Wrapped subtraction.
94+
#[wasm_bindgen(js_name = "subWrapped")]
95+
pub fn sub_wrapped(&self, other: &$name) -> $name {
96+
Self(self.0.sub_wrapped(&other.0))
97+
}
98+
99+
/// Wrapped multiplication.
100+
#[wasm_bindgen(js_name = "mulWrapped")]
101+
pub fn mul_wrapped(&self, other: &$name) -> $name {
102+
Self(self.0.mul_wrapped(&other.0))
103+
}
104+
105+
/// Wrapped division.
106+
#[wasm_bindgen(js_name = "divWrapped")]
107+
pub fn div_wrapped(&self, other: &$name) -> $name {
108+
Self(self.0.div_wrapped(&other.0))
109+
}
110+
111+
/// Power.
112+
#[wasm_bindgen(js_name = "pow")]
113+
pub fn pow(&self, other: &$name) -> $name {
114+
Self(self.0.pow(&other.0))
115+
}
116+
117+
/// Remainder.
118+
#[wasm_bindgen(js_name = "rem")]
119+
pub fn rem(&self, other: &$name) -> $name {
120+
Self(self.0.rem(&other.0))
121+
}
122+
123+
/// Wrapped remainder.
124+
#[wasm_bindgen(js_name = "remWrapped")]
125+
pub fn rem_wrapped(&self, other: &$name) -> $name {
126+
Self(self.0.rem_wrapped(&other.0))
127+
}
128+
129+
/// Convert to Scalar.
130+
#[wasm_bindgen(js_name = "toScalar")]
131+
pub fn to_scalar(&self) -> crate::Scalar {
132+
crate::Scalar(self.0.to_scalar())
133+
}
134+
135+
/// Convert from Field.
136+
#[wasm_bindgen(js_name = "fromField")]
137+
pub fn from_field(field: &crate::Field) -> Result<$name, String> {
138+
<$native>::from_field(&field.0)
139+
.map(Self)
140+
.map_err(|e| e.to_string())
141+
}
142+
143+
/// Convert from Fields.
144+
#[wasm_bindgen(js_name = "fromFields")]
145+
pub fn from_fields(fields: js_sys::Array) -> Result<$name, String> {
146+
// Collect JsValue → Field
147+
let rust_fields: Result<Vec<_>, _> = fields.iter().map(|jsv| {
148+
149+
let field_str = jsv.as_string().ok_or("Expected string for Field")?;
150+
let field = FieldNative::from_str(&field_str).map_err(|e| e.to_string())?;
151+
Ok(field)
152+
}).collect();
153+
154+
let rust_fields = rust_fields?;
155+
<$native>::from_fields(&rust_fields)
156+
.map(Self)
157+
.map_err(|e| e.to_string())
158+
}
159+
76160
/// Clone.
77161
pub fn clone(&self) -> $name {
78162
$name(self.0)

wasm/src/types/scalar.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use wasm_bindgen::prelude::*;
3030
/// Scalar field element.
3131
#[wasm_bindgen]
3232
#[derive(Clone, Debug, Eq, PartialEq)]
33-
pub struct Scalar(ScalarNative);
33+
pub struct Scalar(pub ScalarNative);
3434

3535
#[wasm_bindgen]
3636
impl Scalar {

0 commit comments

Comments
 (0)