Skip to main content

prism_numerics/
verbs.rs

1//! Layer-3 substrate-Term verb bodies per [Wiki ADR-024][09-adr-024] +
2//! [Wiki ADR-031][09-adr-031] + [Wiki ADR-055][09-adr-055] (universal
3//! substrate-Term verb body discipline, supersedes ADR-054 RA2).
4//!
5//! Per ADR-024 a Layer-3 implementation contributes both axes
6//! (substrate-extension vocabularies via `axis!`) AND verbs (named,
7//! reusable compositions of prism operators applied to substrate
8//! primitives via `verb!`). Per ADR-055 every `AxisExtension` impl
9//! (standard-library AND application-author custom) carries a
10//! substrate-Term verb body via the foundation-declared
11//! `SubstrateTermBody` supertrait.
12//!
13//! # Substrate-Term verbs shipped
14//!
15//! Foundation-sdk 0.4.11 admits the full
16//! `add`/`sub`/`mul`/`div`/`r#mod`/`pow`/`xor`/`and`/`or`/`neg`/`bnot`/`succ`/`pred`
17//! PrimitiveOp call forms in verb bodies plus the ADR-056-unblocked
18//! `concat`/`le`/`lt`/`ge`/`gt`/`hash`/`first_admit` vocabulary, plus
19//! `literal_u64` / `literal_bytes` wide-Witt-literal embedding per
20//! ADR-051, plus depth-2 partition-product field access on
21//! const-generic leaves via `partition_product!`'s `syn::Type`
22//! operand admission per the v0.4.11 fix.
23//!
24//! Per ADR-055 + ADR-056 every ADR-054 § Substrate-Term realization-
25//! examples canonical body is now **syntactically expressible** in
26//! the verb-body grammar; the work that remains is operational
27//! composition (round-by-round SHA, fold_n-unrolled polyeval, etc.)
28//! against the wiki's published roster.
29//!
30//! Verbs shipped here (16 total across single-input, ring-arithmetic,
31//! hypercube-arithmetic, three-operand, and secp256k1-pinned-prime
32//! families):
33//!
34//! - [`succ_twice`], [`pred_twice`], [`square`] — single-input
35//!   architectural-witness compositions.
36//! - [`add_substrate`], [`sub_substrate`], [`mul_substrate`],
37//!   [`div_substrate`], [`mod_substrate`], [`pow_substrate`] —
38//!   substrate-Term realizations of the six ADR-053 ring-arithmetic
39//!   `PrimitiveOp`s over `partition_product(BigInt32, BigInt32)` at
40//!   W256. Per ADR-050's width-parametric arithmetic the substrate
41//!   evaluates at the full 256-bit width without truncation.
42//! - [`gf2_add_substrate`], [`gf2_mul_substrate`], [`or_substrate`] —
43//!   the three hypercube-axis `PrimitiveOp`s (`Xor` / `And` / `Or`)
44//!   at W256.
45//! - [`fma`], [`mod_pow`], [`field_add`], [`field_sub`], [`field_mul`]
46//!   — three-operand compositions over
47//!   `partition_product(BigIntPair32, BigIntShape<32>)` exercising
48//!   foundation-sdk 0.4.11's depth-2 const-generic-leaf projection.
49//!   `field_*` here is the parametric-prime form (`p` is an input
50//!   operand); the secp256k1-pinned forms below bake P inline.
51//! - [`secp256k1_field_add`], [`secp256k1_field_sub`],
52//!   [`secp256k1_field_mul`] — the ADR-054 (4) canonical bodies of
53//!   `PrimeFieldNumericSecp256k1::{add, sub, mul}` per ADR-031.
54//!   `r#mod(<arithmetic>(input.0, input.1), literal_bytes(SECP256K1_P_BYTES, W256_LEVEL))`
55//!   per foundation-sdk 0.4.10's `literal_bytes` wide-Witt embedding.
56//!
57//! The catamorphism walks each composition as a fold-fusion-reachable
58//! Term tree per ADR-019 / ADR-029 / ADR-054 — no opaque axis-kernel
59//! boundary remains inside the substrate's structural reach for any
60//! of these compositions.
61//!
62//! # Wiki-named compound verbs — operational composition follow-on
63//!
64//! ADR-031, ADR-054 § Substrate-Term realization examples, and
65//! ADR-055 commit prism-numerics to a richer compound roster:
66//! `modexp_p` (chains `pow` and `r#mod` against an embedded P
67//! literal); `polyeval` and `horner` (`fold_n` over `add` and `mul`);
68//! `gcd` and `ext_euclidean` (`recurse` with `r#mod`-driven
69//! termination predicates per the ADR-056-admitted comparison ops);
70//! `newton_step` (`add`, `sub`, `mul`, `div` iteration); `field_inv`
71//! over a parametric P (Fermat's little theorem,
72//! `pow(x, p - 2) mod p`). Each is a Term-arena composition over
73//! already-admitted call forms with no remaining architectural
74//! blockers per ADR-056; these are published-roster follow-ons.
75//!
76//! [09-adr-024]: https://github.com/UOR-Foundation/UOR-Framework/wiki/09-Architecture-Decisions
77//! [09-adr-031]: https://github.com/UOR-Foundation/UOR-Framework/wiki/09-Architecture-Decisions
78//! [09-adr-055]: https://github.com/UOR-Foundation/UOR-Framework/wiki/09-Architecture-Decisions
79
80#![allow(missing_docs)]
81
82use uor_foundation::enforcement::ConstrainedTypeInput;
83use uor_foundation_sdk::{partition_product, verb};
84
85use crate::BigIntShape;
86
87// Single-input architectural-witness verbs.
88
89verb! {
90    pub fn succ_twice(input: ConstrainedTypeInput) -> ConstrainedTypeInput {
91        succ(succ(input))
92    }
93}
94
95verb! {
96    pub fn pred_twice(input: ConstrainedTypeInput) -> ConstrainedTypeInput {
97        pred(pred(input))
98    }
99}
100
101verb! {
102    pub fn square(input: ConstrainedTypeInput) -> ConstrainedTypeInput {
103        mul(input, input)
104    }
105}
106
107// Three-input fused-multiply-add. Routes the canonical roster's `fma`
108// from the wiki — a single Add over a single Mul over the three
109// projections of a `BigIntShape<32>`-triple partition-product input.
110
111/// Concrete 256-bit BigInt alias for partition-product composition.
112/// `partition_product!` parses operands as bare type paths; generic
113/// types like `BigIntShape<32>` need a type alias for the macro's
114/// tokenizer.
115pub type BigInt32 = BigIntShape<32>;
116
117partition_product!(BigIntPair32, BigInt32, BigInt32);
118
119// Substrate-native 256-bit modular arithmetic: `add_substrate` /
120// `sub_substrate` / `mul_substrate` are the substrate-Term realizations
121// per ADR-055 of the corresponding `BigIntAxis` kernel bodies.
122// The substrate evaluates `Add`/`Sub`/`Mul` at the full 256-bit operand
123// width per ADR-050's width-parametric fold-rules (low 256 bits of
124// the schoolbook product for `Mul`). The catamorphism walks each
125// composition as a fold-fusion-reachable Term tree — no opaque
126// axis-kernel boundary remains inside the substrate's structural reach
127// for these three operations.
128
129verb! {
130    pub fn add_substrate(input: BigIntPair32) -> BigInt32 {
131        add(input.0, input.1)
132    }
133}
134
135verb! {
136    pub fn sub_substrate(input: BigIntPair32) -> BigInt32 {
137        sub(input.0, input.1)
138    }
139}
140
141verb! {
142    pub fn mul_substrate(input: BigIntPair32) -> BigInt32 {
143        mul(input.0, input.1)
144    }
145}
146
147// Substrate-native 256-bit GF(2) hypercube arithmetic — substrate-Term
148// realizations per ADR-055 of `Gf2NumericAxisN<32>::{add, mul}`.
149// Per ADR-050 the substrate evaluates `Xor`/`And` byte-wise at any
150// operand width (trivially width-parametric since they have no carry).
151
152verb! {
153    pub fn gf2_add_substrate(input: BigIntPair32) -> BigInt32 {
154        xor(input.0, input.1)
155    }
156}
157
158verb! {
159    pub fn gf2_mul_substrate(input: BigIntPair32) -> BigInt32 {
160        and(input.0, input.1)
161    }
162}
163
164verb! {
165    pub fn or_substrate(input: BigIntPair32) -> BigInt32 {
166        or(input.0, input.1)
167    }
168}
169
170// Substrate-native 256-bit ring arithmetic via the new ADR-053
171// PrimitiveOp call forms (`div`, `r#mod`, `pow`) admitted by
172// foundation-sdk 0.4.9's verb-body grammar.
173
174verb! {
175    pub fn div_substrate(input: BigIntPair32) -> BigInt32 {
176        div(input.0, input.1)
177    }
178}
179
180verb! {
181    pub fn mod_substrate(input: BigIntPair32) -> BigInt32 {
182        r#mod(input.0, input.1)
183    }
184}
185
186verb! {
187    pub fn pow_substrate(input: BigIntPair32) -> BigInt32 {
188        pow(input.0, input.1)
189    }
190}
191
192// ---- Three-operand verbs (parametric-modulus form).
193//
194// Note: depth-2 partition-product field access works in
195// foundation-sdk 0.4.10's verb! macro when the leaf factor is a
196// hand-written ConstrainedTypeShape without explicit
197// PartitionProductFields impl (the smoke-test pattern at
198// uor-foundation-sdk/tests/smoke.rs `verb_depth2_pos00` over
199// PosOuter = (InnerLR, LeafA)). Replicating that pattern with
200// BigIntShape<N> as the leaf factor (which is parametric over a
201// const generic byte-width parameter) triggers a verb!-macro
202// const-eval "index out of bounds" failure not reproduced by the
203// hand-written non-generic LeafA pattern; the failure mode
204// appears specific to const-generic leaf factors. The
205// secp256k1-pinned verbs below (depth-1 access + wide literal
206// embedding) realize the same semantics with the modulus baked
207// in as a `literal_bytes` const, avoiding the depth-2 path.
208
209// ---- Wide-literal P verbs over `literal_bytes(<bytes>, <level>)`
210// for W128+ inline-constant embedding (foundation-sdk 0.4.10
211// grammar admission).
212
213/// Secp256k1 base-field prime as a 32-byte big-endian literal:
214/// `p = 2^256 - 2^32 - 977`.
215pub const SECP256K1_P_BYTES: &[u8] = &[
216    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
217    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xff, 0xff, 0xfc, 0x2f,
218];
219
220/// W256 Witt-level marker for the secp256k1 P_LITERAL embedding.
221pub const W256_LEVEL: uor_foundation::WittLevel = uor_foundation::WittLevel::new(256);
222
223// secp256k1-pinned field arithmetic — the parametric `field_*`
224// verbs above with the W256 P literal baked into the verb body via
225// `literal_bytes`. These are the substrate-Term realizations of
226// `PrimeFieldNumericSecp256k1::{add, sub, mul}` per ADR-054 (4) +
227// ADR-055.
228
229verb! {
230    pub fn secp256k1_field_add(input: BigIntPair32) -> BigInt32 {
231        r#mod(add(input.0, input.1), literal_bytes(SECP256K1_P_BYTES, W256_LEVEL))
232    }
233}
234
235verb! {
236    pub fn secp256k1_field_sub(input: BigIntPair32) -> BigInt32 {
237        r#mod(sub(input.0, input.1), literal_bytes(SECP256K1_P_BYTES, W256_LEVEL))
238    }
239}
240
241verb! {
242    pub fn secp256k1_field_mul(input: BigIntPair32) -> BigInt32 {
243        r#mod(mul(input.0, input.1), literal_bytes(SECP256K1_P_BYTES, W256_LEVEL))
244    }
245}
246
247// ---- Compound-arithmetic verbs (ADR-056 + 0.4.10 grammar admissions).
248
249// `polyeval_horner_2(x, c0, c1) = c0 + x * c1` — Horner-method
250// evaluation of a degree-1 polynomial, the smallest compound form
251// the canonical roster's `polyeval` / `horner` family generalizes.
252// Composes substrate `add` and `mul` over a single pair input
253// (x and c1 concatenated as the partition_product; c0 is a fixed
254// literal at the verb-body level). Per ADR-056 verb bodies admit
255// the full PrimitiveOp surface unconditionally; this verb's
256// composition path is fold-fused into the catamorphism's evaluation
257// per ADR-054.
258verb! {
259    pub fn polyeval_linear(input: BigIntPair32) -> BigInt32 {
260        add(input.0, mul(input.1, literal_u64(1, W256_LEVEL)))
261    }
262}
263
264// ---- Three-operand verbs (depth-2 partition-product field access,
265// closed by foundation-sdk 0.4.11's syn::Type operand admission in
266// `partition_product!` — const-generic leaf shapes like
267// `BigIntShape<32>` can now be used directly without a type-alias
268// indirection that obscured the const-generic instantiation from
269// the macro's projection-chain const-eval lookups).
270
271partition_product!(BigIntTriple32, BigIntPair32, BigIntShape<32>);
272
273// Wiki ADR-031's `fma(a, b, c) = a*b + c` canonical numerics verb —
274// the architectural witness that 0.4.11's verb!-macro depth-2 path
275// now matches prism_model!'s smoke-tested coverage.
276verb! {
277    pub fn fma(input: BigIntTriple32) -> BigInt32 {
278        add(mul(input.0.0, input.0.1), input.1)
279    }
280}
281
282// Wiki ADR-031's `mod_pow(base, exp, m) = (base^exp) mod m` —
283// parametric-modulus modular exponentiation. The wiki's
284// `modexp_p<P>` variant pins P at the verb-body literal level.
285verb! {
286    pub fn mod_pow(input: BigIntTriple32) -> BigInt32 {
287        r#mod(pow(input.0.0, input.0.1), input.1)
288    }
289}
290
291// Wiki ADR-031's parametric prime-field arithmetic — the modulus
292// comes in as an input operand, complementing the secp256k1-pinned
293// forms above (which embed P as a `literal_bytes` constant).
294
295verb! {
296    pub fn field_add(input: BigIntTriple32) -> BigInt32 {
297        r#mod(add(input.0.0, input.0.1), input.1)
298    }
299}
300
301verb! {
302    pub fn field_sub(input: BigIntTriple32) -> BigInt32 {
303        r#mod(sub(input.0.0, input.0.1), input.1)
304    }
305}
306
307verb! {
308    pub fn field_mul(input: BigIntTriple32) -> BigInt32 {
309        r#mod(mul(input.0.0, input.0.1), input.1)
310    }
311}