Skip to content

Commit ffa3026

Browse files
committed
Add support for U128, passed as [u8; 16] but with human wrappers
1 parent 1d1229d commit ffa3026

File tree

4 files changed

+91
-2
lines changed

4 files changed

+91
-2
lines changed

genbindings.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,16 @@ def java_c_types(fn_arg, ret_arr_len):
139139
assert var_is_arr_regex.match(fn_arg[8:])
140140
rust_obj = "LDKThirtyTwoBytes"
141141
arr_access = "data"
142+
elif fn_arg.startswith("LDKU128"):
143+
if fn_arg == "LDKU128":
144+
fn_arg = "LDKU128 arg"
145+
if fn_arg.startswith("LDKU128*") or fn_arg.startswith("LDKU128 *"):
146+
fn_arg = "uint8_t (" + fn_arg[8:] + ")[16]"
147+
else:
148+
fn_arg = "uint8_t (*" + fn_arg[8:] + ")[16]"
149+
assert var_is_arr_regex.match(fn_arg[8:])
150+
rust_obj = "LDKU128"
151+
arr_access = "le_bytes"
142152
elif fn_arg.startswith("LDKTxid"):
143153
fn_arg = "uint8_t (*" + fn_arg[8:] + ")[32]"
144154
assert var_is_arr_regex.match(fn_arg[8:])
@@ -381,6 +391,8 @@ def java_c_types(fn_arg, ret_arr_len):
381391
else:
382392
java_ty = java_ty + "[]"
383393
java_hu_ty = java_ty
394+
if rust_obj == "LDKU128":
395+
java_hu_ty = consts.u128_native_ty
384396
c_ty = c_ty + "Array"
385397

386398
subty = java_c_types(arr_ty, None)

java_strings.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,7 @@ class CommonBase {
574574
self.file_ext = ".java"
575575
self.ptr_c_ty = "int64_t"
576576
self.ptr_native_ty = "long"
577+
self.u128_native_ty = "UInt128"
577578
self.usize_c_ty = "int64_t"
578579
self.usize_native_ty = "long"
579580
self.native_zero_ptr = "0"
@@ -704,10 +705,14 @@ def cleanup_converted_native_array(self, ty_info, arr_name):
704705

705706
def primitive_arr_from_hu(self, arr_ty, fixed_len, arr_name):
706707
mapped_ty = arr_ty.subty
708+
if arr_ty.rust_obj == "LDKU128":
709+
return ("" + arr_name + ".getLEBytes()", "")
707710
if fixed_len is not None:
708711
return ("InternalUtils.check_arr_len(" + arr_name + ", " + fixed_len + ")", "")
709712
return None
710713
def primitive_arr_to_hu(self, arr_ty, fixed_len, arr_name, conv_name):
714+
if arr_ty.rust_obj == "LDKU128":
715+
return "org.ldk.util.UInt128 " + conv_name + " = new org.ldk.util.UInt128(" + arr_name + ");"
711716
return None
712717

713718
def java_arr_ty_str(self, elem_ty_str):
@@ -729,7 +734,7 @@ def add_ref(self, holder, referent):
729734
def fully_qualified_hu_ty_path(self, ty):
730735
if ty.java_fn_ty_arg.startswith("L") and ty.java_fn_ty_arg.endswith(";"):
731736
return ty.java_fn_ty_arg.strip("L;").replace("/", ".")
732-
if ty.java_hu_ty == "UnqualifiedError" or ty.java_hu_ty == "UInt5" or ty.java_hu_ty == "WitnessVersion":
737+
if ty.java_hu_ty == "UnqualifiedError" or ty.java_hu_ty == "UInt128" or ty.java_hu_ty == "UInt5" or ty.java_hu_ty == "WitnessVersion":
733738
return "org.ldk.util." + ty.java_hu_ty
734739
if not ty.is_native_primitive and ty.rust_obj is not None and not "[]" in ty.java_hu_ty:
735740
return "org.ldk.structs." + ty.java_hu_ty
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package org.ldk.util;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* A 5-bit unsigned integer
7+
*/
8+
public class UInt128 {
9+
private byte[] le_bytes;
10+
11+
/**
12+
* Constructs a 128-bit integer from its little-endian representation
13+
*/
14+
public UInt128(byte[] le_bytes) {
15+
if (le_bytes.length != 16) {
16+
throw new IllegalArgumentException();
17+
}
18+
this.le_bytes = le_bytes;
19+
}
20+
21+
/**
22+
* Constructs a 128-bit integer from a long, ignoring the sign bit
23+
*/
24+
public UInt128(long val) {
25+
byte[] le_bytes = new byte[16];
26+
for (int i = 0; i < 8; i++)
27+
le_bytes[i] = (byte) ((val >> i*8) & 0xff);
28+
this.le_bytes = le_bytes;
29+
}
30+
31+
/**
32+
* @return The value as 16 little endian bytes
33+
*/
34+
public byte[] getLEBytes() { return le_bytes; }
35+
36+
@Override public boolean equals(Object o) {
37+
if (o == null || !(o instanceof UInt128)) return false;
38+
return Arrays.equals(le_bytes, ((UInt128) o).le_bytes);
39+
}
40+
41+
@Override public int hashCode() { return Arrays.hashCode(le_bytes); }
42+
}

typescript_strings.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,16 @@ def __init__(self, DEBUG: bool, target: Target, outdir: str, **kwargs):
166166
167167
168168
169+
/* @internal */
170+
export function encodeUint128 (inputVal: bigint): number {
171+
if (inputVal >= 0x10000000000000000000000000000000n) throw "U128s cannot exceed 128 bits";
172+
const cArrayPointer = wasm.TS_malloc(16 + 8);
173+
const arrayLengthView = new BigUint64Array(wasm.memory.buffer, cArrayPointer, 1);
174+
arrayLengthView[0] = BigInt(16);
175+
const arrayMemoryView = new Uint8Array(wasm.memory.buffer, cArrayPointer + 8, 16);
176+
for (var i = 0; i < 16; i++) arrayMemoryView[i] = Number((inputVal >> BigInt(i)*8n) & 0xffn);
177+
return cArrayPointer;
178+
}
169179
/* @internal */
170180
export function encodeUint8Array (inputArray: Uint8Array|null): number {
171181
if (inputArray == null) return 0;
@@ -210,6 +220,21 @@ def __init__(self, DEBUG: bool, target: Target, outdir: str, **kwargs):
210220
return Number(len % (2n ** 32n));
211221
}
212222
/* @internal */
223+
export function decodeUint128 (arrayPointer: number, free = true): bigint {
224+
const arraySize = getArrayLength(arrayPointer);
225+
if (arraySize != 16) throw "Need 16 bytes for a uint128";
226+
const actualArrayViewer = new Uint8Array(wasm.memory.buffer, arrayPointer + 8, arraySize);
227+
var val = 0n;
228+
for (var i = 0; i < 16; i++) {
229+
val <<= 8n;
230+
val |= BigInt(actualArrayViewer[i]!);
231+
}
232+
if (free) {
233+
wasm.TS_free(arrayPointer);
234+
}
235+
return val;
236+
}
237+
/* @internal */
213238
export function decodeUint8Array (arrayPointer: number, free = true): Uint8Array {
214239
const arraySize = getArrayLength(arrayPointer);
215240
const actualArrayViewer = new Uint8Array(wasm.memory.buffer, arrayPointer + 8, arraySize);
@@ -678,6 +703,7 @@ def __init__(self, DEBUG: bool, target: Target, outdir: str, **kwargs):
678703
self.file_ext = ".mts"
679704
self.ptr_c_ty = "uint64_t"
680705
self.ptr_native_ty = "bigint"
706+
self.u128_native_ty = "bigint"
681707
self.usize_c_ty = "uint32_t"
682708
self.usize_native_ty = "number"
683709
self.native_zero_ptr = "0n"
@@ -760,6 +786,8 @@ def cleanup_converted_native_array(self, ty_info, arr_name):
760786
def primitive_arr_from_hu(self, arr_ty, fixed_len, arr_name):
761787
mapped_ty = arr_ty.subty
762788
inner = arr_name
789+
if arr_ty.rust_obj == "LDKU128":
790+
return ("bindings.encodeUint128(" + inner + ")", "")
763791
if fixed_len is not None:
764792
assert mapped_ty.c_ty == "int8_t"
765793
inner = "bindings.check_arr_len(" + arr_name + ", " + fixed_len + ")"
@@ -777,7 +805,9 @@ def primitive_arr_from_hu(self, arr_ty, fixed_len, arr_name):
777805

778806
def primitive_arr_to_hu(self, arr_ty, fixed_len, arr_name, conv_name):
779807
mapped_ty = arr_ty.subty
780-
if mapped_ty.c_ty == "uint8_t" or mapped_ty.c_ty == "int8_t":
808+
if arr_ty.rust_obj == "LDKU128":
809+
return "const " + conv_name + ": bigint = bindings.decodeUint128(" + arr_name + ");"
810+
elif mapped_ty.c_ty == "uint8_t" or mapped_ty.c_ty == "int8_t":
781811
return "const " + conv_name + ": Uint8Array = bindings.decodeUint8Array(" + arr_name + ");"
782812
elif mapped_ty.c_ty == "uint64_t" or mapped_ty.c_ty == "int64_t":
783813
return "const " + conv_name + ": bigint[] = bindings.decodeUint64Array(" + arr_name + ");"

0 commit comments

Comments
 (0)