1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
// This file is part of Gear.
//
// Copyright (C) 2025 Gear Technologies Inc.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//! BLS12-381 builtin actor implementation.
//!
//! The main function of the module is `process_bls12_381_dispatch` which
//! processes incoming dispatches to the bls12-381 builtin actor.
pub use builtins_common::bls12_381::{Request as Bls12_381Request, Response as Bls12_381Response};
use builtins_common::bls12_381::BlsOpsGasCost;
use gear_core::ids::ActorId;
/// The id of the BLS12-381 builtin actor.
pub const BLS12_381_ID: ActorId = ActorId::new(*b"modl/bia/bls12-381/v-\x01\0/\0\0\0\0\0\0\0\0");
pub(crate) struct BlsOpsGasCostsImpl;
impl BlsOpsGasCost for BlsOpsGasCostsImpl {
fn decode_bytes(_len: u32) -> u64 {
0
}
fn bls12_381_multi_miller_loop(_count: u32) -> u64 {
0
}
fn bls12_381_final_exponentiation() -> u64 {
0
}
fn bls12_381_msm_g1(_count: u32) -> u64 {
0
}
fn bls12_381_msm_g2(_count: u32) -> u64 {
0
}
fn bls12_381_mul_projective_g1(_count: u32) -> u64 {
0
}
fn bls12_381_mul_projective_g2(_count: u32) -> u64 {
0
}
fn bls12_381_aggregate_g1(_count: u32) -> u64 {
0
}
fn bls12_381_map_to_g2affine(_len: u32) -> u64 {
0
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{DEFAULT_USER_ALICE, Log, Program, System};
use ark_bls12_381::{
Bls12_381, Config as Bls12_381Config, G1Affine, G1Projective as G1, G2Affine,
G2Projective as G2,
};
use ark_ec::{
Group, ScalarMul, VariableBaseMSM,
bls12::Bls12Config as Bls12ConfigTrait,
hashing::{HashToCurve, curve_maps::wb, map_to_curve_hasher::MapToCurveBasedHasher},
pairing::Pairing,
short_weierstrass::{Projective as SWProjective, SWCurveConfig},
};
use ark_ff::{UniformRand, biginteger::BigInt, field_hashers::DefaultFieldHasher};
use ark_scale::{
hazmat::ArkScaleProjective,
scale::{Decode, Encode},
};
use ark_std::test_rng;
use builtins_common::bls12_381::{ark_bls12_381, ark_ec, ark_ff, ark_scale};
use demo_constructor::{Arg, Call, Calls, Scheme, WASM_BINARY};
use gear_common::Origin;
use gear_core::limited::LimitedStr;
use std::ops::Mul;
type ArkScaleLocal<T> = ark_scale::ArkScale<T, { ark_scale::HOST_CALL }>;
type ScalarFieldG1 = <G1 as Group>::ScalarField;
type ScalarFieldG2 = <G2 as Group>::ScalarField;
type LimitedStr1024 = LimitedStr<'static, 1024>;
fn create_proxy_program(
sys: &System,
proxy_id: ActorId,
builtin_req: Vec<u8>,
reply_receiver: ActorId,
) -> Program<'_> {
let proxy_scheme = Scheme::predefined(
// init: do nothing
Calls::builder().noop(),
// handle: send message to bls12-381 builtin
Calls::builder().add_call(Call::Send(
Arg::new(BLS12_381_ID.into_bytes()),
Arg::new(builtin_req),
None,
Arg::new(0u128),
Arg::new(0u32),
)),
// handle_reply: load reply payload and forward it to original sender
Calls::builder()
.add_call(Call::LoadBytes)
.add_call(Call::StoreVec("reply_payload".to_string()))
.add_call(Call::Send(
Arg::new(reply_receiver.into_bytes()),
Arg::get("reply_payload"),
Some(Arg::new(0)),
Arg::new(0u128),
Arg::new(0u32),
)),
// handle_signal: noop
Calls::builder(),
);
let proxy_program = Program::from_binary_with_id(sys, proxy_id, WASM_BINARY);
// Initialize proxy with the scheme
let init_mid = proxy_program.send(reply_receiver, proxy_scheme);
let res = sys.run_next_block();
assert!(res.succeed.contains(&init_mid));
proxy_program
}
#[test]
fn test_multi_miller_loop() {
let sys = System::new();
let alice_id = ActorId::from(DEFAULT_USER_ALICE);
let proxy_pid = ActorId::new([3; 32]);
// -----------------------------------------------------------------------
// ------------------------ Prepare payload ------------------------------
// -----------------------------------------------------------------------
let mut rng = test_rng();
let message: G1Affine = G1::rand(&mut rng).into();
let a: ArkScaleLocal<Vec<<Bls12_381 as Pairing>::G1Affine>> = vec![message].into();
let priv_key: ScalarFieldG2 = UniformRand::rand(&mut rng);
let generator = G2::generator();
let pub_key: G2Affine = generator.mul(priv_key).into();
let b: ArkScaleLocal<Vec<<Bls12_381 as Pairing>::G2Affine>> = vec![pub_key].into();
let multi_miller_req = Bls12_381Request::MultiMillerLoop {
a: a.encode(),
b: b.encode(),
};
// -----------------------------------------------------------------------
// ------------------------ Create proxy program -------------------------
// -----------------------------------------------------------------------
let proxy_program =
create_proxy_program(&sys, proxy_pid, multi_miller_req.encode(), alice_id);
// -----------------------------------------------------------------------
// ------------------------- Trigger builtin -----------------------------
// -----------------------------------------------------------------------
let mid = proxy_program.send_bytes(alice_id, b"");
let res = sys.run_next_block();
assert!(res.succeed.contains(&mid));
// -----------------------------------------------------------------------
// ------------------------- Check response ------------------------------
// -----------------------------------------------------------------------
assert!(res.contains(&Log::builder().source(proxy_pid).dest(alice_id)));
let mut logs = res.decoded_log();
let response = logs.pop().expect("no log found");
assert!(matches!(
response.payload(),
Bls12_381Response::MultiMillerLoop(_)
));
}
#[test]
fn test_final_exponentiation() {
let sys = System::new();
let alice_id = ActorId::from(DEFAULT_USER_ALICE);
let proxy_pid = ActorId::new([3; 32]);
// -----------------------------------------------------------------------
// ------------------------ Prepare payload ------------------------------
// -----------------------------------------------------------------------
let mut rng = ark_std::test_rng();
let message: G1Affine = G1::rand(&mut rng).into();
let priv_key: ScalarFieldG2 = UniformRand::rand(&mut rng);
let generator: G2 = G2::generator();
let pub_key: G2Affine = generator.mul(priv_key).into();
let loop_result = <Bls12_381 as Pairing>::multi_miller_loop(vec![message], vec![pub_key]);
let expected = <Bls12_381 as Pairing>::final_exponentiation(loop_result);
let f: ark_scale::ArkScale<<Bls12_381 as Pairing>::TargetField> = loop_result.0.into();
let final_expon_req = Bls12_381Request::FinalExponentiation { f: f.encode() };
// -----------------------------------------------------------------------
// ------------------------ Create proxy program -------------------------
// -----------------------------------------------------------------------
let proxy_program =
create_proxy_program(&sys, proxy_pid, final_expon_req.encode(), alice_id);
// -----------------------------------------------------------------------
// ------------------------- Trigger builtin -----------------------------
// -----------------------------------------------------------------------
let mid = proxy_program.send_bytes(alice_id, b"");
let res = sys.run_next_block();
assert!(res.succeed.contains(&mid));
// -----------------------------------------------------------------------
// ------------------------- Check response ------------------------------
// -----------------------------------------------------------------------
assert!(res.contains(&Log::builder().source(proxy_pid).dest(alice_id)));
let mut logs = res.decoded_log();
let response = logs.pop().expect("no log found");
if let Bls12_381Response::FinalExponentiation(result_bytes) = response.payload() {
let actual = ArkScaleLocal::<<Bls12_381 as Pairing>::TargetField>::decode(
&mut result_bytes.as_ref(),
)
.expect("failed to decode result");
assert!(matches!(expected, Some(inner) if inner.0 == actual.0));
} else {
panic!("unexpected response");
}
}
#[test]
fn test_msm_g1() {
let sys = System::new();
let alice_actor_id = ActorId::from(DEFAULT_USER_ALICE);
let proxy_pid = ActorId::new([3; 32]);
// -----------------------------------------------------------------------
// ------------------------ Prepare payload ------------------------------
// -----------------------------------------------------------------------
let mut rng = test_rng();
let count = 5usize;
let scalars = (0..count)
.map(|_| ScalarFieldG1::rand(&mut rng))
.collect::<Vec<_>>();
let bases = G1::batch_convert_to_mul_base(
&(0..count).map(|_| G1::rand(&mut rng)).collect::<Vec<_>>(),
);
let faulty_ark_scalars: ArkScaleLocal<Vec<ScalarFieldG1>> = scalars[1..].to_vec().into();
let ark_bases: ArkScaleLocal<Vec<G1Affine>> = bases.clone().into();
let faulty_msm_g1_req = Bls12_381Request::MultiScalarMultiplicationG1 {
bases: ark_bases.encode(),
scalars: faulty_ark_scalars.encode(),
};
// -----------------------------------------------------------------------
// ----------------------- Create a faulty proxy -------------------------
// -----------------------------------------------------------------------
// Because of the impl of the demo_constructor, we have to waste 1 program as we
// cannot predefine `handle_reply` without defining `handle` (using
// `Scheme::predefined`). So we have to define a proxy with `handle`
// sending the faulty request
let proxy_program = create_proxy_program(
&sys,
gprimitives::H256::random().cast(),
faulty_msm_g1_req.encode(),
alice_actor_id,
);
// -----------------------------------------------------------------------
// ------------------------- Trigger builtin -----------------------------
// -----------------------------------------------------------------------
let mid = proxy_program.send_bytes(alice_actor_id, b"");
let res = sys.run_next_block();
assert!(res.succeed.contains(&mid));
// -----------------------------------------------------------------------
// ----------------------- Check error response --------------------------
// -----------------------------------------------------------------------
let err_payload =
LimitedStr1024::from_small_str("Multi scalar multiplication: uneven item count")
.into_inner()
.into_owned()
.into_bytes();
assert!(
res.contains(
&Log::builder()
.source(proxy_program.id())
.dest(alice_actor_id)
.payload_bytes(err_payload)
)
);
// -----------------------------------------------------------------------
// ------------------------ Prepare valid payload -------------------------
// -----------------------------------------------------------------------
let expected =
<SWProjective<ark_bls12_381::g1::Config> as VariableBaseMSM>::msm(&bases, &scalars)
.expect("msmexpected result generation failed");
let ark_scalars: ArkScaleLocal<Vec<ScalarFieldG1>> = scalars.into();
let ark_bases: ArkScaleLocal<Vec<G1Affine>> = bases.into();
let msm_g1_req = Bls12_381Request::MultiScalarMultiplicationG1 {
bases: ark_bases.encode(),
scalars: ark_scalars.encode(),
};
// -----------------------------------------------------------------------
// ------------------------ Create proxy program -------------------------
// -----------------------------------------------------------------------
let proxy_program =
create_proxy_program(&sys, proxy_pid, msm_g1_req.encode(), alice_actor_id);
// -----------------------------------------------------------------------
// ------------------------- Trigger builtin -----------------------------
// -----------------------------------------------------------------------
let mid = proxy_program.send_bytes(alice_actor_id, b"");
let res = sys.run_next_block();
assert!(res.succeed.contains(&mid));
// -----------------------------------------------------------------------
// ------------------------- Check response ------------------------------
// -----------------------------------------------------------------------
let mut logs = res.decoded_log();
let response = logs.pop().expect("no log found");
if let Bls12_381Response::MultiScalarMultiplicationG1(result_bytes) = response.payload() {
let actual = ArkScaleProjective::<G1>::decode(&mut result_bytes.as_ref())
.expect("failed to decode result");
assert_eq!(actual.0, expected);
} else {
panic!("unexpected response");
}
}
#[test]
fn test_msm_g2() {
let sys = System::new();
let alice_actor_id = ActorId::from(DEFAULT_USER_ALICE);
let proxy_pid = ActorId::new([3; 32]);
// -----------------------------------------------------------------------
// ------------------------ Prepare payload ------------------------------
// -----------------------------------------------------------------------
let mut rng = test_rng();
let count = 5usize;
let scalars = (0..count)
.map(|_| ScalarFieldG2::rand(&mut rng))
.collect::<Vec<_>>();
let bases = G2::batch_convert_to_mul_base(
&(0..count).map(|_| G2::rand(&mut rng)).collect::<Vec<_>>(),
);
let faulty_ark_scalars: ArkScaleLocal<Vec<ScalarFieldG2>> = scalars[1..].to_vec().into();
let ark_bases: ArkScaleLocal<Vec<G2Affine>> = bases.clone().into();
let faulty_msm_g2_req = Bls12_381Request::MultiScalarMultiplicationG2 {
bases: ark_bases.encode(),
scalars: faulty_ark_scalars.encode(),
};
// -----------------------------------------------------------------------
// ----------------------- Create a faulty proxy -------------------------
// -----------------------------------------------------------------------
// Because of the impl of the demo_constructor, we have to waste 1
// program as we cannot predefine `handle_reply` without defining
// `handle` (using `Scheme::predefined`). So we have to define a
// proxy with `handle` sending the faulty request
let proxy_program = create_proxy_program(
&sys,
gprimitives::H256::random().cast(),
faulty_msm_g2_req.encode(),
alice_actor_id,
);
// -----------------------------------------------------------------------
// ------------------------- Trigger builtin -----------------------------
// -----------------------------------------------------------------------
let mid = proxy_program.send_bytes(alice_actor_id, b"");
let res = sys.run_next_block();
assert!(res.succeed.contains(&mid));
// -----------------------------------------------------------------------
// ----------------------- Check error response --------------------------
// -----------------------------------------------------------------------
let err_payload =
LimitedStr1024::from_small_str("Multi scalar multiplication: uneven item count")
.into_inner()
.into_owned()
.into_bytes();
assert!(
res.contains(
&Log::builder()
.source(proxy_program.id())
.dest(alice_actor_id)
.payload_bytes(err_payload)
)
);
// -----------------------------------------------------------------------
// ------------------------ Prepare valid payload ------------------------
// -----------------------------------------------------------------------
let expected =
<SWProjective<ark_bls12_381::g2::Config> as VariableBaseMSM>::msm(&bases, &scalars)
.expect("msm expected result generation failed");
let ark_scalars: ArkScaleLocal<Vec<ScalarFieldG2>> = scalars.into();
let ark_bases: ArkScaleLocal<Vec<G2Affine>> = bases.into();
let msm_g2_req = Bls12_381Request::MultiScalarMultiplicationG2 {
bases: ark_bases.encode(),
scalars: ark_scalars.encode(),
};
// -----------------------------------------------------------------------
// ------------------------ Create proxy program -------------------------
// -----------------------------------------------------------------------
let proxy_program =
create_proxy_program(&sys, proxy_pid, msm_g2_req.encode(), alice_actor_id);
// -----------------------------------------------------------------------
// ------------------------- Trigger builtin -----------------------------
// -----------------------------------------------------------------------
let mid = proxy_program.send_bytes(alice_actor_id, b"");
let res = sys.run_next_block();
assert!(res.succeed.contains(&mid));
// -----------------------------------------------------------------------
// ------------------------- Check response ------------------------------
// -----------------------------------------------------------------------
let mut logs = res.decoded_log();
let response = logs.pop().expect("no log found");
if let Bls12_381Response::MultiScalarMultiplicationG2(result_bytes) = response.payload() {
let actual = ArkScaleProjective::<G2>::decode(&mut result_bytes.as_ref())
.expect("failed to decode result");
assert_eq!(actual.0, expected);
} else {
panic!("unexpected response");
}
}
#[test]
fn test_projective_multiplication_g1() {
let sys = System::new();
let alice_actor_id = ActorId::from(DEFAULT_USER_ALICE);
let proxy_pid = ActorId::new([3; 32]);
// -----------------------------------------------------------------------
// ------------------------ Prepare payload ------------------------------
// -----------------------------------------------------------------------
let mut rng = test_rng();
let bigint = BigInt::<3>::rand(&mut rng).0.to_vec();
let base = G1::rand(&mut rng);
let expected = <ark_bls12_381::g1::Config as SWCurveConfig>::mul_projective(&base, &bigint);
let ark_bigint: ArkScaleLocal<Vec<u64>> = bigint.into();
let ark_base: ArkScaleProjective<G1> = base.into();
let proj_mul_g1_req = Bls12_381Request::ProjectiveMultiplicationG1 {
base: ark_base.encode(),
scalar: ark_bigint.encode(),
};
// -----------------------------------------------------------------------
// ------------------------ Create proxy program -------------------------
// -----------------------------------------------------------------------
let proxy_program =
create_proxy_program(&sys, proxy_pid, proj_mul_g1_req.encode(), alice_actor_id);
// -----------------------------------------------------------------------
// ------------------------- Trigger builtin -----------------------------
// -----------------------------------------------------------------------
let mid = proxy_program.send_bytes(alice_actor_id, b"");
let res = sys.run_next_block();
assert!(res.succeed.contains(&mid));
// -----------------------------------------------------------------------
// ------------------------- Check response ------------------------------
// -----------------------------------------------------------------------
let mut logs = res.decoded_log();
let response = logs.pop().expect("no log found");
if let Bls12_381Response::ProjectiveMultiplicationG1(result_bytes) = response.payload() {
let actual = ArkScaleProjective::<G1>::decode(&mut result_bytes.as_ref())
.expect("failed to decode result");
assert_eq!(actual.0, expected);
} else {
panic!("unexpected response");
}
}
#[test]
fn test_projective_multiplication_g2() {
let sys = System::new();
let alice_actor_id = ActorId::from(DEFAULT_USER_ALICE);
let proxy_pid = ActorId::new([3; 32]);
// -----------------------------------------------------------------------
// ------------------------ Prepare payload ------------------------------
// -----------------------------------------------------------------------
let mut rng = test_rng();
let bigint = BigInt::<3>::rand(&mut rng).0.to_vec();
let base = G2::rand(&mut rng);
let expected = <ark_bls12_381::g2::Config as SWCurveConfig>::mul_projective(&base, &bigint);
let ark_bigint: ArkScaleLocal<Vec<u64>> = bigint.into();
let ark_base: ArkScaleProjective<G2> = base.into();
let proj_mul_g2_req = Bls12_381Request::ProjectiveMultiplicationG2 {
base: ark_base.encode(),
scalar: ark_bigint.encode(),
};
// -----------------------------------------------------------------------
// ------------------------ Create proxy program -------------------------
// -----------------------------------------------------------------------
let proxy_program =
create_proxy_program(&sys, proxy_pid, proj_mul_g2_req.encode(), alice_actor_id);
// -----------------------------------------------------------------------
// ------------------------- Trigger builtin -----------------------------
// -----------------------------------------------------------------------
let mid = proxy_program.send_bytes(alice_actor_id, b"");
let res = sys.run_next_block();
assert!(res.succeed.contains(&mid));
// -----------------------------------------------------------------------
// ------------------------- Check response ------------------------------
// -----------------------------------------------------------------------
let mut logs = res.decoded_log();
let response = logs.pop().expect("no log found");
if let Bls12_381Response::ProjectiveMultiplicationG2(result_bytes) = response.payload() {
let actual = ArkScaleProjective::<G2>::decode(&mut result_bytes.as_ref())
.expect("failed to decode result");
assert_eq!(actual.0, expected);
} else {
panic!("unexpected response");
}
}
#[test]
fn test_aggregate_g1() {
let sys = System::new();
let alice_actor_id = ActorId::from(DEFAULT_USER_ALICE);
let proxy_pid = ActorId::new([3; 32]);
// -----------------------------------------------------------------------
// ------------------------ Prepare payload ------------------------------
// -----------------------------------------------------------------------
let mut rng = test_rng();
const COUNT: usize = 5;
let points = (0..COUNT).map(|_| G1::rand(&mut rng)).collect::<Vec<_>>();
let ark_points: ArkScaleLocal<Vec<G1>> = points.clone().into();
let aggregate_g1_req = Bls12_381Request::AggregateG1 {
points: ark_points.encode(),
};
// -----------------------------------------------------------------------
// ------------------------ Create proxy program -------------------------
// -----------------------------------------------------------------------
let proxy_program =
create_proxy_program(&sys, proxy_pid, aggregate_g1_req.encode(), alice_actor_id);
// -----------------------------------------------------------------------
// ------------------------- Trigger builtin -----------------------------
// -----------------------------------------------------------------------
let mid = proxy_program.send_bytes(alice_actor_id, b"");
let res = sys.run_next_block();
assert!(res.succeed.contains(&mid));
// -----------------------------------------------------------------------
// ------------------------- Check response ------------------------------
// -----------------------------------------------------------------------
let mut logs = res.decoded_log();
let response = logs.pop().expect("no log found");
if let Bls12_381Response::AggregateG1(result_bytes) = response.payload() {
let actual = ArkScaleLocal::<G1>::decode(&mut result_bytes.as_ref())
.expect("failed to decode result");
let point_first = points.first().unwrap();
let expected = points
.iter()
.skip(1)
.fold(*point_first, |aggregated, point| aggregated + *point);
assert_eq!(actual.0, expected);
} else {
panic!("unexpected response");
}
}
#[test]
fn test_map_to_g2affine() {
let sys = System::new();
let alice_actor_id = ActorId::from(DEFAULT_USER_ALICE);
let proxy_pid = ActorId::new([3; 32]);
// -----------------------------------------------------------------------
// ------------------------ Prepare payload ------------------------------
// -----------------------------------------------------------------------
let message = b"Hello, decentralized world!".to_vec();
let map_to_g2_req = Bls12_381Request::MapToG2Affine {
message: message.clone(),
};
// -----------------------------------------------------------------------
// ------------------------ Create proxy program -------------------------
// -----------------------------------------------------------------------
let proxy_program =
create_proxy_program(&sys, proxy_pid, map_to_g2_req.encode(), alice_actor_id);
// -----------------------------------------------------------------------
// ------------------------- Trigger builtin -----------------------------
// -----------------------------------------------------------------------
let mid = proxy_program.send_bytes(alice_actor_id, b"");
let res = sys.run_next_block();
assert!(res.succeed.contains(&mid));
// -----------------------------------------------------------------------
// ------------------------- Check response ------------------------------
// -----------------------------------------------------------------------
let mut logs = res.decoded_log();
let response = logs.pop().expect("no log found");
if let Bls12_381Response::MapToG2Affine(result_bytes) = response.payload() {
let actual = ArkScaleLocal::<G2Affine>::decode(&mut result_bytes.as_ref())
.expect("failed to decode result");
// Verify the result matches what arkworks would produce
type WBMap = wb::WBMap<<Bls12_381Config as Bls12ConfigTrait>::G2Config>;
const DST_G2: &[u8] = b"BLS_SIG_BLS12381G2_XMD:SHA-256_SSWU_RO_POP_";
let mapper =
MapToCurveBasedHasher::<G2, DefaultFieldHasher<sha2::Sha256>, WBMap>::new(DST_G2)
.expect("mapper creation failed");
let expected = mapper.hash(&message).expect("hash to curve failed");
assert_eq!(actual.0, expected);
} else {
panic!("unexpected response");
}
}
}