Skip to content

Commit f908591

Browse files
authored
Remove script_lines (BitVM#281)
1 parent 8d06b42 commit f908591

File tree

2 files changed

+72
-78
lines changed

2 files changed

+72
-78
lines changed

bitvm/src/bn254/fq6.rs

Lines changed: 36 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -157,58 +157,54 @@ impl Fq6 {
157157
let (hinted_script4, hints4) = Fq2::hinted_mul(2, a.c1, 0, a.c2);
158158
let (hinted_script5, hints5) = Fq2::hinted_square(a.c2);
159159

160-
let mut script = script! {};
161-
let script_lines = [
160+
let script = script! {
162161
// compute s_0 = a_0 ^ 2
163-
Fq2::copy(4),
164-
hinted_script1,
162+
{ Fq2::copy(4) }
163+
{ hinted_script1 }
165164
// compute a_0 + a_2
166-
Fq2::roll(6),
167-
Fq2::copy(4),
168-
Fq2::add(2, 0),
165+
{ Fq2::roll(6) }
166+
{ Fq2::copy(4) }
167+
{ Fq2::add(2, 0) }
169168
// compute s_1 = (a_0 + a_1 + a_2) ^ 2
170-
Fq2::copy(0),
171-
Fq2::copy(8),
172-
Fq2::add(2, 0),
173-
hinted_script2,
169+
{ Fq2::copy(0) }
170+
{ Fq2::copy(8) }
171+
{ Fq2::add(2, 0) }
172+
{ hinted_script2 }
174173
// compute s_2 = (a_0 - a_1 + a_2) ^ 2
175-
Fq2::copy(8),
176-
Fq2::sub(4, 0),
177-
hinted_script3,
174+
{ Fq2::copy(8) }
175+
{ Fq2::sub(4, 0) }
176+
{ hinted_script3 }
178177
// compute s_3 = 2a_1a_2
179-
Fq2::roll(8),
180-
Fq2::copy(8),
181-
hinted_script4,
182-
Fq2::double(0),
178+
{ Fq2::roll(8) }
179+
{ Fq2::copy(8) }
180+
{ hinted_script4 }
181+
{ Fq2::double(0) }
183182
// compute s_4 = a_2 ^ 2
184-
Fq2::roll(8),
185-
hinted_script5,
183+
{ Fq2::roll(8) }
184+
{ hinted_script5 }
186185
// compute t_1 = (s_1 + s_2) / 2
187-
Fq2::copy(6),
188-
Fq2::roll(6),
189-
Fq2::add(2, 0),
190-
Fq2::div2(),
186+
{ Fq2::copy(6) }
187+
{ Fq2::roll(6) }
188+
{ Fq2::add(2, 0) }
189+
{ Fq2::div2() }
191190
// at this point, we have s_0, s_1, s_3, s_4, t_1
192191

193192
// compute c_0 = s_0 + \beta s_3
194-
Fq2::copy(4),
195-
Fq6::mul_fq2_by_nonresidue(),
196-
Fq2::copy(10),
197-
Fq2::add(2, 0),
193+
{ Fq2::copy(4) }
194+
{ Fq6::mul_fq2_by_nonresidue() }
195+
{ Fq2::copy(10) }
196+
{ Fq2::add(2, 0) }
198197
// compute c_1 = s_1 - s_3 - t_1 + \beta s_4
199-
Fq2::copy(4),
200-
Fq6::mul_fq2_by_nonresidue(),
201-
Fq2::copy(4),
202-
Fq2::add(10, 0),
203-
Fq2::sub(10, 0),
204-
Fq2::add(2, 0),
198+
{ Fq2::copy(4) }
199+
{ Fq6::mul_fq2_by_nonresidue() }
200+
{ Fq2::copy(4) }
201+
{ Fq2::add(10, 0) }
202+
{ Fq2::sub(10, 0) }
203+
{ Fq2::add(2, 0) }
205204
// compute c_2 = t_1 - s_0 - s_4
206-
Fq2::add(8, 6),
207-
Fq2::sub(6, 0),
208-
];
209-
for script_line in script_lines {
210-
script = script.push_script(script_line.compile());
211-
}
205+
{ Fq2::add(8, 6) }
206+
{ Fq2::sub(6, 0) }
207+
};
212208

213209
hints.extend(hints1);
214210
hints.extend(hints2);

bitvm/src/signatures/winternitz.rs

Lines changed: 36 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -389,19 +389,18 @@ impl Converter for ToBytesConverter {
389389
ps.byte_message_length()
390390
}
391391
fn get_script(ps: &Parameters) -> Script {
392-
let mut turning_into_bytes = script! {};
393392
if ps.block_length == 8 {
394393
//already bytes
395-
turning_into_bytes = script! {};
394+
script! {}
396395
} else if ps.block_length == 4 {
397-
turning_into_bytes = script! {
396+
script! {
398397
for i in 0..ps.message_length / 2 {
399398
OP_SWAP
400399
for _ in 0..ps.block_length {
401400
OP_DUP OP_ADD
402401
}
403402
OP_ADD
404-
if i != (ps.message_length/2) - 1 {
403+
if i != (ps.message_length / 2) - 1 {
405404
OP_TOALTSTACK
406405
}
407406
}
@@ -410,23 +409,43 @@ impl Converter for ToBytesConverter {
410409
OP_FROMALTSTACK
411410
}
412411
}
413-
};
412+
}
414413
} else {
415414
let mut lens: Vec<u32> = vec![];
416-
let mut script_lines: Vec<Script> = vec![];
415+
let mut split_save = vec![];
417416
for i in 0..ps.message_length {
418417
let start = i * ps.block_length;
419418
let next_stop = start + 8 - (start % 8);
420419
let split = next_stop - start;
420+
split_save.push(split);
421421
if split >= ps.block_length {
422422
lens.push(ps.block_length);
423-
script_lines.push(script! {
424-
OP_TOALTSTACK
425-
});
426423
} else {
427424
lens.push(split);
428425
lens.push(ps.block_length - split);
429-
script_lines.push(script! {
426+
}
427+
}
428+
lens.reverse();
429+
let mut last_bytes_var = (8 - (ps.message_length * ps.block_length % 8)) % 8;
430+
let mut is_last_zero_var = true;
431+
let mut last_bytes_save = vec![];
432+
let mut is_last_zero_save = vec![];
433+
for l in lens.clone() {
434+
last_bytes_save.push(last_bytes_var);
435+
if last_bytes_var >= 8 {
436+
last_bytes_var = 0;
437+
is_last_zero_var = true;
438+
}
439+
is_last_zero_save.push(is_last_zero_var);
440+
is_last_zero_var = false;
441+
last_bytes_var += l;
442+
}
443+
444+
script! {
445+
for split in split_save {
446+
if split >= ps.block_length {
447+
OP_TOALTSTACK
448+
} else {
430449
OP_0
431450
for j in (split..ps.block_length).rev() {
432451
if j != ps.block_length - 1 {
@@ -449,43 +468,22 @@ impl Converter for ToBytesConverter {
449468
OP_SWAP
450469
OP_TOALTSTACK
451470
OP_TOALTSTACK
452-
});
471+
}
453472
}
454-
}
455-
lens.reverse();
456-
let mut last_bytes = (8 - (ps.message_length * ps.block_length % 8)) % 8;
457-
let mut is_last_zero = true;
458-
script_lines.push(script! {
459473
OP_0
460-
});
461-
for l in lens {
462-
if last_bytes >= 8 {
463-
last_bytes = 0;
464-
script_lines.push(script! {
474+
for (l, (last_bytes, is_last_zero)) in lens.into_iter().zip(last_bytes_save.into_iter().zip(is_last_zero_save.into_iter())) {
475+
if last_bytes >= 8 {
465476
OP_0
466-
});
467-
is_last_zero = true;
468-
}
469-
if !is_last_zero {
470-
script_lines.push(script! {
477+
}
478+
if !is_last_zero {
471479
for _ in 0..l {
472480
OP_DUP OP_ADD
473481
}
474-
});
482+
}
483+
OP_FROMALTSTACK OP_ADD
475484
}
476-
is_last_zero = false;
477-
script_lines.push(script! {
478-
OP_FROMALTSTACK
479-
OP_ADD
480-
});
481-
last_bytes += l;
482-
}
483-
484-
for script_line in script_lines {
485-
turning_into_bytes = turning_into_bytes.push_script(script_line.compile());
486485
}
487486
}
488-
turning_into_bytes
489487
}
490488
}
491489

0 commit comments

Comments
 (0)