use crate::arithmetic::scalars::ScalarTraits;
use crate::arithmetic::scalars::{ScalarCanBeZero, ScalarNonZero};
use derive_more::{Deref, From, Into};
use wasm_bindgen::prelude::*;
#[derive(Copy, Clone, Eq, PartialEq, Debug, From, Into, Deref)]
#[wasm_bindgen(js_name = ScalarNonZero)]
pub struct WASMScalarNonZero(pub(crate) ScalarNonZero);
#[wasm_bindgen(js_class = "ScalarNonZero")]
impl WASMScalarNonZero {
#[wasm_bindgen(js_name = toBytes)]
pub fn to_bytes(&self) -> Vec<u8> {
self.0.to_bytes().to_vec()
}
#[wasm_bindgen(js_name = fromBytes)]
pub fn from_bytes(bytes: Vec<u8>) -> Option<WASMScalarNonZero> {
ScalarNonZero::from_slice(bytes.as_slice()).map(WASMScalarNonZero)
}
#[wasm_bindgen(js_name = fromHex)]
pub fn from_hex(hex: &str) -> Option<WASMScalarNonZero> {
ScalarNonZero::from_hex(hex).map(WASMScalarNonZero)
}
#[wasm_bindgen(js_name = toHex)]
pub fn to_hex(&self) -> String {
self.0.to_hex()
}
#[wasm_bindgen]
pub fn random() -> WASMScalarNonZero {
ScalarNonZero::random(&mut rand::rng()).into()
}
#[wasm_bindgen(js_name = fromHash)]
pub fn from_hash(v: Vec<u8>) -> WASMScalarNonZero {
let mut arr = [0u8; 64];
arr.copy_from_slice(&v);
ScalarNonZero::from_hash(&arr).into()
}
#[wasm_bindgen]
pub fn one() -> WASMScalarNonZero {
ScalarNonZero::one().into()
}
#[wasm_bindgen]
pub fn invert(&self) -> WASMScalarNonZero {
self.0.invert().into()
}
#[wasm_bindgen]
pub fn mul(&self, other: &WASMScalarNonZero) -> WASMScalarNonZero {
(self.0 * other.0).into() }
#[wasm_bindgen(js_name = toCanBeZero)]
pub fn to_can_be_zero(self) -> WASMScalarCanBeZero {
let s: ScalarCanBeZero = self.0.into();
WASMScalarCanBeZero(s)
}
}
#[derive(Copy, Clone, Eq, PartialEq, Debug, From, Into, Deref)]
#[wasm_bindgen(js_name = ScalarCanBeZero)]
pub struct WASMScalarCanBeZero(pub(crate) ScalarCanBeZero);
#[wasm_bindgen(js_class = "ScalarCanBeZero")]
impl WASMScalarCanBeZero {
#[wasm_bindgen(js_name = toBytes)]
pub fn to_bytes(&self) -> Vec<u8> {
self.0.to_bytes().to_vec()
}
#[wasm_bindgen(js_name = fromBytes)]
pub fn from_bytes(bytes: Vec<u8>) -> Option<WASMScalarCanBeZero> {
ScalarCanBeZero::from_slice(bytes.as_slice()).map(WASMScalarCanBeZero)
}
#[wasm_bindgen(js_name = fromHex)]
pub fn from_hex(hex: &str) -> Option<WASMScalarCanBeZero> {
ScalarCanBeZero::from_hex(hex).map(WASMScalarCanBeZero)
}
#[wasm_bindgen(js_name = toHex)]
pub fn to_hex(&self) -> String {
self.0.to_hex()
}
#[wasm_bindgen]
pub fn one() -> WASMScalarCanBeZero {
ScalarCanBeZero::one().into()
}
#[wasm_bindgen]
pub fn zero() -> WASMScalarCanBeZero {
ScalarCanBeZero::zero().into()
}
#[wasm_bindgen]
pub fn random() -> WASMScalarCanBeZero {
ScalarCanBeZero::random(&mut rand::rng()).into()
}
#[wasm_bindgen(js_name = isZero)]
pub fn is_zero(&self) -> bool {
self.0.is_zero()
}
#[wasm_bindgen]
pub fn add(&self, other: &WASMScalarCanBeZero) -> WASMScalarCanBeZero {
(self.0 + other.0).into()
}
#[wasm_bindgen]
pub fn sub(&self, other: &WASMScalarCanBeZero) -> WASMScalarCanBeZero {
(self.0 - other.0).into()
}
#[wasm_bindgen(js_name = toNonZero)]
pub fn to_non_zero(self) -> Option<WASMScalarNonZero> {
let s: ScalarNonZero = self.0.try_into().ok()?;
Some(WASMScalarNonZero(s))
}
}