Skip to content

Commit 498e29d

Browse files
committed
bls: syscall for G1 add (and plumbing)
1 parent 66aae90 commit 498e29d

12 files changed

+172
-7
lines changed

config/extra/with-arm.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ else # CROSS=0
2929

3030
include config/extra/with-ucontext.mk
3131
include config/extra/with-secp256k1.mk
32+
include config/extra/with-blst.mk
3233
include config/extra/with-zstd.mk
3334
include config/extra/with-lz4.mk
3435
include config/extra/with-openssl.mk

config/extra/with-blst.mk

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
ifneq (,$(wildcard $(OPT)/lib/libblst.a))
2+
FD_HAS_BLST:=1
3+
CFLAGS+=-DFD_HAS_BLST=1
4+
LDFLAGS+=$(OPT)/lib/libblst.a
5+
else
6+
$(warning "blst not installed, skipping")
7+
endif

config/extra/with-x86-64.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ endif
1515
include config/extra/with-ucontext.mk
1616
include config/extra/with-secp256k1.mk
1717
include config/extra/with-s2nbignum.mk
18+
include config/extra/with-blst.mk
1819
include config/extra/with-zstd.mk
1920
include config/extra/with-lz4.mk
2021
include config/extra/with-openssl.mk

src/ballet/bls/Local.mk

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
ifdef FD_HAS_BLST
2+
3+
$(call add-hdrs,fd_bls12_381.h)
4+
$(call add-objs,fd_bls12_381,fd_ballet)
5+
$(call make-unit-test,test_bls12_381,test_bls12_381,fd_ballet fd_util,$(BLST_LIBS))
6+
7+
$(call run-unit-test,test_bls12_381)
8+
9+
else
10+
11+
$(warning bls12_381 disabled due to lack of libblst)
12+
13+
endif

src/ballet/bls/fd_bls12_381.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include "fd_bls12_381.h"
2+
3+
#include <blst.h>
4+
5+
int
6+
fd_bls12_381_g1_add_syscall( uchar rr[48],
7+
uchar const pp[48],
8+
uchar const qq[48] ) {
9+
blst_p1_affine pa[1], qa[1];
10+
blst_p1 p[1], r[1];
11+
if( FD_UNLIKELY( blst_p1_uncompress( pa, pp )!=BLST_SUCCESS ) ) {
12+
return -1;
13+
}
14+
if( FD_UNLIKELY( blst_p1_uncompress( qa, qq )!=BLST_SUCCESS ) ) {
15+
return -1;
16+
}
17+
blst_p1_from_affine( p, pa );
18+
blst_p1_add_or_double_affine( r, p, qa );
19+
blst_p1_compress( rr, r );
20+
return 0;
21+
}

src/ballet/bls/fd_bls12_381.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#ifndef HEADER_fd_src_ballet_bls_fd_bls12_381_h
2+
#define HEADER_fd_src_ballet_bls_fd_bls12_381_h
3+
4+
#include "../fd_ballet_base.h"
5+
6+
FD_PROTOTYPES_BEGIN
7+
8+
int
9+
fd_bls12_381_g1_add_syscall( uchar r[48],
10+
uchar const p[48],
11+
uchar const q[48] );
12+
13+
FD_PROTOTYPES_END
14+
15+
#endif /* HEADER_fd_src_ballet_bls_fd_bls12_381_h */

src/ballet/bls/test_bls12_381.c

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include "../fd_ballet.h"
2+
#include "fd_bls12_381.h"
3+
#include "../hex/fd_hex.h"
4+
5+
void
6+
log_bench( char const * descr,
7+
ulong iter,
8+
long dt ) {
9+
float khz = 1e6f *(float)iter/(float)dt;
10+
float tau = (float)dt /(float)iter;
11+
FD_LOG_NOTICE(( "%-31s %11.3fK/s/core %10.3f ns/call", descr, (double)khz, (double)tau ));
12+
}
13+
14+
static void
15+
test_add( FD_FN_UNUSED fd_rng_t * rng ) {
16+
// test correctness
17+
//
18+
uchar re[48] = { 0 };
19+
uchar r[48] = { 0 };
20+
uchar p[48] = { 0 };
21+
uchar q[48] = { 0 };
22+
23+
fd_hex_decode( p, "97f1d3a73197d7942695638c4fa9ac0fc3688c4f9774b905a14e3a3f171bac586c55e83ff97a1aeffb3af00adb22c6bb", 48 );
24+
fd_hex_decode( q, "97f1d3a73197d7942695638c4fa9ac0fc3688c4f9774b905a14e3a3f171bac586c55e83ff97a1aeffb3af00adb22c6bb", 48 );
25+
fd_hex_decode( re, "a572cbea904d67468808c8eb50a9450c9721db309128012543902d0ac358a62ae28f75bb8f1c7c42c39a8c5529bf0f4e", 48 );
26+
27+
FD_TEST( fd_bls12_381_g1_add_syscall( r, p, q )==0 );
28+
FD_TEST( fd_memeq( r, re, 48 ) );
29+
}
30+
31+
/**********************************************************************/
32+
33+
int
34+
main( int argc,
35+
char ** argv ) {
36+
fd_boot( &argc, &argv );
37+
fd_rng_t _rng[1]; fd_rng_t * rng = fd_rng_join( fd_rng_new( _rng, 0U, 0UL ) );
38+
39+
test_add ( rng );
40+
41+
FD_LOG_NOTICE(( "pass" ));
42+
fd_halt();
43+
return 0;
44+
}

src/flamenco/vm/Local.mk

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
ifdef FD_HAS_INT128
22
ifdef FD_HAS_HOSTED
33
ifdef FD_HAS_SECP256K1
4+
ifdef FD_HAS_BLST
45

56
$(call add-hdrs,fd_vm_base.h fd_vm.h fd_vm_private.h) # FIXME: PRIVATE TEMPORARILY HERE DUE TO SOME MESSINESS IN FD_VM_SYSCALL.H
67
$(call add-objs,fd_vm fd_vm_interp fd_vm_disasm fd_vm_trace,fd_flamenco)
78

89
$(call add-hdrs,test_vm_util.h)
910
$(call add-objs,test_vm_util,fd_flamenco)
1011

11-
$(call make-bin,fd_vm_tool,fd_vm_tool,fd_flamenco fd_funk fd_ballet fd_util fd_disco,$(SECP256K1_LIBS))
12+
$(call make-bin,fd_vm_tool,fd_vm_tool,fd_flamenco fd_funk fd_ballet fd_util fd_disco,$(SECP256K1_LIBS) $(BLST_LIBS))
1213

1314
# Unfortunately, the get_sysvar syscall handler depends on the funk database
14-
$(call make-unit-test,test_vm_interp,test_vm_interp,fd_flamenco fd_funk fd_ballet fd_util fd_disco,$(SECP256K1_LIBS))
15+
$(call make-unit-test,test_vm_interp,test_vm_interp,fd_flamenco fd_funk fd_ballet fd_util fd_disco,$(SECP256K1_LIBS) $(BLST_LIBS))
1516

1617
$(call make-unit-test,test_vm_base,test_vm_base,fd_flamenco fd_ballet fd_util)
1718

@@ -22,4 +23,5 @@ $(call run-unit-test,test_vm_base)
2223
$(call run-unit-test,test_vm_interp)
2324
endif
2425
endif
26+
endif
2527
endif

src/flamenco/vm/syscall/Local.mk

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
ifdef FD_HAS_INT128
22
ifdef FD_HAS_HOSTED
33
ifdef FD_HAS_SECP256K1
4+
ifdef FD_HAS_BLST
45
$(call add-hdrs,fd_vm_syscall.h fd_vm_syscall_macros.h fd_vm_cpi.h)
56
$(call add-objs,fd_vm_syscall fd_vm_syscall_cpi fd_vm_syscall_hash fd_vm_syscall_crypto fd_vm_syscall_curve fd_vm_syscall_pda fd_vm_syscall_runtime fd_vm_syscall_util,fd_flamenco)
67

@@ -14,3 +15,4 @@ $(call run-unit-test,test_vm_syscall_curve)
1415
endif
1516
endif
1617
endif
18+
endif

src/flamenco/vm/syscall/fd_vm_syscall.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -831,17 +831,20 @@ FD_VM_SYSCALL_DECL( sol_secp256k1_recover );
831831

832832
#define FD_VM_SYSCALL_SOL_CURVE_CURVE25519_EDWARDS ( 0UL) /* ed25519 */
833833
#define FD_VM_SYSCALL_SOL_CURVE_CURVE25519_RISTRETTO ( 1UL) /* ristretto255 */
834+
#define FD_VM_SYSCALL_SOL_CURVE_BLS12_381 ( 2UL) /* bls12-381 */
834835

835836
/* FD_VM_SYSCALL_SOL_CURVE_{...} specifies the curve operation */
836837

837838
#define FD_VM_SYSCALL_SOL_CURVE_ADD ( 0UL) /* add */
838839
#define FD_VM_SYSCALL_SOL_CURVE_SUB ( 1UL) /* add inverse */
839840
#define FD_VM_SYSCALL_SOL_CURVE_MUL ( 2UL) /* scalar mul */
841+
#define FD_VM_SYSCALL_SOL_CURVE_HASH ( 3UL) /* hash to point */
840842

841-
/* FD_VM_SYSCALL_SOL_CURVE_CURVE25519_{...}_SZ specifies the size of inputs/outputs. */
843+
/* FD_VM_SYSCALL_SOL_CURVE_{...}_SZ specifies the size of inputs/outputs. */
842844

843845
#define FD_VM_SYSCALL_SOL_CURVE_CURVE25519_POINT_SZ (32UL) /* point (compressed) */
844846
#define FD_VM_SYSCALL_SOL_CURVE_CURVE25519_SCALAR_SZ (32UL) /* scalar */
847+
#define FD_VM_SYSCALL_SOL_CURVE_BLS12_381_POINT_SZ (48UL) /* point (compressed) */
845848

846849
/* syscall(aa2607ca) sol_curve_validate_point
847850

src/flamenco/vm/syscall/fd_vm_syscall_curve.c

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include "../../../ballet/ed25519/fd_curve25519.h"
44
#include "../../../ballet/ed25519/fd_ristretto255.h"
5+
#include "../../../ballet/bls/fd_bls12_381.h"
56

67
int
78
fd_vm_syscall_sol_curve_validate_point( /**/ void * _vm,
@@ -69,8 +70,10 @@ fd_vm_syscall_sol_curve_group_op( void * _vm,
6970
#define MATCH_ID_OP(crv_id,grp_op) ((crv_id << 4) | grp_op)
7071
#define EDWARDS FD_VM_SYSCALL_SOL_CURVE_CURVE25519_EDWARDS
7172
#define RISTRETTO FD_VM_SYSCALL_SOL_CURVE_CURVE25519_RISTRETTO
73+
#define BLS FD_VM_SYSCALL_SOL_CURVE_BLS12_381
7274

7375
ulong cost = 0UL;
76+
ulong input_sz = 32UL;
7477
switch( curve_id ) {
7578

7679
case EDWARDS:
@@ -113,6 +116,19 @@ fd_vm_syscall_sol_curve_group_op( void * _vm,
113116
}
114117
break;
115118

119+
case BLS:
120+
switch( group_op ) {
121+
122+
case FD_VM_SYSCALL_SOL_CURVE_ADD:
123+
cost = FD_VM_CURVE25519_RISTRETTO_ADD_COST; //FIXME
124+
input_sz = FD_VM_SYSCALL_SOL_CURVE_BLS12_381_POINT_SZ;
125+
break;
126+
127+
default:
128+
goto invalid_error;
129+
}
130+
break;
131+
116132
default:
117133
goto invalid_error;
118134
}
@@ -122,10 +138,9 @@ fd_vm_syscall_sol_curve_group_op( void * _vm,
122138

123139
/* https://github.com/anza-xyz/agave/blob/v1.18.8/programs/bpf_loader/src/syscalls/mod.rs#L949-L958 */
124140

125-
/* Note: left_input_addr is a point for add, sub, BUT it's a scalar for mul.
126-
However, from a memory mapping perspective it's always 32 bytes, so we unify the code. */
127-
uchar const * inputL = FD_VM_MEM_HADDR_LD( vm, left_input_addr, FD_VM_ALIGN_RUST_POD_U8_ARRAY, 32UL );
128-
uchar const * inputR = FD_VM_MEM_HADDR_LD( vm, right_input_addr, FD_VM_ALIGN_RUST_POD_U8_ARRAY, FD_VM_SYSCALL_SOL_CURVE_CURVE25519_POINT_SZ );
141+
/* Note: left_input_addr is a point for add, sub, BUT it's a scalar for mul. */
142+
uchar const * inputL = FD_VM_MEM_HADDR_LD( vm, left_input_addr, FD_VM_ALIGN_RUST_POD_U8_ARRAY, input_sz );
143+
uchar const * inputR = FD_VM_MEM_HADDR_LD( vm, right_input_addr, FD_VM_ALIGN_RUST_POD_U8_ARRAY, input_sz );
129144

130145
switch( MATCH_ID_OP( curve_id, group_op ) ) {
131146

@@ -225,6 +240,16 @@ fd_vm_syscall_sol_curve_group_op( void * _vm,
225240
break;
226241
}
227242

243+
/* BLS12-381 */
244+
case MATCH_ID_OP( BLS, FD_VM_SYSCALL_SOL_CURVE_ADD ): {
245+
uchar * result = FD_VM_MEM_HADDR_ST( vm, result_point_addr, FD_VM_ALIGN_RUST_POD_U8_ARRAY, FD_VM_SYSCALL_SOL_CURVE_BLS12_381_POINT_SZ );
246+
/* Compute add */
247+
if( FD_LIKELY( fd_bls12_381_g1_add_syscall( result, inputL, inputR )==0 ) ) {
248+
ret = 0UL; /* success */
249+
}
250+
break;
251+
}
252+
228253
default:
229254
/* COV: this can never happen because of the previous switch */
230255
return FD_VM_SYSCALL_ERR_INVALID_ATTRIBUTE; /* SyscallError::InvalidAttribute */
@@ -236,6 +261,7 @@ fd_vm_syscall_sol_curve_group_op( void * _vm,
236261
#undef MATCH_ID_OP
237262
#undef EDWARDS
238263
#undef RISTRETTO
264+
#undef BLS
239265

240266
invalid_error:
241267
/* https://github.com/anza-xyz/agave/blob/5b3390b99a6e7665439c623062c1a1dda2803524/programs/bpf_loader/src/syscalls/mod.rs#L1135-L1156 */

src/flamenco/vm/syscall/test_vm_syscall_curve.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "fd_vm_syscall.h"
22
#include "../test_vm_util.h"
3+
#include "../../../ballet/hex/fd_hex.h"
34

45
static inline void set_memory_region( uchar * mem, ulong sz ) { for( ulong i=0UL; i<sz; i++ ) mem[i] = (uchar)(i & 0xffUL); }
56

@@ -391,6 +392,35 @@ main( int argc,
391392
) );
392393
}
393394

395+
{
396+
uchar _points[ 96 ]; uchar * points = _points;
397+
fd_hex_decode( points, "97f1d3a73197d7942695638c4fa9ac0fc3688c4f9774b905a14e3a3f171bac586c55e83ff97a1aeffb3af00adb22c6bb", 48 );
398+
fd_hex_decode( points+48, "97f1d3a73197d7942695638c4fa9ac0fc3688c4f9774b905a14e3a3f171bac586c55e83ff97a1aeffb3af00adb22c6bb", 48 );
399+
400+
uchar _expected[ 48 ];
401+
fd_hex_decode( _expected, "a572cbea904d67468808c8eb50a9450c9721db309128012543902d0ac358a62ae28f75bb8f1c7c42c39a8c5529bf0f4e", 48 );
402+
403+
memcpy( &vm->heap[0], points, 96 );
404+
405+
in0_vaddr = FD_VM_MEM_MAP_HEAP_REGION_START;
406+
in1_vaddr = FD_VM_MEM_MAP_HEAP_REGION_START + 48UL;
407+
result_point_vaddr = FD_VM_MEM_MAP_HEAP_REGION_START + 96UL;
408+
expected_result_host_ptr = _expected;
409+
410+
FD_TEST( test_fd_vm_syscall_sol_curve_group_op(
411+
"fd_vm_syscall_sol_curve_group_op: bls12-381, add",
412+
vm,
413+
FD_VM_SYSCALL_SOL_CURVE_BLS12_381,
414+
FD_VM_SYSCALL_SOL_CURVE_ADD,
415+
in0_vaddr,
416+
in1_vaddr,
417+
result_point_vaddr,
418+
0UL, // ret_code
419+
FD_VM_SUCCESS, // syscall_ret
420+
expected_result_host_ptr
421+
) );
422+
}
423+
394424
fd_vm_delete ( fd_vm_leave ( vm ) );
395425
fd_sha256_delete( fd_sha256_leave( sha ) );
396426
fd_rng_delete ( fd_rng_leave ( rng ) );

0 commit comments

Comments
 (0)