|
| 1 | +// Examples of different SHA-esque operations being performed using sparse form |
| 2 | +// and lookup arguments |
| 3 | + |
| 4 | + |
| 5 | +// python -c "b=4;dtos=lambda d: sum(4**i*int(b) for i, b in enumerate(bin(d)[2:][::-1]));print(f'const transcript field[{2**b}] D_TO_S_{b} = [', ', '.join(str(dtos(i)) for i in range(2**b)), ']', sep='')" |
| 6 | +const transcript field[16] D_TO_S_4 = [0, 1, 4, 5, 16, 17, 20, 21, 64, 65, 68, 69, 80, 81, 84, 85] |
| 7 | + |
| 8 | +const transcript field[8] D_TO_S_3 = [0, 1, 4, 5, 16, 17, 20, 21] |
| 9 | + |
| 10 | +const transcript field[8] D_3 = [0, 1, 2, 3, 4, 5, 6, 7] |
| 11 | + |
| 12 | +// python -c "b=4;dtos=lambda d: sum(4**i*int(b) for i, b in enumerate(bin(d)[2:][::-1]));print(f'const field S_ONES_{b} = {dtos(2**b-1)}');print(f'const field D_ONES_{b} = {2**b-1}')" |
| 13 | +const field S_ONES_4 = 85 |
| 14 | +const field D_ONES_4 = 15 |
| 15 | + |
| 16 | +from "EMBED" import unpack, value_in_array, reverse_lookup, fits_in_bits |
| 17 | + |
| 18 | +// split a number into (unchecked) high and low bits |
| 19 | +def unsafe_split<LOW_BITS,HIGH_BITS>(field x) -> field[2]: |
| 20 | + bool[LOW_BITS+HIGH_BITS] bits = unpack(x) |
| 21 | + field low = 0 |
| 22 | + field high = 0 |
| 23 | + for u32 i in 0..LOW_BITS do |
| 24 | + low = low + 2 ** i * (if bits[LOW_BITS+HIGH_BITS-1-i] then 1 else 0 fi) |
| 25 | + endfor |
| 26 | + for u32 i in LOW_BITS..HIGH_BITS do |
| 27 | + high = high + 2 ** i * (if bits[LOW_BITS+HIGH_BITS-1-i] then 1 else 0 fi) |
| 28 | + endfor |
| 29 | + return [low, high] |
| 30 | + |
| 31 | +// split a 2N bit number into (unchecked) even and odd bits (in sparse form) |
| 32 | +def unsafe_separate_sparse<N>(field x) -> field[2]: |
| 33 | + bool[2*N] bits = unpack(x) |
| 34 | + field even = 0 |
| 35 | + field odd = 0 |
| 36 | + for u32 i in 0..N do |
| 37 | + even = even + 4 ** i * (if bits[2*N-1-(2*i)] then 1 else 0 fi) |
| 38 | + odd = odd + 4 ** i * (if bits[2*N-1-(2*i+1)] then 1 else 0 fi) |
| 39 | + endfor |
| 40 | + return [even, odd] |
| 41 | + |
| 42 | +struct Dual { |
| 43 | + field s |
| 44 | + field d |
| 45 | +} |
| 46 | + |
| 47 | +// convert a dense 8-bit value to dual form; ensures the value fits in 8 bits. |
| 48 | +def dense_to_dual_4(field x) -> Dual: |
| 49 | + field s = D_TO_S_4[x] |
| 50 | + return Dual {s: s, d: x} |
| 51 | + |
| 52 | +// get the even bits of a 16-bit value in dual form; ensures the value fits in 16 bits. |
| 53 | +def split_even_dual_4(field x) -> Dual: |
| 54 | + unsafe witness field[2] split = unsafe_separate_sparse::<8>(x) |
| 55 | + field even = split[0] |
| 56 | + field odd = split[1] |
| 57 | + assert(x == 2 * odd + even) |
| 58 | + field even_d = reverse_lookup(D_TO_S_4, even) |
| 59 | + assert(value_in_array(odd, D_TO_S_4)) |
| 60 | + return Dual { s: even, d: even_d } |
| 61 | + |
| 62 | +// get the odd bits of a 16-bit value in dual form; ensures the value fits in 16 bits. |
| 63 | +def split_odd_dual_4(field x) -> Dual: |
| 64 | + unsafe witness field[2] split = unsafe_separate_sparse::<8>(x) |
| 65 | + // field even = split[0] |
| 66 | + field odd = split[1] |
| 67 | + field even = x - 2 * odd |
| 68 | + // assert(x == 2 * odd + even) |
| 69 | + field odd_d = reverse_lookup(D_TO_S_4, odd) |
| 70 | + assert(value_in_array(even, D_TO_S_4)) |
| 71 | + return Dual { s: odd, d: odd_d } |
| 72 | + |
| 73 | +// get the even and odd bits of a 16-bit value in dual form; ensures the value fits in 16 bits. |
| 74 | +def split_both_dual_4(field x) -> Dual[2]: |
| 75 | + unsafe witness field[2] split = unsafe_separate_sparse::<8>(x) |
| 76 | + field even = split[0] |
| 77 | + field odd = split[1] |
| 78 | + field odd_d = reverse_lookup(D_TO_S_4, odd) |
| 79 | + field even_d = reverse_lookup(D_TO_S_4, even) |
| 80 | + return [Dual { s: even, d: even_d }, Dual { s: odd, d: odd_d }] |
| 81 | + |
| 82 | +// expected cost: 3 observed: 5 |
| 83 | +def and_4(Dual x, Dual y) -> Dual: |
| 84 | + return split_odd_dual_4(x.s + y.s) |
| 85 | + |
| 86 | +def maj_4(Dual x, Dual y, Dual z) -> Dual: |
| 87 | + return split_odd_dual_4(x.s + y.s + z.s) |
| 88 | + |
| 89 | +def xor_4(Dual x, Dual y, Dual z) -> Dual: |
| 90 | + return split_even_dual_4(x.s + y.s + z.s) |
| 91 | + |
| 92 | +def not_4(Dual x) -> Dual: |
| 93 | + return Dual { s: S_ONES_4 - x.s, d: D_ONES_4 - x.d } |
| 94 | + |
| 95 | +def or_4(Dual x, Dual y) -> Dual: |
| 96 | + return not_4(and_4(not_4(x), not_4(y))) |
| 97 | + |
| 98 | +// split s into 8 low bits and 3 high bits, and return the low bits |
| 99 | +// in dual form. |
| 100 | +def normalize_sum_4(field s) -> Dual: |
| 101 | + unsafe witness field[2] split = unsafe_split::<8, 3>(s) |
| 102 | + field low = split[0] |
| 103 | + field high = split[1] |
| 104 | + assert(value_in_array(high, D_3)) |
| 105 | + return dense_to_dual_4(low) |
| 106 | + |
| 107 | +// table costs: |
| 108 | +// 16 + 16 + 8 = 40 |
| 109 | +//do a bitwise AND. |
| 110 | +def main(private field dense_x, private field dense_y) -> field: |
| 111 | + Dual z = dense_to_dual_4(0) |
| 112 | + Dual x = dense_to_dual_4(dense_x) // 1010 (10) |
| 113 | + Dual y = dense_to_dual_4(dense_y) // 1001 (9) |
| 114 | + Dual a = and_4(x, y) // 1000 (8) |
| 115 | + for field i in 0..10 do |
| 116 | + a = and_4(a, y) // idempotent |
| 117 | + endfor |
| 118 | + Dual b = or_4(x, y) // 1011 (11) |
| 119 | + Dual s = normalize_sum_4(b.d + a.d) // 0011 (3) |
| 120 | + return s.d |
| 121 | + // return reverse_lookup(D_TO_S_4, dense_x) * dense_y |
| 122 | + // return reverse_lookup(D_TO_S_4, dense_x) * reverse_lookup(D_TO_S_4, dense_y) |
| 123 | + // return dense_x * dense_y |
| 124 | + |
| 125 | + |
| 126 | + |
| 127 | + |
0 commit comments