-
Notifications
You must be signed in to change notification settings - Fork 145
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Expose some types and gadgets #1860
Changes from all commits
ea19655
fdcb8b6
80fcc08
2635545
a27a6ca
6bcbdf7
9e539b5
90a8822
7b62ad1
0384736
8e3ac5e
ecc4b03
ee52796
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,6 @@ import { Bytes } from '../wrapped-classes.js'; | |
import { chunk } from '../../util/arrays.js'; | ||
import { TupleN } from '../../util/types.js'; | ||
import { divMod32 } from './arithmetic.js'; | ||
import { bytesToWord, wordToBytes } from './bit-slices.js'; | ||
import { bitSlice } from './common.js'; | ||
import { rangeCheck16 } from './range-check.js'; | ||
|
||
|
@@ -70,11 +69,7 @@ function padding(data: FlexibleBytes): UInt32[][] { | |
for (let i = 0; i < paddedMessage.length; i += 4) { | ||
// chunk 4 bytes into one UInt32, as expected by SHA256 | ||
// bytesToWord expects little endian, so we reverse the bytes | ||
chunks.push( | ||
UInt32.Unsafe.fromField( | ||
bytesToWord(paddedMessage.slice(i, i + 4).reverse()) | ||
) | ||
); | ||
chunks.push(UInt32.fromBytesBE(paddedMessage.slice(i, i + 4))); | ||
} | ||
|
||
// split message into 16 element sized message blocks | ||
|
@@ -97,11 +92,11 @@ const SHA256 = { | |
} | ||
|
||
// the working variables H[i] are 32bit, however we want to decompose them into bytes to be more compatible | ||
// wordToBytes expects little endian, so we reverse the bytes | ||
return Bytes.from(H.map((x) => wordToBytes(x.value, 4).reverse()).flat()); | ||
return Bytes.from(H.map((x) => x.toBytesBE()).flat()); | ||
}, | ||
compression: sha256Compression, | ||
createMessageSchedule, | ||
padding, | ||
get initialState() { | ||
return SHA256Constants.H.map((x) => UInt32.from(x)); | ||
}, | ||
|
@@ -239,7 +234,7 @@ function sigma(u: UInt32, bits: TupleN<number, 3>, firstShifted = false) { | |
* | ||
* @returns The updated intermediate hash values after compression. | ||
*/ | ||
function sha256Compression(H: UInt32[], W: UInt32[]) { | ||
function sha256Compression([...H]: UInt32[], W: UInt32[]) { | ||
Comment on lines
-242
to
+237
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this fixes a bug, the compression function mutated its input, which bit me because in dynamic sha2 you don't necessarily want to use the output |
||
// initialize working variables | ||
let a = H[0]; | ||
let b = H[1]; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. exposing the uint32 / bytes converters that we already use internally for sha2 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,6 +45,7 @@ export { | |
InferredProvable, | ||
IsPure, | ||
NestedProvable, | ||
mapValue, | ||
}; | ||
|
||
type ProvableExtension<T, TJson = any> = { | ||
|
@@ -169,3 +170,44 @@ function provableExtends< | |
}, | ||
} satisfies ProvableHashable<S, InferValue<A>>; | ||
} | ||
|
||
function mapValue< | ||
A extends ProvableHashable<any>, | ||
V extends InferValue<A>, | ||
W, | ||
T extends InferProvable<A> | ||
>( | ||
Comment on lines
+174
to
+179
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. method to take one provable and modify its "value" type |
||
provable: A, | ||
there: (x: V) => W, | ||
back: (x: W | T) => V | T | ||
): ProvableHashable<T, W> { | ||
return { | ||
sizeInFields() { | ||
return provable.sizeInFields(); | ||
}, | ||
toFields(value) { | ||
return provable.toFields(value); | ||
}, | ||
toAuxiliary(value) { | ||
return provable.toAuxiliary(value); | ||
}, | ||
fromFields(fields, aux) { | ||
return provable.fromFields(fields, aux); | ||
}, | ||
check(value) { | ||
provable.check(value); | ||
}, | ||
toValue(value) { | ||
return there(provable.toValue(value)); | ||
}, | ||
fromValue(value) { | ||
return provable.fromValue(back(value)); | ||
}, | ||
empty() { | ||
return provable.empty(); | ||
}, | ||
toInput(value) { | ||
return provable.toInput(value); | ||
}, | ||
}; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,3 +19,6 @@ function Bytes(size: number) { | |
Bytes.from = InternalBytes.from; | ||
Bytes.fromHex = InternalBytes.fromHex; | ||
Bytes.fromString = InternalBytes.fromString; | ||
|
||
// expore base class so that we can detect Bytes with `instanceof` | ||
Bytes.Base = InternalBytes; | ||
Comment on lines
+22
to
+24
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should always expose base classes somewhere for type factories, so that users can detect what class they have using |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
some unused imports and an unused method purged here