~/bend-docscommunity

big.bend checks

raw source on the hub · import element-math@0.1.0.0/big.bend as Big

1 import
import Base

Types

type Mod source · line 138 · raw

Data

Definitions

def zeros source · line 13 · raw

@n:Nat -> List<&2, U32>

def or0 source · line 20 · raw

@m:Maybe<&2, U32> -> U32

def nth source · line 27 · raw

@xs:List<&2, U32> -> @n:Nat -> U32

def tail source · line 30 · raw

@xs:List<&2, U32> -> List<&2, U32>

def fit source · line 38 · raw

@k:Nat -> @xs:List<&2, U32> -> List<&2, U32>

Exactly k limbs: truncated, or padded with zeros on the high side.

def pairs source · line 52 · raw

@bs:List<&2, U32> -> List<&2, U32>

Little-endian bytes to limbs.

def from_bytes source · line 64 · raw

@bs:List<&2, U32> -> List<&2, U32>

Big-endian bytes (the form in certificates and signatures) to a number.

def unpair source · line 67 · raw

@xs:List<&2, U32> -> List<&2, U32>

def to_bytes source · line 75 · raw

@x:List<&2, U32> -> @n:Nat -> List<&2, U32>

A number as exactly n big-endian bytes.

def byte_bits source · line 78 · raw

@c:Nat -> @+b:U32 -> List<&2, Bool>

def bits_of_bytes source · line 86 · raw

@bs:List<&2, U32> -> List<&2, Bool>

Bits of big-endian bytes, most significant first.

def is_zero source · line 93 · raw

@xs:List<&2, U32> -> Bool

def eq source · line 100 · raw

@xs:List<&2, U32> -> @ys:List<&2, U32> -> Bool

def add_go source · line 112 · raw

@a:List<&2, U32> -> @+b:List<&2, U32> -> @+c:U32 -> List<&2, U32>

a + b over a's length, the final carry appended as one more limb.

def sub_go source · line 121 · raw

@a:List<&2, U32> -> @+b:List<&2, U32> -> @+br:U32 -> List<&2, U32>

a - b over a's length, the final borrow (0 or 1) appended.

def lt source · line 130 · raw

@+a:List<&2, U32> -> @+b:List<&2, U32> -> Bool

The borrow of a - b: a < b.

def pick source · line 133 · raw

@c:Bool -> @a:List<&2, U32> -> @b:List<&2, U32> -> List<&2, U32>

def add_mod source · line 142 · raw

@+k:Nat -> @+n:List<&2, U32> -> @+a:List<&2, U32> -> @+b:List<&2, U32> -> List<&2, U32>

(a + b) mod n, a and b below n.

def sub_mod source · line 148 · raw

@+k:Nat -> @+n:List<&2, U32> -> @+a:List<&2, U32> -> @+b:List<&2, U32> -> List<&2, U32>

(a - b) mod n, a and b below n.

def doublings source · line 153 · raw

@c:Nat -> @+k:Nat -> @+n:List<&2, U32> -> @+x:List<&2, U32> -> List<&2, U32>

2^c x mod n, doubling c times.

def newton source · line 163 · raw

@+n0:U32 -> @+x:U32 -> U32

-n0^-1 mod 2^16 by Newton's iteration; x = n0 is right to 3 bits for odd n0, and each step doubles the correct bits (U32 products wrap mod 2^32, which keeps the low 16 bits exact).

def ninv_of source · line 166 · raw

@+n0:U32 -> U32

def r_mod source · line 171 · raw

@top:Bool -> @+k:Nat -> @+n:List<&2, U32> -> List<&2, U32>

R mod n: when n's top bit is set, R - n (that is, -n mod R) is below n; otherwise 1 doubled 16k times.

def twos source · line 179 · raw

@c:Nat -> @+x:Nat -> Nat

The number of times 2 divides x, up to c.

def row source · line 190 · raw

@t:List<&2, U32> -> @bs:List<&2, U32> -> @ns:List<&2, U32> -> @+a:U32 -> @+m:U32 -> @+c1:U32 -> @+c2:U32 -> List<&2, U32>

One CIOS row, fused: t + a_i b + m n in a single pass with two carries (each partial sum stays below 2^32), where m = (t_0 + a_i b_0) ninv mod 2^16 makes the low limb zero. t has k + 1 limbs; the row yields k + 2, the first of them zero.

def mont_go source · line 205 · raw

@a:List<&2, U32> -> @+b:List<&2, U32> -> @+n:List<&2, U32> -> @+ninv:U32 -> @+t:List<&2, U32> -> List<&2, U32>

def mmul source · line 214 · raw

@m:Mod -> @+a:List<&2, U32> -> @+b:List<&2, U32> -> List<&2, U32>

a b R^-1 mod n.

def squarings source · line 222 · raw

@c:Nat -> @+m:Mod -> @+x:List<&2, U32> -> List<&2, U32>

Montgomery squaring doubles the exponent: x = 2^e R becomes 2^(2e) R.

def k_of source · line 229 · raw

@m:Mod -> Nat

def n_of source · line 234 · raw

@m:Mod -> List<&2, U32>

def r2_of_mod source · line 241 · raw

@+m:Mod -> List<&2, U32>

R^2 mod n = 2^(16k) R: with 16k = 2^v s (s odd), double R mod n s times to get 2^s R, then square v times.

def with_r2 source · line 248 · raw

@m:Mod -> Mod

def modulus source · line 254 · raw

@+n:List<&2, U32> -> Mod

A modulus from its limbs (odd, above 1, top limb nonzero).

def modulus_of_bytes source · line 257 · raw

@+bs:List<&2, U32> -> Mod

def r2_of source · line 260 · raw

@m:Mod -> List<&2, U32>

def one source · line 265 · raw

@+m:Mod -> List<&2, U32>

def to_mont source · line 269 · raw

@+m:Mod -> @+x:List<&2, U32> -> List<&2, U32>

Into and out of the Montgomery form x R mod n.

def from_mont source · line 272 · raw

@+m:Mod -> @+x:List<&2, U32> -> List<&2, U32>

def mul source · line 276 · raw

@+m:Mod -> @+a:List<&2, U32> -> @+b:List<&2, U32> -> List<&2, U32>

a b mod n for plain (not Montgomery) a, b below n.

def add source · line 279 · raw

@+m:Mod -> @+a:List<&2, U32> -> @+b:List<&2, U32> -> List<&2, U32>

def sub source · line 282 · raw

@+m:Mod -> @+a:List<&2, U32> -> @+b:List<&2, U32> -> List<&2, U32>

def pick_mul source · line 285 · raw

@b:Bool -> @+m:Mod -> @+acc:List<&2, U32> -> @+x:List<&2, U32> -> List<&2, U32>

def exp_go source · line 293 · raw

@bits:List<&2, Bool> -> @+m:Mod -> @+x:List<&2, U32> -> @+acc:List<&2, U32> -> List<&2, U32>

Square and multiply in Montgomery form, exponent bits most significant first.

def pow source · line 301 · raw

@+m:Mod -> @+x:List<&2, U32> -> @+e:List<&2, U32> -> List<&2, U32>

x^e mod n, x plain and below n, e as big-endian bytes.

def inv source · line 305 · raw

@+m:Mod -> @+x:List<&2, U32> -> List<&2, U32>

x^-1 mod a prime p, as x^(p-2) (Fermat); 0 for 0.