Skip to content

Commit 5390fa9

Browse files
authored
[bn254] Add _be suffix to BE compress/decompress functions (#415)
Add _be suffix to BE compress/decompress functions
1 parent c667d88 commit 5390fa9

File tree

2 files changed

+145
-15
lines changed

2 files changed

+145
-15
lines changed

bn254/src/compression.rs

Lines changed: 141 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,20 @@ mod target_arch {
126126
LE,
127127
}
128128

129+
#[deprecated(
130+
since = "3.2.0",
131+
note = "Please use `alt_bn128_g1_decompress_be` instead"
132+
)]
129133
#[inline(always)]
130134
pub fn alt_bn128_g1_decompress(
131135
g1_bytes: &[u8],
136+
) -> Result<[u8; ALT_BN128_G1_POINT_SIZE], AltBn128CompressionError> {
137+
alt_bn128_g1_decompress_be(g1_bytes)
138+
}
139+
140+
#[inline(always)]
141+
pub fn alt_bn128_g1_decompress_be(
142+
g1_bytes: &[u8],
132143
) -> Result<[u8; ALT_BN128_G1_POINT_SIZE], AltBn128CompressionError> {
133144
alt_bn128_apply_g1_decompress(g1_bytes, Endianness::BE)
134145
}
@@ -173,12 +184,26 @@ mod target_arch {
173184
}
174185
}
175186

187+
#[deprecated(
188+
since = "3.2.0",
189+
note = "Please use `alt_bn128_g1_compress_be` instead"
190+
)]
176191
#[inline(always)]
177192
pub fn alt_bn128_g1_compress(
178193
g1_bytes: &[u8],
179194
) -> Result<
180195
[u8; alt_bn128_compression_size::ALT_BN128_G1_COMPRESSED_POINT_SIZE],
181196
AltBn128CompressionError,
197+
> {
198+
alt_bn128_g1_compress_be(g1_bytes)
199+
}
200+
201+
#[inline(always)]
202+
pub fn alt_bn128_g1_compress_be(
203+
g1_bytes: &[u8],
204+
) -> Result<
205+
[u8; alt_bn128_compression_size::ALT_BN128_G1_COMPRESSED_POINT_SIZE],
206+
AltBn128CompressionError,
182207
> {
183208
alt_bn128_apply_g1_compress(g1_bytes, Endianness::BE)
184209
}
@@ -221,9 +246,20 @@ mod target_arch {
221246
}
222247
}
223248

249+
#[deprecated(
250+
since = "3.2.0",
251+
note = "Please use `alt_bn128_g2_decompress_be` instead"
252+
)]
224253
#[inline(always)]
225254
pub fn alt_bn128_g2_decompress(
226255
g2_bytes: &[u8],
256+
) -> Result<[u8; ALT_BN128_G2_POINT_SIZE], AltBn128CompressionError> {
257+
alt_bn128_g2_decompress_be(g2_bytes)
258+
}
259+
260+
#[inline(always)]
261+
pub fn alt_bn128_g2_decompress_be(
262+
g2_bytes: &[u8],
227263
) -> Result<[u8; ALT_BN128_G2_POINT_SIZE], AltBn128CompressionError> {
228264
alt_bn128_apply_g2_decompress(g2_bytes, Endianness::BE)
229265
}
@@ -268,12 +304,26 @@ mod target_arch {
268304
}
269305
}
270306

307+
#[deprecated(
308+
since = "3.2.0",
309+
note = "Please use `alt_bn128_g2_compress_be` instead"
310+
)]
271311
#[inline(always)]
272312
pub fn alt_bn128_g2_compress(
273313
g2_bytes: &[u8],
274314
) -> Result<
275315
[u8; alt_bn128_compression_size::ALT_BN128_G2_COMPRESSED_POINT_SIZE],
276316
AltBn128CompressionError,
317+
> {
318+
alt_bn128_g2_compress_be(g2_bytes)
319+
}
320+
321+
#[inline(always)]
322+
pub fn alt_bn128_g2_compress_be(
323+
g2_bytes: &[u8],
324+
) -> Result<
325+
[u8; alt_bn128_compression_size::ALT_BN128_G2_COMPRESSED_POINT_SIZE],
326+
AltBn128CompressionError,
277327
> {
278328
alt_bn128_apply_g2_compress(g2_bytes, Endianness::BE)
279329
}
@@ -342,6 +392,10 @@ mod target_arch {
342392
solana_define_syscall::definitions as syscalls,
343393
};
344394

395+
#[deprecated(
396+
since = "3.2.0",
397+
note = "Please use `alt_bn128_g1_compress_be` instead"
398+
)]
345399
pub fn alt_bn128_g1_compress(
346400
input: &[u8],
347401
) -> Result<[u8; ALT_BN128_G1_COMPRESSED_POINT_SIZE], AltBn128CompressionError> {
@@ -361,6 +415,25 @@ mod target_arch {
361415
}
362416
}
363417

418+
pub fn alt_bn128_g1_compress_be(
419+
input: &[u8; ALT_BN128_G1_POINT_SIZE],
420+
) -> Result<[u8; ALT_BN128_G1_COMPRESSED_POINT_SIZE], AltBn128CompressionError> {
421+
let mut result_buffer = [0; ALT_BN128_G1_COMPRESSED_POINT_SIZE];
422+
let result = unsafe {
423+
syscalls::sol_alt_bn128_compression(
424+
ALT_BN128_G1_COMPRESS_BE,
425+
input as *const _ as *const u8,
426+
input.len() as u64,
427+
&mut result_buffer as *mut _ as *mut u8,
428+
)
429+
};
430+
431+
match result {
432+
0 => Ok(result_buffer),
433+
_ => Err(AltBn128CompressionError::UnexpectedError),
434+
}
435+
}
436+
364437
pub fn alt_bn128_g1_compress_le(
365438
input: &[u8; ALT_BN128_G1_POINT_SIZE],
366439
) -> Result<[u8; ALT_BN128_G1_COMPRESSED_POINT_SIZE], AltBn128CompressionError> {
@@ -380,6 +453,10 @@ mod target_arch {
380453
}
381454
}
382455

456+
#[deprecated(
457+
since = "3.2.0",
458+
note = "Please use `alt_bn128_g1_decompress_be` instead"
459+
)]
383460
pub fn alt_bn128_g1_decompress(
384461
input: &[u8],
385462
) -> Result<[u8; ALT_BN128_G1_POINT_SIZE], AltBn128CompressionError> {
@@ -399,6 +476,25 @@ mod target_arch {
399476
}
400477
}
401478

479+
pub fn alt_bn128_g1_decompress_be(
480+
input: &[u8; ALT_BN128_G1_COMPRESSED_POINT_SIZE],
481+
) -> Result<[u8; ALT_BN128_G1_POINT_SIZE], AltBn128CompressionError> {
482+
let mut result_buffer = [0; ALT_BN128_G1_POINT_SIZE];
483+
let result = unsafe {
484+
syscalls::sol_alt_bn128_compression(
485+
ALT_BN128_G1_DECOMPRESS_BE,
486+
input as *const _ as *const u8,
487+
input.len() as u64,
488+
&mut result_buffer as *mut _ as *mut u8,
489+
)
490+
};
491+
492+
match result {
493+
0 => Ok(result_buffer),
494+
_ => Err(AltBn128CompressionError::UnexpectedError),
495+
}
496+
}
497+
402498
pub fn alt_bn128_g1_decompress_le(
403499
input: &[u8; ALT_BN128_G1_COMPRESSED_POINT_SIZE],
404500
) -> Result<[u8; ALT_BN128_G1_POINT_SIZE], AltBn128CompressionError> {
@@ -418,6 +514,10 @@ mod target_arch {
418514
}
419515
}
420516

517+
#[deprecated(
518+
since = "3.2.0",
519+
note = "Please use `alt_bn128_g2_compress_be` instead"
520+
)]
421521
pub fn alt_bn128_g2_compress(
422522
input: &[u8],
423523
) -> Result<[u8; ALT_BN128_G2_COMPRESSED_POINT_SIZE], AltBn128CompressionError> {
@@ -437,6 +537,25 @@ mod target_arch {
437537
}
438538
}
439539

540+
pub fn alt_bn128_g2_compress_be(
541+
input: &[u8; ALT_BN128_G2_POINT_SIZE],
542+
) -> Result<[u8; ALT_BN128_G2_COMPRESSED_POINT_SIZE], AltBn128CompressionError> {
543+
let mut result_buffer = [0; ALT_BN128_G2_COMPRESSED_POINT_SIZE];
544+
let result = unsafe {
545+
syscalls::sol_alt_bn128_compression(
546+
ALT_BN128_G2_COMPRESS_BE,
547+
input as *const _ as *const u8,
548+
input.len() as u64,
549+
&mut result_buffer as *mut _ as *mut u8,
550+
)
551+
};
552+
553+
match result {
554+
0 => Ok(result_buffer),
555+
_ => Err(AltBn128CompressionError::UnexpectedError),
556+
}
557+
}
558+
440559
pub fn alt_bn128_g2_compress_le(
441560
input: &[u8; ALT_BN128_G2_POINT_SIZE],
442561
) -> Result<[u8; ALT_BN128_G2_COMPRESSED_POINT_SIZE], AltBn128CompressionError> {
@@ -456,8 +575,19 @@ mod target_arch {
456575
}
457576
}
458577

578+
#[deprecated(
579+
since = "3.2.0",
580+
note = "Please use `alt_bn128_g2_decompress_be` instead"
581+
)]
582+
#[inline(always)]
459583
pub fn alt_bn128_g2_decompress(
460584
input: &[u8; ALT_BN128_G2_COMPRESSED_POINT_SIZE],
585+
) -> Result<[u8; ALT_BN128_G2_POINT_SIZE], AltBn128CompressionError> {
586+
alt_bn128_g2_decompress_be(input)
587+
}
588+
589+
pub fn alt_bn128_g2_decompress_be(
590+
input: &[u8; ALT_BN128_G2_COMPRESSED_POINT_SIZE],
461591
) -> Result<[u8; ALT_BN128_G2_POINT_SIZE], AltBn128CompressionError> {
462592
let mut result_buffer = [0; ALT_BN128_G2_POINT_SIZE];
463593
let result = unsafe {
@@ -503,9 +633,9 @@ mod tests {
503633
ark_serialize::{CanonicalDeserialize, CanonicalSerialize, Compress, Validate},
504634
std::ops::Neg,
505635
target_arch::{
506-
alt_bn128_g1_compress, alt_bn128_g1_compress_le, alt_bn128_g1_decompress,
507-
alt_bn128_g1_decompress_le, alt_bn128_g2_compress, alt_bn128_g2_compress_le,
508-
alt_bn128_g2_decompress, alt_bn128_g2_decompress_le,
636+
alt_bn128_g1_compress_be, alt_bn128_g1_compress_le, alt_bn128_g1_decompress_be,
637+
alt_bn128_g1_decompress_le, alt_bn128_g2_compress_be, alt_bn128_g2_compress_le,
638+
alt_bn128_g2_decompress_be, alt_bn128_g2_decompress_le,
509639
},
510640
};
511641
type G1 = ark_bn254::g1::G1Affine;
@@ -553,10 +683,10 @@ mod tests {
553683
// test be
554684
let compressed_ref: [u8; 32] = convert_endianness::<32, 32>(&compressed_ref);
555685

556-
let decompressed = alt_bn128_g1_decompress(compressed_ref.as_slice()).unwrap();
686+
let decompressed = alt_bn128_g1_decompress_be(compressed_ref.as_slice()).unwrap();
557687

558688
assert_eq!(
559-
alt_bn128_g1_compress(&decompressed).unwrap(),
689+
alt_bn128_g1_compress_be(&decompressed).unwrap(),
560690
compressed_ref
561691
);
562692
assert_eq!(decompressed, *g1_be);
@@ -608,10 +738,10 @@ mod tests {
608738
// test be
609739
let compressed_ref: [u8; 64] = convert_endianness::<64, 64>(&compressed_ref);
610740

611-
let decompressed = alt_bn128_g2_decompress(compressed_ref.as_slice()).unwrap();
741+
let decompressed = alt_bn128_g2_decompress_be(compressed_ref.as_slice()).unwrap();
612742

613743
assert_eq!(
614-
alt_bn128_g2_compress(&decompressed).unwrap(),
744+
alt_bn128_g2_compress_be(&decompressed).unwrap(),
615745
compressed_ref
616746
);
617747
assert_eq!(decompressed, *g2_be);
@@ -621,16 +751,16 @@ mod tests {
621751
#[test]
622752
fn alt_bn128_compression_g1_point_of_infitity() {
623753
let g1_bytes = vec![0u8; 64];
624-
let g1_compressed = alt_bn128_g1_compress(&g1_bytes).unwrap();
625-
let g1_decompressed = alt_bn128_g1_decompress(&g1_compressed).unwrap();
754+
let g1_compressed = alt_bn128_g1_compress_be(&g1_bytes).unwrap();
755+
let g1_decompressed = alt_bn128_g1_decompress_be(&g1_compressed).unwrap();
626756
assert_eq!(g1_bytes, g1_decompressed);
627757
}
628758

629759
#[test]
630760
fn alt_bn128_compression_g2_point_of_infitity() {
631761
let g1_bytes = vec![0u8; 128];
632-
let g1_compressed = alt_bn128_g2_compress(&g1_bytes).unwrap();
633-
let g1_decompressed = alt_bn128_g2_decompress(&g1_compressed).unwrap();
762+
let g1_compressed = alt_bn128_g2_compress_be(&g1_bytes).unwrap();
763+
let g1_decompressed = alt_bn128_g2_decompress_be(&g1_compressed).unwrap();
634764
assert_eq!(g1_bytes, g1_decompressed);
635765
}
636766
}

bn254/tests/curve_operations.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,11 @@ fn alt_bn128_compression_pairing_test_input() {
130130
return;
131131
}
132132
let g1 = input[0..64].to_vec();
133-
let g1_compressed = alt_bn128_g1_compress(&g1).unwrap();
134-
assert_eq!(g1, alt_bn128_g1_decompress(&g1_compressed).unwrap());
133+
let g1_compressed = alt_bn128_g1_compress_be(&g1).unwrap();
134+
assert_eq!(g1, alt_bn128_g1_decompress_be(&g1_compressed).unwrap());
135135
let g2 = input[64..192].to_vec();
136-
let g2_compressed = alt_bn128_g2_compress(&g2).unwrap();
137-
assert_eq!(g2, alt_bn128_g2_decompress(&g2_compressed).unwrap());
136+
let g2_compressed = alt_bn128_g2_compress_be(&g2).unwrap();
137+
assert_eq!(g2, alt_bn128_g2_decompress_be(&g2_compressed).unwrap());
138138

139139
// test le
140140
let g1_le = convert_endianness::<32, 64>(&g1.try_into().unwrap());

0 commit comments

Comments
 (0)