forked from QED-it/go-jubjub
-
Notifications
You must be signed in to change notification settings - Fork 2
/
futil.go
41 lines (30 loc) · 944 Bytes
/
futil.go
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
package futil
// Adc Computes a + b + carry, returning the result and the new carry over.
func Adc(a, b, carry uint64) (uint64, uint64) {
res := FromU64(a)
res = res.Add(b)
res = res.Add(carry)
return res.L, res.H
}
// Sbb Computes a - (b + borrow), returning the result and the new borrow.
func Sbb(a, b, borrow uint64) (uint64, uint64) {
a128 := FromU64(a)
b128 := FromU64(b)
borr128 := FromU64(borrow >> 63)
bBor := b128.AddU128(borr128)
res := a128.SubU128(bBor)
return res.L, res.H
}
// Mac Computes a + (b * c) + carry, returning the result and the new carry over.
func Mac(a, b, c, carry uint64) (uint64, uint64) {
res := FromU64(b)
res = res.MulU64(c)
res = res.Add(a)
res = res.Add(carry)
return res.L, res.H
}
// Load4 interprets a 4-byte unsigned little endian byte-slice as uint64
func Load4(b []byte) uint64 {
return (uint64(b[0]) | (uint64(b[1]) << 8) |
(uint64(b[2]) << 16) | (uint64(b[3]) << 24))
}