Let $b=(X^TX)^{-1}X^Ty$ be the ordinary least squares estimate. Then the sum of squared residuals is
$$\begin{align} (y-Xb)^T(y-Xb)&=y^Ty-y^TXb-b^TX^Ty+b^TX^TXb\\ &=y^Ty-b^TX^Ty. \end{align}$$
The carbohydrate data below is from the book An Introduction to Generalized Linear Models (Fourth Edition)

carbohydrate <- structure(list(carbohydrate = c(33, 40, 37, 27, 30, 43, 34, 48,
30, 38, 50, 51, 30, 36, 41, 42, 46, 24, 35, 37), age = c(33,
47, 49, 35, 46, 52, 62, 23, 32, 42, 31, 61, 63, 40, 50, 64, 56,
61, 48, 28), weight = c(100, 92, 135, 144, 140, 101, 95, 101,
98, 105, 108, 85, 130, 127, 109, 107, 117, 100, 118, 102), protein = c(14,
15, 18, 12, 15, 15, 14, 17, 15, 14, 17, 19, 19, 20, 15, 16, 18,
13, 18, 14)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-20L))
N=dim(carbohydrate)[1]
X <- carbohydrate
X[,1] <- rep(1, N)
y <- as.matrix(carbohydrate$carbohydrate)
#b=(X^TX)^{-1}X^Ty
X <- as.matrix(X)
XTX <- t(X) %% X
XTy <- t(X) %% y
XTX_inv <- matlib::inv(XTX)
b=XTX_inv %*% XTy
#Residual S
t(y - X %% b) %% (y - X %% b)
#Residual S
t(y) %% y - 2(t(y) %% X %% b) + t(b) %% XTX %% b
#Residual S
t(y) %% y - t(b) %*% XTy
[,1]
[1,] 567.6806
[,1]
[1,] 567.6806
[,1]
[1,] 545.8
Why is $(y-Xb)^T(y-Xb)\ne y^Ty-b^TX^Ty$?
It seems the manually calculated coefficients are not correct, the coefficients from the lm function can match:
res.lm=lm(carbohydrate~age+weight+protein,data=carbohydrate)
b2 <- res.lm$coefficients
#Residual S
t(y - X %*% b2) %*% (y - X %*% b2)
#Residual S
t(y) %*% y - 2*(t(y) %*% X %*% b2) + t(b2) %*% XTX %*% b2
#Residual
t(y) %*% y - t(b2) %*% XTy
[,1]
[1,] 567.6629
[,1]
[1,] 567.6629
[,1]
[1,] 567.6629