Skip to content

Commit a49b8f2

Browse files
author
phoebe
authored
math.big: rework function naming and documentation (#18890)
1 parent bd3501a commit a49b8f2

10 files changed

Lines changed: 178 additions & 116 deletions

File tree

vlib/crypto/ed25519/internal/edwards25519/element_test.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ fn (mut v Element) to_big_integer() big.Integer {
364364

365365
// from_big_integer sets v = n, and returns v. The bit length of n must not exceed 256.
366366
fn (mut v Element) from_big_integer(n big.Integer) !Element {
367-
if n.binary_str().len > 32 * 8 {
367+
if n.bin_str().len > 32 * 8 {
368368
return error('invalid edwards25519 element input size')
369369
}
370370
mut bytes, _ := n.bytes()

vlib/crypto/ed25519/internal/edwards25519/scalar_test.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ fn test_scalar_set_uniform_bytes() {
119119
// mod, _ := new(big.Integer).SetString("27742317777372353535851937790883648493", 10)
120120
mut mod := big.integer_from_string('27742317777372353535851937790883648493')!
121121
// mod.Add(mod, new(big.Integer).Lsh(big.NewInt(1), 252))
122-
mod = mod + big.integer_from_i64(1).lshift(252)
122+
mod = mod + big.integer_from_i64(1).left_shift(252)
123123

124124
mut sc := generate_scalar(100)!
125125
inp := rand.bytes(64)!

vlib/math/big/big.js.v

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ pub fn (a &Number) % (b &Number) Number {
8888
return c
8989
}*/
9090

91-
pub fn divmod(a &Number, b &Number) (Number, Number) {
91+
pub fn div_mod(a &Number, b &Number) (Number, Number) {
9292
c := Number{}
9393
d := Number{}
9494
#c.value = a.val.value / b.val.value
@@ -97,6 +97,11 @@ pub fn divmod(a &Number, b &Number) (Number, Number) {
9797
return c, d
9898
}
9999

100+
[deprecated: 'use div_mod(a, b) instead']
101+
pub fn divmod(a &Number, b &Number) (Number, Number) {
102+
return div_mod(a, b)
103+
}
104+
100105
pub fn cmp(a &Number, b &Number) int {
101106
res := 0
102107

@@ -137,37 +142,62 @@ pub fn (a &Number) isqrt() Number {
137142
return b
138143
}
139144

145+
[deprecated: 'use bitwise_and(a, b) instead']
140146
pub fn b_and(a &Number, b &Number) Number {
147+
return bitwise_and(a, b)
148+
}
149+
150+
[deprecated: 'use bitwise_or(a, b) instead']
151+
pub fn b_or(a &Number, b &Number) Number {
152+
return bitwise_or(a, b)
153+
}
154+
155+
[deprecated: 'use bitwise_xor(a, b) instead']
156+
pub fn b_xor(a &Number, b &Number) Number {
157+
return bitwise_xor(a, b)
158+
}
159+
160+
pub fn bitwise_and(a &Number, b &Number) Number {
141161
c := Number{}
142162
#c.value = a.val.value & b.val.value
143163

144164
return c
145165
}
146166

147-
pub fn b_or(a &Number, b &Number) Number {
167+
pub fn bitwise_or(a &Number, b &Number) Number {
148168
c := Number{}
149169
#c.value = a.val.value | b.val.value
150170

151171
return c
152172
}
153173

154-
pub fn b_xor(a &Number, b &Number) Number {
174+
pub fn bitwise_xor(a &Number, b &Number) Number {
155175
c := Number{}
156176
#c.value = a.val.value ^ b.val.value
157177

158178
return c
159179
}
160180

161-
pub fn (a &Number) lshift(nbits int) Number {
181+
[deprecated: 'use a.left_shift(amount) instead']
182+
pub fn (a &Number) lshift(amount int) Number {
183+
return a.left_shift(amount)
184+
}
185+
186+
[deprecated: 'use a.right_shift(amount) instead']
187+
pub fn (a &Number) rshift(amount int) Number {
188+
return a.right_shift(amount)
189+
}
190+
191+
pub fn (a &Number) left_shift(amount int) Number {
162192
c := Number{}
163-
#c.value = a.val.value << BigInt(+nbits)
193+
#c.value = a.val.value << BigInt(+amount)
164194

165195
return c
166196
}
167197

168-
pub fn (a &Number) rshift(nbits int) Number {
198+
pub fn (a &Number) right_shift(amount int) Number {
169199
c := Number{}
170-
#c.value = a.val.value << BigInt(+nbits)
200+
#c.value = a.val.value << BigInt(+amount)
171201

172202
return c
173203
}
@@ -193,6 +223,11 @@ pub fn factorial(nn &Number) Number {
193223
return a
194224
}
195225

226+
[deprecated: 'use factorial_int instead']
196227
pub fn fact(n int) Number {
228+
return factorial_int(n)
229+
}
230+
231+
pub fn factorial_int(n int) Number {
197232
return factorial(from_int(n))
198233
}

vlib/math/big/big_test.v

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -389,15 +389,15 @@ const factorial_test_data = [
389389
]
390390
// vfmt on
391391

392-
// for lshift and rshift
392+
// for left_shift and right_shift
393393
struct ShiftTest {
394394
base TestInteger
395395
amount u32
396396
expected TestInteger
397397
}
398398

399399
// vfmt off
400-
const lshift_test_data = [
400+
const left_shift_test_data = [
401401
ShiftTest{ 45, 2, 45 * 4 },
402402
ShiftTest{ 45, 3, 45 * 8 },
403403
ShiftTest{ 45, 4, 45 * 16 },
@@ -406,7 +406,7 @@ const lshift_test_data = [
406406
ShiftTest{ [u8(1), 1, 1], 56, [u8(1), 1, 1, 0, 0, 0, 0, 0, 0, 0] },
407407
]
408408

409-
const rshift_test_data = [
409+
const right_shift_test_data = [
410410
ShiftTest{ 45, 3, 5 },
411411
ShiftTest{ 0x13374956, 16, 0x1337 },
412412
ShiftTest{ [u8(1), 1, 1, 0, 0, 0, 0, 0, 0, 0], 56, [u8(1), 1, 1] },
@@ -435,7 +435,7 @@ const bit_len_test_data = [
435435
BitLenTest{ big.zero_int, 0 },
436436
BitLenTest{ big.one_int, 1 },
437437
BitLenTest{ u32(0xffffffff), 32 },
438-
BitLenTest{ big.one_int.lshift(1239), 1240 },
438+
BitLenTest{ big.one_int.left_shift(1239), 1240 },
439439
BitLenTest{ '4338476092346017364013796407961305761039463198075691378460917856', 212 },
440440
]
441441
// vfmt on
@@ -639,15 +639,15 @@ fn test_inc_and_dec() {
639639
assert b == c
640640
}
641641

642-
fn test_lshift() {
643-
for t in lshift_test_data {
644-
assert t.base.parse().lshift(t.amount) == t.expected.parse()
642+
fn test_left_shift() {
643+
for t in left_shift_test_data {
644+
assert t.base.parse().left_shift(t.amount) == t.expected.parse()
645645
}
646646
}
647647

648-
fn test_rshift() {
649-
for t in rshift_test_data {
650-
assert t.base.parse().rshift(t.amount) == t.expected.parse()
648+
fn test_right_shift() {
649+
for t in right_shift_test_data {
650+
assert t.base.parse().right_shift(t.amount) == t.expected.parse()
651651
}
652652
}
653653

@@ -693,7 +693,7 @@ fn test_isqrt() {
693693
}
694694

695695
fn test_bitwise_ops() {
696-
a := big.integer_from_int(1).lshift(512)
696+
a := big.integer_from_int(1).left_shift(512)
697697
b := a - big.one_int
698698
assert a.bitwise_and(b) == big.zero_int
699699
assert b.bitwise_xor(b) == big.zero_int
@@ -732,7 +732,7 @@ fn test_set_bit() {
732732
a.set_bit(3, true)
733733
assert a.int() == 40
734734
a.set_bit(50, true)
735-
assert a == big.one_int.lshift(50) + big.integer_from_int(40)
735+
assert a == big.one_int.left_shift(50) + big.integer_from_int(40)
736736
b := a
737737
a.set_bit(100, false)
738738
assert a == b

vlib/math/big/division_array_ops.v

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ fn binary_divide_array_by_array(operand_a []u32, operand_b []u32, mut quotient [
3434

3535
// align
3636
if lead_zer_remainder < lead_zer_divisor {
37-
lshift_in_place(mut divisor, lead_zer_divisor - lead_zer_remainder)
37+
left_shift_in_place(mut divisor, lead_zer_divisor - lead_zer_remainder)
3838
} else if lead_zer_remainder > lead_zer_divisor {
39-
lshift_in_place(mut remainder, lead_zer_remainder - lead_zer_divisor)
39+
left_shift_in_place(mut remainder, lead_zer_remainder - lead_zer_divisor)
4040
}
4141

4242
$if debug {
@@ -47,13 +47,13 @@ fn binary_divide_array_by_array(operand_a []u32, operand_b []u32, mut quotient [
4747
bit_set(mut quotient, bit_idx)
4848
subtract_align_last_byte_in_place(mut remainder, divisor)
4949
}
50-
rshift_in_place(mut divisor, 1)
50+
right_shift_in_place(mut divisor, 1)
5151
}
5252

5353
// adjust
5454
if lead_zer_remainder > lead_zer_divisor {
55-
// rshift_in_place(mut quotient, lead_zer_remainder - lead_zer_divisor)
56-
rshift_in_place(mut remainder, lead_zer_remainder - lead_zer_divisor)
55+
// right_shift_in_place(mut quotient, lead_zer_remainder - lead_zer_divisor)
56+
right_shift_in_place(mut remainder, lead_zer_remainder - lead_zer_divisor)
5757
}
5858
shrink_tail_zeros(mut remainder)
5959
shrink_tail_zeros(mut quotient)
@@ -115,7 +115,7 @@ fn subtract_align_last_byte_in_place(mut a []u32, b []u32) {
115115
// there is no overflow. We know that the last bits are zero
116116
// and that n <= 32
117117
[direct_array_access; inline]
118-
fn lshift_in_place(mut a []u32, n u32) {
118+
fn left_shift_in_place(mut a []u32, n u32) {
119119
mut carry := u32(0)
120120
mut prec_carry := u32(0)
121121
mask := ((u32(1) << n) - 1) << (32 - n)
@@ -130,7 +130,7 @@ fn lshift_in_place(mut a []u32, n u32) {
130130
// logical right shift without control because these digits have already been
131131
// shift left before
132132
[direct_array_access; inline]
133-
fn rshift_in_place(mut a []u32, n u32) {
133+
fn right_shift_in_place(mut a []u32, n u32) {
134134
mut carry := u32(0)
135135
mut prec_carry := u32(0)
136136
mask := u32((1 << n) - 1)

vlib/math/big/division_array_ops_test.v

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,35 @@ module big
22

33
import rand
44

5-
fn test_lshift_in_place() {
5+
fn test_left_shift_in_place() {
66
mut a := [u32(1), 1, 1, 1, 1]
7-
lshift_in_place(mut a, 1)
7+
left_shift_in_place(mut a, 1)
88
assert a == [u32(2), 2, 2, 2, 2]
9-
lshift_in_place(mut a, 7)
9+
left_shift_in_place(mut a, 7)
1010
assert a == [u32(256), 256, 256, 256, 256]
1111
mut b := [u32(0x80000001), 0xc0000000, 0x80000000, 0x7fffffff]
12-
lshift_in_place(mut b, 1)
12+
left_shift_in_place(mut b, 1)
1313
assert b == [u32(2), 0x80000001, 1, 0xffffffff]
1414
mut c := [u32(0x00ffffff), 0xf0f0f0f0, 1, 0x3fffffff, 1]
15-
lshift_in_place(mut c, 2)
15+
left_shift_in_place(mut c, 2)
1616
assert c == [u32(0x3fffffc), 0xc3c3c3c0, 7, 0xfffffffc, 4]
1717
}
1818

19-
fn test_rshift_in_place() {
19+
fn test_right_shift_in_place() {
2020
mut a := [u32(2), 2, 2, 2, 2]
21-
rshift_in_place(mut a, 1)
21+
right_shift_in_place(mut a, 1)
2222
assert a == [u32(1), 1, 1, 1, 1]
2323
a = [u32(256), 256, 256, 256, 256]
24-
rshift_in_place(mut a, 7)
24+
right_shift_in_place(mut a, 7)
2525
assert a == [u32(2), 2, 2, 2, 2]
2626
a = [u32(0), 0, 1]
27-
rshift_in_place(mut a, 1)
27+
right_shift_in_place(mut a, 1)
2828
assert a == [u32(0), 0x80000000, 0]
2929
mut b := [u32(3), 0x80000001, 1, 0xffffffff]
30-
rshift_in_place(mut b, 1)
30+
right_shift_in_place(mut b, 1)
3131
assert b == [u32(0x80000001), 0xc0000000, 0x80000000, 0x7fffffff]
3232
mut c := [u32(0x03ffffff), 0xc3c3c3c0, 7, 0xfffffffc, 4]
33-
rshift_in_place(mut c, 2)
33+
right_shift_in_place(mut c, 2)
3434
assert c == [u32(0x00ffffff), 0xf0f0f0f0, 1, 0x3fffffff, 1]
3535
}
3636

vlib/math/big/exponentiation.v

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ fn (m Integer) montgomery() MontgomeryContext {
2929
// ri := multiplicative inverse of r in the ring Z/nZ
3030
// ri * r == 1 (mod n)
3131
// ni = ((ri * 2^(log_2(n))) - 1) / n
32-
ni: (one_int.lshift(b).mod_inv(n).lshift(b) - one_int) / n
33-
rr: one_int.lshift(b * 2) % n
32+
ni: (one_int.left_shift(b).mod_inv(n).left_shift(b) - one_int) / n
33+
rr: one_int.left_shift(b * 2) % n
3434
}
3535
}
3636

@@ -164,7 +164,7 @@ fn (a Integer) mont_even(x Integer, m Integer) Integer {
164164
}
165165

166166
m1, j := m.rsh_to_set_bit()
167-
m2 := one_int.lshift(j)
167+
m2 := one_int.left_shift(j)
168168

169169
$if debug {
170170
assert m1 * m2 == m
@@ -321,7 +321,7 @@ fn (a Integer) to_mont(ctx MontgomeryContext) Integer {
321321
fn (a Integer) from_mont(ctx MontgomeryContext) Integer {
322322
log2n := u32(ctx.n.bit_len())
323323

324-
r := (a + ((a.mask_bits(log2n) * ctx.ni).mask_bits(log2n) * ctx.n)).rshift(log2n)
324+
r := (a + ((a.mask_bits(log2n) * ctx.ni).mask_bits(log2n) * ctx.n)).right_shift(log2n)
325325

326326
return if r.abs_cmp(ctx.n) >= 0 {
327327
r - ctx.n

0 commit comments

Comments
 (0)