Skip to content

Commit 38c5cca

Browse files
add boolean type
1 parent e17086b commit 38c5cca

File tree

3 files changed

+192
-1
lines changed

3 files changed

+192
-1
lines changed

wasm/src/types/boolean.rs

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
// Copyright (C) 2019-2025 Provable Inc.
2+
// This file is part of the Provable SDK library.
3+
4+
// The Provable SDK library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
9+
// The Provable SDK library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
14+
// You should have received a copy of the GNU General Public License
15+
// along with the Provable SDK library. If not, see <https://www.gnu.org/licenses/>.
16+
17+
use crate::{
18+
Plaintext,
19+
from_js_typed_array,
20+
to_bits_array_le,
21+
types::native::{BooleanNative, LiteralNative, PlaintextNative},
22+
};
23+
use snarkvm_console::prelude::{Double, FromBits, FromBytes, One, Pow, ToBits, ToBytes, Zero};
24+
use snarkvm_wasm::utilities::Uniform;
25+
26+
use js_sys::{Array, Uint8Array};
27+
use once_cell::sync::OnceCell;
28+
use std::{ops::Deref, str::FromStr};
29+
use wasm_bindgen::prelude::*;
30+
31+
/// Boolean element.
32+
#[wasm_bindgen]
33+
#[derive(Clone, Debug, Eq, PartialEq)]
34+
pub struct Boolean(BooleanNative);
35+
36+
37+
#[wasm_bindgen]
38+
impl Boolean {
39+
/// Creates a boolean object from a string representation ("true"/"false").
40+
#[wasm_bindgen(js_name = "fromString")]
41+
#[allow(clippy::should_implement_trait)]
42+
pub fn from_string(boolean: &str) -> Result<Boolean, String> {
43+
Ok(Self(BooleanNative::from_str(boolean).map_err(|e| e.to_string())?))
44+
}
45+
46+
/// Returns the string representation of the boolean element.
47+
#[wasm_bindgen(js_name = "toString")]
48+
#[allow(clippy::inherent_to_string)]
49+
pub fn to_string(&self) -> String {
50+
self.0.to_string()
51+
}
52+
53+
/// Create a boolean element from a Uint8Array of left endian bytes.
54+
#[wasm_bindgen(js_name = "fromBytesLe")]
55+
pub fn from_bytes_le(bytes: &Uint8Array) -> Result<Boolean, String> {
56+
let bytes = bytes.to_vec();
57+
let boolean = BooleanNative::from_bytes_le(&bytes).map_err(|e| e.to_string())?;
58+
Ok(Boolean(boolean))
59+
}
60+
61+
/// Encode the boolean element as a Uint8Array of left endian bytes.
62+
#[wasm_bindgen(js_name = "toBytesLe")]
63+
pub fn to_bytes_le(&self) -> Result<Uint8Array, String> {
64+
let bytes = self.0.to_bytes_le().map_err(|e| e.to_string())?;
65+
Ok(Uint8Array::from(bytes.as_slice()))
66+
}
67+
68+
/// Reconstruct a boolean element from a boolean array representation.
69+
#[wasm_bindgen(js_name = "fromBitsLe")]
70+
pub fn from_bits_le(bits: &Array) -> Result<Boolean, String> {
71+
let bit_vec = from_js_typed_array!(bits, as_bool, "boolean")?;
72+
if bit_vec.len() != 1 {
73+
return Err("Boolean expects exactly 1 bit".to_string());
74+
}
75+
let boolean = BooleanNative::from_bits_le(&bit_vec).map_err(|e| e.to_string())?;
76+
Ok(Boolean(boolean))
77+
}
78+
79+
/// Get the left endian boolean array representation of the boolean element.
80+
#[wasm_bindgen(js_name = "toBitsLe")]
81+
pub fn to_bits_le(&self) -> Array {
82+
to_bits_array_le!(self)
83+
}
84+
85+
/// Create a plaintext from the boolean element.
86+
#[wasm_bindgen(js_name = "toPlaintext")]
87+
pub fn to_plaintext(&self) -> Plaintext {
88+
Plaintext::from(PlaintextNative::Literal(LiteralNative::Boolean(self.0), OnceCell::new()))
89+
}
90+
91+
/// Clone the boolean element.
92+
#[allow(clippy::should_implement_trait)]
93+
pub fn clone(&self) -> Boolean {
94+
Boolean(self.0)
95+
}
96+
97+
/// Generate a random boolean element.
98+
pub fn random() -> Boolean {
99+
let rng = &mut rand::thread_rng();
100+
Boolean(BooleanNative::rand(rng))
101+
}
102+
103+
/// Logical NOT.
104+
pub fn not(&self) -> Boolean {
105+
Boolean(!self.0)
106+
}
107+
108+
/// Logical AND.
109+
pub fn and(&self, other: &Boolean) -> Boolean {
110+
Boolean(self.0 & other.0)
111+
}
112+
113+
/// Logical OR.
114+
pub fn or(&self, other: &Boolean) -> Boolean {
115+
Boolean(self.0 | other.0)
116+
}
117+
118+
/// Logical XOR.
119+
pub fn xor(&self, other: &Boolean) -> Boolean {
120+
Boolean(self.0 ^ other.0)
121+
}
122+
123+
/// Logical NAND.
124+
pub fn nand(&self, other: &Boolean) -> Boolean {
125+
Boolean(!(self.0 & other.0))
126+
}
127+
128+
/// Logical NOR.
129+
pub fn nor(&self, other: &Boolean) -> Boolean {
130+
Boolean(!(self.0 | other.0))
131+
}
132+
133+
/// Check if one boolean element equals another.
134+
pub fn equals(&self, other: &Boolean) -> bool {
135+
self.0 == BooleanNative::from(other)
136+
}
137+
138+
/// `false`
139+
pub fn zero() -> Boolean {
140+
Boolean(BooleanNative::zero())
141+
}
142+
143+
/// `true`
144+
pub fn one() -> Boolean {
145+
Boolean(BooleanNative::one())
146+
}
147+
}
148+
149+
impl Deref for Boolean {
150+
type Target = BooleanNative;
151+
152+
fn deref(&self) -> &Self::Target {
153+
&self.0
154+
}
155+
}
156+
157+
impl From<BooleanNative> for Boolean {
158+
fn from(native: BooleanNative) -> Self {
159+
Self(native)
160+
}
161+
}
162+
163+
impl From<Boolean> for BooleanNative {
164+
fn from(boolean: Boolean) -> Self {
165+
boolean.0
166+
}
167+
}
168+
169+
impl From<&BooleanNative> for Boolean {
170+
fn from(native: &BooleanNative) -> Self {
171+
Self(*native)
172+
}
173+
}
174+
175+
impl From<&Boolean> for BooleanNative {
176+
fn from(boolean: &Boolean) -> Self {
177+
boolean.0
178+
}
179+
}
180+
181+
impl FromStr for Boolean {
182+
type Err = anyhow::Error;
183+
184+
fn from_str(boolean: &str) -> Result<Self, Self::Err> {
185+
Ok(Self(BooleanNative::from_str(boolean)?))
186+
}
187+
}

wasm/src/types/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
// You should have received a copy of the GNU General Public License
1515
// along with the Provable SDK library. If not, see <https://www.gnu.org/licenses/>.
1616

17+
pub mod boolean;
18+
pub use boolean::Boolean;
19+
1720
pub mod field;
1821
pub use field::Field;
1922

wasm/src/types/native/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use snarkvm_console::{
3434
Value,
3535
ValueType,
3636
},
37-
types::{Field, Group, Scalar, U16, U64},
37+
types::{Boolean, Field, Group, Scalar, U16, U64},
3838
};
3939
use snarkvm_ledger_block::{Execution, Input, Output, Transaction, Transition};
4040
use snarkvm_ledger_query::Query;
@@ -55,6 +55,7 @@ pub type SignatureNative = Signature<CurrentNetwork>;
5555
pub type ViewKeyNative = ViewKey<CurrentNetwork>;
5656

5757
// Algebraic & Primitive Data Types
58+
pub type BooleanNative = Boolean<CurrentNetwork>;
5859
pub type FieldNative = Field<CurrentNetwork>;
5960
pub type GroupNative = Group<CurrentNetwork>;
6061
pub type ScalarNative = Scalar<CurrentNetwork>;

0 commit comments

Comments
 (0)